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

taosdata / TDengine / #3597

05 Feb 2025 01:41AM UTC coverage: 63.546% (-0.02%) from 63.562%
#3597

push

travis-ci

web-flow
Merge pull request #29639 from taosdata/feat/3.0/TS-5795

Enh(tsdb):print fid while data file corrupted.

141230 of 285630 branches covered (49.45%)

Branch coverage included in aggregate %.

2 of 4 new or added lines in 1 file covered. (50.0%)

398 existing lines in 102 files now uncovered.

220015 of 282846 relevant lines covered (77.79%)

18937864.43 hits per line

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

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

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

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

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

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

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

76
  *pRes = pChkpoint;
6,381✔
77

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

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

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

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

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

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
11,182✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
11,182✔
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,181✔
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,180✔
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,179✔
281
    if (pActiveInfo->activeId != checkpointId) {
8,083✔
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,082✔
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,081✔
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,336✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,319✔
299
          if (p == NULL) {
8,330!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

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

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

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

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

334
  streamMutexLock(&pTask->lock);
11,180✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
11,184✔
336
  streamMutexUnlock(&pTask->lock);
11,163✔
337
  if (code) {
11,189!
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,189✔
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,189✔
351
    pActiveInfo->activeId = checkpointId;
3,092✔
352
    pActiveInfo->transId = transId;
3,092✔
353

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

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

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

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

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

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

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

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

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

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

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

536
  return code;
8,075✔
537
}
538

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

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

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

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

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

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

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

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

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

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

605
  streamMutexLock(&pTask->lock);
5,418✔
606

607
  if (pReq->checkpointId <= pInfo->checkpointId) {
5,426✔
608
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
110✔
609
            " no need to update checkpoint info, updated checkpointId:%" PRId64 " Ver:%" PRId64 " transId:%d ignored",
610
            id, vgId, pInfo->checkpointId, pInfo->checkpointVer, pReq->checkpointId, pReq->checkpointVer,
611
            pReq->transId);
612
    streamMutexUnlock(&pTask->lock);
110✔
613

614
    {  // destroy the related fill-history tasks
615
      // drop task should not in the meta-lock, and drop the related fill-history task now
616
      if (pReq->dropRelHTask) {
110✔
617
        code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
106✔
618
        int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
106✔
619
        stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped in update checkpointInfo, remain tasks:%d",
106✔
620
                id, vgId, pReq->taskId, numOfTasks);
621
      }
622

623
      if (pReq->dropRelHTask) {
110✔
624
        code = streamMetaCommit(pMeta);
106✔
625
      }
626
    }
627

628
    // always return true
629
    return TSDB_CODE_SUCCESS;
110✔
630
  }
631

632
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
5,316✔
633

634
  if (!restored) {  // during restore procedure, do update checkpoint-info
5,316✔
635
    stDebug("s-task:%s vgId:%d status:%s update the checkpoint-info during restore, checkpointId:%" PRId64 "->%" PRId64
6!
636
            " checkpointVer:%" PRId64 "->%" PRId64 " checkpointTs:%" PRId64 "->%" PRId64,
637
            id, vgId, pStatus.name, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer, pReq->checkpointVer,
638
            pInfo->checkpointTime, pReq->checkpointTs);
639
  } else {  // not in restore status, must be in checkpoint status
640
    if ((pStatus.state == TASK_STATUS__CK) || (pMeta->role == NODE_ROLE_FOLLOWER)) {
5,310!
641
      stDebug("s-task:%s vgId:%d status:%s start to update the checkpoint-info, checkpointId:%" PRId64 "->%" PRId64
5,310✔
642
              " checkpointVer:%" PRId64 "->%" PRId64 " checkpointTs:%" PRId64 "->%" PRId64,
643
              id, vgId, pStatus.name, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer,
644
              pReq->checkpointVer, pInfo->checkpointTime, pReq->checkpointTs);
645
    } else {
646
      stDebug("s-task:%s vgId:%d status:%s NOT update the checkpoint-info, checkpointId:%" PRId64 "->%" PRId64
×
647
              " checkpointVer:%" PRId64 "->%" PRId64,
648
              id, vgId, pStatus.name, pInfo->checkpointId, pReq->checkpointId, pInfo->checkpointVer,
649
              pReq->checkpointVer);
650
    }
651
  }
652

653
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
10,628!
654
                pInfo->processedVer <= pReq->checkpointVer);
5,315✔
655

656
  if (!valid) {
5,313!
657
    stFatal("s-task:%s invalid checkpointId update info recv, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
658
            " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64 " discard it",
659
            id, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
660
            pReq->checkpointVer);
661
    streamMutexUnlock(&pTask->lock);
×
662
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
663
  }
