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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

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

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

43
  if (pTask->info.fillHistory) {
13,779✔
44
    streamId = pTask->streamTaskId.streamId;
4,956✔
45
    taskId = pTask->streamTaskId.taskId;
4,956✔
46
  } else {
47
    streamId = pTask->id.streamId;
8,823✔
48
    taskId = pTask->id.taskId;
8,823✔
49
  }
50

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

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

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

76
  initStorageAPI(&handle.api);
13,780✔
77

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

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

91
  streamSetupScheduleTrigger(pTask);
13,780✔
92

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

96
  return code;
13,780✔
97
}
98

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

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

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

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

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

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

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

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

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

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

153
  SStreamTaskNodeUpdateMsg req = {0};
75✔
154

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

164
  tDecoderClear(&decoder);
75✔
165

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

295
  rsp.code = TSDB_CODE_SUCCESS;
75✔
296

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

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

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

317
    streamMetaClearSetUpdateTaskListComplete(pMeta);
38✔
318

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

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

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

343
  SStreamDispatchReq req = {0};
66,038✔
344

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

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

355
  SStreamTask* pTask = NULL;
66,027✔
356
  int32_t      code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
66,027✔
357
  if (pTask && (code == 0)) {
66,016!
358
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
65,839✔
359
    if (streamProcessDispatchMsg(pTask, &req, &rsp) != 0) {
65,839!
UNCOV
360
      return -1;
×
361
    }
362
    tCleanupStreamDispatchReq(&req);
65,846✔
363
    streamMetaReleaseTask(pMeta, pTask);
65,843✔
364
    return 0;
65,849✔
365
  } else {
366
    tqError("vgId:%d failed to find task:0x%x to handle the dispatch req, it may have been destroyed already",
177!
367
            pMeta->vgId, req.taskId);
368

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

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

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

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

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

398
    return 0;
189✔
399
  }
400
}
401

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

405
  int32_t vgId = pMeta->vgId;
66,043✔
406
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
66,043✔
407
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
66,043✔
408
  pRsp->streamId = htobe64(pRsp->streamId);
66,043✔
409
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
66,041✔
410
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
66,041✔
411
  pRsp->stage = htobe64(pRsp->stage);
66,041✔
412
  pRsp->msgId = htonl(pRsp->msgId);
66,037✔
413

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

417
  SStreamTask* pTask = NULL;
66,037✔
418
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->upstreamTaskId, &pTask);
66,037✔
419
  if (pTask && (code == 0)) {
66,043!
420
    code = streamProcessDispatchRsp(pTask, pRsp, pMsg->code);
65,979✔
421
    streamMetaReleaseTask(pMeta, pTask);
65,980✔
422
    return code;
65,980✔
423
  } else {
424
    tqDebug("vgId:%d failed to handle the dispatch rsp, since find task:0x%x failed", vgId, pRsp->upstreamTaskId);
64✔
425
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
64✔
426
  }
427
}
428

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

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

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

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

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

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

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

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

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

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

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

491
  SStreamTaskCheckReq req;
492
  SStreamTaskCheckRsp rsp = {0};
21,163✔
493

494
  SDecoder decoder;
495

496
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
21,163✔
497
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
21,160✔
498
  tDecoderClear(&decoder);
21,137✔
499

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

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

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

515
  SStreamTaskCheckRsp rsp;
516

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

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

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

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

543
  code = streamTaskProcessCheckRsp(pTask, &rsp);
21,096✔
544
  streamMetaReleaseTask(pMeta, pTask);
21,104✔
545
  return code;
21,108✔
546
}
547

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

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

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

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

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

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

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

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

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

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

608
  return code;
8,659✔
609
}
610

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

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

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

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

635
  SDecoder decoder;
636
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
13,740✔
637
  code = tDecodeStreamTask(&decoder, pTask);
13,743✔
638
  tDecoderClear(&decoder);
13,739✔
639

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

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

649
  streamMetaWLock(pMeta);
13,741✔
650
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
13,746✔
651
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
13,748✔
652
  streamMetaWUnLock(pMeta);
13,749✔
653

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

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

667
      if (restored) {
13,537✔
668
        SStreamTask* p = NULL;
13,430✔
669
        code = streamMetaAcquireTask(pMeta, streamId, taskId, &p);
13,430✔
670
        if ((p != NULL) && (code == 0) && (p->info.fillHistory == 0)) {
13,430!
671
          code = tqStreamStartOneTaskAsync(pMeta, cb, streamId, taskId);
8,609✔
672
        }
673

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

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

688
  return code;
13,748✔
689
}
690

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

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

