• 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

59.79
/source/dnode/vnode/src/tqCommon/tqCommon.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 "tmsgcb.h"
17
#include "tq.h"
18
#include "tstream.h"
19

20
typedef struct SMStreamCheckpointReadyRspMsg {
21
  SMsgHead head;
22
  int64_t  streamId;
23
  int32_t  upstreamTaskId;
24
  int32_t  upstreamNodeId;
25
  int32_t  downstreamTaskId;
26
  int32_t  downstreamNodeId;
27
  int64_t  checkpointId;
28
  int32_t  transId;
29
} SMStreamCheckpointReadyRspMsg;
30

31
static int32_t doProcessDummyRspMsg(SStreamMeta* pMeta, SRpcMsg* pMsg);
32

33
int32_t tqExpandStreamTask(SStreamTask* pTask) {
14,224✔
34
  SStreamMeta* pMeta = pTask->pMeta;
14,224✔
35
  int32_t      vgId = pMeta->vgId;
14,224✔
36
  int64_t      st = taosGetTimestampMs();
14,225✔
37
  int64_t      streamId = 0;
14,225✔
38
  int32_t      taskId = 0;
14,225✔
39
  int32_t      code = 0;
14,225✔
40

41
  tqDebug("s-task:%s vgId:%d start to expand stream task", pTask->id.idStr, vgId);
14,225✔
42

43
  if (pTask->info.fillHistory != STREAM_NORMAL_TASK) {
14,225✔
44
    streamId = pTask->streamTaskId.streamId;
4,816✔
45
    taskId = pTask->streamTaskId.taskId;
4,816✔
46
  } else {
47
    streamId = pTask->id.streamId;
9,409✔
48
    taskId = pTask->id.taskId;
9,409✔
49
  }
50

51
  // sink task does not need the pState
52
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
14,225✔
53
    if (pTask->info.fillHistory == STREAM_RECALCUL_TASK) {
7,434✔
54
      pTask->pRecalState = streamStateRecalatedOpen(pMeta->path, pTask, pTask->id.streamId, pTask->id.taskId);
27✔
55
      if (pTask->pRecalState == NULL) {
27!
56
        tqError("s-task:%s (vgId:%d) failed to open state for task, expand task failed", pTask->id.idStr, vgId);
×
57
        return terrno;
×
58
      } else {
59
        tqDebug("s-task:%s recal state:%p", pTask->id.idStr, pTask->pRecalState);
27!
60
      }
61
    }
62

63
    pTask->pState = streamStateOpen(pMeta->path, pTask, streamId, taskId);
7,434✔
64
    if (pTask->pState == NULL) {
7,434!
65
      tqError("s-task:%s (vgId:%d) failed to open state for task, expand task failed", pTask->id.idStr, vgId);
×
66
      return terrno;
×
67
    } else {
68
      tqDebug("s-task:%s stream state:%p", pTask->id.idStr, pTask->pState);
7,434✔
69
    }
70
  }
71

72
  SReadHandle handle = {
14,225✔
73
      .checkpointId = pTask->chkInfo.checkpointId,
14,225✔
74
      .pStateBackend = NULL,
75
      .fillHistory = pTask->info.fillHistory,
14,225✔
76
      .winRange = pTask->dataRange.window,
77
      .pOtherBackend = NULL,
78
  };
79

80
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,225✔
81
    handle.vnode = ((STQ*)pMeta->ahandle)->pVnode;
7,148✔
82
    handle.initTqReader = 1;
7,148✔
83
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
7,077✔
84
    handle.numOfVgroups = (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList);
286✔
85
  }
86

87
  initStorageAPI(&handle.api);
14,225✔
88

89
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE || pTask->info.taskLevel == TASK_LEVEL__AGG) {
14,224✔
90
    if (pTask->info.fillHistory == STREAM_RECALCUL_TASK) {
7,434✔
91
      handle.pStateBackend = pTask->pRecalState;
27✔
92
      handle.pOtherBackend = pTask->pState;
27✔
93
    } else {
94
      handle.pStateBackend = pTask->pState;
7,407✔
95
      handle.pOtherBackend = NULL;
7,407✔
96
    }
97

98
    code = qCreateStreamExecTaskInfo(&pTask->exec.pExecutor, pTask->exec.qmsg, &handle, vgId, pTask->id.taskId);
7,434✔
99
    if (code) {
7,434!
100
      tqError("s-task:%s failed to expand task, code:%s", pTask->id.idStr, tstrerror(code));
×
101
      return code;
×
102
    }
103

104
    code = qSetTaskId(pTask->exec.pExecutor, pTask->id.taskId, pTask->id.streamId);
7,434✔
105
    if (code) {
7,434!
106
      return code;
×
107
    }
108

109
    code = qSetStreamNotifyInfo(pTask->exec.pExecutor, pTask->notifyInfo.notifyEventTypes,
14,868✔
110
                                pTask->notifyInfo.pSchemaWrapper, pTask->notifyInfo.stbFullName,
7,434✔
111
                                IS_NEW_SUBTB_RULE(pTask), &pTask->notifyEventStat);
7,434!
112
    if (code) {
7,434!
113
      tqError("s-task:%s failed to set stream notify info, code:%s", pTask->id.idStr, tstrerror(code));
×
114
      return code;
×
115
    }
116
  }
117

118
  streamSetupScheduleTrigger(pTask);
14,224✔
119

120
  double el = (taosGetTimestampMs() - st) / 1000.0;
14,225✔
121
  tqDebug("s-task:%s vgId:%d expand stream task completed, elapsed time:%.2fsec", pTask->id.idStr, vgId, el);
14,225✔
122

123
  return code;
14,225✔
124
}
125

126
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
14,551✔
127
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,551✔
128

129
  // checkpoint ver is the kept version, handled data should be the next version.
130
  if (pChkInfo->checkpointId != 0) {
14,551✔
131
    pChkInfo->nextProcessVer = pChkInfo->checkpointVer + 1;
269✔
132
    pChkInfo->processedVer = pChkInfo->checkpointVer;
269✔
133
    pTask->execInfo.startCheckpointId = pChkInfo->checkpointId;
269✔
134

135
    tqInfo("s-task:%s restore from the checkpointId:%" PRId64 " ver:%" PRId64 " currentVer:%" PRId64, pTask->id.idStr,
269!
136
           pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
137
  }
138

139
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
14,551✔
140
}
14,551✔
141

142
int32_t tqStreamTaskStartAsync(SStreamMeta* pMeta, SMsgCb* cb, bool restart) {
19✔
143
  int32_t vgId = pMeta->vgId;
19✔
144
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
19✔
145
  if (numOfTasks == 0) {
19!
146
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
147
    return 0;
×
148
  }
149

150
  tqDebug("vgId:%d start all %d stream task(s) async", vgId, numOfTasks);
19!
151

152
  int32_t type = restart ? STREAM_EXEC_T_RESTART_ALL_TASKS : STREAM_EXEC_T_START_ALL_TASKS;
19!
153
  return streamTaskSchedTask(cb, vgId, 0, 0, type, false);
19✔
154
}
155

156
int32_t tqStreamStartOneTaskAsync(SStreamMeta* pMeta, SMsgCb* cb, int64_t streamId, int32_t taskId) {
9,403✔
157
  int32_t vgId = pMeta->vgId;
9,403✔
158
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
9,403✔
159
  if (numOfTasks == 0) {
9,403!
160
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
161
    return 0;
×
162
  }
163

164
  tqDebug("vgId:%d start task:0x%x async", vgId, taskId);
9,403✔
165
  return streamTaskSchedTask(cb, vgId, streamId, taskId, STREAM_EXEC_T_START_ONE_TASK, false);
9,403✔
166
}
167

