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

taosdata / TDengine / #4761

28 Sep 2025 10:49AM UTC coverage: 57.837% (-1.0%) from 58.866%
#4761

push

travis-ci

web-flow
merge: set version (#33122)

136913 of 302095 branches covered (45.32%)

Branch coverage included in aggregate %.

207750 of 293830 relevant lines covered (70.7%)

5673932.16 hits per line

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

63.72
/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 "osDef.h"
18
#include "taoserror.h"
19
#include "stream.h"
20
#include "vnd.h"
21

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

27
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
35,769!
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
33,439✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
33,440✔
30
#define MAX_LOOP 100
31

32
void tqDestroyTqHandle(void* data) {
1,365✔
33
  if (data == NULL) return;
1,365!
34
  STqHandle* pData = (STqHandle*)data;
1,365✔
35
  qDestroyTask(pData->execHandle.task);
1,365✔
36

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,364✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
1,057!
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
307✔
40
    tqReaderClose(pData->execHandle.pTqReader);
242✔
41
    walCloseReader(pData->pWalReader);
242✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
242✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
65!
44
    walCloseReader(pData->pWalReader);
65✔
45
    tqReaderClose(pData->execHandle.pTqReader);
65✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
65!
47
    nodesDestroyNode(pData->execHandle.execTb.node);
65✔
48
  }
49
  if (pData->msg != NULL) {
1,364!
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
1,364!
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
1,364✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
1,317✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
1,365✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
8,410✔
64
  if (pLeft == NULL || pRight == NULL) {
8,410!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
16,295✔
68
         pLeft->val.version == pRight->val.version;
7,883✔
69
}
70

71
int32_t tqOpen(const char* path, SVnode* pVnode) {
12,575✔
72
  if (path == NULL || pVnode == NULL) {
12,575!
73
    return TSDB_CODE_INVALID_PARA;
×
74
  }
75

76
  bool ignoreTq = pVnode->mounted && !taosCheckExistFile(path);
12,584!
77
  if (ignoreTq) {
12,584!
78
    return 0;
×
79
  }
80

81
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
12,584!
82
  if (pTq == NULL) {
12,584!
83
    return terrno;
×
84
  }
85

86
  pVnode->pTq = pTq;
12,584✔
87
  pTq->pVnode = pVnode;
12,584✔
88

89
  pTq->path = taosStrdup(path);
12,584!
90
  if (pTq->path == NULL) {
12,581!
91
    return terrno;
×
92
  }
93

94
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
12,581✔
95
  if (pTq->pHandle == NULL) {
12,581!
96
    return terrno;
×
97
  }
98
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
12,581✔
99

100
  taosInitRWLatch(&pTq->lock);
12,582✔
101

102
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
12,581✔
103
  if (pTq->pPushMgr == NULL) {
12,582!
104
    return terrno;
×
105
  }
106

107
  pTq->pCheckInfo = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
12,582✔
108
  if (pTq->pCheckInfo == NULL) {
12,518!
109
    return terrno;
×
110
  }
111
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
12,518✔
112

113
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
12,584✔
114
  if (pTq->pOffset == NULL) {
12,584!
115
    return terrno;
×
116
  }
117
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
12,584✔
118

119
  return tqInitialize(pTq);
12,584✔
120
}
121

122
int32_t tqInitialize(STQ* pTq) {
12,584✔
123
  if (pTq == NULL) {
12,584!
124
    return TSDB_CODE_INVALID_PARA;
×
125
  }
126

127
  return tqMetaOpen(pTq);
12,584✔
128
}
129

130
void tqClose(STQ* pTq) {
12,588✔
131
  qDebug("start to close tq");
12,588✔
132
  if (pTq == NULL) {
12,588!
133
    return;
×
134
  }
135

136
  int32_t vgId = 0;
12,588✔
137
  if (pTq->pVnode != NULL) {
12,588✔
138
    vgId = TD_VID(pTq->pVnode);
12,585✔
139
  }
140

141
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
12,588✔
142
  while (pIter) {
12,620✔
143
    STqHandle* pHandle = *(STqHandle**)pIter;
32✔
144
    if (pHandle->msg != NULL) {
32!
145
      tqPushEmptyDataRsp(pHandle, vgId);
32✔
146
      rpcFreeCont(pHandle->msg->pCont);
32✔
147
      taosMemoryFree(pHandle->msg);
32!
148
      pHandle->msg = NULL;
32✔
149
    }
150
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
32✔
151
  }
152

153
  taosHashCleanup(pTq->pHandle);
12,588✔
154
  taosHashCleanup(pTq->pPushMgr);
12,588✔
155
  taosHashCleanup(pTq->pCheckInfo);
12,588✔
156
  taosHashCleanup(pTq->pOffset);
12,588✔
157
  taosMemoryFree(pTq->path);
12,588!
158
  tqMetaClose(pTq);
12,588✔
159
  qDebug("vgId:%d end to close tq", vgId);
12,583✔
160

161
  taosMemoryFree(pTq);
12,590!
162
}
163

164
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
501✔
165
  if (pHandle == NULL) {
501!
166
    return;
×
167
  }
168
  int32_t    code = 0;
501✔
169
  SMqPollReq req = {0};
501✔
170
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
501✔
171
  if (code < 0) {
501!
172
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
173
    return;
×
174
  }
175

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

188
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
501✔
189
  if (code != 0) {
501!
190
    tqError("tqSendDataRsp failed, code:%d", code);
×
191
  }
192
  tDeleteMqDataRsp(&dataRsp);
501✔
193
}
194

195
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
22,346✔
196
                      int32_t vgId) {
197
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
22,346!
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200
  int64_t sver = 0, ever = 0;
22,346✔
201
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
22,346✔
202

203
  char buf1[TSDB_OFFSET_LEN] = {0};
22,346✔
204
  char buf2[TSDB_OFFSET_LEN] = {0};
22,346✔
205
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
22,346✔
206
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
22,346✔
207

208
  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,346!
209
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
210

211
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
22,346✔
212
}
213

