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

taosdata / TDengine / #3661

17 Mar 2025 05:39AM UTC coverage: 62.007% (-0.03%) from 62.039%
#3661

push

travis-ci

web-flow
tests: add tdb ut (#30093)

* fix: compile warnings

* tests: add tdb ut

* test(tdb): fix return code

* test: recover ut

* fix: minor changes

* fix: enable test

* fix: ut errors

---------

Co-authored-by: Minglei Jin <mljin@taosdata.com>

153829 of 317582 branches covered (48.44%)

Branch coverage included in aggregate %.

240310 of 318051 relevant lines covered (75.56%)

19602636.8 hits per line

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

62.36
/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; }
3,450,880!
29
static FORCE_INLINE void tqSetHandleExec(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_EXEC; }
3,449,834✔
30
static FORCE_INLINE void tqSetHandleIdle(STqHandle* pHandle) { if (pHandle != NULL) pHandle->status = TMQ_HANDLE_STATUS_IDLE; }
3,450,534✔
31

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

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

63
static bool tqOffsetEqual(const STqOffset* pLeft, const STqOffset* pRight) {
15,066✔
64
  if (pLeft == NULL || pRight == NULL) {
15,066!
65
    return false;
×
66
  }
67
  return pLeft->val.type == TMQ_OFFSET__LOG && pRight->val.type == TMQ_OFFSET__LOG &&
29,744✔
68
         pLeft->val.version == pRight->val.version;
14,677✔
69
}
70

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

80
  pVnode->pTq = pTq;
11,904✔
81
  pTq->pVnode = pVnode;
11,904✔
82

83
  pTq->path = taosStrdup(path);
11,904!
84
  if (pTq->path == NULL) {
11,898!
85
    return terrno;
×
86
  }
87

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

94
  taosInitRWLatch(&pTq->lock);
11,899✔
95

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

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

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

113
  return tqInitialize(pTq);
11,905✔
114
}
115

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

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

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

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

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

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

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

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

171
  taosMemoryFree(pTq);
11,908!
172
}
173

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

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

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

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

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

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

223
  char buf1[TSDB_OFFSET_LEN] = {0};
3,082,654✔
224
  char buf2[TSDB_OFFSET_LEN] = {0};
3,082,654✔
225
  (void)tFormatOffset(buf1, TSDB_OFFSET_LEN, &(pRsp->reqOffset));
3,082,654✔
226
  (void)tFormatOffset(buf2, TSDB_OFFSET_LEN, &(pRsp->rspOffset));
3,082,662✔
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,
3,082,667✔
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);
3,082,666✔
232
}
233

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

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

249
  tDecoderClear(&decoder);
16,069✔
250

251
  STqOffset* pOffset = &vgOffset.offset;
16,055✔
252

253
  if (pOffset->val.type == TMQ_OFFSET__SNAPSHOT_DATA || pOffset->val.type == TMQ_OFFSET__SNAPSHOT_META) {
16,055!
254
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:snapshot) uid:%" PRId64 ", ts:%" PRId64,
449!
255
            pOffset->subKey, vgId, pOffset->val.uid, pOffset->val.ts);
256
  } else if (pOffset->val.type == TMQ_OFFSET__LOG) {
15,606!
257
    tqDebug("receive offset commit msg to %s on vgId:%d, offset(type:log) version:%" PRId64, pOffset->subKey, vgId,
15,612✔
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;
16,080✔
266
  code = tqMetaGetOffset(pTq, pOffset->subKey, &pSavedOffset);
16,080✔
267
  if (code == 0 && tqOffsetEqual(pOffset, pSavedOffset)) {
16,076✔
268
    tqInfo("not update the offset, vgId:%d sub:%s since committed:%" PRId64 " less than/equal to existed:%" PRId64,
7!
269
           vgId, pOffset->subKey, pOffset->val.version, pSavedOffset->val.version);
270
    goto end;  // no need to update the offset value
7✔
271
  }
272

273
  // save the new offset value
274
  if (taosHashPut(pTq->pOffset, pOffset->subKey, strlen(pOffset->subKey), pOffset, sizeof(STqOffset))) {
16,068!
275
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
276
    return -1;
×
277
  }
278

279
  if (tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), msg,
16,069!
280
                     msgLen >= sizeof(vgOffset.consumerId) ? msgLen - sizeof(vgOffset.consumerId) : 0) < 0) {
281
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
282
    return -1;
×
283
  }
284

285
  return 0;
16,070✔
286
end:
7✔
287
  tOffsetDestroy(&vgOffset.offset.val);
7✔
288
  return code;
7✔
289
}
290

291
int32_t tqProcessSeekReq(STQ* pTq, SRpcMsg* pMsg) {
28✔
292
  if (pTq == NULL || pMsg == NULL) {
28!
293
    return TSDB_CODE_INVALID_PARA;
×
294
  }
295
  SMqSeekReq req = {0};
28✔
296
  int32_t    vgId = TD_VID(pTq->pVnode);
28✔
297
  SRpcMsg    rsp = {.info = pMsg->info};
28✔
298
  int        code = 0;
28✔
299

300
  if (tDeserializeSMqSeekReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
28!
301
    code = TSDB_CODE_OUT_OF_MEMORY;
×
302
    goto end;
×
303
  }
304

305
  tqDebug("tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s", req.consumerId, vgId, req.subKey);
28✔
306
  taosWLockLatch(&pTq->lock);
28✔
307

308
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
28✔
309
  if (pHandle == NULL) {
28!
310
    tqWarn("tmq seek: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", req.consumerId, vgId, req.subKey);
×
311
    code = 0;
×
312
    taosWUnLockLatch(&pTq->lock);
×
313
    goto end;
×
314
  }
315

316
  // 2. check consumer-vg assignment status
317
  if (pHandle->consumerId != req.consumerId) {
28!
318
    tqError("ERROR tmq seek: consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
319
            req.consumerId, vgId, req.subKey, pHandle->consumerId);
320
    taosWUnLockLatch(&pTq->lock);
×
321
    code = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
322
    goto end;
×
323
  }
324

325
  // if consumer register to push manager, push empty to consumer to change vg status from TMQ_VG_STATUS__WAIT to
326
  // TMQ_VG_STATUS__IDLE, otherwise poll data failed after seek.
327
  tqUnregisterPushHandle(pTq, pHandle);
28✔
328
  taosWUnLockLatch(&pTq->lock);
28✔
329

330
end:
28✔
331
  rsp.code = code;
28✔
332
  tmsgSendRsp(&rsp);
28✔
333
  return 0;
28✔
334
}
335

