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

taosdata / TDengine / #3628

03 Mar 2025 05:52AM UTC coverage: 63.79% (+0.2%) from 63.596%
#3628

push

travis-ci

web-flow
Merge pull request #29946 from taosdata/fix/remove-sync-heartbeat-lock

fix: remove sync heartbeat lock

149328 of 300024 branches covered (49.77%)

Branch coverage included in aggregate %.

100 of 184 new or added lines in 14 files covered. (54.35%)

625 existing lines in 112 files now uncovered.

233817 of 300609 relevant lines covered (77.78%)

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

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

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

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

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

76
  *pRes = pChkpoint;
6,273✔
77

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

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

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

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

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

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
11,153✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
11,153✔
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,152✔
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,151✔
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,150✔
281
    if (pActiveInfo->activeId != checkpointId) {
8,071✔
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,070✔
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,069✔
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,404✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,363✔
299
          if (p == NULL) {
8,380!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

303
          if (p->upstreamTaskId == pBlock->srcTaskId) {
8,380✔
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,139✔
315
}
316

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

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

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

334
  streamMutexLock(&pTask->lock);
11,143✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
11,152✔
336
  streamMutexUnlock(&pTask->lock);
11,136✔
337
  if (code) {
11,150!
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,150✔
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,150✔
351
    pActiveInfo->activeId = checkpointId;
3,066✔
352
    pActiveInfo->transId = transId;
3,066✔
353

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

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

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

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

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

447
  return code;
6,084✔
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,038✔
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,038✔
455
  int32_t size = taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
8,038✔
456
  for (int32_t i = 0; i < size; ++i) {
16,375✔
457
    STaskDownstreamReadyInfo* p = taosArrayGet(pInfo->pCheckpointReadyRecvList, i);
8,337✔
458
    if (p == NULL) {
8,337!
459
      return TSDB_CODE_INVALID_PARA;
×
460
    }
461

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

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

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

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

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

536
  return code;
8,038✔
537
}
538

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

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

561
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
32,774✔
562
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
24,748✔
563
    if (pReadyInfo == NULL) {
24,748!
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) {
24,748✔
570
      numOfConfirmed += 1;
16,387✔
571
    }
572
  }
573

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

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

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

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

587
  streamMutexLock(&pInfo->lock);
5,269✔
588
  streamTaskClearActiveInfo(pInfo);
5,299✔
589
  if (clearChkpReadyMsg) {
5,285!
590
    streamClearChkptReadyMsg(pInfo);
5,286✔
591
  }
592
  streamMutexUnlock(&pInfo->lock);
5,283✔
593

594
  stDebug("s-task:%s clear active checkpointInfo, failed checkpointId:%" PRId64 ", latest checkpointId:%" PRId64,
5,297✔
595
          pTask->id.idStr, pInfo->failedId, pTask->chkInfo.checkpointId);
596
}
5,297✔
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,505✔
601
                                           bool* pContinue) {
602
  SStreamMeta*     pMeta = pTask->pMeta;
5,505✔
603
  int32_t          vgId = pMeta->vgId;
5,505✔
604
  int32_t          code = 0;
5,505✔
605
  const char*      id = pTask->id.idStr;
5,505✔
606
  SCheckpointInfo* pInfo = &pTask->chkInfo;
5,505✔
607

608
  *pContinue = true;
5,505✔
609

610
  // not update the checkpoint info if the checkpointId is less than the failed checkpointId
611
  if (pReq->checkpointId < pInfo->pActiveInfo->failedId) {
5,505!
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,505✔
622
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
224✔
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) {
224✔
629
          code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
110✔
630

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

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

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

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

647
  if (!restored) {  // during restore procedure, do update checkpoint-info
5,280✔
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,274!
654
        (pMeta->role == NODE_ROLE_FOLLOWER)) {
29✔
655
      stDebug("s-task:%s vgId:%d status:%s role:%d start to update the checkpoint-info, checkpointId:%" PRId64
5,269✔
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
5!
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,561!
668
                pInfo->processedVer <= pReq->checkpointVer);
5,280!
669

670
  if (!valid) {
5,281!
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,279✔
693
}
694

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

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

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

711
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
5,278✔
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,277✔
715
    pInfo->checkpointId = pReq->checkpointId;
5,272✔
716
    pInfo->checkpointVer = pReq->checkpointVer;
5,272✔
717
    pInfo->checkpointTime = pReq->checkpointTs;
5,272✔
718

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

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

726
  if (pReq->dropRelHTask) {
5,286✔
727
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
3,437✔
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,287✔
733
          streamTaskGetStatusStr(pTask->status.taskStatus), streamTaskGetStatus(pTask).name);
734

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

737
  code = streamMetaSaveTask(pMeta, pTask);
5,287✔
738
  streamMutexUnlock(&pTask->lock);
5,281✔
739

740
  if (code != TSDB_CODE_SUCCESS) {
5,287!
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,287✔
748
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
3,440✔
749
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
3,439✔
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,287✔
755
  return TSDB_CODE_SUCCESS;
5,288✔
756
}
757

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

761
  if (failedId <= 0) {
15✔
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) {
13✔
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
12!
770
              " prev failedId:%" PRId64,
771
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
772
      pInfo->failedId = failedId;
12✔
773
    }
774
  }
775
}
15✔
776

777
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
605✔
778
  streamMutexLock(&pTask->lock);
605✔
779
  ETaskStatus status = streamTaskGetStatus(pTask).state;
606✔
780
  if (status == TASK_STATUS__CK) {
606!
781
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
782
  }
783
  streamMutexUnlock(&pTask->lock);
606✔
784
}
605✔
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,021✔
884
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
6,021✔
885
  if (type == DATA_UPLOAD_DISABLE) {
6,020✔
886
    stDebug("s-task:%s not allowed to upload checkpoint data", pTask->id.idStr);
6,019✔
887
    return 0;
6,019✔
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,018✔
909
  int32_t      code = TSDB_CODE_SUCCESS;
6,018✔
910
  int64_t      startTs = pTask->chkInfo.startTs;
6,018✔
911
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
6,018✔
912
  const char*  id = pTask->id.idStr;
6,018✔
913
  bool         dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
6,018✔
914
  SStreamMeta* pMeta = pTask->pMeta;
6,018✔
915

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

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

927
  // TODO: monitoring the checkpoint-source msg
928
  // send check point response to upstream task
929
  if (code == TSDB_CODE_SUCCESS) {
6,019!
930
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,019✔
931
      code = streamTaskSendCheckpointSourceRsp(pTask);
2,977✔
932
    } else {
933
      code = streamTaskSendCheckpointReadyMsg(pTask);
3,042✔
934
    }
935

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

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

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

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

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

973
  return code;
6,021✔
974
}
975

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

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

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

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