214
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
9,301✔
215
  if (pTq == NULL) {
9,301!
216
    return TSDB_CODE_INVALID_PARA;
×
217
  }
218
  SMqVgOffset vgOffset = {0};
9,301✔
219
  int32_t     vgId = TD_VID(pTq->pVnode);
9,301✔
220

221
  int32_t  code = 0;
9,301✔
222
  SDecoder decoder;
223
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
9,301✔
224
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
9,301!
225
    code = TSDB_CODE_INVALID_MSG;
×
226
    goto end;
×
227
  }
228

229
  tDecoderClear(&decoder);
9,301✔
230

231
  STqOffset* pOffset = &vgOffset.offset;
9,301✔
232

233
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
9,301!
234
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
574!
235
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
236
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
8,727!
237
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
8,727!
238
            pOffset->val.version);
239
  } else {
240
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
241
    code = TSDB_CODE_INVALID_MSG;
×
242
    goto end;
×
243
  }
244

245
  STqOffset* pSavedOffset = NULL;
9,301✔
246
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
9,301✔
247
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
9,298✔
248
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
4!
249
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
250
    goto end;  // no need to update the offset value
4✔
251
  }
252

253
  // save the new offset value
254
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
9,294✔
255
  if (code != 0) {
9,297!
256
    goto end;
×
257
  }
258

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

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

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

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

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

310
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
729✔
311
  if (pTq == NULL) {
729!
312
    return TSDB_CODE_INVALID_PARA;
×
313
  }
314
  void* pIter = NULL;
729✔
315

316
  while (1) {
11✔
317
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
740✔
318
    if (pIter == NULL) {
740✔
319
      break;
694✔
320
    }
321

322
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
323

324
    if (pCheck->ntbUid == tbUid) {
46!
325
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
46✔
326
      for (int32_t i = 0; i < sz; i++) {
168✔
327
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
157✔
328
        if (pForbidColId == NULL) {
157!
329
          continue;
×
330
        }
331

332
        if ((*pForbidColId) == colId) {
157✔
333
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
35✔
334
          return -1;
35✔
335
        }
336
      }
337
    }
338
  }
339

340
  return 0;
694✔
341
}
342

