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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

75.06
/source/dnode/vnode/src/tq/tq.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tq.h"
17
#include <string.h>
18
#include "osDef.h"
19
#include "taoserror.h"
20
#include "stream.h"
21
#include "vnd.h"
22

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

28
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
26,370,522✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
25,208,826✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
25,210,990✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
427,814✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
315,329✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
314,469✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
112,230✔
42
    tqReaderClose(pData->execHandle.pTqReader);
91,516✔
43
    walCloseReader(pData->pWalReader);
91,516✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
91,516✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
20,714✔
46
    walCloseReader(pData->pWalReader);
20,714✔
47
    tqReaderClose(pData->execHandle.pTqReader);
21,002✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
21,002✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
21,002✔
50
  }
51
  if (pData->msg != NULL) {
427,305✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
428,102✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
428,102✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
416,443✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
427,814✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
3,386,101✔
66
  if (pLeft == NULL || pRight == NULL) {
3,386,101✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
6,494,935✔
70
         pLeft->val.version == pRight->val.version;
3,109,152✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,605,492✔
84
  if (pTq == NULL) {
3,604,154✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,604,154✔
89
  pTq->pVnode = pVnode;
3,604,154✔
90

91
  pTq->path = taosStrdup(path);
3,603,770✔
92
  if (pTq->path == NULL) {
3,604,787✔
93
    return terrno;
×
94
  }
95

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,603,640✔
97
  if (pTq->pHandle == NULL) {
3,604,883✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,604,883✔
101

102
  taosInitRWLatch(&pTq->lock);
3,604,762✔
103

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

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

115
  return tqInitialize(pTq);
3,605,492✔
116
}
117

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

123
  return tqMetaOpen(pTq);
3,605,492✔
124
}
125

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

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

137
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
3,605,492✔
138
  while (pIter) {
3,614,508✔
139
    STqHandle* pHandle = *(STqHandle**)pIter;
9,016✔
140
    if (pHandle->msg != NULL) {
9,016✔
141
      tqPushEmptyDataRsp(pHandle, vgId);
9,016✔
142
      rpcFreeCont(pHandle->msg->pCont);
9,016✔
143
      taosMemoryFree(pHandle->msg);
9,016✔
144
      pHandle->msg = NULL;
9,016✔
145
    }
146
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
9,016✔
147
  }
148

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

156
  taosMemoryFree(pTq);
3,605,492✔
157
}
158

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

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

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

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

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

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

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

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

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

224
  tDecoderClear(&decoder);
3,660,951✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
3,660,687✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
3,660,687✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
286,021✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
3,374,930✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
3,375,194✔
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,661,239✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
3,660,951✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
3,660,134✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
850✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
850✔
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,658,966✔
255
  if (code != 0) {
3,659,300✔
256
    goto end;
×
257
  }
258

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

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

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

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

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

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

319
    while (pIter) {
16,010,104✔
320
      STqHandle* pHandle = *(STqHandle**)pIter;
8,410,132✔
321
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
8,410,132✔
322

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

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

342
    taosHashClear(pTq->pPushMgr);
7,599,972✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
7,658,115✔
345
  return 0;
7,658,115✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
25,210,210✔
349
  if (pTq == NULL || pMsg == NULL) {
25,210,210✔
UNCOV
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
25,210,824✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
25,211,423✔
354
  if (code < 0) {
25,204,939✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
25,204,939✔
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;
25,204,939✔
368
  int32_t      reqEpoch = req.epoch;
25,204,939✔
369
  STqOffsetVal reqOffset = req.reqOffset;
25,204,939✔
370
  int32_t      vgId = TD_VID(pTq->pVnode);
25,208,567✔
371
  STqHandle*   pHandle = NULL;
25,209,170✔
372

373
  while (1) {
32,561✔
374
    taosWLockLatch(&pTq->lock);
25,241,150✔
375
    // 1. find handle
376
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
25,245,433✔
377
    if (code != TDB_CODE_SUCCESS) {
25,240,117✔
UNCOV
378
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
UNCOV
379
      taosWUnLockLatch(&pTq->lock);
×
UNCOV
380
      return code;
×
381
    }
382

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

393
    bool exec = tqIsHandleExec(pHandle);
25,239,636✔
394
    if (!exec) {
25,241,119✔
395
      tqSetHandleExec(pHandle);
25,208,826✔
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,
25,205,839✔
398
              req.subKey, pHandle);
399
      taosWUnLockLatch(&pTq->lock);
25,216,365✔
400
      break;
25,213,491✔
401
    }
402
    taosWUnLockLatch(&pTq->lock);
32,293✔
403

404
    tqDebug("tmq poll: consumer:0x%" PRIx64
32,561✔
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,561✔
408
  }
409

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

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

422
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
25,213,171✔
423
  tqSetHandleIdle(pHandle);
25,210,990✔
424

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

428
END:
25,213,757✔
429
  tDestroySMqPollReq(&req);
25,213,744✔
430
  return code;
25,213,424✔
431
}
432

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

440
  SMqVgOffset vgOffset = {0};
1,468✔
441

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

449
  tDecoderClear(&decoder);
1,468✔
450

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

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

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

478
  tmsgSendRsp(&rsp);
1,468✔
479
  return 0;
1,468✔
480
}
481

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

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

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

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

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

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

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

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

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

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

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

603
  taosWLockLatch(&pTq->lock);
271,499✔
604
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
271,179✔
605
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
80,500✔
606
  }
607
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
271,499✔
608
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
255,151✔
609
  }
