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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

71.39
/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) {
3,742,162✔
24
  int32_t    code = TDB_CODE_SUCCESS;
3,742,162✔
25
  int32_t    lino = 0;
3,742,162✔
26
  tqDebug("%s called", __FUNCTION__ );
3,742,162!
27
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
3,742,254!
28

29
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
3,742,254✔
30
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);
3,742,254!
31

32
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
3,741,699✔
33
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
3,742,254!
34

35
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
3,742,254✔
36
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
3,742,155✔
37
  pRsp->withTbName = 0;
3,742,254✔
38
  pRsp->withSchema = false;
3,742,254✔
39

40
END:
3,742,157✔
41
  if (code != 0){
3,742,157!
42
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
43
  }
44
  return code;
3,742,143✔
45
}
46

47
static int32_t tqInitTaosxRsp(SMqDataRsp* pRsp, STqOffsetVal pOffset) {
967,365✔
48
  int32_t    code = TDB_CODE_SUCCESS;
967,365✔
49
  int32_t    lino = 0;
967,365✔
50
  tqDebug("%s called", __FUNCTION__ );
967,365!
51
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
967,895!
52
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
967,895✔
53
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
967,895✔
54

55
  pRsp->withTbName = 1;
967,895✔
56
  pRsp->withSchema = 1;
967,895✔
57
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
967,895✔
58
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);\
967,895!
59

60
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
967,895✔
61
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
967,895!
62

63
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
967,895✔
64
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
967,895!
65

66
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
967,895✔
67
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
967,708!
68

69
END:
967,779✔
70
  if (code != 0){
967,779!
71
    tqError("%s failed at:%d, code:%s", __FUNCTION__ , lino, tstrerror(code));
×
72
    taosArrayDestroy(pRsp->blockData);
×
73
    taosArrayDestroy(pRsp->blockDataLen);
×
74
    taosArrayDestroy(pRsp->blockTbName);
×
75
    taosArrayDestroy(pRsp->blockSchema);
×
76
  }
77
  return code;
967,605✔
78
}
79

80
static int32_t extractResetOffsetVal(STqOffsetVal* pOffsetVal, STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
204,338✔
81
                                     SRpcMsg* pMsg, bool* pBlockReturned) {
82
  if (pOffsetVal == NULL || pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL || pBlockReturned == NULL) {
204,338!
83
    return TSDB_CODE_INVALID_PARA;
×
84
  }
85
  uint64_t   consumerId = pRequest->consumerId;
204,533✔
86
  STqOffset* pOffset = NULL;
204,600✔
87
  int32_t    code = tqMetaGetOffset(pTq, pRequest->subKey, &pOffset);
204,533✔
88
  int32_t    vgId = TD_VID(pTq->pVnode);
204,198✔
89

90
  *pBlockReturned = false;
204,217✔
91
  // In this vnode, data has been polled by consumer for this topic, so let's continue from the last offset value.
92
  if (code == 0) {
202,180✔
93
    tOffsetCopy(pOffsetVal, &pOffset->val);
10,727✔
94

95
    char formatBuf[TSDB_OFFSET_LEN] = {0};
10,727✔
96
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
10,727✔
97
    tqDebug("tmq poll: consumer:0x%" PRIx64
10,727!
98
                ", subkey %s, vgId:%d, existed offset found, offset reset to %s and continue.QID:0x%" PRIx64,
99
            consumerId, pHandle->subKey, vgId, formatBuf, pRequest->reqId);
100
    return 0;
10,727✔
101
  } else {
102
    // no poll occurs in this vnode for this topic, let's seek to the right offset value.
103
    if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
191,453✔
104
      if (pRequest->useSnapshot) {
154,960✔
105
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
60,032✔
106
                consumerId, pHandle->subKey, vgId);
107
        if (pHandle->fetchMeta) {
60,541✔
108
          tqOffsetResetToMeta(pOffsetVal, 0);
109
        } else {
110
          SValue val = {0};
59,715!
111
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
112
        }
113
      } else {
114
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
94,378✔
115
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
95,573✔
116
      }
117
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
37,122✔
118
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
3,977✔
119
      SMqDataRsp dataRsp = {0};
3,977✔
120
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
3,977✔
121

122
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
3,977✔
123
      if (code != 0) {
3,977!
124
        return code;
×
125
      }
126
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
3,977!
127
              pHandle->subKey, vgId, dataRsp.rspOffset.version);
