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

taosdata / TDengine / #4440

04 Jul 2025 02:10AM UTC coverage: 63.29% (-0.4%) from 63.643%
#4440

push

travis-ci

web-flow
fix:(stmt2) heap buffer overflow (#31607)

159782 of 321690 branches covered (49.67%)

Branch coverage included in aggregate %.

19 of 22 new or added lines in 3 files covered. (86.36%)

5735 existing lines in 195 files now uncovered.

246739 of 320626 relevant lines covered (76.96%)

6757056.21 hits per line

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

68.36
/source/dnode/mnode/impl/src/mndMain.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
#define _DEFAULT_SOURCE
17
#include "mndAcct.h"
18
#include "mndAnode.h"
19
#include "mndArbGroup.h"
20
#include "mndCluster.h"
21
#include "mndCompact.h"
22
#include "mndCompactDetail.h"
23
#include "mndConfig.h"
24
#include "mndConsumer.h"
25
#include "mndDb.h"
26
#include "mndDnode.h"
27
#include "mndFunc.h"
28
#include "mndGrant.h"
29
#include "mndIndex.h"
30
#include "mndInfoSchema.h"
31
#include "mndMnode.h"
32
#include "mndPerfSchema.h"
33
#include "mndPrivilege.h"
34
#include "mndProfile.h"
35
#include "mndQnode.h"
36
#include "mndQuery.h"
37
#include "mndShow.h"
38
#include "mndSma.h"
39
#include "mndSnode.h"
40
#include "mndStb.h"
41
#include "mndStream.h"
42
#include "mndSubscribe.h"
43
#include "mndSync.h"
44
#include "mndTelem.h"
45
#include "mndTopic.h"
46
#include "mndTrans.h"
47
#include "mndUser.h"
48
#include "mndVgroup.h"
49
#include "mndView.h"
50
#include "mndBnode.h"
51

52
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
11,862✔
53
  int32_t code = 0;
11,862✔
54
  (void)taosThreadRwlockRdlock(&pMnode->lock);
11,862✔
55
  if (pMnode->stopped) {
11,862!
56
    code = TSDB_CODE_APP_IS_STOPPING;
×
57
  } else if (!mndIsLeader(pMnode)) {
11,862!
58
    code = 1;
×
59
  } else {
60
#if 1
61
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
11,862✔
62
#else
63
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
64
    mTrace("mnode rpc is acquired, ref:%d", ref);
65
#endif
66
  }
67
  (void)taosThreadRwlockUnlock(&pMnode->lock);
11,862✔
68
  TAOS_RETURN(code);
11,862✔
69
}
70

71
static inline void mndReleaseRpc(SMnode *pMnode) {
735,205✔
72
  (void)taosThreadRwlockRdlock(&pMnode->lock);
735,205✔
73
#if 1
74
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
735,232✔
75
#else
76
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
77
  mTrace("mnode rpc is released, ref:%d", ref);
78
#endif
79
  (void)taosThreadRwlockUnlock(&pMnode->lock);
735,225✔
80
}
735,229✔
81

82
static void *mndBuildTimerMsg(int32_t *pContLen) {
134,027✔
83
  terrno = 0;
134,027✔
84
  SMTimerReq timerReq = {0};
134,027✔
85

86
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
134,027✔
87
  if (contLen <= 0) return NULL;
134,027!
88
  void *pReq = rpcMallocCont(contLen);
134,027✔
89
  if (pReq == NULL) return NULL;
134,027!
90

91
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
134,027!
92
    mError("failed to serialize timer msg since %s", terrstr());
×
93
  }
94
  *pContLen = contLen;
134,027✔
95
  return pReq;
134,027✔
96
}
97

98
static void mndPullupTrans(SMnode *pMnode) {
32,108✔
99
  mTrace("pullup trans msg");
32,108✔
100
  int32_t contLen = 0;
32,108✔
101
  void   *pReq = mndBuildTimerMsg(&contLen);
32,108✔
102
  if (pReq != NULL) {
32,108!
103
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS_TIMER, .pCont = pReq, .contLen = contLen};
32,108✔
104
    // TODO check return value
105
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
32,108!
106
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
107
    }
108
  }
109
}
32,108✔
110

111
static void mndPullupCompacts(SMnode *pMnode) {
5,430✔
112
  mTrace("pullup compact timer msg");
5,430✔
113
  int32_t contLen = 0;
5,430✔
114
  void   *pReq = mndBuildTimerMsg(&contLen);
5,430✔
115
  if (pReq != NULL) {
5,430!
116
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_COMPACT_TIMER, .pCont = pReq, .contLen = contLen};
5,430✔
117
    // TODO check return value
118
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
5,430!
119
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
120
    }
121
  }
122
}
5,430✔
123

124
static void mndPullupTtl(SMnode *pMnode) {
6,036✔
125
  mTrace("pullup ttl");
6,036✔
126
  int32_t contLen = 0;
6,036✔
127
  void   *pReq = mndBuildTimerMsg(&contLen);
6,036✔
128
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
6,036✔
129
  // TODO check return value
130
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
6,036!
131
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
132
  }
133
}
6,036✔
134

UNCOV
135
static void mndPullupTrimDb(SMnode *pMnode) {
×
UNCOV
136
  mTrace("pullup s3migrate");
×
UNCOV
137
  int32_t contLen = 0;
×
UNCOV
138
  void   *pReq = mndBuildTimerMsg(&contLen);
×
UNCOV
139
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
140
  // TODO check return value
UNCOV
141
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
142
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
143
  }
UNCOV
144
}
×
145

146
static void mndPullupS3MigrateDb(SMnode *pMnode) {
×
147
  mTrace("pullup trim");
×
148
  int32_t contLen = 0;
×
149
  void   *pReq = mndBuildTimerMsg(&contLen);
×
150
  // TODO check return value
151
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_S3MIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
152
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
153
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
154
  }
