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

taosdata / TDengine / #4895

23 Dec 2025 01:08PM UTC coverage: 65.513% (-0.2%) from 65.72%
#4895

push

travis-ci

web-flow
fix: mem leak (#34023)

6 of 9 new or added lines in 1 file covered. (66.67%)

7770 existing lines in 123 files now uncovered.

184705 of 281937 relevant lines covered (65.51%)

112009834.14 hits per line

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

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

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

80
static inline void mndReleaseRpc(SMnode *pMnode) {
242,247,642✔
81
  (void)taosThreadRwlockRdlock(&pMnode->lock);
242,247,642✔
82
#if 1
83
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
242,249,269✔
84
#else
85
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
86
  mTrace("mnode rpc is released, ref:%d", ref);
87
#endif
88
  (void)taosThreadRwlockUnlock(&pMnode->lock);
242,248,574✔
89
}
242,249,109✔
90

91
static void *mndBuildTimerMsg(int32_t *pContLen) {
56,045,427✔
92
  terrno = 0;
56,045,427✔
93
  SMTimerReq timerReq = {0};
56,046,576✔
94

95
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
56,045,823✔
96
  if (contLen <= 0) return NULL;
56,043,612✔
97
  void *pReq = rpcMallocCont(contLen);
56,043,612✔
98
  if (pReq == NULL) return NULL;
56,045,314✔
99

100
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
56,045,314✔
101
    mError("failed to serialize timer msg since %s", terrstr());
×
102
  }
103
  *pContLen = contLen;
56,046,062✔
104
  return pReq;
56,044,980✔
105
}
106

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

301
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
170,105✔
302
  SSdb *pSdb = pMnode->pSdb;
170,105✔
303

304
  void *pIter = NULL;
170,105✔
305
  while (1) {
784,191✔
306
    SVgObj *pVgroup = NULL;
954,296✔
307
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
954,296✔
308
    if (pIter == NULL) break;
954,296✔
309

310
    bool stateChanged = false;
784,191✔
311
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
2,045,081✔
312
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,584,441✔
313
      if (pGid->dnodeId == dnodeId) {
1,584,441✔
314
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
323,551✔
315
          mInfo(
140,533✔
316
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
317
              "restored:0 "
318
              "canRead:0",
319
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
320
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
140,533✔
321
          pGid->syncRestore = 0;
140,533✔
322
          pGid->syncCanRead = 0;
140,533✔
323
          pGid->startTimeMs = 0;
140,533✔
324
          stateChanged = true;
140,533✔
325
        }
326
        break;
323,551✔
327
      }
328
    }
329

330
    if (stateChanged) {
784,191✔
331
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
140,533✔
332
      if (pDb != NULL && pDb->stateTs != curMs) {
140,533✔
333
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
84,923✔
334
              curMs);
335
        pDb->stateTs = curMs;
84,923✔
336
      }
337
      mndReleaseDb(pMnode, pDb);
140,533✔
338
    }
339

340
    sdbRelease(pSdb, pVgroup);
784,191✔
341
  }
342
}
170,105✔
343

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

348
  SSdb   *pSdb = pMnode->pSdb;
4,434,926✔
349
  int64_t curMs = taosGetTimestampMs();
4,434,926✔
350

351
  void *pIter = NULL;
4,434,926✔
352
  while (1) {
8,595,108✔
353
    SDnodeObj *pDnode = NULL;
13,030,034✔
354
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,030,034✔
355
    if (pIter == NULL) break;
13,030,034✔
356

357
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,595,108✔
358
    if (!online) {
8,595,108✔
359
      mInfo("dnode:%d, in offline state", pDnode->id);
170,105✔
360
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
170,105✔
361
    }
362

363
    sdbRelease(pSdb, pDnode);
8,595,108✔
364
  }
365

366
  mndReleaseRpc(pMnode);
4,434,926✔
367
}
368

