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

taosdata / TDengine / #5035

24 Apr 2026 11:25AM UTC coverage: 73.06% (+0.002%) from 73.058%
#5035

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1344 of 1975 new or added lines in 48 files covered. (68.05%)

14127 existing lines in 142 files now uncovered.

275902 of 377640 relevant lines covered (73.06%)

132208813.58 hits per line

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

80.62
/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 "mndRole.h"
44
#include "mndRsma.h"
45
#include "mndScan.h"
46
#include "mndScanDetail.h"
47
#include "mndSecurityPolicy.h"
48
#include "mndShow.h"
49
#include "mndSma.h"
50
#include "mndSnode.h"
51
#include "mndSsMigrate.h"
52
#include "mndStb.h"
53
#include "mndStream.h"
54
#include "mndSubscribe.h"
55
#include "mndSync.h"
56
#include "mndTelem.h"
57
#include "mndToken.h"
58
#include "mndTopic.h"
59
#include "mndTrans.h"
60
#include "mndUser.h"
61
#include "mndVgroup.h"
62
#include "mndView.h"
63
#include "mndXnode.h"
64
#include "tencrypt.h"
65

66
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
6,407,522✔
67
  int32_t code = 0;
6,407,522✔
68
  (void)taosThreadRwlockRdlock(&pMnode->lock);
6,407,522✔
69
  if (pMnode->stopped) {
6,407,522✔
UNCOV
70
    code = TSDB_CODE_APP_IS_STOPPING;
×
71
  } else if (!mndIsLeader(pMnode)) {
6,407,522✔
UNCOV
72
    code = 1;
×
73
  } else {
74
#if 1
75
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
6,407,522✔
76
#else
77
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
78
    mTrace("mnode rpc is acquired, ref:%d", ref);
79
#endif
80
  }
81
  (void)taosThreadRwlockUnlock(&pMnode->lock);
6,407,522✔
82
  TAOS_RETURN(code);
6,407,522✔
83
}
84

85
static inline void mndReleaseRpc(SMnode *pMnode) {
328,488,689✔
86
  (void)taosThreadRwlockRdlock(&pMnode->lock);
328,488,689✔
87
#if 1
88
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
328,491,090✔
89
#else
90
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
91
  mTrace("mnode rpc is released, ref:%d", ref);
92
#endif
93
  (void)taosThreadRwlockUnlock(&pMnode->lock);
328,490,456✔
94
}
328,490,527✔
95

96
static void *mndBuildTimerMsg(int32_t *pContLen) {
83,788,430✔
97
  terrno = 0;
83,788,430✔
98
  SMTimerReq timerReq = {0};
83,788,430✔
99

100
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
83,788,430✔
101
  if (contLen <= 0) return NULL;
83,788,237✔
102
  void *pReq = rpcMallocCont(contLen);
83,788,237✔
103
  if (pReq == NULL) return NULL;
83,788,237✔
104

105
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
83,788,237✔
UNCOV
106
    mError("failed to serialize timer msg since %s", terrstr());
×
107
  }
108
  *pContLen = contLen;
83,788,430✔
109
  return pReq;
83,788,237✔
110
}
111

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

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

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

151
static void mndPullupInstances(SMnode *pMnode) {
6,764,442✔
152
  mTrace("pullup instance timer msg");
6,764,442✔
153
  int32_t contLen = 0;
6,764,442✔
154
  void   *pReq = mndBuildTimerMsg(&contLen);
6,764,442✔
155
  if (pReq != NULL) {
6,764,442✔
156
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_INSTANCE_TIMER, .pCont = pReq, .contLen = contLen};
6,764,442✔
157
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
6,764,442✔
UNCOV
158
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
159
    }
160
  }
161
}
6,764,442✔
162

163
static void mndPullupTtl(SMnode *pMnode) {
3,522,712✔
164
  mTrace("pullup ttl");
3,522,712✔
165
  int32_t contLen = 0;
3,522,712✔
166
  void   *pReq = mndBuildTimerMsg(&contLen);
3,522,712✔
167
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
3,522,712✔
168
  // TODO check return value
169
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,522,712✔
UNCOV
170
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
171
  }
172
}
3,522,712✔
173

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

185
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,587,682✔
186
  mTrace("pullup trim query");
3,587,682✔
187
  int32_t contLen = 0;
3,587,682✔
188
  void   *pReq = mndBuildTimerMsg(&contLen);
3,587,682✔
189
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,587,682✔
190
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,587,682✔
UNCOV
191
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
192
  }
193
}
3,587,682✔
194

UNCOV
195
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
196
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
197
    return;
×
198
  }
199

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

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

219
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
16,352,535✔
220
  mTrace("pullup arb hb");
16,352,535✔
221
  int32_t contLen = 0;
16,352,535✔
222
  void   *pReq = mndBuildTimerMsg(&contLen);
16,352,535✔
223
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
16,352,535✔
224
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
16,352,535✔
225
}
226

227
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
10,834,147✔
228
  mTrace("pullup arb sync");
10,834,147✔
229
  int32_t contLen = 0;
10,834,147✔
230
  void   *pReq = mndBuildTimerMsg(&contLen);
10,834,147✔
231
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
10,834,147✔
232
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
10,834,147✔
233
}
234

235
static void mndCalMqRebalance(SMnode *pMnode) {
16,932,405✔
236
  int32_t contLen = 0;
16,932,405✔
237
  void   *pReq = mndBuildTimerMsg(&contLen);
16,932,405✔
238
  if (pReq != NULL) {
16,932,405✔
239
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
16,932,405✔
240
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
16,932,405✔
UNCOV
241
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
242
    }
243
  }
244
}
16,932,405✔
245

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

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

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

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

306
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
125,699✔
307
  SSdb *pSdb = pMnode->pSdb;
125,699✔
308

309
  void *pIter = NULL;
125,699✔
310
  while (1) {
467,132✔
311
    SVgObj *pVgroup = NULL;
592,831✔
312
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
592,831✔
313
    if (pIter == NULL) break;
592,831✔
314

315
    bool stateChanged = false;
467,132✔
316
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,181,720✔
317
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
901,300✔
318
      if (pGid->dnodeId == dnodeId) {
901,300✔
319
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
186,712✔
320
          mInfo(
74,878✔
321
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
322
              "restored:0 "
323
              "canRead:0",
324
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
325
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
74,878✔
326
          pGid->syncRestore = 0;
74,878✔
327
          pGid->syncCanRead = 0;
74,878✔
328
          pGid->startTimeMs = 0;
74,878✔
329
          pGid->learnerProgress = 0;
74,878✔
330
          pGid->snapSeq = -1;
74,878✔
331
          stateChanged = true;
74,878✔
332
        }
333
        break;
186,712✔
334
      }
335
    }
