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

taosdata / TDengine / #3630

06 Mar 2025 11:35AM UTC coverage: 63.629% (-0.06%) from 63.692%
#3630

push

travis-ci

web-flow
Merge pull request #30042 from taosdata/doc/internal

docs: format

149060 of 300532 branches covered (49.6%)

Branch coverage included in aggregate %.

233739 of 301077 relevant lines covered (77.63%)

17473135.72 hits per line

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

66.44
/source/libs/stream/src/streamCheckpoint.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 "rsync.h"
17
#include "streamBackendRocksdb.h"
18
#include "streamInt.h"
19
#include "tcs.h"
20

21
static int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName);
22
static int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId);
23
#ifdef BUILD_NO_CALL
24
static int32_t deleteCheckpoint(const char* id);
25
#endif
26
static int32_t continueDispatchCheckpointTriggerBlock(SStreamDataBlock* pBlock, SStreamTask* pTask);
27
static int32_t appendCheckpointIntoInputQ(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId,
28
                                          int32_t transId, int32_t srcTaskId);
29
static int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList);
30
static void    checkpointTriggerMonitorFn(void* param, void* tmrId);
31

32
int32_t createChkptTriggerBlock(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId, int32_t transId,
6,369✔
33
                                int32_t srcTaskId, SStreamDataBlock** pRes) {
34
  SStreamDataBlock* pChkpoint = NULL;
6,369✔
35
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pChkpoint);
6,369✔
36
  if (code) {
6,382!
37
    return code;
×
38
  }
39

40
  pChkpoint->type = checkpointType;
6,382✔
41
  if (checkpointType == STREAM_INPUT__CHECKPOINT_TRIGGER && (pTask->info.taskLevel != TASK_LEVEL__SOURCE)) {
6,382!
42
    pChkpoint->srcTaskId = srcTaskId;
×
43
    if (srcTaskId <= 0) {
×
44
      stDebug("s-task:%s invalid src task id:%d for creating checkpoint trigger block", pTask->id.idStr, srcTaskId);
×
45
      return TSDB_CODE_INVALID_PARA;
×
46
    }
47
  }
48

49
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
6,382!
50
  if (pBlock == NULL) {
6,379!
51
    taosFreeQitem(pChkpoint);
×
52
    return terrno;
×
53
  }
54

55
  pBlock->info.type = STREAM_CHECKPOINT;
6,379✔
56
  pBlock->info.version = checkpointId;
6,379✔
57
  pBlock->info.window.ekey = pBlock->info.window.skey = transId;  // NOTE: set the transId
6,379✔
58
  pBlock->info.rows = 1;
6,379✔
59
  pBlock->info.childId = pTask->info.selfChildId;
6,379✔
60

61
  pChkpoint->blocks = taosArrayInit(4, sizeof(SSDataBlock));  // pBlock;
6,379✔
62
  if (pChkpoint->blocks == NULL) {
6,383!
63
    taosMemoryFree(pBlock);
×
64
    taosFreeQitem(pChkpoint);
×
65
    return terrno;
×
66
  }
67

68
  void* p = taosArrayPush(pChkpoint->blocks, pBlock);
6,383✔
69
  if (p == NULL) {
6,384!
70
    taosArrayDestroy(pChkpoint->blocks);
×
71
    taosMemoryFree(pBlock);
×
72
    taosFreeQitem(pChkpoint);
×
73
    return terrno;
×
74
  }
75

76
  *pRes = pChkpoint;
6,384✔
77

78
  taosMemoryFree(pBlock);
6,384!
79
  return TSDB_CODE_SUCCESS;
6,386✔
80
}
81

82
// this message must be put into inputq successfully, continue retrying until it succeeds
83
// todo must be success
84
int32_t appendCheckpointIntoInputQ(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId, int32_t transId,
6,371✔
85
                                   int32_t srcTaskId) {
86
  SStreamDataBlock* pCheckpoint = NULL;
6,371✔
87
  int32_t code = createChkptTriggerBlock(pTask, checkpointType, checkpointId, transId, srcTaskId, &pCheckpoint);
6,371✔
88
  if (code != TSDB_CODE_SUCCESS) {
6,387!
89
    return code;
×
90
  }
91

92
  if (streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pCheckpoint) < 0) {
6,387!
93
    return TSDB_CODE_OUT_OF_MEMORY;
×
94
  }
95

96
  return streamTrySchedExec(pTask);
6,380✔
97
}
98

99
int32_t streamProcessCheckpointSourceReq(SStreamTask* pTask, SStreamCheckpointSourceReq* pReq) {
3,095✔
100
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
3,095!
101
    return TSDB_CODE_INVALID_MSG;
×
102
  }
103

104
  // todo this status may not be set here.
105
  // 1. set task status to be prepared for check point, no data are allowed to put into inputQ.
106
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_GEN_CHECKPOINT);
3,095✔
107
  if (code != TSDB_CODE_SUCCESS) {
3,085!
108
    stError("s-task:%s failed to handle gen-checkpoint event, failed to start checkpoint procedure", pTask->id.idStr);
×
109
    return code;
×
110
  }
111

112
  pTask->chkInfo.pActiveInfo->transId = pReq->transId;
3,085✔
113
  pTask->chkInfo.pActiveInfo->activeId = pReq->checkpointId;
3,085✔
114
  pTask->chkInfo.startTs = taosGetTimestampMs();
3,077✔
115
  pTask->execInfo.checkpoint += 1;
3,077✔
116

117
  // 2. Put the checkpoint block into inputQ, to make sure all blocks with less version have been handled by this task
118
  // and this is the last item in the inputQ.
119
  return appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT_TRIGGER, pReq->checkpointId, pReq->transId, -1);
3,077✔
120
}
121

122
int32_t streamTaskProcessCheckpointTriggerRsp(SStreamTask* pTask, SCheckpointTriggerRsp* pRsp) {
2✔
123
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
2✔
124
  bool                   unQualified = false;
2✔
125
  const char*            id = pTask->id.idStr;
2✔
126

127
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2!
128
    stError("s-task:%s invalid msg recv, checkpoint-trigger rsp not handled", id);
×
129
    return TSDB_CODE_INVALID_MSG;
×
130
  }
131

132
  if (pRsp->rspCode != TSDB_CODE_SUCCESS) {
2✔
133
    stDebug("s-task:%s retrieve checkpoint-trgger rsp from upstream:0x%x invalid, code:%s", id, pRsp->upstreamTaskId,
1!
134
            tstrerror(pRsp->rspCode));
135
    return TSDB_CODE_SUCCESS;
1✔
136
  }
137

138
  streamMutexLock(&pTask->lock);
1✔
139
  SStreamTaskState status = streamTaskGetStatus(pTask);
1✔
140
  streamMutexUnlock(&pTask->lock);
1✔
141

142
  if (status.state != TASK_STATUS__CK) {
1!
143
    stError("s-task:%s status:%s not in checkpoint status, discard the checkpoint-trigger msg", id, status.name);
×
144
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
145
  }
146

147
  streamMutexLock(&pInfo->lock);
1✔
148
  unQualified = (pInfo->activeId != pRsp->checkpointId || pInfo->transId != pRsp->transId);
1!
149
  streamMutexUnlock(&pInfo->lock);
1✔
150

151
  if (unQualified) {
1!
152
    stError("s-task:%s status:%s not in checkpoint status, discard the checkpoint-trigger msg", id, status.name);
1!
153
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
1✔
154
  }
155

156
  // NOTE: here we do not do the duplicated checkpoint-trigger msg check, since it will be done by following functions.
157
  int32_t code = appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT_TRIGGER, pRsp->checkpointId, pRsp->transId,
×
158
                                            pRsp->upstreamTaskId);
159
  return code;
×
160
}
161