369
static bool mnodeIsNotLeader(SMnode *pMnode) {
280,577,306✔
370
  terrno = 0;
280,577,306✔
371
  (void)taosThreadRwlockRdlock(&pMnode->lock);
280,577,582✔
372
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
280,582,862✔
373
  if (terrno != 0) {
280,582,621✔
374
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
375
    return true;
×
376
  }
377

378
  if (state.state != TAOS_SYNC_STATE_LEADER) {
280,582,002✔
379
    (void)taosThreadRwlockUnlock(&pMnode->lock);
25,659,335✔
380
    terrno = TSDB_CODE_SYN_NOT_LEADER;
25,659,335✔
381
    return true;
25,659,335✔
382
  }
383
  if (!state.restored || !pMnode->restored) {
254,922,667✔
384
    (void)taosThreadRwlockUnlock(&pMnode->lock);
20,539✔
385
    terrno = TSDB_CODE_SYN_RESTORING;
19,133✔
386
    return true;
19,133✔
387
  }
388
  (void)taosThreadRwlockUnlock(&pMnode->lock);
254,902,128✔
389
  return false;
254,904,119✔
390
}
391

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

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

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

418
  if (sec % tsTrimVDbIntervalSec == 0) {
22,994,573✔
419
    mndPullupTrimDb(pMnode);
118✔
420
  }
421

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

447
  if (sec % tsCompactPullupInterval == 0) {
22,994,573✔
448
    mndPullupCompacts(pMnode);
2,111,768✔
449
  }
450

451
  if (sec % tsScanPullupInterval == 0) {
22,994,573✔
452
    mndPullupScans(pMnode);
2,111,768✔
453
  }
454
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,994,573✔
455
    mndPullupInstances(pMnode);
4,436,975✔
456
  }
457
#ifdef USE_TOPIC
458
  if (sec % tsMqRebalanceInterval == 0) {
22,994,573✔
459
    mndCalMqRebalance(pMnode);
11,389,839✔
460
  }
461
#endif
462
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,994,573✔
463
    mndPullupTelem(pMnode);
260✔
464
  }
465
  if (sec % tsUptimeInterval == 0) {
22,994,573✔
466
    mndIncreaseUpTime(pMnode);
19,666✔
467
  }
468
}
22,994,573✔
469

470
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
231,910,202✔
471
  int32_t code = 0;
231,910,202✔
472
#ifndef TD_ASTRA
473
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
231,910,202✔
474
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,393,771✔
475
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
476
    }
477
  }
478

479
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
231,910,202✔
480
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,519,887✔
UNCOV
481
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
482
    }
483
  }
484
#endif
485
}
231,910,202✔
486

487
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
231,910,202✔
488
  if (ms % (tsStatusTimeoutMs) == 0) {
231,910,202✔
489
    mndCheckDnodeOffline(pMnode);
4,434,926✔
490
  }
491
}
231,910,202✔
492

493
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,994,573✔
494
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,994,573✔
495
    mndSyncCheckTimeout(pMnode);
602,666✔
496
  }
497
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,994,573✔
498
    msmHealthCheck(pMnode);
7,520,106✔
499
  }
500
}
22,994,573✔
501

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

507
  while (1) {
255,304,678✔
508
    lastTime++;
255,821,569✔
509
    taosMsleep(100);
255,821,569✔
510

511
    if (mndGetStop(pMnode)) break;
255,821,569✔
512
    if (lastTime % 10 != 0) continue;
255,304,678✔
513

514
    if (mnodeIsNotLeader(pMnode)) {
25,291,517✔
515
      mTrace("timer not process since mnode is not leader");
2,296,944✔
516
      continue;
2,296,944✔
517
    }
518

519
    int64_t sec = lastTime / 10;
22,994,573✔
520
    mndDoTimerCheckSync(pMnode, sec);
22,994,573✔
521

522
    mndDoTimerPullupTask(pMnode, sec);
22,994,573✔
523
  }
524

525
  return NULL;
516,891✔
526
}
527

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

533
  while (1) {
534
    lastTime += 100;
255,808,617✔
535
    taosMsleep(100);
255,808,617✔
536

537
    if (mndGetStop(pMnode)) break;
255,808,617✔
538
    if (lastTime % 10 != 0) continue;
255,291,726✔
539

540
    if (mnodeIsNotLeader(pMnode)) {
255,291,726✔
541
      mTrace("timer not process since mnode is not leader");
23,381,524✔
542
      continue;
23,381,524✔
543
    }
544

545
    mndDoTimerCheckStatus(pMnode, lastTime);
231,910,202✔
546

547
    mndDoArbTimerPullupTask(pMnode, lastTime);
231,910,202✔
548
  }
549

