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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

web-flow
Merge pull request #28973 from taosdata/merge/mainto3.0

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

39.86
/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 deleteCheckpointFile(const char* id, const char* name);
23
static int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId);
24
static int32_t deleteCheckpoint(const char* id);
25
static int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName);
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,724✔
33
                                int32_t srcTaskId, SStreamDataBlock** pRes) {
34
  SStreamDataBlock* pChkpoint = NULL;
6,724✔
35
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pChkpoint);
6,724✔
36
  if (code) {
6,742!
37
    return code;
×
38
  }
39

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

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

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

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

76
  *pRes = pChkpoint;
6,744✔
77

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

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

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

99
int32_t streamProcessCheckpointSourceReq(SStreamTask* pTask, SStreamCheckpointSourceReq* pReq) {
3,278✔
100
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
3,278!
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,278✔
107
  if (code != TSDB_CODE_SUCCESS) {
3,275!
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,275✔
113
  pTask->chkInfo.pActiveInfo->activeId = pReq->checkpointId;
3,275✔
114
  pTask->chkInfo.startTs = taosGetTimestampMs();
3,268✔
115
  pTask->execInfo.checkpoint += 1;
3,268✔
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,268✔
120
}
121

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

127
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
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) {
×
133
    stDebug("s-task:%s retrieve checkpoint-trgger rsp from upstream:0x%x invalid, code:%s", id, pRsp->upstreamTaskId,
×
134
            tstrerror(pRsp->rspCode));
135
    return TSDB_CODE_SUCCESS;
×
136
  }
137

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

142
  if (status.state != TASK_STATUS__CK) {
×
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);
×
148
  unQualified = (pInfo->activeId != pRsp->checkpointId || pInfo->transId != pRsp->transId);
×
149
  streamMutexUnlock(&pInfo->lock);
×
150

151
  if (unQualified) {
×
152
    stError("s-task:%s status:%s not in checkpoint status, discard the checkpoint-trigger msg", id, status.name);
×
153
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
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,
×
163
                                           SRpcHandleInfo* pRpcInfo, int32_t code) {
164
  int32_t  ret = 0;
×
165
  int32_t  tlen = 0;
×
166
  void*    buf = NULL;
×
167
  SEncoder encoder;
168

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

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

182
  tEncodeSize(tEncodeCheckpointTriggerRsp, &req, tlen, ret);
×
183
  if (ret < 0) {
×
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));
×
189
  if (buf == NULL) {
×
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);
×
195
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
196

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

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

209
  return ret;
×
210
}
211

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

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

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
11,869✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
11,869!
241
    stError("s-task:%s vgId:%d current checkpointId:%" PRId64
×
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;
×
245
  }
246

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

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

263
      code = initCheckpointReadyMsg(pTask, pInfo->nodeId, pBlock->srcTaskId, pInfo->childId, checkpointId, &msg);