610

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

616
  return 0;
270,963✔
617
}
618

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

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

636
  taosRLockLatch(&pTq->lock);
1,218,371✔
637
  STqHandle* pHandle = NULL;
1,217,777✔
638
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,217,777✔
639
  if (code != 0){
1,217,195✔
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));
357,010✔
641
  }
642
  taosRUnLockLatch(&pTq->lock);
1,218,060✔
643
  if (pHandle == NULL) {
1,217,777✔
644
    if (req.oldConsumerId != -1) {
357,592✔
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) {
357,592✔
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};
357,592✔
654
    ret = tqMetaCreateHandle(pTq, &req, &handle);
357,592✔
655
    if (ret < 0) {
357,592✔
656
      tqDestroyTqHandle(&handle);
×
657
      goto end;
×
658
    }
659
    taosWLockLatch(&pTq->lock);
357,592✔
660
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
357,592✔
661
    taosWUnLockLatch(&pTq->lock);
357,027✔
662
  } else {
663
    int maxLoop = MAX_LOOP;
860,185✔
664
    while (maxLoop-- > 0) {
860,185✔
665
      taosWLockLatch(&pTq->lock);
860,185✔
666
      bool exec = tqIsHandleExec(pHandle);
860,185✔
667
      if (exec) {
860,185✔
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) {
860,753✔
675
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
568✔
676
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
677
        tqUnregisterPushHandle(pTq, pHandle);
568✔
678
        STqHandle handle = {0};
568✔
679
        ret = tqMetaCreateHandle(pTq, &req, &handle);
568✔
680
        if (ret < 0) {
568✔
681
          tqDestroyTqHandle(&handle);
×
682
          taosWUnLockLatch(&pTq->lock);
×
683
          goto end;
×
684
        }
685
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
568✔
686
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
568✔
687
         req.oldConsumerId, req.newConsumerId);
688
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
859,312✔
689
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
40,040✔
690
      } else {
691
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
819,272✔
692
               req.newConsumerId);
693

694
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
819,525✔
695
        atomic_store_32(&pHandle->epoch, 0);
819,577✔
696
        tqUnregisterPushHandle(pTq, pHandle);
819,577✔
697
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
819,577✔
698
      }
699
      taosWUnLockLatch(&pTq->lock);
857,379✔
700
      break;
858,634✔
701
    }
702
  }
703

704
end:
1,214,214✔
705
  if (req.schema.pSchema != NULL) {
1,216,931✔
706
    taosMemoryFree(req.schema.pSchema);
732,518✔
707
  }
708
  tDecoderClear(&dc);
1,217,103✔
709
  return ret;
1,212,183✔
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