550
  return NULL;
516,891✔
551
}
552

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

566
  (void)taosThreadAttrDestroy(&thAttr);
516,891✔
567
  tmsgReportStartup("mnode-timer", "initialized");
516,891✔
568

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

580
  (void)taosThreadAttrDestroy(&arbAttr);
516,891✔
581
  tmsgReportStartup("mnode-timer", "initialized");
516,891✔
582
  TAOS_RETURN(code);
516,891✔
583
}
584

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

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

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

609
  TAOS_RETURN(code);
517,011✔
610
}
611

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

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

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

647
  TAOS_RETURN(code);
517,011✔
648
}
649

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

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

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

671
  TAOS_RETURN(code);
517,011✔
672
}
673

674
static int32_t mndOpenSdb(SMnode *pMnode) {
517,011✔
675
  int32_t code = 0;
517,011✔
676
  if (!pMnode->deploy) {
517,011✔
677
    code = sdbReadFile(pMnode->pSdb);
179,305✔
678
  }
679

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

682
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
517,011✔
683
  return code;
517,011✔
684
}
685

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

693
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
24,299,517✔
694
  SMnodeStep step = {0};
24,299,517✔
695
  step.name = name;
24,299,517✔
696
  step.initFp = initFp;
24,299,517✔
697
  step.cleanupFp = cleanupFp;
24,299,517✔
698
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
48,599,034✔
699
    TAOS_RETURN(terrno);
×
700
  }
701

702
  TAOS_RETURN(0);
24,299,517✔
703
}
704

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

758
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
516,891✔
759
  if (pMnode->pSteps == NULL) return;
516,891✔
760

761
  if (pos == -1) {
516,891✔
762
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
516,891✔
763
  }
764

765
  for (int32_t s = pos; s >= 0; s--) {
24,810,768✔
766
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
24,293,877✔
767
    mInfo("%s will cleanup", pStep->name);
24,293,877✔
768
    if (pStep->cleanupFp != NULL) {
24,293,877✔
769
      (*pStep->cleanupFp)(pMnode);
23,260,095✔
770
    }
771
  }
772

773
  taosArrayClear(pMnode->pSteps);
516,891✔
774
  taosArrayDestroy(pMnode->pSteps);
516,891✔
775
  pMnode->pSteps = NULL;
516,891✔
776
}
777

778
static int32_t mndExecSteps(SMnode *pMnode) {
517,011✔
779
  int32_t code = 0;
517,011✔
780
  int32_t size = taosArrayGetSize(pMnode->pSteps);
517,011✔
781
  for (int32_t pos = 0; pos < size; pos++) {
24,816,528✔
782
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
24,299,517✔
783
    if (pStep->initFp == NULL) continue;
24,299,517✔
784

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

795
  pMnode->clusterId = mndGetClusterId(pMnode);
517,011✔
796
  TAOS_RETURN(0);
517,011✔
797
}
798

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

810
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
517,011✔
811
  terrno = 0;
517,011✔
812
  mInfo("start to open mnode in %s", path);
517,011✔
813

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

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

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

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

851
  code = mndCreateDir(pMnode, path);
517,011✔
852
  if (code != 0) {
517,011✔
853
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
854
    mndClose(pMnode);
×
855
    terrno = code;
×
856
    return NULL;
×
857
  }
858

859
  code = mndInitSteps(pMnode);
517,011✔
860
  if (code != 0) {
517,011✔
861
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
862
    mndClose(pMnode);
×
863
    terrno = code;
×
864
    return NULL;
×
865
  }
866

867
  code = mndExecSteps(pMnode);
517,011✔
868
  if (code != 0) {
517,011✔
869
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
870
    mndClose(pMnode);
×
871
    terrno = code;
×
872
    return NULL;
×
873
  }
874

875
  mInfo("mnode open successfully");
517,011✔
876
  return pMnode;
517,011✔
877
}
878

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

895
void mndClose(SMnode *pMnode) {
516,891✔
896
  if (pMnode != NULL) {
516,891✔
897
    mInfo("start to close mnode");
516,891✔
898
    mndCleanupSteps(pMnode, -1);
516,891✔
899
    taosMemoryFreeClear(pMnode->path);
516,891✔
900
    taosMemoryFreeClear(pMnode);
516,891✔
901
    mInfo("mnode is closed");
516,891✔
902
  }
903
}
516,891✔
904

905
int32_t mndStart(SMnode *pMnode) {
516,891✔
906
  mndSyncStart(pMnode);
516,891✔
907
  if (pMnode->deploy) {
516,891✔
908
    if (sdbDeploy(pMnode->pSdb) != 0) {
337,706✔
909
      mError("failed to deploy sdb while start mnode");
×
910
      return -1;
×
911
    }
912
    mndSetRestored(pMnode, true);
337,706✔
913
  }
914
  if (mndIsLeader(pMnode)) {
516,891✔
915
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
420,421✔
916
      mError("failed to upgrade sdb while start mnode");
×
917
      return -1;
×
918
    }
919
  }
920
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
516,891✔
921
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
516,891✔
922

923
  return mndInitTimer(pMnode);
516,891✔
924
}
925

926
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
516,891✔
927

928
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
401,380✔
929

930
int32_t mndIsCatchUp(SMnode *pMnode) {
325,692✔
931
  int64_t rid = pMnode->syncMgmt.sync;
325,692✔
932
  return syncIsCatchUp(rid);
325,692✔
933
}
934

935
ESyncRole mndGetRole(SMnode *pMnode) {
325,692✔
936
  int64_t rid = pMnode->syncMgmt.sync;
325,692✔
937
  return syncGetRole(rid);
325,692✔
938
}
939

940
int64_t mndGetTerm(SMnode *pMnode) {
7,630,349✔
941
  int64_t rid = pMnode->syncMgmt.sync;
7,630,349✔
942
  return syncGetTerm(rid);
7,630,349✔
943
}
944

945
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
19,025,329✔
946

947
void mndStop(SMnode *pMnode) {
516,891✔
948
  mndSetStop(pMnode);
516,891✔
949
  mndSyncStop(pMnode);
516,891✔
950
  mndCleanupTimer(pMnode);
516,891✔
951
}
516,891✔
952

953
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
66,240,248✔
954
  SMnode    *pMnode = pMsg->info.node;
66,240,248✔
955
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
66,240,248✔
956

957
  const STraceId *trace = &pMsg->info.traceId;
66,240,248✔
958
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
66,240,248✔
959

960
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
66,240,248✔
961
  if (code != 0) {
66,240,248✔
962
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,638✔
963
            tstrerror(code), code);
964
  }
965

966
  return code;
66,239,934✔
967
}
968

969
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
242,957,221✔
970
  int32_t code = 0;
242,957,221✔
971
  if (!IsReq(pMsg)) TAOS_RETURN(code);
242,957,221✔
972
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
211,654,498✔
973
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
206,340,504✔
974
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
201,365,828✔
975
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
190,706,541✔
976
    TAOS_RETURN(code);
20,953,643✔
977
  }
978

979
  SMnode *pMnode = pMsg->info.node;
190,706,062✔
980
  (void)taosThreadRwlockRdlock(&pMnode->lock);
190,702,684✔
981
  if (pMnode->stopped) {
190,711,846✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,743✔
983
    code = TSDB_CODE_APP_IS_STOPPING;
6,743✔
984
    TAOS_RETURN(code);
6,743✔
985
  }
986

987
  terrno = 0;
190,697,064✔
988
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
190,700,401✔
989
  if (terrno != 0) {
190,705,542✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
991
    code = terrno;
×
992
    TAOS_RETURN(code);
×
993
  }
994

995
  if (state.state != TAOS_SYNC_STATE_LEADER) {
190,704,297✔
996
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,342,599✔
997
    code = TSDB_CODE_SYN_NOT_LEADER;
4,343,290✔
998
    goto _OVER;
4,343,290✔
999
  }
1000

1001
  if (!state.restored || !pMnode->restored) {
186,361,698✔
1002
    (void)taosThreadRwlockUnlock(&pMnode->lock);
812,359✔
1003
    code = TSDB_CODE_SYN_RESTORING;
806,399✔
1004
    goto _OVER;
806,399✔
1005
  }
1006

1007
#if 1
1008
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
185,551,870✔
1009
#else
1010
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1011
  mTrace("mnode rpc is acquired, ref:%d", ref);
1012
#endif
1013

1014
  (void)taosThreadRwlockUnlock(&pMnode->lock);
