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

taosdata / TDengine / #4836

31 Oct 2025 03:37AM UTC coverage: 58.728% (+0.2%) from 58.506%
#4836

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149727 of 324176 branches covered (46.19%)

Branch coverage included in aggregate %.

198923 of 269498 relevant lines covered (73.81%)

238054213.11 hits per line

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

66.65
/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) {
18,279,748✔
60
  int32_t code = 0;
18,279,748✔
61
  (void)taosThreadRwlockRdlock(&pMnode->lock);
18,279,748✔
62
  if (pMnode->stopped) {
18,279,748!
63
    code = TSDB_CODE_APP_IS_STOPPING;
×
64
  } else if (!mndIsLeader(pMnode)) {
18,279,748!
65
    code = 1;
×
66
  } else {
67
#if 1
68
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
18,279,748✔
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);
18,279,748✔
75
  TAOS_RETURN(code);
18,279,748✔
76
}
77

78
static inline void mndReleaseRpc(SMnode *pMnode) {
827,442,545✔
79
  (void)taosThreadRwlockRdlock(&pMnode->lock);
827,442,545✔
80
#if 1
81
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
827,453,632✔
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);
827,446,133✔
87
}
827,455,001✔
88

89
static void *mndBuildTimerMsg(int32_t *pContLen) {
211,288,579✔
90
  terrno = 0;
211,288,579✔
91
  SMTimerReq timerReq = {0};
211,289,902✔
92

93
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
211,289,902✔
94
  if (contLen <= 0) return NULL;
211,278,323!
95
  void *pReq = rpcMallocCont(contLen);
211,278,323✔
96
  if (pReq == NULL) return NULL;
211,277,243!
97

98
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
211,277,243!
99
    mError("failed to serialize timer msg since %s", terrstr());
×
100
  }
101
  *pContLen = contLen;
211,280,390✔
102
  return pReq;
211,279,322✔
103
}
104

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

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

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

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

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

166
static void mndPullupQueryTrimDb(SMnode *pMnode) {
8,859,530✔
167
  mTrace("pullup trim query");
8,859,530✔
168
  int32_t contLen = 0;
8,859,530✔
169
  void   *pReq = mndBuildTimerMsg(&contLen);
8,859,530✔
170
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
8,859,530✔
171
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
8,859,530!
172
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
173
  }
174
}
8,859,530✔
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) {
46,734,904✔
201
  mTrace("pullup arb hb");
46,734,904✔
202
  int32_t contLen = 0;
46,734,904✔
203
  void   *pReq = mndBuildTimerMsg(&contLen);
46,734,904✔
204
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
46,734,904✔
205
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
46,734,904✔
206
}
207

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

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

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

240
static void mndPullupGrant(SMnode *pMnode) {
4,553,072✔
241
  mTrace("pullup grant msg");
4,553,072✔
242
  int32_t contLen = 0;
4,553,072✔
243
  void   *pReq = mndBuildTimerMsg(&contLen);
4,553,072✔
244
  if (pReq != NULL) {
4,553,072!
245
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
4,553,072✔
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) {
4,553,072!
252
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
253
    }
254
  }
255
}
4,553,072✔
256

257
static void mndIncreaseUpTime(SMnode *pMnode) {
89,312✔
258
  mTrace("increate uptime");
89,312!
259
  int32_t contLen = 0;
89,312✔
260
  void   *pReq = mndBuildTimerMsg(&contLen);
89,312✔
261
  if (pReq != NULL) {
89,312!
262
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
89,312✔
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) {
89,312!
269
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
270
    }
271
  }
272
}
89,312✔
273

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

277
  void *pIter = NULL;
459,383✔
278
  while (1) {
2,411,743✔
279
    SVgObj *pVgroup = NULL;
2,871,126✔
280
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
2,871,126✔
281
    if (pIter == NULL) break;
2,871,126✔
282

283
    bool stateChanged = false;
2,411,743✔
284
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
6,112,055✔
285
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
4,678,866✔
286
      if (pGid->dnodeId == dnodeId) {
4,678,866✔
287
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
978,554✔
288
          mInfo(
463,884!
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;
463,884✔
294
          pGid->syncRestore = 0;
463,884✔
295
          pGid->syncCanRead = 0;
463,884✔
296
          pGid->startTimeMs = 0;
463,884✔
297
          stateChanged = true;
463,884✔
298
        }
299
        break;
978,554✔
300
      }
301
    }
302

303
    if (stateChanged) {
2,411,743✔
304
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
463,884✔
305
      if (pDb != NULL && pDb->stateTs != curMs) {
463,884!
306
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
290,934!
307
              curMs);
308
        pDb->stateTs = curMs;
290,934✔
309
      }
310
      mndReleaseDb(pMnode, pDb);
463,884✔
311
    }
312

313
    sdbRelease(pSdb, pVgroup);
2,411,743✔
314
  }
315
}
459,383✔
316

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

321
  SSdb   *pSdb = pMnode->pSdb;
18,278,788✔
322
  int64_t curMs = taosGetTimestampMs();
18,278,788✔
323

324
  void *pIter = NULL;
18,278,788✔
325
  while (1) {
29,586,751✔
326
    SDnodeObj *pDnode = NULL;
47,865,539✔
327
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
47,865,539✔
328
    if (pIter == NULL) break;
47,865,539✔
329

330
    bool online = mndIsDnodeOnline(pDnode, curMs);
29,586,751✔
331
    if (!online) {
29,586,751✔
332
      mInfo("dnode:%d, in offline state", pDnode->id);
459,383!
333
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
459,383✔
334
    }
335

336
    sdbRelease(pSdb, pDnode);
29,586,751✔
337
  }
338

339
  mndReleaseRpc(pMnode);
18,278,788✔
340
}
341

342
static bool mnodeIsNotLeader(SMnode *pMnode) {
1,117,188,399✔
343
  terrno = 0;
1,117,188,399✔
344
  (void)taosThreadRwlockRdlock(&pMnode->lock);
1,117,187,727✔
345
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
1,117,207,042✔
346
  if (terrno != 0) {
1,117,208,375!
347
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
348
    return true;
×
349
  }
350

351
  if (state.state != TAOS_SYNC_STATE_LEADER) {
1,117,202,658✔
352
    (void)taosThreadRwlockUnlock(&pMnode->lock);
74,309,897✔
353
    terrno = TSDB_CODE_SYN_NOT_LEADER;
74,308,909✔
354
    return true;
74,308,909✔
355
  }
356
  if (!state.restored || !pMnode->restored) {
1,042,892,761!
357
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13,124✔
358
    terrno = TSDB_CODE_SYN_RESTORING;
11,185✔
359
    return true;
11,185✔
360
  }
361
  (void)taosThreadRwlockUnlock(&pMnode->lock);
1,042,881,759✔
362
  return false;
1,042,883,014✔
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) {
94,209,913✔
382
  int32_t code = 0;
94,209,913✔
383
#ifndef TD_ASTRA
384
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
94,209,913✔
385
    mndPullupGrant(pMnode);
4,553,072✔
386
  }
387
  if (sec % tsTtlPushIntervalSec == 0) {
94,209,913✔
388
    mndPullupTtl(pMnode);
9,172,616✔
389
  }
390

391
  if (sec % tsTrimVDbIntervalSec == 0) {
94,209,913!
392
    mndPullupTrimDb(pMnode);
×
393
  }
394

395
  if (sec % tsQueryTrimIntervalSec == 0) {
94,209,913✔
396
    mndPullupQueryTrimDb(pMnode);
8,859,530✔
397
  }
398
#endif
399
#ifdef USE_SHARED_STORAGE
400
  if (tsSsEnabled) {
94,209,913!
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) {
94,209,913✔
410
    mndPullupTrans(pMnode);
46,728,606✔
411
  }
412

413
  if (sec % tsCompactPullupInterval == 0) {
94,209,913✔
414
    mndPullupCompacts(pMnode);
8,752,267✔
415
  }
416

417
  if (sec % tsScanPullupInterval == 0) {
94,209,913✔
418
    mndPullupScans(pMnode);
8,753,468✔
419
  }
420
#ifdef USE_TOPIC
421
  if (sec % tsMqRebalanceInterval == 0) {
94,209,913✔
422
    mndCalMqRebalance(pMnode);
46,724,712✔
423
  }
424
#endif
425
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
94,209,913!
426
    mndPullupTelem(pMnode);
1,920✔
427
  }
428
  if (sec % tsUptimeInterval == 0) {
94,209,913!
429
    mndIncreaseUpTime(pMnode);
89,312✔
430
  }
431
}
94,209,913✔
432

433
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
948,677,673✔
434
  int32_t code = 0;
948,677,673✔
435
#ifndef TD_ASTRA
436
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
948,677,673✔
437
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
46,734,904!
438
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
439
    }
440
  }
441

442
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
948,677,673✔
443
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
30,919,495!
444
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
445
    }
446
  }
447
#endif
448
}
948,677,673✔
449

450
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
948,677,673✔
451
  if (ms % (tsStatusTimeoutMs) == 0) {
948,677,673✔
452
    mndCheckDnodeOffline(pMnode);
18,278,788✔
453
  }
454
}
948,677,673✔
455

456
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
94,209,913✔
457
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
94,209,913✔
458
    mndSyncCheckTimeout(pMnode);
2,562,490✔
459
  }
460
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
94,209,913✔
461
    msmHealthCheck(pMnode);
30,922,043✔
462
  }
463
}
94,209,913✔
464

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

470
  while (1) {
1,016,545,012✔
471
    lastTime++;
1,018,291,396✔
472
    taosMsleep(100);
1,018,291,396✔
473

474
    if (mndGetStop(pMnode)) break;
1,018,291,396✔
475
    if (lastTime % 10 != 0) continue;
1,016,545,012✔
476

477
    if (mnodeIsNotLeader(pMnode)) {
100,855,463✔
478
      mTrace("timer not process since mnode is not leader");
6,645,550!
479
      continue;
6,645,550✔
480
    }
481

482
    int64_t sec = lastTime / 10;
94,209,913✔
483
    mndDoTimerCheckSync(pMnode, sec);
94,209,913✔
484

485
    mndDoTimerPullupTask(pMnode, sec);
94,209,913✔
486
  }
487

488
  return NULL;
1,746,384✔
489
}
490

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

496
  while (1) {
497
    lastTime += 100;
1,018,099,589✔
498
    taosMsleep(100);
1,018,099,589✔
499

500
    if (mndGetStop(pMnode)) break;
1,018,099,589✔
501
    if (lastTime % 10 != 0) continue;
1,016,353,205!
502

503
    if (mnodeIsNotLeader(pMnode)) {
1,016,353,205✔
504
      mTrace("timer not process since mnode is not leader");
67,675,532!
505
      continue;
67,675,532✔
506
    }
507

508
    mndDoTimerCheckStatus(pMnode, lastTime);
948,677,673✔
509

510
    mndDoArbTimerPullupTask(pMnode, lastTime);
948,677,673✔
511
  }
512

513
  return NULL;
1,746,384✔
514
}
515

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

529
  (void)taosThreadAttrDestroy(&thAttr);
1,746,384✔
530
  tmsgReportStartup("mnode-timer", "initialized");
1,746,384✔
531

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

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

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

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

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

572
  TAOS_RETURN(code);
1,746,676✔
573
}
574

575
static int32_t mndInitWal(SMnode *pMnode) {
1,746,676✔
576
  int32_t code = 0;
1,746,676✔
577
  char    path[PATH_MAX + 20] = {0};
1,746,676✔
578
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
1,746,676!
579
  SWalCfg cfg = {.vgId = 1,
1,746,676✔
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) {
1,746,676!
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);
1,746,676✔
603
  if (pMnode->pWal == NULL) {
1,746,676!
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);
1,746,676✔
611
}
612

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

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

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

634
  TAOS_RETURN(code);
1,746,676✔
635
}
636

637
static int32_t mndOpenSdb(SMnode *pMnode) {
1,746,676✔
638
  int32_t code = 0;
1,746,676✔
639
  if (!pMnode->deploy) {
1,746,676✔
640
    code = sdbReadFile(pMnode->pSdb);
532,901✔
641
  }
642

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

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

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

656
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
78,600,420✔
657
  SMnodeStep step = {0};
78,600,420✔
658
  step.name = name;
78,600,420✔
659
  step.initFp = initFp;
78,600,420✔
660
  step.cleanupFp = cleanupFp;
78,600,420✔
661
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
157,200,840!
662
    TAOS_RETURN(terrno);
×
663
  }
664

665
  TAOS_RETURN(0);
78,600,420✔
666
}
667

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

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

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

726
  for (int32_t s = pos; s >= 0; s--) {
80,333,664✔
727
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
78,587,280✔
728
    mInfo("%s will cleanup", pStep->name);
78,587,280!
729
    if (pStep->cleanupFp != NULL) {
78,587,280✔
730
      (*pStep->cleanupFp)(pMnode);
75,094,512✔
731
    }
732
  }
733

734
  taosArrayClear(pMnode->pSteps);
1,746,384✔
735
  taosArrayDestroy(pMnode->pSteps);
1,746,384✔
736
  pMnode->pSteps = NULL;
1,746,384✔
737
}
738

739
static int32_t mndExecSteps(SMnode *pMnode) {
1,746,676✔
740
  int32_t code = 0;
1,746,676✔
741
  int32_t size = taosArrayGetSize(pMnode->pSteps);
1,746,676✔
742
  for (int32_t pos = 0; pos < size; pos++) {
80,347,096✔
743
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
78,600,420✔
744
    if (pStep->initFp == NULL) continue;
78,600,420!
745

746
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
78,600,420!
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);
78,600,420!
752
      tmsgReportStartup(pStep->name, "initialized");
78,600,420✔
753
    }
754
  }
755

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

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

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

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

783
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
1,746,676✔
784
  if (code != 0) {
1,746,676!
785
    taosMemoryFree(pMnode);
×
786
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
787
    terrno = code;
×
788
    return NULL;
×
789
  }
790

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

802
  pMnode->deploy = pOption->deploy;
1,746,676!
803
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
1,746,676✔
804
  if (pMnode->pSteps == NULL) {
1,746,676!
805
    taosMemoryFree(pMnode);
×
806
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
807
    mError("failed to open mnode in step 4, since %s", terrstr());
×
808
    return NULL;
×
809
  }
810

811
  code = mndCreateDir(pMnode, path);
1,746,676✔
812
  if (code != 0) {
1,746,676!
813
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
814
    mndClose(pMnode);
×
815
    terrno = code;
×
816
    return NULL;
×
817
  }
818

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

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

835
  mInfo("mnode open successfully");
1,746,676!
836
  return pMnode;
1,746,676✔
837
}
838

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

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

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

876
  return mndInitTimer(pMnode);
1,746,384✔
877
}
878

879
int32_t mndIsCatchUp(SMnode *pMnode) {
1,132,143✔
880
  int64_t rid = pMnode->syncMgmt.sync;
1,132,143✔
881
  return syncIsCatchUp(rid);
1,132,143✔
882
}
883

884
ESyncRole mndGetRole(SMnode *pMnode) {
1,132,143✔
885
  int64_t rid = pMnode->syncMgmt.sync;
1,132,143✔
886
  return syncGetRole(rid);
1,132,143✔
887
}
888

889
int64_t mndGetTerm(SMnode *pMnode) {
31,151,675✔
890
  int64_t rid = pMnode->syncMgmt.sync;
31,151,675✔
891
  return syncGetTerm(rid);
31,151,675✔
892
}
893

894
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
77,894,596✔
895

896
void mndStop(SMnode *pMnode) {
1,746,384✔
897
  mndSetStop(pMnode);
1,746,384✔
898
  mndSyncStop(pMnode);
1,746,384✔
899
  mndCleanupTimer(pMnode);
1,746,384✔
900
}
1,746,384✔
901

902
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
187,915,350✔
903
  SMnode    *pMnode = pMsg->info.node;
187,915,350✔
904
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
187,915,350✔
905

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

909
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
187,915,350✔
910
  if (code != 0) {
187,915,350✔
911
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
6,034!
912
            tstrerror(code), code);
913
  }
914

915
  return code;
187,915,350✔
916
}
917

918
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
825,266,674✔
919
  int32_t code = 0;
825,266,674✔
920
  if (!IsReq(pMsg)) TAOS_RETURN(code);
825,266,674✔
921
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
726,557,797✔
922
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
706,546,627✔
923
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
692,284,469✔
924
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
652,444,376✔
925
    TAOS_RETURN(code);
74,176,251✔
926
  }
927

928
  SMnode *pMnode = pMsg->info.node;
652,447,955✔
929
  (void)taosThreadRwlockRdlock(&pMnode->lock);
652,450,390✔
930
  if (pMnode->stopped) {
652,473,674!
931
    (void)taosThreadRwlockUnlock(&pMnode->lock);
22,370✔
932
    code = TSDB_CODE_APP_IS_STOPPING;
22,370✔
933
    TAOS_RETURN(code);
22,370✔
934
  }
935

936
  terrno = 0;
652,419,359✔
937
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
652,430,960✔
938
  if (terrno != 0) {
652,456,069!
939
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
940
    code = terrno;
×
941
    TAOS_RETURN(code);
×
942
  }
943

944
  if (state.state != TAOS_SYNC_STATE_LEADER) {
652,446,407✔
945
    (void)taosThreadRwlockUnlock(&pMnode->lock);
14,531,329✔
946
    code = TSDB_CODE_SYN_NOT_LEADER;
14,532,280✔
947
    goto _OVER;
14,532,280✔
948
  }
