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

taosdata / TDengine / #4106

19 May 2025 07:15AM UTC coverage: 62.857% (-0.2%) from 63.042%
#4106

push

travis-ci

GitHub
Merge pull request #31115 from taosdata/merge/mainto3.0

156749 of 318088 branches covered (49.28%)

Branch coverage included in aggregate %.

242535 of 317143 relevant lines covered (76.47%)

18746393.97 hits per line

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

59.6
/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 "tqCommon.h"
20
#include "tstream.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; }
50,629!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
47,952✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
47,957✔
31

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

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

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

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

80
  pVnode->pTq = pTq;
14,818✔
81
  pTq->pVnode = pVnode;
14,818✔
82

83
  pTq->path = taosStrdup(path);
14,818!
84
  if (pTq->path == NULL) {
14,812!
85
    return terrno;
×
86
  }
87

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

94
  taosInitRWLatch(&pTq->lock);
14,815✔
95

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

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

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

113
  return tqInitialize(pTq);
14,818✔
114
}
115

116
int32_t tqInitialize(STQ* pTq) {
14,818✔
117
  if (pTq == NULL) {
14,818!
118
    return TSDB_CODE_INVALID_PARA;
×
119
  }
120
  int32_t vgId = TD_VID(pTq->pVnode);
14,818✔
121
  int32_t code = streamMetaOpen(pTq->path, pTq, tqBuildStreamTask, tqExpandStreamTask, vgId, -1,
14,818✔
122
                                tqStartTaskCompleteCallback, &pTq->pStreamMeta);
123
  if (code != TSDB_CODE_SUCCESS) {
14,820!
124
    return code;
×
125
  }
126

127
  streamMetaLoadAllTasks(pTq->pStreamMeta);
14,820✔
128
  return tqMetaOpen(pTq);
14,820✔
129
}
130

131
void tqClose(STQ* pTq) {
14,821✔
132
  qDebug("start to close tq");
14,821✔
133
  if (pTq == NULL) {
14,821!
134
    return;
×
135
  }
136

137
  int32_t vgId = 0;
14,821✔
138
  if (pTq->pVnode != NULL) {
14,821✔
139
    vgId = TD_VID(pTq->pVnode);
14,820✔
140
  } else if (pTq->pStreamMeta != NULL) {
1!
141
    vgId = pTq->pStreamMeta->vgId;
×
142
  }
143

144
  // close the stream meta firstly
145
  streamMetaClose(pTq->pStreamMeta);
14,821✔
146

147
  void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
14,821✔
148
  while (pIter) {
14,854✔
149
    STqHandle* pHandle = *(STqHandle**)pIter;
33✔
150
    if (pHandle->msg != NULL) {
33!
151
      tqPushEmptyDataRsp(pHandle, vgId);
33✔
152
      rpcFreeCont(pHandle->msg->pCont);
33✔
153
      taosMemoryFree(pHandle->msg);
33!
154
      pHandle->msg = NULL;
33✔
155
    }
156
    pIter = taosHashIterate(pTq->pPushMgr, pIter);
33✔
157
  }
158

159
  taosHashCleanup(pTq->pHandle);
14,821✔
160
  taosHashCleanup(pTq->pPushMgr);
14,821✔
161
  taosHashCleanup(pTq->pCheckInfo);
14,821✔
162
  taosHashCleanup(pTq->pOffset);
14,821✔
163
  taosMemoryFree(pTq->path);
14,821!
164
  tqMetaClose(pTq);
14,821✔
165
  qDebug("vgId:%d end to close tq", vgId);
14,821✔
166

167
#if 0
168
  streamMetaFreeTQDuringScanWalError(pTq);
169
#endif
170

171
  taosMemoryFree(pTq);
14,821!
172
}
173

174
void tqNotifyClose(STQ* pTq) {
22,583✔
175
  if (pTq == NULL) {
22,583!
176
    return;
×
177
  }
178

179
  if (pTq->pStreamMeta != NULL) {
22,583!
180
    streamMetaNotifyClose(pTq->pStreamMeta);
22,583✔
181
  }
182
}
183

184
void tqPushEmptyDataRsp(STqHandle* pHandle, int32_t vgId) {
638✔
185
  if (pHandle == NULL) {
638!
186
    return;
×
187
  }
188
  int32_t    code = 0;
638✔
189
  SMqPollReq req = {0};
638✔
190
  code = tDeserializeSMqPollReq(pHandle->msg->pCont, pHandle->msg->contLen, &req);
638✔
191
  if (code < 0) {
638!
192
    tqError("tDeserializeSMqPollReq %d failed, code:%d", pHandle->msg->contLen, code);
×
193
    return;
×
194
  }
195

196
  SMqDataRsp dataRsp = {0};
638✔
197
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
638✔
198
  if (code != 0) {
638!
199
    tqError("tqInitDataRsp failed, code:%d", code);
×
200
    return;
×
201
  }
202
  dataRsp.blockNum = 0;
638✔
203
  char buf[TSDB_OFFSET_LEN] = {0};
638✔
204
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &dataRsp.reqOffset);
638✔
205
  tqInfo("tqPushEmptyDataRsp to consumer:0x%" PRIx64 " vgId:%d, offset:%s, QID:0x%" PRIx64, req.consumerId, vgId, buf,
638!
206
         req.reqId);
207

208
  code = tqSendDataRsp(pHandle, pHandle->msg, &req, &dataRsp, TMQ_MSG_TYPE__POLL_DATA_RSP, vgId);
638✔
209
  if (code != 0) {
638!
210
    tqError("tqSendDataRsp failed, code:%d", code);
×
211
  }
212
  tDeleteMqDataRsp(&dataRsp);
638✔
213
}
214

215
int32_t tqSendDataRsp(STqHandle* pHandle, const SRpcMsg* pMsg, const SMqPollReq* pReq, SMqDataRsp* pRsp, int32_t type,
31,996✔
216
                      int32_t vgId) {
217
  if (pHandle == NULL || pMsg == NULL || pReq == NULL || pRsp == NULL) {
31,996!
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220
  int64_t sver = 0, ever = 0;
32,000✔
221
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
32,000✔
222

223
  char buf1[TSDB_OFFSET_LEN] = {0};
32,001✔
224
  char buf2[TSDB_OFFSET_LEN] = {0};
32,001✔
225
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
32,001✔
226
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
31,996✔
227

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

231
  return tqDoSendDataRsp(&pMsg->info, pRsp, pReq->epoch, pReq->consumerId, type, sver, ever);
32,001✔
232
}
233

234
int32_t tqProcessOffsetCommitReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
5,475✔
235
  if (pTq == NULL) {
5,475!
236
    return TSDB_CODE_INVALID_PARA;
×
237
  }
238
  SMqVgOffset vgOffset = {0};
5,475✔
239
  int32_t     vgId = TD_VID(pTq->pVnode);
5,475✔
240

241
  int32_t  code = 0;
5,475✔
242
  SDecoder decoder;
243
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
5,475✔
244
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
5,491!
245
    code = TSDB_CODE_INVALID_MSG;
×
246
    goto end;
×
247
  }