168
// this is to process request from transaction, always return true.
169
int32_t tqStreamTaskProcessUpdateReq(SStreamMeta* pMeta, SMsgCb* cb, SRpcMsg* pMsg, bool restored, bool isLeader) {
42✔
170
  int32_t      vgId = pMeta->vgId;
42✔
171
  char*        msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
42✔
172
  int32_t      len = pMsg->contLen - sizeof(SMsgHead);
42✔
173
  SRpcMsg      rsp = {.info = pMsg->info, .code = TSDB_CODE_SUCCESS};
42✔
174
  int64_t      st = taosGetTimestampMs();
42✔
175
  bool         updated = false;
42✔
176
  int32_t      code = 0;
42✔
177
  SStreamTask* pTask = NULL;
42✔
178
  SStreamTask* pHTask = NULL;
42✔
179

180
  SStreamTaskNodeUpdateMsg req = {0};
42✔
181

182
  SDecoder decoder;
183
  tDecoderInit(&decoder, (uint8_t*)msg, len);
42✔
184
  if (tDecodeStreamTaskUpdateMsg(&decoder, &req) < 0) {
42!
185
    rsp.code = TSDB_CODE_MSG_DECODE_ERROR;
×
186
    tqError("vgId:%d failed to decode task update msg, code:%s", vgId, tstrerror(rsp.code));
×
187
    tDecoderClear(&decoder);
×
188
    return rsp.code;
×
189
  }
190

191
  tDecoderClear(&decoder);
42✔
192

193
  int32_t gError = streamGetFatalError(pMeta);
42✔
194
  if (gError != 0) {
42!
195
    tqError("vgId:%d global fatal occurs, code:%s, ts:%" PRId64 " func:%s", pMeta->vgId, tstrerror(gError),
×
196
            pMeta->fatalInfo.ts, pMeta->fatalInfo.func);
197
    return 0;
×
198
  }
199

200
  // update the nodeEpset when it exists
201
  streamMetaWLock(pMeta);
42✔
202

203
  // the task epset may be updated again and again, when replaying the WAL, the task may be in stop status.
204
  STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
42✔
205
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
42✔
206
  if (code != 0) {
42!
207
    tqError("vgId:%d failed to acquire task:0x%x when handling update task epset, it may have been dropped", vgId,
×
208
            req.taskId);
209
    rsp.code = TSDB_CODE_SUCCESS;
×
210
    streamMetaWUnLock(pMeta);
×
211
    taosArrayDestroy(req.pNodeList);
×
212
    return rsp.code;
×
213
  }
214

215
  const char* idstr = pTask->id.idStr;
42✔
216

217
  if (req.transId <= 0) {
42!
218
    tqError("vgId:%d invalid update nodeEp task, transId:%d, discard", vgId, req.taskId);
×
219
    rsp.code = TSDB_CODE_SUCCESS;
×
220

221
    streamMetaReleaseTask(pMeta, pTask);
×
222
    streamMetaWUnLock(pMeta);
×
223

224
    taosArrayDestroy(req.pNodeList);
×
225
    return rsp.code;
×
226
  }
227

228
  // info needs to be kept till the new trans to update the nodeEp arrived.
229
  bool update = streamMetaInitUpdateTaskList(pMeta, req.transId);
42✔
230
  if (!update) {
42!
231
    rsp.code = TSDB_CODE_SUCCESS;
×
232

233
    streamMetaReleaseTask(pMeta, pTask);
×
234
    streamMetaWUnLock(pMeta);
×
235

236
    taosArrayDestroy(req.pNodeList);
×
237
    return rsp.code;
×
238
  }
239

240
  // duplicate update epset msg received, discard this redundant message
241
  STaskUpdateEntry entry = {.streamId = req.streamId, .taskId = req.taskId, .transId = req.transId};
42✔
242

243
  void* pReqTask = taosHashGet(pMeta->updateInfo.pTasks, &entry, sizeof(STaskUpdateEntry));
42✔
244
  if (pReqTask != NULL) {
42!
245
    tqDebug("s-task:%s (vgId:%d) already update in transId:%d, discard the nodeEp update msg", idstr, vgId,
×
246
            req.transId);
247
    rsp.code = TSDB_CODE_SUCCESS;
×
248

249
    streamMetaReleaseTask(pMeta, pTask);
×
250
    streamMetaWUnLock(pMeta);
×
251

252
    taosArrayDestroy(req.pNodeList);
×
253
    return rsp.code;
×
254
  }
255

256
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
42✔
257

258
  // send the checkpoint-source-rsp for source task to end the checkpoint trans in mnode
259
  code = streamTaskSendCheckpointsourceRsp(pTask);
42✔
260
  if (code) {
42!
261
    tqError("%s failed to send checkpoint-source rsp, code:%s", pTask->id.idStr, tstrerror(code));
×
262
  }
263
  streamTaskResetStatus(pTask);
42✔
264

265
  streamTaskStopMonitorCheckRsp(&pTask->taskCheckInfo, pTask->id.idStr);
42✔
266

267
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
42✔
268
    code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->hTaskInfo.id, &pHTask);
6✔
269
    if (code != 0) {
6!
270
      tqError(
×
271
          "vgId:%d failed to acquire fill-history task:0x%x when handling update, may have been dropped already, rel "
272
          "stream task:0x%x",
273
          vgId, (uint32_t)pTask->hTaskInfo.id.taskId, req.taskId);
274
      CLEAR_RELATED_FILLHISTORY_TASK(pTask);
×
275
    } else {
276
      tqDebug("s-task:%s fill-history task update nodeEp along with stream task", pHTask->id.idStr);
6!
277
      bool updateEpSet = streamTaskUpdateEpsetInfo(pHTask, req.pNodeList);
6✔
278
      if (updateEpSet) {
6!
279
        updated = updateEpSet;
6✔
280
      }
281

282
      streamTaskResetStatus(pHTask);
6✔
283
      streamTaskStopMonitorCheckRsp(&pHTask->taskCheckInfo, pHTask->id.idStr);
6✔
284
    }
285
  }
286

287
  // stream do update the nodeEp info, write it into stream meta.
288
  if (updated) {
42✔
289
    tqDebug("s-task:%s vgId:%d save task after update epset, and stop task", idstr, vgId);
13!
290
    code = streamMetaSaveTaskInMeta(pMeta, pTask);
13✔
291
    if (code) {
13!
292
      tqError("s-task:%s vgId:%d failed to save task, code:%s", idstr, vgId, tstrerror(code));
×
293
    }
294

295
    if (pHTask != NULL) {
13✔
296
      code = streamMetaSaveTaskInMeta(pMeta, pHTask);
6✔
297
      if (code) {
6!
298
        tqError("s-task:%s vgId:%d failed to save related history task, code:%s", idstr, vgId, tstrerror(code));
×
299
      }
300
    }
301
  } else {
302
    tqDebug("s-task:%s vgId:%d not save task since not update epset actually, stop task", idstr, vgId);
29!
303
  }
304

305
  code = streamTaskStop(pTask);
42✔
306
  if (code) {
42!
307
    tqError("s-task:%s vgId:%d failed to stop task, code:%s", idstr, vgId, tstrerror(code));
×
308
  }
309

310
  if (pHTask != NULL) {
42✔
311
    code = streamTaskStop(pHTask);
6✔
312
    if (code) {
6!
313
      tqError("s-task:%s vgId:%d failed to stop related history task, code:%s", idstr, vgId, tstrerror(code));
×
314
    }
315
  }
