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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

68.18
/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 "mndBnode.h"
21
#include "mndCluster.h"
22
#include "mndCompact.h"
23
#include "mndCompactDetail.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 "mndRetention.h"
40
#include "mndRetentionDetail.h"
41
#include "mndRsma.h"
42
#include "mndScan.h"
43
#include "mndScanDetail.h"
44
#include "mndShow.h"
45
#include "mndSma.h"
46
#include "mndSnode.h"
47
#include "mndSsMigrate.h"
48
#include "mndStb.h"
49
#include "mndStream.h"
50
#include "mndSubscribe.h"
51
#include "mndSync.h"
52
#include "mndTelem.h"
53
#include "mndTopic.h"
54
#include "mndTrans.h"
55
#include "mndUser.h"
56
#include "mndVgroup.h"
57
#include "mndView.h"
58

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

78
static inline void mndReleaseRpc(SMnode *pMnode) {
233,638,411✔
79
  (void)taosThreadRwlockRdlock(&pMnode->lock);
233,638,411✔
80
#if 1
81
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
233,644,065✔
82
#else
83
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
84
  mTrace("mnode rpc is released, ref:%d", ref);
85
#endif
86
  (void)taosThreadRwlockUnlock(&pMnode->lock);
233,642,878✔
87
}
233,642,822✔
88

89
static void *mndBuildTimerMsg(int32_t *pContLen) {
50,120,713✔
90
  terrno = 0;
50,120,713✔
91
  SMTimerReq timerReq = {0};
50,121,273✔
92

93
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
50,120,763✔
94
  if (contLen <= 0) return NULL;
50,119,893!
95
  void *pReq = rpcMallocCont(contLen);
50,119,893✔
96
  if (pReq == NULL) return NULL;
50,118,552!
97

98
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
50,118,552!
99
    mError("failed to serialize timer msg since %s", terrstr());
×
100
  }
101
  *pContLen = contLen;
50,119,647✔
102
  return pReq;
50,120,522✔
103
}
104

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

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

131
static void mndPullupScans(SMnode *pMnode) {
2,029,565✔
132
  mTrace("pullup scan timer msg");
2,029,565✔
133
  int32_t contLen = 0;
2,029,565✔
134
  void   *pReq = mndBuildTimerMsg(&contLen);
2,029,565✔
135
  if (pReq != NULL) {
2,029,565!
136
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_SCAN_TIMER, .pCont = pReq, .contLen = contLen};
2,029,565✔
137
    // TODO check return value
138
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,029,565!
139
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
140
    }
141
  }
142
}
2,029,565✔
143

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

155
static void mndPullupTrimDb(SMnode *pMnode) {
139✔
156
  mTrace("pullup trim");
139!
157
  int32_t contLen = 0;
139✔
158
  void   *pReq = mndBuildTimerMsg(&contLen);
139✔
159
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
139✔
160
  // TODO check return value
161
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
139!
162
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
163
  }
164
}
139✔
165

166
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,060,751✔
167
  mTrace("pullup trim query");
2,060,751✔
168
  int32_t contLen = 0;
2,060,751✔
169
  void   *pReq = mndBuildTimerMsg(&contLen);
2,060,751✔
170
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,060,751✔
171
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,060,751!
172
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
173
  }
174
}
2,060,751✔
175

176
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
177
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
178
    return;
×
179
  }
180

181
  mTrace("pullup ssmigrate db");
×
182
  int32_t contLen = 0;
×
183
  void   *pReq = mndBuildTimerMsg(&contLen);
×
184
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
185
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
186
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
187
  }
188
}
189

190
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
×
191
  mTrace("pullup update ssmigrate progress");
×
192
  int32_t contLen = 0;
×
193
  void   *pReq = mndBuildTimerMsg(&contLen);
×
194
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
195
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
196
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
197
  }
198
}
×
199

200
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
11,093,631✔
201
  mTrace("pullup arb hb");
11,093,631✔
202
  int32_t contLen = 0;
11,093,631✔
203
  void   *pReq = mndBuildTimerMsg(&contLen);
11,093,631✔
204
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,093,631✔
205
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,093,631✔
206
}
207

208
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
7,301,068✔
209
  mTrace("pullup arb sync");
7,301,068✔
210
  int32_t contLen = 0;
7,301,068✔
211
  void   *pReq = mndBuildTimerMsg(&contLen);
7,301,068✔
212
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
7,301,068✔
213
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
7,301,068✔
214
}
215

216
static void mndCalMqRebalance(SMnode *pMnode) {
11,083,605✔
217
  int32_t contLen = 0;
11,083,605✔
218
  void   *pReq = mndBuildTimerMsg(&contLen);
11,083,605✔
219
  if (pReq != NULL) {
11,083,605!
220
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
11,083,605✔
221
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,083,605!
222
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
223
    }
224
  }
225
}
11,083,605✔
226

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

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

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

274
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
137,671✔
275
  SSdb *pSdb = pMnode->pSdb;
137,671✔
276

277
  void *pIter = NULL;
137,671✔
278
  while (1) {
649,145✔
279
    SVgObj *pVgroup = NULL;
786,816✔
280
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
786,816✔
281
    if (pIter == NULL) break;
786,816✔
282

283
    bool stateChanged = false;
649,145✔
284
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,636,800✔
285
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,279,961✔
286
      if (pGid->dnodeId == dnodeId) {
1,279,961✔
287
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
292,306✔
288
          mInfo(
109,260!
289
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
290
              "restored:0 "
291
              "canRead:0",
292
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
293
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
109,260✔
294
          pGid->syncRestore = 0;
109,260✔
295
          pGid->syncCanRead = 0;
109,260✔
296
          pGid->startTimeMs = 0;
109,260✔
297
          stateChanged = true;
109,260✔
298
        }
299
        break;
292,306✔
300
      }
301
    }
