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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

2.42
/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 != STREAM_NORMAL_TASK) {
×
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
    if (pTask->info.fillHistory == STREAM_RECALCUL_TASK) {
×
54
      pTask->pRecalState = streamStateRecalatedOpen(pMeta->path, pTask, pTask->id.streamId, pTask->id.taskId);
×
55
      if (pTask->pRecalState == NULL) {
×
56
        tqError("s-task:%s (vgId:%d) failed to open state for task, expand task failed", pTask->id.idStr, vgId);
×
57
        return terrno;
×
58
      } else {
59
        tqDebug("s-task:%s recal state:%p", pTask->id.idStr, pTask->pRecalState);
×
60
      }
61
    }
62

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

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

80
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE || pTask->info.taskLevel == TASK_LEVEL__MERGE) {
×
81
    handle.vnode = ((STQ*)pMeta->ahandle)->pVnode;
×
82
    handle.initTqReader = 1;
×
83
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
×
84
    handle.numOfVgroups = (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList);
×
85
  }
86

87
  initStorageAPI(&handle.api);
×
88

89
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE || pTask->info.taskLevel == TASK_LEVEL__AGG ||
×
90
      pTask->info.taskLevel == TASK_LEVEL__MERGE) {
×
91
    if (pTask->info.fillHistory == STREAM_RECALCUL_TASK) {
×
92
      handle.pStateBackend = pTask->pRecalState;
×
93
      handle.pOtherBackend = pTask->pState;
×
94
    } else {
95
      handle.pStateBackend = pTask->pState;
×
96
      handle.pOtherBackend = NULL;
×
97
    }
98

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

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

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

118
    qSetStreamMergeInfo(pTask->exec.pExecutor, pTask->pVTables);
×
119
  }
120

121
  streamSetupScheduleTrigger(pTask);
×
122

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

126
  return code;
×
127
}
128

129
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
×
130
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
×
131

132
  // checkpoint ver is the kept version, handled data should be the next version.
133
  if (pChkInfo->checkpointId != 0) {
×
134
    pChkInfo->nextProcessVer = pChkInfo->checkpointVer + 1;
×
135
    pChkInfo->processedVer = pChkInfo->checkpointVer;
×
136
    pTask->execInfo.startCheckpointId = pChkInfo->checkpointId;
×
137

138
    tqInfo("s-task:%s restore from the checkpointId:%" PRId64 " ver:%" PRId64 " currentVer:%" PRId64, pTask->id.idStr,
×
139
           pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
140
  }
141

142
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
×
143
}
×
144

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

153
  tqInfo("vgId:%d start all %d stream task(s) async", vgId, numOfTasks);
×
154

155
  int32_t type = restart ? STREAM_EXEC_T_RESTART_ALL_TASKS : STREAM_EXEC_T_START_ALL_TASKS;
×
156
  return streamTaskSchedTask(cb, vgId, 0, 0, type, false);
×
157
}
158

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

167
  tqDebug("vgId:%d start task:0x%x async", vgId, taskId);
×
168
  return streamTaskSchedTask(cb, vgId, streamId, taskId, STREAM_EXEC_T_START_ONE_TASK, false);
×
169
}
170

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

185
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
186
  code = tDecodeStreamTaskUpdateMsg(&decoder, &req);
×
187
  tDecoderClear(&decoder);
×
188

189
  if (code < 0) {
×
190
    rsp.code = TSDB_CODE_MSG_DECODE_ERROR;
×
191
    tqError("vgId:%d failed to decode task update msg, code:%s", vgId, tstrerror(code));
×
192
    tDestroyNodeUpdateMsg(&req);
×
193
    return rsp.code;
×
194
  }
195

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

203
  // update the nodeEpset when it exists
204
  streamMetaWLock(pMeta);
×
205

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

218
  const char* idstr = pTask->id.idStr;
×
219

220
  if (req.transId <= 0) {
×
221
    tqError("vgId:%d invalid update nodeEp task, transId:%d, discard", vgId, req.taskId);
×
222
    rsp.code = TSDB_CODE_SUCCESS;
×
223

224
    streamMetaReleaseTask(pMeta, pTask);
×
225
    streamMetaWUnLock(pMeta);
×
226

227
    tDestroyNodeUpdateMsg(&req);
×
228
    return rsp.code;
×
229
  }
230

231
  // info needs to be kept till the new trans to update the nodeEp arrived.
232
  bool update = streamMetaInitUpdateTaskList(pMeta, req.transId, req.pTaskList);
×
233
  if (!update) {
×
234
    rsp.code = TSDB_CODE_SUCCESS;
×
235

236
    streamMetaReleaseTask(pMeta, pTask);
×
237
    streamMetaWUnLock(pMeta);
×
238

239
    tDestroyNodeUpdateMsg(&req);
×
240
    return rsp.code;
×
241
  }
242

243
  // duplicate update epset msg received, discard this redundant message
244
  STaskUpdateEntry entry = {.streamId = req.streamId, .taskId = req.taskId, .transId = req.transId};
×
245

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

252
    streamMetaReleaseTask(pMeta, pTask);
×
253
    streamMetaWUnLock(pMeta);
×
254

255
    tDestroyNodeUpdateMsg(&req);
×
256
    return rsp.code;
×
257
  }
258

259
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
×
260

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

268
  streamTaskStopMonitorCheckRsp(&pTask->taskCheckInfo, pTask->id.idStr);
×
269

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

285
      streamTaskResetStatus(pHTask);
×
286
      streamTaskStopMonitorCheckRsp(&pHTask->taskCheckInfo, pHTask->id.idStr);
×
287
    }
288
  }
289

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

298
    if (pHTask != NULL) {
×
299
      code = streamMetaSaveTaskInMeta(pMeta, pHTask);
×
300
      if (code) {
×
301
        tqError("s-task:%s vgId:%d failed to save related history task, code:%s", idstr, vgId, tstrerror(code));
×
302
      }
303
    }
304
  } else {
305
    tqInfo("s-task:%s vgId:%d not save task since not update epset actually, stop task", idstr, vgId);
×
306
  }
307

308
  code = streamTaskStop(pTask);
×
309
  if (code) {
×
310
    tqError("s-task:%s vgId:%d failed to stop task, code:%s", idstr, vgId, tstrerror(code));
×
311
  }
