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

taosdata / TDengine / #4923

09 Jan 2026 08:13AM UTC coverage: 65.373% (+0.2%) from 65.161%
#4923

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%)

3042 existing lines in 131 files now uncovered.

198273 of 303297 relevant lines covered (65.37%)

118980690.73 hits per line

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

75.95
/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,356,722✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
33,446,835✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
33,449,516✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
426,139✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
312,176✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
311,936✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
114,013✔
42
    tqReaderClose(pData->execHandle.pTqReader);
93,034✔
43
    walCloseReader(pData->pWalReader);
93,034✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
93,034✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
21,514✔
46
    walCloseReader(pData->pWalReader);
21,514✔
47
    tqReaderClose(pData->execHandle.pTqReader);
21,514✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
21,514✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
21,514✔
50
  }
51
  if (pData->msg != NULL) {
426,157✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
426,157✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
426,445✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
414,141✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
426,358✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
4,374,820✔
66
  if (pLeft == NULL || pRight == NULL) {
4,374,820✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
8,419,270✔
70
         pLeft->val.version == pRight->val.version;
4,044,450✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,677,142✔
84
  if (pTq == NULL) {
3,676,499✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,676,499✔
89
  pTq->pVnode = pVnode;
3,676,499✔
90

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

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,676,863✔
97
  if (pTq->pHandle == NULL) {
3,677,142✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,676,499✔
101

102
  taosInitRWLatch(&pTq->lock);
3,676,778✔
103

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

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

115
  return tqInitialize(pTq);
3,677,142✔
116
}
117

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

123
  return tqMetaOpen(pTq);
3,677,142✔
124
}
125

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

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

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

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

156
  taosMemoryFree(pTq);
3,676,810✔
157
}
158

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

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

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

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

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

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

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

224
  tDecoderClear(&decoder);
4,648,289✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
4,648,289✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
4,648,289✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
338,171✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
4,310,118✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
4,310,118✔
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;
4,648,617✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
4,648,289✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
4,648,289✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
558✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
558✔
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,647,731✔
255
  if (code != 0) {
4,647,452✔
256
    goto end;
×
257
  }
258

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

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

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

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

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

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

319
    while (pIter) {
20,697,000✔
320
      STqHandle* pHandle = *(STqHandle**)pIter;
10,659,888✔
321
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
10,659,888✔
322

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

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

342
    taosHashClear(pTq->pPushMgr);
10,037,112✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
10,045,916✔
345
  return 0;
10,045,916✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
33,449,193✔
349
  if (pTq == NULL || pMsg == NULL) {
33,449,193✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
33,451,694✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
33,451,644✔
354
  if (code < 0) {
33,450,037✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
33,450,037✔
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,450,037✔
368
  int32_t      reqEpoch = req.epoch;
33,450,037✔
369
  STqOffsetVal reqOffset = req.reqOffset;
33,450,037✔
370
  int32_t      vgId = TD_VID(pTq->pVnode);
33,450,301✔
371
  STqHandle*   pHandle = NULL;
33,452,732✔
372

373
  while (1) {
19,805✔
374
    taosWLockLatch(&pTq->lock);
33,473,234✔
375
    // 1. find handle
376
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
33,473,334✔
377
    if (code != TDB_CODE_SUCCESS) {
33,471,080✔
378
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
3,073✔
379
      taosWUnLockLatch(&pTq->lock);
3,073✔
380
      return code;
3,073✔
381
    }
382

383
    // 2. check rebalance status
384
    if (pHandle->consumerId != consumerId) {
33,468,007✔
385
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
254✔
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;
254✔
389
      taosWUnLockLatch(&pTq->lock);
254✔
390
      goto END;
254✔
391
    }
392

393
    bool exec = tqIsHandleExec(pHandle);
33,466,848✔
394
    if (!exec) {
33,468,361✔
395
      tqSetHandleExec(pHandle);
33,446,835✔
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,448,008✔
398
              req.subKey, pHandle);
399
      taosWUnLockLatch(&pTq->lock);
33,454,339✔
400
      break;
33,452,514✔
401
    }
402
    taosWUnLockLatch(&pTq->lock);
21,526✔
403

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

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

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

422
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
33,452,514✔
423
  tqSetHandleIdle(pHandle);
33,449,516✔
424

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

428
END:
33,453,179✔
429
  tDestroySMqPollReq(&req);
33,452,768✔
430
  return code;
33,452,768✔
431
}
432

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

440
  SMqVgOffset vgOffset = {0};
279✔
441

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

449
  tDecoderClear(&decoder);
279✔
450

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

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

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

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

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

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

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

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

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

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

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

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

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

580
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
268,990✔
581
  if (pHandle) {
268,990✔
582
    while (1) {
×
583
      taosWLockLatch(&pTq->lock);
268,183✔
584
      bool exec = tqIsHandleExec(pHandle);
267,874✔
585

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

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

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

616
  return 0;
268,990✔
617
}
618

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

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

636
  taosRLockLatch(&pTq->lock);
975,059✔
637
  STqHandle* pHandle = NULL;
975,367✔
638
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
975,367✔
639
  if (code != 0){
974,848✔
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));
353,157✔
641
  }
642
  taosRUnLockLatch(&pTq->lock);
975,127✔
643
  if (pHandle == NULL) {
975,367✔
644
    if (req.oldConsumerId != -1) {
353,676✔
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) {
353,676✔
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};
353,676✔
654
    ret = tqMetaCreateHandle(pTq, &req, &handle);
353,676✔
655
    if (ret < 0) {
353,676✔
656
      tqDestroyTqHandle(&handle);
×
657
      goto end;
×
658
    }
659
    taosWLockLatch(&pTq->lock);
353,676✔
660
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
353,676✔
661
    taosWUnLockLatch(&pTq->lock);
353,047✔
662
  } else {
663
    int maxLoop = MAX_LOOP;
621,691✔
664
    while (maxLoop-- > 0) {
621,691✔
665
      taosWLockLatch(&pTq->lock);
621,691✔
666
      bool exec = tqIsHandleExec(pHandle);
621,691✔
667
      if (exec) {
621,691✔
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) {
622,275✔
675
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
584✔
676
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
677
        tqUnregisterPushHandle(pTq, pHandle);
584✔
678
        STqHandle handle = {0};
584✔
679
        ret = tqMetaCreateHandle(pTq, &req, &handle);
584✔
680
        if (ret < 0) {
584✔
681
          tqDestroyTqHandle(&handle);
×
682
          taosWUnLockLatch(&pTq->lock);
×
683
          goto end;
×
684
        }
685
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
584✔
686
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
584✔
687
         req.oldConsumerId, req.newConsumerId);
688
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
621,107✔
689
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
41,197✔
690
      } else {
691
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
579,910✔
692
               req.newConsumerId);
693

694
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
579,910✔
695
        atomic_store_32(&pHandle->epoch, 0);
579,910✔
696
        tqUnregisterPushHandle(pTq, pHandle);
579,910✔
697
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
579,910✔
698
      }
699
      taosWUnLockLatch(&pTq->lock);
621,398✔
700
      break;
621,691✔
701
    }
702
  }
703

704
end:
975,049✔
705
  if (req.schema.pSchema != NULL) {
975,367✔
706
    taosMemoryFree(req.schema.pSchema);
491,781✔
707
  }
708
  tDecoderClear(&dc);
974,729✔
709
  return ret;
973,887✔
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