336
int32_t tqCheckColModifiable(STQ* pTq, int64_t tbUid, int32_t colId) {
191✔
337
  if (pTq == NULL) {
191!
338
    return TSDB_CODE_INVALID_PARA;
×
339
  }
340
  void* pIter = NULL;
191✔
341

342
  while (1) {
11✔
343
    pIter = taosHashIterate(pTq->pCheckInfo, pIter);
202✔
344
    if (pIter == NULL) {
202✔
345
      break;
156✔
346
    }
347

348
    STqCheckInfo* pCheck = (STqCheckInfo*)pIter;
46✔
349

350
    if (pCheck->ntbUid == tbUid) {
46!
351
      int32_t sz = taosArrayGetSize(pCheck->colIdList);
46✔
352
      for (int32_t i = 0; i < sz; i++) {
168✔
353
        int16_t* pForbidColId = taosArrayGet(pCheck->colIdList, i);
157✔
354
        if (pForbidColId == NULL) {
157!
355
          continue;
×
356
        }
357

358
        if ((*pForbidColId) == colId) {
157✔
359
          taosHashCancelIterate(pTq->pCheckInfo, pIter);
35✔
360
          return -1;
35✔
361
        }
362
      }
363
    }
364
  }
365

366
  return 0;
156✔
367
}
368

369
int32_t tqProcessPollPush(STQ* pTq) {
25,008✔
370
  if (pTq == NULL) {
25,008!
371
    return TSDB_CODE_INVALID_PARA;
×
372
  }
373
  int32_t vgId = TD_VID(pTq->pVnode);
25,008✔
374
  taosWLockLatch(&pTq->lock);
25,008✔
375
  if (taosHashGetSize(pTq->pPushMgr) > 0) {
25,008!
376
    void* pIter = taosHashIterate(pTq->pPushMgr, NULL);
25,008✔
377

378
    while (pIter) {
52,194✔
379
      STqHandle* pHandle = *(STqHandle**)pIter;
27,186✔
380
      tqDebug("vgId:%d start set submit for pHandle:%p, consumer:0x%" PRIx64, vgId, pHandle, pHandle->consumerId);
27,186✔
381

382
      if (pHandle->msg == NULL) {
27,186!
383
        tqError("pHandle->msg should not be null");
×
384
        taosHashCancelIterate(pTq->pPushMgr, pIter);
×
385
        break;
×
386
      } else {
387
        SRpcMsg msg = {.msgType = TDMT_VND_TMQ_CONSUME,
27,186✔
388
                       .pCont = pHandle->msg->pCont,
27,186✔
389
                       .contLen = pHandle->msg->contLen,
27,186✔
390
                       .info = pHandle->msg->info};
27,186✔
391
        if (tmsgPutToQueue(&pTq->pVnode->msgCb, QUERY_QUEUE, &msg) != 0){
27,186!
392
          tqError("vgId:%d tmsgPutToQueue failed, consumer:0x%" PRIx64, vgId, pHandle->consumerId);
×
393
        }
394
        taosMemoryFree(pHandle->msg);
27,186!
395
        pHandle->msg = NULL;
27,186✔
396
      }
397

398
      pIter = taosHashIterate(pTq->pPushMgr, pIter);
27,186✔
399
    }
400

401
    taosHashClear(pTq->pPushMgr);
25,008✔
402
  }
403
  taosWUnLockLatch(&pTq->lock);
25,008✔
404
  return 0;
25,008✔
405
}
406

407
int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) {
3,452,267✔
408
  if (pTq == NULL || pMsg == NULL) {
3,452,267!
409
    return TSDB_CODE_INVALID_PARA;
×
410
  }
411
  SMqPollReq req = {0};
3,452,880✔
412
  int        code = tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req);
3,452,880✔
413
  if (code < 0) {
3,451,036!
414
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
415
    terrno = TSDB_CODE_INVALID_MSG;
×
416
    goto END;
×
417
  }
418
  if (req.rawData == 1){
3,451,036✔
419
    req.uidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
19,258✔
420
    if (req.uidHash == NULL) {
21,352!
421
      tqError("tq poll rawData taosHashInit failed");
×
422
      code = terrno;
×
423
      goto END;
×
424
    }
425
  }
426
  int64_t      consumerId = req.consumerId;
3,453,130✔
427
  int32_t      reqEpoch = req.epoch;
3,453,130✔
428
  STqOffsetVal reqOffset = req.reqOffset;
3,453,130✔
429
  int32_t      vgId = TD_VID(pTq->pVnode);
3,453,130✔
430
  STqHandle*   pHandle = NULL;
3,453,130✔
431

432
  while (1) {
1✔
433
    taosWLockLatch(&pTq->lock);
3,453,131✔
434
    // 1. find handle
435
    code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,453,032✔
436
    if (code != TDB_CODE_SUCCESS) {
3,451,052✔
437
      tqError("tmq poll: consumer:0x%" PRIx64 " vgId:%d subkey %s not found", consumerId, vgId, req.subKey);
2,815!
438
      terrno = TSDB_CODE_INVALID_MSG;
2,815✔
439
      taosWUnLockLatch(&pTq->lock);
2,815✔
440
      return -1;
3,430✔
441
    }
442

443
    // 2. check rebalance status
444
    if (pHandle->consumerId != consumerId) {
3,448,237✔
445
      tqError("ERROR tmq poll: consumer:0x%" PRIx64
10!
446
              " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
447
              consumerId, TD_VID(pTq->pVnode), req.subKey, pHandle->consumerId);
448
      terrno = TSDB_CODE_TMQ_CONSUMER_MISMATCH;
10✔
449
      taosWUnLockLatch(&pTq->lock);
10✔
450
      code = -1;
10✔
451
      goto END;
10✔
452
    }
453

454
    bool exec = tqIsHandleExec(pHandle);
3,448,227✔
455
    if (!exec) {
3,448,227!
456
      tqSetHandleExec(pHandle);
3,449,834!
457
      //      qSetTaskCode(pHandle->execHandle.task, TDB_CODE_SUCCESS);
458
      tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle exec, pHandle:%p", consumerId, vgId,
3,449,834✔
459
              req.subKey, pHandle);
460
      taosWUnLockLatch(&pTq->lock);
3,450,636✔
461
      break;
3,450,753✔
462
    }
463
    taosWUnLockLatch(&pTq->lock);
×
464

465
    tqDebug("tmq poll: consumer:0x%" PRIx64
1!
466
            " vgId:%d, topic:%s, subscription is executing, wait for 10ms and retry, pHandle:%p",
467
            consumerId, vgId, req.subKey, pHandle);
