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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/source/dnode/vnode/src/tqCommon/tqCommon.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tmsgcb.h"
17
#include "tq.h"
18
#include "tstream.h"
19

20
typedef struct SMStreamCheckpointReadyRspMsg {
21
  SMsgHead head;
22
  int64_t  streamId;
23
  int32_t  upstreamTaskId;
24
  int32_t  upstreamNodeId;
25
  int32_t  downstreamTaskId;
26
  int32_t  downstreamNodeId;
27
  int64_t  checkpointId;
28
  int32_t  transId;
29
} SMStreamCheckpointReadyRspMsg;
30

31
static int32_t doProcessDummyRspMsg(SStreamMeta* pMeta, SRpcMsg* pMsg);
32

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

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

UNCOV
99
  streamSetupScheduleTrigger(pTask);
×
100

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

UNCOV
104
  return code;
×
105
}
106

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

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

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

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

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

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

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

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

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

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

UNCOV
161
  SStreamTaskNodeUpdateMsg req = {0};
×
162

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

UNCOV
172
  tDecoderClear(&decoder);
×
173

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

181
  // update the nodeEpset when it exists
UNCOV
182
  streamMetaWLock(pMeta);
×
183

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

UNCOV
196
  const char* idstr = pTask->id.idStr;
×
197

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

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

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

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

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

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

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

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

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

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

UNCOV
237
  updated = streamTaskUpdateEpsetInfo(pTask, req.pNodeList);
×
238

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

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

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

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

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

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

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

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

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

UNCOV
303
  rsp.code = TSDB_CODE_SUCCESS;
×
304

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

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

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

UNCOV
325
    streamMetaClearSetUpdateTaskListComplete(pMeta);
×
326

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

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

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

UNCOV
351
  SStreamDispatchReq req = {0};
×
352

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

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

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

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

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

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

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

UNCOV
403
    tmsgSendRsp(&rsp);
×
UNCOV
404
    tCleanupStreamDispatchReq(&req);
×
405

UNCOV
406
    return 0;
×
407
  }
408
}
409

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

499
  SStreamTaskCheckReq req;
UNCOV
500
  SStreamTaskCheckRsp rsp = {0};
×
501

502
  SDecoder decoder;
503

UNCOV
504
  tDecoderInit(&decoder, (uint8_t*)msgBody, msgLen);
×
UNCOV
505
  code = tDecodeStreamTaskCheckReq(&decoder, &req);
×
UNCOV
506
  tDecoderClear(&decoder);
×
507

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

UNCOV
513
  streamTaskProcessCheckMsg(pMeta, &req, &rsp);
×
UNCOV
514
  return streamTaskSendCheckRsp(pMeta, req.upstreamNodeId, &rsp, &pMsg->info, req.upstreamTaskId);
×
515
}
516

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

523
  SStreamTaskCheckRsp rsp;
524

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

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

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

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

UNCOV
551
  code = streamTaskProcessCheckRsp(pTask, &rsp);
×
UNCOV
552
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
553
  return code;
×
554
}
555

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

UNCOV
562
  SStreamCheckpointReadyMsg req = {0};
×
563

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

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

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

UNCOV
590
  code = streamProcessCheckpointReadyMsg(pTask, req.checkpointId, req.downstreamNodeId, req.downstreamTaskId);
×
UNCOV
591
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
592
  if (code) {
×
593
    return code;
×
594
  }
595

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

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

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

UNCOV
613
    pMsg->info.handle = NULL;  // disable auto rsp
×
614
  }
615

UNCOV
616
  return code;
×
617
}
618

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

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

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

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

643
  SDecoder decoder;
UNCOV
644
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
×
UNCOV
645
  code = tDecodeStreamTask(&decoder, pTask);
×
UNCOV
646
  tDecoderClear(&decoder);
×
647

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

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

UNCOV
657
  streamMetaWLock(pMeta);
×
UNCOV
658
  code = streamMetaRegisterTask(pMeta, sversion, pTask, &added);
×
UNCOV
659
  numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
UNCOV
660
  streamMetaWUnLock(pMeta);
×
661

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

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

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

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

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

UNCOV
696
  return code;
×
697
}
698

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

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