316

317
  // keep info
318
  streamMetaAddIntoUpdateTaskList(pMeta, pTask, (pHTask != NULL) ? (pHTask) : NULL, req.transId, st);
42✔
319
  streamMetaReleaseTask(pMeta, pTask);
42✔
320
  streamMetaReleaseTask(pMeta, pHTask);
42✔
321

322
  rsp.code = TSDB_CODE_SUCCESS;
42✔
323

324
  // possibly only handle the stream task.
325
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
42✔
326
  int32_t updateTasks = taosHashGetSize(pMeta->updateInfo.pTasks);
42✔
327

328
  if (restored && isLeader) {
42!
329
    tqDebug("vgId:%d s-task:0x%x update epset transId:%d, set the restart flag", vgId, req.taskId, req.transId);
36!
330
    pMeta->startInfo.tasksWillRestart = 1;
36✔
331
  }
332

333
  if (updateTasks < numOfTasks) {
42✔
334
    if (isLeader) {
20!
335
      tqDebug("vgId:%d closed tasks:%d, unclosed:%d, all tasks will be started when nodeEp update completed", vgId,
20!
336
              updateTasks, (numOfTasks - updateTasks));
337
    } else {
338
      tqDebug("vgId:%d closed tasks:%d, unclosed:%d, follower not restart tasks", vgId, updateTasks,
×
339
              (numOfTasks - updateTasks));
340
    }
341
  } else {
342
    if ((code = streamMetaCommit(pMeta)) < 0) {
22!
343
      // always return true
344
      streamMetaWUnLock(pMeta);
×
345
      taosArrayDestroy(req.pNodeList);
×
346
      return TSDB_CODE_SUCCESS;
×
347
    }
348

349
    streamMetaClearSetUpdateTaskListComplete(pMeta);
22✔
350

351
    if (isLeader) {
22!
352
      if (!restored) {
22✔
353
        tqDebug("vgId:%d vnode restore not completed, not start all tasks", vgId);
3!
354
      } else {
355
        tqDebug("vgId:%d all %d task(s) nodeEp updated and closed, transId:%d", vgId, numOfTasks, req.transId);
19!
356
#if 0
357
      taosMSleep(5000);// for test purpose, to trigger the leader election
358
#endif
359
        code = tqStreamTaskStartAsync(pMeta, cb, true);
19✔
360
        if (code) {
19!
361
          tqError("vgId:%d async start all tasks, failed, code:%s", vgId, tstrerror(code));
×
362
        }
363
      }
364
    } else {
365
      tqDebug("vgId:%d follower nodes not restart tasks", vgId);
×
366
    }
367
  }
368

369
  streamMetaWUnLock(pMeta);
42✔
370
  taosArrayDestroy(req.pNodeList);
42✔
371
  return rsp.code;  // always return true
42✔
372
}
373

374
int32_t tqStreamTaskProcessDispatchReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
59,614✔
375
  char*   msgStr = pMsg->pCont;
59,614✔
376
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
59,614✔
377
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
59,614✔
378

379
  SStreamDispatchReq req = {0};
59,614✔
380

381
  SDecoder decoder;
382
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
59,614✔
383
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
59,614!
384
    tDecoderClear(&decoder);
×
385
    return TSDB_CODE_MSG_DECODE_ERROR;
×
386
  }
387
  tDecoderClear(&decoder);
59,614✔
388

389
  tqDebug("s-task:0x%x recv dispatch msg from 0x%x(vgId:%d)", req.taskId, req.upstreamTaskId, req.upstreamNodeId);
59,614✔
390

391
  SStreamTask* pTask = NULL;
59,614✔
392
  int32_t      code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
59,614✔
393
  if (pTask && (code == 0)) {
59,614!
394
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
59,565✔
395
    if (streamProcessDispatchMsg(pTask, &req, &rsp) != 0) {
59,565!
396
      return -1;
×
397
    }
398
    tCleanupStreamDispatchReq(&req);
59,565✔
399
    streamMetaReleaseTask(pMeta, pTask);
59,565✔
400
    return 0;
59,565✔
401
  } else {
402
    tqError("vgId:%d failed to find task:0x%x to handle the dispatch req, it may have been destroyed already",
49!
403
            pMeta->vgId, req.taskId);
404

405
    SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
49✔
406
    if (pRspHead == NULL) {
49!
407
      tqError("s-task:0x%x send dispatch error rsp, out of memory", req.taskId);
×
408
      return terrno;
×
409
    }
410

411
    pRspHead->vgId = htonl(req.upstreamNodeId);
49✔
412
    if (pRspHead->vgId == 0) {
49!
413
      tqError("vgId:%d invalid dispatch msg from upstream to task:0x%x", pMeta->vgId, req.taskId);
×
414
      return TSDB_CODE_INVALID_MSG;
×
415
    }
416

417
    SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
49✔
418
    pRsp->streamId = htobe64(req.streamId);
49✔
419
    pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
49✔
420
    pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
49✔
421
    pRsp->downstreamNodeId = htonl(pMeta->vgId);
49✔
422
    pRsp->downstreamTaskId = htonl(req.taskId);
49✔
423
    pRsp->msgId = htonl(req.msgId);
49✔
424
    pRsp->stage = htobe64(req.stage);
49✔
425
    pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;
49✔
426

427
    int32_t len = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
49✔
428
    SRpcMsg rsp = {.code = TSDB_CODE_STREAM_TASK_NOT_EXIST, .info = pMsg->info, .contLen = len, .pCont = pRspHead};
49✔
429
    tqError("s-task:0x%x send dispatch error rsp, no task", req.taskId);
49!
430

431
    tmsgSendRsp(&rsp);
49✔
432
    tCleanupStreamDispatchReq(&req);
49✔
433

434
    return 0;
49✔
435
  }
436
}
437

438
int32_t tqStreamTaskProcessDispatchRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
59,614✔
439
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
59,614✔
440

441
  int32_t vgId = pMeta->vgId;
59,614✔
442
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
59,614✔
443
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
59,614✔
444
  pRsp->streamId = htobe64(pRsp->streamId);
59,614✔
445
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
59,614✔
446
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
59,614✔
447
  pRsp->stage = htobe64(pRsp->stage);
59,614✔
448
  pRsp->msgId = htonl(pRsp->msgId);
59,614✔
449

450
  tqDebug("s-task:0x%x vgId:%d recv dispatch-rsp from 0x%x vgId:%d", pRsp->upstreamTaskId, pRsp->upstreamNodeId,
59,614✔
451
          pRsp->downstreamTaskId, pRsp->downstreamNodeId);
452

453
  SStreamTask* pTask = NULL;
59,614✔
454
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
59,614✔
455
  if (pTask && (code == 0)) {
59,614!
456
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
59,604✔
457
    streamMetaReleaseTask(pMeta, pTask);
59,604✔
458
    return code;
59,604✔
459
  } else {
460
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
10✔
461
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
10✔
462
  }
463
}
464

465
int32_t tqStreamTaskProcessRetrieveReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
532✔
466
  char*    msgStr = pMsg->pCont;
532✔
467
  char*    msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
532✔
468
  int32_t  msgLen = pMsg->contLen - sizeof(SMsgHead);
532✔
469
  int32_t  code = 0;
532✔
470
  SDecoder decoder;
471

472
  SStreamRetrieveReq req;
473
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
532✔
474
  code = tDecodeStreamRetrieveReq(&decoder, &req);
532✔
475
  tDecoderClear(&decoder);
532✔
476

477
  if (code) {
532!
478
    tqError("vgId:%d failed to decode retrieve msg, discard it", pMeta->vgId);
×
479
    return code;
×
480
  }