468
    taosMsleep(10);
1✔
469
  }
470

471
  // 3. update the epoch value
472
  if (pHandle->epoch < reqEpoch) {
3,450,753✔
473
    tqDebug("tmq poll: consumer:0x%" PRIx64 " epoch update from %d to %d by poll req", consumerId, pHandle->epoch,
1,448✔
474
            reqEpoch);
475
    pHandle->epoch = reqEpoch;
1,448✔
476
  }
477

478
  char buf[TSDB_OFFSET_LEN] = {0};
3,450,753✔
479
  (void)tFormatOffset(buf, TSDB_OFFSET_LEN, &reqOffset);
3,450,753✔
480
  tqDebug("tmq poll: consumer:0x%" PRIx64 " (epoch %d), subkey %s, recv poll req vgId:%d, req:%s, QID:0x%" PRIx64,
3,450,748✔
481
          consumerId, req.epoch, pHandle->subKey, vgId, buf, req.reqId);
482

483
  code = tqExtractDataForMq(pTq, pHandle, &req, pMsg);
3,450,756✔
484
  tqSetHandleIdle(pHandle);
3,450,534✔
485

486
  tqDebug("tmq poll: consumer:0x%" PRIx64 " vgId:%d, topic:%s, set handle idle, pHandle:%p", consumerId, vgId,
3,450,534✔
487
          req.subKey, pHandle);
488

489
END:
312✔
490
  tDestroySMqPollReq(&req);
3,450,765✔
491
  return code;
3,450,736✔
492
}
493

494
int32_t tqProcessVgCommittedInfoReq(STQ* pTq, SRpcMsg* pMsg) {
5✔
495
  if (pTq == NULL || pMsg == NULL) {
5!
496
    return TSDB_CODE_INVALID_PARA;
×
497
  }
498
  void*   data = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
5✔
499
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
5✔
500

501
  SMqVgOffset vgOffset = {0};
5✔
502

503
  SDecoder decoder;
504
  tDecoderInit(&decoder, (uint8_t*)data, len);
5✔
505
  if (tDecodeMqVgOffset(&decoder, &vgOffset) < 0) {
5!
506
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
507
    return terrno;
×
508
  }
509

510
  tDecoderClear(&decoder);
5✔
511

512
  STqOffset* pSavedOffset = NULL;
5✔
513
  int32_t    code = tqMetaGetOffset(pTq, vgOffset.offset.subKey, &pSavedOffset);
5✔
514
  if (code != 0) {
5✔
515
    return TSDB_CODE_TMQ_NO_COMMITTED;
4✔
516
  }
517
  vgOffset.offset = *pSavedOffset;
1✔
518

519
  tEncodeSize(tEncodeMqVgOffset, &vgOffset, len, code);
1!
520
  if (code < 0) {
1!
521
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
522
  }
523

524
  void* buf = rpcMallocCont(len);
1✔
525
  if (buf == NULL) {
1!
526
    return terrno;
×
527
  }
528
  SEncoder encoder = {0};
1✔
529
  tEncoderInit(&encoder, buf, len);
1✔
530
  code = tEncodeMqVgOffset(&encoder, &vgOffset);
1✔
531
  tEncoderClear(&encoder);
1✔
532
  if (code < 0) {
1!
533
    rpcFreeCont(buf);
×
534
    return TAOS_GET_TERRNO(TSDB_CODE_INVALID_PARA);
×
535
  }
536

537
  SRpcMsg rsp = {.info = pMsg->info, .pCont = buf, .contLen = len, .code = 0};
1✔
538

539
  tmsgSendRsp(&rsp);
1✔
540
  return 0;
1✔
541
}
542

543
int32_t tqProcessVgWalInfoReq(STQ* pTq, SRpcMsg* pMsg) {
43✔
544
  if (pTq == NULL || pMsg == NULL) {
43!
545
    return TSDB_CODE_INVALID_PARA;
×
546
  }
547
  int32_t    code = 0;
43✔
548
  SMqPollReq req = {0};
43✔
549
  if (tDeserializeSMqPollReq(pMsg->pCont, pMsg->contLen, &req) < 0) {
43!
550
    tqError("tDeserializeSMqPollReq %d failed", pMsg->contLen);
×
551
    return TSDB_CODE_INVALID_MSG;
×
552
  }
553

554
  int64_t      consumerId = req.consumerId;
43✔
555
  STqOffsetVal reqOffset = req.reqOffset;
43✔
556
  int32_t      vgId = TD_VID(pTq->pVnode);
43✔
557

558
  // 1. find handle
559
  taosRLockLatch(&pTq->lock);
43✔
560
  STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey));
43✔
561
  if (pHandle == NULL) {
43!
562
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s not found", consumerId, vgId, req.subKey);
×
563
    taosRUnLockLatch(&pTq->lock);
×
564
    return TSDB_CODE_INVALID_MSG;
×
565
  }
566

567
  // 2. check rebalance status
568
  if (pHandle->consumerId != consumerId) {
43!
569
    tqDebug("ERROR consumer:0x%" PRIx64 " vgId:%d, subkey %s, mismatch for saved handle consumer:0x%" PRIx64,
×
570
            consumerId, vgId, req.subKey, pHandle->consumerId);
571
    taosRUnLockLatch(&pTq->lock);
×
572
    return TSDB_CODE_TMQ_CONSUMER_MISMATCH;
×
573
  }
574

575
  int64_t sver = 0, ever = 0;
43✔
576
  walReaderValidVersionRange(pHandle->execHandle.pTqReader->pWalReader, &sver, &ever);
43✔
577
  taosRUnLockLatch(&pTq->lock);
43✔
578

579
  SMqDataRsp dataRsp = {0};
43✔
580
  code = tqInitDataRsp(&dataRsp, req.reqOffset);
43✔
581
  if (code != 0) {
43!
582
    return code;
×
583
  }
584

585
  if (req.useSnapshot == true) {
43!
586
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s snapshot not support wal info", consumerId, vgId, req.subKey);
×
587
    code = TSDB_CODE_INVALID_PARA;
×
588
    goto END;
×
589
  }
590

591
  dataRsp.rspOffset.type = TMQ_OFFSET__LOG;
43✔
592