×
264
      if (code == TSDB_CODE_SUCCESS) {
×
265
        code = tmsgSendReq(&pInfo->epSet, &msg);
×
266
        if (code) {
×
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(
×
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;
×
278
  }
279

280
  if (streamTaskGetStatus(pTask).state == TASK_STATUS__CK) {
11,869✔
281
    if (pActiveInfo->activeId != checkpointId) {
8,601!
282
      stError("s-task:%s vgId:%d active checkpointId:%" PRId64 ", recv invalid checkpoint-trigger checkpointId:%" PRId64
×
283
              " discard",
284
              id, vgId, pActiveInfo->activeId, checkpointId);
285
      return TSDB_CODE_STREAM_INVLD_CHKPT;
×
286
    } else {  // checkpointId == pActiveInfo->activeId
287
      if (pActiveInfo->allUpstreamTriggerRecv == 1) {
8,601!
288
        stDebug(
×
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;
×
293
      }
294

295
      if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
8,601✔
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) {
14,212✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,848✔
299
          if (p == NULL) {
8,866!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

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

314
  return TSDB_CODE_SUCCESS;
11,860✔
315
}
316

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

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

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

334
  streamMutexLock(&pTask->lock);
11,877✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
11,886✔
336
  streamMutexUnlock(&pTask->lock);
11,859✔
337
  if (code) {
11,880!
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,880✔
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,880✔
351
    pActiveInfo->activeId = checkpointId;
3,269✔
352
    pActiveInfo->transId = transId;
3,269✔
353

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

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

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

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

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

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

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

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

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

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

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

536
  return code;
8,596✔
537
}
538

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

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

561
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
34,796✔
562
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
26,222✔
563
    if (pReadyInfo == NULL) {
26,215!
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) {
26,222✔
570
      numOfConfirmed += 1;
17,393✔
571
    }
572
  }
573

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

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

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

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

587
  streamMutexLock(&pInfo->lock);
5,338✔
588
  streamTaskClearActiveInfo(pInfo);
5,352✔
589
  if (clearChkpReadyMsg) {
5,345!
590
    streamClearChkptReadyMsg(pInfo);
5,345✔
591
  }
592
  streamMutexUnlock(&pInfo->lock);
5,344✔
593

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

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

605
  streamMutexLock(&pTask->lock);
7,094✔
606

607
  if (pReq->checkpointId <= pInfo->checkpointId) {
7,097✔
608
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
1,746✔
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);
1,746✔
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
      streamMetaWUnLock(pMeta);
1,745✔
617
      if (pReq->dropRelHTask) {
1,746✔
618
        code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
113✔
619
        int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
113✔
620
        stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped in update checkpointInfo, remain tasks:%d",
113✔
621
                id, vgId, pReq->taskId, numOfTasks);
622
      }
623

624
      streamMetaWLock(pMeta);
1,746✔
625
      if (pReq->dropRelHTask) {
1,744✔
626
        code = streamMetaCommit(pMeta);
113✔
627
      }
628
    }
629

630
    // always return true
631
    return TSDB_CODE_SUCCESS;
1,744✔
632
  }
633

634
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
5,351✔
635

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

655
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
10,695!
656
                pInfo->processedVer <= pReq->checkpointVer);
5,347!
657

658
  if (!valid) {
5,348!
659
    stFatal("invalid checkpoint id check, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
660
            " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64,
661
            pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId, pReq->checkpointVer);
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,348!
667
    pInfo->checkpointId = pReq->checkpointId;
5,348✔
668
    pInfo->checkpointVer = pReq->checkpointVer;
5,348✔
669
    pInfo->checkpointTime = pReq->checkpointTs;
5,348✔
670

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

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

678
  if (pReq->dropRelHTask) {
5,353✔
679
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
3,419✔
680
            pReq->taskId, vgId, pReq->hTaskId);
681
    CLEAR_RELATED_FILLHISTORY_TASK(pTask);
3,415✔
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,349✔
685
          streamTaskGetStatusStr(pTask->status.taskStatus), streamTaskGetStatus(pTask).name);
686

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

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

692
  if (code != TSDB_CODE_SUCCESS) {
5,351!
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
  streamMetaWUnLock(pMeta);
5,351✔
699

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

708
  streamMetaWLock(pMeta);
5,353✔
709
  code = streamMetaCommit(pMeta);
5,352✔
710

711
  return TSDB_CODE_SUCCESS;
5,354✔
712
}
713

714
void streamTaskSetFailedCheckpointId(SStreamTask* pTask, int64_t failedId) {
×
715
  struct SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
716

717
  if (failedId <= 0) {
×
718
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
×
719
           " activeId:%" PRId64,
720
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
721
  } else {
722
    if (failedId <= pInfo->failedId) {
×
723
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
×
724
    } else {
725
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
×
726
              " prev failedId:%" PRId64,
727
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
728
      pInfo->failedId = failedId;
×
729
    }
730
  }
731
}
×
732

733
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
605✔
734
  streamMutexLock(&pTask->lock);
605✔
735
  ETaskStatus status = streamTaskGetStatus(pTask).state;
605✔
736
  if (status == TASK_STATUS__CK) {
605!
737
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
738
  }
739
  streamMutexUnlock(&pTask->lock);
605✔
740
}
605✔
741

742
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
×
743
  int32_t code = 0;
×
744
  int32_t cap = strlen(path) + 64;
×
745

746
  char* filePath = taosMemoryCalloc(1, cap);
×
747
  if (filePath == NULL) {
×
748
    return terrno;
×
749
  }
750

751
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
×
752
  if (nBytes <= 0 || nBytes >= cap) {
×
753
    taosMemoryFree(filePath);
×
754
    return TSDB_CODE_OUT_OF_RANGE;
×
755
  }
756

757
  code = downloadCheckpointDataByName(id, "META", filePath);
×
758
  if (code != 0) {
×
759
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
760
    taosMemoryFree(filePath);
×
761
    return code;
×
762
  }
763

764
  code = remoteChkpGetDelFile(filePath, list);
×
765
  if (code != 0) {
×
766
    stError("%s chkp failed to get to del:%s", id, filePath);
×
767
    taosMemoryFree(filePath);
×
768
  }
769
  return 0;
×
770
}
771

772
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
×
773
  int32_t code = 0;
×
774
  char*   path = NULL;
×
775

776
  SStreamMeta* pMeta = pTask->pMeta;
×
777
  const char*  idStr = pTask->id.idStr;
×
778
  int64_t      now = taosGetTimestampMs();
×
779

780
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
×
781
  if (toDelFiles == NULL) {
×
782
    return terrno;
×
783
  }
784

785
  if ((code = taskDbGenChkpUploadData(pTask->pBackend, pMeta->bkdChkptMgt, checkpointId, type, &path, toDelFiles,
×
786
                                      pTask->id.idStr)) != 0) {
787
    stError("s-task:%s failed to gen upload checkpoint:%" PRId64 ", reason:%s", idStr, checkpointId, tstrerror(code));
×
788
  }
789

790
  if (type == DATA_UPLOAD_S3) {
×
791
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
×
792
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
793
              tstrerror(code));
794
    }
795
  }
796

797
  if (code == TSDB_CODE_SUCCESS) {
×
798
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
×
799
    if (code == TSDB_CODE_SUCCESS) {
×
800
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
×
801
    } else {
802
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
×
803
              tstrerror(code));
804
    }
805
  }
806

807
  if (code == TSDB_CODE_SUCCESS) {
×
808
    int32_t size = taosArrayGetSize(toDelFiles);
×
809
    stDebug("s-task:%s remove redundant %d files", idStr, size);
×
810

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

820
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
×
821
  }
822

823
  taosArrayDestroyP(toDelFiles, taosMemoryFree);
×
824
  double el = (taosGetTimestampMs() - now) / 1000.0;
×
825

826
  if (code == TSDB_CODE_SUCCESS) {
×
827
    stDebug("s-task:%s complete update checkpointId:%" PRId64 ", elapsed time:%.2fs remove local checkpoint data %s",
×
828
            idStr, checkpointId, el, path);
829
    taosRemoveDir(path);
×
830
  } else {
831
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " keep local checkpoint data, elapsed time:%.2fs", idStr,
×
832
            checkpointId, el);
833
  }
834

835
  taosMemoryFree(path);
×
836
  return code;
×
837
}
838

839
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
6,466✔
840
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
6,466✔
841
  if (type == DATA_UPLOAD_DISABLE) {
6,466!
842
    stDebug("s-task:%s not allowed to upload checkpoint data", pTask->id.idStr);
6,466✔
843
    return 0;
6,467✔
844
  }
845

846
  if (pTask == NULL || pTask->pBackend == NULL) {
×
847
    return 0;
×
848
  }
849

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

858
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
×
859
  taskReleaseDb(dbRefId);
×
860

861
  return code;
×
862
}
863

864
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
6,449✔
865
  int32_t      code = TSDB_CODE_SUCCESS;
6,449✔
866
  int64_t      startTs = pTask->chkInfo.startTs;
6,449✔
867
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
6,449✔
868
  const char*  id = pTask->id.idStr;
6,449✔
869
  bool         dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
6,449✔
870
  SStreamMeta* pMeta = pTask->pMeta;
6,459✔
871

872
  // sink task does not need to save the status, and generated the checkpoint
873
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
6,459✔
874
    stDebug("s-task:%s level:%d start gen checkpoint, checkpointId:%" PRId64, id, pTask->info.taskLevel, ckId);
3,461✔
875

876
    int64_t ver = pTask->chkInfo.processedVer;
3,461✔
877
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
3,461✔
878
    if (code != TSDB_CODE_SUCCESS) {
3,464!
879
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
880
    }
881
  }
882

883
  // TODO: monitoring the checkpoint-source msg
884
  // send check point response to upstream task
885
  if (code == TSDB_CODE_SUCCESS) {
6,464!
886
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,464✔
887
      code = streamTaskSendCheckpointSourceRsp(pTask);
3,200✔
888
    } else {
889
      code = streamTaskSendCheckpointReadyMsg(pTask);
3,264✔
890
    }
891

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

899
  if (code == TSDB_CODE_SUCCESS) {
6,466!
900
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
6,466✔
901
    if (code != TSDB_CODE_SUCCESS) {
6,466!
902
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
×
903
    }
904
  } else {
905
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
906
  }
907

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

920
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
×
921
    stDebug("s-task:%s clear checkpoint flag since gen checkpoint failed, checkpointId:%" PRId64, id, ckId);
×
922
  }
923

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

929
  return code;
6,467✔
930
}
931

932
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
1✔
933
  const char*            id = pTask->id.idStr;
1✔
934
  int32_t                vgId = pTask->pMeta->vgId;
1✔
935
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
936
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
1✔
937

938
  // checkpoint-trigger recv flag is set, quit
939
  if (pActiveInfo->allUpstreamTriggerRecv) {
1!
940
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1✔
941
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
1!
942
    return -1;
1✔
943
  }
944

945
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
×
946
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
947
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
×
948
           ", quit",
949
           id, vgId, pTmrInfo->launchChkptId);
950
    return -1;
×
951
  }
952

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

961
  return 0;
×
962
}
963

964
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
×
965
  const char*            id = pTask->id.idStr;
×
966
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
967

968
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
×
969
  if (pNotSendList == NULL) {
×
970
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
971
    return terrno;
×
972
  }
973

974
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
975
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
×
976

977
    bool recved = false;
×
978
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
×
979
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
×
980
      if (pReady == NULL) {
×
981
        continue;
×
982
      }
983

984
      if (pInfo->nodeId == pReady->upstreamNodeId) {
×
985
        recved = true;
×
986
        break;
×
987
      }
988
    }
989

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

1001
  *ppNotSendList = pNotSendList;
×
1002
  return 0;
×
1003
}
1004

1005
static int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray* pNotSendList) {
1✔
1006
  const char*            id = pTask->id.idStr;
1✔
1007
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
1✔
1008
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
1✔
1009
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
1✔
1010
  int32_t                vgId = pTask->pMeta->vgId;
1✔
1011

1012
  int32_t code = doChkptStatusCheck(pTask, param);
1✔
1013
  if (code) {
1!
1014
    return code;
1✔
1015
  }
1016

1017
  code = doFindNotSendUpstream(pTask, pList, &pNotSendList);
×
1018
  if (code) {
×
1019
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1020
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1021
    return code;
×
1022
  }
1023

1024
  // do send retrieve checkpoint trigger msg to upstream
1025
  code = doSendRetrieveTriggerMsg(pTask, pNotSendList);
×
1026
  if (code) {
×
1027
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1028
    code = 0;
×
1029
  }
1030

1031
  return code;
×
1032
}
1033

1034
static void doCleanup(SStreamTask* pTask, SArray* pList) {
107,082✔
1035
  streamMetaReleaseTask(pTask->pMeta, pTask);
107,082✔
1036
  taosArrayDestroy(pList);
107,082✔
1037
}
107,082✔
1038

1039
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
107,093✔
1040
  int32_t      code = 0;
107,093✔
1041
  int32_t      numOfNotSend = 0;
107,093✔
1042
  SArray*      pNotSendList = NULL;
107,093✔
1043
  int64_t      taskRefId = *(int64_t*)param;
107,093✔
1044
  int64_t      now = taosGetTimestampMs();
107,093✔
1045

1046
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
107,093✔
1047
  if (pTask == NULL) {
107,093✔
1048
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
11!
1049
    streamTaskFreeRefId(param);
11✔
1050
    return;
107,093✔
1051
  }
1052

1053
  int32_t                vgId = pTask->pMeta->vgId;
107,082✔
1054
  const char*            id = pTask->id.idStr;
107,082✔
1055
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
107,082✔
1056
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
107,082✔
1057
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
107,082✔
1058

1059
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
107,082!
1060
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1061
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1062
    doCleanup(pTask, pNotSendList);
×
1063
    return;
×
1064
  }
1065

1066
  // check the status every 100ms
1067
  if (streamTaskShouldStop(pTask)) {
107,082✔
1068
    streamCleanBeforeQuitTmr(pTmrInfo, param);
4✔
1069
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
4!
1070
    doCleanup(pTask, pNotSendList);
4✔
1071
    return;
4✔
1072
  }
1073

1074
  if (++pTmrInfo->activeCounter < 50) {
107,078✔
1075
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
105,571✔
1076
                   "trigger-recv-monitor");
1077
    doCleanup(pTask, pNotSendList);
105,571✔
1078
    return;
105,571✔
1079
  }
1080

1081
  pTmrInfo->activeCounter = 0;
1,507✔
1082
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
1,507✔
1083

1084
  streamMutexLock(&pTask->lock);
1,507✔
1085
  SStreamTaskState state = streamTaskGetStatus(pTask);
1,507✔
1086
  streamMutexUnlock(&pTask->lock);
1,507✔
1087

1088
  if (state.state != TASK_STATUS__CK) {
1,507✔
1089
    streamCleanBeforeQuitTmr(pTmrInfo, param);
1,506✔
1090
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
1,506✔
1091
            vgId, state.name);
1092
    doCleanup(pTask, pNotSendList);
1,506✔
1093
    return;
1,506✔
1094
  }
1095

1096
  streamMutexLock(&pActiveInfo->lock);
1✔
1097
  code = chkptTriggerRecvMonitorHelper(pTask, param, pNotSendList);
1✔
1098
  streamMutexUnlock(&pActiveInfo->lock);
1✔
1099

1100
  if (code != TSDB_CODE_SUCCESS) {
1!
1101
    doCleanup(pTask, pNotSendList);
1✔
1102
    return;
1✔
1103
  }
1104

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

1116
  doCleanup(pTask, pNotSendList);
×
1117
}
1118

1119
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
×
1120
  int32_t     code = 0;
×
1121
  int32_t     vgId = pTask->pMeta->vgId;
×
1122
  const char* pId = pTask->id.idStr;
×
1123
  int32_t     size = taosArrayGetSize(pNotSendList);
×
1124
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
×
1125
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1126

1127
  if (size <= 0) {
×
1128
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1129
    return code;
×
1130
  }
1131

1132
  stDebug("s-task:%s %d/%d not recv checkpoint-trigger from upstream(s), start to send trigger-retrieve", pId, size,
×
1133
          numOfUpstream);
1134

1135
  for (int32_t i = 0; i < size; i++) {
×
1136
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
×
1137
    if (pUpstreamTask == NULL) {
×
1138
      return TSDB_CODE_INVALID_PARA;
×
1139
    }
1140

1141
    int32_t  ret = 0;
×
1142
    int32_t  tlen = 0;
×
1143
    void*    buf = NULL;
×
1144
    SRpcMsg  rpcMsg = {0};
×
1145
    SEncoder encoder;
1146

1147
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
×
1148
                                    .downstreamTaskId = pTask->id.taskId,
×
1149
                                    .downstreamNodeId = vgId,
1150
                                    .upstreamTaskId = pUpstreamTask->taskId,
×
1151
                                    .upstreamNodeId = pUpstreamTask->nodeId,
×
1152
                                    .checkpointId = checkpointId};
1153

1154
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
×
1155
    if (ret < 0) {
×
1156
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1157
    }
1158

1159
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
×
1160
    if (buf == NULL) {
×
1161
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1162
      continue;
×
1163
    }
1164

1165
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
×
1166
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
1167

1168
    tEncoderInit(&encoder, abuf, tlen);
×
1169
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
×
1170
      rpcFreeCont(buf);
×
1171
      tEncoderClear(&encoder);
×
1172
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1173
      continue;
×
1174
    }
1175
    tEncoderClear(&encoder);
×
1176

1177
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
×
1178

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

1189
  return code;
×
1190
}
1191

1192
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1193
  int64_t                now = taosGetTimestampMs();
×
1194
  const char*            id = pTask->id.idStr;
×
1195
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1196
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1197

1198
  if (!pInfo->dispatchTrigger) {
×
1199
    return false;
×
1200
  }
1201

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

1210
    if (pSendInfo->nodeId != downstreamNodeId) {
×
1211
      continue;
×
1212
    }
1213

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

1226
    return true;
×
1227
  }
1228

1229
  return false;
×
1230
}
1231

1232
bool streamTaskAlreadySendTrigger(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1233
  int64_t                now = taosGetTimestampMs();
×
1234
  const char*            id = pTask->id.idStr;
×
1235
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1236
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1237

1238
  if (pStatus.state != TASK_STATUS__CK) {
×
1239
    return false;
×
1240
  }
1241

1242
  streamMutexLock(&pInfo->lock);
×
1243
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
×
1244
  streamMutexUnlock(&pInfo->lock);
×
1245

1246
  return send;
×
1247
}
1248

1249
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
×
1250
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
×
1251

1252
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1253
    *pTotal = 1;
×
1254
  } else {
1255
    *pTotal = streamTaskGetNumOfUpstream(pTask);
×
1256
  }
1257
}
×
1258

1259
// record the dispatch checkpoint trigger info in the list
1260
// memory insufficient may cause the stream computing stopped
1261
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
3,310✔
1262
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
3,310✔
1263
  int64_t                now = taosGetTimestampMs();
3,311✔
1264
  int32_t                code = 0;
3,311✔
1265

1266
  streamMutexLock(&pInfo->lock);
3,311✔
1267

1268
  if (sendingChkptId > pInfo->failedId) {
3,316✔
1269
    pInfo->dispatchTrigger = true;
3,314✔
1270
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
3,314✔
1271
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
563✔
1272

1273
      STaskTriggerSendInfo p = {
563✔
1274
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
563✔
1275
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
563✔
1276
      if (px == NULL) {  // pause the stream task, if memory not enough
563!
UNCOV
1277
        code = terrno;
×
1278
      }
1279
    } else {
1280
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
10,888✔
1281
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
8,135✔
1282
        if (pVgInfo == NULL) {
8,135!
UNCOV
1283
          continue;
×
1284
        }
1285

1286
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
8,135✔
1287
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
8,135✔
1288
        if (px == NULL) {  // pause the stream task, if memory not enough
8,137!
UNCOV
1289
          code = terrno;
×
UNCOV
1290
          break;
×
1291
        }
1292
      }
1293
    }
1294
  }
1295

1296
  streamMutexUnlock(&pInfo->lock);
3,309✔
1297

1298
  return code;
3,313✔
1299
}
1300

1301
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
8,631✔
1302
  int32_t num = 0;
8,631✔
1303
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
35,014✔
1304
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
26,365✔
1305
    if (p == NULL) {
26,383!
UNCOV
1306
      continue;
×
1307
    }
