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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

46.2
/source/dnode/vnode/src/tq/tq.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tq.h"
17
#include "osDef.h"
18
#include "taoserror.h"
19
#include "stream.h"
20
#include "vnd.h"
21

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

27
static FORCE_INLINE bool tqIsHandleExec(STqHandle* pHandle) { return pHandle != NULL ? TMQ_HANDLE_STATUS_EXEC == pHandle->status : true; }
1,042!
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
613✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
613✔
30

31
void tqDestroyTqHandle(void* data) {
299✔
32
  if (data == NULL) return;
299!
33
  STqHandle* pData = (STqHandle*)data;
299✔
34
  qDestroyTask(pData->execHandle.task);
299✔
35

36
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
299✔
37
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
297!
38
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
2!
39
    tqReaderClose(pData->execHandle.pTqReader);
2✔
40
    walCloseReader(pData->pWalReader);
2✔
41
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
2✔
42
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
×
43
    walCloseReader(pData->pWalReader);
×
44
    tqReaderClose(pData->execHandle.pTqReader);
×
45
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
×
46
    nodesDestroyNode(pData->execHandle.execTb.node);
×
47
  }
48
  if (pData->msg != NULL) {
299!
49
    rpcFreeCont(pData->msg->pCont);
×
50
    taosMemoryFree(pData->msg);
×
51
    pData->msg = NULL;
×
52
  }
53
  if (pData->block != NULL) {
299!
54
    blockDataDestroy(pData->block);
×
55
  }
56
  if (pData->pRef) {
299!
57
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
299✔
58
  }
59
  taosHashCleanup(pData->tableCreateTimeHash);
299✔
60
}
61

62
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
55✔
63
  if (pLeft == NULL || pRight == NULL) {
55!
64
    return false;
×
65
  }
66
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
107!
67
         pLeft->val.version == pRight->val.version;
52!
68
}
69

70
int32_t tqOpen(const char* path, SVnode* pVnode) {
9,756✔
71
  if (path == NULL || pVnode == NULL) {
9,756!
72
    return TSDB_CODE_INVALID_PARA;
×
73
  }
74

75
  bool ignoreTq = pVnode->mounted && !taosCheckExistFile(path);
9,772!
76
  if (ignoreTq) {
9,772!
77
    return 0;
×
78
  }
79

80
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
9,772!
81
  if (pTq == NULL) {
9,773!
82
    return terrno;
×
83
  }
84

85
  pVnode->pTq = pTq;
9,773✔
86
  pTq->pVnode = pVnode;
9,773✔
87

88
  pTq->path = taosStrdup(path);
9,773!
89
  if (pTq->path == NULL) {
9,771!
90
    return terrno;
×
91
  }
92

93
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
9,771✔
94
  if (pTq->pHandle == NULL) {
9,771!
95
    return terrno;
×
96
  }
97
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
9,771✔
98

99
  taosInitRWLatch(&pTq->lock);
9,772✔
100

101
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
9,772✔
102
  if (pTq->pPushMgr == NULL) {
9,771!
103
    return terrno;
×
104
  }
105

106
  pTq->pCheckInfo = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
9,771✔
107
  if (pTq->pCheckInfo == NULL) {
9,770!
108
    return terrno;
×
109
  }
110
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
9,770✔
111

112
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
9,770✔
113
  if (pTq->pOffset == NULL) {
9,768!
114
    return terrno;
×
115
  }
116
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
9,768✔
117

118
  return tqInitialize(pTq);
9,768✔
119
}
120

121
int32_t tqInitialize(STQ* pTq) {
9,768✔
122
  if (pTq == NULL) {
9,768!
123
    return TSDB_CODE_INVALID_PARA;
×
124
  }
125

126
  return tqMetaOpen(pTq);
9,768✔
127
}
128