248

249
  tDecoderClear(&decoder);
5,478✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
5,470✔
252

253
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
5,470!
254
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
437!
255
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
256
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
5,033!
257
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
5,035✔
258
            pOffset->val.version);
259
  } else {
260
    tqError("invalid commit offset type:%d", pOffset->val.type);
×
261
    code = TSDB_CODE_INVALID_MSG;
×
262
    goto end;
×
263
  }
264

265
  STqOffset* pSavedOffset = NULL;
5,493✔
266
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
5,493✔
267
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
5,496✔
268
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
2!
269
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
270
    goto end;  // no need to update the offset value
2✔
271
  }
272

273
  // save the new offset value
274
  code = taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset));
5,494✔
275
  if (code != 0) {
5,494!
276
    goto end;
×
277
  }
278

279
  return 0;
5,494✔
280
end:
2✔
281
  tOffsetDestroy(&vgOffset.offset.val);
2✔
282
  return code;
2✔
283
}
284

285
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
24✔
286
  if (pTq == NULL || pMsg == NULL) {
24!
287
    return TSDB_CODE_INVALID_PARA;
×
288
  }
289
  SMqSeekReq req = {0};
24✔
290
  int32_t    vgId = TD_VID(pTq->pVnode);
24✔
291
  SRpcMsg    rsp = {.info = pMsg->info};
24✔
292
  int        code = 0;
24✔
293

294
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
24!
295
    code = TSDB_CODE_OUT_OF_MEMORY;
×
296
    goto end;
×
297
  }
298

299
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
24!
300
  taosWLockLatch(&pTq->lock);
24✔
301

302
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
24✔
303
  if (pHandle == NULL) {
24!
304
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
305
    code = TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
306
    taosWUnLockLatch(&pTq->lock);
×
307
    goto end;
×
308
  }
309

310
  // 2. check consumer-vg assignment status
311
  if (pHandle->consumerId != req.consumerId) {
24!
312
    tqError("ERROR tmq seek, consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
313
            req.consumerId, vgId, req.subKey, pHandle->consumerId);
314
    taosWUnLockLatch(&pTq->lock);
×
315
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
316
    goto end;
×
317
  }
318

319
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
320
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
321
  tqUnregisterPushHandle(pTq, pHandle);
24✔
322
  taosWUnLockLatch(&pTq->lock);
24✔
323

324
end:
24✔
325
  rsp.code = code;
24✔
326
  tmsgSendRsp(&rsp);
24✔
327
  return 0;
24✔
328
}
329

330
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
678✔
331
  if (pTq == NULL) {
678!
332
    return TSDB_CODE_INVALID_PARA;
×
333
  }
334
  void* pIter = NULL;
678✔
335

336
  while (1) {
11✔
337
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
689✔
338
    if (pIter == NULL) {
689✔
339
      break;
643✔
340
    }
341

342
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
343

344
    if (pCheck->ntbUid == tbUid) {
46!
345
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
46✔
346
      for (int32_t i = 0; i < sz; i++) {
168✔
347
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
157✔
348
        if (pForbidColId == NULL) {
157!
349
          continue;
×
350
        }
351

352
        if ((*pForbidColId) == colId) {
157✔
353
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
35✔
354
          return -1;
35✔
355
        }
356
      }
357
    }
358
  }
359

360
  return 0;
643✔
361
}
362

363
int32_t tqProcessPollPush(STQ* pTq) {
14,129✔
364
  if (pTq == NULL) {
14,129!
365
    return TSDB_CODE_INVALID_PARA;
×
366
  }
367
  int32_t vgId = TD_VID(pTq->pVnode);
14,129✔
368
  taosWLockLatch(&pTq->lock);
14,129✔
369
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
14,129!
370
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
14,129✔
371

372
    while (pIter) {
28,568✔
373
      STqHandle* pHandle = *(STqHandle**)pIter;
14,439✔
374
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
14,439!
375

376
      if (pHandle->msg == NULL) {
14,439!
377
        tqError("pHandle->msg should not be null");
×
378
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
379
        break;
×
380
      } else {
381
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
14,439✔
382
                       .pCont = pHandle->msg->pCont,
14,439✔
383
                       .contLen = pHandle->msg->contLen,
14,439✔
384
                       .info = pHandle->msg->info};
14,439✔
385
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
14,439!
386
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
387
        }
388
        taosMemoryFree(pHandle->msg);
14,439!
389
        pHandle->msg = NULL;
14,439✔
390
      }
391

392
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
14,439✔
393
    }
394

395
    taosHashClear(pTq->pPushMgr);
14,129✔
396
  }
397
  taosWUnLockLatch(&pTq->lock);
14,129✔
398
  return 0;
14,129✔
399
}
400

401
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
47,953✔
402
  if (pTq == NULL || pMsg == NULL) {
47,953!
403
    return TSDB_CODE_INVALID_PARA;
×
404
  }
405
  SMqPollReq req = {0};
47,957✔
406
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
47,957✔
407
  if (code < 0) {
47,929!
408
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
409
    code = TSDB_CODE_INVALID_MSG;
×
410
    goto END;
×
411
  }
412
  if (req.rawData == 1){
47,929✔
413
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
90✔
414
    if (req.uidHash == NULL) {
114!
415
      tqError("tq poll rawData taosHashInit failed");
×
416
      code = terrno;
×
417
      goto END;
×
418
    }
419
  }
420
  int64_t      consumerId = req.consumerId;
47,953✔
421
  int32_t      reqEpoch = req.epoch;
47,953✔
422
  STqOffsetVal reqOffset = req.reqOffset;
47,953✔
423
  int32_t      vgId = TD_VID(pTq->pVnode);
47,953✔
424
  STqHandle*   pHandle = NULL;
47,953✔
425

426
  while (1) {
5✔
427
    taosWLockLatch(&pTq->lock);
47,958✔
428
    // 1. find handle
429
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
47,971✔
430
    if (code != TDB_CODE_SUCCESS) {
47,938✔
431
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found, msg:%s", consumerId, vgId, req.subKey, tstrerror(code));
10!
432
      taosWUnLockLatch(&pTq->lock);
10✔
433
      return code;
10✔
434
    }
435

436
    // 2. check rebalance status
437
    if (pHandle->consumerId != consumerId) {
47,928!
438
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
×
439
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
440
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
441
      code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
442
      taosWUnLockLatch(&pTq->lock);
×
443
      goto END;
×
444
    }
445

446
    bool exec = tqIsHandleExec(pHandle);
47,928!
447
    if (!exec) {
47,928!
448
      tqSetHandleExec(pHandle);
47,952!
449
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
450
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
47,952!
451
              req.subKey, pHandle);
452
      taosWUnLockLatch(&pTq->lock);
47,958✔
453
      break;
47,961✔
454
    }
455
    taosWUnLockLatch(&pTq->lock);
×
456

457
    tqDebug("tmq poll: consumer:0x%" PRIx64
5!
458
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
459
            consumerId, vgId, req.subKey, pHandle);
460
    taosMsleep(10);
5✔
461
  }
462