312

313
  if (pHTask != NULL) {
×
314
    code = streamTaskStop(pHTask);
×
315
    if (code) {
×
316
      tqError("s-task:%s vgId:%d failed to stop related history task, code:%s", idstr, vgId, tstrerror(code));
×
317
    }
318
  }
319

320
  // keep info
321
  streamMetaAddIntoUpdateTaskList(pMeta, pTask, (pHTask != NULL) ? (pHTask) : NULL, req.transId, st);
×
322
  streamMetaReleaseTask(pMeta, pTask);
×
323
  streamMetaReleaseTask(pMeta, pHTask);
×
324

325
  rsp.code = TSDB_CODE_SUCCESS;
×
326

327
  // possibly only handle the stream task.
328
  int32_t reqUpdateTasks = taosArrayGetSize(req.pTaskList);
×
329
  int32_t updateTasks = taosHashGetSize(pMeta->updateInfo.pTasks);
×
330

331
  if (restored && isLeader) {
×
332
    tqDebug("vgId:%d s-task:0x%x update epset transId:%d, set the restart flag", vgId, req.taskId, req.transId);
×
333
    pMeta->startInfo.tasksWillRestart = 1;
×
334
  }
335

336
  if (updateTasks < reqUpdateTasks) {
×
337
    if (isLeader) {
×
338
      tqInfo("vgId:%d closed tasks:%d, unclosed:%d, all tasks will be started when nodeEp update completed", vgId,
×
339
              updateTasks, (reqUpdateTasks - updateTasks));
340
    } else {
341
      tqInfo("vgId:%d closed tasks:%d, unclosed:%d, follower not restart tasks", vgId, updateTasks,
×
342
              (reqUpdateTasks - updateTasks));
343
    }
344
  } else {
345
    if ((code = streamMetaCommit(pMeta)) < 0) {
×
346
      // always return true
347
      streamMetaWUnLock(pMeta);
×
348
      tDestroyNodeUpdateMsg(&req);
×
349
      tqError("vgId:%d commit meta failed, code:%s not restart the stream tasks", vgId, tstrerror(code));
×
350
      return TSDB_CODE_SUCCESS;
×
351
    }
352

353
    streamMetaClearSetUpdateTaskListComplete(pMeta);
×
354

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

373
  streamMetaWUnLock(pMeta);
×
374
  tDestroyNodeUpdateMsg(&req);
×
375
  return rsp.code;  // always return true
×
376
}
377

378
int32_t tqStreamTaskProcessDispatchReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
379
  char*   msgStr = pMsg->pCont;
×
380
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
381
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
×
382

383
  SStreamDispatchReq req = {0};
×
384

385
  SDecoder decoder;
386
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
387
  if (tDecodeStreamDispatchReq(&decoder, &req) < 0) {
×
388
    tDecoderClear(&decoder);
×
389
    return TSDB_CODE_MSG_DECODE_ERROR;
×
390
  }
391
  tDecoderClear(&decoder);
×
392

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

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

409
    SMsgHead* pRspHead = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
×
410
    if (pRspHead == NULL) {
×
411
      tqError("s-task:0x%x send dispatch error rsp, out of memory", req.taskId);
×
412
      return terrno;
×
413
    }
414

415
    pRspHead->vgId = htonl(req.upstreamNodeId);
×
416
    if (pRspHead->vgId == 0) {
×
417
      tqError("vgId:%d invalid dispatch msg from upstream to task:0x%x", pMeta->vgId, req.taskId);
×
418
      return TSDB_CODE_INVALID_MSG;
×
419
    }
420

421
    SStreamDispatchRsp* pRsp = POINTER_SHIFT(pRspHead, sizeof(SMsgHead));
×
422
    pRsp->streamId = htobe64(req.streamId);
×
423
    pRsp->upstreamTaskId = htonl(req.upstreamTaskId);
×
424
    pRsp->upstreamNodeId = htonl(req.upstreamNodeId);
×
425
    pRsp->downstreamNodeId = htonl(pMeta->vgId);
×
426
    pRsp->downstreamTaskId = htonl(req.taskId);
×
427
    pRsp->msgId = htonl(req.msgId);
×
428
    pRsp->stage = htobe64(req.stage);
×
429
    pRsp->inputStatus = TASK_OUTPUT_STATUS__NORMAL;
×
430

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

435
    tmsgSendRsp(&rsp);
×
436
    tCleanupStreamDispatchReq(&req);
×
437

438
    return 0;
×
439
  }
440
}
441

442
int32_t tqStreamTaskProcessDispatchRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
443
  SStreamDispatchRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
444

445
  int32_t vgId = pMeta->vgId;
×
446
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
×
447
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
×
448
  pRsp->streamId = htobe64(pRsp->streamId);
×
449
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
×
450
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
×
451
  pRsp->stage = htobe64(pRsp->stage);
×
452
  pRsp->msgId = htonl(pRsp->msgId);
×
453

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

457
  SStreamTask* pTask = NULL;
×
458
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
×
459
  if (pTask && (code == 0)) {
×
460
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
×
461
    streamMetaReleaseTask(pMeta, pTask);
×
462
    return code;
×
463
  } else {
464
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
×
465
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
466
  }
467
}
468

469
int32_t tqStreamTaskProcessRetrieveReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
470
  char*    msgStr = pMsg->pCont;
×
471
  char*    msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
472
  int32_t  msgLen = pMsg->contLen - sizeof(SMsgHead);
×
473
  int32_t  code = 0;
×
474
  SDecoder decoder;
475

476
  SStreamRetrieveReq req;
477
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
478
  code = tDecodeStreamRetrieveReq(&decoder, &req);
×
479
  tDecoderClear(&decoder);
×
480

481
  if (code) {
×
482
    tqError("vgId:%d failed to decode retrieve msg, discard it", pMeta->vgId);
×
483
    return code;
×
484
  }
485

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

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

499
  // if task is in ck status, set current ck failed
500
  streamTaskSetCheckpointFailed(pTask);
×
501

502
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
503
    code = streamProcessRetrieveReq(pTask, &req);
×
504
  } else {
505
    req.srcNodeId = pTask->info.nodeId;
×
506
    req.srcTaskId = pTask->id.taskId;
×
507
    code = streamTaskBroadcastRetrieveReq(pTask, &req);
×
508
  }
