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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

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

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

71.19
/source/libs/stream/src/streamStartTask.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 "executor.h"
17
#include "streamBackendRocksdb.h"
18
#include "streamInt.h"
19
#include "tmisce.h"
20
#include "tref.h"
21
#include "tsched.h"
22
#include "tstream.h"
23
#include "ttimer.h"
24
#include "wal.h"
25

26
typedef struct STaskInitTs {
27
  int64_t start;
28
  int64_t end;
29
  bool    success;
30
} STaskInitTs;
31

32
static int32_t prepareBeforeStartTasks(SStreamMeta* pMeta, SArray** pList, int64_t now);
33
static bool    allCheckDownstreamRsp(SStreamMeta* pMeta, STaskStartInfo* pStartInfo, int32_t numOfTotal);
34
static void    displayStatusInfo(SStreamMeta* pMeta, SHashObj* pTaskSet, bool succ);
35

36
// restore the checkpoint id by negotiating the latest consensus checkpoint id
37
int32_t streamMetaStartAllTasks(SStreamMeta* pMeta) {
8,253✔
38
  int32_t code = TSDB_CODE_SUCCESS;
8,253✔
39
  int32_t vgId = pMeta->vgId;
8,253✔
40
  int64_t now = taosGetTimestampMs();
8,253✔
41
  SArray* pTaskList = NULL;
8,253✔
42

43
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
8,253✔
44
  stInfo("vgId:%d start to consensus checkpointId for all %d task(s), start ts:%" PRId64, vgId, numOfTasks, now);
8,252!
45

46
  if (numOfTasks == 0) {
8,253✔
47
    stInfo("vgId:%d no tasks exist, quit from consensus checkpointId", pMeta->vgId);
8,095!
48
    return TSDB_CODE_SUCCESS;
8,095✔
49
  }
50

51
  code = prepareBeforeStartTasks(pMeta, &pTaskList, now);
158✔
52
  if (code != TSDB_CODE_SUCCESS) {
158!
53
    return TSDB_CODE_SUCCESS;  // ignore the error and return directly
×
54
  }
55

56
  // broadcast the check downstream tasks msg only for tasks with related fill-history tasks.
57
  numOfTasks = taosArrayGetSize(pTaskList);
158✔
58

59
  // prepare the fill-history task before starting all stream tasks, to avoid fill-history tasks are started without
60
  // initialization, when the operation of check downstream tasks status is executed far quickly.
61
  for (int32_t i = 0; i < numOfTasks; ++i) {
754✔
62
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
596✔
63
    SStreamTask*   pTask = NULL;
596✔
64
    code = streamMetaAcquireTask(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
596✔
65
    if ((pTask == NULL) || (code != 0)) {
596!
66
      stError("vgId:%d failed to acquire task:0x%x during start task, it may be dropped", pMeta->vgId, pTaskId->taskId);
×
67
      int32_t ret = streamMetaAddFailedTask(pMeta, pTaskId->streamId, pTaskId->taskId);
×
68
      if (ret) {
×
69
        stError("s-task:0x%x add check downstream failed, core:%s", pTaskId->taskId, tstrerror(ret));
×
70
      }
71
      continue;
×
72
    }
73

74
    if ((pTask->pBackend == NULL) && ((pTask->info.fillHistory == 1) || HAS_RELATED_FILLHISTORY_TASK(pTask))) {
596!
75
      code = pMeta->expandTaskFn(pTask);
282✔
76
      if (code != TSDB_CODE_SUCCESS) {
282!
77
        stError("s-task:0x%x vgId:%d failed to expand stream backend", pTaskId->taskId, vgId);
×
78
        streamMetaAddFailedTaskSelf(pTask, pTask->execInfo.readyTs);
×
79
      }
80
    }
81

82
    streamMetaReleaseTask(pMeta, pTask);
596✔
83
  }
84

85
  // Tasks, with related fill-history task or without any checkpoint yet, can be started directly here.
86
  for (int32_t i = 0; i < numOfTasks; ++i) {
754✔
87
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
596✔
88

89
    SStreamTask* pTask = NULL;
596✔
90
    code = streamMetaAcquireTask(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
596✔
91
    if ((pTask == NULL )|| (code != 0)) {
596!
92
      stError("vgId:%d failed to acquire task:0x%x during start tasks", pMeta->vgId, pTaskId->taskId);
×
93
      int32_t ret = streamMetaAddFailedTask(pMeta, pTaskId->streamId, pTaskId->taskId);
×
94
      if (ret) {
×
95
        stError("s-task:0x%x failed add check downstream failed, core:%s", pTaskId->taskId, tstrerror(ret));
×
96
      }
97

98
      continue;
282✔
99
    }
100

101
    STaskExecStatisInfo* pInfo = &pTask->execInfo;
596✔
102

103
    // fill-history task can only be launched by related stream tasks.
104
    if (pTask->info.fillHistory == 1) {
596✔
105
      stDebug("s-task:%s fill-history task wait related stream task start", pTask->id.idStr);
141!
106
      streamMetaReleaseTask(pMeta, pTask);
141✔
107
      continue;
141✔
108
    }
109

110
    // ready now, start the related fill-history task
111
    if (pTask->status.downstreamReady == 1) {
455!
112
      if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
113
        stDebug("s-task:%s downstream ready, no need to check downstream, check only related fill-history task",
×
114
                pTask->id.idStr);
115
        code = streamLaunchFillHistoryTask(pTask);  // todo: how about retry launch fill-history task?
×
116
        if (code) {
×
117
          stError("s-task:%s failed to launch history task, code:%s", pTask->id.idStr, tstrerror(code));
×
118
        }
119
      }
120

121
      code = streamMetaAddTaskLaunchResult(pMeta, pTaskId->streamId, pTaskId->taskId, pInfo->checkTs, pInfo->readyTs,
×
122
                                           true);
123
      streamMetaReleaseTask(pMeta, pTask);
×
124
      continue;
×
125
    }
126

127
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
455✔
128
      int32_t ret = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_INIT);
141✔
129
      if (ret != TSDB_CODE_SUCCESS) {
141!
130
        stError("vgId:%d failed to handle event:%d", pMeta->vgId, TASK_EVENT_INIT);
×
131
        code = ret;
×
132

133
        // do no added into result hashmap if it is failed due to concurrently starting of this stream task.
134
        if (code != TSDB_CODE_STREAM_CONFLICT_EVENT) {
×
135
          streamMetaAddFailedTaskSelf(pTask, pInfo->readyTs);
×
136
        }
137
      }
138

139
      streamMetaReleaseTask(pMeta, pTask);
141✔
140
      continue;
141✔
141
    }
142

143
    // negotiate the consensus checkpoint id for current task
144
    code = streamTaskSendNegotiateChkptIdMsg(pTask);
314✔
145

146
    // this task may has no checkpoint, but others tasks may generate checkpoint already?
147
    streamMetaReleaseTask(pMeta, pTask);
314✔
148
  }
