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

taosdata / TDengine / #4712

06 Sep 2025 04:27PM UTC coverage: 58.144% (-1.0%) from 59.134%
#4712

push

travis-ci

GitHub
test: update case description (#32878)

133123 of 291691 branches covered (45.64%)

Branch coverage included in aggregate %.

201244 of 283375 relevant lines covered (71.02%)

5637899.03 hits per line

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

66.08
/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 "mndSsMigrate.h"
24
#include "mndConfig.h"
25
#include "mndConsumer.h"
26
#include "mndDb.h"
27
#include "mndDnode.h"
28
#include "mndFunc.h"
29
#include "mndGrant.h"
30
#include "mndIndex.h"
31
#include "mndInfoSchema.h"
32
#include "mndMnode.h"
33
#include "mndMount.h"
34
#include "mndPerfSchema.h"
35
#include "mndPrivilege.h"
36
#include "mndProfile.h"
37
#include "mndQnode.h"
38
#include "mndQuery.h"
39
#include "mndShow.h"
40
#include "mndSma.h"
41
#include "mndSnode.h"
42
#include "mndStb.h"
43
#include "mndStream.h"
44
#include "mndSubscribe.h"
45
#include "mndSync.h"
46
#include "mndTelem.h"
47
#include "mndTopic.h"
48
#include "mndTrans.h"
49
#include "mndUser.h"
50
#include "mndVgroup.h"
51
#include "mndView.h"
52
#include "mndBnode.h"
53

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

73
static inline void mndReleaseRpc(SMnode *pMnode) {
646,137✔
74
  (void)taosThreadRwlockRdlock(&pMnode->lock);
646,137✔
75
#if 1
76
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
646,153✔
77
#else
78
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
79
  mTrace("mnode rpc is released, ref:%d", ref);
80
#endif
81
  (void)taosThreadRwlockUnlock(&pMnode->lock);
646,151✔
82
}
646,155✔
83

84
static void *mndBuildTimerMsg(int32_t *pContLen) {
117,687✔
85
  terrno = 0;
117,687✔
86
  SMTimerReq timerReq = {0};
117,687✔
87

88
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
117,687✔
89
  if (contLen <= 0) return NULL;
117,687!
90
  void *pReq = rpcMallocCont(contLen);
117,687✔
91
  if (pReq == NULL) return NULL;
117,687!
92

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

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

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

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

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

148
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
149
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
150
    return;
×
151
  }
152

153
  mTrace("pullup ssmigrate db");
×
154
  int32_t contLen = 0;
×
155
  void   *pReq = mndBuildTimerMsg(&contLen);
×
156
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
157
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
158
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
159
  }
160
}
161

162
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
×
163
  mTrace("pullup update ssmigrate progress");
×
164
  int32_t contLen = 0;
×
165
  void   *pReq = mndBuildTimerMsg(&contLen);
×
166
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
167
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
168
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
169
  }
170
}
×
171

172
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
28,217✔
173
  mTrace("pullup arb hb");
28,217✔
174
  int32_t contLen = 0;
28,217✔
175
  void   *pReq = mndBuildTimerMsg(&contLen);
28,217✔
176
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
28,217✔
177
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
28,217✔
178
}
179

180
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
18,574✔
181
  mTrace("pullup arb sync");
18,574✔
182
  int32_t contLen = 0;
18,574✔
183
  void   *pReq = mndBuildTimerMsg(&contLen);
18,574✔
184
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
18,574✔
185
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
18,574✔
186
}
187

188
static void mndCalMqRebalance(SMnode *pMnode) {
28,204✔
189
  int32_t contLen = 0;
28,204✔
190
  void   *pReq = mndBuildTimerMsg(&contLen);
28,204✔
191
  if (pReq != NULL) {
28,204!
192
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
28,204✔
193
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
28,204!
194
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
195
    }
196
  }
197
}
28,204✔
198

199
static void mndPullupTelem(SMnode *pMnode) {
2✔
200
  mTrace("pullup telem msg");
2!
201
  int32_t contLen = 0;
2✔
202
  void   *pReq = mndBuildTimerMsg(&contLen);
2✔
203
  if (pReq != NULL) {
2!
204
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
2✔
205
    // TODO check return value
206
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
2!
207
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
208
    }
209
  }
210
}
2✔
211

212
static void mndPullupGrant(SMnode *pMnode) {
4,259✔
213
  mTrace("pullup grant msg");
4,259✔
214
  int32_t contLen = 0;
4,259✔
215
  void   *pReq = mndBuildTimerMsg(&contLen);
4,259✔
216
  if (pReq != NULL) {
4,259!
217
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
4,259✔
218
                      .pCont = pReq,
219
                      .contLen = contLen,
220
                      .info.notFreeAhandle = 1,
221
                      .info.ahandle = 0};
222
    // TODO check return value
223
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
4,259!
224
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
225
    }
226
  }
227
}
4,259✔
228

