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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

65.53
/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,
2,808✔
33
                                int32_t srcTaskId, SStreamDataBlock** pRes) {
34
  SStreamDataBlock* pChkpoint = NULL;
2,808✔
35
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pChkpoint);
2,808✔
36
  if (code) {
2,809!
37
    return code;
×
38
  }
39

40
  pChkpoint->type = checkpointType;
2,809✔
41
  if (checkpointType == STREAM_INPUT__CHECKPOINT_TRIGGER && (pTask->info.taskLevel != TASK_LEVEL__SOURCE)) {
2,809!
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));
2,809!
50
  if (pBlock == NULL) {
2,809!
51
    taosFreeQitem(pChkpoint);
×
52
    return terrno;
×
53
  }
54

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

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

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

76
  *pRes = pChkpoint;
2,809✔
77

78
  taosMemoryFree(pBlock);
2,809!
79
  return TSDB_CODE_SUCCESS;
2,809✔
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,
2,808✔
85
                                   int32_t srcTaskId) {
86
  SStreamDataBlock* pCheckpoint = NULL;
2,808✔
87
  int32_t code = createChkptTriggerBlock(pTask, checkpointType, checkpointId, transId, srcTaskId, &pCheckpoint);
2,808✔
88
  if (code != TSDB_CODE_SUCCESS) {
2,809!
89
    return code;
×
90
  }
91

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

96
  return streamTrySchedExec(pTask);
2,809✔
97
}
98

99
int32_t streamProcessCheckpointSourceReq(SStreamTask* pTask, SStreamCheckpointSourceReq* pReq) {
1,397✔
100
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,397!
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);
1,397✔
107
  if (code != TSDB_CODE_SUCCESS) {
1,397!
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;
1,397✔
113
  pTask->chkInfo.pActiveInfo->activeId = pReq->checkpointId;
1,397✔
114
  pTask->chkInfo.startTs = taosGetTimestampMs();
1,397✔
115
  pTask->execInfo.checkpoint += 1;
1,397✔
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);
1,397✔
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) {
1,407✔
213
  pBlock->srcTaskId = pTask->id.taskId;
1,407✔
214
  pBlock->srcVgId = pTask->pMeta->vgId;
1,407✔
215

216
  if (pTask->chkInfo.pActiveInfo->dispatchTrigger == true) {
1,407!
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);
1,407✔
222
  if (code == 0) {
1,407!
223
    code = streamDispatchStreamBlock(pTask);
1,407✔
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;
1,407✔
230
}
231

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
5,817✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
5,817✔
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) {
5,816✔
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) {
5,815✔
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) {
5,814✔
281
    if (pActiveInfo->activeId != checkpointId) {
4,429✔
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) {
4,428✔
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) {
4,427✔
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) {
8,680✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
5,649✔
299
          if (p == NULL) {
5,649!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

303
          if (p->upstreamTaskId == pBlock->srcTaskId) {
5,649✔
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;
5,811✔
315
}
316

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

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

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

334
  streamMutexLock(&pTask->lock);
5,811✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
5,811✔
336
  streamMutexUnlock(&pTask->lock);
5,811✔
337
  if (code) {
5,811!
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
5,811✔
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) {
5,811✔
351
    pActiveInfo->activeId = checkpointId;
1,385✔
352
    pActiveInfo->transId = transId;
1,385✔
353

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

359
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_GEN_CHECKPOINT);
1,385✔
360
    if (code != TSDB_CODE_SUCCESS) {
1,385!
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;
1,385✔
372
    int8_t          old = atomic_val_compare_exchange_8(&pTmrInfo->isActive, 0, 1);
1,385✔
373
    if (old == 0) {
1,385!
374
      stDebug("s-task:%s start checkpoint-trigger monitor in 10s", pTask->id.idStr);
1,385✔
375

376
      int64_t* pTaskRefId = NULL;
1,385✔
377
      code = streamTaskAllocRefId(pTask, &pTaskRefId);
1,385✔
378
      if (code == 0) {
1,385!
379
        streamTmrStart(checkpointTriggerMonitorFn, 200, pTaskRefId, streamTimer, &pTmrInfo->tmrHandle, vgId,
1,385✔
380
                       "trigger-recv-monitor");
381
        pTmrInfo->launchChkptId = pActiveInfo->activeId;
1,385✔
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) {
5,811✔
393
    int8_t type = pTask->outputInfo.type;
1,395✔
394
    pActiveInfo->allUpstreamTriggerRecv = 1;
1,395✔
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);
1,395✔
401
    if (code) {
1,395!
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) {
1,395✔
411
      stDebug("s-task:%s set childIdx:%d, and add checkpoint-trigger block into outputQ", id, pTask->info.selfChildId);
1,362✔
412
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);  // todo handle this failure
1,362✔
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);
33✔
416
      streamFreeQitem((SStreamQueueItem*)pBlock);
33✔
417
    }
418
  } else if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
4,416!
419
    // todo: handle this
420
    // update the child Id for downstream tasks
421
    code = streamAddCheckpointReadyMsg(pTask, pBlock->srcTaskId, pTask->info.selfChildId, checkpointId);
4,416✔
422

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

429
    int32_t num = streamTaskGetNumOfUpstream(pTask);
1,373✔
430
    if (taskLevel == TASK_LEVEL__SINK) {
1,373✔
431
      stDebug("s-task:%s process checkpoint-trigger block, all %d upstreams sent, send ready msg to upstream", id, num);
1,328✔
432
      streamFreeQitem((SStreamQueueItem*)pBlock);
1,328✔
433
      code = streamTaskBuildCheckpoint(pTask);  // todo: not handle error yet
1,328✔
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);
45✔
436
      code = flushStateDataInExecutor(pTask, (SStreamQueueItem*)pBlock);
45✔
437
      if (code) {
45!
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);
45✔
444
    }
445
  }
