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

taosdata / TDengine / #4653

06 Aug 2025 01:23AM UTC coverage: 60.5% (-0.5%) from 60.986%
#4653

push

travis-ci

web-flow
fix: [TD-37190]: disable ignore_nodata_trigger when window type is not interval/sliding or period. (#32405)

138499 of 291677 branches covered (47.48%)

Branch coverage included in aggregate %.

52 of 106 new or added lines in 1 file covered. (49.06%)

1648 existing lines in 119 files now uncovered.

209836 of 284084 relevant lines covered (73.86%)

4732834.84 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

90
  *pBlockReturned = false;
2,505✔
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) {
2,505✔
93
    tOffsetCopy(pOffsetVal, &pOffset->val);
167✔
94

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

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

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

141
  return 0;
1,635✔
142
}
143

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

153
  SMqDataRsp dataRsp = {0};
33,082✔
154
  code = tqInitDataRsp(&dataRsp, *pOffset);
33,082✔
155
  TSDB_CHECK_CODE(code, lino, end);
33,081!
156

157
  code = qSetTaskId(pHandle->execHandle.task, consumerId, pRequest->reqId);
33,081✔
158
  TSDB_CHECK_CODE(code, lino, end);
33,082!
159

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

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

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

194
    tDeleteMqDataRsp(&dataRsp);
33,083✔
195
    return code;
33,082✔
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) {}
4✔
218

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

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

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

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

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

259
END:
×
260
  return code;
×
261
}
262

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

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

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

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

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

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

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

327
  if (offset->type == TMQ_OFFSET__LOG) {
10,519!
328
    walReaderVerifyOffset(pHandle->pWalReader, offset);
10,519✔
329
    int64_t fetchVer = offset->version;
10,519✔
330

331
    uint64_t st = taosGetTimestampMs();
10,519✔
332
    int      totalRows = 0;
10,519✔
333
    int32_t  totalMetaRows = 0;
10,519✔
334
    while (1) {
135,912✔
335
      int32_t savedEpoch = atomic_load_32(&pHandle->epoch);
146,431✔
336
      if (savedEpoch > pRequest->epoch) {
146,299!
337
        tqError("tmq poll: consumer:0x%" PRIx64 " (epoch %d) iter log, savedEpoch error, vgId:%d offset %" PRId64,
×
338
                pRequest->consumerId, pRequest->epoch, vgId, fetchVer);
339
        code = TSDB_CODE_TQ_INTERNAL_ERROR;
×
340
        goto END;
10,517✔
341
      }
342

343
      if (tqFetchLog(pTq, pHandle, &fetchVer, pRequest->reqId) < 0) {
146,299✔
344
        if (totalMetaRows > 0) {
10,153!
345
          SEND_BATCH_META_RSP
×
346
        }
347
        SEND_DATA_RSP
10,153!
348
      }
349

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

354
      // process meta
355
      if (pHead->msgType != TDMT_VND_SUBMIT) {
136,480✔
356
        if (totalRows > 0) {
4!
357
          SEND_DATA_RSP
×
358
        }
359

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

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

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

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

406
      TQ_ERR_GO_TO_END(tqTaosxScanLog(pTq, pHandle, submit, &taosxRsp, &totalRows, pRequest));
136,476!
407

408
      if (pHandle->fetchMeta == ONLY_META && taosArrayGetSize(taosxRsp.createTableReq) > 0){
136,219!
409
        int32_t len = 0;
×
410
        void *pBuf = NULL;
×
411
        code = buildCreateTbBatchReqBinary(&taosxRsp, &pBuf, &len);
×
412
        if (code == 0){
×
413
          code = buildBatchMeta(&btMetaRsp, TDMT_VND_CREATE_TABLE, len, pBuf);
×
414
        }
415
        taosMemoryFree(pBuf);
×
416
        taosArrayDestroyP(taosxRsp.createTableReq, NULL);
×
417
        taosxRsp.createTableReq = NULL;
×
418
        fetchVer++;
×
419
        if (code != 0){
×
420
          goto END;
×
421
        }
422
        totalMetaRows++;
×
423
        if ((taosArrayGetSize(btMetaRsp.batchMetaReq) >= tmqRowSize) ||
×
424
            (taosGetTimestampMs() - st > pRequest->timeout) ||
×
425
            (!pRequest->enableBatchMeta && !pRequest->useSnapshot)) {
×
426
          SEND_BATCH_META_RSP
×
427
        }
428
        continue;
×
429
      }
430

431
      if ((pRequest->rawData == 0 && totalRows >= pRequest->minPollRows) ||
136,219!
432
          (taosGetTimestampMs() - st > pRequest->timeout) ||
135,819!
433
          (pRequest->rawData != 0 && (taosArrayGetSize(taosxRsp.blockData) > pRequest->minPollRows ||
135,953!
434
                                      terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT))) {
×
435
        if (terrno == TSDB_CODE_TMQ_RAW_DATA_SPLIT){
219!
436
          terrno = 0;
×
437
        } else{
438
          fetchVer++;
362✔
439
        }
440
        SEND_DATA_RSP
362!
441
      } else {
442
        fetchVer++;
135,912✔
443
      }
444
    }
445
  }
446

447
END:
×
448
  if (code != 0){
10,642!
449
    tqError("tmq poll: tqTaosxScanLog error. consumerId:0x%" PRIx64 ", in vgId:%d, subkey %s", pRequest->consumerId, vgId,
×
450
            pRequest->subKey);
451
  }
452
  tDeleteMqBatchMetaRsp(&btMetaRsp);
10,642✔
453
  tDeleteSTaosxRsp(&taosxRsp);
10,642✔
454
  return code;
10,643✔
455
}
456