700
  streamMetaWLock(pMeta);
6,972✔
701

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

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

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

721
  streamMetaWUnLock(pMeta);
6,992✔
722

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

733
  // drop the stream task now
734
  code = streamMetaUnregisterTask(pMeta, pReq->streamId, pReq->taskId);
6,987✔
735
  if (code) {
6,989!
UNCOV
736
    tqDebug("s-task:0x%x vgId:%d drop task failed", pReq->taskId, vgId);
×
737
  }
738

739
  // commit the update
740
  streamMetaWLock(pMeta);
6,989✔
741
  int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
6,993✔
742
  tqDebug("vgId:%d task:0x%x dropped, remain tasks:%d", vgId, pReq->taskId, numOfTasks);
6,994✔
743

744
  if (streamMetaCommit(pMeta) < 0) {
6,994✔
745
    // persist to disk
746
  }
747

748
  streamMetaWUnLock(pMeta);
6,996✔
749
  return 0;  // always return success
6,996✔
750
}
751

752
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
7,130✔
753
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
7,130✔
754
  int32_t                    code = 0;
7,130✔
755
  int32_t                    vgId = pMeta->vgId;
7,130✔
756
  SStreamTask*               pTask = NULL;
7,130✔
757

758
  tqDebug("vgId:%d receive msg to update-checkpoint-info for s-task:0x%x", vgId, pReq->taskId);
7,130✔
759

760
  streamMetaWLock(pMeta);
7,130✔
761

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

775
  streamMetaWUnLock(pMeta);
7,134✔
776
  // always return success when handling the requirement issued by mnode during transaction.
777
  return TSDB_CODE_SUCCESS;
7,102✔
778
}
779

780
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
27✔
781
  int32_t vgId = pMeta->vgId;
27✔
782
  int32_t code = 0;
27✔
783
  int64_t st = taosGetTimestampMs();
27✔
784

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

794
  pMeta->startInfo.startAllTasks = 1;
24✔
795
  streamMetaWUnLock(pMeta);
24✔
796

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

801
  streamMetaWLock(pMeta);
24✔
802
  streamMetaClear(pMeta);
24✔
803

804
  int64_t el = taosGetTimestampMs() - st;
24✔
805
  tqInfo("vgId:%d close&reload state elapsed time:%.3fs", vgId, el / 1000.);
24!
806

807
  streamMetaLoadAllTasks(pMeta);
24✔
808

809
  {
810
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
24✔
811
    taosHashClear(pStartInfo->pReadyTaskSet);
24✔
812
    taosHashClear(pStartInfo->pFailedTaskSet);
24✔
813
    pStartInfo->readyTs = 0;
24✔
814
  }
815

816
  if (isLeader && !tsDisableStream) {
24!
817
    streamMetaWUnLock(pMeta);
24✔
818
    code = streamMetaStartAllTasks(pMeta);
24✔
819
  } else {
820
    streamMetaResetStartInfo(&pMeta->startInfo, pMeta->vgId);
×
821
    pMeta->startInfo.restartCount = 0;
×
UNCOV
822
    streamMetaWUnLock(pMeta);
×
UNCOV
823
    tqInfo("vgId:%d, follower node not start stream tasks or stream is disabled", vgId);
×
824
  }
825

826
  code = terrno;
24✔
827
  return code;
24✔
828
}
829

830
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
122,955✔
831
  SStreamTaskRunReq* pReq = pMsg->pCont;
122,955✔
832

833
  int32_t type = pReq->reqType;
122,955✔
834
  int32_t vgId = pMeta->vgId;
122,955✔
835
  int32_t code = 0;
122,955✔
836

837
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
122,955✔
838
    code = streamMetaStartOneTask(pMeta, pReq->streamId, pReq->taskId);
8,707✔
839
    return 0;
8,707✔
840
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
114,248✔
841
    code = streamMetaStartAllTasks(pMeta);
11,455✔
842
    return 0;
11,455✔
843
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
102,793✔
844
    code = restartStreamTasks(pMeta, isLeader);
24✔
845
    return 0;
24✔
846
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
102,769✔
847
    code = streamMetaStopAllTasks(pMeta);
