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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

75.67
/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; }
30,450,601✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
29,065,812✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
29,068,119✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
467,511✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
339,504✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
337,380✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
128,812✔
42
    tqReaderClose(pData->execHandle.pTqReader);
103,968✔
43
    walCloseReader(pData->pWalReader);
103,968✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
103,968✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
24,844✔
46
    walCloseReader(pData->pWalReader);
24,844✔
47
    tqReaderClose(pData->execHandle.pTqReader);
24,844✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
24,844✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
24,844✔
50
  }
51
  if (pData->msg != NULL) {
466,454✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
467,815✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
468,394✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
455,258✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
469,313✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
4,204,024✔
66
  if (pLeft == NULL || pRight == NULL) {
4,204,024✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
8,121,026✔
70
         pLeft->val.version == pRight->val.version;
3,917,002✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
4,003,938✔
84
  if (pTq == NULL) {
4,003,938✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
4,003,938✔
89
  pTq->pVnode = pVnode;
4,003,938✔
90

91
  pTq->path = taosStrdup(path);
4,003,909✔
92
  if (pTq->path == NULL) {
4,003,171✔
93
    return terrno;
×
94
  }
95

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
4,002,404✔
97
  if (pTq->pHandle == NULL) {
4,003,747✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
4,003,747✔
101

102
  taosInitRWLatch(&pTq->lock);
4,002,021✔
103

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

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

115
  return tqInitialize(pTq);
4,003,938✔
116
}
117

118
int32_t tqInitialize(STQ* pTq) {
4,003,938✔
119
  if (pTq == NULL) {
4,003,938✔
120
    return TSDB_CODE_INVALID_PARA;
×
121
  }
122

123
  return tqMetaOpen(pTq);
4,003,938✔
124
}
125

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

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

137
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
4,003,938✔
138
  while (pIter) {
4,020,364✔
139
    STqHandle* pHandle = *(STqHandle**)pIter;
16,426✔
140
    if (pHandle->msg != NULL) {
16,426✔
141
      tqPushEmptyDataRsp(pHandle, vgId);
16,426✔
142
      rpcFreeCont(pHandle->msg->pCont);
16,426✔
143
      taosMemoryFree(pHandle->msg);
16,426✔
144
      pHandle->msg = NULL;
16,426✔
145
    }
146
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
16,426✔
147
  }
148

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

156
  taosMemoryFree(pTq);
4,003,938✔
157
}
158

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

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

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

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

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

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

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

224
  tDecoderClear(&decoder);
4,465,827✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
4,465,827✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
4,465,827✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
297,359✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
4,168,468✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
4,168,468✔
233
            pOffset->val.version);
234
  } else {
235
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
236
    code = TSDB_CODE_INVALID_MSG;
×
237
    goto end;
×
238
  }
239

240
  STqOffset* pSavedOffset = NULL;
4,465,827✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
4,465,827✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
4,465,827✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
895✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
895✔
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,464,582✔
255
  if (code != 0) {
4,464,932✔
256
    goto end;
×
257
  }
258

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

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

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

282
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
1,216✔
283
  if (pHandle == NULL) {
1,216✔
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,216✔
292
    tqError("%s consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
293
            __func__, 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,216✔
302
  taosWUnLockLatch(&pTq->lock);
1,216✔
303

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

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

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

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

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

342
    taosHashClear(pTq->pPushMgr);
9,391,709✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
9,433,359✔
345
  return 0;
9,433,359✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
29,074,674✔
349
  if (pTq == NULL || pMsg == NULL) {
29,074,674✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
29,075,975✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
29,075,975✔
354
  if (code < 0) {
29,075,064✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
29,075,064✔
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;
29,075,064✔
368
  int32_t      reqEpoch = req.epoch;
29,075,064✔
369
  int32_t      vgId = TD_VID(pTq->pVnode);
29,075,064✔
370
  STqHandle*   pHandle = NULL;
29,075,724✔
371

372
  while (1) {
95,010✔
373
    taosWLockLatch(&pTq->lock);
29,171,987✔
374
    // 1. find handle
375
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
29,167,387✔
376
    if (code != TDB_CODE_SUCCESS) {
29,167,100✔
377
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
6,106✔
378
      taosWUnLockLatch(&pTq->lock);
6,106✔
379
      return code;
6,106✔
380
    }
381

382
    // 2. check rebalance status
383
    if (pHandle->consumerId != consumerId) {
29,160,994✔
384
      tqWarn("tmq poll: consumer:0x%" PRIx64" vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
1,144✔
385
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
386
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
1,144✔
387
      taosWUnLockLatch(&pTq->lock);
1,144✔
388
      goto END;
1,144✔
389
    }
390

391
    bool exec = tqIsHandleExec(pHandle);
29,157,909✔
392
    if (!exec) {
29,159,506✔
393
      tqSetHandleExec(pHandle);
29,065,812✔
394
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
395
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
29,063,130✔
396
              req.subKey, pHandle);
397
      taosWUnLockLatch(&pTq->lock);
29,068,676✔
398
      break;
29,069,727✔
399
    }
400
    taosWUnLockLatch(&pTq->lock);
93,694✔
401

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

408
  // 3. update the epoch value
409
  if (pHandle->epoch < reqEpoch) {
29,069,727✔
410
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
464,169✔
411
            reqEpoch);
412
    pHandle->epoch = reqEpoch;
464,440✔
413
  }
414

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

420
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
29,069,727✔
421
  tqSetHandleIdle(pHandle);
29,068,119✔
422

423
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
29,067,116✔
424
          req.subKey, pHandle);
425

426
END:
29,069,735✔
427
  tDestroySMqPollReq(&req);
29,070,871✔
428
  return code;
29,070,529✔
429
}
430

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

438
  SMqVgOffset vgOffset = {0};
304✔
439

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

447
  tDecoderClear(&decoder);
304✔
448

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

456
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
304✔
457
  if (code < 0) {
304✔
458
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
459
  }
460

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

474
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
304✔
475

476
  tmsgSendRsp(&rsp);
304✔
477
  return 0;
304✔
478
}
479

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

491
  int64_t      consumerId = req.consumerId;
608✔
492
  STqOffsetVal reqOffset = req.reqOffset;
608✔
493
  int32_t      vgId = TD_VID(pTq->pVnode);
608✔
494

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

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

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

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

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

528
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
608✔
529

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

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

561
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
608✔
562

563
END:
608✔
564
  tDeleteMqDataRsp(&dataRsp);
608✔
565
  return code;
608✔
566
}
567

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