463
  // 3. update the epoch value
464
  if (pHandle->epoch < reqEpoch) {
47,961✔
465
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,444!
466
            reqEpoch);
467
    pHandle->epoch = reqEpoch;
1,444✔
468
  }
469

470
  char buf[TSDB_OFFSET_LEN] = {0};
47,961✔
471
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
47,961✔
472
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
47,960!
473
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
474

475
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
47,961✔
476
  tqSetHandleIdle(pHandle);
47,957!
477

478
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
47,957!
479
          req.subKey, pHandle);
480

481
END:
×
482
  tDestroySMqPollReq(&req);
47,959✔
483
  return code;
47,960✔
484
}
485

486
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
4✔
487
  if (pTq == NULL || pMsg == NULL) {
4!
488
    return TSDB_CODE_INVALID_PARA;
×
489
  }
490
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
4✔
491
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
4✔
492

493
  SMqVgOffset vgOffset = {0};
4✔
494

495
  SDecoder decoder;
496
  tDecoderInit(&decoder, (uint8_t*)data, len);
4✔
497
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
4!
498
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
499
    return terrno;
×
500
  }
501

502
  tDecoderClear(&decoder);
4✔
503

504
  STqOffset* pSavedOffset = NULL;
4✔
505
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
4✔
506
  if (code != 0) {
4✔
507
    return TSDB_CODE_TMQ_NO_COMMITTED;
1✔
508
  }
509
  vgOffset.offset = *pSavedOffset;
3✔
510

511
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
3!
512
  if (code < 0) {
3!
513
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
514
  }
515

516
  void* buf = rpcMallocCont(len);
3✔
517
  if (buf == NULL) {
3!
518
    return terrno;
×
519
  }
520
  SEncoder encoder = {0};
3✔
521
  tEncoderInit(&encoder, buf, len);
3✔
522
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
3✔
523
  tEncoderClear(&encoder);
3✔
524
  if (code < 0) {
3!
525
    rpcFreeCont(buf);
×
526
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
527
  }
528

529
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
3✔
530

531
  tmsgSendRsp(&rsp);
3✔
532
  return 0;
3✔
533
}
534

535
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
11✔
536
  if (pTq == NULL || pMsg == NULL) {
11!
537
    return TSDB_CODE_INVALID_PARA;
×
538
  }
539
  int32_t    code = 0;
11✔
540
  SMqPollReq req = {0};
11✔
541
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
11!
542
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
543
    return TSDB_CODE_INVALID_MSG;
×
544
  }
545

546
  int64_t      consumerId = req.consumerId;
11✔
547
  STqOffsetVal reqOffset = req.reqOffset;
11✔
548
  int32_t      vgId = TD_VID(pTq->pVnode);
11✔
549

550
  // 1. find handle
551
  taosRLockLatch(&pTq->lock);
11✔
552
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
11✔
553
  if (pHandle == NULL) {
11!
554
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
555
    taosRUnLockLatch(&pTq->lock);
×
556
    return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
×
557
  }
558

559
  // 2. check rebalance status
560
  if (pHandle->consumerId != consumerId) {
11!
561
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
562
            consumerId, vgId, req.subKey, pHandle->consumerId);
563
    taosRUnLockLatch(&pTq->lock);
×
564
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
565
  }
566

567
  int64_t sver = 0, ever = 0;
11✔
568
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
11✔
569
  taosRUnLockLatch(&pTq->lock);
11✔
570

571
  SMqDataRsp dataRsp = {0};
11✔
572
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
11✔
573
  if (code != 0) {
11!
574
    return code;
×
575
  }
576

577
  if (req.useSnapshot == true) {
11!
578
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
579
    code = TSDB_CODE_INVALID_PARA;
×
580
    goto END;
×
581
  }
582

583
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
11✔
584

585
  if (reqOffset.type == TMQ_OFFSET__LOG) {
11✔
586
    dataRsp.rspOffset.version = reqOffset.version;
2✔
587
  } else if (reqOffset.type < 0) {
9!
588
    STqOffset* pOffset = NULL;
9✔
589
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
9✔
590
    if (code == 0) {
9✔
591
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
1!
592
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
593
        code = TSDB_CODE_INVALID_PARA;
×
594
        goto END;
×
595
      }
596

597
      dataRsp.rspOffset.version = pOffset->val.version;
1✔
598
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
1!
599
             req.subKey, dataRsp.rspOffset.version);
600
    } else {
601
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
8!
602
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
8✔
603
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
×
604
        dataRsp.rspOffset.version = ever;
×
605
      }
606
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
8!
607
             dataRsp.rspOffset.version);
608
    }
609
  } else {
610
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
611
            reqOffset.type);
612
    code = TSDB_CODE_INVALID_PARA;
×
613
    goto END;
×
614
  }
615

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

618
END:
11✔
619
  tDeleteMqDataRsp(&dataRsp);
11✔
620
  return code;
11✔
621
}
622

623
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
936✔
624
  if (pTq == NULL || msg == NULL) {
936!
625
    return TSDB_CODE_INVALID_PARA;
×
626
  }
627
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
937✔
628
  int32_t        vgId = TD_VID(pTq->pVnode);
937✔
629

630
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
937!
631
  int32_t code = 0;
937✔
632

633
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
937✔
634
  if (pHandle) {
937✔
635
    while (1) {
×
636
      taosWLockLatch(&pTq->lock);
934✔
637
      bool exec = tqIsHandleExec(pHandle);
934✔
638

639
      if (exec) {
934!
640
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
641
               pHandle->subKey, pHandle);
642
        taosWUnLockLatch(&pTq->lock);
×
643
        taosMsleep(10);
×
644
        continue;
×
645
      }
646
      tqUnregisterPushHandle(pTq, pHandle);
934✔
647
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
934✔
648
      if (code != 0) {
934!
649
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
650
      }
651
      taosWUnLockLatch(&pTq->lock);
934✔
652
      break;
934✔
653
    }
654
  }
655

656
  taosWLockLatch(&pTq->lock);
937✔
657
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
937✔
658
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
277!
659
  }
660
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
937✔
661
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
887✔
662
  }
663

664
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
936!
665
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
666
  }
667
  taosWUnLockLatch(&pTq->lock);
937✔
668

669
  return 0;
937✔
670
}
671

672
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
119✔
673
  if (pTq == NULL || msg == NULL) {
119!
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  STqCheckInfo info = {0};
122✔
677
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
122✔
678
  if (code != 0) {
122!
679
    return code;
×
680
  }
681

682
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
122✔
683
  if (code != 0) {
122!
684
    tDeleteSTqCheckInfo(&info);
×
685
    return code;
×
686
  }
687

688
  return tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
122✔
689
}
690

691
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
13✔
692
  if (pTq == NULL || msg == NULL) {
13!
693
    return TSDB_CODE_INVALID_PARA;
×
694
  }
695
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
13!
696
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
697
  }
698
  return tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
13✔
699
}
700

701
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3,153✔
702
  if (pTq == NULL || msg == NULL) {
3,153!
703
    return TSDB_CODE_INVALID_PARA;
×
704
  }
