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

taosdata / TDengine / #5004

26 Mar 2026 12:51PM UTC coverage: 72.338% (+0.02%) from 72.317%
#5004

push

travis-ci

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

512 of 851 new or added lines in 47 files covered. (60.16%)

5499 existing lines in 143 files now uncovered.

253935 of 351039 relevant lines covered (72.34%)

130889634.0 hits per line

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

70.62
/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; }
17,565,779✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
16,410,989✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
16,414,754✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
476,155✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
355,867✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
353,391✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
120,877✔
42
    tqReaderClose(pData->execHandle.pTqReader);
95,437✔
43
    walCloseReader(pData->pWalReader);
95,437✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
95,437✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
25,440✔
46
    walCloseReader(pData->pWalReader);
25,440✔
47
    tqReaderClose(pData->execHandle.pTqReader);
25,440✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
25,440✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
25,440✔
50
  }
51
  if (pData->msg != NULL) {
475,823✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
476,147✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
476,450✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
464,329✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
476,156✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
9,708,876✔
66
  if (pLeft == NULL || pRight == NULL) {
9,708,876✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
19,141,633✔
70
         pLeft->val.version == pRight->val.version;
9,433,075✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
4,253,826✔
84
  if (pTq == NULL) {
4,254,553✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
4,254,553✔
89
  pTq->pVnode = pVnode;
4,254,553✔
90

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

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
4,253,820✔
97
  if (pTq->pHandle == NULL) {
4,254,369✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
4,254,302✔
101

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

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

110
  return tqInitialize(pTq);
4,254,553✔
111
}
112

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

118
  return tqMetaOpen(pTq);
4,254,486✔
119
}
120

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

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

132
  taosHashCleanup(pTq->pHandle);
4,254,651✔
133
  taosHashCleanup(pTq->pOffset);
4,254,356✔
134
  taosMemoryFree(pTq->path);
4,254,651✔
135
  tqMetaClose(pTq);
4,254,651✔
136
  qDebug("vgId:%d end to close tq", vgId);
4,254,447✔
137

138
  taosMemoryFree(pTq);
4,254,651✔
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,
16,208,379✔
173
                      int32_t vgId) {
174
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
16,208,379✔
UNCOV
175
    return TSDB_CODE_INVALID_PARA;
×
176
  }
177
  int64_t sver = 0, ever = 0;
16,208,379✔
178
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
16,208,379✔
179

180
  char buf1[TSDB_OFFSET_LEN] = {0};
16,208,379✔
181
  char buf2[TSDB_OFFSET_LEN] = {0};
16,208,379✔
182
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
16,208,379✔
183
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
16,208,379✔
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,
16,208,379✔
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);
16,208,379✔
189
}
190

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

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

206
  tDecoderClear(&decoder);
9,981,025✔
207

208
  STqOffset* pOffset = &vgOffset.offset;
9,981,025✔
209

210
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
9,981,025✔
211
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
282,740✔
212
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
213
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
9,698,285✔
214
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
9,697,991✔
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;
9,982,316✔
223
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
9,982,583✔
224
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
9,981,649✔
225
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
6,607✔
226
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
227
    goto end;  // no need to update the offset value
6,607✔
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));
9,975,042✔
237
  if (code != 0) {
9,972,829✔
238
    goto end;
×
239
  }
240

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

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

256
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
1,336✔
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,336✔
262
  taosWLockLatch(&pTq->lock);
1,336✔
263

264
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
1,336✔
265
  if (pHandle == NULL) {
1,336✔
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,336✔
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,336✔
285

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

292
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
16,419,442✔
293
  if (pTq == NULL || pMsg == NULL) {
16,419,442✔
294
    return TSDB_CODE_INVALID_PARA;
×
295
  }
296
  SMqPollReq req = {0};
16,421,024✔
297
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
16,420,973✔
298
  if (code < 0) {
16,413,556✔
299
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
300
    code = TSDB_CODE_INVALID_MSG;
×
301
    goto END;
×
302
  }
303
  if (req.rawData == 1){
16,413,556✔
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;
16,413,556✔
312
  int32_t      reqEpoch = req.epoch;
16,413,556✔
313
  int32_t      vgId = TD_VID(pTq->pVnode);
16,413,556✔
314
  STqHandle*   pHandle = NULL;
16,415,359✔
315

316
  while (1) {
10,255✔
317
    taosWLockLatch(&pTq->lock);
16,406,073✔
318
    // 1. find handle
319
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
16,423,592✔
320
    if (code != TDB_CODE_SUCCESS) {
16,417,838✔
321
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
2,058✔
322
      taosWUnLockLatch(&pTq->lock);
2,058✔
323
      return code;
2,058✔
324
    }
325

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

335
    bool exec = tqIsHandleExec(pHandle);
16,413,973✔
336
    if (!exec) {
16,418,363✔
337
      tqSetHandleExec(pHandle);
16,410,989✔
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,
16,404,182✔
340
              req.subKey, pHandle);
341
      taosWUnLockLatch(&pTq->lock);
16,421,737✔
342
      break;
16,420,236✔
343
    }
344
    taosWUnLockLatch(&pTq->lock);
7,384✔
345

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

352
  // 3. update the epoch value
353
  if (pHandle->epoch < reqEpoch) {
16,420,236✔
354
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
457,150✔
355
            reqEpoch);
356
    pHandle->epoch = reqEpoch;
456,820✔
357
  }
358

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

364
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
16,419,008✔
365
  tqSetHandleIdle(pHandle);
16,414,754✔
366

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

370
END:
16,419,292✔
371
  tDestroySMqPollReq(&req);
16,420,541✔
372
  return code;
16,419,687✔
373
}
374

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

