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

taosdata / TDengine / #3583

17 Jan 2025 07:28AM UTC coverage: 63.876% (+0.07%) from 63.803%
#3583

push

travis-ci

web-flow
Merge pull request #29594 from taosdata/fix/insert-when-2-replicas

fix/insert-when-2-replicas

141608 of 284535 branches covered (49.77%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

396 existing lines in 105 files now uncovered.

220075 of 281695 relevant lines covered (78.13%)

19864675.58 hits per line

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

63.48
/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,536✔
34
  SStreamMeta* pMeta = pTask->pMeta;
14,536✔
35
  int32_t      vgId = pMeta->vgId;
14,536✔
36
  int64_t      st = taosGetTimestampMs();
14,536✔
37
  int64_t      streamId = 0;
14,536✔
38
  int32_t      taskId = 0;
14,536✔
39
  int32_t      code = 0;
14,536✔
40

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

43
  if (pTask->info.fillHistory) {
14,536✔
44
    streamId = pTask->streamTaskId.streamId;
5,047✔
45
    taskId = pTask->streamTaskId.taskId;
5,047✔
46
  } else {
47
    streamId = pTask->id.streamId;
9,489✔
48
    taskId = pTask->id.taskId;
9,489✔
49
  }
50

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

62
  SReadHandle handle = {
14,537✔
63
      .checkpointId = pTask->chkInfo.checkpointId,
14,537✔
64
      .pStateBackend = pTask->pState,
14,537✔
65
      .fillHistory = pTask->info.fillHistory,
14,537✔
66
      .winRange = pTask->dataRange.window,
67
  };
68

69
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,537✔
70
    handle.vnode = ((STQ*)pMeta->ahandle)->pVnode;
7,266✔
71
    handle.initTqReader = 1;
7,266✔
72
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
7,271✔
73
    handle.numOfVgroups = (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList);
313✔
74
  }
75

76
  initStorageAPI(&handle.api);
14,537✔
77

78
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE || pTask->info.taskLevel == TASK_LEVEL__AGG) {
14,537✔
79
    code = qCreateStreamExecTaskInfo(&pTask->exec.pExecutor, pTask->exec.qmsg, &handle, vgId, pTask->id.taskId);
7,579✔
80
    if (code) {
7,577!
81
      tqError("s-task:%s failed to expand task, code:%s", pTask->id.idStr, tstrerror(code));
×
82
      return code;
×
83
    }
84

85
    code = qSetTaskId(pTask->exec.pExecutor, pTask->id.taskId, pTask->id.streamId);
7,577✔
86
    if (code) {
7,578!
87
      return code;
×
88
    }
89
  }
90

91
  streamSetupScheduleTrigger(pTask);
14,536✔
92

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

96
  return code;
14,536✔
97
}
98

99
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
15,017✔
100
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
15,017✔
101

102
  // checkpoint ver is the kept version, handled data should be the next version.
103
  if (pChkInfo->checkpointId != 0) {
15,017✔
104
    pChkInfo->nextProcessVer = pChkInfo->checkpointVer + 1;
257✔
105
    pChkInfo->processedVer = pChkInfo->checkpointVer;
257✔
106
    pTask->execInfo.startCheckpointId = pChkInfo->checkpointId;
257✔
107

108
    tqInfo("s-task:%s restore from the checkpointId:%" PRId64 " ver:%" PRId64 " currentVer:%" PRId64, pTask->id.idStr,
257!
109
           pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
110
  }
111

112
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
15,017✔
113
}
15,017✔
114

115
int32_t tqStreamTaskStartAsync(SStreamMeta* pMeta, SMsgCb* cb, bool restart) {
47✔
116
  int32_t vgId = pMeta->vgId;
47✔
117
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
47✔
118
  if (numOfTasks == 0) {
47!
119
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
120
    return 0;
×
121
  }
122

123
  tqDebug("vgId:%d start all %d stream task(s) async", vgId, numOfTasks);
47!
124

125
  int32_t type = restart ? STREAM_EXEC_T_RESTART_ALL_TASKS : STREAM_EXEC_T_START_ALL_TASKS;
47!
126
  return streamTaskSchedTask(cb, vgId, 0, 0, type);
47✔
127
}
128

129
int32_t tqStreamStartOneTaskAsync(SStreamMeta* pMeta, SMsgCb* cb, int64_t streamId, int32_t taskId) {
9,325✔
130
  int32_t vgId = pMeta->vgId;
9,325✔
131
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
9,325✔
132
  if (numOfTasks == 0) {
9,326!
133
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
134
    return 0;
×
135
  }
136

137
  tqDebug("vgId:%d start task:0x%x async", vgId, taskId);
9,326✔
138
  return streamTaskSchedTask(cb, vgId, streamId, taskId, STREAM_EXEC_T_START_ONE_TASK);
9,326✔
139
}
140