705
  int         ret = 0;
3,168✔
706
  SMqRebVgReq req = {0};
3,168✔
707
  SDecoder    dc = {0};
3,168✔
708

709
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
3,168✔
710
  ret = tDecodeSMqRebVgReq(&dc, &req);
3,163✔
711
  if (ret < 0) {
3,158!
712
    goto end;
×
713
  }
714

715
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
3,158!
716
         req.oldConsumerId, req.newConsumerId);
717

718
  taosRLockLatch(&pTq->lock);
3,170✔
719
  STqHandle* pHandle = NULL;
3,173✔
720
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,173✔
721
  if (code != 0){
3,172✔
722
    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,405!
723
  }
724
  taosRUnLockLatch(&pTq->lock);
3,172✔
725
  if (pHandle == NULL) {
3,173✔
726
    if (req.oldConsumerId != -1) {
1,406!
727
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
×
728
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
729
    }
730
    if (req.newConsumerId == -1) {
1,406!
731
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64, req.vgId, req.newConsumerId);
×
732
      ret = TSDB_CODE_INVALID_PARA;
×
733
      goto end;
×
734
    }
735
    STqHandle handle = {0};
1,406✔
736
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,406✔
737
    if (ret < 0) {
1,406!
738
      tqDestroyTqHandle(&handle);
×
739
      goto end;
×
740
    }
741
    taosWLockLatch(&pTq->lock);
1,406✔
742
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,406✔
743
    taosWUnLockLatch(&pTq->lock);
1,406✔
744
  } else {
745
    while (1) {
×
746
      taosWLockLatch(&pTq->lock);
1,767✔
747
      bool exec = tqIsHandleExec(pHandle);
1,767!
748
      if (exec) {
1,767!
749
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
750
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
751
        taosWUnLockLatch(&pTq->lock);
×
752
        taosMsleep(10);
×
753
        continue;
×
754
      }
755
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,767✔
756
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
163!
757
      } else {
758
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,604!
759
               req.newConsumerId);
760

761
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
1,604✔
762
        atomic_store_32(&pHandle->epoch, 0);
1,604✔
763
        tqUnregisterPushHandle(pTq, pHandle);
1,604✔
764
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
1,603✔
765
      }
766
      taosWUnLockLatch(&pTq->lock);
1,764✔
767
      break;
1,766✔
768
    }
769
  }
770

771
end:
3,172✔
772
  tDecoderClear(&dc);
3,172✔
773
  return ret;
3,172✔
774
}
775

776
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
16,055!
777

778
int32_t tqBuildStreamTask(void* pTqObj, SStreamTask* pTask, int64_t nextProcessVer) {
12,347✔
779
  STQ*             pTq = (STQ*)pTqObj;
12,347✔
780
  int32_t          vgId = TD_VID(pTq->pVnode);
12,347✔
781
  SCheckpointInfo* pChkInfo = NULL;
12,347✔
782

783
  tqDebug("s-task:0x%x start to build task", pTask->id.taskId);
12,347✔
784

785
  int32_t code = streamTaskInit(pTask, pTq->pStreamMeta, &pTq->pVnode->msgCb, nextProcessVer);
12,347✔
786
  if (code != TSDB_CODE_SUCCESS) {
12,353!
787
    return code;
×
788
  }
789

790
  pTask->pBackend = NULL;
12,353✔
791

792
  // sink
793
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
12,353✔
794
  if (pOutputInfo->type == TASK_OUTPUT__SMA) {
12,353!
795
    pOutputInfo->smaSink.vnode = pTq->pVnode;
×
796
    pOutputInfo->smaSink.smaSink = smaHandleRes;
×
797
  } else if (pOutputInfo->type == TASK_OUTPUT__TABLE) {
12,353✔
798
    pOutputInfo->tbSink.vnode = pTq->pVnode;
6,172✔
799
    pOutputInfo->tbSink.tbSinkFunc = tqSinkDataIntoDstTable;
6,172✔
800

801
    int32_t   ver1 = 1;
6,172✔
802
    SMetaInfo info = {0};
6,172✔
803
    code = metaGetInfo(pTq->pVnode->pMeta, pOutputInfo->tbSink.stbUid, &info, NULL);
6,172✔
804
    if (code == TSDB_CODE_SUCCESS) {
6,168✔
805
      ver1 = info.skmVer;
5,795✔
806
    }
807

808
    SSchemaWrapper* pschemaWrapper = pOutputInfo->tbSink.pSchemaWrapper;
6,168✔
809
    pOutputInfo->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
6,168✔
810
    if (pOutputInfo->tbSink.pTSchema == NULL) {
6,175!
811
      return terrno;
×
812
    }
813

814
    pOutputInfo->tbSink.pTbInfo = tSimpleHashInit(10240, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,175✔
815
    if (pOutputInfo->tbSink.pTbInfo == NULL) {
6,178!
816
      tqError("vgId:%d failed init sink tableInfo, code:%s", vgId, tstrerror(terrno));
×
817
      return terrno;
×
818
    }
819

820
    tSimpleHashSetFreeFp(pOutputInfo->tbSink.pTbInfo, freePtr);
6,178✔
821
  }
822

823
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
12,359✔
824
    bool scanDropCtb = pTask->subtableWithoutMd5 ? true : false;
6,273✔
825
    SWalFilterCond cond = {.deleteMsg = 1, .scanDropCtb = scanDropCtb};  // delete msg also extract from wal files
6,273✔
826
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, &cond, pTask->id.taskId);
6,273✔
827
    if (pTask->exec.pWalReader == NULL) {
6,273!
828
      tqError("vgId:%d failed init wal reader, code:%s", vgId, tstrerror(terrno));
×
829
      return terrno;
×
830
    }
831
  }
832

833
  streamTaskResetUpstreamStageInfo(pTask);
12,359✔
834

835
  pChkInfo = &pTask->chkInfo;
12,352✔
836
  tqSetRestoreVersionInfo(pTask);
12,352✔
837

838
  char*       p = streamTaskGetStatus(pTask).name;
12,349✔
839
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
12,348✔
840

841
  if (pTask->info.fillHistory) {
12,352✔
842
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
3,747!
843
           " nextProcessVer:%" PRId64
844
           " child id:%d, level:%d, cur-status:%s, next-status:%s taskType:%d, related stream task:0x%x "
845
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
846
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
847
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
848
           (int32_t)pTask->streamTaskId.taskId, pTask->info.delaySchedParam, nextProcessVer);
849
  } else {
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
8,605✔
851
           " nextProcessVer:%" PRId64
852
           " child id:%d, level:%d, cur-status:%s next-status:%s taskType:%d, related helper-task:0x%x "
853
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
854
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
855
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
856
           (int32_t)pTask->hTaskInfo.id.taskId, pTask->info.delaySchedParam, nextProcessVer);
857

858
    if (pChkInfo->checkpointVer > pChkInfo->nextProcessVer) {
8,615!
859
      tqError("vgId:%d build stream task, s-task:%s, checkpointVer:%" PRId64 " > nextProcessVer:%" PRId64, vgId,
×
860
              pTask->id.idStr, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
861
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
862
    }
863
  }