129
void tqClose(STQ* pTq) {
9,778✔
130
  qDebug("start to close tq");
9,778✔
131
  if (pTq == NULL) {
9,778!
132
    return;
×
133
  }
134

135
  int32_t vgId = 0;
9,778✔
136
  if (pTq->pVnode != NULL) {
9,778✔
137
    vgId = TD_VID(pTq->pVnode);
9,777✔
138
  }
139

140
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
9,778✔
141
  while (pIter) {
9,779✔
142
    STqHandle* pHandle = *(STqHandle**)pIter;
1✔
143
    if (pHandle->msg != NULL) {
1!
144
      tqPushEmptyDataRsp(pHandle, vgId);
1✔
145
      rpcFreeCont(pHandle->msg->pCont);
1✔
146
      taosMemoryFree(pHandle->msg);
1!
147
      pHandle->msg = NULL;
1✔
148
    }
149
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
1✔
150
  }
151

152
  taosHashCleanup(pTq->pHandle);
9,778✔
153
  taosHashCleanup(pTq->pPushMgr);
9,778✔
154
  taosHashCleanup(pTq->pCheckInfo);
9,778✔
155
  taosHashCleanup(pTq->pOffset);
9,778✔
156
  taosMemoryFree(pTq->path);
9,778!
157
  tqMetaClose(pTq);
9,778✔
158
  qDebug("vgId:%d end to close tq", vgId);
9,776✔
159

160
  taosMemoryFree(pTq);
9,781!
161
}
162

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

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

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

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

202
  char buf1[TSDB_OFFSET_LEN] = {0};
576✔
203
  char buf2[TSDB_OFFSET_LEN] = {0};
576✔
204
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
576✔
205
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
575✔
206

207
  tqDebug("tmq poll vgId:%d consumer:0x%" PRIx64 " (epoch %d) start to send rsp, block num:%d, req:%s, rsp:%s, QID:0x%" PRIx64,
576!
208
          vgId, pReq->consumerId, pReq->epoch, pRsp->blockNum, buf1, buf2, pReq->reqId);
209

210
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
576✔
211
}
212

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

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

228
  tDecoderClear(&decoder);
347✔
229

230
  STqOffset* pOffset = &vgOffset.offset;
347✔
231

232
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
347!
233
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
16!
234
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
235
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
331!
236
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
331!
237
            pOffset->val.version);
238
  } else {
239
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
240
    code = TSDB_CODE_INVALID_MSG;
×
241
    goto end;
×
242
  }
243

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

252
  // save the new offset value
253
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
347✔
254
  if (code != 0) {
347!
255
    goto end;
×
256
  }
257

258
  return 0;
347✔
259
end:
×
260
  tOffsetDestroy(&vgOffset.offset.val);
×
261
  return code;
×
262
}
263

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

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

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

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

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

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

303
end:
×
304
  rsp.code = code;
×
305
  tmsgSendRsp(&rsp);
×
306
  return 0;
×
307
}
308

309
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
231✔
310
  if (pTq == NULL) {
231!
311
    return TSDB_CODE_INVALID_PARA;
×
312
  }
313
  void* pIter = NULL;
231✔
314

315
  while (1) {
×
316
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
231✔
317
    if (pIter == NULL) {
231!
318
      break;
231✔
319
    }
320

321
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
×
322

323
    if (pCheck->ntbUid == tbUid) {
×
324
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
×
325
      for (int32_t i = 0; i < sz; i++) {
×
326
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
×
327
        if (pForbidColId == NULL) {
×
328
          continue;
×
329
        }
330

331
        if ((*pForbidColId) == colId) {
×
332
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
×
333
          return -1;
×
334
        }
335
      }
336
    }
337
  }
338

339
  return 0;
231✔
340
}
341

