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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

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

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

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

35
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
200,006✔
36
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
200,006✔
37
  pRsp->withTbName = 0;
200,006✔
38
  pRsp->withSchema = false;
200,006✔
39

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

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

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

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

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

66
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
3,694✔
67
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
3,694✔
68

69
END:
3,694✔
70
  if (code != 0){
3,694✔
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;
3,694✔
78
}
79

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

90
  *pBlockReturned = false;
60,553✔
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) {
60,553✔
93
    tOffsetCopy(pOffsetVal, &pOffset->val);
35,244✔
94

95
    char formatBuf[TSDB_OFFSET_LEN] = {0};
35,244✔
96
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
35,244✔
97
    tqDebug("tmq poll: consumer:0x%" PRIx64
35,244✔
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;
35,244✔
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) {
25,309✔
104
      if (pRequest->useSnapshot) {
21,666✔
105
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
×
106
                consumerId, pHandle->subKey, vgId);
107
        if (pHandle->fetchMeta) {
×
108
          tqOffsetResetToMeta(pOffsetVal, 0);
109
        } else {
110
          SValue val = {0};
×
111
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
112
        }
113
      } else {
114
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
21,666✔
115
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
21,666✔
116
      }
117
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
3,643✔
118
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
3,643✔
119
      SMqDataRsp dataRsp = {0};
3,643✔
120
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
3,643✔
121

122
      code = tqInitDataRsp(&dataRsp, *pOffsetVal);
3,643✔
123
      if (code != 0) {
3,643✔
124
        return code;
×
125
      }
126
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey %s, vgId:%d, (latest) offset reset to %" PRId64, consumerId,
3,643✔
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,643✔
129
      tDeleteMqDataRsp(&dataRsp);
3,643✔
130

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

141
  return 0;
21,666✔
142
}
143

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

153
  SMqDataRsp dataRsp = {0};
173,692✔
154
  code = tqInitDataRsp(&dataRsp, *pOffset);
173,692✔
155
  TSDB_CHECK_CODE(code, lino, end);
173,692✔
156

157
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
173,692✔
158
  TSDB_CHECK_CODE(code, lino, end);
173,692✔
159

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
173,692✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
173,692✔
162
    goto end;
×
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) {
173,692✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
23,205✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
23,205✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
23,205✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
22,671✔
172
      taosWUnLockLatch(&pTq->lock);
22,671✔
173
      goto end;
22,671✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
534✔
176
  }
177

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

182
end:
173,692✔
183
  {
184
    char buf[TSDB_OFFSET_LEN] = {0};
173,692✔
185
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
173,692✔
186
    if (code != 0){
173,692✔
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",
×
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",
173,692✔
191
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
192
    }
193

194
    tDeleteMqDataRsp(&dataRsp);
173,692✔
195
    return code;
173,467✔
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) {}
×
218
static void tDeleteAlterTable(SVAlterTbReq* req) {
×
219
  taosArrayDestroy(req->pMultiTag);
×
220
}
×
221

222
#define POLL_RSP_TYPE(pRequest,taosxRsp) \
223
taosxRsp.createTableNum > 0 ? TMQ_MSG_TYPE__POLL_DATA_META_RSP : \
224
(pRequest->rawData == 1 ? TMQ_MSG_TYPE__POLL_RAW_DATA_RSP : TMQ_MSG_TYPE__POLL_DATA_RSP)
225

226
static int32_t buildBatchMeta(SMqBatchMetaRsp *btMetaRsp, int16_t type, int32_t bodyLen, void* body){
×
227
  int32_t         code = 0;
×
228

229
  if (!btMetaRsp->batchMetaReq) {
×
230
    btMetaRsp->batchMetaReq = taosArrayInit(4, POINTER_BYTES);
×
231
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaReq);
×
232
    btMetaRsp->batchMetaLen = taosArrayInit(4, sizeof(int32_t));
×
233
    TQ_NULL_GO_TO_END(btMetaRsp->batchMetaLen);
×
234
  }
