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

taosdata / TDengine / #4867

26 Nov 2025 05:46AM UTC coverage: 64.473% (-0.03%) from 64.504%
#4867

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

765 of 945 new or added lines in 33 files covered. (80.95%)

3147 existing lines in 126 files now uncovered.

158042 of 245129 relevant lines covered (64.47%)

111495961.54 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) {
3,591,512✔
24
  int32_t    code = TDB_CODE_SUCCESS;
3,591,512✔
25
  int32_t    lino = 0;
3,591,512✔
26
  tqDebug("%s called", __FUNCTION__ );
3,591,512✔
27
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
3,591,536✔
28

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

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

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

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

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

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

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

63
  pRsp->blockTbName = taosArrayInit(0, sizeof(void*));
990,180✔
64
  TSDB_CHECK_NULL(pRsp->blockTbName, code, lino, END, terrno);
989,917✔
65

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

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

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

90
  *pBlockReturned = false;
231,244✔
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) {
230,261✔
93
    tOffsetCopy(pOffsetVal, &pOffset->val);
49,564✔
94

95
    char formatBuf[TSDB_OFFSET_LEN] = {0};
49,564✔
96
    tFormatOffset(formatBuf, TSDB_OFFSET_LEN, pOffsetVal);
49,564✔
97
    tqDebug("tmq poll: consumer:0x%" PRIx64
49,564✔
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;
49,564✔
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) {
180,697✔
104
      if (pRequest->useSnapshot) {
141,762✔
105
        tqDebug("tmq poll: consumer:0x%" PRIx64 ", subkey:%s, vgId:%d, (earliest) set offset to be snapshot",
56,104✔
106
                consumerId, pHandle->subKey, vgId);
107
        if (pHandle->fetchMeta) {
56,534✔
108
          tqOffsetResetToMeta(pOffsetVal, 0);
109
        } else {
110
          SValue val = {0};
55,516✔
111
          tqOffsetResetToData(pOffsetVal, 0, 0, val);
×
112
        }
113
      } else {
114
        walRefFirstVer(pTq->pVnode->pWal, pHandle->pRef);
85,141✔
115
        tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer);
85,504✔
116
      }
117
    } else if (pRequest->reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
39,688✔
118
      walRefLastVer(pTq->pVnode->pWal, pHandle->pRef);
8,455✔
119
      SMqDataRsp dataRsp = {0};
8,455✔
120
      tqOffsetResetToLog(pOffsetVal, pHandle->pRef->refVer + 1);
8,455✔
121

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

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

141
  return 0;
141,814✔
142
}
143

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

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

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

160
  code = tqScanData(pTq, pHandle, &dataRsp, pOffset, pRequest);
3,509,410✔
161
  if (code != 0 && terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
3,509,452✔
162
    goto end;
45,431✔
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,464,021✔
167
    // lock
168
    taosWLockLatch(&pTq->lock);
1,367,847✔
169
    int64_t ver = walGetCommittedVer(pTq->pVnode->pWal);
1,367,847✔
170
    if (dataRsp.rspOffset.version > ver) {  // check if there are data again to avoid lost data
1,367,847✔
171
      code = tqRegisterPushHandle(pTq, pHandle, pMsg);
1,319,545✔
172
      taosWUnLockLatch(&pTq->lock);
1,319,545✔
173
      goto end;
1,319,545✔
174
    }
175
    taosWUnLockLatch(&pTq->lock);
48,302✔
176
  }
177

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

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

194
    tDeleteMqDataRsp(&dataRsp);
3,509,452✔
195
    return code;
3,509,433✔
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) {}
5,438✔
218
static void tDeleteAlterTable(SVAlterTbReq* req) {
880✔
219
  taosArrayDestroy(req->pMultiTag);
880✔
220
}
880✔
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){
60✔
227
  int32_t         code = 0;
60✔
228

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

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

255
  if (code < 0) {
60✔
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));
120✔
260
  TQ_NULL_GO_TO_END (taosArrayPush(btMetaRsp->batchMetaLen, &tLen));
120✔
261

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

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

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

309
  TQ_ERR_GO_TO_END(tqInitTaosxRsp(&taosxRsp, *offset));
