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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 hits per line

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

80.0
/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 "mndInstance.h"
33
#include "mndMnode.h"
34
#include "mndMount.h"
35
#include "mndPerfSchema.h"
36
#include "mndPrivilege.h"
37
#include "mndProfile.h"
38
#include "mndQnode.h"
39
#include "mndQuery.h"
40
#include "mndRetention.h"
41
#include "mndRetentionDetail.h"
42
#include "mndRsma.h"
43
#include "mndScan.h"
44
#include "mndScanDetail.h"
45
#include "mndShow.h"
46
#include "mndSma.h"
47
#include "mndSnode.h"
48
#include "mndSsMigrate.h"
49
#include "mndStb.h"
50
#include "mndStream.h"
51
#include "mndSubscribe.h"
52
#include "mndSync.h"
53
#include "mndTelem.h"
54
#include "mndTopic.h"
55
#include "mndTrans.h"
56
#include "mndUser.h"
57
#include "mndVgroup.h"
58
#include "mndView.h"
59

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

79
static inline void mndReleaseRpc(SMnode *pMnode) {
240,168,784✔
80
  (void)taosThreadRwlockRdlock(&pMnode->lock);
240,168,784✔
81
#if 1
82
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
240,169,916✔
83
#else
84
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
85
  mTrace("mnode rpc is released, ref:%d", ref);
86
#endif
87
  (void)taosThreadRwlockUnlock(&pMnode->lock);
240,169,745✔
88
}
240,172,473✔
89

90
static void *mndBuildTimerMsg(int32_t *pContLen) {
54,872,966✔
91
  terrno = 0;
54,872,966✔
92
  SMTimerReq timerReq = {0};
54,872,966✔
93

94
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
54,872,966✔
95
  if (contLen <= 0) return NULL;
54,872,545✔
96
  void *pReq = rpcMallocCont(contLen);
54,872,545✔
97
  if (pReq == NULL) return NULL;
54,870,045✔
98

99
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
54,870,045✔
100
    mError("failed to serialize timer msg since %s", terrstr());
×
101
  }
102
  *pContLen = contLen;
54,870,342✔
103
  return pReq;
54,872,515✔
104
}
105

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

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

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

145
static void mndPullupInstances(SMnode *pMnode) {
4,334,643✔
146
  mTrace("pullup instance timer msg");
4,334,643✔
147
  int32_t contLen = 0;
4,334,643✔
148
  void   *pReq = mndBuildTimerMsg(&contLen);
4,334,643✔
149
  if (pReq != NULL) {
4,334,643✔
150
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_INSTANCE_TIMER, .pCont = pReq, .contLen = contLen};
4,334,643✔
151
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
4,334,643✔
NEW
152
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
153
    }
154
  }
155
}
4,334,643✔
156

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

168
static void mndPullupTrimDb(SMnode *pMnode) {
116✔
169
  mTrace("pullup trim");
116✔
170
  int32_t contLen = 0;
116✔
171
  void   *pReq = mndBuildTimerMsg(&contLen);
116✔
172
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
116✔
173
  // TODO check return value
174
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
116✔
175
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
176
  }
177
}
116✔
178

179
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,214,290✔
180
  mTrace("pullup trim query");
2,214,290✔
181
  int32_t contLen = 0;
2,214,290✔
182
  void   *pReq = mndBuildTimerMsg(&contLen);
2,214,290✔
183
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,214,290✔
184
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,214,290✔
185
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
186
  }
187
}
2,214,290✔
188

189
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
190
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
191
    return;
×
192
  }
193

194
  mTrace("pullup ssmigrate db");
×
195
  int32_t contLen = 0;
×
196
  void   *pReq = mndBuildTimerMsg(&contLen);
×
197
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
198
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
199
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
200
  }
201
}
202

203
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
×
204
  mTrace("pullup update ssmigrate progress");
×
205
  int32_t contLen = 0;
×
206
  void   *pReq = mndBuildTimerMsg(&contLen);
×
207
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
208
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
209
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
210
  }
211
}
×
212

213
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
11,134,520✔
214
  mTrace("pullup arb hb");
11,134,520✔
215
  int32_t contLen = 0;
11,134,520✔
216
  void   *pReq = mndBuildTimerMsg(&contLen);
11,134,520✔
217
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,134,520✔
218
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,134,520✔
219
}
220

221
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
7,361,458✔
222
  mTrace("pullup arb sync");
7,361,458✔
223
  int32_t contLen = 0;
7,361,458✔
224
  void   *pReq = mndBuildTimerMsg(&contLen);
7,361,458✔
225
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
7,361,458✔
226
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
7,361,458✔
227
}
228

229
static void mndCalMqRebalance(SMnode *pMnode) {
11,137,881✔
230
  int32_t contLen = 0;
11,137,881✔
231
  void   *pReq = mndBuildTimerMsg(&contLen);
11,137,881✔
232
  if (pReq != NULL) {
11,137,881✔
233
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
11,137,881✔
234
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,137,881✔
235
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
768✔
236
    }
237
  }
238
}
11,137,881✔
239

240
static void mndPullupTelem(SMnode *pMnode) {
254✔
241
  mTrace("pullup telem msg");
254✔
242
  int32_t contLen = 0;
254✔
243
  void   *pReq = mndBuildTimerMsg(&contLen);
254✔
244
  if (pReq != NULL) {
254✔
245
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
254✔
246
    // TODO check return value
247
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
254✔
248
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
249
    }
250
  }
251
}
254✔
252

