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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

69.81
/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,691✔
38
  int32_t code = TSDB_CODE_SUCCESS;
8,691✔
39
  int32_t vgId = pMeta->vgId;
8,691✔
40
  int64_t now = taosGetTimestampMs();
8,781✔
41
  SArray* pTaskList = NULL;
8,781✔
42

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

46
  if (numOfTasks == 0) {
8,782✔
47
    stInfo("vgId:%d no tasks exist, quit from consensus checkpointId", pMeta->vgId);
8,689!
48

49
    streamMetaWLock(pMeta);
8,689✔
50
    streamMetaResetStartInfo(&pMeta->startInfo, vgId);
8,689✔
51
    streamMetaWUnLock(pMeta);
8,689✔
52
    return TSDB_CODE_SUCCESS;
8,689✔
53
  }
54

55
  code = prepareBeforeStartTasks(pMeta, &pTaskList, now);
93✔
56
  if (code != TSDB_CODE_SUCCESS) {
93!
57
    return TSDB_CODE_SUCCESS;  // ignore the error and return directly
×
58
  }
59

60
  // broadcast the check downstream tasks msg only for tasks with related fill-history tasks.
61
  numOfTasks = taosArrayGetSize(pTaskList);
93✔
62

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

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

86
    streamMetaReleaseTask(pMeta, pTask);
288✔
87
  }
88

89
  // Tasks, with related fill-history task or without any checkpoint yet, can be started directly here.
90
  for (int32_t i = 0; i < numOfTasks; ++i) {
381✔
91
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
288✔
92

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

102
      continue;
90✔
103
    }
104

105
    STaskExecStatisInfo* pInfo = &pTask->execInfo;
288✔
106

107
    // fill-history task can only be launched by related stream tasks.
108
    if (pTask->info.fillHistory == 1) {
288✔
109
      stDebug("s-task:%s fill-history task wait related stream task start", pTask->id.idStr);
45!
110
      streamMetaReleaseTask(pMeta, pTask);
45✔
111
      continue;
45✔
112
    }
113

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

125
      code = streamMetaAddTaskLaunchResult(pMeta, pTaskId->streamId, pTaskId->taskId, pInfo->checkTs, pInfo->readyTs,
×
126
                                           true);
127
      streamMetaReleaseTask(pMeta, pTask);
×
128
      continue;
×
129
    }
130

131
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
243✔
132
      int32_t ret = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_INIT);
45✔
133
      if (ret != TSDB_CODE_SUCCESS) {
45!
134
        stError("vgId:%d failed to handle event:%d", pMeta->vgId, TASK_EVENT_INIT);
×
135
        code = ret;
×
136

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

143
      streamMetaReleaseTask(pMeta, pTask);
45✔
144
      continue;
45✔
145
    }
146

147
    // negotiate the consensus checkpoint id for current task
148
    code = streamTaskSendNegotiateChkptIdMsg(pTask);
198✔
149

150
    // this task may has no checkpoint, but others tasks may generate checkpoint already?
151
    streamMetaReleaseTask(pMeta, pTask);
198✔
152
  }
153

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

161
int32_t prepareBeforeStartTasks(SStreamMeta* pMeta, SArray** pList, int64_t now) {
93✔
162
  streamMetaWLock(pMeta);
93✔
163

164
  if (pMeta->closeFlag) {
93!
165
    streamMetaWUnLock(pMeta);
×
166
    stError("vgId:%d vnode is closed, not start check task(s) downstream status", pMeta->vgId);
×
167
    return TSDB_CODE_FAILED;
×
168
  }
169

170
  *pList = taosArrayDup(pMeta->pTaskList, NULL);
93✔
171
  if (*pList == NULL) {
93!
172
    return terrno;
×
173
  }
174

175
  taosHashClear(pMeta->startInfo.pReadyTaskSet);
93✔
176
  taosHashClear(pMeta->startInfo.pFailedTaskSet);
93✔
177
  pMeta->startInfo.startTs = now;
93✔
178

179
  int32_t code = streamMetaResetTaskStatus(pMeta);
93✔
180
  streamMetaWUnLock(pMeta);
93✔
181

182
  return code;
93✔
183
}
184