481

482
  SStreamTask* pTask = NULL;
532✔
483
  code = streamMetaAcquireTask(pMeta, req.streamId, req.dstTaskId, &pTask);
532✔
484
  if (pTask == NULL || code != 0) {
531!
485
    tqError("vgId:%d process retrieve req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
486
            req.dstTaskId);
487
    tCleanupStreamRetrieveReq(&req);
×
488
    return code;
×
489
  }
490

491
  // enqueue
492
  tqDebug("s-task:%s (vgId:%d level:%d) recv retrieve req from task:0x%x(vgId:%d), QID:0x%" PRIx64, pTask->id.idStr,
531✔
493
          pTask->pMeta->vgId, pTask->info.taskLevel, req.srcTaskId, req.srcNodeId, req.reqId);
494

495
  // if task is in ck status, set current ck failed
496
  streamTaskSetCheckpointFailed(pTask);
531✔
497

498
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
531✔
499
    code = streamProcessRetrieveReq(pTask, &req);
526✔
500
  } else {
501
    req.srcNodeId = pTask->info.nodeId;
5✔
502
    req.srcTaskId = pTask->id.taskId;
5✔
503
    code = streamTaskBroadcastRetrieveReq(pTask, &req);
5✔
504
  }
505

506
  if (code != TSDB_CODE_SUCCESS) {  // return error not send rsp manually
532!
507
    tqError("s-task:0x%x vgId:%d failed to process retrieve request from 0x%x, code:%s", req.dstTaskId, req.dstNodeId,
×
508
            req.srcTaskId, tstrerror(code));
509
  } else {  // send rsp manually only on success.
510
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
532✔
511
    streamTaskSendRetrieveRsp(&req, &rsp);
532✔
512
  }
513

514
  streamMetaReleaseTask(pMeta, pTask);
529✔
515
  tCleanupStreamRetrieveReq(&req);
529✔
516

517
  // always return success, to disable the auto rsp
518
  return code;
531✔
519
}
520

521
int32_t tqStreamTaskProcessCheckReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
22,709✔
522
  char*   msgStr = pMsg->pCont;
22,709✔
523
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
22,709✔
524
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
22,709✔
525
  int32_t code = 0;
22,709✔
526

527
  SStreamTaskCheckReq req;
528
  SStreamTaskCheckRsp rsp = {0};
22,709✔
529

530
  SDecoder decoder;
531

532
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
22,709✔
533
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
22,709✔
534
  tDecoderClear(&decoder);
22,709✔
535

536
  if (code) {
22,709!
537
    tqError("vgId:%d decode check msg failed, not handle this msg", pMeta->vgId);
×
538
    return code;
×
539
  }
540

541
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
22,709✔
542
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
22,709✔
543
}
544

545
int32_t tqStreamTaskProcessCheckRsp(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
22,705✔
546
  char*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
22,705✔
547
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
22,705✔
548
  int32_t vgId = pMeta->vgId;
22,705✔
549
  int32_t code = TSDB_CODE_SUCCESS;
22,705✔
550

551
  SStreamTaskCheckRsp rsp;
552

553
  SDecoder decoder;
554
  tDecoderInit(&decoder, (uint8_t*)pReq, len);
22,705✔
555
  code = tDecodeStreamTaskCheckRsp(&decoder, &rsp);
22,705✔
556
  if (code < 0) {
22,705!
557
    terrno = TSDB_CODE_INVALID_MSG;
×
558
    tDecoderClear(&decoder);
×
559
    tqError("vgId:%d failed to parse check rsp msg, code:%s", vgId, tstrerror(terrno));
×
560
    return -1;
×
561
  }
562

563
  tDecoderClear(&decoder);
22,705✔
564
  tqDebug("tq task:0x%x (vgId:%d) recv check rsp(QID:0x%" PRIx64 ") from 0x%x (vgId:%d) status %d", rsp.upstreamTaskId,
22,705✔
565
          rsp.upstreamNodeId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.status);
566

567
  if (!isLeader) {
22,705!
568
    tqError("vgId:%d not leader, task:0x%x not handle the check rsp, downstream:0x%x (vgId:%d)", vgId,
×
569
            rsp.upstreamTaskId, rsp.downstreamTaskId, rsp.downstreamNodeId);
570
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
×
571
  }
572

573
  SStreamTask* pTask = NULL;
22,705✔
574
  code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.upstreamTaskId, &pTask);
22,705✔
575
  if ((pTask == NULL) || (code != 0)) {
22,705!
576
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
28✔
577
  }
578

579
  code = streamTaskProcessCheckRsp(pTask, &rsp);
22,677✔
580
  streamMetaReleaseTask(pMeta, pTask);
22,677✔
581
  return code;
22,677✔
582
}
583

584
int32_t tqStreamTaskProcessCheckpointReadyMsg(SStreamMeta* pMeta, SRpcMsg* pMsg) {
8,158✔
585
  int32_t vgId = pMeta->vgId;
8,158✔
586
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
8,158✔
587
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
8,158✔
588
  int32_t code = 0;
8,158✔
589

590
  SStreamCheckpointReadyMsg req = {0};
8,158✔
591

592
  SDecoder decoder;
593
  tDecoderInit(&decoder, (uint8_t*)msg, len);
8,158✔
594
  if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
8,158!
595
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
596
    tDecoderClear(&decoder);
×
597
    return code;
×
598
  }
599
  tDecoderClear(&decoder);
8,158✔
600

601
  SStreamTask* pTask = NULL;
8,158✔
602
  code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
8,158✔
603
  if (code != 0) {
8,158!
604
    tqError("vgId:%d failed to find s-task:0x%x, it may have been destroyed already", vgId, req.downstreamTaskId);
×
605
    return code;
×
606
  }
607

608
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
8,158!
609
    tqDebug("vgId:%d s-task:%s recv invalid the checkpoint-ready msg from task:0x%x (vgId:%d), discard", vgId,
×
610
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
611
    streamMetaReleaseTask(pMeta, pTask);
×
612
    return TSDB_CODE_INVALID_MSG;
×
613
  } else {
614
    tqDebug("vgId:%d s-task:%s received the checkpoint-ready msg from task:0x%x (vgId:%d), handle it", vgId,
8,158✔
615
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
616
  }
617

618
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
8,158✔
619
  streamMetaReleaseTask(pMeta, pTask);
8,158✔
620
  if (code) {
8,158!
621
    return code;
×
622
  }
623

624
  {  // send checkpoint ready rsp
625
    SMStreamCheckpointReadyRspMsg* pReadyRsp = rpcMallocCont(sizeof(SMStreamCheckpointReadyRspMsg));
8,158✔
626
    if (pReadyRsp == NULL) {
8,158!
627
      return terrno;
×
628
    }
629

630
    pReadyRsp->upstreamTaskId = req.upstreamTaskId;
8,158✔
631
    pReadyRsp->upstreamNodeId = req.upstreamNodeId;
8,158✔
632
    pReadyRsp->downstreamTaskId = req.downstreamTaskId;
8,158✔
633
    pReadyRsp->downstreamNodeId = req.downstreamNodeId;
8,158✔
634
    pReadyRsp->checkpointId = req.checkpointId;
8,158✔
635
    pReadyRsp->streamId = req.streamId;
8,158✔
636
    pReadyRsp->head.vgId = htonl(req.downstreamNodeId);
8,158✔
637

638
    SRpcMsg rsp = {.code = 0, .info = pMsg->info, .pCont = pReadyRsp, .contLen = sizeof(SMStreamCheckpointReadyRspMsg)};
8,158✔
639
    tmsgSendRsp(&rsp);
8,158✔
640

641
    pMsg->info.handle = NULL;  // disable auto rsp
8,158✔
642
  }
643

644
  return code;
8,158✔
645
}
646