185,555,805✔
1015
  TAOS_RETURN(code);
185,556,889✔
1016

1017
_OVER:
5,149,689✔
1018
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,149,689✔
1019
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,147,420✔
1020
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,145,549✔
1021
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,147,329✔
1022
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,144,976✔
1023
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,147,846✔
1024
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,146,479✔
1025
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,147,034✔
1026
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,146,184✔
1027
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
5,510✔
1028
           pMnode->stopped, state.restored, syncStr(state.state));
1029
    TAOS_RETURN(code);
5,510✔
1030
  }
1031

1032
  const STraceId *trace = &pMsg->info.traceId;
5,143,574✔
1033
  SEpSet          epSet = {0};
5,144,664✔
1034
  mndGetMnodeEpSet(pMnode, &epSet);
5,144,664✔
1035

1036
  mGDebug(
5,147,049✔
1037
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1038
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1039
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1040
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1041

1042
  if (epSet.numOfEps <= 0) return -1;
5,147,049✔
1043

1044
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
18,677,476✔
1045
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
13,530,427✔
1046
  }
1047

1048
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,147,049✔
1049
  pMsg->info.rsp = rpcMallocCont(contLen);
5,147,256✔
1050
  if (pMsg->info.rsp != NULL) {
5,147,256✔
1051
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,147,256✔
1052
      mError("failed to serialize ep set");
×
1053
    }
1054
    pMsg->info.hasEpSet = 1;
5,145,632✔
1055
    pMsg->info.rspLen = contLen;
5,145,632✔
1056
  }
1057

1058
  TAOS_RETURN(code);
5,145,632✔
1059
}
1060

1061
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
242,969,359✔
1062
  SMnode         *pMnode = pMsg->info.node;
242,969,359✔
1063
  const STraceId *trace = &pMsg->info.traceId;
242,970,053✔
1064
  int32_t         code = TSDB_CODE_SUCCESS;
242,946,005✔
1065

1066
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
242,946,005✔
1067
  MndMsgFpExt fpExt = NULL;
242,964,042✔
1068
  if (fp == NULL) {
242,964,042✔
1069
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,954,588✔
1070
    if (fpExt == NULL) {
20,954,942✔
1071
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1072
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1073
      TAOS_RETURN(code);
×
1074
    }
1075
  }
1076

1077
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
242,964,396✔
1078

1079
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
237,808,043✔
1080
  if (fp)
237,808,484✔
1081
    code = (*fp)(pMsg);
216,853,856✔
1082
  else
1083
    code = (*fpExt)(pMsg, pQueueInfo);
20,954,628✔
1084
  mndReleaseRpc(pMnode);
237,812,870✔
1085

1086
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
237,814,053✔
1087
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
33,038,064✔
1088
  } else if (code == 0) {
204,775,989✔
1089
    mGTrace("msg:%p, successfully processed", pMsg);
201,988,557✔
1090
  } else {
1091
    // TODO removve this wrong set code
1092
    if (code == -1) {
2,787,432✔
1093
      code = terrno;
3,211✔
1094
    }
1095
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,787,432✔
1096
            TMSG_INFO(pMsg->msgType));
1097
  }
1098

1099
  TAOS_RETURN(code);
237,814,053✔
1100
}
1101

1102
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
105,987,255✔
1103
  tmsg_t type = TMSG_INDEX(msgType);
105,987,255✔
1104
  if (type < TDMT_MAX) {
105,987,255✔
1105
    pMnode->msgFp[type] = fp;
105,987,255✔
1106
  }
1107
}
105,987,255✔
1108

1109
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,136,088✔
1110
  tmsg_t type = TMSG_INDEX(msgType);
4,136,088✔
1111
  if (type < TDMT_MAX) {
4,136,088✔
1112
    pMnode->msgFpExt[type] = fp;
4,136,088✔
1113
  }
1114
}
4,136,088✔
1115

1116
// Note: uid 0 is reserved
1117
int64_t mndGenerateUid(const char *name, int32_t len) {
8,972,220✔
1118
  int32_t hashval = MurmurHash3_32(name, len);
8,972,220✔
1119
  do {
×
1120
    int64_t us = taosGetTimestampUs();
8,972,220✔
1121
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
8,972,220✔
1122
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
8,972,220✔
1123
    if (uuid) {
8,972,220✔
1124
      return llabs(uuid);
8,972,220✔
1125
    }
1126
  } while (true);
1127
}
1128