185
void streamMetaResetStartInfo(STaskStartInfo* pStartInfo, int32_t vgId) {
8,758✔
186
  taosHashClear(pStartInfo->pReadyTaskSet);
8,758✔
187
  taosHashClear(pStartInfo->pFailedTaskSet);
8,758✔
188
  pStartInfo->tasksWillRestart = 0;
8,758✔
189
  pStartInfo->readyTs = 0;
8,758✔
190
  pStartInfo->elapsedTime = 0;
8,758✔
191

192
  // reset the sentinel flag value to be 0
193
  pStartInfo->startAllTasks = 0;
8,758✔
194
  stDebug("vgId:%d clear start-all-task info", vgId);
8,758✔
195
}
8,758✔
196

197
int32_t streamMetaAddTaskLaunchResult(SStreamMeta* pMeta, int64_t streamId, int32_t taskId, int64_t startTs,
11,227✔
198
                                      int64_t endTs, bool ready) {
199
  STaskStartInfo* pStartInfo = &pMeta->startInfo;
11,227✔
200
  STaskId         id = {.streamId = streamId, .taskId = taskId};
11,227✔
201
  int32_t         vgId = pMeta->vgId;
11,227✔
202
  bool            allRsp = true;
11,227✔
203
  SStreamTask*    p = NULL;
11,227✔
204

205
  streamMetaWLock(pMeta);
11,227✔
206
  int32_t code = streamMetaAcquireTaskUnsafe(pMeta, &id, &p);
11,227✔
207
  if (code != 0) {  // task does not exist in current vnode, not record the complete info
11,227✔
208
    stError("vgId:%d s-task:0x%x not exists discard the check downstream info", vgId, taskId);
1!
209
    streamMetaWUnLock(pMeta);
1✔
210
    return 0;
1✔
211
  }
212

213
  streamMetaReleaseTask(pMeta, p);
11,226✔
214

215
  if (pStartInfo->startAllTasks != 1) {
11,227✔
216
    int64_t el = endTs - startTs;
10,985✔
217
    stDebug(
10,985✔
218
        "vgId:%d not in start all task(s) process, not record launch result status, s-task:0x%x launch succ:%d elapsed "
219
        "time:%" PRId64 "ms",
220
        vgId, taskId, ready, el);
221
    streamMetaWUnLock(pMeta);
10,985✔
222
    return 0;
10,985✔
223
  }
224

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

240
  int32_t numOfTotal = streamMetaGetNumOfTasks(pMeta);
242✔
241
  int32_t numOfRecv = taosHashGetSize(pStartInfo->pReadyTaskSet) + taosHashGetSize(pStartInfo->pFailedTaskSet);
242✔
242

243
  allRsp = allCheckDownstreamRsp(pMeta, pStartInfo, numOfTotal);
242✔
244
  if (allRsp) {
242✔
245
    pStartInfo->readyTs = taosGetTimestampMs();
69✔
246
    pStartInfo->elapsedTime = (pStartInfo->startTs != 0) ? pStartInfo->readyTs - pStartInfo->startTs : 0;
69!
247

248
    stDebug("vgId:%d all %d task(s) check downstream completed, last completed task:0x%x (succ:%d) startTs:%" PRId64
69✔
249
                ", readyTs:%" PRId64 " total elapsed time:%.2fs",
250
            vgId, numOfTotal, taskId, ready, pStartInfo->startTs, pStartInfo->readyTs,
251
            pStartInfo->elapsedTime / 1000.0);
252

253
    // print the initialization elapsed time and info
254
    displayStatusInfo(pMeta, pStartInfo->pReadyTaskSet, true);
69✔
255
    displayStatusInfo(pMeta, pStartInfo->pFailedTaskSet, false);
69✔
256
    streamMetaResetStartInfo(pStartInfo, vgId);
69✔
257
    streamMetaWUnLock(pMeta);
69✔
258

259
    code = pStartInfo->completeFn(pMeta);
69✔
260
  } else {
261
    streamMetaWUnLock(pMeta);
173✔
262
    stDebug("vgId:%d recv check downstream results, s-task:0x%x succ:%d, received:%d, total:%d", vgId, taskId, ready,
173✔
263
            numOfRecv, numOfTotal);
264
  }
265

266
  return code;
242✔
267
}
268