342
int32_t tqProcessPollPush(STQ* pTq) {
1✔
343
  if (pTq == NULL) {
1!
344
    return TSDB_CODE_INVALID_PARA;
×
345
  }
346
  int32_t vgId = TD_VID(pTq->pVnode);
1✔
347
  taosWLockLatch(&pTq->lock);
1✔
348
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
1!
349
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
1✔
350

351
    while (pIter) {
2✔
352
      STqHandle* pHandle = *(STqHandle**)pIter;
1✔
353
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
1!
354

355
      if (pHandle->msg == NULL) {
1!
356
        tqError("pHandle->msg should not be null");
×
357
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
358
        break;
×
359
      } else {
360
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
1✔
361
                       .pCont = pHandle->msg->pCont,
1✔
362
                       .contLen = pHandle->msg->contLen,
1✔
363
                       .info = pHandle->msg->info};
1✔
364
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
1!
365
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
366
        }
367
        taosMemoryFree(pHandle->msg);
1!
368
        pHandle->msg = NULL;
1✔
369
      }
370

371
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
1✔
372
    }
373

374
    taosHashClear(pTq->pPushMgr);
1✔
375
  }
376
  taosWUnLockLatch(&pTq->lock);
1✔
377
  return 0;
1✔
378
}
379

380
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
614✔
381
  if (pTq == NULL || pMsg == NULL) {
614!
382
    return TSDB_CODE_INVALID_PARA;
×
383
  }
384
  SMqPollReq req = {0};
616✔
385
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
616✔
386
  if (code < 0) {
615!
387
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
388
    code = TSDB_CODE_INVALID_MSG;
×
389
    goto END;
×
390
  }
391
  if (req.rawData == 1){
615!
392
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
393
    if (req.uidHash == NULL) {
1!
394
      tqError("tq poll rawData taosHashInit failed");
×
395
      code = terrno;
×
396
      goto END;
×
397
    }
398
  }
399
  int64_t      consumerId = req.consumerId;
616✔
400
  int32_t      reqEpoch = req.epoch;
616✔
401
  STqOffsetVal reqOffset = req.reqOffset;
616✔
402
  int32_t      vgId = TD_VID(pTq->pVnode);
616✔
403
  STqHandle*   pHandle = NULL;
616✔
404

405
  while (1) {
×
406
    taosWLockLatch(&pTq->lock);
616✔
407
    // 1. find handle
408
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
616✔
409
    if (code != TDB_CODE_SUCCESS) {
615!
410
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
411
      taosWUnLockLatch(&pTq->lock);
×
412
      return code;
×
413
    }
414

415
    // 2. check rebalance status
416
    if (pHandle->consumerId != consumerId) {
615✔
417
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
3!
418
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
419
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
420
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
3✔
421
      taosWUnLockLatch(&pTq->lock);
3✔
422
      goto END;
3✔
423
    }
424

425
    bool exec = tqIsHandleExec(pHandle);
612!
426
    if (!exec) {
612!
427
      tqSetHandleExec(pHandle);
613✔
428
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
429
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
613✔
430
              req.subKey, pHandle);
431
      taosWUnLockLatch(&pTq->lock);
614✔
432
      break;
613✔
433
    }
434
    taosWUnLockLatch(&pTq->lock);
×
435

436
    tqDebug("tmq poll: consumer:0x%" PRIx64
×
437
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
438
            consumerId, vgId, req.subKey, pHandle);
439
    taosMsleep(10);
×
440
  }
441

442
  // 3. update the epoch value
443
  if (pHandle->epoch < reqEpoch) {
613✔
444
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
358!
445
            reqEpoch);
446
    pHandle->epoch = reqEpoch;
358✔
447
  }
448

449
  char buf[TSDB_OFFSET_LEN] = {0};
613✔
450
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
613✔
451
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
613!
452
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
453

454
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
613✔
455
  tqSetHandleIdle(pHandle);
613!
456

457
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
613!
458
          req.subKey, pHandle);
459

460
END:
×
461
  tDestroySMqPollReq(&req);
616✔
462
  return code;
616✔
463
}
464

465
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
×
466
  if (pTq == NULL || pMsg == NULL) {
×
467
    return TSDB_CODE_INVALID_PARA;
×
468
  }
