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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

120594206.86 hits per line

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

74.83
/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; }
34,147,200✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
33,231,612✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
33,232,945✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
424,243✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
310,141✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
309,265✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
114,384✔
42
    tqReaderClose(pData->execHandle.pTqReader);
93,421✔
43
    walCloseReader(pData->pWalReader);
93,421✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
93,421✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
20,963✔
46
    walCloseReader(pData->pWalReader);
20,963✔
47
    tqReaderClose(pData->execHandle.pTqReader);
20,963✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
20,963✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
20,963✔
50
  }
51
  if (pData->msg != NULL) {
423,406✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
424,830✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
424,830✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
412,849✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
424,243✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
4,605,276✔
66
  if (pLeft == NULL || pRight == NULL) {
4,605,276✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
8,888,373✔
70
         pLeft->val.version == pRight->val.version;
4,283,097✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,619,134✔
84
  if (pTq == NULL) {
3,617,073✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,617,073✔
89
  pTq->pVnode = pVnode;
3,617,073✔
90

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

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,616,450✔
97
  if (pTq->pHandle == NULL) {
3,619,134✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,618,432✔
101

102
  taosInitRWLatch(&pTq->lock);
3,618,435✔
103

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

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

115
  return tqInitialize(pTq);
3,619,134✔
116
}
117

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

123
  return tqMetaOpen(pTq);
3,619,134✔
124
}
125

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

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

137
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
3,619,134✔
138
  while (pIter) {
3,627,571✔
139
    STqHandle* pHandle = *(STqHandle**)pIter;
8,437✔
140
    if (pHandle->msg != NULL) {
8,437✔
141
      tqPushEmptyDataRsp(pHandle, vgId);
8,437✔
142
      rpcFreeCont(pHandle->msg->pCont);
8,437✔
143
      taosMemoryFree(pHandle->msg);
8,437✔
144
      pHandle->msg = NULL;
8,437✔
145
    }
146
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
8,437✔
147
  }
148

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

156
  taosMemoryFree(pTq);
3,619,253✔
157
}
158

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

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

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

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

198
  char buf1[TSDB_OFFSET_LEN] = {0};
22,627,109✔
199
  char buf2[TSDB_OFFSET_LEN] = {0};
22,627,109✔
200
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
22,627,109✔
201
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
22,627,109✔
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,
22,627,109✔
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);
22,627,109✔
207
}
208

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

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

224
  tDecoderClear(&decoder);
4,880,384✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
4,880,384✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
4,880,384✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
330,393✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
4,549,991✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
4,549,991✔
233
            pOffset->val.version);
234
  } else {
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;
4,880,384✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
4,880,384✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
4,880,150✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
548✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
548✔
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));
4,879,602✔
255
  if (code != 0) {
4,879,836✔
256
    goto end;
×
257
  }
258

259
  return 0;
4,879,836✔
260
end:
548✔
261
  tOffsetDestroy(&vgOffset.offset.val);
548✔
262
  return code;
548✔
263
}
264

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

274
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
1,096✔
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);
1,096✔
280
  taosWLockLatch(&pTq->lock);
1,096✔
281

282
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
1,096✔
283
  if (pHandle == NULL) {
1,096✔
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) {
1,096✔
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);
1,096✔
302
  taosWUnLockLatch(&pTq->lock);
1,096✔
303

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

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

319
    while (pIter) {
19,845,857✔
320
      STqHandle* pHandle = *(STqHandle**)pIter;
10,259,459✔
321
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
10,259,459✔
322

323
      if (pHandle->msg == NULL) {
10,259,459✔
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,
20,518,918✔
329
                       .pCont = pHandle->msg->pCont,
10,259,459✔
330
                       .contLen = pHandle->msg->contLen,
10,259,459✔
331
                       .info = pHandle->msg->info};
10,259,459✔
332
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
10,259,459✔
333
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
334
        }
335
        taosMemoryFree(pHandle->msg);
10,259,459✔
336
        pHandle->msg = NULL;
10,259,459✔
337
      }
338

339
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
10,259,459✔
340
    }
341

342
    taosHashClear(pTq->pPushMgr);
9,586,398✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
9,600,900✔
345
  return 0;