141
// this is to process request from transaction, always return true.
142
int32_t tqStreamTaskProcessUpdateReq(SStreamMeta* pMeta, SMsgCb* cb, SRpcMsg* pMsg, bool restored) {
103✔
143
  int32_t      vgId = pMeta->vgId;
103✔
144
  char*        msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
103✔
145
  int32_t      len = pMsg->contLen - sizeof(SMsgHead);
103✔
146
  SRpcMsg      rsp = {.info = pMsg->info, .code = TSDB_CODE_SUCCESS};
103✔
147
  int64_t      st = taosGetTimestampMs();
103✔
148
  bool         updated = false;
103✔
149
  int32_t      code = 0;
103✔
150
  SStreamTask* pTask = NULL;
103✔
151
  SStreamTask* pHTask = NULL;
103✔
152

153
  SStreamTaskNodeUpdateMsg req = {0};
103✔
154

155
  SDecoder decoder;
156
  tDecoderInit(&decoder, (uint8_t*)msg, len);
103✔
157
  if (tDecodeStreamTaskUpdateMsg(&decoder, &req) < 0) {
103!
158
    rsp.code = TSDB_CODE_MSG_DECODE_ERROR;
×
159
    tqError("vgId:%d failed to decode task update msg, code:%s", vgId, tstrerror(rsp.code));
×
160
    tDecoderClear(&decoder);
×
161
    return rsp.code;
×
162
  }
163

164
  tDecoderClear(&decoder);
103✔
165

166
  int32_t gError = streamGetFatalError(pMeta);
103✔
167
  if (gError != 0) {
103!
168
    tqError("vgId:%d global fatal occurs, code:%s, ts:%" PRId64 " func:%s", pMeta->vgId, tstrerror(gError),
×
169
            pMeta->fatalInfo.ts, pMeta->fatalInfo.func);
170
    return 0;
×
171
  }
172

173
  // update the nodeEpset when it exists
174
  streamMetaWLock(pMeta);
103✔
175

176
  // the task epset may be updated again and again, when replaying the WAL, the task may be in stop status.
177
  STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
103✔
178
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
103✔
179
  if (code != 0) {
103!
180
    tqError("vgId:%d failed to acquire task:0x%x when handling update task epset, it may have been dropped", vgId,
×
181
            req.taskId);
182
    rsp.code = TSDB_CODE_SUCCESS;
×
183
    streamMetaWUnLock(pMeta);
×
184
    taosArrayDestroy(req.pNodeList);
×
185
    return rsp.code;
×
186
  }
187

188
  const char* idstr = pTask->id.idStr;
103✔
189

190
  if (req.transId <= 0) {
103!
191
    tqError("vgId:%d invalid update nodeEp task, transId:%d, discard", vgId, req.taskId);
×
192
    rsp.code = TSDB_CODE_SUCCESS;
×
193

194
    streamMetaReleaseTask(pMeta, pTask);
×
195
    streamMetaWUnLock(pMeta);
×
196

197
    taosArrayDestroy(req.pNodeList);
×
198
    return rsp.code;
×
199
  }
200

201
  // info needs to be kept till the new trans to update the nodeEp arrived.
202
  bool update = streamMetaInitUpdateTaskList(pMeta, req.transId);
103✔
203
  if (!update) {
103!
204
    rsp.code = TSDB_CODE_SUCCESS;
×
205

206
    streamMetaReleaseTask(pMeta, pTask);
×
207
    streamMetaWUnLock(pMeta);
×
208

209
    taosArrayDestroy(req.pNodeList);
×
210
    return rsp.code;
×
211
  }
212

213
  // duplicate update epset msg received, discard this redundant message
214
  STaskUpdateEntry entry = {.streamId = req.streamId, .taskId = req.taskId, .transId = req.transId};
103✔
215

216
  void* pReqTask = taosHashGet(pMeta->updateInfo.pTasks, &entry, sizeof(STaskUpdateEntry));
103✔
217
  if (pReqTask != NULL) {
103!
218
    tqDebug("s-task:%s (vgId:%d) already update in transId:%d, discard the nodeEp update msg", idstr, vgId,
×
219
            req.transId);
220
    rsp.code = TSDB_CODE_SUCCESS;
×
221

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

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

229
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
103✔
230

231
  // send the checkpoint-source-rsp for source task to end the checkpoint trans in mnode
232
  code = streamTaskSendCheckpointsourceRsp(pTask);
103✔
233
  if (code) {
103!
234
    tqError("%s failed to send checkpoint-source rsp, code:%s", pTask->id.idStr, tstrerror(code));
×
235
  }
236
  streamTaskResetStatus(pTask);
103✔
237

238
  streamTaskStopMonitorCheckRsp(&pTask->taskCheckInfo, pTask->id.idStr);
103✔
239

240
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
103✔
241
    code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->hTaskInfo.id, &pHTask);
91✔
242
    if (code != 0) {
91!
243
      tqError(
×
244
          "vgId:%d failed to acquire fill-history task:0x%x when handling update, may have been dropped already, rel "
245
          "stream task:0x%x",
246
          vgId, (uint32_t)pTask->hTaskInfo.id.taskId, req.taskId);
247
      CLEAR_RELATED_FILLHISTORY_TASK(pTask);
×
248
    } else {
249
      tqDebug("s-task:%s fill-history task update nodeEp along with stream task", pHTask->id.idStr);
91!
250
      bool updateEpSet = streamTaskUpdateEpsetInfo(pHTask, req.pNodeList);
91✔
251
      if (updateEpSet) {
91✔
252
        updated = updateEpSet;
83✔
253
      }
254

255
      streamTaskResetStatus(pHTask);
91✔
256
      streamTaskStopMonitorCheckRsp(&pHTask->taskCheckInfo, pHTask->id.idStr);
91✔
257
    }
258
  }
259

260
  // stream do update the nodeEp info, write it into stream meta.
261
  if (updated) {
103✔
262
    tqDebug("s-task:%s vgId:%d save task after update epset, and stop task", idstr, vgId);
90!
263
    code = streamMetaSaveTask(pMeta, pTask);
90✔
264
    if (code) {
90!
265
      tqError("s-task:%s vgId:%d failed to save task, code:%s", idstr, vgId, tstrerror(code));
×
266
    }
267

268
    if (pHTask != NULL) {
90✔
269
      code = streamMetaSaveTask(pMeta, pHTask);
83✔
270
      if (code) {
83!
271
        tqError("s-task:%s vgId:%d failed to save related history task, code:%s", idstr, vgId, tstrerror(code));
×
272
      }
273
    }
274
  } else {
275
    tqDebug("s-task:%s vgId:%d not save task since not update epset actually, stop task", idstr, vgId);
13!
276
  }
277

278
  code = streamTaskStop(pTask);
103✔
279
  if (code) {
103!
280
    tqError("s-task:%s vgId:%d failed to stop task, code:%s", idstr, vgId, tstrerror(code));
×
281
  }
282

283
  if (pHTask != NULL) {
103✔
284
    code = streamTaskStop(pHTask);
91✔
285
    if (code) {
91!
286
      tqError("s-task:%s vgId:%d failed to stop related history task, code:%s", idstr, vgId, tstrerror(code));
×
287
    }
288
  }
289

290
  // keep info
291
  streamMetaAddIntoUpdateTaskList(pMeta, pTask, (pHTask != NULL) ? (pHTask) : NULL, req.transId, st);
103✔
292
  streamMetaReleaseTask(pMeta, pTask);
103✔
293
  streamMetaReleaseTask(pMeta, pHTask);
103✔
294

295
  rsp.code = TSDB_CODE_SUCCESS;
103✔
296

297
  // possibly only handle the stream task.
298
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
103✔
299
  int32_t updateTasks = taosHashGetSize(pMeta->updateInfo.pTasks);
103✔
300

301
  if (restored) {
103✔
302
    tqDebug("vgId:%d s-task:0x%x update epset transId:%d, set the restart flag", vgId, req.taskId, req.transId);
91!
303
    pMeta->startInfo.tasksWillRestart = 1;
91✔
304
  }
305

306
  if (updateTasks < numOfTasks) {
103✔
307
    tqDebug("vgId:%d closed tasks:%d, unclosed:%d, all tasks will be started when nodeEp update completed", vgId,
50!
308
            updateTasks, (numOfTasks - updateTasks));
309
  } else {
310
    if ((code = streamMetaCommit(pMeta)) < 0) {
53!
311
      // always return true
312
      streamMetaWUnLock(pMeta);
×
313
      taosArrayDestroy(req.pNodeList);
×
314
      return TSDB_CODE_SUCCESS;
×
315
    }
316

317
    streamMetaClearSetUpdateTaskListComplete(pMeta);
53✔
318

319
    if (!restored) {
53✔
320
      tqDebug("vgId:%d vnode restore not completed, not start all tasks", vgId);
6!
321
    } else {
322
      tqDebug("vgId:%d all %d task(s) nodeEp updated and closed, transId:%d", vgId, numOfTasks, req.transId);
47!
323
#if 0
324
      taosMSleep(5000);// for test purpose, to trigger the leader election
325
#endif
326
      code = tqStreamTaskStartAsync(pMeta, cb, true);
47✔
327
      if (code) {
47!
328
        tqError("vgId:%d async start all tasks, failed, code:%s", vgId, tstrerror(code));
×
329
      }
330
    }
331
  }
