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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

75.28
/source/dnode/vnode/src/tq/tq.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
#include <string.h>
18
#include "osDef.h"
19
#include "taoserror.h"
20
#include "stream.h"
21
#include "vnd.h"
22

23
// 0: not init
24
// 1: already inited
25
// 2: wait to be inited or cleanup
26
static int32_t tqInitialize(STQ* pTq);
27

28
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
26,134,911✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
24,893,932✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
24,902,655✔
31
#define MAX_LOOP 100
32

33
void tqDestroyTqHandle(void* data) {
432,386✔
34
  if (data == NULL) return;
432,386✔
35
  STqHandle* pData = (STqHandle*)data;
432,386✔
36
  qDestroyTask(pData->execHandle.task);
432,386✔
37

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
431,251✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
319,491✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
319,552✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
112,324✔
42
    tqReaderClose(pData->execHandle.pTqReader);
90,765✔
43
    walCloseReader(pData->pWalReader);
90,765✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
90,765✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
21,814✔
46
    walCloseReader(pData->pWalReader);
21,814✔
47
    tqReaderClose(pData->execHandle.pTqReader);
21,814✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
21,814✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
21,814✔
50
  }
51
  if (pData->msg != NULL) {
431,815✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
431,560✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
431,815✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
419,976✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
432,070✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
3,092,285✔
66
  if (pLeft == NULL || pRight == NULL) {
3,092,285✔
67
    return false;
302✔
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
5,905,920✔
70
         pLeft->val.version == pRight->val.version;
2,814,219✔
71
}
72

73
int32_t tqOpen(const char* path, SVnode* pVnode) {
3,727,422✔
74
  if (path == NULL || pVnode == NULL) {
3,727,422✔
75
    return TSDB_CODE_INVALID_PARA;
×
76
  }
77

78
  bool ignoreTq = pVnode->mounted && !taosCheckExistFile(path);
3,730,291✔
79
  if (ignoreTq) {
3,730,291✔
80
    return 0;
×
81
  }
82

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,730,291✔
84
  if (pTq == NULL) {
3,730,246✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,730,246✔
89
  pTq->pVnode = pVnode;
3,729,515✔
90

91
  pTq->path = taosStrdup(path);
3,729,984✔
92
  if (pTq->path == NULL) {
3,729,298✔
93
    return terrno;
×
94
  }
95

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,728,567✔
97
  if (pTq->pHandle == NULL) {
3,730,291✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,730,291✔
101

102
  taosInitRWLatch(&pTq->lock);
3,730,246✔
103

104
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
3,730,291✔
105
  if (pTq->pPushMgr == NULL) {
3,730,291✔
106
    return terrno;
×
107
  }
108

109
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
3,730,291✔
110
  if (pTq->pOffset == NULL) {
3,730,291✔
111
    return terrno;
×
112
  }
113
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
3,729,993✔
114

115
  return tqInitialize(pTq);
3,730,291✔
116
}
117

118
int32_t tqInitialize(STQ* pTq) {
3,730,291✔
119
  if (pTq == NULL) {
3,730,291✔
120
    return TSDB_CODE_INVALID_PARA;
×
121
  }
122

123
  return tqMetaOpen(pTq);
3,730,291✔
124
}
125

126
void tqClose(STQ* pTq) {
3,730,291✔
127
  qDebug("start to close tq");
3,730,291✔
128
  if (pTq == NULL) {
3,730,291✔
129
    return;
×
130
  }
131

132
  int32_t vgId = 0;
3,730,291✔
133
  if (pTq->pVnode != NULL) {
3,730,291✔
134
    vgId = TD_VID(pTq->pVnode);
3,730,291✔
135
  }
136

137
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
3,730,291✔
138
  while (pIter) {
3,740,895✔
139
    STqHandle* pHandle = *(STqHandle**)pIter;
10,604✔
140
    if (pHandle->msg != NULL) {
10,604✔
141
      tqPushEmptyDataRsp(pHandle, vgId);
10,604✔
142
      rpcFreeCont(pHandle->msg->pCont);
10,604✔
143
      taosMemoryFree(pHandle->msg);
10,604✔
144
      pHandle->msg = NULL;
10,604✔
145
    }
146
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
10,604✔
147
  }
148

149
  taosHashCleanup(pTq->pHandle);
3,730,291✔
150
  taosHashCleanup(pTq->pPushMgr);
3,730,291✔
151
  taosHashCleanup(pTq->pOffset);
3,730,291✔
152
  taosMemoryFree(pTq->path);
3,730,291✔
153
  tqMetaClose(pTq);
3,730,025✔
154
  qDebug("vgId:%d end to close tq", vgId);
3,730,024✔
155

156
  taosMemoryFree(pTq);
3,730,560✔
157
}
158

159
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
151,320✔
160
  if (pHandle == NULL) {
151,320✔
161
    return;
×
162
  }
163
  int32_t    code = 0;
151,320✔
164
  SMqPollReq req = {0};
151,320✔
165
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
151,320✔
166
  if (code < 0) {
151,320✔
167
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
168
    return;
×
169
  }
170

171
  SMqDataRsp dataRsp = {0};
151,320✔
172
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
151,320✔
173
  if (code != 0) {
151,320✔
174
    tqError("tqInitDataRsp failed, code:%d", code);
×
175
    return;
×
176
  }
177
  dataRsp.blockNum = 0;
151,320✔
178
  char buf[TSDB_OFFSET_LEN] = {0};
151,320✔
179
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.reqOffset);
151,320✔
180
  tqInfo("tqPushEmptyDataRsp to consumer:0x%" PRIx64 " vgId:%d, offset:%s, QID:0x%" PRIx64, req.consumerId, vgId, buf,
151,320✔
181
         req.reqId);
182

183
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
151,320✔
184
  if (code != 0) {
151,320✔
185
    tqError("tqSendDataRsp failed, code:%d", code);
×
186
  }
187
  tDeleteMqDataRsp(&dataRsp);
151,320✔
188
}
189

190
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
16,335,975✔
191
                      int32_t vgId) {
192
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
16,335,975✔
UNCOV
193
    return TSDB_CODE_INVALID_PARA;
×
194
  }