864

865
  return 0;
12,362✔
866
}
867

868
int32_t tqProcessTaskCheckReq(STQ* pTq, SRpcMsg* pMsg) { return tqStreamTaskProcessCheckReq(pTq->pStreamMeta, pMsg); }
31,421✔
869

870
int32_t tqProcessTaskCheckRsp(STQ* pTq, SRpcMsg* pMsg) {
31,243✔
871
  return tqStreamTaskProcessCheckRsp(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
31,243✔
872
}
873

874
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
11,837✔
875
  return tqStreamTaskProcessDeployReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, sversion, msg, msgLen,
11,854✔
876
                                      vnodeIsRoleLeader(pTq->pVnode), pTq->pVnode->restored);
11,837✔
877
}
878

879
static void doStartFillhistoryStep2(SStreamTask* pTask, SStreamTask* pStreamTask, STQ* pTq) {
1,801✔
880
  const char*    id = pTask->id.idStr;
1,801✔
881
  int64_t        nextProcessedVer = pStreamTask->hTaskInfo.haltVer;
1,801✔
882
  SVersionRange* pStep2Range = &pTask->step2Range;
1,801✔
883
  int32_t        vgId = pTask->pMeta->vgId;
1,801✔
884

885
  // if it's an source task, extract the last version in wal.
886
  bool done = streamHistoryTaskSetVerRangeStep2(pTask, nextProcessedVer);
1,801✔
887
  pTask->execInfo.step2Start = taosGetTimestampMs();
1,801✔
888

889
  if (done) {
1,801✔
890
    qDebug("s-task:%s scan wal(step 2) verRange:%" PRId64 "-%" PRId64 " ended, elapsed time:%.2fs", id,
1,045✔
891
           pStep2Range->minVer, pStep2Range->maxVer, 0.0);
892
    int32_t code = streamTaskPutTranstateIntoInputQ(pTask);  // todo: msg lost.
1,045✔
893
    if (code) {
1,045!
894
      qError("s-task:%s failed put trans-state into inputQ, code:%s", id, tstrerror(code));
×
895
    }
896
    (void)streamExecTask(pTask);  // exec directly
1,045✔
897
  } else {
898
    STimeWindow* pWindow = &pTask->dataRange.window;
756✔
899
    tqDebug("s-task:%s level:%d verRange:%" PRId64 "-%" PRId64 " window:%" PRId64 "-%" PRId64
756✔
900
            ", do secondary scan-history from WAL after halt the related stream task:%s",
901
            id, pTask->info.taskLevel, pStep2Range->minVer, pStep2Range->maxVer, pWindow->skey, pWindow->ekey,
902
            pStreamTask->id.idStr);
903
    if (pTask->status.schedStatus != TASK_SCHED_STATUS__WAITING) {
756!
904
      tqError("s-task:%s level:%d unexpected sched-status:%d", id, pTask->info.taskLevel, pTask->status.schedStatus);
×
905
    }
906

907
    int32_t code = streamSetParamForStreamScannerStep2(pTask, pStep2Range, pWindow);
756✔
908
    if (code) {
756!
909
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
910
    }
911

912
    int64_t dstVer = pStep2Range->minVer;
756✔
913
    pTask->chkInfo.nextProcessVer = dstVer;
756✔
914

915
    walReaderSetSkipToVersion(pTask->exec.pWalReader, dstVer);
756✔
916
    tqDebug("s-task:%s wal reader start scan WAL verRange:%" PRId64 "-%" PRId64 ", set sched-status:%d", id, dstVer,
756✔
917
            pStep2Range->maxVer, TASK_SCHED_STATUS__INACTIVE);
918

919
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
756✔
920

921
    // now the fill-history task starts to scan data from wal files.
922
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
756✔
923
  }
924
}
1,801✔
925

926
int32_t handleStep2Async(SStreamTask* pStreamTask, void* param) {
1,801✔
927
  STQ* pTq = param;
1,801✔
928

929
  SStreamMeta* pMeta = pStreamTask->pMeta;
1,801✔
930
  STaskId      hId = pStreamTask->hTaskInfo.id;
1,801✔
931
  SStreamTask* pTask = NULL;
1,801✔
932
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
1,801✔
933
  if (pTask == NULL) {
1,801!
934
    tqWarn("s-task:0x%x failed to acquired it to exec step 2, scan wal quit", (int32_t)hId.taskId);
×
935
    return TSDB_CODE_SUCCESS;
×
936
  }
937

938
  doStartFillhistoryStep2(pTask, pStreamTask, pTq);
1,801✔
939

940
  streamMetaReleaseTask(pMeta, pTask);
1,801✔
941
  return TSDB_CODE_SUCCESS;
1,801✔
942
}
943

944
// this function should be executed by only one thread, so we set a sentinel to protect this function
945
int32_t tqProcessTaskScanHistory(STQ* pTq, SRpcMsg* pMsg) {
1,942✔
946
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
1,942✔
947
  SStreamMeta*           pMeta = pTq->pStreamMeta;
1,942✔
948
  int32_t                code = TSDB_CODE_SUCCESS;
1,942✔
949
  SStreamTask*           pTask = NULL;
1,942✔
950
  SStreamTask*           pStreamTask = NULL;
1,942✔
951
  char*                  pStatus = NULL;
1,942✔
952
  int32_t                taskType = 0;
1,942✔
953

954
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,942✔
955
  if (pTask == NULL) {
1,943✔
956
    tqError("vgId:%d failed to acquire stream task:0x%x during scan history data, task may have been destroyed",
1!
957
            pMeta->vgId, pReq->taskId);
958
    return code;
1✔
959
  }
960

961
  // do recovery step1
962
  const char* id = pTask->id.idStr;
1,942✔
963
  streamMutexLock(&pTask->lock);
1,942✔
964

965
  SStreamTaskState s = streamTaskGetStatus(pTask);
1,942✔
966
  pStatus = s.name;
1,942✔
967
  taskType = pTask->info.fillHistory;
1,942✔
968

969
  if ((s.state != TASK_STATUS__SCAN_HISTORY && taskType == STREAM_HISTORY_TASK) ||
1,942!
970
      (s.state != TASK_STATUS__READY && taskType == STREAM_RECALCUL_TASK) ||
1,942!
971
      (pTask->status.downstreamReady == 0)) {
1,942!
972
    tqError("s-task:%s vgId:%d status:%s downstreamReady:%d not allowed/ready for scan-history data, quit", id,
×
973
            pMeta->vgId, s.name, pTask->status.downstreamReady);
974

975
    streamMutexUnlock(&pTask->lock);
×
976
    streamMetaReleaseTask(pMeta, pTask);
×
977
    return 0;
×
978
  }
979

980
  if (pTask->exec.pExecutor == NULL) {
1,942!
981
    tqError("s-task:%s vgId:%d executor is null, not executor scan history", id, pMeta->vgId);
×
982

983
    streamMutexUnlock(&pTask->lock);
×
984
    streamMetaReleaseTask(pMeta, pTask);
×
985
    return 0;
×
986
  }
987

988
  streamMutexUnlock(&pTask->lock);
1,942✔
989

990
  // avoid multi-thread exec
991
  while (1) {
×
992
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
1,941✔
993
    if (sentinel != 0) {
1,942!
994
      tqDebug("s-task:%s already in scan-history func, wait for 100ms, and try again", id);
×
995
      taosMsleep(100);
×
996
    } else {
997
      break;
1,942✔
998
    }
999
  }
1000

1001
  // let's decide which step should be executed now
1002
  if (pTask->execInfo.step1Start == 0) {
1,942✔
1003
    int64_t ts = taosGetTimestampMs();
1,803✔
1004
    pTask->execInfo.step1Start = ts;
1,803✔
1005
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
1,803✔
1006
  } else {
1007
    if (pTask->execInfo.step2Start == 0) {
139!
1008
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
139!
1009
              id, pTask->execInfo.step1Start, pTask->execInfo.step1El);
1010
    } else {
1011
      tqDebug("s-task:%s already in step2, no need to scan-history data, step2 startTs:%" PRId64, id,
×
1012
              pTask->execInfo.step2Start);
1013

1014
      atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1015
      streamMetaReleaseTask(pMeta, pTask);
×
1016
      return 0;
×
1017
    }
1018
  }