336

337
    if (stateChanged) {
467,132✔
338
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
74,878✔
339
      if (pDb != NULL && pDb->stateTs != curMs) {
74,878✔
340
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
42,336✔
341
              curMs);
342
        pDb->stateTs = curMs;
42,336✔
343
      }
344
      mndReleaseDb(pMnode, pDb);
74,878✔
345
    }
346

347
    sdbRelease(pSdb, pVgroup);
467,132✔
348
  }
349
}
125,699✔
350

351
static void mndCheckDnodeOffline(SMnode *pMnode) {
6,407,448✔
352
  mTrace("check dnode offline");
6,407,448✔
353
  if (mndAcquireRpc(pMnode) != 0) return;
6,407,448✔
354

355
  SSdb   *pSdb = pMnode->pSdb;
6,407,448✔
356
  int64_t curMs = taosGetTimestampMs();
6,407,448✔
357

358
  void *pIter = NULL;
6,407,448✔
359
  while (1) {
10,656,702✔
360
    SDnodeObj *pDnode = NULL;
17,064,150✔
361
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
17,064,150✔
362
    if (pIter == NULL) break;
17,064,150✔
363

364
    bool online = mndIsDnodeOnline(pDnode, curMs);
10,656,702✔
365
    if (!online) {
10,656,702✔
366
      mInfo("dnode:%d, in offline state", pDnode->id);
125,699✔
367
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
125,699✔
368
    }
369

370
    sdbRelease(pSdb, pDnode);
10,656,702✔
371
  }
372

373
  mndReleaseRpc(pMnode);
6,407,448✔
374
}
375

376
static bool mnodeIsNotLeader(SMnode *pMnode) {
378,716,832✔
377
  terrno = 0;
378,716,832✔
378
  (void)taosThreadRwlockRdlock(&pMnode->lock);
378,716,395✔
379
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
378,717,751✔
380
  if (terrno != 0) {
378,717,751✔
UNCOV
381
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
382
    return true;
×
383
  }
384

385
  if (state.state != TAOS_SYNC_STATE_LEADER) {
378,717,107✔
386
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13,503,902✔
387
    terrno = TSDB_CODE_SYN_NOT_LEADER;
13,503,902✔
388
    return true;
13,503,902✔
389
  }
390
  if (!state.restored || !pMnode->restored) {
365,213,205✔
391
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,212✔
392
    terrno = TSDB_CODE_SYN_RESTORING;
5,797✔
393
    return true;
5,797✔
394
  }
395
  (void)taosThreadRwlockUnlock(&pMnode->lock);
365,206,993✔
396
  return false;
365,206,659✔
397
}
398

UNCOV
399
static int32_t minCronTime() {
×
400
  int32_t min = INT32_MAX;
×
401
  min = TMIN(min, tsTtlPushIntervalSec);
×
402
  min = TMIN(min, tsTrimVDbIntervalSec);
×
403
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
404
  min = TMIN(min, tsTransPullupInterval);
×
405
  min = TMIN(min, tsCompactPullupInterval);
×
406
  min = TMIN(min, tsMqRebalanceInterval);
×
407

UNCOV
408
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
409
  min = TMIN(min, telemInt);
×
410
  min = TMIN(min, tsGrantHBInterval);
×
411
  min = TMIN(min, tsUptimeInterval);
×
412

UNCOV
413
  return min <= 1 ? 2 : min;
×
414
}
415
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
33,832,477✔
416
  int32_t code = 0;
33,832,477✔
417
#ifndef TD_ASTRA
418
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
33,832,477✔
419
    mndPullupGrant(pMnode);
1,829,738✔
420
  }
421
  if (sec % tsTtlPushIntervalSec == 0) {
33,832,477✔
422
    mndPullupTtl(pMnode);
3,522,712✔
423
  }
424

425
  if (sec % tsTrimVDbIntervalSec == 0) {
33,832,477✔
426
    mndPullupTrimDb(pMnode);
7,988✔
427
  }
428

429
  if (sec % tsQueryTrimIntervalSec == 0) {
33,832,477✔
430
    mndPullupQueryTrimDb(pMnode);
3,587,682✔
431
  }
432
#endif
433
#ifdef USE_SHARED_STORAGE
434
  if (tsSsEnabled) {
33,832,477✔
435
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
241,916✔
436
      mndPullupUpdateSsMigrateProgress(pMnode);
176,512✔
437
    }
438
    if (tsSsEnabled == 2) {
241,916✔
439
      // By default, both tsTrimVDbIntervalSec and tsSsAutoMigrateIntervalSec are 3600 seconds,
440
      // so, delay half interval to do ss migrate to avoid conflict.
441
      //
442
      // NOTE: this solution is not perfect, there could still be conflict if user changes the
443
      // default value, but it is good enough as user is unlikely to change the default value.
444
      // The best solution is adding a new offset config to all cron tasks, but that would add
445
      // extra complexity.
UNCOV
446
      if ((sec % tsSsAutoMigrateIntervalSec) == (tsSsAutoMigrateIntervalSec / 2)) {
×
447
        mndPullupSsMigrateDb(pMnode);
×
448
      }
449
    }
450
  }
451
#endif
452
#ifdef TD_ENTERPRISE
453
  if (tsAuthReq) {
33,832,477✔
UNCOV
454
    if (sec % tsAuthReqHBInterval == 0) {
×
455
      mndPullupAuth(pMnode);
×
456
    }
457
  }
458
#endif
459
  if (sec % tsTransPullupInterval == 0) {
33,832,477✔
460
    mndPullupTrans(pMnode);
16,932,353✔
461
  }
462

463
  if (sec % tsCompactPullupInterval == 0) {
33,832,477✔
464
    mndPullupCompacts(pMnode);
3,373,175✔
465
  }
466

467
  if (sec % tsScanPullupInterval == 0) {
33,832,477✔
468
    mndPullupScans(pMnode);
3,373,346✔
469
  }
470
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
33,832,477✔
471
    mndPullupInstances(pMnode);
6,764,442✔
472
  }
473
#ifdef USE_TOPIC
474
  if (sec % tsMqRebalanceInterval == 0) {
33,832,477✔
475
    mndCalMqRebalance(pMnode);
16,932,405✔
476
  }
477
#endif
478
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
33,832,477✔
479
    mndPullupTelem(pMnode);
