• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/source/dnode/vnode/src/tq/tqUtil.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tq.h"
17

18
static int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
19
                                 const SMqMetaRsp* pRsp, int32_t vgId);
20
static int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
21
                                      const SMqBatchMetaRsp* pRsp, int32_t vgId);
22

23
int32_t tqInitDataRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
×
24
  int32_t    code = TDB_CODE_SUCCESS;
×
25
  int32_t    lino = 0;
×
26
  tqDebug("%s called", __FUNCTION__ );
×
27
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
×
28

29
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
×
30
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);
×
31

32
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
×
33
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
×
34

35
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
×
36
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
×
37
  
38
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
×
39
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
×
40
  pRsp->withTbName = 0;
×
41
  pRsp->withSchema = 1;
×
42

43
END:
×
44
  if (code != 0){
×
45
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
46
    taosArrayDestroy(pRsp->blockData);
×
47
    taosArrayDestroy(pRsp->blockDataLen);
×
48
    taosArrayDestroy(pRsp->blockSchema);
×
49
  }
50
  return code;
×
51
}
52

53
static int32_t tqInitTaosxRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
×
54
  int32_t    code = TDB_CODE_SUCCESS;
×
55
  int32_t    lino = 0;
×
56
  tqDebug("%s called", __FUNCTION__ );
×
57
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
×
58
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
×
59
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
×
60

61
  pRsp->withTbName = 1;
×
62
  pRsp->withSchema = 1;
×
63
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
×
64
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);\
×
65

66
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
×
67
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
×
68

69
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
×
70
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
×
71

72
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
×
73
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
×
74

75
END:
×
76
  if (code != 0){
×
77
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
78
    taosArrayDestroy(pRsp->blockData);
×
79
    taosArrayDestroy(pRsp->blockDataLen);
×
80
    taosArrayDestroy(pRsp->blockTbName);
×
81
    taosArrayDestroy(pRsp->blockSchema);
×
82
  }
83
  return code;
×
84
}
85

86
static int32_t extractResetOffsetVal(STqOffsetVal* pOffsetVal, STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
87
                                     SRpcMsg* pMsg, bool* pBlockReturned) {
88
  if (pOffsetVal == NULL || pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL || pBlockReturned == NULL) {
×
89
    return TSDB_CODE_INVALID_PARA;
×
90
  }
91
  uint64_t   consumerId = pRequest->consumerId;
×
92
  STqOffset* pOffset = NULL;
×
93
  int32_t    code = tqMetaGetOffset(pTq, pRequest->subKey, &pOffset);
×
94
  int32_t    vgId = TD_VID(pTq->pVnode);
×
95

96
  *pBlockReturned = false;
×
97
  // In this vnode, data has been polled by consumer for this topic, so let's continue from the last offset value.
98
  if (code == 0) {
×
99
    tOffsetCopy(pOffsetVal, &pOffset->val);
×
100

101
    char formatBuf[TSDB_OFFSET_LEN] = {0};
×
102
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
×
103
    tqDebug("tmq poll: consumer:0x%" PRIx64
×
104
                ", subkey %s, vgId:%d, existed offset found, offset reset to %s and continue.QID:0x%" PRIx64,
105
            consumerId, pHandle->subKey, vgId, formatBuf, pRequest->reqId);
106
    return 0;
×
107
  } else {
108
    // no poll occurs in this vnode for this topic, let's seek to the right offset value.
109
    if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
×
110
      if (pRequest->useSnapshot) {
×
111
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
×
112
                consumerId, pHandle->subKey, vgId);
113
        if (pHandle->fetchMeta) {
×
114
          tqOffsetResetToMeta(pOffsetVal, 0);
115
        } else {
116
          SValue val = {0};
×
117
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
118
        }
119
      } else {
120
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
×
121
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
×
122
      }
123
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
124
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
×
125
      SMqDataRsp dataRsp = {0};
×
126
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
×
127

128
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
×
129
      if (code != 0) {
×
130
        return code;
×
131
      }
132
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
×
133
              pHandle->subKey, vgId, dataRsp.rspOffset.version);