1308

1309
    if (p->recved) {
26,383✔
1310
      num++;
17,501✔
1311
    }
1312
  }
1313
  return num;
8,624✔
1314
}
1315

1316
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
8,628✔
1317
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
8,628✔
1318

1319
  int64_t now = taosGetTimestampMs();
8,632✔
1320
  int32_t taskId = 0;
8,632✔
1321
  int32_t total = streamTaskGetNumOfDownstream(pTask);
8,632✔
1322
  bool    alreadyRecv = false;
8,631✔
1323

1324
  streamMutexLock(&pInfo->lock);
8,631✔
1325

1326
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
17,533!
1327
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
17,526✔
1328
    if (p == NULL) {
17,520!
UNCOV
1329
      continue;
×
1330
    }
1331

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

1347
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
8,629✔
1348
  streamMutexUnlock(&pInfo->lock);
8,624✔
1349

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

UNCOV
1360
static int32_t uploadCheckpointToS3(const char* id, const char* path) {
×
UNCOV
1361
  int32_t code = 0;
×
UNCOV
1362
  int32_t nBytes = 0;
×
1363
  /*
1364
  if (s3Init() != 0) {
1365
    return TSDB_CODE_THIRDPARTY_ERROR;
1366
  }
1367
  */
UNCOV
1368
  TdDirPtr pDir = taosOpenDir(path);
×
UNCOV
1369
  if (pDir == NULL) {
×
1370
    return terrno;
×
1371
  }
1372

1373
  TdDirEntryPtr de = NULL;
×
UNCOV
1374
  while ((de = taosReadDir(pDir)) != NULL) {
×
1375
    char* name = taosGetDirEntryName(de);
×
1376
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
×
1377

1378
    char filename[PATH_MAX] = {0};
×
1379
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
×
1380
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
UNCOV
1381
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
UNCOV
1382
        code = TSDB_CODE_OUT_OF_RANGE;
×
1383
        break;
×
1384
      }
1385
    } else {
1386
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
×
UNCOV
1387
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
UNCOV
1388
        code = TSDB_CODE_OUT_OF_RANGE;
×
UNCOV
1389
        break;
×
1390
      }
1391
    }
1392

1393
    char object[PATH_MAX] = {0};
×
1394
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
×
UNCOV
1395
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
UNCOV
1396
      code = TSDB_CODE_OUT_OF_RANGE;
×
1397
      break;
×
1398
    }
1399

UNCOV
1400
    code = tcsPutObjectFromFile2(filename, object, 0);
×
1401
    if (code != 0) {
×
UNCOV
1402
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
×
1403
    } else {
UNCOV
1404
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1405
    }
1406
  }
1407

UNCOV
1408
  int32_t ret = taosCloseDir(&pDir);
×
UNCOV
1409
  if (code == 0 && ret != 0) {
×
1410
    code = ret;
×
1411
  }
1412

1413
  return code;
×
1414
}
1415

UNCOV
1416
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
×
1417
  int32_t nBytes;
1418
  int32_t cap = strlen(id) + strlen(dstName) + 16;
×
1419

UNCOV
1420
  char* buf = taosMemoryCalloc(1, cap);
×
UNCOV
1421
  if (buf == NULL) {
×
1422
    return terrno;
×
1423
  }
1424

1425
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
×
UNCOV
1426
  if (nBytes <= 0 || nBytes >= cap) {
×
1427
    taosMemoryFree(buf);
×
1428
    return TSDB_CODE_OUT_OF_RANGE;
×
1429
  }
1430
  int32_t code = tcsGetObjectToFile(buf, dstName);
×
UNCOV
1431
  if (code != 0) {
×
1432
    taosMemoryFree(buf);
×
1433
    return TAOS_SYSTEM_ERROR(errno);
×
1434
  }
UNCOV
1435
  taosMemoryFree(buf);
×
UNCOV
1436
  return 0;
×
1437
}
1438