593
  if (reqOffset.type == TMQ_OFFSET__LOG) {
43✔
594
    dataRsp.rspOffset.version = reqOffset.version;
17✔
595
  } else if (reqOffset.type < 0) {
26!
596
    STqOffset* pOffset = NULL;
26✔
597
    code = tqMetaGetOffset(pTq, req.subKey, &pOffset);
26✔
598
    if (code == 0) {
26✔
599
      if (pOffset->val.type != TMQ_OFFSET__LOG) {
10!
600
        tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s, no valid wal info", consumerId, vgId, req.subKey);
×
601
        code = TSDB_CODE_INVALID_PARA;
×
602
        goto END;
×
603
      }
604

605
      dataRsp.rspOffset.version = pOffset->val.version;
10✔
606
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from store:%" PRId64, consumerId, vgId,
10!
607
             req.subKey, dataRsp.rspOffset.version);
608
    } else {
609
      if (reqOffset.type == TMQ_OFFSET__RESET_EARLIEST) {
16✔
610
        dataRsp.rspOffset.version = sver;  // not consume yet, set the earliest position
13✔
611
      } else if (reqOffset.type == TMQ_OFFSET__RESET_LATEST) {
3!
612
        dataRsp.rspOffset.version = ever;
3✔
613
      }
614
      tqInfo("consumer:0x%" PRIx64 " vgId:%d subkey:%s get assignment from init:%" PRId64, consumerId, vgId, req.subKey,
16!
615
             dataRsp.rspOffset.version);
616
    }
617
  } else {
618
    tqError("consumer:0x%" PRIx64 " vgId:%d subkey:%s invalid offset type:%d", consumerId, vgId, req.subKey,
×
619
            reqOffset.type);
620
    code = TSDB_CODE_INVALID_PARA;
×
621
    goto END;
×
622
  }
623

624
  code = tqDoSendDataRsp(&pMsg->info, &dataRsp, req.epoch, req.consumerId, TMQ_MSG_TYPE__WALINFO_RSP, sver, ever);
43✔
625

626
END:
43✔
627
  tDeleteMqDataRsp(&dataRsp);
43✔
628
  return code;
43✔
629
}
630

631
int32_t tqProcessDeleteSubReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
924✔
632
  if (pTq == NULL || msg == NULL) {
924!
633
    return TSDB_CODE_INVALID_PARA;
×
634
  }
635
  SMqVDeleteReq* pReq = (SMqVDeleteReq*)msg;
924✔
636
  int32_t        vgId = TD_VID(pTq->pVnode);
924✔
637

638
  tqInfo("vgId:%d, tq process delete sub req %s", vgId, pReq->subKey);
924!
639
  int32_t code = 0;
924✔
640

641
  STqHandle* pHandle = taosHashGet(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
924✔
642
  if (pHandle) {
926✔
643
    while (1) {
×
644
      taosWLockLatch(&pTq->lock);
923✔
645
      bool exec = tqIsHandleExec(pHandle);
923✔
646

647
      if (exec) {
923!
648
        tqInfo("vgId:%d, topic:%s, subscription is executing, delete wait for 10ms and retry, pHandle:%p", vgId,
×
649
               pHandle->subKey, pHandle);
650
        taosWUnLockLatch(&pTq->lock);
×
651
        taosMsleep(10);
×
652
        continue;
×
653
      }
654
      tqUnregisterPushHandle(pTq, pHandle);
923✔
655
      code = taosHashRemove(pTq->pHandle, pReq->subKey, strlen(pReq->subKey));
923✔
656
      if (code != 0) {
921!
657
        tqError("cannot process tq delete req %s, since no such handle", pReq->subKey);
×
658
      }
659
      taosWUnLockLatch(&pTq->lock);
921✔
660
      break;
923✔
661
    }
662
  }
663

664
  taosWLockLatch(&pTq->lock);
926✔
665
  if (taosHashRemove(pTq->pOffset, pReq->subKey, strlen(pReq->subKey)) != 0) {
926✔
666
    tqError("cannot process tq delete req %s, since no such offset in hash", pReq->subKey);
256!
667
  }
668
  if (tqMetaDeleteInfo(pTq, pTq->pOffsetStore, pReq->subKey, strlen(pReq->subKey)) != 0) {
926✔
669
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
253!
670
  }
671

672
  if (tqMetaDeleteInfo(pTq, pTq->pExecStore, pReq->subKey, strlen(pReq->subKey)) < 0) {
926!
673
    tqError("cannot process tq delete req %s, since no such offset in tdb", pReq->subKey);
×
674
  }
675
  taosWUnLockLatch(&pTq->lock);
926✔
676

677
  return 0;
926✔
678
}
679

680
int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
117✔
681
  if (pTq == NULL || msg == NULL) {
117!
682
    return TSDB_CODE_INVALID_PARA;
×
683
  }
684
  STqCheckInfo info = {0};
118✔
685
  int32_t      code = tqMetaDecodeCheckInfo(&info, msg, msgLen >= 0 ? msgLen : 0);
118✔
686
  if (code != 0) {
118!
687
    return code;
×
688
  }
689

690
  code = taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo));
118✔
691
  if (code != 0) {
118!
692
    tDeleteSTqCheckInfo(&info);
×
693
    return code;
×
694
  }
695

696
  return tqMetaSaveInfo(pTq, pTq->pCheckStore, info.topic, strlen(info.topic), msg, msgLen >= 0 ? msgLen : 0);
118✔
697
}
698

699
int32_t tqProcessDelCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
10✔
700
  if (pTq == NULL || msg == NULL) {
10!
701
    return TSDB_CODE_INVALID_PARA;
×
702
  }
703
  if (taosHashRemove(pTq->pCheckInfo, msg, strlen(msg)) < 0) {
10!
704
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
705
  }
706
  return tqMetaDeleteInfo(pTq, pTq->pCheckStore, msg, strlen(msg));
10✔
707
}
708

709
int32_t tqProcessSubscribeReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
3,181✔
710
  if (pTq == NULL || msg == NULL) {
3,181!
711
    return TSDB_CODE_INVALID_PARA;
×
712
  }
713
  int         ret = 0;
3,184✔
714
  SMqRebVgReq req = {0};
3,184✔
715
  SDecoder    dc = {0};
3,184✔
716

717
  tDecoderInit(&dc, (uint8_t*)msg, msgLen);
3,184✔
718
  ret = tDecodeSMqRebVgReq(&dc, &req);
3,181✔
719
  if (ret < 0) {
3,182!
720
    goto end;
×
721
  }
722

723
  tqInfo("vgId:%d, tq process sub req:%s, Id:0x%" PRIx64 " -> Id:0x%" PRIx64, pTq->pVnode->config.vgId, req.subKey,
3,182!
724
         req.oldConsumerId, req.newConsumerId);
725

726
  taosRLockLatch(&pTq->lock);
3,188✔
727
  STqHandle* pHandle = NULL;