1019

1020
  // we have to continue retrying to successfully execute the scan history task.
1021
  if (!streamTaskSetSchedStatusWait(pTask)) {
1,942!
1022
    tqError(
×
1023
        "s-task:%s failed to start scan-history in first stream time window since already started, unexpected "
1024
        "sched-status:%d",
1025
        id, pTask->status.schedStatus);
1026
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1027
    streamMetaReleaseTask(pMeta, pTask);
×
1028
    return 0;
×
1029
  }
1030

1031
  int64_t              st = taosGetTimestampMs();
1,942✔
1032
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
1,942✔
1033

1034
  double el = (taosGetTimestampMs() - st) / 1000.0;
1,942✔
1035
  pTask->execInfo.step1El += el;
1,942✔
1036

1037
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
1,942✔
1038
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
141✔
1039
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
141✔
1040

1041
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
141✔
1042
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
139✔
1043
    } else {
1044
      SStreamTaskState p = streamTaskGetStatus(pTask);
2✔
1045
      ETaskStatus      localStatus = p.state;
2✔
1046

1047
      if (localStatus == TASK_STATUS__PAUSE) {
2!
1048
        tqDebug("s-task:%s is paused in the step1, elapsed time:%.2fs total:%.2fs, sched-status:%d", id, el,
×
1049
                pTask->execInfo.step1El, status);
1050
      } else if (localStatus == TASK_STATUS__STOP || localStatus == TASK_STATUS__DROPPING) {
2!
1051
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
2!
1052
                pTask->execInfo.step1El);
1053
      }
1054
    }
1055

1056
    streamMetaReleaseTask(pMeta, pTask);
141✔
1057
    return 0;
141✔
1058
  }
1059

1060
  // the following procedure should be executed, no matter status is stop/pause or not
1061
  if (taskType == STREAM_HISTORY_TASK) {
1,801!
1062
    tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
1,801✔
1063
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1064
    tqDebug("s-task:%s recalculate ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
×
1065
  } else {
1066
    tqError("s-task:%s fill-history is disabled, unexpected", id);
×
1067
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1068
  }
1069

1070
  // 1. get the related stream task
1071
  code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,801✔
1072
  if (pStreamTask == NULL) {
1,801!
1073

1074
    int32_t ret = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
1075
    if (ret == 0 && pStreamTask != NULL) {
×
1076
      tqWarn("s-task:0x%" PRIx64 " stopped, not ready for related task:%s scan-history work, do nothing",
×
1077
             pTask->streamTaskId.taskId, pTask->id.idStr);
1078
      streamMetaReleaseTask(pMeta, pStreamTask);
×
1079
    } else {
1080
      tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
×
1081
              pTask->streamTaskId.taskId, pTask->id.idStr);
1082

1083
      tqDebug("s-task:%s fill-history task set status to be dropping", id);
×
1084
      code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
1085
    }
1086

1087
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1088
    streamMetaReleaseTask(pMeta, pTask);
×
1089
    return code;
×
1090
  }
1091

1092
  if (pStreamTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,801!
1093
    tqError("s-task:%s fill-history task related stream task level:%d, unexpected", id, pStreamTask->info.taskLevel);
×
1094
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1095
  }
1096

1097
  if (taskType == STREAM_HISTORY_TASK) {
1,801!
1098
    code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
1,801✔
1099
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1100
    // send recalculate end block
1101
    code = streamCreateAddRecalculateEndBlock(pStreamTask);
×
1102
    if (code) {
×
1103
      tqError("s-task:%s failed to create-add recalculate end block, code:%s", id, tstrerror(code));
×
1104
    }
1105
    streamTaskSetSchedStatusInactive(pTask);
×
1106
  }
1107

1108
  streamMetaReleaseTask(pMeta, pStreamTask);
1,801✔
1109

1110
  atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
1,801✔
1111
  streamMetaReleaseTask(pMeta, pTask);
1,801✔
1112
  return code;
1,801✔
1113
}
1114

1115
int32_t tqProcessTaskRunReq(STQ* pTq, SRpcMsg* pMsg) {
788,058✔
1116
  int32_t  code = 0;
788,058✔
1117
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
788,058✔
1118
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
788,058✔
1119
  SDecoder decoder;
1120

1121
  SStreamTaskRunReq req = {0};
788,058✔
1122
  tDecoderInit(&decoder, (uint8_t*)msg, len);
788,058✔
1123
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
788,153!
1124
    tqError("vgId:%d failed to decode task run req, code:%s", pTq->pStreamMeta->vgId, tstrerror(code));
×
1125
    tDecoderClear(&decoder);
×
1126
    return TSDB_CODE_SUCCESS;
×
1127
  }
1128

1129
  tDecoderClear(&decoder);
787,687✔
1130

1131
  // extracted submit data from wal files for all tasks
1132
  if (req.reqType == STREAM_EXEC_T_EXTRACT_WAL_DATA) {
784,787✔
1133
    return tqScanWal(pTq);
685,347✔
1134
  } else {
1135
    code = tqStreamTaskProcessRunReq(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
99,440✔
1136
    if (code) {
100,831✔
1137
      tqError("vgId:%d failed to create task run req, code:%s", TD_VID(pTq->pVnode), tstrerror(code));
11!
1138
    }
1139

1140
    return code;
100,905✔
1141
  }
1142
}
1143

1144
int32_t tqProcessTaskDispatchReq(STQ* pTq, SRpcMsg* pMsg) {
30,731✔
1145
  return tqStreamTaskProcessDispatchReq(pTq->pStreamMeta, pMsg);
30,731✔
1146
}
1147

1148
int32_t tqProcessTaskDispatchRsp(STQ* pTq, SRpcMsg* pMsg) {
31,065✔
1149
  return tqStreamTaskProcessDispatchRsp(pTq->pStreamMeta, pMsg);
31,065✔
1150
}
1151