446

447
  return code;
2,768✔
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,
4,402✔
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;
4,402✔
455
  int32_t size = taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
4,402✔
456
  for (int32_t i = 0; i < size; ++i) {
10,051✔
457
    STaskDownstreamReadyInfo* p = taosArrayGet(pInfo->pCheckpointReadyRecvList, i);
5,649✔
458
    if (p == NULL) {
5,649!
459
      return TSDB_CODE_INVALID_PARA;
×
460
    }
461

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

468
  if (*alreadyRecv) {
4,402!
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(),
4,402✔
474
                                     .downstreamTaskId = downstreamTaskId,
475
                                     .checkpointId = pInfo->activeId,
4,402✔
476
                                     .transId = pInfo->transId,
4,402✔
477
                                     .streamId = streamId,
478
                                     .downstreamNodeId = downstreamNodeId};
479
    void*                    p = taosArrayPush(pInfo->pCheckpointReadyRecvList, &info);
4,402✔
480
    if (p == NULL) {
4,402!
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);
4,402✔
487
  *pTransId = pInfo->transId;
4,402✔
488
  return 0;
4,402✔
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,
4,402✔
496
                                        int32_t downstreamTaskId) {
497
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
4,402✔
498

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

506
  // 1. not in checkpoint status now
507
  SStreamTaskState pStat = streamTaskGetStatus(pTask);
4,402✔
508
  if (pStat.state != TASK_STATUS__CK) {
4,402!
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) {
4,402!
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);
4,402✔
522
  code = processCheckpointReadyHelp(pInfo, total, downstreamNodeId, pTask->id.streamId, downstreamTaskId, id, &notReady,
4,402✔
523
                                    &transId, &alreadyHandled);
524
  streamMutexUnlock(&pInfo->lock);
4,402✔
525

526
  if (alreadyHandled) {
4,402!
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)) {
4,402!
531
      stDebug("s-task:%s all downstream tasks have completed build checkpoint, do checkpoint for current task", id);
1,379✔
532
      code = appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT, checkpointId, transId, -1);
1,379✔
533
    }
534
  }
535

536
  return code;
4,402✔
537
}
538

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

544
  streamMutexLock(&pInfo->lock);
4,402✔
545
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
10,049!
546
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
10,049✔
547
    if (pReadyInfo == NULL) {
10,049!
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) {
10,049!
554
      pReadyInfo->sendCompleted = 1;
4,402✔
555
      stDebug("s-task:%s send checkpoint-ready msg to upstream:0x%x confirmed, checkpointId:%" PRId64 " ts:%" PRId64,
4,402✔
556
              pTask->id.idStr, upstreamTaskId, checkpointId, now);
557
      break;
4,402✔
558
    }
559
  }
560

561
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
20,098✔
562
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
15,696✔
563
    if (pReadyInfo == NULL) {
15,696!
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) {
15,696✔
570
      numOfConfirmed += 1;
10,049✔
571
    }
572
  }
573

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

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

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

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

587
  streamMutexLock(&pInfo->lock);
2,472✔
588
  streamTaskClearActiveInfo(pInfo);
2,472✔
589
  if (clearChkpReadyMsg) {
2,472!
590
    streamClearChkptReadyMsg(pInfo);
2,472✔
591
  }
592
  streamMutexUnlock(&pInfo->lock);
2,472✔
593

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

598
int32_t streamTaskUpdateTaskCheckpointInfo(SStreamTask* pTask, bool restored, SVUpdateCheckpointInfoReq* pReq) {
2,484✔
599
  SStreamMeta*     pMeta = pTask->pMeta;
2,484✔
600
  int32_t          vgId = pMeta->vgId;
2,484✔
601
  int32_t          code = 0;
2,484✔
602
  const char*      id = pTask->id.idStr;
2,484✔
603
  SCheckpointInfo* pInfo = &pTask->chkInfo;
2,484✔
604

605
  streamMutexLock(&pTask->lock);
2,484✔
606

607
  // not update the checkpoint info if the checkpointId is less than the failed checkpointId
608
  if (pReq->checkpointId < pInfo->pActiveInfo->failedId) {
2,484!
UNCOV
609
    stWarn("s-task:%s vgId:%d not update the checkpoint-info, since update checkpointId:%" PRId64
×
610
           " is less than the failed checkpointId:%" PRId64 ", discard the update info",
611
           id, vgId, pReq->checkpointId, pInfo->pActiveInfo->failedId);
UNCOV
612
    streamMutexUnlock(&pTask->lock);
×
613

614
    // always return true
615
    return TSDB_CODE_SUCCESS;
×
616
  }
617

618
  if (pReq->checkpointId <= pInfo->checkpointId) {
2,484✔
619
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
12!
620
            " no need to update checkpoint info, updated checkpointId:%" PRId64 " Ver:%" PRId64 " transId:%d ignored",
621
            id, vgId, pInfo->checkpointId, pInfo->checkpointVer, pReq->checkpointId, pReq->checkpointVer,
622
            pReq->transId);
623
    streamMutexUnlock(&pTask->lock);
12✔
624

625
    {  // destroy the related fill-history tasks
626
      // drop task should not in the meta-lock, and drop the related fill-history task now
627
      if (pReq->dropRelHTask) {
12✔
628
        code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
8✔
629
        int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
8✔
630
        stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped in update checkpointInfo, remain tasks:%d",
8!
631
                id, vgId, pReq->taskId, numOfTasks);
632
      }
633

634
      if (pReq->dropRelHTask) {
12✔
635
        code = streamMetaCommit(pMeta);
8✔
636
      }
637
    }