509

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

518
  streamMetaReleaseTask(pMeta, pTask);
×
519
  tCleanupStreamRetrieveReq(&req);
×
520

521
  // always return success, to disable the auto rsp
522
  return code;
×
523
}
524

525
int32_t tqStreamTaskProcessCheckReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
526
  char*   msgStr = pMsg->pCont;
×
527
  char*   msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
×
528
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
×
529
  int32_t code = 0;
×
530

531
  SStreamTaskCheckReq req;
532
  SStreamTaskCheckRsp rsp = {0};
×
533

534
  SDecoder decoder;
535

536
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
537
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
×
538
  tDecoderClear(&decoder);
×
539

540
  if (code) {
×
541
    tqError("vgId:%d decode check msg failed, not handle this msg", pMeta->vgId);
×
542
    return code;
×
543
  }
544

545
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
×
546
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
×
547
}
548

549
int32_t tqStreamTaskProcessCheckRsp(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
×
550
  char*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
551
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
552
  int32_t vgId = pMeta->vgId;
×
553
  int32_t code = TSDB_CODE_SUCCESS;
×
554

555
  SStreamTaskCheckRsp rsp;
556

557
  SDecoder decoder;
558
  tDecoderInit(&decoder, (uint8_t*)pReq, len);
×
559
  code = tDecodeStreamTaskCheckRsp(&decoder, &rsp);
×
560
  if (code < 0) {
×
561
    terrno = TSDB_CODE_INVALID_MSG;
×
562
    tDecoderClear(&decoder);
×
563
    tqError("vgId:%d failed to parse check rsp msg, code:%s", vgId, tstrerror(terrno));
×
564
    return -1;
×
565
  }
566

567
  tDecoderClear(&decoder);
×
568
  tqDebug("tq task:0x%x (vgId:%d) recv check rsp(QID:0x%" PRIx64 ") from 0x%x (vgId:%d) status %d", rsp.upstreamTaskId,
×
569
          rsp.upstreamNodeId, rsp.reqId, rsp.downstreamTaskId, rsp.downstreamNodeId, rsp.status);
570

571
  if (!isLeader) {
×
572
    tqError("vgId:%d not leader, task:0x%x not handle the check rsp, downstream:0x%x (vgId:%d)", vgId,
×
573
            rsp.upstreamTaskId, rsp.downstreamTaskId, rsp.downstreamNodeId);
574
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId, true);
×
575
  }
576

577
  SStreamTask* pTask = NULL;
×
578
  code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.upstreamTaskId, &pTask);
×
579
  if ((pTask == NULL) || (code != 0)) {
×
580
    return streamMetaAddFailedTask(pMeta, rsp.streamId, rsp.upstreamTaskId, true);
×
581
  }
582

583
  code = streamTaskProcessCheckRsp(pTask, &rsp);
×
584
  streamMetaReleaseTask(pMeta, pTask);
×
585
  return code;
×
586
}
587

588
int32_t tqStreamTaskProcessCheckpointReadyMsg(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
589
  int32_t vgId = pMeta->vgId;
×
590
  char*   msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
591
  int32_t len = pMsg->contLen - sizeof(SMsgHead);
×
592
  int32_t code = 0;
×
593

594
  SStreamCheckpointReadyMsg req = {0};
×
595

596
  SDecoder decoder;
597
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
598
  if (tDecodeStreamCheckpointReadyMsg(&decoder, &req) < 0) {
×
599
    code = TSDB_CODE_MSG_DECODE_ERROR;
×
600
    tDecoderClear(&decoder);
×
601
    return code;
×
602
  }
603
  tDecoderClear(&decoder);
×
604

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

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

622
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
×
623
  streamMetaReleaseTask(pMeta, pTask);
×
624
  if (code) {
×
625
    return code;
×
626
  }
627

628
  {  // send checkpoint ready rsp
629
    SMStreamCheckpointReadyRspMsg* pReadyRsp = rpcMallocCont(sizeof(SMStreamCheckpointReadyRspMsg));
×
630
    if (pReadyRsp == NULL) {
×
631
      return terrno;
×
632
    }
633

634
    pReadyRsp->upstreamTaskId = req.upstreamTaskId;
×
635
    pReadyRsp->upstreamNodeId = req.upstreamNodeId;
×
636
    pReadyRsp->downstreamTaskId = req.downstreamTaskId;
×
637
    pReadyRsp->downstreamNodeId = req.downstreamNodeId;
×
638
    pReadyRsp->checkpointId = req.checkpointId;
×
639
    pReadyRsp->streamId = req.streamId;
×
640
    pReadyRsp->head.vgId = htonl(req.downstreamNodeId);
×
641

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

645
    pMsg->info.handle = NULL;  // disable auto rsp
×
646
  }
647

648
  return code;
×
649
}
650

