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

taosdata / TDengine / #3588

24 Jan 2025 08:57AM UTC coverage: 63.555% (+0.006%) from 63.549%
#3588

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

141359 of 285622 branches covered (49.49%)

Branch coverage included in aggregate %.

219930 of 282844 relevant lines covered (77.76%)

19382902.66 hits per line

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

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

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

43
  if (pTask->info.fillHistory) {
11,897✔
44
    streamId = pTask->streamTaskId.streamId;
4,142✔
45
    taskId = pTask->streamTaskId.taskId;
4,142✔
46
  } else {
47
    streamId = pTask->id.streamId;
7,755✔
48
    taskId = pTask->id.taskId;
7,755✔
49
  }
50

51
  // sink task does not need the pState
52
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
11,897✔
53
    pTask->pState = streamStateOpen(pMeta->path, pTask, streamId, taskId);
6,251✔
54
    if (pTask->pState == NULL) {
6,252!
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);
6,252✔
59
    }
60
  }
61

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

69
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
11,897✔
70
    handle.vnode = ((STQ*)pMeta->ahandle)->pVnode;
5,988✔
71
    handle.initTqReader = 1;
5,988✔
72
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
5,909✔
73
    handle.numOfVgroups = (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList);
264✔
74
  }
75

76
  initStorageAPI(&handle.api);
11,897✔
77

78
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE || pTask->info.taskLevel == TASK_LEVEL__AGG) {
11,897✔
79
    code = qCreateStreamExecTaskInfo(&pTask->exec.pExecutor, pTask->exec.qmsg, &handle, vgId, pTask->id.taskId);
6,252✔
80
    if (code) {
6,251!
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);
6,251✔
86
    if (code) {
6,251!
87
      return code;
×
88
    }
89

90
    code =
91
        qSetStreamNotifyInfo(pTask->exec.pExecutor, pTask->notifyInfo.notifyEventTypes,
6,251✔
92
                             pTask->notifyInfo.pSchemaWrapper, pTask->notifyInfo.stbFullName, IS_NEW_SUBTB_RULE(pTask));
6,251!
93
    if (code) {
6,252!
94
      tqError("s-task:%s failed to set stream notify info, code:%s", pTask->id.idStr, tstrerror(code));
×
95
      return code;
×
96
    }
97
  }
98

99
  streamSetupScheduleTrigger(pTask);
11,897✔
100

101
  double el = (taosGetTimestampMs() - st) / 1000.0;
11,896✔
102
  tqDebug("s-task:%s vgId:%d expand stream task completed, elapsed time:%.2fsec", pTask->id.idStr, vgId, el);
11,896✔
103

104
  return code;
11,896✔
105
}
106

107
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
12,164✔
108
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
12,164✔
109

110
  // checkpoint ver is the kept version, handled data should be the next version.
111
  if (pChkInfo->checkpointId != 0) {
12,164✔
112
    pChkInfo->nextProcessVer = pChkInfo->checkpointVer + 1;
208✔
113
    pChkInfo->processedVer = pChkInfo->checkpointVer;
208✔
114
    pTask->execInfo.startCheckpointId = pChkInfo->checkpointId;
208✔
115

116
    tqInfo("s-task:%s restore from the checkpointId:%" PRId64 " ver:%" PRId64 " currentVer:%" PRId64, pTask->id.idStr,
208!
117
           pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
118
  }
119

120
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
12,164✔
121
}
12,164✔
122

123
int32_t tqStreamTaskStartAsync(SStreamMeta* pMeta, SMsgCb* cb, bool restart) {
44✔
124
  int32_t vgId = pMeta->vgId;
44✔
125
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
44✔
126
  if (numOfTasks == 0) {
44!
127
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
128
    return 0;
×
129
  }
130

131
  tqDebug("vgId:%d start all %d stream task(s) async", vgId, numOfTasks);
44✔
132

133
  int32_t type = restart ? STREAM_EXEC_T_RESTART_ALL_TASKS : STREAM_EXEC_T_START_ALL_TASKS;
44!
134
  return streamTaskSchedTask(cb, vgId, 0, 0, type);
44✔
135
}
136

137
int32_t tqStreamStartOneTaskAsync(SStreamMeta* pMeta, SMsgCb* cb, int64_t streamId, int32_t taskId) {
7,670✔
138
  int32_t vgId = pMeta->vgId;
7,670✔
139
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
7,670✔
140
  if (numOfTasks == 0) {
7,670!
141
    tqDebug("vgId:%d no stream tasks existed to run", vgId);
×
142
    return 0;
×
143
  }
144

145
  tqDebug("vgId:%d start task:0x%x async", vgId, taskId);
7,670✔
146
  return streamTaskSchedTask(cb, vgId, streamId, taskId, STREAM_EXEC_T_START_ONE_TASK);
7,670✔
147
}
148