1152
int32_t tqProcessTaskDropReq(STQ* pTq, char* msg, int32_t msgLen) {
5,978✔
1153
  return tqStreamTaskProcessDropReq(pTq->pStreamMeta, msg, msgLen);
5,978✔
1154
}
1155

1156
int32_t tqProcessTaskUpdateCheckpointReq(STQ* pTq, char* msg, int32_t msgLen) {
8,276✔
1157
  return tqStreamTaskProcessUpdateCheckpointReq(pTq->pStreamMeta, pTq->pVnode->restored, msg);
8,276✔
1158
}
1159

1160
int32_t tqProcessTaskConsenChkptIdReq(STQ* pTq, SRpcMsg* pMsg) {
96✔
1161
  return tqStreamTaskProcessConsenChkptIdReq(pTq->pStreamMeta, pMsg);
96✔
1162
}
1163

1164
int32_t tqProcessTaskPauseReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
722✔
1165
  return tqStreamTaskProcessTaskPauseReq(pTq->pStreamMeta, msg);
722✔
1166
}
1167

1168
int32_t tqProcessTaskResumeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
1,230✔
1169
  return tqStreamTaskProcessTaskResumeReq(pTq, sversion, msg, true);
1,230✔
1170
}
1171

1172
int32_t tqProcessTaskRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
517✔
1173
  return tqStreamTaskProcessRetrieveReq(pTq->pStreamMeta, pMsg);
517✔
1174
}
1175

1176
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
408✔
1177

1178
int32_t tqStreamProgressRetrieveReq(STQ* pTq, SRpcMsg* pMsg) {
×
1179
  char*               msgStr = pMsg->pCont;
×
1180
  char*               msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
1181
  int32_t             msgLen = pMsg->contLen - sizeof(SMsgHead);
×
1182
  int32_t             code = 0;
×
1183
  SStreamProgressReq  req;
1184
  char*               pRspBuf = taosMemoryCalloc(1, sizeof(SMsgHead) + sizeof(SStreamProgressRsp));
×
1185
  SStreamProgressRsp* pRsp = POINTER_SHIFT(pRspBuf, sizeof(SMsgHead));
×
1186
  if (!pRspBuf) {
×
1187
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1188
    code = -1;
×
1189
    goto _OVER;
×
1190
  }
1191

1192
  code = tDeserializeStreamProgressReq(msgBody, msgLen, &req);
×
1193
  if (code == TSDB_CODE_SUCCESS) {
×
1194
    code = tqGetStreamExecInfo(pTq->pVnode, req.streamId, &pRsp->progressDelay, &pRsp->fillHisFinished);
×
1195
  }
1196
  if (code == TSDB_CODE_SUCCESS) {
×
1197
    pRsp->fetchIdx = req.fetchIdx;
×
1198
    pRsp->subFetchIdx = req.subFetchIdx;
×
1199
    pRsp->vgId = req.vgId;
×
1200
    pRsp->streamId = req.streamId;
×
1201
    code = tSerializeStreamProgressRsp(pRsp, sizeof(SStreamProgressRsp) + sizeof(SMsgHead), pRsp);
×
1202
    if (code) {
×
1203
      goto _OVER;
×
1204
    }
1205

1206
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
1207
    rsp.pCont = pRspBuf;
×
1208
    pRspBuf = NULL;
×
1209
    rsp.contLen = sizeof(SMsgHead) + sizeof(SStreamProgressRsp);
×
1210
    tmsgSendRsp(&rsp);
×
1211
  }
1212

1213
_OVER:
×
1214
  if (pRspBuf) {
×
1215
    taosMemoryFree(pRspBuf);
×
1216
  }
1217
  return code;
×
1218
}
1219

1220
// always return success to mnode
1221
//todo: handle failure of build and send msg to mnode
1222
static void doSendChkptSourceRsp(SStreamCheckpointSourceReq* pReq, SRpcHandleInfo* pRpcInfo, int32_t code,
13✔
1223
                                 int32_t taskId) {
1224
  SRpcMsg rsp = {0};
13✔
1225
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
13✔
1226
  if (ret) {  // suppress the error in build checkpoint source rsp
13!
1227
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1228
  }
1229
  tmsgSendRsp(&rsp);  // error occurs
13✔
1230
}
13✔
1231

1232
// no matter what kinds of error happened, make sure the mnode will receive the success execution code.
1233
int32_t tqProcessTaskCheckPointSourceReq(STQ* pTq, SRpcMsg* pMsg, SRpcMsg* pRsp) {
4,959✔
1234
  int32_t                    vgId = TD_VID(pTq->pVnode);
4,959✔
1235
  SStreamMeta*               pMeta = pTq->pStreamMeta;
4,959✔
1236
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
4,959✔
1237
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
4,959✔
1238
  int32_t                    code = 0;
4,959✔
1239
  SStreamCheckpointSourceReq req = {0};
4,959✔
1240
  SDecoder                   decoder = {0};
4,959✔
1241
  SStreamTask*               pTask = NULL;
4,959✔
1242
  int64_t                    checkpointId = 0;
4,959✔
1243

1244
  // disable auto rsp to mnode
1245
  pRsp->info.handle = NULL;
4,959✔
1246

1247
  tDecoderInit(&decoder, (uint8_t*)msg, len);
4,959✔
1248
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
4,960!
1249
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
1250
    tDecoderClear(&decoder);
×
1251
    tqError("vgId:%d failed to decode checkpoint-source msg, code:%s", vgId, tstrerror(code));
×
1252
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1253
    return TSDB_CODE_SUCCESS;  // always return success to mnode,
×
1254
  }
1255

1256
  tDecoderClear(&decoder);
4,942✔
1257

1258
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
4,961✔
1259
    tqDebug("vgId:%d not leader, ignore checkpoint-source msg, checkpontId:%" PRId64 ", s-task:0x%x", vgId,
9!
1260
            req.checkpointId, req.taskId);
1261
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
9✔
1262
    return TSDB_CODE_SUCCESS;  // always return success to mnode
9✔
1263
  }
1264

1265
  if (!pTq->pVnode->restored) {
4,950✔
1266
    tqDebug("vgId:%d checkpoint-source msg received during restoring, checkpointId:%" PRId64
1!
1267
            ", transId:%d s-task:0x%x ignore it",
1268
            vgId, req.checkpointId, req.transId, req.taskId);
1269
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
1✔
1270
    return TSDB_CODE_SUCCESS;  // always return success to mnode
1✔
1271
  }
1272

1273
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
4,949✔
1274
  if (pTask == NULL || code != 0) {
4,948!
1275
    tqError("vgId:%d failed to find s-task:0x%x, ignore checkpoint msg. checkpointId:%" PRId64
×
1276
            " transId:%d it may have been destroyed or stopped",
1277
            vgId, req.taskId, req.checkpointId, req.transId);
1278
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1279
    return TSDB_CODE_SUCCESS;
×
1280
  }
1281