664

665
  // update only it is in checkpoint status, or during restore procedure.
666
  if ((pStatus.state == TASK_STATUS__CK) || (!restored) || (pMeta->role == NODE_ROLE_FOLLOWER)) {
5,313!
667
    pInfo->checkpointId = pReq->checkpointId;
5,313✔
668
    pInfo->checkpointVer = pReq->checkpointVer;
5,313✔
669
    pInfo->checkpointTime = pReq->checkpointTs;
5,313✔
670

671
    if (restored && (pMeta->role == NODE_ROLE_LEADER)) {
5,313✔
672
      code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
5,281✔
673
    }
674
  }
675

676
  streamTaskClearCheckInfo(pTask, true);
5,306✔
677

678
  if (pReq->dropRelHTask) {
5,316✔
679
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
3,455✔
680
            pReq->taskId, vgId, pReq->hTaskId);
681
    CLEAR_RELATED_FILLHISTORY_TASK(pTask);
3,453✔
682
  }
683

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

687
  pTask->status.taskStatus = TASK_STATUS__READY;
5,314✔
688

689
  code = streamMetaSaveTask(pMeta, pTask);
5,314✔
690
  streamMutexUnlock(&pTask->lock);
5,308✔
691

692
  if (code != TSDB_CODE_SUCCESS) {
5,312!
693
    stError("s-task:%s vgId:%d failed to save task info after do checkpoint, checkpointId:%" PRId64 ", since %s", id,
×
694
            vgId, pReq->checkpointId, terrstr());
695
    return TSDB_CODE_SUCCESS;
×
696
  }
697

698
  // drop task should not in the meta-lock, and drop the related fill-history task now
699
  if (pReq->dropRelHTask) {
5,312✔
700
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
3,454✔
701
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
3,454✔
702
    stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
3,454✔
703
            (int32_t)pReq->hTaskId, numOfTasks);
704
  }
705

706
  code = streamMetaCommit(pMeta);
5,312✔
707
  return TSDB_CODE_SUCCESS;
5,319✔
708
}
709

710
void streamTaskSetFailedCheckpointId(SStreamTask* pTask, int64_t failedId) {
10✔
711
  struct SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
10✔
712

713
  if (failedId <= 0) {
10✔
714
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
6!
715
           " activeId:%" PRId64,
716
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
717
  } else {
718
    if (failedId <= pInfo->failedId) {
4✔
719
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
1!
720
    } else {
721
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
3✔
722
              " prev failedId:%" PRId64,
723
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
724
      pInfo->failedId = failedId;
3✔
725
    }
726
  }
727
}
10✔
728

729
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
612✔
730
  streamMutexLock(&pTask->lock);
612✔
731
  ETaskStatus status = streamTaskGetStatus(pTask).state;
614✔
732
  if (status == TASK_STATUS__CK) {
613✔
733
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
6✔
734
  }
735
  streamMutexUnlock(&pTask->lock);
613✔
736
}
613✔
737

738
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
1✔
739
  int32_t code = 0;
1✔
740
  int32_t cap = strlen(path) + 64;
1✔
741

742
  char* filePath = taosMemoryCalloc(1, cap);
1!
743
  if (filePath == NULL) {
1!
744
    return terrno;
×
745
  }
746

747
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
1✔
748
  if (nBytes <= 0 || nBytes >= cap) {
1!
749
    taosMemoryFree(filePath);
×
750
    return TSDB_CODE_OUT_OF_RANGE;
×
751
  }
752

753
  code = downloadCheckpointDataByName(id, "META", filePath);
1✔
754
  if (code != 0) {
1!
755
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
756
    taosMemoryFree(filePath);
×
757
    return code;
×
758
  }
759

760
  code = remoteChkpGetDelFile(filePath, list);
1✔
761
  if (code != 0) {
1!
762
    stError("%s chkp failed to get to del:%s", id, filePath);
1!
763
    taosMemoryFree(filePath);
1!
764
  }
765
  return 0;
1✔
766
}
767

768
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
3✔
769
  int32_t code = 0;
3✔
770
  char*   path = NULL;
3✔
771

772
  SStreamMeta* pMeta = pTask->pMeta;
3✔
773
  const char*  idStr = pTask->id.idStr;
3✔
774
  int64_t      now = taosGetTimestampMs();
3✔
775

776
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
3✔
777
  if (toDelFiles == NULL) {
3!
778
    return terrno;
×
779
  }
780

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

786
  if (type == DATA_UPLOAD_S3) {
3✔
787
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
1!
788
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
789
              tstrerror(code));
790
    }
791
  }
792