3,189✔
728
  int32_t code = tqMetaGetHandle(pTq, req.subKey, &pHandle);
3,189✔
729
  if (code != 0){
3,187✔
730
    tqInfo("vgId:%d, tq process sub req:%s, no such handle, create new one", pTq->pVnode->config.vgId, req.subKey);
1,456!
731
  }
732
  taosRUnLockLatch(&pTq->lock);
3,188✔
733
  if (pHandle == NULL) {
3,189✔
734
    if (req.oldConsumerId != -1) {
1,458✔
735
      tqError("vgId:%d, build new consumer handle %s for consumer:0x%" PRIx64 ", but old consumerId:0x%" PRIx64,
1!
736
              req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId);
737
    }
738
    if (req.newConsumerId == -1) {
1,458✔
739
      tqError("vgId:%d, tq invalid rebalance request, new consumerId %" PRId64 "", req.vgId, req.newConsumerId);
1!
740
      ret = TSDB_CODE_INVALID_PARA;
1✔
741
      goto end;
1✔
742
    }
743
    STqHandle handle = {0};
1,457✔
744
    ret = tqMetaCreateHandle(pTq, &req, &handle);
1,457✔
745
    if (ret < 0) {
1,457!
746
      tqDestroyTqHandle(&handle);
×
747
      goto end;
×
748
    }
749
    taosWLockLatch(&pTq->lock);
1,457✔
750
    ret = tqMetaSaveHandle(pTq, req.subKey, &handle);
1,457✔
751
    taosWUnLockLatch(&pTq->lock);
1,457✔
752
  } else {
753
    while (1) {
×
754
      taosWLockLatch(&pTq->lock);
1,731✔
755
      bool exec = tqIsHandleExec(pHandle);
1,730!
756
      if (exec) {
1,730!
757
        tqInfo("vgId:%d, topic:%s, subscription is executing, sub wait for 10ms and retry, pHandle:%p",
×
758
               pTq->pVnode->config.vgId, pHandle->subKey, pHandle);
759
        taosWUnLockLatch(&pTq->lock);
×
760
        taosMsleep(10);
×
761
        continue;
×
762
      }
763
      if (pHandle->consumerId == req.newConsumerId) {  // do nothing
1,730✔
764
        tqInfo("vgId:%d no switch consumer:0x%" PRIx64 " remains, because redo wal log", req.vgId, req.newConsumerId);
104!
765
      } else {
766
        tqInfo("vgId:%d switch consumer from Id:0x%" PRIx64 " to Id:0x%" PRIx64, req.vgId, pHandle->consumerId,
1,626!
767
               req.newConsumerId);
768

769
        atomic_store_64(&pHandle->consumerId, req.newConsumerId);
1,627✔
770
        atomic_store_32(&pHandle->epoch, 0);
1,627✔
771
        tqUnregisterPushHandle(pTq, pHandle);
1,627✔
772
        ret = tqMetaSaveHandle(pTq, req.subKey, pHandle);
1,627✔
773
      }
774
      taosWUnLockLatch(&pTq->lock);
1,731✔
775
      break;
1,731✔
776
    }
777
  }
778

779
end:
3,189✔
780
  tDecoderClear(&dc);
3,189✔
781
  return ret;
3,189✔
782
}
783

784
static void freePtr(void* ptr) { taosMemoryFree(*(void**)ptr); }
31,476!
785

786
int32_t tqBuildStreamTask(void* pTqObj, SStreamTask* pTask, int64_t nextProcessVer) {
14,443✔
787
  STQ*             pTq = (STQ*)pTqObj;
14,443✔
788
  int32_t          vgId = TD_VID(pTq->pVnode);
14,443✔
789
  SCheckpointInfo* pChkInfo = NULL;
14,443✔
790

791
  tqDebug("s-task:0x%x start to build task", pTask->id.taskId);
14,443✔
792

793
  int32_t code = streamTaskInit(pTask, pTq->pStreamMeta, &pTq->pVnode->msgCb, nextProcessVer);
14,443✔
794
  if (code != TSDB_CODE_SUCCESS) {
14,440!
795
    return code;
×
796
  }
797

798
  pTask->pBackend = NULL;
14,440✔
799

800
  // sink
801
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,440✔
802
  if (pOutputInfo->type == TASK_OUTPUT__SMA) {
14,440!
803
    pOutputInfo->smaSink.vnode = pTq->pVnode;
×
804
    pOutputInfo->smaSink.smaSink = smaHandleRes;
×
805
  } else if (pOutputInfo->type == TASK_OUTPUT__TABLE) {
14,440✔
806
    pOutputInfo->tbSink.vnode = pTq->pVnode;
7,221✔
807
    pOutputInfo->tbSink.tbSinkFunc = tqSinkDataIntoDstTable;
7,221✔
808

809
    int32_t   ver1 = 1;
7,221✔
810
    SMetaInfo info = {0};
7,221✔
811
    code = metaGetInfo(pTq->pVnode->pMeta, pOutputInfo->tbSink.stbUid, &info, NULL);
7,221✔
812
    if (code == TSDB_CODE_SUCCESS) {
7,219✔
813
      ver1 = info.skmVer;
6,755✔
814
    }
815

816
    SSchemaWrapper* pschemaWrapper = pOutputInfo->tbSink.pSchemaWrapper;
7,219✔
817
    pOutputInfo->tbSink.pTSchema = tBuildTSchema(pschemaWrapper->pSchema, pschemaWrapper->nCols, ver1);
7,219✔
818
    if (pOutputInfo->tbSink.pTSchema == NULL) {
7,228!
819
      return terrno;
×
820
    }
821

822
    pOutputInfo->tbSink.pTbInfo = tSimpleHashInit(10240, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
7,228✔
823
    if (pOutputInfo->tbSink.pTbInfo == NULL) {
7,227!
824
      tqError("vgId:%d failed init sink tableInfo, code:%s", vgId, tstrerror(terrno));
×
825
      return terrno;
×
826
    }
827

828
    tSimpleHashSetFreeFp(pOutputInfo->tbSink.pTbInfo, freePtr);
7,227✔
829
  }
830

831
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,445✔
832
    bool scanDropCtb = pTask->subtableWithoutMd5 ? true : false;
7,319✔
833
    SWalFilterCond cond = {.deleteMsg = 1, .scanDropCtb = scanDropCtb};  // delete msg also extract from wal files
7,319✔
834
    pTask->exec.pWalReader = walOpenReader(pTq->pVnode->pWal, &cond, pTask->id.taskId);
7,319✔
835
    if (pTask->exec.pWalReader == NULL) {
7,320✔
836
      tqError("vgId:%d failed init wal reader, code:%s", vgId, tstrerror(terrno));
1!
837
      return terrno;
1✔
838
    }
839
  }
840

841
  streamTaskResetUpstreamStageInfo(pTask);
14,445✔
842

843
  pChkInfo = &pTask->chkInfo;
14,451✔
844
  tqSetRestoreVersionInfo(pTask);
14,451✔
845

846
  char*       p = streamTaskGetStatus(pTask).name;
14,449✔
847
  const char* pNext = streamTaskGetStatusStr(pTask->status.taskStatus);
14,453✔
848

849
  if (pTask->info.fillHistory) {
14,450✔
850
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
4,833!
851
           " nextProcessVer:%" PRId64
852
           " child id:%d, level:%d, cur-status:%s, next-status:%s taskType:%d, related stream 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->streamTaskId.taskId, pTask->info.delaySchedParam, nextProcessVer);
857
  } else {
858
    tqInfo("vgId:%d build stream task, s-task:%s, %p checkpointId:%" PRId64 " checkpointVer:%" PRId64
9,617!
859
           " nextProcessVer:%" PRId64
860
           " child id:%d, level:%d, cur-status:%s next-status:%s taskType:%d, related helper-task:0x%x "
861
           "delaySched:%" PRId64 " ms, inputVer:%" PRId64,
862
           vgId, pTask->id.idStr, pTask, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
863
           pTask->info.selfChildId, pTask->info.taskLevel, p, pNext, pTask->info.fillHistory,
864
           (int32_t)pTask->hTaskInfo.id.taskId, pTask->info.delaySchedParam, nextProcessVer);
865

866
    if (pChkInfo->checkpointVer > pChkInfo->nextProcessVer) {
9,627!
867
      tqError("vgId:%d build stream task, s-task:%s, checkpointVer:%" PRId64 " > nextProcessVer:%" PRId64, vgId,
×
868
              pTask->id.idStr, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
869
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
870
    }
871
  }
872

873
  return 0;
14,460✔
874
}
875

876
int32_t tqProcessTaskCheckReq(STQ* pTq, SRpcMsg* pMsg) { return tqStreamTaskProcessCheckReq(pTq->pStreamMeta, pMsg); }
22,324✔
877

878
int32_t tqProcessTaskCheckRsp(STQ* pTq, SRpcMsg* pMsg) {
22,355✔
879
  return tqStreamTaskProcessCheckRsp(pTq->pStreamMeta, pMsg, vnodeIsRoleLeader(pTq->pVnode));
22,355✔
880
}
881

882
int32_t tqProcessTaskDeployReq(STQ* pTq, int64_t sversion, char* msg, int32_t msgLen) {
13,993✔
883
  return tqStreamTaskProcessDeployReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, sversion, msg, msgLen,
14,013✔
884
                                      vnodeIsRoleLeader(pTq->pVnode), pTq->pVnode->restored);
13,993✔
885
}
886