149
// this is to process request from transaction, always return true.
150
int32_t tqStreamTaskProcessUpdateReq(SStreamMeta* pMeta, SMsgCb* cb, SRpcMsg* pMsg, bool restored) {
99✔
151
  int32_t      vgId = pMeta->vgId;
99✔
152
  char*        msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
99✔
153
  int32_t      len = pMsg->contLen - sizeof(SMsgHead);
99✔
154
  SRpcMsg      rsp = {.info = pMsg->info, .code = TSDB_CODE_SUCCESS};
99✔
155
  int64_t      st = taosGetTimestampMs();
99✔
156
  bool         updated = false;
99✔
157
  int32_t      code = 0;
99✔
158
  SStreamTask* pTask = NULL;
99✔
159
  SStreamTask* pHTask = NULL;
99✔
160

161
  SStreamTaskNodeUpdateMsg req = {0};
99✔
162

163
  SDecoder decoder;
164
  tDecoderInit(&decoder, (uint8_t*)msg, len);
99✔
165
  if (tDecodeStreamTaskUpdateMsg(&decoder, &req) < 0) {
99!
166
    rsp.code = TSDB_CODE_MSG_DECODE_ERROR;
×
167
    tqError("vgId:%d failed to decode task update msg, code:%s", vgId, tstrerror(rsp.code));
×
168
    tDecoderClear(&decoder);
×
169
    return rsp.code;
×
170
  }
171

172
  tDecoderClear(&decoder);
99✔
173

174
  int32_t gError = streamGetFatalError(pMeta);
99✔
175
  if (gError != 0) {
99!
176
    tqError("vgId:%d global fatal occurs, code:%s, ts:%" PRId64 " func:%s", pMeta->vgId, tstrerror(gError),
×
177
            pMeta->fatalInfo.ts, pMeta->fatalInfo.func);
178
    return 0;
×
179
  }
180

181
  // update the nodeEpset when it exists
182
  streamMetaWLock(pMeta);
99✔
183

184
  // the task epset may be updated again and again, when replaying the WAL, the task may be in stop status.
185
  STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
99✔
186
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
99✔
187
  if (code != 0) {
99!
188
    tqError("vgId:%d failed to acquire task:0x%x when handling update task epset, it may have been dropped", vgId,
×
189
            req.taskId);
190
    rsp.code = TSDB_CODE_SUCCESS;
×
191
    streamMetaWUnLock(pMeta);
×
192
    taosArrayDestroy(req.pNodeList);
×
193
    return rsp.code;
×
194
  }
195

196
  const char* idstr = pTask->id.idStr;
99✔
197

198
  if (req.transId <= 0) {
99!
199
    tqError("vgId:%d invalid update nodeEp task, transId:%d, discard", vgId, req.taskId);
×
200
    rsp.code = TSDB_CODE_SUCCESS;
×
201

202
    streamMetaReleaseTask(pMeta, pTask);
×
203
    streamMetaWUnLock(pMeta);
×
204

205
    taosArrayDestroy(req.pNodeList);
×
206
    return rsp.code;
×
207
  }
208

209
  // info needs to be kept till the new trans to update the nodeEp arrived.
210
  bool update = streamMetaInitUpdateTaskList(pMeta, req.transId);
99✔
211
  if (!update) {
99!
212
    rsp.code = TSDB_CODE_SUCCESS;
×
213

214
    streamMetaReleaseTask(pMeta, pTask);
×
215
    streamMetaWUnLock(pMeta);
×
216

217
    taosArrayDestroy(req.pNodeList);
×
218
    return rsp.code;
×
219
  }
220

221
  // duplicate update epset msg received, discard this redundant message
222
  STaskUpdateEntry entry = {.streamId = req.streamId, .taskId = req.taskId, .transId = req.transId};
99✔
223

224
  void* pReqTask = taosHashGet(pMeta->updateInfo.pTasks, &entry, sizeof(STaskUpdateEntry));
99✔
225
  if (pReqTask != NULL) {
99!
226
    tqDebug("s-task:%s (vgId:%d) already update in transId:%d, discard the nodeEp update msg", idstr, vgId,
×
227
            req.transId);
228
    rsp.code = TSDB_CODE_SUCCESS;
×
229

230
    streamMetaReleaseTask(pMeta, pTask);
×
231
    streamMetaWUnLock(pMeta);
×
232

233
    taosArrayDestroy(req.pNodeList);
×
234
    return rsp.code;
×
235
  }
236

237
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
99✔
238

239
  // send the checkpoint-source-rsp for source task to end the checkpoint trans in mnode
240
  code = streamTaskSendCheckpointsourceRsp(pTask);
99✔
241
  if (code) {
99!
242
    tqError("%s failed to send checkpoint-source rsp, code:%s", pTask->id.idStr, tstrerror(code));
×
243
  }
244
  streamTaskResetStatus(pTask);
99✔
245

246
  streamTaskStopMonitorCheckRsp(&pTask->taskCheckInfo, pTask->id.idStr);
99✔
247

248
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
99✔
249
    code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->hTaskInfo.id, &pHTask);
46✔
250
    if (code != 0) {
46!
251
      tqError(
×
252
          "vgId:%d failed to acquire fill-history task:0x%x when handling update, may have been dropped already, rel "
253
          "stream task:0x%x",
254
          vgId, (uint32_t)pTask->hTaskInfo.id.taskId, req.taskId);
255
      CLEAR_RELATED_FILLHISTORY_TASK(pTask);
×
256
    } else {
257
      tqDebug("s-task:%s fill-history task update nodeEp along with stream task", pHTask->id.idStr);
46!
258
      bool updateEpSet = streamTaskUpdateEpsetInfo(pHTask, req.pNodeList);
46✔
259
      if (updateEpSet) {
46✔
260
        updated = updateEpSet;
42✔
261
      }
262

263
      streamTaskResetStatus(pHTask);
46✔
264
      streamTaskStopMonitorCheckRsp(&pHTask->taskCheckInfo, pHTask->id.idStr);
46✔
265
    }
266
  }
267

268
  // stream do update the nodeEp info, write it into stream meta.
269
  if (updated) {
99✔
270
    tqDebug("s-task:%s vgId:%d save task after update epset, and stop task", idstr, vgId);
84✔
271
    code = streamMetaSaveTask(pMeta, pTask);
84✔
272
    if (code) {
84!
273
      tqError("s-task:%s vgId:%d failed to save task, code:%s", idstr, vgId, tstrerror(code));
×
274
    }
275

276
    if (pHTask != NULL) {
84✔
277
      code = streamMetaSaveTask(pMeta, pHTask);
42✔
278
      if (code) {
42!
279
        tqError("s-task:%s vgId:%d failed to save related history task, code:%s", idstr, vgId, tstrerror(code));
×
280
      }
281
    }
282
  } else {
283
    tqDebug("s-task:%s vgId:%d not save task since not update epset actually, stop task", idstr, vgId);
15!
284
  }
285

286
  code = streamTaskStop(pTask);
99✔
287
  if (code) {
99!
288
    tqError("s-task:%s vgId:%d failed to stop task, code:%s", idstr, vgId, tstrerror(code));
×
289
  }
290

291
  if (pHTask != NULL) {
99✔
292
    code = streamTaskStop(pHTask);
46✔
293
    if (code) {
46!
294
      tqError("s-task:%s vgId:%d failed to stop related history task, code:%s", idstr, vgId, tstrerror(code));
×
295
    }
296
  }
297

298
  // keep info
299
  streamMetaAddIntoUpdateTaskList(pMeta, pTask, (pHTask != NULL) ? (pHTask) : NULL, req.transId, st);
99✔
300
  streamMetaReleaseTask(pMeta, pTask);
99✔
301
  streamMetaReleaseTask(pMeta, pHTask);
99✔
302

303
  rsp.code = TSDB_CODE_SUCCESS;
99✔
304

305
  // possibly only handle the stream task.
306
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
99✔
307
  int32_t updateTasks = taosHashGetSize(pMeta->updateInfo.pTasks);
99✔
308

309
  if (restored) {
99✔
310
    tqDebug("vgId:%d s-task:0x%x update epset transId:%d, set the restart flag", vgId, req.taskId, req.transId);
85✔
311
    pMeta->startInfo.tasksWillRestart = 1;
85✔
312
  }
313