162
int32_t streamTaskSendCheckpointTriggerMsg(SStreamTask* pTask, int32_t dstTaskId, int32_t downstreamNodeId,
1✔
163
                                           SRpcHandleInfo* pRpcInfo, int32_t code) {
164
  int32_t  ret = 0;
1✔
165
  int32_t  tlen = 0;
1✔
166
  void*    buf = NULL;
1✔
167
  SEncoder encoder;
168

169
  SCheckpointTriggerRsp req = {.streamId = pTask->id.streamId,
1✔
170
                               .upstreamTaskId = pTask->id.taskId,
1✔
171
                               .taskId = dstTaskId,
172
                               .rspCode = code};
173

174
  if (code == TSDB_CODE_SUCCESS) {
1!
175
    req.checkpointId = pTask->chkInfo.pActiveInfo->activeId;
1✔
176
    req.transId = pTask->chkInfo.pActiveInfo->transId;
1✔
177
  } else {
178
    req.checkpointId = -1;
×
179
    req.transId = -1;
×
180
  }
181

182
  tEncodeSize(tEncodeCheckpointTriggerRsp, &req, tlen, ret);
1!
183
  if (ret < 0) {
1!
184
    stError("s-task:%s encode checkpoint-trigger rsp msg failed, code:%s", pTask->id.idStr, tstrerror(code));
×
185
    return ret;
×
186
  }
187

188
  buf = rpcMallocCont(tlen + sizeof(SMsgHead));
1✔
189
  if (buf == NULL) {
1!
190
    stError("s-task:%s malloc chkpt-trigger rsp failed for task:0x%x, since out of memory", pTask->id.idStr, dstTaskId);
×
191
    return terrno;
×
192
  }
193

194
  ((SMsgHead*)buf)->vgId = htonl(downstreamNodeId);
1✔
195
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
1✔
196

197
  tEncoderInit(&encoder, abuf, tlen);
1✔
198
  if ((ret = tEncodeCheckpointTriggerRsp(&encoder, &req)) < 0) {
1!
199
    rpcFreeCont(buf);
×
200
    tEncoderClear(&encoder);
×
201
    stError("encode checkpoint-trigger rsp failed, code:%s", tstrerror(code));
×
202
    return ret;
×
203
  }
204
  tEncoderClear(&encoder);
1✔
205

206
  SRpcMsg rspMsg = {.code = 0, .pCont = buf, .contLen = tlen + sizeof(SMsgHead), .info = *pRpcInfo};
1✔
207
  tmsgSendRsp(&rspMsg);
1✔
208

209
  return ret;
1✔
210
}
211

212
int32_t continueDispatchCheckpointTriggerBlock(SStreamDataBlock* pBlock, SStreamTask* pTask) {
3,139✔
213
  pBlock->srcTaskId = pTask->id.taskId;
3,139✔
214
  pBlock->srcVgId = pTask->pMeta->vgId;
3,139✔
215

216
  if (pTask->chkInfo.pActiveInfo->dispatchTrigger == true) {
3,139!
217
    stError("s-task:%s already dispatch checkpoint-trigger, not dispatch again", pTask->id.idStr);
×
218
    return 0;
×
219
  }
220

221
  int32_t code = taosWriteQitem(pTask->outputq.queue->pQueue, pBlock);
3,139✔
222
  if (code == 0) {
3,136!
223
    code = streamDispatchStreamBlock(pTask);
3,136✔
224
  } else {
225
    stError("s-task:%s failed to put checkpoint into outputQ, code:%s", pTask->id.idStr, tstrerror(code));
×
226
    streamFreeQitem((SStreamQueueItem*)pBlock);
×
227
  }
228

229
  return code;
3,141✔
230
}
231

232
int32_t doCheckBeforeHandleChkptTrigger(SStreamTask* pTask, int64_t checkpointId, SStreamDataBlock* pBlock,
11,296✔
233
                                        int32_t transId) {
234
  int32_t     code = 0;
11,296✔
235
  int32_t     vgId = pTask->pMeta->vgId;
11,296✔
236
  int32_t     taskLevel = pTask->info.taskLevel;
11,296✔
237
  const char* id = pTask->id.idStr;
11,296✔
238

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
11,296✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
11,296✔
241
    stError("s-task:%s vgId:%d current checkpointId:%" PRId64
1!
242
            " recv expired checkpoint-trigger block, checkpointId:%" PRId64 " transId:%d, discard",
243
            id, vgId, pTask->chkInfo.checkpointId, checkpointId, transId);
244
    return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
245
  }
246

247
  if (pActiveInfo->failedId >= checkpointId) {
11,295✔
248
    stError("s-task:%s vgId:%d checkpointId:%" PRId64 " transId:%d, has been marked failed, failedId:%" PRId64
1!
249
            " discard the checkpoint-trigger block",
250
            id, vgId, checkpointId, transId, pActiveInfo->failedId);
251
    return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
252
  }
253

254
  if (pTask->chkInfo.checkpointId == checkpointId) {
11,294✔
255
    {  // send checkpoint-ready msg to upstream
256
      SRpcMsg                msg = {0};
1✔
257
      SStreamUpstreamEpInfo* pInfo = NULL;
1✔
258
      streamTaskGetUpstreamTaskEpInfo(pTask, pBlock->srcTaskId, &pInfo);
1✔
259
      if (pInfo == NULL) {
1!
260
        return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
261
      }
262

263
      code = initCheckpointReadyMsg(pTask, pInfo->nodeId, pBlock->srcTaskId, pInfo->childId, checkpointId, &msg);
1✔
264
      if (code == TSDB_CODE_SUCCESS) {
1!
265
        code = tmsgSendReq(&pInfo->epSet, &msg);
1✔
266
        if (code) {
1!
267
          stError("s-task:%s vgId:%d failed send chkpt-ready msg to upstream, code:%s", id, vgId, tstrerror(code));
×
268
        }
269
      }
270
    }
271

272
    stWarn(
1!
273
        "s-task:%s vgId:%d recv already finished checkpoint-trigger, send checkpoint-ready to upstream:0x%x to resume "
274
        "the interrupted checkpoint",
275
        id, vgId, pBlock->srcTaskId);
276

277
    return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
278
  }
279

280
  if (streamTaskGetStatus(pTask).state == TASK_STATUS__CK) {
11,293✔
281
    if (pActiveInfo->activeId != checkpointId) {
8,184✔
282
      stError("s-task:%s vgId:%d active checkpointId:%" PRId64 ", recv invalid checkpoint-trigger checkpointId:%" PRId64
1!
283
              " discard",
284
              id, vgId, pActiveInfo->activeId, checkpointId);
285
      return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
286
    } else {  // checkpointId == pActiveInfo->activeId
287
      if (pActiveInfo->allUpstreamTriggerRecv == 1) {
8,183✔
288
        stDebug(
1!
289
            "s-task:%s vgId:%d all upstream checkpoint-trigger recv, discard this checkpoint-trigger, "
290
            "checkpointId:%" PRId64 " transId:%d",
291
            id, vgId, checkpointId, transId);
292
        return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
293
      }
294

295
      if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
8,182✔
296
        //  check if already recv or not, and duplicated checkpoint-trigger msg recv, discard it
297
        for (int32_t i = 0; i < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++i) {
13,538✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,458✔
299
          if (p == NULL) {
8,461!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

303
          if (p->upstreamTaskId == pBlock->srcTaskId) {
8,461✔
304
            stWarn("s-task:%s repeatly recv checkpoint-trigger msg from task:0x%x vgId:%d, checkpointId:%" PRId64
1!
305
                   ", prev recvTs:%" PRId64 " discard",
306
                   pTask->id.idStr, p->upstreamTaskId, p->upstreamNodeId, p->checkpointId, p->recvTs);
307
            return TSDB_CODE_STREAM_INVLD_CHKPT;
1✔
308
          }
309
        }
310
      }
311
    }
312
  }
313

314
  return TSDB_CODE_SUCCESS;
11,285✔
315
}
316