793
  if (code == TSDB_CODE_SUCCESS) {
3✔
794
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
2✔
795
    if (code == TSDB_CODE_SUCCESS) {
2✔
796
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
1!
797
    } else {
798
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
1!
799
              tstrerror(code));
800
    }
801
  }
802

803
  if (code == TSDB_CODE_SUCCESS) {
3✔
804
    int32_t size = taosArrayGetSize(toDelFiles);
1✔
805
    stDebug("s-task:%s remove redundant %d files", idStr, size);
1!
806

807
    for (int i = 0; i < size; i++) {
1!
808
      char* pName = taosArrayGetP(toDelFiles, i);
×
809
      code = deleteCheckpointFile(idStr, pName);
×
810
      if (code != 0) {
×
811
        stDebug("s-task:%s failed to remove file: %s", idStr, pName);
×
812
        break;
×
813
      }
814
    }
815

816
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
1!
817
  }
818

819
  taosArrayDestroyP(toDelFiles, NULL);
3✔
820
  double el = (taosGetTimestampMs() - now) / 1000.0;
3✔
821

822
  if (code == TSDB_CODE_SUCCESS) {
3✔
823
    stDebug("s-task:%s complete update checkpointId:%" PRId64 ", elapsed time:%.2fs remove local checkpoint data %s",
1!
824
            idStr, checkpointId, el, path);
825
    taosRemoveDir(path);
1✔
826
  } else {
827
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " keep local checkpoint data, elapsed time:%.2fs", idStr,
2!
828
            checkpointId, el);
829
  }
830

831
  taosMemoryFree(path);
3!
832
  return code;
3✔
833
}
834

835
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
6,124✔
836
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
6,124✔
837
  if (type == DATA_UPLOAD_DISABLE) {
6,124✔
838
    stDebug("s-task:%s not allowed to upload checkpoint data", pTask->id.idStr);
6,123✔
839
    return 0;
6,123✔
840
  }
841

842
  if (pTask == NULL || pTask->pBackend == NULL) {
1!
843
    return 0;
×
844
  }
845

846
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
1✔
847
  void*   pBackend = taskAcquireDb(dbRefId);
1✔
848
  if (pBackend == NULL) {
1!
849
    stError("s-task:%s failed to acquire db during update checkpoint data, failed to upload checkpointData",
×
850
            pTask->id.idStr);
851
    return -1;
×
852
  }
853

854
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
1✔
855
  taskReleaseDb(dbRefId);
1✔
856

857
  return code;
1✔
858
}
859

860
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
6,119✔
861
  int32_t      code = TSDB_CODE_SUCCESS;
6,119✔
862
  int64_t      startTs = pTask->chkInfo.startTs;
6,119✔
863
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
6,119✔
864
  const char*  id = pTask->id.idStr;
6,119✔
865
  bool         dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
6,119✔
866
  SStreamMeta* pMeta = pTask->pMeta;
6,121✔
867

868
  // sink task does not need to save the status, and generated the checkpoint
869
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
6,121✔
870
    stDebug("s-task:%s level:%d start gen checkpoint, checkpointId:%" PRId64, id, pTask->info.taskLevel, ckId);
3,296✔
871

872
    int64_t ver = pTask->chkInfo.processedVer;
3,296✔
873
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
3,296✔
874
    if (code != TSDB_CODE_SUCCESS) {
3,299!
875
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
876
    }
877
  }
878

879
  // TODO: monitoring the checkpoint-source msg
880
  // send check point response to upstream task
881
  if (code == TSDB_CODE_SUCCESS) {
6,123!
882
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,123✔
883
      code = streamTaskSendCheckpointSourceRsp(pTask);
3,034✔
884
    } else {
885
      code = streamTaskSendCheckpointReadyMsg(pTask);
3,089✔
886
    }
887

888
    if (code != TSDB_CODE_SUCCESS) {
6,124!
889
      // todo: let's retry send rsp to mnode, checkpoint-ready has monitor now
890
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%" PRId64 ", code:%s", id, ckId,
×
891
              tstrerror(code));
892
    }
893
  }
894

895
  if (code == TSDB_CODE_SUCCESS) {
6,124!
896
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
6,124✔
897
    if (code != TSDB_CODE_SUCCESS) {
6,124✔
898
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
1!
899
    }
900
  } else {
901
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
902
  }
903

904
  // TODO: monitoring the checkpoint-report msg
905
  // update the latest checkpoint info if all works are done successfully, for rsma, the pMsgCb is null.