195
  int64_t sver = 0, ever = 0;
16,335,975✔
196
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
16,335,975✔
197

198
  char buf1[TSDB_OFFSET_LEN] = {0};
16,335,975✔
199
  char buf2[TSDB_OFFSET_LEN] = {0};
16,335,975✔
200
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
16,335,975✔
201
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
16,335,975✔
202

203
  tqDebug("tmq poll vgId:%d consumer:0x%" PRIx64 " (epoch %d) start to send rsp, block num:%d, req:%s, rsp:%s, QID:0x%" PRIx64,
16,335,975✔
204
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
205

206
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
16,335,975✔
207
}
208

209
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3,368,838✔
210
  if (pTq == NULL) {
3,368,838✔
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213
  SMqVgOffset vgOffset = {0};
3,368,838✔
214
  int32_t     vgId = TD_VID(pTq->pVnode);
3,368,838✔
215

216
  int32_t  code = 0;
3,368,838✔
217
  SDecoder decoder;
3,368,784✔
218
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
3,368,838✔
219
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
3,368,838✔
220
    code = TSDB_CODE_INVALID_MSG;
×
221
    goto end;
×
222
  }
223

224
  tDecoderClear(&decoder);
3,368,838✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
3,368,838✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
3,368,838✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
288,635✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
3,080,203✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
3,080,536✔
233
            pOffset->val.version);
234
  } else {
UNCOV
235
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
236
    code = TSDB_CODE_INVALID_MSG;
×
237
    goto end;
×
238
  }
239

240
  STqOffset* pSavedOffset = NULL;
3,369,055✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
3,368,506✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
3,367,625✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
885✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
885✔
246
  }
247

248
  // if (pOffset->val.type == TMQ_OFFSET__LOG && vgOffset.markWal) {
249
  //   int32_t ret = walSetKeepVersion(pTq->pVnode->pWal, pOffset->val.version);