UNCOV
708
  streamMetaWLock(pMeta);
×
709

UNCOV
710
  STaskId id = {.streamId = pReq->streamId, .taskId = pReq->taskId};
×
UNCOV
711
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
×
UNCOV
712
  if (code == 0) {
×
UNCOV
713
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
UNCOV
714
      hTaskId.streamId = pTask->hTaskInfo.id.streamId;
×
UNCOV
715
      hTaskId.taskId = pTask->hTaskInfo.id.taskId;
×
716
    }
717

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

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

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

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

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

UNCOV
749
  if (streamMetaCommit(pMeta) < 0) {
×
750
    // persist to disk
751
  }
752

UNCOV
753
  streamMetaWUnLock(pMeta);
×
NEW
754
  tqDebug("vgId:%d process drop task:0x%x completed", vgId, pReq->taskId);
×
755

UNCOV
756
  return 0;  // always return success
×
757
}
758

UNCOV
759
int32_t tqStreamTaskProcessUpdateCheckpointReq(SStreamMeta* pMeta, bool restored, char* msg) {
×
UNCOV
760
  SVUpdateCheckpointInfoReq* pReq = (SVUpdateCheckpointInfoReq*)msg;
×
UNCOV
761
  int32_t                    code = 0;
×
UNCOV
762
  int32_t                    vgId = pMeta->vgId;
×
UNCOV
763
  SStreamTask*               pTask = NULL;
×
764

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

UNCOV
767
  streamMetaWLock(pMeta);
×
768

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

UNCOV
782
  streamMetaWUnLock(pMeta);
×
783
  // always return success when handling the requirement issued by mnode during transaction.
UNCOV
784
  return TSDB_CODE_SUCCESS;
×
785
}
786

UNCOV
787
static int32_t restartStreamTasks(SStreamMeta* pMeta, bool isLeader) {
×
UNCOV
788
  int32_t vgId = pMeta->vgId;
×
UNCOV
789
  int32_t code = 0;
×
UNCOV
790
  int64_t st = taosGetTimestampMs();
×
791

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

UNCOV
801
  pMeta->startInfo.startAllTasks = 1;
×
UNCOV
802
  streamMetaWUnLock(pMeta);
×
803

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

UNCOV
808
  streamMetaWLock(pMeta);
×
UNCOV
809
  streamMetaClear(pMeta);
×
810

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

UNCOV
814
  streamMetaLoadAllTasks(pMeta);
×
815

816
  {
UNCOV
817
    STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
UNCOV
818
    taosHashClear(pStartInfo->pReadyTaskSet);
×
UNCOV
819
    taosHashClear(pStartInfo->pFailedTaskSet);
×
UNCOV
820
    pStartInfo->readyTs = 0;
×
821
  }
822

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

UNCOV
833
  code = terrno;
×
UNCOV
834
  return code;
×
835
}
836