651
int32_t tqStreamTaskProcessDeployReq(SStreamMeta* pMeta, SMsgCb* cb, int64_t sversion, char* msg, int32_t msgLen,
×
652
                                     bool isLeader, bool restored) {
653
  int32_t code = 0;
×
654
  int32_t vgId = pMeta->vgId;
×
655
  int32_t numOfTasks = 0;
×
656
  int32_t taskId = -1;
×
657
  int64_t streamId = -1;
×
658
  bool    added = false;
×
659
  int32_t size = sizeof(SStreamTask);
×
660

661
  if (tsDisableStream) {
×
662
    tqInfo("vgId:%d stream disabled, not deploy stream tasks", vgId);
×
663
    return code;
×
664
  }
665

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

668
  // 1.deserialize msg and build task
669
  SStreamTask* pTask = taosMemoryCalloc(1, size);
×
670
  if (pTask == NULL) {
×
671
    tqError("vgId:%d failed to create stream task due to out of memory, alloc size:%d", vgId, size);
×
672
    return terrno;
×
673
  }
674

675
  SDecoder decoder;
676
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
×
677
  code = tDecodeStreamTask(&decoder, pTask);
×
678
  tDecoderClear(&decoder);
×
679

680
  if (code != TSDB_CODE_SUCCESS) {
×
681
    taosMemoryFree(pTask);
×
682
    return TSDB_CODE_INVALID_MSG;
×
683
  }
684

685
  // 2.save task, use the latest commit version as the initial start version of stream task.
686
  taskId = pTask->id.taskId;
×
687
  streamId = pTask->id.streamId;
×
688

689
  streamMetaWLock(pMeta);
×
690
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
×
691
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
692
  streamMetaWUnLock(pMeta);
×
693

694
  if (code < 0) {
×
695
    tqError("vgId:%d failed to register s-task:0x%x into meta, existed tasks:%d, code:%s", vgId, taskId, numOfTasks,
×
696
            tstrerror(code));
697
    return code;
×
698
  }
699

700
  // added into meta store, pTask cannot be reference since it may have been destroyed by other threads already now if
701
  // it is added into the meta store
702
  if (added) {
×
703
    // only handled in the leader node
704
    if (isLeader) {
×
705
      tqDebug("vgId:%d s-task:0x%x is deployed and add into meta, numOfTasks:%d", vgId, taskId, numOfTasks);
×
706

707
      if (restored) {
×
708
        SStreamTask* p = NULL;
×
709
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
×
710
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
×
711
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
×
712
        }
713

714
        if (p != NULL) {
×
715
          streamMetaReleaseTask(pMeta, p);
×
716
        }
717
      } else {
718
        tqWarn("s-task:0x%x not launched since vnode(vgId:%d) not ready", taskId, vgId);
×
719
      }
720

721
    } else {
722
      tqDebug("vgId:%d not leader, not launch stream task s-task:0x%x", vgId, taskId);
×
723
    }
724
  } else {
725
    tqWarn("vgId:%d failed to add s-task:0x%x, since already exists in meta store, total:%d", vgId, taskId, numOfTasks);
×
726
  }
727

728
  return code;
×
729
}
730

731
int32_t tqStreamTaskProcessDropReq(SStreamMeta* pMeta, char* msg, int32_t msgLen) {
×
732
  SVDropStreamTaskReq* pReq = (SVDropStreamTaskReq*)msg;
×
733
  int32_t              code = 0;
×
734
  int32_t              vgId = pMeta->vgId;
×
735
  STaskId              hTaskId = {0};
×
736
  SStreamTask*         pTask = NULL;
×
737

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

740
  streamMetaWLock(pMeta);
×
741

742
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
743
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
744
  if (code == 0) {
×
745
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
746
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
×
747
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
×
748
    }
749

750
    // clear the relationship, and then release the stream tasks, to avoid invalid accessing of already freed
751
    // related stream(history) task
752
    streamTaskSetRemoveBackendFiles(pTask);
×
753
    code = streamTaskClearHTaskAttr(pTask, pReq->resetRelHalt);
×
754
    streamMetaReleaseTask(pMeta, pTask);
×
755

756
    if (code) {
×
757
      tqError("s-task:0x%x failed to clear related fill-history info, still exists", pReq->taskId);
×
758
    }
759
  }
760

761
  // drop the related fill-history task firstly
762
  if (hTaskId.taskId != 0 && hTaskId.streamId != 0) {
×
763
    tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x firstly", pReq->taskId, vgId, (int32_t)hTaskId.taskId);
×
764
    code = streamMetaUnregisterTask(pMeta, hTaskId.streamId, hTaskId.taskId);
×
765
    if (code) {
×
766
      tqDebug("s-task:0x%x vgId:%d drop rel fill-history task:0x%x failed", pReq->taskId, vgId,
×
767
              (int32_t)hTaskId.taskId);
768
    }
769
  }
770

771
  // drop the stream task now
772
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
×
773
  if (code) {
×
774
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
775
  }
776

777
  // commit the update
778
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
779
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
×
780
  if (numOfTasks == 0) {
×
781
    streamMetaResetStartInfo(&pMeta->startInfo, vgId);
×
782
  }
783

784
  if (streamMetaCommit(pMeta) < 0) {
×
785
    // persist to disk
786
  }
787

788
  streamMetaWUnLock(pMeta);
×
789
  tqDebug("vgId:%d process drop task:0x%x completed", vgId, pReq->taskId);
×
790

791
  return 0;  // always return success
×
792
}
793

794
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
×
795
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
×
796
  int32_t                    code = 0;
×
797
  int32_t                    vgId = pMeta->vgId;
×
798
  SStreamTask*               pTask = NULL;
×
799

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

802
  streamMetaWLock(pMeta);
×
803

804
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
805
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
806
  if (code == 0) {
×
807
    code = streamTaskUpdateTaskCheckpointInfo(pTask, restored, pReq);
×
808
    streamMetaReleaseTask(pMeta, pTask);
×
809
  } else {  // failed to get the task.
810
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
811
    tqError(
×
812
        "vgId:%d failed to locate the s-task:0x%x to update the checkpoint info, numOfTasks:%d, it may have been "
813
        "dropped already",
814
        vgId, pReq->taskId, numOfTasks);
815
  }
816

817
  streamMetaWUnLock(pMeta);
×
818
  // always return success when handling the requirement issued by mnode during transaction.
819
  return TSDB_CODE_SUCCESS;
×
820
}
821

822
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
×
823
  int32_t         vgId = pMeta->vgId;
×
824
  int32_t         code = 0;
×
825
  int64_t         st = taosGetTimestampMs();
×
826
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
827