1005
  return 0;
1✔
1006
}
1007

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

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

1018
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
2✔
1019
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
1✔
1020

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

1028
      if (pInfo->nodeId == pReady->upstreamNodeId) {
1!
1029
        recved = true;
×
1030
        break;
×
1031
      }
1032
    }
1033

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

1045
  *ppNotSendList = pNotSendList;
1✔
1046
  return 0;
1✔
1047
}
1048

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

1056
  int32_t code = doChkptStatusCheck(pTask, param);
74✔
1057
  if (code) {
74✔
1058
    return code;
73✔
1059
  }
1060

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

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

1075
  return code;
1✔
1076
}
1077

1078
static void doCleanup(SStreamTask* pTask, SArray* pList) {
109,856✔
1079
  streamMetaReleaseTask(pTask->pMeta, pTask);
109,856✔
1080
  taosArrayDestroy(pList);
109,856✔
1081
}
109,856✔
1082

1083
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
109,856✔
1084
  int32_t      code = 0;
109,856✔
1085
  int32_t      numOfNotSend = 0;
109,856✔
1086
  SArray*      pNotSendList = NULL;
109,856✔
1087
  int64_t      taskRefId = *(int64_t*)param;
109,856✔
1088
  int64_t      now = taosGetTimestampMs();
109,856✔
1089

1090
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
109,856✔
1091
  if (pTask == NULL) {
109,856!
UNCOV
1092
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
×
UNCOV
1093
    streamTaskFreeRefId(param);
×
1094
    return;
109,856✔
1095
  }
1096

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

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

1110
  // check the status every 100ms
1111
  if (streamTaskShouldStop(pTask)) {
109,856!
UNCOV
1112
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
UNCOV
1113
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
×
UNCOV
1114
    doCleanup(pTask, pNotSendList);
×
UNCOV
1115
    return;
×
1116
  }
1117

1118
  if (++pTmrInfo->activeCounter < 50) {
109,856✔
1119
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
108,271✔
1120
                   "trigger-recv-monitor");
1121
    doCleanup(pTask, pNotSendList);
108,271✔
1122
    return;
108,271✔
1123
  }
1124

1125
  pTmrInfo->activeCounter = 0;
1,585✔
1126
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
1,585✔
1127

1128
  streamMutexLock(&pTask->lock);
1,585✔
1129
  SStreamTaskState state = streamTaskGetStatus(pTask);
1,585✔
1130
  streamMutexUnlock(&pTask->lock);
1,585✔
1131

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

1140
  streamMutexLock(&pActiveInfo->lock);
73✔
1141
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
73✔
1142
  streamMutexUnlock(&pActiveInfo->lock);
73✔
1143

1144
  if (code != TSDB_CODE_SUCCESS) {
73!
1145
    doCleanup(pTask, pNotSendList);
73✔
1146
    return;
73✔
1147
  }
1148

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

1160
  doCleanup(pTask, pNotSendList);
×
1161
}
1162

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

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

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

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

1185
    int32_t  ret = 0;