128
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, (pRequest->rawData == 1) ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
3,977!
129
      tDeleteMqDataRsp(&dataRsp);
3,977✔
130

131
      *pBlockReturned = true;
3,977✔
132
      return code;
3,977✔
133
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_NONE) {
32,973!
134
      tqError("tmq poll: subkey:%s, no offset committed for consumer:0x%" PRIx64
33,348✔
135
                  " in vg %d, subkey %s, reset none failed",
136
              pHandle->subKey, consumerId, vgId, pRequest->subKey);
137
      return TSDB_CODE_TQ_NO_COMMITTED_OFFSET;
33,624✔
138
    }
139
  }
140

141
  return 0;
156,116✔
142
}
143

144
static int32_t extractDataAndRspForNormalSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
3,658,817✔
145
                                                   SRpcMsg* pMsg, STqOffsetVal* pOffset) {
146
  int32_t    code = TDB_CODE_SUCCESS;
3,658,817✔
147
  int32_t    lino = 0;
3,658,817✔
148
  tqDebug("%s called", __FUNCTION__ );
3,658,817✔
149
  uint64_t consumerId = pRequest->consumerId;
3,659,765✔
150
  int32_t  vgId = TD_VID(pTq->pVnode);
3,659,663✔
151
  terrno = 0;
3,659,663✔
152

153
  SMqDataRsp dataRsp = {0};
3,659,663✔
154
  code = tqInitDataRsp(&dataRsp, *pOffset);
3,659,663✔
155
  TSDB_CHECK_CODE(code, lino, end);
3,659,566!
156

157
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
3,659,566✔
158
  TSDB_CHECK_CODE(code, lino, end);
3,659,483!
159

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
3,659,483✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
3,659,552✔
162
    goto end;
49,044✔
163
  }
164

165
  //   till now, all data has been transferred to consumer, new data needs to push client once arrived.
166
  if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST && dataRsp.blockNum == 0) {
3,610,508✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
1,367,185✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
1,367,185✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
1,367,185✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
1,297,367✔
172
      taosWUnLockLatch(&pTq->lock);
1,297,367✔
173
      goto end;
1,297,367✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
69,818✔
176
  }
177

178
  // reqOffset represents the current date offset, may be changed if wal not exists
179
  tOffsetCopy(&dataRsp.reqOffset, pOffset);
2,313,252✔
180
  code = tqSendDataRsp(pHandle, pMsg, pRequest, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
2,313,252✔
181

182
end:
3,659,178✔
183
  {
184
    char buf[TSDB_OFFSET_LEN] = {0};
3,659,275✔
185
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
3,659,372✔
186
    if (code != 0){
3,658,808✔
187
      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",
48,965!
188
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId, tstrerror(code), lino);
189
    } else {
190
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, rsp block:%d, rsp offset type:%s, QID:0x%" PRIx64 " success",
3,609,843!
191
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
192
    }
193

194
    tDeleteMqDataRsp(&dataRsp);
3,659,584✔
195
    return code;
3,659,595✔
196
  }
197
}
198

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

217
static void tDeleteCommon(void* parm) {}
7,596✔
218

219
#define POLL_RSP_TYPE(pRequest,taosxRsp) \
220
taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : \
221
(pRequest->rawData == 1 ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP)
222

223
static int32_t buildBatchMeta(SMqBatchMetaRsp *btMetaRsp, int16_t type, int32_t bodyLen, void* body){
72✔
224
  int32_t         code = 0;
72✔
225

226
  if (!btMetaRsp->batchMetaReq) {
72!
227
    btMetaRsp->batchMetaReq = taosArrayInit(4, POINTER_BYTES);
72✔
228
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaReq);
72!
229
    btMetaRsp->batchMetaLen = taosArrayInit(4, sizeof(int32_t));
72✔
230
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaLen);
72!
231
  }
232

233
  SMqMetaRsp tmpMetaRsp = {0};
72✔
234
  tmpMetaRsp.resMsgType = type;
72✔
235
  tmpMetaRsp.metaRspLen = bodyLen;
72✔
236
  tmpMetaRsp.metaRsp = body;