4,989✔
848
    return 0;
4,989✔
849
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
97,780✔
850
    code = streamMetaAddFailedTask(pMeta, pReq->streamId, pReq->taskId);
3✔
851
    return code;
3✔
852
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
97,777✔
853
    SStreamTask* pTask = NULL;
18,091✔
854
    code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
18,091✔
855

856
    if (pTask != NULL && (code == 0)) {
18,060!
857
      char* pStatus = NULL;
18,070✔
858
      if (streamTaskReadyToRun(pTask, &pStatus)) {
18,070✔
859
        int64_t execTs = pTask->status.lastExecTs;
18,073✔
860
        int32_t idle = taosGetTimestampMs() - execTs;
18,080✔
861
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
18,080✔
862

863
        code = streamResumeTask(pTask);
18,080✔
864
      } else {
865
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
8✔
866
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
1!
867
                pTask->id.idStr, pStatus, status);
868
      }
869
      streamMetaReleaseTask(pMeta, pTask);
18,086✔
870
    }
871

872
    return code;
18,077✔
873
  }
874

875
  SStreamTask* pTask = NULL;
79,686✔
876
  code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
79,686✔
877
  if ((pTask != NULL) && (code == 0)) {  // even in halt status, the data in inputQ must be processed
79,756!
878
    char* p = NULL;
79,734✔
879
    if (streamTaskReadyToRun(pTask, &p)) {
79,734✔
880
      tqDebug("vgId:%d s-task:%s status:%s start to process block from inputQ, next checked ver:%" PRId64, vgId,
79,327✔
881
              pTask->id.idStr, p, pTask->chkInfo.nextProcessVer);
882
      (void)streamExecTask(pTask);
79,327✔
883
    } else {
884
      int8_t status = streamTaskSetSchedStatusInactive(pTask);
368✔
885
      tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
371!
886
              pTask->id.idStr, p, status);
887
    }
888

889
    streamMetaReleaseTask(pMeta, pTask);
79,741✔
890
    return 0;
79,755✔
891
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
892
    // todo add one function to handle this
893
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, pReq->taskId);
22!
894
    return code;
34✔
895
  }
896
}
897

898
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
1,770✔
899
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
1,770✔
900
  int32_t         vgId = pMeta->vgId;
1,770✔
901
  bool            scanWal = false;
1,770✔
902
  int32_t         code = 0;
1,770✔
903

904
  streamMetaWLock(pMeta);
1,770✔
905
  if (pStartInfo->startAllTasks == 1) {
1,770!
UNCOV
906
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
907
            pMeta->startInfo.restartCount);
908
  } else {  // not in starting procedure
909
    bool allReady = streamMetaAllTasksReady(pMeta);
1,770✔
910

911
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
1,770!
912
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
913
      pStartInfo->restartCount -= 1;
3✔
914
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
3!
915
              pStartInfo->restartCount);
916
      streamMetaWUnLock(pMeta);
3✔
917

918
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
3✔
919
    } else {
920
      if (pStartInfo->restartCount == 0) {
1,767!
921
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
1,767✔
922
      } else if (allReady) {
×
UNCOV
923
        pStartInfo->restartCount = 0;
×
UNCOV
924
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
925
      }
926

927
      scanWal = true;
1,767✔
928
    }
929
  }
930

931
  streamMetaWUnLock(pMeta);
1,767✔
932

933
  if (scanWal && (vgId != SNODE_HANDLE)) {
1,767!
934
    tqDebug("vgId:%d start scan wal for executing tasks", vgId);
1,766✔
935
    code = tqScanWalAsync(pMeta->ahandle, true);
1,766✔
936
  }
937

938
  return code;
1,767✔
939
}
940

UNCOV
941
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
942
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
×
943

944
  SStreamTask* pTask = NULL;
×
945
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
UNCOV
946
  if (pTask == NULL || (code != 0)) {
×
947
    tqError("vgId:%d process task-reset req, failed to acquire task:0x%x, it may have been dropped already",
×
948
            pMeta->vgId, pReq->taskId);
UNCOV
949
    return TSDB_CODE_SUCCESS;
×
950
  }
951

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

UNCOV
954
  streamMutexLock(&pTask->lock);
×
UNCOV
955
  streamTaskClearCheckInfo(pTask, true);
×
956

957
  // clear flag set during do checkpoint, and open inputQ for all upstream tasks