253
static void mndPullupGrant(SMnode *pMnode) {
1,225,837✔
254
  mTrace("pullup grant msg");
1,225,837✔
255
  int32_t contLen = 0;
1,225,837✔
256
  void   *pReq = mndBuildTimerMsg(&contLen);
1,225,837✔
257
  if (pReq != NULL) {
1,225,837✔
258
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,225,837✔
259
                      .pCont = pReq,
260
                      .contLen = contLen,
261
                      .info.notFreeAhandle = 1,
262
                      .info.ahandle = 0};
263
    // TODO check return value
264
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,225,837✔
265
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
266
    }
267
  }
268
}
1,225,837✔
269

270
static void mndPullupAuth(SMnode *pMnode) {
×
271
  mTrace("pullup auth msg");
×
272
  int32_t contLen = 0;
×
273
  void   *pReq = mndBuildTimerMsg(&contLen);
×
274
  if (pReq != NULL) {
×
275
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_AUTH_HB_TIMER, .pCont = pReq, .contLen = contLen, .info.notFreeAhandle = 1, .info.ahandle = 0};
×
276
    // TODO check return value
277
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
278
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
279
    }
280
  }
281
}
×
282

283
static void mndIncreaseUpTime(SMnode *pMnode) {
17,950✔
284
  mTrace("increate uptime");
17,950✔
285
  int32_t contLen = 0;
17,950✔
286
  void   *pReq = mndBuildTimerMsg(&contLen);
17,950✔
287
  if (pReq != NULL) {
17,950✔
288
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
17,950✔
289
                      .pCont = pReq,
290
                      .contLen = contLen,
291
                      .info.notFreeAhandle = 1,
292
                      .info.ahandle = 0};
293
    // TODO check return value
294
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
17,950✔
295
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
296
    }
297
  }
298
}
17,950✔
299

300
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
159,534✔
301
  SSdb *pSdb = pMnode->pSdb;
159,534✔
302

303
  void *pIter = NULL;
159,534✔
304
  while (1) {
757,603✔
305
    SVgObj *pVgroup = NULL;
917,137✔
306
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
917,137✔
307
    if (pIter == NULL) break;
917,137✔
308

309
    bool stateChanged = false;
757,603✔
310
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,901,020✔
311
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,452,158✔
312
      if (pGid->dnodeId == dnodeId) {
1,452,158✔
313
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
308,741✔
314
          mInfo(
130,262✔
315
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
316
              "restored:0 "
317
              "canRead:0",
318
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
319
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
130,262✔
320
          pGid->syncRestore = 0;
130,262✔
321
          pGid->syncCanRead = 0;
130,262✔
322
          pGid->startTimeMs = 0;
130,262✔
323
          stateChanged = true;
130,262✔
324
        }
325
        break;
308,741✔
326
      }
327
    }
328

329
    if (stateChanged) {
757,603✔
330
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
130,262✔
331
      if (pDb != NULL && pDb->stateTs != curMs) {
130,262✔
332
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
85,456✔
333
              curMs);
334
        pDb->stateTs = curMs;
85,456✔
335
      }
336
      mndReleaseDb(pMnode, pDb);
130,262✔
337
    }
338

339
    sdbRelease(pSdb, pVgroup);
757,603✔
340
  }
341
}
159,534✔
342

343
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,332,345✔
344
  mTrace("check dnode offline");
4,332,345✔
345
  if (mndAcquireRpc(pMnode) != 0) return;
4,332,345✔
346

347
  SSdb   *pSdb = pMnode->pSdb;
4,332,345✔
348
  int64_t curMs = taosGetTimestampMs();
4,332,345✔
349

350
  void *pIter = NULL;
4,332,345✔
351
  while (1) {
8,494,128✔
352
    SDnodeObj *pDnode = NULL;
12,826,473✔
353
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,826,473✔
354
    if (pIter == NULL) break;
12,826,473✔
355

356
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,494,128✔
357
    if (!online) {
8,494,128✔
358
      mInfo("dnode:%d, in offline state", pDnode->id);
159,534✔
359
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
159,534✔
360
    }
361

362
    sdbRelease(pSdb, pDnode);
8,494,128✔
363
  }
364

365
  mndReleaseRpc(pMnode);
4,332,345✔
366
}
367

368
static bool mnodeIsNotLeader(SMnode *pMnode) {
272,948,361✔
369
  terrno = 0;
272,948,361✔
370
  (void)taosThreadRwlockRdlock(&pMnode->lock);
272,948,477✔
371
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
272,954,896✔
372
  if (terrno != 0) {
272,955,043✔
373
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
374
    return true;
×
375
  }
376

377
  if (state.state != TAOS_SYNC_STATE_LEADER) {
272,954,281✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
23,982,837✔
379
    terrno = TSDB_CODE_SYN_NOT_LEADER;
23,982,837✔
380
    return true;
23,982,837✔
381
  }
382
  if (!state.restored || !pMnode->restored) {
248,971,444✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,167✔
384
    terrno = TSDB_CODE_SYN_RESTORING;
6,379✔
385
    return true;
6,379✔
386
  }
387
  (void)taosThreadRwlockUnlock(&pMnode->lock);
248,965,827✔
388
  return false;
248,965,216✔
389
}
390

391
static int32_t minCronTime() {
×
392
  int32_t min = INT32_MAX;
×
393
  min = TMIN(min, tsTtlPushIntervalSec);
×
394
  min = TMIN(min, tsTrimVDbIntervalSec);
×
395
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
396
  min = TMIN(min, tsTransPullupInterval);
×
397
  min = TMIN(min, tsCompactPullupInterval);
×
398
  min = TMIN(min, tsMqRebalanceInterval);
×
399

400
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
401
  min = TMIN(min, telemInt);
×
402
  min = TMIN(min, tsGrantHBInterval);
×
403
  min = TMIN(min, tsUptimeInterval);
×
404

405
  return min <= 1 ? 2 : min;
×
406
}
407
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,472,683✔
408
  int32_t code = 0;