72✔
237
  uint32_t len = 0;
72✔
238
  tEncodeSize(tEncodeMqMetaRsp, &tmpMetaRsp, len, code);
72!
239
  if (TSDB_CODE_SUCCESS != code) {
72!
240
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
241
    goto END;
×
242
  }
243
  int32_t tLen = sizeof(SMqRspHead) + len;
72✔
244
  void*   tBuf = taosMemoryCalloc(1, tLen);
72!
245
  TQ_NULL_GO_TO_END(tBuf);
72!
246
  void*    metaBuff = POINTER_SHIFT(tBuf, sizeof(SMqRspHead));
72✔
247
  SEncoder encoder = {0};
72✔
248
  tEncoderInit(&encoder, metaBuff, len);
72✔
249
  code = tEncodeMqMetaRsp(&encoder, &tmpMetaRsp);
72✔
250
  tEncoderClear(&encoder);
72✔
251

252
  if (code < 0) {
72!
253
    tqError("tmq extract meta from log, tEncodeMqMetaRsp error");
×
254
    goto END;
×
255
  }
256
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaReq, &tBuf));
144!
257
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaLen, &tLen));
144!
258

259
END:
72✔
260
  return code;
72✔
261
}
262

263
static int32_t buildCreateTbBatchReqBinary(SMqDataRsp *taosxRsp, void** pBuf, int32_t *len){
24✔
264
  int32_t code = 0;
24✔
265
  SVCreateTbBatchReq pReq = {0};
24✔
266
  pReq.nReqs = taosArrayGetSize(taosxRsp->createTableReq);
24✔
267
  pReq.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
24✔
268
  TQ_NULL_GO_TO_END(pReq.pArray);
24!
269
  for (int i = 0; i < taosArrayGetSize(taosxRsp->createTableReq); i++){
120✔
270
    void   *createTableReq = taosArrayGetP(taosxRsp->createTableReq, i);
96✔
271
    TQ_NULL_GO_TO_END(taosArrayPush(pReq.pArray, createTableReq));
192!
272
  }
273
  tEncodeSize(tEncodeSVCreateTbBatchReq, &pReq, *len, code);
24!
274
  if (code < 0) {
24!
275
    goto END;
×
276
  }
277
  *len += sizeof(SMsgHead);
24✔
278
  *pBuf = taosMemoryMalloc(*len);
24!
279
  TQ_NULL_GO_TO_END(pBuf);
24!
280
  SEncoder coder = {0};
24✔
281
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *len);
24✔
282
  code = tEncodeSVCreateTbBatchReq(&coder, &pReq);
24✔
283
  tEncoderClear(&coder);
24✔
284

285
END:
24✔
286
  taosArrayDestroy(pReq.pArray);
24✔
287
  return code;
24✔
288
}
289

290
#define SEND_BATCH_META_RSP \
291
tqOffsetResetToLog(&btMetaRsp.rspOffset, fetchVer);\
292
code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);\
293
goto END;
294