134
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, (pRequest->rawData == 1) ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
135
      tDeleteMqDataRsp(&dataRsp);
×
136

137
      *pBlockReturned = true;
×
138
      return code;
×
139
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_NONE) {
×
140
      tqError("tmq poll: subkey:%s, no offset committed for consumer:0x%" PRIx64
×
141
                  " in vg %d, subkey %s, reset none failed",
142
              pHandle->subKey, consumerId, vgId, pRequest->subKey);
143
      return TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
×
144
    }
145
  }
146

147
  return 0;
×
148
}
149

150
static int32_t extractDataAndRspForNormalSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
151
                                                   SRpcMsg* pMsg, STqOffsetVal* pOffset) {
152
  int32_t    code = TDB_CODE_SUCCESS;
×
153
  int32_t    lino = 0;
×
154
  tqDebug("%s called", __FUNCTION__ );
×
155
  uint64_t consumerId = pRequest->consumerId;
×
156
  int32_t  vgId = TD_VID(pTq->pVnode);
×
157
  terrno = 0;
×
158

159
  SMqDataRsp dataRsp = {0};
×
160
  code = tqInitDataRsp(&dataRsp, *pOffset);
×
161
  TSDB_CHECK_CODE(code, lino, end);
×
162

163
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
×
164
  TSDB_CHECK_CODE(code, lino, end);
×
165

166
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
×
167
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
×
168
    goto end;
×
169
  }
170

171
  if (terrno == TSDB_CODE_TMQ_FETCH_TIMEOUT && dataRsp.blockNum == 0) {
×
172
    dataRsp.timeout = true;
×
173
  }
174
  //   till now, all data has been transferred to consumer, new data needs to push client once arrived.
175
  if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST && dataRsp.blockNum == 0) {
×
176
    // lock
177
    taosWLockLatch(&pTq->lock);
×
178
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
×
179
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
×
180
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
×
181
      taosWUnLockLatch(&pTq->lock);
×
182
      goto end;
×
183
    }
184
    taosWUnLockLatch(&pTq->lock);
×
185
  }
186

187
  // reqOffset represents the current date offset, may be changed if wal not exists
188
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
×
189
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
190

191
end:
×
192
  {
193
    char buf[TSDB_OFFSET_LEN] = {0};
×
194
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
×
195
    if (code != 0){
×
196
      tqError("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " error msg:%s, line:%d",
×
197
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, tstrerror(code), lino);
198
    } else {
199
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " success",
×
200
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
201
    }
202

203
    tDeleteMqDataRsp(&dataRsp);
×
204
    return code;
×
205
  }
206
}
207

208
#define PROCESS_EXCLUDED_MSG(TYPE, DECODE_FUNC, DELETE_FUNC)                                               \
209
  SDecoder decoder = {0};                                                                                  \
210
  TYPE     req = {0};                                                                                      \
211
  void*    data = POINTER_SHIFT(pHead->body, sizeof(SMsgHead));                                            \
212
  int32_t  len = pHead->bodyLen - sizeof(SMsgHead);                                                        \
213
  tDecoderInit(&decoder, data, len);                                                                       \
214
  if (DECODE_FUNC(&decoder, &req) == 0 && (req.source & TD_REQ_FROM_TAOX) != 0) {                          \
215
    tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, jump meta for, vgId:%d offset %" PRId64 \
216
            " msgType %d",                                                                                 \
217
            pRequest->consumerId, pRequest->epoch, vgId, fetchVer, pHead->msgType);                        \
218
    fetchVer++;                                                                                            \
219
    DELETE_FUNC(&req);                                                                                     \
220
    tDecoderClear(&decoder);                                                                               \
221
    continue;                                                                                              \
222
  }                                                                                                        \