302

303
    if (stateChanged) {
649,145✔
304
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
109,260✔
305
      if (pDb != NULL && pDb->stateTs != curMs) {
109,260!
306
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
61,347!
307
              curMs);
308
        pDb->stateTs = curMs;
61,347✔
309
      }
310
      mndReleaseDb(pMnode, pDb);
109,260✔
311
    }
312

313
    sdbRelease(pSdb, pVgroup);
649,145✔
314
  }
315
}
137,671✔
316

317
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,294,845✔
318
  mTrace("check dnode offline");
4,294,845✔
319
  if (mndAcquireRpc(pMnode) != 0) return;
4,294,845!
320

321
  SSdb   *pSdb = pMnode->pSdb;
4,294,845✔
322
  int64_t curMs = taosGetTimestampMs();
4,294,845✔
323

324
  void *pIter = NULL;
4,294,845✔
325
  while (1) {
7,436,261✔
326
    SDnodeObj *pDnode = NULL;
11,731,106✔
327
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
11,731,106✔
328
    if (pIter == NULL) break;
11,731,106✔
329

330
    bool online = mndIsDnodeOnline(pDnode, curMs);
7,436,261✔
331
    if (!online) {
7,436,261✔
332
      mInfo("dnode:%d, in offline state", pDnode->id);
137,671!
333
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
137,671✔
334
    }
335

336
    sdbRelease(pSdb, pDnode);
7,436,261✔
337
  }
338

339
  mndReleaseRpc(pMnode);
4,294,845✔
340
}
341

342
static bool mnodeIsNotLeader(SMnode *pMnode) {
264,520,992✔
343
  terrno = 0;
264,520,992✔
344
  (void)taosThreadRwlockRdlock(&pMnode->lock);
264,521,537✔
345
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
264,528,705✔
346
  if (terrno != 0) {
264,528,705!
347
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
348
    return true;
×
349
  }
350

351
  if (state.state != TAOS_SYNC_STATE_LEADER) {
264,528,525✔
352
    (void)taosThreadRwlockUnlock(&pMnode->lock);
15,755,642✔
353
    terrno = TSDB_CODE_SYN_NOT_LEADER;
15,755,642✔
354
    return true;
15,755,642✔
355
  }
356
  if (!state.restored || !pMnode->restored) {
248,772,883!
357
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,142✔
358
    terrno = TSDB_CODE_SYN_RESTORING;
5,084✔
359
    return true;
5,084✔
360
  }
361
  (void)taosThreadRwlockUnlock(&pMnode->lock);
248,766,790✔
362
  return false;
248,768,309✔
363
}
364

365
static int32_t minCronTime() {
×
366
  int32_t min = INT32_MAX;
×
367
  min = TMIN(min, tsTtlPushIntervalSec);
×
368
  min = TMIN(min, tsTrimVDbIntervalSec);
×
369
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
370
  min = TMIN(min, tsTransPullupInterval);
×
371
  min = TMIN(min, tsCompactPullupInterval);
×
372
  min = TMIN(min, tsMqRebalanceInterval);
×
373

374
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
375
  min = TMIN(min, telemInt);
×
376
  min = TMIN(min, tsGrantHBInterval);
×
377
  min = TMIN(min, tsUptimeInterval);
×
378

379
  return min <= 1 ? 2 : min;
×
380
}
381
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,410,020✔
382
  int32_t code = 0;
22,410,020✔
383
#ifndef TD_ASTRA
384
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,410,020✔
385
    mndPullupGrant(pMnode);
1,360,076✔
386
  }
387
  if (sec % tsTtlPushIntervalSec == 0) {
22,410,020✔
388
    mndPullupTtl(pMnode);
2,059,005✔
389
  }
390

391
  if (sec % tsTrimVDbIntervalSec == 0) {
22,410,020!
392
    mndPullupTrimDb(pMnode);
139✔
393
  }
394

395
  if (sec % tsQueryTrimIntervalSec == 0) {
22,410,020✔
396
    mndPullupQueryTrimDb(pMnode);
2,060,751✔
397
  }
398
#endif
399
#ifdef USE_SHARED_STORAGE
400
  if (tsSsEnabled) {
22,410,020!
401
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
402
      mndPullupUpdateSsMigrateProgress(pMnode);
×
403
    }
404
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
405
      mndPullupSsMigrateDb(pMnode);
×
406
    }
407
  }
408
#endif
409
  if (sec % tsTransPullupInterval == 0) {
22,410,020✔
410
    mndPullupTrans(pMnode);
11,086,078✔
411
  }
412

413
  if (sec % tsCompactPullupInterval == 0) {
22,410,020✔
414
    mndPullupCompacts(pMnode);
2,028,648✔
415
  }
416

417
  if (sec % tsScanPullupInterval == 0) {
22,410,020✔
418
    mndPullupScans(pMnode);
2,029,565✔
419
  }
420
#ifdef USE_TOPIC
421
  if (sec % tsMqRebalanceInterval == 0) {
22,410,020✔
422
    mndCalMqRebalance(pMnode);
11,083,605✔
423
  }
