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

taosdata / TDengine / #4971

28 Feb 2026 08:05AM UTC coverage: 67.671% (-0.04%) from 67.707%
#4971

push

travis-ci

web-flow
fix(planner): disable project block merge in non-top-level subplans (#34617)

208281 of 307783 relevant lines covered (67.67%)

130135765.28 hits per line

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

74.33
/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; }
29,917,909✔
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
28,591,844✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
28,590,476✔
31
#define MAX_LOOP 100
32

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

38
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
472,334✔
39
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
342,864✔
40
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
343,182✔
41
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
128,884✔
42
    tqReaderClose(pData->execHandle.pTqReader);
105,431✔
43
    walCloseReader(pData->pWalReader);
105,431✔
44
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
105,431✔
45
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
23,453✔
46
    walCloseReader(pData->pWalReader);
23,453✔
47
    tqReaderClose(pData->execHandle.pTqReader);
23,453✔
48
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
23,453✔
49
    nodesDestroyNode(pData->execHandle.execTb.node);
23,453✔
50
  }
51
  if (pData->msg != NULL) {
471,054✔
52
    rpcFreeCont(pData->msg->pCont);
×
53
    taosMemoryFree(pData->msg);
×
54
    pData->msg = NULL;
×
55
  }
56
  if (pData->block != NULL) {
472,651✔
57
    blockDataDestroy(pData->block);
×
58
  }
59
  if (pData->pRef) {
472,651✔
60
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
459,322✔
61
  }
62
  taosHashCleanup(pData->tableCreateTimeHash);
472,314✔
63
}
64

65
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
4,044,742✔
66
  if (pLeft == NULL || pRight == NULL) {
4,044,742✔
67
    return false;
×
68
  }
69
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
7,789,903✔
70
         pLeft->val.version == pRight->val.version;
3,746,027✔
71
}
72

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

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

83
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
3,980,163✔
84
  if (pTq == NULL) {
3,978,665✔
85
    return terrno;
×
86
  }
87

88
  pVnode->pTq = pTq;
3,978,665✔
89
  pTq->pVnode = pVnode;
3,978,581✔
90

91
  pTq->path = taosStrdup(path);
3,978,996✔
92
  if (pTq->path == NULL) {
3,977,859✔
93
    return terrno;
×
94
  }
95

96
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
3,978,167✔
97
  if (pTq->pHandle == NULL) {
3,980,163✔
98
    return terrno;
×
99
  }
100
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
3,979,438✔
101

102
  taosInitRWLatch(&pTq->lock);
3,980,081✔
103

104
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
3,979,747✔
105
  if (pTq->pPushMgr == NULL) {
3,979,828✔
106
    return terrno;
×
107
  }
108

109
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
3,980,578✔
110
  if (pTq->pOffset == NULL) {
3,980,578✔
111
    return terrno;
×
112
  }
113
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
3,980,578✔
114

115
  return tqInitialize(pTq);
3,979,828✔
116
}
117

118
int32_t tqInitialize(STQ* pTq) {
3,980,220✔
119
  if (pTq == NULL) {
3,980,220✔
120
    return TSDB_CODE_INVALID_PARA;
×
121
  }
122

123
  return tqMetaOpen(pTq);
3,980,220✔
124
}
125