990,029✔
310
  if (offset->type != TMQ_OFFSET__LOG) {
989,946✔
311
    TQ_ERR_GO_TO_END(tqScanTaosx(pTq, pHandle, &taosxRsp, &btMetaRsp, offset, pRequest));
8,926✔
312

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

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

330
  if (offset->type == TMQ_OFFSET__LOG) {
981,422✔
331
    walReaderVerifyOffset(pHandle->pWalReader, offset);
981,596✔
332
    int64_t fetchVer = offset->version;
981,172✔
333

334
    uint64_t st = taosGetTimestampMs();
981,150✔
335
    int      totalRows = 0;
981,150✔
336
    int32_t  totalMetaRows = 0;
981,139✔
337
    while (1) {
13,864,226✔
338
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
14,845,365✔
339
      if (savedEpoch > pRequest->epoch) {
14,819,372✔
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) {
14,816,072✔
347
        if (totalMetaRows > 0) {
944,616✔
348
          SEND_BATCH_META_RSP
80✔
349
        }
350
        SEND_DATA_RSP
1,889,152✔
351
      }
352

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

357
      // process meta
358
      if (pHead->msgType != TDMT_VND_SUBMIT) {
13,935,068✔
359
        if (totalRows > 0) {
11,232✔
360
          SEND_DATA_RSP
1,048✔
361
        }
362

363
        if ((pRequest->sourceExcluded & TD_REQ_FROM_TAOX) != 0) {
10,708✔
364
          if (pHead->msgType == TDMT_VND_CREATE_TABLE) {
10,378✔
365
            PROCESS_EXCLUDED_MSG(SVCreateTbBatchReq, tDecodeSVCreateTbBatchReq, tDeleteSVCreateTbBatchReq)
3,300✔
366
          } else if (pHead->msgType == TDMT_VND_ALTER_TABLE) {
7,078✔
367
            PROCESS_EXCLUDED_MSG(SVAlterTbReq, tDecodeSVAlterTbReq, tDeleteAlterTable)
880✔
368
          } else if (pHead->msgType == TDMT_VND_CREATE_STB || pHead->msgType == TDMT_VND_ALTER_STB) {
7,776✔
369
            PROCESS_EXCLUDED_MSG(SVCreateStbReq, tDecodeSVCreateStbReq, tDeleteCommon)
5,338✔
370
          } else if (pHead->msgType == TDMT_VND_DELETE) {
860✔
371
            PROCESS_EXCLUDED_MSG(SDeleteRes, tDecodeDeleteRes, tDeleteCommon)
100✔
372
          }
373
        }
374

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

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

402
      // process data
403
      SPackedData submit = {
41,770,504✔
404
          .msgStr = POINTER_SHIFT(pHead->body, sizeof(SSubmitReq2Msg)),
13,923,165✔
405
          .msgLen = pHead->bodyLen - sizeof(SSubmitReq2Msg),
13,923,698✔
406
          .ver = pHead->version,
13,923,610✔
407
      };
408

409
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
13,923,196✔
410

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

441
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
13,900,841✔
442
          (taosGetTimestampMs() - st > pRequest->timeout) ||
13,859,305✔
443
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
13,874,878✔
UNCOV
444
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
445
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
54,691✔
UNCOV
446
          terrno = 0;
×
447
        } else{
448
          fetchVer++;
32,508✔
449
        }
450
        SEND_DATA_RSP
65,016✔
451
      } else {
452
        fetchVer++;
13,852,194✔
453
      }
454
    }
455
  }
456

457
END:
989,512✔
458
  if (code != 0){
989,605✔
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);
989,605✔
463
  tDeleteSTaosxRsp(&taosxRsp);
989,835✔
464
  return code;
988,738✔
465
}
466

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

475
  // reset the offset if needed
476
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
4,539,334✔
477
    bool blockReturned = false;
231,318✔
478
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
231,318✔
479
    if (code != 0) {
231,136✔
480
      goto END;
31,412✔
481
    }
482

483
    // empty block returned, quit
484
    if (blockReturned) {
199,724✔
485
      goto END;
8,455✔
486
    }
487
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
4,308,112✔
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) {
4,499,495✔
494
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
3,509,452✔
495
  } else {
496
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
990,118✔
497
  }
498

499
END:
4,539,150✔
500
  if (code != 0){
4,538,945✔
501
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
76,843✔
502
  }
503
  tOffsetDestroy(&reqOffset);
4,538,945✔
504
  return code;
4,539,203✔
505
}
506

507
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
3,215,875✔
508
                          int64_t ever) {
509
  if (pMsgHead == NULL) {
3,215,875✔
UNCOV
510
    return;
×
511
  }
512
  pMsgHead->consumerId = consumerId;
3,215,875✔
513
  pMsgHead->epoch = epoch;
3,216,182✔
514
  pMsgHead->mqMsgType = type;
3,216,108✔
515
  pMsgHead->walsver = sver;
3,215,922✔
516
  pMsgHead->walever = ever;
3,215,833✔
517
}
518

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

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

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

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

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

556
  return 0;
956✔
557
}
558

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

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

580
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3,928✔
581

582
  SEncoder encoder = {0};
3,928✔
583
  tEncoderInit(&encoder, abuf, len);
3,928✔
584
  code = tEncodeMqMetaRsp(&encoder, pRsp);
3,928✔
585
  tEncoderClear(&encoder);
3,928✔
586
  if (code < 0) {
3,928✔
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};
3,928✔
592

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

597
  return 0;
3,928✔
598
}
599

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

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

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

623
  int32_t tlen = sizeof(SMqRspHead) + len;
3,210,880✔
624
  void*   buf = rpcMallocCont(tlen);
3,210,880✔
625
  if (buf == NULL) {
3,211,179✔
UNCOV
626
    return terrno;
×
627
  }
628

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

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

634
  SEncoder encoder = {0};
3,211,395✔
635
  tEncoderInit(&encoder, abuf, len);
3,211,775✔
636

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

651
  tmsgSendRsp(&rsp);
3,210,258✔
652
  return 0;
3,211,500✔
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