469
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
470
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
471

472
  SMqVgOffset vgOffset = {0};
×
473

474
  SDecoder decoder;
475
  tDecoderInit(&decoder, (uint8_t*)data, len);
×
476
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
×
477
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
478
    return terrno;
×
479
  }
480

481
  tDecoderClear(&decoder);
×
482

483
  STqOffset* pSavedOffset = NULL;
×
484
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
×
485
  if (code != 0) {
×
486
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
487
  }
488
  vgOffset.offset = *pSavedOffset;
×
489

490
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
×
491
  if (code < 0) {
×
492
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
493
  }
494

495
  void* buf = rpcMallocCont(len);
×
496
  if (buf == NULL) {
×
497
    return terrno;
×
498
  }
499
  SEncoder encoder = {0};
×
500
  tEncoderInit(&encoder, buf, len);
×
501
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
×
502
  tEncoderClear(&encoder);
×
503
  if (code < 0) {
×
504
    rpcFreeCont(buf);
×
505
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
506
  }
507

508
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
×
509

510
  tmsgSendRsp(&rsp);
×
511
  return 0;
×
512
}
513

514
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
×
515
  if (pTq == NULL || pMsg == NULL) {
×
516
    return TSDB_CODE_INVALID_PARA;
×
517
  }
518
  int32_t    code = 0;
×
519
  SMqPollReq req = {0};
×
520
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
×
521
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
522
    return TSDB_CODE_INVALID_MSG;
×
523
  }
524

525
  int64_t      consumerId = req.consumerId;
×
526
  STqOffsetVal reqOffset = req.reqOffset;
×
527
  int32_t      vgId = TD_VID(pTq->pVnode);
×
528

529
  // 1. find handle
530
  taosRLockLatch(&pTq->lock);
×
531
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
×
532
  if (pHandle == NULL) {
×
533
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
534
    taosRUnLockLatch(&pTq->lock);
×
535
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
536
  }
537

538
  // 2. check rebalance status
539
  if (pHandle->consumerId != consumerId) {
×
540
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
541
            consumerId, vgId, req.subKey, pHandle->consumerId);
542
    taosRUnLockLatch(&pTq->lock);
×
543
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
544
  }
545

546
  int64_t sver = 0, ever = 0;
×
547
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
548
  taosRUnLockLatch(&pTq->lock);
×
549

550
  SMqDataRsp dataRsp = {0};
×
551
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
×
552
  if (code != 0) {
×
553
    return code;
×
554
  }
555

556
  if (req.useSnapshot == true) {
×
557
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
558
    code = TSDB_CODE_INVALID_PARA;
×
559
    goto END;
×
560
  }
561

562
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
×
563

564
  if (reqOffset.type == TMQ_OFFSET__LOG) {
×
565
    dataRsp.rspOffset.version = reqOffset.version;
×
566
  } else if (reqOffset.type < 0) {
×
567
    STqOffset* pOffset = NULL;
×
568
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
×
569
    if (code == 0) {
×
570
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
×
571
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
572
        code = TSDB_CODE_INVALID_PARA;
×
573
        goto END;
×
574
      }
575

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

595
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
×
596

597
END:
×
598
  tDeleteMqDataRsp(&dataRsp);
×
599
  return code;
×
600
}
601

602
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
79✔
603
  if (pTq == NULL || msg == NULL) {
79!
604
    return TSDB_CODE_INVALID_PARA;
×
605
  }
606
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
79✔
607
  int32_t        vgId = TD_VID(pTq->pVnode);
79✔
608

609
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
79!
610
  int32_t code = 0;
79✔
611

612
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
79✔
613
  if (pHandle) {
79!
614
    while (1) {
×
615
      taosWLockLatch(&pTq->lock);
79✔
616
      bool exec = tqIsHandleExec(pHandle);
79✔
617

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

635
  taosWLockLatch(&pTq->lock);
79✔
636
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
79!
637
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
×
638
  }
639
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
79!
640
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
79!
641
  }