223
  DELETE_FUNC(&req);                                                                                       \
224
  tDecoderClear(&decoder);
225

226
static void tDeleteCommon(void* parm) {}
×
227
static void tDeleteAlterTable(SVAlterTbReq* req) {
×
228
  taosArrayDestroy(req->pMultiTag);
×
229
}
×
230

231
#define POLL_RSP_TYPE(pRequest,taosxRsp) \
232
taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : \
233
(pRequest->rawData == 1 ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP)
234

235
static int32_t buildBatchMeta(SMqBatchMetaRsp *btMetaRsp, int16_t type, int32_t bodyLen, void* body){
×
236
  int32_t         code = 0;
×
237

238
  if (!btMetaRsp->batchMetaReq) {
×
239
    btMetaRsp->batchMetaReq = taosArrayInit(4, POINTER_BYTES);
×
240
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaReq);
×
241
    btMetaRsp->batchMetaLen = taosArrayInit(4, sizeof(int32_t));
×
242
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaLen);
×
243
  }
244

245
  SMqMetaRsp tmpMetaRsp = {0};
×
246
  tmpMetaRsp.resMsgType = type;
×
247
  tmpMetaRsp.metaRspLen = bodyLen;
×
248
  tmpMetaRsp.metaRsp = body;
×
249
  uint32_t len = 0;
×
250
  tEncodeSize(tEncodeMqMetaRsp, &tmpMetaRsp, len, code);
×
251
  if (TSDB_CODE_SUCCESS != code) {
×
252
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
253
    goto END;
×
254
  }
255
  int32_t tLen = sizeof(SMqRspHead) + len;
×
256
  void*   tBuf = taosMemoryCalloc(1, tLen);
×
257
  TQ_NULL_GO_TO_END(tBuf);
×
258
  void*    metaBuff = POINTER_SHIFT(tBuf, sizeof(SMqRspHead));
×
259
  SEncoder encoder = {0};
×
260
  tEncoderInit(&encoder, metaBuff, len);
×
261
  code = tEncodeMqMetaRsp(&encoder, &tmpMetaRsp);
×
262
  tEncoderClear(&encoder);
×
263

264
  if (code < 0) {
×
265
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
266
    goto END;
×
267
  }
268
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaReq, &tBuf));
×
269
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaLen, &tLen));
×
270

271
END:
×
272
  return code;
×
273
}
274

275
static int32_t buildCreateTbBatchReqBinary(SMqDataRsp *taosxRsp, void** pBuf, int32_t *len){
×
276
  int32_t code = 0;
×
277
  SVCreateTbBatchReq pReq = {0};
×
278
  pReq.nReqs = taosArrayGetSize(taosxRsp->createTableReq);
×
279
  pReq.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
×
280
  TQ_NULL_GO_TO_END(pReq.pArray);
×
281
  for (int i = 0; i < taosArrayGetSize(taosxRsp->createTableReq); i++){
×
282
    void   *createTableReq = taosArrayGetP(taosxRsp->createTableReq, i);
×
283
    TQ_NULL_GO_TO_END(taosArrayPush(pReq.pArray, createTableReq));
×
284
  }
285
  tEncodeSize(tEncodeSVCreateTbBatchReq, &pReq, *len, code);
×
286
  if (code < 0) {
×
287
    goto END;
×
288
  }
289
  *len += sizeof(SMsgHead);
×
290
  *pBuf = taosMemoryMalloc(*len);
×
291
  TQ_NULL_GO_TO_END(pBuf);
×
292
  SEncoder coder = {0};
×
293
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *len);
×
294
  code = tEncodeSVCreateTbBatchReq(&coder, &pReq);
×
295
  tEncoderClear(&coder);
×
296

297
END:
×
298
  taosArrayDestroy(pReq.pArray);
×
299
  return code;
×
300
}
301

302
#define SEND_BATCH_META_RSP \
303
tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);\
304
code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);\
305
goto END;
306