1129
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
130✔
1130
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1131
  int32_t code = mndAcquireRpc(pMnode);
130✔
1132
  if (code < 0) {
130✔
1133
    TAOS_RETURN(code);
×
1134
  } else if (code == 1) {
130✔
1135
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1136
  }
1137

1138
  SSdb   *pSdb = pMnode->pSdb;
130✔
1139
  int64_t ms = taosGetTimestampMs();
130✔
1140

1141
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
130✔
1142
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
130✔
1143
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
130✔
1144
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
130✔
1145
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
130✔
1146
      pStbInfo->stbs == NULL) {
130✔
1147
    mndReleaseRpc(pMnode);
×
1148
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1149
    if (terrno != 0) code = terrno;
×
1150
    TAOS_RETURN(code);
×
1151
  }
1152

1153
  // cluster info
1154
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
130✔
1155
  pClusterInfo->monitor_interval = tsMonitorInterval;
130✔
1156
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
130✔
1157
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
130✔
1158
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
130✔
1159
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
130✔
1160
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
130✔
1161

1162
  void *pIter = NULL;
130✔
1163
  while (1) {
130✔
1164
    SDnodeObj *pObj = NULL;
260✔
1165
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
260✔
1166
    if (pIter == NULL) break;
260✔
1167

1168
    SMonDnodeDesc desc = {0};
130✔
1169
    desc.dnode_id = pObj->id;
130✔
1170
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
130✔
1171
    if (mndIsDnodeOnline(pObj, ms)) {
130✔
1172
      tstrncpy(desc.status, "ready", sizeof(desc.status));
130✔
1173
    } else {
1174
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1175
    }
1176
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
260✔
1177
      mError("failed put dnode into array, but continue at this monitor report")
×
1178
    }
1179
    sdbRelease(pSdb, pObj);
130✔
1180
  }
1181

1182
  pIter = NULL;
130✔
1183
  while (1) {
130✔
1184
    SMnodeObj *pObj = NULL;
260✔
1185
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
260✔
1186
    if (pIter == NULL) break;
260✔
1187

1188
    SMonMnodeDesc desc = {0};
130✔
1189
    desc.mnode_id = pObj->id;
130✔
1190
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
130✔
1191

1192
    if (pObj->id == pMnode->selfDnodeId) {
130✔
1193
      pClusterInfo->first_ep_dnode_id = pObj->id;
130✔
1194
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
130✔
1195
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1196
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
130✔
1197
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1198
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
130✔
1199
      desc.syncState = TAOS_SYNC_STATE_LEADER;
130✔
1200
    } else {
1201
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1202
      desc.syncState = pObj->syncState;
×
1203
    }
1204
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
260✔
1205
      mError("failed to put mnode into array, but continue at this monitor report");
×
1206
    }
1207
    sdbRelease(pSdb, pObj);
130✔
1208
  }
1209

1210
  // vgroup info
1211
  pIter = NULL;
130✔
1212
  while (1) {
260✔
1213
    SVgObj *pVgroup = NULL;
390✔
1214
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
390✔
1215
    if (pIter == NULL) break;
390✔
1216

1217
    if (pVgroup->mountVgId) {
260✔
1218
      sdbRelease(pSdb, pVgroup);
×
1219
      continue;
×
1220
    }
1221

1222
    pClusterInfo->vgroups_total++;
260✔
1223
    pClusterInfo->tbs_total += pVgroup->numOfTables;
260✔
1224

1225
    SMonVgroupDesc desc = {0};
260✔
1226
    desc.vgroup_id = pVgroup->vgId;
260✔
1227

1228
    SName name = {0};
260✔
1229
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
260✔
1230
    if (code < 0) {
260✔
1231
      mError("failed to get db name since %s", tstrerror(code));
×
1232
      sdbRelease(pSdb, pVgroup);
×
1233
      TAOS_RETURN(code);
×
1234
    }
1235
    (void)tNameGetDbName(&name, desc.database_name);
260✔
1236

1237
    desc.tables_num = pVgroup->numOfTables;
260✔
1238
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
260✔
1239
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
260✔
1240
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
520✔
1241
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
260✔
1242
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
260✔
1243
      pVnDesc->dnode_id = pVgid->dnodeId;
260✔
1244
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
260✔
1245
      pVnDesc->syncState = pVgid->syncState;
260✔
1246
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
260✔
1247
        tstrncpy(desc.status, "ready", sizeof(desc.status));
260✔
1248
        pClusterInfo->vgroups_alive++;
260✔
1249
      }
