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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

43
  if (pTask->info.fillHistory) {
×
44
    streamId = pTask->streamTaskId.streamId;
×
45
    taskId = pTask->streamTaskId.taskId;
×
46
  } else {
47
    streamId = pTask->id.streamId;
×
48
    taskId = pTask->id.taskId;
×
49
  }
50

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

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

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

76
  initStorageAPI(&handle.api);
×
77

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

90
    code = qSetStreamNotifyInfo(pTask->exec.pExecutor, pTask->notifyInfo.notifyEventTypes,
×
91
                                pTask->notifyInfo.pSchemaWrapper, pTask->notifyInfo.stbFullName,
×
92
                                IS_NEW_SUBTB_RULE(pTask), &pTask->notifyEventStat);
×
93
    if (code) {
×
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);
×
100

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

104
  return code;
×
105
}
106

107
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
×
108
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
×
109

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

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

120
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
×
121
}
×
122

123
int32_t tqStreamTaskStartAsync(SStreamMeta* pMeta, SMsgCb* cb, bool restart) {
×
124
  int32_t vgId = pMeta->vgId;
×
125
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
×
126
  if (numOfTasks == 0) {
×
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);
×
132

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

137
int32_t tqStreamStartOneTaskAsync(SStreamMeta* pMeta, SMsgCb* cb, int64_t streamId, int32_t taskId) {
×
138
  int32_t vgId = pMeta->vgId;
×
139
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
×
140
  if (numOfTasks == 0) {
×
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);
×
146
  return streamTaskSchedTask(cb, vgId, streamId, taskId, STREAM_EXEC_T_START_ONE_TASK, false);
×
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, bool isLeader) {
×
151
  int32_t      vgId = pMeta->vgId;
×
152
  char*        msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
153
  int32_t      len = pMsg->contLen - sizeof(SMsgHead);
×
154
  SRpcMsg      rsp = {.info = pMsg->info, .code = TSDB_CODE_SUCCESS};
×
155
  int64_t      st = taosGetTimestampMs();
×
156
  bool         updated = false;
×
157
  int32_t      code = 0;
×
158
  SStreamTask* pTask = NULL;
×
159
  SStreamTask* pHTask = NULL;
×
160

161
  SStreamTaskNodeUpdateMsg req = {0};
×
162

163
  SDecoder decoder;
164
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
165
  if (tDecodeStreamTaskUpdateMsg(&decoder, &req) < 0) {
×
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);
×
173

174
  int32_t gError = streamGetFatalError(pMeta);
×
175
  if (gError != 0) {
×
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);
×
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};
×
186
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
187
  if (code != 0) {
×
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;
×
197

198
  if (req.transId <= 0) {
×
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);
×
211
  if (!update) {
×
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};
×
223

224
  void* pReqTask = taosHashGet(pMeta->updateInfo.pTasks, &entry, sizeof(STaskUpdateEntry));
×
225
  if (pReqTask != NULL) {
×
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);
×
238

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

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

248
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
249
    code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->hTaskInfo.id, &pHTask);
×
250
    if (code != 0) {
×
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);
×
258
      bool updateEpSet = streamTaskUpdateEpsetInfo(pHTask, req.pNodeList);
×
259
      if (updateEpSet) {
×
260
        updated = updateEpSet;
×
261
      }
262

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

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

276
    if (pHTask != NULL) {
×
277
      code = streamMetaSaveTaskInMeta(pMeta, pHTask);
×
278
      if (code) {
×
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);
×
284
  }
285

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

291
  if (pHTask != NULL) {
×
292
    code = streamTaskStop(pHTask);
×
293
    if (code) {
×
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);
×
300
  streamMetaReleaseTask(pMeta, pTask);
×
301
  streamMetaReleaseTask(pMeta, pHTask);
×
302

303
  rsp.code = TSDB_CODE_SUCCESS;
×
304

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

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

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

330
    streamMetaClearSetUpdateTaskListComplete(pMeta);
×
331

332
    if (isLeader) {
×
333
      if (!restored) {
×
334
        tqDebug("vgId:%d vnode restore not completed, not start all tasks", vgId);
×
335
      } else {
336
        tqDebug("vgId:%d all %d task(s) nodeEp updated and closed, transId:%d", vgId, numOfTasks, req.transId);
×
337
#if 0
338
      taosMSleep(5000);// for test purpose, to trigger the leader election
339
#endif
340
        code = tqStreamTaskStartAsync(pMeta, cb, true);
×
341
        if (code) {
×
342
          tqError("vgId:%d async start all tasks, failed, code:%s", vgId, tstrerror(code));
×
343
        }
344
      }
345
    } else {
346
      tqDebug("vgId:%d follower nodes not restart tasks", vgId);
×
347
    }
348
  }
349

350
  streamMetaWUnLock(pMeta);
×
351
  taosArrayDestroy(req.pNodeList);
×
352
  return rsp.code;  // always return true
×
353
}
354

355
int32_t tqStreamTaskProcessDispatchReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
356
  char*   msgStr = pMsg->pCont;
×
357
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
358
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
×
359

360
  SStreamDispatchReq req = {0};
×
361

362
  SDecoder decoder;
363
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
364
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
×
365
    tDecoderClear(&decoder);
×
366
    return TSDB_CODE_MSG_DECODE_ERROR;
×
367
  }
368
  tDecoderClear(&decoder);