155
}
×
156

157
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
31,267✔
158
  mTrace("pullup arb hb");
31,267✔
159
  int32_t contLen = 0;
31,267✔
160
  void   *pReq = mndBuildTimerMsg(&contLen);
31,267✔
161
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
31,267✔
162
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
31,267✔
163
}
164

165
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
20,496✔
166
  mTrace("pullup arb sync");
20,496✔
167
  int32_t contLen = 0;
20,496✔
168
  void   *pReq = mndBuildTimerMsg(&contLen);
20,496✔
169
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
20,496✔
170
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
20,496✔
171
}
172

173
static void mndCalMqRebalance(SMnode *pMnode) {
31,260✔
174
  int32_t contLen = 0;
31,260✔
175
  void   *pReq = mndBuildTimerMsg(&contLen);
31,260✔
176
  if (pReq != NULL) {
31,260!
177
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
31,260✔
178
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
31,260!
179
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
180
    }
181
  }
182
}
31,260✔
183

184
static void mndStreamCheckpointTimer(SMnode *pMnode) {
1,446✔
185
  SMStreamDoCheckpointMsg *pMsg = rpcMallocCont(sizeof(SMStreamDoCheckpointMsg));
1,446✔
186
  if (pMsg != NULL) {
1,446!
187
    int32_t size = sizeof(SMStreamDoCheckpointMsg);
1,446✔
188
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_BEGIN_CHECKPOINT, .pCont = pMsg, .contLen = size};
1,446✔
189
    // TODO check return value
190
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,446!
191
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
192
    }
193
  }
194
}
1,446✔
195

196
static void mndStreamCheckNode(SMnode *pMnode) {
39✔
197
  int32_t contLen = 0;
39✔
198
  void   *pReq = mndBuildTimerMsg(&contLen);
39✔
199
  if (pReq != NULL) {
39!
200
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_NODECHECK_TIMER, .pCont = pReq, .contLen = contLen};
39✔
201
    // TODO check return value
202
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
39!
203
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
204
    }
205
  }
206
}
39✔
207

208
static void mndStreamCheckStatus(SMnode *pMnode) {
1✔
209
  int32_t contLen = 0;
1✔
210
  void   *pReq = mndBuildTimerMsg(&contLen);
1✔
211
  if (pReq != NULL) {
1!
212
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_CHECK_STREAM_TIMER, .pCont = pReq, .contLen = contLen};
1✔
213
    // TODO check return value
214
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1!
215
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
216
    }
217
  }
218
}
1✔
219

220
static void mndStreamConsensusChkpt(SMnode *pMnode) {
1,446✔
221
  int32_t contLen = 0;
1,446✔
222
  void   *pReq = mndBuildTimerMsg(&contLen);
1,446✔
223
  if (pReq != NULL) {
1,446!
224
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_CONSEN_TIMER, .pCont = pReq, .contLen = contLen};
1,446✔
225
    // TODO check return value
226
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,446!
227
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
228
    }
229
  }
230
}
1,446✔
231

232
static void mndPullupTelem(SMnode *pMnode) {
2✔
233
  mTrace("pullup telem msg");
2!
234
  int32_t contLen = 0;
2✔
235
  void   *pReq = mndBuildTimerMsg(&contLen);
2✔
236
  if (pReq != NULL) {
2!
237
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
2✔
238
    // TODO check return value
239
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
2!
240
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
241
    }
242
  }
243
}
2✔
244

245
static void mndPullupGrant(SMnode *pMnode) {
5,919✔
246
  mTrace("pullup grant msg");
5,919✔
247
  int32_t contLen = 0;
5,919✔
248
  void   *pReq = mndBuildTimerMsg(&contLen);
5,919✔
249
  if (pReq != NULL) {
5,919!
250
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
5,919✔
251
                      .pCont = pReq,
252
                      .contLen = contLen,
253
                      .info.notFreeAhandle = 1,
254
                      .info.ahandle = 0};
255
    // TODO check return value
256
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
5,919!
257
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
258
    }
259
  }
260
}
5,919✔
261

262
static void mndIncreaseUpTime(SMnode *pMnode) {
23✔
263
  mTrace("increate uptime");
23!
264
  int32_t contLen = 0;
23✔
265
  void   *pReq = mndBuildTimerMsg(&contLen);
23✔
266
  if (pReq != NULL) {
23!
267
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
23✔
268
                      .pCont = pReq,
269
                      .contLen = contLen,
270
                      .info.notFreeAhandle = 1,
271
                      .info.ahandle = 0};
272
    // TODO check return value
273
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
23!
274
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
275
    }
276
  }
277
}
23✔
278

279
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
582✔
280
  SSdb *pSdb = pMnode->pSdb;
582✔
281

282
  void *pIter = NULL;
582✔
283
  while (1) {
2,312✔
284
    SVgObj *pVgroup = NULL;
2,894✔
285
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
2,894✔
286
    if (pIter == NULL) break;
2,894✔
287

288
    bool stateChanged = false;
2,312✔
289
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
5,254✔
290
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
3,603✔
291
      if (pGid->dnodeId == dnodeId) {
3,603✔
292
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
661✔
293
          mInfo(
421!
294
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:error restored:0 "
295
              "canRead:0",
296
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
297
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
421✔
298
          pGid->syncRestore = 0;
421✔
299
          pGid->syncCanRead = 0;
421✔
300
          pGid->startTimeMs = 0;
421✔
301
          stateChanged = true;
421✔
302
        }
303
        break;
661✔
304
      }
305
    }
306