887
static void doStartFillhistoryStep2(SStreamTask* pTask, SStreamTask* pStreamTask, STQ* pTq) {
2,347✔
888
  const char*    id = pTask->id.idStr;
2,347✔
889
  int64_t        nextProcessedVer = pStreamTask->hTaskInfo.haltVer;
2,347✔
890
  SVersionRange* pStep2Range = &pTask->step2Range;
2,347✔
891
  int32_t        vgId = pTask->pMeta->vgId;
2,347✔
892

893
  // if it's an source task, extract the last version in wal.
894
  bool done = streamHistoryTaskSetVerRangeStep2(pTask, nextProcessedVer);
2,347✔
895
  pTask->execInfo.step2Start = taosGetTimestampMs();
2,347✔
896

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

915
    int32_t code = streamSetParamForStreamScannerStep2(pTask, pStep2Range, pWindow);
1,203✔
916
    if (code) {
1,203!
917
      tqError("s-task:%s level:%d failed to set step2 param", id, pTask->info.taskLevel);
×
918
    }
919

920
    int64_t dstVer = pStep2Range->minVer;
1,203✔
921
    pTask->chkInfo.nextProcessVer = dstVer;
1,203✔
922

923
    walReaderSetSkipToVersion(pTask->exec.pWalReader, dstVer);
1,203✔
924
    tqDebug("s-task:%s wal reader start scan WAL verRange:%" PRId64 "-%" PRId64 ", set sched-status:%d", id, dstVer,
1,203✔
925
            pStep2Range->maxVer, TASK_SCHED_STATUS__INACTIVE);
926

927
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
1,203✔
928

929
    // now the fill-history task starts to scan data from wal files.
930
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
1,203✔
931
  }
932
}
2,347✔
933

934
int32_t handleStep2Async(SStreamTask* pStreamTask, void* param) {
2,347✔
935
  STQ* pTq = param;
2,347✔
936

937
  SStreamMeta* pMeta = pStreamTask->pMeta;
2,347✔
938
  STaskId      hId = pStreamTask->hTaskInfo.id;
2,347✔
939
  SStreamTask* pTask = NULL;
2,347✔
940
  int32_t      code = streamMetaAcquireTask(pStreamTask->pMeta, hId.streamId, hId.taskId, &pTask);
2,347✔
941
  if (pTask == NULL) {
2,347!
942
    tqWarn("s-task:0x%x failed to acquired it to exec step 2, scan wal quit", (int32_t)hId.taskId);
×
943
    return TSDB_CODE_SUCCESS;
×
944
  }
945

946
  doStartFillhistoryStep2(pTask, pStreamTask, pTq);
2,347✔
947

948
  streamMetaReleaseTask(pMeta, pTask);
2,347✔
949
  return TSDB_CODE_SUCCESS;
2,347✔
950
}
951

