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

taosdata / TDengine / #4943

30 Jan 2026 06:19AM UTC coverage: 66.718% (-0.07%) from 66.788%
#4943

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1122 of 2018 new or added lines in 72 files covered. (55.6%)

823 existing lines in 156 files now uncovered.

204811 of 306978 relevant lines covered (66.72%)

123993567.34 hits per line

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

79.91
/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,381,005✔
66
  int32_t code = 0;
6,381,005✔
67
  (void)taosThreadRwlockRdlock(&pMnode->lock);
6,381,005✔
68
  if (pMnode->stopped) {
6,381,005✔
69
    code = TSDB_CODE_APP_IS_STOPPING;
×
70
  } else if (!mndIsLeader(pMnode)) {
6,381,005✔
71
    code = 1;
×
72
  } else {
73
#if 1
74
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
6,381,005✔
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,381,005✔
81
  TAOS_RETURN(code);
6,381,005✔
82
}
83

84
static inline void mndReleaseRpc(SMnode *pMnode) {
290,226,301✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
290,226,301✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
290,227,556✔
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);
290,226,749✔
93
}
290,226,117✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
82,161,799✔
96
  terrno = 0;
82,161,799✔
97
  SMTimerReq timerReq = {0};
82,162,173✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
82,162,173✔
100
  if (contLen <= 0) return NULL;
82,161,799✔
101
  void *pReq = rpcMallocCont(contLen);
82,161,799✔
102
  if (pReq == NULL) return NULL;
82,161,983✔
103

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

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

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

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

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

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

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

184
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,382,884✔
185
  mTrace("pullup trim query");
3,382,884✔
186
  int32_t contLen = 0;
3,382,884✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
3,382,884✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,382,884✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,382,884✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
3,382,884✔
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) {
×
209
  mTrace("pullup update ssmigrate progress");
×
210
  int32_t contLen = 0;
×
211
  void   *pReq = mndBuildTimerMsg(&contLen);
×
212
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
213
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
214
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
215
  }
216
}
×
217

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

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

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

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

258
static void mndPullupGrant(SMnode *pMnode) {
1,636,426✔
259
  mTrace("pullup grant msg");
1,636,426✔
260
  int32_t contLen = 0;
1,636,426✔
261
  void   *pReq = mndBuildTimerMsg(&contLen);
1,636,426✔
262
  if (pReq != NULL) {
1,636,426✔
263
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,636,426✔
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,636,426✔
270
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
271
    }
272
  }
273
}
1,636,426✔
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) {
107,764✔
289
  mTrace("increate uptime");
107,764✔
290
  int32_t contLen = 0;
107,764✔
291
  void   *pReq = mndBuildTimerMsg(&contLen);
107,764✔
292
  if (pReq != NULL) {
107,764✔
293
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
107,764✔
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) {
107,764✔
300
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
301
    }
302
  }
303
}
107,764✔
304

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

308
  void *pIter = NULL;
149,270✔
309
  while (1) {
544,702✔
310
    SVgObj *pVgroup = NULL;
693,972✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
693,972✔
312
    if (pIter == NULL) break;
693,972✔
313

314
    bool stateChanged = false;
544,702✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,420,523✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,113,623✔
317
      if (pGid->dnodeId == dnodeId) {
1,113,623✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
237,802✔
319
          mInfo(
61,333✔
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;
61,333✔
325
          pGid->syncRestore = 0;
61,333✔
326
          pGid->syncCanRead = 0;
61,333✔
327
          pGid->startTimeMs = 0;
61,333✔
328
          pGid->learnerProgress = 0;
61,333✔
329
          pGid->snapSeq = -1;
61,333✔
330
          stateChanged = true;
61,333✔
331
        }
332
        break;
237,802✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
544,702✔
347
  }
348
}
149,270✔
349

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

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

357
  void *pIter = NULL;
6,380,951✔
358
  while (1) {
10,321,171✔
359
    SDnodeObj *pDnode = NULL;
16,702,122✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
16,702,122✔
361
    if (pIter == NULL) break;
16,702,122✔
362

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

369
    sdbRelease(pSdb, pDnode);
10,321,171✔
370
  }
371

372
  mndReleaseRpc(pMnode);
6,380,951✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
374,327,388✔
376
  terrno = 0;
