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

taosdata / TDengine / #3638

11 Mar 2025 12:59PM UTC coverage: 3.066% (-18.3%) from 21.409%
#3638

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

5914 of 287117 branches covered (2.06%)

Branch coverage included in aggregate %.

11588 of 283747 relevant lines covered (4.08%)

142.17 hits per line

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

0.0
/source/libs/stream/src/streamCheckpoint.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "rsync.h"
17
#include "streamBackendRocksdb.h"
18
#include "streamInt.h"
19
#include "tcs.h"
20

21
static int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName);
22
static int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId);
23
#ifdef BUILD_NO_CALL
24
static int32_t deleteCheckpoint(const char* id);
25
#endif
26
static int32_t continueDispatchCheckpointTriggerBlock(SStreamDataBlock* pBlock, SStreamTask* pTask);
27
static int32_t appendCheckpointIntoInputQ(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId,
28
                                          int32_t transId, int32_t srcTaskId);
29
static int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList);
30
static void    checkpointTriggerMonitorFn(void* param, void* tmrId);
31

32
int32_t createChkptTriggerBlock(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId, int32_t transId,
×
33
                                int32_t srcTaskId, SStreamDataBlock** pRes) {
34
  SStreamDataBlock* pChkpoint = NULL;
×
35
  int32_t code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SSDataBlock), (void**)&pChkpoint);
×
36
  if (code) {
×
37
    return code;
×
38
  }
39

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

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

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

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

76
  *pRes = pChkpoint;
×
77

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

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

96
  return streamTrySchedExec(pTask, true);
×
97
}
98

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

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

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

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

303
          if (p->upstreamTaskId == pBlock->srcTaskId) {
×
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;
×
315
}
316

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

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

331
  checkpointId = pDataBlock->info.version;
×
332
  transId = pDataBlock->info.window.skey;
×
333

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

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

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

376
      int64_t* pTaskRefId = NULL;
×
377
      code = streamTaskAllocRefId(pTask, &pTaskRefId);
×
378
      if (code == 0) {
×
379
        streamTmrStart(checkpointTriggerMonitorFn, 200, pTaskRefId, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
380
                       "trigger-recv-monitor");
381
        pTmrInfo->launchChkptId = pActiveInfo->activeId;
×
382
      }
383
    } else {  // already launched, do nothing
384
      stError("s-task:%s previous checkpoint-trigger monitor tmr is set, not start new one", pTask->id.idStr);
×
385
    }
386
  }
387

388
#if 0
389
  taosMsleep(20*1000);
390
#endif
391

392
  if (taskLevel == TASK_LEVEL__SOURCE) {
×
393
    int8_t type = pTask->outputInfo.type;
×
394
    pActiveInfo->allUpstreamTriggerRecv = 1;
×
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);
×
401
    if (code) {
×
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) {
×
411
      stDebug("s-task:%s set childIdx:%d, and add checkpoint-trigger block into outputQ", id, pTask->info.selfChildId);
×
412
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);  // todo handle this failure
×
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);
×
416
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
417
    }
418
  } else if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
×
419
    // todo: handle this
420
    // update the child Id for downstream tasks
421
    code = streamAddCheckpointReadyMsg(pTask, pBlock->srcTaskId, pTask->info.selfChildId, checkpointId);
×
422

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

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

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

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

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

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

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

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

536
  return code;
×
537
}
538

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

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

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

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

577
  streamMutexUnlock(&pInfo->lock);
×
578
  return TSDB_CODE_SUCCESS;
×
579
}
580

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

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

587
  streamMutexLock(&pInfo->lock);
×
588
  streamTaskClearActiveInfo(pInfo);
×
589
  if (clearChkpReadyMsg) {
×
590
    streamClearChkptReadyMsg(pInfo);
×
591
  }
592
  streamMutexUnlock(&pInfo->lock);
×
593

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