UNCOV
837
int32_t tqStreamTaskProcessRunReq(SStreamMeta* pMeta, SRpcMsg* pMsg, bool isLeader) {
×
UNCOV
838
  int32_t  code = 0;
×
UNCOV
839
  int32_t  vgId = pMeta->vgId;
×
UNCOV
840
  char*    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
UNCOV
841
  int32_t  len = pMsg->contLen - sizeof(SMsgHead);
×
842
  SDecoder decoder;
843

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

UNCOV
852
  tDecoderClear(&decoder);
×
853

UNCOV
854
  int32_t type = req.reqType;
×
UNCOV
855
  if (type == STREAM_EXEC_T_START_ONE_TASK) {
×
UNCOV
856
    code = streamMetaStartOneTask(pMeta, req.streamId, req.taskId);
×
UNCOV
857
    return 0;
×
UNCOV
858
  } else if (type == STREAM_EXEC_T_START_ALL_TASKS) {
×
UNCOV
859
    code = streamMetaStartAllTasks(pMeta);
×
UNCOV
860
    return 0;
×
UNCOV
861
  } else if (type == STREAM_EXEC_T_RESTART_ALL_TASKS) {
×
UNCOV
862
    code = restartStreamTasks(pMeta, isLeader);
×
UNCOV
863
    return 0;
×
UNCOV
864
  } else if (type == STREAM_EXEC_T_STOP_ALL_TASKS) {
×
UNCOV
865
    code = streamMetaStopAllTasks(pMeta);
×
UNCOV
866
    return 0;
×
UNCOV
867
  } else if (type == STREAM_EXEC_T_ADD_FAILED_TASK) {
×
868
    code = streamMetaAddFailedTask(pMeta, req.streamId, req.taskId);
×
869
    return code;
×
NEW
870
  } else if (type == STREAM_EXEC_T_STOP_ONE_TASK) {
×
NEW
871
    code = streamMetaStopOneTask(pMeta, req.streamId, req.taskId);
×
NEW
872
    return code;
×
UNCOV
873
  } else if (type == STREAM_EXEC_T_RESUME_TASK) {  // task resume to run after idle for a while
×
UNCOV
874
    SStreamTask* pTask = NULL;
×
UNCOV
875
    code = streamMetaAcquireTask(pMeta, req.streamId, req.taskId, &pTask);
×
876

UNCOV
877
    if (pTask != NULL && (code == 0)) {
×
UNCOV
878
      char* pStatus = NULL;
×
UNCOV
879
      if (streamTaskReadyToRun(pTask, &pStatus)) {
×
UNCOV
880
        int64_t execTs = pTask->status.lastExecTs;
×
UNCOV
881
        int32_t idle = taosGetTimestampMs() - execTs;
×
UNCOV
882
        tqDebug("s-task:%s task resume to run after idle for:%dms from:%" PRId64, pTask->id.idStr, idle, execTs);
×
883

UNCOV
884
        code = streamResumeTask(pTask);
×
885
      } else {
886
        int8_t status = streamTaskSetSchedStatusInactive(pTask);
×
887
        tqDebug("vgId:%d s-task:%s ignore run req since not in ready state, status:%s, sched-status:%d", vgId,
×
888
                pTask->id.idStr, pStatus, status);
889
      }
UNCOV
890
      streamMetaReleaseTask(pMeta, pTask);
×
891
    }
892

UNCOV
893
    return code;
×
894
  }
895

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

UNCOV
910
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
911
    return 0;
×
912
  } else {  // NOTE: pTask->status.schedStatus is not updated since it is not be handled by the run exec.
913
    // todo add one function to handle this
914
    tqError("vgId:%d failed to found s-task, taskId:0x%x may have been dropped", vgId, req.taskId);
×
UNCOV
915
    return code;
×
916
  }
917
}
918

UNCOV
919
int32_t tqStartTaskCompleteCallback(SStreamMeta* pMeta) {
×
UNCOV
920
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
×
UNCOV
921
  int32_t         vgId = pMeta->vgId;
×
UNCOV
922
  bool            scanWal = false;
×
UNCOV
923
  int32_t         code = 0;
×
924

UNCOV
925
  streamMetaWLock(pMeta);
×
UNCOV
926
  if (pStartInfo->startAllTasks == 1) {
×
927
    tqDebug("vgId:%d already in start tasks procedure in other thread, restartCounter:%d, do nothing", vgId,
×
928
            pMeta->startInfo.restartCount);
929
  } else {  // not in starting procedure
UNCOV
930
    bool allReady = streamMetaAllTasksReady(pMeta);
×
931

UNCOV
932
    if ((pStartInfo->restartCount > 0) && (!allReady)) {
×
933
      // if all tasks are ready now, do NOT restart again, and reset the value of pStartInfo->restartCount
934
      pStartInfo->restartCount -= 1;
×
935
      tqDebug("vgId:%d role:%d need to restart all tasks again, restartCounter:%d", vgId, pMeta->role,
×
936
              pStartInfo->restartCount);
937
      streamMetaWUnLock(pMeta);
×
938

939
      return restartStreamTasks(pMeta, (pMeta->role == NODE_ROLE_LEADER));
×
940
    } else {
UNCOV
941
      if (pStartInfo->restartCount == 0) {
×
UNCOV
942
        tqDebug("vgId:%d start all tasks completed in callbackFn, restartCounter is 0", pMeta->vgId);
×
943
      } else if (allReady) {
×
944
        pStartInfo->restartCount = 0;
×
945
        tqDebug("vgId:%d all tasks are ready, reset restartCounter 0, not restart tasks", vgId);
×
946
      }
947

UNCOV
948
      scanWal = true;
×
949
    }
950
  }
951

UNCOV
952
  streamMetaWUnLock(pMeta);
×
953

UNCOV
954
  if (scanWal && (vgId != SNODE_HANDLE)) {
×
UNCOV
955
    tqDebug("vgId:%d start scan wal for executing tasks", vgId);
×
UNCOV
956
    code = tqScanWalAsync(pMeta->ahandle, true);
×
957
  }
958

UNCOV
959
  return code;
×
960
}
961

