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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

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

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

43.07
/source/dnode/mnode/impl/src/mndStreamHb.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 "mndStream.h"
17
#include "mndTrans.h"
18
#include "mndMnode.h"
19
#include "tmisce.h"
20

21
typedef struct SFailedCheckpointInfo {
22
  int64_t streamUid;
23
  int64_t checkpointId;
24
  int32_t transId;
25
} SFailedCheckpointInfo;
26

27
static int32_t mndStreamSendUpdateChkptInfoMsg(SMnode *pMnode);
28
static int32_t mndSendDropOrphanTasksMsg(SMnode *pMnode, SArray *pList);
29
static int32_t mndSendResetFromCheckpointMsg(SMnode *pMnode, int64_t streamId, int32_t transId, int64_t checkpointId);
30
static void    updateStageInfo(STaskStatusEntry *pTaskEntry, int64_t stage);
31
static void    addIntoFailedChkptList(SArray *pList, const SFailedCheckpointInfo *pInfo);
32
static int32_t setNodeEpsetExpiredFlag(const SArray *pNodeList);
33
static int32_t suspendAllStreams(SMnode *pMnode, SRpcHandleInfo *info);
34
static bool    validateHbMsg(const SArray *pNodeList, int32_t vgId);
35
static void    cleanupAfterProcessHbMsg(SStreamHbMsg *pReq, SArray *pFailedChkptList, SArray *pOrphanTasks);
36
static void    doSendHbMsgRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SEpSet* pEpset, int32_t vgId, int32_t msgId);
37
static void    checkforOrphanTask(SMnode* pMnode, STaskStatusEntry* p, SArray* pOrphanTasks);
38

39
void updateStageInfo(STaskStatusEntry *pTaskEntry, int64_t stage) {
67✔
40
  int32_t numOfNodes = taosArrayGetSize(execInfo.pNodeList);
67✔
41
  for (int32_t j = 0; j < numOfNodes; ++j) {
129!
42
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, j);
129✔
43
    if (pNodeEntry == NULL) {
129!
44
      continue;
×
45
    }
46

47
    if (pNodeEntry->nodeId == pTaskEntry->nodeId) {
129✔
48
      mInfo("vgId:%d stage updated from %" PRId64 " to %" PRId64 ", nodeUpdate trigger by s-task:0x%" PRIx64,
67!
49
            pTaskEntry->nodeId, pTaskEntry->stage, stage, pTaskEntry->id.taskId);
50

51
      pNodeEntry->stageUpdated = true;
67✔
52
      pTaskEntry->stage = stage;
67✔
53
      break;
67✔
54
    }
55
  }
56
}
67✔
57

58
void addIntoFailedChkptList(SArray *pList, const SFailedCheckpointInfo *pInfo) {
×
59
  int32_t num = taosArrayGetSize(pList);
×
60
  for (int32_t i = 0; i < num; ++i) {
×
61
    SFailedCheckpointInfo *p = taosArrayGet(pList, i);
×
62
    if (p && (p->transId == pInfo->transId)) {
×
63
      return;
×
64
    }
65
  }
66

67
  void *p = taosArrayPush(pList, pInfo);
×
68
  if (p == NULL) {
×
69
    mError("failed to push failed checkpoint info checkpointId:%" PRId64 " in list", pInfo->checkpointId);
×
70
  }
71
}
72

73
int32_t mndCreateStreamResetStatusTrans(SMnode *pMnode, SStreamObj *pStream, int64_t chkptId) {
1✔
74
  STrans *pTrans = NULL;
1✔
75
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_RESET_NAME,
1✔
76
                               " reset from failed checkpoint", &pTrans);
77
  if (pTrans == NULL || code) {
1!
78
    sdbRelease(pMnode->pSdb, pStream);
×
79
    return terrno;
×
80
  }
81

82
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_TASK_RESET_NAME, pStream->uid);
1✔
83
  if (code) {
1!
84
    sdbRelease(pMnode->pSdb, pStream);
×
85
    mndTransDrop(pTrans);
×
86
    return code;
×
87
  }
88

89
  code = mndStreamSetResetTaskAction(pMnode, pTrans, pStream, chkptId);
1✔
90
  if (code) {
1!
91
    sdbRelease(pMnode->pSdb, pStream);
×
92
    mndTransDrop(pTrans);
×
93
    return code;
×
94
  }
