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

taosdata / TDengine / #5013

03 Apr 2026 03:59PM UTC coverage: 72.317% (+0.01%) from 72.305%
#5013

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

13131 existing lines in 160 files now uncovered.

257489 of 356056 relevant lines covered (72.32%)

129893134.08 hits per line

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

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

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

28
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
16,255,055✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
15,050,358✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
15,057,783✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
452,174✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
333,622✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
333,018✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
118,884✔
42
    tqReaderClose(pData->execHandle.pTqReader);
93,520✔
43
    walCloseReader(pData->pWalReader);
93,520✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
93,520✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
25,729✔
46
    walCloseReader(pData->pWalReader);
25,729✔
47
    tqReaderClose(pData->execHandle.pTqReader);
25,729✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
25,729✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
25,729✔
50
  }
51
  if (pData->msg != NULL) {
452,588✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
452,875✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
452,920✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
440,744✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
452,640✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
6,710,027✔
66
  if (pLeft == NULL || pRight == NULL) {
6,710,027✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
13,118,044✔
70
         pLeft->val.version == pRight->val.version;
6,408,017✔
71
}
72

73
int32_t tqOpen(const char* path, SVnode* pVnode) {
4,299,098✔
74
  if (path == NULL || pVnode == NULL) {
4,299,098✔
75
    return TSDB_CODE_INVALID_PARA;
×
76
  }
77

78
  bool ignoreTq = pVnode->mounted && !taosCheckExistFile(path);
4,300,963✔
79
  if (ignoreTq) {
4,300,963✔
80
    return 0;
×
81
  }
82

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
4,300,963✔
84
  if (pTq == NULL) {
4,301,289✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
4,301,289✔
89
  pTq->pVnode = pVnode;
4,300,947✔
90

91
  pTq->path = taosStrdup(path);
4,300,636✔
92
  if (pTq->path == NULL) {
4,300,977✔
93
    return terrno;
×
94
  }
95

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
4,300,666✔
97
  if (pTq->pHandle == NULL) {
4,301,289✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
4,301,289✔
101

102
  taosInitRWLatch(&pTq->lock);
4,300,300✔
103

104
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
4,301,289✔
105
  if (pTq->pOffset == NULL) {
4,301,289✔
106
    return terrno;
×
107
  }
108
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
4,301,289✔
109

110
  return tqInitialize(pTq);
4,301,289✔
111
}
112

113
int32_t tqInitialize(STQ* pTq) {
4,301,289✔
114
  if (pTq == NULL) {
4,301,289✔
115
    return TSDB_CODE_INVALID_PARA;
×
116
  }
117

118
  return tqMetaOpen(pTq);
4,301,289✔
119
}
120

121
void tqClose(STQ* pTq) {
4,301,387✔
122
  qDebug("start to close tq");
4,301,387✔
123
  if (pTq == NULL) {
4,301,387✔
124
    return;
×
125
  }
126

127
  int32_t vgId = 0;
4,301,387✔
128
  if (pTq->pVnode != NULL) {
4,301,387✔
129
    vgId = TD_VID(pTq->pVnode);
4,301,289✔
130
  }
131

132
  taosHashCleanup(pTq->pHandle);
4,301,387✔
133
  taosHashCleanup(pTq->pOffset);
4,301,387✔
134
  taosMemoryFree(pTq->path);
4,301,387✔
135
  tqMetaClose(pTq);
4,301,387✔
136
  qDebug("vgId:%d end to close tq", vgId);
4,300,658✔
137

138
  taosMemoryFree(pTq);
4,301,023✔
139
}
140

141
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
×
142
  if (pHandle == NULL) {
×
143
    return;
×
144
  }
145
  int32_t    code = 0;
×
146
  SMqPollReq req = {0};
×
147
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
×
148
  if (code < 0) {
×
149
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
150
    return;
×
151
  }
152

153
  SMqDataRsp dataRsp = {0};
×
154
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
×
155
  if (code != 0) {
×
156
    tqError("tqInitDataRsp failed, code:%d", code);
×
157
    return;
×
158
  }
159
  dataRsp.blockNum = 0;
×
160
  char buf[TSDB_OFFSET_LEN] = {0};
×
161
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.reqOffset);
×
162
  tqInfo("tqPushEmptyDataRsp to consumer:0x%" PRIx64 " vgId:%d, offset:%s, QID:0x%" PRIx64, req.consumerId, vgId, buf,
×
163
         req.reqId);
164

165
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
×
166
  if (code != 0) {
×
167
    tqError("tqSendDataRsp failed, code:%d", code);
×
168
  }
169
  tDeleteMqDataRsp(&dataRsp);
×
170
}
171

172
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
14,851,792✔
173
                      int32_t vgId) {
174
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
14,851,792✔
175
    return TSDB_CODE_INVALID_PARA;
×
176
  }
