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

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

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

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

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

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

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

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

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

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

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

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

76
  *pRes = pChkpoint;
6,901✔
77

78
  taosMemoryFree(pBlock);
6,901✔
79
  return TSDB_CODE_SUCCESS;
6,908✔
80
}
81

82
// this message must be put into inputq successfully, continue retrying until it succeeds
83
// todo must be success
84
int32_t appendCheckpointIntoInputQ(SStreamTask* pTask, int32_t checkpointType, int64_t checkpointId, int32_t transId,
6,888✔
85
                                   int32_t srcTaskId) {
86
  SStreamDataBlock* pCheckpoint = NULL;
6,888✔
87
  int32_t code = createChkptTriggerBlock(pTask, checkpointType, checkpointId, transId, srcTaskId, &pCheckpoint);
6,888✔
88
  if (code != TSDB_CODE_SUCCESS) {
6,907!
89
    return code;
×
90
  }
91

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

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

99
int32_t streamProcessCheckpointSourceReq(SStreamTask* pTask, SStreamCheckpointSourceReq* pReq) {
3,349✔
100
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
3,349!
101
    return TSDB_CODE_INVALID_MSG;
×
102
  }
103

104
  // todo this status may not be set here.
105
  // 1. set task status to be prepared for check point, no data are allowed to put into inputQ.
106
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_GEN_CHECKPOINT);
3,349✔
107
  if (code != TSDB_CODE_SUCCESS) {
3,343!
108
    stError("s-task:%s failed to handle gen-checkpoint event, failed to start checkpoint procedure", pTask->id.idStr);
×
109
    return code;
×
110
  }
111

112
  pTask->chkInfo.pActiveInfo->transId = pReq->transId;
3,343✔
113
  pTask->chkInfo.pActiveInfo->activeId = pReq->checkpointId;
3,343✔
114
  pTask->chkInfo.startTs = taosGetTimestampMs();
3,336✔
115
  pTask->execInfo.checkpoint += 1;
3,336✔
116

117
  // 2. Put the checkpoint block into inputQ, to make sure all blocks with less version have been handled by this task
118
  // and this is the last item in the inputQ.
119
  return appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT_TRIGGER, pReq->checkpointId, pReq->transId, -1);
3,336✔
120
}
121

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

127
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
128
    stError("s-task:%s invalid msg recv, checkpoint-trigger rsp not handled", id);
×
129
    return TSDB_CODE_INVALID_MSG;
×
130
  }
131

132
  if (pRsp->rspCode != TSDB_CODE_SUCCESS) {
×
133
    stDebug("s-task:%s retrieve checkpoint-trgger rsp from upstream:0x%x invalid, code:%s", id, pRsp->upstreamTaskId,
×
134
            tstrerror(pRsp->rspCode));
135
    return TSDB_CODE_SUCCESS;
×
136
  }
137

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

142
  if (status.state != TASK_STATUS__CK) {
×
143
    stError("s-task:%s status:%s not in checkpoint status, discard the checkpoint-trigger msg", id, status.name);
×
144
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
145
  }
146

147
  streamMutexLock(&pInfo->lock);
×
148
  unQualified = (pInfo->activeId != pRsp->checkpointId || pInfo->transId != pRsp->transId);
×
149
  streamMutexUnlock(&pInfo->lock);
×
150

151
  if (unQualified) {
×
152
    stError("s-task:%s status:%s not in checkpoint status, discard the checkpoint-trigger msg", id, status.name);
×
153
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
154
  }
155

156
  // NOTE: here we do not do the duplicated checkpoint-trigger msg check, since it will be done by following functions.
157
  int32_t code = appendCheckpointIntoInputQ(pTask, STREAM_INPUT__CHECKPOINT_TRIGGER, pRsp->checkpointId, pRsp->transId,
×
158
                                            pRsp->upstreamTaskId);
159
  return code;
×
160
}
161

162
int32_t streamTaskSendCheckpointTriggerMsg(SStreamTask* pTask, int32_t dstTaskId, int32_t downstreamNodeId,
×
163
                                           SRpcHandleInfo* pRpcInfo, int32_t code) {
164
  int32_t  ret = 0;
×
165
  int32_t  tlen = 0;
×
166
  void*    buf = NULL;
×
167
  SEncoder encoder;
168

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

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

182
  tEncodeSize(tEncodeCheckpointTriggerRsp, &req, tlen, ret);
×
183
  if (ret < 0) {
×
184
    stError("s-task:%s encode checkpoint-trigger rsp msg failed, code:%s", pTask->id.idStr, tstrerror(code));
×
185
    return ret;
×
186
  }
187

188
  buf = rpcMallocCont(tlen + sizeof(SMsgHead));
×
189
  if (buf == NULL) {
×
190
    stError("s-task:%s malloc chkpt-trigger rsp failed for task:0x%x, since out of memory", pTask->id.idStr, dstTaskId);
×
191
    return terrno;
×
192
  }
193

194
  ((SMsgHead*)buf)->vgId = htonl(downstreamNodeId);
×
195
  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
×
196

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

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

209
  return ret;
×
210
}
211

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

216
  if (pTask->chkInfo.pActiveInfo->dispatchTrigger == true) {
3,383!
217
    stError("s-task:%s already dispatch checkpoint-trigger, not dispatch again", pTask->id.idStr);
×
218
    return 0;
×
219
  }
220

221
  int32_t code = taosWriteQitem(pTask->outputq.queue->pQueue, pBlock);
3,383✔
222
  if (code == 0) {
3,388!
223
    code = streamDispatchStreamBlock(pTask);
3,388✔
224
  } else {
225
    stError("s-task:%s failed to put checkpoint into outputQ, code:%s", pTask->id.idStr, tstrerror(code));
×
226
    streamFreeQitem((SStreamQueueItem*)pBlock);
×
227
  }
228

229
  return code;
3,388✔
230
}
231

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

239
  SActiveCheckpointInfo* pActiveInfo = pTask->chkInfo.pActiveInfo;
12,072✔
240
  if (pTask->chkInfo.checkpointId > checkpointId) {
12,072!
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) {
12,072!
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) {
12,072!
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) {
12,072✔
281
    if (pActiveInfo->activeId != checkpointId) {
8,727!
282
      stError("s-task:%s vgId:%d active checkpointId:%" PRId64 ", recv invalid checkpoint-trigger checkpointId:%" PRId64
×
283
              " discard",
284
              id, vgId, pActiveInfo->activeId, checkpointId);
285
      return TSDB_CODE_STREAM_INVLD_CHKPT;
×
286
    } else {  // checkpointId == pActiveInfo->activeId
287
      if (pActiveInfo->allUpstreamTriggerRecv == 1) {
8,727!
288
        stDebug(
×
289
            "s-task:%s vgId:%d all upstream checkpoint-trigger recv, discard this checkpoint-trigger, "
290
            "checkpointId:%" PRId64 " transId:%d",
291
            id, vgId, checkpointId, transId);
292
        return TSDB_CODE_STREAM_INVLD_CHKPT;
×
293
      }
294

295
      if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
8,727✔
296
        //  check if already recv or not, and duplicated checkpoint-trigger msg recv, discard it
297
        for (int32_t i = 0; i < taosArrayGetSize(pActiveInfo->pReadyMsgList); ++i) {
14,345✔
298
          STaskCheckpointReadyInfo* p = taosArrayGet(pActiveInfo->pReadyMsgList, i);
8,912✔
299
          if (p == NULL) {
8,934!
300
            return TSDB_CODE_INVALID_PARA;
×
301
          }
302

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

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

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

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

334
  streamMutexLock(&pTask->lock);
12,071✔
335
  code = doCheckBeforeHandleChkptTrigger(pTask, checkpointId, pBlock, transId);
12,085✔
336
  streamMutexUnlock(&pTask->lock);
12,060✔
337
  if (code) {
12,086!
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
12,086✔
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) {
12,087✔
351
    pActiveInfo->activeId = checkpointId;
3,341✔
352
    pActiveInfo->transId = transId;
3,341✔
353

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

359
    code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_GEN_CHECKPOINT);
3,342✔
360
    if (code != TSDB_CODE_SUCCESS) {
3,331!
361
      stError("s-task:%s handle checkpoint-trigger block failed, code:%s", id, tstrerror(code));
×
362
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
363
      return code;
×
364
    }
365

366
    // if previous launched timer not started yet, not start a new timer
367
    // todo: fix this bug: previous set checkpoint-trigger check tmr is running, while we happen to try to launch
368
    //  a new checkpoint-trigger timer right now.
369
    //  And if we don't start a new timer, and the lost of checkpoint-trigger message may cause the whole checkpoint
370
    //  procedure to be stucked.
371
    SStreamTmrInfo* pTmrInfo = &pActiveInfo->chkptTriggerMsgTmr;
3,331✔
372
    int8_t          old = atomic_val_compare_exchange_8(&pTmrInfo->isActive, 0, 1);
3,331✔
373
    if (old == 0) {
3,339!
374
      stDebug("s-task:%s start checkpoint-trigger monitor in 10s", pTask->id.idStr);
3,341✔
375

376
      int64_t* pTaskRefId = NULL;
3,341✔
377
      code = streamTaskAllocRefId(pTask, &pTaskRefId);
3,341✔
378
      if (code == 0) {
3,344!
379
        streamTmrStart(checkpointTriggerMonitorFn, 200, pTaskRefId, streamTimer, &pTmrInfo->tmrHandle, vgId,
3,344✔
380
                       "trigger-recv-monitor");
381
        pTmrInfo->launchChkptId = pActiveInfo->activeId;
3,344✔
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) {
12,069✔
393
    int8_t type = pTask->outputInfo.type;
3,318✔
394
    pActiveInfo->allUpstreamTriggerRecv = 1;
3,318✔
395

396
    // We need to transfer state here, before dispatching checkpoint-trigger to downstream tasks.
397
    // The transfer of state may generate new data that need to dispatch to downstream tasks,
398
    // Otherwise, those new generated data by executors that is kept in outputQ, may be lost if this program crashed
399
    // before the next checkpoint.
400
    code = flushStateDataInExecutor(pTask, (SStreamQueueItem*)pBlock);
3,318✔
401
    if (code) {
3,328!
402
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
403
      return code;
×
404
    }
405

406
#if 0
407
    chkptFailedByRetrieveReqToSource(pTask, checkpointId);
408
#endif
409

410
    if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
3,328✔
411
      stDebug("s-task:%s set childIdx:%d, and add checkpoint-trigger block into outputQ", id, pTask->info.selfChildId);
3,116✔
412
      code = continueDispatchCheckpointTriggerBlock(pBlock, pTask);  // todo handle this failure
3,116✔
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);
212✔
416
      streamFreeQitem((SStreamQueueItem*)pBlock);
212✔
417
    }
418
  } else if (taskLevel == TASK_LEVEL__SINK || taskLevel == TASK_LEVEL__AGG) {
8,751!
419
    // todo: handle this
420
    // update the child Id for downstream tasks
421
    code = streamAddCheckpointReadyMsg(pTask, pBlock->srcTaskId, pTask->info.selfChildId, checkpointId);
8,751✔
422

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

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

447
  return code;
6,666✔
448
}
449

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

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

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

486
  *pNotReady = numOfDownstream - taosArrayGetSize(pInfo->pCheckpointReadyRecvList);
8,689✔
487
  *pTransId = pInfo->transId;
8,686✔
488
  return 0;
8,686✔
489
}
490

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

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

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

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

521
  streamMutexLock(&pInfo->lock);
8,691✔
522
  code = processCheckpointReadyHelp(pInfo, total, downstreamNodeId, pTask->id.streamId, downstreamTaskId, id, &notReady,
8,731✔
523
                                    &transId, &alreadyHandled);
524
  streamMutexUnlock(&pInfo->lock);
8,686✔
525

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

536
  return code;
8,731✔
537
}
538

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

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

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

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