647
int32_t tqStreamTaskProcessDeployReq(SStreamMeta* pMeta, SMsgCb* cb, int64_t sversion, char* msg, int32_t msgLen,
14,077✔
648
                                     bool isLeader, bool restored) {
649
  int32_t code = 0;
14,077✔
650
  int32_t vgId = pMeta->vgId;
14,077✔
651
  int32_t numOfTasks = 0;
14,077✔
652
  int32_t taskId = -1;
14,077✔
653
  int64_t streamId = -1;
14,077✔
654
  bool    added = false;
14,077✔
655
  int32_t size = sizeof(SStreamTask);
14,077✔
656

657
  if (tsDisableStream) {
14,077!
658
    tqInfo("vgId:%d stream disabled, not deploy stream tasks", vgId);
×
659
    return code;
×
660
  }
661

662
  tqDebug("vgId:%d receive new stream task deploy msg, start to build stream task", vgId);
14,077✔
663

664
  // 1.deserialize msg and build task
665
  SStreamTask* pTask = taosMemoryCalloc(1, size);
14,078!
666
  if (pTask == NULL) {
14,093!
667
    tqError("vgId:%d failed to create stream task due to out of memory, alloc size:%d", vgId, size);
×
668
    return terrno;
×
669
  }
670

671
  SDecoder decoder;
672
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
14,093✔
673
  code = tDecodeStreamTask(&decoder, pTask);
14,095✔
674
  tDecoderClear(&decoder);
14,088✔
675

676
  if (code != TSDB_CODE_SUCCESS) {
14,095!
677
    taosMemoryFree(pTask);
×
678
    return TSDB_CODE_INVALID_MSG;
×
679
  }
680

681
  // 2.save task, use the latest commit version as the initial start version of stream task.
682
  taskId = pTask->id.taskId;
14,095✔
683
  streamId = pTask->id.streamId;
14,095✔
684

685
  streamMetaWLock(pMeta);
14,095✔
686
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
14,093✔
687
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
14,102✔
688
  streamMetaWUnLock(pMeta);
14,103✔
689

690
  if (code < 0) {
14,102!
691
    tqError("vgId:%d failed to register s-task:0x%x into meta, existed tasks:%d, code:%s", vgId, taskId, numOfTasks,
×
692
            tstrerror(code));
693
    return code;
×
694
  }
695

696
  // added into meta store, pTask cannot be reference since it may have been destroyed by other threads already now if
697
  // it is added into the meta store
698
  if (added) {
14,102!
699
    // only handled in the leader node
700
    if (isLeader) {
14,102✔
701
      tqDebug("vgId:%d s-task:0x%x is deployed and add into meta, numOfTasks:%d", vgId, taskId, numOfTasks);
14,074✔
702

703
      if (restored) {
14,074✔
704
        SStreamTask* p = NULL;
13,994✔
705
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
13,994✔
706
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
13,994!
707
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
9,193✔
708
        }
709

710
        if (p != NULL) {
13,995!
711
          streamMetaReleaseTask(pMeta, p);
13,995✔
712
        }
713
      } else {
714
        tqWarn("s-task:0x%x not launched since vnode(vgId:%d) not ready", taskId, vgId);
80!
715
      }
716

717
    } else {
718
      tqDebug("vgId:%d not leader, not launch stream task s-task:0x%x", vgId, taskId);
28!
719
    }
720
  } else {
721
    tqWarn("vgId:%d failed to add s-task:0x%x, since already exists in meta store, total:%d", vgId, taskId, numOfTasks);
×
722
  }
723

724
  return code;
14,102✔
725
}
726

727
int32_t tqStreamTaskProcessDropReq(SStreamMeta* pMeta, char* msg, int32_t msgLen) {
6,992✔
728
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
6,992✔
729
  int32_t              code = 0;
6,992✔
730
  int32_t              vgId = pMeta->vgId;
6,992✔
731
  STaskId              hTaskId = {0};
6,992✔
732
  SStreamTask*         pTask = NULL;
6,992✔
733

734
  tqDebug("vgId:%d receive msg to drop s-task:0x%x", vgId, pReq->taskId);
6,992✔
735

736
  streamMetaWLock(pMeta);
6,992✔
737

738
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
7,004✔
739
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
7,004✔
740
  if (code == 0) {
7,003!
741
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
7,003✔
742
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
1,326✔
743
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
1,326✔
744
    }
745

746
    // clear the relationship, and then release the stream tasks, to avoid invalid accessing of already freed
747
    // related stream(history) task
748
    streamTaskSetRemoveBackendFiles(pTask);
7,003✔
749
    code = streamTaskClearHTaskAttr(pTask, pReq->resetRelHalt);
6,993✔
750
    streamMetaReleaseTask(pMeta, pTask);
6,999✔
751

752
    if (code) {
7,003!
753
      tqError("s-task:0x%x failed to clear related fill-history info, still exists", pReq->taskId);
×
754
    }
755
  }
756

757
  // drop the related fill-history task firstly
758
  if (hTaskId.taskId != 0 && hTaskId.streamId != 0) {
7,003!
759
    tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x firstly", pReq->taskId, vgId, (int32_t)hTaskId.taskId);
1,327✔
760
    code = streamMetaUnregisterTask(pMeta, hTaskId.streamId, hTaskId.taskId);
1,327✔
761
    if (code) {
1,327!
762
      tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x failed", pReq->taskId, vgId,
×
763
              (int32_t)hTaskId.taskId);
764
    }
765
  }
766

767
  // drop the stream task now
768
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
7,003✔
769
  if (code) {
7,003!
770
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
771
  }
772

773
  // commit the update
774
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
7,003✔
775
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
7,004✔
776

777
  if (streamMetaCommit(pMeta) < 0) {
7,004✔
778
    // persist to disk
779
  }
780

781
  streamMetaWUnLock(pMeta);
7,008✔
782
  tqDebug("vgId:%d process drop task:0x%x completed", vgId, pReq->taskId);
7,008✔
783

784
  return 0;  // always return success
7,008✔
785
}
786

787
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
5,415✔
788
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
5,415✔
789
  int32_t                    code = 0;
5,415✔
790
  int32_t                    vgId = pMeta->vgId;
5,415✔
791
  SStreamTask*               pTask = NULL;
5,415✔
792

793
  tqDebug("vgId:%d receive msg to update-checkpoint-info for s-task:0x%x", vgId, pReq->taskId);
5,415✔
794

795
  streamMetaWLock(pMeta);
5,415✔
796

797
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
5,417✔
798
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
5,417✔
799
  if (code == 0) {
5,421!
800
    code = streamTaskUpdateTaskCheckpointInfo(pTask, restored, pReq);
5,421✔
801
    streamMetaReleaseTask(pMeta, pTask);
5,422✔
802
  } else {  // failed to get the task.
803
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
804
    tqError(
×
805
        "vgId:%d failed to locate the s-task:0x%x to update the checkpoint info, numOfTasks:%d, it may have been "
806
        "dropped already",
807
        vgId, pReq->taskId, numOfTasks);
808
  }
809

810
  streamMetaWUnLock(pMeta);
5,422✔
811
  // always return success when handling the requirement issued by mnode during transaction.
812
  return TSDB_CODE_SUCCESS;
5,422✔
813
}
814

815
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
19✔
816
  int32_t vgId = pMeta->vgId;