307
#define SEND_DATA_RSP \
308
tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);\
309
code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, POLL_RSP_TYPE(pRequest, taosxRsp), vgId);\
310
goto END;
311

312
static int32_t extractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
×
313
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
314
  int32_t         vgId = TD_VID(pTq->pVnode);
×
315
  SMqDataRsp      taosxRsp = {0};
×
316
  SMqBatchMetaRsp btMetaRsp = {0};
×
317
  int32_t         code = 0;
×
318

319
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
×
320
  if (offset->type != TMQ_OFFSET__LOG) {
×
321
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
×
322

323
    if (taosArrayGetSize(btMetaRsp.batchMetaReq) > 0) {
×
324
      code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
×
325
      tqDebug("tmq poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send meta offset type:%d,uid:%" PRId64 ",ts:%" PRId64,
×
326
              pRequest->consumerId, pHandle->subKey, vgId, btMetaRsp.rspOffset.type, btMetaRsp.rspOffset.uid,btMetaRsp.rspOffset.ts);
327
      goto END;
×
328
    }
329

330
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
×
331
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
332
    if (taosxRsp.blockNum > 0) {
×
333
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
334
      goto END;
×
335
    } else {
336
      tOffsetCopy(offset, &taosxRsp.rspOffset);
×
337
    }
338
  }
339

340
  if (offset->type == TMQ_OFFSET__LOG) {
×
341
    walReaderVerifyOffset(pHandle->pWalReader, offset);
×
342
    int64_t fetchVer = offset->version;
×
343

344
    uint64_t st = taosGetTimestampMs();
×
345
    int      totalRows = 0;
×
346
    int32_t  totalMetaRows = 0;
×
347
    while (1) {
×
348
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
×
349
      if (savedEpoch > pRequest->epoch) {
×
350
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
351
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
352
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
353
        goto END;
×
354
      }
355

356
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
×
357
        if (totalMetaRows > 0) {
×
358
          SEND_BATCH_META_RSP
×
359
        }
360
        SEND_DATA_RSP
×
361
      }
362

363
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
×
364
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
×
365
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
366

367
      // process meta
368
      if (pHead->msgType != TDMT_VND_SUBMIT) {
×
369
        if (totalRows > 0) {
×
370
          SEND_DATA_RSP
×
371
        }
372

373
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
×
374
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
×
375
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
×
376
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
×
377
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteAlterTable)
×
378
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
×
379
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
×
380
          } else if (pHead->msgType == TDMT_VND_DELETE) {
×
381
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
×
382
          }
383
        }
384

385
        tqDebug("fetch meta msg, ver:%" PRId64 ", vgId:%d, type:%s, enable batch meta:%d", pHead->version, vgId,
×
386
                TMSG_INFO(pHead->msgType), pRequest->enableBatchMeta);
387
        if (!pRequest->enableBatchMeta && !pRequest->useSnapshot) {
×
388
          SMqMetaRsp metaRsp = {0};
×
389
          tqOffsetResetToLog(&metaRsp.rspOffset, fetchVer + 1);
×
390
          metaRsp.resMsgType = pHead->msgType;
×
391
          metaRsp.metaRspLen = pHead->bodyLen;
×
392
          metaRsp.metaRsp = pHead->body;
×
393
          code = tqSendMetaPollRsp(pHandle, pMsg, pRequest, &metaRsp, vgId);
×
394
          goto END;
×
395
        }
396
        code = buildBatchMeta(&btMetaRsp, pHead->msgType, pHead->bodyLen, pHead->body);
×
397
        fetchVer++;
×
398
        if (code != 0){
×
399
          goto END;
×
400
        }
401
        totalMetaRows++;
×
402
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= pRequest->minPollRows) || (taosGetTimestampMs() - st > pRequest->timeout)) {
×
403
          SEND_BATCH_META_RSP
×
404
        }
405
        continue;
×
406
      }
407