317
int32_t streamProcessCheckpointTriggerBlock(SStreamTask* pTask, SStreamDataBlock* pBlock) {
11,286✔
318
  int64_t                checkpointId = 0;
11,286✔
319
  int32_t                transId = 0;
11,286✔
320
  const char*            id = pTask->id.idStr;
11,286✔
321
  int32_t                code = TSDB_CODE_SUCCESS;
11,286✔
322
  int32_t                vgId = pTask->pMeta->vgId;
11,286✔
323
  int32_t                taskLevel = pTask->info.taskLevel;
11,286✔
324
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
11,286✔
325

326
  SSDataBlock* pDataBlock = taosArrayGet(pBlock->blocks, 0);
11,286✔
327
  if (pDataBlock == NULL) {
11,291!
328
    return TSDB_CODE_INVALID_PARA;
×
329
  }
330

331
  checkpointId = pDataBlock->info.version;
11,291✔
332
  transId = pDataBlock->info.window.skey;
11,291✔
333

334
  streamMutexLock(&pTask->lock);
11,291✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
11,296✔
336
  streamMutexUnlock(&pTask->lock);
11,284✔
337
  if (code) {
11,295!
338
    if (taskLevel != TASK_LEVEL__SOURCE) { // the checkpoint-trigger is discard, open the inputQ for upstream tasks
×
339
      streamTaskOpenUpstreamInput(pTask, pBlock->srcTaskId);
×
340
    }
341
    streamFreeQitem((SStreamQueueItem*)pBlock);
×
342
    return code;
×
343
  }
344

345
  stDebug("s-task:%s vgId:%d start to handle the checkpoint-trigger block, checkpointId:%" PRId64 " ver:%" PRId64
11,295✔
346
          ", transId:%d current active checkpointId:%" PRId64,
347
          id, vgId, pTask->chkInfo.checkpointId, pTask->chkInfo.checkpointVer, transId, checkpointId);
348

349
  // set task status
350
  if (streamTaskGetStatus(pTask).state != TASK_STATUS__CK) {
11,295✔
351
    pActiveInfo->activeId = checkpointId;
3,111✔
352
    pActiveInfo->transId = transId;
3,111✔
353

354
    if (pTask->chkInfo.startTs == 0) {
3,111!
355
      pTask->chkInfo.startTs = taosGetTimestampMs();
3,111✔
356
      pTask->execInfo.checkpoint += 1;
3,111✔
357
    }
358

359
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_GEN_CHECKPOINT);
3,111✔
360
    if (code != TSDB_CODE_SUCCESS) {
3,112!
361
      stError("s-task:%s handle checkpoint-trigger block failed, code:%s", id, tstrerror(code));
×
362
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
363
      return code;
×
364
    }
365

366
    // if previous launched timer not started yet, not start a new timer
367
    // todo: fix this bug: previous set checkpoint-trigger check tmr is running, while we happen to try to launch
368
    //  a new checkpoint-trigger timer right now.
369
    //  And if we don't start a new timer, and the lost of checkpoint-trigger message may cause the whole checkpoint
370
    //  procedure to be stucked.
371
    SStreamTmrInfo* pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
3,112✔
372
    int8_t          old = atomic_val_compare_exchange_8(&pTmrInfo->isActive, 0, 1);
3,112✔
373
    if (old == 0) {
3,112!
374
      stDebug("s-task:%s start checkpoint-trigger monitor in 10s", pTask->id.idStr);
3,112✔
375

376
      int64_t* pTaskRefId = NULL;
3,112✔
377
      code = streamTaskAllocRefId(pTask, &pTaskRefId);
3,112✔
378
      if (code == 0) {
3,112!
379
        streamTmrStart(checkpointTriggerMonitorFn, 200, pTaskRefId, streamTimer, &pTmrInfo->tmrHandle, vgId,
3,112✔
380
                       "trigger-recv-monitor");
381
        pTmrInfo->launchChkptId = pActiveInfo->activeId;
3,112✔
382
      }
383
    } else {  // already launched, do nothing
384
      stError("s-task:%s previous checkpoint-trigger monitor tmr is set, not start new one", pTask->id.idStr);
×
385
    }
386
  }
387

388
#if 0
389
  taosMsleep(20*1000);
390
#endif
391

392
  if (taskLevel == TASK_LEVEL__SOURCE) {
11,281✔
393
    int8_t type = pTask->outputInfo.type;
3,093✔
394
    pActiveInfo->allUpstreamTriggerRecv = 1;
3,093✔
395

396
    // We need to transfer state here, before dispatching checkpoint-trigger to downstream tasks.
397
    // The transfer of state may generate new data that need to dispatch to downstream tasks,
398
    // Otherwise, those new generated data by executors that is kept in outputQ, may be lost if this program crashed
399
    // before the next checkpoint.
400
    code = flushStateDataInExecutor(pTask, (SStreamQueueItem*)pBlock);
3,093✔
401
    if (code) {
3,102!
402
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
403
      return code;
×
404
    }
405

406
#if 0
407
    chkptFailedByRetrieveReqToSource(pTask, checkpointId);
408
#endif
409

410
    if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
3,102✔
411
      stDebug("s-task:%s set childIdx:%d, and add checkpoint-trigger block into outputQ", id, pTask->info.selfChildId);
2,895✔
412
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);  // todo handle this failure
2,895✔
413
    } else {  // only one task exists, no need to dispatch downstream info
414
      code =
415
          appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT, pActiveInfo->activeId, pActiveInfo->transId, -1);
207✔
416
      streamFreeQitem((SStreamQueueItem*)pBlock);
207✔
417
    }
418
  } else if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
8,188!
419
    // todo: handle this
420
    // update the child Id for downstream tasks
421
    code = streamAddCheckpointReadyMsg(pTask, pBlock->srcTaskId, pTask->info.selfChildId, checkpointId);
8,188✔
422

423
    // there are still some upstream tasks not send checkpoint request, do nothing and wait for then
424
    if (pActiveInfo->allUpstreamTriggerRecv != 1) {
8,195✔
425
      streamFreeQitem((SStreamQueueItem*)pBlock);
5,122✔
426
      return code;
5,122✔
427
    }
428

429
    int32_t num = streamTaskGetNumOfUpstream(pTask);
3,073✔
430
    if (taskLevel == TASK_LEVEL__SINK) {
3,074✔
431
      stDebug("s-task:%s process checkpoint-trigger block, all %d upstreams sent, send ready msg to upstream", id, num);
2,830✔
432
      streamFreeQitem((SStreamQueueItem*)pBlock);
2,830✔
433
      code = streamTaskBuildCheckpoint(pTask);  // todo: not handle error yet
2,831✔
434
    } else {                                    // source & agg tasks need to forward the checkpoint msg downwards
435
      stDebug("s-task:%s process checkpoint-trigger block, all %d upstreams sent, forwards to downstream", id, num);
244✔
436
      code = flushStateDataInExecutor(pTask, (SStreamQueueItem*)pBlock);
244✔
437
      if (code) {
244!
438
        return code;
×
439
      }
440

441
      // Put the checkpoint-trigger block into outputQ, to make sure all blocks with less version have been handled by
442
      // this task already. And then, dispatch check point msg to all downstream tasks
443
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);
244✔
444
    }
445
  }
446

447
  return code;
6,179✔
448
}
449

450
// only when all downstream tasks are send checkpoint rsp, we can start the checkpoint procedure for the agg task
451
static int32_t processCheckpointReadyHelp(SActiveCheckpointInfo* pInfo, int32_t numOfDownstream,
8,154✔
452
                                          int32_t downstreamNodeId, int64_t streamId, int32_t downstreamTaskId,
453
                                          const char* id, int32_t* pNotReady, int32_t* pTransId, bool* alreadyRecv) {
454
  *alreadyRecv = false;
8,154✔
455
  int32_t size = taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
8,154✔
456
  for (int32_t i = 0; i < size; ++i) {
16,600✔
457
    STaskDownstreamReadyInfo* p = taosArrayGet(pInfo->pCheckpointReadyRecvList, i);
8,446✔
458
    if (p == NULL) {
8,446!
459
      return TSDB_CODE_INVALID_PARA;
×
460
    }
461

462
    if (p->downstreamTaskId == downstreamTaskId) {
8,446!
463
      (*alreadyRecv) = true;
×
464
      break;
×
465
    }
466
  }
467

468
  if (*alreadyRecv) {
8,154!
469
    stDebug("s-task:%s already recv checkpoint-ready msg from downstream:0x%x, ignore. %d/%d downstream not ready", id,
×
470
            downstreamTaskId, (int32_t)(numOfDownstream - taosArrayGetSize(pInfo->pCheckpointReadyRecvList)),
471
            numOfDownstream);
472
  } else {
473
    STaskDownstreamReadyInfo info = {.recvTs = taosGetTimestampMs(),
8,154✔
474
                                     .downstreamTaskId = downstreamTaskId,
475
                                     .checkpointId = pInfo->activeId,
8,154✔
476
                                     .transId = pInfo->transId,
8,154✔
477
                                     .streamId = streamId,
478
                                     .downstreamNodeId = downstreamNodeId};
479
    void*                    p = taosArrayPush(pInfo->pCheckpointReadyRecvList, &info);
8,154✔
480
    if (p == NULL) {
8,154!
481
      stError("s-task:%s failed to set checkpoint ready recv msg, code:%s", id, tstrerror(terrno));
×
482
      return terrno;
×
483
    }
484
  }
485

486
  *pNotReady = numOfDownstream - taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
8,154✔
487
  *pTransId = pInfo->transId;
8,154✔
488
  return 0;
8,154✔
489
}
490

491
/**
492
 * All down stream tasks have successfully completed the check point task.
493
 * Current stream task is allowed to start to do checkpoint things in ASYNC model.
494
 */
