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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

35.97
/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; }
34!
28
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
22✔
29
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
22✔
30

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

36
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
9!
37
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
9!
38
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
×
39
    tqReaderClose(pData->execHandle.pTqReader);
×
40
    walCloseReader(pData->pWalReader);
×
41
    taosHashCleanup(pData->execHandle.execDb.pFilterOutTbUid);
×
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) {
9!
49
    rpcFreeCont(pData->msg->pCont);
×
50
    taosMemoryFree(pData->msg);
×
51
    pData->msg = NULL;
×
52
  }
53
  if (pData->block != NULL) {
9!
54
    blockDataDestroy(pData->block);
×
55
  }
56
  if (pData->pRef) {
9!
57
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
9✔
58
  }
59
  taosHashCleanup(pData->tableCreateTimeHash);
9✔
60
}
61

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

70
int32_t tqOpen(const char* path, SVnode* pVnode) {
53✔
71
  if (path == NULL || pVnode == NULL) {
53!
72
    return TSDB_CODE_INVALID_PARA;
×
73
  }
74
  STQ* pTq = taosMemoryCalloc(1, sizeof(STQ));
53!
75
  if (pTq == NULL) {
53!
76
    return terrno;
×
77
  }
78

79
  pVnode->pTq = pTq;
53✔
80
  pTq->pVnode = pVnode;
53✔
81

82
  pTq->path = taosStrdup(path);
53!
83
  if (pTq->path == NULL) {
53!
84
    return terrno;
×
85
  }
86

87
  pTq->pHandle = taosHashInit(64, MurmurHash3_32, true, HASH_ENTRY_LOCK);
53✔
88
  if (pTq->pHandle == NULL) {
53!
89
    return terrno;
×
90
  }
91
  taosHashSetFreeFp(pTq->pHandle, tqDestroyTqHandle);
53✔
92

93
  taosInitRWLatch(&pTq->lock);
53✔
94

95
  pTq->pPushMgr = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
53✔
96
  if (pTq->pPushMgr == NULL) {
53!
97
    return terrno;
×
98
  }
99

100
  pTq->pCheckInfo = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
53✔
101
  if (pTq->pCheckInfo == NULL) {
53!
102
    return terrno;
×
103
  }
104
  taosHashSetFreeFp(pTq->pCheckInfo, (FDelete)tDeleteSTqCheckInfo);
53✔
105

106
  pTq->pOffset = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
53✔
107
  if (pTq->pOffset == NULL) {
53!
108
    return terrno;
×
109
  }
110
  taosHashSetFreeFp(pTq->pOffset, (FDelete)tDeleteSTqOffset);
53✔
111

112
  return tqInitialize(pTq);
53✔
113
}
114

115
int32_t tqInitialize(STQ* pTq) {
53✔
116
  if (pTq == NULL) {
53!
117
    return TSDB_CODE_INVALID_PARA;
×
118
  }
119

120
  return tqMetaOpen(pTq);
53✔
121
}
122

123
void tqClose(STQ* pTq) {
54✔
124
  qDebug("start to close tq");
54✔
125
  if (pTq == NULL) {
54!
126
    return;
×
127
  }
128

129
  int32_t vgId = 0;
54✔
130
  if (pTq->pVnode != NULL) {
54✔
131
    vgId = TD_VID(pTq->pVnode);
53✔
132
  }
133

134
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
54✔
135
  while (pIter) {
54!
136
    STqHandle* pHandle = *(STqHandle**)pIter;
×
137
    if (pHandle->msg != NULL) {
×
138
      tqPushEmptyDataRsp(pHandle, vgId);
×
139
      rpcFreeCont(pHandle->msg->pCont);
×
140
      taosMemoryFree(pHandle->msg);
×
141
      pHandle->msg = NULL;
×
142
    }
143
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
×
144
  }
145

146
  taosHashCleanup(pTq->pHandle);
54✔
147
  taosHashCleanup(pTq->pPushMgr);
54✔
148
  taosHashCleanup(pTq->pCheckInfo);
54✔
149
  taosHashCleanup(pTq->pOffset);
54✔
150
  taosMemoryFree(pTq->path);
54!
151
  tqMetaClose(pTq);
54✔
152
  qDebug("vgId:%d end to close tq", vgId);
54✔
153

154
  taosMemoryFree(pTq);
54!
155
}
156

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

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

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

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

196
  char buf1[TSDB_OFFSET_LEN] = {0};
21✔
197
  char buf2[TSDB_OFFSET_LEN] = {0};
21✔
198
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
21✔
199
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
21✔
200

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

204
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
21✔
205
}
206

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

214
  int32_t  code = 0;
11✔
215
  SDecoder decoder;
216
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
11✔
217
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
11!
218
    code = TSDB_CODE_INVALID_MSG;
×
219
    goto end;
×
220
  }
221

222
  tDecoderClear(&decoder);
11✔
223

224
  STqOffset* pOffset = &vgOffset.offset;
11✔
225

226
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
11!
227
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
×
228
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
229
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
11!
230
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
11!
231
            pOffset->val.version);
