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

taosdata / TDengine / #4848

12 Nov 2025 01:06AM UTC coverage: 63.27% (+0.6%) from 62.651%
#4848

push

travis-ci

web-flow
Merge f12882a7a into e27395247

33 of 36 new or added lines in 4 files covered. (91.67%)

2652 existing lines in 104 files now uncovered.

138980 of 219661 relevant lines covered (63.27%)

110230098.27 hits per line

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

77.02
/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,025,207✔
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
4,499,699✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
4,502,117✔
30
#define MAX_LOOP 100
31

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

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
127,506✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
108,006✔
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
19,500✔
40
    tqReaderClose(pData->execHandle.pTqReader);
16,188✔
41
    walCloseReader(pData->pWalReader);
16,188✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
16,188✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,364✔
44
    walCloseReader(pData->pWalReader);
3,364✔
45
    tqReaderClose(pData->execHandle.pTqReader);
3,364✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
3,364✔
47
    nodesDestroyNode(pData->execHandle.execTb.node);
3,364✔
48
  }
49
  if (pData->msg != NULL) {
127,447✔
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
127,558✔
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
127,558✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
126,996✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
127,558✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
436,649✔
64
  if (pLeft == NULL || pRight == NULL) {
436,649✔
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
820,235✔
68
         pLeft->val.version == pRight->val.version;
383,628✔
69
}
70

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

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

81
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,998,932✔
82
  if (pTq == NULL) {
3,998,454✔
83
    return terrno;
×
84
  }
85

86
  pVnode->pTq = pTq;
3,998,454✔
87
  pTq->pVnode = pVnode;
3,998,932✔
88

89
  pTq->path = taosStrdup(path);
3,998,455✔
90
  if (pTq->path == NULL) {
3,998,932✔
91
    return terrno;
×
92
  }
93

94
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,997,129✔
95
  if (pTq->pHandle == NULL) {
3,998,932✔
96
    return terrno;
×
97
  }
98
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,998,454✔
99

100
  taosInitRWLatch(&pTq->lock);
3,998,932✔
101

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

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

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

119
  return tqInitialize(pTq);
3,998,932✔
120
}
121

122
int32_t tqInitialize(STQ* pTq) {
3,998,932✔
123
  if (pTq == NULL) {
3,998,932✔
124
    return TSDB_CODE_INVALID_PARA;
×
125
  }
126

127
  return tqMetaOpen(pTq);
3,998,932✔
128
}
129

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

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

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

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

161
  taosMemoryFree(pTq);
3,998,932✔
162
}
163

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

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

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

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

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

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

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

229
  tDecoderClear(&decoder);
529,583✔
230

231
  STqOffset* pOffset = &vgOffset.offset;
529,501✔
232

233
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
529,501✔
234
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
55,177✔
235
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
236
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
474,386✔
237
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
474,304✔
238
            pOffset->val.version);
239
  } else {
240
    tqError("invalid commit offset type:%d", pOffset->val.type);
82✔
241
    code = TSDB_CODE_INVALID_MSG;
×
242
    goto end;
×
243
  }
244

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

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

259
  return 0;
529,316✔
260
end:
56✔
261
  tOffsetDestroy(&vgOffset.offset.val);
56✔
262
  return code;
56✔
263
}
264

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

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

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

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

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

316
  while (1) {
912✔
317
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
418,834✔
318
    if (pIter == NULL) {
418,834✔
319
      break;
415,409✔
320
    }
321

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

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

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

340
  return 0;
415,409✔
341
}
342

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

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

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

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

375
    taosHashClear(pTq->pPushMgr);
1,113,601✔
376
  }
377
  taosWUnLockLatch(&pTq->lock);
1,125,238✔
378
  return 0;
1,125,325✔
379
}
380

381
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
4,502,286✔
382
  if (pTq == NULL || pMsg == NULL) {
4,502,286✔
UNCOV
383
    return TSDB_CODE_INVALID_PARA;
×
384
  }
385
  SMqPollReq req = {0};
4,502,856✔
386
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
4,503,027✔
387
  if (code < 0) {
4,501,693✔
388
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
389
    code = TSDB_CODE_INVALID_MSG;
×
390
    goto END;
×
391
  }
392
  if (req.rawData == 1){
4,501,693✔
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,501,693✔
401
  int32_t      reqEpoch = req.epoch;
4,501,693✔
402
  STqOffsetVal reqOffset = req.reqOffset;
4,501,693✔
403
  int32_t      vgId = TD_VID(pTq->pVnode);
4,502,276✔
404
  STqHandle*   pHandle = NULL;
4,502,449✔
405

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

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

426
    bool exec = tqIsHandleExec(pHandle);
4,511,875✔
427
    if (!exec) {
4,510,393✔
428
      tqSetHandleExec(pHandle);
4,499,699✔
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,501,206✔
431
              req.subKey, pHandle);
432
      taosWUnLockLatch(&pTq->lock);
4,505,256✔
433
      break;
4,502,911✔
434
    }
435
    taosWUnLockLatch(&pTq->lock);
10,694✔
436

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

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

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

455
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
4,502,834✔
456
  tqSetHandleIdle(pHandle);
4,502,117✔
457

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

461
END:
4,502,739✔
462
  tDestroySMqPollReq(&req);
4,503,177✔
463
  return code;
4,503,125✔
464
}
465

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

473
  SMqVgOffset vgOffset = {0};
28✔
474

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

482
  tDecoderClear(&decoder);
28✔
483

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

649
  return 0;
88,680✔
650
}
651

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

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

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

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

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

703
  taosRLockLatch(&pTq->lock);
549,205✔
704
  STqHandle* pHandle = NULL;
549,292✔
705
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
549,292✔
706
  if (code != 0){
549,027✔
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,324✔
708
  }
709
  taosRUnLockLatch(&pTq->lock);
549,109✔
710
  if (pHandle == NULL) {
549,292✔
711
    if (req.oldConsumerId != -1) {
124,589✔
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,589✔
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,589✔
721
    ret = tqMetaCreateHandle(pTq, &req, &handle);
124,565✔
722
    if (ret < 0) {
124,589✔
723
      tqDestroyTqHandle(&handle);
×
724
      goto end;
×
725
    }
726
    taosWLockLatch(&pTq->lock);
124,589✔
727
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
124,589✔
728
    taosWUnLockLatch(&pTq->lock);
124,502✔
729
  } else {
730
    int maxLoop = MAX_LOOP;
424,703✔
731
    while (maxLoop-- > 0) {
424,703✔
732
      taosWLockLatch(&pTq->lock);
424,703✔
733
      bool exec = tqIsHandleExec(pHandle);
424,703✔
734
      if (exec) {
424,625✔
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);
×
UNCOV
739
        continue;
×
740
      }
741
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
424,625✔
742
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
1,869✔
743
      } else {
744
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
422,732✔
745
               req.newConsumerId);
746

747
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
422,896✔
748
        atomic_store_32(&pHandle->epoch, 0);
422,834✔
749
        tqUnregisterPushHandle(pTq, pHandle);
422,834✔
750
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
422,834✔
751
      }
752
      taosWUnLockLatch(&pTq->lock);
424,036✔
753
      break;
423,467✔
754
    }
755
  }
756

757
end:
546,940✔
758
  tDecoderClear(&dc);
548,039✔
759
  return ret;
547,776✔
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