332

333
  streamMetaWUnLock(pMeta);
103✔
334
  taosArrayDestroy(req.pNodeList);
103✔
335
  return rsp.code;  // always return true
103✔
336
}
337

338
int32_t tqStreamTaskProcessDispatchReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
50,970✔
339
  char*   msgStr = pMsg->pCont;
50,970✔
340
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
50,970✔
341
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
50,970✔
342

343
  SStreamDispatchReq req = {0};
50,970✔
344

345
  SDecoder decoder;
346
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
50,970✔
347
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
50,964!
348
    tDecoderClear(&decoder);
×
349
    return TSDB_CODE_MSG_DECODE_ERROR;
×
350
  }
351
  tDecoderClear(&decoder);
50,951✔
352

353
  tqDebug("s-task:0x%x recv dispatch msg from 0x%x(vgId:%d)", req.taskId, req.upstreamTaskId, req.upstreamNodeId);
50,961✔
354

355
  SStreamTask* pTask = NULL;
50,961✔
356
  int32_t      code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
50,961✔
357
  if (pTask && (code == 0)) {
50,962!
358
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
50,889✔
359
    if (streamProcessDispatchMsg(pTask, &req, &rsp) != 0) {
50,889!
360
      return -1;
×
361
    }
362
    tCleanupStreamDispatchReq(&req);
50,884✔
363
    streamMetaReleaseTask(pMeta, pTask);
50,877✔
364
    return 0;
50,894✔
365
  } else {
366
    tqError("vgId:%d failed to find task:0x%x to handle the dispatch req, it may have been destroyed already",
73!
367
            pMeta->vgId, req.taskId);
368

369
    SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
73✔
370
    if (pRspHead == NULL) {
76!
371
      tqError("s-task:0x%x send dispatch error rsp, out of memory", req.taskId);
×
372
      return terrno;
×
373
    }
374

375
    pRspHead->vgId = htonl(req.upstreamNodeId);
76✔
376
    if (pRspHead->vgId == 0) {
76!
377
      tqError("vgId:%d invalid dispatch msg from upstream to task:0x%x", pMeta->vgId, req.taskId);
×
378
      return TSDB_CODE_INVALID_MSG;
×
379
    }
380

381
    SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
76✔
382
    pRsp->streamId = htobe64(req.streamId);
76✔
383
    pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
76✔
384
    pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
76✔
385
    pRsp->downstreamNodeId = htonl(pMeta->vgId);
76✔
386
    pRsp->downstreamTaskId = htonl(req.taskId);
76✔
387
    pRsp->msgId = htonl(req.msgId);
76✔
388
    pRsp->stage = htobe64(req.stage);
76✔
389
    pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;
76✔
390

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

395
    tmsgSendRsp(&rsp);
76✔
396
    tCleanupStreamDispatchReq(&req);
76✔
397

398
    return 0;
76✔
399
  }
400
}
401

402
int32_t tqStreamTaskProcessDispatchRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
50,970✔
403
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
50,970✔
404

405
  int32_t vgId = pMeta->vgId;
50,970✔
406
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
50,970✔
407
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
50,970✔
408
  pRsp->streamId = htobe64(pRsp->streamId);
50,970✔
409
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
50,972✔
410
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
50,972✔
411
  pRsp->stage = htobe64(pRsp->stage);
50,972✔
412
  pRsp->msgId = htonl(pRsp->msgId);
50,973✔
413

414
  tqDebug("s-task:0x%x vgId:%d recv dispatch-rsp from 0x%x vgId:%d", pRsp->upstreamTaskId, pRsp->upstreamNodeId,
50,973✔
415
          pRsp->downstreamTaskId, pRsp->downstreamNodeId);
416

417
  SStreamTask* pTask = NULL;
50,973✔
418
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
50,973✔
419
  if (pTask && (code == 0)) {
50,967!
420
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
50,897✔
421
    streamMetaReleaseTask(pMeta, pTask);
50,906✔
422
    return code;
50,903✔
423
  } else {
424
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
70✔
425
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
71✔
426
  }
427
}
428

429
int32_t tqStreamTaskProcessRetrieveReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
564✔
430
  char*    msgStr = pMsg->pCont;
564✔
431
  char*    msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
564✔
432
  int32_t  msgLen = pMsg->contLen - sizeof(SMsgHead);
564✔
433
  int32_t  code = 0;
564✔
434
  SDecoder decoder;
435

436
  SStreamRetrieveReq req;
437
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
564✔
438
  code = tDecodeStreamRetrieveReq(&decoder, &req);
564✔
439
  tDecoderClear(&decoder);
564✔
440

441
  if (code) {
565!
442
    tqError("vgId:%d failed to decode retrieve msg, discard it", pMeta->vgId);
×
443
    return code;
×
444
  }
445

446
  SStreamTask* pTask = NULL;
565✔
447
  code = streamMetaAcquireTask(pMeta, req.streamId, req.dstTaskId, &pTask);
565✔
448
  if (pTask == NULL || code != 0) {
565!
449
    tqError("vgId:%d process retrieve req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
450
            req.dstTaskId);
451
    tCleanupStreamRetrieveReq(&req);
×
452
    return code;
×
453
  }
454

455
  // enqueue
456
  tqDebug("s-task:%s (vgId:%d level:%d) recv retrieve req from task:0x%x(vgId:%d),QID:0x%" PRIx64, pTask->id.idStr,
565✔
457
          pTask->pMeta->vgId, pTask->info.taskLevel, req.srcTaskId, req.srcNodeId, req.reqId);
458

459
  // if task is in ck status, set current ck failed
460
  streamTaskSetCheckpointFailed(pTask);
565✔
461

462
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
565✔
463
    code = streamProcessRetrieveReq(pTask, &req);
559✔
464
  } else {
465
    req.srcNodeId = pTask->info.nodeId;
6✔
466
    req.srcTaskId = pTask->id.taskId;
6✔
467
    code = streamTaskBroadcastRetrieveReq(pTask, &req);
6✔
468
  }
469