×
369

370
  tqDebug("s-task:0x%x recv dispatch msg from 0x%x(vgId:%d)", req.taskId, req.upstreamTaskId, req.upstreamNodeId);
×
371

372
  SStreamTask* pTask = NULL;
×
373
  int32_t      code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
374
  if (pTask && (code == 0)) {
×
375
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
376
    if (streamProcessDispatchMsg(pTask, &req, &rsp) != 0) {
×
377
      return -1;
×
378
    }
379
    tCleanupStreamDispatchReq(&req);
×
380
    streamMetaReleaseTask(pMeta, pTask);
×
381
    return 0;
×
382
  } else {
383
    tqError("vgId:%d failed to find task:0x%x to handle the dispatch req, it may have been destroyed already",
×
384
            pMeta->vgId, req.taskId);
385

386
    SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
×
387
    if (pRspHead == NULL) {
×
388
      tqError("s-task:0x%x send dispatch error rsp, out of memory", req.taskId);
×
389
      return terrno;
×
390
    }
391

392
    pRspHead->vgId = htonl(req.upstreamNodeId);
×
393
    if (pRspHead->vgId == 0) {
×
394
      tqError("vgId:%d invalid dispatch msg from upstream to task:0x%x", pMeta->vgId, req.taskId);
×
395
      return TSDB_CODE_INVALID_MSG;
×
396
    }
397

398
    SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
×
399
    pRsp->streamId = htobe64(req.streamId);
×
400
    pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
×
401
    pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
×
402
    pRsp->downstreamNodeId = htonl(pMeta->vgId);
×
403
    pRsp->downstreamTaskId = htonl(req.taskId);
×
404
    pRsp->msgId = htonl(req.msgId);
×
405
    pRsp->stage = htobe64(req.stage);
×
406
    pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;
×
407

408
    int32_t len = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
×
409
    SRpcMsg rsp = {.code = TSDB_CODE_STREAM_TASK_NOT_EXIST, .info = pMsg->info, .contLen = len, .pCont = pRspHead};
×
410
    tqError("s-task:0x%x send dispatch error rsp, no task", req.taskId);
×
411

412
    tmsgSendRsp(&rsp);
×
413
    tCleanupStreamDispatchReq(&req);
×
414

415
    return 0;
×
416
  }
417
}
418

419
int32_t tqStreamTaskProcessDispatchRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
420
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
421

422
  int32_t vgId = pMeta->vgId;
×
423
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
×
424
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
×
425
  pRsp->streamId = htobe64(pRsp->streamId);
×
426
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
×
427
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
×
428
  pRsp->stage = htobe64(pRsp->stage);
×
429
  pRsp->msgId = htonl(pRsp->msgId);
×
430

431
  tqDebug("s-task:0x%x vgId:%d recv dispatch-rsp from 0x%x vgId:%d", pRsp->upstreamTaskId, pRsp->upstreamNodeId,
×
432
          pRsp->downstreamTaskId, pRsp->downstreamNodeId);
433

434
  SStreamTask* pTask = NULL;
×
435
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
×
436
  if (pTask && (code == 0)) {
×
437
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
×
438
    streamMetaReleaseTask(pMeta, pTask);
×
439
    return code;
×
440
  } else {
441
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
×
442
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
443
  }
444
}
445

446
int32_t tqStreamTaskProcessRetrieveReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
447
  char*    msgStr = pMsg->pCont;
×
448
  char*    msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
449
  int32_t  msgLen = pMsg->contLen - sizeof(SMsgHead);
×
450
  int32_t  code = 0;
×
451
  SDecoder decoder;
452

453
  SStreamRetrieveReq req;
454
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
455
  code = tDecodeStreamRetrieveReq(&decoder, &req);
×
456
  tDecoderClear(&decoder);
×
457

458
  if (code) {
×
459
    tqError("vgId:%d failed to decode retrieve msg, discard it", pMeta->vgId);
×
460
    return code;
×
461
  }
462

463
  SStreamTask* pTask = NULL;
×
464
  code = streamMetaAcquireTask(pMeta, req.streamId, req.dstTaskId, &pTask);
×
465
  if (pTask == NULL || code != 0) {
×
466
    tqError("vgId:%d process retrieve req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
467
            req.dstTaskId);
468
    tCleanupStreamRetrieveReq(&req);
×
469
    return code;
×
470
  }
471

472
  // enqueue
473
  tqDebug("s-task:%s (vgId:%d level:%d) recv retrieve req from task:0x%x(vgId:%d), QID:0x%" PRIx64, pTask->id.idStr,
×
474
          pTask->pMeta->vgId, pTask->info.taskLevel, req.srcTaskId, req.srcNodeId, req.reqId);
475

476
  // if task is in ck status, set current ck failed
477
  streamTaskSetCheckpointFailed(pTask);
×
478

479
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
480
    code = streamProcessRetrieveReq(pTask, &req);
×
481
  } else {
482
    req.srcNodeId = pTask->info.nodeId;
×
483
    req.srcTaskId = pTask->id.taskId;
×
484
    code = streamTaskBroadcastRetrieveReq(pTask, &req);
×
485
  }
486