457
int32_t tqExtractDataForMq(STQ* pTq, STqHandle* pHandle, const SMqPollReq* pRequest, SRpcMsg* pMsg) {
44,428✔
458
  if (pTq == NULL || pHandle == NULL || pRequest == NULL || pMsg == NULL) {
44,428!
459
    return TSDB_CODE_TMQ_INVALID_MSG;
×
460
  }
461
  int32_t      code = 0;
44,429✔
462
  STqOffsetVal reqOffset = {0};
44,429✔
463
  tOffsetCopy(&reqOffset, &pRequest->reqOffset);
44,429✔
464

465
  // reset the offset if needed
466
  if (IS_OFFSET_RESET_TYPE(pRequest->reqOffset.type)) {
44,429✔
467
    bool blockReturned = false;
2,506✔
468
    code = extractResetOffsetVal(&reqOffset, pTq, pHandle, pRequest, pMsg, &blockReturned);
2,506✔
469
    if (code != 0) {
2,505✔
470
      goto END;
703✔
471
    }
472

473
    // empty block returned, quit
474
    if (blockReturned) {
1,857✔
475
      goto END;
55✔
476
    }
477
  } else if (reqOffset.type == 0) {  // use the consumer specified offset
41,923!
478
    uError("req offset type is 0");
×
479
    code = TSDB_CODE_TMQ_INVALID_MSG;
×
480
    goto END;
×
481
  }
482

483
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
43,725✔
484
    code = extractDataAndRspForNormalSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
33,081✔
485
  } else {
486
    code = extractDataAndRspForDbStbSubscribe(pTq, pHandle, pRequest, pMsg, &reqOffset);
10,644✔
487
  }
488

489
END:
44,422✔
490
  if (code != 0){
44,422✔
491
    uError("failed to extract data for mq, msg:%s", tstrerror(code));
1,324!
492
  }
493
  tOffsetDestroy(&reqOffset);
44,422✔
494
  return code;
44,427✔
495
}
496

497
static void initMqRspHead(SMqRspHead* pMsgHead, int32_t type, int32_t epoch, int64_t consumerId, int64_t sver,
30,487✔
498
                          int64_t ever) {
499
  if (pMsgHead == NULL) {
30,487!
500
    return;
×
501
  }
502
  pMsgHead->consumerId = consumerId;
30,487✔
503
  pMsgHead->epoch = epoch;
30,487✔
504
  pMsgHead->mqMsgType = type;
30,487✔
505
  pMsgHead->walsver = sver;
30,487✔
506
  pMsgHead->walever = ever;
30,487✔
507
}
508

509
int32_t tqSendBatchMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq,
3✔
510
                               const SMqBatchMetaRsp* pRsp, int32_t vgId) {
511
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
3!
512
    return TSDB_CODE_TMQ_INVALID_MSG;
×
513
  }
514
  int32_t len = 0;
3✔
515
  int32_t code = 0;
3✔
516
  tEncodeSize(tEncodeMqBatchMetaRsp, pRsp, len, code);
3!
517
  if (code < 0) {
3!
518
    return TAOS_GET_TERRNO(code);
×
519
  }
520
  int32_t tlen = sizeof(SMqRspHead) + len;
3✔
521
  void*   buf = rpcMallocCont(tlen);
3✔
522
  if (buf == NULL) {
3!
523
    return TAOS_GET_TERRNO(terrno);
×
524
  }
525

526
  int64_t sver = 0, ever = 0;
3✔
527
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
3✔
528
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_BATCH_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
3✔
529

530
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
3✔
531

532
  SEncoder encoder = {0};
3✔
533
  tEncoderInit(&encoder, abuf, len);
3✔
534
  code = tEncodeMqBatchMetaRsp(&encoder, pRsp);
3✔
535
  tEncoderClear(&encoder);
3✔
536
  if (code < 0) {
3!
537
    rpcFreeCont(buf);
×
538
    return TAOS_GET_TERRNO(code);
×
539
  }