638

639
    // always return true
640
    return TSDB_CODE_SUCCESS;
12✔
641
  }
642

643
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
2,472✔
644

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

664
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
4,944!
665
                pInfo->processedVer <= pReq->checkpointVer);
2,472!
666

667
  if (!valid) {
2,472!
668
    stFatal("s-task:%s invalid checkpointId update info recv, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
669
            " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64 " discard it",
670
            id, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
671
            pReq->checkpointVer);
672
    streamMutexUnlock(&pTask->lock);
×
673
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
674
  }
675

676
  // update only it is in checkpoint status, or during restore procedure.
677
  if ((pStatus.state == TASK_STATUS__CK) || (!restored) || (pMeta->role == NODE_ROLE_FOLLOWER)) {
2,472!
678
    pInfo->checkpointId = pReq->checkpointId;
2,472✔
679
    pInfo->checkpointVer = pReq->checkpointVer;
2,472✔
680
    pInfo->checkpointTime = pReq->checkpointTs;
2,472✔
681

682
    if (restored && (pMeta->role == NODE_ROLE_LEADER)) {
2,472✔
683
      code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
2,442✔
684
    }
685
  }
686

687
  streamTaskClearCheckInfo(pTask, true);
2,472✔
688

689
  if (pReq->dropRelHTask) {
2,472✔
690
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
2,410✔
691
            pReq->taskId, vgId, pReq->hTaskId);
692
    CLEAR_RELATED_FILLHISTORY_TASK(pTask);
2,410✔
693
  }
694

695
  stDebug("s-task:0x%x set the persistent status attr to be ready, prev:%s, status in sm:%s", pReq->taskId,
2,472✔
696
          streamTaskGetStatusStr(pTask->status.taskStatus), streamTaskGetStatus(pTask).name);
697

698
  pTask->status.taskStatus = TASK_STATUS__READY;
2,472✔
699

700
  code = streamMetaSaveTask(pMeta, pTask);
2,472✔
701
  streamMutexUnlock(&pTask->lock);
2,472✔
702

703
  if (code != TSDB_CODE_SUCCESS) {
2,472!
704
    stError("s-task:%s vgId:%d failed to save task info after do checkpoint, checkpointId:%" PRId64 ", since %s", id,
×
705
            vgId, pReq->checkpointId, terrstr());
706
    return TSDB_CODE_SUCCESS;
×
707
  }
708

709
  // drop task should not in the meta-lock, and drop the related fill-history task now
710
  if (pReq->dropRelHTask) {
2,472✔
711
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
2,410✔
712
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
2,410✔
713
    stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
2,410✔
714
            (int32_t)pReq->hTaskId, numOfTasks);
715
  }
716

717
  code = streamMetaCommit(pMeta);
2,472✔
718
  return TSDB_CODE_SUCCESS;
2,472✔
719
}
720

721
void streamTaskSetFailedCheckpointId(SStreamTask* pTask, int64_t failedId) {
4✔
722
  struct SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
4✔
723

724
  if (failedId <= 0) {
4✔
725
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
2!
726
           " activeId:%" PRId64,
727
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
728
  } else {
729
    if (failedId <= pInfo->failedId) {
2✔
730
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
1!
731
    } else {
732
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
1!
733
              " prev failedId:%" PRId64,
734
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
735
      pInfo->failedId = failedId;
1✔
736
    }
737
  }
738
}
4✔
739

740
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
465✔
741
  streamMutexLock(&pTask->lock);
465✔
742
  ETaskStatus status = streamTaskGetStatus(pTask).state;
466✔
743
  if (status == TASK_STATUS__CK) {
466!
744
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
745
  }
746
  streamMutexUnlock(&pTask->lock);
466✔
747
}
466✔
748

749
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
1✔
750
  int32_t code = 0;
1✔
751
  int32_t cap = strlen(path) + 64;
1✔
752

753
  char* filePath = taosMemoryCalloc(1, cap);
1!
754
  if (filePath == NULL) {
1!
755
    return terrno;
×
756
  }
757

758
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
1✔
759
  if (nBytes <= 0 || nBytes >= cap) {
1!
760
    taosMemoryFree(filePath);
×
761
    return TSDB_CODE_OUT_OF_RANGE;
×
762
  }
763

764
  code = downloadCheckpointDataByName(id, "META", filePath);
1✔
765
  if (code != 0) {
1!
766
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
767
    taosMemoryFree(filePath);
×
768
    return code;
×
769
  }
770

771
  code = remoteChkpGetDelFile(filePath, list);
1✔
772
  if (code != 0) {
1!
773
    stError("%s chkp failed to get to del:%s", id, filePath);
1!
774
    taosMemoryFree(filePath);
1!
775
  }
776
  return 0;
1✔
777
}
778

779
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
3✔
780
  int32_t code = 0;
3✔
781
  char*   path = NULL;
3✔
782

783
  SStreamMeta* pMeta = pTask->pMeta;
3✔
784
  const char*  idStr = pTask->id.idStr;
3✔
785
  int64_t      now = taosGetTimestampMs();
3✔
786

787
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
3✔
788
  if (toDelFiles == NULL) {
3!
789
    return terrno;
×
790
  }
791

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