314
  if (updateTasks < numOfTasks) {
99✔
315
    tqDebug("vgId:%d closed tasks:%d, unclosed:%d, all tasks will be started when nodeEp update completed", vgId,
48✔
316
            updateTasks, (numOfTasks - updateTasks));
317
  } else {
318
    if ((code = streamMetaCommit(pMeta)) < 0) {
51!
319
      // always return true
320
      streamMetaWUnLock(pMeta);
×
321
      taosArrayDestroy(req.pNodeList);
×
322
      return TSDB_CODE_SUCCESS;
×
323
    }
324

325
    streamMetaClearSetUpdateTaskListComplete(pMeta);
51✔
326

327
    if (!restored) {
51✔
328
      tqDebug("vgId:%d vnode restore not completed, not start all tasks", vgId);
7!
329
    } else {
330
      tqDebug("vgId:%d all %d task(s) nodeEp updated and closed, transId:%d", vgId, numOfTasks, req.transId);
44✔
331
#if 0
332
      taosMSleep(5000);// for test purpose, to trigger the leader election
333
#endif
334
      code = tqStreamTaskStartAsync(pMeta, cb, true);
44✔
335
      if (code) {
44!
336
        tqError("vgId:%d async start all tasks, failed, code:%s", vgId, tstrerror(code));
×
337
      }
338
    }
339
  }
340

341
  streamMetaWUnLock(pMeta);
99✔
342
  taosArrayDestroy(req.pNodeList);
99✔
343
  return rsp.code;  // always return true
99✔
344
}
345

346
int32_t tqStreamTaskProcessDispatchReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
49,539✔
347
  char*   msgStr = pMsg->pCont;
49,539✔
348
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
49,539✔
349
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
49,539✔
350

351
  SStreamDispatchReq req = {0};
49,539✔
352

353
  SDecoder decoder;
354
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
49,539✔
355
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
49,534!
356
    tDecoderClear(&decoder);
×
357
    return TSDB_CODE_MSG_DECODE_ERROR;
×
358
  }
359
  tDecoderClear(&decoder);
49,530✔
360

361
  tqDebug("s-task:0x%x recv dispatch msg from 0x%x(vgId:%d)", req.taskId, req.upstreamTaskId, req.upstreamNodeId);
49,532✔
362

363
  SStreamTask* pTask = NULL;
49,532✔
364
  int32_t      code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
49,532✔
365
  if (pTask && (code == 0)) {
49,525!
366
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
49,375✔
367
    if (streamProcessDispatchMsg(pTask, &req, &rsp) != 0) {
49,375✔
368
      return -1;
2✔
369
    }
370
    tCleanupStreamDispatchReq(&req);
49,370✔
371
    streamMetaReleaseTask(pMeta, pTask);
49,365✔
372
    return 0;
49,377✔
373
  } else {
374
    tqError("vgId:%d failed to find task:0x%x to handle the dispatch req, it may have been destroyed already",
150!
375
            pMeta->vgId, req.taskId);
376

377
    SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
150✔
378
    if (pRspHead == NULL) {
156!
379
      tqError("s-task:0x%x send dispatch error rsp, out of memory", req.taskId);
×
380
      return terrno;
×
381
    }
382

383
    pRspHead->vgId = htonl(req.upstreamNodeId);
156✔
384
    if (pRspHead->vgId == 0) {
156!
385
      tqError("vgId:%d invalid dispatch msg from upstream to task:0x%x", pMeta->vgId, req.taskId);
×
386
      return TSDB_CODE_INVALID_MSG;
×
387
    }
388

389
    SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
156✔
390
    pRsp->streamId = htobe64(req.streamId);
156✔
391
    pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
156✔
392
    pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
156✔
393
    pRsp->downstreamNodeId = htonl(pMeta->vgId);
156✔
394
    pRsp->downstreamTaskId = htonl(req.taskId);
156✔
395
    pRsp->msgId = htonl(req.msgId);
156✔
396
    pRsp->stage = htobe64(req.stage);
156✔
397
    pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;
156✔
398

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

403
    tmsgSendRsp(&rsp);
156✔
404
    tCleanupStreamDispatchReq(&req);
156✔
405

406
    return 0;
156✔
407
  }
408
}
409

410
int32_t tqStreamTaskProcessDispatchRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
49,537✔
411
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
49,537✔
412

413
  int32_t vgId = pMeta->vgId;
49,537✔
414
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
49,537✔
415
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
49,537✔
416
  pRsp->streamId = htobe64(pRsp->streamId);
49,537✔
417
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
49,536✔
418
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
49,536✔
419
  pRsp->stage = htobe64(pRsp->stage);
49,536✔
420
  pRsp->msgId = htonl(pRsp->msgId);
49,536✔
421

422
  tqDebug("s-task:0x%x vgId:%d recv dispatch-rsp from 0x%x vgId:%d", pRsp->upstreamTaskId, pRsp->upstreamNodeId,
49,536✔
423
          pRsp->downstreamTaskId, pRsp->downstreamNodeId);
424

425
  SStreamTask* pTask = NULL;
49,536✔
426
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
49,536✔
427
  if (pTask && (code == 0)) {
49,535!
428
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
49,499✔
429
    streamMetaReleaseTask(pMeta, pTask);
49,499✔
430
    return code;
49,502✔
431
  } else {
432
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
36!
433
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
37✔
434
  }
435
}
436

437
int32_t tqStreamTaskProcessRetrieveReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
486✔
438
  char*    msgStr = pMsg->pCont;
486✔
439
  char*    msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
486✔
440
  int32_t  msgLen = pMsg->contLen - sizeof(SMsgHead);
486✔
441
  int32_t  code = 0;
486✔
442
  SDecoder decoder;
443

444
  SStreamRetrieveReq req;
445
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
486✔
446
  code = tDecodeStreamRetrieveReq(&decoder, &req);
486✔
447
  tDecoderClear(&decoder);
486✔
448

449
  if (code) {
486!
450
    tqError("vgId:%d failed to decode retrieve msg, discard it", pMeta->vgId);
×
451
    return code;
×
452
  }
453

454
  SStreamTask* pTask = NULL;
486✔
455
  code = streamMetaAcquireTask(pMeta, req.streamId, req.dstTaskId, &pTask);
486✔
456
  if (pTask == NULL || code != 0) {
486!
457
    tqError("vgId:%d process retrieve req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
458
            req.dstTaskId);
459
    tCleanupStreamRetrieveReq(&req);
×
460
    return code;
×
461
  }
462

463
  // enqueue
464
  tqDebug("s-task:%s (vgId:%d level:%d) recv retrieve req from task:0x%x(vgId:%d),QID:0x%" PRIx64, pTask->id.idStr,
486✔
465
          pTask->pMeta->vgId, pTask->info.taskLevel, req.srcTaskId, req.srcNodeId, req.reqId);