470
  if (code != TSDB_CODE_SUCCESS) {  // return error not send rsp manually
565!
471
    tqError("s-task:0x%x vgId:%d failed to process retrieve request from 0x%x, code:%s", req.dstTaskId, req.dstNodeId,
×
472
            req.srcTaskId, tstrerror(code));
473
  } else {  // send rsp manually only on success.
474
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
565✔
475
    streamTaskSendRetrieveRsp(&req, &rsp);
565✔
476
  }
477

478
  streamMetaReleaseTask(pMeta, pTask);
565✔
479
  tCleanupStreamRetrieveReq(&req);
565✔
480

481
  // always return success, to disable the auto rsp
482
  return code;
565✔
483
}
484

485
int32_t tqStreamTaskProcessCheckReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
22,402✔
486
  char*   msgStr = pMsg->pCont;
22,402✔
487
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
22,402✔
488
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
22,402✔
489
  int32_t code = 0;
22,402✔
490

491
  SStreamTaskCheckReq req;
492
  SStreamTaskCheckRsp rsp = {0};
22,402✔
493

494
  SDecoder decoder;
495

496
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
22,402✔
497
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
22,397✔
498
  tDecoderClear(&decoder);
22,379✔
499

500
  if (code) {
22,386!
501
    tqError("vgId:%d decode check msg failed, not handle this msg", pMeta->vgId);
×
502
    return code;
×
503
  }
504

505
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
22,386✔
506
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
22,380✔
507
}
508

509
int32_t tqStreamTaskProcessCheckRsp(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
22,334✔
510
  char*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
22,334✔
511
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
22,334✔
512
  int32_t vgId = pMeta->vgId;
22,334✔
513
  int32_t code = TSDB_CODE_SUCCESS;
22,334✔
514

515
  SStreamTaskCheckRsp rsp;
516

517
  SDecoder decoder;
518
  tDecoderInit(&decoder, (uint8_t*)pReq, len);
22,334✔
519
  code = tDecodeStreamTaskCheckRsp(&decoder, &rsp);
22,327✔
520
  if (code < 0) {
22,282!
521
    terrno = TSDB_CODE_INVALID_MSG;
×
522
    tDecoderClear(&decoder);
×
523
    tqError("vgId:%d failed to parse check rsp msg, code:%s", vgId, tstrerror(terrno));
×
524
    return -1;
×
525
  }
526

527
  tDecoderClear(&decoder);
22,282✔
528
  tqDebug("tq task:0x%x (vgId:%d) recv check rsp(QID:0x%" PRIx64 ") from 0x%x (vgId:%d) status %d", rsp.upstreamTaskId,
22,320✔
529
          rsp.upstreamNodeId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.status);
530

531
  if (!isLeader) {
22,322!
532
    tqError("vgId:%d not leader, task:0x%x not handle the check rsp, downstream:0x%x (vgId:%d)", vgId,
×
533
            rsp.upstreamTaskId, rsp.downstreamTaskId, rsp.downstreamNodeId);
534
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
×
535
  }
536

537
  SStreamTask* pTask = NULL;
22,322✔
538
  code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.upstreamTaskId, &pTask);
22,322✔
539
  if ((pTask == NULL) || (code != 0)) {
22,325!
540
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
32✔
541
  }
542

543
  code = streamTaskProcessCheckRsp(pTask, &rsp);
22,293✔
544
  streamMetaReleaseTask(pMeta, pTask);
22,302✔
545
  return code;
22,306✔
546
}
547

548
int32_t tqStreamTaskProcessCheckpointReadyMsg(SStreamMeta* pMeta, SRpcMsg* pMsg) {
8,199✔
549
  int32_t vgId = pMeta->vgId;
8,199✔
550
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
8,199✔
551
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
8,199✔
552
  int32_t code = 0;
8,199✔
553

554
  SStreamCheckpointReadyMsg req = {0};
8,199✔
555

556
  SDecoder decoder;
557
  tDecoderInit(&decoder, (uint8_t*)msg, len);
8,199✔
558
  if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
8,190!
559
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
560
    tDecoderClear(&decoder);
×
561
    return code;
×
562
  }
563
  tDecoderClear(&decoder);
8,185✔
564

565
  SStreamTask* pTask = NULL;
8,206✔
566
  code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
8,206✔
567
  if (code != 0) {
8,193✔
568
    tqError("vgId:%d failed to find s-task:0x%x, it may have been destroyed already", vgId, req.downstreamTaskId);
5!
569
    return code;
5✔
570
  }
571

572
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
8,188!
573
    tqDebug("vgId:%d s-task:%s recv invalid the checkpoint-ready msg from task:0x%x (vgId:%d), discard", vgId,
×
574
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
575
    streamMetaReleaseTask(pMeta, pTask);
×
576
    return TSDB_CODE_INVALID_MSG;
×
577
  } else {
578
    tqDebug("vgId:%d s-task:%s received the checkpoint-ready msg from task:0x%x (vgId:%d), handle it", vgId,
8,188✔
579
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
580
  }
581

582
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
8,188✔
583
  streamMetaReleaseTask(pMeta, pTask);
8,200✔
584
  if (code) {
8,204!
585
    return code;
×
586
  }
587

588
  {  // send checkpoint ready rsp
589
    SMStreamCheckpointReadyRspMsg* pReadyRsp = rpcMallocCont(sizeof(SMStreamCheckpointReadyRspMsg));
8,204✔
590
    if (pReadyRsp == NULL) {
8,197!
591
      return terrno;
×
592
    }
593

594
    pReadyRsp->upstreamTaskId = req.upstreamTaskId;
8,197✔
595
    pReadyRsp->upstreamNodeId = req.upstreamNodeId;
8,197✔
596
    pReadyRsp->downstreamTaskId = req.downstreamTaskId;
8,197✔
597
    pReadyRsp->downstreamNodeId = req.downstreamNodeId;
8,197✔
598
    pReadyRsp->checkpointId = req.checkpointId;
8,197✔
599
    pReadyRsp->streamId = req.streamId;
8,197✔
600
    pReadyRsp->head.vgId = htonl(req.downstreamNodeId);
8,197✔
601

602
    SRpcMsg rsp = {.code = 0, .info = pMsg->info, .pCont = pReadyRsp, .contLen = sizeof(SMStreamCheckpointReadyRspMsg)};
8,197✔
603
    tmsgSendRsp(&rsp);
8,197✔
604

605
    pMsg->info.handle = NULL;  // disable auto rsp
8,203✔
606
  }
607

608
  return code;
8,203✔
609
}
610