232
  } else {
233
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
234
    code = TSDB_CODE_INVALID_MSG;
×
235
    goto end;
×
236
  }
237

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

246
  // save the new offset value
247
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
11✔
248
  if (code != 0) {
11!
249
    goto end;
×
250
  }
251

252
  return 0;
11✔
253
end:
×
254
  tOffsetDestroy(&vgOffset.offset.val);
×
255
  return code;
×
256
}
257

258
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
×
259
  if (pTq == NULL || pMsg == NULL) {
×
260
    return TSDB_CODE_INVALID_PARA;
×
261
  }
262
  SMqSeekReq req = {0};
×
263
  int32_t    vgId = TD_VID(pTq->pVnode);
×
264
  SRpcMsg    rsp = {.info = pMsg->info};
×
265
  int        code = 0;
×
266

267
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
×
268
    code = TSDB_CODE_OUT_OF_MEMORY;
×
269
    goto end;
×
270
  }
271

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

275
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
×
276
  if (pHandle == NULL) {
×
277
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
278
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
279
    taosWUnLockLatch(&pTq->lock);
×
280
    goto end;
×
281
  }
282

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

292
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
293
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
294
  tqUnregisterPushHandle(pTq, pHandle);
×
295
  taosWUnLockLatch(&pTq->lock);
×
296

297
end:
×
298
  rsp.code = code;
×
299
  tmsgSendRsp(&rsp);
×
300
  return 0;
×
301
}
302

303
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
×
304
  if (pTq == NULL) {
×
305
    return TSDB_CODE_INVALID_PARA;
×
306
  }
307
  void* pIter = NULL;
×
308

309
  while (1) {
×
310
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
×
311
    if (pIter == NULL) {
×
312
      break;
×
313
    }
314

315
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
×
316

317
    if (pCheck->ntbUid == tbUid) {
×
318
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
×
319
      for (int32_t i = 0; i < sz; i++) {
×
320
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
×
321
        if (pForbidColId == NULL) {
×
322
          continue;
×
323
        }
324

325
        if ((*pForbidColId) == colId) {
×
326
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
×
327
          return -1;
×
328
        }
329
      }
330
    }
331
  }
332

333
  return 0;
×
334
}
335

336
int32_t tqProcessPollPush(STQ* pTq) {
1✔
337
  if (pTq == NULL) {
1!
338
    return TSDB_CODE_INVALID_PARA;
×
339
  }
340
  int32_t vgId = TD_VID(pTq->pVnode);
1✔
341
  taosWLockLatch(&pTq->lock);
1✔
342
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
1!
343
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
1✔
344

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

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

365
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
1✔
366
    }
367

368
    taosHashClear(pTq->pPushMgr);
1✔
369
  }
370
  taosWUnLockLatch(&pTq->lock);
1✔
371
  return 0;
1✔
372
}
373

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

399
  while (1) {
×
400
    taosWLockLatch(&pTq->lock);
22✔
401
    // 1. find handle
402
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
22✔
403
    if (code != TDB_CODE_SUCCESS) {
22!
404
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
×
405
      taosWUnLockLatch(&pTq->lock);
×
406
      return code;
×
407
    }
408

409
    // 2. check rebalance status
410
    if (pHandle->consumerId != consumerId) {
22!
411
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
×
412
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
413
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
414
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
415
      taosWUnLockLatch(&pTq->lock);
×
416
      goto END;
×
417
    }
418

419
    bool exec = tqIsHandleExec(pHandle);
22!
420
    if (!exec) {
22!
421
      tqSetHandleExec(pHandle);
22!
422
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
423
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
22!
424
              req.subKey, pHandle);
425
      taosWUnLockLatch(&pTq->lock);
22✔
426
      break;
22✔
427
    }
428
    taosWUnLockLatch(&pTq->lock);
×
429

430
    tqDebug("tmq poll: consumer:0x%" PRIx64
×
431
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
432
            consumerId, vgId, req.subKey, pHandle);
433
    taosMsleep(10);
×
434
  }
435

436
  // 3. update the epoch value
437
  if (pHandle->epoch < reqEpoch) {
22✔
438
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
13!
439
            reqEpoch);
440
    pHandle->epoch = reqEpoch;
13✔
441
  }
442

443
  char buf[TSDB_OFFSET_LEN] = {0};
22✔
444
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
22✔
445
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
22!
446
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
447

448
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
22✔
449
  tqSetHandleIdle(pHandle);
22!
450

451
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
22!
452
          req.subKey, pHandle);
453

454
END:
×
455
  tDestroySMqPollReq(&req);
22✔
456
  return code;
22✔
457
}
458

459
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
×
460
  if (pTq == NULL || pMsg == NULL) {
×
461
    return TSDB_CODE_INVALID_PARA;
×
462
  }
463
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
464
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
465

466
  SMqVgOffset vgOffset = {0};
×
467

468
  SDecoder decoder;
469
  tDecoderInit(&decoder, (uint8_t*)data, len);
×
470
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
×
471
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
472
    return terrno;
×
473
  }
474

475
  tDecoderClear(&decoder);
×
476

477
  STqOffset* pSavedOffset = NULL;