466

467
  // if task is in ck status, set current ck failed
468
  streamTaskSetCheckpointFailed(pTask);
486✔
469

470
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
486✔
471
    code = streamProcessRetrieveReq(pTask, &req);
480✔
472
  } else {
473
    req.srcNodeId = pTask->info.nodeId;
6✔
474
    req.srcTaskId = pTask->id.taskId;
6✔
475
    code = streamTaskBroadcastRetrieveReq(pTask, &req);
6✔
476
  }
477

478
  if (code != TSDB_CODE_SUCCESS) {  // return error not send rsp manually
485!
479
    tqError("s-task:0x%x vgId:%d failed to process retrieve request from 0x%x, code:%s", req.dstTaskId, req.dstNodeId,
×
480
            req.srcTaskId, tstrerror(code));
481
  } else {  // send rsp manually only on success.
482
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
485✔
483
    streamTaskSendRetrieveRsp(&req, &rsp);
485✔
484
  }
485

486
  streamMetaReleaseTask(pMeta, pTask);
486✔
487
  tCleanupStreamRetrieveReq(&req);
486✔
488

489
  // always return success, to disable the auto rsp
490
  return code;
486✔
491
}
492

493
int32_t tqStreamTaskProcessCheckReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
18,368✔
494
  char*   msgStr = pMsg->pCont;
18,368✔
495
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
18,368✔
496
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
18,368✔
497
  int32_t code = 0;
18,368✔
498

499
  SStreamTaskCheckReq req;
500
  SStreamTaskCheckRsp rsp = {0};
18,368✔
501

502
  SDecoder decoder;
503

504
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
18,368✔
505
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
18,368✔
506
  tDecoderClear(&decoder);
18,352✔
507

508
  if (code) {
18,360!
509
    tqError("vgId:%d decode check msg failed, not handle this msg", pMeta->vgId);
×
510
    return code;
×
511
  }
512

513
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
18,360✔
514
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
18,353✔
515
}
516

517
int32_t tqStreamTaskProcessCheckRsp(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
18,554✔
518
  char*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
18,554✔
519
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
18,554✔
520
  int32_t vgId = pMeta->vgId;
18,554✔
521
  int32_t code = TSDB_CODE_SUCCESS;
18,554✔
522

523
  SStreamTaskCheckRsp rsp;
524

525
  SDecoder decoder;
526
  tDecoderInit(&decoder, (uint8_t*)pReq, len);
18,554✔
527
  code = tDecodeStreamTaskCheckRsp(&decoder, &rsp);
18,551✔
528
  if (code < 0) {
18,535!
529
    terrno = TSDB_CODE_INVALID_MSG;
×
530
    tDecoderClear(&decoder);
×
531
    tqError("vgId:%d failed to parse check rsp msg, code:%s", vgId, tstrerror(terrno));
×
532
    return -1;
×
533
  }
534

535
  tDecoderClear(&decoder);
18,535✔
536
  tqDebug("tq task:0x%x (vgId:%d) recv check rsp(QID:0x%" PRIx64 ") from 0x%x (vgId:%d) status %d", rsp.upstreamTaskId,
18,544✔
537
          rsp.upstreamNodeId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.status);
538

539
  if (!isLeader) {
18,543✔
540
    tqError("vgId:%d not leader, task:0x%x not handle the check rsp, downstream:0x%x (vgId:%d)", vgId,
3!
541
            rsp.upstreamTaskId, rsp.downstreamTaskId, rsp.downstreamNodeId);
542
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
3✔
543
  }
544

545
  SStreamTask* pTask = NULL;
18,540✔
546
  code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.upstreamTaskId, &pTask);
18,540✔
547
  if ((pTask == NULL) || (code != 0)) {
18,550✔
548
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
43✔
549
  }
550

551
  code = streamTaskProcessCheckRsp(pTask, &rsp);
18,507✔
552
  streamMetaReleaseTask(pMeta, pTask);
18,514✔
553
  return code;
18,515✔
554
}
555

556
int32_t tqStreamTaskProcessCheckpointReadyMsg(SStreamMeta* pMeta, SRpcMsg* pMsg) {
7,069✔
557
  int32_t vgId = pMeta->vgId;
7,069✔
558
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
7,069✔
559
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
7,069✔
560
  int32_t code = 0;
7,069✔
561

562
  SStreamCheckpointReadyMsg req = {0};
7,069✔
563

564
  SDecoder decoder;
565
  tDecoderInit(&decoder, (uint8_t*)msg, len);
7,069✔
566
  if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
7,067!
567
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
568
    tDecoderClear(&decoder);
×
569
    return code;
×
570
  }
571
  tDecoderClear(&decoder);
7,059✔
572

573
  SStreamTask* pTask = NULL;
7,072✔
574
  code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
7,072✔
575
  if (code != 0) {
7,066✔
576
    tqError("vgId:%d failed to find s-task:0x%x, it may have been destroyed already", vgId, req.downstreamTaskId);
6!
577
    return code;
6✔
578
  }
579

580
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
7,060!
581
    tqDebug("vgId:%d s-task:%s recv invalid the checkpoint-ready msg from task:0x%x (vgId:%d), discard", vgId,
×
582
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
583
    streamMetaReleaseTask(pMeta, pTask);
×
584
    return TSDB_CODE_INVALID_MSG;
×
585
  } else {
586
    tqDebug("vgId:%d s-task:%s received the checkpoint-ready msg from task:0x%x (vgId:%d), handle it", vgId,
7,060✔
587
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
588
  }
589

590
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
7,060✔
591
  streamMetaReleaseTask(pMeta, pTask);
7,056✔
592
  if (code) {
7,063!
593
    return code;
×
594
  }
595

596
  {  // send checkpoint ready rsp
597
    SMStreamCheckpointReadyRspMsg* pReadyRsp = rpcMallocCont(sizeof(SMStreamCheckpointReadyRspMsg));
7,063✔
598
    if (pReadyRsp == NULL) {
7,063!
599
      return terrno;
×
600
    }
601

602
    pReadyRsp->upstreamTaskId = req.upstreamTaskId;
7,063✔
603
    pReadyRsp->upstreamNodeId = req.upstreamNodeId;
7,063✔
604
    pReadyRsp->downstreamTaskId = req.downstreamTaskId;
7,063✔
605
    pReadyRsp->downstreamNodeId = req.downstreamNodeId;
7,063✔
606
    pReadyRsp->checkpointId = req.checkpointId;
7,063✔
607
    pReadyRsp->streamId = req.streamId;
7,063✔
608
    pReadyRsp->head.vgId = htonl(req.downstreamNodeId);
7,063✔
609

610
    SRpcMsg rsp = {.code = 0, .info = pMsg->info, .pCont = pReadyRsp, .contLen = sizeof(SMStreamCheckpointReadyRspMsg)};
7,063✔
611
    tmsgSendRsp(&rsp);
7,063✔
612

613
    pMsg->info.handle = NULL;  // disable auto rsp
7,066✔
614
  }
615

616
  return code;
7,066✔
617
}
618