487
  if (code != TSDB_CODE_SUCCESS) {  // return error not send rsp manually
×
488
    tqError("s-task:0x%x vgId:%d failed to process retrieve request from 0x%x, code:%s", req.dstTaskId, req.dstNodeId,
×
489
            req.srcTaskId, tstrerror(code));
490
  } else {  // send rsp manually only on success.
491
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
492
    streamTaskSendRetrieveRsp(&req, &rsp);
×
493
  }
494

495
  streamMetaReleaseTask(pMeta, pTask);
×
496
  tCleanupStreamRetrieveReq(&req);
×
497

498
  // always return success, to disable the auto rsp
499
  return code;
×
500
}
501

502
int32_t tqStreamTaskProcessCheckReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
503
  char*   msgStr = pMsg->pCont;
×
504
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
505
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
×
506
  int32_t code = 0;
×
507

508
  SStreamTaskCheckReq req;
509
  SStreamTaskCheckRsp rsp = {0};
×
510

511
  SDecoder decoder;
512

513
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
514
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
×
515
  tDecoderClear(&decoder);
×
516

517
  if (code) {
×
518
    tqError("vgId:%d decode check msg failed, not handle this msg", pMeta->vgId);
×
519
    return code;
×
520
  }
521

522
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
×
523
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
×
524
}
525

526
int32_t tqStreamTaskProcessCheckRsp(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
×
527
  char*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
528
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
529
  int32_t vgId = pMeta->vgId;
×
530
  int32_t code = TSDB_CODE_SUCCESS;
×
531

532
  SStreamTaskCheckRsp rsp;
533

534
  SDecoder decoder;
535
  tDecoderInit(&decoder, (uint8_t*)pReq, len);
×
536
  code = tDecodeStreamTaskCheckRsp(&decoder, &rsp);
×
537
  if (code < 0) {
×
538
    terrno = TSDB_CODE_INVALID_MSG;
×
539
    tDecoderClear(&decoder);
×
540
    tqError("vgId:%d failed to parse check rsp msg, code:%s", vgId, tstrerror(terrno));
×
541
    return -1;
×
542
  }
543

544
  tDecoderClear(&decoder);
×
545
  tqDebug("tq task:0x%x (vgId:%d) recv check rsp(QID:0x%" PRIx64 ") from 0x%x (vgId:%d) status %d", rsp.upstreamTaskId,
×
546
          rsp.upstreamNodeId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.status);
547

548
  if (!isLeader) {
×
549
    tqError("vgId:%d not leader, task:0x%x not handle the check rsp, downstream:0x%x (vgId:%d)", vgId,
×
550
            rsp.upstreamTaskId, rsp.downstreamTaskId, rsp.downstreamNodeId);
551
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
×
552
  }
553

554
  SStreamTask* pTask = NULL;
×
555
  code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.upstreamTaskId, &pTask);
×
556
  if ((pTask == NULL) || (code != 0)) {
×
557
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId);
×
558
  }
559

560
  code = streamTaskProcessCheckRsp(pTask, &rsp);
×
561
  streamMetaReleaseTask(pMeta, pTask);
×
562
  return code;
×
563
}
564

565
int32_t tqStreamTaskProcessCheckpointReadyMsg(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
566
  int32_t vgId = pMeta->vgId;
×
567
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
568
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
569
  int32_t code = 0;
×
570

571
  SStreamCheckpointReadyMsg req = {0};
×
572

573
  SDecoder decoder;
574
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
575
  if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
×
576
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
577
    tDecoderClear(&decoder);
×
578
    return code;
×
579
  }
580
  tDecoderClear(&decoder);
×
581

582
  SStreamTask* pTask = NULL;
×
583
  code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
584
  if (code != 0) {
×
585
    tqError("vgId:%d failed to find s-task:0x%x, it may have been destroyed already", vgId, req.downstreamTaskId);
×
586
    return code;
×
587
  }
588

589
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
590
    tqDebug("vgId:%d s-task:%s recv invalid the checkpoint-ready msg from task:0x%x (vgId:%d), discard", vgId,
×
591
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
592
    streamMetaReleaseTask(pMeta, pTask);
×
593
    return TSDB_CODE_INVALID_MSG;
×
594
  } else {
595
    tqDebug("vgId:%d s-task:%s received the checkpoint-ready msg from task:0x%x (vgId:%d), handle it", vgId,
×
596
            pTask->id.idStr, req.downstreamTaskId, req.downstreamNodeId);
597
  }
598

599
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
×
600
  streamMetaReleaseTask(pMeta, pTask);
×
601
  if (code) {
×
602
    return code;
×
603
  }
604

605
  {  // send checkpoint ready rsp
606
    SMStreamCheckpointReadyRspMsg* pReadyRsp = rpcMallocCont(sizeof(SMStreamCheckpointReadyRspMsg));
×
607
    if (pReadyRsp == NULL) {
×
608
      return terrno;
×
609
    }
610

611
    pReadyRsp->upstreamTaskId = req.upstreamTaskId;
×
612
    pReadyRsp->upstreamNodeId = req.upstreamNodeId;
×
613
    pReadyRsp->downstreamTaskId = req.downstreamTaskId;
×
614
    pReadyRsp->downstreamNodeId = req.downstreamNodeId;
×
615
    pReadyRsp->checkpointId = req.checkpointId;
×
616
    pReadyRsp->streamId = req.streamId;
×
617
    pReadyRsp->head.vgId = htonl(req.downstreamNodeId);
×
618

619
    SRpcMsg rsp = {.code = 0, .info = pMsg->info, .pCont = pReadyRsp, .contLen = sizeof(SMStreamCheckpointReadyRspMsg)};
×
620
    tmsgSendRsp(&rsp);
×
621

622
    pMsg->info.handle = NULL;  // disable auto rsp
×
623
  }
