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

taosdata / TDengine / #4870

26 Nov 2025 05:46AM UTC coverage: 64.545% (+0.006%) from 64.539%
#4870

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

768 of 945 new or added lines in 33 files covered. (81.27%)

2982 existing lines in 119 files now uncovered.

158219 of 245129 relevant lines covered (64.55%)

112474797.36 hits per line

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

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

29
  pRsp->blockData = taosArrayInit(0, sizeof(void*));
4,046,908✔
30
  TSDB_CHECK_NULL(pRsp->blockData, code, lino, END, terrno);
4,046,832✔
31

32
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
4,046,832✔
33
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
4,046,908✔
34

35
  tOffsetCopy(&pRsp->reqOffset, &pOffset);
4,046,839✔
36
  tOffsetCopy(&pRsp->rspOffset, &pOffset);
4,046,831✔
37
  pRsp->withTbName = 0;
4,046,908✔
38
  pRsp->withSchema = false;
4,046,908✔
39

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

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

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

60
  pRsp->blockDataLen = taosArrayInit(0, sizeof(int32_t));
1,219,499✔
61
  TSDB_CHECK_NULL(pRsp->blockDataLen, code, lino, END, terrno);
1,219,430✔
62

63
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
1,219,499✔
64
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
1,219,430✔
65

66
  pRsp->blockSchema = taosArrayInit(0, sizeof(void*));
1,219,407✔
67
  TSDB_CHECK_NULL(pRsp->blockSchema, code, lino, END, terrno);
1,219,430✔
68

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

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

90
  *pBlockReturned = false;
263,090✔
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) {
262,123✔
93
    tOffsetCopy(pOffsetVal, &pOffset->val);
59,248✔
94

95
    char formatBuf[TSDB_OFFSET_LEN] = {0};
59,248✔
96
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
59,248✔
97
    tqDebug("tmq poll: consumer:0x%" PRIx64
59,248✔
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;
59,248✔
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) {
202,875✔
104
      if (pRequest->useSnapshot) {
160,597✔
105
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
62,215✔
106
                consumerId, pHandle->subKey, vgId);
107
        if (pHandle->fetchMeta) {
62,539✔
108
          tqOffsetResetToMeta(pOffsetVal, 0);
109
        } else {
110
          SValue val = {0};
61,756✔
111
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
112
        }
113
      } else {
114
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
96,922✔
115
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
98,086✔
116
      }
117
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
42,938✔
118
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
8,827✔
119
      SMqDataRsp dataRsp = {0};
8,827✔
120
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
8,827✔
121

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

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

141
  return 0;
159,733✔
142
}
143

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

153
  SMqDataRsp dataRsp = {0};
3,963,115✔
154
  code = tqInitDataRsp(&dataRsp, *pOffset);
3,963,115✔
155
  TSDB_CHECK_CODE(code, lino, end);
3,963,038✔
156

157
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
3,963,038✔
158
  TSDB_CHECK_CODE(code, lino, end);
3,963,092✔
159

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
3,963,092✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
3,963,115✔
162
    goto end;
50,344✔
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,912,771✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
1,552,291✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
1,552,291✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
1,552,291✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
1,491,960✔
172
      taosWUnLockLatch(&pTq->lock);
1,491,960✔
173
      goto end;
1,491,960✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
60,331✔
176
  }
177

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

182
end:
3,962,957✔
183
  {
184
    char buf[TSDB_OFFSET_LEN] = {0};
3,963,115✔
185
    tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.rspOffset);
3,963,115✔
186
    if (code != 0){
3,962,911✔
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",
50,344✔
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,912,567✔
191
              consumerId, pHandle->subKey, vgId, dataRsp.blockNum, buf, pRequest->reqId);
192
    }
193

194
    tDeleteMqDataRsp(&dataRsp);
3,963,217✔
195
    return code;
3,962,970✔
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) {}
6,271✔
218
static void tDeleteAlterTable(SVAlterTbReq* req) {
1,012✔
219
  taosArrayDestroy(req->pMultiTag);
1,012✔
220
}
1,012✔
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){
69✔
227
  int32_t         code = 0;
69✔
228

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

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

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

262
END:
69✔
263
  return code;
69✔
264
}
265

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

288
END:
23✔
289
  taosArrayDestroy(pReq.pArray);
23✔
290
  return code;