598
// The checkpointInfo can be updated in the following three cases:
599
// 1. follower tasks; 2. leader task with status of TASK_STATUS__CK; 3. restore not completed
600
static int32_t doUpdateCheckpointInfoCheck(SStreamTask* pTask, bool restored, SVUpdateCheckpointInfoReq* pReq,
×
601
                                           bool* pContinue) {
602
  SStreamMeta*     pMeta = pTask->pMeta;
×
603
  int32_t          vgId = pMeta->vgId;
×
604
  int32_t          code = 0;
×
605
  const char*      id = pTask->id.idStr;
×
606
  SCheckpointInfo* pInfo = &pTask->chkInfo;
×
607

608
  *pContinue = true;
×
609

610
  // not update the checkpoint info if the checkpointId is less than the failed checkpointId
611
  if (pReq->checkpointId < pInfo->pActiveInfo->failedId) {
×
612
    stWarn("s-task:%s vgId:%d not update the checkpoint-info, since update checkpointId:%" PRId64
×
613
           " is less than the failed checkpointId:%" PRId64 ", discard",
614
           id, vgId, pReq->checkpointId, pInfo->pActiveInfo->failedId);
615

616
    *pContinue = false;
×
617
    return TSDB_CODE_SUCCESS;
×
618
  }
619

620
  // it's an expired checkpointInfo update msg, we still try to drop the required drop fill-history task.
621
  if (pReq->checkpointId <= pInfo->checkpointId) {
×
622
    stDebug("s-task:%s vgId:%d latest checkpointId:%" PRId64 " Ver:%" PRId64
×
623
            " no need to update checkpoint info, updated checkpointId:%" PRId64 " Ver:%" PRId64 " transId:%d ignored",
624
            id, vgId, pInfo->checkpointId, pInfo->checkpointVer, pReq->checkpointId, pReq->checkpointVer,
625
            pReq->transId);
626

627
    { // destroy the related fill-history tasks
628
      if (pReq->dropRelHTask) {
×
629
          code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
×
630

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

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

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

645
  SStreamTaskState status = streamTaskGetStatus(pTask);
×
646

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

667
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
×
668
                pInfo->processedVer <= pReq->checkpointVer);
×
669

670
  if (!valid) {
×
671
    // invalid update checkpoint info for leader, since the processedVer is greater than the checkpointVer
672
    // It is possible for follower tasks that the processedVer is greater than the checkpointVer, and the processed info
673
    // in follower tasks will be discarded, since the leader/follower switch happens before the checkpoint of the
674
    // processedVer being generated.
675
    if (pMeta->role == NODE_ROLE_LEADER) {
×
676

677
      stFatal("s-task:%s checkpointId update info recv, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
678
              " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64 " discard it",
679
              id, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
680
              pReq->checkpointVer);
681

682
      *pContinue = false;
×
683
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
684
    } else {
685
      stInfo("s-task:%s vgId:%d follower recv checkpointId update info, current checkpointId:%" PRId64
×
686
             " checkpointVer:%" PRId64 " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64,
687
             id, pMeta->vgId, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
688
             pReq->checkpointVer);
689
    }
690
  }
691

692
  return TSDB_CODE_SUCCESS;
×
693
}
694

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

703
  streamMutexLock(&pTask->lock);
×
704
  code = doUpdateCheckpointInfoCheck(pTask, restored, pReq, &continueUpdate);
×
705

706
  if (!continueUpdate) {
×
707
    streamMutexUnlock(&pTask->lock);
×
708
    return code;
×
709
  }
710

711
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
×
712

713
  // update only it is in checkpoint status, or during restore procedure.
714
  if ((pStatus.state == TASK_STATUS__CK) || (!restored) || (pMeta->role == NODE_ROLE_FOLLOWER)) {
×
715
    pInfo->checkpointId = pReq->checkpointId;
×
716
    pInfo->checkpointVer = pReq->checkpointVer;
×
717
    pInfo->checkpointTime = pReq->checkpointTs;
×
718

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

724
  streamTaskClearCheckInfo(pTask, true);
×
725

726
  if (pReq->dropRelHTask) {
×
727
    stDebug("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
×
728
            pReq->taskId, vgId, pReq->hTaskId);
729
    CLEAR_RELATED_FILLHISTORY_TASK(pTask);
×
730
  }
731

732
  stDebug("s-task:0x%x set the persistent status attr to be ready, prev:%s, status in sm:%s", pReq->taskId,
×
733
          streamTaskGetStatusStr(pTask->status.taskStatus), streamTaskGetStatus(pTask).name);
734

735
  pTask->status.taskStatus = TASK_STATUS__READY;
×
736

737
  code = streamMetaSaveTaskInMeta(pMeta, pTask);
×
738
  streamMutexUnlock(&pTask->lock);
×
739

740
  if (code != TSDB_CODE_SUCCESS) {
×
741
    stError("s-task:%s vgId:%d failed to save task info after do checkpoint, checkpointId:%" PRId64 ", since %s", id,
×
742
            vgId, pReq->checkpointId, terrstr());
743
    return TSDB_CODE_SUCCESS;
×
744
  }
745

746
  // drop task should not in the meta-lock, and drop the related fill-history task now
747
  if (pReq->dropRelHTask) {
×
748
    code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
×
749
    int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
750
    stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
×
751
            (int32_t)pReq->hTaskId, numOfTasks);
752
  }
753

754
  code = streamMetaCommit(pMeta);
×
755
  return TSDB_CODE_SUCCESS;
×
756
}
757

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

761
  if (failedId <= 0) {
×
762
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
×
763
           " activeId:%" PRId64,
764
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
765
  } else {
766
    if (failedId <= pInfo->failedId) {
×
767
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
×
768
    } else {
769
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
×
770
              " prev failedId:%" PRId64,
771
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
772
      pInfo->failedId = failedId;
×
773
    }
774
  }
775
}
×
776