229
static void mndIncreaseUpTime(SMnode *pMnode) {
9✔
230
  mTrace("increate uptime");
9!
231
  int32_t contLen = 0;
9✔
232
  void   *pReq = mndBuildTimerMsg(&contLen);
9✔
233
  if (pReq != NULL) {
9!
234
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
9✔
235
                      .pCont = pReq,
236
                      .contLen = contLen,
237
                      .info.notFreeAhandle = 1,
238
                      .info.ahandle = 0};
239
    // TODO check return value
240
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
9!
241
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
242
    }
243
  }
244
}
9✔
245

246
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
295✔
247
  SSdb *pSdb = pMnode->pSdb;
295✔
248

249
  void *pIter = NULL;
295✔
250
  while (1) {
739✔
251
    SVgObj *pVgroup = NULL;
1,034✔
252
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,034✔
253
    if (pIter == NULL) break;
1,034✔
254

255
    bool stateChanged = false;
739✔
256
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,886✔
257
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,405✔
258
      if (pGid->dnodeId == dnodeId) {
1,405✔
259
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
258✔
260
          mInfo(
47!
261
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:error restored:0 "
262
              "canRead:0",
263
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
264
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
47✔
265
          pGid->syncRestore = 0;
47✔
266
          pGid->syncCanRead = 0;
47✔
267
          pGid->startTimeMs = 0;
47✔
268
          stateChanged = true;
47✔
269
        }
270
        break;
258✔
271
      }
272
    }
273

274
    if (stateChanged) {
739✔
275
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
47✔
276
      if (pDb != NULL && pDb->stateTs != curMs) {
47!
277
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
27!
278
              curMs);
279
        pDb->stateTs = curMs;
27✔
280
      }
281
      mndReleaseDb(pMnode, pDb);
47✔
282
    }
283

284
    sdbRelease(pSdb, pVgroup);
739✔
285
  }
286
}
295✔
287

288
static void mndCheckDnodeOffline(SMnode *pMnode) {
10,814✔
289
  mTrace("check dnode offline");
10,814✔
290
  if (mndAcquireRpc(pMnode) != 0) return;
10,814!
291

292
  SSdb   *pSdb = pMnode->pSdb;
10,814✔
293
  int64_t curMs = taosGetTimestampMs();
10,814✔
294

295
  void *pIter = NULL;
10,814✔
296
  while (1) {
18,715✔
297
    SDnodeObj *pDnode = NULL;
29,529✔
298
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
29,529✔
299
    if (pIter == NULL) break;
29,529✔
300

301
    bool online = mndIsDnodeOnline(pDnode, curMs);
18,715✔
302
    if (!online) {
18,715✔
303
      mInfo("dnode:%d, in offline state", pDnode->id);
295!
304
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
295✔
305
    }
306

307
    sdbRelease(pSdb, pDnode);
18,715✔
308
  }
309

310
  mndReleaseRpc(pMnode);
10,814✔
311
}
312

313
static bool mnodeIsNotLeader(SMnode *pMnode) {
63,546✔
314
  terrno = 0;
63,546✔
315
  (void)taosThreadRwlockRdlock(&pMnode->lock);
63,546✔
316
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
63,546✔
317
  if (terrno != 0) {
63,546!
318
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
319
    return true;
×
320
  }
321

322
  if (state.state != TAOS_SYNC_STATE_LEADER) {
63,546✔
323
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,218✔
324
    terrno = TSDB_CODE_SYN_NOT_LEADER;
6,218✔
325
    return true;
6,218✔
326
  }
327
  if (!state.restored || !pMnode->restored) {
57,328!
328
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
329
    terrno = TSDB_CODE_SYN_RESTORING;
×
330
    return true;
×
331
  }
332
  (void)taosThreadRwlockUnlock(&pMnode->lock);
57,328✔
333
  return false;
57,328✔
334
}
335

336
static int32_t minCronTime() {
×
337
  int32_t min = INT32_MAX;
×
338
  min = TMIN(min, tsTtlPushIntervalSec);
×
339
  min = TMIN(min, tsTrimVDbIntervalSec);
×
340
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
341
  min = TMIN(min, tsTransPullupInterval);
×
342
  min = TMIN(min, tsCompactPullupInterval);
×
343
  min = TMIN(min, tsMqRebalanceInterval);
×
344
  min = TMIN(min, tsArbHeartBeatIntervalSec);
×
345
  min = TMIN(min, tsArbCheckSyncIntervalSec);
×
346

347
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
348
  min = TMIN(min, telemInt);
×
349
  min = TMIN(min, tsGrantHBInterval);
×
350
  min = TMIN(min, tsUptimeInterval);
×
351

352
  return min <= 1 ? 2 : min;
×
353
}
354
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
57,328✔
355
  int32_t code = 0;
57,328✔
356
#ifndef TD_ASTRA
357
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
57,328✔
358
    mndPullupGrant(pMnode);
4,259✔
359
  }
360
  if (sec % tsTtlPushIntervalSec == 0) {
57,328✔
361
    mndPullupTtl(pMnode);
5,158✔
362
  }
363

364
  if (sec % tsTrimVDbIntervalSec == 0) {
57,328!
365
    mndPullupTrimDb(pMnode);
×
366
  }
367
#endif
368
#ifdef USE_SHARED_STORAGE
369
  if (tsSsEnabled) {
57,328!
370
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
371
      mndPullupUpdateSsMigrateProgress(pMnode);
×
372
    }
373
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
374
      mndPullupSsMigrateDb(pMnode);
×
375
    }
376
  }
377
#endif
378
  if (sec % tsTransPullupInterval == 0) {
57,328✔
379
    mndPullupTrans(pMnode);
28,208✔
380
  }
381

382
  if (sec % tsCompactPullupInterval == 0) {
57,328✔
383
    mndPullupCompacts(pMnode);
5,056✔
384
  }
385
#ifdef USE_TOPIC
386
  if (sec % tsMqRebalanceInterval == 0) {
57,328✔
387
    mndCalMqRebalance(pMnode);
28,204✔
388
  }
389
#endif
390
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
57,328!
391
    mndPullupTelem(pMnode);
2✔
392
  }
393
  if (sec % tsUptimeInterval == 0) {
57,328✔
394
    mndIncreaseUpTime(pMnode);
9✔
395
  }
396
#ifndef TD_ASTRA
397
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
57,328✔
398
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
28,217!
399
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
400
    }
401
  }
402

403
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
57,328✔
404
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
18,574!
405
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
406
    }
407
  }
408
#endif
409
}
57,328✔
410

411
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
57,328✔
412
  if (sec % (tsStatusInterval * 5) == 0) {
57,328✔
413
    mndCheckDnodeOffline(pMnode);
10,814✔
414
  }
415
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
57,328✔
416
    mndSyncCheckTimeout(pMnode);
1,341✔
417
  }
418
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
57,328!
419
    msmHealthCheck(pMnode);
18,574✔
420
  }
421
}
57,328✔
422

423
static void *mndThreadFp(void *param) {
1,920✔
424
  SMnode *pMnode = param;
1,920✔
425
  int64_t lastTime = 0;
1,920✔
426
  setThreadName("mnode-timer");
1,920✔
427

428
  while (1) {
644,549✔
429
    lastTime++;
646,469✔
430
    taosMsleep(100);
646,469✔
431

432
    if (mndGetStop(pMnode)) break;
646,469✔
433
    if (lastTime % 10 != 0) continue;
644,549✔
434

435
    if (mnodeIsNotLeader(pMnode)) {
63,546✔
436
      mTrace("timer not process since mnode is not leader");
6,218!
437
      continue;
6,218✔
438
    }
439

440
    int64_t sec = lastTime / 10;
57,328✔
441
    mndDoTimerCheckTask(pMnode, sec);
57,328✔
442

443
    mndDoTimerPullupTask(pMnode, sec);
57,328✔
444
  }
445

446
  return NULL;
1,920✔
447
}
448

449
static int32_t mndInitTimer(SMnode *pMnode) {
1,920✔
450
  int32_t      code = 0;
1,920✔
451
  TdThreadAttr thAttr;
452
  (void)taosThreadAttrInit(&thAttr);
1,920✔
453
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,920✔
454
#ifdef TD_COMPACT_OS
455
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
456
#endif
457
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode)) != 0) {
1,920!
458
    mError("failed to create timer thread since %s", tstrerror(code));
×
459
    TAOS_RETURN(code);
×
460
  }
461

462
  (void)taosThreadAttrDestroy(&thAttr);
1,920✔
463
  tmsgReportStartup("mnode-timer", "initialized");
1,920✔
464
  TAOS_RETURN(code);
1,920✔
465
}
466

467
static void mndCleanupTimer(SMnode *pMnode) {
1,920✔
468
  if (taosCheckPthreadValid(pMnode->thread)) {
1,920!
469
    (void)taosThreadJoin(pMnode->thread, NULL);
1,920✔
470
    taosThreadClear(&pMnode->thread);
1,920✔
471
  }
472
}
1,920✔
473

474
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
1,920✔
475
  int32_t code = 0;
1,920✔
476
  pMnode->path = taosStrdup(path);
1,920!
477
  if (pMnode->path == NULL) {
1,920!
478
    code = terrno;
×
479
    TAOS_RETURN(code);
×
480
  }
481

482
  if (taosMkDir(pMnode->path) != 0) {
1,920!
483
    code = terrno;
×
484
    TAOS_RETURN(code);
×
485
  }
486

487
  TAOS_RETURN(code);
1,920✔
488
}
489

490
static int32_t mndInitWal(SMnode *pMnode) {
1,920✔
491
  int32_t code = 0;
1,920✔
492
  char    path[PATH_MAX + 20] = {0};
1,920✔
493
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
1,920✔
494
  SWalCfg cfg = {.vgId = 1,
1,920✔
495
                 .fsyncPeriod = 0,
496
                 .rollPeriod = -1,
497
                 .segSize = -1,
498
                 .committed = -1,
499
                 .retentionPeriod = 0,
500
                 .retentionSize = 0,
501
                 .level = TAOS_WAL_FSYNC,
502
                 .encryptAlgorithm = 0,
503
                 .encryptKey = {0}};
504

505
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
506
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
1,920!
507
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
508
    if (tsEncryptKey[0] == '\0') {
×
509
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
510
      TAOS_RETURN(code);
×
511
    } else {
512
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
513
    }
514
  }