149

150
  // prepare the fill-history task before starting all stream tasks, to avoid fill-history tasks are started without
151
  // initialization, when the operation of check downstream tasks status is executed far quickly.
152
  stInfo("vgId:%d start all task(s) completed", pMeta->vgId);
158!
153
  taosArrayDestroy(pTaskList);
158✔
154
  return code;
158✔
155
}
156

157
int32_t prepareBeforeStartTasks(SStreamMeta* pMeta, SArray** pList, int64_t now) {
158✔
158
  streamMetaWLock(pMeta);
158✔
159

160
  if (pMeta->closeFlag) {
158!
161
    streamMetaWUnLock(pMeta);
×
162
    stError("vgId:%d vnode is closed, not start check task(s) downstream status", pMeta->vgId);
×
163
    return TSDB_CODE_FAILED;
×
164
  }
165

166
  *pList = taosArrayDup(pMeta->pTaskList, NULL);
158✔
167
  if (*pList == NULL) {
158!
168
    return terrno;
×
169
  }
170

171
  taosHashClear(pMeta->startInfo.pReadyTaskSet);
158✔
172
  taosHashClear(pMeta->startInfo.pFailedTaskSet);
158✔
173
  pMeta->startInfo.startTs = now;
158✔
174

175
  int32_t code = streamMetaResetTaskStatus(pMeta);
158✔
176
  streamMetaWUnLock(pMeta);
158✔
177

178
  return code;
158✔
179
}
180