495
int32_t streamProcessCheckpointReadyMsg(SStreamTask* pTask, int64_t checkpointId, int32_t downstreamNodeId,
8,153✔
496
                                        int32_t downstreamTaskId) {
497
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,153✔
498

499
  const char* id = pTask->id.idStr;
8,153✔
500
  int32_t     total = streamTaskGetNumOfDownstream(pTask);
8,153✔
501
  int32_t     code = 0;
8,153✔
502
  int32_t     notReady = 0;
8,153✔
503
  int32_t     transId = 0;
8,153✔
504
  bool        alreadyHandled = false;
8,153✔
505

506
  // 1. not in checkpoint status now
507
  SStreamTaskState pStat = streamTaskGetStatus(pTask);
8,153✔
508
  if (pStat.state != TASK_STATUS__CK) {
8,153!
509
    stError("s-task:%s status:%s discard checkpoint-ready msg from task:0x%x", id, pStat.name, downstreamTaskId);
×
510
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
511
  }
512

513
  // 2. expired checkpoint-ready msg, invalid checkpoint-ready msg
514
  if (pTask->chkInfo.checkpointId > checkpointId || pInfo->activeId != checkpointId) {
8,153!
515
    stError("s-task:%s status:%s checkpointId:%" PRId64 " new arrival checkpoint-ready msg (checkpointId:%" PRId64
×
516
            ") from task:0x%x, expired and discard",
517
            id, pStat.name, pTask->chkInfo.checkpointId, checkpointId, downstreamTaskId);
518
    return TSDB_CODE_INVALID_MSG;
×
519
  }
520

521
  streamMutexLock(&pInfo->lock);
8,153✔
522
  code = processCheckpointReadyHelp(pInfo, total, downstreamNodeId, pTask->id.streamId, downstreamTaskId, id, &notReady,
8,154✔
523
                                    &transId, &alreadyHandled);
524
  streamMutexUnlock(&pInfo->lock);
8,154✔
525

526
  if (alreadyHandled) {
8,154!
527
    stDebug("s-task:%s checkpoint-ready msg checkpointId:%" PRId64 " from task:0x%x already handled, not handle again",
×
528
            id, checkpointId, downstreamTaskId);
529
  } else {
530
    if ((notReady == 0) && (code == 0) && (!alreadyHandled)) {
8,154!
531
      stDebug("s-task:%s all downstream tasks have completed build checkpoint, do checkpoint for current task", id);
3,078✔
532
      code = appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT, checkpointId, transId, -1);
3,078✔
533
    }
534
  }
535

536
  return code;
8,154✔
537
}
538

539
int32_t streamTaskProcessCheckpointReadyRsp(SStreamTask* pTask, int32_t upstreamTaskId, int64_t checkpointId) {
8,151✔
540
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,151✔
541
  int64_t                now = taosGetTimestampMs();
8,152✔
542
  int32_t                numOfConfirmed = 0;
8,152✔
543

544
  streamMutexLock(&pInfo->lock);
8,152✔
545
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
16,614!
546
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
16,614✔
547
    if (pReadyInfo == NULL) {
16,614!
548
      stError("s-task:%s invalid index during iterate the checkpoint-ready msg list, index:%d, ignore and continue",
×
549
              pTask->id.idStr, i);
550
      continue;
×
551
    }
552

553
    if (pReadyInfo->upstreamTaskId == upstreamTaskId && pReadyInfo->checkpointId == checkpointId) {
16,614!
554
      pReadyInfo->sendCompleted = 1;
8,152✔
555
      stDebug("s-task:%s send checkpoint-ready msg to upstream:0x%x confirmed, checkpointId:%" PRId64 " ts:%" PRId64,
8,152✔
556
              pTask->id.idStr, upstreamTaskId, checkpointId, now);
557
      break;
8,152✔
558
    }
559
  }
560

561
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
33,228✔
562
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
25,076✔
563
    if (pReadyInfo == NULL) {
25,076!
564
      stError("s-task:%s invalid index during iterate the checkpoint-ready msg list, index:%d, ignore and continue",
×
565
              pTask->id.idStr, i);
566
      continue;
×
567
    }
568

569
    if (pReadyInfo->sendCompleted == 1) {
25,076✔
570
      numOfConfirmed += 1;
16,614✔
571
    }
572
  }
573

574
  stDebug("s-task:%s send checkpoint-ready msg to %d upstream confirmed, checkpointId:%" PRId64, pTask->id.idStr,
8,152✔
575
          numOfConfirmed, checkpointId);
576

577
  streamMutexUnlock(&pInfo->lock);
8,152✔
578
  return TSDB_CODE_SUCCESS;
8,152✔
579
}
580

581
void streamTaskClearCheckInfo(SStreamTask* pTask, bool clearChkpReadyMsg) {
5,302✔
582
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
5,302✔
583

584
  pTask->chkInfo.startTs = 0;             // clear the recorded start time
5,302✔
585
  streamTaskOpenAllUpstreamInput(pTask);  // open inputQ for all upstream tasks
5,302✔
586

587
  streamMutexLock(&pInfo->lock);
5,300✔
588
  streamTaskClearActiveInfo(pInfo);
5,332✔
589
  if (clearChkpReadyMsg) {
5,310✔
590
    streamClearChkptReadyMsg(pInfo);
5,309✔
591
  }
592
  streamMutexUnlock(&pInfo->lock);
5,314✔
593

594
  stDebug("s-task:%s clear active checkpointInfo, failed checkpointId:%" PRId64 ", latest checkpointId:%" PRId64,
5,331✔
595
          pTask->id.idStr, pInfo->failedId, pTask->chkInfo.checkpointId);
596
}
5,331✔
597

598
// The checkpointInfo can be updated in the following three cases:
599
// 1. follower tasks; 2. leader task with status of TASK_STATUS__CK; 3. restore not completed
600
static int32_t doUpdateCheckpointInfoCheck(SStreamTask* pTask, bool restored, SVUpdateCheckpointInfoReq* pReq,
5,427✔
601
                                           bool* pContinue) {
602
  SStreamMeta*     pMeta = pTask->pMeta;
5,427✔
603
  int32_t          vgId = pMeta->vgId;
5,427✔
604
  int32_t          code = 0;
5,427✔
605
  const char*      id = pTask->id.idStr;
5,427✔
606
  SCheckpointInfo* pInfo = &pTask->chkInfo;
5,427✔
607

608
  *pContinue = true;
5,427✔
609

610
  // not update the checkpoint info if the checkpointId is less than the failed checkpointId
611
  if (pReq->checkpointId < pInfo->pActiveInfo->failedId) {
5,427!
612
    stWarn("s-task:%s vgId:%d not update the checkpoint-info, since update checkpointId:%" PRId64
×
613
           " is less than the failed checkpointId:%" PRId64 ", discard",
614
           id, vgId, pReq->checkpointId, pInfo->pActiveInfo->failedId);
615

616
    *pContinue = false;
×
617
    return TSDB_CODE_SUCCESS;
×
618
  }
619

620
  // it's an expired checkpointInfo update msg, we still try to drop the required drop fill-history task.
621
  if (pReq->checkpointId <= pInfo->checkpointId) {
5,427✔
622
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
96!
623
            " no need to update checkpoint info, updated checkpointId:%" PRId64 " Ver:%" PRId64 " transId:%d ignored",
624
            id, vgId, pInfo->checkpointId, pInfo->checkpointVer, pReq->checkpointId, pReq->checkpointVer,
625
            pReq->transId);
626

627
    { // destroy the related fill-history tasks
628
      if (pReq->dropRelHTask) {
96!
629
          code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
×
630

631
          int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
632
          stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped in update checkpointInfo, remain tasks:%d",
×
633
                  id, vgId, pReq->taskId, numOfTasks);
634

635
          //todo: task may not exist, commit anyway, optimize this later
636
          code = streamMetaCommit(pMeta);
×
637
      }
638
    }
639

640
    *pContinue = false;
96✔
641
    // always return true
642
    return TSDB_CODE_SUCCESS;
96✔
643
  }
644

645
  SStreamTaskState status = streamTaskGetStatus(pTask);
5,331✔
646

647
  if (!restored) {  // during restore procedure, do update checkpoint-info
5,327✔
648
    stDebug("s-task:%s vgId:%d status:%s update the checkpoint-info during restore, checkpointId:%" PRId64 "->%" PRId64
6!
649
            " checkpointVer:%" PRId64 "->%" PRId64 " checkpointTs:%" PRId64 "->%" PRId64,
650
            id, vgId, status.name, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer, pReq->checkpointVer,
651
            pInfo->checkpointTime, pReq->checkpointTs);
652
  } else {  // not in restore status, must be in checkpoint status
653
    if (((status.state == TASK_STATUS__CK) && (pMeta->role == NODE_ROLE_LEADER)) ||
5,321!
654
        (pMeta->role == NODE_ROLE_FOLLOWER)) {
13!
655
      stDebug("s-task:%s vgId:%d status:%s role:%d start to update the checkpoint-info, checkpointId:%" PRId64
5,321✔
656
              "->%" PRId64 " checkpointVer:%" PRId64 "->%" PRId64 " checkpointTs:%" PRId64 "->%" PRId64,
657
              id, vgId, status.name, pMeta->role, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer,
658
              pReq->checkpointVer, pInfo->checkpointTime, pReq->checkpointTs);
659
    } else {
660
      stDebug("s-task:%s vgId:%d status:%s NOT update the checkpoint-info, checkpointId:%" PRId64 "->%" PRId64
×
661
              " checkpointVer:%" PRId64 "->%" PRId64,
662
              id, vgId, status.name, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer,
663
              pReq->checkpointVer);
664
    }
665
  }
666

667
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
10,653!
668
                pInfo->processedVer <= pReq->checkpointVer);