828
  if (pStartInfo->startAllTasks == 1) {
×
829
    // wait for the checkpoint id rsp, this rsp will be expired
830
    if (pStartInfo->curStage == START_MARK_REQ_CHKPID) {
×
831
      SStartTaskStageInfo* pCurStageInfo = taosArrayGetLast(pStartInfo->pStagesList);
×
832
      tqInfo("vgId:%d only mark the req consensus checkpointId flag, reqTs:%"PRId64 " ignore and continue", vgId, pCurStageInfo->ts);
×
833

834
      taosArrayClear(pStartInfo->pStagesList);
×
835
      pStartInfo->curStage = 0;
×
836
      goto _start;
×
837

838
    } else if (pStartInfo->curStage == START_WAIT_FOR_CHKPTID) {
×
839
      SStartTaskStageInfo* pCurStageInfo = taosArrayGetLast(pStartInfo->pStagesList);
×
840
      tqInfo("vgId:%d already sent consensus-checkpoint msg(waiting for chkptid) expired, reqTs:%" PRId64
×
841
             " rsp will be discarded",
842
             vgId, pCurStageInfo->ts);
843

844
      taosArrayClear(pStartInfo->pStagesList);
×
845
      pStartInfo->curStage = 0;
×
846
      goto _start;
×
847

848
    } else if (pStartInfo->curStage == START_CHECK_DOWNSTREAM) {
×
849
      int32_t numOfRecv = taosHashGetSize(pStartInfo->pReadyTaskSet);
×
850
      taosHashGetSize(pStartInfo->pFailedTaskSet);
×
851

852
      int32_t newTotal = taosArrayGetSize(pStartInfo->pRecvChkptIdTasks);
×
853
      tqDebug(
×
854
          "vgId:%d start all tasks procedure is interrupted by transId:%d, wait for partial tasks rsp. recv check "
855
          "downstream results, received:%d results, total req tasks:%d",
856
          vgId, pMeta->updateInfo.activeTransId, numOfRecv, newTotal);
857

858
      bool allRsp = allCheckDownstreamRspPartial(pStartInfo, newTotal, pMeta->vgId);
×
859
      if (allRsp) {
×
860
        tqDebug("vgId:%d all partial results received, continue the restart procedure", pMeta->vgId);
×
861
        streamMetaResetStartInfo(pStartInfo, vgId);
×
862
        goto _start;
×
863
      } else {
864
        pStartInfo->restartCount += 1;
×
865
        SStartTaskStageInfo* pCurStageInfo = taosArrayGetLast(pStartInfo->pStagesList);
×
866

867
        tqDebug("vgId:%d in start tasks procedure (check downstream), reqTs:%" PRId64
×
868
                ", inc restartCounter by 1 and wait for it completes, "
869
                "remaining restart:%d",
870
                vgId, pCurStageInfo->ts, pStartInfo->restartCount);
871
      }
872
    } else {
873
      tqInfo("vgId:%d in start procedure, but not start to do anything yet, do nothing", vgId);
×
874
    }
875

876
    return TSDB_CODE_SUCCESS;
×
877
  }
878

879
_start:
×
880

881
  pStartInfo->startAllTasks = 1;
×
882
  terrno = 0;
×
883
  tqInfo("vgId:%d tasks are all updated and stopped, restart all tasks, triggered by transId:%d, ts:%" PRId64, vgId,
×
884
         pMeta->updateInfo.completeTransId, pMeta->updateInfo.completeTs);
885

886
  streamMetaClear(pMeta);
×
887

888
  int64_t el = taosGetTimestampMs() - st;
×
889
  tqInfo("vgId:%d clear&close stream meta completed, elapsed time:%.3fs", vgId, el / 1000.);
×
890

891
  streamMetaLoadAllTasks(pMeta);
×
892

893
  if (isLeader && !tsDisableStream) {
×
894
    code = streamMetaStartAllTasks(pMeta);
×
895
  } else {
896
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
×
897
    pStartInfo->restartCount = 0;
×
898
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
×
899
  }
900

901
  code = terrno;
×
902
  return code;
×
903
}
904

905
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
53✔
906
  int32_t  code = 0;
53✔
907
  int32_t  vgId = pMeta->vgId;
53✔
908
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
53✔
909
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
53✔
910
  SDecoder decoder;
911

912
  SStreamTaskRunReq req = {0};
53✔
913
  tDecoderInit(&decoder, (uint8_t*)msg, len);
53✔
914
  if ((code = tDecodeStreamTaskRunReq(&decoder, &req)) < 0) {
53!
915
    tqError("vgId:%d failed to decode task run req, code:%s", pMeta->vgId, tstrerror(code));
×
916
    tDecoderClear(&decoder);
×
917
    return TSDB_CODE_SUCCESS;
×
918
  }
919

920
  tDecoderClear(&decoder);
53✔
921

922
  int32_t type = req.reqType;
53✔
923
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
53!
924
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
×
925
    return 0;
×
926
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
53!
927
    streamMetaWLock(pMeta);
53✔
928
    code = streamMetaStartAllTasks(pMeta);
53✔
929
    streamMetaWUnLock(pMeta);
53✔
930
    return 0;
53✔
931
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
×
932
    streamMetaWLock(pMeta);
×
933
    code = restartStreamTasks(pMeta, isLeader);
×
934
    streamMetaWUnLock(pMeta);
×
935
    return 0;
×
936
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
×
937
    code = streamMetaStopAllTasks(pMeta);
×
938
    return 0;
×
939
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
×
940
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId, true);
×
941
    return code;
×
942
  } else if (type == STREAM_EXEC_T_STOP_ONE_TASK) {
×
943
    code = streamMetaStopOneTask(pMeta, req.streamId, req.taskId);
×
944
    return code;
×
945
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
×
946
    SStreamTask* pTask = NULL;
×
947
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
948

949
    if (pTask != NULL && (code == 0)) {
×
950
      char* pStatus = NULL;
×
951
      if (streamTaskReadyToRun(pTask, &pStatus)) {
×
952
        int64_t execTs = pTask->status.lastExecTs;
×
953
        int32_t idle = taosGetTimestampMs() - execTs;
×
954
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
×
955

956
        code = streamResumeTask(pTask);
×
957
      } else {
958
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
959
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
960
                pTask->id.idStr, pStatus, status);
961
      }
962
      streamMetaReleaseTask(pMeta, pTask);
×
963
    }
964

965
    return code;
×
966
  }
967

968
  SStreamTask* pTask = NULL;
×
969
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
970
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
×
971
    char* p = NULL;
×
972
    if (streamTaskReadyToRun(pTask, &p)) {
×
973
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
×
974
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
975
      (void)streamExecTask(pTask);
×
976
    } else {
977
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
978
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
979
              pTask->id.idStr, p, status);
980
    }
981

982
    streamMetaReleaseTask(pMeta, pTask);
×
983
    return 0;
×
984
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
985
    // todo add one function to handle this
986
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
×
987
    return code;
×
988
  }
989
}
990

991
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
×
992
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
993
  int32_t         vgId = pMeta->vgId;
×
994
  bool            scanWal = false;
×
995
  int32_t         code = 0;
×
996

997
//  streamMetaWLock(pMeta);
998
  if (pStartInfo->startAllTasks == 1) {
×
999
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
1000
            pMeta->startInfo.restartCount);