624

625
  return code;
×
626
}
627

628
int32_t tqStreamTaskProcessDeployReq(SStreamMeta* pMeta, SMsgCb* cb, int64_t sversion, char* msg, int32_t msgLen,
×
629
                                     bool isLeader, bool restored) {
630
  int32_t code = 0;
×
631
  int32_t vgId = pMeta->vgId;
×
632
  int32_t numOfTasks = 0;
×
633
  int32_t taskId = -1;
×
634
  int64_t streamId = -1;
×
635
  bool    added = false;
×
636
  int32_t size = sizeof(SStreamTask);
×
637

638
  if (tsDisableStream) {
×
639
    tqInfo("vgId:%d stream disabled, not deploy stream tasks", vgId);
×
640
    return code;
×
641
  }
642

643
  tqDebug("vgId:%d receive new stream task deploy msg, start to build stream task", vgId);
×
644

645
  // 1.deserialize msg and build task
646
  SStreamTask* pTask = taosMemoryCalloc(1, size);
×
647
  if (pTask == NULL) {
×
648
    tqError("vgId:%d failed to create stream task due to out of memory, alloc size:%d", vgId, size);
×
649
    return terrno;
×
650
  }
651

652
  SDecoder decoder;
653
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
×
654
  code = tDecodeStreamTask(&decoder, pTask);
×
655
  tDecoderClear(&decoder);
×
656

657
  if (code != TSDB_CODE_SUCCESS) {
×
658
    taosMemoryFree(pTask);
×
659
    return TSDB_CODE_INVALID_MSG;
×
660
  }
661

662
  // 2.save task, use the latest commit version as the initial start version of stream task.
663
  taskId = pTask->id.taskId;
×
664
  streamId = pTask->id.streamId;
×
665

666
  streamMetaWLock(pMeta);
×
667
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
×
668
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
669
  streamMetaWUnLock(pMeta);
×
670

671
  if (code < 0) {
×
672
    tqError("vgId:%d failed to register s-task:0x%x into meta, existed tasks:%d, code:%s", vgId, taskId, numOfTasks,
×
673
            tstrerror(code));
674
    return code;
×
675
  }
676

677
  // added into meta store, pTask cannot be reference since it may have been destroyed by other threads already now if
678
  // it is added into the meta store
679
  if (added) {
×
680
    // only handled in the leader node
681
    if (isLeader) {
×
682
      tqDebug("vgId:%d s-task:0x%x is deployed and add into meta, numOfTasks:%d", vgId, taskId, numOfTasks);
×
683

684
      if (restored) {
×
685
        SStreamTask* p = NULL;
×
686
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
×
687
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
×
688
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
×
689
        }
690

691
        if (p != NULL) {
×
692
          streamMetaReleaseTask(pMeta, p);
×
693
        }
694
      } else {
695
        tqWarn("s-task:0x%x not launched since vnode(vgId:%d) not ready", taskId, vgId);
×
696
      }
697

698
    } else {
699
      tqDebug("vgId:%d not leader, not launch stream task s-task:0x%x", vgId, taskId);
×
700
    }
701
  } else {
702
    tqWarn("vgId:%d failed to add s-task:0x%x, since already exists in meta store, total:%d", vgId, taskId, numOfTasks);
×
703
  }
704

705
  return code;
×
706
}
707

708
int32_t tqStreamTaskProcessDropReq(SStreamMeta* pMeta, char* msg, int32_t msgLen) {
×
709
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
×
710
  int32_t              code = 0;
×
711
  int32_t              vgId = pMeta->vgId;
×
712
  STaskId              hTaskId = {0};
×
713
  SStreamTask*         pTask = NULL;
×
714

715
  tqDebug("vgId:%d receive msg to drop s-task:0x%x", vgId, pReq->taskId);
×
716

717
  streamMetaWLock(pMeta);
×
718

719
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
720
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
721
  if (code == 0) {
×
722
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
723
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
×
724
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
×
725
    }
726

727
    // clear the relationship, and then release the stream tasks, to avoid invalid accessing of already freed
728
    // related stream(history) task
729
    streamTaskSetRemoveBackendFiles(pTask);
×
730
    code = streamTaskClearHTaskAttr(pTask, pReq->resetRelHalt);
×
731
    streamMetaReleaseTask(pMeta, pTask);
×
732

733
    if (code) {
×
734
      tqError("s-task:0x%x failed to clear related fill-history info, still exists", pReq->taskId);
×
735
    }
736
  }
737

738
  // drop the related fill-history task firstly
739
  if (hTaskId.taskId != 0 && hTaskId.streamId != 0) {
×
740
    tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x firstly", pReq->taskId, vgId, (int32_t)hTaskId.taskId);
×
741
    code = streamMetaUnregisterTask(pMeta, hTaskId.streamId, hTaskId.taskId);
×
742
    if (code) {
×
743
      tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x failed", pReq->taskId, vgId,
×
744
              (int32_t)hTaskId.taskId);
745
    }
746
  }
747

748
  // drop the stream task now
749
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
×
750
  if (code) {
×
751
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
752
  }
