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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

0.0
/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

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

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

UNCOV
51
      pNodeEntry->stageUpdated = true;
×
UNCOV
52
      pTaskEntry->stage = stage;
×
UNCOV
53
      break;
×
54
    }
55
  }
UNCOV
56
}
×
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

UNCOV
73
int32_t mndCreateStreamResetStatusTrans(SMnode *pMnode, SStreamObj *pStream, int64_t chkptId) {
×
UNCOV
74
  STrans *pTrans = NULL;
×
UNCOV
75
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_RESET_NAME,
×
76
                               " reset from failed checkpoint", &pTrans);
UNCOV
77
  if (pTrans == NULL || code) {
×
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) {
×
UNCOV
105
    mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
106
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
107
    mndTransDrop(pTrans);
×
UNCOV
108
    return code;
×
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

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

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

UNCOV
184
  return code;
×
185
}
186

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

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

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

201
  int32_t code = tSerializeDropOrphanTaskMsg(pReq, contLen, &msg);
×
202
  if (code <= 0) {
×
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};
×
207
  code = tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
208
  if (code) {
×
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);
×
212
  }
213

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

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

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

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

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

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

UNCOV
274
    if (!setFlag) {
×
275
      mError("failed to set nodeUpdate flag, nodeId:%d not exists in nodelist", *pVgId);
×
276
      return TSDB_CODE_FAILED;
×
277
    }
278
  }
UNCOV
279
  return TSDB_CODE_SUCCESS;
×
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

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

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

UNCOV
343
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
×
344

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

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

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

UNCOV
363
  mndGetMnodeEpSet(pMnode, &mnodeEpset);
×
364

UNCOV
365
  streamMutexLock(&execInfo.lock);
×
366

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

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

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

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

UNCOV
384
    if (pEntry->nodeId != req.vgId) {
×
UNCOV
385
      continue;
×
386
    }
387

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

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

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

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

UNCOV
410
  bool snodeChanged = false;
×
UNCOV
411
  for (int32_t i = 0; i < req.numOfTasks; ++i) {
×
UNCOV
412
    STaskStatusEntry *p = taosArrayGet(req.pTaskStatus, i);
×
UNCOV
413
    if (p == NULL) {
×
414
      continue;
×
415
    }
416

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

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

UNCOV
432
      SStreamObj *pStream = NULL;
×
UNCOV
433
      code = mndGetStreamObj(pMnode, p->id.streamId, &pStream);
×
UNCOV
434
      if (code) {
×
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

UNCOV
440
      int32_t numOfTasks = mndGetNumOfStreamTasks(pStream);
×
441

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

UNCOV
450
      mndReleaseStream(pMnode, pStream);
×
451
    }
452

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

UNCOV
461
      if ((pChkInfo->activeId != 0) && pChkInfo->failed) {
×
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

UNCOV
477
    if (p->status != TASK_STATUS__READY) {
×
UNCOV
478
      mDebug("received s-task:0x%" PRIx64 " not in ready status:%s", p->id.taskId, streamTaskGetStatusStr(p->status));
×
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
UNCOV
484
  if (taosArrayGetSize(pFailedChkpt) > 0) {
×
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.
UNCOV
521
  if (taosArrayGetSize(pOrphanTasks) > 0) {
×
522
    code = mndSendDropOrphanTasksMsg(pMnode, pOrphanTasks);
×
523
    if (code) {
×
524
      mError("failed to send drop orphan tasks msg, code:%s, try next time", tstrerror(code));
×
525
    }
526
  }
527

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

UNCOV
535
  streamMutexUnlock(&execInfo.lock);
×
536

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

UNCOV
540
  return code;
×
541
}
542

UNCOV
543
bool validateHbMsg(const SArray *pNodeList, int32_t vgId) {
×
UNCOV
544
  for (int32_t i = 0; i < taosArrayGetSize(pNodeList); ++i) {
×
UNCOV
545
    SNodeEntry *pEntry = taosArrayGet(pNodeList, i);
×
UNCOV
546
    if ((pEntry) && (pEntry->nodeId == vgId)) {
×
UNCOV
547
      return true;
×
548
    }
549
  }
550

UNCOV
551
  return false;
×
552
}
553

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

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

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

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

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

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

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

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

UNCOV
594
  tmsgSendRsp(&rsp);
×
UNCOV
595
  pRpcInfo->handle = NULL;  // disable auto rsp
×
596
}
597

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

UNCOV
601
  int32_t code = mndGetStreamObj(pMnode, p->id.streamId, &pStream);
×
UNCOV
602
  if (code) {
×
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

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

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