5,327✔
669

670
  if (!valid) {
5,326!
671
    // invalid update checkpoint info for leader, since the processedVer is greater than the checkpointVer
672
    // It is possible for follower tasks that the processedVer is greater than the checkpointVer, and the processed info
673
    // in follower tasks will be discarded, since the leader/follower switch happens before the checkpoint of the
674
    // processedVer being generated.
675
    if (pMeta->role == NODE_ROLE_LEADER) {
×
676

677
      stFatal("s-task:%s checkpointId update info recv, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
678
              " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64 " discard it",
679
              id, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
680
              pReq->checkpointVer);
681

682
      *pContinue = false;
×
683
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
684
    } else {
685
      stInfo("s-task:%s vgId:%d follower recv checkpointId update info, current checkpointId:%" PRId64
×
686
             " checkpointVer:%" PRId64 " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64,
687
             id, pMeta->vgId, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
688
             pReq->checkpointVer);
689
    }
690
  }
691

692
  return TSDB_CODE_SUCCESS;
5,319✔
693
}
694

695
int32_t streamTaskUpdateTaskCheckpointInfo(SStreamTask* pTask, bool restored, SVUpdateCheckpointInfoReq* pReq) {
5,427✔
696
  SStreamMeta*     pMeta = pTask->pMeta;
5,427✔
697
  int32_t          vgId = pMeta->vgId;
5,427✔
698
  int32_t          code = 0;
5,427✔
699
  const char*      id = pTask->id.idStr;
5,427✔
700
  SCheckpointInfo* pInfo = &pTask->chkInfo;
5,427✔
701
  bool             continueUpdate = true;
5,427✔
702

703
  streamMutexLock(&pTask->lock);
5,427✔
704
  code = doUpdateCheckpointInfoCheck(pTask, restored, pReq, &continueUpdate);
5,429✔
705

706
  if (!continueUpdate) {
5,415✔
707
    streamMutexUnlock(&pTask->lock);
96✔
708
    return code;
96✔
709
  }
710

711
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
5,319✔
712

713
  // update only it is in checkpoint status, or during restore procedure.
714
  if ((pStatus.state == TASK_STATUS__CK) || (!restored) || (pMeta->role == NODE_ROLE_FOLLOWER)) {
5,318!
715
    pInfo->checkpointId = pReq->checkpointId;
5,318✔
716
    pInfo->checkpointVer = pReq->checkpointVer;
5,318✔
717
    pInfo->checkpointTime = pReq->checkpointTs;
5,318✔
718

719
    if (restored && (pMeta->role == NODE_ROLE_LEADER)) {
5,318✔
720
      code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
5,299✔
721
    }
722
  }
723

724
  streamTaskClearCheckInfo(pTask, true);
5,303✔
725

726
  if (pReq->dropRelHTask) {
5,331✔
727
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
3,438✔
728
            pReq->taskId, vgId, pReq->hTaskId);
729
    CLEAR_RELATED_FILLHISTORY_TASK(pTask);
3,438✔
730
  }
731

732
  stDebug("s-task:0x%x set the persistent status attr to be ready, prev:%s, status in sm:%s", pReq->taskId,
5,331✔
733
          streamTaskGetStatusStr(pTask->status.taskStatus), streamTaskGetStatus(pTask).name);
734

735
  pTask->status.taskStatus = TASK_STATUS__READY;
5,331✔
736

737
  code = streamMetaSaveTaskInMeta(pMeta, pTask);
5,331✔
738
  streamMutexUnlock(&pTask->lock);
5,331✔
739

740
  if (code != TSDB_CODE_SUCCESS) {
5,332!
741
    stError("s-task:%s vgId:%d failed to save task info after do checkpoint, checkpointId:%" PRId64 ", since %s", id,
×
742
            vgId, pReq->checkpointId, terrstr());
743
    return TSDB_CODE_SUCCESS;
×
744
  }
745

746
  // drop task should not in the meta-lock, and drop the related fill-history task now
747
  if (pReq->dropRelHTask) {
5,332✔
748
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
3,438✔
749
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
3,440✔
750
    stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
3,440✔
751
            (int32_t)pReq->hTaskId, numOfTasks);
752
  }
753

754
  code = streamMetaCommit(pMeta);
5,334✔
755
  return TSDB_CODE_SUCCESS;
5,333✔
756
}
757

758
void streamTaskSetFailedCheckpointId(SStreamTask* pTask, int64_t failedId) {
4✔
759
  struct SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
4✔
760

761
  if (failedId <= 0) {
4✔
762
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
2!
763
           " activeId:%" PRId64,
764
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
765
  } else {
766
    if (failedId <= pInfo->failedId) {
2✔
767
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
1!
768
    } else {
769
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
1!
770
              " prev failedId:%" PRId64,
771
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
772
      pInfo->failedId = failedId;
1✔
773
    }
774
  }
775
}
4✔
776

777
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
523✔
778
  streamMutexLock(&pTask->lock);
523✔
779
  ETaskStatus status = streamTaskGetStatus(pTask).state;
524✔
780
  if (status == TASK_STATUS__CK) {
524!
781
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
782
  }
783
  streamMutexUnlock(&pTask->lock);
524✔
784
}
523✔
785

786
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
1✔
787
  int32_t code = 0;
1✔
788
  int32_t cap = strlen(path) + 64;
1✔
789

790
  char* filePath = taosMemoryCalloc(1, cap);
1!
791
  if (filePath == NULL) {
1!
792
    return terrno;
×
793
  }
794

795
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
1✔
796
  if (nBytes <= 0 || nBytes >= cap) {
1!
797
    taosMemoryFree(filePath);
×
798
    return TSDB_CODE_OUT_OF_RANGE;
×
799
  }
800

801
  code = downloadCheckpointDataByName(id, "META", filePath);
1✔
802
  if (code != 0) {
1!
803
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
804
    taosMemoryFree(filePath);
×
805
    return code;
×
806
  }
807

808
  code = remoteChkpGetDelFile(filePath, list);
1✔
809
  if (code != 0) {
1!
810
    stError("%s chkp failed to get to del:%s", id, filePath);
1!
811
    taosMemoryFree(filePath);
1!
812
  }
813
  return 0;
1✔
814
}
815

816
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
3✔
817
  int32_t code = 0;
3✔
818
  char*   path = NULL;
3✔
819

820
  SStreamMeta* pMeta = pTask->pMeta;
3✔
821
  const char*  idStr = pTask->id.idStr;
3✔
822
  int64_t      now = taosGetTimestampMs();
3✔
823

824
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
3✔
825
  if (toDelFiles == NULL) {
3!
826
    return terrno;
×
827
  }
828

829
  if ((code = taskDbGenChkpUploadData(pTask->pBackend, pMeta->bkdChkptMgt, checkpointId, type, &path, toDelFiles,
3✔
830
                                      pTask->id.idStr)) != 0) {
831
    stError("s-task:%s failed to gen upload checkpoint:%" PRId64 ", reason:%s", idStr, checkpointId, tstrerror(code));
1!
832
  }
833

834
  if (type == DATA_UPLOAD_S3) {
3✔
835
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
1!
836
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
837
              tstrerror(code));
838
    }
839
  }
840

841
  if (code == TSDB_CODE_SUCCESS) {
3✔
842
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
2✔
843
    if (code == TSDB_CODE_SUCCESS) {
2✔
844
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
1!
845
    } else {
846
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
1!
847
              tstrerror(code));
848
    }
849
  }
850

851
  if (code == TSDB_CODE_SUCCESS) {
3✔
852
    int32_t size = taosArrayGetSize(toDelFiles);
1✔
853
    stDebug("s-task:%s remove redundant %d files", idStr, size);
1!
854

855
    for (int i = 0; i < size; i++) {
1!
856
      char* pName = taosArrayGetP(toDelFiles, i);
×
857
      code = deleteCheckpointFile(idStr, pName);
×
858
      if (code != 0) {
×
859
        stDebug("s-task:%s failed to remove file: %s", idStr, pName);
×
860
        break;
×
861
      }
862
    }
863

864
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
1!
865
  }
866

867
  taosArrayDestroyP(toDelFiles, NULL);
3✔
868
  double el = (taosGetTimestampMs() - now) / 1000.0;
3✔
869

870
  if (code == TSDB_CODE_SUCCESS) {
3✔
871
    stDebug("s-task:%s complete update checkpointId:%" PRId64 ", elapsed time:%.2fs remove local checkpoint data %s",
1!
872
            idStr, checkpointId, el, path);
873
    taosRemoveDir(path);
1✔
874
  } else {
875
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " keep local checkpoint data, elapsed time:%.2fs", idStr,
2!
876
            checkpointId, el);
877
  }
878

879
  taosMemoryFree(path);
3!
880
  return code;
3✔
881
}
882