753

754
  // commit the update
755
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
756
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
×
757

758
  if (streamMetaCommit(pMeta) < 0) {
×
759
    // persist to disk
760
  }
761

762
  streamMetaWUnLock(pMeta);
×
763
  tqDebug("vgId:%d process drop task:0x%x completed", vgId, pReq->taskId);
×
764

765
  return 0;  // always return success
×
766
}
767

768
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
×
769
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
×
770
  int32_t                    code = 0;
×
771
  int32_t                    vgId = pMeta->vgId;
×
772
  SStreamTask*               pTask = NULL;
×
773

774
  tqDebug("vgId:%d receive msg to update-checkpoint-info for s-task:0x%x", vgId, pReq->taskId);
×
775

776
  streamMetaWLock(pMeta);
×
777

778
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
779
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
780
  if (code == 0) {
×
781
    code = streamTaskUpdateTaskCheckpointInfo(pTask, restored, pReq);
×
782
    streamMetaReleaseTask(pMeta, pTask);
×
783
  } else {  // failed to get the task.
784
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
785
    tqError(
×
786
        "vgId:%d failed to locate the s-task:0x%x to update the checkpoint info, numOfTasks:%d, it may have been "
787
        "dropped already",
788
        vgId, pReq->taskId, numOfTasks);
789
  }
790

791
  streamMetaWUnLock(pMeta);
×
792
  // always return success when handling the requirement issued by mnode during transaction.
793
  return TSDB_CODE_SUCCESS;
×
794
}
795

796
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
×
797
  int32_t vgId = pMeta->vgId;
×
798
  int32_t code = 0;
×
799
  int64_t st = taosGetTimestampMs();
×
800

801
  streamMetaWLock(pMeta);
×
802
  if (pMeta->startInfo.startAllTasks == 1) {
×
803
    pMeta->startInfo.restartCount += 1;
×
804
    tqDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
805
            pMeta->startInfo.restartCount);
806
    streamMetaWUnLock(pMeta);
×
807
    return TSDB_CODE_SUCCESS;
×
808
  }
809

810
  pMeta->startInfo.startAllTasks = 1;
×
811
  streamMetaWUnLock(pMeta);
×
812

813
  terrno = 0;
×
814
  tqInfo("vgId:%d tasks are all updated and stopped, restart all tasks, triggered by transId:%d, ts:%" PRId64, vgId,
×
815
         pMeta->updateInfo.completeTransId, pMeta->updateInfo.completeTs);
816

817
  streamMetaWLock(pMeta);
×
818
  streamMetaClear(pMeta);
×
819

820
  int64_t el = taosGetTimestampMs() - st;
×
821
  tqInfo("vgId:%d close&reload state elapsed time:%.3fs", vgId, el / 1000.);
×
822

823
  streamMetaLoadAllTasks(pMeta);
×
824

825
  {
826
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
827
    taosHashClear(pStartInfo->pReadyTaskSet);
×
828
    taosHashClear(pStartInfo->pFailedTaskSet);
×
829
    pStartInfo->readyTs = 0;
×
830
  }
831

832
  if (isLeader && !tsDisableStream) {
×
833
    streamMetaWUnLock(pMeta);
×
834
    code = streamMetaStartAllTasks(pMeta);
×
835
  } else {
836
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
×
837
    pMeta->startInfo.restartCount = 0;
×
838
    streamMetaWUnLock(pMeta);
×
839
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
×
840
  }
841

842
  code = terrno;
×
843
  return code;
×
844
}
845

846
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
×
847
  int32_t  code = 0;
×
848
  int32_t  vgId = pMeta->vgId;
×
849
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
850
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
851
  SDecoder decoder;
852

853
  SStreamTaskRunReq req = {0};
×
854
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
855
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
×
856
    tqError("vgId:%d failed to decode task run req, code:%s", pMeta->vgId, tstrerror(code));
×
857
    tDecoderClear(&decoder);
×
858
    return TSDB_CODE_SUCCESS;
×
859
  }
860

861
  tDecoderClear(&decoder);
×
862

863
  int32_t type = req.reqType;
×
864
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
×
865
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
×
866
    return 0;
×
867
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
×
868
    code = streamMetaStartAllTasks(pMeta);
×
869
    return 0;
×
870
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
×
871
    code = restartStreamTasks(pMeta, isLeader);
×
872
    return 0;
×
873
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
×
874
    code = streamMetaStopAllTasks(pMeta);
×
875
    return 0;
×
876
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
×
877
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
878
    return code;
×
879
  } else if (type == STREAM_EXEC_T_STOP_ONE_TASK) {
×
880
    code = streamMetaStopOneTask(pMeta, req.streamId, req.taskId);
×
881
    return code;
×
882
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
×
883
    SStreamTask* pTask = NULL;
×
884
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
885

886
    if (pTask != NULL && (code == 0)) {
×
887
      char* pStatus = NULL;
×
888
      if (streamTaskReadyToRun(pTask, &pStatus)) {
×
889
        int64_t execTs = pTask->status.lastExecTs;
×
890
        int32_t idle = taosGetTimestampMs() - execTs;
×
891
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
×
892

893
        code = streamResumeTask(pTask);
×
894
      } else {
895
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
896
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
897
                pTask->id.idStr, pStatus, status);
898
      }
899
      streamMetaReleaseTask(pMeta, pTask);
×
900
    }
