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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 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
        type == TASK_OUTPUT__VTABLE_MAP) {
412
      stDebug("s-task:%s set childIdx:%d, and add checkpoint-trigger block into outputQ", id, pTask->info.selfChildId);
×
413
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);  // todo handle this failure
×
414
    } else {  // only one task exists, no need to dispatch downstream info
415
      code =
416
          appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT, pActiveInfo->activeId, pActiveInfo->transId, -1);
×
417
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
418
    }
419
  } else if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
×
420
    // todo: handle this
421
    // update the child Id for downstream tasks
422
    code = streamAddCheckpointReadyMsg(pTask, pBlock->srcTaskId, pTask->info.selfChildId, checkpointId);
×
423

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

430
    int32_t num = streamTaskGetNumOfUpstream(pTask);
×
431
    if (taskLevel == TASK_LEVEL__SINK) {
×
432
      stDebug("s-task:%s process checkpoint-trigger block, all %d upstreams sent, send ready msg to upstream", id, num);
×
433
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
434
      code = streamTaskBuildCheckpoint(pTask);  // todo: not handle error yet
×
435
    } else {                                    // source & agg tasks need to forward the checkpoint msg downwards
436
      stDebug("s-task:%s process checkpoint-trigger block, all %d upstreams sent, forwards to downstream", id, num);
×
437
      code = flushStateDataInExecutor(pTask, (SStreamQueueItem*)pBlock);
×
438
      if (code) {
×
439
        return code;
×
440
      }
441

442
      // Put the checkpoint-trigger block into outputQ, to make sure all blocks with less version have been handled by
443
      // this task already. And then, dispatch check point msg to all downstream tasks
444
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);
×
445
    }
446
  }
447

448
  return code;
×
449
}
450

451
// only when all downstream tasks are send checkpoint rsp, we can start the checkpoint procedure for the agg task
452
static int32_t processCheckpointReadyHelp(SActiveCheckpointInfo* pInfo, int32_t numOfDownstream,
×
453
                                          int32_t downstreamNodeId, int64_t streamId, int32_t downstreamTaskId,
454
                                          const char* id, int32_t* pNotReady, int32_t* pTransId, bool* alreadyRecv) {
455
  *alreadyRecv = false;
×
456
  int32_t size = taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
×
457
  for (int32_t i = 0; i < size; ++i) {
×
458
    STaskDownstreamReadyInfo* p = taosArrayGet(pInfo->pCheckpointReadyRecvList, i);
×
459
    if (p == NULL) {
×
460
      return TSDB_CODE_INVALID_PARA;
×
461
    }
462

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

469
  if (*alreadyRecv) {
×
470
    stDebug("s-task:%s already recv checkpoint-ready msg from downstream:0x%x, ignore. %d/%d downstream not ready", id,
×
471
            downstreamTaskId, (int32_t)(numOfDownstream - taosArrayGetSize(pInfo->pCheckpointReadyRecvList)),
472
            numOfDownstream);
473
  } else {
474
    STaskDownstreamReadyInfo info = {.recvTs = taosGetTimestampMs(),
×
475
                                     .downstreamTaskId = downstreamTaskId,
476
                                     .checkpointId = pInfo->activeId,
×
477
                                     .transId = pInfo->transId,
×
478
                                     .streamId = streamId,
479
                                     .downstreamNodeId = downstreamNodeId};
480
    void*                    p = taosArrayPush(pInfo->pCheckpointReadyRecvList, &info);
×
481
    if (p == NULL) {
×
482
      stError("s-task:%s failed to set checkpoint ready recv msg, code:%s", id, tstrerror(terrno));
×
483
      return terrno;
×
484
    }
485
  }
486

487
  *pNotReady = numOfDownstream - taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
×
488
  *pTransId = pInfo->transId;
×
489
  return 0;
×
490
}
491

492
/**
493
 * All down stream tasks have successfully completed the check point task.
494
 * Current stream task is allowed to start to do checkpoint things in ASYNC model.
495
 */
496
int32_t streamProcessCheckpointReadyMsg(SStreamTask* pTask, int64_t checkpointId, int32_t downstreamNodeId,
×
497
                                        int32_t downstreamTaskId) {
498
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
499

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

507
  // 1. not in checkpoint status now
508
  SStreamTaskState pStat = streamTaskGetStatus(pTask);
×
509
  if (pStat.state != TASK_STATUS__CK) {
×
510
    stError("s-task:%s status:%s discard checkpoint-ready msg from task:0x%x", id, pStat.name, downstreamTaskId);
×
511
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
512
  }
513

514
  // 2. expired checkpoint-ready msg, invalid checkpoint-ready msg
515
  if (pTask->chkInfo.checkpointId > checkpointId || pInfo->activeId != checkpointId) {
×
516
    stError("s-task:%s status:%s checkpointId:%" PRId64 " new arrival checkpoint-ready msg (checkpointId:%" PRId64
×
517
            ") from task:0x%x, expired and discard",
518
            id, pStat.name, pTask->chkInfo.checkpointId, checkpointId, downstreamTaskId);
519
    return TSDB_CODE_INVALID_MSG;
×
520
  }
521

522
  streamMutexLock(&pInfo->lock);
×
523
  code = processCheckpointReadyHelp(pInfo, total, downstreamNodeId, pTask->id.streamId, downstreamTaskId, id, &notReady,
×
524
                                    &transId, &alreadyHandled);
525
  streamMutexUnlock(&pInfo->lock);
×
526

527
  if (alreadyHandled) {
×
528
    stDebug("s-task:%s checkpoint-ready msg checkpointId:%" PRId64 " from task:0x%x already handled, not handle again",
×
529
            id, checkpointId, downstreamTaskId);
530
  } else {
531
    if ((notReady == 0) && (code == 0) && (!alreadyHandled)) {
×
532
      stDebug("s-task:%s all downstream tasks have completed build checkpoint, do checkpoint for current task", id);
×
533
      code = appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT, checkpointId, transId, -1);
×
534
    }
535
  }
536

537
  return code;
×
538
}
539

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

545
  streamMutexLock(&pInfo->lock);
×
546
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
×
547
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
×
548
    if (pReadyInfo == NULL) {
×
549
      stError("s-task:%s invalid index during iterate the checkpoint-ready msg list, index:%d, ignore and continue",
×
550
              pTask->id.idStr, i);
551
      continue;
×
552
    }
553