906
  if (code == TSDB_CODE_SUCCESS) {
6,124✔
907
    if (pTask->pMsgCb != NULL) {
6,123✔
908
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
6,109✔
909
    }
910
  } else {  // clear the checkpoint info if failed
911
    // set failed checkpoint id before clear the checkpoint info
912
    streamMutexLock(&pTask->lock);
1✔
913
    streamTaskSetFailedCheckpointId(pTask, ckId);
1✔
914
    streamMutexUnlock(&pTask->lock);
1✔
915

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

920
  double el = (taosGetTimestampMs() - startTs) / 1000.0;
6,118✔
921
  stInfo("s-task:%s vgId:%d level:%d, checkpointId:%" PRId64 " ver:%" PRId64 " elapsed time:%.2fs, %s ", id,
6,118!
922
         pMeta->vgId, pTask->info.taskLevel, ckId, pTask->chkInfo.checkpointVer, el,
923
         (code == TSDB_CODE_SUCCESS) ? "succ" : "failed");
924

925
  return code;
6,124✔
926
}
927

928
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
3✔
929
  const char*            id = pTask->id.idStr;
3✔
930
  int32_t                vgId = pTask->pMeta->vgId;
3✔
931
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
3✔
932
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
3✔
933

934
  // checkpoint-trigger recv flag is set, quit
935
  if (pActiveInfo->allUpstreamTriggerRecv) {
3!
936
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
937
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
×
938
    return -1;
×
939
  }
940

941
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
3✔
942
    streamCleanBeforeQuitTmr(pTmrInfo, param);
2✔
943
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
2!
944
           ", quit",
945
           id, vgId, pTmrInfo->launchChkptId);
946
    return -1;
2✔
947
  }
948

949
  // active checkpoint info is cleared for now
950
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
1!
951
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
952
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from retrieve checkpoint-trigger send tmr", id,
×
953
           vgId);
954
    return -1;
×
955
  }
956

957
  return 0;
1✔
958
}
959

960
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
1✔
961
  const char*            id = pTask->id.idStr;
1✔
962
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
963

964
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
1✔
965
  if (pNotSendList == NULL) {
1!
966
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
967
    return terrno;
×
968
  }
969

970
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
2✔
971
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
1✔
972

973
    bool recved = false;
1✔
974
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
2✔
975
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
1✔
976
      if (pReady == NULL) {
1!
977
        continue;
×
978
      }
979

980
      if (pInfo->nodeId == pReady->upstreamNodeId) {
1!
981
        recved = true;
×
982
        break;
×
983
      }
984
    }
985

986
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
1!
987
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
1✔
988
      void* px = taosArrayPush(pNotSendList, pInfo);
1✔
989
      if (px == NULL) {
1!
990
        stError("s-task:%s failed to record not send info, code: out of memory", id);
×
991
        taosArrayDestroy(pNotSendList);
×
992
        return terrno;
×
993
      }
994
    }
995
  }
996

997
  *ppNotSendList = pNotSendList;
1✔
998
  return 0;
1✔
999
}
1000

1001
int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray** ppNotSendList) {
3✔
1002
  const char*            id = pTask->id.idStr;
3✔
1003
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
3✔
1004
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
3✔
1005
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
3✔
1006
  int32_t                vgId = pTask->pMeta->vgId;
3✔
1007

1008
  int32_t code = doChkptStatusCheck(pTask, param);
3✔
1009
  if (code) {
3✔
1010
    return code;
2✔
1011
  }
1012

1013
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
1✔
1014
  if (code) {
1!
1015
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1016
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1017
    return code;
×
1018
  }
1019

1020
  // do send retrieve checkpoint trigger msg to upstream
1021
  code = doSendRetrieveTriggerMsg(pTask, *ppNotSendList);
1✔
1022
  if (code) {
1!
1023
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1024
    code = 0;
×
1025
  }
1026

1027
  return code;
1✔
1028
}
1029

1030
static void doCleanup(SStreamTask* pTask, SArray* pList) {
109,644✔
1031
  streamMetaReleaseTask(pTask->pMeta, pTask);
109,644✔
1032
  taosArrayDestroy(pList);
109,644✔
1033
}
109,644✔
1034

1035
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
109,654✔
1036
  int32_t      code = 0;
109,654✔
1037
  int32_t      numOfNotSend = 0;
109,654✔
1038
  SArray*      pNotSendList = NULL;
109,654✔
1039
  int64_t      taskRefId = *(int64_t*)param;
109,654✔
1040
  int64_t      now = taosGetTimestampMs();
109,654✔
1041

1042
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
109,654✔
1043
  if (pTask == NULL) {
109,654✔
1044
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
10!
1045
    streamTaskFreeRefId(param);
10✔
1046
    return;
109,654✔
1047
  }
1048

1049
  int32_t                vgId = pTask->pMeta->vgId;
109,644✔
1050
  const char*            id = pTask->id.idStr;