1001
  } else {  // not in starting procedure
1002
    bool allReady = streamMetaAllTasksReady(pMeta);
×
1003

1004
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
×
1005
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
1006
      pStartInfo->restartCount -= 1;
×
1007
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
×
1008
              pStartInfo->restartCount);
1009

1010
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
×
1011
    } else {
1012
      if (pStartInfo->restartCount == 0) {
×
1013
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
×
1014
      } else if (allReady) {
×
1015
        pStartInfo->restartCount = 0;
×
1016
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
1017
      }
1018

1019
      scanWal = true;
×
1020
    }
1021
  }
1022

1023
//  streamMetaWUnLock(pMeta);
1024

1025
  return code;
×
1026
}
1027

1028
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
1029
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
1030

1031
  SStreamTask* pTask = NULL;
×
1032
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
1033
  if (pTask == NULL || (code != 0)) {
×
1034
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
1035
            pMeta->vgId, pReq->taskId);
1036
    return TSDB_CODE_SUCCESS;
×
1037
  }
1038

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

1041
  streamMutexLock(&pTask->lock);
×
1042

1043
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
1044
  streamTaskClearCheckInfo(pTask, true);
×
1045

1046
  // clear flag set during do checkpoint, and open inputQ for all upstream tasks
1047
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1048
  if (pState.state == TASK_STATUS__CK) {
×
1049
    streamTaskSetStatusReady(pTask);
×
1050
    tqDebug("s-task:%s reset checkpoint status to ready", pTask->id.idStr);
×
1051
  } else if (pState.state == TASK_STATUS__UNINIT) {
×
1052
    //    tqDebug("s-task:%s start task by checking downstream tasks", pTask->id.idStr);
1053
    //    tqStreamTaskRestoreCheckpoint(pMeta, pTask->id.streamId, pTask->id.taskId);
1054
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
1055
  } else {
1056
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
1057
  }
1058

1059
  streamMutexUnlock(&pTask->lock);
×
1060

1061
  streamMetaReleaseTask(pMeta, pTask);
×
1062
  return TSDB_CODE_SUCCESS;
×
1063
}
1064

1065
int32_t tqStreamTaskProcessAllTaskStopReq(SStreamMeta* pMeta, SMsgCb* pMsgCb, SRpcMsg* pMsg) {
16✔
1066
  int32_t  code = 0;
16✔
1067
  int32_t  vgId = pMeta->vgId;
16✔
1068
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
16✔
1069
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
16✔
1070
  SDecoder decoder;
1071

1072
  SStreamTaskStopReq req = {0};
16✔
1073
  tDecoderInit(&decoder, (uint8_t*)msg, len);
16✔
1074
  if ((code = tDecodeStreamTaskStopReq(&decoder, &req)) < 0) {
16!
1075
    tqError("vgId:%d failed to decode stop all streams, code:%s", pMeta->vgId, tstrerror(code));
×
1076
    tDecoderClear(&decoder);
×
1077
    return TSDB_CODE_SUCCESS;
×
1078
  }
1079

1080
  tDecoderClear(&decoder);
16✔
1081

1082
  // stop all stream tasks, only invoked when trying to drop db
1083
  if (req.streamId <= 0) {
16!
1084
    tqDebug("vgId:%d recv msg to stop all tasks in sync before dropping vnode", vgId);
16!
1085
    code = streamMetaStopAllTasks(pMeta);
16✔
1086
    if (code) {
16!
1087
      tqError("vgId:%d failed to stop all tasks, code:%s", vgId, tstrerror(code));
×
1088
    }
1089

1090
  } else {  // stop only one stream tasks
1091

1092
  }
1093

1094
  // always return success
1095
  return TSDB_CODE_SUCCESS;
16✔
1096
}
1097

1098
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1099
  SRetrieveChkptTriggerReq req = {0};
×
1100
  SStreamTask*             pTask = NULL;
×
1101
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1102
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
1103
  SDecoder                 decoder = {0};
×
1104

1105
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1106
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1107
    tDecoderClear(&decoder);
×
1108
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
1109
    return TSDB_CODE_INVALID_MSG;
×
1110
  }
1111
  tDecoderClear(&decoder);
×
1112

1113
  int32_t code = streamMetaAcquireTask(pMeta, req.streamId, req.upstreamTaskId, &pTask);
×
1114
  if (pTask == NULL || (code != 0)) {
×
1115
    tqError("vgId:%d process retrieve checkpoint-trigger, checkpointId:%" PRId64
×
1116
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
1117
            pMeta->vgId, req.checkpointId, (int32_t)req.downstreamTaskId, req.upstreamTaskId);
1118
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1119
  }
1120

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

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

1128
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1129
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
1130
    streamMetaReleaseTask(pMeta, pTask);
×
1131
    return code;
×
1132
  }
1133

1134
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1135
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1136
    int32_t transId = 0;
×
1137
    int64_t checkpointId = 0;
×
1138

1139
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
1140
    if (checkpointId != req.checkpointId) {
×
1141
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1142
              " req:%" PRId64,
1143
              pTask->id.idStr, req.downstreamTaskId, checkpointId, req.checkpointId);
1144
      streamMetaReleaseTask(pMeta, pTask);
×
1145
      return TSDB_CODE_INVALID_MSG;
×
1146
    }
1147

1148
    if (streamTaskAlreadySendTrigger(pTask, req.downstreamNodeId)) {
×
1149
      // re-send the lost checkpoint-trigger msg to downstream task
1150
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1151
              (int32_t)req.downstreamTaskId, checkpointId, transId);
1152
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1153
                                                TSDB_CODE_SUCCESS);
1154
    } else {  // not send checkpoint-trigger yet, wait
1155
      int32_t recv = 0, total = 0;
×
1156
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1157

1158
      if (recv == total) {  // add the ts info
×
1159
        tqWarn("s-task:%s all upstream send checkpoint-source/trigger, but not processed yet, wait", pTask->id.idStr);
×
1160
      } else {
1161
        tqWarn(
×
1162
            "s-task:%s not all upstream send checkpoint-source/trigger, total recv:%d/%d, wait for all upstream "
1163
            "sending checkpoint-source/trigger",
1164
            pTask->id.idStr, recv, total);
1165
      }
1166
      code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1167
                                                TSDB_CODE_ACTION_IN_PROGRESS);
1168
    }