177
  int64_t sver = 0, ever = 0;
14,851,792✔
178
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
14,851,792✔
179

180
  char buf1[TSDB_OFFSET_LEN] = {0};
14,851,792✔
181
  char buf2[TSDB_OFFSET_LEN] = {0};
14,851,792✔
182
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
14,851,792✔
183
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
14,851,792✔
184

185
  tqDebug("tmq poll vgId:%d consumer:0x%" PRIx64 " (epoch %d) start to send rsp, block num:%d, req:%s, rsp:%s, QID:0x%" PRIx64,
14,851,427✔
186
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
187

188
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
14,851,427✔
189
}
190

191
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
6,965,410✔
192
  if (pTq == NULL) {
6,965,410✔
193
    return TSDB_CODE_INVALID_PARA;
×
194
  }
195
  SMqVgOffset vgOffset = {0};
6,965,410✔
196
  int32_t     vgId = TD_VID(pTq->pVnode);
6,965,410✔
197

198
  int32_t  code = 0;
6,965,410✔
199
  SDecoder decoder;
6,965,355✔
200
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
6,965,410✔
201
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
6,965,410✔
202
    code = TSDB_CODE_INVALID_MSG;
×
203
    goto end;
×
204
  }
205

206
  tDecoderClear(&decoder);
6,965,410✔
207

208
  STqOffset* pOffset = &vgOffset.offset;
6,965,410✔
209

210
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
6,965,410✔
211
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
309,796✔
212
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
213
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
6,655,614✔
214
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
6,656,283✔
215
            pOffset->val.version);
216
  } else {
217
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
218
    code = TSDB_CODE_INVALID_MSG;
×
219
    goto end;
×
220
  }
221

222
  STqOffset* pSavedOffset = NULL;
6,966,079✔
223
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
6,965,410✔
224
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
6,964,726✔
225
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
9,626✔
226
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
227
    goto end;  // no need to update the offset value
9,626✔
228
  }
229

230
  // if (pOffset->val.type == TMQ_OFFSET__LOG && vgOffset.markWal) {
231
  //   int32_t ret = walSetKeepVersion(pTq->pVnode->pWal, pOffset->val.version);
232
  //   tqDebug("set wal reader keep version to %" PRId64 " for vgId:%d sub:%s, code:%d", pOffset->val.version, vgId,
233
  //           pOffset->subKey, ret);
234
  // }
235
  // save the new offset value
236
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
6,955,100✔
237
  if (code != 0) {
6,953,811✔
238
    goto end;
×
239
  }
240

241
  return 0;
6,953,811✔
242
end:
9,626✔
243
  tOffsetDestroy(&vgOffset.offset.val);
9,626✔
244
  return code;
9,626✔
245
}
246

247
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
1,276✔
248
  if (pTq == NULL || pMsg == NULL) {
1,276✔
249
    return TSDB_CODE_INVALID_PARA;
×
250
  }
251
  SMqSeekReq req = {0};
1,276✔
252
  int32_t    vgId = TD_VID(pTq->pVnode);
1,276✔
253
  SRpcMsg    rsp = {.info = pMsg->info};
1,276✔
254
  int        code = 0;
1,276✔
255

256
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
1,276✔
257
    code = TSDB_CODE_OUT_OF_MEMORY;
×
258
    goto end;
×
259
  }
260

261
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
1,276✔
262
  taosWLockLatch(&pTq->lock);
1,276✔
263

264
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
1,276✔
265
  if (pHandle == NULL) {
1,276✔
266
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
267
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
268
    taosWUnLockLatch(&pTq->lock);
×
269
    goto end;
×
270
  }
271

272
  // 2. check consumer-vg assignment status
273
  if (pHandle->consumerId != req.consumerId) {
1,276✔
274
    tqError("%s consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
275
            __func__, req.consumerId, vgId, req.subKey, pHandle->consumerId);
276
    taosWUnLockLatch(&pTq->lock);
×
277
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
278
    goto end;
×
279
  }
280