777
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
×
778
  streamMutexLock(&pTask->lock);
×
779
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
780
  if (status == TASK_STATUS__CK) {
×
781
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
782
  }
783
  streamMutexUnlock(&pTask->lock);
×
784
}
×
785

786
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
×
787
  int32_t code = 0;
×
788
  int32_t cap = strlen(path) + 64;
×
789

790
  char* filePath = taosMemoryCalloc(1, cap);
×
791
  if (filePath == NULL) {
×
792
    return terrno;
×
793
  }
794

795
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
×
796
  if (nBytes <= 0 || nBytes >= cap) {
×
797
    taosMemoryFree(filePath);
×
798
    return TSDB_CODE_OUT_OF_RANGE;
×
799
  }
800

801
  code = downloadCheckpointDataByName(id, "META", filePath);
×
802
  if (code != 0) {
×
803
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
804
    taosMemoryFree(filePath);
×
805
    return code;
×
806
  }
807

808
  code = remoteChkpGetDelFile(filePath, list);
×
809
  if (code != 0) {
×
810
    stError("%s chkp failed to get to del:%s", id, filePath);
×
811
    taosMemoryFree(filePath);
×
812
  }
813
  return 0;
×
814
}
815

816
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
×
817
  int32_t      code = 0;
×
818
  char*        path = NULL;
×
819
  int64_t      chkptSize = 0;
×
820
  SStreamMeta* pMeta = pTask->pMeta;
×
821
  const char*  idStr = pTask->id.idStr;
×
822
  int64_t      now = taosGetTimestampMs();
×
823

824
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
×
825
  if (toDelFiles == NULL) {
×
826
    stError("s-task:%s failed to prepare array list during upload checkpoint, code:%s", pTask->id.idStr,
×
827
            tstrerror(terrno));
828
    return terrno;
×
829
  }
830

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

836
  if (type == DATA_UPLOAD_S3) {
×
837
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
×
838
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
839
              tstrerror(code));
840
    }
841
  }
842

843
  if (code == TSDB_CODE_SUCCESS) {
×
844
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
×
845
    if (code == TSDB_CODE_SUCCESS) {
×
846
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
×
847
    } else {
848
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
×
849
              tstrerror(code));
850
    }
851
  }
852

853
  int32_t num = taosArrayGetSize(toDelFiles);
×
854
  if (code == TSDB_CODE_SUCCESS && num > 0) {
×
855
    stDebug("s-task:%s remove redundant %d files", idStr, num);
×
856

857
    for (int i = 0; i < num; i++) {
×
858
      char* pName = taosArrayGetP(toDelFiles, i);
×
859
      code = deleteCheckpointFile(idStr, pName);
×
860
      if (code != 0) {
×
861
        stDebug("s-task:%s failed to remove file: %s", idStr, pName);
×
862
        break;
×
863
      }
864
    }
865

866
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
×
867
  }
868

869
  taosArrayDestroyP(toDelFiles, NULL);
×
870
  double el = (taosGetTimestampMs() - now) / 1000.0;
×
871

872
  if (code == TSDB_CODE_SUCCESS) {
×
873
    code = taosGetDirSize(path, &chkptSize);
×
874
    stDebug("s-task:%s complete upload checkpointId:%" PRId64
×
875
            ", elapsed time:%.2fs, checkpointSize:%.2fKiB local dir:%s",
876
            idStr, checkpointId, el, SIZE_IN_KiB(chkptSize), path);
877
  } else {
878
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " elapsed time:%.2fs, checkpointSize:%.2fKiB", idStr,
×
879
            checkpointId, el, SIZE_IN_KiB(chkptSize));
880
  }
881

882
  taosMemoryFree(path);
×
883
  return code;
×
884
}
885

886
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
×
887
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
×
888
  if (type == DATA_UPLOAD_DISABLE) {
×
889
    stDebug("s-task:%s not config to backup checkpoint data at snode, checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
890
    return 0;
×
891
  }
892

893
  if (pTask == NULL || pTask->pBackend == NULL) {
×
894
    return 0;
×
895
  }
896

897
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
×
898
  void*   pBackend = taskAcquireDb(dbRefId);
×
899
  if (pBackend == NULL) {
×
900
    stError("s-task:%s failed to acquire db during update checkpoint data, failed to upload checkpointData",
×
901
            pTask->id.idStr);
902
    return -1;
×
903
  }
904

905
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
×
906
  taskReleaseDb(dbRefId);
×
907

908
  return code;
×
909
}
910