222✔
480
  }
481
  if (sec % tsUptimeInterval == 0) {
33,832,477✔
482
    mndIncreaseUpTime(pMnode);
101,173✔
483
  }
484
}
33,832,477✔
485

486
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
331,375,575✔
487
  int32_t code = 0;
331,375,575✔
488
#ifndef TD_ASTRA
489
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
331,375,575✔
490
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
16,352,535✔
UNCOV
491
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
492
    }
493
  }
494

495
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
331,375,575✔
496
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
10,834,147✔
UNCOV
497
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
498
    }
499
  }
500
#endif
501
}
331,375,575✔
502

503
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
331,375,575✔
504
  if (ms % (tsStatusTimeoutMs) == 0) {
331,375,575✔
505
    mndCheckDnodeOffline(pMnode);
6,407,448✔
506
  }
507
}
331,375,575✔
508

509
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
33,832,477✔
510
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
33,832,477✔
511
    mndSyncCheckTimeout(pMnode);
1,133,058✔
512
  }
513
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
33,832,477✔
514
    msmHealthCheck(pMnode);
11,271,228✔
515
  }
516
}
33,832,477✔
517

518
static void *mndThreadSecFp(void *param) {
495,860✔
519
  SMnode *pMnode = param;
495,860✔
520
  int64_t lastSec = 0;
495,860✔
521
  setThreadName("mnode-timer");
495,860✔
522

523
  while (1) {
344,144,057✔
524
    if (mndGetStop(pMnode)) break;
344,639,917✔
525

526
    int64_t nowSec = taosGetTimestampMs() / 1000;
344,144,057✔
527
    if (nowSec == lastSec) {
344,144,057✔
528
      taosMsleep(100);
309,031,310✔
529
      continue;
309,031,310✔
530
    }
531
    lastSec = nowSec;
35,112,747✔
532

533
    if (mnodeIsNotLeader(pMnode)) {
35,112,747✔
534
      taosMsleep(100);
1,280,270✔
535
      mTrace("timer not process since mnode is not leader");
1,280,270✔
536
      continue;
1,280,270✔
537
    }
538

539
    mndDoTimerCheckSync(pMnode, nowSec);
33,832,477✔
540

541
    mndDoTimerPullupTask(pMnode, nowSec);
33,832,477✔
542

543
    taosMsleep(100);
33,832,477✔
544
  }
545

546
  return NULL;
495,860✔
547
}
548

549
static void *mndThreadMsFp(void *param) {
495,860✔
550
  SMnode *pMnode = param;
495,860✔
551
  int64_t lastTime = 0;
495,860✔
552
  setThreadName("mnode-arb-timer");
495,860✔
553

554
  while (1) {
555
    lastTime += 100;
344,100,864✔
556
    taosMsleep(100);
344,100,864✔
557

558
    if (mndGetStop(pMnode)) break;
344,100,864✔
559
    if (lastTime % 10 != 0) continue;
343,605,004✔
560

561
    if (mnodeIsNotLeader(pMnode)) {
343,605,004✔
562
      mTrace("timer not process since mnode is not leader");
12,229,429✔
563
      continue;
12,229,429✔
564
    }
565

566
    mndDoTimerCheckStatus(pMnode, lastTime);
331,375,575✔
567

568
    mndDoArbTimerPullupTask(pMnode, lastTime);
331,375,575✔
569
  }
570

571
  return NULL;
495,860✔
572
}
573

574
static int32_t mndInitTimer(SMnode *pMnode) {
495,860✔
575
  int32_t      code = 0;
495,860✔
576
  TdThreadAttr thAttr;
494,073✔
577
  (void)taosThreadAttrInit(&thAttr);
495,860✔
578
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
495,860✔
579
#ifdef TD_COMPACT_OS
580
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
581
#endif
582
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
495,860✔
UNCOV
583
    mError("failed to create timer thread since %s", tstrerror(code));
×
584
    TAOS_RETURN(code);
×
585
  }
586

587
  (void)taosThreadAttrDestroy(&thAttr);
495,860✔
588
  tmsgReportStartup("mnode-timer", "initialized");
495,860✔
589

590
  TdThreadAttr arbAttr;
494,073✔
591
  (void)taosThreadAttrInit(&arbAttr);
495,860✔
592
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
495,860✔
593
#ifdef TD_COMPACT_OS
594
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
595
#endif
596
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
495,860✔
UNCOV
597
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
598
    TAOS_RETURN(code);
×
599
  }
600

601
  (void)taosThreadAttrDestroy(&arbAttr);
495,860✔
602
  tmsgReportStartup("mnode-timer", "initialized");
495,860✔
603
  TAOS_RETURN(code);
495,860✔
604
}
605

606
static void mndCleanupTimer(SMnode *pMnode) {
495,860✔
607
  if (taosCheckPthreadValid(pMnode->thread)) {
495,860✔
608
    (void)taosThreadJoin(pMnode->thread, NULL);
495,860✔
609
    taosThreadClear(&pMnode->thread);
495,860✔
610
  }
611
  if (taosCheckPthreadValid(pMnode->arbThread)) {
495,860✔
612
    (void)taosThreadJoin(pMnode->arbThread, NULL);
495,860✔
613
    taosThreadClear(&pMnode->arbThread);
495,860✔
614
  }
615
}
495,860✔
616

617
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
496,017✔
618
  int32_t code = 0;
496,017✔
619
  pMnode->path = taosStrdup(path);
496,017✔
620
  if (pMnode->path == NULL) {
496,017✔
UNCOV
621
    code = terrno;
×
622
    TAOS_RETURN(code);
×
623
  }
624

625
  if (taosMkDir(pMnode->path) != 0) {
496,017✔
UNCOV
626
    code = terrno;
×
627
    TAOS_RETURN(code);
×
628
  }
629

630
  TAOS_RETURN(code);
496,017✔
631
}
632