269
// check all existed tasks are received rsp
270
bool allCheckDownstreamRsp(SStreamMeta* pMeta, STaskStartInfo* pStartInfo, int32_t numOfTotal) {
242✔
271
  for (int32_t i = 0; i < numOfTotal; ++i) {
684✔
272
    SStreamTaskId* pTaskId = taosArrayGet(pMeta->pTaskList, i);
615✔
273
    if (pTaskId == NULL) {
615!
274
      continue;
×
275
    }
276

277
    STaskId idx = {.streamId = pTaskId->streamId, .taskId = pTaskId->taskId};
615✔
278
    void*   px = taosHashGet(pStartInfo->pReadyTaskSet, &idx, sizeof(idx));
615✔
279
    if (px == NULL) {
615✔
280
      px = taosHashGet(pStartInfo->pFailedTaskSet, &idx, sizeof(idx));
224✔
281
      if (px == NULL) {
224✔
282
        return false;
173✔
283
      }
284
    }
285
  }
286

287
  return true;
69✔
288
}
289

290
void displayStatusInfo(SStreamMeta* pMeta, SHashObj* pTaskSet, bool succ) {
138✔
291
  int32_t vgId = pMeta->vgId;
138✔
292
  void*   pIter = NULL;
138✔
293
  size_t  keyLen = 0;
138✔
294

295
  stInfo("vgId:%d %d tasks check-downstream completed, %s", vgId, taosHashGetSize(pTaskSet),
138!
296
         succ ? "success" : "failed");
297

298
  while ((pIter = taosHashIterate(pTaskSet, pIter)) != NULL) {
379✔
299
    STaskInitTs* pInfo = pIter;
241✔
300
    void*        key = taosHashGetKey(pIter, &keyLen);
241✔
301
    SStreamTask* pTask = NULL;
241✔
302
    int32_t      code = streamMetaAcquireTaskUnsafe(pMeta, key, &pTask);
241✔
303
    if (code == 0) {
241!
304
      stInfo("s-task:%s level:%d vgId:%d, init:%" PRId64 ", initEnd:%" PRId64 ", %s", pTask->id.idStr,
241!
305
             pTask->info.taskLevel, vgId, pInfo->start, pInfo->end, pInfo->success ? "success" : "failed");
306
      streamMetaReleaseTask(pMeta, pTask);
241✔
307
    } else {
308
      stInfo("s-task:0x%x is dropped already, %s", (int32_t)((STaskId*)key)->taskId, succ ? "success" : "failed");
×
309
    }
310
  }
311
}
138✔
312

313
int32_t streamMetaInitStartInfo(STaskStartInfo* pStartInfo) {
10,703✔
314
  _hash_fn_t fp = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
10,703✔
315

316
  pStartInfo->pReadyTaskSet = taosHashInit(64, fp, false, HASH_NO_LOCK);
10,716✔
317
  if (pStartInfo->pReadyTaskSet == NULL) {
10,715!
318
    return terrno;
×
319
  }
320

321
  pStartInfo->pFailedTaskSet = taosHashInit(4, fp, false, HASH_NO_LOCK);
10,715✔
322
  if (pStartInfo->pFailedTaskSet == NULL) {
10,714!
323
    return terrno;
×
324
  }
325

326
  return 0;
10,715✔
327
}
328

329
void streamMetaClearStartInfo(STaskStartInfo* pStartInfo) {
10,715✔
330
  taosHashCleanup(pStartInfo->pReadyTaskSet);
10,715✔
331
  taosHashCleanup(pStartInfo->pFailedTaskSet);
10,717✔
332
  pStartInfo->readyTs = 0;
10,716✔
333
  pStartInfo->elapsedTime = 0;
10,716✔
334
  pStartInfo->startTs = 0;
10,716✔
335
  pStartInfo->startAllTasks = 0;
10,716✔
336
  pStartInfo->tasksWillRestart = 0;
10,716✔
337
  pStartInfo->restartCount = 0;
10,716✔
338
}
10,716✔
339