281
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
282
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
283
  // tqUnregisterPushHandle(pTq, pHandle);
284
  taosWUnLockLatch(&pTq->lock);
1,276✔
285

286
end:
1,276✔
287
  rsp.code = code;
1,276✔
288
  tmsgSendRsp(&rsp);
1,276✔
289
  return 0;
1,276✔
290
}
291

292
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
15,057,878✔
293
  if (pTq == NULL || pMsg == NULL) {
15,057,878✔
294
    return TSDB_CODE_INVALID_PARA;
×
295
  }
296
  SMqPollReq req = {0};
15,058,563✔
297
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
15,058,563✔
298
  if (code < 0) {
15,054,032✔
299
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
300
    code = TSDB_CODE_INVALID_MSG;
×
301
    goto END;
×
302
  }
303
  if (req.rawData == 1){
15,054,032✔
304
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
305
    if (req.uidHash == NULL) {
×
306
      tqError("tq poll rawData taosHashInit failed");
×
307
      code = terrno;
×
308
      goto END;
×
309
    }
310
  }
311
  int64_t      consumerId = req.consumerId;
15,054,032✔
312
  int32_t      reqEpoch = req.epoch;
15,054,032✔
313
  int32_t      vgId = TD_VID(pTq->pVnode);
15,054,032✔
314
  STqHandle*   pHandle = NULL;
15,054,416✔
315

316
  while (1) {
67,869✔
317
    taosWLockLatch(&pTq->lock);
15,108,540✔
318
    // 1. find handle
319
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
15,120,872✔
320
    if (code != TDB_CODE_SUCCESS) {
15,110,857✔
UNCOV
321
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
UNCOV
322
      taosWUnLockLatch(&pTq->lock);
×
UNCOV
323
      return code;
×
324
    }
325

326
    // 2. check rebalance status
327
    if (pHandle->consumerId != consumerId) {
15,110,857✔
328
      tqWarn("tmq poll: consumer:0x%" PRIx64" vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
307✔
329
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
330
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
307✔
331
      taosWUnLockLatch(&pTq->lock);
307✔
332
      goto END;
307✔
333
    }
334

335
    bool exec = tqIsHandleExec(pHandle);
15,115,393✔
336
    if (!exec) {
15,115,096✔
337
      tqSetHandleExec(pHandle);
15,050,358✔
338
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
339
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
15,049,026✔
340
              req.subKey, pHandle);
341
      taosWUnLockLatch(&pTq->lock);
15,060,867✔
342
      break;
15,059,961✔
343
    }
344
    taosWUnLockLatch(&pTq->lock);
64,754✔
345

346
    tqDebug("tmq poll: consumer:0x%" PRIx64
67,869✔
347
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
348
            consumerId, vgId, req.subKey, pHandle);
349
    taosMsleep(10);
67,869✔
350
  }
351

352
  // 3. update the epoch value
353
  if (pHandle->epoch < reqEpoch) {
15,059,961✔
354
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
439,919✔
355
            reqEpoch);
356
    pHandle->epoch = reqEpoch;
439,919✔
357
  }
358

359
  char buf[TSDB_OFFSET_LEN] = {0};
15,059,961✔
360
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &req.reqOffset);
15,060,326✔
361
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
15,060,326✔
362
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
363

364
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
15,060,678✔
365
  tqSetHandleIdle(pHandle);
15,057,783✔
366

367
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
15,056,178✔
368
          req.subKey, pHandle);
369

370
END:
15,059,821✔
371
  tDestroySMqPollReq(&req);
15,060,633✔
372
  return code;
15,060,271✔
373
}
374

375
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
319✔
376
  if (pTq == NULL || pMsg == NULL) {
319✔
377
    return TSDB_CODE_INVALID_PARA;
×
378
  }
379
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
319✔
380
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
319✔
381

382
  SMqVgOffset vgOffset = {0};
319✔
383

384
  SDecoder decoder;
319✔
385
  tDecoderInit(&decoder, (uint8_t*)data, len);
319✔
386
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
319✔
387
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
388
    return terrno;
×
389
  }
390

391
  tDecoderClear(&decoder);
319✔
392

393
  STqOffset* pSavedOffset = NULL;
319✔
394
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
319✔
395
  if (code != 0) {
319✔
396
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
397
  }
398
  vgOffset.offset = *pSavedOffset;
319✔
399

400
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
319✔
401
  if (code < 0) {
319✔
402
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
403
  }
