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

taosdata / TDengine / #4849

13 Nov 2025 07:17AM UTC coverage: 63.829% (+0.6%) from 63.27%
#4849

push

travis-ci

guanshengliang
test: enable auto upload

148928 of 233322 relevant lines covered (63.83%)

115593255.19 hits per line

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

76.19
/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; }
5,290,972✔
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
4,769,982✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
4,771,467✔
30
#define MAX_LOOP 100
31

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

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
127,915✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
107,948✔
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
19,991✔
40
    tqReaderClose(pData->execHandle.pTqReader);
16,361✔
41
    walCloseReader(pData->pWalReader);
16,361✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
16,361✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,630✔
44
    walCloseReader(pData->pWalReader);
3,630✔
45
    tqReaderClose(pData->execHandle.pTqReader);
3,630✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
3,630✔
47
    nodesDestroyNode(pData->execHandle.execTb.node);
3,630✔
48
  }
49
  if (pData->msg != NULL) {
127,915✔
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
127,955✔
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
127,979✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
127,173✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
127,963✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
458,752✔
64
  if (pLeft == NULL || pRight == NULL) {
458,752✔
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
861,802✔
68
         pLeft->val.version == pRight->val.version;
403,050✔
69
}
70

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

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

81
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
4,051,276✔
82
  if (pTq == NULL) {
4,050,500✔
83
    return terrno;
×
84
  }
85

86
  pVnode->pTq = pTq;
4,050,500✔
87
  pTq->pVnode = pVnode;
4,050,500✔
88

89
  pTq->path = taosStrdup(path);
4,050,500✔
90
  if (pTq->path == NULL) {
4,051,276✔
91
    return terrno;
×
92
  }
93

94
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
4,050,500✔
95
  if (pTq->pHandle == NULL) {
4,051,276✔
96
    return terrno;
×
97
  }
98
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
4,051,276✔
99

100
  taosInitRWLatch(&pTq->lock);
4,051,276✔
101

102
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
4,051,276✔
103
  if (pTq->pPushMgr == NULL) {
4,051,276✔
104
    return terrno;
×
105
  }
106

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

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

119
  return tqInitialize(pTq);
4,051,252✔
120
}
121

122
int32_t tqInitialize(STQ* pTq) {
4,050,476✔
123
  if (pTq == NULL) {
4,050,476✔
124
    return TSDB_CODE_INVALID_PARA;
×
125
  }
126

127
  return tqMetaOpen(pTq);
4,050,476✔
128
}
129

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

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

141
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
4,051,276✔
142
  while (pIter) {
4,052,441✔
143
    STqHandle* pHandle = *(STqHandle**)pIter;
1,165✔
144
    if (pHandle->msg != NULL) {
1,165✔
145
      tqPushEmptyDataRsp(pHandle, vgId);
1,165✔
146
      rpcFreeCont(pHandle->msg->pCont);
1,165✔
147
      taosMemoryFree(pHandle->msg);
1,165✔
148
      pHandle->msg = NULL;
1,165✔
149
    }
150
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
1,165✔
151
  }
152

153
  taosHashCleanup(pTq->pHandle);
4,051,276✔
154
  taosHashCleanup(pTq->pPushMgr);
4,051,276✔
155
  taosHashCleanup(pTq->pCheckInfo);
4,051,276✔
156
  taosHashCleanup(pTq->pOffset);
4,051,276✔
157
  taosMemoryFree(pTq->path);
4,051,276✔
158
  tqMetaClose(pTq);
4,051,276✔
159
  qDebug("vgId:%d end to close tq", vgId);
4,050,563✔
160

161
  taosMemoryFree(pTq);
4,051,276✔
162
}
163

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

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

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

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

203
  char buf1[TSDB_OFFSET_LEN] = {0};
3,324,569✔
204
  char buf2[TSDB_OFFSET_LEN] = {0};
3,324,569✔
205
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
3,324,569✔
206
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
3,324,481✔
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,
3,324,569✔
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);
3,324,569✔
212
}
213

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

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

229
  tDecoderClear(&decoder);
550,300✔
230

231
  STqOffset* pOffset = &vgOffset.offset;
550,300✔
232

233
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
550,300✔
234
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
57,957✔
235
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
236
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
492,343✔
237
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
492,343✔
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;
550,300✔
246
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
550,300✔
247
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
550,151✔
248
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
236✔
249
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
250
    goto end;  // no need to update the offset value
236✔
251
  }
252

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

259
  return 0;
549,995✔
260
end:
236✔
261
  tOffsetDestroy(&vgOffset.offset.val);
236✔
262
  return code;
236✔
263
}
264

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

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

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

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

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

316
  while (1) {
975✔
317
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
415,836✔
318
    if (pIter == NULL) {
415,836✔
319
      break;
412,151✔
320
    }
321

322
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
3,685✔
323

324
    if (pCheck->ntbUid == tbUid) {
3,685✔
325
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
3,685✔
326
      for (int32_t i = 0; i < sz; i++) {
14,246✔
327
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
13,271✔
328
        if (pForbidColId == NULL) {
13,271✔
329
          continue;
×
330
        }
331

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

340
  return 0;
412,151✔
341
}
342

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

352
    while (pIter) {
2,597,986✔
353
      STqHandle* pHandle = *(STqHandle**)pIter;
1,369,076✔
354
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
1,369,076✔
355

356
      if (pHandle->msg == NULL) {
1,369,076✔
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,
2,738,152✔
362
                       .pCont = pHandle->msg->pCont,
1,369,076✔
363
                       .contLen = pHandle->msg->contLen,
1,369,076✔
364
                       .info = pHandle->msg->info};
1,369,076✔
365
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
1,369,076✔
366
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
367
        }
368
        taosMemoryFree(pHandle->msg);
1,369,076✔
369
        pHandle->msg = NULL;
1,369,076✔
370
      }
371

372
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
1,369,076✔
373
    }