343
int32_t tqProcessPollPush(STQ* pTq) {
9,566✔
344
  if (pTq == NULL) {
9,566!
345
    return TSDB_CODE_INVALID_PARA;
×
346
  }
347
  int32_t vgId = TD_VID(pTq->pVnode);
9,566✔
348
  taosWLockLatch(&pTq->lock);
9,566✔
349
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
9,566✔
350
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
9,564✔
351

352
    while (pIter) {
19,137✔
353
      STqHandle* pHandle = *(STqHandle**)pIter;
9,573✔
354
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
9,573!
355

356
      if (pHandle->msg == NULL) {
9,573!
357
        tqError("pHandle->msg should not be null");
×
358
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
359
        break;
×
360
      } else {
361
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
9,573✔
362
                       .pCont = pHandle->msg->pCont,
9,573✔
363
                       .contLen = pHandle->msg->contLen,
9,573✔
364
                       .info = pHandle->msg->info};
9,573✔
365
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
9,573!
366
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
367
        }
368
        taosMemoryFree(pHandle->msg);
9,573!
369
        pHandle->msg = NULL;
9,573✔
370
      }
371

372
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
9,573✔
373
    }
374

375
    taosHashClear(pTq->pPushMgr);
9,564✔
376
  }
377
  taosWUnLockLatch(&pTq->lock);
9,566✔
378
  return 0;
9,566✔
379
}
380

381
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
33,433✔
382
  if (pTq == NULL || pMsg == NULL) {
33,433!
383
    return TSDB_CODE_INVALID_PARA;
×
384
  }
385
  SMqPollReq req = {0};
33,442✔
386
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
33,442✔
387
  if (code < 0) {
33,432!
388
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
389
    code = TSDB_CODE_INVALID_MSG;
×
390
    goto END;
×
391
  }
392
  if (req.rawData == 1){
33,432!
393
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
394
    if (req.uidHash == NULL) {
13!
395
      tqError("tq poll rawData taosHashInit failed");
×
396
      code = terrno;
×
397
      goto END;
×
398
    }
399
  }
400
  int64_t      consumerId = req.consumerId;
33,445✔
401
  int32_t      reqEpoch = req.epoch;
33,445✔
402
  STqOffsetVal reqOffset = req.reqOffset;
33,445✔
403
  int32_t      vgId = TD_VID(pTq->pVnode);
33,445✔
404
  STqHandle*   pHandle = NULL;
33,445✔
405

406
  while (1) {
×
407
    taosWLockLatch(&pTq->lock);
33,445✔
408
    // 1. find handle
409
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
33,443✔
410
    if (code != TDB_CODE_SUCCESS) {
33,428!
411
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
412
      taosWUnLockLatch(&pTq->lock);
×
413
      return code;
×
414
    }
415

416
    // 2. check rebalance status
417
    if (pHandle->consumerId != consumerId) {
33,428✔
418
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
3!
419
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
420
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
421
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
3✔
422
      taosWUnLockLatch(&pTq->lock);
3✔
423
      goto END;
3✔
424
    }
425

426
    bool exec = tqIsHandleExec(pHandle);
33,425!
427
    if (!exec) {
33,425!
428
      tqSetHandleExec(pHandle);
33,439!
429
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
430
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
33,439!
431
              req.subKey, pHandle);
432
      taosWUnLockLatch(&pTq->lock);
33,443✔
433
      break;
33,443✔
434
    }
435
    taosWUnLockLatch(&pTq->lock);
×
436

437
    tqDebug("tmq poll: consumer:0x%" PRIx64
×
438
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
439
            consumerId, vgId, req.subKey, pHandle);
440
    taosMsleep(10);
×
441
  }
442

443
  // 3. update the epoch value
444
  if (pHandle->epoch < reqEpoch) {
33,443✔
445
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,143!
446
            reqEpoch);
447
    pHandle->epoch = reqEpoch;
1,143✔
448
  }
449

450
  char buf[TSDB_OFFSET_LEN] = {0};
33,443✔
451
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
33,443✔
452
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
33,443!
453
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
454

455
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
33,443✔
456
  tqSetHandleIdle(pHandle);
33,440!
457

458
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
33,440!
459
          req.subKey, pHandle);
460

461
END:
×
462
  tDestroySMqPollReq(&req);
33,443✔
463
  return code;
33,446✔
464
}
465

466
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
3✔
467
  if (pTq == NULL || pMsg == NULL) {
3!
468
    return TSDB_CODE_INVALID_PARA;
×
469
  }
470
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3✔
471
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
3✔
472

473
  SMqVgOffset vgOffset = {0};
3✔
474

475
  SDecoder decoder;
476
  tDecoderInit(&decoder, (uint8_t*)data, len);
3✔
477
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
3!
478
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
479
    return terrno;
×
480
  }
481

482
  tDecoderClear(&decoder);
3✔
483

484
  STqOffset* pSavedOffset = NULL;
