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

taosdata / TDengine / #5012

03 Apr 2026 03:59PM UTC coverage: 72.305% (+0.005%) from 72.3%
#5012

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%)

13105 existing lines in 173 files now uncovered.

257446 of 356056 relevant lines covered (72.3%)

131779375.07 hits per line

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

70.86
/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,701,403✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
15,537,451✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
15,537,289✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
466,793✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
346,426✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
346,434✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
121,707✔
42
    tqReaderClose(pData->execHandle.pTqReader);
95,757✔
43
    walCloseReader(pData->pWalReader);
95,757✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
95,757✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
25,950✔
46
    walCloseReader(pData->pWalReader);
25,950✔
47
    tqReaderClose(pData->execHandle.pTqReader);
25,950✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
25,950✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
25,950✔
50
  }
51
  if (pData->msg != NULL) {
466,800✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
467,158✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
467,158✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
454,058✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
466,479✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
9,467,803✔
66
  if (pLeft == NULL || pRight == NULL) {
9,467,803✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
18,617,859✔
70
         pLeft->val.version == pRight->val.version;
9,150,397✔
71
}
72

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

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

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

88
  pVnode->pTq = pTq;
4,415,103✔
89
  pTq->pVnode = pVnode;
4,415,103✔
90

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

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

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

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

110
  return tqInitialize(pTq);
4,415,103✔
111
}
112

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

118
  return tqMetaOpen(pTq);
4,415,103✔
119
}
120

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

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

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

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

180
  char buf1[TSDB_OFFSET_LEN] = {0};
15,309,105✔
181
  char buf2[TSDB_OFFSET_LEN] = {0};
15,309,105✔
182
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
15,308,821✔
183
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
15,308,380✔
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,
15,309,105✔
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);
15,309,105✔
189
}
190

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

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

206
  tDecoderClear(&decoder);
9,731,799✔
207

208
  STqOffset* pOffset = &vgOffset.offset;
9,731,511✔
209

210
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
9,731,511✔
211
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
327,199✔
212
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
213
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
9,405,022✔
214
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
9,405,742✔
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,732,176✔
223
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
9,732,176✔
224
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
9,732,154✔
225
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
4,829✔
226
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
227
    goto end;  // no need to update the offset value
4,829✔
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,726,984✔
237
  if (code != 0) {
9,725,957✔
238
    goto end;
×
239
  }
240

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

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

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

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

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

292
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
15,546,215✔
293
  if (pTq == NULL || pMsg == NULL) {
15,546,215✔
294
    return TSDB_CODE_INVALID_PARA;
×
295
  }
296
  SMqPollReq req = {0};
15,546,975✔
297
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
15,546,605✔
298
  if (code < 0) {
15,537,857✔
299
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
300
    code = TSDB_CODE_INVALID_MSG;
×
301
    goto END;
×
302
  }
303
  if (req.rawData == 1){
15,537,857✔
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,537,857✔
312
  int32_t      reqEpoch = req.epoch;
15,537,857✔
313
  int32_t      vgId = TD_VID(pTq->pVnode);
15,537,857✔
314
  STqHandle*   pHandle = NULL;
15,541,746✔
315

316
  while (1) {
12,962✔
317
    taosWLockLatch(&pTq->lock);
15,543,314✔
318
    // 1. find handle
319
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
15,554,720✔
320
    if (code != TDB_CODE_SUCCESS) {
15,548,872✔
321
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
3,428✔
322
      taosWUnLockLatch(&pTq->lock);
3,428✔
323
      return code;
3,428✔
324
    }
325

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

335
    bool exec = tqIsHandleExec(pHandle);
15,546,112✔
336
    if (!exec) {
15,546,541✔
337
      tqSetHandleExec(pHandle);
15,537,451✔
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,532,109✔
340
              req.subKey, pHandle);
341
      taosWUnLockLatch(&pTq->lock);
15,541,849✔
342
      break;
15,544,377✔
343
    }
344
    taosWUnLockLatch(&pTq->lock);
9,112✔
345

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

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

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

364
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
15,544,718✔
365
  tqSetHandleIdle(pHandle);
15,537,289✔
366

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

370
END:
15,542,843✔
371
  tDestroySMqPollReq(&req);
15,545,027✔
372
  return code;
15,545,027✔
373
}
374

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

382
  SMqVgOffset vgOffset = {0};
335✔
383

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

391
  tDecoderClear(&decoder);
335✔
392

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

545
  taosWLockLatch(&pTq->lock);
286,877✔
546
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
286,877✔
547
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
89,788✔
548
  }
549
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
286,522✔
550
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
267,542✔
551
  }
552

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

558
  return 0;
286,204✔
559
}
560

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

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

578
  taosRLockLatch(&pTq->lock);  
1,266,368✔
579
  STqHandle* pHandle = NULL;
1,265,990✔
580
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,265,990✔
581
  if (code != 0){
1,265,365✔
582
    tqInfo("vgId:%d, tmq subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
396,339✔
583
  }
584
  taosRUnLockLatch(&pTq->lock);
1,265,990✔
585
  if (pHandle == NULL) {
1,265,367✔
586
    if (req.oldConsumerId != -1) {
396,964✔
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) {
396,964✔
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};
396,964✔
596
    ret = tqMetaCreateHandle(pTq, &req, &handle);
396,964✔
597
    if (ret < 0) {
396,964✔
598
      tqDestroyTqHandle(&handle);
×
599
      goto end;
×
600
    }
601
    taosWLockLatch(&pTq->lock);
396,964✔
602
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
396,964✔
603
    taosWUnLockLatch(&pTq->lock);
396,625✔
604
  } else {
605
    int maxLoop = MAX_LOOP;
868,403✔
606
    while (maxLoop-- > 0) {
868,687✔
607
      taosWLockLatch(&pTq->lock);
868,687✔
608
      bool exec = tqIsHandleExec(pHandle);
869,026✔
609
      if (exec) {
867,942✔
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;
284✔
615
      }
616
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
868,622✔
617
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
680✔
618
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
619
        // tqUnregisterPushHandle(pTq, pHandle);
620
        STqHandle handle = {0};
680✔
621
        ret = tqMetaCreateHandle(pTq, &req, &handle);
680✔
622
        if (ret < 0) {
680✔
623
          tqDestroyTqHandle(&handle);
×
624
          taosWUnLockLatch(&pTq->lock);
×
625
          goto end;
×
626
        }
627
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
680✔
628
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
680✔
629
         req.oldConsumerId, req.newConsumerId);
630
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
867,140✔
631
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
46,542✔
632
      } else {
633
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
821,804✔
634
               req.newConsumerId);
635

636
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
821,804✔
637
        atomic_store_32(&pHandle->epoch, 0);
821,804✔
638
        // tqUnregisterPushHandle(pTq, pHandle);
639
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
821,804✔
640
      }
641
      taosWUnLockLatch(&pTq->lock);
867,145✔
642
      break;
867,963✔
643
    }
644
  }
645

646
end:
1,263,912✔
647
  if (req.schema.pSchema != NULL) {
1,264,957✔
648
    taosMemoryFree(req.schema.pSchema);
746,424✔
649
  }
650
  tDecoderClear(&dc);
1,261,789✔
651
  return ret;
1,260,945✔
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