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

taosdata / TDengine / #3557

16 Dec 2024 01:23AM UTC coverage: 58.204% (+33.4%) from 24.813%
#3557

push

travis-ci

web-flow
Merge pull request #29105 from taosdata/fix/3_liaohj

enh: update the mnode epset in stream hb rsp.

128414 of 285296 branches covered (45.01%)

Branch coverage included in aggregate %.

36 of 64 new or added lines in 5 files covered. (56.25%)

225 existing lines in 34 files now uncovered.

201842 of 282116 relevant lines covered (71.55%)

19596843.67 hits per line

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

44.05
/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) {
81✔
40
  int32_t numOfNodes = taosArrayGetSize(execInfo.pNodeList);
81✔
41
  for (int32_t j = 0; j < numOfNodes; ++j) {
159!
42
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, j);
159✔
43
    if (pNodeEntry == NULL) {
159!
44
      continue;
×
45
    }
46

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

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

58
void addIntoFailedChkptList(SArray *pList, const SFailedCheckpointInfo *pInfo) {
×
UNCOV
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

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

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

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

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

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

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

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

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

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

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

UNCOV
130
    if (p->transId == transId && p->streamId == streamId) {
×
UNCOV
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

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

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

142
  // let's remember that this trans had been killed already
UNCOV
143
  void *px = taosArrayPush(execInfo.pKilledChkptTrans, &p);
×
UNCOV
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

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

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

158
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_TASK_RESET, .pCont = pReq, .contLen = size};
×
UNCOV
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

UNCOV
166
  return code;
×
167
}
168

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

176
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_UPDATE_CHKPT_EVT, .pCont = pMsg, .contLen = size};
25,743✔
177
  int32_t code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
25,743✔
178
  if (code) {
25,743!
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");
25,743✔
182
  }
183

184
  return code;
25,743✔
185
}
186

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

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

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

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

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

214
  return code;
2✔
215
}
216

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

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

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

229
  code = mndGetStreamObj(pMnode, pMsg->streamId, &pStream);
×
UNCOV
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);
×
UNCOV
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 {
UNCOV
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

UNCOV
245
  mndReleaseStream(pMnode, pStream);
×
UNCOV
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) {
54!
265
      SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
54✔
266
      if ((pNodeEntry) && (pNodeEntry->nodeId == *pVgId)) {
54!
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!
UNCOV
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

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

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

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

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

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

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

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

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

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

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

343
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
25,763✔
344

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

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

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

363
  mndGetMnodeEpSet(pMnode, &mnodeEpset);
25,764✔
364

365
  streamMutexLock(&execInfo.lock);
25,764✔
366

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

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

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

378
  for(int32_t i = 0; i < taosArrayGetSize(execInfo.pNodeList); ++i) {
183,464✔
379
    SNodeEntry* pEntry = taosArrayGet(execInfo.pNodeList, i);
157,721✔
380
    if (pEntry == NULL) {
157,721!
UNCOV
381
      continue;
×
382
    }
383

384
    if (pEntry->nodeId != req.vgId) {
157,721✔
385
      continue;
131,963✔
386
    }
387

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

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

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

404
  int32_t numOfUpdated = taosArrayGetSize(req.pUpdateNodes);
25,743✔
405
  if (numOfUpdated > 0) {
25,743✔
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;
25,743✔
411
  for (int32_t i = 0; i < req.numOfTasks; ++i) {
80,094✔
412
    STaskStatusEntry *p = taosArrayGet(req.pTaskStatus, i);
54,351✔
413
    if (p == NULL) {
54,351!
UNCOV
414
      continue;
×
415
    }
416

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

423
    STaskCkptInfo *pChkInfo = &p->checkpointInfo;
54,306✔
424
    if (pChkInfo->consensusChkptId != 0) {
54,306✔
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!
UNCOV
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) {
54,306✔
454
      updateStageInfo(pTaskEntry, p->stage);
81✔
455
      if (pTaskEntry->nodeId == SNODE_HANDLE) {
81✔
456
        snodeChanged = true;
1✔
457
      }
458
    } else {
459
      streamTaskStatusCopy(pTaskEntry, p);
54,225✔
460

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

UNCOV
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) {
54,306✔
478
      mDebug("received s-task:0x%" PRIx64 " not in ready status:%s", p->id.taskId, streamTaskGetStatusStr(p->status));
3,905✔
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) {
25,743!
UNCOV
485
    bool allReady = true;
×
486

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

UNCOV
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) {
×
UNCOV
501
        SFailedCheckpointInfo *pInfo = taosArrayGet(pFailedChkpt, i);
×
UNCOV
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

UNCOV
510
        code = mndSendResetFromCheckpointMsg(pMnode, pInfo->streamUid, pInfo->transId, pInfo->checkpointId);
×
511
        if (code) {
×
UNCOV
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) {
25,743✔
522
    code = mndSendDropOrphanTasksMsg(pMnode, pOrphanTasks);
2✔
523
    if (code) {
2!
UNCOV
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
25,743!
529
    code = mndStreamSendUpdateChkptInfoMsg(pMnode);
25,743✔
530
    if (code) {
25,743!
UNCOV
531
      mError("failed to send update checkpointInfo msg, code:%s, try next time", tstrerror(code));
×
532
    }
533
  }
534

535
  streamMutexUnlock(&execInfo.lock);
25,743✔
536

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

540
  return code;
25,743✔
541
}
542

543
bool validateHbMsg(const SArray *pNodeList, int32_t vgId) {
25,764✔
544
  for (int32_t i = 0; i < taosArrayGetSize(pNodeList); ++i) {
94,145✔
545
    SNodeEntry *pEntry = taosArrayGet(pNodeList, i);
94,139✔
546
    if ((pEntry) && (pEntry->nodeId == vgId)) {
94,139!
547
      return true;
25,758✔
548
    }
549
  }
550

551
  return false;
6✔
552
}
553

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

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

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

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

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

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

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

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

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

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

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

606
    SOrphanTask oTask = {.streamId = p->id.streamId, .taskId = p->id.taskId, .nodeId = p->nodeId};
3✔
607
    void       *px = taosArrayPush(pOrphanTasks, &oTask);
3✔
608
    if (px == NULL) {
3!
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) {
42!
613
      mndReleaseStream(pMnode, pStream);
42✔
614
    }
615

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