408
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
×
409
        SEND_BATCH_META_RSP
×
410
      }
411

412
      // process data
413
      SPackedData submit = {
×
414
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
×
415
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
×
416
          .ver = pHead->version,
×
417
      };
418

419
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
×
420

421
      if (pHandle->fetchMeta == ONLY_META && taosArrayGetSize(taosxRsp.createTableReq) > 0){
×
422
        int32_t len = 0;
×
423
        void *pBuf = NULL;
×
424
        code = buildCreateTbBatchReqBinary(&taosxRsp, &pBuf, &len);
×
425
        if (code == 0){
×
426
          code = buildBatchMeta(&btMetaRsp, TDMT_VND_CREATE_TABLE, len, pBuf);
×
427
        }
428
        taosMemoryFree(pBuf);
×
429
        for (int i = 0; i < taosArrayGetSize(taosxRsp.createTableReq); i++) {
×
430
          void* pCreateTbReq = taosArrayGetP(taosxRsp.createTableReq, i);
×
431
          if (pCreateTbReq != NULL) {
×
432
            tDestroySVSubmitCreateTbReq(pCreateTbReq, TSDB_MSG_FLG_DECODE);
×
433
          }
434
          taosMemoryFree(pCreateTbReq);
×
435
        }
436
        taosArrayDestroy(taosxRsp.createTableReq);
×
437
        taosxRsp.createTableReq = NULL;
×
438
        fetchVer++;
×
439
        if (code != 0){
×
440
          goto END;
×
441
        }
442
        totalMetaRows++;
×
443
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= pRequest->minPollRows) ||
×
444
            (taosGetTimestampMs() - st > pRequest->timeout) ||
×
445
            (!pRequest->enableBatchMeta && !pRequest->useSnapshot)) {
×
446
          SEND_BATCH_META_RSP
×
447
        }
448
        continue;
×
449
      }
450

451
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
×
452
          (taosGetTimestampMs() - st > pRequest->timeout) ||
×
453
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
×
454
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
455
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
×
456
          terrno = 0;
×
457
        } else{
458
          fetchVer++;
×
459
        }
460
        SEND_DATA_RSP
×
461
      } else {
462
        fetchVer++;
×
463
      }
464
    }
465
  }
466

467
END:
×
468
  if (code != 0){
×
469
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
470
            pRequest->subKey);
471
  }
472
  tDeleteMqBatchMetaRsp(&btMetaRsp);
×
473
  tDeleteSTaosxRsp(&taosxRsp);
×
474
  return code;
×
475
}
476

477
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
×
478
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
×
479
    return TSDB_CODE_TMQ_INVALID_MSG;
×
480
  }
481
  int32_t      code = 0;
×
482
  STqOffsetVal reqOffset = {0};
×
483
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
×
484

485
  // reset the offset if needed
486
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
×
487
    bool blockReturned = false;
×
488
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
×
489
    if (code != 0) {
×
490
      goto END;
×
491
    }
492

493
    // empty block returned, quit
494
    if (blockReturned) {
×
495
      goto END;
×
496
    }
497
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
×
498
    uError("req offset type is 0");
×
499
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
500
    goto END;
×
501
  }
502

503
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
×
504
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
×
505
  } else {
506
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
×
507
  }
508

509
END:
×
510
  if (code != 0){
×
511
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
×
512
  }
513
  tOffsetDestroy(&reqOffset);
×
514
  return code;
×
515
}
516

517
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
×
518
                          int64_t ever) {
519
  if (pMsgHead == NULL) {
×
520
    return;
×
521
  }
522
  pMsgHead->consumerId = consumerId;
×
523
  pMsgHead->epoch = epoch;
×
524
  pMsgHead->mqMsgType = type;
×
525
  pMsgHead->walsver = sver;
×
526
  pMsgHead->walever = ever;
×
527
}
528

529
int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
×
530
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
531
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
×
532
    return TSDB_CODE_TMQ_INVALID_MSG;