95

96
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
1✔
97
  if (code != TSDB_CODE_SUCCESS) {
1!
98
    sdbRelease(pMnode->pSdb, pStream);
×
99
    mndTransDrop(pTrans);
×
100
    return code;
×
101
  }
102

103
  code = mndTransPrepare(pMnode, pTrans);
1✔
104
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1!
105
    mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
1!
106
    sdbRelease(pMnode->pSdb, pStream);
1✔
107
    mndTransDrop(pTrans);
1✔
108
    return code;
1✔
109
  }
110

111
  sdbRelease(pMnode->pSdb, pStream);
×
112
  mndTransDrop(pTrans);
×
113

114
  if (code == 0) {
×
115
    code = TSDB_CODE_ACTION_IN_PROGRESS;
×
116
  }
117
  return code;
×
118
}
119

120
int32_t mndSendResetFromCheckpointMsg(SMnode *pMnode, int64_t streamId, int32_t transId, int64_t checkpointId) {
×
121
  int32_t size = sizeof(SStreamTaskResetMsg);
×
122

123
  int32_t num = taosArrayGetSize(execInfo.pKilledChkptTrans);
×
124
  for (int32_t i = 0; i < num; ++i) {
×
125
    SStreamTaskResetMsg *p = taosArrayGet(execInfo.pKilledChkptTrans, i);
×
126
    if (p == NULL) {
×
127
      continue;
×
128
    }
129

130
    if (p->transId == transId && p->streamId == streamId) {
×
131
      mDebug("already reset stream:0x%" PRIx64 ", not send reset-msg again for transId:%d", streamId, transId);
×
132
      return TSDB_CODE_SUCCESS;
×
133
    }
134
  }
135

136
  if (num >= 10) {
×
137
    taosArrayRemove(execInfo.pKilledChkptTrans, 0);  // remove this first, append new reset trans in the tail
×
138
  }
139

140
  SStreamTaskResetMsg p = {.streamId = streamId, .transId = transId, .checkpointId = checkpointId};
×
141

142
  // let's remember that this trans had been killed already
143
  void *px = taosArrayPush(execInfo.pKilledChkptTrans, &p);
×
144
  if (px == NULL) {
×
145
    mError("failed to push reset-msg trans:%d into the killed chkpt trans list, size:%d", transId, num - 1);
×
146
    return terrno;
×
147
  }
148

149
  SStreamTaskResetMsg *pReq = rpcMallocCont(size);
×
150
  if (pReq == NULL) {
×
151
    return terrno;
×
152
  }
153

154
  pReq->streamId = streamId;
×
155
  pReq->transId = transId;
×
156
  pReq->checkpointId = checkpointId;
×
157

158
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_TASK_RESET, .pCont = pReq, .contLen = size};
×
159
  int32_t code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
160
  if (code) {
×
161
    mError("failed to put reset-task msg into write queue, code:%s", tstrerror(code));
×
162
  } else {
163
    mDebug("send reset task status msg for transId:%d succ", transId);
×
164
  }
165

166
  return code;
×
167
}
168

169
int32_t mndStreamSendUpdateChkptInfoMsg(SMnode *pMnode) {  // here reuse the doCheckpointmsg
6,343✔
170
  int32_t size = sizeof(SMStreamDoCheckpointMsg);
6,343✔
171
  void   *pMsg = rpcMallocCont(size);
6,343✔
172
  if (pMsg == NULL) {
6,343!
173
    return terrno;
×
174
  }
175

176
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_UPDATE_CHKPT_EVT, .pCont = pMsg, .contLen = size};
6,343✔
177
  int32_t code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
6,343✔
178
  if (code) {
6,343!
179
    mError("failed to put update-checkpoint-info msg into write queue, code:%s", tstrerror(code));
×
180
  } else {
181
    mDebug("send update checkpoint-info msg succ");
6,343✔
182
  }
183

184
  return code;
6,343✔
185
}
186