883
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
6,129✔
884
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
6,129✔
885
  if (type == DATA_UPLOAD_DISABLE) {
6,126✔
886
    stDebug("s-task:%s not allowed to upload checkpoint data", pTask->id.idStr);
6,125✔
887
    return 0;
6,125✔
888
  }
889

890
  if (pTask == NULL || pTask->pBackend == NULL) {
1!
891
    return 0;
×
892
  }
893

894
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
1✔
895
  void*   pBackend = taskAcquireDb(dbRefId);
1✔
896
  if (pBackend == NULL) {
1!
897
    stError("s-task:%s failed to acquire db during update checkpoint data, failed to upload checkpointData",
×
898
            pTask->id.idStr);
899
    return -1;
×
900
  }
901

902
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
1✔
903
  taskReleaseDb(dbRefId);
1✔
904

905
  return code;
1✔
906
}
907

908
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
6,129✔
909
  int32_t      code = TSDB_CODE_SUCCESS;
6,129✔
910
  int64_t      startTs = pTask->chkInfo.startTs;
6,129✔
911
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
6,129✔
912
  const char*  id = pTask->id.idStr;
6,129✔
913
  SStreamMeta* pMeta = pTask->pMeta;
6,129✔
914

915
  streamMutexLock(&pTask->lock);
6,129✔
916
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
6,126✔
917
  streamMutexUnlock(&pTask->lock);
6,126✔
918

919
  // sink task does not need to save the status, and generated the checkpoint
920
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
6,128✔
921
    stDebug("s-task:%s level:%d start gen checkpoint, checkpointId:%" PRId64, id, pTask->info.taskLevel, ckId);
3,297✔
922

923
    int64_t ver = pTask->chkInfo.processedVer;
3,297✔
924
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
3,297✔
925
    if (code != TSDB_CODE_SUCCESS) {
3,297!
926
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
927
    }
928
  }
929

930
  // TODO: monitoring the checkpoint-source msg
931
  // send check point response to upstream task
932
  if (code == TSDB_CODE_SUCCESS) {
6,127!
933
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,127✔
934
      code = streamTaskSendCheckpointSourceRsp(pTask);
3,039✔
935
    } else {
936
      code = streamTaskSendCheckpointReadyMsg(pTask);
3,088✔
937
    }
938

939
    if (code != TSDB_CODE_SUCCESS) {
6,129!
940
      // todo: let's retry send rsp to mnode, checkpoint-ready has monitor now
941
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%" PRId64 ", code:%s", id, ckId,
×
942
              tstrerror(code));
943
    }
944
  }
945

946
  if (code == TSDB_CODE_SUCCESS) {
6,129!
947
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
6,129✔
948
    if (code != TSDB_CODE_SUCCESS) {
6,126✔
949
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
1!
950
    }
951
  } else {
952
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
953
  }
954

955
  // TODO: monitoring the checkpoint-report msg
956
  // update the latest checkpoint info if all works are done successfully, for rsma, the pMsgCb is null.
957
  if (code == TSDB_CODE_SUCCESS) {
6,126✔
958
    if (pTask->pMsgCb != NULL) {
6,125✔
959
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
6,112✔
960
    }
961
  } else {  // clear the checkpoint info if failed
962
    // set failed checkpoint id before clear the checkpoint info
963
    streamMutexLock(&pTask->lock);
1✔
964
    streamTaskSetFailedCheckpointId(pTask, ckId);
1✔
965
    streamMutexUnlock(&pTask->lock);
1✔
966

967
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
1✔
968
    stDebug("s-task:%s clear checkpoint flag since gen checkpoint failed, checkpointId:%" PRId64, id, ckId);
1!
969
  }
970

971
  double el = (taosGetTimestampMs() - startTs) / 1000.0;
6,127✔
972
  stInfo("s-task:%s vgId:%d level:%d, checkpointId:%" PRId64 " ver:%" PRId64 " elapsed time:%.2fs, %s ", id,
6,127!
973
         pMeta->vgId, pTask->info.taskLevel, ckId, pTask->chkInfo.checkpointVer, el,
974
         (code == TSDB_CODE_SUCCESS) ? "succ" : "failed");
975

976
  return code;
6,129✔
977
}
978

979
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
2✔
980
  const char*            id = pTask->id.idStr;
2✔
981
  int32_t                vgId = pTask->pMeta->vgId;
2✔
982
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
2✔
983
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
2✔
984

985
  // checkpoint-trigger recv flag is set, quit
986
  if (pActiveInfo->allUpstreamTriggerRecv) {
2✔
987
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1✔
988
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
1!
989
    return -1;
1✔
990
  }
991

992
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
1!
993
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
994
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
×
995
           ", quit",
996
           id, vgId, pTmrInfo->launchChkptId);
997
    return -1;
×
998
  }
999

1000
  // active checkpoint info is cleared for now
1001
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
1!
1002
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1003
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from retrieve checkpoint-trigger send tmr", id,
×
1004
           vgId);
1005
    return -1;
×
1006
  }
1007

1008
  return 0;
1✔
1009
}
1010

1011
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
1✔
1012
  const char*            id = pTask->id.idStr;
1✔
1013
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
1014

1015
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
1✔
1016
  if (pNotSendList == NULL) {
1!
1017
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
1018
    return terrno;
×
1019
  }
1020

1021
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
2✔
1022
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
1✔
1023

1024
    bool recved = false;
1✔
1025
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
2✔
1026
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
1✔
1027
      if (pReady == NULL) {
1!
1028
        continue;
×
1029
      }
1030

1031
      if (pInfo->nodeId == pReady->upstreamNodeId) {
1!
1032
        recved = true;
×
1033
        break;
×
1034
      }
1035
    }
1036

1037
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
1!
1038
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
1✔
1039
      void* px = taosArrayPush(pNotSendList, pInfo);
1✔
1040
      if (px == NULL) {
1!
1041
        stError("s-task:%s failed to record not send info, code: out of memory", id);
×
1042
        taosArrayDestroy(pNotSendList);
×
1043
        return terrno;
×
1044
      }
1045
    }
1046
  }
1047

1048
  *ppNotSendList = pNotSendList;
1✔
1049
  return 0;
1✔
1050
}
1051

1052
int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray** ppNotSendList) {
2✔
1053
  const char*            id = pTask->id.idStr;
2✔
1054
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
2✔
1055
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
2✔
1056
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
2✔
1057
  int32_t                vgId = pTask->pMeta->vgId;
2✔
1058

1059
  int32_t code = doChkptStatusCheck(pTask, param);
2✔
1060
  if (code) {
2✔
1061
    return code;
1✔
1062
  }
1063

1064
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
1✔
1065
  if (code) {
1!
1066
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1067
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1068
    return code;
×
1069
  }
1070

1071
  // do send retrieve checkpoint trigger msg to upstream
1072
  code = doSendRetrieveTriggerMsg(pTask, *ppNotSendList);
1✔
1073
  if (code) {
1!
1074
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1075
    code = 0;
×
1076
  }
1077

1078
  return code;
1✔
1079
}
1080

1081
static void doCleanup(SStreamTask* pTask, SArray* pList) {
109,073✔
1082
  streamMetaReleaseTask(pTask->pMeta, pTask);
109,073✔
1083
  taosArrayDestroy(pList);
109,073✔
1084
}
109,073✔
1085

1086
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
109,075✔
1087
  int32_t      code = 0;
109,075✔
1088
  int32_t      numOfNotSend = 0;
109,075✔
1089
  SArray*      pNotSendList = NULL;
109,075✔
1090
  int64_t      taskRefId = *(int64_t*)param;
109,075✔
1091
  int64_t      now = taosGetTimestampMs();
109,075✔
1092

1093
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
109,075✔
1094
  if (pTask == NULL) {
109,075✔
1095
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
2!
1096
    streamTaskFreeRefId(param);
2✔
1097
    return;
109,075✔
1098
  }
1099

1100
  int32_t                vgId = pTask->pMeta->vgId;
109,073✔
1101
  const char*            id = pTask->id.idStr;
109,073✔
1102
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
109,073✔
1103
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
109,073✔
1104
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
109,073✔
1105

1106
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
109,073!
1107
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1108
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1109
    doCleanup(pTask, pNotSendList);
×
1110
    return;
×
1111
  }
1112

1113
  // check the status every 100ms
1114
  if (streamTaskShouldStop(pTask)) {
109,073✔
1115
    streamCleanBeforeQuitTmr(pTmrInfo, param);
81✔
1116
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
81✔
1117
    doCleanup(pTask, pNotSendList);
81✔
1118
    return;
81✔
1119
  }
1120

1121
  if (++pTmrInfo->activeCounter < 50) {
108,992✔
1122
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
107,380✔
1123
                   "trigger-recv-monitor");
1124
    doCleanup(pTask, pNotSendList);
107,380✔
1125
    return;
107,380✔
1126
  }
1127