569
    if (pReadyInfo->sendCompleted == 1) {
26,482✔
570
      numOfConfirmed += 1;
17,588✔
571
    }
572
  }
573

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

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

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

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

587
  streamMutexLock(&pInfo->lock);
5,486✔
588
  streamTaskClearActiveInfo(pInfo);
5,503✔
589
  if (clearChkpReadyMsg) {
5,495!
590
    streamClearChkptReadyMsg(pInfo);
5,495✔
591
  }
592
  streamMutexUnlock(&pInfo->lock);
5,496✔
593

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

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

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

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

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

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

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

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

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

655
  bool valid = (pInfo->checkpointId <= pReq->checkpointId && pInfo->checkpointVer <= pReq->checkpointVer &&
11,000!
656
                pInfo->processedVer <= pReq->checkpointVer);
5,499!
657

658
  if (!valid) {
5,501!
659
    stFatal("invalid checkpoint id check, current checkpointId:%" PRId64 " checkpointVer:%" PRId64
×
660
            " processedVer:%" PRId64 " req checkpointId:%" PRId64 " checkpointVer:%" PRId64,
661
            pInfo->checkpointId, pInfo->checkpointVer, pInfo->processedVer, pReq->checkpointId, pReq->checkpointVer);
662
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
663
  }
664

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

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

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

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

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

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

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

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

698
  streamMetaWUnLock(pMeta);
5,503✔
699

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

708
  streamMetaWLock(pMeta);
5,502✔
709
  code = streamMetaCommit(pMeta);
5,501✔
710

711
  return TSDB_CODE_SUCCESS;
5,502✔
712
}
713

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

861
  return code;
×
862
}
863

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

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

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

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

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

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

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

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

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

929
  return code;
6,613✔
930
}
931

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

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

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

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

961
  return 0;
×
962
}
963

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

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

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

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

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

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

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

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

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

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

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

1031
  return code;
×
1032
}
1033

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1189
  return code;
×
1190
}
1191

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

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

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

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

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

1226
    return true;
×
1227
  }
1228

1229
  return false;
×
1230
}
1231

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

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

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

1246
  return send;
×
1247
}
1248

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

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

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

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

1268
  pInfo->dispatchTrigger = true;
3,388✔
1269
  if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
3,388✔
1270
    STaskDispatcherFixed* pDispatch = &pTask->outputInfo.fixedDispatcher;
593✔
1271

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

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

1293
  streamMutexUnlock(&pInfo->lock);
3,380✔
1294

1295
  return code;
3,387✔
1296
}
1297

1298
int32_t streamTaskGetNumOfConfirmed(SActiveCheckpointInfo* pInfo) {
8,765✔
1299
  int32_t num = 0;
8,765✔
1300
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
35,381✔
1301
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
26,595✔
1302
    if (p == NULL) {
26,616!
1303
      continue;
×
1304
    }
1305

1306
    if (p->recved) {
26,616✔
1307
      num++;
17,692✔
1308
    }
1309
  }
1310
  return num;
8,751✔
1311
}
1312

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

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

1321
  streamMutexLock(&pInfo->lock);
8,756✔
1322

1323
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pDispatchTriggerList); ++i) {
17,723!
1324
    STaskTriggerSendInfo* p = taosArrayGet(pInfo->pDispatchTriggerList, i);
17,712✔
1325
    if (p == NULL) {
17,709!
1326
      continue;
×
1327
    }
1328

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

1344
  int32_t numOfConfirmed = streamTaskGetNumOfConfirmed(pInfo);
8,765✔
1345
  streamMutexUnlock(&pInfo->lock);
8,753✔
1346

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

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

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

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

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

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

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

1410
  return code;
×
1411
}
1412

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

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

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

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

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

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

1462
  return 0;
×
1463
}
1464

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

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

1478
  return 0;
×
1479
}
1480

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

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

1493
  return 0;
×
1494
}
1495

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

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

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

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

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

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

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

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

1559
  return code;
60✔
1560
}
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