424
#endif
425
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,410,020!
426
    mndPullupTelem(pMnode);
346✔
427
  }
428
  if (sec % tsUptimeInterval == 0) {
22,410,020!
429
    mndIncreaseUpTime(pMnode);
18,361✔
430
  }
431
}
22,410,020✔
432

433
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
226,358,469✔
434
  int32_t code = 0;
226,358,469✔
435
#ifndef TD_ASTRA
436
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
226,358,469✔
437
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,093,631!
438
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
439
    }
440
  }
441

442
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
226,358,469✔
443
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,301,068!
444
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
445
    }
446
  }
447
#endif
448
}
226,358,469✔
449

450
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
226,358,469✔
451
  if (ms % (tsStatusTimeoutMs) == 0) {
226,358,469✔
452
    mndCheckDnodeOffline(pMnode);
4,294,845✔
453
  }
454
}
226,358,469✔
455

456
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,410,020✔
457
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,410,020✔
458
    mndSyncCheckTimeout(pMnode);
579,728✔
459
  }
460
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,410,020✔
461
    msmHealthCheck(pMnode);
7,302,695✔
462
  }
463
}
22,410,020✔
464

465
static void *mndThreadSecFp(void *param) {
549,533✔
466
  SMnode *pMnode = param;
549,533✔
467
  int64_t lastTime = 0;
549,533✔
468
  setThreadName("mnode-timer");
549,533✔
469

470
  while (1) {
240,736,458✔
471
    lastTime++;
241,285,991✔
472
    taosMsleep(100);
241,285,991✔
473

474
    if (mndGetStop(pMnode)) break;
241,285,991✔
475
    if (lastTime % 10 != 0) continue;
240,736,458✔
476

477
    if (mnodeIsNotLeader(pMnode)) {
23,817,142✔
478
      mTrace("timer not process since mnode is not leader");
1,407,122!
479
      continue;
1,407,122✔
480
    }
481

482
    int64_t sec = lastTime / 10;
22,410,020✔
483
    mndDoTimerCheckSync(pMnode, sec);
22,410,020✔
484

485
    mndDoTimerPullupTask(pMnode, sec);
22,410,020✔
486
  }
487

488
  return NULL;
549,533✔
489
}
490

491
static void *mndThreadMsFp(void *param) {
549,533✔
492
  SMnode *pMnode = param;
549,533✔
493
  int64_t lastTime = 0;
549,533✔
494
  setThreadName("mnode-arb-timer");
549,533✔
495

496
  while (1) {
497
    lastTime += 100;
241,261,606✔
498
    taosMsleep(100);
241,261,606✔
499

500
    if (mndGetStop(pMnode)) break;
241,261,606✔
501
    if (lastTime % 10 != 0) continue;
240,712,073!
502

503
    if (mnodeIsNotLeader(pMnode)) {
240,712,073✔
504
      mTrace("timer not process since mnode is not leader");
14,353,604!
505
      continue;
14,353,604✔
506
    }
507

508
    mndDoTimerCheckStatus(pMnode, lastTime);
226,358,469✔
509

510
    mndDoArbTimerPullupTask(pMnode, lastTime);
226,358,469✔
511
  }
512

513
  return NULL;
549,533✔
514
}
515

516
static int32_t mndInitTimer(SMnode *pMnode) {
549,533✔
517
  int32_t      code = 0;
549,533✔
518
  TdThreadAttr thAttr;
548,284✔
519
  (void)taosThreadAttrInit(&thAttr);
549,533✔
520
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
549,533✔
521
#ifdef TD_COMPACT_OS
522
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
523
#endif
524
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
549,533!
525
    mError("failed to create timer thread since %s", tstrerror(code));
×
526
    TAOS_RETURN(code);
×
527
  }
528

529
  (void)taosThreadAttrDestroy(&thAttr);
549,533✔
530
  tmsgReportStartup("mnode-timer", "initialized");
549,533✔
531

532
  TdThreadAttr arbAttr;
548,284✔
533
  (void)taosThreadAttrInit(&arbAttr);
549,533✔
534
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
549,533✔
535
#ifdef TD_COMPACT_OS
536
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
537
#endif
538
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
549,533!
539
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
540
    TAOS_RETURN(code);
×
541
  }
542

543
  (void)taosThreadAttrDestroy(&arbAttr);
549,533✔
544
  tmsgReportStartup("mnode-timer", "initialized");
549,533✔
545
  TAOS_RETURN(code);
549,533✔
546
}
547

548
static void mndCleanupTimer(SMnode *pMnode) {
549,533✔
549
  if (taosCheckPthreadValid(pMnode->thread)) {
549,533!
550
    (void)taosThreadJoin(pMnode->thread, NULL);
549,533✔
551
    taosThreadClear(&pMnode->thread);
549,533✔
552
  }
553
  if (taosCheckPthreadValid(pMnode->arbThread)) {
549,533!
554
    (void)taosThreadJoin(pMnode->arbThread, NULL);
549,533✔
555
    taosThreadClear(&pMnode->arbThread);
549,533✔
556
  }
557
}
549,533✔
558

559
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
549,757✔
560
  int32_t code = 0;
549,757✔
561
  pMnode->path = taosStrdup(path);
549,757!
562
  if (pMnode->path == NULL) {
549,757!
563
    code = terrno;
×
564
    TAOS_RETURN(code);
×
565
  }
566

567
  if (taosMkDir(pMnode->path) != 0) {
549,757!
568
    code = terrno;
×
569
    TAOS_RETURN(code);
×
570
  }
571

572
  TAOS_RETURN(code);
549,757✔
573
}
574

575
static int32_t mndInitWal(SMnode *pMnode) {
549,757✔
576
  int32_t code = 0;
549,757✔
577
  char    path[PATH_MAX + 20] = {0};
549,757✔
578
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
549,757!
579
  SWalCfg cfg = {.vgId = 1,
549,757✔
580
                 .fsyncPeriod = 0,
581
                 .rollPeriod = -1,
582
                 .segSize = -1,
583
                 .committed = -1,
584
                 .retentionPeriod = 0,
585
                 .retentionSize = 0,
586
                 .level = TAOS_WAL_FSYNC,
587
                 .encryptAlgorithm = 0,
588
                 .encryptKey = {0}};
589

590
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
591
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
549,757!
592
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
593
    if (tsEncryptKey[0] == '\0') {
×
594
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
595
      TAOS_RETURN(code);
×
596
    } else {
597
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
598
    }
599
  }
600
#endif
601

602
  pMnode->pWal = walOpen(path, &cfg);
549,757✔
603
  if (pMnode->pWal == NULL) {
549,757!
604
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
605
    if (terrno != 0) code = terrno;
×
606
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
607
    TAOS_RETURN(code);
×
608
  }
609

610
  TAOS_RETURN(code);
549,757✔
611
}
612

613
static void mndCloseWal(SMnode *pMnode) {
549,533✔
614
  if (pMnode->pWal != NULL) {
549,533!
615
    walClose(pMnode->pWal);
549,533✔
616
    pMnode->pWal = NULL;
549,533✔
617
  }
618
}
549,533✔
619

620
static int32_t mndInitSdb(SMnode *pMnode) {
549,757✔
621
  int32_t code = 0;
549,757✔
622
  SSdbOpt opt = {0};
549,757✔
623
  opt.path = pMnode->path;
549,757✔
624
  opt.pMnode = pMnode;
549,757✔
625
  opt.pWal = pMnode->pWal;
549,757✔
626

627
  pMnode->pSdb = sdbInit(&opt);
549,757✔
628
  if (pMnode->pSdb == NULL) {
549,757!
629
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
630
    if (terrno != 0) code = terrno;
×
631
    TAOS_RETURN(code);
×
632
  }
633

634
  TAOS_RETURN(code);
549,757✔
635
}
636

637
static int32_t mndOpenSdb(SMnode *pMnode) {
549,757✔
638
  int32_t code = 0;
549,757✔
639
  if (!pMnode->deploy) {
549,757✔
640
    code = sdbReadFile(pMnode->pSdb);
161,466✔
641
  }
642

643
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
549,757!
644

645
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
549,757✔
646
  return code;
549,757✔
647
}
648

649
static void mndCleanupSdb(SMnode *pMnode) {
549,533✔
650
  if (pMnode->pSdb) {
549,533!
651
    sdbCleanup(pMnode->pSdb);
549,533✔
652
    pMnode->pSdb = NULL;
549,533✔
653
  }
654
}
549,533✔
655

656
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
24,739,065✔
657
  SMnodeStep step = {0};
24,739,065✔
658
  step.name = name;
24,739,065✔
659
  step.initFp = initFp;
24,739,065✔
660
  step.cleanupFp = cleanupFp;
24,739,065✔
661
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
49,478,130!
662
    TAOS_RETURN(terrno);
×
663
  }
664

665
  TAOS_RETURN(0);
24,739,065✔
666
}
667

668
static int32_t mndInitSteps(SMnode *pMnode) {
549,757✔
669
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
549,757!
670
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
549,757!
671
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
549,757!
672
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
549,757!
673
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
549,757!
674
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
549,757!
675
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
549,757!
676
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
549,757!
677
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
549,757!
678
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
549,757!
679
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
549,757!
680
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
549,757!
681
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
549,757!
682
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
549,757!
683
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
549,757!
684
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
549,757!
685
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
549,757!
686
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
549,757!
687
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
549,757!
688
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
549,757!
689
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
549,757!
690
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
549,757!
691
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
549,757!
692
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
549,757!
693
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
549,757!
694
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
549,757!
695
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
549,757!
696
#ifdef USE_MOUNT
697
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
549,757!
698
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
549,757!
699
#endif
700
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
549,757!
701
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
549,757!
702
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
549,757!
703
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
549,757!
704
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
549,757!
705
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
549,757!
706
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
549,757!
707
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
549,757!
708
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
549,757!
709
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
549,757!
710
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
549,757!
711
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
549,757!
712
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
549,757!
713
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
549,757!
714
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
549,757!
715
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
549,757!
716
  return 0;
549,757✔
717
}
718

719
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
549,533✔
720
  if (pMnode->pSteps == NULL) return;
549,533!
721

722
  if (pos == -1) {
549,533!
723
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
549,533✔
724
  }
725

726
  for (int32_t s = pos; s >= 0; s--) {
25,278,518✔
727
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
24,728,985✔
728
    mInfo("%s will cleanup", pStep->name);
24,728,985!
729
    if (pStep->cleanupFp != NULL) {
24,728,985✔
730
      (*pStep->cleanupFp)(pMnode);
23,629,919✔
731
    }
732
  }
733

734
  taosArrayClear(pMnode->pSteps);
549,533✔
735
  taosArrayDestroy(pMnode->pSteps);