109,644✔
1051
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
109,644✔
1052
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
109,644✔
1053
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
109,644✔
1054

1055
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
109,644!
1056
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1057
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1058
    doCleanup(pTask, pNotSendList);
×
1059
    return;
×
1060
  }
1061

1062
  // check the status every 100ms
1063
  if (streamTaskShouldStop(pTask)) {
109,644✔
1064
    streamCleanBeforeQuitTmr(pTmrInfo, param);
4✔
1065
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
4!
1066
    doCleanup(pTask, pNotSendList);
4✔
1067
    return;
4✔
1068
  }
1069

1070
  if (++pTmrInfo->activeCounter < 50) {
109,640✔
1071
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
108,064✔
1072
                   "trigger-recv-monitor");
1073
    doCleanup(pTask, pNotSendList);
108,064✔
1074
    return;
108,064✔
1075
  }
1076

1077
  pTmrInfo->activeCounter = 0;
1,576✔
1078
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
1,576✔
1079

1080
  streamMutexLock(&pTask->lock);
1,576✔
1081
  SStreamTaskState state = streamTaskGetStatus(pTask);
1,576✔
1082
  streamMutexUnlock(&pTask->lock);
1,576✔
1083

1084
  if (state.state != TASK_STATUS__CK) {
1,576✔
1085
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1,574✔
1086
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
1,574✔
1087
            vgId, state.name);
1088
    doCleanup(pTask, pNotSendList);
1,574✔
1089
    return;
1,574✔
1090
  }
1091

1092
  streamMutexLock(&pActiveInfo->lock);
2✔
1093
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
2✔
1094
  streamMutexUnlock(&pActiveInfo->lock);
2✔
1095

1096
  if (code != TSDB_CODE_SUCCESS) {
2!
1097
    doCleanup(pTask, pNotSendList);
2✔
1098
    return;
2✔
1099
  }
1100

1101
  // check every 100ms
1102
  numOfNotSend = taosArrayGetSize(pNotSendList);
×
1103
  if (numOfNotSend > 0) {
×
1104
    stDebug("s-task:%s start to monitor checkpoint-trigger in 10s", id);
×
1105
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1106
                   "trigger-recv-monitor");
1107
  } else {
1108
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1109
    stDebug("s-task:%s all checkpoint-trigger recved, quit from monitor checkpoint-trigger tmr", id);
×
1110
  }
1111

1112
  doCleanup(pTask, pNotSendList);
×
1113
}
1114

1115
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
1✔
1116
  int32_t     code = 0;
1✔
1117
  int32_t     vgId = pTask->pMeta->vgId;
1✔
1118
  const char* pId = pTask->id.idStr;
1✔
1119
  int32_t     size = taosArrayGetSize(pNotSendList);
1✔
1120
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
1✔
1121
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
1✔
1122

1123
  if (size <= 0) {
1!
1124
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1125
    return code;
×
1126
  }
1127

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

1131
  for (int32_t i = 0; i < size; i++) {
2✔
1132
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
1✔
1133
    if (pUpstreamTask == NULL) {
1!
1134
      return TSDB_CODE_INVALID_PARA;
×
1135
    }
1136

1137
    int32_t  ret = 0;
1✔
1138
    int32_t  tlen = 0;
1✔
1139
    void*    buf = NULL;
1✔
1140
    SRpcMsg  rpcMsg = {0};
1✔
1141
    SEncoder encoder;
1142

1143
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
1✔
1144
                                    .downstreamTaskId = pTask->id.taskId,
1✔
1145
                                    .downstreamNodeId = vgId,
1146
                                    .upstreamTaskId = pUpstreamTask->taskId,
1✔
1147
                                    .upstreamNodeId = pUpstreamTask->nodeId,
1✔
1148
                                    .checkpointId = checkpointId};
1149

1150
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
1!
1151
    if (ret < 0) {
1!
1152
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1153
    }
1154

1155
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
1✔
1156
    if (buf == NULL) {
1!
1157
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1158
      continue;
×
1159
    }
1160

1161
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
1✔
1162
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
1✔
1163

1164
    tEncoderInit(&encoder, abuf, tlen);
1✔
1165
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
1!
1166
      rpcFreeCont(buf);
×
1167
      tEncoderClear(&encoder);
×
1168
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1169
      continue;
×
1170
    }
1171
    tEncoderClear(&encoder);
1✔
1172

1173
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
1✔
1174

1175
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
1✔
1176
    if (code == TSDB_CODE_SUCCESS) {
1!
1177
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
1!
1178
              vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1179
    } else {
1180
      stError("s-task:%s vgId:%d failed to send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64,
×
1181
              pId, vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1182
    }
1183
  }
1184

1185
  return code;
1✔
1186
}
1187

1188
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
1✔
1189
  int64_t                now = taosGetTimestampMs();
1✔
1190
  const char*            id = pTask->id.idStr;
1✔
1191
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1✔
1192
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
1✔
1193

1194
  if (!pInfo->dispatchTrigger) {
1!
1195
    return false;
×
1196
  }
1197

1198
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
1✔
1199
  for (int32_t i = 0; i < num; ++i) {
1!
1200
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
1✔
1201
    if (pSendInfo == NULL) {
1!
1202
      stError("s-task:%s invalid index in dispatch-trigger list, index:%d, size:%d, ignore and continue", id, i, num);
×
1203
      continue;
×
1204
    }
1205

1206
    if (pSendInfo->nodeId != downstreamNodeId) {
1!
1207
      continue;
×
1208
    }
1209

1210
    // has send trigger msg to downstream node,
1211
    double before = (now - pSendInfo->sendTs) / 1000.0;
1✔
1212
    if (pSendInfo->recved) {
1!
1213
      stWarn("s-task:%s checkpoint-trigger msg already send at:%" PRId64
×
1214
             "(%.2fs before) and recv confirmed by downstream:0x%x, checkpointId:%" PRId64 ", transId:%d",
1215
             id, pSendInfo->sendTs, before, pSendInfo->taskId, pInfo->activeId, pInfo->transId);
1216
    } else {
1217
      stWarn("s-task:%s checkpoint-trigger already send at:%" PRId64 "(%.2fs before), checkpointId:%" PRId64
1!
1218
             ", transId:%d",
1219
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1220
    }
1221

1222
    return true;
1✔
1223
  }
1224

1225
  return false;
×
1226
}
1227

1228
bool streamTaskAlreadySendTrigger(SStreamTask* pTask, int32_t downstreamNodeId) {
1✔
1229
  int64_t                now = taosGetTimestampMs();
1✔
1230
  const char*            id = pTask->id.idStr;
1✔
1231
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
1✔
1232
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
1✔
1233

1234
  if (pStatus.state != TASK_STATUS__CK) {
1!
1235
    return false;
×
1236
  }
1237

1238
  streamMutexLock(&pInfo->lock);
1✔
1239
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
1✔
1240
  streamMutexUnlock(&pInfo->lock);
1✔
1241

1242
  return send;
1✔
1243
}
1244

1245
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
2✔
1246
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
2✔
1247

1248
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2✔
1249
    *pTotal = 1;
1✔
1250
  } else {
1251
    *pTotal = streamTaskGetNumOfUpstream(pTask);
1✔
1252
  }
1253
}
2✔
1254

1255
// record the dispatch checkpoint trigger info in the list
1256
// memory insufficient may cause the stream computing stopped
1257
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
3,112✔
1258
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
3,112✔
1259
  int64_t                now = taosGetTimestampMs();
3,114✔
1260
  int32_t                code = 0;
3,114✔
1261

1262
  streamMutexLock(&pInfo->lock);
3,114✔
1263

1264
  if (sendingChkptId > pInfo->failedId) {
3,121✔
1265
    pInfo->dispatchTrigger = true;
3,119✔
1266
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
3,119✔
1267
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
555✔
1268

1269
      STaskTriggerSendInfo p = {
555✔
1270
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
555✔
1271
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
555✔
1272
      if (px == NULL) {  // pause the stream task, if memory not enough
554!
1273
        code = terrno;
×
1274
      }
1275
    } else {
1276
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
10,143✔
1277
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
7,583✔
1278
        if (pVgInfo == NULL) {
7,580!
1279
          continue;
×
1280
        }
1281

1282
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
7,580✔
1283
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
7,580✔
1284
        if (px == NULL) {  // pause the stream task, if memory not enough
7,579!
1285
          code = terrno;
×
1286
          break;
×
1287
        }
1288
      }
1289
    }
1290
  }
1291

1292
  streamMutexUnlock(&pInfo->lock);
3,105✔
1293

1294
  return code;
3,118✔
1295
}
1296

1297
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
8,113✔
1298
  int32_t num = 0;
8,113✔
1299
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
32,924✔
1300
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
24,801✔
1301
    if (p == NULL) {
24,811!
1302
      continue;
×
1303
    }
1304

1305
    if (p->recved) {
24,811✔
1306
      num++;
16,463✔
1307
    }
1308
  }
1309
  return num;
8,106✔
1310
}
1311

1312
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
8,107✔
1313
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,107✔
1314

1315
  int64_t now = taosGetTimestampMs();
8,110✔
1316
  int32_t taskId = 0;
