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

taosdata / TDengine / #4918

08 Jan 2026 11:50AM UTC coverage: 65.916% (+0.5%) from 65.42%
#4918

push

travis-ci

web-flow
merge: from main to 3.0 branch #34215

16 of 22 new or added lines in 3 files covered. (72.73%)

788 existing lines in 116 files now uncovered.

204221 of 309822 relevant lines covered (65.92%)

126504701.11 hits per line

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

87.93
/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) {
21,168,138✔
24
  int32_t    code = TDB_CODE_SUCCESS;
21,168,138✔
25
  int32_t    lino = 0;
21,168,138✔
26
  tqDebug("%s called", __FUNCTION__ );
21,168,138✔
27
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
21,168,138✔
28

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

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

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

43
END:
21,168,138✔
44
  if (code != 0){
21,168,138✔
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;
21,168,138✔
51
}
52

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

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

66
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
5,612,014✔
67
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
5,612,268✔
68

69
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
5,612,014✔
70
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
5,612,268✔
71

72
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
5,611,384✔
73
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
5,611,983✔
74

75
END:
5,611,384✔
76
  if (code != 0){
5,611,384✔
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;
5,612,014✔
84
}
85

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

96
  *pBlockReturned = false;
624,921✔
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) {
625,846✔
99
    tOffsetCopy(pOffsetVal, &pOffset->val);
97,183✔
100

101
    char formatBuf[TSDB_OFFSET_LEN] = {0};
97,183✔
102
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
97,183✔
103
    tqDebug("tmq poll: consumer:0x%" PRIx64
97,183✔
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;
97,183✔
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) {
528,663✔
110
      if (pRequest->useSnapshot) {
399,977✔
111
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
191,451✔
112
                consumerId, pHandle->subKey, vgId);
113
        if (pHandle->fetchMeta) {
191,748✔
114
          tqOffsetResetToMeta(pOffsetVal, 0);
115
        } else {
116
          SValue val = {0};
187,457✔
117
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
118
        }
119
      } else {
120
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
207,364✔
121
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
209,127✔
122
      }
123
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
129,018✔
124
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
15,354✔
125
      SMqDataRsp dataRsp = {0};
15,354✔
126
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
15,354✔
127

128
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
15,354✔
129
      if (code != 0) {
15,354✔
130
        return code;
×
131
      }
132
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
15,354✔
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);
15,354✔
135
      tDeleteMqDataRsp(&dataRsp);
15,354✔
136

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

147
  return 0;
401,167✔
148
}
149

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

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

163
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
21,008,025✔
164
  TSDB_CHECK_CODE(code, lino, end);
21,007,775✔
165

166
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
21,007,775✔
167
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
21,008,025✔
168
    goto end;
145,075✔
169
  }
170

171
  //   till now, all data has been transferred to consumer, new data needs to push client once arrived.
172
  if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST && dataRsp.blockNum == 0) {
20,862,950✔
173
    // lock
174
    taosWLockLatch(&pTq->lock);
9,320,825✔
175
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
9,320,825✔
176
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
9,320,825✔
177
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
9,107,227✔
178
      taosWUnLockLatch(&pTq->lock);
9,107,227✔
179
      goto end;
9,107,227✔
180
    }
181
    taosWUnLockLatch(&pTq->lock);
213,598✔
182
  }
183

184
  // reqOffset represents the current date offset, may be changed if wal not exists
185
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
11,755,723✔
186
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
11,755,723✔
187

188
end:
21,007,703✔
189
  {
190
    char buf[TSDB_OFFSET_LEN] = {0};
21,008,025✔
191
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
21,008,025✔
192
    if (code != 0){
21,007,406✔
193
      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",
144,778✔
194
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, tstrerror(code), lino);
195
    } else {
196
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " success",
20,862,628✔
197
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
198
    }
199

200
    tDeleteMqDataRsp(&dataRsp);
21,008,025✔
201
    return code;
21,007,418✔
202
  }
203
}
204

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

223
static void tDeleteCommon(void* parm) {}
69,230✔
224
static void tDeleteAlterTable(SVAlterTbReq* req) {
11,176✔
225
  taosArrayDestroy(req->pMultiTag);
11,176✔
226
}
11,176✔
227

228
#define POLL_RSP_TYPE(pRequest,taosxRsp) \
229
taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : \
230
(pRequest->rawData == 1 ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP)
231