382
  SMqVgOffset vgOffset = {0};
922✔
383

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

391
  tDecoderClear(&decoder);
922✔
392

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

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

405
  void* buf = rpcMallocCont(len);
922✔
406
  if (buf == NULL) {
922✔
407
    return terrno;
×
408
  }
409
  SEncoder encoder = {0};
922✔
410
  tEncoderInit(&encoder, buf, len);
922✔
411
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
922✔
412
  tEncoderClear(&encoder);
922✔
413
  if (code < 0) {
922✔
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};
922✔
419

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

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

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

439
  // 1. find handle
440
  taosRLockLatch(&pTq->lock);
668✔
441
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
668✔
442
  if (pHandle == NULL) {
668✔
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) {
668✔
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;
668✔
457
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
668✔
458
  taosRUnLockLatch(&pTq->lock);
668✔
459

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

466
  if (req.useSnapshot == true) {
668✔
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;
668✔
473

474
  if (reqOffset.type == TMQ_OFFSET__LOG) {
668✔
475
    dataRsp.rspOffset.version = reqOffset.version;
334✔
476
  } else if (reqOffset.type < 0) {
334✔
477
    STqOffset* pOffset = NULL;
334✔
478
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
334✔
479
    if (code == 0) {
334✔
480
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
334✔
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;
334✔
487
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
334✔
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);
668✔
506

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

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

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

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

528
      if (exec) {
298,295✔
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));
298,295✔
537
      if (code != 0) {
298,001✔
538
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
539
      }
540
      taosWUnLockLatch(&pTq->lock);
298,001✔
541
      break;
298,295✔
542
    }
543
  }
544

545
  taosWLockLatch(&pTq->lock);
298,921✔
546
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
298,921✔
547
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
91,153✔
548
  }
549
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
298,921✔
550
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
280,241✔
551
  }
552

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

558
  return 0;
298,576✔
559
}
560

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

569
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
1,260,023✔
570
  ret = tDecodeSMqRebVgReq(&dc, &req);
1,260,023✔
571
  if (ret < 0) {
1,259,397✔
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,259,397✔
576
         req.oldConsumerId, req.newConsumerId);
577

578
  taosRLockLatch(&pTq->lock);  
1,260,734✔
579
  STqHandle* pHandle = NULL;
1,260,023✔
580
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,260,023✔
581
  if (code != 0){
1,259,680✔
582
    tqInfo("vgId:%d, tmq subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
406,169✔
583
  }
584
  taosRUnLockLatch(&pTq->lock);
1,260,023✔
585
  if (pHandle == NULL) {
1,260,023✔
586
    if (req.oldConsumerId != -1) {
406,512✔
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) {
406,512✔
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};
406,512✔
596
    ret = tqMetaCreateHandle(pTq, &req, &handle);
406,512✔
597
    if (ret < 0) {
406,512✔
598
      tqDestroyTqHandle(&handle);
×
599
      goto end;
×
600
    }
601
    taosWLockLatch(&pTq->lock);
406,512✔
602
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
406,512✔
603
    taosWUnLockLatch(&pTq->lock);
404,841✔
604
  } else {
605
    int maxLoop = MAX_LOOP;
853,511✔
606
    while (maxLoop-- > 0) {
853,511✔
607
      taosWLockLatch(&pTq->lock);
853,511✔
608
      bool exec = tqIsHandleExec(pHandle);
853,511✔
609
      if (exec) {
853,511✔
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);
×
614
        continue;
×
615
      }
616
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
854,163✔
617
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
652✔
618
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
619
        // tqUnregisterPushHandle(pTq, pHandle);
620
        STqHandle handle = {0};
652✔
621
        ret = tqMetaCreateHandle(pTq, &req, &handle);
652✔
622
        if (ret < 0) {
652✔
623
          tqDestroyTqHandle(&handle);
×
624
          taosWUnLockLatch(&pTq->lock);
×
625
          goto end;
×
626
        }
627
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
652✔
628
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
652✔
629
         req.oldConsumerId, req.newConsumerId);
630
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
852,859✔
631
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
46,582✔
632
      } else {
633
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
806,277✔
634
               req.newConsumerId);
635

636
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
806,277✔
637
        atomic_store_32(&pHandle->epoch, 0);
806,277✔
638
        // tqUnregisterPushHandle(pTq, pHandle);
639
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
806,277✔
640
      }
641
      taosWUnLockLatch(&pTq->lock);
849,357✔
642
      break;
853,138✔
643
    }
644
  }
645

646
end:
1,256,685✔
647
  if (req.schema.pSchema != NULL) {
1,257,669✔
648
    taosMemoryFree(req.schema.pSchema);
722,372✔
649
  }
650
  tDecoderClear(&dc);
1,255,647✔
651
  return ret;
1,250,128✔
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