181
void streamMetaResetStartInfo(STaskStartInfo* pStartInfo, int32_t vgId) {
1,773✔
182
  taosHashClear(pStartInfo->pReadyTaskSet);
1,773✔
183
  taosHashClear(pStartInfo->pFailedTaskSet);
1,773✔
184
  pStartInfo->tasksWillRestart = 0;
1,773✔
185
  pStartInfo->readyTs = 0;
1,773✔
186
  pStartInfo->elapsedTime = 0;
1,773✔
187

188
  // reset the sentinel flag value to be 0
189
  pStartInfo->startAllTasks = 0;
1,773✔
190
  stDebug("vgId:%d clear start-all-task info", vgId);
1,773✔
191
}
1,773✔
192

193
int32_t streamMetaAddTaskLaunchResult(SStreamMeta* pMeta, int64_t streamId, int32_t taskId, int64_t startTs,
13,761✔
194
                                      int64_t endTs, bool ready) {
195
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
13,761✔
196
  STaskId         id = {.streamId = streamId, .taskId = taskId};
13,761✔
197
  int32_t         vgId = pMeta->vgId;
13,761✔
198
  bool            allRsp = true;
13,761✔
199
  SStreamTask*    p = NULL;
13,761✔
200

201
  streamMetaWLock(pMeta);
13,761✔
202
  int32_t code = streamMetaAcquireTaskUnsafe(pMeta, &id, &p);
13,767✔
203
  if (code != 0) {  // task does not exist in current vnode, not record the complete info
13,766✔
204
    stError("vgId:%d s-task:0x%x not exists discard the check downstream info", vgId, taskId);
1!
205
    streamMetaWUnLock(pMeta);
1✔
206
    return 0;
1✔
207
  }
208

209
  streamMetaReleaseTask(pMeta, p);
13,765✔
210

211
  if (pStartInfo->startAllTasks != 1) {
13,765✔
212
    int64_t el = endTs - startTs;
10,479✔
213
    stDebug(
10,479✔
214
        "vgId:%d not in start all task(s) process, not record launch result status, s-task:0x%x launch succ:%d elapsed "
215
        "time:%" PRId64 "ms",
216
        vgId, taskId, ready, el);
217
    streamMetaWUnLock(pMeta);
10,479✔
218
    return 0;
10,479✔
219
  }
220

221
  STaskInitTs initTs = {.start = startTs, .end = endTs, .success = ready};
3,286✔
222
  SHashObj*   pDst = ready ? pStartInfo->pReadyTaskSet : pStartInfo->pFailedTaskSet;
3,286✔
223
  code = taosHashPut(pDst, &id, sizeof(id), &initTs, sizeof(STaskInitTs));
3,286✔
224
  if (code) {
3,286✔
225
    if (code == TSDB_CODE_DUP_KEY) {
2!
226
      stError("vgId:%d record start task result failed, s-task:0x%" PRIx64
2!
227
              " already exist start results in meta start task result hashmap",
228
              vgId, id.taskId);
229
    } else {
230
      stError("vgId:%d failed to record start task:0x%" PRIx64 " results, start all tasks failed", vgId, id.taskId);
×
231
    }
232
    streamMetaWUnLock(pMeta);
2✔
233
    return code;
2✔
234
  }
235

236
  int32_t numOfTotal = streamMetaGetNumOfTasks(pMeta);
3,284✔
237
  int32_t numOfRecv = taosHashGetSize(pStartInfo->pReadyTaskSet) + taosHashGetSize(pStartInfo->pFailedTaskSet);
3,284✔
238

239
  allRsp = allCheckDownstreamRsp(pMeta, pStartInfo, numOfTotal);
3,284✔
240
  if (allRsp) {
3,284✔
241
    pStartInfo->readyTs = taosGetTimestampMs();
1,767✔
242
    pStartInfo->elapsedTime = (pStartInfo->startTs != 0) ? pStartInfo->readyTs - pStartInfo->startTs : 0;
1,767✔
243

244
    stDebug("vgId:%d all %d task(s) check downstream completed, last completed task:0x%x (succ:%d) startTs:%" PRId64
1,767✔
245
                ", readyTs:%" PRId64 " total elapsed time:%.2fs",
246
            vgId, numOfTotal, taskId, ready, pStartInfo->startTs, pStartInfo->readyTs,
247
            pStartInfo->elapsedTime / 1000.0);
248

249
    // print the initialization elapsed time and info
250
    displayStatusInfo(pMeta, pStartInfo->pReadyTaskSet, true);
1,767✔
251
    displayStatusInfo(pMeta, pStartInfo->pFailedTaskSet, false);
1,767✔
252
    streamMetaResetStartInfo(pStartInfo, vgId);
1,767✔
253
    streamMetaWUnLock(pMeta);
1,767✔
254

255
    code = pStartInfo->completeFn(pMeta);
1,767✔
256
  } else {
257
    streamMetaWUnLock(pMeta);
1,517✔
258
    stDebug("vgId:%d recv check downstream results, s-task:0x%x succ:%d, received:%d, total:%d", vgId, taskId, ready,
1,517✔
259
            numOfRecv, numOfTotal);
260
  }
261

262
  return code;
3,284✔
263
}
264