232
static int32_t buildBatchMeta(SMqBatchMetaRsp *btMetaRsp, int16_t type, int32_t bodyLen, void* body){
1,593✔
233
  int32_t         code = 0;
1,593✔
234

235
  if (!btMetaRsp->batchMetaReq) {
1,593✔
236
    btMetaRsp->batchMetaReq = taosArrayInit(4, POINTER_BYTES);
1,593✔
237
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaReq);
1,593✔
238
    btMetaRsp->batchMetaLen = taosArrayInit(4, sizeof(int32_t));
1,593✔
239
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaLen);
1,593✔
240
  }
241

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

261
  if (code < 0) {
1,593✔
262
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
263
    goto END;
×
264
  }
265
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaReq, &tBuf));
3,186✔
266
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaLen, &tLen));
3,186✔
267

268
END:
1,593✔
269
  return code;
1,593✔
270
}
271

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

294
END:
1,085✔
295
  taosArrayDestroy(pReq.pArray);
1,085✔
296
  return code;
1,085✔
297
}
298

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

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

309
static int32_t extractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
5,612,268✔
310
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
311
  int32_t         vgId = TD_VID(pTq->pVnode);
5,612,268✔
312
  SMqDataRsp      taosxRsp = {0};
5,612,014✔
313
  SMqBatchMetaRsp btMetaRsp = {0};
5,611,760✔
314
  int32_t         code = 0;
5,612,014✔
315

316
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
5,612,014✔
317
  if (offset->type != TMQ_OFFSET__LOG) {
5,612,014✔
318
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
42,613✔
319

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

327
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
38,538✔
328
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
329
    if (taosxRsp.blockNum > 0) {
38,538✔
330
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
35,688✔
331
      goto END;
35,688✔
332
    } else {
333
      tOffsetCopy(offset, &taosxRsp.rspOffset);
2,850✔
334
    }
335
  }
336

337
  if (offset->type == TMQ_OFFSET__LOG) {
5,572,251✔
338
    walReaderVerifyOffset(pHandle->pWalReader, offset);
5,572,505✔
339
    int64_t fetchVer = offset->version;
5,570,873✔
340

341
    uint64_t st = taosGetTimestampMs();
5,570,614✔
342
    int      totalRows = 0;
5,570,614✔
343
    int32_t  totalMetaRows = 0;
5,570,572✔
344
    while (1) {
41,134,829✔
345
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
46,705,401✔
346
      if (savedEpoch > pRequest->epoch) {
46,689,577✔
347
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
348
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
349
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
350
        goto END;
×
351
      }
352

353
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
46,693,950✔
354
        if (totalMetaRows > 0) {
5,375,510✔
355
          SEND_BATCH_META_RSP
1,016✔
356
        }
357
        SEND_DATA_RSP
10,750,004✔
358
      }
359

360
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
41,380,174✔
361
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
41,380,982✔
362
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
363

364
      // process meta
365
      if (pHead->msgType != TDMT_VND_SUBMIT) {
41,380,982✔
366
        if (totalRows > 0) {
143,182✔
367
          SEND_DATA_RSP
12,796✔
368
        }
369

370
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
136,784✔
371
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
131,968✔
372
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
41,910✔
373
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
90,058✔
374
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteAlterTable)
11,176✔
375
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
99,090✔
376
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
67,960✔
377
          } else if (pHead->msgType == TDMT_VND_DELETE) {
10,922✔
378
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
1,270✔
379
          }
380
        }
381

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

405
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
41,236,969✔
406
        SEND_BATCH_META_RSP
×
407
      }
408

409
      // process data
410
      SPackedData submit = {
123,714,231✔
411
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
41,236,969✔
412
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
41,238,077✔
413
          .ver = pHead->version,
41,238,077✔
414
      };
415

416
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
41,238,077✔
417

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

448
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
41,186,457✔
449
          (taosGetTimestampMs() - st > pRequest->timeout) ||
41,047,930✔
450
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
41,078,304✔
451
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
452
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
173,373✔
453
          terrno = 0;
×
454
        } else{
455
          fetchVer++;
138,834✔
456
        }
457
        SEND_DATA_RSP
277,668✔
458
      } else {
459
        fetchVer++;
41,039,222✔
460
      }
461
    }
462
  }
463

464
END:
5,610,906✔
465
  if (code != 0){
5,609,086✔
466
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
467
            pRequest->subKey);
468
  }
469
  tDeleteMqBatchMetaRsp(&btMetaRsp);