UNCOV
962
int32_t tqStreamTaskProcessTaskResetReq(SStreamMeta* pMeta, char* pMsg) {
×
UNCOV
963
  SVResetStreamTaskReq* pReq = (SVResetStreamTaskReq*)pMsg;
×
964

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

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

UNCOV
975
  streamMutexLock(&pTask->lock);
×
976

UNCOV
977
  streamTaskSetFailedCheckpointId(pTask, pReq->chkptId);
×
UNCOV
978
  streamTaskClearCheckInfo(pTask, true);
×
979

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

UNCOV
993
  streamMutexUnlock(&pTask->lock);
×
994

UNCOV
995
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
996
  return TSDB_CODE_SUCCESS;
×
997
}
998

UNCOV
999
int32_t tqStreamTaskProcessRetrieveTriggerReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
UNCOV
1000
  SRetrieveChkptTriggerReq req = {0};
×
UNCOV
1001
  SStreamTask*             pTask = NULL;
×
UNCOV
1002
  char*                    msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
UNCOV
1003
  int32_t                  len = pMsg->contLen - sizeof(SMsgHead);
×
UNCOV
1004
  SDecoder                 decoder = {0};
×
1005

UNCOV
1006
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
UNCOV
1007
  if (tDecodeRetrieveChkptTriggerReq(&decoder, &req) < 0) {
×
1008
    tDecoderClear(&decoder);
×
1009
    tqError("vgId:%d invalid retrieve checkpoint-trigger req received", pMeta->vgId);
×
1010
    return TSDB_CODE_INVALID_MSG;
×
1011
  }
UNCOV
1012
  tDecoderClear(&decoder);
×
1013

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

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

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

UNCOV
1029
    code = streamTaskSendCheckpointTriggerMsg(pTask, req.downstreamTaskId, req.downstreamNodeId, &pMsg->info,
×
1030
                                              TSDB_CODE_STREAM_TASK_IVLD_STATUS);
UNCOV
1031
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1032
    return code;
×
1033
  }
1034

1035
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
1036
  if (pState.state == TASK_STATUS__CK) {  // recv the checkpoint-source/trigger already
×
1037
    int32_t transId = 0;
×
1038
    int64_t checkpointId = 0;
×
1039

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

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

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

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

1083
  streamMetaReleaseTask(pMeta, pTask);
×
1084
  return code;
×
1085
}
1086

UNCOV
1087
int32_t tqStreamTaskProcessRetrieveTriggerRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
UNCOV
1088
  SCheckpointTriggerRsp rsp = {0};
×
UNCOV
1089
  SStreamTask*          pTask = NULL;
×
UNCOV
1090
  char*                 msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
UNCOV
1091
  int32_t               len = pMsg->contLen - sizeof(SMsgHead);
×
UNCOV
1092
  SDecoder              decoder = {0};
×
1093

UNCOV
1094
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
UNCOV
1095
  if (tDecodeCheckpointTriggerRsp(&decoder, &rsp) < 0) {
×
1096
    tDecoderClear(&decoder);
×
1097
    tqError("vgId:%d invalid retrieve checkpoint-trigger rsp received", pMeta->vgId);
×
1098
    return TSDB_CODE_INVALID_MSG;
×
1099
  }
UNCOV
1100
  tDecoderClear(&decoder);
×
1101

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