126
void tqClose(STQ* pTq) {
3,980,382✔
127
  qDebug("start to close tq");
3,980,382✔
128
  if (pTq == NULL) {
3,980,578✔
129
    return;
×
130
  }
131

132
  int32_t vgId = 0;
3,980,578✔
133
  if (pTq->pVnode != NULL) {
3,980,578✔
134
    vgId = TD_VID(pTq->pVnode);
3,980,578✔
135
  }
136

137
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
3,980,578✔
138
  while (pIter) {
3,991,107✔
139
    STqHandle* pHandle = *(STqHandle**)pIter;
10,529✔
140
    if (pHandle->msg != NULL) {
10,529✔
141
      tqPushEmptyDataRsp(pHandle, vgId);
10,529✔
142
      rpcFreeCont(pHandle->msg->pCont);
10,529✔
143
      taosMemoryFree(pHandle->msg);
10,529✔
144
      pHandle->msg = NULL;
10,529✔
145
    }
146
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
10,529✔
147
  }
148

149
  taosHashCleanup(pTq->pHandle);
3,980,578✔
150
  taosHashCleanup(pTq->pPushMgr);
3,980,578✔
151
  taosHashCleanup(pTq->pOffset);
3,980,578✔
152
  taosMemoryFree(pTq->path);
3,980,578✔
153
  tqMetaClose(pTq);
3,980,578✔
154
  qDebug("vgId:%d end to close tq", vgId);
3,980,578✔
155

156
  taosMemoryFree(pTq);
3,980,869✔
157
}
158

159
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
174,732✔
160
  if (pHandle == NULL) {
174,732✔
161
    return;
×
162
  }
163
  int32_t    code = 0;
174,732✔
164
  SMqPollReq req = {0};
174,732✔
165
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
174,732✔
166
  if (code < 0) {
174,732✔
167
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
168
    return;
×
169
  }
170

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

183
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
174,732✔
184
  if (code != 0) {
174,378✔
185
    tqError("tqSendDataRsp failed, code:%d", code);
×
186
  }
187
  tDeleteMqDataRsp(&dataRsp);
174,378✔
188
}
189

190
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
18,509,487✔
191
                      int32_t vgId) {
192
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
18,509,487✔
193
    return TSDB_CODE_INVALID_PARA;
×
194
  }
195
  int64_t sver = 0, ever = 0;
18,509,487✔
196
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
18,509,487✔
197

198
  char buf1[TSDB_OFFSET_LEN] = {0};
18,509,487✔
199
  char buf2[TSDB_OFFSET_LEN] = {0};
18,509,487✔
200
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
18,509,487✔
201
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
18,509,487✔
202

203
  tqDebug("tmq poll vgId:%d consumer:0x%" PRIx64 " (epoch %d) start to send rsp, block num:%d, req:%s, rsp:%s, QID:0x%" PRIx64,
18,509,487✔
204
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
205

206
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
18,509,487✔
207
}
208

209
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
4,308,826✔
210
  if (pTq == NULL) {
4,308,826✔
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213
  SMqVgOffset vgOffset = {0};
4,308,826✔
214
  int32_t     vgId = TD_VID(pTq->pVnode);
4,308,826✔
215

216
  int32_t  code = 0;
4,308,826✔
217
  SDecoder decoder;
4,308,773✔
218
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
4,308,826✔
219
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
4,308,826✔
220
    code = TSDB_CODE_INVALID_MSG;
×
221
    goto end;
×
222
  }
223

224
  tDecoderClear(&decoder);
4,308,550✔
225

226
  STqOffset* pOffset = &vgOffset.offset;
4,308,826✔
227

228
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
4,308,826✔
229
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
311,733✔
230
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
231
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
3,996,496✔
232
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
3,996,220✔
233
            pOffset->val.version);
234
  } else {
235
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
236
    code = TSDB_CODE_INVALID_MSG;
×
237
    goto end;
×
238
  }
239

240
  STqOffset* pSavedOffset = NULL;
4,309,147✔
241
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
4,308,826✔
242
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
4,308,826✔
243
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
1,191✔
244
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
245
    goto end;  // no need to update the offset value
1,191✔
246
  }
247

248
  // if (pOffset->val.type == TMQ_OFFSET__LOG && vgOffset.markWal) {
249
  //   int32_t ret = walSetKeepVersion(pTq->pVnode->pWal, pOffset->val.version);
250
  //   tqDebug("set wal reader keep version to %" PRId64 " for vgId:%d sub:%s, code:%d", pOffset->val.version, vgId,
251
  //           pOffset->subKey, ret);
252
  // }
253
  // save the new offset value