5,609,086✔
470
  tDeleteSTaosxRsp(&taosxRsp);
5,610,321✔
471
  return code;
5,603,617✔
472
}
473

474
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
26,748,116✔
475
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
26,748,116✔
UNCOV
476
    return TSDB_CODE_TMQ_INVALID_MSG;
×
477
  }
478
  int32_t      code = 0;
26,749,311✔
479
  STqOffsetVal reqOffset = {0};
26,749,311✔
480
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
26,749,311✔
481

482
  // reset the offset if needed
483
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
26,749,311✔
484
    bool blockReturned = false;
627,076✔
485
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
627,076✔
486
    if (code != 0) {
627,046✔
487
      goto END;
113,664✔
488
    }
489

490
    // empty block returned, quit
491
    if (blockReturned) {
513,382✔
492
      goto END;
15,354✔
493
    }
494
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
26,121,943✔
495
    uError("req offset type is 0");
×
496
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
497
    goto END;
×
498
  }
499

500
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
26,619,427✔
501
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
21,008,025✔
502
  } else {
503
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
5,612,268✔
504
  }
505

506
END:
26,746,746✔
507
  if (code != 0){
26,739,878✔
508
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
258,739✔
509
  }
510
  tOffsetDestroy(&reqOffset);
26,739,878✔
511
  return code;
26,746,900✔
512
}
513

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

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

543
  int64_t sver = 0, ever = 0;
5,668✔
544
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
5,668✔
545
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
5,668✔
546

547
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
5,668✔
548

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

559
  tmsgSendRsp(&resp);
5,668✔
560
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
5,668✔
561
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
562

563
  return 0;
5,668✔
564
}
565

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

583
  int64_t sver = 0, ever = 0;
50,678✔
584
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
50,678✔
585
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
50,678✔
586

587
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
50,678✔
588

589
  SEncoder encoder = {0};
50,678✔
590
  tEncoderInit(&encoder, abuf, len);
50,678✔
591
  code = tEncodeMqMetaRsp(&encoder, pRsp);
50,678✔
592
  tEncoderClear(&encoder);
50,678✔
593
  if (code < 0) {
50,678✔
594
    rpcFreeCont(buf);
×
595
    return TAOS_GET_TERRNO(code);
×
596
  }
597

598
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
50,678✔
599

600
  tmsgSendRsp(&resp);
50,678✔
601
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type %d, offset type:%d", vgId,
50,678✔
602
          pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
603

604
  return 0;
50,678✔
605
}
606

607
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
17,471,758✔
608
                        int32_t type, int64_t sver, int64_t ever) {
609
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
17,471,758✔
610
    return TSDB_CODE_TMQ_INVALID_MSG;
×
611
  }
612
  int32_t len = 0;
17,471,758✔
613
  int32_t code = 0;
17,471,758✔
614

615
  if (type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP){
17,471,758✔
616
    pRsp->withSchema = 0;
×
617
  }
618
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
17,471,758✔
619
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
4,512✔
620
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
621
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
17,467,246✔
622
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
4,512✔
623
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
4,512✔
624
  }
625

626
  if (code < 0) {
17,470,829✔
627
    return TAOS_GET_TERRNO(code);
×
628
  }
629

630
  int32_t tlen = sizeof(SMqRspHead) + len;
17,470,829✔
631
  void*   buf = rpcMallocCont(tlen);
17,470,829✔
632
  if (buf == NULL) {
17,470,491✔
633
    return terrno;
×
634
  }
635

636
  SMqRspHead* pHead = (SMqRspHead*)buf;
17,470,491✔
637
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
17,470,491✔
638

639
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
17,468,130✔
640

641
  SEncoder encoder = {0};
17,467,876✔
642
  tEncoderInit(&encoder, abuf, len);
17,467,872✔
643

644
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
17,469,698✔
645
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
4,512✔
646
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
647
    code = tEncodeMqDataRsp(&encoder, pRsp);
17,465,186✔
648
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
4,512✔
649
    code = tEncodeSTaosxRsp(&encoder, pRsp);
4,512✔
650
  }
651
  tEncoderClear(&encoder);
17,467,194✔
652
  if (code < 0) {
17,467,845✔
653
    rpcFreeCont(buf);
×
654
    return TAOS_GET_TERRNO(code);
×
655
  }
656
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
17,467,845✔
657

658
  tmsgSendRsp(&rsp);
17,469,726✔
659
  return 0;
17,469,433✔
660
}
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