611
int32_t tqStreamTaskProcessDeployReq(SStreamMeta* pMeta, SMsgCb* cb, int64_t sversion, char* msg, int32_t msgLen,
14,341✔
612
                                     bool isLeader, bool restored) {
613
  int32_t code = 0;
14,341✔
614
  int32_t vgId = pMeta->vgId;
14,341✔
615
  int32_t numOfTasks = 0;
14,341✔
616
  int32_t taskId = -1;
14,341✔
617
  int64_t streamId = -1;
14,341✔
618
  bool    added = false;
14,341✔
619
  int32_t size = sizeof(SStreamTask);
14,341✔
620

621
  if (tsDisableStream) {
14,341!
622
    tqInfo("vgId:%d stream disabled, not deploy stream tasks", vgId);
×
623
    return code;
×
624
  }
625

626
  tqDebug("vgId:%d receive new stream task deploy msg, start to build stream task", vgId);
14,341✔
627

628
  // 1.deserialize msg and build task
629
  SStreamTask* pTask = taosMemoryCalloc(1, size);
14,341!
630
  if (pTask == NULL) {
14,352!
631
    tqError("vgId:%d failed to create stream task due to out of memory, alloc size:%d", vgId, size);
×
632
    return terrno;
×
633
  }
634

635
  SDecoder decoder;
636
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
14,352✔
637
  code = tDecodeStreamTask(&decoder, pTask);
14,353✔
638
  tDecoderClear(&decoder);
14,356✔
639

640
  if (code != TSDB_CODE_SUCCESS) {
14,350!
641
    taosMemoryFree(pTask);
×
642
    return TSDB_CODE_INVALID_MSG;
×
643
  }
644

645
  // 2.save task, use the latest commit version as the initial start version of stream task.
646
  taskId = pTask->id.taskId;
14,350✔
647
  streamId = pTask->id.streamId;
14,350✔
648

649
  streamMetaWLock(pMeta);
14,350✔
650
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
14,355✔
651
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
14,359✔
652
  streamMetaWUnLock(pMeta);
14,360✔
653

654
  if (code < 0) {
14,361!
655
    tqError("vgId:%d failed to register s-task:0x%x into meta, existed tasks:%d, code:%s", vgId, taskId, numOfTasks,
×
656
            tstrerror(code));
657
    return code;
×
658
  }
659

660
  // added into meta store, pTask cannot be reference since it may have been destroyed by other threads already now if
661
  // it is added into the meta store
662
  if (added) {
14,361✔
663
    // only handled in the leader node
664
    if (isLeader) {
14,146✔
665
      tqDebug("vgId:%d s-task:0x%x is deployed and add into meta, numOfTasks:%d", vgId, taskId, numOfTasks);
14,093✔
666

667
      if (restored) {
14,090✔
668
        SStreamTask* p = NULL;
13,988✔
669
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
13,988✔
670
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
13,993!
671
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
9,155✔
672
        }
673

674
        if (p != NULL) {
13,993✔
675
          streamMetaReleaseTask(pMeta, p);
13,991✔
676
        }
677
      } else {
678
        tqWarn("s-task:0x%x not launched since vnode(vgId:%d) not ready", taskId, vgId);
102!
679
      }
680

681
    } else {
682
      tqDebug("vgId:%d not leader, not launch stream task s-task:0x%x", vgId, taskId);
53✔
683
    }
684
  } else {
685
    tqWarn("vgId:%d failed to add s-task:0x%x, since already exists in meta store, total:%d", vgId, taskId, numOfTasks);
215!
686
  }
687

688
  return code;
14,359✔
689
}
690

691
int32_t tqStreamTaskProcessDropReq(SStreamMeta* pMeta, char* msg, int32_t msgLen) {
7,009✔
692
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
7,009✔
693
  int32_t              code = 0;
7,009✔
694
  int32_t              vgId = pMeta->vgId;
7,009✔
695
  STaskId              hTaskId = {0};
7,009✔
696
  SStreamTask*         pTask = NULL;
7,009✔
697

698
  tqDebug("vgId:%d receive msg to drop s-task:0x%x", vgId, pReq->taskId);
7,009✔
699

700
  streamMetaWLock(pMeta);
7,009✔
701

702
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
7,031✔
703
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
7,031✔
704
  if (code == 0) {
7,025!
705
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
7,025✔
706
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
1,314✔
707
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
1,314✔
708
    }
709

710
    // clear the relationship, and then release the stream tasks, to avoid invalid accessing of already freed
711
    // related stream(history) task
712
    streamTaskSetRemoveBackendFiles(pTask);
7,025✔
713
    code = streamTaskClearHTaskAttr(pTask, pReq->resetRelHalt);
7,012✔
714
    streamMetaReleaseTask(pMeta, pTask);
7,019✔
715

716
    if (code) {
7,031✔
717
      tqError("s-task:0x%x failed to clear related fill-history info, still exists", pReq->taskId);
4!
718
    }
719
  }
720

721
  // drop the related fill-history task firstly
722
  if (hTaskId.taskId != 0 && hTaskId.streamId != 0) {
7,031✔
723
    tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x firstly", pReq->taskId, vgId, (int32_t)hTaskId.taskId);
1,258✔
724
    code = streamMetaUnregisterTask(pMeta, hTaskId.streamId, hTaskId.taskId);
1,258✔
725
    if (code) {
1,258!
726
      tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x failed", pReq->taskId, vgId,
×
727
              (int32_t)hTaskId.taskId);
728
    }
729
  }
730

731
  // drop the stream task now
732
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
7,031✔
733
  if (code) {
7,029!
734
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
735
  }
736

737
  // commit the update
738
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
7,029✔
739
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
7,031✔
740

741
  if (streamMetaCommit(pMeta) < 0) {
7,031✔
742
    // persist to disk
743
  }
744

745
  streamMetaWUnLock(pMeta);
7,035✔
746
  return 0;  // always return success
7,035✔
747
}
748

749
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
5,642✔
750
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
5,642✔
751
  int32_t                    code = 0;
5,642✔
752
  int32_t                    vgId = pMeta->vgId;
5,642✔
753
  SStreamTask*               pTask = NULL;
5,642✔
754

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

757
  streamMetaWLock(pMeta);
5,642✔
758

759
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
5,650✔
760
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
5,650✔
761
  if (code == 0) {
5,651!
762
    code = streamTaskUpdateTaskCheckpointInfo(pTask, restored, pReq);
5,651✔
763
    streamMetaReleaseTask(pMeta, pTask);
5,653✔
764
  } else {  // failed to get the task.
765
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
766
    tqError(
×
767
        "vgId:%d failed to locate the s-task:0x%x to update the checkpoint info, numOfTasks:%d, it may have been "
768
        "dropped already",
769
        vgId, pReq->taskId, numOfTasks);
770
  }
771

772
  streamMetaWUnLock(pMeta);
5,654✔
773
  // always return success when handling the requirement issued by mnode during transaction.
774
  return TSDB_CODE_SUCCESS;
5,654✔
775
}
776

777
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
49✔
778
  int32_t vgId = pMeta->vgId;
49✔
779
  int32_t code = 0;