1250
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
260✔
1251
        pClusterInfo->vnodes_alive++;
260✔
1252
      }
1253
      pClusterInfo->vnodes_total++;
260✔
1254
    }
1255

1256
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
520✔
1257
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1258
    }
1259
    sdbRelease(pSdb, pVgroup);
260✔
1260
  }
1261

1262
  // stb info
1263
  pIter = NULL;
130✔
1264
  while (1) {
×
1265
    SStbObj *pStb = NULL;
130✔
1266
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
130✔
1267
    if (pIter == NULL) break;
130✔
1268

1269
    SMonStbDesc desc = {0};
×
1270

1271
    SName name1 = {0};
×
1272
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1273
    if (code < 0) {
×
1274
      mError("failed to get db name since %s", tstrerror(code));
×
1275
      sdbRelease(pSdb, pStb);
×
1276
      TAOS_RETURN(code);
×
1277
    }
1278
    (void)tNameGetDbName(&name1, desc.database_name);
×
1279

1280
    SName name2 = {0};
×
1281
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1282
    if (code < 0) {
×
1283
      mError("failed to get table name since %s", tstrerror(code));
×
1284
      sdbRelease(pSdb, pStb);
×
1285
      TAOS_RETURN(code);
×
1286
    }
1287
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1288

1289
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1290
      mError("failed to put stb into array, but continue at this monitor report");
×
1291
    }
1292
    sdbRelease(pSdb, pStb);
×
1293
  }
1294

1295
  // grant info
1296
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
130✔
1297
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
130✔
1298
  if (pMnode->grant.expireTimeMS == 0) {
130✔
1299
    pGrantInfo->expire_time = 0;
×
1300
    pGrantInfo->timeseries_total = 0;
×
1301
  }
1302

1303
  mndReleaseRpc(pMnode);
130✔
1304
  TAOS_RETURN(code);
130✔
1305
}
1306

1307
int32_t mndResetTimer(SMnode *pMnode){
×
1308
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1309
}
1310

1311
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
25,369,107✔
1312
  mTrace("mnode get load");
25,369,107✔
1313
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
25,369,107✔
1314
  pLoad->syncState = state.state;
25,369,107✔
1315
  pLoad->syncRestore = state.restored;
25,369,107✔
1316
  pLoad->syncTerm = state.term;
25,369,107✔
1317
  pLoad->roleTimeMs = state.roleTimeMs;
25,369,107✔
1318
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
25,369,107✔
1319
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1320
  return 0;
25,369,107✔
1321
}
1322

1323
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,519,887✔
1324
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,519,887✔
1325
  return state.roleTimeMs;
7,519,887✔
1326
}
1327

1328
void mndSetRestored(SMnode *pMnode, bool restored) {
516,891✔
1329
  if (restored) {
516,891✔
1330
    (void)taosThreadRwlockWrlock(&pMnode->lock);
516,891✔
1331
    pMnode->restored = true;
516,891✔
1332
    (void)taosThreadRwlockUnlock(&pMnode->lock);
516,891✔
1333
    mInfo("mnode set restored:%d", restored);
516,891✔
1334
  } else {
1335
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1336
    pMnode->restored = false;
×
1337
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1338
    mInfo("mnode set restored:%d", restored);
×
1339
    while (1) {
1340
      if (pMnode->rpcRef <= 0) break;
×
1341
      taosMsleep(3);
×
1342
    }
1343
  }
1344
}
516,891✔
1345

1346
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1347

1348
void mndSetStop(SMnode *pMnode) {
516,891✔
1349
  (void)taosThreadRwlockWrlock(&pMnode->lock);
516,891✔
1350
  pMnode->stopped = true;
516,891✔
1351
  (void)taosThreadRwlockUnlock(&pMnode->lock);
516,891✔
1352
  mInfo("mnode set stopped");
516,891✔
1353
}
516,891✔
1354

1355
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
511,616,550✔
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