911
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
×
912
  int32_t      code = TSDB_CODE_SUCCESS;
×
913
  int64_t      startTs = pTask->chkInfo.startTs;
×
914
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
×
915
  const char*  id = pTask->id.idStr;
×
916
  SStreamMeta* pMeta = pTask->pMeta;
×
917

918
  streamMutexLock(&pTask->lock);
×
919
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
×
920
  streamMutexUnlock(&pTask->lock);
×
921

922
  // sink task does not need to save the status, and generated the checkpoint
923
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
×
924
    stDebug("s-task:%s level:%d start gen checkpoint, checkpointId:%" PRId64, id, pTask->info.taskLevel, ckId);
×
925

926
    int64_t ver = pTask->chkInfo.processedVer;
×
927
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
×
928
    if (code != TSDB_CODE_SUCCESS) {
×
929
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
930
    }
931

932
    int64_t et = taosGetTimestampMs();
×
933
    stDebug("s-task:%s gen local checkpoint completed, elapsed time:%.2fs", id, (et - startTs) / 1000.0);
×
934
  }
935

936
  // TODO: monitoring the checkpoint-source msg
937
  // send check point response to upstream task
938
  if (code == TSDB_CODE_SUCCESS) {
×
939
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
940
      code = streamTaskSendCheckpointSourceRsp(pTask);
×
941
    } else {
942
      code = streamTaskSendCheckpointReadyMsg(pTask);
×
943
    }
944

945
    if (code != TSDB_CODE_SUCCESS) {
×
946
      // todo: let's retry send rsp to mnode, checkpoint-ready has monitor now
947
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%" PRId64 ", code:%s", id, ckId,
×
948
              tstrerror(code));
949
    }
950
  }
951

952
  if (code == TSDB_CODE_SUCCESS) {
×
953
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
×
954
    if (code != TSDB_CODE_SUCCESS) {
×
955
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
×
956
    }
957
  } else {
958
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
959
  }
960

961
  // TODO: monitoring the checkpoint-report msg
962
  // update the latest checkpoint info if all works are done successfully, for rsma, the pMsgCb is null.
963
  if (code == TSDB_CODE_SUCCESS) {
×
964
    if (pTask->pMsgCb != NULL) {
×
965
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
×
966
    }
967
  } else {  // clear the checkpoint info if failed
968
    // set failed checkpoint id before clear the checkpoint info
969
    streamMutexLock(&pTask->lock);
×
970
    streamTaskSetFailedCheckpointId(pTask, ckId);
×
971
    streamMutexUnlock(&pTask->lock);
×
972

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

977
  double el = (taosGetTimestampMs() - startTs) / 1000.0;
×
978
  stInfo("s-task:%s vgId:%d level:%d, checkpointId:%" PRId64 " ver:%" PRId64 " elapsed time:%.2fs, %s ", id,
×
979
         pMeta->vgId, pTask->info.taskLevel, ckId, pTask->chkInfo.checkpointVer, el,
980
         (code == TSDB_CODE_SUCCESS) ? "succ" : "failed");
981

982
  return code;
×
983
}
984

985
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
×
986
  const char*            id = pTask->id.idStr;
×
987
  int32_t                vgId = pTask->pMeta->vgId;
×
988
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
989
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
990

991
  // checkpoint-trigger recv flag is set, quit
992
  if (pActiveInfo->allUpstreamTriggerRecv) {
×
993
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
994
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
×
995
    return -1;
×
996
  }
997

998
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
×
999
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1000
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
×
1001
           ", quit",
1002
           id, vgId, pTmrInfo->launchChkptId);
1003
    return -1;
×
1004
  }
1005

1006
  // active checkpoint info is cleared for now
1007
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
×
1008
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1009
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from retrieve checkpoint-trigger send tmr", id,
×
1010
           vgId);
1011
    return -1;
×
1012
  }
1013

1014
  return 0;
×
1015
}
1016

1017
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
×
1018
  const char*            id = pTask->id.idStr;
×
1019
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1020

1021
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
×
1022
  if (pNotSendList == NULL) {
×
1023
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
1024
    return terrno;
×
1025
  }
1026

1027
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1028
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
×
1029

1030
    bool recved = false;
×
1031
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
×
1032
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
×
1033
      if (pReady == NULL) {
×
1034
        continue;
×
1035
      }
1036

1037
      if (pInfo->nodeId == pReady->upstreamNodeId) {
×
1038
        recved = true;
×
1039
        break;
×
1040
      }
1041
    }
1042

1043
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
×
1044
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
×
1045
      void* px = taosArrayPush(pNotSendList, pInfo);
×
1046
      if (px == NULL) {
×
1047
        stError("s-task:%s failed to record not send info, code: out of memory", id);
×
1048
        taosArrayDestroy(pNotSendList);
×
1049
        return terrno;
×
1050
      }