307
    if (stateChanged) {
2,312✔
308
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
421✔
309
      if (pDb != NULL && pDb->stateTs != curMs) {
421!
310
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
275!
311
              curMs);
312
        pDb->stateTs = curMs;
275✔
313
      }
314
      mndReleaseDb(pMnode, pDb);
421✔
315
    }
316

317
    sdbRelease(pSdb, pVgroup);
2,312✔
318
  }
319
}
582✔
320

321
static void mndCheckDnodeOffline(SMnode *pMnode) {
11,846✔
322
  mTrace("check dnode offline");
11,846✔
323
  if (mndAcquireRpc(pMnode) != 0) return;
11,846!
324

325
  SSdb   *pSdb = pMnode->pSdb;
11,846✔
326
  int64_t curMs = taosGetTimestampMs();
11,846✔
327

328
  void *pIter = NULL;
11,846✔
329
  while (1) {
21,873✔
330
    SDnodeObj *pDnode = NULL;
33,719✔
331
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
33,719✔
332
    if (pIter == NULL) break;
33,719✔
333

334
    bool online = mndIsDnodeOnline(pDnode, curMs);
21,873✔
335
    if (!online) {
21,873✔
336
      mInfo("dnode:%d, in offline state", pDnode->id);
582!
337
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
582✔
338
    }
339

340
    sdbRelease(pSdb, pDnode);
21,873✔
341
  }
342

343
  mndReleaseRpc(pMnode);
11,846✔
344
}
345

346
static bool mnodeIsNotLeader(SMnode *pMnode) {
68,834✔
347
  terrno = 0;
68,834✔
348
  (void)taosThreadRwlockRdlock(&pMnode->lock);
68,834✔
349
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
68,834✔
350
  if (terrno != 0) {
68,834!
351
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
352
    return true;
×
353
  }
354

355
  if (state.state != TAOS_SYNC_STATE_LEADER) {
68,834✔
356
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,098✔
357
    terrno = TSDB_CODE_SYN_NOT_LEADER;
5,098✔
358
    return true;
5,098✔
359
  }
360
  if (!state.restored || !pMnode->restored) {
63,736!
361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
41✔
362
    terrno = TSDB_CODE_SYN_RESTORING;
41✔
363
    return true;
41✔
364
  }
365
  (void)taosThreadRwlockUnlock(&pMnode->lock);
63,695✔
366
  return false;
63,695✔
367
}
368

369
static int32_t minCronTime() {
×
370
  int32_t min = INT32_MAX;
×
371
  min = TMIN(min, tsTtlPushIntervalSec);
×
372
  min = TMIN(min, tsTrimVDbIntervalSec);
×
373
  min = TMIN(min, tsS3MigrateIntervalSec);
×
374
  min = TMIN(min, tsTransPullupInterval);
×
375
  min = TMIN(min, tsCompactPullupInterval);
×
376
  min = TMIN(min, tsMqRebalanceInterval);
×
377
  min = TMIN(min, tsStreamCheckpointInterval);
×
378
  min = TMIN(min, tsStreamNodeCheckInterval);
×
379
  min = TMIN(min, tsArbHeartBeatIntervalSec);
×
380
  min = TMIN(min, tsArbCheckSyncIntervalSec);
×
381

382
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
383
  min = TMIN(min, telemInt);
×
384
  min = TMIN(min, tsGrantHBInterval);
×
385
  min = TMIN(min, tsUptimeInterval);
×
386

387
  return min <= 1 ? 2 : min;
×
388
}
389
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
63,695✔
390
  int32_t code = 0;
63,695✔
391
#ifndef TD_ASTRA  
392
  if (sec % tsTtlPushIntervalSec == 0) {
63,695✔
393
    mndPullupTtl(pMnode);
6,036✔
394
  }
395

396
  if (sec % tsTrimVDbIntervalSec == 0) {
63,695!
UNCOV
397
    mndPullupTrimDb(pMnode);
×
398
  }
399
#endif
400
#ifdef USE_S3
401
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
63,695!
402
    mndPullupS3MigrateDb(pMnode);
×
403
  }
404
#endif
405
  if (sec % tsTransPullupInterval == 0) {
63,695✔
406
    mndPullupTrans(pMnode);
32,108✔
407
  }
408

409
  if (sec % tsCompactPullupInterval == 0) {
63,695✔
410
    mndPullupCompacts(pMnode);
5,430✔
411
  }
412
#ifdef USE_TOPIC
413
  if (sec % tsMqRebalanceInterval == 0) {
63,695✔
414
    mndCalMqRebalance(pMnode);
31,260✔
415
  }
416
#endif
417
#ifdef USE_STREAM
418
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
63,695✔
419
    mndStreamCheckpointTimer(pMnode);
1,446✔
420
  }
421

422
  if (sec % tsStreamNodeCheckInterval == 0) {
63,695✔
423
    mndStreamCheckNode(pMnode);
39✔
424
  }
425

426
  if (sec % (tsStreamFailedTimeout/1000) == 0) {
63,695✔
427
    mndStreamCheckStatus(pMnode);
1✔
428
  }
429

430
  if (sec % 30 == 0) {
63,695✔
431
    mndStreamConsensusChkpt(pMnode);
1,446✔
432
  }
433

434
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
63,695!
435
    mndPullupTelem(pMnode);
2✔
436
  }
437
#endif
438
#ifndef TD_ASTRA
439
  if (sec % tsGrantHBInterval == 0) {
63,695✔
440
    mndPullupGrant(pMnode);
5,919✔
441
  }
442
#endif
443
  if (sec % tsUptimeInterval == 0) {
63,695✔
444
    mndIncreaseUpTime(pMnode);
23✔
445
  }
446
#ifndef TD_ASTRA
447
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
63,695✔
448
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
31,267!
449
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
450
    }
451
  }
