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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 hits per line

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

76.6
/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,727,315✔
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
5,171,442✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
5,174,459✔
30
#define MAX_LOOP 100
31

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

37
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
144,697✔
38
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
121,125✔
39
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
23,577✔
40
    tqReaderClose(pData->execHandle.pTqReader);
19,546✔
41
    walCloseReader(pData->pWalReader);
19,546✔
42
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
19,546✔
43
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
4,142✔
44
    walCloseReader(pData->pWalReader);
4,142✔
45
    tqReaderClose(pData->execHandle.pTqReader);
4,167✔
46
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
4,142✔
47
    nodesDestroyNode(pData->execHandle.execTb.node);
4,131✔
48
  }
49
  if (pData->msg != NULL) {
144,672✔
50
    rpcFreeCont(pData->msg->pCont);
×
51
    taosMemoryFree(pData->msg);
×
52
    pData->msg = NULL;
×
53
  }
54
  if (pData->block != NULL) {
144,759✔
55
    blockDataDestroy(pData->block);
×
56
  }
57
  if (pData->pRef) {
144,648✔
58
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
143,791✔
59
  }
60
  taosHashCleanup(pData->tableCreateTimeHash);
144,732✔
61
}
62

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
499,538✔
64
  if (pLeft == NULL || pRight == NULL) {
499,538✔
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
931,753✔
68
         pLeft->val.version == pRight->val.version;
432,215✔
69
}
70

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

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

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

86
  pVnode->pTq = pTq;
4,153,669✔
87
  pTq->pVnode = pVnode;
4,153,669✔
88

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

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

100
  taosInitRWLatch(&pTq->lock);
4,153,669✔
101

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

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

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

119
  return tqInitialize(pTq);
4,153,669✔
120
}
121

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

127
  return tqMetaOpen(pTq);
4,153,669✔
128
}
129

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

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

141
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
4,153,669✔
142
  while (pIter) {
4,154,966✔
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,153,669✔
154
  taosHashCleanup(pTq->pPushMgr);
4,153,669✔
155
  taosHashCleanup(pTq->pCheckInfo);
4,153,669✔
156
  taosHashCleanup(pTq->pOffset);
4,153,669✔
157
  taosMemoryFree(pTq->path);
4,153,669✔
158
  tqMetaClose(pTq);
4,153,669✔
159
  qDebug("vgId:%d end to close tq", vgId);
4,153,331✔
160

161
  taosMemoryFree(pTq);
4,153,669✔
162
}
163

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

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

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

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

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

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

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

229
  tDecoderClear(&decoder);
599,628✔
230

231
  STqOffset* pOffset = &vgOffset.offset;
599,628✔
232

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

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

253
  // if (pOffset->val.type == TMQ_OFFSET__LOG && vgOffset.markWal) {
254
  //   int32_t ret = walSetKeepVersion(pTq->pVnode->pWal, pOffset->val.version);
255
  //   tqDebug("set wal reader keep version to %" PRId64 " for vgId:%d sub:%s, code:%d", pOffset->val.version, vgId,
256
  //           pOffset->subKey, ret);
257
  // }
258
  // save the new offset value
259
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
599,153✔
260
  if (code != 0) {
599,613✔
261
    goto end;
×
262
  }
263

264
  return 0;
599,613✔
265
end:
90✔
266
  tOffsetDestroy(&vgOffset.offset.val);
90✔
267
  return code;
90✔
268
}
269

270
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
266✔
271
  if (pTq == NULL || pMsg == NULL) {
266✔
272
    return TSDB_CODE_INVALID_PARA;
×
273
  }
274
  SMqSeekReq req = {0};
266✔
275
  int32_t    vgId = TD_VID(pTq->pVnode);
266✔
276
  SRpcMsg    rsp = {.info = pMsg->info};
266✔
277
  int        code = 0;
266✔
278

279
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
266✔
280
    code = TSDB_CODE_OUT_OF_MEMORY;
×
281
    goto end;
×
282
  }
283

284
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
266✔
285
  taosWLockLatch(&pTq->lock);
266✔
286

287
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
266✔
288
  if (pHandle == NULL) {
266✔
289
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
290
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
291
    taosWUnLockLatch(&pTq->lock);
×
292
    goto end;
×
293
  }
294

295
  // 2. check consumer-vg assignment status