250
  //   tqDebug("set wal reader keep version to %" PRId64 " for vgId:%d sub:%s, code:%d", pOffset->val.version, vgId,
251
  //           pOffset->subKey, ret);
252
  // }
253
  // save the new offset value
254
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
3,366,458✔
255
  if (code != 0) {
3,366,370✔
256
    goto end;
×
257
  }
258

259
  return 0;
3,366,370✔
260
end:
885✔
261
  tOffsetDestroy(&vgOffset.offset.val);
885✔
262
  return code;
885✔
263
}
264

265
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
2,974✔
266
  if (pTq == NULL || pMsg == NULL) {
2,974✔
267
    return TSDB_CODE_INVALID_PARA;
×
268
  }
269
  SMqSeekReq req = {0};
2,974✔
270
  int32_t    vgId = TD_VID(pTq->pVnode);
2,974✔
271
  SRpcMsg    rsp = {.info = pMsg->info};
2,974✔
272
  int        code = 0;
2,974✔
273

274
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
2,974✔
275
    code = TSDB_CODE_OUT_OF_MEMORY;
×
276
    goto end;
×
277
  }
278

279
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
2,974✔
280
  taosWLockLatch(&pTq->lock);
2,974✔
281

282
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
2,974✔
283
  if (pHandle == NULL) {
2,974✔
284
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
285
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
286
    taosWUnLockLatch(&pTq->lock);
×
287
    goto end;
×
288
  }
289

290
  // 2. check consumer-vg assignment status
291
  if (pHandle->consumerId != req.consumerId) {
2,974✔
292
    tqError("ERROR tmq seek, consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
293
            req.consumerId, vgId, req.subKey, pHandle->consumerId);
294
    taosWUnLockLatch(&pTq->lock);
×
295
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
296
    goto end;
×
297
  }
298

299
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
300
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
301
  tqUnregisterPushHandle(pTq, pHandle);
2,974✔
302
  taosWUnLockLatch(&pTq->lock);
2,974✔
303

304
end:
2,974✔
305
  rsp.code = code;
2,974✔
306
  tmsgSendRsp(&rsp);
2,974✔
307
  return 0;
2,974✔
308
}
309

310
int32_t tqProcessPollPush(STQ* pTq) {
7,761,610✔
311
  if (pTq == NULL) {
7,761,610✔
312
    return TSDB_CODE_INVALID_PARA;
×
313
  }
314
  int32_t vgId = TD_VID(pTq->pVnode);
7,761,610✔
315
  taosWLockLatch(&pTq->lock);
7,761,610✔
316
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
7,761,610✔
317
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
7,705,955✔
318

319
    while (pIter) {
15,926,468✔
320
      STqHandle* pHandle = *(STqHandle**)pIter;
8,220,513✔
321
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
8,220,513✔
322

323
      if (pHandle->msg == NULL) {
8,220,513✔
324
        tqError("pHandle->msg should not be null");
×
325
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
326
        break;
×
327
      } else {
328
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
16,441,026✔
329
                       .pCont = pHandle->msg->pCont,
8,220,513✔
330
                       .contLen = pHandle->msg->contLen,
8,220,513✔
331
                       .info = pHandle->msg->info};
8,220,513✔
332
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
8,220,513✔
333
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
334
        }
335
        taosMemoryFree(pHandle->msg);
8,220,513✔
336
        pHandle->msg = NULL;
8,220,513✔
337
      }
338

339
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
8,220,513✔
340
    }
341

342
    taosHashClear(pTq->pPushMgr);
7,705,955✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
7,761,610✔
345
  return 0;