1169
  } else {  // upstream not recv the checkpoint-source/trigger till now
1170
    if (!(pState.state == TASK_STATUS__READY || pState.state == TASK_STATUS__HALT)) {
×
1171
      tqFatal("s-task:%s invalid task status:%s", pTask->id.idStr, pState.name);
×
1172
    }
1173

1174
    tqWarn(
×
1175
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1176
        "upstream sending checkpoint-source/trigger",
1177
        pTask->id.idStr);
1178
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1179
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1180
  }
1181

1182
  streamMetaReleaseTask(pMeta, pTask);
×
1183
  return code;
×
1184
}
1185

1186
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1187
  SCheckpointTriggerRsp rsp = {0};
×
1188
  SStreamTask*          pTask = NULL;
×
1189
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1190
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
1191
  SDecoder              decoder = {0};
×
1192

1193
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1194
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1195
    tDecoderClear(&decoder);
×
1196
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1197
    return TSDB_CODE_INVALID_MSG;
×
1198
  }
1199
  tDecoderClear(&decoder);
×
1200

1201
  int32_t code = streamMetaAcquireTask(pMeta, rsp.streamId, rsp.taskId, &pTask);
×
1202
  if (pTask == NULL || (code != 0)) {
×
1203
    tqError(
×
1204
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1205
        pMeta->vgId, rsp.taskId);
1206
    return code;
×
1207
  }
1208

1209
  tqDebug(
×
1210
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1211
      ", transId:%d",
1212
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1213

1214
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
1215
  streamMetaReleaseTask(pMeta, pTask);
×
1216
  return code;
×
1217
}
1218

1219
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
×
1220
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
×
1221

1222
  SStreamTask* pTask = NULL;
×
1223
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
1224
  if (pTask == NULL || (code != 0)) {
×
1225
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
1226
            pReq->taskId);
1227
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1228
    return TSDB_CODE_SUCCESS;
×
1229
  }
1230

1231
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
×
1232
  streamTaskPause(pTask);
×
1233

1234
  SStreamTask* pHistoryTask = NULL;
×
1235
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
1236
    pHistoryTask = NULL;
×
1237
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
×
1238
    if (pHistoryTask == NULL || (code != 0)) {
×
1239
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1240
              ", it may have been dropped already",
1241
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
1242
      streamMetaReleaseTask(pMeta, pTask);
×
1243

1244
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1245
      return TSDB_CODE_SUCCESS;
×
1246
    }
1247

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

1250
    streamTaskPause(pHistoryTask);
×
1251
    streamMetaReleaseTask(pMeta, pHistoryTask);
×
1252
  }
1253

1254
  streamMetaReleaseTask(pMeta, pTask);
×
1255
  return TSDB_CODE_SUCCESS;
×
1256
}
1257

1258
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
×
1259
                                       bool fromVnode) {
1260
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
×
1261
  int32_t      vgId = pMeta->vgId;
×
1262
  int32_t      code = 0;
×
1263

1264
  streamTaskResume(pTask);
×
1265
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
1266

1267
  int32_t level = pTask->info.taskLevel;
×
1268
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
×
1269
    // no lock needs to secure the access of the version
1270
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
×
1271
      // discard all the data  when the stream task is suspended.
1272
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
×
1273
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
×
1274
              ", schedStatus:%d",
1275
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1276
    } else {  // from the previous paused version and go on
1277
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
×
1278
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1279
    }
1280

1281
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
×
1282
      pTask->hTaskInfo.operatorOpen = false;
×
1283
      code = streamStartScanHistoryAsync(pTask, igUntreated);
×
1284
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
×
1285
      //      code = tqScanWalAsync((STQ*)handle, false);
1286
    } else {
1287
      code = streamTrySchedExec(pTask, false);
×
1288
    }
1289
  }
1290

1291
  return code;
×
1292
}
1293

1294
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
×
1295
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
×
1296

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

1299
  SStreamTask* pTask = NULL;
×
1300
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
1301
  if (pTask == NULL || (code != 0)) {
×
1302
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1303
    return TSDB_CODE_SUCCESS;
×
1304
  }
1305

1306
  streamMutexLock(&pTask->lock);
×
1307
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1308
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
×
1309
  streamMutexUnlock(&pTask->lock);
×
1310

1311
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
×
1312
  if (code != 0) {
×
1313
    streamMetaReleaseTask(pMeta, pTask);
×
1314
    tqError("s-task:%s failed to resume tasks, code:%s", pTask->id.idStr, tstrerror(code));
×
1315
    return TSDB_CODE_SUCCESS;
×
1316
  }
1317

1318
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
×
1319
  SStreamTask* pHTask = NULL;
×
1320
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
×
1321
  if (pHTask && (code == 0)) {
×
1322
    streamMutexLock(&pHTask->lock);
×
1323
    SStreamTaskState p = streamTaskGetStatus(pHTask);
×
1324
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
×
1325
    streamMutexUnlock(&pHTask->lock);
×
1326

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

1330
    streamMetaReleaseTask(pMeta, pHTask);
×
1331
  }
1332

1333
  streamMetaReleaseTask(pMeta, pTask);
×
1334
  return TSDB_CODE_SUCCESS;
×
1335
}
1336

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

1339
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
×
1340
  rpcFreeCont(pMsg->pCont);
×
1341
  pMsg->pCont = NULL;
×
1342
  return TSDB_CODE_SUCCESS;
×
1343
}
1344

1345
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1346
  SMStreamHbRspMsg rsp = {0};
×
1347
  int32_t          code = 0;
×
1348
  SDecoder         decoder;
1349
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1350
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
×
1351

1352
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1353
  code = tDecodeStreamHbRsp(&decoder, &rsp);
×
1354
  if (code < 0) {
×
1355
    terrno = TSDB_CODE_INVALID_MSG;
×
1356
    tDecoderClear(&decoder);
×
1357
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1358
    return terrno;
×
1359
  }
1360

1361
  tDecoderClear(&decoder);
×
1362
  return streamProcessHeartbeatRsp(pMeta, &rsp);
×
1363
}
1364

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

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

1369
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1370
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
×
1371

1372
  SStreamTask* pTask = NULL;
×
1373
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
×
1374
  if (pTask == NULL || (code != 0)) {
×
1375
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
×
1376
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1377
    return code;
×
1378
  }