340
int32_t streamMetaStartOneTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId) {
7,363✔
341
  int32_t      code = 0;
7,363✔
342
  int32_t      vgId = pMeta->vgId;
7,363✔
343
  SStreamTask* pTask = NULL;
7,363✔
344
  bool         continueExec = true;
7,363✔
345

346
  stInfo("vgId:%d start task:0x%x by checking it's downstream status", vgId, taskId);
7,363!
347

348
  code = streamMetaAcquireTask(pMeta, streamId, taskId, &pTask);
7,364✔
349
  if ((pTask == NULL) || (code != 0)) {
7,364!
350
    stError("vgId:%d failed to acquire task:0x%x when starting task", vgId, taskId);
5!
351
    int32_t ret = streamMetaAddFailedTask(pMeta, streamId, taskId);
5✔
352
    if (ret) {
5!
353
      stError("s-task:0x%x add check downstream failed, core:%s", taskId, tstrerror(ret));
×
354
    }
355

356
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
5✔
357
  }
358

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

367
  // the start all tasks procedure may happen to start the newly deployed stream task, and results in the
368
  // concurrently start this task by two threads.
369
  streamMutexLock(&pTask->lock);
7,359✔
370

371
  SStreamTaskState status = streamTaskGetStatus(pTask);
7,358✔
372
  if (status.state != TASK_STATUS__UNINIT) {
7,358!
373
    stError("s-task:0x%x vgId:%d status:%s not uninit status, not start stream task", taskId, vgId, status.name);
×
374
    continueExec = false;
×
375
  } else {
376
    continueExec = true;
7,358✔
377
  }
378
  streamMutexUnlock(&pTask->lock);
7,358✔
379

380
  if (!continueExec) {
7,359!
381
    streamMetaReleaseTask(pMeta, pTask);
×
382
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
383
  }
384

385
  if(pTask->status.downstreamReady != 0) {
7,359!
386
    stFatal("s-task:0x%x downstream should be not ready, but it ready here, internal error happens", taskId);
×
387
    streamMetaReleaseTask(pMeta, pTask);
×
388
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
389
  }
390

391
  // avoid initialization and destroy running concurrently.
392
  streamMutexLock(&pTask->lock);
7,359✔
393
  if (pTask->pBackend == NULL) {
7,358!
394
    code = pMeta->expandTaskFn(pTask);
7,358✔
395
    streamMutexUnlock(&pTask->lock);
7,359✔
396

397
    if (code != TSDB_CODE_SUCCESS) {
7,359!
398
      streamMetaAddFailedTaskSelf(pTask, pInfo->readyTs);
×
399
    }
400
  } else {
401
    streamMutexUnlock(&pTask->lock);
×
402
  }
403

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

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

418
  streamMetaReleaseTask(pMeta, pTask);
7,358✔
419
  return code;
7,359✔
420
}
421