633
static int32_t mndInitWal(SMnode *pMnode) {
496,017✔
634
  int32_t code = 0;
496,017✔
635
  char    path[PATH_MAX + 20] = {0};
496,017✔
636
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
496,017✔
637
  SWalCfg cfg = {.vgId = 1,
496,017✔
638
                 .fsyncPeriod = 0,
639
                 .rollPeriod = -1,
640
                 .segSize = -1,
641
                 .committed = -1,
642
                 .retentionPeriod = 0,
643
                 .retentionSize = 0,
644
                 .level = TAOS_WAL_FSYNC,
645
                 .encryptAlgr = 0,
646
                 .encryptData = {0}};
647

648
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
649
  if (taosWaitCfgKeyLoaded() != 0) {
496,017✔
UNCOV
650
    code = terrno;
×
651
    TAOS_RETURN(code);
×
652
  }
653
  if (tsMetaKey[0] != '\0') {
496,017✔
654
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
5,520✔
655
  }
656
#endif
657

658
  pMnode->pWal = walOpen(path, &cfg);
496,017✔
659
  if (pMnode->pWal == NULL) {
496,017✔
UNCOV
660
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
661
    if (terrno != 0) code = terrno;
×
662
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
663
    TAOS_RETURN(code);
×
664
  }
665

666
  TAOS_RETURN(code);
496,017✔
667
}
668

669
static void mndCloseWal(SMnode *pMnode) {
495,948✔
670
  if (pMnode->pWal != NULL) {
495,948✔
671
    walClose(pMnode->pWal);
495,948✔
672
    pMnode->pWal = NULL;
495,948✔
673
  }
674
}
495,948✔
675

676
// Forward declarations for mmFile.c functions
677
extern int32_t mmReadFile(const char *path, SMnodeOpt *pOption);
678
extern int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption);
679

680
// Callback function to persist encrypted flag to mnode.json
681
static int32_t mndPersistEncryptedFlag(void *param) {
7,799✔
682
  SMnode *pMnode = (SMnode *)param;
7,799✔
683
  if (pMnode == NULL) {
7,799✔
UNCOV
684
    return TSDB_CODE_INVALID_PARA;
×
685
  }
686
  
687
  mInfo("persisting encrypted flag to mnode.json");
7,799✔
688
  
689
  SMnodeOpt option = {0};
7,799✔
690
  int32_t code = mmReadFile(pMnode->path, &option);
7,799✔
691
  if (code != 0) {
7,799✔
UNCOV
692
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
693
    return code;
×
694
  }
695
  
696
  option.encrypted = true;
7,799✔
697
  code = mmWriteFile(pMnode->path, &option);
7,799✔
698
  if (code != 0) {
7,799✔
UNCOV
699
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
700
    return code;
×
701
  }
702
  
703
  // Also update mnode's encrypted flag
704
  pMnode->encrypted = true;
7,799✔
705
  
706
  mInfo("successfully persisted encrypted flag to mnode.json");
7,799✔
707
  return 0;
7,799✔
708
}
709

710
static int32_t mndInitSdb(SMnode *pMnode) {
496,017✔
711
  int32_t code = 0;
496,017✔
712
  SSdbOpt opt = {0};
496,017✔
713
  opt.path = pMnode->path;
496,017✔
714
  opt.pMnode = pMnode;
496,017✔
715
  opt.pWal = pMnode->pWal;
496,017✔
716

717
  pMnode->pSdb = sdbInit(&opt);
496,017✔
718
  if (pMnode->pSdb == NULL) {
496,017✔
UNCOV
719
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
720
    if (terrno != 0) code = terrno;
×
721
    TAOS_RETURN(code);
×
722
  }
723

724
  TAOS_RETURN(code);
496,017✔
725
}
726

727
static int32_t mndOpenSdb(SMnode *pMnode) {
496,017✔
728
  int32_t code = 0;
496,017✔
729
  
730
  pMnode->pSdb->encrypted = pMnode->encrypted;
496,017✔
731
  
732
  // Set callback for persisting encrypted flag
733
  pMnode->pSdb->persistEncryptedFlagFp = mndPersistEncryptedFlag;
496,017✔
734
  pMnode->pSdb->pMnodeForCallback = pMnode;
496,017✔
735

736
  if (!pMnode->deploy) {
496,017✔
737
    code = sdbReadFile(pMnode->pSdb);
139,474✔
738
  }
739

740
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
496,017✔
741

742
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
496,017✔
743
  return code;
496,017✔
744
}
745

746
static void mndCleanupSdb(SMnode *pMnode) {
495,948✔
747
  if (pMnode->pSdb) {
495,948✔
748
    sdbCleanup(pMnode->pSdb);
495,948✔
749
    pMnode->pSdb = NULL;
495,948✔
750
  }
751
}
495,948✔
752

753
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
25,296,867✔
754
  SMnodeStep step = {0};
25,296,867✔
755
  step.name = name;
25,296,867✔
756
  step.initFp = initFp;
25,296,867✔
757
  step.cleanupFp = cleanupFp;
25,296,867✔
758
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
50,593,734✔
UNCOV
759
    TAOS_RETURN(terrno);
×
760
  }
761

762
  TAOS_RETURN(0);
25,296,867✔
763
}
764

765
static int32_t mndInitSteps(SMnode *pMnode) {
496,017✔
766
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
496,017✔
767
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
496,017✔
768
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
496,017✔
769
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
496,017✔
770
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-security-policy", mndInitSecurityPolicy, mndCleanupSecurityPolicy));
496,017✔
771
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-encrypt-algorithms", mndInitEncryptAlgr, mndCleanupEncryptAlgr));
496,017✔
772
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
496,017✔
773
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
496,017✔
774
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
496,017✔
775
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
496,017✔
776
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
496,017✔
777
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-xnode", mndInitXnode, mndCleanupXnode));
496,017✔
778
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
496,017✔
779
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
496,017✔
780
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
496,017✔
781
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-role", mndInitRole, mndCleanupRole));
496,017✔
782
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
496,017✔
783
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-token", mndInitToken, mndCleanupToken));
496,017✔
784
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
496,017✔
785
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
496,017✔
786
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
496,017✔
787
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
496,017✔
788
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-instance", mndInitInstance, mndCleanupInstance));
496,017✔
789
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
496,017✔
790
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
496,017✔
791
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
496,017✔
792
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
496,017✔
793
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
496,017✔
794
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
496,017✔
795
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
496,017✔
796
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
496,017✔
797
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
496,017✔
798
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
496,017✔
799
#ifdef USE_MOUNT
800
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
496,017✔
801
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
496,017✔
802
#endif
803
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
496,017✔
804
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
496,017✔
805
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
496,017✔
806
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
496,017✔
807
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
496,017✔
808
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
496,017✔
809
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
496,017✔
810
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
496,017✔
811
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
496,017✔
812
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
496,017✔
813
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
496,017✔
814
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
496,017✔
815
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
496,017✔
816
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
496,017✔
817
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
496,017✔
818
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
496,017✔
819
  return 0;
496,017✔
820
}
821

822
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
495,948✔
823
  if (pMnode->pSteps == NULL) return;
495,948✔
824