901

902
    return code;
×
903
  }
904

905
  SStreamTask* pTask = NULL;
×
906
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
907
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
×
908
    char* p = NULL;
×
909
    if (streamTaskReadyToRun(pTask, &p)) {
×
910
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
×
911
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
912
      (void)streamExecTask(pTask);
×
913
    } else {
914
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
915
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
916
              pTask->id.idStr, p, status);
917
    }
918

919
    streamMetaReleaseTask(pMeta, pTask);
×
920
    return 0;
×
921
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
922
    // todo add one function to handle this
923
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
×
924
    return code;
×
925
  }
926
}
927

928
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
×
929
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
930
  int32_t         vgId = pMeta->vgId;
×
931
  bool            scanWal = false;
×
932
  int32_t         code = 0;
×
933

934
  streamMetaWLock(pMeta);
×
935
  if (pStartInfo->startAllTasks == 1) {
×
936
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
937
            pMeta->startInfo.restartCount);
938
  } else {  // not in starting procedure
939
    bool allReady = streamMetaAllTasksReady(pMeta);
×
940

941
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
×
942
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
943
      pStartInfo->restartCount -= 1;
×
944
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
×
945
              pStartInfo->restartCount);
946
      streamMetaWUnLock(pMeta);
×
947

948
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
×
949
    } else {
950
      if (pStartInfo->restartCount == 0) {
×
951
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
×
952
      } else if (allReady) {
×
953
        pStartInfo->restartCount = 0;
×
954
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
955
      }
956

957
      scanWal = true;
×
958
    }
959
  }
960

961
  streamMetaWUnLock(pMeta);
×
962

963
  return code;
×
964
}
965

966
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
967
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
968

969
  SStreamTask* pTask = NULL;
×
970
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
971
  if (pTask == NULL || (code != 0)) {
×
972
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
973
            pMeta->vgId, pReq->taskId);
974
    return TSDB_CODE_SUCCESS;
×
975
  }
976

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

979
  streamMutexLock(&pTask->lock);
×
980

981
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
982
  streamTaskClearCheckInfo(pTask, true);
×
983

984
  // clear flag set during do checkpoint, and open inputQ for all upstream tasks
985
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
986
  if (pState.state == TASK_STATUS__CK) {
×
987
    streamTaskSetStatusReady(pTask);
×
988
    tqDebug("s-task:%s reset checkpoint status to ready", pTask->id.idStr);
×
989
  } else if (pState.state == TASK_STATUS__UNINIT) {
×
990
    //    tqDebug("s-task:%s start task by checking downstream tasks", pTask->id.idStr);
991
    //    tqStreamTaskRestoreCheckpoint(pMeta, pTask->id.streamId, pTask->id.taskId);
992
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
993
  } else {
994
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
995
  }
996

997
  streamMutexUnlock(&pTask->lock);
×
998

999
  streamMetaReleaseTask(pMeta, pTask);
×
1000
  return TSDB_CODE_SUCCESS;
×
1001
}
1002

1003
int32_t tqStreamTaskProcessAllTaskStopReq(SStreamMeta* pMeta, SMsgCb* pMsgCb, SRpcMsg* pMsg) {
×
1004
  int32_t  code = 0;
×
1005
  int32_t  vgId = pMeta->vgId;
×
1006
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1007
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
1008
  SDecoder decoder;
1009

1010
  SStreamTaskStopReq req = {0};
×
1011
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1012
  if ((code = tDecodeStreamTaskStopReq(&decoder, &req)) < 0) {
×
1013
    tqError("vgId:%d failed to decode stop all streams, code:%s", pMeta->vgId, tstrerror(code));
×
1014
    tDecoderClear(&decoder);
×
1015
    return TSDB_CODE_SUCCESS;
×
1016
  }
1017

1018
  tDecoderClear(&decoder);
×
1019

1020
  // stop all stream tasks, only invoked when trying to drop db
1021
  if (req.streamId <= 0) {
×
1022
    tqDebug("vgId:%d recv msg to stop all tasks in sync before dropping vnode", vgId);
×
1023
    code = streamMetaStopAllTasks(pMeta);
×
1024
    if (code) {
×
1025
      tqError("vgId:%d failed to stop all tasks, code:%s", vgId, tstrerror(code));
×
1026
    }
1027

1028
  } else {  // stop only one stream tasks
1029

1030
  }
1031

1032
  // always return success
1033
  return TSDB_CODE_SUCCESS;
×
1034
}
1035

1036
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1037
  SRetrieveChkptTriggerReq req = {0};
×
1038
  SStreamTask*             pTask = NULL;
×
1039
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1040
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
1041
  SDecoder                 decoder = {0};
×
1042

1043
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1044
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1045
    tDecoderClear(&decoder);
×
1046
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
1047
    return TSDB_CODE_INVALID_MSG;
×
1048
  }
1049
  tDecoderClear(&decoder);
×
1050

1051
  int32_t code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
1052
  if (pTask == NULL || (code != 0)) {
×
1053
    tqError("vgId:%d process retrieve checkpoint-trigger, checkpointId:%" PRId64
×
1054
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
1055
            pMeta->vgId, req.checkpointId, (int32_t)req.downstreamTaskId, req.upstreamTaskId);
1056
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1057
  }
1058

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

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

1066
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1067
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
1068
    streamMetaReleaseTask(pMeta, pTask);