7,761,610✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
24,900,359✔
349
  if (pTq == NULL || pMsg == NULL) {
24,900,359✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
24,902,527✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
24,902,526✔
354
  if (code < 0) {
24,887,597✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
24,887,597✔
360
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
361
    if (req.uidHash == NULL) {
×
362
      tqError("tq poll rawData taosHashInit failed");
×
363
      code = terrno;
×
364
      goto END;
×
365
    }
366
  }
367
  int64_t      consumerId = req.consumerId;
24,887,597✔
368
  int32_t      reqEpoch = req.epoch;
24,887,597✔
369
  STqOffsetVal reqOffset = req.reqOffset;
24,887,597✔
370
  int32_t      vgId = TD_VID(pTq->pVnode);
24,899,987✔
371
  STqHandle*   pHandle = NULL;
24,901,950✔
372

373
  while (1) {
26,572✔
374
    taosWLockLatch(&pTq->lock);
24,928,391✔
375
    // 1. find handle
376
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
24,927,854✔
377
    if (code != TDB_CODE_SUCCESS) {
24,923,281✔
378
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
379
      taosWUnLockLatch(&pTq->lock);
×
380
      return code;
×
381
    }
382

383
    // 2. check rebalance status
384
    if (pHandle->consumerId != consumerId) {
24,923,281✔
385
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
262✔
386
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
387
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
388
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
262✔
389
      taosWUnLockLatch(&pTq->lock);
262✔
390
      goto END;
262✔
391
    }
392

393
    bool exec = tqIsHandleExec(pHandle);
24,923,928✔
394
    if (!exec) {
24,923,536✔
395
      tqSetHandleExec(pHandle);
24,893,932✔
396
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
397
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
24,894,116✔
398
              req.subKey, pHandle);
399
      taosWUnLockLatch(&pTq->lock);
24,903,836✔
400
      break;
24,905,424✔
401
    }
402
    taosWUnLockLatch(&pTq->lock);
29,604✔
403

404
    tqDebug("tmq poll: consumer:0x%" PRIx64
26,572✔
405
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
406
            consumerId, vgId, req.subKey, pHandle);
407
    taosMsleep(10);
26,572✔
408
  }
409

410
  // 3. update the epoch value
411
  if (pHandle->epoch < reqEpoch) {
24,905,424✔
412
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
435,406✔
413
            reqEpoch);
414
    pHandle->epoch = reqEpoch;
435,406✔
415
  }
416

417
  char buf[TSDB_OFFSET_LEN] = {0};
24,905,424✔
418
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
24,905,424✔
419
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
24,905,424✔
420
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
421

422
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
24,904,769✔
423
  tqSetHandleIdle(pHandle);
24,902,655✔
424

425
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
24,901,434✔
426
          req.subKey, pHandle);
427

428
END:
24,905,244✔
429
  tDestroySMqPollReq(&req);
24,905,686✔
430
  return code;
24,905,686✔
431
}
432

433
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
894✔
434
  if (pTq == NULL || pMsg == NULL) {
894✔
435
    return TSDB_CODE_INVALID_PARA;
×
436
  }
437
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
894✔
438
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
894✔
439

440
  SMqVgOffset vgOffset = {0};
894✔
441

442
  SDecoder decoder;
894✔
443
  tDecoderInit(&decoder, (uint8_t*)data, len);
894✔
444
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
894✔
445
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
446
    return terrno;
×
447
  }
448

449
  tDecoderClear(&decoder);
894✔
450

451
  STqOffset* pSavedOffset = NULL;
894✔
452
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
894✔
453
  if (code != 0) {
894✔
454
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
455
  }
456
  vgOffset.offset = *pSavedOffset;
894✔
457

458
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
894✔
459
  if (code < 0) {
894✔
460
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
461
  }
462

463
  void* buf = rpcMallocCont(len);
894✔
464
  if (buf == NULL) {
894✔
465
    return terrno;
×
466
  }
467
  SEncoder encoder = {0};
894✔
468
  tEncoderInit(&encoder, buf, len);
894✔
469
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
894✔
470
  tEncoderClear(&encoder);
894✔
471
  if (code < 0) {
894✔
472
    rpcFreeCont(buf);
×
473
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
474
  }
475

476
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
894✔
477

478
  tmsgSendRsp(&rsp);
894✔
479
  return 0;
894✔
480
}
481

482
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
584✔
483
  if (pTq == NULL || pMsg == NULL) {
584✔
484
    return TSDB_CODE_INVALID_PARA;
×
485
  }