295
#define SEND_DATA_RSP \
296
tqOffsetResetToLog(&taosxRsp.rspOffset, fetchVer);\
297
code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, POLL_RSP_TYPE(pRequest, taosxRsp), vgId);\
298
goto END;
299
static int32_t extractDataAndRspForDbStbSubscribe(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest,
967,715✔
300
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
301
  int32_t         vgId = TD_VID(pTq->pVnode);
967,715✔
302
  SMqDataRsp      taosxRsp = {0};
967,791✔
303
  SMqBatchMetaRsp btMetaRsp = {0};
967,715✔
304
  int32_t         code = 0;
967,871✔
305

306
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
967,871!
307
  if (offset->type != TMQ_OFFSET__LOG) {
967,572✔
308
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
10,052!
309

310
    if (taosArrayGetSize(btMetaRsp.batchMetaReq) > 0) {
10,052✔
311
      code = tqSendBatchMetaPollRsp(pHandle, pMsg, pRequest, &btMetaRsp, vgId);
936✔
312
      tqDebug("tmq poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send meta offset type:%d,uid:%" PRId64 ",ts:%" PRId64,
936!
313
              pRequest->consumerId, pHandle->subKey, vgId, btMetaRsp.rspOffset.type, btMetaRsp.rspOffset.uid,btMetaRsp.rspOffset.ts);
314
      goto END;
936✔
315
    }
316

317
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
9,116!
318
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
319
    if (taosxRsp.blockNum > 0) {
9,116✔
320
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
8,936✔
321
      goto END;
8,936✔
322
    } else {
323
      tOffsetCopy(offset, &taosxRsp.rspOffset);
180✔
324
    }
325
  }
326

327
  if (offset->type == TMQ_OFFSET__LOG) {
957,790!
328
    walReaderVerifyOffset(pHandle->pWalReader, offset);
958,023✔
329
    int64_t fetchVer = offset->version;
957,817✔
330

331
    uint64_t st = taosGetTimestampMs();
957,650✔
332
    int      totalRows = 0;
957,650✔
333
    int32_t  totalMetaRows = 0;
957,841✔
334
    while (1) {
17,038,974✔
335
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
17,996,815✔
336
      if (savedEpoch > pRequest->epoch) {
17,990,596!
337
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
338
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
339
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
340
        goto END;
×
341
      }
342

343
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
17,986,144✔
344
        if (totalMetaRows > 0) {
921,238✔
345
          SEND_BATCH_META_RSP
96✔
346
        }
347
        SEND_DATA_RSP
1,842,380!
348
      }
349

350
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
17,089,892✔
351
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
17,090,184!
352
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
353

354
      // process meta
355
      if (pHead->msgType != TDMT_VND_SUBMIT) {
17,091,017✔
356
        if (totalRows > 0) {
13,378✔
357
          SEND_DATA_RSP
1,250!
358
        }
359

360
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
12,753✔
361
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
12,468✔
362
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
3,960!
363
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
8,508✔
364
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteCommon)
1,056!
365
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
9,360✔
366
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
6,420!
367
          } else if (pHead->msgType == TDMT_VND_DELETE) {
1,032✔
368
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
120!
369
          }
370
        }
371

372
        tqDebug("fetch meta msg, ver:%" PRId64 ", vgId:%d, type:%s, enable batch meta:%d", pHead->version, vgId,
4,665!
373
                TMSG_INFO(pHead->msgType), pRequest->enableBatchMeta);
374
        if (!pRequest->enableBatchMeta && !pRequest->useSnapshot) {
4,665!
375
          SMqMetaRsp metaRsp = {0};
4,617✔
376
          tqOffsetResetToLog(&metaRsp.rspOffset, fetchVer + 1);
4,617✔
377
          metaRsp.resMsgType = pHead->msgType;
4,617✔
378
          metaRsp.metaRspLen = pHead->bodyLen;
4,617✔
379
          metaRsp.metaRsp = pHead->body;
4,617✔
380
          code = tqSendMetaPollRsp(pHandle, pMsg, pRequest, &metaRsp, vgId);
4,617✔
381
          goto END;
4,617✔
382
        }
383
        code = buildBatchMeta(&btMetaRsp, pHead->msgType, pHead->bodyLen, pHead->body);
48✔
384
        fetchVer++;
48✔
385
        if (code != 0){
48!
386
          goto END;
×
387
        }
388
        totalMetaRows++;
48✔
389
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= tmqRowSize) || (taosGetTimestampMs() - st > pRequest->timeout)) {
96!
390
          SEND_BATCH_META_RSP
×
391
        }
392
        continue;
48✔
393
      }
394

395
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
17,076,806!
396
        SEND_BATCH_META_RSP
×
397
      }
398

399
      // process data
400
      SPackedData submit = {
51,230,320✔
401
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
17,076,806✔
402
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
17,076,806✔
403
          .ver = pHead->version,
17,076,806✔
404
      };
405

406
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
17,076,708!
407