575
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
288,469✔
576
  int32_t code = 0;
288,469✔
577

578
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
288,469✔
579
  if (pHandle) {
288,469✔
UNCOV
580
    while (1) {
×
581
      taosWLockLatch(&pTq->lock);
287,036✔
582
      bool exec = tqIsHandleExec(pHandle);
287,036✔
583

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

601
  taosWLockLatch(&pTq->lock);
288,120✔
602
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
288,469✔
603
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
118,976✔
604
  }
605
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
288,149✔
606
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
266,236✔
607
  }
608

609
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
288,890✔
610
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
611
  }
612
  taosWUnLockLatch(&pTq->lock);
287,899✔
613

614
  return 0;
288,140✔
615
}
616

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

625
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
1,396,532✔
626
  ret = tDecodeSMqRebVgReq(&dc, &req);
1,396,532✔
627
  if (ret < 0) {
1,395,192✔
628
    goto end;
×
629
  }
630

631
  tqInfo("vgId:%d, tmq subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
1,395,192✔
632
         req.oldConsumerId, req.newConsumerId);
633

634
  taosRLockLatch(&pTq->lock);  
1,397,100✔
635
  STqHandle* pHandle = NULL;
1,396,532✔
636
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,396,532✔
637
  if (code != 0){
1,395,950✔
638
    tqInfo("vgId:%d, tmq subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
390,294✔
639
  }
640
  taosRUnLockLatch(&pTq->lock);
1,396,221✔
641
  if (pHandle == NULL) {
1,396,221✔
642
    if (req.oldConsumerId != -1) {
390,565✔
643
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
644
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
645
    }
646
    if (req.newConsumerId == -1) {
390,565✔
647
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
648
      ret = TSDB_CODE_INVALID_PARA;
×
649
      goto end;
×
650
    }
651
    STqHandle handle = {0};
390,565✔
652
    ret = tqMetaCreateHandle(pTq, &req, &handle);
390,565✔
653
    if (ret < 0) {
390,565✔
654
      tqDestroyTqHandle(&handle);
×
655
      goto end;
×
656
    }
657
    taosWLockLatch(&pTq->lock);
390,565✔
658
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
390,565✔
659
    taosWUnLockLatch(&pTq->lock);
387,824✔
660
  } else {
661
    int maxLoop = MAX_LOOP;
1,005,656✔
662
    while (maxLoop-- > 0) {
1,005,656✔
663
      taosWLockLatch(&pTq->lock);
1,005,656✔
664
      bool exec = tqIsHandleExec(pHandle);
1,005,656✔
665
      if (exec) {
1,005,967✔
666
        tqInfo("vgId:%d, topic:%s, subscribe is executing, subscribe wait for 10ms and retry, pHandle:%p",
×
667
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
668
        taosWUnLockLatch(&pTq->lock);
×
669
        taosMsleep(10);
×
670
        continue;
×
671
      }
672
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
1,006,589✔
673
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
622✔
674
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
675
        tqUnregisterPushHandle(pTq, pHandle);
622✔
676
        STqHandle handle = {0};
622✔
677
        ret = tqMetaCreateHandle(pTq, &req, &handle);
622✔
678
        if (ret < 0) {
622✔
679
          tqDestroyTqHandle(&handle);
×
680
          taosWUnLockLatch(&pTq->lock);
×
681
          goto end;
×
682
        }
683
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
622✔
684
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
622✔
685
         req.oldConsumerId, req.newConsumerId);
686
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,005,656✔
687
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
44,324✔
688
      } else {
689
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
961,021✔
690
               req.newConsumerId);
691

692
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
961,021✔
693
        atomic_store_32(&pHandle->epoch, 0);
961,021✔
694
        tqUnregisterPushHandle(pTq, pHandle);
961,021✔
695
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
961,021✔
696
      }
697
      taosWUnLockLatch(&pTq->lock);
1,003,773✔
698
      break;
1,003,779✔
699
    }
700
  }
701

702
end:
1,391,005✔
703
  if (req.schema.pSchema != NULL) {
1,395,586✔
704
    taosMemoryFree(req.schema.pSchema);
859,218✔
705
  }
706
  tDecoderClear(&dc);
1,393,248✔
707
  return ret;
1,387,286✔
708
}
709

710
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