797
  if (type == DATA_UPLOAD_S3) {
3✔
798
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
1!
799
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
800
              tstrerror(code));
801
    }
802
  }
803

804
  if (code == TSDB_CODE_SUCCESS) {
3✔
805
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
2✔
806
    if (code == TSDB_CODE_SUCCESS) {
2✔
807
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
1!
808
    } else {
809
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
1!
810
              tstrerror(code));
811
    }
812
  }
813

814
  if (code == TSDB_CODE_SUCCESS) {
3✔
815
    int32_t size = taosArrayGetSize(toDelFiles);
1✔
816
    stDebug("s-task:%s remove redundant %d files", idStr, size);
1!
817

818
    for (int i = 0; i < size; i++) {
1!
819
      char* pName = taosArrayGetP(toDelFiles, i);
×
820
      code = deleteCheckpointFile(idStr, pName);
×
821
      if (code != 0) {
×
822
        stDebug("s-task:%s failed to remove file: %s", idStr, pName);
×
823
        break;
×
824
      }
825
    }
826

827
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
1!
828
  }
829

830
  taosArrayDestroyP(toDelFiles, NULL);
3✔
831
  double el = (taosGetTimestampMs() - now) / 1000.0;
3✔
832

833
  if (code == TSDB_CODE_SUCCESS) {
3✔
834
    stDebug("s-task:%s complete update checkpointId:%" PRId64 ", elapsed time:%.2fs remove local checkpoint data %s",
1!
835
            idStr, checkpointId, el, path);
836
    taosRemoveDir(path);
1✔
837
  } else {
838
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " keep local checkpoint data, elapsed time:%.2fs", idStr,
2!
839
            checkpointId, el);
840
  }
841

842
  taosMemoryFree(path);
3!
843
  return code;
3✔
844
}
845

846
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
2,747✔
847
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
2,747✔
848
  if (type == DATA_UPLOAD_DISABLE) {
2,747✔
849
    stDebug("s-task:%s not allowed to upload checkpoint data", pTask->id.idStr);
2,746✔
850
    return 0;
2,746✔
851
  }
852

853
  if (pTask == NULL || pTask->pBackend == NULL) {
1!
854
    return 0;
×
855
  }
856

857
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
1✔
858
  void*   pBackend = taskAcquireDb(dbRefId);
1✔
859
  if (pBackend == NULL) {
1!
860
    stError("s-task:%s failed to acquire db during update checkpoint data, failed to upload checkpointData",
×
861
            pTask->id.idStr);
862
    return -1;
×
863
  }
864

865
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
1✔
866
  taskReleaseDb(dbRefId);
1✔
867

868
  return code;
1✔
869
}
870

871
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
2,747✔
872
  int32_t      code = TSDB_CODE_SUCCESS;
2,747✔
873
  int64_t      startTs = pTask->chkInfo.startTs;
2,747✔
874
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
2,747✔
875
  const char*  id = pTask->id.idStr;
2,747✔
876
  bool         dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
2,747✔
877
  SStreamMeta* pMeta = pTask->pMeta;
2,747✔
878

879
  // sink task does not need to save the status, and generated the checkpoint
880
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
2,747✔
881
    stDebug("s-task:%s level:%d start gen checkpoint, checkpointId:%" PRId64, id, pTask->info.taskLevel, ckId);
1,418✔
882

883
    int64_t ver = pTask->chkInfo.processedVer;
1,418✔
884
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
1,418✔
885
    if (code != TSDB_CODE_SUCCESS) {
1,418!
886
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
887
    }
888
  }
889

890
  // TODO: monitoring the checkpoint-source msg
891
  // send check point response to upstream task
892
  if (code == TSDB_CODE_SUCCESS) {
2,747!
893
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2,747✔
894
      code = streamTaskSendCheckpointSourceRsp(pTask);
1,359✔
895
    } else {
896
      code = streamTaskSendCheckpointReadyMsg(pTask);
1,388✔
897
    }
898

899
    if (code != TSDB_CODE_SUCCESS) {
2,747!
900
      // todo: let's retry send rsp to mnode, checkpoint-ready has monitor now
901
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%" PRId64 ", code:%s", id, ckId,
×
902
              tstrerror(code));
903
    }
904
  }
905

906
  if (code == TSDB_CODE_SUCCESS) {
2,747!
907
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
2,747✔
908
    if (code != TSDB_CODE_SUCCESS) {
2,747✔
909
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
1!
910
    }
911
  } else {
912
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
913
  }
914

915
  // TODO: monitoring the checkpoint-report msg
916
  // update the latest checkpoint info if all works are done successfully, for rsma, the pMsgCb is null.
917
  if (code == TSDB_CODE_SUCCESS) {
2,747✔
918
    if (pTask->pMsgCb != NULL) {
2,746✔
919
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
2,732✔
920
    }
921
  } else {  // clear the checkpoint info if failed
922
    // set failed checkpoint id before clear the checkpoint info
923
    streamMutexLock(&pTask->lock);
1✔
924
    streamTaskSetFailedCheckpointId(pTask, ckId);
1✔
925
    streamMutexUnlock(&pTask->lock);
1✔
926

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

931
  double el = (taosGetTimestampMs() - startTs) / 1000.0;
2,747✔
932
  stInfo("s-task:%s vgId:%d level:%d, checkpointId:%" PRId64 " ver:%" PRId64 " elapsed time:%.2fs, %s ", id,
2,747!
933
         pMeta->vgId, pTask->info.taskLevel, ckId, pTask->chkInfo.checkpointVer, el,
934
         (code == TSDB_CODE_SUCCESS) ? "succ" : "failed");
935

936
  return code;
2,747✔
937
}
938