486
  int32_t    code = 0;
584✔
487
  SMqPollReq req = {0};
584✔
488
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
584✔
489
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
490
    return TSDB_CODE_INVALID_MSG;
×
491
  }
492

493
  int64_t      consumerId = req.consumerId;
584✔
494
  STqOffsetVal reqOffset = req.reqOffset;
584✔
495
  int32_t      vgId = TD_VID(pTq->pVnode);
584✔
496

497
  // 1. find handle
498
  taosRLockLatch(&pTq->lock);
584✔
499
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
584✔
500
  if (pHandle == NULL) {
584✔
501
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
502
    taosRUnLockLatch(&pTq->lock);
×
503
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
504
  }
505

506
  // 2. check rebalance status
507
  if (pHandle->consumerId != consumerId) {
584✔
508
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
509
            consumerId, vgId, req.subKey, pHandle->consumerId);
510
    taosRUnLockLatch(&pTq->lock);
×
511
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
512
  }
513

514
  int64_t sver = 0, ever = 0;
584✔
515
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
584✔
516
  taosRUnLockLatch(&pTq->lock);
584✔
517

518
  SMqDataRsp dataRsp = {0};
584✔
519
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
584✔
520
  if (code != 0) {
584✔
521
    return code;
×
522
  }
523

524
  if (req.useSnapshot == true) {
584✔
525
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
526
    code = TSDB_CODE_INVALID_PARA;
×
527
    goto END;
×
528
  }
529

530
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
584✔
531

532
  if (reqOffset.type == TMQ_OFFSET__LOG) {
584✔
533
    dataRsp.rspOffset.version = reqOffset.version;
292✔
534
  } else if (reqOffset.type < 0) {
292✔
535
    STqOffset* pOffset = NULL;
292✔
536
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
292✔
537
    if (code == 0) {
292✔
538
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
292✔
539
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
540
        code = TSDB_CODE_INVALID_PARA;
×
541
        goto END;
×
542
      }
543

544
      dataRsp.rspOffset.version = pOffset->val.version;
292✔
545
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
292✔
546
             req.subKey, dataRsp.rspOffset.version);
547
    } else {
548
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
×
549
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
×
550
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
551
        dataRsp.rspOffset.version = ever;
×
552
      }
553
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
×
554
             dataRsp.rspOffset.version);
555
    }
556
  } else {
557
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
558
            reqOffset.type);
559
    code = TSDB_CODE_INVALID_PARA;
×
560
    goto END;
×
561
  }
562

563
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
584✔
564

565
END:
584✔
566
  tDeleteMqDataRsp(&dataRsp);
584✔
567
  return code;
584✔
568
}
569

570
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
270,036✔
571
  if (pTq == NULL || msg == NULL) {
270,036✔
572
    return TSDB_CODE_INVALID_PARA;
×
573
  }
574
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
270,036✔
575
  int32_t        vgId = TD_VID(pTq->pVnode);
270,036✔
576

577
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
270,036✔
578
  int32_t code = 0;
270,338✔
579

580
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
270,338✔
581
  if (pHandle) {
270,036✔
582
    while (1) {
×
583
      taosWLockLatch(&pTq->lock);
269,504✔
584
      bool exec = tqIsHandleExec(pHandle);
269,504✔
585

586
      if (exec) {
269,504✔
587
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
588
               pHandle->subKey, pHandle);
589
        taosWUnLockLatch(&pTq->lock);
×
590
        taosMsleep(10);
×
591
        continue;
×
592
      }
593
      tqUnregisterPushHandle(pTq, pHandle);
269,504✔
594
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
269,504✔
595
      if (code != 0) {
268,678✔
596
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
597
      }
598
      taosWUnLockLatch(&pTq->lock);
268,678✔
599
      break;
269,504✔
600
    }
601
  }
602

603
  taosWLockLatch(&pTq->lock);
270,036✔
604
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
270,036✔
605
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
78,521✔
606
  }
607
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
270,036✔
608
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
254,199✔
609
  }
610

