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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 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; }
25,457,340✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
24,281,812✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
24,284,817✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
431,896✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
316,813✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
315,984✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
115,083✔
42
    tqReaderClose(pData->execHandle.pTqReader);
93,811✔
43
    walCloseReader(pData->pWalReader);
93,811✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
93,811✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
21,272✔
46
    walCloseReader(pData->pWalReader);
21,272✔
47
    tqReaderClose(pData->execHandle.pTqReader);
21,597✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
21,272✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
21,597✔
50
  }
51
  if (pData->msg != NULL) {
430,758✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
431,912✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
431,631✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
420,087✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
432,533✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
3,342,889✔
66
  if (pLeft == NULL || pRight == NULL) {
3,342,889✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
6,406,903✔
70
         pLeft->val.version == pRight->val.version;
3,064,014✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,646,873✔
84
  if (pTq == NULL) {
3,647,551✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,647,551✔
89
  pTq->pVnode = pVnode;
3,647,551✔
90

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

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,647,286✔
97
  if (pTq->pHandle == NULL) {
3,647,551✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,647,460✔
101

102
  taosInitRWLatch(&pTq->lock);
3,647,551✔
103

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

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

115
  return tqInitialize(pTq);
3,647,547✔
116
}
117

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

123
  return tqMetaOpen(pTq);
3,647,547✔
124
}
125

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

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

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

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

156
  taosMemoryFree(pTq);
3,647,551✔
157
}
158

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

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

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

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

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

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

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

224
  tDecoderClear(&decoder);
3,621,944✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
3,621,944✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
3,621,944✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
289,767✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
3,332,177✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
3,332,177✔
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;
3,622,249✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
3,622,249✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
3,621,944✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
556✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
556✔
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,621,388✔
255
  if (code != 0) {
3,620,472✔
256
    goto end;
×
257
  }
258

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

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

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

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

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

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

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

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

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

342
    taosHashClear(pTq->pPushMgr);
7,551,304✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
7,600,887✔
345
  return 0;
7,600,887✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
24,284,821✔
349
  if (pTq == NULL || pMsg == NULL) {
24,284,821✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
24,289,499✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
24,289,168✔
354
  if (code < 0) {
24,283,018✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
24,283,018✔
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,283,018✔
368
  int32_t      reqEpoch = req.epoch;
24,283,018✔
369
  STqOffsetVal reqOffset = req.reqOffset;
24,283,018✔
370
  int32_t      vgId = TD_VID(pTq->pVnode);
24,284,005✔
371
  STqHandle*   pHandle = NULL;
24,288,313✔
372

373
  while (1) {
22,223✔
374
    taosWLockLatch(&pTq->lock);
24,310,553✔
375
    // 1. find handle
376
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
24,309,759✔
377
    if (code != TDB_CODE_SUCCESS) {
24,304,584✔
378
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
6,184✔
379
      taosWUnLockLatch(&pTq->lock);
6,184✔
380
      return code;
6,184✔
381
    }
382

383
    // 2. check rebalance status
384
    if (pHandle->consumerId != consumerId) {
24,298,400✔
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);
24,301,595✔
394
    if (!exec) {
24,305,088✔
395
      tqSetHandleExec(pHandle);
24,281,812✔
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,278,163✔
398
              req.subKey, pHandle);
399
      taosWUnLockLatch(&pTq->lock);
24,287,207✔
400
      break;
24,287,770✔
401
    }
402
    taosWUnLockLatch(&pTq->lock);
23,276✔
403

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

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

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

422
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
24,287,770✔
423
  tqSetHandleIdle(pHandle);
24,284,817✔
424

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

428
END:
24,286,695✔
429
  tDestroySMqPollReq(&req);
24,287,770✔
430
  return code;
24,287,520✔
431
}
432

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

440
  SMqVgOffset vgOffset = {0};
278✔
441

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

449
  tDecoderClear(&decoder);
278✔
450

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

603
  taosWLockLatch(&pTq->lock);
265,026✔
604
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
264,761✔
605
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
77,369✔
606
  }
607
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
264,717✔
608
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
250,097✔
609
  }
610

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

616
  return 0;
265,026✔
617
}
618

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

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

636
  taosRLockLatch(&pTq->lock);
1,250,320✔
637
  STqHandle* pHandle = NULL;
1,250,320✔
638
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,250,320✔
639
  if (code != 0){
1,249,116✔
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));
358,132✔
641
  }
642
  taosRUnLockLatch(&pTq->lock);
1,249,766✔
643
  if (pHandle == NULL) {
1,250,320✔
644
    if (req.oldConsumerId != -1) {
359,059✔
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) {
359,059✔
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};
359,059✔
654
    ret = tqMetaCreateHandle(pTq, &req, &handle);
359,059✔
655
    if (ret < 0) {
359,059✔
656
      tqDestroyTqHandle(&handle);
×
657
      goto end;
×
658
    }
659
    taosWLockLatch(&pTq->lock);
359,059✔
660
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
359,059✔
661
    taosWUnLockLatch(&pTq->lock);
357,912✔
662
  } else {
663
    int maxLoop = MAX_LOOP;
891,261✔
664
    while (maxLoop-- > 0) {
891,261✔
665
      taosWLockLatch(&pTq->lock);
891,261✔
666
      bool exec = tqIsHandleExec(pHandle);
891,261✔
667
      if (exec) {
891,261✔
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) {
891,843✔
675
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
582✔
676
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
677
        tqUnregisterPushHandle(pTq, pHandle);
582✔
678
        STqHandle handle = {0};
582✔
679
        ret = tqMetaCreateHandle(pTq, &req, &handle);
582✔
680
        if (ret < 0) {
582✔
681
          tqDestroyTqHandle(&handle);
×
682
          taosWUnLockLatch(&pTq->lock);
×
683
          goto end;
×
684
        }
685
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
582✔
686
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
582✔
687
         req.oldConsumerId, req.newConsumerId);
688
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
890,679✔
689
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
42,329✔
690
      } else {
691
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
848,350✔
692
               req.newConsumerId);
693

694
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
848,350✔
695
        atomic_store_32(&pHandle->epoch, 0);
848,350✔
696
        tqUnregisterPushHandle(pTq, pHandle);
848,350✔
697
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
848,060✔
698
      }
699
      taosWUnLockLatch(&pTq->lock);
889,749✔
700
      break;
890,595✔
701
    }
702
  }
703

704
end:
1,246,898✔
705
  if (req.schema.pSchema != NULL) {
1,248,317✔
706
    taosMemoryFree(req.schema.pSchema);
761,591✔
707
  }
708
  tDecoderClear(&dc);
1,248,512✔
709
  return ret;
1,246,217✔
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