515
#endif
516

517
  pMnode->pWal = walOpen(path, &cfg);
1,920✔
518
  if (pMnode->pWal == NULL) {
1,920!
519
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
520
    if (terrno != 0) code = terrno;
×
521
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
522
    TAOS_RETURN(code);
×
523
  }
524

525
  TAOS_RETURN(code);
1,920✔
526
}
527

528
static void mndCloseWal(SMnode *pMnode) {
1,920✔
529
  if (pMnode->pWal != NULL) {
1,920!
530
    walClose(pMnode->pWal);
1,920✔
531
    pMnode->pWal = NULL;
1,920✔
532
  }
533
}
1,920✔
534

535
static int32_t mndInitSdb(SMnode *pMnode) {
1,920✔
536
  int32_t code = 0;
1,920✔
537
  SSdbOpt opt = {0};
1,920✔
538
  opt.path = pMnode->path;
1,920✔
539
  opt.pMnode = pMnode;
1,920✔
540
  opt.pWal = pMnode->pWal;
1,920✔
541

542
  pMnode->pSdb = sdbInit(&opt);
1,920✔
543
  if (pMnode->pSdb == NULL) {
1,920!
544
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
545
    if (terrno != 0) code = terrno;
×
546
    TAOS_RETURN(code);
×
547
  }
548

549
  TAOS_RETURN(code);
1,920✔
550
}
551

552
static int32_t mndOpenSdb(SMnode *pMnode) {
1,920✔
553
  int32_t code = 0;
1,920✔
554
  if (!pMnode->deploy) {
1,920✔
555
    code = sdbReadFile(pMnode->pSdb);
501✔
556
  }
557

558
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
1,920!
559

560
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
1,920✔
561
  return code;
1,920✔
562
}
563

564
static void mndCleanupSdb(SMnode *pMnode) {
1,920✔
565
  if (pMnode->pSdb) {
1,920!
566
    sdbCleanup(pMnode->pSdb);
1,920✔
567
    pMnode->pSdb = NULL;
1,920✔
568
  }
569
}
1,920✔
570

571
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
76,800✔
572
  SMnodeStep step = {0};
76,800✔
573
  step.name = name;
76,800✔
574
  step.initFp = initFp;
76,800✔
575
  step.cleanupFp = cleanupFp;
76,800✔
576
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
153,600!
577
    TAOS_RETURN(terrno);
×
578
  }
579

580
  TAOS_RETURN(0);
76,800✔
581
}
582

583
static int32_t mndInitSteps(SMnode *pMnode) {
1,920✔
584
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
1,920!
585
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
1,920!
586
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
1,920!
587
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
1,920!
588
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
1,920!
589
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
1,920!
590
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
1,920!
591
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
1,920!
592
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
1,920!
593
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
1,920!
594
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
1,920!
595
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
1,920!
596
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
1,920!
597
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
1,920!
598
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
1,920!
599
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
1,920!
600
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
1,920!
601
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
1,920!
602
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
1,920!
603
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
1,920!
604
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
1,920!
605
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
1,920!
606
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
1,920!
607
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
1,920!
608
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
1,920!
609
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
1,920!
610
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
1,920!
611
#ifdef USE_MOUNT
612
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
1,920!
613
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
1,920!
614
#endif
615
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
1,920!
616
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
1,920!
617
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
1,920!
618
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
1,920!
619
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
1,920!
620
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
1,920!
621
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
1,920!
622
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
1,920!
623
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
1,920!
624
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
1,920!
625
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
1,920!
626
  return 0;
1,920✔
627
}
628

629
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
1,920✔
630
  if (pMnode->pSteps == NULL) return;
1,920!
631

632
  if (pos == -1) {
1,920!
633
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
1,920✔
634
  }
635

636
  for (int32_t s = pos; s >= 0; s--) {
78,720✔
637
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
76,800✔
638
    mInfo("%s will cleanup", pStep->name);
76,800!
639
    if (pStep->cleanupFp != NULL) {
76,800✔
640
      (*pStep->cleanupFp)(pMnode);
72,960✔
641
    }
642
  }
643

644
  taosArrayClear(pMnode->pSteps);
1,920✔
645
  taosArrayDestroy(pMnode->pSteps);
1,920✔
646
  pMnode->pSteps = NULL;
1,920✔
647
}
648

649
static int32_t mndExecSteps(SMnode *pMnode) {
1,920✔
650
  int32_t code = 0;
1,920✔
651
  int32_t size = taosArrayGetSize(pMnode->pSteps);
1,920✔
652
  for (int32_t pos = 0; pos < size; pos++) {
78,720✔
653
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
76,800✔
654
    if (pStep->initFp == NULL) continue;
76,800!
655

656
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
76,800!
657
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
658
      mndCleanupSteps(pMnode, pos);
×
659
      TAOS_RETURN(code);
×
660
    } else {
661
      mInfo("%s is initialized", pStep->name);
76,800!
662
      tmsgReportStartup(pStep->name, "initialized");
76,800✔
663
    }
664
  }
665

666
  pMnode->clusterId = mndGetClusterId(pMnode);
1,920✔
667
  TAOS_RETURN(0);
1,920✔
668
}
669

670
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
1,920✔
671
  pMnode->msgCb = pOption->msgCb;
1,920✔
672
  pMnode->selfDnodeId = pOption->dnodeId;
1,920✔
673
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
1,920✔
674
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
1,920✔
675
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
1,920✔
676
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
1,920✔
677
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
1,920✔
678
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
1,920✔
679
}
1,920✔
680

681
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
1,920✔
682
  terrno = 0;
1,920✔
683
  mInfo("start to open mnode in %s", path);
1,920!
684

685
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
1,920!
686
  if (pMnode == NULL) {
1,920!
687
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
688
    mError("failed to open mnode since %s", terrstr());
×
689
    return NULL;
×
690
  }
691
  (void)memset(pMnode, 0, sizeof(SMnode));
1,920✔
692

693
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
1,920✔
694
  if (code != 0) {
1,920!
695
    taosMemoryFree(pMnode);
×
696
    mError("failed to open mnode lock since %s", tstrerror(code));
×
697
    return NULL;
×
698
  }
699

700
  char timestr[24] = "1970-01-01 00:00:00.00";
1,920✔
701
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
1,920✔
702
  if (code < 0) {
1,920!
703
    mError("failed to parse time since %s", tstrerror(code));
×
704
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
705
    taosMemoryFree(pMnode);
×
706
    return NULL;
×
707
  }
708
  mndSetOptions(pMnode, pOption);
1,920✔
709

710
  pMnode->deploy = pOption->deploy;
1,920✔
711
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
1,920✔
712
  if (pMnode->pSteps == NULL) {
1,920!
713
    taosMemoryFree(pMnode);
×
714
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
715
    mError("failed to open mnode since %s", terrstr());
×
716
    return NULL;
×
717
  }
718

719
  code = mndCreateDir(pMnode, path);
1,920✔
720
  if (code != 0) {
1,920!
721
    code = terrno;
×
722
    mError("failed to open mnode since %s", tstrerror(code));
×
723
    mndClose(pMnode);
×
724
    terrno = code;
×
725
    return NULL;
×
726
  }
727

728
  code = mndInitSteps(pMnode);
1,920✔
729
  if (code != 0) {
1,920!
730
    code = terrno;
×
731
    mError("failed to open mnode since %s", tstrerror(code));
×
732
    mndClose(pMnode);
×
733
    terrno = code;
×
734
    return NULL;
×
735
  }
736

737
  code = mndExecSteps(pMnode);
1,920✔
738
  if (code != 0) {
1,920!
739
    code = terrno;
×
740
    mError("failed to open mnode since %s", tstrerror(code));
×
741
    mndClose(pMnode);
×
742
    terrno = code;
×
743
    return NULL;
×
744
  }
745

746
  mInfo("mnode open successfully");
1,920!
747
  return pMnode;
1,920✔
748
}
749

750
void mndPreClose(SMnode *pMnode) {
1,920✔
751
  if (pMnode != NULL) {
1,920!
752
    int32_t code = 0;
1,920✔
753
    // TODO check return value
754
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
1,920✔
755
    if (code < 0) {
1,920!
756
      mError("failed to transfer leader since %s", tstrerror(code));
×
757
    }
758
    syncPreStop(pMnode->syncMgmt.sync);
1,920✔
759
    code = sdbWriteFile(pMnode->pSdb, 0);
1,920✔
760
    if (code < 0) {
1,920!
761
      mError("failed to write sdb since %s", tstrerror(code));
×
762
    }
763
  }
764
}
1,920✔
765

766
void mndClose(SMnode *pMnode) {
1,920✔
767
  if (pMnode != NULL) {
1,920!
768
    mInfo("start to close mnode");
1,920!
769
    mndCleanupSteps(pMnode, -1);
1,920✔
770
    taosMemoryFreeClear(pMnode->path);
1,920!
771
    taosMemoryFreeClear(pMnode);
1,920!
772
    mInfo("mnode is closed");
1,920!
773
  }
774
}
1,920✔
775

776
int32_t mndStart(SMnode *pMnode) {
1,920✔
777
  mndSyncStart(pMnode);
1,920✔
778
  if (pMnode->deploy) {
1,920✔
779
    if (sdbDeploy(pMnode->pSdb) != 0) {
1,419!
780
      mError("failed to deploy sdb while start mnode");
×
781
      return -1;
×
782
    }
783
    mndSetRestored(pMnode, true);
1,419✔
784
  }
785
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
1,920✔
786

787
  return mndInitTimer(pMnode);
1,920✔
788
}
789