UNCOV
187
int32_t mndSendDropOrphanTasksMsg(SMnode *pMnode, SArray *pList) {
×
UNCOV
188
  SMStreamDropOrphanMsg msg = {.pList = pList};
×
189

UNCOV
190
  int32_t num = taosArrayGetSize(pList);
×
UNCOV
191
  int32_t contLen = tSerializeDropOrphanTaskMsg(NULL, 0, &msg);
×
UNCOV
192
  if (contLen <= 0) {
×
193
    return terrno;
×
194
  }
195

UNCOV
196
  void *pReq = rpcMallocCont(contLen);
×
UNCOV
197
  if (pReq == NULL) {
×
198
    return terrno;
×
199
  }
200

UNCOV
201
  int32_t code = tSerializeDropOrphanTaskMsg(pReq, contLen, &msg);
×
UNCOV
202
  if (code <= 0) {
×
203
    mError("failed to serialize the drop orphan task msg, code:%s", tstrerror(code));
×
204
  }
205

UNCOV
206
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_DROP_ORPHANTASKS, .pCont = pReq, .contLen = contLen};
×
UNCOV
207
  code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
UNCOV
208
  if (code) {
×
209
    mError("failed to put drop-orphan task msg into write queue, code:%s", tstrerror(code));
×
210
  } else {
UNCOV
211
    mDebug("send drop %d orphan tasks msg succ", num);
×
212
  }
213

UNCOV
214
  return code;
×
215
}
216

217
int32_t mndProcessResetStatusReq(SRpcMsg *pReq) {
×
218
  SMnode     *pMnode = pReq->info.node;
×
219
  int32_t     code = TSDB_CODE_SUCCESS;
×
220
  SStreamObj *pStream = NULL;
×
221

222
  SStreamTaskResetMsg *pMsg = pReq->pCont;
×
223
  mndKillTransImpl(pMnode, pMsg->transId, "");
×
224

225
  streamMutexLock(&execInfo.lock);
×
226
  code = mndResetChkptReportInfo(execInfo.pChkptStreams, pMsg->streamId);  // do thing if failed
×
227
  streamMutexUnlock(&execInfo.lock);
×
228

229
  code = mndGetStreamObj(pMnode, pMsg->streamId, &pStream);
×
230
  if (pStream == NULL || code != 0) {
×
231
    code = TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
232
    mError("failed to acquire the streamObj:0x%" PRIx64 " to reset checkpoint, may have been dropped", pStream->uid);
×
233
  } else {
234
    code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_TASK_RESET_NAME, false);
×
235
    if (code) {
×
236
      mError("stream:%s other trans exists in DB:%s, dstTable:%s failed to start reset-status trans", pStream->name,
×
237
             pStream->sourceDb, pStream->targetSTbName);
238
    } else {
239
      mDebug("stream:%s (0x%" PRIx64 ") reset checkpoint procedure, transId:%d, create reset trans", pStream->name,
×
240
             pStream->uid, pMsg->transId);
241
      code = mndCreateStreamResetStatusTrans(pMnode, pStream, pMsg->checkpointId);
×
242
    }
243
  }
244

245
  mndReleaseStream(pMnode, pStream);
×
246
  return code;
×
247
}
248

249
int32_t setNodeEpsetExpiredFlag(const SArray *pNodeList) {
26✔
250
  int32_t num = taosArrayGetSize(pNodeList);
26✔
251
  mInfo("set node expired for %d nodes", num);
26!
252

253
  for (int k = 0; k < num; ++k) {
52✔
254
    int32_t *pVgId = taosArrayGet(pNodeList, k);
26✔
255
    if (pVgId == NULL) {
26!
256
      continue;
×
257
    }
258

259
    mInfo("set node expired for nodeId:%d, total:%d", *pVgId, num);
26!
260

261
    bool    setFlag = false;
26✔
262
    int32_t numOfNodes = taosArrayGetSize(execInfo.pNodeList);
26✔
263

264
    for (int i = 0; i < numOfNodes; ++i) {
56!
265
      SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
56✔
266
      if ((pNodeEntry) && (pNodeEntry->nodeId == *pVgId)) {
56!
267
        mInfo("vgId:%d expired for some stream tasks, needs update nodeEp", *pVgId);
26!
268
        pNodeEntry->stageUpdated = true;
26✔
269
        setFlag = true;
26✔
270
        break;
26✔
271
      }
272
    }
273

274
    if (!setFlag) {
26!
275
      mError("failed to set nodeUpdate flag, nodeId:%d not exists in nodelist", *pVgId);
×
276
      return TSDB_CODE_FAILED;
×
277
    }
278
  }
279
  return TSDB_CODE_SUCCESS;
26✔
280
}
281

282
int32_t suspendAllStreams(SMnode *pMnode, SRpcHandleInfo *info) {
×
283
  SSdb       *pSdb = pMnode->pSdb;
×
284
  SStreamObj *pStream = NULL;
×
285
  void       *pIter = NULL;
×
286
  int32_t     code = 0;
×
287

288
  while (1) {
289
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
290
    if (pIter == NULL) break;
×
291

292
    if (pStream->status != STREAM_STATUS__PAUSE) {
×
293
      SMPauseStreamReq reqPause = {0};
×
294
      tstrncpy(reqPause.name, pStream->name, sizeof(reqPause.name));
×
295
      reqPause.igNotExists = 1;
×
296

297
      int32_t contLen = tSerializeSMPauseStreamReq(NULL, 0, &reqPause);
×
298
      void   *pHead = rpcMallocCont(contLen);
×
299
      if (pHead == NULL) {
×
300
        code = TSDB_CODE_OUT_OF_MEMORY;
×
301
        sdbRelease(pSdb, pStream);
×
302
        continue;
×
303
      }
304

305
      code = tSerializeSMPauseStreamReq(pHead, contLen, &reqPause);
×
306
      if (code) {
×
307
        sdbRelease(pSdb, pStream);
×
308
        continue;
×
309
      }
310

311
      SRpcMsg rpcMsg = {
×
312
          .msgType = TDMT_MND_PAUSE_STREAM,
313
          .pCont = pHead,
314
          .contLen = contLen,
315
          .info = *info,
316
      };
317

318
      code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
319
      mInfo("receive pause stream:%s, %s, %" PRId64 ", because grant expired, code:%s", pStream->name, reqPause.name,
×
320
            pStream->uid, tstrerror(code));
321
    }
322

323
    sdbRelease(pSdb, pStream);
×
324
  }
325
  return code;
×
326
}
327

328
int32_t mndProcessStreamHb(SRpcMsg *pReq) {
6,348✔
329
  SMnode      *pMnode = pReq->info.node;
6,348✔
330
  SStreamHbMsg req = {0};
6,348✔
331
  SArray      *pFailedChkpt = NULL;
6,348✔
332
  SArray      *pOrphanTasks = NULL;
6,348✔
333
  int32_t      code = 0;
6,348✔
334
  SDecoder     decoder = {0};
6,348✔
335
  SEpSet       mnodeEpset = {0};
6,348✔
336

337
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
6,348!
338
    if (suspendAllStreams(pMnode, &pReq->info) < 0) {
×
339
      return code;
×
340
    }
341
  }
342

343
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
6,346✔
344

345
  if (tDecodeStreamHbMsg(&decoder, &req) < 0) {
6,346!
346
    tCleanupStreamHbMsg(&req);
×
347
    tDecoderClear(&decoder);
×
348
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
349
  }
350
  tDecoderClear(&decoder);
6,347✔
351

352
  mDebug("receive stream-meta hb from vgId:%d, active numOfTasks:%d, HbMsgId:%d, HbMsgTs:%" PRId64, req.vgId,
6,348✔
353
         req.numOfTasks, req.msgId, req.ts);
354

355
  pFailedChkpt = taosArrayInit(4, sizeof(SFailedCheckpointInfo));
6,348✔
356
  pOrphanTasks = taosArrayInit(4, sizeof(SOrphanTask));
6,347✔
357
  if (pFailedChkpt == NULL || pOrphanTasks == NULL) {
6,348!
358
    taosArrayDestroy(pFailedChkpt);
×
359
    taosArrayDestroy(pOrphanTasks);
×
360
    TAOS_RETURN(terrno);
×
361
  }
362

363
  mndGetMnodeEpSet(pMnode, &mnodeEpset);
6,348✔
364

365
  streamMutexLock(&execInfo.lock);
6,347✔
366

367
  mndInitStreamExecInfo(pMnode, &execInfo);
6,348✔
368
  if (!validateHbMsg(execInfo.pNodeList, req.vgId)) {
6,348✔
369
    mError("vgId:%d not exists in nodeList buf, discarded", req.vgId);
1!
370

371
    doSendHbMsgRsp(terrno, &pReq->info, &mnodeEpset, req.vgId, req.msgId);
1✔
372

373
    streamMutexUnlock(&execInfo.lock);
1✔
374
    cleanupAfterProcessHbMsg(&req, pFailedChkpt, pOrphanTasks);
1✔
375
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
1✔
376
  }
377

378
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pNodeList); ++i) {
48,570✔
379
    SNodeEntry *pEntry = taosArrayGet(execInfo.pNodeList, i);
42,227✔
380
    if (pEntry == NULL) {
42,227!
381
      continue;
×
382
    }
383

384
    if (pEntry->nodeId != req.vgId) {
42,227✔
385
      continue;
35,880✔
386
    }
387

388
    if ((pEntry->lastHbMsgId == req.msgId) && (pEntry->lastHbMsgTs == req.ts)) {
6,347✔
389
      mError("vgId:%d HbMsgId:%d already handled, bh msg discard, and send HbRsp", pEntry->nodeId, req.msgId);
4!
390

391
      // return directly and allow the vnode to continue to send the next HbMsg.
392
      terrno = TSDB_CODE_SUCCESS;
4✔
393
      doSendHbMsgRsp(terrno, &pReq->info, &mnodeEpset, req.vgId, req.msgId);
4✔
394

395
      streamMutexUnlock(&execInfo.lock);
4✔
396
      cleanupAfterProcessHbMsg(&req, pFailedChkpt, pOrphanTasks);
4✔
397
      return terrno;
4✔
398
    } else {
399
      pEntry->lastHbMsgId = req.msgId;
6,343✔
400
      pEntry->lastHbMsgTs = req.ts;
6,343✔
401
    }
402
  }
403

404
  int32_t numOfUpdated = taosArrayGetSize(req.pUpdateNodes);
6,343✔
405
  if (numOfUpdated > 0) {
6,343✔
406
    mDebug("%d stream node(s) need updated from hbMsg(vgId:%d)", numOfUpdated, req.vgId);
26✔
407
    int32_t unused = setNodeEpsetExpiredFlag(req.pUpdateNodes);
26✔
408
  }
409

410
  bool snodeChanged = false;
6,343✔
411
  for (int32_t i = 0; i < req.numOfTasks; ++i) {
28,322✔
412
    STaskStatusEntry *p = taosArrayGet(req.pTaskStatus, i);
21,979✔
413
    if (p == NULL) {
21,979!
414
      continue;
×
415
    }
416

417
    STaskStatusEntry *pTaskEntry = taosHashGet(execInfo.pTaskMap, &p->id, sizeof(p->id));
21,979✔
418
    if (pTaskEntry == NULL) {
21,979✔
419
      checkforOrphanTask(pMnode, p, pOrphanTasks);
40✔
420
      continue;
40✔
421
    }
422

423
    STaskCkptInfo *pChkInfo = &p->checkpointInfo;
21,939✔
424
    if (pChkInfo->consensusChkptId != 0) {
21,939✔
425
      SRestoreCheckpointInfo cp = {
131✔
426
          .streamId = p->id.streamId,
131✔
427
          .taskId = p->id.taskId,
131✔
428
          .checkpointId = p->checkpointInfo.latestId,
131✔
429
          .startTs = pChkInfo->consensusTs,
131✔
430
      };
431

432
      SStreamObj *pStream = NULL;
131✔
433
      code = mndGetStreamObj(pMnode, p->id.streamId, &pStream);
131✔
434
      if (code) {
131!
435
        mError("stream:0x%" PRIx64 " not exist, failed to handle consensus checkpoint-info req for task:0x%x, code:%s",
×
436
               p->id.streamId, (int32_t)p->id.taskId, tstrerror(code));
437
        continue;
×
438
      }
439

440
      int32_t numOfTasks = mndGetNumOfStreamTasks(pStream);
131✔
441

442
      SCheckpointConsensusInfo *pInfo = NULL;
131✔
443
      code = mndGetConsensusInfo(execInfo.pStreamConsensus, p->id.streamId, numOfTasks, &pInfo);
131✔
444
      if (code == 0) {
131!
445
        mndAddConsensusTasks(pInfo, &cp);
131✔
446
      } else {
447
        mError("failed to get consensus checkpoint-info for stream:0x%" PRIx64, p->id.streamId);
×
448
      }
449

450
      mndReleaseStream(pMnode, pStream);
131✔
451
    }
452

453
    if (pTaskEntry->stage != p->stage && pTaskEntry->stage != -1) {
21,939✔
454
      updateStageInfo(pTaskEntry, p->stage);
67✔
455
      if (pTaskEntry->nodeId == SNODE_HANDLE) {
67✔
456
        snodeChanged = true;
1✔
457
      }
458
    } else {
459
      streamTaskStatusCopy(pTaskEntry, p);
21,872✔
460

461
      if ((pChkInfo->activeId != 0) && pChkInfo->failed) {
21,872!
462
        mError("stream task:0x%" PRIx64 " checkpointId:%" PRId64 " transId:%d failed, kill it", p->id.taskId,
×
463
               pChkInfo->activeId, pChkInfo->activeTransId);
464

465
        SFailedCheckpointInfo info = {
×
466
            .transId = pChkInfo->activeTransId, .checkpointId = pChkInfo->activeId, .streamUid = p->id.streamId};
×
467
        addIntoFailedChkptList(pFailedChkpt, &info);
×
468

469
        // remove failed trans from pChkptStreams
470
        code = mndResetChkptReportInfo(execInfo.pChkptStreams, p->id.streamId);
×
471
        if (code) {
×
472
          mError("failed to remove stream:0x%" PRIx64 " in checkpoint stream list", p->id.streamId);
×
473
        }
474
      }
475
    }
476

477
    if (p->status != TASK_STATUS__READY) {
21,939✔
478
      mDebug("received s-task:0x%" PRIx64 " not in ready status:%s", p->id.taskId, streamTaskGetStatusStr(p->status));
3,247✔
479
    }
480
  }
481

482
  // current checkpoint is failed, rollback from the checkpoint trans
483
  // kill the checkpoint trans and then set all tasks status to be normal
484
  if (taosArrayGetSize(pFailedChkpt) > 0) {
6,343!
485
    bool allReady = true;
×
486

487
    if (pMnode != NULL) {
×
488
      SArray *p = NULL;
×
489
      code = mndTakeVgroupSnapshot(pMnode, &allReady, &p);
×
490
      taosArrayDestroy(p);
×
491
      if (code) {
×
492
        mError("failed to get the vgroup snapshot, ignore it and continue");
×
493
      }
494
    } else {
495
      allReady = false;
×
496
    }
497

498
    if (allReady || snodeChanged) {
×
499
      // if the execInfo.activeCheckpoint == 0, the checkpoint is restoring from wal
500
      for (int32_t i = 0; i < taosArrayGetSize(pFailedChkpt); ++i) {
×
501
        SFailedCheckpointInfo *pInfo = taosArrayGet(pFailedChkpt, i);
×
502
        if (pInfo == NULL) {
×
503
          continue;
×
504
        }
505

506
        mInfo("stream:0x%" PRIx64 " checkpointId:%" PRId64
×
507
              " transId:%d failed issue task-reset trans to reset all tasks status",
508
              pInfo->streamUid, pInfo->checkpointId, pInfo->transId);
509

510
        code = mndSendResetFromCheckpointMsg(pMnode, pInfo->streamUid, pInfo->transId, pInfo->checkpointId);
×
511
        if (code) {
×
512
          mError("failed to create reset task trans, code:%s", tstrerror(code));
×
513
        }
514
      }
515
    } else {
516
      mInfo("not all vgroups are ready, wait for next HB from stream tasks to reset the task status");
×
517
    }
518
  }
519

520
  // handle the orphan tasks that are invalid but not removed in some vnodes or snode due to some unknown errors.
521
  if (taosArrayGetSize(pOrphanTasks) > 0) {
6,343!
UNCOV
522
    code = mndSendDropOrphanTasksMsg(pMnode, pOrphanTasks);
×
UNCOV
523
    if (code) {
×
524
      mError("failed to send drop orphan tasks msg, code:%s, try next time", tstrerror(code));
×
525
    }
526
  }
527

528
  if (pMnode != NULL) {  // make sure that the unit test case can work
6,343!
529
    code = mndStreamSendUpdateChkptInfoMsg(pMnode);
6,343✔
530
    if (code) {
6,343!
531
      mError("failed to send update checkpointInfo msg, code:%s, try next time", tstrerror(code));
×
532
    }
533
  }
534

535
  streamMutexUnlock(&execInfo.lock);
6,343✔
536

537
  doSendHbMsgRsp(TSDB_CODE_SUCCESS, &pReq->info, &mnodeEpset, req.vgId, req.msgId);