825
  if (pos == -1) {
495,948✔
826
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
495,948✔
827
  }
828

829
  for (int32_t s = pos; s >= 0; s--) {
25,789,296✔
830
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
25,293,348✔
831
    mInfo("%s will cleanup", pStep->name);
25,293,348✔
832
    if (pStep->cleanupFp != NULL) {
25,293,348✔
833
      (*pStep->cleanupFp)(pMnode);
24,301,452✔
834
    }
835
  }
836

837
  taosArrayClear(pMnode->pSteps);
495,948✔
838
  taosArrayDestroy(pMnode->pSteps);
495,948✔
839
  pMnode->pSteps = NULL;
495,948✔
840
}
841

842
static int32_t mndExecSteps(SMnode *pMnode) {
496,017✔
843
  int32_t code = 0;
496,017✔
844
  int32_t size = taosArrayGetSize(pMnode->pSteps);
496,017✔
845
  for (int32_t pos = 0; pos < size; pos++) {
25,792,884✔
846
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
25,296,867✔
847
    if (pStep->initFp == NULL) continue;
25,296,867✔
848

849
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
25,296,867✔
UNCOV
850
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
UNCOV
851
      mndCleanupSteps(pMnode, pos);
×
852
      TAOS_RETURN(code);
×
853
    } else {
854
      mInfo("%s is initialized", pStep->name);
25,296,867✔
855
      tmsgReportStartup(pStep->name, "initialized");
25,296,867✔
856
    }
857
  }
858

859
  pMnode->clusterId = mndGetClusterId(pMnode);
496,017✔
860
  TAOS_RETURN(0);
496,017✔
861
}
862

863
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
496,017✔
864
  pMnode->msgCb = pOption->msgCb;
496,017✔
865
  pMnode->selfDnodeId = pOption->dnodeId;
496,017✔
866
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
496,017✔
867
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
496,017✔
868
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
496,017✔
869
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
496,017✔
870
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
496,017✔
871
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
496,017✔
872
  pMnode->encrypted = pOption->encrypted;
496,017✔
873
}
496,017✔
874

875
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
496,017✔
876
  terrno = 0;
496,017✔
877
  mInfo("start to open mnode in %s", path);
496,017✔
878

879
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
496,017✔
880
  if (pMnode == NULL) {
496,017✔
UNCOV
881
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
882
    mError("failed to open mnode in step 1, since %s", terrstr());
×
883
    return NULL;
×
884
  }
885
  (void)memset(pMnode, 0, sizeof(SMnode));
496,017✔
886

887
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
496,017✔
888
  if (code != 0) {
496,017✔
UNCOV
889
    taosMemoryFree(pMnode);
×
UNCOV
890
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
891
    terrno = code;
×
892
    return NULL;
×
893
  }
894

895
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
496,017✔
896
        pOption->numOfTotalReplicas);
897
  mndSetOptions(pMnode, pOption);
496,017✔
898

899
  pMnode->deploy = pOption->deploy;
496,017✔
900
  pMnode->version = pOption->version;
496,017✔
901
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
496,017✔
902
  if (pMnode->pSteps == NULL) {
496,017✔
UNCOV
903
    taosMemoryFree(pMnode);
×
UNCOV
904
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
905
    mError("failed to open mnode in step 4, since %s", terrstr());
×
906
    return NULL;
×
907
  }
908

909
  code = mndCreateDir(pMnode, path);
496,017✔
910
  if (code != 0) {
496,017✔
UNCOV
911
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
UNCOV
912
    mndClose(pMnode);
×
913
    terrno = code;
×
914
    return NULL;
×
915
  }
916

917
  code = mndInitSteps(pMnode);
496,017✔
918
  if (code != 0) {
496,017✔
UNCOV
919
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
UNCOV
920
    mndClose(pMnode);
×
921
    terrno = code;
×
922
    return NULL;
×
923
  }
924

925
  code = mndExecSteps(pMnode);
496,017✔
926
  if (code != 0) {
496,017✔
UNCOV
927
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
UNCOV
928
    mndClose(pMnode);
×
929
    terrno = code;
×
930
    return NULL;
×
931
  }
932

933
  mInfo("mnode open successfully");
496,017✔
934
  return pMnode;
496,017✔
935
}
936

937
void mndPreClose(SMnode *pMnode) {
495,860✔
938
  if (pMnode != NULL) {
495,860✔
939
    int32_t code = 0;
495,860✔
940
    // TODO check return value
941
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
495,860✔
942
    if (code < 0) {
495,860✔
UNCOV
943
      mError("failed to transfer leader since %s", tstrerror(code));
×
944
    }
945
    syncPreStop(pMnode->syncMgmt.sync);
495,860✔
946
    code = sdbWriteFile(pMnode->pSdb, 0);
495,860✔
947
    if (code < 0) {
495,860✔
948
      mError("failed to write sdb since %s", tstrerror(code));
773✔
949
    }
950
  }
951
}
495,860✔
952

953
void mndClose(SMnode *pMnode) {
495,948✔
954
  if (pMnode != NULL) {
495,948✔
955
    mInfo("start to close mnode");
495,948✔
956
    mndCleanupSteps(pMnode, -1);
495,948✔
957
    taosMemoryFreeClear(pMnode->path);
495,948✔
958
    taosMemoryFreeClear(pMnode);
495,948✔
959
    mInfo("mnode is closed");
495,948✔
960
  }
961
}
495,948✔
962

963
int32_t mndStart(SMnode *pMnode) {
495,860✔
964
  int32_t code = 0;
495,860✔
965
  mndSyncStart(pMnode);
495,860✔
966
  if (pMnode->deploy) {
495,860✔
967
    if (sdbDeploy(pMnode->pSdb) != 0) {
356,543✔
UNCOV
968
      mError("failed to deploy sdb while start mnode");
×
UNCOV
969
      return -1;
×
970
    }
971
    mndSetRestored(pMnode, true);
356,543✔
972
  }
973
  if (mndIsLeader(pMnode)) {
495,860✔
974
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
447,336✔
UNCOV
975
      mError("failed to upgrade sdb while start mnode");
×
UNCOV
976
      return -1;
×
977
    }
978
#ifdef TD_ENTERPRISE
979
    if (tsSodEnforceMode) {
447,336✔
NEW
980
      if ((code = mndProcessEnforceSod(pMnode)) != 0) {
×
NEW
981
        if (code == TSDB_CODE_MND_ROLE_NO_VALID_SYSDBA || code == TSDB_CODE_MND_ROLE_NO_VALID_SYSSEC ||
×
982
            code == TSDB_CODE_MND_ROLE_NO_VALID_SYSAUDIT) {
NEW
983
          mInfo("enter SoD pending mode. Enforce SoD by command line failed since %s", tstrerror(code));
×
NEW
984
        } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
NEW
985
          mInfo("enter SoD pending mode. Enforce SoD is in progress");
×
986
        } else {
NEW
987
          mError("failed to enforce SoD by command line since %s", tstrerror(code));
×
NEW
988
          TAOS_RETURN(code);
×
989
        }
990
      } else {
NEW
991
        mndSetSoDPhase(pMnode, TSDB_SOD_PHASE_STABLE);
×
992
      }
993
    }