958
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
959
  if (pState.state == TASK_STATUS__CK) {
×
960
    int32_t tranId = 0;
×
UNCOV
961
    int64_t activeChkId = 0;
×
962
    streamTaskGetActiveCheckpointInfo(pTask, &tranId, &activeChkId);
×
963

UNCOV
964
    tqDebug("s-task:%s reset task status from checkpoint, current checkpointingId:%" PRId64 ", transId:%d",
×
965
            pTask->id.idStr, activeChkId, tranId);
966

UNCOV
967
    streamTaskSetStatusReady(pTask);
×
UNCOV
968
  } else if (pState.state == TASK_STATUS__UNINIT) {
×
969
    //    tqDebug("s-task:%s start task by checking downstream tasks", pTask->id.idStr);
970
    //    tqStreamTaskRestoreCheckpoint(pMeta, pTask->id.streamId, pTask->id.taskId);
971
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
972
  } else {
UNCOV
973
    tqDebug("s-task:%s status:%s do nothing after receiving reset-task from mnode", pTask->id.idStr, pState.name);
×
974
  }
975

976
  streamMutexUnlock(&pTask->lock);
×
977

UNCOV
978
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
979
  return TSDB_CODE_SUCCESS;
×
980
}
981

UNCOV
982
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
983
  SRetrieveChkptTriggerReq* pReq = (SRetrieveChkptTriggerReq*)pMsg->pCont;
×
984

985
  SStreamTask* pTask = NULL;
×
986
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->upstreamTaskId, &pTask);
×
UNCOV
987
  if (pTask == NULL || (code != 0)) {
×
UNCOV
988
    tqError("vgId:%d process retrieve checkpoint trigger, checkpointId:%" PRId64
×
989
            " from s-task:0x%x, failed to acquire task:0x%x, it may have been dropped already",
990
            pMeta->vgId, pReq->checkpointId, (int32_t)pReq->downstreamTaskId, pReq->upstreamTaskId);
UNCOV
991
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
992
  }
993

UNCOV
994
  tqDebug("s-task:0x%x recv retrieve checkpoint-trigger msg from downstream s-task:0x%x, checkpointId:%" PRId64,
×
995
          pReq->upstreamTaskId, (int32_t)pReq->downstreamTaskId, pReq->checkpointId);
996

UNCOV
997
  if (pTask->status.downstreamReady != 1) {
×
UNCOV
998
    tqError("s-task:%s not ready for checkpoint-trigger retrieve from 0x%x, since downstream not ready",
×
999
            pTask->id.idStr, (int32_t)pReq->downstreamTaskId);
1000

1001
    code = streamTaskSendCheckpointTriggerMsg(pTask, pReq->downstreamTaskId, pReq->downstreamNodeId, &pMsg->info,
×
1002
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
UNCOV
1003
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1004
    return code;
×
1005
  }
1006

1007
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1008
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
UNCOV
1009
    int32_t transId = 0;
×
1010
    int64_t checkpointId = 0;
×
1011

1012
    streamTaskGetActiveCheckpointInfo(pTask, &transId, &checkpointId);
×
UNCOV
1013
    if (checkpointId != pReq->checkpointId) {
×
UNCOV
1014
      tqError("s-task:%s invalid checkpoint-trigger retrieve msg from 0x%" PRIx64 ", current checkpointId:%" PRId64
×
1015
              " req:%" PRId64,
1016
              pTask->id.idStr, pReq->downstreamTaskId, checkpointId, pReq->checkpointId);
UNCOV
1017
      streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1018
      return TSDB_CODE_INVALID_MSG;
×
1019
    }
1020

1021
    if (streamTaskAlreadySendTrigger(pTask, pReq->downstreamNodeId)) {
×
1022
      // re-send the lost checkpoint-trigger msg to downstream task
1023
      tqDebug("s-task:%s re-send checkpoint-trigger to:0x%x, checkpointId:%" PRId64 ", transId:%d", pTask->id.idStr,
×
1024
              (int32_t)pReq->downstreamTaskId, checkpointId, transId);
UNCOV
1025
      code = streamTaskSendCheckpointTriggerMsg(pTask, pReq->downstreamTaskId, pReq->downstreamNodeId, &pMsg->info,
×
1026
                                                TSDB_CODE_SUCCESS);
1027
    } else {  // not send checkpoint-trigger yet, wait
UNCOV
1028
      int32_t recv = 0, total = 0;
×
1029
      streamTaskGetTriggerRecvStatus(pTask, &recv, &total);
×
1030

UNCOV
1031
      if (recv == total) {  // add the ts info
×
1032
        tqWarn("s-task:%s all upstream send checkpoint-source/trigger, but not processed yet, wait", pTask->id.idStr);
×
1033
      } else {
UNCOV
1034
        tqWarn(
×
1035
            "s-task:%s not all upstream send checkpoint-source/trigger, total recv:%d/%d, wait for all upstream "
1036
            "sending checkpoint-source/trigger",
1037
            pTask->id.idStr, recv, total);
1038
      }
UNCOV
1039
      code = streamTaskSendCheckpointTriggerMsg(pTask, pReq->downstreamTaskId, pReq->downstreamNodeId, &pMsg->info,
×
1040
                                                TSDB_CODE_ACTION_IN_PROGRESS);
1041
    }
1042
  } else {  // upstream not recv the checkpoint-source/trigger till now
UNCOV
1043
    if (!(pState.state == TASK_STATUS__READY || pState.state == TASK_STATUS__HALT)) {
×
UNCOV
1044
      tqFatal("s-task:%s invalid task status:%s", pTask->id.idStr, pState.name);
×
1045
    }
1046

UNCOV
1047
    tqWarn(
×
1048
        "s-task:%s not recv checkpoint-source from mnode or checkpoint-trigger from upstream yet, wait for all "
1049
        "upstream sending checkpoint-source/trigger",
1050
        pTask->id.idStr);
UNCOV
1051
    code = streamTaskSendCheckpointTriggerMsg(pTask, pReq->downstreamTaskId, pReq->downstreamNodeId, &pMsg->info,
×
1052
                                              TSDB_CODE_ACTION_IN_PROGRESS);
1053
  }