790
int32_t mndIsCatchUp(SMnode *pMnode) {
1,231✔
791
  int64_t rid = pMnode->syncMgmt.sync;
1,231✔
792
  return syncIsCatchUp(rid);
1,231✔
793
}
794

795
ESyncRole mndGetRole(SMnode *pMnode) {
1,231✔
796
  int64_t rid = pMnode->syncMgmt.sync;
1,231✔
797
  return syncGetRole(rid);
1,231✔
798
}
799

800
int64_t mndGetTerm(SMnode *pMnode) {
18,749✔
801
  int64_t rid = pMnode->syncMgmt.sync;
18,749✔
802
  return syncGetTerm(rid);
18,749✔
803
}
804

805
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
46,959✔
806

807
void mndStop(SMnode *pMnode) {
1,920✔
808
  mndSetStop(pMnode);
1,920✔
809
  mndSyncStop(pMnode);
1,920✔
810
  mndCleanupTimer(pMnode);
1,920✔
811
}
1,920✔
812

813
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
176,738✔
814
  SMnode    *pMnode = pMsg->info.node;
176,738✔
815
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
176,738✔
816

817
  const STraceId *trace = &pMsg->info.traceId;
176,738✔
818
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
176,738!
819

820
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
176,738✔
821
  if (code != 0) {
176,736✔
822
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
2!
823
            tstrerror(code), code);
824
  }
825

826
  return code;
176,736✔
827
}
828

829
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
649,188✔
830
  int32_t code = 0;
649,188✔
831
  if (!IsReq(pMsg)) TAOS_RETURN(code);
649,188✔
832
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
569,945✔
833
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
551,192!
834
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
538,027✔
835
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
500,426!
836
    TAOS_RETURN(code);
69,508✔
837
  }
838

839
  SMnode *pMnode = pMsg->info.node;
500,437✔
840
  (void)taosThreadRwlockRdlock(&pMnode->lock);
500,437✔
841
  if (pMnode->stopped) {
500,975✔
842
    (void)taosThreadRwlockUnlock(&pMnode->lock);
26✔
843
    code = TSDB_CODE_APP_IS_STOPPING;
26✔
844
    TAOS_RETURN(code);
26✔
845
  }
846

847
  terrno = 0;
500,949✔
848
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
500,576✔
849
  if (terrno != 0) {
501,043!
850
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
851
    code = terrno;
×
852
    TAOS_RETURN(code);
×
853
  }
854

855
  if (state.state != TAOS_SYNC_STATE_LEADER) {
501,034✔
856
    (void)taosThreadRwlockUnlock(&pMnode->lock);
14,301✔
857
    code = TSDB_CODE_SYN_NOT_LEADER;
14,301✔
858
    goto _OVER;
14,301✔
859
  }
860

861
  if (!state.restored || !pMnode->restored) {
486,733✔
862
    (void)taosThreadRwlockUnlock(&pMnode->lock);
471✔
863
    code = TSDB_CODE_SYN_RESTORING;
463✔
864
    goto _OVER;
463✔
865
  }
866

867
#if 1
868
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
486,262✔
869
#else
870
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
871
  mTrace("mnode rpc is acquired, ref:%d", ref);
872
#endif
873

874
  (void)taosThreadRwlockUnlock(&pMnode->lock);
486,273✔
875
  TAOS_RETURN(code);
486,263✔
876

877
_OVER:
14,764✔
878
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
14,764!
879
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
14,764!
880
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
14,764!
881
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
14,764!
882
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
14,764!
883
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
14,764!
884
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
14,764!
885
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER) {
14,764!
886
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
887
           pMnode->stopped, state.restored, syncStr(state.state));
888
    TAOS_RETURN(code);
×
889
  }
890

891
  const STraceId *trace = &pMsg->info.traceId;
14,764✔
892
  SEpSet          epSet = {0};
14,764✔
893
  mndGetMnodeEpSet(pMnode, &epSet);
14,764✔
894

895
  mGDebug(
14,764!
896
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
897
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
898
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
899
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
900

901
  if (epSet.numOfEps <= 0) return -1;
14,764!
902

903
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
57,341✔
904
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
42,577✔
905
  }
906

907
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
14,764✔
908
  pMsg->info.rsp = rpcMallocCont(contLen);
14,763✔
909
  if (pMsg->info.rsp != NULL) {
14,762!
910
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
14,762!
911
      mError("failed to serialize ep set");
×
912
    }
913
    pMsg->info.hasEpSet = 1;
14,760✔
914
    pMsg->info.rspLen = contLen;
14,760✔
915
  }
916

917
  TAOS_RETURN(code);
14,760✔
918
}
919

920
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
649,106✔
921
  SMnode         *pMnode = pMsg->info.node;
649,106✔
922
  const STraceId *trace = &pMsg->info.traceId;
649,106✔
923
  int32_t         code = TSDB_CODE_SUCCESS;
649,106✔
924

925
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
649,106✔
926
  MndMsgFpExt fpExt = NULL;
649,106✔
927
  if (fp == NULL) {
649,106✔
928
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
69,826✔
929
    if (fpExt == NULL) {
69,826!
930
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
931
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
932
      TAOS_RETURN(code);
×
933
    }
934
  }
935

936
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
649,106✔
937

938
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
635,283!
939
  if (fp)
635,283✔
940
    code = (*fp)(pMsg);
565,457✔
941
  else
942
    code = (*fpExt)(pMsg, pQueueInfo);
69,826✔
943
  mndReleaseRpc(pMnode);
635,319✔
944

945
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
635,339✔
946
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
97,189!
947
  } else if (code == 0) {
538,150✔
948
    mGTrace("msg:%p, successfully processed", pMsg);
530,613!
949
  } else {
950
    // TODO removve this wrong set code
951
    if (code == -1) {
7,537✔
952
      code = terrno;
3✔
953
    }
954
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
7,537!
955
            TMSG_INFO(pMsg->msgType));
956
  }
957

958
  TAOS_RETURN(code);
635,339✔
959
}
960

961
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
328,320✔
962
  tmsg_t type = TMSG_INDEX(msgType);
328,320✔
963
  if (type < TDMT_MAX) {
328,320!
964
    pMnode->msgFp[type] = fp;
328,320✔
965
  }
966
}
328,320✔
967

968
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
15,360✔
969
  tmsg_t type = TMSG_INDEX(msgType);
15,360✔
970
  if (type < TDMT_MAX) {
15,360!
971
    pMnode->msgFpExt[type] = fp;
15,360✔
972
  }
973
}
15,360✔
974

975
// Note: uid 0 is reserved
976
int64_t mndGenerateUid(const char *name, int32_t len) {
31,693✔
977
  int32_t hashval = MurmurHash3_32(name, len);
31,693✔
978
  do {
×
979
    int64_t us = taosGetTimestampUs();
31,693✔
980
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
31,693✔
981
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
31,693✔
982
    if (uuid) {
31,693!
983
      return llabs(uuid);
31,693✔
984
    }
985
  } while (true);
986
}
987

988
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
1✔
989
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
990
  int32_t code = mndAcquireRpc(pMnode);
1✔
991
  if (code < 0) {
1!
992
    TAOS_RETURN(code);
×
993
  } else if (code == 1) {
1!
994
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
995
  }
996

997
  SSdb   *pSdb = pMnode->pSdb;
1✔
998
  int64_t ms = taosGetTimestampMs();
1✔
999

1000
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
1✔
1001
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
1✔
1002
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
1✔
1003
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
1✔
1004
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
1!
1005
      pStbInfo->stbs == NULL) {
1!
1006
    mndReleaseRpc(pMnode);
×
1007
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1008
    if (terrno != 0) code = terrno;
×
1009
    TAOS_RETURN(code);
×
1010
  }
1011

1012
  // cluster info
1013
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
1✔
1014
  pClusterInfo->monitor_interval = tsMonitorInterval;
1✔
1015
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
1✔
1016
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
1✔
1017
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
1✔
1018
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
1✔
1019
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
1✔
1020

1021
  void *pIter = NULL;
1✔
1022
  while (1) {
1✔
1023
    SDnodeObj *pObj = NULL;
2✔
1024
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
2✔
1025
    if (pIter == NULL) break;
2✔
1026

1027
    SMonDnodeDesc desc = {0};
1✔
1028
    desc.dnode_id = pObj->id;
1✔
1029
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
1✔
1030
    if (mndIsDnodeOnline(pObj, ms)) {
1!
1031
      tstrncpy(desc.status, "ready", sizeof(desc.status));
1✔
1032
    } else {
1033
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1034
    }
1035
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
2!
1036
      mError("failed put dnode into array, but continue at this monitor report")
×
1037
    }
1038
    sdbRelease(pSdb, pObj);
1✔
1039
  }
1040

1041
  pIter = NULL;
1✔
1042
  while (1) {
1✔
1043
    SMnodeObj *pObj = NULL;
2✔
1044
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
2✔
1045
    if (pIter == NULL) break;
2✔
1046

1047
    SMonMnodeDesc desc = {0};
1✔
1048
    desc.mnode_id = pObj->id;
1✔
1049
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
1✔
1050

1051
    if (pObj->id == pMnode->selfDnodeId) {
1!
1052
      pClusterInfo->first_ep_dnode_id = pObj->id;
1✔
1053
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
1✔
1054
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1055
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
1✔
1056
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1057
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
1✔
1058
      desc.syncState = TAOS_SYNC_STATE_LEADER;
1✔
1059
    } else {
1060
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1061
      desc.syncState = pObj->syncState;
×
1062
    }
1063
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
2!
1064
      mError("failed to put mnode into array, but continue at this monitor report");
×
1065
    }
1066
    sdbRelease(pSdb, pObj);
1✔
1067
  }
1068

1069
  // vgroup info
1070
  pIter = NULL;