404

405
  void* buf = rpcMallocCont(len);
319✔
406
  if (buf == NULL) {
319✔
407
    return terrno;
×
408
  }
409
  SEncoder encoder = {0};
319✔
410
  tEncoderInit(&encoder, buf, len);
319✔
411
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
319✔
412
  tEncoderClear(&encoder);
319✔
413
  if (code < 0) {
319✔
414
    rpcFreeCont(buf);
×
415
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
416
  }
417

418
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
319✔
419

420
  tmsgSendRsp(&rsp);
319✔
421
  return 0;
319✔
422
}
423

424
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
638✔
425
  if (pTq == NULL || pMsg == NULL) {
638✔
426
    return TSDB_CODE_INVALID_PARA;
×
427
  }
428
  int32_t    code = 0;
638✔
429
  SMqPollReq req = {0};
638✔
430
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
638✔
431
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
432
    return TSDB_CODE_INVALID_MSG;
×
433
  }
434

435
  int64_t      consumerId = req.consumerId;
638✔
436
  STqOffsetVal reqOffset = req.reqOffset;
638✔
437
  int32_t      vgId = TD_VID(pTq->pVnode);
638✔
438

439
  // 1. find handle
440
  taosRLockLatch(&pTq->lock);
638✔
441
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
638✔
442
  if (pHandle == NULL) {
638✔
443
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
444
    taosRUnLockLatch(&pTq->lock);
×
445
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
446
  }
447

448
  // 2. check rebalance status
449
  if (pHandle->consumerId != consumerId) {
638✔
450
    tqError("%s consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
451
            __func__, consumerId, vgId, req.subKey, pHandle->consumerId);
452
    taosRUnLockLatch(&pTq->lock);
×
453
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
454
  }
455

456
  int64_t sver = 0, ever = 0;
638✔
457
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
638✔
458
  taosRUnLockLatch(&pTq->lock);
638✔
459

460
  SMqDataRsp dataRsp = {0};
638✔
461
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
638✔
462
  if (code != 0) {
638✔
463
    return code;
×
464
  }
465

466
  if (req.useSnapshot == true) {
638✔
467
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
468
    code = TSDB_CODE_INVALID_PARA;
×
469
    goto END;
×
470
  }
471

472
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
638✔
473

474
  if (reqOffset.type == TMQ_OFFSET__LOG) {
638✔
475
    dataRsp.rspOffset.version = reqOffset.version;
319✔
476
  } else if (reqOffset.type < 0) {
319✔
477
    STqOffset* pOffset = NULL;
319✔
478
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
319✔
479
    if (code == 0) {
319✔
480
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
319✔
481
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
482
        code = TSDB_CODE_INVALID_PARA;
×
483
        goto END;
×
484
      }
485

486
      dataRsp.rspOffset.version = pOffset->val.version;
319✔
487
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
319✔
488
             req.subKey, dataRsp.rspOffset.version);
489
    } else {
490
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
×
491
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
×
492
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
493
        dataRsp.rspOffset.version = ever;
×
494
      }
495
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
×
496
             dataRsp.rspOffset.version);
497
    }
498
  } else {
499
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
500
            reqOffset.type);
501
    code = TSDB_CODE_INVALID_PARA;
×
502
    goto END;
×
503
  }
504

505
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
638✔
506

507
END:
638✔
508
  tDeleteMqDataRsp(&dataRsp);
638✔
509
  return code;
638✔
510
}
511

512
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
284,571✔
513
  if (pTq == NULL || msg == NULL) {
284,571✔
514
    return TSDB_CODE_INVALID_PARA;
×
515
  }
516
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
284,571✔
517
  int32_t        vgId = TD_VID(pTq->pVnode);
284,571✔
518

519
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
284,571✔
520
  int32_t code = 0;
284,571✔
521

522
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
284,571✔
523
  if (pHandle) {
284,571✔
524
    while (1) {
×
525
      taosWLockLatch(&pTq->lock);
283,960✔
526
      bool exec = tqIsHandleExec(pHandle);
283,960✔
527

528
      if (exec) {
283,960✔
529
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
530
               pHandle->subKey, pHandle);
531
        taosWUnLockLatch(&pTq->lock);
×
532
        taosMsleep(10);
×
533
        continue;
×
534
      }
535
      // tqUnregisterPushHandle(pTq, pHandle);
536
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
283,960✔
537
      if (code != 0) {
283,393✔
538
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
539
      }
540
      taosWUnLockLatch(&pTq->lock);
283,393✔
541
      break;
283,960✔
542
    }