×
533
  }
534
  int32_t len = 0;
×
535
  int32_t code = 0;
×
536
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
×
537
  if (code < 0) {
×
538
    return TAOS_GET_TERRNO(code);
×
539
  }
540
  int32_t tlen = sizeof(SMqRspHead) + len;
×
541
  void*   buf = rpcMallocCont(tlen);
×
542
  if (buf == NULL) {
×
543
    return TAOS_GET_TERRNO(terrno);
×
544
  }
545

546
  int64_t sver = 0, ever = 0;
×
547
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
548
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
×
549

550
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
551

552
  SEncoder encoder = {0};
×
553
  tEncoderInit(&encoder, abuf, len);
×
554
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
×
555
  tEncoderClear(&encoder);
×
556
  if (code < 0) {
×
557
    rpcFreeCont(buf);
×
558
    return TAOS_GET_TERRNO(code);
×
559
  }
560
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
×
561

562
  tmsgSendRsp(&resp);
×
563
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
×
564
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
565

566
  return 0;
×
567
}
568

569
int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp,
×
570
                          int32_t vgId) {
571
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
×
572
    return TSDB_CODE_TMQ_INVALID_MSG;
×
573
  }
574
  int32_t len = 0;
×
575
  int32_t code = 0;
×
576
  tEncodeSize(tEncodeMqMetaRsp, pRsp, len, code);
×
577
  if (code < 0) {
×
578
    return TAOS_GET_TERRNO(code);
×
579
  }
580
  int32_t tlen = sizeof(SMqRspHead) + len;
×
581
  void*   buf = rpcMallocCont(tlen);
×
582
  if (buf == NULL) {
×
583
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
584
  }
585

586
  int64_t sver = 0, ever = 0;
×
587
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
588
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
×
589

590
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
591

592
  SEncoder encoder = {0};
×
593
  tEncoderInit(&encoder, abuf, len);
×
594
  code = tEncodeMqMetaRsp(&encoder, pRsp);
×
595
  tEncoderClear(&encoder);
×
596
  if (code < 0) {
×
597
    rpcFreeCont(buf);
×
598
    return TAOS_GET_TERRNO(code);
×
599
  }
600

601
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
×
602

603
  tmsgSendRsp(&resp);
×
604
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type %d, offset type:%d", vgId,
×
605
          pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
606

607
  return 0;
×
608
}
609

610
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
×
611
                        int32_t type, int64_t sver, int64_t ever) {
612
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
×
613
    return TSDB_CODE_TMQ_INVALID_MSG;
×
614
  }
615
  int32_t len = 0;
×
616
  int32_t code = 0;
×
617

618
  if (type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP){
×
619
    pRsp->withSchema = 0;
×
620
  }
621
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
×
622
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
623
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
624
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
×
625
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
626
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
×
627
  }
628

629
  if (code < 0) {
×
630
    return TAOS_GET_TERRNO(code);
×
631
  }
632

633
  int32_t tlen = sizeof(SMqRspHead) + len;
×
634
  void*   buf = rpcMallocCont(tlen);
×
635
  if (buf == NULL) {
×
636
    return terrno;
×
637
  }
638

639
  SMqRspHead* pHead = (SMqRspHead*)buf;
×
640
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
×
641

642
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
643

644
  SEncoder encoder = {0};
×
645
  tEncoderInit(&encoder, abuf, len);
×
646

647
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
×
648
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
649
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
650
    code = tEncodeMqDataRsp(&encoder, pRsp);
×
651
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
652
    code = tEncodeSTaosxRsp(&encoder, pRsp);
×
653
  }
654
  tEncoderClear(&encoder);
×
655
  if (code < 0) {
×
656
    rpcFreeCont(buf);
×
657
    return TAOS_GET_TERRNO(code);
×
658
  }
659
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
×
660

661
  tmsgSendRsp(&rsp);
×
662
  return 0;
×
663
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc