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

taosdata / TDengine / #4852

14 Nov 2025 08:06AM UTC coverage: 63.812% (+0.06%) from 63.754%
#4852

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

3142 existing lines in 128 files now uncovered.

149263 of 233910 relevant lines covered (63.81%)

118719500.98 hits per line

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

76.81
/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; }
6,319,873✔
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
5,747,751✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
5,750,939✔
30
#define MAX_LOOP 100
31

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

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
138,884✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
115,620✔
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
23,317✔
40
    tqReaderClose(pData->execHandle.pTqReader);
19,161✔
41
    walCloseReader(pData->pWalReader);
19,161✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
19,161✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
4,156✔
44
    walCloseReader(pData->pWalReader);
4,156✔
45
    tqReaderClose(pData->execHandle.pTqReader);
4,156✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
4,156✔
47
    nodesDestroyNode(pData->execHandle.execTb.node);
4,156✔
48
  }
49
  if (pData->msg != NULL) {
138,653✔
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
138,937✔
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
138,937✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
137,756✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
138,840✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
505,774✔
64
  if (pLeft == NULL || pRight == NULL) {
505,774✔
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
947,917✔
68
         pLeft->val.version == pRight->val.version;
442,143✔
69
}
70

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

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

81
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
4,152,708✔
82
  if (pTq == NULL) {
4,152,055✔
83
    return terrno;
×
84
  }
85

86
  pVnode->pTq = pTq;
4,152,055✔
87
  pTq->pVnode = pVnode;
4,150,570✔
88

89
  pTq->path = taosStrdup(path);
4,150,570✔
90
  if (pTq->path == NULL) {
4,152,708✔
91
    return terrno;
×
92
  }
93

94
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
4,151,936✔
95
  if (pTq->pHandle == NULL) {
4,152,055✔
96
    return terrno;
×
97
  }
98
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
4,152,055✔
99

100
  taosInitRWLatch(&pTq->lock);
4,152,708✔
101

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

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

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

119
  return tqInitialize(pTq);
4,152,029✔
120
}
121

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

127
  return tqMetaOpen(pTq);
4,151,376✔
128
}
129

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

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

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

153
  taosHashCleanup(pTq->pHandle);
4,152,708✔
154
  taosHashCleanup(pTq->pPushMgr);
4,152,708✔
155
  taosHashCleanup(pTq->pCheckInfo);
4,152,708✔
156
  taosHashCleanup(pTq->pOffset);
4,152,708✔
157
  taosMemoryFree(pTq->path);
4,152,708✔
158
  tqMetaClose(pTq);
4,152,561✔
159
  qDebug("vgId:%d end to close tq", vgId);
4,151,707✔
160

161
  taosMemoryFree(pTq);
4,151,997✔
162
}
163

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

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

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

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

203
  char buf1[TSDB_OFFSET_LEN] = {0};
4,037,835✔
204
  char buf2[TSDB_OFFSET_LEN] = {0};
4,037,835✔
205
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
4,037,835✔
206
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
4,037,766✔
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,
4,037,767✔
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);
4,037,835✔
212
}
213

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

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

229
  tDecoderClear(&decoder);
604,008✔
230

231
  STqOffset* pOffset = &vgOffset.offset;
604,008✔
232

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

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

259
  return 0;
603,537✔
260
end:
84✔
261
  tOffsetDestroy(&vgOffset.offset.val);
84✔
262
  return code;
84✔
263
}
264

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

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

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

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

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

316
  while (1) {
1,062✔
317
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
422,560✔
318
    if (pIter == NULL) {
422,560✔
319
      break;
418,496✔
320
    }
321

322
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
4,064✔
323

324
    if (pCheck->ntbUid == tbUid) {
4,064✔
325
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
4,064✔
326
      for (int32_t i = 0; i < sz; i++) {
15,640✔
327
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
14,578✔
328
        if (pForbidColId == NULL) {
14,578✔
329
          continue;
×
330
        }
331

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

340
  return 0;
418,496✔
341
}
342

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

352
    while (pIter) {
3,048,294✔
353
      STqHandle* pHandle = *(STqHandle**)pIter;
1,621,842✔
354
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
1,621,842✔
355

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

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

375
    taosHashClear(pTq->pPushMgr);
1,426,452✔
376
  }
377
  taosWUnLockLatch(&pTq->lock);
1,440,279✔
378
  return 0;
1,440,279✔
379
}
380

381
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
5,750,560✔
382
  if (pTq == NULL || pMsg == NULL) {
5,750,560✔
383
    return TSDB_CODE_INVALID_PARA;
×
384
  }
385
  SMqPollReq req = {0};
5,751,748✔
386
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
5,752,028✔
387
  if (code < 0) {
5,748,018✔
388
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
389
    code = TSDB_CODE_INVALID_MSG;
×
390
    goto END;
×
391
  }
392
  if (req.rawData == 1){
5,748,018✔
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;
5,748,018✔
401
  int32_t      reqEpoch = req.epoch;
5,748,018✔
402
  STqOffsetVal reqOffset = req.reqOffset;
5,748,018✔
403
  int32_t      vgId = TD_VID(pTq->pVnode);
5,751,409✔
404
  STqHandle*   pHandle = NULL;
5,752,645✔
405

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

416
    // 2. check rebalance status
417
    if (pHandle->consumerId != consumerId) {
5,760,246✔
418
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
1,072✔
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;
1,072✔
422
      taosWUnLockLatch(&pTq->lock);
1,072✔
423
      goto END;
1,072✔
424
    }
425

426
    bool exec = tqIsHandleExec(pHandle);
5,760,896✔
427
    if (!exec) {
5,761,111✔
428
      tqSetHandleExec(pHandle);
5,747,751✔
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,
5,746,357✔
431
              req.subKey, pHandle);
432
      taosWUnLockLatch(&pTq->lock);
5,751,259✔
433
      break;
5,752,097✔
434
    }
435
    taosWUnLockLatch(&pTq->lock);
13,360✔
436

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

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

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

455
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
5,751,934✔
456
  tqSetHandleIdle(pHandle);
5,750,939✔
457

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

461
END:
5,752,451✔
462
  tDestroySMqPollReq(&req);
5,753,169✔
463
  return code;
5,753,026✔
464
}
465

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

473
  SMqVgOffset vgOffset = {0};
42✔
474

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

482
  tDecoderClear(&decoder);
42✔
483

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

649
  return 0;
93,375✔
650
}
651

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

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

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

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

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

703
  taosRLockLatch(&pTq->lock);
600,095✔
704
  STqHandle* pHandle = NULL;
600,095✔
705
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
600,095✔
706
  if (code != 0){
599,518✔
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));
133,562✔
708
  }
709
  taosRUnLockLatch(&pTq->lock);
600,153✔
710
  if (pHandle == NULL) {
600,095✔
711
    if (req.oldConsumerId != -1) {
134,207✔
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) {
134,207✔
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};
134,207✔
721
    ret = tqMetaCreateHandle(pTq, &req, &handle);
134,207✔
722
    if (ret < 0) {
134,111✔
723
      tqDestroyTqHandle(&handle);
×
724
      goto end;
×
725
    }
726
    taosWLockLatch(&pTq->lock);
134,111✔
727
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
134,207✔
728
    taosWUnLockLatch(&pTq->lock);
134,017✔
729
  } else {
730
    int maxLoop = MAX_LOOP;
465,888✔
731
    while (maxLoop-- > 0) {
465,888✔
732
      taosWLockLatch(&pTq->lock);
465,888✔
733
      bool exec = tqIsHandleExec(pHandle);
465,634✔
734
      if (exec) {
465,888✔
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
465,888✔
742
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
3,162✔
743
      } else {
744
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
462,472✔
745
               req.newConsumerId);
746

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

757
end:
598,680✔
758
  tDecoderClear(&dc);
598,406✔
759
  return ret;
597,647✔
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