235

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

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

262
END:
×
263
  return code;
×
264
}
265

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

288
END:
×
289
  taosArrayDestroy(pReq.pArray);
×
290
  return code;
×
291
}
292

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

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

309
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
3,694✔
310
  if (offset->type != TMQ_OFFSET__LOG) {
3,694✔
311
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
×
312

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

320
    tqDebug("taosx poll: consumer:0x%" PRIx64 " subkey:%s vgId:%d, send data blockNum:%d, offset type:%d,uid:%" PRId64",ts:%" PRId64,
×
321
            pRequest->consumerId, pHandle->subKey, vgId, taosxRsp.blockNum, taosxRsp.rspOffset.type, taosxRsp.rspOffset.uid, taosxRsp.rspOffset.ts);
322
    if (taosxRsp.blockNum > 0) {
×
323
      code = tqSendDataRsp(pHandle, pMsg, pRequest, &taosxRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
324
      goto END;
×
325
    } else {
326
      tOffsetCopy(offset, &taosxRsp.rspOffset);
×
327
    }
328
  }
329

330
  if (offset->type == TMQ_OFFSET__LOG) {
3,694✔
331
    walReaderVerifyOffset(pHandle->pWalReader, offset);
3,694✔
332
    int64_t fetchVer = offset->version;
3,694✔
333

334
    uint64_t st = taosGetTimestampMs();
3,694✔
335
    int      totalRows = 0;
3,694✔
336
    int32_t  totalMetaRows = 0;
3,694✔
337
    while (1) {
3,162,494✔
338
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
3,166,188✔
339
      if (savedEpoch > pRequest->epoch) {
3,167,318✔
340
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
341
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
342
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
343
        goto END;
×
344
      }
345

346
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
3,167,092✔
347
        if (totalMetaRows > 0) {
3,016✔
348
          SEND_BATCH_META_RSP
×
349
        }
350
        SEND_DATA_RSP
6,032✔
351
      }
352

353
      SWalCont* pHead = &pHandle->pWalReader->pHead->head;
3,185,772✔
354
      tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, vgId:%d offset %" PRId64 " msgType %s",
3,185,772✔
355
              pRequest->consumerId, pRequest->epoch, vgId, fetchVer, TMSG_INFO(pHead->msgType));
356

357
      // process meta
358
      if (pHead->msgType != TDMT_VND_SUBMIT) {
3,185,772✔
359
        if (totalRows > 0) {
×
360
          SEND_DATA_RSP
×
361
        }
362

363
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
×
364
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
×
365
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
×
366
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
×
367
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteAlterTable)
×
368
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
×
369
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
×
370
          } else if (pHead->msgType == TDMT_VND_DELETE) {
×
371
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
×
372
          }
373
        }
374

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

398
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
3,185,772✔
399
        SEND_BATCH_META_RSP
×
400
      }
401

402
      // process data
403
      SPackedData submit = {
9,557,316✔
404
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
3,185,772✔
405
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
3,185,772✔
406
          .ver = pHead->version,
3,185,772✔
407
      };
408

409
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
3,185,772✔
410

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

441
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
3,176,054✔
442
          (taosGetTimestampMs() - st > pRequest->timeout) ||
3,173,342✔
443
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
3,179,896✔
444
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
445
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
32,544✔
446
          terrno = 0;
×
447
        } else{
448
          fetchVer++;
678✔
449
        }
450
        SEND_DATA_RSP
1,356✔
451
      } else {
452
        fetchVer++;
3,160,008✔
453
      }
454
    }
455
  }
456

457
END:
3,694✔
458
  if (code != 0){
3,694✔
459
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
460
            pRequest->subKey);
461
  }
462
  tDeleteMqBatchMetaRsp(&btMetaRsp);
3,694✔
463
  tDeleteSTaosxRsp(&taosxRsp);
3,694✔
464
  return code;
3,694✔
465
}
466

467
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
181,029✔
468
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
181,029✔
469
    return TSDB_CODE_TMQ_INVALID_MSG;