549,533✔
736
  pMnode->pSteps = NULL;
549,533✔
737
}
738

739
static int32_t mndExecSteps(SMnode *pMnode) {
549,757✔
740
  int32_t code = 0;
549,757✔
741
  int32_t size = taosArrayGetSize(pMnode->pSteps);
549,757✔
742
  for (int32_t pos = 0; pos < size; pos++) {
25,288,822✔
743
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
24,739,065✔
744
    if (pStep->initFp == NULL) continue;
24,739,065!
745

746
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
24,739,065!
747
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
748
      mndCleanupSteps(pMnode, pos);
×
749
      TAOS_RETURN(code);
×
750
    } else {
751
      mInfo("%s is initialized", pStep->name);
24,739,065!
752
      tmsgReportStartup(pStep->name, "initialized");
24,739,065✔
753
    }
754
  }
755

756
  pMnode->clusterId = mndGetClusterId(pMnode);
549,757✔
757
  TAOS_RETURN(0);
549,757✔
758
}
759

760
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
549,757✔
761
  pMnode->msgCb = pOption->msgCb;
549,757✔
762
  pMnode->selfDnodeId = pOption->dnodeId;
549,757✔
763
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
549,757✔
764
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
549,757✔
765
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
549,757✔
766
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
549,757✔
767
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
549,757!
768
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
549,757!
769
}
549,757✔
770

771
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
549,757✔
772
  terrno = 0;
549,757✔
773
  mInfo("start to open mnode in %s", path);
549,757!
774

775
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
549,757!
776
  if (pMnode == NULL) {
549,757!
777
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
778
    mError("failed to open mnode since %s", terrstr());
×
779
    return NULL;
×
780
  }
781
  (void)memset(pMnode, 0, sizeof(SMnode));
549,757!
782

783
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
549,757✔
784
  if (code != 0) {
549,757!
785
    taosMemoryFree(pMnode);
×
786
    mError("failed to open mnode lock since %s", tstrerror(code));
×
787
    return NULL;
×
788
  }
789

790
  char timestr[24] = "1970-01-01 00:00:00.00";
549,757✔
791
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
549,757✔
792
  if (code < 0) {
549,757!
793
    mError("failed to parse time since %s", tstrerror(code));
×
794
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
795
    taosMemoryFree(pMnode);
×
796
    return NULL;
×
797
  }
798
  mndSetOptions(pMnode, pOption);
549,757✔
799

800
  pMnode->deploy = pOption->deploy;
549,757!
801
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
549,757✔
802
  if (pMnode->pSteps == NULL) {
549,757!
803
    taosMemoryFree(pMnode);
×
804
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
805
    mError("failed to open mnode since %s", terrstr());
×
806
    return NULL;
×
807
  }
808

809
  code = mndCreateDir(pMnode, path);
549,757✔
810
  if (code != 0) {
549,757!
811
    code = terrno;
×
812
    mError("failed to open mnode since %s", tstrerror(code));
×
813
    mndClose(pMnode);
×
814
    terrno = code;
×
815
    return NULL;
×
816
  }
817

818
  code = mndInitSteps(pMnode);
549,757✔
819
  if (code != 0) {
549,757!
820
    code = terrno;
×
821
    mError("failed to open mnode since %s", tstrerror(code));
×
822
    mndClose(pMnode);
×
823
    terrno = code;
×
824
    return NULL;
×
825
  }
826

827
  code = mndExecSteps(pMnode);
549,757✔
828
  if (code != 0) {
549,757!
829
    code = terrno;
×
830
    mError("failed to open mnode since %s", tstrerror(code));
×
831
    mndClose(pMnode);
×
832
    terrno = code;
×
833
    return NULL;
×
834
  }
835

836
  mInfo("mnode open successfully");
549,757!
837
  return pMnode;
549,757✔
838
}
839

840
void mndPreClose(SMnode *pMnode) {
549,533✔
841
  if (pMnode != NULL) {
549,533!
842
    int32_t code = 0;
549,533✔
843
    // TODO check return value
844
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
549,533✔
845
    if (code < 0) {
549,533!
846
      mError("failed to transfer leader since %s", tstrerror(code));
×
847
    }
848
    syncPreStop(pMnode->syncMgmt.sync);
549,533✔
849
    code = sdbWriteFile(pMnode->pSdb, 0);
549,533✔
850
    if (code < 0) {
549,533!
851
      mError("failed to write sdb since %s", tstrerror(code));
×
852
    }
853
  }
854
}
549,533✔
855

856
void mndClose(SMnode *pMnode) {
549,533✔
857
  if (pMnode != NULL) {
549,533!
858
    mInfo("start to close mnode");
549,533!
859
    mndCleanupSteps(pMnode, -1);
549,533✔
860
    taosMemoryFreeClear(pMnode->path);
549,533!
861
    taosMemoryFreeClear(pMnode);
549,533!
862
    mInfo("mnode is closed");
549,533!
863
  }
864
}
549,533✔
865

866
int32_t mndStart(SMnode *pMnode) {
549,533✔
867
  mndSyncStart(pMnode);
549,533✔
868
  if (pMnode->deploy) {
549,533✔
869
    if (sdbDeploy(pMnode->pSdb) != 0) {
388,291!
870
      mError("failed to deploy sdb while start mnode");
×
871
      return -1;
×
872
    }
873
    mndSetRestored(pMnode, true);
388,291✔
874
  }
875
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
549,533✔
876

877
  return mndInitTimer(pMnode);
549,533✔
878
}
879

880
int32_t mndIsCatchUp(SMnode *pMnode) {
281,163✔
881
  int64_t rid = pMnode->syncMgmt.sync;
281,163✔
882
  return syncIsCatchUp(rid);
281,163✔
883
}
884

885
ESyncRole mndGetRole(SMnode *pMnode) {
281,163✔
886
  int64_t rid = pMnode->syncMgmt.sync;
281,163✔
887
  return syncGetRole(rid);
281,163✔
888
}
889

890
int64_t mndGetTerm(SMnode *pMnode) {
7,444,069✔
891
  int64_t rid = pMnode->syncMgmt.sync;
7,444,069✔
892
  return syncGetTerm(rid);
7,444,069✔
893
}
894

895
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,531,221✔
896

897
void mndStop(SMnode *pMnode) {
549,533✔
898
  mndSetStop(pMnode);
549,533✔
899
  mndSyncStop(pMnode);
549,533✔
900
  mndCleanupTimer(pMnode);
549,533✔
901
}
549,533✔
902

903
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
48,371,882✔
904
  SMnode    *pMnode = pMsg->info.node;
48,371,882✔
905
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
48,371,882✔
906

907
  const STraceId *trace = &pMsg->info.traceId;
48,371,771✔
908
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
48,371,771!
909

910
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
48,371,771✔
911
  if (code != 0) {
48,371,882✔
912
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
3,221!
913
            tstrerror(code), code);
914
  }
915

916
  return code;
48,371,882✔
917
}
918

919
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
233,271,069✔
920
  int32_t code = 0;
233,271,069✔
921
  if (!IsReq(pMsg)) TAOS_RETURN(code);
233,271,069✔
922
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
206,366,944✔
923
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
201,166,975✔
924
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
196,449,885✔
925
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
186,082,222✔
926
    TAOS_RETURN(code);
20,294,143✔
927
  }
928

929
  SMnode *pMnode = pMsg->info.node;
186,075,423✔
930
  (void)taosThreadRwlockRdlock(&pMnode->lock);
186,082,091✔
931
  if (pMnode->stopped) {
186,086,208!
932
    (void)taosThreadRwlockUnlock(&pMnode->lock);
8,706✔
933
    code = TSDB_CODE_APP_IS_STOPPING;
8,706✔
934
    TAOS_RETURN(code);
8,706✔
935
  }
936

937
  terrno = 0;
186,075,378✔
938
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
186,074,403✔
939
  if (terrno != 0) {
186,078,668!
940
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
941
    code = terrno;
×
942
    TAOS_RETURN(code);
×
943
  }
944

945
  if (state.state != TAOS_SYNC_STATE_LEADER) {
186,077,703✔
946
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,655,246✔
947
    code = TSDB_CODE_SYN_NOT_LEADER;
3,655,419✔
948
    goto _OVER;
3,655,419✔
949
  }
950

951
  if (!state.restored || !pMnode->restored) {
182,422,457!
952
    (void)taosThreadRwlockUnlock(&pMnode->lock);
269,520✔
953
    code = TSDB_CODE_SYN_RESTORING;
267,456✔
954
    goto _OVER;
267,456✔
955
  }
956

957
#if 1
958
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
182,153,753✔
959
#else
960
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
961
  mTrace("mnode rpc is acquired, ref:%d", ref);
962
#endif
963

964
  (void)taosThreadRwlockUnlock(&pMnode->lock);
182,154,905✔
965
  TAOS_RETURN(code);
182,151,669✔
966

967
_OVER:
3,922,875✔
968
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,922,875!
969
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,922,290!
970
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,921,900✔
971
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,921,690!
972
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,921,868✔
973
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,921,252!
974
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,921,900!
975
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,921,690!
976
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER) {
3,921,457✔
977
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
516!
978
           pMnode->stopped, state.restored, syncStr(state.state));
979
    TAOS_RETURN(code);
516✔
980
  }
981

982
  const STraceId *trace = &pMsg->info.traceId;
3,921,425✔
983
  SEpSet          epSet = {0};
3,921,457✔
984
  mndGetMnodeEpSet(pMnode, &epSet);
3,921,457✔
985

986
  mGDebug(
3,921,900!
987
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
988
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
989
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
990
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
991

992
  if (epSet.numOfEps <= 0) return -1;
3,921,900!
993

994
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
14,858,705✔
995
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
10,936,805✔
996
  }
997

998
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,921,900✔
999
  pMsg->info.rsp = rpcMallocCont(contLen);
3,920,598✔
1000
  if (pMsg->info.rsp != NULL) {
3,921,722!
1001
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,921,900!
1002
      mError("failed to serialize ep set");
×
1003
    }
1004
    pMsg->info.hasEpSet = 1;
3,921,540✔
1005
    pMsg->info.rspLen = contLen;
3,921,718✔
1006
  }
1007

1008
  TAOS_RETURN(code);
3,921,718✔
1009
}
1010

1011
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
233,277,471✔
1012
  SMnode         *pMnode = pMsg->info.node;
233,277,471✔
1013
  const STraceId *trace = &pMsg->info.traceId;
233,278,051✔
1014
  int32_t         code = TSDB_CODE_SUCCESS;
233,265,538✔
1015

1016
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
233,265,538✔
1017
  MndMsgFpExt fpExt = NULL;