642

643
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
79!
644
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
645
  }
646
  taosWUnLockLatch(&pTq->lock);
79✔
647

648
  return 0;
79✔
649
}
650

651
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
75✔
652
  if (pTq == NULL || msg == NULL) {
75!
653
    return TSDB_CODE_INVALID_PARA;
×
654
  }
655
  STqCheckInfo info = {0};
75✔
656
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
75✔
657
  if (code != 0) {
75!
658
    return code;
×
659
  }
660

661
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
75✔
662
  if (code != 0) {
75!
663
    tDeleteSTqCheckInfo(&info);
×
664
    return code;
×
665
  }
666
  taosWLockLatch(&pTq->lock);
75✔
667
  code  = tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
75✔
668
  taosWUnLockLatch(&pTq->lock);
75✔
669
  return code;
75✔
670
}
671

672
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
2✔
673
  if (pTq == NULL || msg == NULL) {
2!
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
2!
677
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
678
  }
679
  taosWLockLatch(&pTq->lock);
2✔
680
  int32_t code = tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
2✔
681
  taosWUnLockLatch(&pTq->lock);
2✔
682
  return code;
2✔
683
}
684

685
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
650✔
686
  if (pTq == NULL || msg == NULL) {
650!
687
    return TSDB_CODE_INVALID_PARA;
×
688
  }
689
  int         ret = 0;
650✔
690
  SMqRebVgReq req = {0};
650✔
691
  SDecoder    dc = {0};
650✔
692

693
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
650✔
694
  ret = tDecodeSMqRebVgReq(&dc, &req);
650✔
695
  if (ret < 0) {
650!
696
    goto end;
×
697
  }
698

699
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
650!
700
         req.oldConsumerId, req.newConsumerId);
701

702
  taosRLockLatch(&pTq->lock);
650✔
703
  STqHandle* pHandle = NULL;
650✔
704
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
650✔
705
  if (code != 0){
650✔
706
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one, msg:%s", pTq->pVnode->config.vgId, req.subKey, tstrerror(code));
299!
707
  }
708
  taosRUnLockLatch(&pTq->lock);
650✔
709
  if (pHandle == NULL) {
650✔
710
    if (req.oldConsumerId != -1) {
299!
711
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
712
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
713
    }
714
    if (req.newConsumerId == -1) {
299!
715
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
716
      ret = TSDB_CODE_INVALID_PARA;
×
717
      goto end;
×
718
    }
719
    STqHandle handle = {0};
299✔
720
    ret = tqMetaCreateHandle(pTq, &req, &handle);
299✔
721
    if (ret < 0) {
299!
722
      tqDestroyTqHandle(&handle);
×
723
      goto end;
×
724
    }
725
    taosWLockLatch(&pTq->lock);
299✔
726
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
299✔
727
    taosWUnLockLatch(&pTq->lock);
299✔
728
  } else {
729
    while (1) {
×
730
      taosWLockLatch(&pTq->lock);
351✔
731
      bool exec = tqIsHandleExec(pHandle);
351!
732
      if (exec) {
351!
733
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
734
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
735
        taosWUnLockLatch(&pTq->lock);
×
736
        taosMsleep(10);
×
737
        continue;
×
738
      }
739
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
351!
740
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
×
741
      } else {
742
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
351!
743
               req.newConsumerId);
744

745
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
351✔
746
        atomic_store_32(&pHandle->epoch, 0);
351✔
747
        tqUnregisterPushHandle(pTq, pHandle);
351✔
748
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
351✔
749
      }
750
      taosWUnLockLatch(&pTq->lock);
349✔
751
      break;
351✔
752
    }
753
  }
754

755
end:
650✔
756
  tDecoderClear(&dc);
650✔
757
  return ret;
650✔
758
}
759

760
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