554
    if (pReadyInfo->upstreamTaskId == upstreamTaskId && pReadyInfo->checkpointId == checkpointId) {
×
555
      pReadyInfo->sendCompleted = 1;
×
556
      stDebug("s-task:%s send checkpoint-ready msg to upstream:0x%x confirmed, checkpointId:%" PRId64 " ts:%" PRId64,
×
557
              pTask->id.idStr, upstreamTaskId, checkpointId, now);
558
      break;
×
559
    }
560
  }
561

562
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pReadyMsgList); ++i) {
×
563
    STaskCheckpointReadyInfo* pReadyInfo = taosArrayGet(pInfo->pReadyMsgList, i);
×
564
    if (pReadyInfo == NULL) {
×
565
      stError("s-task:%s invalid index during iterate the checkpoint-ready msg list, index:%d, ignore and continue",
×
566
              pTask->id.idStr, i);
567
      continue;
×
568
    }
569

570
    if (pReadyInfo->sendCompleted == 1) {
×
571
      numOfConfirmed += 1;
×
572
    }
573
  }
574

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

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

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

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

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

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

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

609
  *pContinue = true;
×
610

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

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

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

628
    { // destroy the related fill-history tasks
629
      if (pReq->dropRelHTask) {
×
630
        if (pTask->info.trigger != STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE) {
×
631
          code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
×
632

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

638
          // todo: task may not exist, commit anyway, optimize this later
639
          code = streamMetaCommit(pMeta);
×
640
        }
641
      }
642
    }
643

644
    *pContinue = false;
×
645
    // always return true
646
    return TSDB_CODE_SUCCESS;
×
647
  }
648

649
  SStreamTaskState status = streamTaskGetStatus(pTask);
×
650

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

671
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
×
672
                pInfo->processedVer <= pReq->checkpointVer);
×
673

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

681
      stFatal("s-task:%s checkpointId update info recv, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
682
              " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64 " discard it",
683
              id, pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId,
684
              pReq->checkpointVer);
685

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

696
  return TSDB_CODE_SUCCESS;
×
697
}
698

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

707
  streamMutexLock(&pTask->lock);
×
708
  code = doUpdateCheckpointInfoCheck(pTask, restored, pReq, &continueUpdate);
×
709

710
  if (!continueUpdate) {
×
711
    streamMutexUnlock(&pTask->lock);
×
712
    return code;
×
713
  }
714

715
  SStreamTaskState pStatus = streamTaskGetStatus(pTask);
×
716

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

723
    if (restored && (pMeta->role == NODE_ROLE_LEADER)) {
×
724
      code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_CHECKPOINT_DONE);
×
725
    }
726
  }
727

728
  streamTaskClearCheckInfo(pTask, true);
×
729

730
  if (pReq->dropRelHTask) {
×
731
    if (pTask->info.trigger != STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE) {
×
732
      stInfo("s-task:0x%x vgId:%d drop the related fill-history task:0x%" PRIx64 " after update checkpoint",
×
733
              pReq->taskId, vgId, pReq->hTaskId);
734
      CLEAR_RELATED_FILLHISTORY_TASK(pTask);
×
735
    } else {
736
      stInfo("s-task:0x%x vgId:%d update the related fill-history task:0x%" PRIx64" to be recalculate task",
×
737
             pReq->taskId, vgId, pReq->hTaskId);
738
    }
739
  }
740

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

744
  pTask->status.taskStatus = TASK_STATUS__READY;
×
745

746
  code = streamMetaSaveTaskInMeta(pMeta, pTask);
×
747
  streamMutexUnlock(&pTask->lock);
×
748

749
  if (code != TSDB_CODE_SUCCESS) {
×
750
    stError("s-task:%s vgId:%d failed to save task info after do checkpoint, checkpointId:%" PRId64 ", since %s", id,
×
751
            vgId, pReq->checkpointId, tstrerror(code));
752
    return TSDB_CODE_SUCCESS;
×
753
  }
754

755
  // drop task should not in the meta-lock, and drop the related fill-history task now
756
  if (pReq->dropRelHTask) {
×
757
    if (pTask->info.trigger != STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE) {
×
758
      code = streamMetaUnregisterTask(pMeta, pReq->hStreamId, pReq->hTaskId);
×
759
      int32_t numOfTasks = streamMetaGetNumOfTasks(pMeta);
×
760
      stDebug("s-task:%s vgId:%d related fill-history task:0x%x dropped, remain tasks:%d", id, vgId,
×
761
              (int32_t)pReq->hTaskId, numOfTasks);
762
    } else {
763
      STaskId hTaskId = {.streamId = pReq->hStreamId, .taskId = pReq->hTaskId};
×
764

765
      SStreamTask* pHTask = NULL;
×
766
      code = streamMetaAcquireTaskUnsafe(pMeta, &hTaskId, &pHTask);
×
767
      if (code == 0 && pHTask != NULL) {
×
768
        stInfo("s-task:0x%x fill-history updated to recalculate task, reset step2Start ts, stream task:0x%x",
×
769
               (int32_t)hTaskId.taskId, pReq->taskId);
770

771
        streamMutexLock(&pHTask->lock);
×
772

773
        pHTask->info.fillHistory = STREAM_RECALCUL_TASK;
×
774
        pHTask->execInfo.step2Start = 0; // clear the step2start timestamp
×
775

776
        SStreamTaskState status = streamTaskGetStatus(pHTask);
×
777
        if (status.state == TASK_STATUS__SCAN_HISTORY) {
×
778
          code = streamTaskHandleEvent(pHTask->status.pSM, TASK_EVENT_SCANHIST_DONE);
×
779
        }
780

781
        if (pHTask->pBackend != NULL) {
×
782
          streamFreeTaskState(pHTask, TASK_STATUS__READY);
×
783
          pHTask->pBackend = NULL;
×
784
        }
785

786
        if (pHTask->exec.pExecutor != NULL) {
×
787
          qDestroyTask(pHTask->exec.pExecutor);
×
788
          pHTask->exec.pExecutor = NULL;
×
789
        }
790

791
        pMeta->expandTaskFn(pHTask);
×
792

793
        streamMutexUnlock(&pHTask->lock);
×
794

795
        code = streamMetaSaveTaskInMeta(pMeta, pHTask);
×
796
        streamMetaReleaseTask(pMeta, pHTask);
×
797
      }
798
    }
799
  }
800

801
  code = streamMetaCommit(pMeta);
×
802
  return TSDB_CODE_SUCCESS;
×
803
}
804

805
void streamTaskSetFailedCheckpointId(SStreamTask* pTask, int64_t failedId) {
×
806
  struct SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
807

808
  if (failedId <= 0) {
×
809
    stWarn("s-task:%s failedId is 0, not update the failed checkpoint info, current failedId:%" PRId64
×
810
           " activeId:%" PRId64,
811
           pTask->id.idStr, pInfo->failedId, pInfo->activeId);
812
  } else {
813
    if (failedId <= pInfo->failedId) {
×
814
      stDebug("s-task:%s failedId:%" PRId64 " not update to:%" PRId64, pTask->id.idStr, pInfo->failedId, failedId);
×
815
    } else {
816
      stDebug("s-task:%s mark and set the failed checkpointId:%" PRId64 " (transId:%d) activeId:%" PRId64
×
817
              " prev failedId:%" PRId64,
818
              pTask->id.idStr, failedId, pInfo->transId, pInfo->activeId, pInfo->failedId);
819
      pInfo->failedId = failedId;
×
820
    }
821
  }
822
}
×
823

824
void streamTaskSetCheckpointFailed(SStreamTask* pTask) {
×
825
  streamMutexLock(&pTask->lock);
×
826
  ETaskStatus status = streamTaskGetStatus(pTask).state;
×
827
  if (status == TASK_STATUS__CK) {
×
828
    streamTaskSetFailedCheckpointId(pTask, pTask->chkInfo.pActiveInfo->activeId);
×
829
  }
830
  streamMutexUnlock(&pTask->lock);
×
831
}
×
832

833
static int32_t getCheckpointDataMeta(const char* id, const char* path, SArray* list) {
×
834
  int32_t code = 0;
×
835
  int32_t cap = strlen(path) + 64;
×
836

837
  char* filePath = taosMemoryCalloc(1, cap);
×
838
  if (filePath == NULL) {
×
839
    return terrno;
×
840
  }
841

842
  int32_t nBytes = snprintf(filePath, cap, "%s%s%s", path, TD_DIRSEP, "META_TMP");
×
843
  if (nBytes <= 0 || nBytes >= cap) {
×
844
    taosMemoryFree(filePath);
×
845
    return TSDB_CODE_OUT_OF_RANGE;
×
846
  }
847

848
  code = downloadCheckpointDataByName(id, "META", filePath);
×
849
  if (code != 0) {
×
850
    stError("%s chkp failed to download meta file:%s", id, filePath);
×
851
    taosMemoryFree(filePath);
×
852
    return code;
×
853
  }
854

855
  code = remoteChkpGetDelFile(filePath, list);
×
856
  if (code != 0) {
×
857
    stError("%s chkp failed to get to del:%s", id, filePath);
×
858
    taosMemoryFree(filePath);
×
859
  }
860
  return 0;
×
861
}
862

863
int32_t uploadCheckpointData(SStreamTask* pTask, int64_t checkpointId, int64_t dbRefId, ECHECKPOINT_BACKUP_TYPE type) {
×
864
  int32_t      code = 0;
×
865
  char*        path = NULL;
×
866
  int64_t      chkptSize = 0;
×
867
  SStreamMeta* pMeta = pTask->pMeta;
×
868
  const char*  idStr = pTask->id.idStr;
×
869
  int64_t      now = taosGetTimestampMs();
×
870

871
  SArray* toDelFiles = taosArrayInit(4, POINTER_BYTES);
×
872
  if (toDelFiles == NULL) {
×
873
    stError("s-task:%s failed to prepare array list during upload checkpoint, code:%s", pTask->id.idStr,
×
874
            tstrerror(terrno));
875
    return terrno;
×
876
  }
877

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

883
  if (type == DATA_UPLOAD_S3) {
×
884
    if (code == TSDB_CODE_SUCCESS && (code = getCheckpointDataMeta(idStr, path, toDelFiles)) != 0) {
×
885
      stError("s-task:%s failed to get checkpointData for checkpointId:%" PRId64 ", reason:%s", idStr, checkpointId,
×
886
              tstrerror(code));
887
    }
888
  }
889

890
  if (code == TSDB_CODE_SUCCESS) {
×
891
    code = streamTaskUploadCheckpoint(idStr, path, checkpointId);
×
892
    if (code == TSDB_CODE_SUCCESS) {
×
893
      stDebug("s-task:%s upload checkpointId:%" PRId64 " to remote succ", idStr, checkpointId);
×
894
    } else {
895
      stError("s-task:%s failed to upload checkpointId:%" PRId64 " path:%s,reason:%s", idStr, checkpointId, path,
×
896
              tstrerror(code));
897
    }
898
  }
899

900
  int32_t num = taosArrayGetSize(toDelFiles);
×
901
  if (code == TSDB_CODE_SUCCESS && num > 0) {
×
902
    stDebug("s-task:%s remove redundant %d files", idStr, num);
×
903

904
    for (int i = 0; i < num; i++) {
×
905
      char* pName = taosArrayGetP(toDelFiles, i);
×
906
      code = deleteCheckpointFile(idStr, pName);
×
907
      if (code != 0) {
×
908
        stDebug("s-task:%s failed to remove file: %s", idStr, pName);
×
909
        break;
×
910
      }
911
    }
912

913
    stDebug("s-task:%s remove redundant files in uploading checkpointId:%" PRId64 " data", idStr, checkpointId);
×
914
  }
915

916
  taosArrayDestroyP(toDelFiles, NULL);
×
917
  double el = (taosGetTimestampMs() - now) / 1000.0;
×
918

919
  if (code == TSDB_CODE_SUCCESS) {
×
920
    code = taosGetDirSize(path, &chkptSize);
×
921
    stDebug("s-task:%s complete upload checkpointId:%" PRId64
×
922
            ", elapsed time:%.2fs, checkpointSize:%.2fKiB local dir:%s",
923
            idStr, checkpointId, el, SIZE_IN_KiB(chkptSize), path);
924
  } else {
925
    stDebug("s-task:%s failed to upload checkpointId:%" PRId64 " elapsed time:%.2fs, checkpointSize:%.2fKiB", idStr,
×
926
            checkpointId, el, SIZE_IN_KiB(chkptSize));
927
  }
928

929
  taosMemoryFree(path);
×
930
  return code;
×
931
}
932

