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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

1.78
/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

UNCOV
33
int32_t tqExpandStreamTask(SStreamTask* pTask) {
×
UNCOV
34
  SStreamMeta* pMeta = pTask->pMeta;
×
UNCOV
35
  int32_t      vgId = pMeta->vgId;
×
UNCOV
36
  int64_t      st = taosGetTimestampMs();
×
UNCOV
37
  int64_t      streamId = 0;
×
UNCOV
38
  int32_t      taskId = 0;
×
UNCOV
39
  int32_t      code = 0;
×
40

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

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

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

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

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

UNCOV
76
  initStorageAPI(&handle.api);
×
77

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

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

UNCOV
91
  streamSetupScheduleTrigger(pTask);
×
92

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

UNCOV
96
  return code;
×
97
}
98

UNCOV
99
void tqSetRestoreVersionInfo(SStreamTask* pTask) {
×
UNCOV
100
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
×
101

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

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

UNCOV
112
  pTask->execInfo.startCheckpointVer = pChkInfo->nextProcessVer;
×
UNCOV
113
}
×
114

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

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

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

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

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

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

UNCOV
153
  SStreamTaskNodeUpdateMsg req = {0};
×
154

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

UNCOV
164
  tDecoderClear(&decoder);
×
165

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

173
  // update the nodeEpset when it exists
UNCOV
174
  streamMetaWLock(pMeta);
×
175

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

UNCOV
188
  const char* idstr = pTask->id.idStr;
×
189

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

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

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

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

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

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

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

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

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

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

UNCOV
229
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
×
230

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

UNCOV
238
  streamTaskStopMonitorCheckRsp(&pTask->taskCheckInfo, pTask->id.idStr);
×
239

UNCOV
240
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
UNCOV
241
    code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->hTaskInfo.id, &pHTask);
×
UNCOV
242
    if (code != 0) {
×
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 {
UNCOV
249
      tqDebug("s-task:%s fill-history task update nodeEp along with stream task", pHTask->id.idStr);
×
UNCOV
250
      bool updateEpSet = streamTaskUpdateEpsetInfo(pHTask, req.pNodeList);
×
UNCOV
251
      if (updateEpSet) {
×
UNCOV
252
        updated = updateEpSet;
×
253
      }
254

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

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

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

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

UNCOV
283
  if (pHTask != NULL) {
×
UNCOV
284
    code = streamTaskStop(pHTask);
×
UNCOV
285
    if (code) {
×
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
UNCOV
291
  streamMetaAddIntoUpdateTaskList(pMeta, pTask, (pHTask != NULL) ? (pHTask) : NULL, req.transId, st);
×
UNCOV
292
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
293
  streamMetaReleaseTask(pMeta, pHTask);
×
294

UNCOV
295
  rsp.code = TSDB_CODE_SUCCESS;
×
296

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

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

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

UNCOV
317
    streamMetaClearSetUpdateTaskListComplete(pMeta);
×
318

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

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

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

UNCOV
343
  SStreamDispatchReq req = {0};
×
344

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

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

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

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

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

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

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

UNCOV
395
    tmsgSendRsp(&rsp);
×
UNCOV
396
    tCleanupStreamDispatchReq(&req);
×
397

UNCOV
398
    return 0;
×
399
  }
400
}
401

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

UNCOV
405
  int32_t vgId = pMeta->vgId;
×
UNCOV
406
  pRsp->upstreamNodeId = htonl(pRsp->upstreamNodeId);
×
UNCOV
407
  pRsp->upstreamTaskId = htonl(pRsp->upstreamTaskId);
×
UNCOV
408
  pRsp->streamId = htobe64(pRsp->streamId);
×
UNCOV
409
  pRsp->downstreamTaskId = htonl(pRsp->downstreamTaskId);
×
UNCOV
410
  pRsp->downstreamNodeId = htonl(pRsp->downstreamNodeId);
×
UNCOV
411
  pRsp->stage = htobe64(pRsp->stage);
×
UNCOV
412
  pRsp->msgId = htonl(pRsp->msgId);
×
413

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

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

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

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

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

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

455
  // enqueue
UNCOV
456
  tqDebug("s-task:%s (vgId:%d level:%d) recv retrieve req from task:0x%x(vgId:%d),QID:0x%" PRIx64, pTask->id.idStr,
×
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
UNCOV
460
  streamTaskSetCheckpointFailed(pTask);
×
461

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

UNCOV
470
  if (code != TSDB_CODE_SUCCESS) {  // return error not send rsp manually
×
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.
UNCOV
474
    SRpcMsg rsp = {.info = pMsg->info, .code = 0};
×
UNCOV
475
    streamTaskSendRetrieveRsp(&req, &rsp);
×
476
  }
477

UNCOV
478
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
479
  tCleanupStreamRetrieveReq(&req);
×
480

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

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

491
  SStreamTaskCheckReq req;
UNCOV
492
  SStreamTaskCheckRsp rsp = {0};
×
493

494
  SDecoder decoder;
495

UNCOV
496
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
UNCOV
497
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
×
UNCOV
498
  tDecoderClear(&decoder);
×
499

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

UNCOV
505
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
×
UNCOV
506
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
×
507
}
508

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

515
  SStreamTaskCheckRsp rsp;
516

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

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

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

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

UNCOV
543
  code = streamTaskProcessCheckRsp(pTask, &rsp);
×
UNCOV
544
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
545
  return code;
×
546
}
547

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

UNCOV
554
  SStreamCheckpointReadyMsg req = {0};
×
555

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

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

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

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

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

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

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

UNCOV
605
    pMsg->info.handle = NULL;  // disable auto rsp
×
606
  }
