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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

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

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

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

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

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

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

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

76
  *pRes = pChkpoint;
4,219✔
77

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

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

96
  return streamTrySchedExec(pTask);
4,229✔
97
}
98

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

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

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
8,447✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
8,447!
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) {
8,447!
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) {
8,447!
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) {
8,447✔
281
    if (pActiveInfo->activeId != checkpointId) {
6,346!
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) {
6,346!
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) {
6,346✔
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) {
11,615✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
7,340✔
299
          if (p == NULL) {
7,361!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

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

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

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

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

334
  streamMutexLock(&pTask->lock);
8,449✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
8,455✔
336
  streamMutexUnlock(&pTask->lock);
8,445✔
337
  if (code) {
8,455!
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
8,455✔
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) {
8,455✔
351
    pActiveInfo->activeId = checkpointId;
2,105✔
352
    pActiveInfo->transId = transId;
2,105✔
353

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

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

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

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

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

447
  return code;
4,191✔
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,
6,326✔
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;
6,326✔
455
  int32_t size = taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
6,326✔
456
  for (int32_t i = 0; i < size; ++i) {
13,649✔
457
    STaskDownstreamReadyInfo* p = taosArrayGet(pInfo->pCheckpointReadyRecvList, i);
7,329✔
458
    if (p == NULL) {
7,323!
459
      return TSDB_CODE_INVALID_PARA;
×
460
    }
461

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

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

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

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

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

536
  return code;
6,333✔
537
}
538

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

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

561
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
27,314✔
562
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
20,995✔
563
    if (pReadyInfo == NULL) {
20,990!
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) {
20,990✔
570
      numOfConfirmed += 1;
13,656✔
571
    }
572
  }
573

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

577
  streamMutexUnlock(&pInfo->lock);
6,308✔
578
  return TSDB_CODE_SUCCESS;
6,329✔
579
}
580

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

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

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

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

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

605
  streamMutexLock(&pTask->lock);
4,311✔
606

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

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

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

632
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
3,342✔
633

634
  if (!restored) {  // during restore procedure, do update checkpoint-info
3,338✔
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)) {
3,332!
641
      stDebug("s-task:%s vgId:%d status:%s start to update the checkpoint-info, checkpointId:%" PRId64 "->%" PRId64
3,332✔
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 &&
6,683!
654
                pInfo->processedVer <= pReq->checkpointVer);
3,341!
655

656
  if (!valid) {
3,342!
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)) {
3,342!
667
    pInfo->checkpointId = pReq->checkpointId;
3,342✔
668
    pInfo->checkpointVer = pReq->checkpointVer;
3,342✔
669
    pInfo->checkpointTime = pReq->checkpointTs;
3,342✔
670

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

676
  streamTaskClearCheckInfo(pTask, true);
3,343✔
677

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

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

687
  pTask->status.taskStatus = TASK_STATUS__READY;
3,338✔
688

689
  code = streamMetaSaveTask(pMeta, pTask);
3,338✔
690
  streamMutexUnlock(&pTask->lock);
3,342✔
691

692
  if (code != TSDB_CODE_SUCCESS) {
3,344!
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) {
3,344✔
700
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
3,295✔
701
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
3,294✔
702
    stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
3,295✔
703
            (int32_t)pReq->hTaskId, numOfTasks);
704
  }
705

706
  code = streamMetaCommit(pMeta);
3,344✔
707
  return TSDB_CODE_SUCCESS;
3,343✔
708
}
709

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

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

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

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

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

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

753
  code = downloadCheckpointDataByName(id, "META", filePath);
×
754
  if (code != 0) {
×
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);
×
761
  if (code != 0) {
×
762
    stError("%s chkp failed to get to del:%s", id, filePath);
×
763
    taosMemoryFree(filePath);
×
764
  }
765
  return 0;
×
766
}
767

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

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

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

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

786
  if (type == DATA_UPLOAD_S3) {
×
787
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
×
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) {
×
794
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
×
795
    if (code == TSDB_CODE_SUCCESS) {
×
796
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
×
797
    } else {
798
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
×
799
              tstrerror(code));
800
    }
801
  }
802

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

807
    for (int i = 0; i < size; i++) {
×
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);
×
817
  }
818

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

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

831
  taosMemoryFree(path);
×
832
  return code;
×
833
}
834

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

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

846
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
×
847
  void*   pBackend = taskAcquireDb(dbRefId);
×
848
  if (pBackend == NULL) {
×
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);
×
855
  taskReleaseDb(dbRefId);
×
856

857
  return code;
×
858
}
859

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

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

872
    int64_t ver = pTask->chkInfo.processedVer;
2,115✔
873
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
2,115✔
874
    if (code != TSDB_CODE_SUCCESS) {
2,119!
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) {
4,141!
882
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
4,141✔
883
      code = streamTaskSendCheckpointSourceRsp(pTask);
2,033✔
884
    } else {
885
      code = streamTaskSendCheckpointReadyMsg(pTask);
2,108✔
886
    }
887

888
    if (code != TSDB_CODE_SUCCESS) {
4,143!
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) {
4,143!
896
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
4,143✔
897
    if (code != TSDB_CODE_SUCCESS) {
4,143!
898
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
×
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) {
4,143!
907
    if (pTask->pMsgCb != NULL) {
4,143✔
908
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
4,129✔
909
    }
910
  } else {  // clear the checkpoint info if failed
911
    // set failed checkpoint id before clear the checkpoint info
912
    streamMutexLock(&pTask->lock);
×
913
    streamTaskSetFailedCheckpointId(pTask, ckId);
×
914
    streamMutexUnlock(&pTask->lock);
×
915

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

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

925
  return code;
4,143✔
926
}
927

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

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

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

949
  // active checkpoint info is cleared for now
950
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
×
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;
×
958
}
959

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

964
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
×
965
  if (pNotSendList == NULL) {
×
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) {
×
971
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
×
972

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

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

986
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
×
987
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
×
988
      void* px = taosArrayPush(pNotSendList, pInfo);
×
989
      if (px == NULL) {
×
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;
×
998
  return 0;
×
999
}
1000

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

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

1013
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
×
1014
  if (code) {
×
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);
×
1022
  if (code) {
×
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;
×
1028
}
1029

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

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

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

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

1055
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
59,915!
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)) {
59,915✔
1064
    streamCleanBeforeQuitTmr(pTmrInfo, param);
8✔
1065
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
8!
1066
    doCleanup(pTask, pNotSendList);
8✔
1067
    return;
8✔
1068
  }
1069

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

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

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

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

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

1096
  if (code != TSDB_CODE_SUCCESS) {
1!
1097
    doCleanup(pTask, pNotSendList);
1✔
1098
    return;
1✔
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) {
×
1116
  int32_t     code = 0;
×
1117
  int32_t     vgId = pTask->pMeta->vgId;
×
1118
  const char* pId = pTask->id.idStr;
×
1119
  int32_t     size = taosArrayGetSize(pNotSendList);
×
1120
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
×
1121
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1122

1123
  if (size <= 0) {
×
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,
×
1129
          numOfUpstream);
1130

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

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

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

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

1155
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
×
1156
    if (buf == NULL) {
×
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);
×
1162
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
1163

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

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

1175
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
×
1176
    if (code == TSDB_CODE_SUCCESS) {
×
1177
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
×
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;
×
1186
}
1187

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

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

1198
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
×
1199
  for (int32_t i = 0; i < num; ++i) {
×
1200
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1201
    if (pSendInfo == NULL) {
×
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) {
×
1207
      continue;
×
1208
    }
1209

1210
    // has send trigger msg to downstream node,
1211
    double before = (now - pSendInfo->sendTs) / 1000.0;
×
1212
    if (pSendInfo->recved) {
×
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
×
1218
             ", transId:%d",
1219
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1220
    }
1221

1222
    return true;
×
1223
  }
1224

1225
  return false;
×
1226
}
1227

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

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

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

1242
  return send;
×
1243
}
1244

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

1248
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1249
    *pTotal = 1;
×
1250
  } else {
1251
    *pTotal = streamTaskGetNumOfUpstream(pTask);
×
1252
  }
1253
}
×
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) {
2,145✔
1258
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
2,145✔
1259
  int64_t                now = taosGetTimestampMs();
2,145✔
1260
  int32_t                code = 0;
2,145✔
1261

1262
  streamMutexLock(&pInfo->lock);
2,145✔
1263

1264
  if (sendingChkptId > pInfo->failedId) {
2,147!
1265
    pInfo->dispatchTrigger = true;
2,147✔
1266
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
2,147✔
1267
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
194✔
1268

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

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

1292
  streamMutexUnlock(&pInfo->lock);
2,143✔
1293

1294
  return code;
2,146✔
1295
}
1296

1297
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
6,372✔
1298
  int32_t num = 0;
6,372✔
1299
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
27,502✔
1300
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
21,127✔
1301
    if (p == NULL) {
21,130!
1302
      continue;
×
1303
    }
1304

1305
    if (p->recved) {
21,130✔
1306
      num++;
13,749✔
1307
    }
1308
  }
1309
  return num;
6,369✔
1310
}
1311

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

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

1320
  streamMutexLock(&pInfo->lock);
6,372✔
1321

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

1328
    if (p->nodeId == vgId) {
13,754✔
1329
      if (p->recved) {
6,372!
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;
6,372✔
1336
        p->recvTs = taosGetTimestampMs();
6,372✔
1337
        taskId = p->taskId;
6,372✔
1338
      }
1339
      break;
6,372✔
1340
    }
1341
  }
1342

1343
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
6,371✔
1344
  streamMutexUnlock(&pInfo->lock);
6,369✔
1345

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

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

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

1374
    char filename[PATH_MAX] = {0};
×
1375
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
×
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);
×
1383
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1384
        code = TSDB_CODE_OUT_OF_RANGE;
×
1385
        break;
×
1386
      }
1387
    }
1388

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

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

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

1409
  return code;
×
1410
}
1411

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

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

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

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

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

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

1461
  return 0;
×
1462
}
1463

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

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

1477
  return 0;
×
1478
}
1479

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

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

1492
  return 0;
×
1493
}
1494

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

1508
int32_t deleteCheckpointFile(const char* id, const char* name) {
×
1509
  char object[128] = {0};
×
1510

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

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

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

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

1545
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
83✔
1546
  int32_t code = 0;
83✔
1547
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
83✔
1548
    return code;
43✔
1549
  }
1550

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

1558
  return code;
40✔
1559
}
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