1051
    }
1052
  }
1053

1054
  *ppNotSendList = pNotSendList;
×
1055
  return 0;
×
1056
}
1057

1058
int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray** ppNotSendList) {
×
1059
  const char*            id = pTask->id.idStr;
×
1060
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
×
1061
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1062
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
1063
  int32_t                vgId = pTask->pMeta->vgId;
×
1064

1065
  int32_t code = doChkptStatusCheck(pTask, param);
×
1066
  if (code) {
×
1067
    return code;
×
1068
  }
1069

1070
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
×
1071
  if (code) {
×
1072
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1073
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1074
    return code;
×
1075
  }
1076

1077
  // do send retrieve checkpoint trigger msg to upstream
1078
  code = doSendRetrieveTriggerMsg(pTask, *ppNotSendList);
×
1079
  if (code) {
×
1080
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1081
    code = 0;
×
1082
  }
1083

1084
  return code;
×
1085
}
1086

1087
static void doCleanup(SStreamTask* pTask, SArray* pList) {
×
1088
  streamMetaReleaseTask(pTask->pMeta, pTask);
×
1089
  taosArrayDestroy(pList);
×
1090
}
×
1091

1092
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
×
1093
  int32_t      code = 0;
×
1094
  int32_t      numOfNotSend = 0;
×
1095
  SArray*      pNotSendList = NULL;
×
1096
  int64_t      taskRefId = *(int64_t*)param;
×
1097
  int64_t      now = taosGetTimestampMs();
×
1098

1099
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
×
1100
  if (pTask == NULL) {
×
1101
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
×
1102
    streamTaskFreeRefId(param);
×
1103
    return;
×
1104
  }
1105

1106
  int32_t                vgId = pTask->pMeta->vgId;
×
1107
  const char*            id = pTask->id.idStr;
×
1108
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
×
1109
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1110
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
1111

1112
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1113
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1114
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1115
    doCleanup(pTask, pNotSendList);
×
1116
    return;
×
1117
  }
1118

1119
  // check the status every 100ms
1120
  if (streamTaskShouldStop(pTask)) {
×
1121
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1122
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
×
1123
    doCleanup(pTask, pNotSendList);
×
1124
    return;
×
1125
  }
1126

1127
  if (++pTmrInfo->activeCounter < 50) {
×
1128
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1129
                   "trigger-recv-monitor");
1130
    doCleanup(pTask, pNotSendList);
×
1131
    return;
×
1132
  }
1133

1134
  pTmrInfo->activeCounter = 0;
×
1135
  stDebug("s-task:%s vgId:%d checkpoint-trigger monitor in tmr, ts:%" PRId64, id, vgId, now);
×
1136

1137
  streamMutexLock(&pTask->lock);
×
1138
  SStreamTaskState state = streamTaskGetStatus(pTask);
×
1139
  streamMutexUnlock(&pTask->lock);
×
1140

1141
  if (state.state != TASK_STATUS__CK) {
×
1142
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1143
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
×
1144
            vgId, state.name);
1145
    doCleanup(pTask, pNotSendList);
×
1146
    return;
×
1147
  }
1148

1149
  streamMutexLock(&pActiveInfo->lock);
×
1150
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
×
1151
  streamMutexUnlock(&pActiveInfo->lock);
×
1152

1153
  if (code != TSDB_CODE_SUCCESS) {
×
1154
    doCleanup(pTask, pNotSendList);
×
1155
    return;
×
1156
  }
1157

1158
  // check every 100ms
1159
  numOfNotSend = taosArrayGetSize(pNotSendList);
×
1160
  if (numOfNotSend > 0) {
×
1161
    stDebug("s-task:%s start to monitor checkpoint-trigger in 10s", id);
×
1162
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1163
                   "trigger-recv-monitor");
1164
  } else {
1165
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1166
    stDebug("s-task:%s all checkpoint-trigger recved, quit from monitor checkpoint-trigger tmr", id);
×
1167
  }
1168

1169
  doCleanup(pTask, pNotSendList);
×
1170
}
1171

1172
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
×
1173
  int32_t     code = 0;
×
1174
  int32_t     vgId = pTask->pMeta->vgId;
×
1175
  const char* pId = pTask->id.idStr;
×
1176
  int32_t     size = taosArrayGetSize(pNotSendList);
×
1177
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
×
1178
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1179

1180
  if (size <= 0) {
×
1181
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1182
    return code;
×
1183
  }
1184

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

1188
  for (int32_t i = 0; i < size; i++) {
×
1189
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
×
1190
    if (pUpstreamTask == NULL) {
×
1191
      return TSDB_CODE_INVALID_PARA;
×
1192
    }
1193

1194
    int32_t  ret = 0;
×
1195
    int32_t  tlen = 0;
×
1196
    void*    buf = NULL;
×
1197
    SRpcMsg  rpcMsg = {0};
×
1198
    SEncoder encoder;
1199

1200
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
×
1201
                                    .downstreamTaskId = pTask->id.taskId,
×
1202
                                    .downstreamNodeId = vgId,
1203
                                    .upstreamTaskId = pUpstreamTask->taskId,
×
1204
                                    .upstreamNodeId = pUpstreamTask->nodeId,
×
1205
                                    .checkpointId = checkpointId};
1206

1207
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
×
1208
    if (ret < 0) {
×
1209
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1210
    }
1211

1212
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
×
1213
    if (buf == NULL) {
×
1214
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1215
      continue;
×
1216
    }
1217

1218
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
×
1219
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
1220

1221
    tEncoderInit(&encoder, abuf, tlen);
×
1222
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
×
1223
      rpcFreeCont(buf);
×
1224
      tEncoderClear(&encoder);
×
1225
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1226
      continue;
×
1227
    }
1228
    tEncoderClear(&encoder);
×
1229

1230
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
×
1231

1232
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
×
1233
    if (code == TSDB_CODE_SUCCESS) {
×
1234
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
×
1235
              vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1236
    } else {
1237
      stError("s-task:%s vgId:%d failed to send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64,
×
1238
              pId, vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1239
    }
1240
  }
1241

1242
  return code;
×
1243
}
1244

1245
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1246
  int64_t                now = taosGetTimestampMs();
×
1247
  const char*            id = pTask->id.idStr;
×
1248
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1249
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1250

1251
  if (!pInfo->dispatchTrigger) {
×
1252
    return false;
×
1253
  }
1254

1255
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
×
1256
  for (int32_t i = 0; i < num; ++i) {
×
1257
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1258
    if (pSendInfo == NULL) {
×
1259
      stError("s-task:%s invalid index in dispatch-trigger list, index:%d, size:%d, ignore and continue", id, i, num);
×
1260
      continue;
×
1261
    }
1262

1263
    if (pSendInfo->nodeId != downstreamNodeId) {
×
1264
      continue;
×
1265
    }
1266

1267
    // has send trigger msg to downstream node,
1268
    double before = (now - pSendInfo->sendTs) / 1000.0;
×
1269
    if (pSendInfo->recved) {
×
1270
      stWarn("s-task:%s checkpoint-trigger msg already send at:%" PRId64
×
1271
             "(%.2fs before) and recv confirmed by downstream:0x%x, checkpointId:%" PRId64 ", transId:%d",
1272
             id, pSendInfo->sendTs, before, pSendInfo->taskId, pInfo->activeId, pInfo->transId);
1273
    } else {
1274
      stWarn("s-task:%s checkpoint-trigger already send at:%" PRId64 "(%.2fs before), checkpointId:%" PRId64
×
1275
             ", transId:%d",
1276
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1277
    }
1278

1279
    return true;
×
1280
  }
1281

1282
  return false;
×
1283
}
1284

1285
bool streamTaskAlreadySendTrigger(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1286
  int64_t                now = taosGetTimestampMs();
×
1287
  const char*            id = pTask->id.idStr;
×
1288
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1289
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1290

1291
  if (pStatus.state != TASK_STATUS__CK) {
×
1292
    return false;
×
1293
  }
1294

1295
  streamMutexLock(&pInfo->lock);
×
1296
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
×
1297
  streamMutexUnlock(&pInfo->lock);
×
1298

1299
  return send;
×
1300
}
1301

1302
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
×
1303
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
×
1304

1305
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1306
    *pTotal = 1;
×
1307
  } else {
1308
    *pTotal = streamTaskGetNumOfUpstream(pTask);
×
1309
  }
1310
}
×
1311

1312
// record the dispatch checkpoint trigger info in the list
1313
// memory insufficient may cause the stream computing stopped
1314
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
×
1315
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1316
  int64_t                now = taosGetTimestampMs();
×
1317
  int32_t                code = 0;
×
1318

1319
  streamMutexLock(&pInfo->lock);
×
1320

1321
  if (sendingChkptId > pInfo->failedId) {
×
1322
    pInfo->dispatchTrigger = true;
×
1323
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
×
1324
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
×
1325

1326
      STaskTriggerSendInfo p = {
×
1327
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
×
1328
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
×
1329
      if (px == NULL) {  // pause the stream task, if memory not enough
×
1330
        code = terrno;
×
1331
      }
1332
    } else {
1333
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
×
1334
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
×
1335
        if (pVgInfo == NULL) {
×
1336
          continue;
×
1337
        }
1338

1339
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
×
1340
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
×
1341
        if (px == NULL) {  // pause the stream task, if memory not enough
×
1342
          code = terrno;
×
1343
          break;
×
1344
        }
1345
      }
1346
    }