1054

UNCOV
1055
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1056
  return code;
×
1057
}
1058

UNCOV
1059
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
1060
  SCheckpointTriggerRsp* pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
1061

1062
  SStreamTask* pTask = NULL;
×
1063
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->taskId, &pTask);
×
UNCOV
1064
  if (pTask == NULL || (code != 0)) {
×
UNCOV
1065
    tqError(
×
1066
        "vgId:%d process retrieve checkpoint-trigger, failed to acquire task:0x%x, it may have been dropped already",
1067
        pMeta->vgId, pRsp->taskId);
UNCOV
1068
    return code;
×
1069
  }
1070

UNCOV
1071
  tqDebug(
×
1072
      "s-task:%s recv re-send checkpoint-trigger msg from through retrieve/rsp channel, upstream:0x%x, "
1073
      "checkpointId:%" PRId64 ", transId:%d",
1074
      pTask->id.idStr, pRsp->upstreamTaskId, pRsp->checkpointId, pRsp->transId);
1075

1076
  code = streamTaskProcessCheckpointTriggerRsp(pTask, pRsp);
×
UNCOV
1077
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1078
  return code;
×
1079
}
1080

1081
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
1,297✔
1082
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
1,297✔
1083

1084
  SStreamTask* pTask = NULL;
1,297✔
1085
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,297✔
1086
  if (pTask == NULL || (code != 0)) {
1,301!
UNCOV
1087
    tqError("vgId:%d process pause req, failed to acquire task:0x%x, it may have been dropped already", pMeta->vgId,
×
1088
            pReq->taskId);
1089
    // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
UNCOV
1090
    return TSDB_CODE_SUCCESS;
×
1091
  }
1092

1093
  tqDebug("s-task:%s receive pause msg from mnode", pTask->id.idStr);
1,301✔
1094
  streamTaskPause(pTask);
1,301✔
1095

1096
  SStreamTask* pHistoryTask = NULL;
1,303✔
1097
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
1,303✔
1098
    pHistoryTask = NULL;
36✔
1099
    code = streamMetaAcquireTask(pMeta, pTask->hTaskInfo.id.streamId, pTask->hTaskInfo.id.taskId, &pHistoryTask);
36✔
1100
    if (pHistoryTask == NULL || (code != 0)) {
36!
UNCOV
1101
      tqError("vgId:%d process pause req, failed to acquire fill-history task:0x%" PRIx64
×
1102
              ", it may have been dropped already",
1103
              pMeta->vgId, pTask->hTaskInfo.id.taskId);
UNCOV
1104
      streamMetaReleaseTask(pMeta, pTask);
×
1105

1106
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
UNCOV
1107
      return TSDB_CODE_SUCCESS;
×
1108
    }