19✔
817
  int32_t code = 0;
19✔
818
  int64_t st = taosGetTimestampMs();
19✔
819

820
  streamMetaWLock(pMeta);
19✔
821
  if (pMeta->startInfo.startAllTasks == 1) {
19!
822
    pMeta->startInfo.restartCount += 1;
×
823
    tqDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
824
            pMeta->startInfo.restartCount);
825
    streamMetaWUnLock(pMeta);
×
826
    return TSDB_CODE_SUCCESS;
×
827
  }
828

829
  pMeta->startInfo.startAllTasks = 1;
19✔
830

831
  terrno = 0;
19✔
832
  tqInfo("vgId:%d tasks are all updated and stopped, restart all tasks, triggered by transId:%d, ts:%" PRId64, vgId,
19!
833
         pMeta->updateInfo.completeTransId, pMeta->updateInfo.completeTs);
834

835
  streamMetaClear(pMeta);
19✔
836

837
  int64_t el = taosGetTimestampMs() - st;
19✔
838
  tqInfo("vgId:%d close&reload state elapsed time:%.3fs", vgId, el / 1000.);
19!
839

840
  streamMetaLoadAllTasks(pMeta);
19✔
841

842
  {
843
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
19✔
844
    taosHashClear(pStartInfo->pReadyTaskSet);
19✔
845
    taosHashClear(pStartInfo->pFailedTaskSet);
19✔
846
    pStartInfo->readyTs = 0;
19✔
847
  }
848

849
  if (isLeader && !tsDisableStream) {
19!
850
    streamMetaWUnLock(pMeta);
19✔
851
    code = streamMetaStartAllTasks(pMeta);
19✔
852
  } else {
853
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
×
854
    pMeta->startInfo.restartCount = 0;
×
855
    streamMetaWUnLock(pMeta);
×
856
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
×
857
  }
858

859
  code = terrno;
19✔
860
  return code;
19✔
861
}
862

863
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
100,244✔
864
  int32_t  code = 0;
100,244✔
865
  int32_t  vgId = pMeta->vgId;
100,244✔
866
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
100,244✔
867
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
100,244✔
868
  SDecoder decoder;
869

870
  SStreamTaskRunReq req = {0};
100,244✔
871
  tDecoderInit(&decoder, (uint8_t*)msg, len);
100,244✔
872
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
100,455!
873
    tqError("vgId:%d failed to decode task run req, code:%s", pMeta->vgId, tstrerror(code));
×
874
    tDecoderClear(&decoder);
×
875
    return TSDB_CODE_SUCCESS;
×
876
  }
877

878
  tDecoderClear(&decoder);
100,459✔
879

880
  int32_t type = req.reqType;
100,467✔
881
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
100,467✔
882
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
9,399✔
883
    return 0;
9,403✔
884
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
91,068✔
885
    code = streamMetaStartAllTasks(pMeta);
9,988✔
886
    return 0;
9,987✔
887
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
81,080✔
888
    code = restartStreamTasks(pMeta, isLeader);
19✔
889
    return 0;
19✔
890
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
81,061✔
891
    code = streamMetaStopAllTasks(pMeta);
4,634✔
892
    return 0;
4,634✔
893
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
76,427!
894
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
895
    return code;
×
896
  } else if (type == STREAM_EXEC_T_STOP_ONE_TASK) {
76,427!
897
    code = streamMetaStopOneTask(pMeta, req.streamId, req.taskId);
×
898
    return code;
×
899
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
76,427✔
900
    SStreamTask* pTask = NULL;
9,437✔
901
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
9,437✔
902

903
    if (pTask != NULL && (code == 0)) {
9,428!
904
      char* pStatus = NULL;
9,432✔
905
      if (streamTaskReadyToRun(pTask, &pStatus)) {
9,432!
906
        int64_t execTs = pTask->status.lastExecTs;
9,438✔
907
        int32_t idle = taosGetTimestampMs() - execTs;
9,438✔
908
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
9,438✔
909

910
        code = streamResumeTask(pTask);
9,438✔
911
      } else {
912
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
913
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
914
                pTask->id.idStr, pStatus, status);
915
      }
916
      streamMetaReleaseTask(pMeta, pTask);
9,438✔
917
    }
918

919
    return code;
9,434✔
920
  }
921

922
  SStreamTask* pTask = NULL;
66,990✔
923
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
66,990✔
924
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
66,937!
925
    char* p = NULL;
66,956✔
926
    if (streamTaskReadyToRun(pTask, &p)) {
66,956✔
927
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
66,867✔
928
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
929
      (void)streamExecTask(pTask);
66,867✔
930
    } else {
931
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
18✔
932
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
29!
933
              pTask->id.idStr, p, status);
934
    }
935

936
    streamMetaReleaseTask(pMeta, pTask);
66,888✔
937
    return 0;
66,966✔
938
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
939
    // todo add one function to handle this
940
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
×
941
    return code;
10✔
942
  }
943
}
944

945
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
65✔
946
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
65✔
947
  int32_t         vgId = pMeta->vgId;
65✔
948
  bool            scanWal = false;
65✔
949
  int32_t         code = 0;
65✔
950

951
  streamMetaWLock(pMeta);
65✔
952
  if (pStartInfo->startAllTasks == 1) {
65!
953
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
954
            pMeta->startInfo.restartCount);
955
  } else {  // not in starting procedure
956
    bool allReady = streamMetaAllTasksReady(pMeta);
65✔
957

958
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
65!
959
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
960
      pStartInfo->restartCount -= 1;
×
961
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
×
962
              pStartInfo->restartCount);
963
      streamMetaWUnLock(pMeta);
×
964

965
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
×
966
    } else {
967
      if (pStartInfo->restartCount == 0) {
65!
968
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
65✔
969
      } else if (allReady) {
×
970
        pStartInfo->restartCount = 0;
×
971
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
972
      }
973

974
      scanWal = true;
65✔
975
    }
976
  }
977

978
  streamMetaWUnLock(pMeta);
65✔
979

980
  return code;
65✔
981
}
982

983
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
984
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
985

986
  SStreamTask* pTask = NULL;
×
987
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
988
  if (pTask == NULL || (code != 0)) {
×
989
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
990
            pMeta->vgId, pReq->taskId);
991
    return TSDB_CODE_SUCCESS;
×
992
  }
993

994
  tqDebug("s-task:%s receive task-reset msg from mnode, reset status and ready for data processing", pTask->id.idStr);
×
995

996
  streamMutexLock(&pTask->lock);
×
997

998
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
999
  streamTaskClearCheckInfo(pTask, true);
×
1000

1001
  // clear flag set during do checkpoint, and open inputQ for all upstream tasks
1002
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1003
  if (pState.state == TASK_STATUS__CK) {
×
1004
    streamTaskSetStatusReady(pTask);
×
1005
    tqDebug("s-task:%s reset checkpoint status to ready", pTask->id.idStr);
×
1006
  } else if (pState.state == TASK_STATUS__UNINIT) {
×
1007
    //    tqDebug("s-task:%s start task by checking downstream tasks", pTask->id.idStr);
1008
    //    tqStreamTaskRestoreCheckpoint(pMeta, pTask->id.streamId, pTask->id.taskId);
1009
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
1010
  } else {
1011
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
1012
  }
1013

1014
  streamMutexUnlock(&pTask->lock);
×
1015

1016
  streamMetaReleaseTask(pMeta, pTask);
×
1017
  return TSDB_CODE_SUCCESS;
×
1018
}
1019