374,327,388✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
374,327,388✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
374,327,471✔
379
  if (terrno != 0) {
374,327,293✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
374,324,756✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13,204,217✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
13,204,217✔
387
    return true;
13,204,217✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
361,120,539✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,212✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
5,009✔
392
    return true;
5,009✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
361,115,543✔
395
  return false;
361,116,557✔
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,270,975✔
415
  int32_t code = 0;
33,270,975✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
33,270,975✔
418
    mndPullupGrant(pMnode);
1,636,426✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
33,270,975✔
421
    mndPullupTtl(pMnode);
3,479,409✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
33,270,975✔
425
    mndPullupTrimDb(pMnode);
10,157✔
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
33,270,975✔
429
    mndPullupQueryTrimDb(pMnode);
3,382,884✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
33,270,975✔
434
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
435
      mndPullupUpdateSsMigrateProgress(pMnode);
×
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
33,270,975✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
33,270,975✔
450
    mndPullupTrans(pMnode);
16,631,473✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
33,270,975✔
454
    mndPullupCompacts(pMnode);
3,327,309✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
33,270,975✔
458
    mndPullupScans(pMnode);
3,327,309✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
33,270,975✔
461
    mndPullupInstances(pMnode);
6,655,069✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
33,270,975✔
465
    mndCalMqRebalance(pMnode);
16,631,412✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
33,270,975✔
469
    mndPullupTelem(pMnode);
162✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
33,270,975✔
472
    mndIncreaseUpTime(pMnode);
107,764✔
473
  }
474
}
33,270,975✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
327,847,466✔
477
  int32_t code = 0;
327,847,466✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
327,847,466✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
16,217,434✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
327,847,466✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
10,755,365✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
327,847,466✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
327,847,466✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
327,847,466✔
495
    mndCheckDnodeOffline(pMnode);
6,380,951✔
496
  }
497
}
327,847,466✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
33,270,975✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
33,270,975✔
501
    mndSyncCheckTimeout(pMnode);
1,104,181✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
33,270,975✔
504
    msmHealthCheck(pMnode);
11,081,246✔
505
  }
506
}
33,270,975✔
507

508
static void *mndThreadSecFp(void *param) {
401,108✔
509
  SMnode *pMnode = param;
401,108✔
510
  int64_t lastSec = 0;
401,108✔
511
  setThreadName("mnode-timer");
401,108✔
512

513
  while (1) {
340,252,923✔
514
    if (mndGetStop(pMnode)) break;
340,654,031✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
340,252,923✔
517
    if (nowSec == lastSec) {
340,252,923✔
518
      taosMsleep(100);
305,738,826✔
519
      continue;
305,738,826✔
520
    }
521
    lastSec = nowSec;
34,514,097✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
34,514,097✔
524
      taosMsleep(100);
1,243,122✔
525
      mTrace("timer not process since mnode is not leader");
1,243,122✔
526
      continue;
1,243,122✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
33,270,975✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
33,270,975✔
532

533
    taosMsleep(100);
33,270,975✔
534
  }
535

536
  return NULL;
401,108✔
537
}
538

539
static void *mndThreadMsFp(void *param) {
401,108✔
540
  SMnode *pMnode = param;
401,108✔
541
  int64_t lastTime = 0;
401,108✔
542
  setThreadName("mnode-arb-timer");
401,108✔
543

544
  while (1) {
545
    lastTime += 100;
340,214,678✔
546
    taosMsleep(100);
340,214,678✔
547

548
    if (mndGetStop(pMnode)) break;
340,214,678✔
549
    if (lastTime % 10 != 0) continue;
339,813,570✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
339,813,570✔
552
      mTrace("timer not process since mnode is not leader");
11,966,104✔
553
      continue;
11,966,104✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
327,847,466✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
327,847,466✔
559
  }
560

561
  return NULL;
401,108✔
562
}
563

564
static int32_t mndInitTimer(SMnode *pMnode) {
401,108✔
565
  int32_t      code = 0;
401,108✔
566
  TdThreadAttr thAttr;
400,977✔
567
  (void)taosThreadAttrInit(&thAttr);
401,108✔
568
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
401,108✔
569
#ifdef TD_COMPACT_OS
570
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
571
#endif
572
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
401,108✔
573
    mError("failed to create timer thread since %s", tstrerror(code));
×
574
    TAOS_RETURN(code);
×
575
  }
576

577
  (void)taosThreadAttrDestroy(&thAttr);
401,108✔
578
  tmsgReportStartup("mnode-timer", "initialized");
401,108✔
579

580
  TdThreadAttr arbAttr;
400,977✔
581
  (void)taosThreadAttrInit(&arbAttr);
401,108✔
582
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
401,108✔
583
#ifdef TD_COMPACT_OS
584
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
585
#endif
586
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
401,108✔
587
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
588
    TAOS_RETURN(code);
×
589
  }
590

591
  (void)taosThreadAttrDestroy(&arbAttr);
401,108✔
592
  tmsgReportStartup("mnode-timer", "initialized");
401,108✔
593
  TAOS_RETURN(code);
401,108✔
594
}
595

596
static void mndCleanupTimer(SMnode *pMnode) {
401,108✔
597
  if (taosCheckPthreadValid(pMnode->thread)) {
401,108✔
598
    (void)taosThreadJoin(pMnode->thread, NULL);
401,108✔
599
    taosThreadClear(&pMnode->thread);
401,108✔
600
  }
601
  if (taosCheckPthreadValid(pMnode->arbThread)) {
401,108✔
602
    (void)taosThreadJoin(pMnode->arbThread, NULL);
401,108✔
603
    taosThreadClear(&pMnode->arbThread);
401,108✔
604
  }
605
}
401,108✔
606

607
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
401,165✔
608
  int32_t code = 0;
401,165✔
609
  pMnode->path = taosStrdup(path);
401,165✔
610
  if (pMnode->path == NULL) {
401,165✔
611
    code = terrno;
×
612
    TAOS_RETURN(code);
×
613
  }
614

615
  if (taosMkDir(pMnode->path) != 0) {
401,165✔
616
    code = terrno;
×
617
    TAOS_RETURN(code);
×
618
  }
619

620
  TAOS_RETURN(code);
401,165✔
621
}
622

623
static int32_t mndInitWal(SMnode *pMnode) {
401,165✔
624
  int32_t code = 0;
401,165✔
625
  char    path[PATH_MAX + 20] = {0};
401,165✔
626
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
401,165✔
627
  SWalCfg cfg = {.vgId = 1,
401,165✔
628
                 .fsyncPeriod = 0,
629
                 .rollPeriod = -1,
630
                 .segSize = -1,
631
                 .committed = -1,
632
                 .retentionPeriod = 0,
633
                 .retentionSize = 0,
634
                 .level = TAOS_WAL_FSYNC,
635
                 .encryptAlgr = 0,
636
                 .encryptData = {0}};
637

638
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
639
  if (taosWaitCfgKeyLoaded() != 0) {
401,165✔
640
    code = terrno;
×
641
    TAOS_RETURN(code);
×
642
  }
643
  if (tsMetaKey[0] != '\0') {
401,165✔
644
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
2,938✔
645
  }
646
#endif
647

648
  pMnode->pWal = walOpen(path, &cfg);
401,165✔
649
  if (pMnode->pWal == NULL) {
401,165✔
650
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
651
    if (terrno != 0) code = terrno;
×
652
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
653
    TAOS_RETURN(code);
×
654
  }
655

656
  TAOS_RETURN(code);
401,165✔
657
}
658

659
static void mndCloseWal(SMnode *pMnode) {
401,108✔
660
  if (pMnode->pWal != NULL) {
401,108✔
661
    walClose(pMnode->pWal);
401,108✔
662
    pMnode->pWal = NULL;
401,108✔
663
  }
664
}
401,108✔
665

666
// Forward declarations for mmFile.c functions
667
extern int32_t mmReadFile(const char *path, SMnodeOpt *pOption);
668
extern int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption);
669

670
// Callback function to persist encrypted flag to mnode.json
671
static int32_t mndPersistEncryptedFlag(void *param) {
4,434✔
672
  SMnode *pMnode = (SMnode *)param;
4,434✔
673
  if (pMnode == NULL) {
4,434✔
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  
677
  mInfo("persisting encrypted flag to mnode.json");
4,434✔
678
  
679
  SMnodeOpt option = {0};
4,434✔
680
  int32_t code = mmReadFile(pMnode->path, &option);
4,434✔
681
  if (code != 0) {
4,434✔
682
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
683
    return code;
×
684
  }
685
  
686
  option.encrypted = true;
4,434✔
687
  code = mmWriteFile(pMnode->path, &option);
4,434✔
688
  if (code != 0) {
4,434✔
689
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
690
    return code;
×
691
  }
692
  
693
  // Also update mnode's encrypted flag
694
  pMnode->encrypted = true;
4,434✔
695
  
696
  mInfo("successfully persisted encrypted flag to mnode.json");
4,434✔
697
  return 0;
4,434✔
698
}
699

700
static int32_t mndInitSdb(SMnode *pMnode) {
401,165✔
701
  int32_t code = 0;
401,165✔
702
  SSdbOpt opt = {0};
401,165✔
703
  opt.path = pMnode->path;
401,165✔
704
  opt.pMnode = pMnode;
401,165✔
705
  opt.pWal = pMnode->pWal;
401,165✔
706

707
  pMnode->pSdb = sdbInit(&opt);
401,165✔
708
  if (pMnode->pSdb == NULL) {
401,165✔
709
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
710
    if (terrno != 0) code = terrno;
×
711
    TAOS_RETURN(code);
×
712
  }
713

714
  TAOS_RETURN(code);
401,165✔
715
}
716

717
static int32_t mndOpenSdb(SMnode *pMnode) {
401,165✔
718
  int32_t code = 0;
401,165✔
719
  
720
  pMnode->pSdb->encrypted = pMnode->encrypted;
401,165✔
721
  
722
  // Set callback for persisting encrypted flag
723
  pMnode->pSdb->persistEncryptedFlagFp = mndPersistEncryptedFlag;
401,165✔
724
  pMnode->pSdb->pMnodeForCallback = pMnode;
401,165✔
725

726
  if (!pMnode->deploy) {
401,165✔
727
    code = sdbReadFile(pMnode->pSdb);
115,555✔
728
  }
729

730
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
401,165✔
731

732
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
401,165✔
733
  return code;
401,165✔
734
}
735

736
static void mndCleanupSdb(SMnode *pMnode) {
401,108✔
737
  if (pMnode->pSdb) {
401,108✔
738
    sdbCleanup(pMnode->pSdb);
401,108✔
739
    pMnode->pSdb = NULL;
401,108✔
740
  }
741
}
401,108✔
742

743
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
20,058,250✔
744
  SMnodeStep step = {0};
20,058,250✔
745
  step.name = name;
20,058,250✔
746
  step.initFp = initFp;
20,058,250✔
747
  step.cleanupFp = cleanupFp;
20,058,250✔
748
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
40,116,500✔
749
    TAOS_RETURN(terrno);
×
750
  }
751

752
  TAOS_RETURN(0);
20,058,250✔
753
}
754

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

811
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
401,108✔
812
  if (pMnode->pSteps == NULL) return;
401,108✔
813

814
  if (pos == -1) {
401,108✔
815
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
401,108✔
816
  }
817

818
  for (int32_t s = pos; s >= 0; s--) {
20,456,508✔
819
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
20,055,400✔
820
    mInfo("%s will cleanup", pStep->name);
20,055,400✔
821
    if (pStep->cleanupFp != NULL) {
20,055,400✔
822
      (*pStep->cleanupFp)(pMnode);
19,253,184✔
823
    }
824
  }
825

826
  taosArrayClear(pMnode->pSteps);
401,108✔
827
  taosArrayDestroy(pMnode->pSteps);
401,108✔
828
  pMnode->pSteps = NULL;
401,108✔
829
}
830

831
static int32_t mndExecSteps(SMnode *pMnode) {
401,165✔
832
  int32_t code = 0;
401,165✔
833
  int32_t size = taosArrayGetSize(pMnode->pSteps);
401,165✔
834
  for (int32_t pos = 0; pos < size; pos++) {
20,459,415✔
835
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
20,058,250✔
836
    if (pStep->initFp == NULL) continue;
20,058,250✔
837

838
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
20,058,250✔
839
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
840
      mndCleanupSteps(pMnode, pos);
×
841
      TAOS_RETURN(code);
×
842
    } else {
843
      mInfo("%s is initialized", pStep->name);
20,058,250✔
844
      tmsgReportStartup(pStep->name, "initialized");
20,058,250✔
845
    }
846
  }
847

848
  pMnode->clusterId = mndGetClusterId(pMnode);
401,165✔
849
  TAOS_RETURN(0);
401,165✔
850
}
851

852
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
401,165✔
853
  pMnode->msgCb = pOption->msgCb;
401,165✔
854
  pMnode->selfDnodeId = pOption->dnodeId;
401,165✔
855
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
401,165✔
856
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
401,165✔
857
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
401,165✔
858
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
401,165✔
859
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
401,165✔
860
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
401,165✔
861
  pMnode->encrypted = pOption->encrypted;
401,165✔
862
}
401,165✔
863

864
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
401,165✔
865
  terrno = 0;
401,165✔
866
  mInfo("start to open mnode in %s", path);
401,165✔
867

868
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
401,165✔
869
  if (pMnode == NULL) {
401,165✔
870
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
871
    mError("failed to open mnode in step 1, since %s", terrstr());
×
872
    return NULL;
×
873
  }
874
  (void)memset(pMnode, 0, sizeof(SMnode));
401,165✔
875

876
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
401,165✔
877
  if (code != 0) {
401,165✔
878
    taosMemoryFree(pMnode);
×
879
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
880
    terrno = code;
×
881
    return NULL;
×
882
  }
883

884
  char timestr[24] = "1970-01-01 00:00:00.00";
401,165✔
885
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
401,165✔
886
  if (code < 0) {
401,165✔
887
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
888
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
889
    taosMemoryFree(pMnode);
×
890
    terrno = code;
×
891
    return NULL;
×
892
  }
893

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

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

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

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

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

932
  mInfo("mnode open successfully");
401,165✔
933
  return pMnode;
401,165✔
934
}
935

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

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

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

980
  return mndInitTimer(pMnode);
401,108✔
981
}
982

983
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
401,108✔
984

985
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
314,246✔
986

987
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
314,246✔
988

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

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

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

1004
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
27,068,353✔
1005

1006
void mndStop(SMnode *pMnode) {
401,108✔
1007
  mndSetStop(pMnode);
401,108✔
1008
  mndSyncStop(pMnode);
401,108✔
1009
  mndCleanupTimer(pMnode);
401,108✔
1010
}
401,108✔
1011

1012
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
55,874,371✔
1013
  SMnode    *pMnode = pMsg->info.node;
55,874,371✔
1014
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
55,874,533✔
1015

1016
  const STraceId *trace = &pMsg->info.traceId;
55,874,533✔
1017
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
55,874,371✔
1018

1019
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
55,874,371✔
1020
  if (code != 0) {
55,874,533✔
1021
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
607✔
1022
            tstrerror(code), code);
1023
  }
1024

1025
  return code;
55,874,533✔
1026
}
1027

1028
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
286,437,516✔
1029
  int32_t code = 0;
286,437,516✔
1030
  if (!IsReq(pMsg)) TAOS_RETURN(code);
286,437,516✔
1031
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
252,214,732✔
1032
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
247,401,260✔
1033
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
243,430,780✔
1034
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
233,957,733✔
1035
    TAOS_RETURN(code);
18,258,048✔
1036
  }
1037

1038
  SMnode *pMnode = pMsg->info.node;
233,957,852✔
1039
  (void)taosThreadRwlockRdlock(&pMnode->lock);
233,956,369✔
1040
  if (pMnode->stopped) {
233,958,336✔
1041
    (void)taosThreadRwlockUnlock(&pMnode->lock);
131,395✔
1042
    code = TSDB_CODE_APP_IS_STOPPING;
131,395✔
1043
    TAOS_RETURN(code);
131,395✔
1044
  }
1045

1046
  terrno = 0;
233,826,101✔
1047
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
233,825,690✔
1048
  if (terrno != 0) {
233,827,393✔
1049
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1050
    code = terrno;
×
1051
    TAOS_RETURN(code);
×
1052
  }
1053

1054
  if (state.state != TAOS_SYNC_STATE_LEADER) {
233,825,713✔
1055
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,749,327✔
1056
    code = TSDB_CODE_SYN_NOT_LEADER;
1,749,327✔
1057
    goto _OVER;
1,749,327✔
1058
  }
1059

1060
  if (!state.restored || !pMnode->restored) {
232,076,386✔
1061
    (void)taosThreadRwlockUnlock(&pMnode->lock);
717,483✔
1062
    code = TSDB_CODE_SYN_RESTORING;
715,399✔
1063
    goto _OVER;
715,399✔
1064
  }
1065

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

1073
  (void)taosThreadRwlockUnlock(&pMnode->lock);
231,362,100✔
1074
  TAOS_RETURN(code);
231,362,449✔
1075

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

1091
  const STraceId *trace = &pMsg->info.traceId;
2,464,726✔
1092
  SEpSet          epSet = {0};
2,464,570✔
1093
  mndGetMnodeEpSet(pMnode, &epSet);
2,464,570✔
1094

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

1101
  if (epSet.numOfEps <= 0) return -1;
2,464,801✔
1102

1103
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
8,312,385✔
1104
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
5,847,584✔
1105
  }
1106

1107
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,464,801✔
1108
  pMsg->info.rsp = rpcMallocCont(contLen);
2,464,801✔
1109
  if (pMsg->info.rsp != NULL) {
2,464,722✔
1110
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,464,801✔
1111
      mError("failed to serialize ep set");
×
1112
    }
1113
    pMsg->info.hasEpSet = 1;
2,464,355✔
1114
    pMsg->info.rspLen = contLen;
2,463,644✔
1115
  }
1116

1117
  TAOS_RETURN(code);
2,464,005✔
1118
}
1119

1120
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
286,445,080✔
1121
  SMnode         *pMnode = pMsg->info.node;
286,445,080✔
1122
  const STraceId *trace = &pMsg->info.traceId;
286,446,238✔
1123
  int32_t         code = TSDB_CODE_SUCCESS;
286,445,925✔
1124

1125
#ifdef TD_ENTERPRISE
1126
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
286,445,925✔
1127
    SCachedTokenInfo ti = {0};
18,271✔
1128
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
18,271✔
1129
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,049✔
1130
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
2,049✔
1131
      TAOS_RETURN(code);
2,049✔
1132
    }
1133
    if (ti.enabled == 0) {
16,222✔
1134
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,518✔
1135
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,518✔
1136
      TAOS_RETURN(code);
2,518✔
1137
    }
1138
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
13,704✔
1139
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1140
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1141
      TAOS_RETURN(code);
×
1142
    }
1143
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
13,704✔
1144
  }
1145
#endif
1146

1147
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
286,439,329✔
1148
  MndMsgFpExt fpExt = NULL;
286,439,688✔
1149
  if (fp == NULL) {
286,439,688✔
1150
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
18,258,716✔
1151
    if (fpExt == NULL) {
18,258,716✔
1152
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1153
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1154
      TAOS_RETURN(code);
×
1155
    }
1156
  }
1157

1158
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
286,439,688✔
1159

1160
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
283,840,147✔
1161
  if (fp)
283,841,005✔
1162
    code = (*fp)(pMsg);
265,582,289✔
1163
  else
1164
    code = (*fpExt)(pMsg, pQueueInfo);
18,258,716✔
1165
  mndReleaseRpc(pMnode);
283,845,124✔
1166

1167
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
283,845,112✔
1168
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
29,163,279✔
1169
  } else if (code == 0) {
254,681,833✔
1170
    mGTrace("msg:%p, successfully processed", pMsg);
252,133,250✔
1171
  } else {
1172
    // TODO removve this wrong set code
1173
    if (code == -1) {
2,548,583✔
1174
      code = terrno;
×
1175
    }
1176
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,548,583✔
1177
            TMSG_INFO(pMsg->msgType));
1178
  }
1179

1180
  TAOS_RETURN(code);
283,845,112✔
1181
}
1182

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

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

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

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

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

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

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

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

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

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

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

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

1291
  // vgroup info
1292
  pIter = NULL;
54✔
1293
  while (1) {
108✔
1294
    SVgObj *pVgroup = NULL;
162✔
1295
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
162✔
1296
    if (pIter == NULL) break;
162✔
1297

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

1303
    pClusterInfo->vgroups_total++;
108✔
1304
    pClusterInfo->tbs_total += pVgroup->numOfTables;
108✔
1305

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

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

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

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

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

1351
    SMonStbDesc desc = {0};
×
1352

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

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

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

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

1385
  mndReleaseRpc(pMnode);
54✔
1386
  TAOS_RETURN(code);
54✔
1387
}
1388

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

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

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

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

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

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

1437
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
680,852,819✔
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