452

453
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
63,695✔
454
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
20,496!
455
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
456
    }
457
  }
458
#endif
459
}
63,695✔
460
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
63,695✔
461
  if (sec % (tsStatusInterval * 5) == 0) {
63,695✔
462
    mndCheckDnodeOffline(pMnode);
11,846✔
463
  }
464
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
63,695✔
465
    mndSyncCheckTimeout(pMnode);
1,446✔
466
  }
467
}
63,695✔
468

469
static void *mndThreadFp(void *param) {
2,557✔
470
  SMnode *pMnode = param;
2,557✔
471
  int64_t lastTime = 0;
2,557✔
472
  setThreadName("mnode-timer");
2,557✔
473

474
  while (1) {
700,066✔
475
    lastTime++;
702,623✔
476
    taosMsleep(100);
702,623✔
477

478
    if (mndGetStop(pMnode)) break;
702,623✔
479
    if (lastTime % 10 != 0) continue;
700,066✔
480

481
    if (mnodeIsNotLeader(pMnode)) {
68,834✔
482
      mTrace("timer not process since mnode is not leader");
5,139!
483
      continue;
5,139✔
484
    }
485

486
    int64_t sec = lastTime / 10;
63,695✔
487
    mndDoTimerCheckTask(pMnode, sec);
63,695✔
488

489
    mndDoTimerPullupTask(pMnode, sec);
63,695✔
490
  }
491

492
  return NULL;
2,557✔
493
}
494

495
static int32_t mndInitTimer(SMnode *pMnode) {
2,557✔
496
  int32_t      code = 0;
2,557✔
497
  TdThreadAttr thAttr;
498
  (void)taosThreadAttrInit(&thAttr);
2,557✔
499
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,557✔
500
#ifdef TD_COMPACT_OS
501
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
502
#endif
503
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode)) != 0) {
2,557!
504
    mError("failed to create timer thread since %s", tstrerror(code));
×
505
    TAOS_RETURN(code);
×
506
  }
507

508
  (void)taosThreadAttrDestroy(&thAttr);
2,557✔
509
  tmsgReportStartup("mnode-timer", "initialized");
2,557✔
510
  TAOS_RETURN(code);
2,557✔
511
}
512

513
static void mndCleanupTimer(SMnode *pMnode) {
2,557✔
514
  if (taosCheckPthreadValid(pMnode->thread)) {
2,557!
515
    (void)taosThreadJoin(pMnode->thread, NULL);
2,557✔
516
    taosThreadClear(&pMnode->thread);
2,557✔
517
  }
518
}
2,557✔
519

520
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
2,560✔
521
  int32_t code = 0;
2,560✔
522
  pMnode->path = taosStrdup(path);
2,560!
523
  if (pMnode->path == NULL) {
2,560!
524
    code = terrno;
×
525
    TAOS_RETURN(code);
×
526
  }
527

528
  if (taosMkDir(pMnode->path) != 0) {
2,560!
529
    code = terrno;
×
530
    TAOS_RETURN(code);
×
531
  }
532

533
  TAOS_RETURN(code);
2,560✔
534
}
535

536
static int32_t mndInitWal(SMnode *pMnode) {
2,560✔
537
  int32_t code = 0;
2,560✔
538
  char    path[PATH_MAX + 20] = {0};
2,560✔
539
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
2,560✔
540
  SWalCfg cfg = {.vgId = 1,
2,560✔
541
                 .fsyncPeriod = 0,
542
                 .rollPeriod = -1,
543
                 .segSize = -1,
544
                 .committed = -1,
545
                 .retentionPeriod = 0,
546
                 .retentionSize = 0,
547
                 .level = TAOS_WAL_FSYNC,
548
                 .encryptAlgorithm = 0,
549
                 .encryptKey = {0}};
550

551
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
552
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
2,560!
553
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
554
    if (tsEncryptKey[0] == '\0') {
×
555
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
556
      TAOS_RETURN(code);
×
557
    } else {
558
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
559
    }
560
  }
561
#endif
562

563
  pMnode->pWal = walOpen(path, &cfg);
2,560✔
564
  if (pMnode->pWal == NULL) {
2,560!
565
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
566
    if (terrno != 0) code = terrno;
×
567
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
568
    TAOS_RETURN(code);
×
569
  }
570

571
  TAOS_RETURN(code);
2,560✔
572
}
573

574
static void mndCloseWal(SMnode *pMnode) {
2,557✔
575
  if (pMnode->pWal != NULL) {
2,557!
576
    walClose(pMnode->pWal);
2,557✔
577
    pMnode->pWal = NULL;
2,557✔
578
  }
579
}
2,557✔
580

581
static int32_t mndInitSdb(SMnode *pMnode) {
2,560✔
582
  int32_t code = 0;
2,560✔
583
  SSdbOpt opt = {0};
2,560✔
584
  opt.path = pMnode->path;
2,560✔
585
  opt.pMnode = pMnode;
2,560✔
586
  opt.pWal = pMnode->pWal;
2,560✔
587

588
  pMnode->pSdb = sdbInit(&opt);
2,560✔
589
  if (pMnode->pSdb == NULL) {
2,560!
590
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
591
    if (terrno != 0) code = terrno;
×
592
    TAOS_RETURN(code);
×
593
  }
594

595
  TAOS_RETURN(code);
2,560✔
596
}
597

598
static int32_t mndOpenSdb(SMnode *pMnode) {
2,560✔
599
  int32_t code = 0;
2,560✔
600
  if (!pMnode->deploy) {
2,560✔
601
    code = sdbReadFile(pMnode->pSdb);
643✔
602
  }
603

604
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
2,560!
605

606
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
2,560✔
607
  return code;
2,560✔
608
}
609

610
static void mndCleanupSdb(SMnode *pMnode) {
2,557✔
611
  if (pMnode->pSdb) {
2,557!
612
    sdbCleanup(pMnode->pSdb);
2,557✔
613
    pMnode->pSdb = NULL;
2,557✔
614
  }
615
}
2,557✔
616

617
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
94,720✔
618
  SMnodeStep step = {0};
94,720✔
619
  step.name = name;
94,720✔
620
  step.initFp = initFp;
94,720✔
621
  step.cleanupFp = cleanupFp;
94,720✔
622
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
189,440!
623
    TAOS_RETURN(terrno);
×
624
  }
625

626
  TAOS_RETURN(0);
94,720✔
627
}
628

629
static int32_t mndInitSteps(SMnode *pMnode) {
2,560✔
630
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
2,560!
631
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
2,560!
632
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
2,560!
633
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
2,560!
634
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
2,560!
635
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
2,560!
636
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
2,560!
637
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
2,560!
638
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
2,560!
639
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
2,560!
640
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
2,560!
641
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
2,560!
642
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
2,560!
643
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
2,560!
644
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
2,560!
645
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
2,560!
646
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
2,560!
647
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
2,560!
648
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
2,560!
649
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
2,560!
650
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
2,560!
651
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
2,560!
652
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
2,560!
653
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
2,560!
654
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
2,560!
655
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
2,560!
656
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
2,560!
657
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
2,560!
658
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
2,560!
659
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
2,560!
660
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
2,560!
661
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
2,560!
662
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
2,560!
663
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
2,560!
664
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
2,560!
665
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
2,560!
666
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
2,560!
667
  return 0;
2,560✔
668
}
669

670
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
2,557✔
671
  if (pMnode->pSteps == NULL) return;
2,557!
672

673
  if (pos == -1) {
2,557!
674
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
2,557✔
675
  }
676

677
  for (int32_t s = pos; s >= 0; s--) {
97,166✔
678
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
94,609✔
679
    mInfo("%s will cleanup", pStep->name);
94,609!
680
    if (pStep->cleanupFp != NULL) {
94,609✔
681
      (*pStep->cleanupFp)(pMnode);
89,495✔
682
    }
683
  }
684

685
  taosArrayClear(pMnode->pSteps);
2,557✔
686
  taosArrayDestroy(pMnode->pSteps);
2,557✔
687
  pMnode->pSteps = NULL;
2,557✔
688
}
689

690
static int32_t mndExecSteps(SMnode *pMnode) {
2,560✔
691
  int32_t code = 0;
2,560✔
692
  int32_t size = taosArrayGetSize(pMnode->pSteps);
2,560✔
693
  for (int32_t pos = 0; pos < size; pos++) {
97,280✔
694
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
94,720✔
695
    if (pStep->initFp == NULL) continue;
94,720!
696

697
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
94,720!
698
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
699
      mndCleanupSteps(pMnode, pos);
×
700
      TAOS_RETURN(code);
×
701
    } else {
702
      mInfo("%s is initialized", pStep->name);
94,720!
703
      tmsgReportStartup(pStep->name, "initialized");
94,720✔
704
    }
705
  }
706

707
  pMnode->clusterId = mndGetClusterId(pMnode);
2,560✔
708
  TAOS_RETURN(0);
2,560✔
709
}
710

711
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
2,560✔
712
  pMnode->msgCb = pOption->msgCb;
2,560✔
713
  pMnode->selfDnodeId = pOption->dnodeId;
2,560✔
714
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
2,560✔
715
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
2,560✔
716
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
2,560✔
717
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
2,560✔
718
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
2,560✔
719
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
2,560✔
720
}
2,560✔
721

722
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
2,560✔
723
  terrno = 0;
2,560✔
724
  mInfo("start to open mnode in %s", path);
2,560!
725

726
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
2,560!
727
  if (pMnode == NULL) {
2,560!
728
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
729
    mError("failed to open mnode since %s", terrstr());
×
730
    return NULL;
×
731
  }
732
  (void)memset(pMnode, 0, sizeof(SMnode));
2,560✔
733

734
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
2,560✔
735
  if (code != 0) {
2,560!
736
    taosMemoryFree(pMnode);
×
737
    mError("failed to open mnode lock since %s", tstrerror(code));
×
738
    return NULL;
×
739
  }
740

741
  char timestr[24] = "1970-01-01 00:00:00.00";
2,560✔
742
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
2,560✔
743
  if (code < 0) {
2,560!
744
    mError("failed to parse time since %s", tstrerror(code));
×
745
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
746
    taosMemoryFree(pMnode);
×
747
    return NULL;
×
748
  }
749
  mndSetOptions(pMnode, pOption);
2,560✔
750

751
  pMnode->deploy = pOption->deploy;
2,560✔
752
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
2,560✔
753
  if (pMnode->pSteps == NULL) {
2,560!
754
    taosMemoryFree(pMnode);
×
755
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
756
    mError("failed to open mnode since %s", terrstr());
×
757
    return NULL;
×
758
  }
759

760
  code = mndCreateDir(pMnode, path);
2,560✔
761
  if (code != 0) {
2,560!
762
    code = terrno;
×
763
    mError("failed to open mnode since %s", tstrerror(code));
×
764
    mndClose(pMnode);
×
765
    terrno = code;
×
766
    return NULL;
×
767
  }
768

769
  code = mndInitSteps(pMnode);
2,560✔
770
  if (code != 0) {
2,560!
771
    code = terrno;
×
772
    mError("failed to open mnode since %s", tstrerror(code));
×
773
    mndClose(pMnode);
×
774
    terrno = code;
×
775
    return NULL;
×
776
  }
777

778
  code = mndExecSteps(pMnode);
2,560✔
779
  if (code != 0) {
2,560!
780
    code = terrno;
×
781
    mError("failed to open mnode since %s", tstrerror(code));
×
782
    mndClose(pMnode);
×
783
    terrno = code;
×
784
    return NULL;
×
785
  }
786

787
  mInfo("mnode open successfully");
2,560!
788
  return pMnode;
2,560✔
789
}
790

791
void mndPreClose(SMnode *pMnode) {
2,557✔
792
  if (pMnode != NULL) {
2,557!
793
    int32_t code = 0;
2,557✔
794
    // TODO check return value
795
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
2,557✔
796
    if (code < 0) {
2,557✔
797
      mError("failed to transfer leader since %s", tstrerror(code));
4!
798
    }
799
    syncPreStop(pMnode->syncMgmt.sync);
2,557✔
800
    code = sdbWriteFile(pMnode->pSdb, 0);
2,557✔
801
    if (code < 0) {
2,557!
802
      mError("failed to write sdb since %s", tstrerror(code));
×
803
    }
804
  }
805
}
2,557✔
806

807
void mndClose(SMnode *pMnode) {
2,557✔
808
  if (pMnode != NULL) {
2,557!
809
    mInfo("start to close mnode");
2,557!
810
    mndCleanupSteps(pMnode, -1);
2,557✔
811
    taosMemoryFreeClear(pMnode->path);
2,557!
812
    taosMemoryFreeClear(pMnode);
2,557!
813
    mInfo("mnode is closed");
2,557!
814
  }
815
}
2,557✔
816

817
int32_t mndStart(SMnode *pMnode) {
2,557✔
818
  mndSyncStart(pMnode);
2,557✔
819
  if (pMnode->deploy) {
2,557✔
820
    if (sdbDeploy(pMnode->pSdb) != 0) {
1,917!
821
      mError("failed to deploy sdb while start mnode");
×
822
      return -1;
×
823
    }
824
    mndSetRestored(pMnode, true);
1,917✔
825
  }
826
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
2,557✔
827

828
  return mndInitTimer(pMnode);
2,557✔
829
}
830

831
int32_t mndIsCatchUp(SMnode *pMnode) {
913✔
832
  int64_t rid = pMnode->syncMgmt.sync;
913✔
833
  return syncIsCatchUp(rid);
913✔
834
}
835

836
ESyncRole mndGetRole(SMnode *pMnode) {
913✔
837
  int64_t rid = pMnode->syncMgmt.sync;
913✔
838
  return syncGetRole(rid);
913✔
839
}
840

841
int64_t mndGetTerm(SMnode *pMnode) {
20,569✔
842
  int64_t rid = pMnode->syncMgmt.sync;
20,569✔
843
  return syncGetTerm(rid);
20,569✔
844
}
845

846
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
51,838✔
847

848
void mndStop(SMnode *pMnode) {
2,557✔
849
  mndSetStop(pMnode);
2,557✔
850
  mndSyncStop(pMnode);
2,557✔
851
  mndCleanupTimer(pMnode);
2,557✔
852
}
2,557✔
853

854
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
192,920✔
855
  SMnode    *pMnode = pMsg->info.node;
192,920✔
856
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
192,920✔
857

858
  const STraceId *trace = &pMsg->info.traceId;
192,920✔
859
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
192,920!
860

861
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
192,920✔
862
  if (code != 0) {
192,920✔
863
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
4!
864
            tstrerror(code), code);
865
  }
866

867
  return code;
192,920✔
868
}
869

870
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
750,815✔
871
  int32_t code = 0;
750,815✔
872
  if (!IsReq(pMsg)) TAOS_RETURN(code);
750,815✔
873
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
636,759✔
874
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
615,946!
875
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
600,894✔
876
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
559,539!
877
    TAOS_RETURN(code);
77,203✔
878
  }
879

880
  SMnode *pMnode = pMsg->info.node;
559,556✔
881
  (void)taosThreadRwlockRdlock(&pMnode->lock);
559,556✔
882
  if (pMnode->stopped) {
560,181✔
883
    (void)taosThreadRwlockUnlock(&pMnode->lock);
46✔
884
    code = TSDB_CODE_APP_IS_STOPPING;
46✔
885
    TAOS_RETURN(code);
46✔
886
  }
887

888
  terrno = 0;
560,135✔
889
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
559,763✔
890
  if (terrno != 0) {
560,166!
891
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
892
    code = terrno;
×
893
    TAOS_RETURN(code);
×
894
  }
895

896
  if (state.state != TAOS_SYNC_STATE_LEADER) {
560,163✔
897
    (void)taosThreadRwlockUnlock(&pMnode->lock);
25,945✔
898
    code = TSDB_CODE_SYN_NOT_LEADER;
25,943✔
899
    goto _OVER;
25,943✔
900
  }
901

902
  if (!state.restored || !pMnode->restored) {
534,218✔
903
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,268✔
904
    code = TSDB_CODE_SYN_RESTORING;
2,265✔
905
    goto _OVER;
2,265✔
906
  }
907

908
#if 1
909
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
531,950✔
910
#else
911
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
912
  mTrace("mnode rpc is acquired, ref:%d", ref);
913
#endif
914

915
  (void)taosThreadRwlockUnlock(&pMnode->lock);
531,942✔
916
  TAOS_RETURN(code);
531,943✔
917

918
_OVER:
28,208✔
919
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
28,208!
920
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
28,208!
921
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
28,209!
922
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
28,209!
923
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
28,210!
924
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
28,210!
925
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER) {
28,210!
926
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
927
           pMnode->stopped, state.restored, syncStr(state.state));
928
    TAOS_RETURN(code);
×
929
  }
930

931
  const STraceId *trace = &pMsg->info.traceId;
28,208✔
932
  SEpSet          epSet = {0};
28,208✔
933
  mndGetMnodeEpSet(pMnode, &epSet);
28,208✔
934

935
  mGDebug(
28,210!
936
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
937
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
938
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
939
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
940

941
  if (epSet.numOfEps <= 0) return -1;
28,210!
942

943
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
109,318✔
944
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
81,108✔
945
  }
946

947
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
28,210✔
948
  pMsg->info.rsp = rpcMallocCont(contLen);
28,209✔
949
  if (pMsg->info.rsp != NULL) {
28,210!
950
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
28,210!
951
      mError("failed to serialize ep set");
×
952
    }
953
    pMsg->info.hasEpSet = 1;
28,209✔
954
    pMsg->info.rspLen = contLen;
28,209✔
955
  }
956

957
  TAOS_RETURN(code);
28,209✔
958
}
959

960
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
750,792✔
961
  SMnode         *pMnode = pMsg->info.node;
750,792✔
962
  const STraceId *trace = &pMsg->info.traceId;
750,792✔
963
  int32_t         code = TSDB_CODE_SUCCESS;
750,792✔
964

965
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
750,792✔
966
  MndMsgFpExt fpExt = NULL;
750,792✔
967
  if (fp == NULL) {
750,792✔
968
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
77,357✔
969
    if (fpExt == NULL) {
77,357!
UNCOV
970
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
971
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
UNCOV
972
      TAOS_RETURN(code);
×
973
    }
974
  }
975

976
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
750,792✔
977

978
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
723,298!
979
  if (fp)
723,298✔
980
    code = (*fp)(pMsg);
645,943✔
981
  else
982
    code = (*fpExt)(pMsg, pQueueInfo);
77,355✔
983
  mndReleaseRpc(pMnode);
723,340✔
984

985
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
723,368✔
986
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
106,454!
987
  } else if (code == 0) {
616,914✔
988
    mGTrace("msg:%p, successfully processed", pMsg);
608,103!
989
  } else {
990
    // TODO removve this wrong set code
991
    if (code == -1) {
8,811✔
992
      code = terrno;
9✔
993
    }
994
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
8,811!
995
            TMSG_INFO(pMsg->msgType));
996
  }
997

998
  TAOS_RETURN(code);
723,368✔
999
}
1000

1001
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
481,280✔
1002
  tmsg_t type = TMSG_INDEX(msgType);
481,280✔
1003
  if (type < TDMT_MAX) {
481,280!
1004
    pMnode->msgFp[type] = fp;
481,280✔
1005
  }
1006
}
481,280✔
1007

1008
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
20,480✔
1009
  tmsg_t type = TMSG_INDEX(msgType);
20,480✔
1010
  if (type < TDMT_MAX) {
20,480!
1011
    pMnode->msgFpExt[type] = fp;
20,480✔
1012
  }
1013
}
20,480✔
1014

1015
// Note: uid 0 is reserved
1016
int64_t mndGenerateUid(const char *name, int32_t len) {
54,887✔
1017
  int32_t hashval = MurmurHash3_32(name, len);
54,887✔
1018
  do {
×
1019
    int64_t us = taosGetTimestampUs();
54,892✔
1020
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
54,892✔
1021
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
54,892✔
1022
    if (uuid) {
54,893!
1023
      return llabs(uuid);
54,893✔
1024
    }
1025
  } while (true);
1026
}
1027

1028
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
16✔
1029
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1030
  int32_t code = mndAcquireRpc(pMnode);
16✔
1031
  if (code < 0) {
16!
1032
    TAOS_RETURN(code);
×
1033
  } else if (code == 1) {
16!
1034
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1035
  }
1036

1037
  SSdb   *pSdb = pMnode->pSdb;
16✔
1038
  int64_t ms = taosGetTimestampMs();
16✔
1039

1040
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
16✔
1041
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
16✔
1042
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
16✔
1043
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
16✔
1044
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
16!
1045
      pStbInfo->stbs == NULL) {
16!
1046
    mndReleaseRpc(pMnode);
×
1047
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1048
    if (terrno != 0) code = terrno;
×
1049
    TAOS_RETURN(code);
×
1050
  }
1051

1052
  // cluster info
1053
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
16✔
1054
  pClusterInfo->monitor_interval = tsMonitorInterval;
16✔
1055
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
16✔
1056
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
16✔
1057
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
16✔
1058
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
16✔
1059
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
16✔
1060

1061
  void *pIter = NULL;
16✔
1062
  while (1) {
16✔
1063
    SDnodeObj *pObj = NULL;
32✔
1064
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
32✔
1065
    if (pIter == NULL) break;
32✔
1066

1067
    SMonDnodeDesc desc = {0};
16✔
1068
    desc.dnode_id = pObj->id;
16✔
1069
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
16✔
1070
    if (mndIsDnodeOnline(pObj, ms)) {
16✔
1071
      tstrncpy(desc.status, "ready", sizeof(desc.status));
15✔
1072
    } else {
1073
      tstrncpy(desc.status, "offline", sizeof(desc.status));
1✔
1074
    }
1075
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
32!
1076
      mError("failed put dnode into array, but continue at this monitor report")
×
1077
    }
1078
    sdbRelease(pSdb, pObj);
16✔
1079
  }
1080

1081
  pIter = NULL;