933
int32_t streamTaskRemoteBackupCheckpoint(SStreamTask* pTask, int64_t checkpointId) {
×
934
  ECHECKPOINT_BACKUP_TYPE type = streamGetCheckpointBackupType();
×
935
  if (type == DATA_UPLOAD_DISABLE) {
×
936
    stDebug("s-task:%s not config to backup checkpoint data at snode, checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
937
    return 0;
×
938
  }
939

940
  if (pTask == NULL || pTask->pBackend == NULL) {
×
941
    return 0;
×
942
  }
943

944
  int64_t dbRefId = taskGetDBRef(pTask->pBackend);
×
945
  void*   pBackend = taskAcquireDb(dbRefId);
×
946
  if (pBackend == NULL) {
×
947
    stError("s-task:%s failed to acquire db during update checkpoint data, failed to upload checkpointData",
×
948
            pTask->id.idStr);
949
    return -1;
×
950
  }
951

952
  int32_t code = uploadCheckpointData(pTask, checkpointId, taskGetDBRef(pTask->pBackend), type);
×
953
  taskReleaseDb(dbRefId);
×
954

955
  return code;
×
956
}
957

958
int32_t streamTaskBuildCheckpoint(SStreamTask* pTask) {
×
959
  int32_t      code = TSDB_CODE_SUCCESS;
×
960
  int64_t      startTs = pTask->chkInfo.startTs;
×
961
  int64_t      ckId = pTask->chkInfo.pActiveInfo->activeId;
×
962
  const char*  id = pTask->id.idStr;
×
963
  SStreamMeta* pMeta = pTask->pMeta;
×
964

965
  streamMutexLock(&pTask->lock);
×
966
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
×
967
  streamMutexUnlock(&pTask->lock);
×
968

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

973
    int64_t ver = pTask->chkInfo.processedVer;
×
974
    code = streamBackendDoCheckpoint(pTask->pBackend, ckId, ver);
×
975
    if (code != TSDB_CODE_SUCCESS) {
×
976
      stError("s-task:%s gen checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(terrno));
×
977
    }
978

979
    int64_t et = taosGetTimestampMs();
×
980
    stDebug("s-task:%s gen local checkpoint completed, elapsed time:%.2fs", id, (et - startTs) / 1000.0);
×
981
  }
982

983
  // TODO: monitoring the checkpoint-source msg
984
  // send check point response to upstream task
985
  if (code == TSDB_CODE_SUCCESS) {
×
986
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
987
      code = streamTaskSendCheckpointSourceRsp(pTask);
×
988
    } else {
989
      code = streamTaskSendCheckpointReadyMsg(pTask);
×
990
    }
991

992
    if (code != TSDB_CODE_SUCCESS) {
×
993
      // todo: let's retry send rsp to mnode, checkpoint-ready has monitor now
994
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%" PRId64 ", code:%s", id, ckId,
×
995
              tstrerror(code));
996
    }
997
  }
998

999
  if (code == TSDB_CODE_SUCCESS) {
×
1000
    code = streamTaskRemoteBackupCheckpoint(pTask, ckId);
×
1001
    if (code != TSDB_CODE_SUCCESS) {
×
1002
      stError("s-task:%s upload checkpointId:%" PRId64 " data failed, code:%s", id, ckId, tstrerror(code));
×
1003
    }
1004
  } else {
1005
    stError("s-task:%s taskInfo failed, checkpoint:%" PRId64 " failed, code:%s", id, ckId, tstrerror(code));
×
1006
  }
1007

1008
  // TODO: monitoring the checkpoint-report msg
1009
  // update the latest checkpoint info if all works are done successfully, for rsma, the pMsgCb is null.
1010
  if (code == TSDB_CODE_SUCCESS) {
×
1011
    if (pTask->pMsgCb != NULL) {
×
1012
      code = streamSendChkptReportMsg(pTask, &pTask->chkInfo, dropRelHTask);
×
1013
    }
1014
  } else {  // clear the checkpoint info if failed
1015
    // set failed checkpoint id before clear the checkpoint info
1016
    streamMutexLock(&pTask->lock);
×
1017
    streamTaskSetFailedCheckpointId(pTask, ckId);
×
1018
    streamMutexUnlock(&pTask->lock);
×
1019

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

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

1029
  return code;
×
1030
}
1031

1032
static int32_t doChkptStatusCheck(SStreamTask* pTask, void* param) {
×
1033
  const char*            id = pTask->id.idStr;
×
1034
  int32_t                vgId = pTask->pMeta->vgId;
×
1035
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1036
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
1037

1038
  // checkpoint-trigger recv flag is set, quit
1039
  if (pActiveInfo->allUpstreamTriggerRecv) {
×
1040
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1041
    stDebug("s-task:%s vgId:%d all checkpoint-trigger recv, quit from monitor checkpoint-trigger", id, vgId);
×
1042
    return -1;
×
1043
  }
1044

1045
  if (pTmrInfo->launchChkptId != pActiveInfo->activeId) {
×
1046
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1047
    stWarn("s-task:%s vgId:%d checkpoint-trigger retrieve by previous checkpoint procedure, checkpointId:%" PRId64
×
1048
           ", quit",
1049
           id, vgId, pTmrInfo->launchChkptId);
1050
    return -1;
×
1051
  }
1052

1053
  // active checkpoint info is cleared for now
1054
  if ((pActiveInfo->activeId == 0) || (pActiveInfo->transId == 0) || (pTask->chkInfo.startTs == 0)) {
×
1055
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1056
    stWarn("s-task:%s vgId:%d active checkpoint may be cleared, quit from retrieve checkpoint-trigger send tmr", id,
×
1057
           vgId);
1058
    return -1;
×
1059
  }
1060

1061
  return 0;
×
1062
}
1063

1064
static int32_t doFindNotSendUpstream(SStreamTask* pTask, SArray* pList, SArray** ppNotSendList) {
×
1065
  const char*            id = pTask->id.idStr;
×
1066
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1067

1068
  SArray* pNotSendList = taosArrayInit(4, sizeof(SStreamUpstreamEpInfo));
×
1069
  if (pNotSendList == NULL) {
×
1070
    stDebug("s-task:%s start to triggerMonitor, reason:%s", id, tstrerror(terrno));
×
1071
    return terrno;
×
1072
  }
1073

1074
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1075
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pList, i);
×
1076

1077
    bool recved = false;
×
1078
    for (int32_t j = 0; j < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++j) {
×
1079
      STaskCheckpointReadyInfo* pReady = taosArrayGet(pActiveInfo->pReadyMsgList, j);
×
1080
      if (pReady == NULL) {
×
1081
        continue;
×
1082
      }
1083

1084
      if (pInfo->nodeId == pReady->upstreamNodeId) {
×
1085
        recved = true;
×
1086
        break;
×
1087
      }
1088
    }
1089

1090
    if (!recved) {  // make sure the inputQ is opened for not recv upstream checkpoint-trigger message
×
1091
      streamTaskOpenUpstreamInput(pTask, pInfo->taskId);
×
1092
      void* px = taosArrayPush(pNotSendList, pInfo);
×
1093
      if (px == NULL) {
×
1094
        stError("s-task:%s failed to record not send info, code: out of memory", id);
×
1095
        taosArrayDestroy(pNotSendList);
×
1096
        return terrno;
×
1097
      }
1098
    }
1099
  }
1100

1101
  *ppNotSendList = pNotSendList;
×
1102
  return 0;
×
1103
}
1104

1105
int32_t chkptTriggerRecvMonitorHelper(SStreamTask* pTask, void* param, SArray** ppNotSendList) {
×
1106
  const char*            id = pTask->id.idStr;
×
1107
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
×
1108
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1109
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
1110
  int32_t                vgId = pTask->pMeta->vgId;
×
1111

1112
  int32_t code = doChkptStatusCheck(pTask, param);
×
1113
  if (code) {
×
1114
    return code;
×
1115
  }
1116

1117
  code = doFindNotSendUpstream(pTask, pList, ppNotSendList);
×
1118
  if (code) {
×
1119
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1120
    stDebug("s-task:%s failed to find not send upstream, code:%s, out of tmr", id, tstrerror(code));
×
1121
    return code;
×
1122
  }
1123

1124
  // do send retrieve checkpoint trigger msg to upstream
1125
  code = doSendRetrieveTriggerMsg(pTask, *ppNotSendList);
×
1126
  if (code) {
×
1127
    stError("s-task:%s vgId:%d failed to retrieve trigger msg, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
1128
    code = 0;
×
1129
  }
1130

1131
  return code;
×
1132
}
1133

1134
static void doCleanup(SStreamTask* pTask, SArray* pList) {
×
1135
  streamMetaReleaseTask(pTask->pMeta, pTask);
×
1136
  taosArrayDestroy(pList);
×
1137
}
×
1138

1139
void checkpointTriggerMonitorFn(void* param, void* tmrId) {
×
1140
  int32_t      code = 0;
×
1141
  int32_t      numOfNotSend = 0;
×
1142
  SArray*      pNotSendList = NULL;
×
1143
  int64_t      taskRefId = *(int64_t*)param;
×
1144
  int64_t      now = taosGetTimestampMs();
×
1145

1146
  SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, taskRefId);
×
1147
  if (pTask == NULL) {
×
1148
    stError("invalid task rid:%" PRId64 " failed to acquired stream-task at %s", taskRefId, __func__);
×
1149
    streamTaskFreeRefId(param);
×
1150
    return;
×
1151
  }
1152

1153
  int32_t                vgId = pTask->pMeta->vgId;
×
1154
  const char*            id = pTask->id.idStr;
×
1155
  SArray*                pList = pTask->upstreamInfo.pList;  // send msg to retrieve checkpoint trigger msg
×
1156
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
×
1157
  SStreamTmrInfo*        pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
×
1158

1159
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1160
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1161
    stError("s-task:%s source task should not start the checkpoint-trigger monitor fn, quit", id);
×
1162
    doCleanup(pTask, pNotSendList);
×
1163
    return;
×
1164
  }
1165

1166
  // check the status every 100ms
1167
  if (streamTaskShouldStop(pTask)) {
×
1168
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1169
    stDebug("s-task:%s vgId:%d quit from monitor checkpoint-trigger", id, vgId);
×
1170
    doCleanup(pTask, pNotSendList);
×
1171
    return;
×
1172
  }
1173

1174
  if (++pTmrInfo->activeCounter < 50) {
×
1175
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1176
                   "trigger-recv-monitor");
1177
    doCleanup(pTask, pNotSendList);
×
1178
    return;
×
1179
  }
1180

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

1184
  streamMutexLock(&pTask->lock);
×
1185
  SStreamTaskState state = streamTaskGetStatus(pTask);
×
1186
  streamMutexUnlock(&pTask->lock);
×
1187

1188
  if (state.state != TASK_STATUS__CK) {
×
1189
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1190
    stDebug("s-task:%s vgId:%d status:%s not in checkpoint status, quit from monitor checkpoint-trigger", id,
×
1191
            vgId, state.name);
1192
    doCleanup(pTask, pNotSendList);
×
1193
    return;
×
1194
  }
1195

1196
  streamMutexLock(&pActiveInfo->lock);
×
1197
  code = chkptTriggerRecvMonitorHelper(pTask, param, &pNotSendList);
×
1198
  streamMutexUnlock(&pActiveInfo->lock);
×
1199

1200
  if (code != TSDB_CODE_SUCCESS) {
×
1201
    doCleanup(pTask, pNotSendList);
×
1202
    return;
×
1203
  }
1204

1205
  // check every 100ms
1206
  numOfNotSend = taosArrayGetSize(pNotSendList);
×
1207
  if (numOfNotSend > 0) {
×
1208
    stDebug("s-task:%s start to monitor checkpoint-trigger in 10s", id);
×
1209
    streamTmrStart(checkpointTriggerMonitorFn, 200, param, streamTimer, &pTmrInfo->tmrHandle, vgId,
×
1210
                   "trigger-recv-monitor");
1211
  } else {
1212
    streamCleanBeforeQuitTmr(pTmrInfo, param);
×
1213
    stDebug("s-task:%s all checkpoint-trigger recved, quit from monitor checkpoint-trigger tmr", id);
×
1214
  }
1215

1216
  doCleanup(pTask, pNotSendList);
×
1217
}
1218

1219
int32_t doSendRetrieveTriggerMsg(SStreamTask* pTask, SArray* pNotSendList) {
×
1220
  int32_t     code = 0;
×
1221
  int32_t     vgId = pTask->pMeta->vgId;
×
1222
  const char* pId = pTask->id.idStr;
×
1223
  int32_t     size = taosArrayGetSize(pNotSendList);
×
1224
  int32_t     numOfUpstream = streamTaskGetNumOfUpstream(pTask);
×
1225
  int64_t     checkpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1226

1227
  if (size <= 0) {
×
1228
    stDebug("s-task:%s all upstream checkpoint trigger recved, no need to send retrieve", pId);
×
1229
    return code;
×
1230
  }
1231

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

1235
  for (int32_t i = 0; i < size; i++) {
×
1236
    SStreamUpstreamEpInfo* pUpstreamTask = taosArrayGet(pNotSendList, i);
×
1237
    if (pUpstreamTask == NULL) {
×
1238
      return TSDB_CODE_INVALID_PARA;
×
1239
    }
1240

1241
    int32_t  ret = 0;
×
1242
    int32_t  tlen = 0;
×
1243
    void*    buf = NULL;
×
1244
    SRpcMsg  rpcMsg = {0};
×
1245
    SEncoder encoder;
1246

1247
    SRetrieveChkptTriggerReq req = {.streamId = pTask->id.streamId,
×
1248
                                    .downstreamTaskId = pTask->id.taskId,
×
1249
                                    .downstreamNodeId = vgId,
1250
                                    .upstreamTaskId = pUpstreamTask->taskId,
×
1251
                                    .upstreamNodeId = pUpstreamTask->nodeId,
×
1252
                                    .checkpointId = checkpointId};
1253

1254
    tEncodeSize(tEncodeRetrieveChkptTriggerReq, &req, tlen, ret);
×
1255
    if (ret < 0) {
×
1256
      stError("encode retrieve checkpoint-trigger msg failed, code:%s", tstrerror(code));
×
1257
    }
1258

1259
    buf = rpcMallocCont(tlen + sizeof(SMsgHead));
×
1260
    if (buf == NULL) {
×
1261
      stError("vgId:%d failed to create retrieve checkpoint-trigger msg for task:%s exec, code:out of memory", vgId, pId);
×
1262
      continue;
×
1263
    }
1264

1265
    ((SRetrieveChkptTriggerReq*)buf)->head.vgId = htonl(pUpstreamTask->nodeId);
×
1266
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
1267

1268
    tEncoderInit(&encoder, abuf, tlen);
×
1269
    if ((code = tEncodeRetrieveChkptTriggerReq(&encoder, &req)) < 0) {
×
1270
      rpcFreeCont(buf);
×
1271
      tEncoderClear(&encoder);
×
1272
      stError("encode retrieve checkpoint-trigger req failed, code:%s", tstrerror(code));
×
1273
      continue;
×
1274
    }
1275
    tEncoderClear(&encoder);
×
1276

1277
    initRpcMsg(&rpcMsg, TDMT_STREAM_RETRIEVE_TRIGGER, buf, tlen + sizeof(SMsgHead));
×
1278

1279
    code = tmsgSendReq(&pUpstreamTask->epSet, &rpcMsg);
×
1280
    if (code == TSDB_CODE_SUCCESS) {
×
1281
      stDebug("s-task:%s vgId:%d send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64, pId,
×
1282
              vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1283
    } else {
1284
      stError("s-task:%s vgId:%d failed to send checkpoint-trigger retrieve msg to 0x%x(vgId:%d) checkpointId:%" PRId64,
×
1285
              pId, vgId, pUpstreamTask->taskId, pUpstreamTask->nodeId, checkpointId);
1286
    }
1287
  }
1288

1289
  return code;
×
1290
}
1291

1292
static int32_t isAlreadySendTriggerNoLock(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1293
  int64_t                now = taosGetTimestampMs();
×
1294
  const char*            id = pTask->id.idStr;
×
1295
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1296
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1297

1298
  if (!pInfo->dispatchTrigger) {
×
1299
    return false;
×
1300
  }
1301

1302
  int32_t num = taosArrayGetSize(pInfo->pDispatchTriggerList);
×
1303
  for (int32_t i = 0; i < num; ++i) {
×
1304
    STaskTriggerSendInfo* pSendInfo = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1305
    if (pSendInfo == NULL) {
×
1306
      stError("s-task:%s invalid index in dispatch-trigger list, index:%d, size:%d, ignore and continue", id, i, num);
×
1307
      continue;
×
1308
    }
1309

1310
    if (pSendInfo->nodeId != downstreamNodeId) {
×
1311
      continue;
×
1312
    }
1313

1314
    // has send trigger msg to downstream node,
1315
    double before = (now - pSendInfo->sendTs) / 1000.0;
×
1316
    if (pSendInfo->recved) {
×
1317
      stWarn("s-task:%s checkpoint-trigger msg already send at:%" PRId64
×
1318
             "(%.2fs before) and recv confirmed by downstream:0x%x, checkpointId:%" PRId64 ", transId:%d",
1319
             id, pSendInfo->sendTs, before, pSendInfo->taskId, pInfo->activeId, pInfo->transId);
1320
    } else {
1321
      stWarn("s-task:%s checkpoint-trigger already send at:%" PRId64 "(%.2fs before), checkpointId:%" PRId64
×
1322
             ", transId:%d",
1323
             id, pSendInfo->sendTs, before, pInfo->activeId, pInfo->transId);
1324
    }
1325

1326
    return true;
×
1327
  }
1328

1329
  return false;
×
1330
}
1331

1332
bool streamTaskAlreadySendTrigger(SStreamTask* pTask, int32_t downstreamNodeId) {
×
1333
  int64_t                now = taosGetTimestampMs();
×
1334
  const char*            id = pTask->id.idStr;
×
1335
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1336
  SStreamTaskState       pStatus = streamTaskGetStatus(pTask);
×
1337

1338
  if (pStatus.state != TASK_STATUS__CK) {
×
1339
    return false;
×
1340
  }
1341

1342
  streamMutexLock(&pInfo->lock);
×
1343
  bool send = isAlreadySendTriggerNoLock(pTask, downstreamNodeId);
×
1344
  streamMutexUnlock(&pInfo->lock);
×
1345

1346
  return send;
×
1347
}
1348

1349
void streamTaskGetTriggerRecvStatus(SStreamTask* pTask, int32_t* pRecved, int32_t* pTotal) {
×
1350
  *pRecved = taosArrayGetSize(pTask->chkInfo.pActiveInfo->pReadyMsgList);
×
1351

1352
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1353
    *pTotal = 1;
×
1354
  } else {
1355
    *pTotal = streamTaskGetNumOfUpstream(pTask);
×
1356
  }
1357
}
×
1358

1359
// record the dispatch checkpoint trigger info in the list
1360
// memory insufficient may cause the stream computing stopped
1361
int32_t streamTaskInitTriggerDispatchInfo(SStreamTask* pTask, int64_t sendingChkptId) {
×
1362
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1363
  int64_t                now = taosGetTimestampMs();
×
1364
  int32_t                code = 0;
×
1365

1366
  streamMutexLock(&pInfo->lock);
×
1367

1368
  if (sendingChkptId > pInfo->failedId) {
×
1369
    pInfo->dispatchTrigger = true;
×
1370
    if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
×
1371
      STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
×
1372

1373
      STaskTriggerSendInfo p = {
×
1374
          .sendTs = now, .recved = false, .nodeId = pDispatch->nodeId, .taskId = pDispatch->taskId};
×
1375
      void* px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
×
1376
      if (px == NULL) {  // pause the stream task, if memory not enough
×
1377
        code = terrno;
×
1378
      }
1379
    } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
×
1380
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
×
1381
        STaskDispatcherFixed* pAddr = taosArrayGet(pTask->outputInfo.vtableMapDispatcher.taskInfos, i);
×
1382
        if (pAddr == NULL) {
×
1383
          continue;
×
1384
        }
1385

1386
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pAddr->nodeId, .taskId = pAddr->taskId};
×
1387
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
×
1388
        if (px == NULL) {  // pause the stream task, if memory not enough
×
1389
          code = terrno;
×
1390
          break;
×
1391
        }
1392
      }
1393
    } else {
1394
      for (int32_t i = 0; i < streamTaskGetNumOfDownstream(pTask); ++i) {
×
1395
        SVgroupInfo* pVgInfo = taosArrayGet(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos, i);
×
1396
        if (pVgInfo == NULL) {
×
1397
          continue;
×
1398
        }
1399

1400
        STaskTriggerSendInfo p = {.sendTs = now, .recved = false, .nodeId = pVgInfo->vgId, .taskId = pVgInfo->taskId};
×
1401
        void*                px = taosArrayPush(pInfo->pDispatchTriggerList, &p);
×
1402
        if (px == NULL) {  // pause the stream task, if memory not enough
×
1403
          code = terrno;
×
1404
          break;
×
1405
        }
1406
      }
1407
    }
1408
  }
1409

1410
  streamMutexUnlock(&pInfo->lock);
×
1411

1412
  return code;
×
1413
}
1414

1415
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
×
1416
  int32_t num = 0;
×
1417
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
×
1418
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1419
    if (p == NULL) {
×
1420
      continue;
×
1421
    }
1422

1423
    if (p->recved) {
×
1424
      num++;
×
1425
    }
1426
  }
1427
  return num;
×
1428
}
1429

1430
void streamTaskSetTriggerDispatchConfirmed(SStreamTask* pTask, int32_t vgId) {
×
1431
  SActiveCheckpointInfo* pInfo = pTask->chkInfo.pActiveInfo;
×
1432

1433
  int64_t now = taosGetTimestampMs();
×
1434
  int32_t taskId = 0;
×
1435
  int32_t total = streamTaskGetNumOfDownstream(pTask);
×
1436
  bool    alreadyRecv = false;
×
1437

1438
  streamMutexLock(&pInfo->lock);
×
1439

1440
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
×
1441
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
×
1442
    if (p == NULL) {
×
1443
      continue;
×
1444
    }
1445

1446
    if (p->nodeId == vgId) {
×
1447
      if (p->recved) {
×
1448
        stWarn("s-task:%s already recv checkpoint-trigger msg rsp from vgId:%d down:0x%x %.2fs ago, req send:%" PRId64
×
1449
               " discard",
1450
               pTask->id.idStr, vgId, p->taskId, (now - p->recvTs) / 1000.0, p->sendTs);
1451
        alreadyRecv = true;
×
1452
      } else {
1453
        p->recved = true;
×
1454
        p->recvTs = taosGetTimestampMs();
×
1455
        taskId = p->taskId;
×
1456
      }
1457
      break;
×
1458
    }
1459
  }
1460

1461
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
×
1462
  streamMutexUnlock(&pInfo->lock);
×
1463

1464
  if (taskId == 0) {
×
1465
    stError("s-task:%s recv invalid trigger-dispatch confirm, vgId:%d", pTask->id.idStr, vgId);
×
1466
  } else {
1467
    if (!alreadyRecv) {
×
1468
      stDebug("s-task:%s set downstream:0x%x(vgId:%d) checkpoint-trigger dispatch confirmed, total confirmed:%d/%d",
×
1469
              pTask->id.idStr, taskId, vgId, numOfConfirmed, total);
1470
    }
1471
  }
1472
}
×
1473

1474
int32_t uploadCheckpointToS3(const char* id, const char* path) {
×
1475
  int32_t code = 0;
×
1476
  int32_t nBytes = 0;
×
1477
  /*
1478
  if (s3Init() != 0) {
1479
    return TSDB_CODE_THIRDPARTY_ERROR;
1480
  }
1481
  */
1482
  TdDirPtr pDir = taosOpenDir(path);
×
1483
  if (pDir == NULL) {
×
1484
    return terrno;
×
1485
  }
1486

1487
  TdDirEntryPtr de = NULL;
×
1488
  while ((de = taosReadDir(pDir)) != NULL) {
×
1489
    char* name = taosGetDirEntryName(de);
×
1490
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || taosDirEntryIsDir(de)) continue;
×
1491

1492
    char filename[PATH_MAX] = {0};
×
1493
    if (path[strlen(path) - 1] == TD_DIRSEP_CHAR) {
×
1494
      nBytes = snprintf(filename, sizeof(filename), "%s%s", path, name);
×
1495
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1496
        code = TSDB_CODE_OUT_OF_RANGE;
×
1497
        break;
×
1498
      }
1499
    } else {
1500
      nBytes = snprintf(filename, sizeof(filename), "%s%s%s", path, TD_DIRSEP, name);
×
1501
      if (nBytes <= 0 || nBytes >= sizeof(filename)) {
×
1502
        code = TSDB_CODE_OUT_OF_RANGE;
×
1503
        break;
×
1504
      }
1505
    }
1506

1507
    char object[PATH_MAX] = {0};
×
1508
    nBytes = snprintf(object, sizeof(object), "%s%s%s", id, TD_DIRSEP, name);
×
1509
    if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
1510
      code = TSDB_CODE_OUT_OF_RANGE;
×
1511
      break;
×
1512
    }
1513

1514
    code = tcsPutObjectFromFile2(filename, object, 0);
×
1515
    if (code != 0) {
×
1516
      stError("[tcs] failed to upload checkpoint:%s, reason:%s", filename, tstrerror(code));
×
1517
    } else {
1518
      stDebug("[tcs] upload checkpoint:%s", filename);
×
1519
    }
1520
  }
1521

1522
  int32_t ret = taosCloseDir(&pDir);
×
1523
  if (code == 0 && ret != 0) {
×
1524
    code = ret;
×
1525
  }
1526

1527
  return code;
×
1528
}
1529

1530
int32_t downloadCheckpointByNameS3(const char* id, const char* fname, const char* dstName) {
×
1531
  int32_t nBytes;
1532
  int32_t cap = strlen(id) + strlen(dstName) + 16;
×
1533

1534
  char* buf = taosMemoryCalloc(1, cap);
×
1535
  if (buf == NULL) {
×
1536
    return terrno;
×
1537
  }
1538

1539
  nBytes = snprintf(buf, cap, "%s/%s", id, fname);
×
1540
  if (nBytes <= 0 || nBytes >= cap) {
×
1541
    taosMemoryFree(buf);
×
1542
    return TSDB_CODE_OUT_OF_RANGE;
×
1543
  }
1544
  int32_t code = tcsGetObjectToFile(buf, dstName);
×
1545
  if (code != 0) {
×
1546
    taosMemoryFree(buf);
×
1547
    return TAOS_SYSTEM_ERROR(ERRNO);
×
1548
  }
1549
  taosMemoryFree(buf);
×
1550
  return 0;
×
1551
}
1552

1553
ECHECKPOINT_BACKUP_TYPE streamGetCheckpointBackupType() {
×
1554
  if (strlen(tsSnodeAddress) != 0) {
×
1555
    return DATA_UPLOAD_RSYNC;
×
1556
  } else if (tsS3StreamEnabled) {
×
1557
    return DATA_UPLOAD_S3;
×
1558
  } else {
1559
    return DATA_UPLOAD_DISABLE;
×
1560
  }
1561
}
1562

1563
int32_t streamTaskUploadCheckpoint(const char* id, const char* path, int64_t checkpointId) {
×
1564
  int32_t code = 0;
×
1565
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
1566
    stError("invalid parameters in upload checkpoint, %s", id);
×
1567
    return TSDB_CODE_INVALID_CFG;
×
1568
  }
1569

1570
  if (strlen(tsSnodeAddress) != 0) {
×
1571
    code = uploadByRsync(id, path, checkpointId);
×
1572
    if (code != 0) {
×
1573
      return TAOS_SYSTEM_ERROR(ERRNO);
×
1574
    }
1575
  } else if (tsS3StreamEnabled) {
×
1576
    return uploadCheckpointToS3(id, path);
×
1577
  }
1578

1579
  return 0;
×
1580
}
1581

1582
// fileName:  CURRENT
1583
int32_t downloadCheckpointDataByName(const char* id, const char* fname, const char* dstName) {
×
1584
  if (id == NULL || fname == NULL || strlen(id) == 0 || strlen(fname) == 0 || strlen(fname) >= PATH_MAX) {
×
1585
    stError("down load checkpoint data parameters invalid");
×
1586
    return TSDB_CODE_INVALID_PARA;
×
1587
  }
1588

1589
  if (strlen(tsSnodeAddress) != 0) {
×
1590
    return 0;
×
1591
  } else if (tsS3StreamEnabled) {
×
1592
    return downloadCheckpointByNameS3(id, fname, dstName);
×
1593
  }
1594

1595
  return 0;
×
1596
}
1597

1598
int32_t streamTaskDownloadCheckpointData(const char* id, char* path, int64_t checkpointId) {
×
1599
  if (id == NULL || path == NULL || strlen(id) == 0 || strlen(path) == 0 || strlen(path) >= PATH_MAX) {
×
1600
    stError("down checkpoint data parameters invalid");
×
1601
    return -1;
×
1602
  }
1603

1604
  if (strlen(tsSnodeAddress) != 0) {
×
1605
    return downloadByRsync(id, path, checkpointId);
×
1606
  } else if (tsS3StreamEnabled) {
×
1607
    return tcsGetObjectsByPrefix(id, path);
×
1608
  }
1609

1610
  return 0;
×
1611
}
1612

1613
#ifdef BUILD_NO_CALL
1614
int32_t deleteCheckpoint(const char* id) {
1615
  if (id == NULL || strlen(id) == 0) {
1616
    stError("deleteCheckpoint parameters invalid");
1617
    return TSDB_CODE_INVALID_PARA;
1618
  }
1619
  if (strlen(tsSnodeAddress) != 0) {
1620
    return deleteRsync(id);
1621
  } else if (tsS3StreamEnabled) {
1622
    tcsDeleteObjectsByPrefix(id);
1623
  }
1624
  return 0;
1625
}
1626
#endif
1627

1628
int32_t deleteCheckpointFile(const char* id, const char* name) {
×
1629
  char object[128] = {0};
×
1630

1631
  int32_t nBytes = snprintf(object, sizeof(object), "%s/%s", id, name);
×
1632
  if (nBytes <= 0 || nBytes >= sizeof(object)) {
×
1633
    return TSDB_CODE_OUT_OF_RANGE;
×
1634
  }
1635

1636
  char*   tmp = object;
×
1637
  int32_t code = tcsDeleteObjects((const char**)&tmp, 1);
×
1638
  if (code != 0) {
×
1639
    return TSDB_CODE_THIRDPARTY_ERROR;
×
1640
  }
1641
  return code;
×
1642
}
1643

1644
int32_t streamTaskSendNegotiateChkptIdMsg(SStreamTask* pTask) {
×
1645
  streamMutexLock(&pTask->lock);
×
1646
  ETaskStatus p = streamTaskGetStatus(pTask).state;
×
1647
  streamTaskSetReqConsenChkptId(pTask, taosGetTimestampMs());
×
1648
  streamMutexUnlock(&pTask->lock);
×
1649

1650
  // 1. stop the executo at first
1651
  if (pTask->exec.pExecutor != NULL) {
×
1652
    // we need to make sure the underlying operator is stopped right, otherwise, SIGSEG may occur,
1653
    // waiting at most for 10min
1654
    if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
×
1655
      int32_t code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS, 600000);
×
1656
      if (code != TSDB_CODE_SUCCESS) {
×
1657
        stError("s-task:%s failed to kill task related query handle, code:%s", pTask->id.idStr, tstrerror(code));
×
1658
      }
1659
    }
1660

1661
    qDestroyTask(pTask->exec.pExecutor);
×
1662
    pTask->exec.pExecutor = NULL;
×
1663
  }
1664

1665
  // 2. destroy backend after stop executor
1666
  if (pTask->pBackend != NULL) {
×
1667
    streamFreeTaskState(pTask, p);
×
1668
    pTask->pBackend = NULL;
×
1669
  }
1670

1671
  return 0;
×
1672
}
1673

1674
int32_t streamTaskSendCheckpointsourceRsp(SStreamTask* pTask) {
×
1675
  int32_t code = 0;
×
1676
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
×
1677
    return code;
×
1678
  }
1679

1680
  streamMutexLock(&pTask->lock);
×
1681
  SStreamTaskState p = streamTaskGetStatus(pTask);
×
1682
  if (p.state == TASK_STATUS__CK) {
×
1683
    code = streamTaskSendCheckpointSourceRsp(pTask);
×
1684
  }
1685
  streamMutexUnlock(&pTask->lock);
×
1686

1687
  return code;
×
1688
}
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