952
// this function should be executed by only one thread, so we set a sentinel to protect this function
953
int32_t tqProcessTaskScanHistory(STQ* pTq, SRpcMsg* pMsg) {
2,533✔
954
  SStreamScanHistoryReq* pReq = (SStreamScanHistoryReq*)pMsg->pCont;
2,533✔
955
  SStreamMeta*           pMeta = pTq->pStreamMeta;
2,533✔
956
  int32_t                code = TSDB_CODE_SUCCESS;
2,533✔
957
  SStreamTask*           pTask = NULL;
2,533✔
958
  SStreamTask*           pStreamTask = NULL;
2,533✔
959
  char*                  pStatus = NULL;
2,533✔
960
  int32_t                taskType = 0;
2,533✔
961

962
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
2,533✔
963
  if (pTask == NULL) {
2,533✔
964
    tqError("vgId:%d failed to acquire stream task:0x%x during scan history data, task may have been destroyed",
1!
965
            pMeta->vgId, pReq->taskId);
966
    return code;
1✔
967
  }
968

969
  // do recovery step1
970
  const char* id = pTask->id.idStr;
2,532✔
971
  streamMutexLock(&pTask->lock);
2,532✔
972

973
  SStreamTaskState s = streamTaskGetStatus(pTask);
2,532✔
974
  pStatus = s.name;
2,532✔
975
  taskType = pTask->info.fillHistory;
2,532✔
976

977
  if ((s.state != TASK_STATUS__SCAN_HISTORY && taskType == STREAM_HISTORY_TASK) ||
2,532!
978
      (s.state != TASK_STATUS__READY && taskType == STREAM_RECALCUL_TASK) ||
2,532!
979
      (pTask->status.downstreamReady == 0)) {
2,532!
980
    tqError("s-task:%s vgId:%d status:%s downstreamReady:%d not allowed/ready for scan-history data, quit", id,
×
981
            pMeta->vgId, s.name, pTask->status.downstreamReady);
982

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

988
  if (pTask->exec.pExecutor == NULL) {
2,532!
989
    tqError("s-task:%s vgId:%d executor is null, not executor scan history", id, pMeta->vgId);
×
990

991
    streamMutexUnlock(&pTask->lock);
×
992
    streamMetaReleaseTask(pMeta, pTask);
×
993
    return 0;
×
994
  }
995

996
  streamMutexUnlock(&pTask->lock);
2,532✔
997

998
  // avoid multi-thread exec
999
  while (1) {
×
1000
    int32_t sentinel = atomic_val_compare_exchange_32(&pTask->status.inScanHistorySentinel, 0, 1);
2,532✔
1001
    if (sentinel != 0) {
2,532!
1002
      tqDebug("s-task:%s already in scan-history func, wait for 100ms, and try again", id);
×
1003
      taosMsleep(100);
×
1004
    } else {
1005
      break;
2,532✔
1006
    }
1007
  }
1008

1009
  // let's decide which step should be executed now
1010
  if (pTask->execInfo.step1Start == 0) {
2,532✔
1011
    int64_t ts = taosGetTimestampMs();
2,349✔
1012
    pTask->execInfo.step1Start = ts;
2,349✔
1013
    tqDebug("s-task:%s start scan-history stage(step 1), status:%s, step1 startTs:%" PRId64, id, pStatus, ts);
2,349✔
1014
  } else {
1015
    if (pTask->execInfo.step2Start == 0) {
183✔
1016
      tqDebug("s-task:%s continue exec scan-history(step1), original step1 startTs:%" PRId64 ", already elapsed:%.2fs",
151!
1017
              id, pTask->execInfo.step1Start, pTask->execInfo.step1El);
1018
    } else {
1019
      tqDebug("s-task:%s already in step2, no need to scan-history data, step2 startTs:%" PRId64, id,
32!
1020
              pTask->execInfo.step2Start);
1021

1022
      atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
32✔
1023
      streamMetaReleaseTask(pMeta, pTask);
32✔
1024
      return 0;
32✔
1025
    }
1026
  }
1027

1028
  // we have to continue retrying to successfully execute the scan history task.
1029
  if (!streamTaskSetSchedStatusWait(pTask)) {
2,500!
1030
    tqError(
×
1031
        "s-task:%s failed to start scan-history in first stream time window since already started, unexpected "
1032
        "sched-status:%d",
1033
        id, pTask->status.schedStatus);
1034
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
×
1035
    streamMetaReleaseTask(pMeta, pTask);
×
1036
    return 0;
×
1037
  }
1038

1039
  int64_t              st = taosGetTimestampMs();
2,500✔
1040
  SScanhistoryDataInfo retInfo = streamScanHistoryData(pTask, st);
2,500✔
1041

1042
  double el = (taosGetTimestampMs() - st) / 1000.0;
2,500✔
1043
  pTask->execInfo.step1El += el;
2,500✔
1044

1045
  if (retInfo.ret == TASK_SCANHISTORY_QUIT || retInfo.ret == TASK_SCANHISTORY_REXEC) {
2,500✔
1046
    int8_t status = streamTaskSetSchedStatusInactive(pTask);
153✔
1047
    atomic_store_32(&pTask->status.inScanHistorySentinel, 0);
153✔
1048

1049
    if (retInfo.ret == TASK_SCANHISTORY_REXEC) {
153✔
1050
      streamExecScanHistoryInFuture(pTask, retInfo.idleTime);
151✔
1051
    } else {
1052
      SStreamTaskState p = streamTaskGetStatus(pTask);
2✔
1053
      ETaskStatus      localStatus = p.state;
2✔
1054

1055
      if (localStatus == TASK_STATUS__PAUSE) {
2!
1056
        tqDebug("s-task:%s is paused in the step1, elapsed time:%.2fs total:%.2fs, sched-status:%d", id, el,
×
1057
                pTask->execInfo.step1El, status);
1058
      } else if (localStatus == TASK_STATUS__STOP || localStatus == TASK_STATUS__DROPPING) {
2!
1059
        tqDebug("s-task:%s status:%p not continue scan-history data, total elapsed time:%.2fs quit", id, p.name,
2!
1060
                pTask->execInfo.step1El);
1061
      }
1062
    }
1063

1064
    streamMetaReleaseTask(pMeta, pTask);
153✔
1065
    return 0;
153✔
1066
  }
1067

1068
  // the following procedure should be executed, no matter status is stop/pause or not
1069
  if (taskType == STREAM_HISTORY_TASK) {
2,347!
1070
    tqDebug("s-task:%s scan-history(step 1) ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
2,347✔
1071
  } else if (taskType == STREAM_RECALCUL_TASK) {
×
1072
    tqDebug("s-task:%s recalculate ended, elapsed time:%.2fs", id, pTask->execInfo.step1El);
×
1073
  } else {
1074
    tqError("s-task:%s fill-history is disabled, unexpected", id);
×
1075
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1076
  }
1077

1078
  // 1. get the related stream task
1079
  code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
2,347✔
1080
  if (pStreamTask == NULL) {
2,347!
1081
    tqError("failed to find s-task:0x%" PRIx64 ", it may have been destroyed, drop related fill-history task:%s",
×
1082
            pTask->streamTaskId.taskId, pTask->id.idStr);
1083

1084
    tqDebug("s-task:%s fill-history task set status to be dropping", id);
×
1085
    code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
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) {
2,347!
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) {
2,347!
1098
    code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, handleStep2Async, pTq);
2,347✔
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);
2,347✔
1109

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

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

1121
  SStreamTaskRunReq req = {0};
351,140✔
1122
  tDecoderInit(&decoder, (uint8_t*)msg, len);
351,140✔
1123
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
350,877!
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);
350,800✔
1130

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

1140
    return code;
98,192✔
1141
  }