1128
  pTmrInfo->activeCounter = 0;
1,612✔
1129
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
1,612✔
1130

1131
  streamMutexLock(&pTask->lock);
1,612✔
1132
  SStreamTaskState state = streamTaskGetStatus(pTask);
1,612✔
1133
  streamMutexUnlock(&pTask->lock);
1,612✔
1134

1135
  if (state.state != TASK_STATUS__CK) {
1,612✔
1136
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1,611✔
1137
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
1,611✔
1138
            vgId, state.name);
1139
    doCleanup(pTask, pNotSendList);
1,611✔
1140
    return;
1,611✔
1141
  }
1142

1143
  streamMutexLock(&pActiveInfo->lock);
1✔
1144
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
1✔
1145
  streamMutexUnlock(&pActiveInfo->lock);
1✔
1146

1147
  if (code != TSDB_CODE_SUCCESS) {
1!
1148
    doCleanup(pTask, pNotSendList);
1✔
1149
    return;
1✔
1150
  }
1151

1152
  // check every 100ms
1153
  numOfNotSend = taosArrayGetSize(pNotSendList);
×
1154
  if (numOfNotSend > 0) {
×
1155
    stDebug("s-task:%s start to monitor checkpoint-trigger in 10s", id);
×
1156
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1157
                   "trigger-recv-monitor");
1158
  } else {
1159
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1160
    stDebug("s-task:%s all checkpoint-trigger recved, quit from monitor checkpoint-trigger tmr", id);
×
1161
  }
1162

1163
  doCleanup(pTask, pNotSendList);
×
1164
}
1165

1166
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
1✔
1167
  int32_t     code = 0;
1✔
1168
  int32_t     vgId = pTask->pMeta->vgId;
1✔
1169
  const char* pId = pTask->id.idStr;
1✔
1170
  int32_t     size = taosArrayGetSize(pNotSendList);
1✔
1171
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
1✔
1172
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
1✔
1173

1174
  if (size <= 0) {
1!
1175
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1176
    return code;
×
1177
  }
1178

1179
  stDebug("s-task:%s %d/%d not recv checkpoint-trigger from upstream(s), start to send trigger-retrieve", pId, size,
1!
1180
          numOfUpstream);
1181

1182
  for (int32_t i = 0; i < size; i++) {
2✔
1183
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
1✔
1184
    if (pUpstreamTask == NULL) {
1!
1185
      return TSDB_CODE_INVALID_PARA;
×
1186
    }
1187

1188
    int32_t  ret = 0;
1✔
1189
    int32_t  tlen = 0;
1✔
1190
    void*    buf = NULL;
1✔
1191
    SRpcMsg  rpcMsg = {0};
1✔
1192
    SEncoder encoder;
1193

1194
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
1✔
1195
                                    .downstreamTaskId = pTask->id.taskId,
1✔
1196
                                    .downstreamNodeId = vgId,
1197
                                    .upstreamTaskId = pUpstreamTask->taskId,
1✔
1198
                                    .upstreamNodeId = pUpstreamTask->nodeId,
1✔
1199
                                    .checkpointId = checkpointId};
1200

1201
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
1!
1202
    if (ret < 0) {
1!
1203
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1204
    }
1205

1206
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
1✔
1207
    if (buf == NULL) {
1!
1208
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1209
      continue;
×
1210
    }
1211

1212
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
1✔
1213
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
1✔
1214

1215
    tEncoderInit(&encoder, abuf, tlen);
1✔
1216
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
1!
1217
      rpcFreeCont(buf);
×
1218
      tEncoderClear(&encoder);
×
1219
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1220
      continue;
×
1221
    }
1222
    tEncoderClear(&encoder);
1✔
1223

1224
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
1✔
1225

1226
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
1✔
1227
    if (code == TSDB_CODE_SUCCESS) {
1!
1228
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
1!
1229
              vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1230
    } else {
1231
      stError("s-task:%s vgId:%d failed to send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64,
×
1232
              pId, vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1233
    }
1234
  }
1235

1236
  return code;
1✔
1237
}
1238

1239
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
1✔
1240
  int64_t                now = taosGetTimestampMs();
1✔
1241
  const char*            id = pTask->id.idStr;
1✔
1242
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1✔
1243
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
1✔
1244

1245
  if (!pInfo->dispatchTrigger) {
1!
1246
    return false;
×
1247
  }
1248

1249
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
1✔
1250
  for (int32_t i = 0; i < num; ++i) {
1!
1251
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
1✔
1252
    if (pSendInfo == NULL) {
1!
1253
      stError("s-task:%s invalid index in dispatch-trigger list, index:%d, size:%d, ignore and continue", id, i, num);
×
1254
      continue;
×
1255
    }
1256

1257
    if (pSendInfo->nodeId != downstreamNodeId) {
1!
1258
      continue;
×
1259
    }
1260

1261
    // has send trigger msg to downstream node,
1262
    double before = (now - pSendInfo->sendTs) / 1000.0;
1✔
1263
    if (pSendInfo->recved) {
1!
1264
      stWarn("s-task:%s checkpoint-trigger msg already send at:%" PRId64
×
1265
             "(%.2fs before) and recv confirmed by downstream:0x%x, checkpointId:%" PRId64 ", transId:%d",
1266
             id, pSendInfo->sendTs, before, pSendInfo->taskId, pInfo->activeId, pInfo->transId);
1267
    } else {
1268
      stWarn("s-task:%s checkpoint-trigger already send at:%" PRId64 "(%.2fs before), checkpointId:%" PRId64
1!
1269
             ", transId:%d",
1270
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1271
    }
1272

1273
    return true;
1✔
1274
  }
1275

1276
  return false;
×
1277
}
1278

1279
bool streamTaskAlreadySendTrigger(SStreamTask* pTask, int32_t downstreamNodeId) {
1✔
1280
  int64_t                now = taosGetTimestampMs();
1✔
1281
  const char*            id = pTask->id.idStr;
1✔
1282
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1✔
1283
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
1✔
1284

1285
  if (pStatus.state != TASK_STATUS__CK) {
1!
1286
    return false;
×
1287
  }
1288

1289
  streamMutexLock(&pInfo->lock);
1✔
1290
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
1✔
1291
  streamMutexUnlock(&pInfo->lock);
1✔
1292

1293
  return send;
1✔
1294
}
1295

1296
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
2✔
1297
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
2✔
1298

1299
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2✔
1300
    *pTotal = 1;
1✔
1301
  } else {
1302
    *pTotal = streamTaskGetNumOfUpstream(pTask);
1✔
1303
  }
1304
}
2✔
1305

1306
// record the dispatch checkpoint trigger info in the list
1307
// memory insufficient may cause the stream computing stopped
1308
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
3,138✔
1309
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
3,138✔
1310
  int64_t                now = taosGetTimestampMs();
3,141✔
1311
  int32_t                code = 0;
3,141✔
1312

1313
  streamMutexLock(&pInfo->lock);
3,141✔
1314

1315
  if (sendingChkptId > pInfo->failedId) {
3,141✔
1316
    pInfo->dispatchTrigger = true;
3,140✔
1317
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
3,140✔
1318
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
535✔
1319

1320
      STaskTriggerSendInfo p = {
535✔
1321
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
535✔
1322
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
535✔
1323
      if (px == NULL) {  // pause the stream task, if memory not enough
532!
1324
        code = terrno;
×
1325
      }
1326
    } else {
1327
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
10,337✔
1328
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
7,736✔
1329
        if (pVgInfo == NULL) {
7,734!
1330
          continue;
×
1331
        }
1332

1333
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
7,734✔
1334
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
7,734✔
1335
        if (px == NULL) {  // pause the stream task, if memory not enough
7,732!
1336
          code = terrno;
×
1337
          break;
×
1338
        }
1339
      }
1340
    }
1341
  }
1342

1343
  streamMutexUnlock(&pInfo->lock);
3,128✔
1344

1345
  return code;
3,139✔
1346
}
1347

1348
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
8,245✔
1349
  int32_t num = 0;
8,245✔
1350
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
33,566✔
1351
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
25,321✔
1352
    if (p == NULL) {
25,321!
1353
      continue;
×
1354
    }
1355

1356
    if (p->recved) {
25,321✔
1357
      num++;
16,782✔
1358
    }
1359
  }
1360
  return num;
8,245✔
1361
}
1362

1363
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
8,245✔
1364
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,245✔
1365

1366
  int64_t now = taosGetTimestampMs();
8,245✔
1367
  int32_t taskId = 0;
8,245✔
1368
  int32_t total = streamTaskGetNumOfDownstream(pTask);
8,245✔
1369
  bool    alreadyRecv = false;
8,245✔
1370

1371
  streamMutexLock(&pInfo->lock);
8,245✔
1372

1373
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
16,782!
1374
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
16,782✔
1375
    if (p == NULL) {
16,782!
1376
      continue;
×
1377
    }