1282
  if (pTask->status.downstreamReady != 1) {
4,951!
1283
    // record the latest failed checkpoint id
1284
    streamTaskSetFailedChkptInfo(pTask, req.transId, req.checkpointId);
×
1285
    tqError("s-task:%s not ready for checkpoint, since downstream not ready, ignore this checkpointId:%" PRId64
×
1286
            ", transId:%d set it failed",
1287
            pTask->id.idStr, req.checkpointId, req.transId);
1288

1289
    streamMetaReleaseTask(pMeta, pTask);
×
1290
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1291
    return TSDB_CODE_SUCCESS;  // todo retry handle error
×
1292
  }
1293

1294
  // todo save the checkpoint failed info
1295
  streamMutexLock(&pTask->lock);
4,951✔
1296
  ETaskStatus status = streamTaskGetStatus(pTask).state;
4,967✔
1297

1298
  if (req.mndTrigger == 1) {
4,952✔
1299
    if (status == TASK_STATUS__HALT || status == TASK_STATUS__PAUSE) {
3,359!
1300
      tqError("s-task:%s not ready for checkpoint, since it is halt, ignore checkpointId:%" PRId64 ", set it failure",
2!
1301
              pTask->id.idStr, req.checkpointId);
1302

1303
      streamMutexUnlock(&pTask->lock);
2✔
1304
      streamMetaReleaseTask(pMeta, pTask);
3✔
1305
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
3✔
1306
      return TSDB_CODE_SUCCESS;
3✔
1307
    }
1308
  } else {
1309
    if (status != TASK_STATUS__HALT) {
1,593!
1310
      tqError("s-task:%s should in halt status, let's halt it directly", pTask->id.idStr);
×
1311
      //      streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_HALT);
1312
    }
1313
  }
1314

1315
  // check if the checkpoint msg already sent or not.
1316
  if (status == TASK_STATUS__CK) {
4,941!
1317
    streamTaskGetActiveCheckpointInfo(pTask, NULL, &checkpointId);
×
1318

1319
    tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1320
           " transId:%d already handled, ignore msg and continue process checkpoint",
1321
           pTask->id.idStr, checkpointId, req.transId);
1322

1323
    streamMutexUnlock(&pTask->lock);
×
1324
    streamMetaReleaseTask(pMeta, pTask);
×
1325
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SYN_PROPOSE_NOT_READY, req.taskId);
×
1326
    return TSDB_CODE_SUCCESS;
×
1327
  } else {  // checkpoint already finished, and not in checkpoint status
1328
    if (req.checkpointId <= pTask->chkInfo.checkpointId) {
4,941!
1329
      tqWarn("s-task:%s repeatly recv checkpoint-source msg checkpointId:%" PRId64
×
1330
             " transId:%d already handled, return success",
1331
             pTask->id.idStr, req.checkpointId, req.transId);
1332

1333
      streamMutexUnlock(&pTask->lock);
×
1334
      streamMetaReleaseTask(pMeta, pTask);
×
1335
      doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1336
      return TSDB_CODE_SUCCESS;
×
1337
    }
1338
  }
1339

1340
  code = streamProcessCheckpointSourceReq(pTask, &req);
4,941✔
1341
  streamMutexUnlock(&pTask->lock);
4,960✔
1342

1343
  if (code) {
4,958!
1344
    qError("s-task:%s (vgId:%d) failed to process checkpoint-source req, code:%s", pTask->id.idStr, vgId,
×
1345
           tstrerror(code));
1346
    streamMetaReleaseTask(pMeta, pTask);
×
1347
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1348
    return TSDB_CODE_SUCCESS;
×
1349
  }
1350

1351
  if (req.mndTrigger) {
4,958✔
1352
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64 ", transId:%d, ",
3,365!
1353
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId);
1354
  } else {
1355
    const char* pPrevStatus = streamTaskGetStatusStr(streamTaskGetPrevStatus(pTask));
1,593✔
1356
    tqInfo("s-task:%s (vgId:%d) level:%d receive checkpoint-source msg chkpt:%" PRId64
1,593!
1357
           ", transId:%d after transfer-state, prev status:%s",
1358
           pTask->id.idStr, vgId, pTask->info.taskLevel, req.checkpointId, req.transId, pPrevStatus);
1359
  }
1360

1361
  code = streamAddCheckpointSourceRspMsg(&req, &pMsg->info, pTask);
4,965✔
1362
  if (code != TSDB_CODE_SUCCESS) {
4,963!
1363
    streamTaskSetCheckpointFailed(pTask);  // set the checkpoint failed
×
1364
    doSendChkptSourceRsp(&req, &pMsg->info, TSDB_CODE_SUCCESS, req.taskId);
×
1365
  }
1366

1367
  streamMetaReleaseTask(pMeta, pTask);
4,963✔
1368
  return TSDB_CODE_SUCCESS;
4,964✔
1369
}
1370

1371
// downstream task has complete the stream task checkpoint procedure, let's start the handle the rsp by execute task
1372
int32_t tqProcessTaskCheckpointReadyMsg(STQ* pTq, SRpcMsg* pMsg) {
8,586✔
1373
  int32_t vgId = TD_VID(pTq->pVnode);
8,586✔
1374

1375
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
8,586!
1376
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1377
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1378
    int32_t  code = 0;
×
1379
    SDecoder decoder;
1380

1381
    SStreamCheckpointReadyMsg req = {0};
×
1382
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1383
    if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
×
1384
      code = TSDB_CODE_MSG_DECODE_ERROR;
×
1385
      tDecoderClear(&decoder);
×
1386
      return code;
×
1387
    }
1388
    tDecoderClear(&decoder);
×
1389

1390
    tqError("vgId:%d not leader, s-task:0x%x ignore the retrieve checkpoint-trigger msg from s-task:0x%x vgId:%d", vgId,
×
1391
            req.upstreamTaskId, req.downstreamTaskId, req.downstreamNodeId);
1392

1393
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1394
  }
1395

1396
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
8,599✔
1397
}
1398

1399
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
52✔
1400
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
104✔
1401
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
52✔
1402
}
1403

1404
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1405
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1406
}
1407

1408
int32_t tqProcessAllTaskStopReq(STQ* pTq, SRpcMsg* pMsg) {
4,921✔
1409
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
4,921✔
1410
}
1411

1412
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1413
  int32_t vgId = TD_VID(pTq->pVnode);
×
1414

1415
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1416
    SRetrieveChkptTriggerReq req = {0};
×
1417

1418
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1419
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1420
    SDecoder decoder = {0};
×
1421

1422
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1423
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1424
      tDecoderClear(&decoder);
×
1425
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1426
      return TSDB_CODE_INVALID_MSG;
×
1427
    }
1428
    tDecoderClear(&decoder);
×
1429

1430
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1431
            req.downstreamTaskId);
1432
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1433
  }
1434

1435
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1436
}
1437

1438
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1439
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1440
}
1441

1442
// this function is needed, do not try to remove it.
1443
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
69,400✔
1444

1445
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
3,283✔
1446
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
3,283✔
1447
}
1448

1449
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
8,597✔
1450
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
8,597✔
1451
}
1452

1453
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
9,250✔
1454
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
9,250✔
1455
}
1456

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