23✔
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,
1,219,391✔
303
                                                  SRpcMsg* pMsg, STqOffsetVal* offset) {
304
  int32_t         vgId = TD_VID(pTq->pVnode);
1,219,391✔
305
  SMqDataRsp      taosxRsp = {0};
1,219,368✔
306
  SMqBatchMetaRsp btMetaRsp = {0};
1,219,268✔
307
  int32_t         code = 0;
1,219,356✔
308

309
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
1,219,356✔
310
  if (offset->type != TMQ_OFFSET__LOG) {
1,219,267✔
311
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
10,945✔
312

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

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

330
  if (offset->type == TMQ_OFFSET__LOG) {
1,208,574✔
331
    walReaderVerifyOffset(pHandle->pWalReader, offset);
1,208,783✔
332
    int64_t fetchVer = offset->version;
1,208,643✔
333

334
    uint64_t st = taosGetTimestampMs();
1,208,443✔
335
    int      totalRows = 0;
1,208,443✔
336
    int32_t  totalMetaRows = 0;
1,208,583✔
337
    while (1) {
16,098,309✔
338
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
17,306,892✔
339
      if (savedEpoch > pRequest->epoch) {
17,305,396✔
UNCOV
340
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
341
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
UNCOV
342
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
343
        goto END;
×
344
      }
345

346
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
17,303,477✔
347
        if (totalMetaRows > 0) {
1,168,182✔
348
          SEND_BATCH_META_RSP
92✔
349
        }
350
        SEND_DATA_RSP
2,336,202✔
351
      }
352

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

357
      // process meta
358
      if (pHead->msgType != TDMT_VND_SUBMIT) {
16,161,988✔
359
        if (totalRows > 0) {
13,013✔
360
          SEND_DATA_RSP
1,234✔
361
        }
362

363
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
12,396✔
364
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
11,952✔
365
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
3,795✔
366
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
8,157✔
367
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteAlterTable)
1,012✔
368
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
8,977✔
369
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
6,156✔
370
          } else if (pHead->msgType == TDMT_VND_DELETE) {
989✔
371
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
115✔
372
          }
373
        }
374

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

398
      if (totalMetaRows > 0 && pHandle->fetchMeta != ONLY_META) {
16,148,972✔
UNCOV
399
        SEND_BATCH_META_RSP
×
400
      }
401

402
      // process data
403
      SPackedData submit = {
48,446,704✔
404
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
16,148,972✔
405
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
16,148,917✔
406
          .ver = pHead->version,
16,148,917✔
407
      };
408

409
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
16,148,870✔
410

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

441
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
16,138,248✔
442
          (taosGetTimestampMs() - st > pRequest->timeout) ||
16,094,882✔
443
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
16,107,967✔
UNCOV
444
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
445
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
51,682✔
UNCOV
446
          terrno = 0;
×
447
        } else{
448
          fetchVer++;
35,362✔
449
        }
450
        SEND_DATA_RSP
70,724✔
451
      } else {
452
        fetchVer++;
16,088,683✔
453
      }
454
    }
455
  }
456

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

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

475
  // reset the offset if needed
476
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
5,225,438✔
477
    bool blockReturned = false;
263,312✔
478
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
263,381✔
479
    if (code != 0) {
262,932✔
480
      goto END;
34,416✔
481
    }
482

483
    // empty block returned, quit
484
    if (blockReturned) {
228,516✔
485
      goto END;
8,827✔
486
    }
487
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
4,962,367✔
UNCOV
488
    uError("req offset type is 0");
×
UNCOV
489
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
UNCOV
490
    goto END;
×
491
  }
492

493
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
5,181,832✔
494
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
3,962,980✔
495
  } else {
496
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
1,219,453✔
497
  }
498

499
END:
5,225,419✔
500
  if (code != 0){
5,224,923✔
501
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
84,760✔
502
  }
503
  tOffsetDestroy(&reqOffset);
5,224,923✔
504
  return code;
5,225,251✔
505
}
506

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

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

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

540
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
1,055✔
541

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

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

556
  return 0;
1,055✔
557
}
558

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

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

580
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
4,599✔
581

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

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

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

597
  return 0;
4,599✔
598
}
599

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

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

619
  if (code < 0) {
3,717,909✔
UNCOV
620
    return TAOS_GET_TERRNO(code);
×
621
  }
622

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

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

632
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3,717,892✔
633

634
  SEncoder encoder = {0};
3,717,962✔
635
  tEncoderInit(&encoder, abuf, len);
3,718,278✔
636

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

651
  tmsgSendRsp(&rsp);
3,717,256✔
652
  return 0;
3,717,941✔
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