1347
  }
1348

1349
  streamMutexUnlock(&pInfo->lock);
×
1350

1351
  return code;
×
1352
}
1353

1354
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
×
1355
  int32_t num = 0;
×
1356
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
×
1357
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1358
    if (p == NULL) {
×
1359
      continue;
×
1360
    }
1361

1362
    if (p->recved) {
×
1363
      num++;
×
1364
    }
1365
  }
1366
  return num;
×
1367
}
1368

1369
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
×
1370
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1371

1372
  int64_t now = taosGetTimestampMs();
×
1373
  int32_t taskId = 0;
×
1374
  int32_t total = streamTaskGetNumOfDownstream(pTask);
×
1375
  bool    alreadyRecv = false;
×
1376

1377
  streamMutexLock(&pInfo->lock);
×
1378

1379
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
×
1380
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1381
    if (p == NULL) {
×
1382
      continue;
×
1383
    }
1384

1385
    if (p->nodeId == vgId) {
×
1386
      if (p->recved) {
×
1387
        stWarn("s-task:%s already recv checkpoint-trigger msg rsp from vgId:%d down:0x%x %.2fs ago, req send:%" PRId64
×
1388
               " discard",
1389
               pTask->id.idStr, vgId, p->taskId, (now - p->recvTs) / 1000.0, p->sendTs);
1390
        alreadyRecv = true;
×
1391
      } else {
1392
        p->recved = true;
×
1393
        p->recvTs = taosGetTimestampMs();
×
1394
        taskId = p->taskId;
×
1395
      }
1396
      break;
×
1397
    }
1398
  }
1399

1400
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
×
1401
  streamMutexUnlock(&pInfo->lock);
×
1402

1403
  if (taskId == 0) {
×
1404
    stError("s-task:%s recv invalid trigger-dispatch confirm, vgId:%d", pTask->id.idStr, vgId);
×
1405
  } else {
1406
    if (!alreadyRecv) {
×
1407
      stDebug("s-task:%s set downstream:0x%x(vgId:%d) checkpoint-trigger dispatch confirmed, total confirmed:%d/%d",
×
1408
              pTask->id.idStr, taskId, vgId, numOfConfirmed, total);
1409
    }
1410
  }
1411
}
×
1412

1413
int32_t uploadCheckpointToS3(const char* id, const char* path) {
×
1414
  int32_t code = 0;
×
1415
  int32_t nBytes = 0;
×
1416
  /*
1417
  if (s3Init() != 0) {
1418
    return TSDB_CODE_THIRDPARTY_ERROR;
1419
  }
1420
  */
1421
  TdDirPtr pDir = taosOpenDir(path);
×
1422
  if (pDir == NULL) {
×
1423
    return terrno;
×
1424
  }
1425

1426
  TdDirEntryPtr de = NULL;
×
1427
  while ((de = taosReadDir(pDir)) != NULL) {
×
1428
    char* name = taosGetDirEntryName(de);
×
1429
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
×
1430

1431
    char filename[PATH_MAX] = {0};
×
1432
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
×
1433
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
1434
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1435
        code = TSDB_CODE_OUT_OF_RANGE;
×
1436
        break;
×
1437
      }
1438
    } else {
1439
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
×
1440
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1441
        code = TSDB_CODE_OUT_OF_RANGE;
×
1442
        break;
×
1443
      }
1444
    }
1445

1446
    char object[PATH_MAX] = {0};
×
1447
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
×
1448
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
1449
      code = TSDB_CODE_OUT_OF_RANGE;
×
1450
      break;
×
1451
    }
1452

1453
    code = tcsPutObjectFromFile2(filename, object, 0);
×
1454
    if (code != 0) {
×
1455
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
×
1456
    } else {
1457
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1458
    }
1459
  }
1460

1461
  int32_t ret = taosCloseDir(&pDir);
×
1462
  if (code == 0 && ret != 0) {
×
1463
    code = ret;
×
1464
  }
1465

1466
  return code;
×
1467
}
1468

1469
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
×
1470
  int32_t nBytes;
1471
  int32_t cap = strlen(id) + strlen(dstName) + 16;
×
1472

1473
  char* buf = taosMemoryCalloc(1, cap);
×
1474
  if (buf == NULL) {
×
1475
    return terrno;
×
1476
  }
1477

1478
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
×
1479
  if (nBytes <= 0 || nBytes >= cap) {
×
1480
    taosMemoryFree(buf);
×
1481
    return TSDB_CODE_OUT_OF_RANGE;
×
1482
  }
1483
  int32_t code = tcsGetObjectToFile(buf, dstName);