UNCOV
1110
  tqDebug(
×
1111
      "s-task:%s recv re-send checkpoint-trigger msg through retrieve/rsp channel, upstream:0x%x, checkpointId:%" PRId64
1112
      ", transId:%d",
1113
      pTask->id.idStr, rsp.upstreamTaskId, rsp.checkpointId, rsp.transId);
1114

UNCOV
1115
  code = streamTaskProcessCheckpointTriggerRsp(pTask, &rsp);
×
UNCOV
1116
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1117
  return code;
×
1118
}
1119

UNCOV
1120
int32_t tqStreamTaskProcessTaskPauseReq(SStreamMeta* pMeta, char* pMsg) {
×
UNCOV
1121
  SVPauseStreamTaskReq* pReq = (SVPauseStreamTaskReq*)pMsg;
×
1122

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

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

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

1145
      // since task is in [STOP|DROPPING] state, it is safe to assume the pause is active
1146
      return TSDB_CODE_SUCCESS;
×
1147
    }
1148

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

UNCOV
1151
    streamTaskPause(pHistoryTask);
×
UNCOV
1152
    streamMetaReleaseTask(pMeta, pHistoryTask);
×
1153
  }
1154

UNCOV
1155
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1156
  return TSDB_CODE_SUCCESS;
×
1157
}
1158

UNCOV
1159
static int32_t tqProcessTaskResumeImpl(void* handle, SStreamTask* pTask, int64_t sversion, int8_t igUntreated,
×
1160
                                       bool fromVnode) {
UNCOV
1161
  SStreamMeta* pMeta = fromVnode ? ((STQ*)handle)->pStreamMeta : handle;
×
UNCOV
1162
  int32_t      vgId = pMeta->vgId;
×
UNCOV
1163
  int32_t      code = 0;
×
1164

UNCOV
1165
  streamTaskResume(pTask);
×
UNCOV
1166
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
1167

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

UNCOV
1182
    if (level == TASK_LEVEL__SOURCE && pTask->info.fillHistory && status == TASK_STATUS__SCAN_HISTORY) {
×
UNCOV
1183
      pTask->hTaskInfo.operatorOpen = false;
×
UNCOV
1184
      code = streamStartScanHistoryAsync(pTask, igUntreated);
×
UNCOV
1185
    } else if (level == TASK_LEVEL__SOURCE && (streamQueueGetNumOfItems(pTask->inputq.queue) == 0)) {
×
UNCOV
1186
      code = tqScanWalAsync((STQ*)handle, false);
×
1187
    } else {
UNCOV
1188
      code = streamTrySchedExec(pTask);
×
1189
    }
1190
  }
1191

UNCOV
1192
  return code;
×
1193
}
1194