1020
int32_t tqStreamTaskProcessAllTaskStopReq(SStreamMeta* pMeta, SMsgCb* pMsgCb, SRpcMsg* pMsg) {
4,145✔
1021
  int32_t  code = 0;
4,145✔
1022
  int32_t  vgId = pMeta->vgId;
4,145✔
1023
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
4,145✔
1024
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
4,145✔
1025
  SDecoder decoder;
1026

1027
  SStreamTaskStopReq req = {0};
4,145✔
1028
  tDecoderInit(&decoder, (uint8_t*)msg, len);
4,145✔
1029
  if ((code = tDecodeStreamTaskStopReq(&decoder, &req)) < 0) {
4,141!
1030
    tqError("vgId:%d failed to decode stop all streams, code:%s", pMeta->vgId, tstrerror(code));
×
1031
    tDecoderClear(&decoder);
×
1032
    return TSDB_CODE_SUCCESS;
×
1033
  }
1034

1035
  tDecoderClear(&decoder);
4,151✔
1036

1037
  // stop all stream tasks, only invoked when trying to drop db
1038
  if (req.streamId <= 0) {
4,146!
1039
    tqDebug("vgId:%d recv msg to stop all tasks in sync before dropping vnode", vgId);
4,149✔
1040
    code = streamMetaStopAllTasks(pMeta);
4,149✔
1041
    if (code) {
4,153!
1042
      tqError("vgId:%d failed to stop all tasks, code:%s", vgId, tstrerror(code));
×
1043
    }
1044

1045
  } else {  // stop only one stream tasks
1046

1047
  }
1048

1049
  // always return success
1050
  return TSDB_CODE_SUCCESS;
4,153✔
1051
}
1052

1053
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1054
  SRetrieveChkptTriggerReq req = {0};
×
1055
  SStreamTask*             pTask = NULL;
×
1056
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1057
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
1058
  SDecoder                 decoder = {0};
×
1059

1060
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1061
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1062
    tDecoderClear(&decoder);
×
1063
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
1064
    return TSDB_CODE_INVALID_MSG;
×
1065
  }
1066
  tDecoderClear(&decoder);
×
1067

1068
  int32_t code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
1069
  if (pTask == NULL || (code != 0)) {
×
1070
    tqError("vgId:%d process retrieve checkpoint-trigger, checkpointId:%" PRId64
×
1071
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
1072
            pMeta->vgId, req.checkpointId, (int32_t)req.downstreamTaskId, req.upstreamTaskId);
1073
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1074
  }
1075

1076
  tqDebug("s-task:0x%x recv retrieve checkpoint-trigger msg from downstream s-task:0x%x, checkpointId:%" PRId64,
×
1077
          req.upstreamTaskId, (int32_t)req.downstreamTaskId, req.checkpointId);
1078

1079
  if (pTask->status.downstreamReady != 1) {
×
1080
    tqError("s-task:%s not ready for checkpoint-trigger retrieve from 0x%x, since downstream not ready",
×
1081
            pTask->id.idStr, (int32_t)req.downstreamTaskId);
1082

1083
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1084
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
1085
    streamMetaReleaseTask(pMeta, pTask);
×
1086
    return code;
×
1087
  }
1088

1089
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1090
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1091
    int32_t transId = 0;
×
1092
    int64_t checkpointId = 0;
×
1093

1094
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
1095
    if (checkpointId != req.checkpointId) {
×
1096
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1097
              " req:%" PRId64,
1098
              pTask->id.idStr, req.downstreamTaskId, checkpointId, req.checkpointId);
1099
      streamMetaReleaseTask(pMeta, pTask);
×
1100
      return TSDB_CODE_INVALID_MSG;
×
1101
    }
1102

1103
    if (streamTaskAlreadySendTrigger(pTask, req.downstreamNodeId)) {
×
1104
      // re-send the lost checkpoint-trigger msg to downstream task
1105
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1106
              (int32_t)req.downstreamTaskId, checkpointId, transId);
1107
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1108
                                                TSDB_CODE_SUCCESS);
1109
    } else {  // not send checkpoint-trigger yet, wait
1110
      int32_t recv = 0, total = 0;
×
1111
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1112

1113
      if (recv == total) {  // add the ts info
×
1114
        tqWarn("s-task:%s all upstream send checkpoint-source/trigger, but not processed yet, wait", pTask->id.idStr);
×
1115
      } else {
1116
        tqWarn(
×
1117
            "s-task:%s not all upstream send checkpoint-source/trigger, total recv:%d/%d, wait for all upstream "
1118
            "sending checkpoint-source/trigger",
1119
            pTask->id.idStr, recv, total);
1120
      }
1121
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1122
                                                TSDB_CODE_ACTION_IN_PROGRESS);
1123
    }
1124
  } else {  // upstream not recv the checkpoint-source/trigger till now
1125
    if (!(pState.state == TASK_STATUS__READY || pState.state == TASK_STATUS__HALT)) {
×
1126
      tqFatal("s-task:%s invalid task status:%s", pTask->id.idStr, pState.name);
×
1127
    }
1128

1129
    tqWarn(
×
1130
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1131
        "upstream sending checkpoint-source/trigger",
1132
        pTask->id.idStr);
1133
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1134
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1135
  }
1136

1137
  streamMetaReleaseTask(pMeta, pTask);
×
1138
  return code;
×
1139
}
1140

1141
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1142
  SCheckpointTriggerRsp rsp = {0};
×
1143
  SStreamTask*          pTask = NULL;
×
1144
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1145
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
1146
  SDecoder              decoder = {0};
×
1147

1148
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1149
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1150
    tDecoderClear(&decoder);
×
1151
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1152
    return TSDB_CODE_INVALID_MSG;
×
1153
  }
1154
  tDecoderClear(&decoder);
×
1155

1156
  int32_t code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.taskId, &pTask);
×
1157
  if (pTask == NULL || (code != 0)) {
×
1158
    tqError(
×
1159
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1160
        pMeta->vgId, rsp.taskId);
1161
    return code;
×
1162
  }
1163

1164
  tqDebug(
×
1165
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1166
      ", transId:%d",
1167
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1168

1169
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
1170
  streamMetaReleaseTask(pMeta, pTask);
×
1171
  return code;
×
1172
}
1173

1174
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
1,372✔
1175
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
1,372✔
1176

1177
  SStreamTask* pTask = NULL;
1,372✔
1178
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,372✔
1179
  if (pTask == NULL || (code != 0)) {
1,375!
1180
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
1181
            pReq->taskId);
1182
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1183
    return TSDB_CODE_SUCCESS;
×
1184
  }
1185

1186
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
1,376✔
1187
  streamTaskPause(pTask);
1,376✔
1188

1189
  SStreamTask* pHistoryTask = NULL;
1,379✔
1190
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
1,379✔
1191
    pHistoryTask = NULL;
72✔
1192
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
72✔
1193
    if (pHistoryTask == NULL || (code != 0)) {
72!
1194
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1195
              ", it may have been dropped already",
1196
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
1197
      streamMetaReleaseTask(pMeta, pTask);
×
1198

1199
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1200
      return TSDB_CODE_SUCCESS;
×
1201
    }
1202

1203
    tqDebug("s-task:%s fill-history task handle paused along with related stream task", pHistoryTask->id.idStr);
72!
1204

1205
    streamTaskPause(pHistoryTask);
72✔
1206
    streamMetaReleaseTask(pMeta, pHistoryTask);
72✔
1207
  }
1208

1209
  streamMetaReleaseTask(pMeta, pTask);
1,379✔
1210
  return TSDB_CODE_SUCCESS;
1,379✔
1211
}
1212