1109

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

1112
    streamTaskPause(pHistoryTask);
36✔
1113
    streamMetaReleaseTask(pMeta, pHistoryTask);
36✔
1114
  }
1115

1116
  streamMetaReleaseTask(pMeta, pTask);
1,303✔
1117
  return TSDB_CODE_SUCCESS;
1,303✔
1118
}
1119

1120
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
1,308✔
1121
                                       bool fromVnode) {
1122
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
1,308!
1123
  int32_t      vgId = pMeta->vgId;
1,308✔
1124
  int32_t      code = 0;
1,308✔
1125

1126
  streamTaskResume(pTask);
1,308✔
1127
  ETaskStatus status = streamTaskGetStatus(pTask).state;
1,308✔
1128

1129
  int32_t level = pTask->info.taskLevel;
1,308✔
1130
  if (status == TASK_STATUS__READY || status == TASK_STATUS__SCAN_HISTORY || status == TASK_STATUS__CK) {
1,308!
1131
    // no lock needs to secure the access of the version
1132
    if (igUntreated && level == TASK_LEVEL__SOURCE && !pTask->info.fillHistory) {
1,308!
1133
      // discard all the data  when the stream task is suspended.
1134
      walReaderSetSkipToVersion(pTask->exec.pWalReader, sversion);
295✔
1135
      tqDebug("vgId:%d s-task:%s resume to exec, prev paused version:%" PRId64 ", start from vnode ver:%" PRId64
295✔
1136
              ", schedStatus:%d",
1137
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1138
    } else {  // from the previous paused version and go on
1139
      tqDebug("vgId:%d s-task:%s resume to exec, from paused ver:%" PRId64 ", vnode ver:%" PRId64 ", schedStatus:%d",
1,013✔
1140
              vgId, pTask->id.idStr, pTask->chkInfo.nextProcessVer, sversion, pTask->status.schedStatus);
1141
    }
1142

1143
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
1,308✔
1144
      pTask->hTaskInfo.operatorOpen = false;
4✔
1145
      code = streamStartScanHistoryAsync(pTask, igUntreated);
4✔
1146
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
1,304✔
1147
      code = tqScanWalAsync((STQ*)handle, false);
647✔
1148
    } else {
1149
      code = streamTrySchedExec(pTask);
657✔
1150
    }
1151
  }
1152

1153
  return code;
1,308✔
1154
}
1155

1156
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
1,296✔
1157
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
1,296✔
1158

1159
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
1,296!
1160

1161
  SStreamTask* pTask = NULL;
1,296✔
1162
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
1,296✔
1163
  if (pTask == NULL || (code != 0)) {
1,296!
UNCOV
1164
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
UNCOV
1165
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1166
  }
1167

1168
  streamMutexLock(&pTask->lock);
1,296✔
1169
  SStreamTaskState pState = streamTaskGetStatus(pTask);
1,296✔
1170
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
1,296✔
1171
  streamMutexUnlock(&pTask->lock);
1,296✔
1172

1173
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
1,296✔
1174
  if (code != 0) {
1,296!
UNCOV
1175
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1176
    return code;
×
1177
  }
1178

1179
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
1,296✔
1180
  SStreamTask* pHTask = NULL;
1,296✔
1181
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
1,296✔
1182
  if (pHTask && (code == 0)) {
1,296!
1183
    streamMutexLock(&pHTask->lock);
12✔
1184
    SStreamTaskState p = streamTaskGetStatus(pHTask);
12✔
1185
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
12!
1186
    streamMutexUnlock(&pHTask->lock);
12✔
1187

1188
    code = tqProcessTaskResumeImpl(handle, pHTask, sversion, pReq->igUntreated, fromVnode);
12✔
1189
    streamMetaReleaseTask(pMeta, pHTask);
12✔
1190
  }
1191

1192
  return code;
1,296✔
1193
}
1194

UNCOV
1195
int32_t tqStreamTasksGetTotalNum(SStreamMeta* pMeta) { return taosArrayGetSize(pMeta->pTaskList); }
×
1196

1197
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
11,233✔
1198
  rpcFreeCont(pMsg->pCont);
11,233✔
1199
  pMsg->pCont = NULL;