422
int32_t streamMetaStopAllTasks(SStreamMeta* pMeta) {
7,692✔
423
  streamMetaRLock(pMeta);
7,692✔
424

425
  SArray* pTaskList = NULL;
7,699✔
426
  int32_t num = taosArrayGetSize(pMeta->pTaskList);
7,699✔
427
  stDebug("vgId:%d stop all %d stream task(s)", pMeta->vgId, num);
7,698✔
428

429
  if (num == 0) {
7,701✔
430
    stDebug("vgId:%d stop all %d task(s) completed, elapsed time:0 Sec.", pMeta->vgId, num);
7,533✔
431
    streamMetaRUnLock(pMeta);
7,533✔
432
    return TSDB_CODE_SUCCESS;
7,531✔
433
  }
434

435
  int64_t st = taosGetTimestampMs();
168✔
436

437
  // send hb msg to mnode before closing all tasks.
438
  int32_t code = streamMetaSendMsgBeforeCloseTasks(pMeta, &pTaskList);
168✔
439
  if (code != TSDB_CODE_SUCCESS) {
168!
440
    streamMetaRUnLock(pMeta);
×
441
    return code;
×
442
  }
443

444
  int32_t numOfTasks = taosArrayGetSize(pTaskList);
168✔
445
  for (int32_t i = 0; i < numOfTasks; ++i) {
661✔
446
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
493✔
447
    SStreamTask*   pTask = NULL;
493✔
448

449
    code = streamMetaAcquireTaskNoLock(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
493✔
450
    if (code != TSDB_CODE_SUCCESS) {
493✔
451
      continue;
59✔
452
    }
453

454
    int32_t ret = streamTaskStop(pTask);
434✔
455
    if (ret) {
434!
456
      stError("s-task:0x%x failed to stop task, code:%s", pTaskId->taskId, tstrerror(ret));
×
457
    }
458

459
    streamMetaReleaseTask(pMeta, pTask);
434✔
460
  }
461

462
  taosArrayDestroy(pTaskList);
168✔
463

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

467
  streamMetaRUnLock(pMeta);
168✔
468
  return code;
168✔
469
}
470

471
int32_t streamTaskCheckIfReqConsenChkptId(SStreamTask* pTask, int64_t ts) {
22,958✔
472
  SConsenChkptInfo* pConChkptInfo = &pTask->status.consenChkptInfo;
22,958✔
473

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

485
    // not recv consensus-checkpoint rsp for 60sec, send it again in hb to mnode
486
    if ((pConChkptInfo->status == TASK_CONSEN_CHKPT_SEND) && el > 60) {
22,774!
487
      pConChkptInfo->statusTs = ts;
×
488

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

496
  return 0;
22,774✔
497
}
498

499
void streamTaskSetConsenChkptIdRecv(SStreamTask* pTask, int32_t transId, int64_t ts) {
157✔
500
  SConsenChkptInfo* pInfo = &pTask->status.consenChkptInfo;
157✔
501
  pInfo->consenChkptTransId = transId;
157✔
502
  pInfo->status = TASK_CONSEN_CHKPT_RECV;
157✔
503
  pInfo->statusTs = ts;
157✔
504

505
  stInfo("s-task:%s set recv consen-checkpointId, transId:%d", pTask->id.idStr, transId);
157!
506
}
157✔
507

508
void streamTaskSetReqConsenChkptId(SStreamTask* pTask, int64_t ts) {
198✔
509
  SConsenChkptInfo* pInfo = &pTask->status.consenChkptInfo;
198✔
510
  int32_t           prevTrans = pInfo->consenChkptTransId;
198✔
511

512
  pInfo->status = TASK_CONSEN_CHKPT_REQ;
198✔
513
  pInfo->statusTs = ts;
198✔
514
  pInfo->consenChkptTransId = 0;
198✔
515

516
  stDebug("s-task:%s set req consen-checkpointId flag, prev transId:%d, ts:%" PRId64, pTask->id.idStr, prevTrans, ts);
198✔
517
}
198✔
518

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

528
  stDebug("vgId:%d add start failed task:0x%x", pMeta->vgId, taskId);
43!
529

530
  streamMetaRLock(pMeta);
43✔
531

532
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
43✔
533
  if (code == 0) {
43✔
534
    startTs = pTask->taskCheckInfo.startTs;
11✔
535
    hasFillhistoryTask = HAS_RELATED_FILLHISTORY_TASK(pTask);
11✔
536
    hId = pTask->hTaskInfo.id;
11✔
537
    streamMetaReleaseTask(pMeta, pTask);
11✔
538

539
    streamMetaRUnLock(pMeta);
11✔
540

541
    // add the failed task info, along with the related fill-history task info into tasks list.
542
    code = streamMetaAddTaskLaunchResult(pMeta, streamId, taskId, startTs, now, false);
11✔
543
    if (hasFillhistoryTask) {
11✔
544
      code = streamMetaAddTaskLaunchResult(pMeta, hId.streamId, hId.taskId, startTs, now, false);
4✔
545
    }
546
  } else {
547
    streamMetaRUnLock(pMeta);
32✔
548

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

554
  return code;
43✔
555
}
556

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

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