3✔
485
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
3✔
486
  if (code != 0) {
3!
487
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
488
  }
489
  vgOffset.offset = *pSavedOffset;
3✔
490

491
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
3!
492
  if (code < 0) {
3!
493
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
494
  }
495

496
  void* buf = rpcMallocCont(len);
3✔
497
  if (buf == NULL) {
3!
498
    return terrno;
×
499
  }
500
  SEncoder encoder = {0};
3✔
501
  tEncoderInit(&encoder, buf, len);
3✔
502
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
3✔
503
  tEncoderClear(&encoder);
3✔
504
  if (code < 0) {
3!
505
    rpcFreeCont(buf);
×
506
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
507
  }
508

509
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
3✔
510

511
  tmsgSendRsp(&rsp);
3✔
512
  return 0;
3✔
513
}
514

515
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
3✔
516
  if (pTq == NULL || pMsg == NULL) {
3!
517
    return TSDB_CODE_INVALID_PARA;
×
518
  }
519
  int32_t    code = 0;
3✔
520
  SMqPollReq req = {0};
3✔
521
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
3!
522
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
523
    return TSDB_CODE_INVALID_MSG;
×
524
  }
525

526
  int64_t      consumerId = req.consumerId;
3✔
527
  STqOffsetVal reqOffset = req.reqOffset;
3✔
528
  int32_t      vgId = TD_VID(pTq->pVnode);
3✔
529

530
  // 1. find handle
531
  taosRLockLatch(&pTq->lock);
3✔
532
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
3✔
533
  if (pHandle == NULL) {
3!
534
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
535
    taosRUnLockLatch(&pTq->lock);
×
536
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
537
  }
538

539
  // 2. check rebalance status
540
  if (pHandle->consumerId != consumerId) {
3!
541
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
542
            consumerId, vgId, req.subKey, pHandle->consumerId);
543
    taosRUnLockLatch(&pTq->lock);
×
544
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
545
  }
546

547
  int64_t sver = 0, ever = 0;
3✔
548
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
3✔
549
  taosRUnLockLatch(&pTq->lock);
3✔
550

551
  SMqDataRsp dataRsp = {0};
3✔
552
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
3✔
553
  if (code != 0) {
3!
554
    return code;
×
555
  }
556

557
  if (req.useSnapshot == true) {
3!
558
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
559
    code = TSDB_CODE_INVALID_PARA;
×
560
    goto END;
×
561
  }
562

563
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
3✔
564

565
  if (reqOffset.type == TMQ_OFFSET__LOG) {
3✔
566
    dataRsp.rspOffset.version = reqOffset.version;
1✔
567
  } else if (reqOffset.type < 0) {
2!
568
    STqOffset* pOffset = NULL;
2✔
569
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
2✔
570
    if (code == 0) {
2✔
571
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
1!
572
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
573
        code = TSDB_CODE_INVALID_PARA;
×
574
        goto END;
×
575
      }
576

577
      dataRsp.rspOffset.version = pOffset->val.version;
1✔
578
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
1!
579
             req.subKey, dataRsp.rspOffset.version);
580
    } else {
581
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
1!
582
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
1✔
583
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
584
        dataRsp.rspOffset.version = ever;
×
585
      }
586
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
1!
587
             dataRsp.rspOffset.version);
588
    }
589
  } else {
590
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
591
            reqOffset.type);
592
    code = TSDB_CODE_INVALID_PARA;
×
593
    goto END;
×
594
  }
595

596
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
3✔
597

598
END:
3✔
599
  tDeleteMqDataRsp(&dataRsp);
3✔
600
  return code;
3✔
601
}
602

603
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
848✔
604
  if (pTq == NULL || msg == NULL) {
848!
605
    return TSDB_CODE_INVALID_PARA;
×
606
  }
607
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
848✔
608
  int32_t        vgId = TD_VID(pTq->pVnode);
848✔
609

610
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
848!
611
  int32_t code = 0;
848✔
612

613
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
848✔
614
  if (pHandle) {
848✔
615
    while (1) {
×
616
      taosWLockLatch(&pTq->lock);
845✔
617
      bool exec = tqIsHandleExec(pHandle);
845✔
618

619
      if (exec) {
845!
620
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
621
               pHandle->subKey, pHandle);
622
        taosWUnLockLatch(&pTq->lock);
×
623
        taosMsleep(10);
×
624
        continue;
×
625
      }
626
      tqUnregisterPushHandle(pTq, pHandle);
845✔
627
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
845✔
628
      if (code != 0) {
845!
629
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
630
      }
631
      taosWUnLockLatch(&pTq->lock);
845✔
632
      break;
845✔
633
    }