×
478
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
×
479
  if (code != 0) {
×
480
    return TSDB_CODE_TMQ_NO_COMMITTED;
×
481
  }
482
  vgOffset.offset = *pSavedOffset;
×
483

484
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
×
485
  if (code < 0) {
×
486
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
487
  }
488

489
  void* buf = rpcMallocCont(len);
×
490
  if (buf == NULL) {
×
491
    return terrno;
×
492
  }
493
  SEncoder encoder = {0};
×
494
  tEncoderInit(&encoder, buf, len);
×
495
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
×
496
  tEncoderClear(&encoder);
×
497
  if (code < 0) {
×
498
    rpcFreeCont(buf);
×
499
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
500
  }
501

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

504
  tmsgSendRsp(&rsp);
×
505
  return 0;
×
506
}
507

508
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
×
509
  if (pTq == NULL || pMsg == NULL) {
×
510
    return TSDB_CODE_INVALID_PARA;
×
511
  }
512
  int32_t    code = 0;
×
513
  SMqPollReq req = {0};
×
514
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
×
515
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
516
    return TSDB_CODE_INVALID_MSG;
×
517
  }
518

519
  int64_t      consumerId = req.consumerId;
×
520
  STqOffsetVal reqOffset = req.reqOffset;
×
521
  int32_t      vgId = TD_VID(pTq->pVnode);
×
522

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

532
  // 2. check rebalance status
533
  if (pHandle->consumerId != consumerId) {
×
534
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
535
            consumerId, vgId, req.subKey, pHandle->consumerId);
536
    taosRUnLockLatch(&pTq->lock);
×
537
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
538
  }
539

540
  int64_t sver = 0, ever = 0;
×
541
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
×
542
  taosRUnLockLatch(&pTq->lock);
×
543

544
  SMqDataRsp dataRsp = {0};
×
545
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
×
546
  if (code != 0) {
×
547
    return code;
×
548
  }
549

550
  if (req.useSnapshot == true) {
×
551
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
552
    code = TSDB_CODE_INVALID_PARA;
×
553
    goto END;
×
554
  }
555

556
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
×
557

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

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

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

591
END:
×
592
  tDeleteMqDataRsp(&dataRsp);
×
593
  return code;
×
594
}
595

596
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
×
597
  if (pTq == NULL || msg == NULL) {
×
598
    return TSDB_CODE_INVALID_PARA;
×
599
  }
600
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
×
601
  int32_t        vgId = TD_VID(pTq->pVnode);
×
602

603
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
×
604
  int32_t code = 0;
×
605

606
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
×
607
  if (pHandle) {
×
608
    while (1) {
×
609
      taosWLockLatch(&pTq->lock);
×
610
      bool exec = tqIsHandleExec(pHandle);
×
611

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

629
  taosWLockLatch(&pTq->lock);
×
630
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
×
631
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
×
632
  }
633
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
×
634
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
635
  }
636

637
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
×
638
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
639
  }
640
  taosWUnLockLatch(&pTq->lock);
×
641

642
  return 0;
×
643
}
644

645
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3✔
646
  if (pTq == NULL || msg == NULL) {
3!
647
    return TSDB_CODE_INVALID_PARA;
×
648
  }
649
  STqCheckInfo info = {0};
3✔
650
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
3✔
651
  if (code != 0) {
3!
652
    return code;
×
653
  }
654

655
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
3✔
656
  if (code != 0) {
3!
657
    tDeleteSTqCheckInfo(&info);
×
658
    return code;
×
659
  }
660
  taosWLockLatch(&pTq->lock);
3✔
661
  code  = tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
3✔
662
  taosWUnLockLatch(&pTq->lock);
3✔
663
  return code;
3✔
664
}
665

666
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
×
667
  if (pTq == NULL || msg == NULL) {
×
668
    return TSDB_CODE_INVALID_PARA;
×
669
  }
670
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
×
671
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
672
  }
673
  taosWLockLatch(&pTq->lock);
×
674
  int32_t code = tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
×
675
  taosWUnLockLatch(&pTq->lock);
×
676
  return code;
×
677
}
678

679
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
21✔
680
  if (pTq == NULL || msg == NULL) {
21!
681
    return TSDB_CODE_INVALID_PARA;
×
682
  }
683
  int         ret = 0;
21✔
684
  SMqRebVgReq req = {0};
21✔
685
  SDecoder    dc = {0};
21✔
686

687
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
21✔
688
  ret = tDecodeSMqRebVgReq(&dc, &req);
21✔
689
  if (ret < 0) {
21!
690
    goto end;
×
691
  }
692

693
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
21!
694
         req.oldConsumerId, req.newConsumerId);
695

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

739
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
12✔
740
        atomic_store_32(&pHandle->epoch, 0);
12✔
741
        tqUnregisterPushHandle(pTq, pHandle);
12✔
742
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
12✔
743
      }
744
      taosWUnLockLatch(&pTq->lock);
12✔
745
      break;
12✔
746
    }
747
  }
748

749
end:
21✔
750
  tDecoderClear(&dc);
21✔
751
  return ret;
21✔
752
}
753

754
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