1✔
1071
  while (1) {
6✔
1072
    SVgObj *pVgroup = NULL;
7✔
1073
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
7✔
1074
    if (pIter == NULL) break;
7✔
1075

1076
    if (pVgroup->mountVgId) {
6!
1077
      sdbRelease(pSdb, pVgroup);
×
1078
      continue;
×
1079
    }
1080

1081
    pClusterInfo->vgroups_total++;
6✔
1082
    pClusterInfo->tbs_total += pVgroup->numOfTables;
6✔
1083

1084
    SMonVgroupDesc desc = {0};
6✔
1085
    desc.vgroup_id = pVgroup->vgId;
6✔
1086

1087
    SName name = {0};
6✔
1088
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
6✔
1089
    if (code < 0) {
6!
1090
      mError("failed to get db name since %s", tstrerror(code));
×
1091
      sdbRelease(pSdb, pVgroup);
×
1092
      TAOS_RETURN(code);
×
1093
    }
1094
    (void)tNameGetDbName(&name, desc.database_name);
6✔
1095

1096
    desc.tables_num = pVgroup->numOfTables;
6✔
1097
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
6✔
1098
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
6✔
1099
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
12✔
1100
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
6✔
1101
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
6✔
1102
      pVnDesc->dnode_id = pVgid->dnodeId;
6✔
1103
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
6✔
1104
      pVnDesc->syncState = pVgid->syncState;
6✔
1105
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
6!
1106
        tstrncpy(desc.status, "ready", sizeof(desc.status));
6✔
1107
        pClusterInfo->vgroups_alive++;
6✔
1108
      }
1109
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
6!
1110
        pClusterInfo->vnodes_alive++;
6✔
1111
      }
1112
      pClusterInfo->vnodes_total++;
6✔
1113
    }
1114

1115
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
12!
1116
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1117
    }
1118
    sdbRelease(pSdb, pVgroup);
6✔
1119
  }
1120

1121
  // stb info
1122
  pIter = NULL;
1✔
1123
  while (1) {
1✔
1124
    SStbObj *pStb = NULL;
2✔
1125
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
2✔
1126
    if (pIter == NULL) break;
2✔
1127

1128
    SMonStbDesc desc = {0};
1✔
1129

1130
    SName name1 = {0};
1✔
1131
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1✔
1132
    if (code < 0) {
1!
1133
      mError("failed to get db name since %s", tstrerror(code));
×
1134
      sdbRelease(pSdb, pStb);
×
1135
      TAOS_RETURN(code);
×
1136
    }
1137
    (void)tNameGetDbName(&name1, desc.database_name);
1✔
1138

1139
    SName name2 = {0};
1✔
1140
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1✔
1141
    if (code < 0) {
1!
1142
      mError("failed to get table name since %s", tstrerror(code));
×
1143
      sdbRelease(pSdb, pStb);
×
1144
      TAOS_RETURN(code);
×
1145
    }
1146
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
1✔
1147

1148
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
2!
1149
      mError("failed to put stb into array, but continue at this monitor report");
×
1150
    }
1151
    sdbRelease(pSdb, pStb);
1✔
1152
  }
1153

1154
  // grant info
1155
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
1✔
1156
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
1✔
1157
  if (pMnode->grant.expireTimeMS == 0) {
1!
1158
    pGrantInfo->expire_time = 0;
×
1159
    pGrantInfo->timeseries_total = 0;
×
1160
  }
1161

1162
  mndReleaseRpc(pMnode);
1✔
1163
  TAOS_RETURN(code);
1✔
1164
}
1165

1166
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
63,482✔
1167
  mTrace("mnode get load");
63,482✔
1168
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
63,482✔
1169
  pLoad->syncState = state.state;
63,482✔
1170
  pLoad->syncRestore = state.restored;
63,482✔
1171
  pLoad->syncTerm = state.term;
63,482✔
1172
  pLoad->roleTimeMs = state.roleTimeMs;
63,482✔
1173
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
63,482✔
1174
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1175
  return 0;
63,482✔
1176
}
1177

1178
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
18,574✔
1179
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
18,574✔
1180
  return state.roleTimeMs;
18,574✔
1181
}
1182

1183
void mndSetRestored(SMnode *pMnode, bool restored) {
1,920✔
1184
  if (restored) {
1,920!
1185
    (void)taosThreadRwlockWrlock(&pMnode->lock);
1,920✔
1186
    pMnode->restored = true;
1,920✔
1187
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,920✔
1188
    mInfo("mnode set restored:%d", restored);
1,920!
1189
  } else {
1190
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1191
    pMnode->restored = false;
×
1192
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1193
    mInfo("mnode set restored:%d", restored);
×
1194
    while (1) {
1195
      if (pMnode->rpcRef <= 0) break;
×
1196
      taosMsleep(3);
×
1197
    }
1198
  }
1199
}
1,920✔
1200

1201
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1202

1203
void mndSetStop(SMnode *pMnode) {
1,920✔
1204
  (void)taosThreadRwlockWrlock(&pMnode->lock);
1,920✔
1205
  pMnode->stopped = true;
1,920✔
1206
  (void)taosThreadRwlockUnlock(&pMnode->lock);
1,920✔
1207
  mInfo("mnode set stopped");
1,920!
1208
}
1,920✔
1209

1210
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
646,469✔
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