49✔
780
  int64_t st = taosGetTimestampMs();
49✔
781

782
  streamMetaWLock(pMeta);
49✔
783
  if (pMeta->startInfo.startAllTasks == 1) {
49✔
784
    pMeta->startInfo.restartCount += 1;
2✔
785
    tqDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
2!
786
            pMeta->startInfo.restartCount);
787
    streamMetaWUnLock(pMeta);
2✔
788
    return TSDB_CODE_SUCCESS;
2✔
789
  }
790

791
  pMeta->startInfo.startAllTasks = 1;
47✔
792
  streamMetaWUnLock(pMeta);
47✔
793

794
  terrno = 0;
47✔
795
  tqInfo("vgId:%d tasks are all updated and stopped, restart all tasks, triggered by transId:%d, ts:%" PRId64, vgId,
47!
796
         pMeta->updateInfo.completeTransId, pMeta->updateInfo.completeTs);
797

798
  streamMetaWLock(pMeta);
47✔
799
  streamMetaClear(pMeta);
47✔
800

801
  int64_t el = taosGetTimestampMs() - st;
47✔
802
  tqInfo("vgId:%d close&reload state elapsed time:%.3fs", vgId, el / 1000.);
47!
803

804
  streamMetaLoadAllTasks(pMeta);
47✔
805

806
  {
807
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
47✔
808
    taosHashClear(pStartInfo->pReadyTaskSet);
47✔
809
    taosHashClear(pStartInfo->pFailedTaskSet);
47✔
810
    pStartInfo->readyTs = 0;
47✔
811
  }
812

813
  if (isLeader && !tsDisableStream) {
47!
814
    streamMetaWUnLock(pMeta);
41✔
815
    code = streamMetaStartAllTasks(pMeta);
41✔
816
  } else {
817
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
6✔
818
    pMeta->startInfo.restartCount = 0;
6✔
819
    streamMetaWUnLock(pMeta);
6✔
820
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
6!
821
  }
822

823
  code = terrno;
47✔
824
  return code;
47✔
825
}
826

827
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
102,837✔
828
  int32_t  code = 0;
102,837✔
829
  int32_t  vgId = pMeta->vgId;
102,837✔
830
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
102,837✔
831
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
102,837✔
832
  SDecoder decoder;
833

834
  SStreamTaskRunReq req = {0};
102,837✔
835
  tDecoderInit(&decoder, (uint8_t*)msg, len);
102,837✔
836
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
102,936!
837
    tqError("vgId:%d failed to decode task run req, code:%s", pMeta->vgId, tstrerror(code));
×
838
    tDecoderClear(&decoder);
×
839
    return TSDB_CODE_SUCCESS;
×
840
  }
841

842
  tDecoderClear(&decoder);
102,931✔
843

844
  int32_t type = req.reqType;
102,834✔
845
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
102,834✔
846
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
9,207✔
847
    return 0;
9,326✔
848
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
93,627✔
849
    code = streamMetaStartAllTasks(pMeta);
10,381✔
850
    return 0;
10,381✔
851
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
83,246✔
852
    code = restartStreamTasks(pMeta, isLeader);
47✔
853
    return 0;
47✔
854
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
83,199✔
855
    code = streamMetaStopAllTasks(pMeta);
4,734✔
856
    return 0;
4,734✔
857
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
78,465✔
858
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
2✔
859
    return code;
2✔
860
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
78,463✔
861
    SStreamTask* pTask = NULL;
10,112✔
862
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
10,112✔
863

864
    if (pTask != NULL && (code == 0)) {
10,111!
865
      char* pStatus = NULL;
10,104✔
866
      if (streamTaskReadyToRun(pTask, &pStatus)) {
10,104✔
867
        int64_t execTs = pTask->status.lastExecTs;
10,096✔
868
        int32_t idle = taosGetTimestampMs() - execTs;
10,100✔
869
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
10,100✔
870

871
        code = streamResumeTask(pTask);
10,100✔
872
      } else {
873
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
1✔
874
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
875
                pTask->id.idStr, pStatus, status);
876
      }
877
      streamMetaReleaseTask(pMeta, pTask);
10,103✔
878
    }
879

880
    return code;
10,113✔
881
  }
882

883
  SStreamTask* pTask = NULL;
68,351✔
884
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
68,351✔
885
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
68,329!
886
    char* p = NULL;
68,318✔
887
    if (streamTaskReadyToRun(pTask, &p)) {
68,318✔
888
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
68,233✔
889
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
890
      (void)streamExecTask(pTask);
68,233✔
891
    } else {
892
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
13✔
893
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
26!
894
              pTask->id.idStr, p, status);
895
    }
896

897
    streamMetaReleaseTask(pMeta, pTask);
68,301✔
898
    return 0;
68,323✔
899
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
900
    // todo add one function to handle this
901
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
11!
902
    return code;
31✔
903
  }
904
}
905

906
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
1,939✔
907
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
1,939✔
908
  int32_t         vgId = pMeta->vgId;
1,939✔
909
  bool            scanWal = false;
1,939✔
910
  int32_t         code = 0;
1,939✔
911

912
  streamMetaWLock(pMeta);
1,939✔
913
  if (pStartInfo->startAllTasks == 1) {
1,939!
914
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
915
            pMeta->startInfo.restartCount);
916
  } else {  // not in starting procedure
917
    bool allReady = streamMetaAllTasksReady(pMeta);
1,939✔
918

919
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
1,939!
920
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
921
      pStartInfo->restartCount -= 1;
2✔
922
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
2!
923
              pStartInfo->restartCount);
924
      streamMetaWUnLock(pMeta);
2✔
925

926
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
2✔
927
    } else {
928
      if (pStartInfo->restartCount == 0) {
1,937!
929
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
1,937✔
930
      } else if (allReady) {
×
931
        pStartInfo->restartCount = 0;
×
932
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
933
      }
934

935
      scanWal = true;
1,937✔
936
    }
937
  }
938

939
  streamMetaWUnLock(pMeta);
1,937✔
940

941
  if (scanWal && (vgId != SNODE_HANDLE)) {
1,937!
942
    tqDebug("vgId:%d start scan wal for executing tasks", vgId);
1,934✔
943
    code = tqScanWalAsync(pMeta->ahandle, true);
1,934✔
944
  }
945

946
  return code;
1,937✔
947
}
948

949
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
950
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
951

952
  SStreamTask* pTask = NULL;
×
953
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
954
  if (pTask == NULL || (code != 0)) {
×
955
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
956
            pMeta->vgId, pReq->taskId);
957
    return TSDB_CODE_SUCCESS;
×
958
  }
959

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

962
  streamMutexLock(&pTask->lock);
×
963
  streamTaskClearCheckInfo(pTask, true);
×
964

965
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
966

967
  // clear flag set during do checkpoint, and open inputQ for all upstream tasks