6,343✔
538
  cleanupAfterProcessHbMsg(&req, pFailedChkpt, pOrphanTasks);
6,343✔
539

540
  return code;
6,343✔
541
}
542

543
bool validateHbMsg(const SArray *pNodeList, int32_t vgId) {
6,348✔
544
  for (int32_t i = 0; i < taosArrayGetSize(pNodeList); ++i) {
26,786✔
545
    SNodeEntry *pEntry = taosArrayGet(pNodeList, i);
26,785✔
546
    if ((pEntry) && (pEntry->nodeId == vgId)) {
26,785!
547
      return true;
6,347✔
548
    }
549
  }
550

551
  return false;
1✔
552
}
553

554
void cleanupAfterProcessHbMsg(SStreamHbMsg *pReq, SArray *pFailedChkptList, SArray *pOrphanTasks) {
6,348✔
555
  tCleanupStreamHbMsg(pReq);
6,348✔
556
  taosArrayDestroy(pFailedChkptList);
6,348✔
557
  taosArrayDestroy(pOrphanTasks);
6,348✔
558
}
6,348✔
559

560
void doSendHbMsgRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SEpSet* pMndEpset, int32_t vgId, int32_t msgId) {
6,348✔
561
  int32_t ret = 0;
6,348✔
562
  int32_t tlen = 0;
6,348✔
563
  void   *buf = NULL;
6,348✔
564

565
  SMStreamHbRspMsg msg = {.msgId = msgId};//, .mndEpset = *pMndEpset};
6,348✔
566
  epsetAssign(&msg.mndEpset, pMndEpset);
6,348✔
567

568
  tEncodeSize(tEncodeStreamHbRsp, &msg, tlen, ret);
6,348!
569
  if (ret < 0) {
6,348!
570
    mError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
571
  }
572

573
  buf = rpcMallocCont(tlen + sizeof(SMsgHead));
6,348✔
574
  if (buf == NULL) {
6,348!
575
    mError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
576
    return;
×
577
  }
578

579
  ((SMStreamHbRspMsg *)buf)->head.vgId = htonl(vgId);
6,348✔
580
  void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
6,348✔
581

582
  SEncoder encoder;
583
  tEncoderInit(&encoder, abuf, tlen);
6,348✔
584
  if ((code = tEncodeStreamHbRsp(&encoder, &msg)) < 0) {
6,348!
585
    rpcFreeCont(buf);
×
586
    tEncoderClear(&encoder);
×
587
    mError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
588
    return;
×
589
  }
590
  tEncoderClear(&encoder);
6,348✔
591

592
  SRpcMsg rsp = {.code = code, .info = *pRpcInfo, .contLen = tlen + sizeof(SMsgHead), .pCont = buf};
6,348✔
593

594
  tmsgSendRsp(&rsp);
6,348✔
595
  pRpcInfo->handle = NULL;  // disable auto rsp
6,348✔
596
}
597

598
void checkforOrphanTask(SMnode *pMnode, STaskStatusEntry *p, SArray *pOrphanTasks) {
40✔
599
  SStreamObj *pStream = NULL;
40✔
600

601
  int32_t code = mndGetStreamObj(pMnode, p->id.streamId, &pStream);
40✔
602
  if (code) {
40!
UNCOV
603
    mError("stream:0x%" PRIx64 " not exists, s-task:0x%" PRIx64 " not found in task list, add into orphan list",
×
604
           p->id.streamId, p->id.taskId);
605

UNCOV
606
    SOrphanTask oTask = {.streamId = p->id.streamId, .taskId = p->id.taskId, .nodeId = p->nodeId};
×
UNCOV
607
    void       *px = taosArrayPush(pOrphanTasks, &oTask);
×
UNCOV
608
    if (px == NULL) {
×
609
      mError("failed to put task into orphan list, taskId:0x%" PRIx64 ", code:%s", p->id.taskId, tstrerror(terrno));
×
610
    }
611
  } else {
612
    if (pStream != NULL) {
40!
613
      mndReleaseStream(pMnode, pStream);
40✔
614
    }
615

616
    mError("s-task:0x%" PRIx64 " not found in task list but exists in mnode meta, data inconsistent, not drop yet",
40!
617
           p->id.taskId);
618
  }
619
}
40✔
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