543
  }
544

545
  taosWLockLatch(&pTq->lock);
284,571✔
546
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
284,571✔
547
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
90,787✔
548
  }
549
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
284,234✔
550
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
265,738✔
551
  }
552

553
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
284,287✔
554
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
555
  }
556
  taosWUnLockLatch(&pTq->lock);
282,983✔
557

558
  return 0;
283,887✔
559
}
560

561
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,240,095✔
562
  if (pTq == NULL || msg == NULL) {
1,240,095✔
563
    return TSDB_CODE_INVALID_PARA;
×
564
  }
565
  int         ret = 0;
1,240,430✔
566
  SMqRebVgReq req = {0};
1,240,430✔
567
  SDecoder    dc = {0};
1,240,430✔
568

569
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
1,240,430✔
570
  ret = tDecodeSMqRebVgReq(&dc, &req);
1,240,430✔
571
  if (ret < 0) {
1,240,430✔
572
    goto end;
×
573
  }
574

575
  tqInfo("vgId:%d, tmq subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
1,240,430✔
576
         req.oldConsumerId, req.newConsumerId);
577

578
  taosRLockLatch(&pTq->lock);  
1,240,087✔
579
  STqHandle* pHandle = NULL;
1,240,430✔
580
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,240,430✔
581
  if (code != 0){
1,239,789✔
582
    tqInfo("vgId:%d, tmq subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
384,087✔
583
  }
584
  taosRUnLockLatch(&pTq->lock);
1,240,430✔
585
  if (pHandle == NULL) {
1,240,430✔
586
    if (req.oldConsumerId != -1) {
384,728✔
587
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
588
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
589
    }
590
    if (req.newConsumerId == -1) {
384,728✔
591
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
592
      ret = TSDB_CODE_INVALID_PARA;
×
593
      goto end;
×
594
    }
595
    STqHandle handle = {0};
384,728✔
596
    ret = tqMetaCreateHandle(pTq, &req, &handle);
384,728✔
597
    if (ret < 0) {
384,728✔
598
      tqDestroyTqHandle(&handle);
×
599
      goto end;
×
600
    }
601
    taosWLockLatch(&pTq->lock);
384,728✔
602
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
384,728✔
603
    taosWUnLockLatch(&pTq->lock);
383,514✔
604
  } else {
605
    int maxLoop = MAX_LOOP;
855,702✔
606
    while (maxLoop-- > 0) {
855,702✔
607
      taosWLockLatch(&pTq->lock);
855,702✔
608
      bool exec = tqIsHandleExec(pHandle);
855,702✔
609
      if (exec) {
855,702✔
610
        tqInfo("vgId:%d, topic:%s, subscribe is executing, subscribe wait for 10ms and retry, pHandle:%p",
×
611
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
612
        taosWUnLockLatch(&pTq->lock);
×
613
        taosMsleep(10);
×
UNCOV
614
        continue;
×
615
      }
616
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
856,366✔
617
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
664✔
618
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
619
        // tqUnregisterPushHandle(pTq, pHandle);
620
        STqHandle handle = {0};
664✔
621
        ret = tqMetaCreateHandle(pTq, &req, &handle);
664✔
622
        if (ret < 0) {
664✔
623
          tqDestroyTqHandle(&handle);
×
624
          taosWUnLockLatch(&pTq->lock);
×
625
          goto end;
×
626
        }
627
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
664✔
628
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
664✔
629
         req.oldConsumerId, req.newConsumerId);
630
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
855,038✔
631
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
44,768✔
632
      } else {
633
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
809,904✔
634
               req.newConsumerId);
635

636
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
809,904✔
637
        atomic_store_32(&pHandle->epoch, 0);
810,270✔
638
        // tqUnregisterPushHandle(pTq, pHandle);
639
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
810,270✔
640
      }
641
      taosWUnLockLatch(&pTq->lock);
852,691✔
642
      break;
854,309✔
643
    }
644
  }
645

646
end:
1,236,741✔
647
  if (req.schema.pSchema != NULL) {
1,236,110✔
648
    taosMemoryFree(req.schema.pSchema);
731,487✔
649
  }
650
  tDecoderClear(&dc);
1,233,380✔
651
  return ret;
1,229,224✔
652
}
653

654
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