254
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
4,306,769✔
255
  if (code != 0) {
4,306,466✔
256
    goto end;
×
257
  }
258

259
  return 0;
4,306,466✔
260
end:
1,191✔
261
  tOffsetDestroy(&vgOffset.offset.val);
1,191✔
262
  return code;
1,191✔
263
}
264

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

274
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
1,232✔
275
    code = TSDB_CODE_OUT_OF_MEMORY;
×
276
    goto end;
×
277
  }
278

279
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
1,232✔
280
  taosWLockLatch(&pTq->lock);
1,232✔
281

282
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
1,232✔
283
  if (pHandle == NULL) {
1,232✔
284
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
285
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
286
    taosWUnLockLatch(&pTq->lock);
×
287
    goto end;
×
288
  }
289

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

299
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
300
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
301
  tqUnregisterPushHandle(pTq, pHandle);
1,232✔
302
  taosWUnLockLatch(&pTq->lock);
1,232✔
303

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

310
int32_t tqProcessPollPush(STQ* pTq) {
9,156,791✔
311
  if (pTq == NULL) {
9,156,791✔
312
    return TSDB_CODE_INVALID_PARA;
×
313
  }
314
  int32_t vgId = TD_VID(pTq->pVnode);
9,156,791✔
315
  taosWLockLatch(&pTq->lock);
9,156,791✔
316
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
9,156,791✔
317
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
9,101,380✔
318

319
    while (pIter) {
18,948,252✔
320
      STqHandle* pHandle = *(STqHandle**)pIter;
9,846,872✔
321
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
9,846,872✔
322

323
      if (pHandle->msg == NULL) {
9,846,872✔
324
        tqError("pHandle->msg should not be null");
×
325
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
326
        break;
×
327
      } else {
328
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
19,693,744✔
329
                       .pCont = pHandle->msg->pCont,
9,846,872✔
330
                       .contLen = pHandle->msg->contLen,
9,846,872✔
331
                       .info = pHandle->msg->info};
9,846,872✔
332
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
9,846,872✔
333
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
334
        }
335
        taosMemoryFree(pHandle->msg);
9,846,872✔
336
        pHandle->msg = NULL;
9,846,872✔
337
      }
338

339
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
9,846,872✔
340
    }
341

342
    taosHashClear(pTq->pPushMgr);
9,101,380✔
343
  }
344
  taosWUnLockLatch(&pTq->lock);
9,156,791✔
345
  return 0;
9,156,791✔
346
}
347

348
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
28,593,733✔
349
  if (pTq == NULL || pMsg == NULL) {
28,593,733✔
350
    return TSDB_CODE_INVALID_PARA;
×
351
  }
352
  SMqPollReq req = {0};
28,594,724✔
353
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
28,594,724✔
354
  if (code < 0) {
28,590,913✔
355
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
356
    code = TSDB_CODE_INVALID_MSG;
×
357
    goto END;
×
358
  }
359
  if (req.rawData == 1){
28,590,913✔
360
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
361
    if (req.uidHash == NULL) {
×
362
      tqError("tq poll rawData taosHashInit failed");
×
363
      code = terrno;
×
364
      goto END;
×
365
    }
366
  }
367
  int64_t      consumerId = req.consumerId;
28,590,913✔
368
  int32_t      reqEpoch = req.epoch;
28,590,913✔
369
  int32_t      vgId = TD_VID(pTq->pVnode);
28,590,913✔
370
  STqHandle*   pHandle = NULL;
28,591,885✔
371