619
int32_t tqStreamTaskProcessDeployReq(SStreamMeta* pMeta, SMsgCb* cb, int64_t sversion, char* msg, int32_t msgLen,
11,709✔
620
                                     bool isLeader, bool restored) {
621
  int32_t code = 0;
11,709✔
622
  int32_t vgId = pMeta->vgId;
11,709✔
623
  int32_t numOfTasks = 0;
11,709✔
624
  int32_t taskId = -1;
11,709✔
625
  int64_t streamId = -1;
11,709✔
626
  bool    added = false;
11,709✔
627
  int32_t size = sizeof(SStreamTask);
11,709✔
628

629
  if (tsDisableStream) {
11,709!
630
    tqInfo("vgId:%d stream disabled, not deploy stream tasks", vgId);
×
631
    return code;
×
632
  }
633

634
  tqDebug("vgId:%d receive new stream task deploy msg, start to build stream task", vgId);
11,709✔
635

636
  // 1.deserialize msg and build task
637
  SStreamTask* pTask = taosMemoryCalloc(1, size);
11,709!
638
  if (pTask == NULL) {
11,714!
639
    tqError("vgId:%d failed to create stream task due to out of memory, alloc size:%d", vgId, size);
×
640
    return terrno;
×
641
  }
642

643
  SDecoder decoder;
644
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
11,714✔
645
  code = tDecodeStreamTask(&decoder, pTask);
11,714✔
646
  tDecoderClear(&decoder);
11,712✔
647

648
  if (code != TSDB_CODE_SUCCESS) {
11,711!
649
    taosMemoryFree(pTask);
×
650
    return TSDB_CODE_INVALID_MSG;
×
651
  }
652

653
  // 2.save task, use the latest commit version as the initial start version of stream task.
654
  taskId = pTask->id.taskId;
11,711✔
655
  streamId = pTask->id.streamId;
11,711✔
656

657
  streamMetaWLock(pMeta);
11,711✔
658
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
11,717✔
659
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
11,716✔
660
  streamMetaWUnLock(pMeta);
11,717✔
661

662
  if (code < 0) {
11,716!
663
    tqError("vgId:%d failed to register s-task:0x%x into meta, existed tasks:%d, code:%s", vgId, taskId, numOfTasks,
×
664
            tstrerror(code));
665
    return code;
×
666
  }
667

668
  // added into meta store, pTask cannot be reference since it may have been destroyed by other threads already now if
669
  // it is added into the meta store
670
  if (added) {
11,716✔
671
    // only handled in the leader node
672
    if (isLeader) {
11,615✔
673
      tqDebug("vgId:%d s-task:0x%x is deployed and add into meta, numOfTasks:%d", vgId, taskId, numOfTasks);
11,571✔
674

675
      if (restored) {
11,573✔
676
        SStreamTask* p = NULL;
11,549✔
677
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
11,549✔
678
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
11,549!
679
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
7,496✔
680
        }
681

682
        if (p != NULL) {
11,549!
683
          streamMetaReleaseTask(pMeta, p);
11,549✔
684
        }
685
      } else {
686
        tqWarn("s-task:0x%x not launched since vnode(vgId:%d) not ready", taskId, vgId);
24!
687
      }
688

689
    } else {
690
      tqDebug("vgId:%d not leader, not launch stream task s-task:0x%x", vgId, taskId);
44!
691
    }
692
  } else {
693
    tqWarn("vgId:%d failed to add s-task:0x%x, since already exists in meta store, total:%d", vgId, taskId, numOfTasks);
101!
694
  }
695

696
  return code;
11,716✔
697
}
698

699
int32_t tqStreamTaskProcessDropReq(SStreamMeta* pMeta, char* msg, int32_t msgLen) {
5,333✔
700
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
5,333✔
701
  int32_t              code = 0;
5,333✔
702
  int32_t              vgId = pMeta->vgId;
5,333✔
703
  STaskId              hTaskId = {0};
5,333✔
704
  SStreamTask*         pTask = NULL;
5,333✔
705

706
  tqDebug("vgId:%d receive msg to drop s-task:0x%x", vgId, pReq->taskId);
5,333✔
707

708
  streamMetaWLock(pMeta);
5,333✔
709

710
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
5,347✔
711
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
5,347✔
712
  if (code == 0) {
5,343!
713
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
5,343✔
714
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
1,259✔
715
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
1,259✔
716
    }
717

718
    // clear the relationship, and then release the stream tasks, to avoid invalid accessing of already freed
719
    // related stream(history) task
720
    streamTaskSetRemoveBackendFiles(pTask);
5,343✔
721
    code = streamTaskClearHTaskAttr(pTask, pReq->resetRelHalt);
5,337✔
722
    streamMetaReleaseTask(pMeta, pTask);
5,345✔
723

724
    if (code) {
5,350✔
725
      tqError("s-task:0x%x failed to clear related fill-history info, still exists", pReq->taskId);
5!
726
    }
727
  }
728

729
  // drop the related fill-history task firstly
730
  if (hTaskId.taskId != 0 && hTaskId.streamId != 0) {
5,350✔
731
    tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x firstly", pReq->taskId, vgId, (int32_t)hTaskId.taskId);
1,201✔
732
    code = streamMetaUnregisterTask(pMeta, hTaskId.streamId, hTaskId.taskId);
1,201✔
733
    if (code) {
1,198!
734
      tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x failed", pReq->taskId, vgId,
×
735
              (int32_t)hTaskId.taskId);
736
    }
737
  }
738

739
  // drop the stream task now
740
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
5,347✔
741
  if (code) {
5,346!
742
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
743
  }
744

745
  // commit the update
746
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
5,346✔
747
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
5,350✔
748

749
  if (streamMetaCommit(pMeta) < 0) {
5,350✔
750
    // persist to disk
751
  }
752

753
  streamMetaWUnLock(pMeta);
5,351✔
754
  return 0;  // always return success
5,351✔
755
}
756

757
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
4,598✔
758
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
4,598✔
759
  int32_t                    code = 0;
4,598✔
760
  int32_t                    vgId = pMeta->vgId;
4,598✔
761
  SStreamTask*               pTask = NULL;
4,598✔
762

763
  tqDebug("vgId:%d receive msg to update-checkpoint-info for s-task:0x%x", vgId, pReq->taskId);