×
470
  }
471
  int32_t      code = 0;
181,029✔
472
  STqOffsetVal reqOffset = {0};
181,029✔
473
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
181,029✔
474

475
  // reset the offset if needed
476
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
181,029✔
477
    bool blockReturned = false;
60,553✔
478
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
60,553✔
479
    if (code != 0) {
60,553✔
480
      goto END;
×
481
    }
482

483
    // empty block returned, quit
484
    if (blockReturned) {
60,553✔
485
      goto END;
3,643✔
486
    }
487
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
120,476✔
488
    uError("req offset type is 0");
×
489
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
490
    goto END;
×
491
  }
492

493
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
177,386✔
494
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
173,692✔
495
  } else {
496
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
3,694✔
497
  }
498

499
END:
181,029✔
500
  if (code != 0){
180,804✔
501
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
×
502
  }
503
  tOffsetDestroy(&reqOffset);
180,804✔
504
  return code;
181,029✔
505
}
506

507
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
181,029✔
508
                          int64_t ever) {
509
  if (pMsgHead == NULL) {
181,029✔
510
    return;
×
511
  }
512
  pMsgHead->consumerId = consumerId;
181,029✔
513
  pMsgHead->epoch = epoch;
181,029✔
514
  pMsgHead->mqMsgType = type;
181,029✔
515
  pMsgHead->walsver = sver;
181,029✔
516
  pMsgHead->walever = ever;
181,029✔
517
}
518

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

536
  int64_t sver = 0, ever = 0;
×
537
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
538
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
×
539

540
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
541

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

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

556
  return 0;
×
557
}
558

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

576
  int64_t sver = 0, ever = 0;
×
577
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
578
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
×
579

580
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
×
581

582
  SEncoder encoder = {0};
×
583
  tEncoderInit(&encoder, abuf, len);
×
584
  code = tEncodeMqMetaRsp(&encoder, pRsp);
×
585
  tEncoderClear(&encoder);
×
586
  if (code < 0) {
×
587
    rpcFreeCont(buf);
×
588
    return TAOS_GET_TERRNO(code);
×
589
  }
590

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

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

597
  return 0;
×
598
}
599

600
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
181,029✔
601
                        int32_t type, int64_t sver, int64_t ever) {
602
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
181,029✔
603
    return TSDB_CODE_TMQ_INVALID_MSG;
×
604
  }
605
  int32_t len = 0;
181,029✔
606
  int32_t code = 0;
181,029✔
607

608
  if (type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP){
181,029✔
609
    pRsp->withSchema = 0;
×
610
  }
611
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
181,029✔
612
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
613
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
614
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
181,029✔
615
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
616
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
×
617
  }
618

619
  if (code < 0) {
181,029✔
620
    return TAOS_GET_TERRNO(code);
×
621
  }
622

623
  int32_t tlen = sizeof(SMqRspHead) + len;
181,029✔
624
  void*   buf = rpcMallocCont(tlen);
181,029✔
625
  if (buf == NULL) {
181,029✔
626
    return terrno;
×
627
  }
628

629
  SMqRspHead* pHead = (SMqRspHead*)buf;
181,029✔
630
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
181,029✔
631

632
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
181,029✔
633

634
  SEncoder encoder = {0};
181,029✔
635
  tEncoderInit(&encoder, abuf, len);
181,029✔
636

637
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
181,029✔
638
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
639
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
640
    code = tEncodeMqDataRsp(&encoder, pRsp);
181,029✔
641
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
642
    code = tEncodeSTaosxRsp(&encoder, pRsp);
×
643
  }
644
  tEncoderClear(&encoder);
181,029✔
645
  if (code < 0) {
181,029✔
646
    rpcFreeCont(buf);
×
647
    return TAOS_GET_TERRNO(code);
×
648
  }
649
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
181,029✔
650

651
  tmsgSendRsp(&rsp);
181,029✔
652
  return 0;
181,029✔
653
}
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