UNCOV
1195
int32_t tqStreamTaskProcessTaskResumeReq(void* handle, int64_t sversion, char* msg, bool fromVnode) {
×
UNCOV
1196
  SVResumeStreamTaskReq* pReq = (SVResumeStreamTaskReq*)msg;
×
1197

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

UNCOV
1200
  SStreamTask* pTask = NULL;
×
UNCOV
1201
  int32_t      code = streamMetaAcquireTask(pMeta, pReq->streamId, pReq->taskId, &pTask);
×
UNCOV
1202
  if (pTask == NULL || (code != 0)) {
×
1203
    tqError("s-task:0x%x failed to acquire task to resume, it may have been dropped or stopped", pReq->taskId);
×
1204
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1205
  }
1206

UNCOV
1207
  streamMutexLock(&pTask->lock);
×
UNCOV
1208
  SStreamTaskState pState = streamTaskGetStatus(pTask);
×
UNCOV
1209
  tqDebug("s-task:%s start to resume from paused, current status:%s", pTask->id.idStr, pState.name);
×
UNCOV
1210
  streamMutexUnlock(&pTask->lock);
×
1211

UNCOV
1212
  code = tqProcessTaskResumeImpl(handle, pTask, sversion, pReq->igUntreated, fromVnode);
×
UNCOV
1213
  if (code != 0) {
×
1214
    streamMetaReleaseTask(pMeta, pTask);
×
1215
    return code;
×
1216
  }
1217

UNCOV
1218
  STaskId*     pHTaskId = &pTask->hTaskInfo.id;
×
UNCOV
1219
  SStreamTask* pHTask = NULL;
×
UNCOV
1220
  code = streamMetaAcquireTask(pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
×
UNCOV
1221
  if (pHTask && (code == 0)) {
×
UNCOV
1222
    streamMutexLock(&pHTask->lock);
×
UNCOV
1223
    SStreamTaskState p = streamTaskGetStatus(pHTask);
×
UNCOV
1224
    tqDebug("s-task:%s related history task start to resume from paused, current status:%s", pHTask->id.idStr, p.name);
×
UNCOV
1225
    streamMutexUnlock(&pHTask->lock);
×
1226

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

UNCOV
1230
    streamMetaReleaseTask(pMeta, pHTask);
×
1231
  }
1232

UNCOV
1233
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1234
  return TSDB_CODE_SUCCESS;
×
1235
}
1236

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

UNCOV
1239
int32_t doProcessDummyRspMsg(SStreamMeta* UNUSED_PARAM(pMeta), SRpcMsg* pMsg) {
×
UNCOV
1240
  rpcFreeCont(pMsg->pCont);
×
UNCOV
1241
  pMsg->pCont = NULL;
×
UNCOV
1242
  return TSDB_CODE_SUCCESS;
×
1243
}
1244

UNCOV
1245
int32_t tqStreamProcessStreamHbRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
UNCOV
1246
  SMStreamHbRspMsg rsp = {0};
×
UNCOV
1247
  int32_t          code = 0;
×
1248
  SDecoder         decoder;
UNCOV
1249
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
UNCOV
1250
  int32_t          len = pMsg->contLen - sizeof(SMsgHead);
×
1251

UNCOV
1252
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
UNCOV
1253
  code = tDecodeStreamHbRsp(&decoder, &rsp);
×
UNCOV
1254
  if (code < 0) {
×
1255
    terrno = TSDB_CODE_INVALID_MSG;
×
1256
    tDecoderClear(&decoder);
×
1257
    tqError("vgId:%d failed to parse hb rsp msg, code:%s", pMeta->vgId, tstrerror(terrno));
×
1258
    return terrno;
×
1259
  }
1260

UNCOV
1261
  tDecoderClear(&decoder);
×
UNCOV
1262
  return streamProcessHeartbeatRsp(pMeta, &rsp);
×
1263
}
1264

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

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

UNCOV
1269
int32_t tqStreamProcessCheckpointReadyRsp(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
UNCOV
1270
  SMStreamCheckpointReadyRspMsg* pRsp = pMsg->pCont;
×
1271

UNCOV
1272
  SStreamTask* pTask = NULL;
×
UNCOV
1273
  int32_t      code = streamMetaAcquireTask(pMeta, pRsp->streamId, pRsp->downstreamTaskId, &pTask);
×
UNCOV
1274
  if (pTask == NULL || (code != 0)) {
×
UNCOV
1275
    tqError("vgId:%d failed to acquire task:0x%x when handling checkpoint-ready msg, it may have been dropped",
×
1276
            pRsp->downstreamNodeId, pRsp->downstreamTaskId);
UNCOV
1277
    return code;
×
1278
  }
1279

UNCOV
1280
  code = streamTaskProcessCheckpointReadyRsp(pTask, pRsp->upstreamTaskId, pRsp->checkpointId);
×
UNCOV
1281
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1282
  return code;
×
1283
}
1284

UNCOV
1285
int32_t tqStreamTaskProcessConsenChkptIdReq(SStreamMeta* pMeta, SRpcMsg* pMsg) {
×
UNCOV
1286
  int32_t                vgId = pMeta->vgId;
×
UNCOV
1287
  int32_t                code = 0;
×
UNCOV
1288
  SStreamTask*           pTask = NULL;
×
UNCOV
1289
  char*                  msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
×
UNCOV
1290
  int32_t                len = pMsg->contLen - sizeof(SMsgHead);
×
UNCOV
1291
  int64_t                now = taosGetTimestampMs();
×
1292
  SDecoder               decoder;
UNCOV
1293
  SRestoreCheckpointInfo req = {0};
×
1294

UNCOV
1295
  tDecoderInit(&decoder, (uint8_t*)msg, len);
×
UNCOV
1296
  if ((code = tDecodeRestoreCheckpointInfo(&decoder, &req)) < 0) {
×
1297
    tqError("vgId:%d failed to decode set consensus checkpointId req, code:%s", vgId, tstrerror(code));
×
1298
    tDecoderClear(&decoder);
×
1299
    return TSDB_CODE_SUCCESS;
×
1300
  }
1301

UNCOV
1302
  tDecoderClear(&decoder);
×
1303

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

1314
    return 0;
×
1315
  }