4,598✔
764

765
  streamMetaWLock(pMeta);
4,598✔
766

767
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
4,605✔
768
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
4,605✔
769
  if (code == 0) {
4,604!
770
    code = streamTaskUpdateTaskCheckpointInfo(pTask, restored, pReq);
4,604✔
771
    streamMetaReleaseTask(pMeta, pTask);
4,607✔
772
  } else {  // failed to get the task.
773
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
774
    tqError(
×
775
        "vgId:%d failed to locate the s-task:0x%x to update the checkpoint info, numOfTasks:%d, it may have been "
776
        "dropped already",
777
        vgId, pReq->taskId, numOfTasks);
778
  }
779

780
  streamMetaWUnLock(pMeta);
4,608✔
781
  // always return success when handling the requirement issued by mnode during transaction.
782
  return TSDB_CODE_SUCCESS;
4,608✔
783
}
784

785
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
51✔
786
  int32_t vgId = pMeta->vgId;
51✔
787
  int32_t code = 0;
51✔
788
  int64_t st = taosGetTimestampMs();
51✔
789

790
  streamMetaWLock(pMeta);
51✔
791
  if (pMeta->startInfo.startAllTasks == 1) {
51✔
792
    pMeta->startInfo.restartCount += 1;
7✔
793
    tqDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
7!
794
            pMeta->startInfo.restartCount);
795
    streamMetaWUnLock(pMeta);
7✔
796
    return TSDB_CODE_SUCCESS;
7✔
797
  }
798

799
  pMeta->startInfo.startAllTasks = 1;
44✔
800
  streamMetaWUnLock(pMeta);
44✔
801

802
  terrno = 0;
44✔
803
  tqInfo("vgId:%d tasks are all updated and stopped, restart all tasks, triggered by transId:%d, ts:%" PRId64, vgId,
44!
804
         pMeta->updateInfo.completeTransId, pMeta->updateInfo.completeTs);
805

806
  streamMetaWLock(pMeta);
44✔
807
  streamMetaClear(pMeta);
44✔
808

809
  int64_t el = taosGetTimestampMs() - st;
44✔
810
  tqInfo("vgId:%d close&reload state elapsed time:%.3fs", vgId, el / 1000.);
44!
811

812
  streamMetaLoadAllTasks(pMeta);
44✔
813

814
  {
815
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
44✔
816
    taosHashClear(pStartInfo->pReadyTaskSet);
44✔
817
    taosHashClear(pStartInfo->pFailedTaskSet);
44✔
818
    pStartInfo->readyTs = 0;
44✔
819
  }
820

821
  if (isLeader && !tsDisableStream) {
44!
822
    streamMetaWUnLock(pMeta);
39✔
823
    code = streamMetaStartAllTasks(pMeta);
39✔
824
  } else {
825
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
5✔
826
    pMeta->startInfo.restartCount = 0;
5✔
827
    streamMetaWUnLock(pMeta);
5✔
828
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
5!
829
  }
830

831
  code = terrno;
44✔
832
  return code;
44✔
833
}
834

835
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
84,937✔
836
  int32_t  code = 0;
84,937✔
837
  int32_t  vgId = pMeta->vgId;
84,937✔
838
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
84,937✔
839
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
84,937✔
840
  SDecoder decoder;
841

842
  SStreamTaskRunReq req = {0};
84,937✔
843
  tDecoderInit(&decoder, (uint8_t*)msg, len);
84,937✔
844
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
85,070!
845
    tqError("vgId:%d failed to decode task run req, code:%s", pMeta->vgId, tstrerror(code));
×
846
    tDecoderClear(&decoder);
×
847
    return TSDB_CODE_SUCCESS;
×
848
  }
849

850
  tDecoderClear(&decoder);
85,060✔
851

852
  int32_t type = req.reqType;
85,071✔
853
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
85,071✔
854
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
7,673✔
855
    return 0;
7,670✔
856
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
77,398✔
857
    code = streamMetaStartAllTasks(pMeta);
10,252✔
858
    return 0;
10,252✔
859
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
67,146✔
860
    code = restartStreamTasks(pMeta, isLeader);
44✔
861
    return 0;
44✔
862
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
67,102✔
863
    code = streamMetaStopAllTasks(pMeta);
4,725✔
864
    return 0;
4,726✔
865
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
62,377✔
866
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
3✔
867
    return code;
3✔
868
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
62,374✔
869
    SStreamTask* pTask = NULL;
8,714✔
870
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
8,714✔
871

872
    if (pTask != NULL && (code == 0)) {
8,714!
873
      char* pStatus = NULL;
8,716✔
874
      if (streamTaskReadyToRun(pTask, &pStatus)) {
8,716!
875
        int64_t execTs = pTask->status.lastExecTs;
8,713✔
876
        int32_t idle = taosGetTimestampMs() - execTs;
8,712✔
877
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
8,712✔
878

879
        code = streamResumeTask(pTask);
8,712✔
880
      } else {
881
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
882
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
883
                pTask->id.idStr, pStatus, status);
884
      }
885
      streamMetaReleaseTask(pMeta, pTask);
8,717✔
886
    }
887

888
    return code;
8,716✔
889
  }
890

891
  SStreamTask* pTask = NULL;
53,660✔
892
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
53,660✔
893
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
53,649!
894
    char* p = NULL;
53,644✔
895
    if (streamTaskReadyToRun(pTask, &p)) {
53,644✔
896
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
53,527✔
897
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
898
      (void)streamExecTask(pTask);
53,528✔
899
    } else {
900
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
99✔
901
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
105!
902
              pTask->id.idStr, p, status);
903
    }
904

905
    streamMetaReleaseTask(pMeta, pTask);
53,645✔
906
    return 0;
53,651✔
907
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
908
    // todo add one function to handle this
909
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
5!
910
    return code;
13✔
911
  }
912
}
913

914
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
1,476✔
915
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
1,476✔
916
  int32_t         vgId = pMeta->vgId;
1,476✔
917
  bool            scanWal = false;
1,476✔
918
  int32_t         code = 0;
1,476✔
919

920
  streamMetaWLock(pMeta);
1,476✔
921
  if (pStartInfo->startAllTasks == 1) {
1,476!
922
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
923
            pMeta->startInfo.restartCount);
924
  } else {  // not in starting procedure
925
    bool allReady = streamMetaAllTasksReady(pMeta);
1,476✔
926

927
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
1,476!
928
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
929
      pStartInfo->restartCount -= 1;
7✔
930
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
7!
931
              pStartInfo->restartCount);
932
      streamMetaWUnLock(pMeta);
7✔
933