540
  SRpcMsg resp = {.info = pMsg->info, .pCont = buf, .contLen = tlen, .code = 0};
3✔
541

542
  tmsgSendRsp(&resp);
3✔
543
  tqDebug("vgId:%d, from consumer:0x%" PRIx64 " (epoch %d) send rsp, res msg type: batch meta, size:%ld offset type:%d",
3!
544
          vgId, pReq->consumerId, pReq->epoch, taosArrayGetSize(pRsp->batchMetaReq), pRsp->rspOffset.type);
545

546
  return 0;
3✔
547
}
548

549
int32_t tqSendMetaPollRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, const SMqMetaRsp* pRsp,
4✔
550
                          int32_t vgId) {
551
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
4!
552
    return TSDB_CODE_TMQ_INVALID_MSG;
×
553
  }
554
  int32_t len = 0;
4✔
555
  int32_t code = 0;
4✔
556
  tEncodeSize(tEncodeMqMetaRsp, pRsp, len, code);
4!
557
  if (code < 0) {
4!
558
    return TAOS_GET_TERRNO(code);
×
559
  }
560
  int32_t tlen = sizeof(SMqRspHead) + len;
4✔
561
  void*   buf = rpcMallocCont(tlen);
4✔
562
  if (buf == NULL) {
4!
563
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
564
  }
565

566
  int64_t sver = 0, ever = 0;
4✔
567
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
4✔
568
  initMqRspHead(buf, TMQ_MSG_TYPE__POLL_META_RSP, pReq->epoch, pReq->consumerId, sver, ever);
4✔
569

570
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
4✔
571

572
  SEncoder encoder = {0};
4✔
573
  tEncoderInit(&encoder, abuf, len);
4✔
574
  code = tEncodeMqMetaRsp(&encoder, pRsp);
4✔
575
  tEncoderClear(&encoder);
4✔
576
  if (code < 0) {
4!
577
    rpcFreeCont(buf);
×
578
    return TAOS_GET_TERRNO(code);
×
579
  }
580

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

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

587
  return 0;
4✔
588
}
589

590
int32_t tqDoSendDataRsp(const SRpcHandleInfo* pRpcHandleInfo, SMqDataRsp* pRsp, int32_t epoch, int64_t consumerId,
30,480✔
591
                        int32_t type, int64_t sver, int64_t ever) {
592
  if (pRpcHandleInfo == NULL || pRsp == NULL) {
30,480!
593
    return TSDB_CODE_TMQ_INVALID_MSG;
×
594
  }
595
  int32_t len = 0;
30,480✔
596
  int32_t code = 0;
30,480✔
597

598
  if (type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP){
30,480!
599
    pRsp->withSchema = 0;
×
600
  }
601
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
30,480!
UNCOV
602
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
603
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
604
    tEncodeSize(tEncodeMqDataRsp, pRsp, len, code);
30,480!
UNCOV
605
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
UNCOV
606
    tEncodeSize(tEncodeSTaosxRsp, pRsp, len, code);
×
607
  }
608

609
  if (code < 0) {
30,479!
610
    return TAOS_GET_TERRNO(code);
×
611
  }
612

613
  int32_t tlen = sizeof(SMqRspHead) + len;
30,479✔
614
  void*   buf = rpcMallocCont(tlen);
30,479✔
615
  if (buf == NULL) {
30,480!
616
    return terrno;
×
617
  }
618

619
  SMqRspHead* pHead = (SMqRspHead*)buf;
30,480✔
620
  initMqRspHead(pHead, type, epoch, consumerId, sver, ever);
30,480✔
621

622
  void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead));
30,478✔
623

624
  SEncoder encoder = {0};
30,478✔
625
  tEncoderInit(&encoder, abuf, len);
30,478✔
626

627
  if (type == TMQ_MSG_TYPE__POLL_DATA_RSP ||
30,478!
UNCOV
628
      type == TMQ_MSG_TYPE__WALINFO_RSP ||
×
629
      type == TMQ_MSG_TYPE__POLL_RAW_DATA_RSP) {
630
    code = tEncodeMqDataRsp(&encoder, pRsp);
30,478✔
UNCOV
631
  } else if (type == TMQ_MSG_TYPE__POLL_DATA_META_RSP) {
×
UNCOV
632
    code = tEncodeSTaosxRsp(&encoder, pRsp);
×
633
  }
634
  tEncoderClear(&encoder);
30,478✔
635
  if (code < 0) {
30,476!
636
    rpcFreeCont(buf);
×
637
    return TAOS_GET_TERRNO(code);
×
638
  }
639
  SRpcMsg rsp = {.info = *pRpcHandleInfo, .pCont = buf, .contLen = tlen, .code = 0};
30,476✔
640

641
  tmsgSendRsp(&rsp);
30,476✔
642
  return 0;
30,477✔
643
}
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