994
#endif
995
  }
996
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
495,860✔
997
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
495,860✔
998

999
  return mndInitTimer(pMnode);
495,860✔
1000
}
1001

1002
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
495,860✔
1003

1004
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
390,847✔
1005

1006
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
390,847✔
1007

1008
int32_t mndIsCatchUp(SMnode *pMnode) {
195,179✔
1009
  int64_t rid = pMnode->syncMgmt.sync;
195,179✔
1010
  return syncIsCatchUp(rid);
195,179✔
1011
}
1012

1013
ESyncRole mndGetRole(SMnode *pMnode) {
195,179✔
1014
  int64_t rid = pMnode->syncMgmt.sync;
195,179✔
1015
  return syncGetRole(rid);
195,179✔
1016
}
1017

1018
int64_t mndGetTerm(SMnode *pMnode) {
10,959,049✔
1019
  int64_t rid = pMnode->syncMgmt.sync;
10,959,049✔
1020
  return syncGetTerm(rid);
10,959,049✔
1021
}
1022

1023
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
27,314,694✔
1024

1025
void mndStop(SMnode *pMnode) {
495,860✔
1026
  mndSetStop(pMnode);
495,860✔
1027
  mndSyncStop(pMnode);
495,860✔
1028
  mndCleanupTimer(pMnode);
495,860✔
1029
}
495,860✔
1030

1031
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
64,677,984✔
1032
  SMnode    *pMnode = pMsg->info.node;
64,677,984✔
1033
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
64,677,984✔
1034

1035
  const STraceId *trace = &pMsg->info.traceId;
64,677,984✔
1036
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
64,677,984✔
1037

1038
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
64,677,984✔
1039
  if (code != 0) {
64,677,984✔
1040
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
633✔
1041
            tstrerror(code), code);
1042
  }
1043

1044
  return code;
64,677,984✔
1045
}
1046

1047
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
324,690,779✔
1048
  int32_t code = 0;
324,690,779✔
1049
  if (!IsReq(pMsg)) TAOS_RETURN(code);
324,690,779✔
1050
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
285,872,425✔
1051
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
279,815,620✔
1052
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
274,715,675✔
1053
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
262,758,389✔
1054
    TAOS_RETURN(code);
23,113,811✔
1055
  }
1056

1057
  SMnode *pMnode = pMsg->info.node;
262,761,360✔
1058
  (void)taosThreadRwlockRdlock(&pMnode->lock);
262,755,035✔
1059
  if (pMnode->stopped) {
262,761,633✔
1060
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,121✔
1061
    code = TSDB_CODE_APP_IS_STOPPING;
3,121✔
1062
    TAOS_RETURN(code);
3,121✔
1063
  }
1064

1065
  terrno = 0;
262,752,427✔
1066
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
262,755,430✔
1067
  if (terrno != 0) {
262,763,382✔
UNCOV
1068
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
1069
    code = terrno;
×
UNCOV
1070
    TAOS_RETURN(code);
×
1071
  }
1072

1073
  if (state.state != TAOS_SYNC_STATE_LEADER) {
262,761,205✔
1074
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,116,073✔
1075
    code = TSDB_CODE_SYN_NOT_LEADER;
2,116,073✔
1076
    goto _OVER;
2,116,073✔
1077
  }
1078

1079
  if (!state.restored || !pMnode->restored) {
260,645,132✔
1080
    (void)taosThreadRwlockUnlock(&pMnode->lock);
496,282✔
1081
    code = TSDB_CODE_SYN_RESTORING;
495,957✔
1082
    goto _OVER;
495,957✔
1083
  }
1084

1085
#if 1
1086
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
260,148,850✔
1087
#else
1088
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1089
  mTrace("mnode rpc is acquired, ref:%d", ref);
1090
#endif
1091

1092
  (void)taosThreadRwlockUnlock(&pMnode->lock);
260,148,539✔
1093
  TAOS_RETURN(code);
260,149,602✔
1094

1095
_OVER:
2,612,030✔
1096
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,612,030✔
1097
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,611,122✔
1098
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,610,809✔
1099
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,610,522✔
1100
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,610,794✔
1101
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,610,928✔
1102
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,611,871✔
1103
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,610,250✔
1104
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,609,805✔
UNCOV
1105
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1106
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1107
    TAOS_RETURN(code);
×
1108
  }
1109

1110
  const STraceId *trace = &pMsg->info.traceId;
2,609,401✔
1111
  SEpSet          epSet = {0};
2,609,514✔
1112
  mndGetMnodeEpSet(pMnode, &epSet);
2,610,040✔
1113

1114
  mGDebug(
2,612,030✔
1115
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1116
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1117
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1118
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1119

1120
  if (epSet.numOfEps <= 0) return -1;
2,612,030✔
1121

1122
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
9,231,233✔
1123
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,619,203✔
1124
  }
1125

1126
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,612,030✔
1127
  pMsg->info.rsp = rpcMallocCont(contLen);
2,610,629✔
1128
  if (pMsg->info.rsp != NULL) {
2,611,919✔
1129
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,611,919✔
UNCOV
1130
      mError("failed to serialize ep set");
×
1131
    }
1132
    pMsg->info.hasEpSet = 1;
2,609,752✔
1133
    pMsg->info.rspLen = contLen;
2,609,926✔
1134
  }
1135

1136
  TAOS_RETURN(code);
2,610,617✔
1137
}
1138

1139
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
324,702,219✔
1140
  SMnode         *pMnode = pMsg->info.node;
324,702,219✔
1141
  const STraceId *trace = &pMsg->info.traceId;
324,702,514✔
1142
  int32_t         code = TSDB_CODE_SUCCESS;
324,701,650✔
1143

1144
#ifdef TD_ENTERPRISE
1145
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
324,701,650✔
1146
    SCachedTokenInfo ti = {0};
20,312✔
1147
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
20,312✔
1148
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,931✔
1149
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,931✔
1150
      TAOS_RETURN(code);
1,931✔
1151
    }
1152
    if (ti.enabled == 0) {
18,381✔
1153
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
3,164✔
1154
      code = TSDB_CODE_MND_TOKEN_DISABLED;
3,164✔
1155
      TAOS_RETURN(code);
3,164✔
1156
    }
1157
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
15,217✔
UNCOV
1158
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
1159
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
UNCOV
1160
      TAOS_RETURN(code);
×
1161
    }
1162
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
15,217✔
1163
  }
1164
#endif
1165

1166
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
324,687,824✔
1167
  MndMsgFpExt fpExt = NULL;
324,693,855✔
1168
  if (fp == NULL) {
324,693,855✔
1169
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
23,113,349✔
1170
    if (fpExt == NULL) {
23,113,349✔
UNCOV
1171
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
1172
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
UNCOV
1173
      TAOS_RETURN(code);
×
1174
    }
1175
  }
1176

1177
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
324,693,855✔
1178

1179
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
322,075,855✔
1180
  if (fp)
322,076,004✔
1181
    code = (*fp)(pMsg);
298,962,655✔
1182
  else
1183
    code = (*fpExt)(pMsg, pQueueInfo);
23,113,349✔
1184
  mndReleaseRpc(pMnode);
322,079,269✔
1185

1186
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
322,082,612✔
1187
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
36,345,898✔
1188
  } else if (code == 0) {
285,736,714✔
1189
    mGTrace("msg:%p, successfully processed", pMsg);
272,496,128✔
1190
  } else {
1191
    // TODO removve this wrong set code
1192
    if (code == -1) {
13,240,586✔
1193
      code = terrno;
×
1194
    }
1195
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
13,240,586✔
1196
            TMSG_INFO(pMsg->msgType));
1197
  }
1198

1199
  TAOS_RETURN(code);
322,082,612✔
1200
}
1201

1202
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
119,540,097✔
1203
  tmsg_t type = TMSG_INDEX(msgType);
119,540,097✔
1204
  if (type < TDMT_MAX) {
119,540,097✔
1205
    pMnode->msgFp[type] = fp;
119,540,097✔
1206
  }
1207
}
119,540,097✔
1208

1209
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,968,136✔
1210
  tmsg_t type = TMSG_INDEX(msgType);
3,968,136✔
1211
  if (type < TDMT_MAX) {
3,968,136✔
1212
    pMnode->msgFpExt[type] = fp;
3,968,136✔
1213
  }
1214
}
3,968,136✔
1215

1216
// Note: uid 0 is reserved
1217
int64_t mndGenerateUid(const char *name, int32_t len) {
12,754,255✔
1218
  int32_t hashval = MurmurHash3_32(name, len);
12,754,255✔
UNCOV
1219
  do {
×
1220
    int64_t us = taosGetTimestampUs();
12,754,255✔
1221
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
12,754,255✔
1222
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
12,754,255✔
1223
    if (uuid) {
12,754,255✔
1224
      return llabs(uuid);
12,754,255✔
1225
    }
1226
  } while (true);
1227
}
1228

1229
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
74✔
1230
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1231
  int32_t code = mndAcquireRpc(pMnode);
74✔
1232
  if (code < 0) {
74✔
UNCOV
1233
    TAOS_RETURN(code);
×
1234
  } else if (code == 1) {
74✔
UNCOV
1235
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1236
  }
1237

1238
  SSdb   *pSdb = pMnode->pSdb;
74✔
1239
  int64_t ms = taosGetTimestampMs();
74✔
1240

1241
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
74✔
1242
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
74✔
1243
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
74✔
1244
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
74✔
1245
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
74✔
1246
      pStbInfo->stbs == NULL) {
74✔
UNCOV
1247
    mndReleaseRpc(pMnode);
×
UNCOV
1248
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1249
    if (terrno != 0) code = terrno;
×
UNCOV
1250
    TAOS_RETURN(code);
×
1251
  }
1252

1253
  // cluster info
1254
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
74✔
1255
  pClusterInfo->monitor_interval = tsMonitorInterval;
74✔
1256
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
74✔
1257
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
74✔
1258
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
74✔
1259
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
74✔
1260
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
74✔
1261

1262
  void *pIter = NULL;
74✔
1263
  while (1) {
74✔
1264
    SDnodeObj *pObj = NULL;
148✔
1265
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
148✔
1266
    if (pIter == NULL) break;
148✔
1267

1268
    SMonDnodeDesc desc = {0};
74✔
1269
    desc.dnode_id = pObj->id;
74✔
1270
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
74✔
1271
    if (mndIsDnodeOnline(pObj, ms)) {
74✔
1272
      tstrncpy(desc.status, "ready", sizeof(desc.status));
74✔
1273
    } else {
UNCOV
1274
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1275
    }
1276
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
148✔
UNCOV
1277
      mError("failed put dnode into array, but continue at this monitor report")
×
1278
    }
1279
    sdbRelease(pSdb, pObj);
74✔
1280
  }
1281

1282
  pIter = NULL;
74✔
1283
  while (1) {
74✔
1284
    SMnodeObj *pObj = NULL;
148✔
1285
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
148✔
1286
    if (pIter == NULL) break;
148✔
1287

1288
    SMonMnodeDesc desc = {0};
74✔
1289
    desc.mnode_id = pObj->id;
74✔
1290
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
74✔
1291

1292
    if (pObj->id == pMnode->selfDnodeId) {
74✔
1293
      pClusterInfo->first_ep_dnode_id = pObj->id;
74✔
1294
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
74✔
1295
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1296
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
74✔
1297
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1298
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
74✔
1299
      desc.syncState = TAOS_SYNC_STATE_LEADER;
74✔
1300
    } else {
UNCOV
1301
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
UNCOV
1302
      desc.syncState = pObj->syncState;
×
1303
    }
1304
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
148✔
UNCOV
1305
      mError("failed to put mnode into array, but continue at this monitor report");
×
1306
    }
1307
    sdbRelease(pSdb, pObj);
74✔
1308
  }
1309

1310
  // vgroup info
1311
  pIter = NULL;