1142
}
1143

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

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

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

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

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

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

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

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

1176
int32_t tqProcessTaskRetrieveRsp(STQ* pTq, SRpcMsg* pMsg) { return 0; }
406✔
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,
15✔
1223
                                 int32_t taskId) {
1224
  SRpcMsg rsp = {0};
15✔
1225
  int32_t ret = streamTaskBuildCheckpointSourceRsp(pReq, pRpcInfo, &rsp, code);
15✔
1226
  if (ret) {  // suppress the error in build checkpoint source rsp
15!
1227
    tqError("s-task:0x%x failed to build checkpoint-source rsp, code:%s", taskId, tstrerror(ret));
×
1228
  }
1229
  tmsgSendRsp(&rsp);  // error occurs
15✔
1230
}
15✔
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) {
3,117✔
1234
  int32_t                    vgId = TD_VID(pTq->pVnode);
3,117✔
1235
  SStreamMeta*               pMeta = pTq->pStreamMeta;
3,117✔
1236
  char*                      msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3,117✔
1237
  int32_t                    len = pMsg->contLen - sizeof(SMsgHead);
3,117✔
1238
  int32_t                    code = 0;
3,117✔
1239
  SStreamCheckpointSourceReq req = {0};
3,117✔
1240
  SDecoder                   decoder = {0};
3,117✔
1241
  SStreamTask*               pTask = NULL;
3,117✔
1242
  int64_t                    checkpointId = 0;
3,117✔
1243

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

1247
  tDecoderInit(&decoder, (uint8_t*)msg, len);
3,117✔
1248
  if (tDecodeStreamCheckpointSourceReq(&decoder, &req) < 0) {
3,118!
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);
3,117✔
1257

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

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

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

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

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

1293
  // todo save the checkpoint failed info
1294
  streamMutexLock(&pTask->lock);
3,104✔
1295
  ETaskStatus status = streamTaskGetStatus(pTask).state;
3,109✔
1296

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

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

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

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

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

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

1339
  code = streamProcessCheckpointSourceReq(pTask, &req);
3,104✔
1340
  streamMutexUnlock(&pTask->lock);
3,105✔
1341

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

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

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

1366
  streamMetaReleaseTask(pMeta, pTask);
3,111✔
1367
  return TSDB_CODE_SUCCESS;
3,109✔
1368
}
1369

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

1374
  SStreamCheckpointReadyMsg* pReq = (SStreamCheckpointReadyMsg*)pMsg->pCont;
7,712✔
1375
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
7,712!
1376
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from 0x%x", vgId,
×
1377
            (int32_t)pReq->downstreamTaskId);
1378
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1379
  }
1380

1381
  return tqStreamTaskProcessCheckpointReadyMsg(pTq->pStreamMeta, pMsg);
7,712✔
1382
}
1383

1384
int32_t tqProcessTaskUpdateReq(STQ* pTq, SRpcMsg* pMsg) {
40✔
1385
  return tqStreamTaskProcessUpdateReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg,
80✔
1386
                                      pTq->pVnode->restored, (pTq->pStreamMeta->role == NODE_ROLE_LEADER));
40✔
1387
}
1388

1389
int32_t tqProcessTaskResetReq(STQ* pTq, SRpcMsg* pMsg) {
×
1390
  return tqStreamTaskProcessTaskResetReq(pTq->pStreamMeta, pMsg->pCont);
×
1391
}
1392

1393
int32_t tqProcessAllTaskStopReq(STQ* pTq, SRpcMsg* pMsg) {
4,145✔
1394
  return tqStreamTaskProcessAllTaskStopReq(pTq->pStreamMeta, &pTq->pVnode->msgCb, pMsg);
4,145✔
1395
}
1396

1397
int32_t tqProcessTaskRetrieveTriggerReq(STQ* pTq, SRpcMsg* pMsg) {
×
1398
  int32_t vgId = TD_VID(pTq->pVnode);
×
1399

1400
  if (!vnodeIsRoleLeader(pTq->pVnode)) {
×
1401
    SRetrieveChkptTriggerReq req = {0};
×
1402

1403
    char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1404
    int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1405
    SDecoder decoder = {0};
×
1406

1407
    tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1408
    if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1409
      tDecoderClear(&decoder);
×
1410
      tqError("vgId:%d invalid retrieve checkpoint-trigger req received", vgId);
×
1411
      return TSDB_CODE_INVALID_MSG;
×
1412
    }
1413
    tDecoderClear(&decoder);
×
1414

1415
    tqError("vgId:%d not leader, ignore the retrieve checkpoint-trigger msg from s-task:0x%" PRId64, vgId,
×
1416
            req.downstreamTaskId);
1417
    return TSDB_CODE_STREAM_NOT_LEADER;
×
1418
  }
1419

1420
  return tqStreamTaskProcessRetrieveTriggerReq(pTq->pStreamMeta, pMsg);
×
1421
}
1422

1423
int32_t tqProcessTaskRetrieveTriggerRsp(STQ* pTq, SRpcMsg* pMsg) {
×
1424
  return tqStreamTaskProcessRetrieveTriggerRsp(pTq->pStreamMeta, pMsg);
×
1425
}
1426

1427
// this function is needed, do not try to remove it.
1428
int32_t tqProcessStreamHbRsp(STQ* pTq, SRpcMsg* pMsg) { return tqStreamProcessStreamHbRsp(pTq->pStreamMeta, pMsg); }
24,176✔
1429

1430
int32_t tqProcessStreamReqCheckpointRsp(STQ* pTq, SRpcMsg* pMsg) {
4,357✔
1431
  return tqStreamProcessReqCheckpointRsp(pTq->pStreamMeta, pMsg);
4,357✔
1432
}
1433

1434
int32_t tqProcessTaskCheckpointReadyRsp(STQ* pTq, SRpcMsg* pMsg) {
7,712✔
1435
  return tqStreamProcessCheckpointReadyRsp(pTq->pStreamMeta, pMsg);
7,712✔
1436
}
1437

1438
int32_t tqProcessTaskChkptReportRsp(STQ* pTq, SRpcMsg* pMsg) {
5,928✔
1439
  return tqStreamProcessChkptReportRsp(pTq->pStreamMeta, pMsg);
5,928✔
1440
}
1441

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