265
// check all existed tasks are received rsp
266
bool allCheckDownstreamRsp(SStreamMeta* pMeta, STaskStartInfo* pStartInfo, int32_t numOfTotal) {
3,284✔
267
  for (int32_t i = 0; i < numOfTotal; ++i) {
8,673✔
268
    SStreamTaskId* pTaskId = taosArrayGet(pMeta->pTaskList, i);
6,906✔
269
    if (pTaskId == NULL) {
6,906!
270
      continue;
×
271
    }
272

273
    STaskId idx = {.streamId = pTaskId->streamId, .taskId = pTaskId->taskId};
6,906✔
274
    void*   px = taosHashGet(pStartInfo->pReadyTaskSet, &idx, sizeof(idx));
6,906✔
275
    if (px == NULL) {
6,906✔
276
      px = taosHashGet(pStartInfo->pFailedTaskSet, &idx, sizeof(idx));
1,653✔
277
      if (px == NULL) {
1,653✔
278
        return false;
1,517✔
279
      }
280
    }
281
  }
282

283
  return true;
1,767✔
284
}
285

286
void displayStatusInfo(SStreamMeta* pMeta, SHashObj* pTaskSet, bool succ) {
3,534✔
287
  int32_t vgId = pMeta->vgId;
3,534✔
288
  void*   pIter = NULL;
3,534✔
289
  size_t  keyLen = 0;
3,534✔
290

291
  stInfo("vgId:%d %d tasks check-downstream completed, %s", vgId, taosHashGetSize(pTaskSet),
3,534!
292
         succ ? "success" : "failed");
293

294
  while ((pIter = taosHashIterate(pTaskSet, pIter)) != NULL) {
6,735✔
295
    STaskInitTs* pInfo = pIter;
3,201✔
296
    void*        key = taosHashGetKey(pIter, &keyLen);
3,201✔
297
    SStreamTask* pTask = NULL;
3,201✔
298
    int32_t      code = streamMetaAcquireTaskUnsafe(pMeta, key, &pTask);
3,201✔
299
    if (code == 0) {
3,201✔
300
      stInfo("s-task:%s level:%d vgId:%d, init:%" PRId64 ", initEnd:%" PRId64 ", %s", pTask->id.idStr,
3,188!
301
             pTask->info.taskLevel, vgId, pInfo->start, pInfo->end, pInfo->success ? "success" : "failed");
302
      streamMetaReleaseTask(pMeta, pTask);
3,188✔
303
    } else {
304
      stInfo("s-task:0x%x is dropped already, %s", (int32_t)((STaskId*)key)->taskId, succ ? "success" : "failed");
13!
305
    }
306
  }
307
}
3,534✔
308

309
int32_t streamMetaInitStartInfo(STaskStartInfo* pStartInfo) {
9,625✔
310
  _hash_fn_t fp = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
9,625✔
311

312
  pStartInfo->pReadyTaskSet = taosHashInit(64, fp, false, HASH_NO_LOCK);
9,633✔
313
  if (pStartInfo->pReadyTaskSet == NULL) {
9,633!
314
    return terrno;
×
315
  }
316

317
  pStartInfo->pFailedTaskSet = taosHashInit(4, fp, false, HASH_NO_LOCK);
9,633✔
318
  if (pStartInfo->pFailedTaskSet == NULL) {
9,633✔
319
    return terrno;
1✔
320
  }
321

322
  return 0;
9,632✔
323
}
324