1213
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
2,634✔
1214
                                       bool fromVnode) {
1215
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
2,634✔
1216
  int32_t      vgId = pMeta->vgId;
2,634✔
1217
  int32_t      code = 0;
2,634✔
1218

1219
  streamTaskResume(pTask);
2,634✔
1220
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,644✔
1221

1222
  int32_t level = pTask->info.taskLevel;
2,644✔
1223
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
2,644!
1224
    // no lock needs to secure the access of the version
1225
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
2,644!
1226
      // discard all the data  when the stream task is suspended.
1227
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
589✔
1228
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
589✔
1229
              ", schedStatus:%d",
1230
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1231
    } else {  // from the previous paused version and go on
1232
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
2,055✔
1233
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1234
    }
1235

1236
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
2,644✔
1237
      pTask->hTaskInfo.operatorOpen = false;
32✔
1238
      code = streamStartScanHistoryAsync(pTask, igUntreated);
32✔
1239
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
2,612✔
1240
//      code = tqScanWalAsync((STQ*)handle, false);
1241
    } else {
1242
      code = streamTrySchedExec(pTask, false);
1,328✔
1243
    }
1244
  }
1245

1246
  return code;
2,644✔
1247
}
1248

1249
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
2,538✔
1250
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
2,538✔
1251

1252
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
2,538✔
1253

1254
  SStreamTask* pTask = NULL;
2,538✔
1255
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
2,538✔
1256
  if (pTask == NULL || (code != 0)) {
2,543!
1257
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1258
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1259
  }
1260

1261
  streamMutexLock(&pTask->lock);
2,543✔
1262
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,544✔
1263
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
2,544✔
1264
  streamMutexUnlock(&pTask->lock);
2,544✔
1265

1266
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
2,542✔
1267
  if (code != 0) {
2,548!
1268
    streamMetaReleaseTask(pMeta, pTask);
×
1269
    return code;
×
1270
  }
1271

1272
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
2,548✔
1273
  SStreamTask* pHTask = NULL;
2,548✔
1274
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
2,548✔
1275
  if (pHTask && (code == 0)) {
2,548!
1276
    streamMutexLock(&pHTask->lock);
96✔
1277
    SStreamTaskState p = streamTaskGetStatus(pHTask);
96✔
1278
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
96!
1279
    streamMutexUnlock(&pHTask->lock);
96✔
1280

1281
    code = tqProcessTaskResumeImpl(handle, pHTask, sversion, pReq->igUntreated, fromVnode);
96✔
1282
    tqDebug("s-task:%s resume complete, code:%s", pHTask->id.idStr, tstrerror(code));
96!
1283

1284
    streamMetaReleaseTask(pMeta, pHTask);
96✔
1285
  }
1286

1287
  streamMetaReleaseTask(pMeta, pTask);
2,548✔
1288
  return TSDB_CODE_SUCCESS;
2,548✔
1289
}
1290

1291
int32_t tqStreamTasksGetTotalNum(SStreamMeta* pMeta) { return taosArrayGetSize(pMeta->pTaskList); }
×
1292

1293
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
10,524✔
1294
  rpcFreeCont(pMsg->pCont);
10,524✔
1295
  pMsg->pCont = NULL;
10,524✔
1296
  return TSDB_CODE_SUCCESS;
10,524✔
1297
}
1298

1299
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
27,696✔
1300
  SMStreamHbRspMsg rsp = {0};
27,696✔
1301
  int32_t          code = 0;
27,696✔
1302
  SDecoder         decoder;
1303
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
27,696✔
1304
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
27,696✔
1305

1306
  tDecoderInit(&decoder, (uint8_t*)msg, len);
27,696✔
1307
  code = tDecodeStreamHbRsp(&decoder, &rsp);
27,696✔
1308
  if (code < 0) {
27,695!
1309
    terrno = TSDB_CODE_INVALID_MSG;
×
1310
    tDecoderClear(&decoder);
×
1311
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1312
    return terrno;
×
1313
  }
1314

1315
  tDecoderClear(&decoder);
27,695✔
1316
  return streamProcessHeartbeatRsp(pMeta, &rsp);
27,696✔
1317
}
1318

1319
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
4,386✔
1320

1321
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
6,138✔
1322

1323
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
8,158✔
1324
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
8,158✔
1325

1326
  SStreamTask* pTask = NULL;
8,158✔
1327
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
8,158✔
1328
  if (pTask == NULL || (code != 0)) {
8,158!
1329
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
×
1330
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1331
    return code;
×
1332
  }
1333

1334
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
8,158✔
1335
  streamMetaReleaseTask(pMeta, pTask);
8,158✔
1336
  return code;
8,158✔
1337
}
1338

1339
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
211✔
1340
  int32_t                vgId = pMeta->vgId;
211✔
1341
  int32_t                code = 0;
211✔
1342
  SStreamTask*           pTask = NULL;
211✔
1343
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
211✔
1344
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
211✔
1345
  int64_t                now = taosGetTimestampMs();
211✔
1346
  SDecoder               decoder;
1347
  SRestoreCheckpointInfo req = {0};
211✔
1348

1349
  tDecoderInit(&decoder, (uint8_t*)msg, len);
211✔
1350
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
211!
1351
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1352
    tDecoderClear(&decoder);
×
1353
    return TSDB_CODE_SUCCESS;
×
1354
  }
1355

1356
  tDecoderClear(&decoder);
211✔
1357

1358
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
211✔
1359
  if (pTask == NULL || (code != 0)) {
211!
1360
    tqError("vgId:%d process consensus checkpointId req, failed to acquire task:0x%x, it may have been dropped already",
×
1361
            pMeta->vgId, req.taskId);
1362
    // ignore this code to avoid error code over write
1363
    int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
1364
    if (ret) {
×
1365
      tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1366
    }
1367

1368
    return 0;
×
1369
  }
1370

1371
  // discard the rsp, since it is expired.
1372
  if (req.startTs < pTask->execInfo.created) {
211✔
1373
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
2!
1374
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1375
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1376
           pTask->execInfo.created);
1377
    streamMetaAddFailedTaskSelf(pTask, now);
2✔
1378
    streamMetaReleaseTask(pMeta, pTask);
2✔
1379
    return TSDB_CODE_SUCCESS;
2✔
1380
  }
1381

1382
  tqDebug("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64 " from mnode",
209✔
1383
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId);
1384

1385
  streamMutexLock(&pTask->lock);
209✔
1386
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
209!
1387
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1388
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1389

1390
    streamMutexUnlock(&pTask->lock);
×
1391
    streamMetaReleaseTask(pMeta, pTask);
×
1392
    return 0;
×
1393
  }
1394

1395
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
209✔
1396
  if (pConsenInfo->consenChkptTransId >= req.transId) {
209!
1397
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1398
            pConsenInfo->consenChkptTransId, req.transId);
1399
    streamMutexUnlock(&pTask->lock);
×
1400
    streamMetaReleaseTask(pMeta, pTask);
×
1401
    return TSDB_CODE_SUCCESS;
×
1402
  }
1403

1404
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
209!
1405
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1406
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1407
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1408
    tqSetRestoreVersionInfo(pTask);
×
1409
  } else {
1410
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
209✔
1411
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1412
  }
1413

1414
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
209✔
1415
  streamMutexUnlock(&pTask->lock);
209✔
1416

1417
  if (pMeta->role == NODE_ROLE_LEADER) {
209!
1418
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
209✔
1419
    if (code) {
209!
1420
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1421
    }
1422
  } else {
1423
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1424
  }
1425

1426
  streamMetaReleaseTask(pMeta, pTask);
209✔
1427
  return 0;
209✔
1428
}
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