949

950
  if (!state.restored || !pMnode->restored) {
637,915,078!
951
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,589,702✔
952
    code = TSDB_CODE_SYN_RESTORING;
1,584,174✔
953
    goto _OVER;
1,584,174✔
954
  }
955

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

963
  (void)taosThreadRwlockUnlock(&pMnode->lock);
636,332,027✔
964
  TAOS_RETURN(code);
636,337,631✔
965

966
_OVER:
16,116,454✔
967
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
16,116,454✔
968
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
16,107,630✔
969
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
16,098,844!
970
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
16,101,405!
971
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
16,101,088!
972
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
16,099,346!
973
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
16,099,346!
974
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
16,097,707✔
975
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER) {
16,090,741✔
976
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
18,137!
977
           pMnode->stopped, state.restored, syncStr(state.state));
978
    TAOS_RETURN(code);
18,137✔
979
  }
980

981
  const STraceId *trace = &pMsg->info.traceId;
16,088,910✔
982
  SEpSet          epSet = {0};
16,092,998✔
983
  mndGetMnodeEpSet(pMnode, &epSet);
16,094,470✔
984

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

991
  if (epSet.numOfEps <= 0) return -1;
16,095,434!
992

993
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
59,887,040✔
994
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
43,791,606✔
995
  }
996

997
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
16,095,434✔
998
  pMsg->info.rsp = rpcMallocCont(contLen);
16,093,538✔
999
  if (pMsg->info.rsp != NULL) {
16,089,649!
1000
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
16,090,393!
1001
      mError("failed to serialize ep set");
×
1002
    }
1003
    pMsg->info.hasEpSet = 1;
16,089,429✔
1004
    pMsg->info.rspLen = contLen;
16,092,548✔
1005
  }
1006

1007
  TAOS_RETURN(code);
16,090,401✔
1008
}
1009

1010
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
825,303,238✔
1011
  SMnode         *pMnode = pMsg->info.node;
825,303,238✔
1012
  const STraceId *trace = &pMsg->info.traceId;
825,303,478✔
1013
  int32_t         code = TSDB_CODE_SUCCESS;
825,251,968✔
1014

1015
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
825,251,968✔
1016
  MndMsgFpExt fpExt = NULL;
825,297,506✔
1017
  if (fp == NULL) {
825,297,506✔
1018
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
74,131,921✔
1019
    if (fpExt == NULL) {
74,130,635!
1020
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1021
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1022
      TAOS_RETURN(code);
×
1023
    }
1024
  }
1025

1026
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
825,296,220✔
1027

1028
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
809,163,330!
1029
  if (fp)
809,164,574✔
1030
    code = (*fp)(pMsg);
735,032,653✔
1031
  else
1032
    code = (*fpExt)(pMsg, pQueueInfo);
74,131,921✔
1033
  mndReleaseRpc(pMnode);
809,162,617✔
1034

1035
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
809,175,253✔
1036
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
101,835,508!
1037
  } else if (code == 0) {
707,339,745✔
1038
    mGTrace("msg:%p, successfully processed", pMsg);
698,157,785!
1039
  } else {
1040
    // TODO removve this wrong set code
1041
    if (code == -1) {
9,181,960✔
1042
      code = terrno;
8,853✔
1043
    }
1044
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
9,181,960!
1045
            TMSG_INFO(pMsg->msgType));
1046
  }
1047

1048
  TAOS_RETURN(code);
809,175,253✔
1049
}
1050

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

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

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

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

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

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

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

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

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

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

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

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

1159
  // vgroup info
1160
  pIter = NULL;
960✔
1161
  while (1) {
1,920✔
1162
    SVgObj *pVgroup = NULL;
2,880✔
1163
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
2,880✔
1164
    if (pIter == NULL) break;
2,880✔
1165

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

1171
    pClusterInfo->vgroups_total++;
1,920✔
1172
    pClusterInfo->tbs_total += pVgroup->numOfTables;
1,920✔
1173

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

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

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

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

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

1218
    SMonStbDesc desc = {0};
×
1219

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

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

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

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

1252
  mndReleaseRpc(pMnode);
960✔
1253
  TAOS_RETURN(code);
960✔
1254
}
1255

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

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

1272
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
30,919,495✔
1273
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
30,919,495✔
1274
  return state.roleTimeMs;
30,919,495✔
1275
}
1276

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

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

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

1304
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
2,036,340,260!
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