968
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
969
  if (pState.state == TASK_STATUS__CK) {
×
970
    streamTaskSetStatusReady(pTask);
×
971
    tqDebug("s-task:%s reset checkpoint status to ready", pTask->id.idStr);
×
972
  } else if (pState.state == TASK_STATUS__UNINIT) {
×
973
    //    tqDebug("s-task:%s start task by checking downstream tasks", pTask->id.idStr);
974
    //    tqStreamTaskRestoreCheckpoint(pMeta, pTask->id.streamId, pTask->id.taskId);
975
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
976
  } else {
977
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
978
  }
979

980
  streamMutexUnlock(&pTask->lock);
×
981

982
  streamMetaReleaseTask(pMeta, pTask);
×
983
  return TSDB_CODE_SUCCESS;
×
984
}
985

986
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
987
  SRetrieveChkptTriggerReq req = {0};
×
988
  SStreamTask*             pTask = NULL;
×
989
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
990
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
991
  SDecoder                 decoder = {0};
×
992

993
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
994
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
995
    tDecoderClear(&decoder);
×
996
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
997
    return TSDB_CODE_INVALID_MSG;
×
998
  }
999
  tDecoderClear(&decoder);
×
1000

1001
  int32_t code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
1002
  if (pTask == NULL || (code != 0)) {
×
1003
    tqError("vgId:%d process retrieve checkpoint-trigger, checkpointId:%" PRId64
×
1004
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
1005
            pMeta->vgId, req.checkpointId, (int32_t)req.downstreamTaskId, req.upstreamTaskId);
1006
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1007
  }
1008

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

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

1016
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1017
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
1018
    streamMetaReleaseTask(pMeta, pTask);
×
1019
    return code;
×
1020
  }
1021

1022
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1023
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1024
    int32_t transId = 0;
×
1025
    int64_t checkpointId = 0;
×
1026

1027
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
1028
    if (checkpointId != req.checkpointId) {
×
1029
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1030
              " req:%" PRId64,
1031
              pTask->id.idStr, req.downstreamTaskId, checkpointId, req.checkpointId);
1032
      streamMetaReleaseTask(pMeta, pTask);
×
1033
      return TSDB_CODE_INVALID_MSG;
×
1034
    }
1035

1036
    if (streamTaskAlreadySendTrigger(pTask, req.downstreamNodeId)) {
×
1037
      // re-send the lost checkpoint-trigger msg to downstream task
1038
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1039
              (int32_t)req.downstreamTaskId, checkpointId, transId);
1040
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1041
                                                TSDB_CODE_SUCCESS);
1042
    } else {  // not send checkpoint-trigger yet, wait
1043
      int32_t recv = 0, total = 0;
×
1044
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1045

1046
      if (recv == total) {  // add the ts info
×
1047
        tqWarn("s-task:%s all upstream send checkpoint-source/trigger, but not processed yet, wait", pTask->id.idStr);
×
1048
      } else {
1049
        tqWarn(
×
1050
            "s-task:%s not all upstream send checkpoint-source/trigger, total recv:%d/%d, wait for all upstream "
1051
            "sending checkpoint-source/trigger",
1052
            pTask->id.idStr, recv, total);
1053
      }
1054
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1055
                                                TSDB_CODE_ACTION_IN_PROGRESS);
1056
    }
1057
  } else {  // upstream not recv the checkpoint-source/trigger till now
1058
    if (!(pState.state == TASK_STATUS__READY || pState.state == TASK_STATUS__HALT)) {
×
1059
      tqFatal("s-task:%s invalid task status:%s", pTask->id.idStr, pState.name);
×
1060
    }
1061

1062
    tqWarn(
×
1063
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1064
        "upstream sending checkpoint-source/trigger",
1065
        pTask->id.idStr);
1066
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1067
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1068
  }
1069

1070
  streamMetaReleaseTask(pMeta, pTask);
×
1071
  return code;
×
1072
}
1073

1074
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1075
  SCheckpointTriggerRsp rsp = {0};
×
1076
  SStreamTask*          pTask = NULL;
×
1077
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1078
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
1079
  SDecoder              decoder = {0};
×
1080

1081
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1082
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1083
    tDecoderClear(&decoder);
×
1084
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1085
    return TSDB_CODE_INVALID_MSG;
×
1086
  }
1087
  tDecoderClear(&decoder);
×
1088

1089
  int32_t code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.taskId, &pTask);
×
1090
  if (pTask == NULL || (code != 0)) {
×
1091
    tqError(
×
1092
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1093
        pMeta->vgId, rsp.taskId);
1094
    return code;
×
1095
  }
1096

1097
  tqDebug(
×
1098
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1099
      ", transId:%d",
1100
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1101

1102
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
1103
  streamMetaReleaseTask(pMeta, pTask);
×
1104
  return code;
×
1105
}
1106

1107
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
1,410✔
1108
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
1,410✔
1109

1110
  SStreamTask* pTask = NULL;
1,410✔
1111
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,410✔
1112
  if (pTask == NULL || (code != 0)) {
1,414!
1113
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
1!
1114
            pReq->taskId);
1115
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1116
    return TSDB_CODE_SUCCESS;
×
1117
  }
1118

1119
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
1,413✔
1120
  streamTaskPause(pTask);
1,413✔
1121

1122
  SStreamTask* pHistoryTask = NULL;
1,414✔
1123
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
1,414✔
1124
    pHistoryTask = NULL;
72✔
1125
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
72✔
1126
    if (pHistoryTask == NULL || (code != 0)) {
72!
UNCOV
1127
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1128
              ", it may have been dropped already",
1129
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
UNCOV
1130
      streamMetaReleaseTask(pMeta, pTask);
×
1131

1132
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1133
      return TSDB_CODE_SUCCESS;
×
1134
    }
1135

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

1138
    streamTaskPause(pHistoryTask);
72✔
1139
    streamMetaReleaseTask(pMeta, pHistoryTask);
72✔
1140
  }
1141

1142
  streamMetaReleaseTask(pMeta, pTask);
1,414✔
1143
  return TSDB_CODE_SUCCESS;
1,414✔
1144
}
1145

1146
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
2,690✔
1147
                                       bool fromVnode) {
1148
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
2,690✔
1149
  int32_t      vgId = pMeta->vgId;
2,690✔
1150
  int32_t      code = 0;
2,690✔
1151

1152
  streamTaskResume(pTask);
2,690✔
1153
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,697✔
1154

1155
  int32_t level = pTask->info.taskLevel;
2,697✔
1156
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
2,697!
1157
    // no lock needs to secure the access of the version
1158
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
2,697!
1159
      // discard all the data  when the stream task is suspended.
1160
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
589✔
1161
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
589✔
1162
              ", schedStatus:%d",
1163
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1164
    } else {  // from the previous paused version and go on
1165
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
2,108✔
1166
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1167
    }