372
  while (1) {
59,901✔
373
    taosWLockLatch(&pTq->lock);
28,648,417✔
374
    // 1. find handle
375
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
28,651,584✔
376
    if (code != TDB_CODE_SUCCESS) {
28,647,503✔
377
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
378
      taosWUnLockLatch(&pTq->lock);
×
379
      return code;
×
380
    }
381

382
    // 2. check rebalance status
383
    if (pHandle->consumerId != consumerId) {
28,647,503✔
384
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
×
385
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
386
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
387
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
388
      taosWUnLockLatch(&pTq->lock);
×
389
      goto END;
×
390
    }
391

392
    bool exec = tqIsHandleExec(pHandle);
28,650,985✔
393
    if (!exec) {
28,649,847✔
394
      tqSetHandleExec(pHandle);
28,591,844✔
395
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
396
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
28,588,734✔
397
              req.subKey, pHandle);
398
      taosWUnLockLatch(&pTq->lock);
28,597,366✔
399
      break;
28,595,701✔
400
    }
401
    taosWUnLockLatch(&pTq->lock);
58,003✔
402

403
    tqDebug("tmq poll: consumer:0x%" PRIx64
59,901✔
404
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
405
            consumerId, vgId, req.subKey, pHandle);
406
    taosMsleep(10);
59,901✔
407
  }
408

409
  // 3. update the epoch value
410
  if (pHandle->epoch < reqEpoch) {
28,595,701✔
411
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
463,066✔
412
            reqEpoch);
413
    pHandle->epoch = reqEpoch;
463,066✔
414
  }
415

416
  char buf[TSDB_OFFSET_LEN] = {0};
28,595,701✔
417
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &req.reqOffset);
28,595,701✔
418
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
28,595,701✔
419
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
420

421
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
28,595,350✔
422
  tqSetHandleIdle(pHandle);
28,590,476✔
423

424
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
28,590,050✔
425
          req.subKey, pHandle);
426

427
END:
28,596,348✔
428
  tDestroySMqPollReq(&req);
28,595,353✔
429
  return code;
28,595,353✔
430
}
431

432
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
308✔
433
  if (pTq == NULL || pMsg == NULL) {
308✔
434
    return TSDB_CODE_INVALID_PARA;
×
435
  }
436
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
308✔
437
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
308✔
438

439
  SMqVgOffset vgOffset = {0};
308✔
440

441
  SDecoder decoder;
308✔
442
  tDecoderInit(&decoder, (uint8_t*)data, len);
308✔
443
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
308✔
444
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
445
    return terrno;
×
446
  }
447

448
  tDecoderClear(&decoder);
308✔
449

450
  STqOffset* pSavedOffset = NULL;
308✔
451
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
308✔
452
  if (code != 0) {
308✔
453
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
454
  }
455
  vgOffset.offset = *pSavedOffset;
308✔
456

457
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
308✔
458
  if (code < 0) {
308✔
459
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
460
  }
461

462
  void* buf = rpcMallocCont(len);
308✔
463
  if (buf == NULL) {
308✔
464
    return terrno;
×
465
  }
466
  SEncoder encoder = {0};
308✔
467
  tEncoderInit(&encoder, buf, len);
308✔
468
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
308✔
469
  tEncoderClear(&encoder);
308✔
470
  if (code < 0) {
308✔
471
    rpcFreeCont(buf);
×
472
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
473
  }
474

475
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
308✔
476

477
  tmsgSendRsp(&rsp);
308✔
478
  return 0;
308✔
479
}
480

481
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
616✔
482
  if (pTq == NULL || pMsg == NULL) {
616✔
483
    return TSDB_CODE_INVALID_PARA;
×
484
  }
485
  int32_t    code = 0;
616✔
486
  SMqPollReq req = {0};
616✔
487
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
616✔
488
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
489
    return TSDB_CODE_INVALID_MSG;
×
490
  }
491

492
  int64_t      consumerId = req.consumerId;
616✔
493
  STqOffsetVal reqOffset = req.reqOffset;
616✔
494
  int32_t      vgId = TD_VID(pTq->pVnode);
616✔
495

496
  // 1. find handle
497
  taosRLockLatch(&pTq->lock);
616✔
498
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
616✔
499
  if (pHandle == NULL) {
616✔
500
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
501
    taosRUnLockLatch(&pTq->lock);
×
502
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
503
  }
504