×
1069
    return code;
×
1070
  }
1071

1072
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1073
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1074
    int32_t transId = 0;
×
1075
    int64_t checkpointId = 0;
×
1076

1077
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
1078
    if (checkpointId != req.checkpointId) {
×
1079
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1080
              " req:%" PRId64,
1081
              pTask->id.idStr, req.downstreamTaskId, checkpointId, req.checkpointId);
1082
      streamMetaReleaseTask(pMeta, pTask);
×
1083
      return TSDB_CODE_INVALID_MSG;
×
1084
    }
1085

1086
    if (streamTaskAlreadySendTrigger(pTask, req.downstreamNodeId)) {
×
1087
      // re-send the lost checkpoint-trigger msg to downstream task
1088
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1089
              (int32_t)req.downstreamTaskId, checkpointId, transId);
1090
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1091
                                                TSDB_CODE_SUCCESS);
1092
    } else {  // not send checkpoint-trigger yet, wait
1093
      int32_t recv = 0, total = 0;
×
1094
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1095

1096
      if (recv == total) {  // add the ts info
×
1097
        tqWarn("s-task:%s all upstream send checkpoint-source/trigger, but not processed yet, wait", pTask->id.idStr);
×
1098
      } else {
1099
        tqWarn(
×
1100
            "s-task:%s not all upstream send checkpoint-source/trigger, total recv:%d/%d, wait for all upstream "
1101
            "sending checkpoint-source/trigger",
1102
            pTask->id.idStr, recv, total);
1103
      }
1104
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1105
                                                TSDB_CODE_ACTION_IN_PROGRESS);
1106
    }
1107
  } else {  // upstream not recv the checkpoint-source/trigger till now
1108
    if (!(pState.state == TASK_STATUS__READY || pState.state == TASK_STATUS__HALT)) {
×
1109
      tqFatal("s-task:%s invalid task status:%s", pTask->id.idStr, pState.name);
×
1110
    }
1111

1112
    tqWarn(
×
1113
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1114
        "upstream sending checkpoint-source/trigger",
1115
        pTask->id.idStr);
1116
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1117
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1118
  }
1119

1120
  streamMetaReleaseTask(pMeta, pTask);
×
1121
  return code;
×
1122
}
1123

1124
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1125
  SCheckpointTriggerRsp rsp = {0};
×
1126
  SStreamTask*          pTask = NULL;
×
1127
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1128
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
1129
  SDecoder              decoder = {0};
×
1130

1131
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1132
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1133
    tDecoderClear(&decoder);
×
1134
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1135
    return TSDB_CODE_INVALID_MSG;
×
1136
  }
1137
  tDecoderClear(&decoder);
×
1138

1139
  int32_t code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.taskId, &pTask);
×
1140
  if (pTask == NULL || (code != 0)) {
×
1141
    tqError(
×
1142
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1143
        pMeta->vgId, rsp.taskId);
1144
    return code;
×
1145
  }
1146

1147
  tqDebug(
×
1148
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1149
      ", transId:%d",
1150
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1151

1152
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
1153
  streamMetaReleaseTask(pMeta, pTask);
×
1154
  return code;
×
1155
}
1156

1157
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
×
1158
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
×
1159

1160
  SStreamTask* pTask = NULL;
×
1161
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
1162
  if (pTask == NULL || (code != 0)) {
×
1163
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
1164
            pReq->taskId);
1165
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1166
    return TSDB_CODE_SUCCESS;
×
1167
  }
1168

1169
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
×
1170
  streamTaskPause(pTask);
×
1171

1172
  SStreamTask* pHistoryTask = NULL;
×
1173
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
1174
    pHistoryTask = NULL;
×
1175
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
×
1176
    if (pHistoryTask == NULL || (code != 0)) {
×
1177
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1178
              ", it may have been dropped already",
1179
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
1180
      streamMetaReleaseTask(pMeta, pTask);
×
1181

1182
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1183
      return TSDB_CODE_SUCCESS;
×
1184
    }
1185

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

1188
    streamTaskPause(pHistoryTask);
×
1189
    streamMetaReleaseTask(pMeta, pHistoryTask);
×
1190
  }
1191

1192
  streamMetaReleaseTask(pMeta, pTask);
×
1193
  return TSDB_CODE_SUCCESS;
×
1194
}
1195

1196
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
×
1197
                                       bool fromVnode) {
1198
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
×
1199
  int32_t      vgId = pMeta->vgId;
×
1200
  int32_t      code = 0;
×
1201

1202
  streamTaskResume(pTask);
×
1203
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
1204

1205
  int32_t level = pTask->info.taskLevel;
×
1206
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
×
1207
    // no lock needs to secure the access of the version
1208
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
×
1209
      // discard all the data  when the stream task is suspended.
1210
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
×
1211
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
×
1212
              ", schedStatus:%d",
1213
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1214
    } else {  // from the previous paused version and go on
1215
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
×
1216
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1217
    }
1218

1219
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
×
1220
      pTask->hTaskInfo.operatorOpen = false;
×
1221
      code = streamStartScanHistoryAsync(pTask, igUntreated);
×
1222
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
×
1223
//      code = tqScanWalAsync((STQ*)handle, false);
1224
    } else {
1225
      code = streamTrySchedExec(pTask, false);
×
1226
    }
1227
  }
1228