408
      if (pHandle->fetchMeta == ONLY_META && taosArrayGetSize(taosxRsp.createTableReq) > 0){
17,053,342✔
409
        int32_t len = 0;
24✔
410
        void *pBuf = NULL;
24✔
411
        code = buildCreateTbBatchReqBinary(&taosxRsp, &pBuf, &len);
24✔
412
        if (code == 0){
24!
413
          code = buildBatchMeta(&btMetaRsp, TDMT_VND_CREATE_TABLE, len, pBuf);
24✔
414
        }
415
        taosMemoryFree(pBuf);
24!
416
        for (int i = 0; i < taosArrayGetSize(taosxRsp.createTableReq); i++) {
120✔
417
          void* pCreateTbReq = taosArrayGetP(taosxRsp.createTableReq, i);
96✔
418
          if (pCreateTbReq != NULL) {
96!
419
            tDestroySVSubmitCreateTbReq(pCreateTbReq, TSDB_MSG_FLG_DECODE);
96✔
420
          }
421
          taosMemoryFree(pCreateTbReq);
96!
422
        }
423
        taosArrayDestroy(taosxRsp.createTableReq);
24✔
424
        taosxRsp.createTableReq = NULL;
24✔
425
        fetchVer++;
24✔
426
        if (code != 0){
24!
427
          goto END;
×
428
        }
429
        totalMetaRows++;
24✔
430
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= tmqRowSize) ||
24!
431
            (taosGetTimestampMs() - st > pRequest->timeout) ||
24!
432
            (!pRequest->enableBatchMeta && !pRequest->useSnapshot)) {
24!
433
          SEND_BATCH_META_RSP
48✔
434
        }
435
        continue;
×
436
      }
437

438
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
17,069,338!
439
          (taosGetTimestampMs() - st > pRequest->timeout) ||
17,029,309!
440
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
17,038,661!
441
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
442
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
36,360!
443
          terrno = 0;
×
444
        } else{
445
          fetchVer++;
31,519✔
446
        }
447
        SEND_DATA_RSP
63,038!
448
      } else {
449
        fetchVer++;
17,029,050✔
450
      }
451
    }
452
  }
453

454
END:
967,365✔
455
  if (code != 0){
967,431!
456
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
457
            pRequest->subKey);
458
  }
459
  tDeleteMqBatchMetaRsp(&btMetaRsp);
967,431✔
460
  tDeleteSTaosxRsp(&taosxRsp);
967,402✔
461
  return code;
966,052✔
462
}
463

464
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
4,664,693✔
465
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
4,664,693!
466
    return TSDB_CODE_TMQ_INVALID_MSG;
×
467
  }
468
  int32_t      code = 0;
4,665,045✔
469
  STqOffsetVal reqOffset = {0};
4,665,045✔
470
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
4,665,092✔
471

472
  // reset the offset if needed
473
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
4,665,081✔
474
    bool blockReturned = false;
204,533✔
475
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
204,600✔
476
    if (code != 0) {
204,444✔
477
      goto END;
33,624✔
478
    }
479

480
    // empty block returned, quit
481
    if (blockReturned) {
170,820!
482
      goto END;
3,977✔
483
    }
484
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
4,460,415!
485
    uError("req offset type is 0");
×
486
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
487
    goto END;
×
488
  }
489

490
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,627,088✔
491
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
3,658,769✔
492
  } else {
493
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
967,694✔
494
  }
495

496
END:
4,664,687✔
497
  if (code != 0){
4,664,351✔
498
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
82,600!
499
  }
500
  tOffsetDestroy(&reqOffset);
4,664,419✔
501
  return code;
4,664,323✔
502
}
503

504
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
3,363,175✔
505
                          int64_t ever) {
506
  if (pMsgHead == NULL) {
3,363,175!
507
    return;
×
508
  }
509
  pMsgHead->consumerId = consumerId;
3,363,175✔
510
  pMsgHead->epoch = epoch;
3,363,544✔
511
  pMsgHead->mqMsgType = type;
3,362,920✔
512
  pMsgHead->walsver = sver;
3,362,526✔
513
  pMsgHead->walever = ever;
3,363,075✔
514
}
515

516
int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
1,008✔
517
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
518
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
1,008!
519
    return TSDB_CODE_TMQ_INVALID_MSG;
×
520
  }
521
  int32_t len = 0;
1,008✔
522
  int32_t code = 0;
1,008✔
523
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
1,008!
524
  if (code < 0) {
1,008!
525
    return TAOS_GET_TERRNO(code);
×
526
  }
527
  int32_t tlen = sizeof(SMqRspHead) + len;
1,008✔
528
  void*   buf = rpcMallocCont(tlen);
1,008✔
529
  if (buf == NULL) {
1,008!
530
    return TAOS_GET_TERRNO(terrno);
×
531
  }