296
  if (pHandle->consumerId != req.consumerId) {
266✔
297
    tqError("ERROR tmq seek, consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
298
            req.consumerId, vgId, req.subKey, pHandle->consumerId);
299
    taosWUnLockLatch(&pTq->lock);
×
300
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
301
    goto end;
×
302
  }
303

304
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
305
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
306
  tqUnregisterPushHandle(pTq, pHandle);
266✔
307
  taosWUnLockLatch(&pTq->lock);
266✔
308

309
end:
266✔
310
  rsp.code = code;
266✔
311
  tmsgSendRsp(&rsp);
266✔
312
  return 0;
266✔
313
}
314

315
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
435,318✔
316
  if (pTq == NULL) {
435,318✔
317
    return TSDB_CODE_INVALID_PARA;
×
318
  }
319
  void* pIter = NULL;
435,318✔
320

321
  while (1) {
1,095✔
322
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
436,413✔
323
    if (pIter == NULL) {
436,413✔
324
      break;
432,229✔
325
    }
326

327
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
4,184✔
328

329
    if (pCheck->ntbUid == tbUid) {
4,184✔
330
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
4,184✔
331
      for (int32_t i = 0; i < sz; i++) {
16,108✔
332
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
15,013✔
333
        if (pForbidColId == NULL) {
15,013✔
334
          continue;
×
335
        }
336

337
        if ((*pForbidColId) == colId) {
15,013✔
338
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
3,089✔
339
          return -1;
3,089✔
340
        }
341
      }
342
    }
343
  }
344

345
  return 0;
432,229✔
346
}
347

348
int32_t tqProcessPollPush(STQ* pTq) {
1,356,623✔
349
  if (pTq == NULL) {
1,356,623✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  int32_t vgId = TD_VID(pTq->pVnode);
1,356,623✔
353
  taosWLockLatch(&pTq->lock);
1,356,623✔
354
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
1,356,623✔
355
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
1,343,216✔
356

357
    while (pIter) {
2,819,410✔
358
      STqHandle* pHandle = *(STqHandle**)pIter;
1,476,194✔
359
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
1,476,194✔
360

361
      if (pHandle->msg == NULL) {
1,476,194✔
362
        tqError("pHandle->msg should not be null");
×
363
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
364
        break;
×
365
      } else {
366
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
2,952,388✔
367
                       .pCont = pHandle->msg->pCont,
1,476,194✔
368
                       .contLen = pHandle->msg->contLen,
1,476,194✔
369
                       .info = pHandle->msg->info};
1,476,194✔
370
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
1,476,194✔
371
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
372
        }
373
        taosMemoryFree(pHandle->msg);
1,476,194✔
374
        pHandle->msg = NULL;
1,476,194✔
375
      }
376

377
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
1,476,194✔
378
    }
379

380
    taosHashClear(pTq->pPushMgr);
1,343,216✔
381
  }
382
  taosWUnLockLatch(&pTq->lock);
1,356,623✔
383
  return 0;
1,356,623✔
384
}
385

386
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
5,174,540✔
387
  if (pTq == NULL || pMsg == NULL) {
5,174,540✔
388
    return TSDB_CODE_INVALID_PARA;
×
389
  }
390
  SMqPollReq req = {0};
5,175,482✔
391
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
5,175,482✔
392
  if (code < 0) {
5,172,054✔
393
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
394
    code = TSDB_CODE_INVALID_MSG;
×
395
    goto END;
×
396
  }
397
  if (req.rawData == 1){
5,172,054✔
398
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
399
    if (req.uidHash == NULL) {
×
400
      tqError("tq poll rawData taosHashInit failed");
×
401
      code = terrno;
×
402
      goto END;
×
403
    }
404
  }
405
  int64_t      consumerId = req.consumerId;
5,172,054✔
406
  int32_t      reqEpoch = req.epoch;
5,172,054✔
407
  STqOffsetVal reqOffset = req.reqOffset;
5,172,054✔
408
  int32_t      vgId = TD_VID(pTq->pVnode);
5,175,589✔
409
  STqHandle*   pHandle = NULL;
5,174,235✔
410