16✔
1082
  while (1) {
16✔
1083
    SMnodeObj *pObj = NULL;
32✔
1084
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
32✔
1085
    if (pIter == NULL) break;
32✔
1086

1087
    SMonMnodeDesc desc = {0};
16✔
1088
    desc.mnode_id = pObj->id;
16✔
1089
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
16✔
1090

1091
    if (pObj->id == pMnode->selfDnodeId) {
16!
1092
      pClusterInfo->first_ep_dnode_id = pObj->id;
16✔
1093
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
16✔
1094
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1095
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
16✔
1096
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1097
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
16✔
1098
      desc.syncState = TAOS_SYNC_STATE_LEADER;
16✔
1099
    } else {
1100
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1101
      desc.syncState = pObj->syncState;
×
1102
    }
1103
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
32!
1104
      mError("failed to put mnode into array, but continue at this monitor report");
×
1105
    }
1106
    sdbRelease(pSdb, pObj);
16✔
1107
  }
1108

1109
  // vgroup info
1110
  pIter = NULL;
16✔
1111
  while (1) {
38✔
1112
    SVgObj *pVgroup = NULL;
54✔
1113
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
54✔
1114
    if (pIter == NULL) break;
54✔
1115

1116
    pClusterInfo->vgroups_total++;
38✔
1117
    pClusterInfo->tbs_total += pVgroup->numOfTables;
38✔
1118

1119
    SMonVgroupDesc desc = {0};
38✔
1120
    desc.vgroup_id = pVgroup->vgId;
38✔
1121

1122
    SName name = {0};
38✔
1123
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
38✔
1124
    if (code < 0) {
38!
1125
      mError("failed to get db name since %s", tstrerror(code));
×
1126
      sdbRelease(pSdb, pVgroup);
×
1127
      TAOS_RETURN(code);
×
1128
    }
1129
    (void)tNameGetDbName(&name, desc.database_name);
38✔
1130

1131
    desc.tables_num = pVgroup->numOfTables;
38✔
1132
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
38✔
1133
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
38✔
1134
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
76✔
1135
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
38✔
1136
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
38✔
1137
      pVnDesc->dnode_id = pVgid->dnodeId;
38✔
1138
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
38✔
1139
      pVnDesc->syncState = pVgid->syncState;
38✔
1140
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
38!
1141
        tstrncpy(desc.status, "ready", sizeof(desc.status));
38✔
1142
        pClusterInfo->vgroups_alive++;
38✔
1143
      }
1144
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
38!
1145
        pClusterInfo->vnodes_alive++;
38✔
1146
      }
1147
      pClusterInfo->vnodes_total++;
38✔
1148
    }
1149

1150
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
76!
1151
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1152
    }
1153
    sdbRelease(pSdb, pVgroup);
38✔
1154
  }
1155

1156
  // stb info
1157
  pIter = NULL;
16✔
1158
  while (1) {
15✔
1159
    SStbObj *pStb = NULL;
31✔
1160
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
31✔
1161
    if (pIter == NULL) break;
31✔
1162

1163
    SMonStbDesc desc = {0};
15✔
1164

1165
    SName name1 = {0};
15✔
1166
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
15✔
1167
    if (code < 0) {
15!
1168
      mError("failed to get db name since %s", tstrerror(code));
×
1169
      sdbRelease(pSdb, pStb);
×
1170
      TAOS_RETURN(code);
×
1171
    }
1172
    (void)tNameGetDbName(&name1, desc.database_name);
15✔
1173

1174
    SName name2 = {0};
15✔
1175
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
15✔
1176
    if (code < 0) {
15!
1177
      mError("failed to get table name since %s", tstrerror(code));
×
1178
      sdbRelease(pSdb, pStb);
×
1179
      TAOS_RETURN(code);
×
1180
    }
1181
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
15✔
1182

1183
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
30!
1184
      mError("failed to put stb into array, but continue at this monitor report");
×
1185
    }
1186
    sdbRelease(pSdb, pStb);
15✔
1187
  }
1188

1189
  // grant info
1190
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
16✔
1191
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
16✔
1192
  if (pMnode->grant.expireTimeMS == 0) {
16!
1193
    pGrantInfo->expire_time = 0;
×
1194
    pGrantInfo->timeseries_total = 0;
×
1195
  }
1196

1197
  mndReleaseRpc(pMnode);
16✔
1198
  TAOS_RETURN(code);
16✔
1199
}
1200

1201
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
68,866✔
1202
  mTrace("mnode get load");
68,866✔
1203
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
68,866✔
1204
  pLoad->syncState = state.state;
68,866✔
1205
  pLoad->syncRestore = state.restored;
68,866✔
1206
  pLoad->syncTerm = state.term;
68,866✔
1207
  pLoad->roleTimeMs = state.roleTimeMs;
68,866✔
1208
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
68,866✔
1209
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1210
  return 0;
68,866✔
1211
}
1212

1213
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
20,496✔
1214
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
20,496✔
1215
  return state.roleTimeMs;
20,496✔
1216
}
1217

1218
void mndSetRestored(SMnode *pMnode, bool restored) {
2,557✔
1219
  if (restored) {
2,557!
1220
    (void)taosThreadRwlockWrlock(&pMnode->lock);
2,557✔
1221
    pMnode->restored = true;
2,557✔
1222
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,557✔
1223
    mInfo("mnode set restored:%d", restored);
2,557!
1224
  } else {
1225
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1226
    pMnode->restored = false;
×
1227
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1228
    mInfo("mnode set restored:%d", restored);
×
1229
    while (1) {
1230
      if (pMnode->rpcRef <= 0) break;
×
1231
      taosMsleep(3);
×
1232
    }
1233
  }
1234
}
2,557✔
1235

1236
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1237

1238
void mndSetStop(SMnode *pMnode) {
2,557✔
1239
  (void)taosThreadRwlockWrlock(&pMnode->lock);
2,557✔
1240
  pMnode->stopped = true;
2,557✔
1241
  (void)taosThreadRwlockUnlock(&pMnode->lock);
2,557✔
1242
  mInfo("mnode set stopped");
2,557!
1243
}
2,557✔
1244

1245
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
702,623✔
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