8,110✔
1317
  int32_t total = streamTaskGetNumOfDownstream(pTask);
8,110✔
1318
  bool    alreadyRecv = false;
8,110✔
1319

1320
  streamMutexLock(&pInfo->lock);
8,110✔
1321

1322
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
16,467!
1323
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
16,464✔
1324
    if (p == NULL) {
16,462!
1325
      continue;
×
1326
    }
1327

1328
    if (p->nodeId == vgId) {
16,462✔
1329
      if (p->recved) {
8,112!
1330
        stWarn("s-task:%s already recv checkpoint-trigger msg rsp from vgId:%d down:0x%x %.2fs ago, req send:%" PRId64
×
1331
               " discard",
1332
               pTask->id.idStr, vgId, p->taskId, (now - p->recvTs) / 1000.0, p->sendTs);
1333
        alreadyRecv = true;
×
1334
      } else {
1335
        p->recved = true;
8,112✔
1336
        p->recvTs = taosGetTimestampMs();
8,112✔
1337
        taskId = p->taskId;
8,112✔
1338
      }
1339
      break;
8,112✔
1340
    }
1341
  }
1342

1343
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
8,111✔
1344
  streamMutexUnlock(&pInfo->lock);
8,106✔
1345

1346
  if (taskId == 0) {
8,119!
1347
    stError("s-task:%s recv invalid trigger-dispatch confirm, vgId:%d", pTask->id.idStr, vgId);
×
1348
  } else {
1349
    if (!alreadyRecv) {
8,119!
1350
      stDebug("s-task:%s set downstream:0x%x(vgId:%d) checkpoint-trigger dispatch confirmed, total confirmed:%d/%d",
8,119✔
1351
              pTask->id.idStr, taskId, vgId, numOfConfirmed, total);
1352
    }
1353
  }
1354
}
8,119✔
1355

1356
int32_t uploadCheckpointToS3(const char* id, const char* path) {
1✔
1357
  int32_t code = 0;
1✔
1358
  int32_t nBytes = 0;
1✔
1359
  /*
1360
  if (s3Init() != 0) {
1361
    return TSDB_CODE_THIRDPARTY_ERROR;
1362
  }
1363
  */
1364
  TdDirPtr pDir = taosOpenDir(path);
1✔
1365
  if (pDir == NULL) {
1!
1366
    return terrno;
×
1367
  }
1368

1369
  TdDirEntryPtr de = NULL;
1✔
1370
  while ((de = taosReadDir(pDir)) != NULL) {
5✔
1371
    char* name = taosGetDirEntryName(de);
4✔
1372
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
4!
1373

1374
    char filename[PATH_MAX] = {0};
2✔
1375
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
2!
1376
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
1377
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1378
        code = TSDB_CODE_OUT_OF_RANGE;
×
1379
        break;
×
1380
      }
1381
    } else {
1382
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
2✔
1383
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
2!
1384
        code = TSDB_CODE_OUT_OF_RANGE;
×
1385
        break;
×
1386
      }
1387
    }
1388

1389
    char object[PATH_MAX] = {0};
2✔
1390
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
2✔
1391
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
2!
1392
      code = TSDB_CODE_OUT_OF_RANGE;
×
1393
      break;
×
1394
    }
1395

1396
    code = tcsPutObjectFromFile2(filename, object, 0);
2✔
1397
    if (code != 0) {
2!
1398
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
2!
1399
    } else {
1400
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1401
    }
1402
  }
1403

1404
  int32_t ret = taosCloseDir(&pDir);
1✔
1405
  if (code == 0 && ret != 0) {
1!
1406
    code = ret;
×
1407
  }
1408

1409
  return code;
1✔
1410
}
1411

1412
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
1✔
1413
  int32_t nBytes;
1414
  int32_t cap = strlen(id) + strlen(dstName) + 16;
1✔
1415

1416
  char* buf = taosMemoryCalloc(1, cap);
1!
1417
  if (buf == NULL) {
1!
1418
    return terrno;
×
1419
  }
1420

1421
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
1✔
1422
  if (nBytes <= 0 || nBytes >= cap) {
1!
1423
    taosMemoryFree(buf);
×
1424
    return TSDB_CODE_OUT_OF_RANGE;
×
1425
  }
1426
  int32_t code = tcsGetObjectToFile(buf, dstName);
1✔
1427
  if (code != 0) {
1!
1428
    taosMemoryFree(buf);
1!
1429
    return TAOS_SYSTEM_ERROR(errno);
1✔
1430
  }
1431
  taosMemoryFree(buf);
×
1432
  return 0;
×
1433
}
1434