22,472,683✔
409
#ifndef TD_ASTRA
410
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,472,683✔
411
    mndPullupGrant(pMnode);
1,225,837✔
412
  }
413
  if (sec % tsTtlPushIntervalSec == 0) {
22,472,683✔
414
    mndPullupTtl(pMnode);
2,176,763✔
415
  }
416

417
  if (sec % tsTrimVDbIntervalSec == 0) {
22,472,683✔
418
    mndPullupTrimDb(pMnode);
116✔
419
  }
420

421
  if (sec % tsQueryTrimIntervalSec == 0) {
22,472,683✔
422
    mndPullupQueryTrimDb(pMnode);
2,214,290✔
423
  }
424
#endif
425
#ifdef USE_SHARED_STORAGE
426
  if (tsSsEnabled) {
22,472,683✔
427
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
428
      mndPullupUpdateSsMigrateProgress(pMnode);
×
429
    }
430
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
431
      mndPullupSsMigrateDb(pMnode);
×
432
    }
433
  }
434
#endif
435
#ifdef TD_ENTERPRISE
436
  if (tsAuthReq) {
22,472,683✔
437
    if (sec % tsAuthReqHBInterval == 0) {
×
438
      mndPullupAuth(pMnode);
×
439
    }
440
  }
441
#endif
442
  if (sec % tsTransPullupInterval == 0) {
22,472,683✔
443
    mndPullupTrans(pMnode);
11,138,474✔
444
  }
445

446
  if (sec % tsCompactPullupInterval == 0) {
22,472,683✔
447
    mndPullupCompacts(pMnode);
2,065,390✔
448
  }
449

450
  if (sec % tsScanPullupInterval == 0) {
22,472,683✔
451
    mndPullupScans(pMnode);
2,065,390✔
452
  }
453
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,472,683✔
454
    mndPullupInstances(pMnode);
4,334,643✔
455
  }
456
#ifdef USE_TOPIC
457
  if (sec % tsMqRebalanceInterval == 0) {
22,472,683✔
458
    mndCalMqRebalance(pMnode);
11,137,881✔
459
  }
460
#endif
461
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,472,683✔
462
    mndPullupTelem(pMnode);
254✔
463
  }
464
  if (sec % tsUptimeInterval == 0) {
22,472,683✔
465
    mndIncreaseUpTime(pMnode);
17,950✔
466
  }
467
}
22,472,683✔
468

469
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
226,493,656✔
470
  int32_t code = 0;
226,493,656✔
471
#ifndef TD_ASTRA
472
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
226,493,656✔
473
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,134,520✔
474
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
475
    }
476
  }
477

478
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
226,493,656✔
479
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,361,458✔
480
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
481
    }
482
  }
483
#endif
484
}
226,493,656✔
485

486
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
226,493,656✔
487
  if (ms % (tsStatusTimeoutMs) == 0) {
226,493,656✔
488
    mndCheckDnodeOffline(pMnode);
4,332,345✔
489
  }
490
}
226,493,656✔
491

492
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,472,683✔
493
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,472,683✔
494
    mndSyncCheckTimeout(pMnode);
600,983✔
495
  }
496
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,472,683✔
497
    msmHealthCheck(pMnode);
7,364,297✔
498
  }
499
}
22,472,683✔
500

501
static void *mndThreadSecFp(void *param) {
489,197✔
502
  SMnode *pMnode = param;
489,197✔
503
  int64_t lastTime = 0;
489,197✔
504
  setThreadName("mnode-timer");
489,197✔
505

506
  while (1) {
248,383,594✔
507
    lastTime++;
248,872,791✔
508
    taosMsleep(100);
248,872,791✔
509

510
    if (mndGetStop(pMnode)) break;
248,872,791✔
511
    if (lastTime % 10 != 0) continue;
248,383,594✔
512

513
    if (mnodeIsNotLeader(pMnode)) {
24,618,996✔
514
      mTrace("timer not process since mnode is not leader");
2,146,313✔
515
      continue;
2,146,313✔
516
    }
517

518
    int64_t sec = lastTime / 10;
22,472,683✔
519
    mndDoTimerCheckSync(pMnode, sec);
22,472,683✔
520

521
    mndDoTimerPullupTask(pMnode, sec);
22,472,683✔
522
  }
523

524
  return NULL;
489,197✔
525
}
526

527
static void *mndThreadMsFp(void *param) {
489,197✔
528
  SMnode *pMnode = param;
489,197✔
529
  int64_t lastTime = 0;
489,197✔
530
  setThreadName("mnode-arb-timer");
489,197✔
531

532
  while (1) {
533
    lastTime += 100;
248,825,756✔
534
    taosMsleep(100);
248,825,756✔
535

536
    if (mndGetStop(pMnode)) break;
248,825,756✔
537
    if (lastTime % 10 != 0) continue;
248,336,559✔
538

539
    if (mnodeIsNotLeader(pMnode)) {
248,336,559✔
540
      mTrace("timer not process since mnode is not leader");
21,842,903✔
541
      continue;
21,842,903✔
542
    }
543

544
    mndDoTimerCheckStatus(pMnode, lastTime);
226,493,656✔
545

546
    mndDoArbTimerPullupTask(pMnode, lastTime);
226,493,656✔
547
  }
548

549
  return NULL;
489,197✔
550
}
551

552
static int32_t mndInitTimer(SMnode *pMnode) {
489,197✔
553
  int32_t      code = 0;
489,197✔
554
  TdThreadAttr thAttr;
486,579✔
555
  (void)taosThreadAttrInit(&thAttr);
489,197✔
556
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
489,197✔
557
#ifdef TD_COMPACT_OS
558
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
559
#endif
560
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
489,197✔
561
    mError("failed to create timer thread since %s", tstrerror(code));
×
562
    TAOS_RETURN(code);
×
563
  }