611
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
269,519✔
612
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
613
  }
614
  taosWUnLockLatch(&pTq->lock);
269,774✔
615

616
  return 0;
269,734✔
617
}
618

619
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,304,804✔
620
  if (pTq == NULL || msg == NULL) {
1,304,804✔
621
    return TSDB_CODE_INVALID_PARA;
×
622
  }
623
  int         ret = 0;
1,305,733✔
624
  SMqRebVgReq req = {0};
1,305,733✔
625
  SDecoder    dc = {0};
1,305,733✔
626

627
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
1,305,733✔
628
  ret = tDecodeSMqRebVgReq(&dc, &req);
1,305,733✔
629
  if (ret < 0) {
1,304,780✔
630
    goto end;
×
631
  }
632

633
  tqInfo("vgId:%d, tq process subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
1,304,780✔
634
         req.oldConsumerId, req.newConsumerId);
635

636
  taosRLockLatch(&pTq->lock);
1,305,763✔
637
  STqHandle* pHandle = NULL;
1,305,733✔
638
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,305,733✔
639
  if (code != 0){
1,304,129✔
640
    tqInfo("vgId:%d, tq process subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
362,348✔
641
  }
642
  taosRUnLockLatch(&pTq->lock);
1,305,347✔
643
  if (pHandle == NULL) {
1,305,051✔
644
    if (req.oldConsumerId != -1) {
363,270✔
645
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
646
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
647
    }
648
    if (req.newConsumerId == -1) {
363,270✔
649
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
650
      ret = TSDB_CODE_INVALID_PARA;
×
651
      goto end;
×
652
    }
653
    STqHandle handle = {0};
363,270✔
654
    ret = tqMetaCreateHandle(pTq, &req, &handle);
363,270✔
655
    if (ret < 0) {
363,270✔
656
      tqDestroyTqHandle(&handle);
×
657
      goto end;
×
658
    }
659
    taosWLockLatch(&pTq->lock);
363,270✔
660
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
363,270✔
661
    taosWUnLockLatch(&pTq->lock);
362,354✔
662
  } else {
663
    int maxLoop = MAX_LOOP;
941,781✔
664
    while (maxLoop-- > 0) {
941,781✔
665
      taosWLockLatch(&pTq->lock);
941,781✔
666
      bool exec = tqIsHandleExec(pHandle);
941,479✔
667
      if (exec) {
941,479✔
668
        tqInfo("vgId:%d, topic:%s, subscribe is executing, subscribe wait for 10ms and retry, pHandle:%p",
×
669
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
670
        taosWUnLockLatch(&pTq->lock);
×
671
        taosMsleep(10);
×
672
        continue;
×
673
      }
674
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
942,075✔
675
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
596✔
676
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
677
        tqUnregisterPushHandle(pTq, pHandle);
596✔
678
        STqHandle handle = {0};
596✔
679
        ret = tqMetaCreateHandle(pTq, &req, &handle);
596✔
680
        if (ret < 0) {
596✔
681
          tqDestroyTqHandle(&handle);
×
682
          taosWUnLockLatch(&pTq->lock);
×
683
          goto end;
×
684
        }
685
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
596✔
686
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
596✔
687
         req.oldConsumerId, req.newConsumerId);
688
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
941,565✔
689
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
40,756✔
690
      } else {
691
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
900,785✔
692
               req.newConsumerId);
693

694
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
900,785✔
695
        atomic_store_32(&pHandle->epoch, 0);
901,111✔
696
        tqUnregisterPushHandle(pTq, pHandle);
901,111✔
697
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
901,111✔
698
      }
699
      taosWUnLockLatch(&pTq->lock);
939,883✔
700
      break;
940,976✔
701
    }
702
  }
703

704
end:
1,302,501✔
705
  if (req.schema.pSchema != NULL) {
1,303,202✔
706
    taosMemoryFree(req.schema.pSchema);
815,035✔
707
  }
708
  tDecoderClear(&dc);
1,298,953✔
709
  return ret;
1,296,796✔
710
}
711

712
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
×
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