325
void streamMetaClearStartInfo(STaskStartInfo* pStartInfo) {
9,631✔
326
  taosHashCleanup(pStartInfo->pReadyTaskSet);
9,631✔
327
  taosHashCleanup(pStartInfo->pFailedTaskSet);
9,632✔
328
  pStartInfo->readyTs = 0;
9,632✔
329
  pStartInfo->elapsedTime = 0;
9,632✔
330
  pStartInfo->startTs = 0;
9,632✔
331
  pStartInfo->startAllTasks = 0;
9,632✔
332
  pStartInfo->tasksWillRestart = 0;
9,632✔
333
  pStartInfo->restartCount = 0;
9,632✔
334
}
9,632✔
335

336
int32_t streamMetaStartOneTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId) {
8,811✔
337
  int32_t      code = 0;
8,811✔
338
  int32_t      vgId = pMeta->vgId;
8,811✔
339
  SStreamTask* pTask = NULL;
8,811✔
340
  bool         continueExec = true;
8,811✔
341

342
  stInfo("vgId:%d start task:0x%x by checking it's downstream status", vgId, taskId);
8,811!
343

344
  code = streamMetaAcquireTask(pMeta, streamId, taskId, &pTask);
8,811✔
345
  if ((pTask == NULL) || (code != 0)) {
8,812!
346
    stError("vgId:%d failed to acquire task:0x%x when starting task", vgId, taskId);
×
347
    int32_t ret = streamMetaAddFailedTask(pMeta, streamId, taskId);
×
348
    if (ret) {
×
349
      stError("s-task:0x%x add check downstream failed, core:%s", taskId, tstrerror(ret));
×
350
    }
351

352
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
353
  }
354

355
  // fill-history task can only be launched by related stream tasks.
356
  STaskExecStatisInfo* pInfo = &pTask->execInfo;
8,812✔
357
  if (pTask->info.fillHistory == 1) {
8,812!
358
    stError("s-task:0x%x vgId:%d fill-history task, not start here", taskId, vgId);
×
359
    streamMetaReleaseTask(pMeta, pTask);
×
360
    return TSDB_CODE_SUCCESS;
×
361
  }
362

363
  // the start all tasks procedure may happen to start the newly deployed stream task, and results in the
364
  // concurrently start this task by two threads.
365
  streamMutexLock(&pTask->lock);
8,812✔
366

367
  SStreamTaskState status = streamTaskGetStatus(pTask);
8,813✔
368
  if (status.state != TASK_STATUS__UNINIT) {
8,813!
UNCOV
369
    stError("s-task:0x%x vgId:%d status:%s not uninit status, not start stream task", taskId, vgId, status.name);
×
UNCOV
370
    continueExec = false;
×
371
  } else {
372
    continueExec = true;
8,813✔
373
  }
374
  streamMutexUnlock(&pTask->lock);
8,813✔
375

376
  if (!continueExec) {
8,813!
UNCOV
377
    streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
378
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
379
  }
380

381
  if(pTask->status.downstreamReady != 0) {
8,813!
382
    stFatal("s-task:0x%x downstream should be not ready, but it ready here, internal error happens", taskId);
×
383
    streamMetaReleaseTask(pMeta, pTask);
×
384
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
385
  }
386

387
  // avoid initialization and destroy running concurrently.
388
  streamMutexLock(&pTask->lock);
8,813✔
389
  if (pTask->pBackend == NULL) {
8,813!
390
    code = pMeta->expandTaskFn(pTask);
8,813✔
391
    streamMutexUnlock(&pTask->lock);
8,813✔
392

393
    if (code != TSDB_CODE_SUCCESS) {
8,812!
394
      streamMetaAddFailedTaskSelf(pTask, pInfo->readyTs);
×
395
    }
396
  } else {
397
    streamMutexUnlock(&pTask->lock);
×
398
  }
399

400
  // concurrently start task may cause the latter started task be failed, and also failed to added into meta result.
401
  if (code == TSDB_CODE_SUCCESS) {
8,812!
402
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_INIT);
8,812✔
403
    if (code != TSDB_CODE_SUCCESS) {
8,812!
404
      stError("s-task:%s vgId:%d failed to handle event:init-task, code:%s", pTask->id.idStr, pMeta->vgId,
×
405
              tstrerror(code));
406

407
      // do no added into result hashmap if it is failed due to concurrently starting of this stream task.
408
      if (code != TSDB_CODE_STREAM_CONFLICT_EVENT) {
×
409
        streamMetaAddFailedTaskSelf(pTask, pInfo->readyTs);
×
410
      }
411
    }
412
  }