1229
  return code;
×
1230
}
1231

1232
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
×
1233
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
×
1234

1235
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
×
1236

1237
  SStreamTask* pTask = NULL;
×
1238
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
1239
  if (pTask == NULL || (code != 0)) {
×
1240
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1241
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1242
  }
1243

1244
  streamMutexLock(&pTask->lock);
×
1245
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1246
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
×
1247
  streamMutexUnlock(&pTask->lock);
×
1248

1249
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
×
1250
  if (code != 0) {
×
1251
    streamMetaReleaseTask(pMeta, pTask);
×
1252
    return code;
×
1253
  }
1254

1255
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
×
1256
  SStreamTask* pHTask = NULL;
×
1257
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
×
1258
  if (pHTask && (code == 0)) {
×
1259
    streamMutexLock(&pHTask->lock);
×
1260
    SStreamTaskState p = streamTaskGetStatus(pHTask);
×
1261
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
×
1262
    streamMutexUnlock(&pHTask->lock);
×
1263

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

1267
    streamMetaReleaseTask(pMeta, pHTask);
×
1268
  }
1269

1270
  streamMetaReleaseTask(pMeta, pTask);
×
1271
  return TSDB_CODE_SUCCESS;
×
1272
}
1273

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

1276
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
×
1277
  rpcFreeCont(pMsg->pCont);
×
1278
  pMsg->pCont = NULL;
×
1279
  return TSDB_CODE_SUCCESS;
×
1280
}
1281

1282
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1283
  SMStreamHbRspMsg rsp = {0};
×
1284
  int32_t          code = 0;
×
1285
  SDecoder         decoder;
1286
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1287
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
×
1288

1289
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1290
  code = tDecodeStreamHbRsp(&decoder, &rsp);
×
1291
  if (code < 0) {
×
1292
    terrno = TSDB_CODE_INVALID_MSG;
×
1293
    tDecoderClear(&decoder);
×
1294
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1295
    return terrno;
×
1296
  }
1297

1298
  tDecoderClear(&decoder);
×
1299
  return streamProcessHeartbeatRsp(pMeta, &rsp);
×
1300
}
1301

1302
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
×
1303

1304
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
×
1305

1306
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1307
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
×
1308

1309
  SStreamTask* pTask = NULL;
×
1310
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
×
1311
  if (pTask == NULL || (code != 0)) {
×
1312
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
×
1313
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1314
    return code;
×
1315
  }
1316

1317
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
×
1318
  streamMetaReleaseTask(pMeta, pTask);
×
1319
  return code;
×
1320
}
1321

1322
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1323
  int32_t                vgId = pMeta->vgId;
×
1324
  int32_t                code = 0;
×
1325
  SStreamTask*           pTask = NULL;
×
1326
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1327
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
×
1328
  int64_t                now = taosGetTimestampMs();
×
1329
  SDecoder               decoder;
1330
  SRestoreCheckpointInfo req = {0};
×
1331

1332
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1333
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
×
1334
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1335
    tDecoderClear(&decoder);
×
1336
    return TSDB_CODE_SUCCESS;
×
1337
  }
1338

1339
  tDecoderClear(&decoder);
×
1340

1341
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
1342
  if (pTask == NULL || (code != 0)) {
×
1343
    tqError("vgId:%d process consensus checkpointId req, failed to acquire task:0x%x, it may have been dropped already",
×
1344
            pMeta->vgId, req.taskId);
1345
    // ignore this code to avoid error code over write
1346
    int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
1347
    if (ret) {
×
1348
      tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1349
    }
1350

1351
    return 0;
×
1352
  }
1353

1354
  // discard the rsp, since it is expired.
1355
  if (req.startTs < pTask->execInfo.created) {
×
1356
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
×
1357
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1358
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1359
           pTask->execInfo.created);
1360
    streamMetaAddFailedTaskSelf(pTask, now);
×
1361
    streamMetaReleaseTask(pMeta, pTask);
×
1362
    return TSDB_CODE_SUCCESS;
×
1363
  }
1364

1365
  tqDebug("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64 " from mnode",
×
1366
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId);
1367

1368
  streamMutexLock(&pTask->lock);
×
1369
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
×
1370
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1371
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1372

1373
    streamMutexUnlock(&pTask->lock);
×
1374
    streamMetaReleaseTask(pMeta, pTask);
×
1375
    return 0;
×
1376
  }
1377

1378
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
×
1379
  if (pConsenInfo->consenChkptTransId >= req.transId) {
×
1380
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1381
            pConsenInfo->consenChkptTransId, req.transId);
1382
    streamMutexUnlock(&pTask->lock);
×
1383
    streamMetaReleaseTask(pMeta, pTask);
×
1384
    return TSDB_CODE_SUCCESS;
×
1385
  }
1386

1387
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
×
1388
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1389
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1390
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1391
    tqSetRestoreVersionInfo(pTask);
×
1392
  } else {
1393
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
×
1394
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1395
  }
1396

1397
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
×
1398
  streamMutexUnlock(&pTask->lock);
×
1399

1400
  if (pMeta->role == NODE_ROLE_LEADER) {
×
1401
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
×
1402
    if (code) {
×
1403
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1404
    }
1405
  } else {
1406
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1407
  }
1408

1409
  streamMetaReleaseTask(pMeta, pTask);
×
1410
  return 0;
×
1411
}
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