1378

1379
    if (p->nodeId == vgId) {
16,782✔
1380
      if (p->recved) {
8,245!
1381
        stWarn("s-task:%s already recv checkpoint-trigger msg rsp from vgId:%d down:0x%x %.2fs ago, req send:%" PRId64
×
1382
               " discard",
1383
               pTask->id.idStr, vgId, p->taskId, (now - p->recvTs) / 1000.0, p->sendTs);
1384
        alreadyRecv = true;
×
1385
      } else {
1386
        p->recved = true;
8,245✔
1387
        p->recvTs = taosGetTimestampMs();
8,245✔
1388
        taskId = p->taskId;
8,245✔
1389
      }
1390
      break;
8,245✔
1391
    }
1392
  }
1393

1394
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
8,245✔
1395
  streamMutexUnlock(&pInfo->lock);
8,245✔
1396

1397
  if (taskId == 0) {
8,245!
1398
    stError("s-task:%s recv invalid trigger-dispatch confirm, vgId:%d", pTask->id.idStr, vgId);
×
1399
  } else {
1400
    if (!alreadyRecv) {
8,245!
1401
      stDebug("s-task:%s set downstream:0x%x(vgId:%d) checkpoint-trigger dispatch confirmed, total confirmed:%d/%d",
8,245✔
1402
              pTask->id.idStr, taskId, vgId, numOfConfirmed, total);
1403
    }
1404
  }
1405
}
8,245✔
1406

1407
int32_t uploadCheckpointToS3(const char* id, const char* path) {
1✔
1408
  int32_t code = 0;
1✔
1409
  int32_t nBytes = 0;
1✔
1410
  /*
1411
  if (s3Init() != 0) {
1412
    return TSDB_CODE_THIRDPARTY_ERROR;
1413
  }
1414
  */
1415
  TdDirPtr pDir = taosOpenDir(path);
1✔
1416
  if (pDir == NULL) {
1!
1417
    return terrno;
×
1418
  }
1419

1420
  TdDirEntryPtr de = NULL;
1✔
1421
  while ((de = taosReadDir(pDir)) != NULL) {
5✔
1422
    char* name = taosGetDirEntryName(de);
4✔
1423
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
4!
1424

1425
    char filename[PATH_MAX] = {0};
2✔
1426
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
2!
1427
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
1428
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1429
        code = TSDB_CODE_OUT_OF_RANGE;
×
1430
        break;
×
1431
      }
1432
    } else {
1433
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
2✔
1434
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
2!
1435
        code = TSDB_CODE_OUT_OF_RANGE;
×
1436
        break;
×
1437
      }
1438
    }
1439

1440
    char object[PATH_MAX] = {0};
2✔
1441
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
2✔
1442
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
2!
1443
      code = TSDB_CODE_OUT_OF_RANGE;
×
1444
      break;
×
1445
    }
1446

1447
    code = tcsPutObjectFromFile2(filename, object, 0);
2✔
1448
    if (code != 0) {
2!
1449
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
2!
1450
    } else {
1451
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1452
    }
1453
  }
1454

1455
  int32_t ret = taosCloseDir(&pDir);
1✔
1456
  if (code == 0 && ret != 0) {
1!
1457
    code = ret;
×
1458
  }
1459

1460
  return code;
1✔
1461
}
1462

1463
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
1✔
1464
  int32_t nBytes;
1465
  int32_t cap = strlen(id) + strlen(dstName) + 16;
1✔
1466

1467
  char* buf = taosMemoryCalloc(1, cap);
1!
1468
  if (buf == NULL) {
1!
1469
    return terrno;
×
1470
  }
1471

1472
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
1✔
1473
  if (nBytes <= 0 || nBytes >= cap) {
1!
1474
    taosMemoryFree(buf);
×
1475
    return TSDB_CODE_OUT_OF_RANGE;
×
1476
  }
1477
  int32_t code = tcsGetObjectToFile(buf, dstName);
1✔
1478
  if (code != 0) {
1!
1479
    taosMemoryFree(buf);
1!
1480
    return TAOS_SYSTEM_ERROR(errno);
1✔
1481
  }
1482
  taosMemoryFree(buf);
×
1483
  return 0;
×
1484
}
1485

1486
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
6,138✔
1487
  if (strlen(tsSnodeAddress) != 0) {
6,138✔
1488
    return DATA_UPLOAD_RSYNC;
1✔
1489
  } else if (tsS3StreamEnabled) {
6,137!
1490
    return DATA_UPLOAD_S3;
×
1491
  } else {
1492
    return DATA_UPLOAD_DISABLE;
6,137✔
1493
  }
1494
}
1495

1496
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
2✔
1497
  int32_t code = 0;
2✔
1498
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
2!
1499
    stError("invalid parameters in upload checkpoint, %s", id);
1!
1500
    return TSDB_CODE_INVALID_CFG;
1✔
1501
  }
1502

1503
  if (strlen(tsSnodeAddress) != 0) {
1!
1504
    code = uploadByRsync(id, path, checkpointId);
×
1505
    if (code != 0) {
×
1506
      return TAOS_SYSTEM_ERROR(errno);
×
1507
    }
1508
  } else if (tsS3StreamEnabled) {
1!
1509
    return uploadCheckpointToS3(id, path);
×
1510
  }
1511

1512
  return 0;
1✔
1513
}
1514

1515
// fileName:  CURRENT
1516
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
1✔
1517
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
1!
1518
    stError("down load checkpoint data parameters invalid");
×
1519
    return TSDB_CODE_INVALID_PARA;
×
1520
  }
1521

1522
  if (strlen(tsSnodeAddress) != 0) {
1!
1523
    return 0;
×
1524
  } else if (tsS3StreamEnabled) {
1!
1525
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1526
  }
1527

1528
  return 0;
1✔
1529
}
1530

1531
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
1✔
1532
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
1!
1533
    stError("down checkpoint data parameters invalid");
×
1534
    return -1;
×
1535
  }
1536

1537
  if (strlen(tsSnodeAddress) != 0) {
1!
1538
    return downloadByRsync(id, path, checkpointId);
1✔
1539
  } else if (tsS3StreamEnabled) {
×
1540
    return tcsGetObjectsByPrefix(id, path);
×
1541
  }
1542

1543
  return 0;
×
1544
}
1545

1546
#ifdef BUILD_NO_CALL
1547
int32_t deleteCheckpoint(const char* id) {
1548
  if (id == NULL || strlen(id) == 0) {
1549
    stError("deleteCheckpoint parameters invalid");
1550
    return TSDB_CODE_INVALID_PARA;
1551
  }
1552
  if (strlen(tsSnodeAddress) != 0) {
1553
    return deleteRsync(id);
1554
  } else if (tsS3StreamEnabled) {
1555
    tcsDeleteObjectsByPrefix(id);
1556
  }
1557
  return 0;
1558
}
1559
#endif
1560

1561
int32_t deleteCheckpointFile(const char* id, const char* name) {
1✔
1562
  char object[128] = {0};
1✔
1563

1564
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
1✔
1565
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
1!
1566
    return TSDB_CODE_OUT_OF_RANGE;
×
1567
  }
1568

1569
  char*   tmp = object;
1✔
1570
  int32_t code = tcsDeleteObjects((const char**)&tmp, 1);
1✔
1571
  if (code != 0) {
1!
1572
    return TSDB_CODE_THIRDPARTY_ERROR;
1✔
1573
  }
1574
  return code;
×
1575
}
1576

1577
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
404✔
1578
  streamMutexLock(&pTask->lock);
404✔
1579
  ETaskStatus p = streamTaskGetStatus(pTask).state;
406✔
1580
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
406✔
1581
  streamMutexUnlock(&pTask->lock);
406✔
1582

1583
  if (pTask->pBackend != NULL) {
406!
1584
    streamFreeTaskState(pTask, p);
×
1585
    pTask->pBackend = NULL;
×
1586
  }
1587

1588
  streamMetaWLock(pTask->pMeta);
406✔
1589
  if (pTask->exec.pExecutor != NULL) {
406!
1590
    qDestroyTask(pTask->exec.pExecutor);
×
1591
    pTask->exec.pExecutor = NULL;
×
1592
  }
1593
  streamMetaWUnLock(pTask->pMeta);
406✔
1594

1595
  return 0;
406✔
1596
}
1597

1598
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
85✔
1599
  int32_t code = 0;
85✔
1600
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
85✔
1601
    return code;
44✔
1602
  }
1603

1604
  streamMutexLock(&pTask->lock);
41✔
1605
  SStreamTaskState p = streamTaskGetStatus(pTask);
41✔
1606
  if (p.state == TASK_STATUS__CK) {
41!
1607
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1608
  }
1609
  streamMutexUnlock(&pTask->lock);
41✔
1610

1611
  return code;
41✔
1612
}
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