934
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
7✔
935
    } else {
936
      if (pStartInfo->restartCount == 0) {
1,469!
937
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
1,469✔
938
      } else if (allReady) {
×
939
        pStartInfo->restartCount = 0;
×
940
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
941
      }
942

943
      scanWal = true;
1,469✔
944
    }
945
  }
946

947
  streamMetaWUnLock(pMeta);
1,469✔
948

949
  if (scanWal && (vgId != SNODE_HANDLE)) {
1,469!
950
    tqDebug("vgId:%d start scan wal for executing tasks", vgId);
1,466✔
951
    code = tqScanWalAsync(pMeta->ahandle, true);
1,466✔
952
  }
953

954
  return code;
1,469✔
955
}
956

957
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
958
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
959

960
  SStreamTask* pTask = NULL;
×
961
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
962
  if (pTask == NULL || (code != 0)) {
×
963
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
964
            pMeta->vgId, pReq->taskId);
965
    return TSDB_CODE_SUCCESS;
×
966
  }
967

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

970
  streamMutexLock(&pTask->lock);
×
971
  streamTaskClearCheckInfo(pTask, true);
×
972

973
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
974

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

988
  streamMutexUnlock(&pTask->lock);
×
989

990
  streamMetaReleaseTask(pMeta, pTask);
×
991
  return TSDB_CODE_SUCCESS;
×
992
}
993

994
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
995
  SRetrieveChkptTriggerReq req = {0};
×
996
  SStreamTask*             pTask = NULL;
×
997
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
998
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
999
  SDecoder                 decoder = {0};
×
1000

1001
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1002
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1003
    tDecoderClear(&decoder);
×
1004
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
1005
    return TSDB_CODE_INVALID_MSG;
×
1006
  }
1007
  tDecoderClear(&decoder);
×
1008

1009
  int32_t code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
1010
  if (pTask == NULL || (code != 0)) {
×
1011
    tqError("vgId:%d process retrieve checkpoint-trigger, checkpointId:%" PRId64
×
1012
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
1013
            pMeta->vgId, req.checkpointId, (int32_t)req.downstreamTaskId, req.upstreamTaskId);
1014
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1015
  }
1016

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

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

1024
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1025
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
1026
    streamMetaReleaseTask(pMeta, pTask);
×
1027
    return code;
×
1028
  }
1029

1030
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1031
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1032
    int32_t transId = 0;
×
1033
    int64_t checkpointId = 0;
×
1034

1035
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
1036
    if (checkpointId != req.checkpointId) {
×
1037
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1038
              " req:%" PRId64,
1039
              pTask->id.idStr, req.downstreamTaskId, checkpointId, req.checkpointId);
1040
      streamMetaReleaseTask(pMeta, pTask);
×
1041
      return TSDB_CODE_INVALID_MSG;
×
1042
    }
1043

1044
    if (streamTaskAlreadySendTrigger(pTask, req.downstreamNodeId)) {
×
1045
      // re-send the lost checkpoint-trigger msg to downstream task
1046
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1047
              (int32_t)req.downstreamTaskId, checkpointId, transId);
1048
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1049
                                                TSDB_CODE_SUCCESS);
1050
    } else {  // not send checkpoint-trigger yet, wait
1051
      int32_t recv = 0, total = 0;
×
1052
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1053

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

1070
    tqWarn(
×
1071
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1072
        "upstream sending checkpoint-source/trigger",
1073
        pTask->id.idStr);
1074
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1075
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1076
  }
1077

1078
  streamMetaReleaseTask(pMeta, pTask);
×
1079
  return code;
×
1080
}
1081

1082
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1083
  SCheckpointTriggerRsp rsp = {0};
×
1084
  SStreamTask*          pTask = NULL;
×
1085
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1086
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
1087
  SDecoder              decoder = {0};
×
1088

1089
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1090
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1091
    tDecoderClear(&decoder);
×
1092
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1093
    return TSDB_CODE_INVALID_MSG;
×
1094
  }
1095
  tDecoderClear(&decoder);
×
1096

1097
  int32_t code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.taskId, &pTask);
×
1098
  if (pTask == NULL || (code != 0)) {
×
1099
    tqError(
×
1100
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1101
        pMeta->vgId, rsp.taskId);
1102
    return code;
×
1103
  }
1104

1105
  tqDebug(
×
1106
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1107
      ", transId:%d",
1108
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1109

1110
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
1111
  streamMetaReleaseTask(pMeta, pTask);
×
1112
  return code;
×
1113
}
1114

1115
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
805✔
1116
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
805✔
1117

1118
  SStreamTask* pTask = NULL;
805✔
1119
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
805✔
1120
  if (pTask == NULL || (code != 0)) {
805!
1121
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
1122
            pReq->taskId);
1123
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1124
    return TSDB_CODE_SUCCESS;
×
1125
  }
1126

1127
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
805!
1128
  streamTaskPause(pTask);
805✔
1129

1130
  SStreamTask* pHistoryTask = NULL;
805✔
1131
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
805!
1132
    pHistoryTask = NULL;
×
1133
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
×
1134
    if (pHistoryTask == NULL || (code != 0)) {
×
1135
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1136
              ", it may have been dropped already",
1137
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
1138
      streamMetaReleaseTask(pMeta, pTask);
×
1139

1140
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1141
      return TSDB_CODE_SUCCESS;
×
1142
    }
1143

1144
    tqDebug("s-task:%s fill-history task handle paused along with related stream task", pHistoryTask->id.idStr);
×
1145

1146
    streamTaskPause(pHistoryTask);
×
1147
    streamMetaReleaseTask(pMeta, pHistoryTask);
×
1148
  }
1149

1150
  streamMetaReleaseTask(pMeta, pTask);
805✔
1151
  return TSDB_CODE_SUCCESS;
805✔
1152
}
1153

1154
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
1,470✔
1155
                                       bool fromVnode) {
1156
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
1,470✔
1157
  int32_t      vgId = pMeta->vgId;
1,470✔
1158
  int32_t      code = 0;
1,470✔
1159

1160
  streamTaskResume(pTask);
1,470✔
1161
  ETaskStatus status = streamTaskGetStatus(pTask).state;
1,470✔
1162

1163
  int32_t level = pTask->info.taskLevel;
1,470✔
1164
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
1,470!
1165
    // no lock needs to secure the access of the version
1166
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
1,470!
1167
      // discard all the data  when the stream task is suspended.
1168
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
337✔
1169
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
337!
1170
              ", schedStatus:%d",
1171
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1172
    } else {  // from the previous paused version and go on
1173
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
1,133!
1174
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1175
    }
1176

1177
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
1,470!
1178
      pTask->hTaskInfo.operatorOpen = false;