1168

1169
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
2,697✔
1170
      pTask->hTaskInfo.operatorOpen = false;
51✔
1171
      code = streamStartScanHistoryAsync(pTask, igUntreated);
51✔
1172
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
2,646✔
1173
      code = tqScanWalAsync((STQ*)handle, false);
1,292✔
1174
    } else {
1175
      code = streamTrySchedExec(pTask);
1,354✔
1176
    }
1177
  }
1178

1179
  return code;
2,696✔
1180
}
1181

1182
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
2,575✔
1183
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
2,575✔
1184

1185
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
2,575✔
1186

1187
  SStreamTask* pTask = NULL;
2,575✔
1188
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
2,575✔
1189
  if (pTask == NULL || (code != 0)) {
2,578!
1190
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1191
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1192
  }
1193

1194
  streamMutexLock(&pTask->lock);
2,579✔
1195
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,579✔
1196
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
2,579✔
1197
  streamMutexUnlock(&pTask->lock);
2,579✔
1198

1199
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
2,577✔
1200
  if (code != 0) {
2,582!
1201
    streamMetaReleaseTask(pMeta, pTask);
×
1202
    return code;
×
1203
  }
1204

1205
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
2,582✔
1206
  SStreamTask* pHTask = NULL;
2,582✔
1207
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
2,582✔
1208
  if (pHTask && (code == 0)) {
2,583!
1209
    streamMutexLock(&pHTask->lock);
114✔
1210
    SStreamTaskState p = streamTaskGetStatus(pHTask);
114✔
1211
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
114!
1212
    streamMutexUnlock(&pHTask->lock);
114✔
1213

1214
    code = tqProcessTaskResumeImpl(handle, pHTask, sversion, pReq->igUntreated, fromVnode);
114✔
1215
    tqDebug("s-task:%s resume complete, code:%s", pHTask->id.idStr, tstrerror(code));
114!
1216

1217
    streamMetaReleaseTask(pMeta, pHTask);
114✔
1218
  }
1219

1220
  return TSDB_CODE_SUCCESS;
2,583✔
1221
}
1222

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

1225
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
10,711✔
1226
  rpcFreeCont(pMsg->pCont);
10,711✔
1227
  pMsg->pCont = NULL;
10,710✔
1228
  return TSDB_CODE_SUCCESS;
10,710✔
1229
}
1230

1231
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
29,504✔
1232
  SMStreamHbRspMsg rsp = {0};
29,504✔
1233
  int32_t          code = 0;
29,504✔
1234
  SDecoder         decoder;
1235
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
29,504✔
1236
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
29,504✔
1237

1238
  tDecoderInit(&decoder, (uint8_t*)msg, len);
29,504✔
1239
  code = tDecodeStreamHbRsp(&decoder, &rsp);
29,491✔
1240
  if (code < 0) {
29,501!
1241
    terrno = TSDB_CODE_INVALID_MSG;
×
1242
    tDecoderClear(&decoder);
×
1243
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1244
    return terrno;
×
1245
  }
1246

1247
  tDecoderClear(&decoder);
29,501✔
1248
  return streamProcessHeartbeatRsp(pMeta, &rsp);
29,503✔
1249
}
1250

1251
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
4,463✔
1252

1253
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
6,248✔
1254

1255
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
8,209✔
1256
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
8,209✔
1257

1258
  SStreamTask* pTask = NULL;
8,209✔
1259
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
8,209✔
1260
  if (pTask == NULL || (code != 0)) {
8,206!
1261
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
4!
1262
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1263
    return code;
4✔
1264
  }
1265

1266
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
8,202✔
1267
  streamMetaReleaseTask(pMeta, pTask);
8,199✔
1268
  return code;
8,199✔
1269
}
1270

1271
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
175✔
1272
  int32_t                vgId = pMeta->vgId;
175✔
1273
  int32_t                code = 0;
175✔
1274
  SStreamTask*           pTask = NULL;
175✔
1275
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
175✔
1276
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
175✔
1277
  int64_t                now = taosGetTimestampMs();
175✔
1278
  SDecoder               decoder;
1279
  SRestoreCheckpointInfo req = {0};
175✔
1280

1281
  tDecoderInit(&decoder, (uint8_t*)msg, len);
175✔
1282
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
175!
1283
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1284
    tDecoderClear(&decoder);
×
1285
    return TSDB_CODE_SUCCESS;
×
1286
  }
1287

1288
  tDecoderClear(&decoder);
175✔
1289

1290
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
175✔
1291
  if (pTask == NULL || (code != 0)) {
175!
1292
    tqError("vgId:%d process consensus checkpointId req, failed to acquire task:0x%x, it may have been dropped already",
×
1293
            pMeta->vgId, req.taskId);
1294
    // ignore this code to avoid error code over write
1295
    int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
1296
    if (ret) {
×
1297
      tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1298
    }
1299

1300
    return 0;
×
1301
  }
1302

1303
  // discard the rsp, since it is expired.
1304
  if (req.startTs < pTask->execInfo.created) {
175✔
1305
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
4!
1306
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1307
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1308
           pTask->execInfo.created);
1309
    streamMetaAddFailedTaskSelf(pTask, now);
4✔
1310
    streamMetaReleaseTask(pMeta, pTask);
4✔
1311
    return TSDB_CODE_SUCCESS;
4✔
1312
  }
1313

1314
  tqDebug("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64 " from mnode",
171✔
1315
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId);
1316

1317
  streamMutexLock(&pTask->lock);
171✔
1318
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
171!
1319
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1320
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1321

1322
    streamMutexUnlock(&pTask->lock);
×
1323
    streamMetaReleaseTask(pMeta, pTask);
×
1324
    return 0;
×
1325
  }
1326

1327
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
171✔
1328
  if (pConsenInfo->consenChkptTransId >= req.transId) {
171!
1329
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1330
            pConsenInfo->consenChkptTransId, req.transId);
1331
    streamMutexUnlock(&pTask->lock);
×
1332
    streamMetaReleaseTask(pMeta, pTask);
×
1333
    return TSDB_CODE_SUCCESS;
×
1334
  }
1335

1336
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
171!
1337
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1338
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1339
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1340
    tqSetRestoreVersionInfo(pTask);
×
1341
  } else {
1342
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
171✔
1343
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1344
  }
1345

1346
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
171✔
1347
  streamMutexUnlock(&pTask->lock);
171✔
1348

1349
  if (pMeta->role == NODE_ROLE_LEADER) {
171!
1350
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
171✔
1351
    if (code) {
171!
1352
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1353
    }
1354
  } else {
1355
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1356
  }
1357

1358
  streamMetaReleaseTask(pMeta, pTask);
171✔
1359
  return 0;
171✔
1360
}
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