505
  // 2. check rebalance status
506
  if (pHandle->consumerId != consumerId) {
616✔
507
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
508
            consumerId, vgId, req.subKey, pHandle->consumerId);
509
    taosRUnLockLatch(&pTq->lock);
×
510
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
511
  }
512

513
  int64_t sver = 0, ever = 0;
616✔
514
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
616✔
515
  taosRUnLockLatch(&pTq->lock);
616✔
516

517
  SMqDataRsp dataRsp = {0};
616✔
518
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
616✔
519
  if (code != 0) {
616✔
520
    return code;
×
521
  }
522

523
  if (req.useSnapshot == true) {
616✔
524
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
525
    code = TSDB_CODE_INVALID_PARA;
×
526
    goto END;
×
527
  }
528

529
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
616✔
530

531
  if (reqOffset.type == TMQ_OFFSET__LOG) {
616✔
532
    dataRsp.rspOffset.version = reqOffset.version;
308✔
533
  } else if (reqOffset.type < 0) {
308✔
534
    STqOffset* pOffset = NULL;
308✔
535
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
308✔
536
    if (code == 0) {
308✔
537
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
308✔
538
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
539
        code = TSDB_CODE_INVALID_PARA;
×
540
        goto END;
×
541
      }
542

543
      dataRsp.rspOffset.version = pOffset->val.version;
308✔
544
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
308✔
545
             req.subKey, dataRsp.rspOffset.version);
546
    } else {
547
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
×
548
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
×
549
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
550
        dataRsp.rspOffset.version = ever;
×
551
      }
552
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
×
553
             dataRsp.rspOffset.version);
554
    }
555
  } else {
556
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
557
            reqOffset.type);
558
    code = TSDB_CODE_INVALID_PARA;
×
559
    goto END;
×
560
  }
561

562
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
616✔
563

564
END:
616✔
565
  tDeleteMqDataRsp(&dataRsp);
616✔
566
  return code;
616✔
567
}
568

569
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
290,841✔
570
  if (pTq == NULL || msg == NULL) {
290,841✔
571
    return TSDB_CODE_INVALID_PARA;
×
572
  }
573
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
290,841✔
574
  int32_t        vgId = TD_VID(pTq->pVnode);
290,841✔
575

576
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
290,841✔
577
  int32_t code = 0;
290,841✔
578

579
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
290,841✔
580
  if (pHandle) {
290,841✔
581
    while (1) {
×
582
      taosWLockLatch(&pTq->lock);
290,273✔
583
      bool exec = tqIsHandleExec(pHandle);
290,273✔
584

585
      if (exec) {
290,273✔
586
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
587
               pHandle->subKey, pHandle);
588
        taosWUnLockLatch(&pTq->lock);
×
589
        taosMsleep(10);
×
590
        continue;
×
591
      }
592
      tqUnregisterPushHandle(pTq, pHandle);
290,273✔
593
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
290,273✔
594
      if (code != 0) {
290,273✔
595
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
596
      }
597
      taosWUnLockLatch(&pTq->lock);
290,273✔
598
      break;
290,273✔
599
    }
600
  }
601

602
  taosWLockLatch(&pTq->lock);
290,841✔
603
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
290,841✔
604
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
119,801✔
605
  }
606
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
290,841✔
607
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
274,810✔
608
  }
609

610
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
290,875✔
611
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
612
  }
613
  taosWUnLockLatch(&pTq->lock);
289,937✔
614

615
  return 0;
290,279✔
616
}
617

618
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,370,068✔
619
  if (pTq == NULL || msg == NULL) {
1,370,068✔
620
    return TSDB_CODE_INVALID_PARA;
×
621
  }
622
  int         ret = 0;
1,370,398✔
623
  SMqRebVgReq req = {0};
1,370,398✔
624
  SDecoder    dc = {0};
1,370,398✔
625

626
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
1,370,398✔
627
  ret = tDecodeSMqRebVgReq(&dc, &req);
1,370,398✔
628
  if (ret < 0) {
1,369,842✔
629
    goto end;
×
630
  }
631

632
  tqInfo("vgId:%d, tmq subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
1,369,842✔
633
         req.oldConsumerId, req.newConsumerId);
634

635
  taosRLockLatch(&pTq->lock);  
1,370,493✔
636
  STqHandle* pHandle = NULL;
1,370,398✔
637
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
1,370,398✔
638
  if (code != 0){
1,368,799✔
639
    tqInfo("vgId:%d, tmq subscribe req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
392,466✔
640
  }
641
  taosRUnLockLatch(&pTq->lock);
1,370,062✔
642
  if (pHandle == NULL) {
1,369,763✔
643
    if (req.oldConsumerId != -1) {
393,445✔
644
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
645
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
646
    }
647
    if (req.newConsumerId == -1) {
393,445✔
648
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
649
      ret = TSDB_CODE_INVALID_PARA;
×
650
      goto end;
×
651
    }
652
    STqHandle handle = {0};
393,445✔
653
    ret = tqMetaCreateHandle(pTq, &req, &handle);
393,445✔
654
    if (ret < 0) {
393,445✔
655
      tqDestroyTqHandle(&handle);
×
656
      goto end;
×
657
    }
658
    taosWLockLatch(&pTq->lock);
393,445✔
659
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
393,445✔
660
    taosWUnLockLatch(&pTq->lock);
391,946✔
661
  } else {
662
    int maxLoop = MAX_LOOP;
976,318✔
663
    while (maxLoop-- > 0) {
976,953✔
664
      taosWLockLatch(&pTq->lock);
976,953✔
665
      bool exec = tqIsHandleExec(pHandle);
976,651✔
666
      if (exec) {
976,651✔
667
        tqInfo("vgId:%d, topic:%s, subscribe is executing, subscribe wait for 10ms and retry, pHandle:%p",
×
668
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
669
        taosWUnLockLatch(&pTq->lock);
×
670
        taosMsleep(10);
×
671
        continue;
635✔
672
      }
673
      if (req.subType == TOPIC_SUB_TYPE__COLUMN && strcmp(req.qmsg, pHandle->execHandle.execCol.qmsg) != 0) {
977,287✔
674
        tqInfo("vgId:%d, topic:%s, subscribe qmsg changed from %s to %s, need recreate handle", pTq->pVnode->config.vgId,
636✔
675
               pHandle->subKey, pHandle->execHandle.execCol.qmsg, req.qmsg);
676
        tqUnregisterPushHandle(pTq, pHandle);
636✔
677
        STqHandle handle = {0};
636✔
678
        ret = tqMetaCreateHandle(pTq, &req, &handle);
636✔
679
        if (ret < 0) {
636✔
680
          tqDestroyTqHandle(&handle);
×
681
          taosWUnLockLatch(&pTq->lock);
×
682
          goto end;
×
683
        }
684
        ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
636✔
685
        tqInfo("vgId:%d, reload subscribe req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
636✔
686
         req.oldConsumerId, req.newConsumerId);
687
      } else if (pHandle->consumerId == req.newConsumerId) {  // do nothing
976,015✔
688
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
44,054✔
689
      } else {
690
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
931,984✔
691
               req.newConsumerId);
692

693
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
931,984✔
694
        atomic_store_32(&pHandle->epoch, 0);
932,263✔
695
        tqUnregisterPushHandle(pTq, pHandle);
932,263✔
696
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
931,909✔
697
      }
698
      taosWUnLockLatch(&pTq->lock);
975,968✔
699
      break;
975,952✔
700
    }
701
  }
702

703
end:
1,366,545✔
704
  if (req.schema.pSchema != NULL) {
1,369,113✔
705
    taosMemoryFree(req.schema.pSchema);
833,621✔
706
  }
707
  tDecoderClear(&dc);
1,366,582✔
708
  return ret;
1,364,207✔
709
}
710

711
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