411
  while (1) {
7,891✔
412
    taosWLockLatch(&pTq->lock);
5,182,739✔
413
    // 1. find handle
414
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
5,183,030✔
415
    if (code != TDB_CODE_SUCCESS) {
5,180,884✔
UNCOV
416
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
UNCOV
417
      taosWUnLockLatch(&pTq->lock);
×
UNCOV
418
      return code;
×
419
    }
420

421
    // 2. check rebalance status
422
    if (pHandle->consumerId != consumerId) {
5,180,884✔
423
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
765✔
424
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
425
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
426
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
765✔
427
      taosWUnLockLatch(&pTq->lock);
765✔
428
      goto END;
765✔
429
    }
430

431
    bool exec = tqIsHandleExec(pHandle);
5,180,458✔
432
    if (!exec) {
5,181,386✔
433
      tqSetHandleExec(pHandle);
5,171,442✔
434
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
435
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
5,172,219✔
436
              req.subKey, pHandle);
437
      taosWUnLockLatch(&pTq->lock);
5,174,429✔
438
      break;
5,175,515✔
439
    }
440
    taosWUnLockLatch(&pTq->lock);
9,944✔
441

442
    tqDebug("tmq poll: consumer:0x%" PRIx64
7,891✔
443
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
444
            consumerId, vgId, req.subKey, pHandle);
445
    taosMsleep(10);
7,891✔
446
  }
447

448
  // 3. update the epoch value
449
  if (pHandle->epoch < reqEpoch) {
5,175,515✔
450
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
185,078✔
451
            reqEpoch);
452
    pHandle->epoch = reqEpoch;
185,078✔
453
  }
454

455
  char buf[TSDB_OFFSET_LEN] = {0};
5,175,515✔
456
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
5,175,515✔
457
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
5,175,515✔
458
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
459

460
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
5,175,515✔
461
  tqSetHandleIdle(pHandle);
5,174,459✔
462

463
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
5,174,365✔
464
          req.subKey, pHandle);
465

466
END:
5,175,625✔
467
  tDestroySMqPollReq(&req);
5,176,280✔
468
  return code;
5,176,280✔
469
}
470

471
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
45✔
472
  if (pTq == NULL || pMsg == NULL) {
45✔
473
    return TSDB_CODE_INVALID_PARA;
×
474
  }
475
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
45✔
476
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
45✔
477

478
  SMqVgOffset vgOffset = {0};
45✔
479

480
  SDecoder decoder;
45✔
481
  tDecoderInit(&decoder, (uint8_t*)data, len);
45✔
482
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
45✔
483
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
484
    return terrno;
×
485
  }
486

487
  tDecoderClear(&decoder);
45✔
488

489
  STqOffset* pSavedOffset = NULL;
45✔
490
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
45✔
491
  if (code != 0) {
45✔
492
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
493
  }
494
  vgOffset.offset = *pSavedOffset;
45✔
495

496
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
45✔
497
  if (code < 0) {
45✔
498
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
499
  }
500

501
  void* buf = rpcMallocCont(len);
45✔
502
  if (buf == NULL) {
45✔
503
    return terrno;
×
504
  }
505
  SEncoder encoder = {0};
45✔
506
  tEncoderInit(&encoder, buf, len);
45✔
507
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
45✔
508
  tEncoderClear(&encoder);
45✔
509
  if (code < 0) {
45✔
510
    rpcFreeCont(buf);
×
511
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
512
  }
513

514
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
45✔
515

516
  tmsgSendRsp(&rsp);
45✔
517
  return 0;
45✔
518
}
519

520
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
202✔
521
  if (pTq == NULL || pMsg == NULL) {
202✔
522
    return TSDB_CODE_INVALID_PARA;
×
523
  }
524
  int32_t    code = 0;
202✔
525
  SMqPollReq req = {0};
202✔
526
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
202✔
527
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
528
    return TSDB_CODE_INVALID_MSG;
×
529
  }
530

531
  int64_t      consumerId = req.consumerId;
202✔
532
  STqOffsetVal reqOffset = req.reqOffset;
202✔
533
  int32_t      vgId = TD_VID(pTq->pVnode);
202✔
534

535
  // 1. find handle
536
  taosRLockLatch(&pTq->lock);
202✔
537
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
202✔
538
  if (pHandle == NULL) {
202✔
539
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
540
    taosRUnLockLatch(&pTq->lock);
×
541
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
542
  }
543

544
  // 2. check rebalance status
545
  if (pHandle->consumerId != consumerId) {
202✔
546
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
547
            consumerId, vgId, req.subKey, pHandle->consumerId);
548
    taosRUnLockLatch(&pTq->lock);
×
549
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
550
  }
551

552
  int64_t sver = 0, ever = 0;