11,233✔
1200
  return TSDB_CODE_SUCCESS;
11,233✔
1201
}
1202

1203
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
25,720✔
1204
  return streamProcessHeartbeatRsp(pMeta, pMsg->pCont);
25,720✔
1205
}
1206

1207
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
4,734✔
1208

1209
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
6,499✔
1210

1211
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
8,663✔
1212
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
8,663✔
1213

1214
  SStreamTask* pTask = NULL;
8,663✔
1215
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
8,663✔
1216
  if (pTask == NULL || (code != 0)) {
8,662!
1217
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
7!
1218
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
1219
    return code;
7✔
1220
  }
1221

1222
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
8,655✔
1223
  streamMetaReleaseTask(pMeta, pTask);
8,657✔
1224
  return code;
8,655✔
1225
}
1226

1227
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
98✔
1228
  int32_t                vgId = pMeta->vgId;
98✔
1229
  int32_t                code = 0;
98✔
1230
  SStreamTask*           pTask = NULL;
98✔
1231
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
98✔
1232
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
98✔
1233
  int64_t                now = taosGetTimestampMs();
98✔
1234
  SDecoder               decoder;
1235
  SRestoreCheckpointInfo req = {0};
98✔
1236

1237
  tDecoderInit(&decoder, (uint8_t*)msg, len);
98✔
1238
  if (tDecodeRestoreCheckpointInfo(&decoder, &req) < 0) {
98!
1239
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
UNCOV
1240
    tDecoderClear(&decoder);
×
UNCOV
1241
    return TSDB_CODE_SUCCESS;
×
1242
  }
1243

1244
  tDecoderClear(&decoder);
98✔
1245

1246
  code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
98✔
1247
  if (pTask == NULL || (code != 0)) {
98!
UNCOV
1248
    tqError("vgId:%d process consensus checkpointId req, failed to acquire task:0x%x, it may have been dropped already",
×
1249
            pMeta->vgId, req.taskId);
1250
    // ignore this code to avoid error code over write
1251
    int32_t ret = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
UNCOV
1252
    if (ret) {
×
UNCOV
1253
      tqError("s-task:0x%x failed add check downstream failed, core:%s", req.taskId, tstrerror(ret));
×
1254
    }
1255

UNCOV
1256
    return 0;
×
1257
  }
1258

1259
  // discard the rsp, since it is expired.
1260
  if (req.startTs < pTask->execInfo.created) {
98!
UNCOV
1261
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
×
1262
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1263
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1264
           pTask->execInfo.created);
1265
    streamMetaAddFailedTaskSelf(pTask, now);
×
UNCOV
1266
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1267
    return TSDB_CODE_SUCCESS;
×
1268
  }
1269

1270
  tqDebug("s-task:%s vgId:%d checkpointId:%" PRId64 " restore to consensus-checkpointId:%" PRId64 " from mnode",
98✔
1271
          pTask->id.idStr, vgId, pTask->chkInfo.checkpointId, req.checkpointId);
1272

1273
  streamMutexLock(&pTask->lock);
98✔
1274
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
98!
UNCOV
1275
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1276
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1277

1278
    streamMutexUnlock(&pTask->lock);
×
UNCOV
1279
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1280
    return 0;
×
1281
  }
1282

1283
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
98✔
1284
  if (pConsenInfo->consenChkptTransId >= req.transId) {
98!
1285
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1286
            pConsenInfo->consenChkptTransId, req.transId);
1287
    streamMutexUnlock(&pTask->lock);
×
UNCOV
1288
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1289
    return TSDB_CODE_SUCCESS;
×
1290
  }
1291

1292
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
98!
1293
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1294
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
UNCOV
1295
    pTask->chkInfo.checkpointId = req.checkpointId;
×
UNCOV
1296
    tqSetRestoreVersionInfo(pTask);
×
1297
  } else {
1298
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
98✔
1299
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1300
  }
1301

1302
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
98✔
1303
  streamMutexUnlock(&pTask->lock);
98✔
1304

1305
  if (pMeta->role == NODE_ROLE_LEADER) {
98!
1306
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
98✔
1307
    if (code) {
98!
UNCOV
1308
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1309
    }
1310
  } else {
UNCOV
1311
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1312
  }
1313

1314
  streamMetaReleaseTask(pMeta, pTask);
98✔
1315
  return 0;
98✔
1316
}
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