564

565
  (void)taosThreadAttrDestroy(&thAttr);
489,197✔
566
  tmsgReportStartup("mnode-timer", "initialized");
489,197✔
567

568
  TdThreadAttr arbAttr;
486,579✔
569
  (void)taosThreadAttrInit(&arbAttr);
489,197✔
570
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
489,197✔
571
#ifdef TD_COMPACT_OS
572
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
573
#endif
574
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
489,197✔
575
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
576
    TAOS_RETURN(code);
×
577
  }
578

579
  (void)taosThreadAttrDestroy(&arbAttr);
489,197✔
580
  tmsgReportStartup("mnode-timer", "initialized");
489,197✔
581
  TAOS_RETURN(code);
489,197✔
582
}
583

584
static void mndCleanupTimer(SMnode *pMnode) {
489,197✔
585
  if (taosCheckPthreadValid(pMnode->thread)) {
489,197✔
586
    (void)taosThreadJoin(pMnode->thread, NULL);
489,197✔
587
    taosThreadClear(&pMnode->thread);
489,197✔
588
  }
589
  if (taosCheckPthreadValid(pMnode->arbThread)) {
489,197✔
590
    (void)taosThreadJoin(pMnode->arbThread, NULL);
489,197✔
591
    taosThreadClear(&pMnode->arbThread);
489,197✔
592
  }
593
}
489,197✔
594

595
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
489,969✔
596
  int32_t code = 0;
489,969✔
597
  pMnode->path = taosStrdup(path);
489,969✔
598
  if (pMnode->path == NULL) {
489,969✔
599
    code = terrno;
×
600
    TAOS_RETURN(code);
×
601
  }
602

603
  if (taosMkDir(pMnode->path) != 0) {
489,969✔
604
    code = terrno;
×
605
    TAOS_RETURN(code);
×
606
  }
607

608
  TAOS_RETURN(code);
489,969✔
609
}
610

611
static int32_t mndInitWal(SMnode *pMnode) {
489,969✔
612
  int32_t code = 0;
489,969✔
613
  char    path[PATH_MAX + 20] = {0};
489,969✔
614
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
489,969✔
615
  SWalCfg cfg = {.vgId = 1,
489,969✔
616
                 .fsyncPeriod = 0,
617
                 .rollPeriod = -1,
618
                 .segSize = -1,
619
                 .committed = -1,
620
                 .retentionPeriod = 0,
621
                 .retentionSize = 0,
622
                 .level = TAOS_WAL_FSYNC,
623
                 .encryptAlgorithm = 0,
624
                 .encryptKey = {0}};
625

626
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
627
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
489,969✔
628
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
629
    if (tsEncryptKey[0] == '\0') {
×
630
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
631
      TAOS_RETURN(code);
×
632
    } else {
633
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
634
    }
635
  }
636
#endif
637

638
  pMnode->pWal = walOpen(path, &cfg);
489,969✔
639
  if (pMnode->pWal == NULL) {
489,969✔
640
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
641
    if (terrno != 0) code = terrno;
×
642
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
643
    TAOS_RETURN(code);
×
644
  }
645

646
  TAOS_RETURN(code);
489,969✔
647
}
648

649
static void mndCloseWal(SMnode *pMnode) {
489,197✔
650
  if (pMnode->pWal != NULL) {
489,197✔
651
    walClose(pMnode->pWal);
489,197✔
652
    pMnode->pWal = NULL;
489,197✔
653
  }
654
}
489,197✔
655

656
static int32_t mndInitSdb(SMnode *pMnode) {
489,969✔
657
  int32_t code = 0;
489,969✔
658
  SSdbOpt opt = {0};
489,969✔
659
  opt.path = pMnode->path;
489,969✔
660
  opt.pMnode = pMnode;
489,969✔
661
  opt.pWal = pMnode->pWal;
489,969✔
662

663
  pMnode->pSdb = sdbInit(&opt);
489,969✔
664
  if (pMnode->pSdb == NULL) {
489,969✔
665
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
666
    if (terrno != 0) code = terrno;
×
667
    TAOS_RETURN(code);
×
668
  }
669

670
  TAOS_RETURN(code);
489,969✔
671
}
672

673
static int32_t mndOpenSdb(SMnode *pMnode) {
489,969✔
674
  int32_t code = 0;
489,969✔
675
  if (!pMnode->deploy) {
489,969✔
676
    code = sdbReadFile(pMnode->pSdb);
171,838✔
677
  }
678

679
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
489,969✔
680

681
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
489,969✔
682
  return code;
489,969✔
683
}
684

685
static void mndCleanupSdb(SMnode *pMnode) {
489,197✔
686
  if (pMnode->pSdb) {
489,197✔
687
    sdbCleanup(pMnode->pSdb);
489,197✔
688
    pMnode->pSdb = NULL;
489,197✔
689
  }
690
}
489,197✔
691

692
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
22,538,574✔
693
  SMnodeStep step = {0};
22,538,574✔
694
  step.name = name;
22,538,574✔
695
  step.initFp = initFp;
22,538,574✔
696
  step.cleanupFp = cleanupFp;
22,538,574✔
697
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
45,077,148✔
698
    TAOS_RETURN(terrno);
×
699
  }
700

701
  TAOS_RETURN(0);
22,538,574✔
702
}
703

704
static int32_t mndInitSteps(SMnode *pMnode) {
489,969✔
705
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
489,969✔
706
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
489,969✔
707
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
489,969✔
708
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
489,969✔
709
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
489,969✔
710
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
489,969✔
711
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
489,969✔
712
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
489,969✔
713
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
489,969✔
714
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
489,969✔
715
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
489,969✔
716
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
489,969✔
717
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
489,969✔
718
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
489,969✔
719
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
489,969✔
720
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
489,969✔
721
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
489,969✔
722
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-instance", mndInitInstance, mndCleanupInstance));
489,969✔
723
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
489,969✔
724
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
489,969✔
725
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
489,969✔
726
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
489,969✔
727
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
489,969✔
728
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
489,969✔
729
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
489,969✔
730
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
489,969✔
731
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
489,969✔
732
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
489,969✔
733
#ifdef USE_MOUNT
734
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
489,969✔
735
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
489,969✔
736
#endif
737
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
489,969✔
738
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
489,969✔
739
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
489,969✔
740
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
489,969✔
741
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
489,969✔
742
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
489,969✔
743
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
489,969✔
744
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
489,969✔
745
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
489,969✔
746
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
489,969✔
747
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
489,969✔
748
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
489,969✔
749
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
489,969✔
750
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
489,969✔
751
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
489,969✔
752
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
489,969✔
753
  return 0;
489,969✔
754
}
755

756
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
489,197✔
757
  if (pMnode->pSteps == NULL) return;
489,197✔
758

759
  if (pos == -1) {
489,197✔
760
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
489,197✔
761
  }
762

763
  for (int32_t s = pos; s >= 0; s--) {
22,992,259✔
764
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,503,062✔
765
    mInfo("%s will cleanup", pStep->name);
22,503,062✔
766
    if (pStep->cleanupFp != NULL) {
22,503,062✔
767
      (*pStep->cleanupFp)(pMnode);
21,524,668✔
768
    }
769
  }
770

771
  taosArrayClear(pMnode->pSteps);
489,197✔
772
  taosArrayDestroy(pMnode->pSteps);
489,197✔
773
  pMnode->pSteps = NULL;
489,197✔
774
}
775

776
static int32_t mndExecSteps(SMnode *pMnode) {
489,969✔
777
  int32_t code = 0;
489,969✔
778
  int32_t size = taosArrayGetSize(pMnode->pSteps);
489,969✔
779
  for (int32_t pos = 0; pos < size; pos++) {
23,028,543✔
780
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,538,574✔
781
    if (pStep->initFp == NULL) continue;
22,538,574✔
782

783
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
22,538,574✔
784
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
785
      mndCleanupSteps(pMnode, pos);
×
786
      TAOS_RETURN(code);
×
787
    } else {
788
      mInfo("%s is initialized", pStep->name);
22,538,574✔
789
      tmsgReportStartup(pStep->name, "initialized");
22,538,574✔
790
    }
791
  }
792

793
  pMnode->clusterId = mndGetClusterId(pMnode);
489,969✔
794
  TAOS_RETURN(0);
489,969✔
795
}
796

797
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
489,969✔
798
  pMnode->msgCb = pOption->msgCb;
489,969✔
799
  pMnode->selfDnodeId = pOption->dnodeId;
489,969✔
800
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
489,969✔
801
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
489,969✔
802
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
489,969✔
803
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
489,969✔
804
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
489,969✔
805
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
489,969✔
806
}
489,969✔
807

808
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
489,969✔
809
  terrno = 0;
489,969✔
810
  mInfo("start to open mnode in %s", path);
489,969✔
811

812
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
489,969✔
813
  if (pMnode == NULL) {
489,969✔
814
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
815
    mError("failed to open mnode in step 1, since %s", terrstr());
×
816
    return NULL;
×
817
  }
818
  (void)memset(pMnode, 0, sizeof(SMnode));
489,969✔
819

820
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
489,969✔
821
  if (code != 0) {
489,969✔
822
    taosMemoryFree(pMnode);
×
823
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
824
    terrno = code;
×
825
    return NULL;
×
826
  }
827

828
  char timestr[24] = "1970-01-01 00:00:00.00";
489,969✔
829
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
489,969✔
830
  if (code < 0) {
489,969✔
831
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
832
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
833
    taosMemoryFree(pMnode);
×
834
    terrno = code;
×
835
    return NULL;
×
836
  }
837
  mndSetOptions(pMnode, pOption);
489,969✔
838

839
  pMnode->deploy = pOption->deploy;
489,969✔
840
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
489,969✔
841
  if (pMnode->pSteps == NULL) {
489,969✔
842
    taosMemoryFree(pMnode);
×
843
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
844
    mError("failed to open mnode in step 4, since %s", terrstr());
×
845
    return NULL;
×
846
  }
847

848
  code = mndCreateDir(pMnode, path);
489,969✔
849
  if (code != 0) {
489,969✔
850
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
851
    mndClose(pMnode);
×
852
    terrno = code;
×
853
    return NULL;
×
854
  }
855

856
  code = mndInitSteps(pMnode);
489,969✔
857
  if (code != 0) {
489,969✔
858
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
859
    mndClose(pMnode);
×
860
    terrno = code;
×
861
    return NULL;
×
862
  }
863

864
  code = mndExecSteps(pMnode);
489,969✔
865
  if (code != 0) {
489,969✔
866
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
867
    mndClose(pMnode);
×
868
    terrno = code;
×
869
    return NULL;
×
870
  }
871

872
  mInfo("mnode open successfully");
489,969✔
873
  return pMnode;
489,969✔
874
}
875

876
void mndPreClose(SMnode *pMnode) {
489,197✔
877
  if (pMnode != NULL) {
489,197✔
878
    int32_t code = 0;
489,197✔
879
    // TODO check return value
880
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
489,197✔
881
    if (code < 0) {
489,197✔
882
      mError("failed to transfer leader since %s", tstrerror(code));
×
883
    }
884
    syncPreStop(pMnode->syncMgmt.sync);
489,197✔
885
    code = sdbWriteFile(pMnode->pSdb, 0);
489,197✔
886
    if (code < 0) {
489,197✔
887
      mError("failed to write sdb since %s", tstrerror(code));
740✔
888
    }
889
  }
890
}
489,197✔
891

892
void mndClose(SMnode *pMnode) {
489,197✔
893
  if (pMnode != NULL) {
489,197✔
894
    mInfo("start to close mnode");
489,197✔
895
    mndCleanupSteps(pMnode, -1);
489,197✔
896
    taosMemoryFreeClear(pMnode->path);
489,197✔
897
    taosMemoryFreeClear(pMnode);
489,197✔
898
    mInfo("mnode is closed");
489,197✔
899
  }
900
}
489,197✔
901

902
int32_t mndStart(SMnode *pMnode) {
489,197✔
903
  mndSyncStart(pMnode);
489,197✔
904
  if (pMnode->deploy) {
489,197✔
905
    if (sdbDeploy(pMnode->pSdb) != 0) {
318,131✔
906
      mError("failed to deploy sdb while start mnode");
×
907
      return -1;
×
908
    }
909
    mndSetRestored(pMnode, true);
318,131✔
910
  }
911
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
489,197✔
912

913
  return mndInitTimer(pMnode);
489,197✔
914
}
915

916
int32_t mndIsCatchUp(SMnode *pMnode) {
296,482✔
917
  int64_t rid = pMnode->syncMgmt.sync;
296,482✔
918
  return syncIsCatchUp(rid);
296,482✔
919
}
920

921
ESyncRole mndGetRole(SMnode *pMnode) {
296,482✔
922
  int64_t rid = pMnode->syncMgmt.sync;
296,482✔
923
  return syncGetRole(rid);
296,482✔
924
}
925

926
int64_t mndGetTerm(SMnode *pMnode) {
7,460,652✔
927
  int64_t rid = pMnode->syncMgmt.sync;
7,460,652✔
928
  return syncGetTerm(rid);
7,460,652✔
929
}
930

931
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,596,001✔
932

933
void mndStop(SMnode *pMnode) {
489,197✔
934
  mndSetStop(pMnode);
489,197✔
935
  mndSyncStop(pMnode);
489,197✔
936
  mndCleanupTimer(pMnode);
489,197✔
937
}
489,197✔
938

939
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
63,424,850✔
940
  SMnode    *pMnode = pMsg->info.node;
63,424,850✔
941
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
63,424,850✔
942

943
  const STraceId *trace = &pMsg->info.traceId;
63,424,850✔
944
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
63,424,850✔
945

946
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
63,424,850✔
947
  if (code != 0) {
63,424,850✔
948
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,600✔
949
            tstrerror(code), code);
950
  }
951

952
  return code;
63,424,850✔
953
}
954

955
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
241,062,500✔
956
  int32_t code = 0;
241,062,500✔
957
  if (!IsReq(pMsg)) TAOS_RETURN(code);
241,062,500✔
958
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
210,179,009✔
959
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
204,908,412✔
960
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
199,975,071✔
961
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
189,419,341✔
962
    TAOS_RETURN(code);
20,756,552✔
963
  }
964

965
  SMnode *pMnode = pMsg->info.node;
189,418,248✔
966
  (void)taosThreadRwlockRdlock(&pMnode->lock);
189,421,176✔
967
  if (pMnode->stopped) {
189,419,056✔
968
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,436✔
969
    code = TSDB_CODE_APP_IS_STOPPING;
5,436✔
970
    TAOS_RETURN(code);
5,436✔
971
  }
972

973
  terrno = 0;
189,412,759✔
974
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
189,408,884✔
975
  if (terrno != 0) {
189,416,820✔
976
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
977
    code = terrno;
×
978
    TAOS_RETURN(code);
×
979
  }
980

981
  if (state.state != TAOS_SYNC_STATE_LEADER) {
189,416,189✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,296,247✔
983
    code = TSDB_CODE_SYN_NOT_LEADER;
4,296,546✔
984
    goto _OVER;
4,296,546✔
985
  }
986

987
  if (!state.restored || !pMnode->restored) {
185,119,942✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
929,844✔
989
    code = TSDB_CODE_SYN_RESTORING;
928,187✔
990
    goto _OVER;
928,187✔
991
  }
992

993
#if 1
994
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
184,190,430✔
995
#else
996
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
997
  mTrace("mnode rpc is acquired, ref:%d", ref);
998
#endif
999

1000
  (void)taosThreadRwlockUnlock(&pMnode->lock);
184,191,111✔
1001
  TAOS_RETURN(code);
184,189,768✔
1002

1003
_OVER:
5,224,733✔
1004
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,224,733✔
1005
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,223,573✔
1006
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,222,703✔
1007
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,222,703✔
1008
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,222,404✔
1009
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,222,413✔
1010
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,222,413✔
1011
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,222,413✔
1012
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,222,114✔
1013
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
1,721✔
1014
           pMnode->stopped, state.restored, syncStr(state.state));
1015
    TAOS_RETURN(code);
1,721✔
1016
  }
1017

1018
  const STraceId *trace = &pMsg->info.traceId;
5,221,843✔
1019
  SEpSet          epSet = {0};
5,222,114✔
1020
  mndGetMnodeEpSet(pMnode, &epSet);
5,222,142✔
1021

1022
  mGDebug(
5,222,413✔
1023
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1024
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1025
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1026
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1027

1028
  if (epSet.numOfEps <= 0) return -1;
5,222,413✔
1029

1030
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
18,653,149✔
1031
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
13,430,736✔
1032
  }
1033

1034
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,222,413✔
1035
  pMsg->info.rsp = rpcMallocCont(contLen);
5,220,958✔
1036
  if (pMsg->info.rsp != NULL) {
5,222,142✔
1037
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,222,413✔
1038
      mError("failed to serialize ep set");
×
1039
    }
1040
    pMsg->info.hasEpSet = 1;
5,221,012✔
1041
    pMsg->info.rspLen = contLen;
5,221,814✔
1042
  }
1043

1044
  TAOS_RETURN(code);
5,220,733✔
1045
}
1046

1047
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
241,069,407✔
1048
  SMnode         *pMnode = pMsg->info.node;
241,069,407✔
1049
  const STraceId *trace = &pMsg->info.traceId;
241,070,966✔
1050
  int32_t         code = TSDB_CODE_SUCCESS;
241,058,019✔
1051

1052
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
241,058,019✔
1053
  MndMsgFpExt fpExt = NULL;
241,067,978✔
1054
  if (fp == NULL) {
241,067,978✔
1055
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,765,350✔
1056
    if (fpExt == NULL) {
20,765,350✔
1057
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1058
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1059
      TAOS_RETURN(code);
×
1060
    }
1061
  }
1062

1063
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
241,067,978✔
1064

1065
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
235,837,902✔
1066
  if (fp)
235,837,929✔
1067
    code = (*fp)(pMsg);
215,072,579✔
1068
  else
1069
    code = (*fpExt)(pMsg, pQueueInfo);
20,765,350✔
1070
  mndReleaseRpc(pMnode);
235,838,536✔
1071

1072
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
235,837,555✔
1073
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
31,860,975✔
1074
  } else if (code == 0) {
203,976,580✔
1075
    mGTrace("msg:%p, successfully processed", pMsg);
201,377,976✔
1076
  } else {
1077
    // TODO removve this wrong set code
1078
    if (code == -1) {
2,598,604✔
1079
      code = terrno;
3,153✔
1080
    }
1081
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,598,604✔
1082
            TMSG_INFO(pMsg->msgType));
1083
  }
1084

1085
  TAOS_RETURN(code);
235,837,555✔
1086
}
1087

1088
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
96,033,924✔
1089
  tmsg_t type = TMSG_INDEX(msgType);
96,033,924✔
1090
  if (type < TDMT_MAX) {
96,033,924✔
1091
    pMnode->msgFp[type] = fp;
96,033,924✔
1092
  }
1093
}
96,033,924✔
1094

1095
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,919,752✔
1096
  tmsg_t type = TMSG_INDEX(msgType);
3,919,752✔
1097
  if (type < TDMT_MAX) {
3,919,752✔
1098
    pMnode->msgFpExt[type] = fp;
3,919,752✔
1099
  }
1100
}
3,919,752✔
1101

1102
// Note: uid 0 is reserved
1103
int64_t mndGenerateUid(const char *name, int32_t len) {
9,617,790✔
1104
  int32_t hashval = MurmurHash3_32(name, len);
9,617,790✔
1105
  do {
×
1106
    int64_t us = taosGetTimestampUs();
9,618,015✔
1107
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,618,015✔
1108
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,618,015✔
1109
    if (uuid) {
9,618,015✔
1110
      return llabs(uuid);
9,618,015✔
1111
    }
1112
  } while (true);
1113
}
1114

1115
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
127✔
1116
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1117
  int32_t code = mndAcquireRpc(pMnode);
127✔
1118
  if (code < 0) {
127✔
1119
    TAOS_RETURN(code);
×
1120
  } else if (code == 1) {
127✔
1121
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1122
  }
1123

1124
  SSdb   *pSdb = pMnode->pSdb;
127✔
1125
  int64_t ms = taosGetTimestampMs();
127✔
1126

1127
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
127✔
1128
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
127✔
1129
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
127✔
1130
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
127✔
1131
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
127✔
1132
      pStbInfo->stbs == NULL) {
127✔
1133
    mndReleaseRpc(pMnode);
×
1134
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1135
    if (terrno != 0) code = terrno;
×
1136
    TAOS_RETURN(code);
×
1137
  }
1138

1139
  // cluster info
1140
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
127✔
1141
  pClusterInfo->monitor_interval = tsMonitorInterval;
127✔
1142
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
127✔
1143
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
127✔
1144
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
127✔
1145
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
127✔
1146
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
127✔
1147

1148
  void *pIter = NULL;
127✔
1149
  while (1) {
127✔
1150
    SDnodeObj *pObj = NULL;
254✔
1151
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
254✔
1152
    if (pIter == NULL) break;
254✔
1153

1154
    SMonDnodeDesc desc = {0};
127✔
1155
    desc.dnode_id = pObj->id;
127✔
1156
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
127✔
1157
    if (mndIsDnodeOnline(pObj, ms)) {
127✔
1158
      tstrncpy(desc.status, "ready", sizeof(desc.status));
127✔
1159
    } else {
1160
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1161
    }
1162
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
254✔
1163
      mError("failed put dnode into array, but continue at this monitor report")
×
1164
    }
1165
    sdbRelease(pSdb, pObj);
127✔
1166
  }
1167

1168
  pIter = NULL;
127✔
1169
  while (1) {
127✔
1170
    SMnodeObj *pObj = NULL;
254✔
1171
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
254✔
1172
    if (pIter == NULL) break;
254✔
1173

1174
    SMonMnodeDesc desc = {0};
127✔
1175
    desc.mnode_id = pObj->id;
127✔
1176
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
127✔
1177

1178
    if (pObj->id == pMnode->selfDnodeId) {
127✔
1179
      pClusterInfo->first_ep_dnode_id = pObj->id;
127✔
1180
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
127✔
1181
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1182
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
127✔
1183
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1184
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
127✔
1185
      desc.syncState = TAOS_SYNC_STATE_LEADER;
127✔
1186
    } else {
1187
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1188
      desc.syncState = pObj->syncState;
×
1189
    }
1190
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
254✔
1191
      mError("failed to put mnode into array, but continue at this monitor report");
×
1192
    }