202✔
553
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
202✔
554
  taosRUnLockLatch(&pTq->lock);
202✔
555

556
  SMqDataRsp dataRsp = {0};
202✔
557
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
202✔
558
  if (code != 0) {
202✔
559
    return code;
×
560
  }
561

562
  if (req.useSnapshot == true) {
202✔
563
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
564
    code = TSDB_CODE_INVALID_PARA;
×
565
    goto END;
×
566
  }
567

568
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
202✔
569

570
  if (reqOffset.type == TMQ_OFFSET__LOG) {
202✔
571
    dataRsp.rspOffset.version = reqOffset.version;
45✔
572
  } else if (reqOffset.type < 0) {
157✔
573
    STqOffset* pOffset = NULL;
157✔
574
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
157✔
575
    if (code == 0) {
157✔
576
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
45✔
577
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
578
        code = TSDB_CODE_INVALID_PARA;
×
579
        goto END;
×
580
      }
581

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

601
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
202✔
602

603
END:
202✔
604
  tDeleteMqDataRsp(&dataRsp);
202✔
605
  return code;
202✔
606
}
607

608
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
96,456✔
609
  if (pTq == NULL || msg == NULL) {
96,456✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
96,456✔
613
  int32_t        vgId = TD_VID(pTq->pVnode);
96,456✔
614

615
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
96,456✔
616
  int32_t code = 0;
96,456✔
617

618
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
96,456✔
619
  if (pHandle) {
96,416✔
620
    while (1) {
×
621
      taosWLockLatch(&pTq->lock);
96,291✔
622
      bool exec = tqIsHandleExec(pHandle);
96,331✔
623

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

641
  taosWLockLatch(&pTq->lock);
96,381✔
642
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
96,456✔
643
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
23,581✔
644
  }
645
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
96,456✔
646
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
95,076✔
647
  }
648

649
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
96,456✔
650
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
651
  }
652
  taosWUnLockLatch(&pTq->lock);
95,795✔
653

654
  return 0;
96,263✔
655
}
656

657
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
9,766✔
658
  if (pTq == NULL || msg == NULL) {
9,766✔
659
    return TSDB_CODE_INVALID_PARA;
×
660
  }
661
  STqCheckInfo info = {0};
9,766✔
662
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
9,766✔
663
  if (code != 0) {
9,766✔
664
    return code;
×
665
  }
666

667
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
9,766✔
668
  if (code != 0) {
9,766✔
669
    tDeleteSTqCheckInfo(&info);
×
670
    return code;
×
671
  }
672
  taosWLockLatch(&pTq->lock);
9,766✔
673
  code  = tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
9,766✔
674
  taosWUnLockLatch(&pTq->lock);
9,766✔
675
  return code;
9,766✔
676
}
677

678
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
6,608✔
679
  if (pTq == NULL || msg == NULL) {
6,608✔
680
    return TSDB_CODE_INVALID_PARA;
×
681
  }
682
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
6,608✔
683
    return TSDB_CODE_TSC_INTERNAL_ERROR;
25✔
684
  }
685
  taosWLockLatch(&pTq->lock);
6,583✔
686
  int32_t code = tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
6,583✔
687
  taosWUnLockLatch(&pTq->lock);
6,583✔
688
  return code;
6,583✔
689
}
690

691
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
591,085✔
692
  if (pTq == NULL || msg == NULL) {
591,085✔
693
    return TSDB_CODE_INVALID_PARA;
×
694
  }
695
  int         ret = 0;
591,085✔
696
  SMqRebVgReq req = {0};
591,085✔
697
  SDecoder    dc = {0};
591,085✔
698

699
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
591,085✔
700
  ret = tDecodeSMqRebVgReq(&dc, &req);
591,085✔
701
  if (ret < 0) {
590,728✔
702
    goto end;
×
703
  }
704

705
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
590,728✔
706
         req.oldConsumerId, req.newConsumerId);
707

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

752
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
447,886✔
753
        atomic_store_32(&pHandle->epoch, 0);
447,886✔
754
        tqUnregisterPushHandle(pTq, pHandle);
447,886✔
755
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
447,886✔
756
      }
757
      taosWUnLockLatch(&pTq->lock);
450,281✔
758
      break;
449,915✔
759
    }
760
  }
761

762
end:
588,966✔
763
  tDecoderClear(&dc);
590,179✔
764
  return ret;
590,432✔
765
}
766

767
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