374

375
    taosHashClear(pTq->pPushMgr);
1,228,910✔
376
  }
377
  taosWUnLockLatch(&pTq->lock);
1,242,015✔
378
  return 0;
1,242,015✔
379
}
380

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

406
  while (1) {
8,656✔
407
    taosWLockLatch(&pTq->lock);
4,780,965✔
408
    // 1. find handle
409
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
4,780,630✔
410
    if (code != TDB_CODE_SUCCESS) {
4,778,785✔
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) {
4,778,785✔
418
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
744✔
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;
744✔
422
      taosWUnLockLatch(&pTq->lock);
744✔
423
      goto END;
744✔
424
    }
425

426
    bool exec = tqIsHandleExec(pHandle);
4,780,208✔
427
    if (!exec) {
4,777,748✔
428
      tqSetHandleExec(pHandle);
4,769,982✔
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,
4,769,606✔
431
              req.subKey, pHandle);
432
      taosWUnLockLatch(&pTq->lock);
4,772,770✔
433
      break;
4,772,657✔
434
    }
435
    taosWUnLockLatch(&pTq->lock);
8,759✔
436

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

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

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

455
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
4,772,657✔
456
  tqSetHandleIdle(pHandle);
4,771,467✔
457

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

461
END:
4,772,261✔
462
  tDestroySMqPollReq(&req);
4,773,401✔
463
  return code;
4,773,401✔
464
}
465

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

473
  SMqVgOffset vgOffset = {0};
174✔
474

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

482
  tDecoderClear(&decoder);
174✔
483

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

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

496
  void* buf = rpcMallocCont(len);
174✔
497
  if (buf == NULL) {
174✔
498
    return terrno;
×
499
  }
500
  SEncoder encoder = {0};
174✔
501
  tEncoderInit(&encoder, buf, len);
174✔
502
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
174✔
503
  tEncoderClear(&encoder);
174✔
504
  if (code < 0) {
174✔
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};
174✔
510

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

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

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

530
  // 1. find handle
531
  taosRLockLatch(&pTq->lock);
172✔
532
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
172✔
533
  if (pHandle == NULL) {
172✔
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) {
172✔
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;
172✔
548
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
172✔
549
  taosRUnLockLatch(&pTq->lock);
172✔
550

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

557
  if (req.useSnapshot == true) {
172✔
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;
172✔
564

565
  if (reqOffset.type == TMQ_OFFSET__LOG) {
172✔
566
    dataRsp.rspOffset.version = reqOffset.version;
34✔
567
  } else if (reqOffset.type < 0) {
138✔
568
    STqOffset* pOffset = NULL;
138✔
569
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
138✔
570
    if (code == 0) {
138✔
571
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
34✔
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;
34✔
578
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
34✔
579
             req.subKey, dataRsp.rspOffset.version);
580
    } else {
581
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
104✔
582
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
104✔
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,
104✔
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);
172✔
597

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

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

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

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

619
      if (exec) {
87,239✔
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);
87,239✔
627
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
87,194✔
628
      if (code != 0) {
87,072✔
629
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
630
      }
631
      taosWUnLockLatch(&pTq->lock);
87,072✔
632
      break;
87,199✔
633
    }
634
  }
635

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

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

649
  return 0;
87,149✔
650
}
651

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

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

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

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

694
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
548,178✔
695
  ret = tDecodeSMqRebVgReq(&dc, &req);
548,178✔
696
  if (ret < 0) {
547,908✔
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,
547,908✔
701
         req.oldConsumerId, req.newConsumerId);
702

703
  taosRLockLatch(&pTq->lock);
548,178✔
704
  STqHandle* pHandle = NULL;
548,178✔
705
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
548,178✔
706
  if (code != 0){
547,674✔
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));
124,310✔
708
  }
709
  taosRUnLockLatch(&pTq->lock);
548,138✔
710
  if (pHandle == NULL) {
548,178✔
711
    if (req.oldConsumerId != -1) {
124,653✔
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) {
124,653✔
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};
124,653✔
721
    ret = tqMetaCreateHandle(pTq, &req, &handle);
124,653✔
722
    if (ret < 0) {
124,653✔
723
      tqDestroyTqHandle(&handle);
×
724
      goto end;
×
725
    }
726
    taosWLockLatch(&pTq->lock);
124,653✔
727
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
124,653✔
728
    taosWUnLockLatch(&pTq->lock);
124,585✔
729
  } else {
730
    int maxLoop = MAX_LOOP;
423,525✔
731
    while (maxLoop-- > 0) {
423,525✔
732
      taosWLockLatch(&pTq->lock);
423,525✔
733
      bool exec = tqIsHandleExec(pHandle);
423,525✔
734
      if (exec) {
423,525✔
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
423,525✔
742
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
1,853✔
743
      } else {
744
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
421,672✔
745
               req.newConsumerId);
746

747
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
421,637✔
748
        atomic_store_32(&pHandle->epoch, 0);
421,672✔
749
        tqUnregisterPushHandle(pTq, pHandle);
421,672✔
750
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
421,672✔
751
      }
752
      taosWUnLockLatch(&pTq->lock);
422,874✔
753
      break;
423,441✔
754
    }
755
  }
756

757
end:
546,476✔
758
  tDecoderClear(&dc);
548,178✔
759
  return ret;
546,390✔
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