1379

1380
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
×
1381
  streamMetaReleaseTask(pMeta, pTask);
×
1382
  return code;
×
1383
}
1384

1385
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1386
  int32_t                vgId = pMeta->vgId;
×
1387
  int32_t                code = 0;
×
1388
  SStreamTask*           pTask = NULL;
×
1389
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1390
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
×
1391
  int64_t                now = taosGetTimestampMs();
×
1392
  SDecoder               decoder;
1393
  SRestoreCheckpointInfo req = {0};
×
1394

1395
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
1396
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
×
1397
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1398
    tDecoderClear(&decoder);
×
1399
    return TSDB_CODE_SUCCESS;
×
1400
  }
1401

1402
  tDecoderClear(&decoder);
×
1403

1404
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
1405
  if (pTask == NULL || (code != 0)) {
×
1406
    // ignore this code to avoid error code over writing
1407
    if (pMeta->role == NODE_ROLE_LEADER) {
×
1408
      tqError("vgId:%d process consensus checkpointId req:%" PRId64
×
1409
              " transId:%d, failed to acquire task:0x%x, it may have been dropped/stopped already",
1410
              pMeta->vgId, req.checkpointId, req.transId, req.taskId);
1411

1412
      int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId, true);
×
1413
      if (ret) {
×
1414
        tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1415
      }
1416
    } else {
1417
      tqDebug("vgId:%d task:0x%x stopped in follower node, not set the consensus checkpointId:%" PRId64 " transId:%d",
×
1418
              pMeta->vgId, req.taskId, req.checkpointId, req.transId);
1419
    }
1420

1421
    return 0;
×
1422
  }
1423

1424
  // discard the rsp, since it is expired.
1425
  if (req.startTs < pTask->execInfo.created) {
×
1426
    tqWarn("s-task:%s vgId:%d createTs:%" PRId64 " recv expired consensus checkpointId:%" PRId64
×
1427
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1428
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1429
           pTask->execInfo.created);
1430
    if (pMeta->role == NODE_ROLE_LEADER) {
×
1431
      streamMetaAddFailedTaskSelf(pTask, now, true);
×
1432
    }
1433

1434
    streamMetaReleaseTask(pMeta, pTask);
×
1435
    return TSDB_CODE_SUCCESS;
×
1436
  }
1437

1438
  tqInfo("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64
×
1439
          " transId:%d from mnode, reqTs:%" PRId64 " task createTs:%" PRId64,
1440
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId, req.transId, req.startTs,
1441
          pTask->execInfo.created);
1442

1443
  streamMutexLock(&pTask->lock);
×
1444
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
×
1445

1446
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
×
1447
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1448
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1449

1450
    streamMutexUnlock(&pTask->lock);
×
1451
    streamMetaReleaseTask(pMeta, pTask);
×
1452
    return 0;
×
1453
  }
1454

1455
  if (pConsenInfo->consenChkptTransId >= req.transId) {
×
1456
    tqWarn("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1457
            pConsenInfo->consenChkptTransId, req.transId);
1458
    streamMutexUnlock(&pTask->lock);
×
1459
    streamMetaReleaseTask(pMeta, pTask);
×
1460
    return TSDB_CODE_SUCCESS;
×
1461
  }
1462

1463
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
×
1464
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1465
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1466
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1467
    tqSetRestoreVersionInfo(pTask);
×
1468
  } else {
1469
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
×
1470
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1471
  }
1472

1473
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
×
1474
  streamMutexUnlock(&pTask->lock);
×
1475

1476
  streamMetaWLock(pTask->pMeta);
×
1477
  if (pMeta->startInfo.curStage == START_WAIT_FOR_CHKPTID) {
×
1478
    pMeta->startInfo.curStage = START_CHECK_DOWNSTREAM;
×
1479

1480
    SStartTaskStageInfo info = {.stage = pMeta->startInfo.curStage, .ts = now};
×
1481
    taosArrayPush(pMeta->startInfo.pStagesList, &info);
×
1482

1483
    tqDebug("vgId:%d wait_for_chkptId stage -> check_down_stream stage, reqTs:%" PRId64 " , numOfStageHist:%d",
×
1484
            pMeta->vgId, info.ts, (int32_t)taosArrayGetSize(pMeta->startInfo.pStagesList));
1485
  }
1486

1487
  if (pMeta->role == NODE_ROLE_LEADER) {
×
1488
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
1489

1490
    bool exist = false;
×
1491
    for (int32_t i = 0; i < taosArrayGetSize(pMeta->startInfo.pRecvChkptIdTasks); ++i) {
×
1492
      STaskId* pId = taosArrayGet(pMeta->startInfo.pRecvChkptIdTasks, i);
×
1493
      if (id.streamId == pId->streamId && id.taskId == pId->taskId) {
×
1494
        exist = true;
×
1495
        break;
×
1496
      }
1497
    }
1498

1499
    if (!exist) {
×
1500
      void* p = taosArrayPush(pMeta->startInfo.pRecvChkptIdTasks, &id);
×
1501
      if (p == NULL) {  // todo handle error, not record the newly attached, start may dead-lock
×
1502
        tqError("s-task:0x%x failed to add into recv checkpointId task list, code:%s", req.taskId, tstrerror(code));
×
1503
      } else {
1504
        int32_t num = taosArrayGetSize(pMeta->startInfo.pRecvChkptIdTasks);
×
1505
        tqDebug("s-task:0x%x vgId:%d added into recv checkpointId task list, already recv %d", req.taskId, req.nodeId,
×
1506
                num);
1507
      }
1508
    } else {
1509
      int32_t num = taosArrayGetSize(pMeta->startInfo.pRecvChkptIdTasks);
×
1510
      tqDebug("s-task:0x%x vgId:%d already exist in recv consensus checkpontId, total existed:%d", req.taskId,
×
1511
              req.nodeId, num);
1512
    }
1513

1514
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
×
1515
    if (code != 0) {
×
1516
      // todo remove the newly added, otherwise, deadlock exist
1517
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1518
    }
1519
  } else {
1520
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1521
  }
1522

1523
  streamMetaWUnLock(pTask->pMeta);
×
1524

1525
  streamMetaReleaseTask(pMeta, pTask);
×
1526
  return 0;
×
1527
}
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