634
  }
635

636
  taosWLockLatch(&pTq->lock);
848✔
637
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
848✔
638
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
213!
639
  }
640
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
848✔
641
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
797!
642
  }
643

644
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
848!
645
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
646
  }
647
  taosWUnLockLatch(&pTq->lock);
848✔
648

649
  return 0;
848✔
650
}
651

652
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
48✔
653
  if (pTq == NULL || msg == NULL) {
48!
654
    return TSDB_CODE_INVALID_PARA;
×
655
  }
656
  STqCheckInfo info = {0};
48✔
657
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
48✔
658
  if (code != 0) {
48!
659
    return code;
×
660
  }
661

662
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
48✔
663
  if (code != 0) {
48!
664
    tDeleteSTqCheckInfo(&info);
×
665
    return code;
×
666
  }
667
  taosWLockLatch(&pTq->lock);
48✔
668
  code  = tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
48✔
669
  taosWUnLockLatch(&pTq->lock);
48✔
670
  return code;
48✔
671
}
672

673
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
10✔
674
  if (pTq == NULL || msg == NULL) {
10!
675
    return TSDB_CODE_INVALID_PARA;
×
676
  }
677
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
10!
678
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
679
  }
680
  taosWLockLatch(&pTq->lock);
10✔
681
  int32_t code = tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
10✔
682
  taosWUnLockLatch(&pTq->lock);
10✔
683
  return code;
10✔
684
}
685

686
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
2,641✔
687
  if (pTq == NULL || msg == NULL) {
2,641!
688
    return TSDB_CODE_INVALID_PARA;
×
689
  }
690
  int         ret = 0;
2,641✔
691
  SMqRebVgReq req = {0};
2,641✔
692
  SDecoder    dc = {0};
2,641✔
693

694
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
2,641✔
695
  ret = tDecodeSMqRebVgReq(&dc, &req);
2,641✔
696
  if (ret < 0) {
2,641!
697
    goto end;
×
698
  }
699

700
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
2,641!
701
         req.oldConsumerId, req.newConsumerId);
702

703
  taosRLockLatch(&pTq->lock);
2,641✔
704
  STqHandle* pHandle = NULL;
2,641✔
705
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
2,641✔
706
  if (code != 0){
2,640✔
707
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
1,142!
708
  }
709
  taosRUnLockLatch(&pTq->lock);
2,640✔
710
  if (pHandle == NULL) {
2,641✔
711
    if (req.oldConsumerId != -1) {
1,142!
712
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
713
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
714
    }
715
    if (req.newConsumerId == -1) {
1,142!
716
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
717
      ret = TSDB_CODE_INVALID_PARA;
×
718
      goto end;
×
719
    }
720
    STqHandle handle = {0};
1,142✔
721
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,142✔
722
    if (ret < 0) {
1,142!
723
      tqDestroyTqHandle(&handle);
×
724
      goto end;
×
725
    }
726
    taosWLockLatch(&pTq->lock);
1,142✔
727
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,142✔
728
    taosWUnLockLatch(&pTq->lock);
1,136✔
729
  } else {
730
    int maxLoop = MAX_LOOP;
1,499✔
731
    while (maxLoop-- > 0) {
1,499!
732
      taosWLockLatch(&pTq->lock);
1,499✔
733
      bool exec = tqIsHandleExec(pHandle);
1,499!
734
      if (exec) {
1,499!
735
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
736
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
737
        taosWUnLockLatch(&pTq->lock);
×
738
        taosMsleep(10);
×
739
        continue;
×
740
      }
741
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,499✔
742
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
155!
743
      } else {
744
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,344!
745
               req.newConsumerId);
746

747
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
1,344✔
748
        atomic_store_32(&pHandle->epoch, 0);
1,344✔
749
        tqUnregisterPushHandle(pTq, pHandle);
1,344✔
750
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
1,344✔
751
      }
752
      taosWUnLockLatch(&pTq->lock);
1,499✔
753
      break;
1,499✔
754
    }
755
  }
756

757
end:
×
758
  tDecoderClear(&dc);
2,641✔
759
  return ret;
2,634✔
760
}
761

762
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