1435
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
6,132✔
1436
  if (strlen(tsSnodeAddress) != 0) {
6,132✔
1437
    return DATA_UPLOAD_RSYNC;
1✔
1438
  } else if (tsS3StreamEnabled) {
6,131!
1439
    return DATA_UPLOAD_S3;
×
1440
  } else {
1441
    return DATA_UPLOAD_DISABLE;
6,131✔
1442
  }
1443
}
1444

1445
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
2✔
1446
  int32_t code = 0;
2✔
1447
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
2!
1448
    stError("invalid parameters in upload checkpoint, %s", id);
1!
1449
    return TSDB_CODE_INVALID_CFG;
1✔
1450
  }
1451

1452
  if (strlen(tsSnodeAddress) != 0) {
1!
1453
    code = uploadByRsync(id, path, checkpointId);
×
1454
    if (code != 0) {
×
1455
      return TAOS_SYSTEM_ERROR(errno);
×
1456
    }
1457
  } else if (tsS3StreamEnabled) {
1!
1458
    return uploadCheckpointToS3(id, path);
×
1459
  }
1460

1461
  return 0;
1✔
1462
}
1463

1464
// fileName:  CURRENT
1465
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
1✔
1466
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
1!
1467
    stError("down load checkpoint data parameters invalid");
×
1468
    return TSDB_CODE_INVALID_PARA;
×
1469
  }
1470

1471
  if (strlen(tsSnodeAddress) != 0) {
1!
1472
    return 0;
×
1473
  } else if (tsS3StreamEnabled) {
1!
1474
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1475
  }
1476

1477
  return 0;
1✔
1478
}
1479

1480
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
1✔
1481
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
1!
1482
    stError("down checkpoint data parameters invalid");
×
1483
    return -1;
×
1484
  }
1485

1486
  if (strlen(tsSnodeAddress) != 0) {
1!
1487
    return downloadByRsync(id, path, checkpointId);
1✔
1488
  } else if (tsS3StreamEnabled) {
×
1489
    return tcsGetObjectsByPrefix(id, path);
×
1490
  }
1491

1492
  return 0;
×
1493
}
1494

1495
#ifdef BUILD_NO_CALL
1496
int32_t deleteCheckpoint(const char* id) {
1497
  if (id == NULL || strlen(id) == 0) {
1498
    stError("deleteCheckpoint parameters invalid");
1499
    return TSDB_CODE_INVALID_PARA;
1500
  }
1501
  if (strlen(tsSnodeAddress) != 0) {
1502
    return deleteRsync(id);
1503
  } else if (tsS3StreamEnabled) {
1504
    tcsDeleteObjectsByPrefix(id);
1505
  }
1506
  return 0;
1507
}
1508
#endif
1509

1510
int32_t deleteCheckpointFile(const char* id, const char* name) {
1✔
1511
  char object[128] = {0};
1✔
1512

1513
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
1✔
1514
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
1!
1515
    return TSDB_CODE_OUT_OF_RANGE;
×
1516
  }
1517

1518
  char*   tmp = object;
1✔
1519
  int32_t code = tcsDeleteObjects((const char**)&tmp, 1);
1✔
1520
  if (code != 0) {
1!
1521
    return TSDB_CODE_THIRDPARTY_ERROR;
1✔
1522
  }
1523
  return code;
×
1524
}
1525

1526
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
365✔
1527
  streamMutexLock(&pTask->lock);
365✔
1528
  ETaskStatus p = streamTaskGetStatus(pTask).state;
366✔
1529
  //  if (pInfo->alreadySendChkptId == true) {
1530
  //    stDebug("s-task:%s already start to consensus-checkpointId, not start again before it completed", id);
1531
  //    streamMutexUnlock(&pTask->lock);
1532
  //    return TSDB_CODE_SUCCESS;
1533
  //  } else {
1534
  //    pInfo->alreadySendChkptId = true;
1535
  //  }
1536
  //
1537
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
366✔
1538
  streamMutexUnlock(&pTask->lock);
366✔
1539

1540
  if (pTask->pBackend != NULL) {
366!
1541
    streamFreeTaskState(pTask, p);
×
1542
    pTask->pBackend = NULL;
×
1543
  }
1544
  return 0;
366✔
1545
}
1546

1547
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
63✔
1548
  int32_t code = 0;
63✔
1549
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
63✔
1550
    return code;
33✔
1551
  }
1552

1553
  streamMutexLock(&pTask->lock);
30✔
1554
  SStreamTaskState p = streamTaskGetStatus(pTask);
31✔
1555
  if (p.state == TASK_STATUS__CK) {
31!
1556
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1557
  }
1558
  streamMutexUnlock(&pTask->lock);
31✔
1559

1560
  return code;
31✔
1561
}
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