×
1484
  if (code != 0) {
×
1485
    taosMemoryFree(buf);
×
1486
    return TAOS_SYSTEM_ERROR(errno);
×
1487
  }
1488
  taosMemoryFree(buf);
×
1489
  return 0;
×
1490
}
1491

1492
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
×
1493
  if (strlen(tsSnodeAddress) != 0) {
×
1494
    return DATA_UPLOAD_RSYNC;
×
1495
  } else if (tsS3StreamEnabled) {
×
1496
    return DATA_UPLOAD_S3;
×
1497
  } else {
1498
    return DATA_UPLOAD_DISABLE;
×
1499
  }
1500
}
1501

1502
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
×
1503
  int32_t code = 0;
×
1504
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
1505
    stError("invalid parameters in upload checkpoint, %s", id);
×
1506
    return TSDB_CODE_INVALID_CFG;
×
1507
  }
1508

1509
  if (strlen(tsSnodeAddress) != 0) {
×
1510
    code = uploadByRsync(id, path, checkpointId);
×
1511
    if (code != 0) {
×
1512
      return TAOS_SYSTEM_ERROR(errno);
×
1513
    }
1514
  } else if (tsS3StreamEnabled) {
×
1515
    return uploadCheckpointToS3(id, path);
×
1516
  }
1517

1518
  return 0;
×
1519
}
1520

1521
// fileName:  CURRENT
1522
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
×
1523
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
×
1524
    stError("down load checkpoint data parameters invalid");
×
1525
    return TSDB_CODE_INVALID_PARA;
×
1526
  }
1527

1528
  if (strlen(tsSnodeAddress) != 0) {
×
1529
    return 0;
×
1530
  } else if (tsS3StreamEnabled) {
×
1531
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1532
  }
1533

1534
  return 0;
×
1535
}
1536

1537
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
×
1538
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
1539
    stError("down checkpoint data parameters invalid");
×
1540
    return -1;
×
1541
  }
1542

1543
  if (strlen(tsSnodeAddress) != 0) {
×
1544
    return downloadByRsync(id, path, checkpointId);
×
1545
  } else if (tsS3StreamEnabled) {
×
1546
    return tcsGetObjectsByPrefix(id, path);
×
1547
  }
1548

1549
  return 0;
×
1550
}
1551

1552
#ifdef BUILD_NO_CALL
1553
int32_t deleteCheckpoint(const char* id) {
1554
  if (id == NULL || strlen(id) == 0) {
1555
    stError("deleteCheckpoint parameters invalid");
1556
    return TSDB_CODE_INVALID_PARA;
1557
  }
1558
  if (strlen(tsSnodeAddress) != 0) {
1559
    return deleteRsync(id);
1560
  } else if (tsS3StreamEnabled) {
1561
    tcsDeleteObjectsByPrefix(id);
1562
  }
1563
  return 0;
1564
}
1565
#endif
1566

1567
int32_t deleteCheckpointFile(const char* id, const char* name) {
×
1568
  char object[128] = {0};
×
1569

1570
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
×
1571
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
1572
    return TSDB_CODE_OUT_OF_RANGE;
×
1573
  }
1574

1575
  char*   tmp = object;
×
1576
  int32_t code = tcsDeleteObjects((const char**)&tmp, 1);
×
1577
  if (code != 0) {
×
1578
    return TSDB_CODE_THIRDPARTY_ERROR;
×
1579
  }
1580
  return code;
×
1581
}
1582

1583
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
×
1584
  streamMutexLock(&pTask->lock);
×
1585
  ETaskStatus p = streamTaskGetStatus(pTask).state;
×
1586
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
×
1587
  streamMutexUnlock(&pTask->lock);
×
1588

1589
  if (pTask->pBackend != NULL) {
×
1590
    streamFreeTaskState(pTask, p);
×
1591
    pTask->pBackend = NULL;
×
1592
  }
1593

1594
  streamMetaWLock(pTask->pMeta);
×
1595
  if (pTask->exec.pExecutor != NULL) {
×
1596
    qDestroyTask(pTask->exec.pExecutor);
×
1597
    pTask->exec.pExecutor = NULL;
×
1598
  }
1599
  streamMetaWUnLock(pTask->pMeta);
×
1600

1601
  return 0;
×
1602
}
1603

1604
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
×
1605
  int32_t code = 0;
×
1606
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
×
1607
    return code;
×
1608
  }
1609

1610
  streamMutexLock(&pTask->lock);
×
1611
  SStreamTaskState p = streamTaskGetStatus(pTask);
×
1612
  if (p.state == TASK_STATUS__CK) {
×
1613
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1614
  }
1615
  streamMutexUnlock(&pTask->lock);
×
1616

1617
  return code;
×
1618
}
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