233,278,851✔
1018
  if (fp == NULL) {
233,278,851✔
1019
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,288,710✔
1020
    if (fpExt == NULL) {
20,288,710!
1021
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1022
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1023
      TAOS_RETURN(code);
×
1024
    }
1025
  }
1026

1027
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
233,278,851✔
1028

1029
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
229,345,282!
1030
  if (fp)
229,345,716✔
1031
    code = (*fp)(pMsg);
209,057,006✔
1032
  else
1033
    code = (*fpExt)(pMsg, pQueueInfo);
20,288,710✔
1034
  mndReleaseRpc(pMnode);
229,345,526✔
1035

1036
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
229,345,796✔
1037
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
28,444,372!
1038
  } else if (code == 0) {
200,901,424✔
1039
    mGTrace("msg:%p, successfully processed", pMsg);
197,695,323!
1040
  } else {
1041
    // TODO removve this wrong set code
1042
    if (code == -1) {
3,206,101✔
1043
      code = terrno;
3,638✔
1044
    }
1045
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
3,206,101!
1046
            TMSG_INFO(pMsg->msgType));
1047
  }
1048

1049
  TAOS_RETURN(code);
229,345,796✔
1050
}
1051

1052
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
103,904,073✔
1053
  tmsg_t type = TMSG_INDEX(msgType);
103,904,073✔
1054
  if (type < TDMT_MAX) {
103,904,073!
1055
    pMnode->msgFp[type] = fp;
103,904,073✔
1056
  }
1057
}
103,904,073✔
1058

1059
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,398,056✔
1060
  tmsg_t type = TMSG_INDEX(msgType);
4,398,056✔
1061
  if (type < TDMT_MAX) {
4,398,056!
1062
    pMnode->msgFpExt[type] = fp;
4,398,056✔
1063
  }
1064
}
4,398,056✔
1065

1066
// Note: uid 0 is reserved
1067
int64_t mndGenerateUid(const char *name, int32_t len) {
9,442,432✔
1068
  int32_t hashval = MurmurHash3_32(name, len);
9,442,432✔
1069
  do {
×
1070
    int64_t us = taosGetTimestampUs();
9,442,432✔
1071
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,442,432✔
1072
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,442,432✔
1073
    if (uuid) {
9,442,773!
1074
      return llabs(uuid);
9,442,773✔
1075
    }
1076
  } while (true);
1077
}
1078

1079
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
173✔
1080
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1081
  int32_t code = mndAcquireRpc(pMnode);
173✔
1082
  if (code < 0) {
173!
1083
    TAOS_RETURN(code);
×
1084
  } else if (code == 1) {
173!
1085
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1086
  }
1087

1088
  SSdb   *pSdb = pMnode->pSdb;
173✔
1089
  int64_t ms = taosGetTimestampMs();
173✔
1090

1091
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
173✔
1092
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
173✔
1093
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
173✔
1094
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
173✔
1095
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
173!
1096
      pStbInfo->stbs == NULL) {
173!
1097
    mndReleaseRpc(pMnode);
×
1098
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1099
    if (terrno != 0) code = terrno;
×
1100
    TAOS_RETURN(code);
×
1101
  }
1102

1103
  // cluster info
1104
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
173!
1105
  pClusterInfo->monitor_interval = tsMonitorInterval;
173✔
1106
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
173✔
1107
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
173✔
1108
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
173✔
1109
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
173✔
1110
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
173✔
1111

1112
  void *pIter = NULL;
173✔
1113
  while (1) {
173✔
1114
    SDnodeObj *pObj = NULL;
346✔
1115
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
346✔
1116
    if (pIter == NULL) break;
346✔
1117

1118
    SMonDnodeDesc desc = {0};
173✔
1119
    desc.dnode_id = pObj->id;
173✔
1120
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
173!
1121
    if (mndIsDnodeOnline(pObj, ms)) {
173!
1122
      tstrncpy(desc.status, "ready", sizeof(desc.status));
173✔
1123
    } else {
1124
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1125
    }
1126
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
346!
1127
      mError("failed put dnode into array, but continue at this monitor report")
×
1128
    }
1129
    sdbRelease(pSdb, pObj);
173✔
1130
  }
1131

1132
  pIter = NULL;
173✔
1133
  while (1) {
173✔
1134
    SMnodeObj *pObj = NULL;
346✔
1135
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
346✔
1136
    if (pIter == NULL) break;
346✔
1137

1138
    SMonMnodeDesc desc = {0};
173✔
1139
    desc.mnode_id = pObj->id;
173✔
1140
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
173!
1141

1142
    if (pObj->id == pMnode->selfDnodeId) {
173!
1143
      pClusterInfo->first_ep_dnode_id = pObj->id;
173✔
1144
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
173!
1145
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1146
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
173✔
1147
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1148
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
173!
1149
      desc.syncState = TAOS_SYNC_STATE_LEADER;
173✔
1150
    } else {
1151
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1152
      desc.syncState = pObj->syncState;
×
1153
    }
1154
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
346!
1155
      mError("failed to put mnode into array, but continue at this monitor report");
×
1156
    }
1157
    sdbRelease(pSdb, pObj);
173✔
1158
  }
1159

1160
  // vgroup info
1161
  pIter = NULL;