1✔
1186
    int32_t  tlen = 0;
1✔
1187
    void*    buf = NULL;
1✔
1188
    SRpcMsg  rpcMsg = {0};
1✔
1189
    SEncoder encoder;
1190

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

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

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

1209
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
1✔
1210
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
1✔
1211

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

1221
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
1✔
1222

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

1233
  return code;
1✔
1234
}
1235

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

1242
  if (!pInfo->dispatchTrigger) {
1!
1243
    return false;
×
1244
  }
1245

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

1254
    if (pSendInfo->nodeId != downstreamNodeId) {
1!
1255
      continue;
×
1256
    }
1257

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

1270
    return true;
1✔
1271
  }
1272

1273
  return false;
×
1274
}
1275

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

1282
  if (pStatus.state != TASK_STATUS__CK) {
1!
1283
    return false;
×
1284
  }
1285

1286
  streamMutexLock(&pInfo->lock);
1✔
1287
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
1✔
1288
  streamMutexUnlock(&pInfo->lock);
1✔
1289

1290
  return send;
1✔
1291
}
1292

1293
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
2✔
1294
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
2✔
1295

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

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

1310
  streamMutexLock(&pInfo->lock);
3,093✔
1311

1312
  if (sendingChkptId > pInfo->failedId) {
3,094✔
1313
    pInfo->dispatchTrigger = true;
3,093✔
1314
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
3,093✔
1315
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
532✔
1316

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

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

1340
  streamMutexUnlock(&pInfo->lock);
3,085✔
1341

1342
  return code;
3,092✔
1343
}
1344

1345
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
8,143✔
1346
  int32_t num = 0;
8,143✔
1347
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
33,206✔
1348
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
25,063✔
1349
    if (p == NULL) {
25,063!
1350
      continue;
×
1351
    }
1352

1353
    if (p->recved) {
25,063✔
1354
      num++;
16,603✔
1355
    }
1356
  }
1357
  return num;
8,143✔
1358
}
1359

1360
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
8,143✔
1361
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,143✔
1362

1363
  int64_t now = taosGetTimestampMs();
8,143✔
1364
  int32_t taskId = 0;
8,143✔
1365
  int32_t total = streamTaskGetNumOfDownstream(pTask);
8,143✔
1366
  bool    alreadyRecv = false;
8,143✔
1367

1368
  streamMutexLock(&pInfo->lock);
8,143✔
1369

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

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

1391
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
8,143✔
1392
  streamMutexUnlock(&pInfo->lock);
8,143✔
1393

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

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

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

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

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

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

1452
  int32_t ret = taosCloseDir(&pDir);
1✔
1453
  if (code == 0 && ret != 0) {
1!
1454
    code = ret;
×
1455
  }
1456

1457
  return code;
1✔
1458
}
1459

1460
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
1✔
1461
  int32_t nBytes;
1462
  int32_t cap = strlen(id) + strlen(dstName) + 16;
1✔
1463

1464
  char* buf = taosMemoryCalloc(1, cap);
1!
1465
  if (buf == NULL) {
1!
1466
    return terrno;
×
1467
  }
1468

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

1483
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
6,030✔
1484
  if (strlen(tsSnodeAddress) != 0) {
6,030✔
1485
    return DATA_UPLOAD_RSYNC;
1✔
1486
  } else if (tsS3StreamEnabled) {
6,029!
1487
    return DATA_UPLOAD_S3;
×
1488
  } else {
1489
    return DATA_UPLOAD_DISABLE;
6,029✔
1490
  }
1491
}
1492

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

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

1509
  return 0;
1✔
1510
}
1511

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

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

1525
  return 0;
1✔
1526
}
1527

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

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

1540
  return 0;
×
1541
}
1542

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

1558
int32_t deleteCheckpointFile(const char* id, const char* name) {
1✔
1559
  char object[128] = {0};
1✔
1560

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

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

1574
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
438✔
1575
  streamMutexLock(&pTask->lock);
438✔
1576
  ETaskStatus p = streamTaskGetStatus(pTask).state;
438✔
1577
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
438✔
1578
  streamMutexUnlock(&pTask->lock);
438✔
1579

1580
  if (pTask->pBackend != NULL) {
438!
1581
    streamFreeTaskState(pTask, p);
×
1582
    pTask->pBackend = NULL;
×
1583
  }
1584
  return 0;
438✔
1585
}
1586

1587
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
85✔
1588
  int32_t code = 0;
85✔
1589
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
85✔
1590
    return code;
44✔
1591
  }
1592

1593
  streamMutexLock(&pTask->lock);
41✔
1594
  SStreamTaskState p = streamTaskGetStatus(pTask);
41✔
1595
  if (p.state == TASK_STATUS__CK) {
41!
1596
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1597
  }
1598
  streamMutexUnlock(&pTask->lock);
41✔
1599

1600
  return code;
41✔
1601
}
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