939
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
1✔
940
  const char*            id = pTask->id.idStr;
1✔
941
  int32_t                vgId = pTask->pMeta->vgId;
1✔
942
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
943
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
1✔
944

945
  // checkpoint-trigger recv flag is set, quit
946
  if (pActiveInfo->allUpstreamTriggerRecv) {
1!
947
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
948
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
×
949
    return -1;
×
950
  }
951

952
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
1!
953
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
954
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
×
955
           ", quit",
956
           id, vgId, pTmrInfo->launchChkptId);
957
    return -1;
×
958
  }
959

960
  // active checkpoint info is cleared for now
961
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
1!
962
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
963
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from retrieve checkpoint-trigger send tmr", id,
×
964
           vgId);
965
    return -1;
×
966
  }
967

968
  return 0;
1✔
969
}
970

971
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
1✔
972
  const char*            id = pTask->id.idStr;
1✔
973
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
974

975
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
1✔
976
  if (pNotSendList == NULL) {
1!
977
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
978
    return terrno;
×
979
  }
980

981
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
2✔
982
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
1✔
983

984
    bool recved = false;
1✔
985
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
2✔
986
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
1✔
987
      if (pReady == NULL) {
1!
988
        continue;
×
989
      }
990

991
      if (pInfo->nodeId == pReady->upstreamNodeId) {
1!
992
        recved = true;
×
993
        break;
×
994
      }
995
    }
996

997
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
1!
998
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
1✔
999
      void* px = taosArrayPush(pNotSendList, pInfo);
1✔
1000
      if (px == NULL) {
1!
1001
        stError("s-task:%s failed to record not send info, code: out of memory", id);
×
1002
        taosArrayDestroy(pNotSendList);
×
1003
        return terrno;
×
1004
      }
1005
    }
1006
  }
1007

1008
  *ppNotSendList = pNotSendList;
1✔
1009
  return 0;
1✔
1010
}
1011

1012
int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray** ppNotSendList) {
1✔
1013
  const char*            id = pTask->id.idStr;
1✔
1014
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
1✔
1015
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
1016
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
1✔
1017
  int32_t                vgId = pTask->pMeta->vgId;
1✔
1018

1019
  int32_t code = doChkptStatusCheck(pTask, param);
1✔
1020
  if (code) {
1!
1021
    return code;
×
1022
  }
1023

1024
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
1✔
1025
  if (code) {
1!
1026
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1027
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1028
    return code;
×
1029
  }
1030

1031
  // do send retrieve checkpoint trigger msg to upstream
1032
  code = doSendRetrieveTriggerMsg(pTask, *ppNotSendList);
1✔
1033
  if (code) {
1!
1034
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1035
    code = 0;
×
1036
  }
1037

1038
  return code;
1✔
1039
}
1040

1041
static void doCleanup(SStreamTask* pTask, SArray* pList) {
43,247✔
1042
  streamMetaReleaseTask(pTask->pMeta, pTask);
43,247✔
1043
  taosArrayDestroy(pList);
43,247✔
1044
}
43,247✔
1045

1046
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
43,250✔
1047
  int32_t      code = 0;
43,250✔
1048
  int32_t      numOfNotSend = 0;
43,250✔
1049
  SArray*      pNotSendList = NULL;
43,250✔
1050
  int64_t      taskRefId = *(int64_t*)param;
43,250✔
1051
  int64_t      now = taosGetTimestampMs();
43,250✔
1052

1053
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
43,250✔
1054
  if (pTask == NULL) {
43,250✔
1055
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
3!
1056
    streamTaskFreeRefId(param);
3✔
1057
    return;
43,250✔
1058
  }
1059

1060
  int32_t                vgId = pTask->pMeta->vgId;
43,247✔
1061
  const char*            id = pTask->id.idStr;
43,247✔
1062
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
43,247✔
1063
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
43,247✔
1064
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
43,247✔
1065

1066
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
43,247!
1067
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1068
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1069
    doCleanup(pTask, pNotSendList);
×
1070
    return;
×
1071
  }
1072

1073
  // check the status every 100ms
1074
  if (streamTaskShouldStop(pTask)) {
43,247✔
1075
    streamCleanBeforeQuitTmr(pTmrInfo, param);
14✔
1076
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
14!
1077
    doCleanup(pTask, pNotSendList);
14✔
1078
    return;
14✔
1079
  }
1080

1081
  if (++pTmrInfo->activeCounter < 50) {
43,233✔
1082
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
42,801✔
1083
                   "trigger-recv-monitor");
1084
    doCleanup(pTask, pNotSendList);
42,801✔
1085
    return;
42,801✔
1086
  }
1087

1088
  pTmrInfo->activeCounter = 0;
432✔
1089
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
432✔
1090

1091
  streamMutexLock(&pTask->lock);
432✔
1092
  SStreamTaskState state = streamTaskGetStatus(pTask);
432✔
1093
  streamMutexUnlock(&pTask->lock);
432✔
1094

1095
  if (state.state != TASK_STATUS__CK) {
432!
1096
    streamCleanBeforeQuitTmr(pTmrInfo, param);
432✔
1097
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
432✔
1098
            vgId, state.name);
1099
    doCleanup(pTask, pNotSendList);
432✔
1100
    return;
432✔
1101
  }
1102

1103
  streamMutexLock(&pActiveInfo->lock);
×
1104
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
×
1105
  streamMutexUnlock(&pActiveInfo->lock);