×
1179
      code = streamStartScanHistoryAsync(pTask, igUntreated);
×
1180
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
1,470!
1181
      code = tqScanWalAsync((STQ*)handle, false);
733✔
1182
    } else {
1183
      code = streamTrySchedExec(pTask);
737✔
1184
    }
1185
  }
1186

1187
  return code;
1,470✔
1188
}
1189

1190
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
1,470✔
1191
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
1,470✔
1192

1193
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
1,470✔
1194

1195
  SStreamTask* pTask = NULL;
1,470✔
1196
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,470✔
1197
  if (pTask == NULL || (code != 0)) {
1,470!
1198
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1199
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1200
  }
1201

1202
  streamMutexLock(&pTask->lock);
1,470✔
1203
  SStreamTaskState pState = streamTaskGetStatus(pTask);
1,470✔
1204
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
1,470!
1205
  streamMutexUnlock(&pTask->lock);
1,470✔
1206

1207
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
1,470✔
1208
  if (code != 0) {
1,470!
1209
    streamMetaReleaseTask(pMeta, pTask);
×
1210
    return code;
×
1211
  }
1212

1213
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
1,470✔
1214
  SStreamTask* pHTask = NULL;
1,470✔
1215
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
1,470✔
1216
  if (pHTask && (code == 0)) {
1,470!
1217
    streamMutexLock(&pHTask->lock);
×
1218
    SStreamTaskState p = streamTaskGetStatus(pHTask);
×
1219
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
×
1220
    streamMutexUnlock(&pHTask->lock);
×
1221

1222
    code = tqProcessTaskResumeImpl(handle, pHTask, sversion, pReq->igUntreated, fromVnode);
×
1223
    tqDebug("s-task:%s resume complete, code:%s", pHTask->id.idStr, tstrerror(code));
×
1224

1225
    streamMetaReleaseTask(pMeta, pHTask);
×
1226
  }
1227

1228
  return TSDB_CODE_SUCCESS;
1,470✔
1229
}
1230

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

1233
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
9,044✔
1234
  rpcFreeCont(pMsg->pCont);
9,044✔
1235
  pMsg->pCont = NULL;
9,044✔
1236
  return TSDB_CODE_SUCCESS;
9,044✔
1237
}
1238

1239
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
25,317✔
1240
  SMStreamHbRspMsg rsp = {0};
25,317✔
1241
  int32_t          code = 0;
25,317✔
1242
  SDecoder         decoder;
1243
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
25,317✔
1244
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
25,317✔
1245

1246
  tDecoderInit(&decoder, (uint8_t*)msg, len);
25,317✔
1247
  code = tDecodeStreamHbRsp(&decoder, &rsp);
25,317✔
1248
  if (code < 0) {
25,316!
1249
    terrno = TSDB_CODE_INVALID_MSG;
×
1250
    tDecoderClear(&decoder);
×
1251
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1252
    return terrno;
×
1253
  }
1254

1255
  tDecoderClear(&decoder);
25,316✔
1256
  return streamProcessHeartbeatRsp(pMeta, &rsp);
25,316✔
1257
}
1258

1259
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
3,712✔
1260

1261
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
5,332✔
1262

1263
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
7,069✔
1264
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
7,069✔
1265

1266
  SStreamTask* pTask = NULL;
7,069✔
1267
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
7,069✔
1268
  if (pTask == NULL || (code != 0)) {
7,068!
1269
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
3!
1270
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1271
    return code;
3✔
1272
  }
1273

1274
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
7,065✔
1275
  streamMetaReleaseTask(pMeta, pTask);
7,065✔
1276
  return code;
7,065✔
1277
}
1278

1279
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
190✔
1280
  int32_t                vgId = pMeta->vgId;
190✔
1281
  int32_t                code = 0;
190✔
1282
  SStreamTask*           pTask = NULL;
190✔
1283
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
190✔
1284
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
190✔
1285
  int64_t                now = taosGetTimestampMs();
190✔
1286
  SDecoder               decoder;
1287
  SRestoreCheckpointInfo req = {0};
190✔
1288

1289
  tDecoderInit(&decoder, (uint8_t*)msg, len);
190✔
1290
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
190!
1291
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1292
    tDecoderClear(&decoder);
×
1293
    return TSDB_CODE_SUCCESS;
×
1294
  }
1295

1296
  tDecoderClear(&decoder);
190✔
1297

1298
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
190✔
1299
  if (pTask == NULL || (code != 0)) {
190!
1300
    tqError("vgId:%d process consensus checkpointId req, failed to acquire task:0x%x, it may have been dropped already",
×
1301
            pMeta->vgId, req.taskId);
1302
    // ignore this code to avoid error code over write
1303
    int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
1304
    if (ret) {
×
1305
      tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1306
    }
1307

1308
    return 0;
×
1309
  }
1310

1311
  // discard the rsp, since it is expired.
1312
  if (req.startTs < pTask->execInfo.created) {
190✔
1313
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
16!
1314
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1315
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1316
           pTask->execInfo.created);
1317
    streamMetaAddFailedTaskSelf(pTask, now);
16✔
1318
    streamMetaReleaseTask(pMeta, pTask);
16✔
1319
    return TSDB_CODE_SUCCESS;
16✔
1320
  }
1321

1322
  tqDebug("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64 " from mnode",
174✔
1323
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId);
1324

1325
  streamMutexLock(&pTask->lock);
174✔
1326
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
174!
1327
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1328
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1329

1330
    streamMutexUnlock(&pTask->lock);
×
1331
    streamMetaReleaseTask(pMeta, pTask);
×
1332
    return 0;
×
1333
  }
1334

1335
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
174✔
1336
  if (pConsenInfo->consenChkptTransId >= req.transId) {
174!
1337
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1338
            pConsenInfo->consenChkptTransId, req.transId);
1339
    streamMutexUnlock(&pTask->lock);
×
1340
    streamMetaReleaseTask(pMeta, pTask);
×
1341
    return TSDB_CODE_SUCCESS;
×
1342
  }
1343

1344
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
174!
1345
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1346
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1347
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1348
    tqSetRestoreVersionInfo(pTask);
×
1349
  } else {
1350
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
174✔
1351
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1352
  }
1353

1354
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
174✔
1355
  streamMutexUnlock(&pTask->lock);
174✔
1356

1357
  if (pMeta->role == NODE_ROLE_LEADER) {
174!
1358
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
174✔
1359
    if (code) {
174!
1360
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1361
    }
1362
  } else {
1363
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1364
  }
1365

1366
  streamMetaReleaseTask(pMeta, pTask);
174✔
1367
  return 0;
174✔
1368
}
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