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

taosdata / TDengine / #4653

06 Aug 2025 01:23AM UTC coverage: 60.5% (-0.5%) from 60.986%
#4653

push

travis-ci

web-flow
fix: [TD-37190]: disable ignore_nodata_trigger when window type is not interval/sliding or period. (#32405)

138499 of 291677 branches covered (47.48%)

Branch coverage included in aggregate %.

52 of 106 new or added lines in 1 file covered. (49.06%)

1648 existing lines in 119 files now uncovered.

209836 of 284084 relevant lines covered (73.86%)

4732834.84 hits per line

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

63.48
/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; }
46,885!
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
44,419✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
44,421✔
30

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

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

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

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

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

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

85
  pVnode->pTq = pTq;
15,105✔
86
  pTq->pVnode = pVnode;
15,105✔
87

88
  pTq->path = taosStrdup(path);
15,105!
89
  if (pTq->path == NULL) {
15,104!
90
    return terrno;
×
91
  }
92

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

99
  taosInitRWLatch(&pTq->lock);
15,099✔
100

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

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

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

118
  return tqInitialize(pTq);
15,107✔
119
}
120

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

126
  return tqMetaOpen(pTq);
15,107✔
127
}
128

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

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

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

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

160
  taosMemoryFree(pTq);
15,110!
161
}
162

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

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

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

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

202
  char buf1[TSDB_OFFSET_LEN] = {0};
30,477✔
203
  char buf2[TSDB_OFFSET_LEN] = {0};
30,477✔
204
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
30,477✔
205
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
30,477✔
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,
30,477!
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);
30,477✔
211
}
212

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

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

228
  tDecoderClear(&decoder);
10,459✔
229

230
  STqOffset* pOffset = &vgOffset.offset;
10,458✔
231

232
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
10,458!
233
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
559!
234
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
235
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
9,899!
236
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
9,899!
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;
10,459✔
245
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
10,459✔
246
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
10,459✔
247
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
4!
248
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
249
    goto end;  // no need to update the offset value
4✔
250
  }
251

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

258
  return 0;
10,455✔
259
end:
4✔
260
  tOffsetDestroy(&vgOffset.offset.val);
4✔
261
  return code;
4✔
262
}
263

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

273
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
8!
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);
8!
279
  taosWLockLatch(&pTq->lock);
8✔
280

281
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
8✔
282
  if (pHandle == NULL) {
8!
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) {
8!
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);
8✔
301
  taosWUnLockLatch(&pTq->lock);
8✔
302

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

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

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

321
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
322

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

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

339
  return 0;
698✔
340
}
341

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

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

355
      if (pHandle->msg == NULL) {
12,621!
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,
12,621✔
361
                       .pCont = pHandle->msg->pCont,
12,621✔
362
                       .contLen = pHandle->msg->contLen,
12,621✔
363
                       .info = pHandle->msg->info};
12,621✔
364
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
12,621!
365
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
366
        }
367
        taosMemoryFree(pHandle->msg);
12,621!
368
        pHandle->msg = NULL;
12,621✔
369
      }
370

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

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

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

405
  while (1) {
×
406
    taosWLockLatch(&pTq->lock);
44,426✔
407
    // 1. find handle
408
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
44,427✔
409
    if (code != TDB_CODE_SUCCESS) {
44,408!
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) {
44,408✔
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);
44,405!
426
    if (!exec) {
44,405!
427
      tqSetHandleExec(pHandle);
44,419!
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,
44,419!
430
              req.subKey, pHandle);
431
      taosWUnLockLatch(&pTq->lock);
44,426✔
432
      break;
44,429✔
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) {
44,429✔
444
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,344!
445
            reqEpoch);
446
    pHandle->epoch = reqEpoch;
1,344✔
447
  }
448

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

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

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

UNCOV
460
END:
×
461
  tDestroySMqPollReq(&req);
44,429✔
462
  return code;
44,431✔
463
}
464

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

472
  SMqVgOffset vgOffset = {0};
1✔
473

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

481
  tDecoderClear(&decoder);
1✔
482

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

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

495
  void* buf = rpcMallocCont(len);
1✔
496
  if (buf == NULL) {
1!
497
    return terrno;
×
498
  }
499
  SEncoder encoder = {0};
1✔
500
  tEncoderInit(&encoder, buf, len);
1✔
501
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
1✔
502
  tEncoderClear(&encoder);
1✔
503
  if (code < 0) {
1!
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};
1✔
509

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

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

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

529
  // 1. find handle
530
  taosRLockLatch(&pTq->lock);
3✔
531
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
3✔
532
  if (pHandle == NULL) {
3!
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) {
3!
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;
3✔
547
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
3✔
548
  taosRUnLockLatch(&pTq->lock);
3✔
549

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

556
  if (req.useSnapshot == true) {
3!
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;
3✔
563

564
  if (reqOffset.type == TMQ_OFFSET__LOG) {
3✔
565
    dataRsp.rspOffset.version = reqOffset.version;
1✔
566
  } else if (reqOffset.type < 0) {
2!
567
    STqOffset* pOffset = NULL;
2✔
568
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
2✔
569
    if (code == 0) {
2✔
570
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
1!
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;
1✔
577
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
1!
578
             req.subKey, dataRsp.rspOffset.version);
579
    } else {
580
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
1!
581
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
1✔
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,
1!
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);
3✔
596

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

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

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

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

618
      if (exec) {
797!
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);
797✔
626
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
797✔
627
      if (code != 0) {
797!
628
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
629
      }
630
      taosWUnLockLatch(&pTq->lock);
797✔
631
      break;
797✔
632
    }
633
  }
634

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

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

648
  return 0;
800✔
649
}
650

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

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

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

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

693
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
2,970✔
694
  ret = tDecodeSMqRebVgReq(&dc, &req);
2,969✔
695
  if (ret < 0) {
2,967!
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,
2,967!
700
         req.oldConsumerId, req.newConsumerId);
701

702
  taosRLockLatch(&pTq->lock);
2,969✔
703
  STqHandle* pHandle = NULL;
2,970✔
704
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
2,970✔
705
  if (code != 0){
2,968✔
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));
1,285!
707
  }
708
  taosRUnLockLatch(&pTq->lock);
2,970✔
709
  if (pHandle == NULL) {
2,970✔
710
    if (req.oldConsumerId != -1) {
1,287!
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) {
1,287!
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};
1,287✔
720
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,287✔
721
    if (ret < 0) {
1,287!
722
      tqDestroyTqHandle(&handle);
×
723
      goto end;
×
724
    }
725
    taosWLockLatch(&pTq->lock);
1,287✔
726
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,286✔
727
    taosWUnLockLatch(&pTq->lock);
1,287✔
728
  } else {
729
    while (1) {
×
730
      taosWLockLatch(&pTq->lock);
1,683✔
731
      bool exec = tqIsHandleExec(pHandle);
1,683!
732
      if (exec) {
1,683!
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
1,683✔
740
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
157!
741
      } else {
742
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,526!
743
               req.newConsumerId);
744

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

755
end:
2,970✔
756
  tDecoderClear(&dc);
2,970✔
757
  return ret;
2,970✔
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