1316

1317
  // discard the rsp, since it is expired.
UNCOV
1318
  if (req.startTs < pTask->execInfo.created) {
×
UNCOV
1319
    tqWarn("s-task:%s vgId:%d create time:%" PRId64 " recv expired consensus checkpointId:%" PRId64
×
1320
           " from task createTs:%" PRId64 " < task createTs:%" PRId64 ", discard",
1321
           pTask->id.idStr, pMeta->vgId, pTask->execInfo.created, req.checkpointId, req.startTs,
1322
           pTask->execInfo.created);
UNCOV
1323
    streamMetaAddFailedTaskSelf(pTask, now);
×
UNCOV
1324
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1325
    return TSDB_CODE_SUCCESS;
×
1326
  }
1327

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

UNCOV
1331
  streamMutexLock(&pTask->lock);
×
UNCOV
1332
  if (pTask->chkInfo.checkpointId < req.checkpointId) {
×
1333
    tqFatal("s-task:%s vgId:%d invalid consensus-checkpointId:%" PRId64 ", greater than existed checkpointId:%" PRId64,
×
1334
            pTask->id.idStr, vgId, req.checkpointId, pTask->chkInfo.checkpointId);
1335

1336
    streamMutexUnlock(&pTask->lock);
×
1337
    streamMetaReleaseTask(pMeta, pTask);
×
1338
    return 0;
×
1339
  }
1340

UNCOV
1341
  SConsenChkptInfo* pConsenInfo = &pTask->status.consenChkptInfo;
×
UNCOV
1342
  if (pConsenInfo->consenChkptTransId >= req.transId) {
×
1343
    tqDebug("s-task:%s vgId:%d latest consensus transId:%d, expired consensus trans:%d, discard", pTask->id.idStr, vgId,
×
1344
            pConsenInfo->consenChkptTransId, req.transId);
1345
    streamMutexUnlock(&pTask->lock);
×
1346
    streamMetaReleaseTask(pMeta, pTask);
×
1347
    return TSDB_CODE_SUCCESS;
×
1348
  }
1349

UNCOV
1350
  if (pTask->chkInfo.checkpointId != req.checkpointId) {
×
1351
    tqDebug("s-task:%s vgId:%d update the checkpoint from %" PRId64 " to %" PRId64 " transId:%d", pTask->id.idStr, vgId,
×
1352
            pTask->chkInfo.checkpointId, req.checkpointId, req.transId);
1353
    pTask->chkInfo.checkpointId = req.checkpointId;
×
1354
    tqSetRestoreVersionInfo(pTask);
×
1355
  } else {
UNCOV
1356
    tqDebug("s-task:%s vgId:%d consensus-checkpointId:%" PRId64 " equals to current id, transId:%d not update",
×
1357
            pTask->id.idStr, vgId, req.checkpointId, req.transId);
1358
  }
1359

UNCOV
1360
  streamTaskSetConsenChkptIdRecv(pTask, req.transId, now);
×
UNCOV
1361
  streamMutexUnlock(&pTask->lock);
×
1362

UNCOV
1363
  if (pMeta->role == NODE_ROLE_LEADER) {
×
UNCOV
1364
    code = tqStreamStartOneTaskAsync(pMeta, pTask->pMsgCb, req.streamId, req.taskId);
×
UNCOV
1365
    if (code) {
×
1366
      tqError("s-task:0x%x vgId:%d failed start task async, code:%s", req.taskId, vgId, tstrerror(code));
×
1367
    }
1368
  } else {
1369
    tqDebug("vgId:%d follower not start task:%s", vgId, pTask->id.idStr);
×
1370
  }
1371

UNCOV
1372
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
1373
  return 0;
×
1374
}
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