173✔
1162
  while (1) {
1,038✔
1163
    SVgObj *pVgroup = NULL;
1,211✔
1164
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,211✔
1165
    if (pIter == NULL) break;
1,211✔
1166

1167
    if (pVgroup->mountVgId) {
1,038!
1168
      sdbRelease(pSdb, pVgroup);
×
1169
      continue;
×
1170
    }
1171

1172
    pClusterInfo->vgroups_total++;
1,038✔
1173
    pClusterInfo->tbs_total += pVgroup->numOfTables;
1,038✔
1174

1175
    SMonVgroupDesc desc = {0};
1,038✔
1176
    desc.vgroup_id = pVgroup->vgId;
1,038✔
1177

1178
    SName name = {0};
1,038✔
1179
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,038✔
1180
    if (code < 0) {
1,038!
1181
      mError("failed to get db name since %s", tstrerror(code));
×
1182
      sdbRelease(pSdb, pVgroup);
×
1183
      TAOS_RETURN(code);
×
1184
    }
1185
    (void)tNameGetDbName(&name, desc.database_name);
1,038✔
1186

1187
    desc.tables_num = pVgroup->numOfTables;
1,038✔
1188
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
1,038✔
1189
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
1,038✔
1190
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
2,076✔
1191
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
1,038✔
1192
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
1,038✔
1193
      pVnDesc->dnode_id = pVgid->dnodeId;
1,038✔
1194
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
1,038!
1195
      pVnDesc->syncState = pVgid->syncState;
1,038✔
1196
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
1,038!
1197
        tstrncpy(desc.status, "ready", sizeof(desc.status));
1,038✔
1198
        pClusterInfo->vgroups_alive++;
1,038✔
1199
      }
1200
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
1,038!
1201
        pClusterInfo->vnodes_alive++;
1,038✔
1202
      }
1203
      pClusterInfo->vnodes_total++;
1,038✔
1204
    }
1205

1206
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
2,076!
1207
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1208
    }
1209
    sdbRelease(pSdb, pVgroup);
1,038✔
1210
  }
1211

1212
  // stb info
1213
  pIter = NULL;
173✔
1214
  while (1) {
173✔
1215
    SStbObj *pStb = NULL;
346✔
1216
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
346✔
1217
    if (pIter == NULL) break;
346✔
1218

1219
    SMonStbDesc desc = {0};
173✔
1220

1221
    SName name1 = {0};
173✔
1222
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
173✔
1223
    if (code < 0) {
173!
1224
      mError("failed to get db name since %s", tstrerror(code));
×
1225
      sdbRelease(pSdb, pStb);
×
1226
      TAOS_RETURN(code);
×
1227
    }
1228
    (void)tNameGetDbName(&name1, desc.database_name);
173✔
1229

1230
    SName name2 = {0};
173✔
1231
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
173✔
1232
    if (code < 0) {
173!
1233
      mError("failed to get table name since %s", tstrerror(code));
×
1234
      sdbRelease(pSdb, pStb);
×
1235
      TAOS_RETURN(code);
×
1236
    }
1237
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
173!
1238

1239
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
346!
1240
      mError("failed to put stb into array, but continue at this monitor report");
×
1241
    }
1242
    sdbRelease(pSdb, pStb);
173✔
1243
  }
1244

1245
  // grant info
1246
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
173✔
1247
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
173✔
1248
  if (pMnode->grant.expireTimeMS == 0) {
173!
1249
    pGrantInfo->expire_time = 0;
×
1250
    pGrantInfo->timeseries_total = 0;
×
1251
  }
1252

1253
  mndReleaseRpc(pMnode);
173✔
1254
  TAOS_RETURN(code);
173✔
1255
}
1256

1257
int32_t mndResetTimer(SMnode *pMnode){
×
1258
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1259
}
1260

1261
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
23,722,530✔
1262
  mTrace("mnode get load");
23,722,530✔
1263
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
23,722,530✔
1264
  pLoad->syncState = state.state;
23,722,530✔
1265
  pLoad->syncRestore = state.restored;
23,722,530!
1266
  pLoad->syncTerm = state.term;
23,722,530✔
1267
  pLoad->roleTimeMs = state.roleTimeMs;
23,722,530✔
1268
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
23,722,530✔
1269
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1270
  return 0;
23,722,530✔
1271
}
1272

1273
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,301,068✔
1274
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,301,068✔
1275
  return state.roleTimeMs;
7,301,068✔
1276
}
1277

1278
void mndSetRestored(SMnode *pMnode, bool restored) {
549,533✔
1279
  if (restored) {
549,533!
1280
    (void)taosThreadRwlockWrlock(&pMnode->lock);
549,533✔
1281
    pMnode->restored = true;
549,533✔
1282
    (void)taosThreadRwlockUnlock(&pMnode->lock);
549,533✔
1283
    mInfo("mnode set restored:%d", restored);
549,533!
1284
  } else {
1285
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1286
    pMnode->restored = false;
×
1287
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1288
    mInfo("mnode set restored:%d", restored);
×
1289
    while (1) {
1290
      if (pMnode->rpcRef <= 0) break;
×
1291
      taosMsleep(3);
×
1292
    }
1293
  }
1294
}
549,533✔
1295

1296
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1297

1298
void mndSetStop(SMnode *pMnode) {
549,533✔
1299
  (void)taosThreadRwlockWrlock(&pMnode->lock);
549,533✔
1300
  pMnode->stopped = true;
549,533✔
1301
  (void)taosThreadRwlockUnlock(&pMnode->lock);
549,533✔
1302
  mInfo("mnode set stopped");
549,533!
1303
}
549,533✔
1304

1305
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
482,535,125!
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