9,600,900✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
33,234,111✔
349
  if (pTq == NULL || pMsg == NULL) {
33,234,111✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
33,234,985✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
33,234,985✔
354
  if (code < 0) {
33,229,053✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
33,229,053✔
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;
33,229,053✔
368
  int32_t      reqEpoch = req.epoch;
33,229,053✔
369
  STqOffsetVal reqOffset = req.reqOffset;
33,229,053✔
370
  int32_t      vgId = TD_VID(pTq->pVnode);
33,231,752✔
371
  STqHandle*   pHandle = NULL;
33,233,528✔
372

373
  while (1) {
32,502✔
374
    taosWLockLatch(&pTq->lock);
33,267,498✔
375
    // 1. find handle
376
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
33,268,049✔
377
    if (code != TDB_CODE_SUCCESS) {
33,266,127✔
378
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
3,009✔
379
      taosWUnLockLatch(&pTq->lock);
3,009✔
380
      return code;
3,009✔
381
    }
382

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

393
    bool exec = tqIsHandleExec(pHandle);
33,264,039✔
394
    if (!exec) {
33,263,574✔
395
      tqSetHandleExec(pHandle);
33,231,612✔
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,
33,229,367✔
398
              req.subKey, pHandle);
399
      taosWUnLockLatch(&pTq->lock);
33,235,916✔
400
      break;
33,235,068✔
401
    }
402
    taosWUnLockLatch(&pTq->lock);
31,984✔
403

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

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

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

422
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
33,235,068✔
423
  tqSetHandleIdle(pHandle);
33,232,945✔
424

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

428
END:
33,235,474✔
429
  tDestroySMqPollReq(&req);
33,235,068✔
430
  return code;
33,234,813✔
431
}
432

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

440
  SMqVgOffset vgOffset = {0};
274✔
441

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

449
  tDecoderClear(&decoder);
274✔
450

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

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

463
  void* buf = rpcMallocCont(len);
274✔
464
  if (buf == NULL) {
274✔
465
    return terrno;
×
466
  }
467
  SEncoder encoder = {0};
274✔
468
  tEncoderInit(&encoder, buf, len);
274✔
469
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
274✔
470
  tEncoderClear(&encoder);
274✔
471
  if (code < 0) {
274✔
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};
274✔
477

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

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

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

497
  // 1. find handle
498
  taosRLockLatch(&pTq->lock);
548✔
499
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
548✔
500
  if (pHandle == NULL) {
548✔
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) {
548✔
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;
548✔
515
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
548✔
516
  taosRUnLockLatch(&pTq->lock);
548✔
517

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

524
  if (req.useSnapshot == true) {
548✔
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;
548✔
531

532
  if (reqOffset.type == TMQ_OFFSET__LOG) {
548✔
533
    dataRsp.rspOffset.version = reqOffset.version;
274✔
534
  } else if (reqOffset.type < 0) {
274✔
535
    STqOffset* pOffset = NULL;
274✔
536
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
274✔
537
    if (code == 0) {
274✔
538
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
274✔
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;
274✔
545
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
274✔
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);
548✔
564

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

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

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

580
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
265,232✔
581
  if (pHandle) {
265,232✔
582
    while (1) {
×
583
      taosWLockLatch(&pTq->lock);
264,180✔
584
      bool exec = tqIsHandleExec(pHandle);
264,180✔
585

586
      if (exec) {
264,180✔
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);
264,180✔
594
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
264,180✔
595
      if (code != 0) {
263,906✔
596
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
597
      }
598
      taosWUnLockLatch(&pTq->lock);
263,906✔
599
      break;
263,875✔
600
    }
601
  }
602

603
  taosWLockLatch(&pTq->lock);
264,927✔
604
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
264,927✔
605
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
75,483✔
606
  }
607
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
265,232✔
608
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
249,784✔
609
  }
610

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

616
  return 0;
265,232✔
617
}
618

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

627
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
970,802✔
628
  ret = tDecodeSMqRebVgReq(&dc, &req);
971,137✔
629
  if (ret < 0) {
970,527✔
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,
970,527✔
634
         req.oldConsumerId, req.newConsumerId);
635

636
  taosRLockLatch(&pTq->lock);
971,087✔
637
  STqHandle* pHandle = NULL;
971,137✔
638
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
971,137✔
639
  if (code != 0){
970,601✔
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));
351,620✔
641
  }
642
  taosRUnLockLatch(&pTq->lock);
970,862✔
643
  if (pHandle == NULL) {
970,811✔
644
    if (req.oldConsumerId != -1) {
352,156✔
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) {
352,156✔
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};
352,156✔
654
    ret = tqMetaCreateHandle(pTq, &req, &handle);
352,156✔
655
    if (ret < 0) {
352,156✔
656
      tqDestroyTqHandle(&handle);
×
657
      goto end;
×
658
    }
659
    taosWLockLatch(&pTq->lock);
352,156✔
660
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
352,156✔
661
    taosWUnLockLatch(&pTq->lock);
351,559✔
662
  } else {
663
    int maxLoop = MAX_LOOP;
618,655✔
664
    while (maxLoop-- > 0) {
618,655✔
665
      taosWLockLatch(&pTq->lock);
618,655✔
666
      bool exec = tqIsHandleExec(pHandle);
618,981✔
667
      if (exec) {
618,655✔
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) {
619,233✔
675
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
578✔
676
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
677
        tqUnregisterPushHandle(pTq, pHandle);
578✔
678
        STqHandle handle = {0};
578✔
679
        ret = tqMetaCreateHandle(pTq, &req, &handle);
578✔
680
        if (ret < 0) {
578✔
681
          tqDestroyTqHandle(&handle);
×
682
          taosWUnLockLatch(&pTq->lock);
×
683
          goto end;
×
684
        }
685
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
578✔
686
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
578✔
687
         req.oldConsumerId, req.newConsumerId);
688
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
618,403✔
689
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
40,841✔
690
      } else {
691
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
577,236✔
692
               req.newConsumerId);
693

694
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
577,236✔
695
        atomic_store_32(&pHandle->epoch, 0);
577,562✔
696
        tqUnregisterPushHandle(pTq, pHandle);
577,562✔
697
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
577,236✔
698
      }
699
      taosWUnLockLatch(&pTq->lock);
617,318✔
700
      break;
618,405✔
701
    }
702
  }
703

704
end:
969,640✔
705
  if (req.schema.pSchema != NULL) {
970,248✔
706
    taosMemoryFree(req.schema.pSchema);
488,669✔
707
  }
708
  tDecoderClear(&dc);
968,859✔
709
  return ret;
966,083✔
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