607

UNCOV
608
  return code;
×
609
}
610

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

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

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

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

635
  SDecoder decoder;
UNCOV
636
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
×
UNCOV
637
  code = tDecodeStreamTask(&decoder, pTask);
×
UNCOV
638
  tDecoderClear(&decoder);
×
639

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

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

UNCOV
649
  streamMetaWLock(pMeta);
×
UNCOV
650
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
×
UNCOV
651
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
UNCOV
652
  streamMetaWUnLock(pMeta);
×
653

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

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

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

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

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

UNCOV
688
  return code;
×
689
}
690

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

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

UNCOV
700
  streamMetaWLock(pMeta);
×
701

UNCOV
702
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
UNCOV
703
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
UNCOV
704
  if (code == 0) {
×
UNCOV
705
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
UNCOV
706
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
×
UNCOV
707
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
×
708
    }
709

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

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

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

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

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

UNCOV
741
  if (streamMetaCommit(pMeta) < 0) {
×
742
    // persist to disk
743
  }
744

UNCOV
745
  streamMetaWUnLock(pMeta);
×
UNCOV
746
  return 0;  // always return success
×
747
}
748

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

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

UNCOV
757
  streamMetaWLock(pMeta);
×
758

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

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

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

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

UNCOV
791
  pMeta->startInfo.startAllTasks = 1;
×
UNCOV
792
  streamMetaWUnLock(pMeta);
×
793

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

UNCOV
798
  streamMetaWLock(pMeta);
×
UNCOV
799
  streamMetaClear(pMeta);
×
800

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

UNCOV
804
  streamMetaLoadAllTasks(pMeta);
×
805

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

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

UNCOV
823
  code = terrno;
×
UNCOV
824
  return code;
×
825
}
826

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

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

842
  tDecoderClear(&decoder);
1,005✔
843

844
  int32_t type = req.reqType;
1,005✔
845
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
1,005!
UNCOV
846
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
×
UNCOV
847
    return 0;
×
848
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
1,005✔
849
    code = streamMetaStartAllTasks(pMeta);
181✔
850
    return 0;
181✔
851
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
824!
UNCOV
852
    code = restartStreamTasks(pMeta, isLeader);
×
UNCOV
853
    return 0;
×
854
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
824!
855
    code = streamMetaStopAllTasks(pMeta);
824✔
856
    return 0;
824✔
UNCOV
857
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
×
UNCOV
858
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
UNCOV
859
    return code;
×
UNCOV
860
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
×
UNCOV
861
    SStreamTask* pTask = NULL;
×
UNCOV
862
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
863

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

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

UNCOV
880
    return code;
×
881
  }
882

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

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

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

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

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

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

UNCOV
935
      scanWal = true;
×
936
    }
937
  }
938

UNCOV
939
  streamMetaWUnLock(pMeta);
×
940

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

UNCOV
946
  return code;
×
947
}
948

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1107
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
×
UNCOV
1108
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
×
1109

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

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

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

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

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

UNCOV
1138
    streamTaskPause(pHistoryTask);
×
UNCOV
1139
    streamMetaReleaseTask(pMeta, pHistoryTask);
×
1140
  }
1141

UNCOV
1142
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1143
  return TSDB_CODE_SUCCESS;
×
1144
}
1145

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

UNCOV
1152
  streamTaskResume(pTask);
×
UNCOV
1153
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
1154

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

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

UNCOV
1179
  return code;
×
1180
}
1181

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

UNCOV
1185
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
×
1186

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

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

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

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

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

UNCOV
1217
    streamMetaReleaseTask(pMeta, pHTask);
×
1218
  }
1219

UNCOV
1220
  return TSDB_CODE_SUCCESS;
×
1221
}
1222

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

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

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

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

UNCOV
1247
  tDecoderClear(&decoder);
×
UNCOV
1248
  return streamProcessHeartbeatRsp(pMeta, &rsp);
×
1249
}
1250

UNCOV
1251
int32_t tqStreamProcessReqCheckpointRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
×
1252

UNCOV
1253
int32_t tqStreamProcessChkptReportRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) { return doProcessDummyRspMsg(pMeta, pMsg); }
×
1254

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

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

UNCOV
1266
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
×
UNCOV
1267
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1268
  return code;
×
1269
}
1270

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

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

UNCOV
1288
  tDecoderClear(&decoder);
×
1289

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

1300
    return 0;
×
1301
  }
1302

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

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

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

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

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

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

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

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

UNCOV
1358
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1359
  return 0;
×
1360
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc