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

taosdata / TDengine / #5027

17 Apr 2026 01:15AM UTC coverage: 72.996% (+0.03%) from 72.966%
#5027

push

travis-ci

web-flow
fix: replace \s with [[:space:]] in shell regex for macOS BSD compat (#35162)

6 of 7 new or added lines in 2 files covered. (85.71%)

404 existing lines in 122 files now uncovered.

273279 of 374375 relevant lines covered (73.0%)

130951667.08 hits per line

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

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
319,049,436✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
319,049,436✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
319,050,932✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
319,049,619✔
93
}
319,050,229✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
82,751,410✔
96
  terrno = 0;
82,751,410✔
97
  SMTimerReq timerReq = {0};
82,751,410✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
82,751,410✔
100
  if (contLen <= 0) return NULL;
82,751,241✔
101
  void *pReq = rpcMallocCont(contLen);
82,751,241✔
102
  if (pReq == NULL) return NULL;
82,751,410✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
82,751,410✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
82,751,410✔
108
  return pReq;
82,751,410✔
109
}
110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

305
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
143,950✔
306
  SSdb *pSdb = pMnode->pSdb;
143,950✔
307

308
  void *pIter = NULL;
143,950✔
309
  while (1) {
394,983✔
310
    SVgObj *pVgroup = NULL;
538,933✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
538,933✔
312
    if (pIter == NULL) break;
538,933✔
313

314
    bool stateChanged = false;
394,983✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
988,492✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
769,479✔
317
      if (pGid->dnodeId == dnodeId) {
769,479✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
175,970✔
319
          mInfo(
72,052✔
320
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
321
              "restored:0 "
322
              "canRead:0",
323
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
324
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
72,052✔
325
          pGid->syncRestore = 0;
72,052✔
326
          pGid->syncCanRead = 0;
72,052✔
327
          pGid->startTimeMs = 0;
72,052✔
328
          pGid->learnerProgress = 0;
72,052✔
329
          pGid->snapSeq = -1;
72,052✔
330
          stateChanged = true;
72,052✔
331
        }
332
        break;
175,970✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
394,983✔
347
  }
348
}
143,950✔
349

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

354
  SSdb   *pSdb = pMnode->pSdb;
6,327,049✔
355
  int64_t curMs = taosGetTimestampMs();
6,327,049✔
356

357
  void *pIter = NULL;
6,327,049✔
358
  while (1) {
10,383,802✔
359
    SDnodeObj *pDnode = NULL;
16,710,851✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
16,710,851✔
361
    if (pIter == NULL) break;
16,710,851✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
10,383,802✔
364
    if (!online) {
10,383,802✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
143,950✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
143,950✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
10,383,802✔
370
  }
371

372
  mndReleaseRpc(pMnode);
6,327,049✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
373,204,003✔
376
  terrno = 0;
373,204,003✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
373,204,005✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
373,203,113✔
379
  if (terrno != 0) {
373,202,606✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
373,203,530✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,626,768✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
12,626,768✔
387
    return true;
12,626,768✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
360,576,762✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
16,028✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
14,305✔
392
    return true;
14,305✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
360,560,734✔
395
  return false;
360,561,947✔
396
}
397

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

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

412
  return min <= 1 ? 2 : min;
×
413
}
414
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
33,435,366✔
415
  int32_t code = 0;
33,435,366✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
33,435,366✔
418
    mndPullupGrant(pMnode);
1,752,758✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
33,435,366✔
421
    mndPullupTtl(pMnode);
3,505,525✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
33,435,366✔
425
    mndPullupTrimDb(pMnode);
15,745✔
426
  }
427

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

462
  if (sec % tsCompactPullupInterval == 0) {
33,435,366✔
463
    mndPullupCompacts(pMnode);
3,332,750✔
464
  }
465

466
  if (sec % tsScanPullupInterval == 0) {
33,435,366✔
467
    mndPullupScans(pMnode);
3,332,820✔
468
  }
469
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
33,435,366✔
470
    mndPullupInstances(pMnode);
6,686,979✔
471
  }
472
#ifdef USE_TOPIC
473
  if (sec % tsMqRebalanceInterval == 0) {
33,435,366✔
474
    mndCalMqRebalance(pMnode);
16,715,154✔
475
  }
476
#endif
477
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
33,435,366✔
478
    mndPullupTelem(pMnode);
165✔
479
  }
480
  if (sec % tsUptimeInterval == 0) {
33,435,366✔
481
    mndIncreaseUpTime(pMnode);
104,186✔
482
  }
483
}
33,435,366✔
484

485
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
327,127,566✔
486
  int32_t code = 0;
327,127,566✔
487
#ifndef TD_ASTRA
488
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
327,127,566✔
489
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
16,153,680✔
490
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
491
    }
492
  }
493

494
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
327,127,566✔
495
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
10,700,723✔
496
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
497
    }
498
  }
499
#endif
500
}
327,127,566✔
501

502
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
327,127,566✔
503
  if (ms % (tsStatusTimeoutMs) == 0) {
327,127,566✔
504
    mndCheckDnodeOffline(pMnode);
6,327,049✔
505
  }
506
}
327,127,566✔
507

508
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
33,435,366✔
509
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
33,435,366✔
510
    mndSyncCheckTimeout(pMnode);
1,100,541✔
511
  }
512
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
33,435,366✔
513
    msmHealthCheck(pMnode);
11,130,523✔
514
  }
515
}
33,435,366✔
516

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

522
  while (1) {
339,101,129✔
523
    if (mndGetStop(pMnode)) break;
339,572,274✔
524

525
    int64_t nowSec = taosGetTimestampMs() / 1000;
339,101,129✔
526
    if (nowSec == lastSec) {
339,101,129✔
527
      taosMsleep(100);
304,468,158✔
528
      continue;
304,468,158✔
529
    }
530
    lastSec = nowSec;
34,632,971✔
531

532
    if (mnodeIsNotLeader(pMnode)) {
34,632,971✔
533
      taosMsleep(100);
1,197,605✔
534
      mTrace("timer not process since mnode is not leader");
1,197,605✔
535
      continue;
1,197,605✔
536
    }
537

538
    mndDoTimerCheckSync(pMnode, nowSec);
33,435,366✔
539

540
    mndDoTimerPullupTask(pMnode, nowSec);
33,435,366✔
541

542
    taosMsleep(100);
33,435,366✔
543
  }
544

545
  return NULL;
471,145✔
546
}
547

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

553
  while (1) {
554
    lastTime += 100;
339,042,179✔
555
    taosMsleep(100);
339,042,179✔
556

557
    if (mndGetStop(pMnode)) break;
339,042,179✔
558
    if (lastTime % 10 != 0) continue;
338,571,034✔
559

560
    if (mnodeIsNotLeader(pMnode)) {
338,571,034✔
561
      mTrace("timer not process since mnode is not leader");
11,443,468✔
562
      continue;
11,443,468✔
563
    }
564

565
    mndDoTimerCheckStatus(pMnode, lastTime);
327,127,566✔
566

567
    mndDoArbTimerPullupTask(pMnode, lastTime);
327,127,566✔
568
  }
569

570
  return NULL;
471,145✔
571
}
572

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

586
  (void)taosThreadAttrDestroy(&thAttr);
471,145✔
587
  tmsgReportStartup("mnode-timer", "initialized");
471,145✔
588

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

600
  (void)taosThreadAttrDestroy(&arbAttr);
471,145✔
601
  tmsgReportStartup("mnode-timer", "initialized");
471,145✔
602
  TAOS_RETURN(code);
471,145✔
603
}
604

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

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

624
  if (taosMkDir(pMnode->path) != 0) {
471,277✔
625
    code = terrno;
×
626
    TAOS_RETURN(code);
×
627
  }
628

629
  TAOS_RETURN(code);
471,277✔
630
}
631

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

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

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

665
  TAOS_RETURN(code);
471,277✔
666
}
667

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

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

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

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

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

723
  TAOS_RETURN(code);
471,277✔
724
}
725

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

735
  if (!pMnode->deploy) {
471,277✔
736
    code = sdbReadFile(pMnode->pSdb);
131,070✔
737
  }
738

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

741
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
471,277✔
742
  return code;
471,277✔
743
}
744

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

752
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
23,563,850✔
753
  SMnodeStep step = {0};
23,563,850✔
754
  step.name = name;
23,563,850✔
755
  step.initFp = initFp;
23,563,850✔
756
  step.cleanupFp = cleanupFp;
23,563,850✔
757
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
47,127,700✔
758
    TAOS_RETURN(terrno);
×
759
  }
760

761
  TAOS_RETURN(0);
23,563,850✔
762
}
763

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

820
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
471,215✔
821
  if (pMnode->pSteps == NULL) return;
471,215✔
822

823
  if (pos == -1) {
471,215✔
824
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
471,215✔
825
  }
826

827
  for (int32_t s = pos; s >= 0; s--) {
24,031,965✔
828
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
23,560,750✔
829
    mInfo("%s will cleanup", pStep->name);
23,560,750✔
830
    if (pStep->cleanupFp != NULL) {
23,560,750✔
831
      (*pStep->cleanupFp)(pMnode);
22,618,320✔
832
    }
833
  }
834

835
  taosArrayClear(pMnode->pSteps);
471,215✔
836
  taosArrayDestroy(pMnode->pSteps);
471,215✔
837
  pMnode->pSteps = NULL;
471,215✔
838
}
839

840
static int32_t mndExecSteps(SMnode *pMnode) {
471,277✔
841
  int32_t code = 0;
471,277✔
842
  int32_t size = taosArrayGetSize(pMnode->pSteps);
471,277✔
843
  for (int32_t pos = 0; pos < size; pos++) {
24,035,127✔
844
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
23,563,850✔
845
    if (pStep->initFp == NULL) continue;
23,563,850✔
846

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

857
  pMnode->clusterId = mndGetClusterId(pMnode);
471,277✔
858
  TAOS_RETURN(0);
471,277✔
859
}
860

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

873
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
471,277✔
874
  terrno = 0;
471,277✔
875
  mInfo("start to open mnode in %s", path);
471,277✔
876

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

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

893
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
471,277✔
894
        pOption->numOfTotalReplicas);
895
  mndSetOptions(pMnode, pOption);
471,277✔
896

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

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

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

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

931
  mInfo("mnode open successfully");
471,277✔
932
  return pMnode;
471,277✔
933
}
934

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

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

961
int32_t mndStart(SMnode *pMnode) {
471,145✔
962
  mndSyncStart(pMnode);
471,145✔
963
  if (pMnode->deploy) {
471,145✔
964
    if (sdbDeploy(pMnode->pSdb) != 0) {
340,207✔
965
      mError("failed to deploy sdb while start mnode");
×
966
      return -1;
×
967
    }
968
    mndSetRestored(pMnode, true);
340,207✔
969
  }
970
  if (mndIsLeader(pMnode)) {
471,145✔
971
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
427,223✔
972
      mError("failed to upgrade sdb while start mnode");
×
973
      return -1;
×
974
    }
975
  }
976
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
471,145✔
977
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
471,145✔
978

979
  return mndInitTimer(pMnode);
471,145✔
980
}
981

982
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
471,145✔
983

984
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
371,545✔
985

986
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
371,545✔
987

988
int32_t mndIsCatchUp(SMnode *pMnode) {
165,245✔
989
  int64_t rid = pMnode->syncMgmt.sync;
165,245✔
990
  return syncIsCatchUp(rid);
165,245✔
991
}
992

993
ESyncRole mndGetRole(SMnode *pMnode) {
165,245✔
994
  int64_t rid = pMnode->syncMgmt.sync;
165,245✔
995
  return syncGetRole(rid);
165,245✔
996
}
997

998
int64_t mndGetTerm(SMnode *pMnode) {
10,825,167✔
999
  int64_t rid = pMnode->syncMgmt.sync;
10,825,167✔
1000
  return syncGetTerm(rid);
10,825,167✔
1001
}
1002

1003
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
26,979,965✔
1004

1005
void mndStop(SMnode *pMnode) {
471,145✔
1006
  mndSetStop(pMnode);
471,145✔
1007
  mndSyncStop(pMnode);
471,145✔
1008
  mndCleanupTimer(pMnode);
471,145✔
1009
}
471,145✔
1010

1011
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
60,548,960✔
1012
  SMnode    *pMnode = pMsg->info.node;
60,548,960✔
1013
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
60,548,960✔
1014

1015
  const STraceId *trace = &pMsg->info.traceId;
60,548,960✔
1016
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
60,548,960✔
1017

1018
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
60,548,960✔
1019
  if (code != 0) {
60,548,960✔
1020
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
829✔
1021
            tstrerror(code), code);
1022
  }
1023

1024
  return code;
60,548,960✔
1025
}
1026

1027
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
316,120,155✔
1028
  int32_t code = 0;
316,120,155✔
1029
  if (!IsReq(pMsg)) TAOS_RETURN(code);
316,120,155✔
1030
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
278,437,936✔
1031
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
272,658,700✔
1032
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
267,784,197✔
1033
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
256,342,831✔
1034
    TAOS_RETURN(code);
22,098,891✔
1035
  }
1036

1037
  SMnode *pMnode = pMsg->info.node;
256,340,075✔
1038
  (void)taosThreadRwlockRdlock(&pMnode->lock);
256,339,216✔
1039
  if (pMnode->stopped) {
256,338,843✔
1040
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,861✔
1041
    code = TSDB_CODE_APP_IS_STOPPING;
1,861✔
1042
    TAOS_RETURN(code);
1,861✔
1043
  }
1044

1045
  terrno = 0;
256,335,834✔
1046
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
256,338,772✔
1047
  if (terrno != 0) {
256,344,576✔
1048
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1049
    code = terrno;
×
1050
    TAOS_RETURN(code);
×
1051
  }
1052

1053
  if (state.state != TAOS_SYNC_STATE_LEADER) {
256,342,829✔
1054
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,695,161✔
1055
    code = TSDB_CODE_SYN_NOT_LEADER;
2,695,161✔
1056
    goto _OVER;
2,695,161✔
1057
  }
1058

1059
  if (!state.restored || !pMnode->restored) {
253,647,668✔
1060
    (void)taosThreadRwlockUnlock(&pMnode->lock);
717,425✔
1061
    code = TSDB_CODE_SYN_RESTORING;
711,197✔
1062
    goto _OVER;
711,197✔
1063
  }
1064

1065
#if 1
1066
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
252,931,613✔
1067
#else
1068
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1069
  mTrace("mnode rpc is acquired, ref:%d", ref);
1070
#endif
1071

1072
  (void)taosThreadRwlockUnlock(&pMnode->lock);
252,936,314✔
1073
  TAOS_RETURN(code);
252,935,833✔
1074

1075
_OVER:
3,406,358✔
1076
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,406,358✔
1077
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,406,663✔
1078
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,406,663✔
1079
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,406,663✔
1080
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,406,024✔
1081
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,406,663✔
1082
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,405,719✔
1083
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,406,663✔
1084
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,406,588✔
UNCOV
1085
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1086
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1087
    TAOS_RETURN(code);
×
1088
  }
1089

1090
  const STraceId *trace = &pMsg->info.traceId;
3,405,850✔
1091
  SEpSet          epSet = {0};
3,406,000✔
1092
  mndGetMnodeEpSet(pMnode, &epSet);
3,406,358✔
1093

1094
  mGDebug(
3,406,358✔
1095
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1096
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1097
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1098
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1099

1100
  if (epSet.numOfEps <= 0) return -1;
3,406,663✔
1101

1102
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
12,076,453✔
1103
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
8,669,790✔
1104
  }
1105

1106
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,406,663✔
1107
  pMsg->info.rsp = rpcMallocCont(contLen);
3,404,959✔
1108
  if (pMsg->info.rsp != NULL) {
3,406,663✔
1109
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,406,663✔
1110
      mError("failed to serialize ep set");
×
1111
    }
1112
    pMsg->info.hasEpSet = 1;
3,406,002✔
1113
    pMsg->info.rspLen = contLen;
3,406,486✔
1114
  }
1115

1116
  TAOS_RETURN(code);
3,406,593✔
1117
}
1118

1119
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
316,133,309✔
1120
  SMnode         *pMnode = pMsg->info.node;
316,133,309✔
1121
  const STraceId *trace = &pMsg->info.traceId;
316,135,211✔
1122
  int32_t         code = TSDB_CODE_SUCCESS;
316,135,019✔
1123

1124
#ifdef TD_ENTERPRISE
1125
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
316,135,019✔
1126
    SCachedTokenInfo ti = {0};
19,094✔
1127
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
19,094✔
1128
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,882✔
1129
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,882✔
1130
      TAOS_RETURN(code);
1,882✔
1131
    }
1132
    if (ti.enabled == 0) {
17,212✔
1133
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,735✔
1134
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,735✔
1135
      TAOS_RETURN(code);
2,735✔
1136
    }
1137
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
14,477✔
1138
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1139
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1140
      TAOS_RETURN(code);
×
1141
    }
1142
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
14,477✔
1143
  }
1144
#endif
1145

1146
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
316,117,660✔
1147
  MndMsgFpExt fpExt = NULL;
316,127,661✔
1148
  if (fp == NULL) {
316,127,661✔
1149
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
22,100,281✔
1150
    if (fpExt == NULL) {
22,101,062✔
1151
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1152
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1153
      TAOS_RETURN(code);
×
1154
    }
1155
  }
1156

1157
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
316,128,442✔
1158

1159
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
312,718,228✔
1160
  if (fp)
312,719,074✔
1161
    code = (*fp)(pMsg);
290,618,012✔
1162
  else
1163
    code = (*fpExt)(pMsg, pQueueInfo);
22,101,062✔
1164
  mndReleaseRpc(pMnode);
312,721,426✔
1165

1166
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
312,723,123✔
1167
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
34,800,206✔
1168
  } else if (code == 0) {
277,922,917✔
1169
    mGTrace("msg:%p, successfully processed", pMsg);
266,462,537✔
1170
  } else {
1171
    // TODO removve this wrong set code
1172
    if (code == -1) {
11,460,380✔
1173
      code = terrno;
×
1174
    }
1175
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
11,460,380✔
1176
            TMSG_INFO(pMsg->msgType));
1177
  }
1178

1179
  TAOS_RETURN(code);
312,723,123✔
1180
}
1181

1182
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
113,106,480✔
1183
  tmsg_t type = TMSG_INDEX(msgType);
113,106,480✔
1184
  if (type < TDMT_MAX) {
113,106,480✔
1185
    pMnode->msgFp[type] = fp;
113,106,480✔
1186
  }
1187
}
113,106,480✔
1188

1189
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,770,216✔
1190
  tmsg_t type = TMSG_INDEX(msgType);
3,770,216✔
1191
  if (type < TDMT_MAX) {
3,770,216✔
1192
    pMnode->msgFpExt[type] = fp;
3,770,216✔
1193
  }
1194
}
3,770,216✔
1195

1196
// Note: uid 0 is reserved
1197
int64_t mndGenerateUid(const char *name, int32_t len) {
12,177,583✔
1198
  int32_t hashval = MurmurHash3_32(name, len);
12,177,583✔
1199
  do {
×
1200
    int64_t us = taosGetTimestampUs();
12,177,612✔
1201
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
12,177,612✔
1202
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
12,177,612✔
1203
    if (uuid) {
12,177,612✔
1204
      return llabs(uuid);
12,177,612✔
1205
    }
1206
  } while (true);
1207
}
1208

1209
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
57✔
1210
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1211
  int32_t code = mndAcquireRpc(pMnode);
57✔
1212
  if (code < 0) {
57✔
1213
    TAOS_RETURN(code);
×
1214
  } else if (code == 1) {
57✔
1215
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1216
  }
1217

1218
  SSdb   *pSdb = pMnode->pSdb;
57✔
1219
  int64_t ms = taosGetTimestampMs();
57✔
1220

1221
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
57✔
1222
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
57✔
1223
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
57✔
1224
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
57✔
1225
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
57✔
1226
      pStbInfo->stbs == NULL) {
57✔
1227
    mndReleaseRpc(pMnode);
×
1228
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1229
    if (terrno != 0) code = terrno;
×
1230
    TAOS_RETURN(code);
×
1231
  }
1232

1233
  // cluster info
1234
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
57✔
1235
  pClusterInfo->monitor_interval = tsMonitorInterval;
57✔
1236
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
57✔
1237
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
57✔
1238
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
57✔
1239
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
57✔
1240
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
57✔
1241

1242
  void *pIter = NULL;
57✔
1243
  while (1) {
57✔
1244
    SDnodeObj *pObj = NULL;
114✔
1245
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
114✔
1246
    if (pIter == NULL) break;
114✔
1247

1248
    SMonDnodeDesc desc = {0};
57✔
1249
    desc.dnode_id = pObj->id;
57✔
1250
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
57✔
1251
    if (mndIsDnodeOnline(pObj, ms)) {
57✔
1252
      tstrncpy(desc.status, "ready", sizeof(desc.status));
57✔
1253
    } else {
1254
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1255
    }
1256
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
114✔
1257
      mError("failed put dnode into array, but continue at this monitor report")
×
1258
    }
1259
    sdbRelease(pSdb, pObj);
57✔
1260
  }
1261

1262
  pIter = NULL;
57✔
1263
  while (1) {
57✔
1264
    SMnodeObj *pObj = NULL;
114✔
1265
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
114✔
1266
    if (pIter == NULL) break;
114✔
1267

1268
    SMonMnodeDesc desc = {0};
57✔
1269
    desc.mnode_id = pObj->id;
57✔
1270
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
57✔
1271

1272
    if (pObj->id == pMnode->selfDnodeId) {
57✔
1273
      pClusterInfo->first_ep_dnode_id = pObj->id;
57✔
1274
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
57✔
1275
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1276
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
57✔
1277
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1278
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
57✔
1279
      desc.syncState = TAOS_SYNC_STATE_LEADER;
57✔
1280
    } else {
1281
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1282
      desc.syncState = pObj->syncState;
×
1283
    }
1284
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
114✔
1285
      mError("failed to put mnode into array, but continue at this monitor report");
×
1286
    }
1287
    sdbRelease(pSdb, pObj);
57✔
1288
  }
1289

1290
  // vgroup info
1291
  pIter = NULL;
57✔
1292
  while (1) {
114✔
1293
    SVgObj *pVgroup = NULL;
171✔
1294
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
171✔
1295
    if (pIter == NULL) break;
171✔
1296

1297
    if (pVgroup->mountVgId) {
114✔
1298
      sdbRelease(pSdb, pVgroup);
×
1299
      continue;
×
1300
    }
1301

1302
    pClusterInfo->vgroups_total++;
114✔
1303
    pClusterInfo->tbs_total += pVgroup->numOfTables;
114✔
1304

1305
    SMonVgroupDesc desc = {0};
114✔
1306
    desc.vgroup_id = pVgroup->vgId;
114✔
1307

1308
    SName name = {0};
114✔
1309
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
114✔
1310
    if (code < 0) {
114✔
1311
      mError("failed to get db name since %s", tstrerror(code));
×
1312
      sdbCancelFetch(pSdb, pIter);
×
1313
      sdbRelease(pSdb, pVgroup);
×
1314
      TAOS_RETURN(code);
×
1315
    }
1316
    (void)tNameGetDbName(&name, desc.database_name);
114✔
1317

1318
    desc.tables_num = pVgroup->numOfTables;
114✔
1319
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
114✔
1320
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
114✔
1321
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
228✔
1322
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
114✔
1323
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
114✔
1324
      pVnDesc->dnode_id = pVgid->dnodeId;
114✔
1325
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
114✔
1326
      pVnDesc->syncState = pVgid->syncState;
114✔
1327
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
114✔
1328
        tstrncpy(desc.status, "ready", sizeof(desc.status));
114✔
1329
        pClusterInfo->vgroups_alive++;
114✔
1330
      }
1331
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
114✔
1332
        pClusterInfo->vnodes_alive++;
114✔
1333
      }
1334
      pClusterInfo->vnodes_total++;
114✔
1335
    }
1336

1337
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
228✔
1338
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1339
    }
1340
    sdbRelease(pSdb, pVgroup);
114✔
1341
  }
1342

1343
  // stb info
1344
  pIter = NULL;
57✔
1345
  while (1) {
×
1346
    SStbObj *pStb = NULL;
57✔
1347
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
57✔
1348
    if (pIter == NULL) break;
57✔
1349

1350
    SMonStbDesc desc = {0};
×
1351

1352
    SName name1 = {0};
×
1353
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1354
    if (code < 0) {
×
1355
      mError("failed to get db name since %s", tstrerror(code));
×
1356
      sdbRelease(pSdb, pStb);
×
1357
      TAOS_RETURN(code);
×
1358
    }
1359
    (void)tNameGetDbName(&name1, desc.database_name);
×
1360

1361
    SName name2 = {0};
×
1362
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1363
    if (code < 0) {
×
1364
      mError("failed to get table name since %s", tstrerror(code));
×
1365
      sdbRelease(pSdb, pStb);
×
1366
      TAOS_RETURN(code);
×
1367
    }
1368
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1369

1370
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1371
      mError("failed to put stb into array, but continue at this monitor report");
×
1372
    }
1373
    sdbRelease(pSdb, pStb);
×
1374
  }
1375

1376
  // grant info
1377
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
57✔
1378
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
57✔
1379
  if (pMnode->grant.expireTimeMS == 0) {
57✔
1380
    pGrantInfo->expire_time = 0;
×
1381
    pGrantInfo->timeseries_total = 0;
×
1382
  }
1383

1384
  mndReleaseRpc(pMnode);
57✔
1385
  TAOS_RETURN(code);
57✔
1386
}
1387

1388
int32_t mndResetTimer(SMnode *pMnode){
×
1389
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1390
}
1391

1392
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
33,553,000✔
1393
  mTrace("mnode get load");
33,553,000✔
1394
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
33,553,000✔
1395
  pLoad->syncState = state.state;
33,553,000✔
1396
  pLoad->syncRestore = state.restored;
33,553,000✔
1397
  pLoad->syncTerm = state.term;
33,553,000✔
1398
  pLoad->roleTimeMs = state.roleTimeMs;
33,553,000✔
1399
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
33,553,000✔
1400
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1401
  return 0;
33,553,000✔
1402
}
1403

1404
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
10,700,723✔
1405
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
10,700,723✔
1406
  return state.roleTimeMs;
10,700,723✔
1407
}
1408

1409
void mndSetRestored(SMnode *pMnode, bool restored) {
471,050✔
1410
  if (restored) {
471,050✔
1411
    (void)taosThreadRwlockWrlock(&pMnode->lock);
471,050✔
1412
    pMnode->restored = true;
471,050✔
1413
    (void)taosThreadRwlockUnlock(&pMnode->lock);
471,050✔
1414
    mInfo("mnode set restored:%d", restored);
471,050✔
1415
  } else {
1416
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1417
    pMnode->restored = false;
×
1418
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1419
    mInfo("mnode set restored:%d", restored);
×
1420
    while (1) {
1421
      if (pMnode->rpcRef <= 0) break;
×
1422
      taosMsleep(3);
×
1423
    }
1424
  }
1425
}
471,050✔
1426

1427
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1428

1429
void mndSetStop(SMnode *pMnode) {
471,145✔
1430
  (void)taosThreadRwlockWrlock(&pMnode->lock);
471,145✔
1431
  pMnode->stopped = true;
471,145✔
1432
  (void)taosThreadRwlockUnlock(&pMnode->lock);
471,145✔
1433
  mInfo("mnode set stopped");
471,145✔
1434
}
471,145✔
1435

1436
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
678,602,487✔
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