1439
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
6,482✔
1440
  if (strlen(tsSnodeAddress) != 0) {
6,482!
UNCOV
1441
    return DATA_UPLOAD_RSYNC;
×
1442
  } else if (tsS3StreamEnabled) {
6,482!
UNCOV
1443
    return DATA_UPLOAD_S3;
×
1444
  } else {
1445
    return DATA_UPLOAD_DISABLE;
6,482✔
1446
  }
1447
}
1448

1449
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
×
1450
  int32_t code = 0;
×
UNCOV
1451
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
UNCOV
1452
    stError("invalid parameters in upload checkpoint, %s", id);
×
1453
    return TSDB_CODE_INVALID_CFG;
×
1454
  }
1455

1456
  if (strlen(tsSnodeAddress) != 0) {
×
UNCOV
1457
    code = uploadByRsync(id, path, checkpointId);
×
1458
    if (code != 0) {
×
1459
      return TAOS_SYSTEM_ERROR(errno);
×
1460
    }
UNCOV
1461
  } else if (tsS3StreamEnabled) {
×
1462
    return uploadCheckpointToS3(id, path);
×
1463
  }
1464

UNCOV
1465
  return 0;
×
1466
}
1467

1468
// fileName:  CURRENT
1469
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
×
UNCOV
1470
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
×
UNCOV
1471
    stError("down load checkpoint data parameters invalid");
×
1472
    return TSDB_CODE_INVALID_PARA;
×
1473
  }
1474

1475
  if (strlen(tsSnodeAddress) != 0) {
×
UNCOV
1476
    return 0;
×
UNCOV
1477
  } else if (tsS3StreamEnabled) {
×
1478
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1479
  }
1480

1481
  return 0;
×
1482
}
1483

1484
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
×
UNCOV
1485
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
UNCOV
1486
    stError("down checkpoint data parameters invalid");
×
1487
    return -1;
×
1488
  }
1489

1490
  if (strlen(tsSnodeAddress) != 0) {
×
UNCOV
1491
    return downloadByRsync(id, path, checkpointId);
×
UNCOV
1492
  } else if (tsS3StreamEnabled) {
×
1493
    return tcsGetObjectsByPrefix(id, path);
×
1494
  }
1495

1496
  return 0;
×
1497
}
1498

1499
int32_t deleteCheckpoint(const char* id) {
×
UNCOV
1500
  if (id == NULL || strlen(id) == 0) {
×
1501
    stError("deleteCheckpoint parameters invalid");
×
1502
    return TSDB_CODE_INVALID_PARA;
×
1503
  }
1504
  if (strlen(tsSnodeAddress) != 0) {
×
UNCOV
1505
    return deleteRsync(id);
×
1506
  } else if (tsS3StreamEnabled) {
×
UNCOV
1507
    tcsDeleteObjectsByPrefix(id);
×
1508
  }
1509
  return 0;
×
1510
}
1511

1512
int32_t deleteCheckpointFile(const char* id, const char* name) {
×
1513
  char object[128] = {0};
×
1514

UNCOV
1515
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
×
UNCOV
1516
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
1517
    return TSDB_CODE_OUT_OF_RANGE;
×
1518
  }
1519

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

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

1542
  if (pTask->pBackend != NULL) {
303!
UNCOV
1543
    streamFreeTaskState(pTask, p);
×
UNCOV
1544
    pTask->pBackend = NULL;
×
1545
  }
1546
  return 0;
303✔
1547
}
1548

1549
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
90✔
1550
  int32_t code = 0;
90✔
1551
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
90✔
1552
    return code;
46✔
1553
  }
1554

1555
  streamMutexLock(&pTask->lock);
44✔
1556
  SStreamTaskState p = streamTaskGetStatus(pTask);
44✔
1557
  if (p.state == TASK_STATUS__CK) {
44!
UNCOV
1558
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1559
  }
1560
  streamMutexUnlock(&pTask->lock);
44✔
1561

1562
  return code;
44✔
1563
}
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