1193
    sdbRelease(pSdb, pObj);
127✔
1194
  }
1195

1196
  // vgroup info
1197
  pIter = NULL;
127✔
1198
  while (1) {
254✔
1199
    SVgObj *pVgroup = NULL;
381✔
1200
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
381✔
1201
    if (pIter == NULL) break;
381✔
1202

1203
    if (pVgroup->mountVgId) {
254✔
1204
      sdbRelease(pSdb, pVgroup);
×
1205
      continue;
×
1206
    }
1207

1208
    pClusterInfo->vgroups_total++;
254✔
1209
    pClusterInfo->tbs_total += pVgroup->numOfTables;
254✔
1210

1211
    SMonVgroupDesc desc = {0};
254✔
1212
    desc.vgroup_id = pVgroup->vgId;
254✔
1213

1214
    SName name = {0};
254✔
1215
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
254✔
1216
    if (code < 0) {
254✔
1217
      mError("failed to get db name since %s", tstrerror(code));
×
1218
      sdbRelease(pSdb, pVgroup);
×
1219
      TAOS_RETURN(code);
×
1220
    }
1221
    (void)tNameGetDbName(&name, desc.database_name);
254✔
1222

1223
    desc.tables_num = pVgroup->numOfTables;
254✔
1224
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
254✔
1225
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
254✔
1226
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
508✔
1227
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
254✔
1228
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
254✔
1229
      pVnDesc->dnode_id = pVgid->dnodeId;
254✔
1230
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
254✔
1231
      pVnDesc->syncState = pVgid->syncState;
254✔
1232
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
254✔
1233
        tstrncpy(desc.status, "ready", sizeof(desc.status));
254✔
1234
        pClusterInfo->vgroups_alive++;
254✔
1235
      }
1236
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
254✔
1237
        pClusterInfo->vnodes_alive++;
254✔
1238
      }
1239
      pClusterInfo->vnodes_total++;
254✔
1240
    }
1241

1242
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
508✔
1243
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1244
    }
1245
    sdbRelease(pSdb, pVgroup);
254✔
1246
  }
1247

1248
  // stb info
1249
  pIter = NULL;
127✔
1250
  while (1) {
×
1251
    SStbObj *pStb = NULL;
127✔
1252
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
127✔
1253
    if (pIter == NULL) break;
127✔
1254

1255
    SMonStbDesc desc = {0};
×
1256

1257
    SName name1 = {0};
×
1258
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1259
    if (code < 0) {
×
1260
      mError("failed to get db name since %s", tstrerror(code));
×
1261
      sdbRelease(pSdb, pStb);
×
1262
      TAOS_RETURN(code);
×
1263
    }
1264
    (void)tNameGetDbName(&name1, desc.database_name);
×
1265

1266
    SName name2 = {0};
×
1267
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1268
    if (code < 0) {
×
1269
      mError("failed to get table name since %s", tstrerror(code));
×
1270
      sdbRelease(pSdb, pStb);
×
1271
      TAOS_RETURN(code);
×
1272
    }
1273
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1274

1275
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1276
      mError("failed to put stb into array, but continue at this monitor report");
×
1277
    }
1278
    sdbRelease(pSdb, pStb);
×
1279
  }
1280

1281
  // grant info
1282
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
127✔
1283
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
127✔
1284
  if (pMnode->grant.expireTimeMS == 0) {
127✔
1285
    pGrantInfo->expire_time = 0;
×
1286
    pGrantInfo->timeseries_total = 0;
×
1287
  }
1288

1289
  mndReleaseRpc(pMnode);
127✔
1290
  TAOS_RETURN(code);
127✔
1291
}
1292

1293
int32_t mndResetTimer(SMnode *pMnode){
×
1294
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1295
}
1296

1297
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
24,688,642✔
1298
  mTrace("mnode get load");
24,688,642✔
1299
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
24,688,642✔
1300
  pLoad->syncState = state.state;
24,688,642✔
1301
  pLoad->syncRestore = state.restored;
24,688,642✔
1302
  pLoad->syncTerm = state.term;
24,688,642✔
1303
  pLoad->roleTimeMs = state.roleTimeMs;
24,688,642✔
1304
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
24,688,642✔
1305
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1306
  return 0;
24,688,642✔
1307
}
1308

1309
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,361,458✔
1310
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,361,458✔
1311
  return state.roleTimeMs;
7,361,458✔
1312
}
1313

1314
void mndSetRestored(SMnode *pMnode, bool restored) {
489,197✔
1315
  if (restored) {
489,197✔
1316
    (void)taosThreadRwlockWrlock(&pMnode->lock);
489,197✔
1317
    pMnode->restored = true;
489,197✔
1318
    (void)taosThreadRwlockUnlock(&pMnode->lock);
489,197✔
1319
    mInfo("mnode set restored:%d", restored);
489,197✔
1320
  } else {
1321
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1322
    pMnode->restored = false;
×
1323
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1324
    mInfo("mnode set restored:%d", restored);
×
1325
    while (1) {
1326
      if (pMnode->rpcRef <= 0) break;
×
1327
      taosMsleep(3);
×
1328
    }
1329
  }
1330
}
489,197✔
1331

1332
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1333

1334
void mndSetStop(SMnode *pMnode) {
489,197✔
1335
  (void)taosThreadRwlockWrlock(&pMnode->lock);
489,197✔
1336
  pMnode->stopped = true;
489,197✔
1337
  (void)taosThreadRwlockUnlock(&pMnode->lock);
489,197✔
1338
  mInfo("mnode set stopped");
489,197✔
1339
}
489,197✔
1340

1341
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
497,686,903✔
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