×
1106

1107
  if (code != TSDB_CODE_SUCCESS) {
×
1108
    doCleanup(pTask, pNotSendList);
×
1109
    return;
×
1110
  }
1111

1112
  // check every 100ms
1113
  numOfNotSend = taosArrayGetSize(pNotSendList);
×
1114
  if (numOfNotSend > 0) {
×
1115
    stDebug("s-task:%s start to monitor checkpoint-trigger in 10s", id);
×
1116
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1117
                   "trigger-recv-monitor");
1118
  } else {
1119
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1120
    stDebug("s-task:%s all checkpoint-trigger recved, quit from monitor checkpoint-trigger tmr", id);
×
1121
  }
1122

1123
  doCleanup(pTask, pNotSendList);
×
1124
}
1125

1126
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
1✔
1127
  int32_t     code = 0;
1✔
1128
  int32_t     vgId = pTask->pMeta->vgId;
1✔
1129
  const char* pId = pTask->id.idStr;
1✔
1130
  int32_t     size = taosArrayGetSize(pNotSendList);
1✔
1131
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
1✔
1132
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
1✔
1133

1134
  if (size <= 0) {
1!
1135
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1136
    return code;
×
1137
  }
1138

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

1142
  for (int32_t i = 0; i < size; i++) {
2✔
1143
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
1✔
1144
    if (pUpstreamTask == NULL) {
1!
1145
      return TSDB_CODE_INVALID_PARA;
×
1146
    }
1147

1148
    int32_t  ret = 0;
1✔
1149
    int32_t  tlen = 0;
1✔
1150
    void*    buf = NULL;
1✔
1151
    SRpcMsg  rpcMsg = {0};
1✔
1152
    SEncoder encoder;
1153

1154
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
1✔
1155
                                    .downstreamTaskId = pTask->id.taskId,
1✔
1156
                                    .downstreamNodeId = vgId,
1157
                                    .upstreamTaskId = pUpstreamTask->taskId,
1✔
1158
                                    .upstreamNodeId = pUpstreamTask->nodeId,
1✔
1159
                                    .checkpointId = checkpointId};
1160

1161
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
1!
1162
    if (ret < 0) {
1!
1163
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1164
    }
1165

1166
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
1✔
1167
    if (buf == NULL) {
1!
1168
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1169
      continue;
×
1170
    }
1171

1172
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
1✔
1173
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
1✔
1174

1175
    tEncoderInit(&encoder, abuf, tlen);
1✔
1176
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
1!
1177
      rpcFreeCont(buf);
×
1178
      tEncoderClear(&encoder);
×
1179
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1180
      continue;
×
1181
    }
1182
    tEncoderClear(&encoder);
1✔
1183

1184
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
1✔
1185

1186
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
1✔
1187
    if (code == TSDB_CODE_SUCCESS) {
1!
1188
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
1!
1189
              vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1190
    } else {
1191
      stError("s-task:%s vgId:%d failed to send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64,
×
1192
              pId, vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1193
    }
1194
  }
1195

1196
  return code;
1✔
1197
}
1198

1199
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
1✔
1200
  int64_t                now = taosGetTimestampMs();
1✔
1201
  const char*            id = pTask->id.idStr;
1✔
1202
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1✔
1203
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
1✔
1204

1205
  if (!pInfo->dispatchTrigger) {
1!
1206
    return false;
×
1207
  }
1208

1209
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
1✔
1210
  for (int32_t i = 0; i < num; ++i) {
1!
1211
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
1✔
1212
    if (pSendInfo == NULL) {
1!
1213
      stError("s-task:%s invalid index in dispatch-trigger list, index:%d, size:%d, ignore and continue", id, i, num);
×
1214
      continue;
×
1215
    }
1216

1217
    if (pSendInfo->nodeId != downstreamNodeId) {
1!
1218
      continue;
×
1219
    }
1220

1221
    // has send trigger msg to downstream node,
1222
    double before = (now - pSendInfo->sendTs) / 1000.0;
1✔
1223
    if (pSendInfo->recved) {
1!
1224
      stWarn("s-task:%s checkpoint-trigger msg already send at:%" PRId64
×
1225
             "(%.2fs before) and recv confirmed by downstream:0x%x, checkpointId:%" PRId64 ", transId:%d",
1226
             id, pSendInfo->sendTs, before, pSendInfo->taskId, pInfo->activeId, pInfo->transId);
1227
    } else {
1228
      stWarn("s-task:%s checkpoint-trigger already send at:%" PRId64 "(%.2fs before), checkpointId:%" PRId64
1!
1229
             ", transId:%d",
1230
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1231
    }
1232

1233
    return true;
1✔
1234
  }
1235

1236
  return false;
×
1237
}
1238

1239
bool streamTaskAlreadySendTrigger(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 (pStatus.state != TASK_STATUS__CK) {
1!
1246
    return false;
×
1247
  }
1248

1249
  streamMutexLock(&pInfo->lock);
1✔
1250
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
1✔
1251
  streamMutexUnlock(&pInfo->lock);
1✔
1252

1253
  return send;
1✔
1254
}
1255

1256
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
2✔
1257
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
2✔
1258

1259
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2✔
1260
    *pTotal = 1;
1✔
1261
  } else {
1262
    *pTotal = streamTaskGetNumOfUpstream(pTask);
1✔
1263
  }
1264
}
2✔
1265

1266
// record the dispatch checkpoint trigger info in the list
1267
// memory insufficient may cause the stream computing stopped
1268
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
1,406✔
1269
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1,406✔
1270
  int64_t                now = taosGetTimestampMs();
1,407✔
1271
  int32_t                code = 0;
1,407✔
1272

1273
  streamMutexLock(&pInfo->lock);
1,407✔
1274

1275
  if (sendingChkptId > pInfo->failedId) {
1,407!
1276
    pInfo->dispatchTrigger = true;
1,407✔
1277
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
1,407✔
1278
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
113✔
1279

1280
      STaskTriggerSendInfo p = {
113✔
1281
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
113✔
1282
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
113✔
1283
      if (px == NULL) {  // pause the stream task, if memory not enough
113!
1284
        code = terrno;
×
1285
      }
1286
    } else {
1287
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
5,633✔
1288
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
4,339✔
1289
        if (pVgInfo == NULL) {
4,339!
1290
          continue;
×
1291
        }
1292

1293
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
4,339✔
1294
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
4,339✔
1295
        if (px == NULL) {  // pause the stream task, if memory not enough
4,339!
1296
          code = terrno;
×
1297
          break;
×
1298
        }
1299
      }
1300
    }
1301
  }
1302

1303
  streamMutexUnlock(&pInfo->lock);
1,407✔
1304

1305
  return code;
1,407✔
1306
}
1307

1308
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
4,423✔
1309
  int32_t num = 0;
4,423✔
1310
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
20,169✔
1311
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
15,746✔
1312
    if (p == NULL) {
15,746!
1313
      continue;
×
1314
    }
1315

1316
    if (p->recved) {
15,746✔
1317
      num++;
10,084✔
1318
    }
1319
  }
1320
  return num;
4,423✔
1321
}
1322

1323
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
4,423✔
1324
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
4,423✔
1325

1326
  int64_t now = taosGetTimestampMs();
4,423✔
1327
  int32_t taskId = 0;
4,423✔
1328
  int32_t total = streamTaskGetNumOfDownstream(pTask);
4,423✔
1329
  bool    alreadyRecv = false;
4,423✔
1330

1331
  streamMutexLock(&pInfo->lock);
4,423✔
1332

1333
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
10,085!
1334
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
10,085✔
1335
    if (p == NULL) {
10,085!
1336
      continue;
×
1337
    }
1338

1339
    if (p->nodeId == vgId) {
10,085✔
1340
      if (p->recved) {
4,423!
1341
        stWarn("s-task:%s already recv checkpoint-trigger msg rsp from vgId:%d down:0x%x %.2fs ago, req send:%" PRId64
×
1342
               " discard",
1343
               pTask->id.idStr, vgId, p->taskId, (now - p->recvTs) / 1000.0, p->sendTs);
1344
        alreadyRecv = true;
×
1345
      } else {
1346
        p->recved = true;
4,423✔
1347
        p->recvTs = taosGetTimestampMs();
4,423✔
1348
        taskId = p->taskId;
4,423✔
1349
      }
1350
      break;
4,423✔
1351
    }
1352
  }
1353

1354
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
4,423✔
1355
  streamMutexUnlock(&pInfo->lock);
4,423✔
1356

1357
  if (taskId == 0) {
4,423!
1358
    stError("s-task:%s recv invalid trigger-dispatch confirm, vgId:%d", pTask->id.idStr, vgId);
×
1359
  } else {
1360
    if (!alreadyRecv) {
4,423!
1361
      stDebug("s-task:%s set downstream:0x%x(vgId:%d) checkpoint-trigger dispatch confirmed, total confirmed:%d/%d",
4,423✔
1362
              pTask->id.idStr, taskId, vgId, numOfConfirmed, total);
1363
    }
1364
  }
1365
}
4,423✔
1366

1367
int32_t uploadCheckpointToS3(const char* id, const char* path) {
1✔
1368
  int32_t code = 0;
1✔
1369
  int32_t nBytes = 0;
1✔
1370
  /*
1371
  if (s3Init() != 0) {
1372
    return TSDB_CODE_THIRDPARTY_ERROR;
1373
  }
1374
  */
1375
  TdDirPtr pDir = taosOpenDir(path);
1✔
1376
  if (pDir == NULL) {
1!
1377
    return terrno;
×
1378
  }
1379

1380
  TdDirEntryPtr de = NULL;
1✔
1381
  while ((de = taosReadDir(pDir)) != NULL) {
5✔
1382
    char* name = taosGetDirEntryName(de);
4✔
1383
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
4!
1384

1385
    char filename[PATH_MAX] = {0};
2✔
1386
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
2!
1387
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
1388
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1389
        code = TSDB_CODE_OUT_OF_RANGE;
×
1390
        break;
×
1391
      }
1392
    } else {
1393
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
2✔
1394
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
2!
1395
        code = TSDB_CODE_OUT_OF_RANGE;
×
1396
        break;
×
1397
      }
1398
    }
1399

1400
    char object[PATH_MAX] = {0};
2✔
1401
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
2✔
1402
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
2!
1403
      code = TSDB_CODE_OUT_OF_RANGE;
×
1404
      break;
×
1405
    }
1406

1407
    code = tcsPutObjectFromFile2(filename, object, 0);
2✔
1408
    if (code != 0) {
2!
1409
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
2!
1410
    } else {
1411
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1412
    }
1413
  }
1414

1415
  int32_t ret = taosCloseDir(&pDir);
1✔
1416
  if (code == 0 && ret != 0) {
1!
1417
    code = ret;
×
1418
  }
1419

1420
  return code;
1✔
1421
}
1422

1423
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
1✔
1424
  int32_t nBytes;
1425
  int32_t cap = strlen(id) + strlen(dstName) + 16;
1✔
1426

1427
  char* buf = taosMemoryCalloc(1, cap);
1!
1428
  if (buf == NULL) {
1!
1429
    return terrno;
×
1430
  }
1431

1432
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
1✔
1433
  if (nBytes <= 0 || nBytes >= cap) {
1!
1434
    taosMemoryFree(buf);
×
1435
    return TSDB_CODE_OUT_OF_RANGE;
×
1436
  }
1437
  int32_t code = tcsGetObjectToFile(buf, dstName);
1✔
1438
  if (code != 0) {
1!
1439
    taosMemoryFree(buf);
1!
1440
    return TAOS_SYSTEM_ERROR(errno);
1✔
1441
  }
1442
  taosMemoryFree(buf);
×
1443
  return 0;
×
1444
}
1445

1446
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
2,755✔
1447
  if (strlen(tsSnodeAddress) != 0) {
2,755✔
1448
    return DATA_UPLOAD_RSYNC;
1✔
1449
  } else if (tsS3StreamEnabled) {
2,754!
1450
    return DATA_UPLOAD_S3;
×
1451
  } else {
1452
    return DATA_UPLOAD_DISABLE;
2,754✔
1453
  }
1454
}
1455

1456
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
2✔
1457
  int32_t code = 0;
2✔
1458
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
2!
1459
    stError("invalid parameters in upload checkpoint, %s", id);
1!
1460
    return TSDB_CODE_INVALID_CFG;
1✔
1461
  }
1462

1463
  if (strlen(tsSnodeAddress) != 0) {
1!
1464
    code = uploadByRsync(id, path, checkpointId);
×
1465
    if (code != 0) {
×
1466
      return TAOS_SYSTEM_ERROR(errno);
×
1467
    }
1468
  } else if (tsS3StreamEnabled) {
1!
1469
    return uploadCheckpointToS3(id, path);
×
1470
  }
1471

1472
  return 0;
1✔
1473
}
1474

1475
// fileName:  CURRENT
1476
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
1✔
1477
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
1!
1478
    stError("down load checkpoint data parameters invalid");
×
1479
    return TSDB_CODE_INVALID_PARA;
×
1480
  }
1481

1482
  if (strlen(tsSnodeAddress) != 0) {
1!
1483
    return 0;
×
1484
  } else if (tsS3StreamEnabled) {
1!
1485
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1486
  }
1487

1488
  return 0;
1✔
1489
}
1490

1491
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
1✔
1492
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
1!
1493
    stError("down checkpoint data parameters invalid");
×
1494
    return -1;
×
1495
  }
1496

1497
  if (strlen(tsSnodeAddress) != 0) {
1!
1498
    return downloadByRsync(id, path, checkpointId);
1✔
1499
  } else if (tsS3StreamEnabled) {
×
1500
    return tcsGetObjectsByPrefix(id, path);
×
1501
  }
1502

1503
  return 0;
×
1504
}
1505

1506
#ifdef BUILD_NO_CALL
1507
int32_t deleteCheckpoint(const char* id) {
1508
  if (id == NULL || strlen(id) == 0) {
1509
    stError("deleteCheckpoint parameters invalid");
1510
    return TSDB_CODE_INVALID_PARA;
1511
  }
1512
  if (strlen(tsSnodeAddress) != 0) {
1513
    return deleteRsync(id);
1514
  } else if (tsS3StreamEnabled) {
1515
    tcsDeleteObjectsByPrefix(id);
1516
  }
1517
  return 0;
1518
}
1519
#endif
1520

1521
int32_t deleteCheckpointFile(const char* id, const char* name) {
1✔
1522
  char object[128] = {0};
1✔
1523

1524
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
1✔
1525
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
1!
1526
    return TSDB_CODE_OUT_OF_RANGE;
×
1527
  }
1528

1529
  char*   tmp = object;
1✔
1530
  int32_t code = tcsDeleteObjects((const char**)&tmp, 1);
1✔
1531
  if (code != 0) {
1!
1532
    return TSDB_CODE_THIRDPARTY_ERROR;
1✔
1533
  }
1534
  return code;
×
1535
}
1536

1537
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
125✔
1538
  streamMutexLock(&pTask->lock);
125✔
1539
  ETaskStatus p = streamTaskGetStatus(pTask).state;
125✔
1540
  //  if (pInfo->alreadySendChkptId == true) {
1541
  //    stDebug("s-task:%s already start to consensus-checkpointId, not start again before it completed", id);
1542
  //    streamMutexUnlock(&pTask->lock);
1543
  //    return TSDB_CODE_SUCCESS;
1544
  //  } else {
1545
  //    pInfo->alreadySendChkptId = true;
1546
  //  }
1547
  //
1548
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
125✔
1549
  streamMutexUnlock(&pTask->lock);
125✔
1550

1551
  if (pTask->pBackend != NULL) {
125!
1552
    streamFreeTaskState(pTask, p);
×
1553
    pTask->pBackend = NULL;
×
1554
  }
1555
  return 0;
125✔
1556
}
1557

1558
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
31✔
1559
  int32_t code = 0;
31✔
1560
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
31✔
1561
    return code;
17✔
1562
  }
1563

1564
  streamMutexLock(&pTask->lock);
14✔
1565
  SStreamTaskState p = streamTaskGetStatus(pTask);
14✔
1566
  if (p.state == TASK_STATUS__CK) {
14!
1567
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1568
  }
1569
  streamMutexUnlock(&pTask->lock);
14✔
1570

1571
  return code;
14✔
1572
}
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