413

414
  streamMetaReleaseTask(pMeta, pTask);
8,812✔
415
  return code;
8,813✔
416
}
417

418
int32_t streamMetaStopAllTasks(SStreamMeta* pMeta) {
3,217✔
419
  streamMetaRLock(pMeta);
3,217✔
420

421
  SArray* pTaskList = NULL;
3,233✔
422
  int32_t num = taosArrayGetSize(pMeta->pTaskList);
3,233✔
423
  stDebug("vgId:%d stop all %d stream task(s)", pMeta->vgId, num);
3,233✔
424

425
  if (num == 0) {
3,233✔
426
    stDebug("vgId:%d stop all %d task(s) completed, elapsed time:0 Sec.", pMeta->vgId, num);
3,143!
427
    streamMetaRUnLock(pMeta);
3,143✔
428
    return TSDB_CODE_SUCCESS;
3,143✔
429
  }
430

431
  int64_t st = taosGetTimestampMs();
90✔
432

433
  // send hb msg to mnode before closing all tasks.
434
  int32_t code = streamMetaSendMsgBeforeCloseTasks(pMeta, &pTaskList);
90✔
435
  if (code != TSDB_CODE_SUCCESS) {
90!
436
    streamMetaRUnLock(pMeta);
×
437
    return code;
×
438
  }
439

440
  int32_t numOfTasks = taosArrayGetSize(pTaskList);
90✔
441
  for (int32_t i = 0; i < numOfTasks; ++i) {
402✔
442
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
312✔
443
    SStreamTask*   pTask = NULL;
312✔
444

445
    code = streamMetaAcquireTaskNoLock(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
312✔
446
    if (code != TSDB_CODE_SUCCESS) {
312✔
447
      continue;
140✔
448
    }
449

450
    int64_t refId = pTask->id.refId;
172✔
451
    int32_t ret = streamTaskStop(pTask);
172✔
452
    if (ret) {
172!
453
      stError("s-task:0x%x failed to stop task, code:%s", pTaskId->taskId, tstrerror(ret));
×
454
    }
455

456
    streamMetaReleaseTask(pMeta, pTask);
172✔
457
  }
458

459
  taosArrayDestroy(pTaskList);
90✔
460

461
  double el = (taosGetTimestampMs() - st) / 1000.0;
90✔
462
  stDebug("vgId:%d stop all %d task(s) completed, elapsed time:%.2f Sec.", pMeta->vgId, num, el);
90✔
463

464
  streamMetaRUnLock(pMeta);
90✔
465
  return code;
90✔
466
}
467

468
int32_t streamTaskCheckIfReqConsenChkptId(SStreamTask* pTask, int64_t ts) {
26,853✔
469
  SConsenChkptInfo* pConChkptInfo = &pTask->status.consenChkptInfo;
26,853✔
470

471
  int32_t vgId = pTask->pMeta->vgId;
26,853✔
472
  if (pConChkptInfo->status == TASK_CONSEN_CHKPT_REQ) {
26,853✔
473
    // mark the sending of req consensus checkpoint request.
474
    pConChkptInfo->status = TASK_CONSEN_CHKPT_SEND;
129✔
475
    pConChkptInfo->statusTs = ts;
129✔
476
    stDebug("s-task:%s vgId:%d set requiring consensus-chkptId in hbMsg, ts:%" PRId64, pTask->id.idStr,
129✔
477
            vgId, pConChkptInfo->statusTs);
478
    return 1;
129✔
479
  } else {
480
    int32_t el = (ts - pConChkptInfo->statusTs) / 1000;
26,724✔
481

482
    // not recv consensus-checkpoint rsp for 60sec, send it again in hb to mnode
483
    if ((pConChkptInfo->status == TASK_CONSEN_CHKPT_SEND) && el > 60) {
26,724!
484
      pConChkptInfo->statusTs = ts;
×
485

486
      stWarn(
×
487
          "s-task:%s vgId:%d not recv consensus-chkptId for %ds(more than 60s), set requiring in Hb again, ts:%" PRId64,
488
          pTask->id.idStr, vgId, el, pConChkptInfo->statusTs);
489
      return 1;
×
490
    }
491
  }
492

493
  return 0;
26,724✔
494
}
495

496
void streamTaskSetConsenChkptIdRecv(SStreamTask* pTask, int32_t transId, int64_t ts) {
111✔
497
  SConsenChkptInfo* pInfo = &pTask->status.consenChkptInfo;
111✔
498
  pInfo->consenChkptTransId = transId;
111✔
499
  pInfo->status = TASK_CONSEN_CHKPT_RECV;
111✔
500
  pInfo->statusTs = ts;
111✔
501

502
  stInfo("s-task:%s set recv consen-checkpointId, transId:%d", pTask->id.idStr, transId);
111!
503
}
111✔
504

505
void streamTaskSetReqConsenChkptId(SStreamTask* pTask, int64_t ts) {
312✔
506
  SConsenChkptInfo* pInfo = &pTask->status.consenChkptInfo;
312✔
507
  int32_t           prevTrans = pInfo->consenChkptTransId;
312✔
508

509
  pInfo->status = TASK_CONSEN_CHKPT_REQ;
312✔
510
  pInfo->statusTs = ts;
312✔
511
  pInfo->consenChkptTransId = 0;
312✔
512

513
  stDebug("s-task:%s set req consen-checkpointId flag, prev transId:%d, ts:%" PRId64, pTask->id.idStr, prevTrans, ts);
312✔
514
}
312✔
515

516
int32_t streamMetaAddFailedTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId) {
9✔
517
  int32_t      code = TSDB_CODE_SUCCESS;
9✔
518
  int64_t      now = taosGetTimestampMs();
9✔
519
  int64_t      startTs = 0;
9✔
520
  bool         hasFillhistoryTask = false;
9✔
521
  STaskId      hId = {0};
9✔
522
  STaskId      id = {.streamId = streamId, .taskId = taskId};
9✔
523
  SStreamTask* pTask = NULL;
9✔
524

525
  stDebug("vgId:%d add start failed task:0x%x", pMeta->vgId, taskId);
9!
526

527
  streamMetaRLock(pMeta);
9✔
528

529
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
9✔
530
  if (code == 0) {
9✔
531
    startTs = pTask->taskCheckInfo.startTs;
3✔
532
    hasFillhistoryTask = HAS_RELATED_FILLHISTORY_TASK(pTask);
3✔
533
    hId = pTask->hTaskInfo.id;
3✔
534
    streamMetaReleaseTask(pMeta, pTask);
3✔
535

536
    streamMetaRUnLock(pMeta);
3✔
537

538
    // add the failed task info, along with the related fill-history task info into tasks list.
539
    code = streamMetaAddTaskLaunchResult(pMeta, streamId, taskId, startTs, now, false);
3✔
540
    if (hasFillhistoryTask) {
3!
541
      code = streamMetaAddTaskLaunchResult(pMeta, hId.streamId, hId.taskId, startTs, now, false);
3✔
542
    }
543
  } else {
544
    streamMetaRUnLock(pMeta);
6✔
545

546
    stError("failed to locate the stream task:0x%" PRIx64 "-0x%x (vgId:%d), it may have been destroyed or stopped",
6!
547
            streamId, taskId, pMeta->vgId);
548
    code = TSDB_CODE_STREAM_TASK_NOT_EXIST;
6✔
549
  }
550

551
  return code;
9✔
552
}
553

554
void streamMetaAddFailedTaskSelf(SStreamTask* pTask, int64_t failedTs) {
67✔
555
  int32_t startTs = pTask->execInfo.checkTs;
67✔
556
  int32_t code = streamMetaAddTaskLaunchResult(pTask->pMeta, pTask->id.streamId, pTask->id.taskId, startTs, failedTs, false);
67✔
557
  if (code) {
67✔
558
    stError("s-task:%s failed to add self task failed to start, code:%s", pTask->id.idStr, tstrerror(code));
2!
559
  }
560

561
  // automatically set the related fill-history task to be failed.
562
  if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
67✔
563
    STaskId* pId = &pTask->hTaskInfo.id;
56✔
564
    code = streamMetaAddTaskLaunchResult(pTask->pMeta, pId->streamId, pId->taskId, startTs, failedTs, false);
56✔
565
    if (code) {
56!
566
      stError("s-task:0x%" PRIx64 " failed to add self task failed to start, code:%s", pId->taskId, tstrerror(code));
×
567
    }
568
  }
569
}
67✔
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