• 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/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
UNCOV
37
int32_t streamMetaStartAllTasks(SStreamMeta* pMeta) {
×
UNCOV
38
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
39
  int32_t vgId = pMeta->vgId;
×
UNCOV
40
  int64_t now = taosGetTimestampMs();
×
UNCOV
41
  SArray* pTaskList = NULL;
×
42

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

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

UNCOV
51
  code = prepareBeforeStartTasks(pMeta, &pTaskList, now);
×
UNCOV
52
  if (code != TSDB_CODE_SUCCESS) {
×
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.
UNCOV
57
  numOfTasks = taosArrayGetSize(pTaskList);
×
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.
UNCOV
61
  for (int32_t i = 0; i < numOfTasks; ++i) {
×
UNCOV
62
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
×
UNCOV
63
    SStreamTask*   pTask = NULL;
×
UNCOV
64
    code = streamMetaAcquireTask(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
×
UNCOV
65
    if ((pTask == NULL) || (code != 0)) {
×
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

UNCOV
74
    if ((pTask->pBackend == NULL) && ((pTask->info.fillHistory == 1) || HAS_RELATED_FILLHISTORY_TASK(pTask))) {
×
UNCOV
75
      code = pMeta->expandTaskFn(pTask);
×
UNCOV
76
      if (code != TSDB_CODE_SUCCESS) {
×
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

UNCOV
82
    streamMetaReleaseTask(pMeta, pTask);
×
83
  }
84

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

UNCOV
89
    SStreamTask* pTask = NULL;
×
UNCOV
90
    code = streamMetaAcquireTask(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
×
UNCOV
91
    if ((pTask == NULL )|| (code != 0)) {
×
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

UNCOV
98
      continue;
×
99
    }
100

UNCOV
101
    STaskExecStatisInfo* pInfo = &pTask->execInfo;
×
102

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

110
    // ready now, start the related fill-history task
UNCOV
111
    if (pTask->status.downstreamReady == 1) {
×
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

UNCOV
127
    if (HAS_RELATED_FILLHISTORY_TASK(pTask)) {
×
UNCOV
128
      int32_t ret = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_INIT);
×
UNCOV
129
      if (ret != TSDB_CODE_SUCCESS) {
×
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

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

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

146
    // this task may has no checkpoint, but others tasks may generate checkpoint already?
UNCOV
147
    streamMetaReleaseTask(pMeta, pTask);
×
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.
UNCOV
152
  stInfo("vgId:%d start all task(s) completed", pMeta->vgId);
×
UNCOV
153
  taosArrayDestroy(pTaskList);
×
UNCOV
154
  return code;
×
155
}
156

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

UNCOV
160
  if (pMeta->closeFlag) {
×
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

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

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

UNCOV
175
  int32_t code = streamMetaResetTaskStatus(pMeta);
×
UNCOV
176
  streamMetaWUnLock(pMeta);
×
177

UNCOV
178
  return code;
×
179
}
180

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

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

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

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

UNCOV
209
  streamMetaReleaseTask(pMeta, p);
×
210

UNCOV
211
  if (pStartInfo->startAllTasks != 1) {
×
UNCOV
212
    int64_t el = endTs - startTs;
×
UNCOV
213
    stDebug(
×
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);
UNCOV
217
    streamMetaWUnLock(pMeta);
×
UNCOV
218
    return 0;
×
219
  }
220

UNCOV
221
  STaskInitTs initTs = {.start = startTs, .end = endTs, .success = ready};
×
UNCOV
222
  SHashObj*   pDst = ready ? pStartInfo->pReadyTaskSet : pStartInfo->pFailedTaskSet;
×
UNCOV
223
  code = taosHashPut(pDst, &id, sizeof(id), &initTs, sizeof(STaskInitTs));
×
UNCOV
224
  if (code) {
×
225
    if (code == TSDB_CODE_DUP_KEY) {
×
226
      stError("vgId:%d record start task result failed, s-task:0x%" PRIx64
×
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);
×
233
    return code;
×
234
  }
235

UNCOV
236
  int32_t numOfTotal = streamMetaGetNumOfTasks(pMeta);
×
UNCOV
237
  int32_t numOfRecv = taosHashGetSize(pStartInfo->pReadyTaskSet) + taosHashGetSize(pStartInfo->pFailedTaskSet);
×
238

UNCOV
239
  allRsp = allCheckDownstreamRsp(pMeta, pStartInfo, numOfTotal);
×
UNCOV
240
  if (allRsp) {
×
UNCOV
241
    pStartInfo->readyTs = taosGetTimestampMs();
×
UNCOV
242
    pStartInfo->elapsedTime = (pStartInfo->startTs != 0) ? pStartInfo->readyTs - pStartInfo->startTs : 0;
×
243

UNCOV
244
    stDebug("vgId:%d all %d task(s) check downstream completed, last completed task:0x%x (succ:%d) startTs:%" PRId64
×
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
UNCOV
250
    displayStatusInfo(pMeta, pStartInfo->pReadyTaskSet, true);
×
UNCOV
251
    displayStatusInfo(pMeta, pStartInfo->pFailedTaskSet, false);
×
UNCOV
252
    streamMetaResetStartInfo(pStartInfo, vgId);
×
UNCOV
253
    streamMetaWUnLock(pMeta);
×
254

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

UNCOV
262
  return code;
×
263
}
264

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

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

UNCOV
283
  return true;
×
284
}
285

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

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

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

UNCOV
309
int32_t streamMetaInitStartInfo(STaskStartInfo* pStartInfo) {
×
UNCOV
310
  _hash_fn_t fp = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
×
311

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

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

UNCOV
322
  return 0;
×
323
}
324

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

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

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

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

UNCOV
352
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
353
  }
354

355
  // fill-history task can only be launched by related stream tasks.
UNCOV
356
  STaskExecStatisInfo* pInfo = &pTask->execInfo;
×
UNCOV
357
  if (pTask->info.fillHistory == 1) {
×
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.
UNCOV
365
  streamMutexLock(&pTask->lock);
×
366

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

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

UNCOV
381
  if(pTask->status.downstreamReady != 0) {
×
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.
UNCOV
388
  streamMutexLock(&pTask->lock);
×
UNCOV
389
  if (pTask->pBackend == NULL) {
×
UNCOV
390
    code = pMeta->expandTaskFn(pTask);
×
UNCOV
391
    streamMutexUnlock(&pTask->lock);
×
392

UNCOV
393
    if (code != TSDB_CODE_SUCCESS) {
×
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.
UNCOV
401
  if (code == TSDB_CODE_SUCCESS) {
×
UNCOV
402
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_INIT);
×
UNCOV
403
    if (code != TSDB_CODE_SUCCESS) {
×
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

UNCOV
414
  streamMetaReleaseTask(pMeta, pTask);
×
UNCOV
415
  return code;
×
416
}
417

UNCOV
418
int32_t streamMetaStopAllTasks(SStreamMeta* pMeta) {
×
UNCOV
419
  streamMetaRLock(pMeta);
×
420

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

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

UNCOV
431
  int64_t st = taosGetTimestampMs();
×
432

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

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

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

UNCOV
450
    int32_t ret = streamTaskStop(pTask);
×
UNCOV
451
    if (ret) {
×
452
      stError("s-task:0x%x failed to stop task, code:%s", pTaskId->taskId, tstrerror(ret));
×
453
    }
454

UNCOV
455
    streamMetaReleaseTask(pMeta, pTask);
×
456
  }
457

UNCOV
458
  taosArrayDestroy(pTaskList);
×
459

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

UNCOV
463
  streamMetaRUnLock(pMeta);
×
UNCOV
464
  return code;
×
465
}
466

UNCOV
467
int32_t streamTaskCheckIfReqConsenChkptId(SStreamTask* pTask, int64_t ts) {
×
UNCOV
468
  SConsenChkptInfo* pConChkptInfo = &pTask->status.consenChkptInfo;
×
469

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

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

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

UNCOV
492
  return 0;
×
493
}
494

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

UNCOV
501
  stInfo("s-task:%s set recv consen-checkpointId, transId:%d", pTask->id.idStr, transId);
×
UNCOV
502
}
×
503

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

UNCOV
508
  pInfo->status = TASK_CONSEN_CHKPT_REQ;
×
UNCOV
509
  pInfo->statusTs = ts;
×
UNCOV
510
  pInfo->consenChkptTransId = 0;
×
511

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

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

UNCOV
524
  stDebug("vgId:%d add start failed task:0x%x", pMeta->vgId, taskId);
×
525

UNCOV
526
  streamMetaRLock(pMeta);
×
527

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

535
    streamMetaRUnLock(pMeta);
×
536

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

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

UNCOV
550
  return code;
×
551
}
552

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

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