532

533
  int64_t sver = 0, ever = 0;
1,008✔
534
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
1,008✔
535
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
1,008✔
536

537
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
1,008✔
538

539
  SEncoder encoder = {0};
1,008✔
540
  tEncoderInit(&encoder, abuf, len);
1,008✔
541
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
1,008✔
542
  tEncoderClear(&encoder);
1,008✔
543
  if (code < 0) {
1,008!
544
    rpcFreeCont(buf);
×
545
    return TAOS_GET_TERRNO(code);
×
546
  }
547
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
1,008✔
548

549
  tmsgSendRsp(&resp);
1,008✔
550
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
1,008!
551
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
552

553
  return 0;
1,008✔
554
}
555

556
int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp,
4,617✔
557
                          int32_t vgId) {
558
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
4,617!
559
    return TSDB_CODE_TMQ_INVALID_MSG;
×
560
  }
561
  int32_t len = 0;
4,617✔
562
  int32_t code = 0;
4,617✔
563
  tEncodeSize(tEncodeMqMetaRsp, pRsp, len, code);
4,617!
564
  if (code < 0) {
4,617!
565
    return TAOS_GET_TERRNO(code);
×
566
  }
567
  int32_t tlen = sizeof(SMqRspHead) + len;
4,617✔
568
  void*   buf = rpcMallocCont(tlen);
4,617✔
569
  if (buf == NULL) {
4,617!
570
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
571
  }
572

573
  int64_t sver = 0, ever = 0;
4,617✔
574
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
4,617✔
575
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
4,617✔
576

577
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
4,593✔
578

579
  SEncoder encoder = {0};
4,617✔
580
  tEncoderInit(&encoder, abuf, len);
4,617✔
581
  code = tEncodeMqMetaRsp(&encoder, pRsp);
4,593✔
582
  tEncoderClear(&encoder);
4,617✔
583
  if (code < 0) {
4,617!
584
    rpcFreeCont(buf);
×
585
    return TAOS_GET_TERRNO(code);
×
586
  }
587

588
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
4,617✔
589

590
  tmsgSendRsp(&resp);
4,617✔
591
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type %d, offset type:%d", vgId,
4,617!
592
          pReq->consumerId, pReq->epoch, pRsp->resMsgType, pRsp->rspOffset.type);
593

594
  return 0;
4,617✔
595
}
596

597
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
3,358,113✔
598
                        int32_t type, int64_t sver, int64_t ever) {
599
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
3,358,113!
600
    return TSDB_CODE_TMQ_INVALID_MSG;
×
601
  }
602
  int32_t len = 0;
3,358,113✔
603
  int32_t code = 0;
3,358,113✔
604

605
  if (type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP){
3,358,113!
606
    pRsp->withSchema = 0;
×
607
  }
608
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
3,358,113✔
609
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
469!
610
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
611
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
3,357,644!
612
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
469!
613
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
469!
614
  }
615

616
  if (code < 0) {
3,357,129!
617
    return TAOS_GET_TERRNO(code);
×
618
  }
619

620
  int32_t tlen = sizeof(SMqRspHead) + len;
3,357,129✔
621
  void*   buf = rpcMallocCont(tlen);
3,357,129✔
622
  if (buf == NULL) {
3,357,550!
623
    return terrno;
×
624
  }
625

626
  SMqRspHead* pHead = (SMqRspHead*)buf;
3,357,550✔
627
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
3,357,550✔
628

629
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3,356,511✔
630

631
  SEncoder encoder = {0};
3,357,547✔
632
  tEncoderInit(&encoder, abuf, len);
3,357,780✔
633

634
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
3,357,335✔
635
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
469!
636
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
637
    code = tEncodeMqDataRsp(&encoder, pRsp);
3,356,866✔
638
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
469!
639
    code = tEncodeSTaosxRsp(&encoder, pRsp);
469✔
640
  }
641
  tEncoderClear(&encoder);
3,356,824✔
642
  if (code < 0) {
3,356,498!
643
    rpcFreeCont(buf);
×
644
    return TAOS_GET_TERRNO(code);
×
645
  }
646
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
3,356,498✔
647

648
  tmsgSendRsp(&rsp);
3,355,459✔
649
  return 0;
3,357,803✔
650
}
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