74✔
1312
  while (1) {
148✔
1313
    SVgObj *pVgroup = NULL;
222✔
1314
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
222✔
1315
    if (pIter == NULL) break;
222✔
1316

1317
    if (pVgroup->mountVgId) {
148✔
UNCOV
1318
      sdbRelease(pSdb, pVgroup);
×
UNCOV
1319
      continue;
×
1320
    }
1321

1322
    pClusterInfo->vgroups_total++;
148✔
1323
    pClusterInfo->tbs_total += pVgroup->numOfTables;
148✔
1324

1325
    SMonVgroupDesc desc = {0};
148✔
1326
    desc.vgroup_id = pVgroup->vgId;
148✔
1327

1328
    SName name = {0};
148✔
1329
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
148✔
1330
    if (code < 0) {
148✔
UNCOV
1331
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
1332
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
1333
      sdbRelease(pSdb, pVgroup);
×
UNCOV
1334
      TAOS_RETURN(code);
×
1335
    }
1336
    (void)tNameGetDbName(&name, desc.database_name);
148✔
1337

1338
    desc.tables_num = pVgroup->numOfTables;
148✔
1339
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
148✔
1340
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
148✔
1341
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
296✔
1342
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
148✔
1343
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
148✔
1344
      pVnDesc->dnode_id = pVgid->dnodeId;
148✔
1345
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
148✔
1346
      pVnDesc->syncState = pVgid->syncState;
148✔
1347
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
148✔
1348
        tstrncpy(desc.status, "ready", sizeof(desc.status));
148✔
1349
        pClusterInfo->vgroups_alive++;
148✔
1350
      }
1351
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
148✔
1352
        pClusterInfo->vnodes_alive++;
148✔
1353
      }
1354
      pClusterInfo->vnodes_total++;
148✔
1355
    }
1356

1357
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
296✔
UNCOV
1358
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1359
    }
1360
    sdbRelease(pSdb, pVgroup);
148✔
1361
  }
1362

1363
  // stb info
1364
  pIter = NULL;
74✔
UNCOV
1365
  while (1) {
×
1366
    SStbObj *pStb = NULL;
74✔
1367
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
74✔
1368
    if (pIter == NULL) break;
74✔
1369

UNCOV
1370
    SMonStbDesc desc = {0};
×
1371

UNCOV
1372
    SName name1 = {0};
×
UNCOV
1373
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1374
    if (code < 0) {
×
UNCOV
1375
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
1376
      sdbRelease(pSdb, pStb);
×
UNCOV
1377
      TAOS_RETURN(code);
×
1378
    }
UNCOV
1379
    (void)tNameGetDbName(&name1, desc.database_name);
×
1380

UNCOV
1381
    SName name2 = {0};
×
UNCOV
1382
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1383
    if (code < 0) {
×
UNCOV
1384
      mError("failed to get table name since %s", tstrerror(code));
×
1385
      sdbRelease(pSdb, pStb);
×
UNCOV
1386
      TAOS_RETURN(code);
×
1387
    }
UNCOV
1388
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1389

1390
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
UNCOV
1391
      mError("failed to put stb into array, but continue at this monitor report");
×
1392
    }
1393
    sdbRelease(pSdb, pStb);
×
1394
  }
1395

1396
  // grant info
1397
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
74✔
1398
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
74✔
1399
  if (pMnode->grant.expireTimeMS == 0) {
74✔
UNCOV
1400
    pGrantInfo->expire_time = 0;
×
1401
    pGrantInfo->timeseries_total = 0;
×
1402
  }
1403

1404
  mndReleaseRpc(pMnode);
74✔
1405
  TAOS_RETURN(code);
74✔
1406
}
1407

1408
int32_t mndResetTimer(SMnode *pMnode){
×
UNCOV
1409
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1410
}
1411

1412
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
34,040,838✔
1413
  mTrace("mnode get load");
34,040,838✔
1414
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
34,040,838✔
1415
  pLoad->syncState = state.state;
34,040,838✔
1416
  pLoad->syncRestore = state.restored;
34,040,838✔
1417
  pLoad->syncTerm = state.term;
34,040,838✔
1418
  pLoad->roleTimeMs = state.roleTimeMs;
34,040,838✔
1419
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
34,040,838✔
1420
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1421
  return 0;
34,040,838✔
1422
}
1423

1424
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
10,834,147✔
1425
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
10,834,147✔
1426
  return state.roleTimeMs;
10,834,147✔
1427
}
1428

1429
void mndSetRestored(SMnode *pMnode, bool restored) {
495,860✔
1430
  if (restored) {
495,860✔
1431
    (void)taosThreadRwlockWrlock(&pMnode->lock);
495,860✔
1432
    pMnode->restored = true;
495,860✔
1433
    (void)taosThreadRwlockUnlock(&pMnode->lock);
495,860✔
1434
    mInfo("mnode set restored:%d", restored);
495,860✔
1435
  } else {
UNCOV
1436
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
UNCOV
1437
    pMnode->restored = false;
×
UNCOV
1438
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
1439
    mInfo("mnode set restored:%d", restored);
×
1440
    while (1) {
UNCOV
1441
      if (pMnode->rpcRef <= 0) break;
×
UNCOV
1442
      taosMsleep(3);
×
1443
    }
1444
  }
1445
}
495,860✔
1446

UNCOV
1447
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1448

1449
void mndSetStop(SMnode *pMnode) {
495,860✔
1450
  (void)taosThreadRwlockWrlock(&pMnode->lock);
495,860✔
1451
  pMnode->stopped = true;
495,860✔
1452
  (void)taosThreadRwlockUnlock(&pMnode->lock);
495,860✔
1453
  mInfo("mnode set stopped");
495,860✔
1454
}
495,860✔
1455

1456
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
688,734,203✔
1457

1458
void mndSetSoDPhase(SMnode *pMnode, int8_t phase) {
356✔
1459
  (void)taosThreadRwlockWrlock(&pMnode->lock);
356✔
1460
  pMnode->sodPhase = phase;
356✔
1461
  (void)taosThreadRwlockUnlock(&pMnode->lock);
356✔
1462
}
356✔
1463

1464
int8_t mndGetSoDPhase(SMnode *pMnode) {
71,750✔
1465
  int8_t result = TSDB_SOD_PHASE_STABLE;
71,750✔
1466
  (void)taosThreadRwlockRdlock(&pMnode->lock);
71,750✔
1467
  result = pMnode->sodPhase;
71,750✔
1468
  (void)taosThreadRwlockUnlock(&pMnode->lock);
71,750✔
1469
  if (result < TSDB_SOD_PHASE_STABLE || result > TSDB_SOD_PHASE_ENFORCE) {
71,750✔
NEW
1470
    mWarn("invalid SoD phase:%d, reset to stable", result);
×
NEW
1471
    result = TSDB_SOD_PHASE_STABLE;
×
1472
  }
1473
  return result;
71,750✔
1474
}
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