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

taosdata / TDengine / #4898

26 Dec 2025 09:58AM UTC coverage: 65.061% (-0.7%) from 65.717%
#4898

push

travis-ci

web-flow
feat: support encryption of configuration files, data files and metadata files (#33801)

350 of 1333 new or added lines in 31 files covered. (26.26%)

2796 existing lines in 159 files now uncovered.

184024 of 282850 relevant lines covered (65.06%)

113940470.33 hits per line

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

78.95
/source/dnode/mnode/impl/src/mndMain.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndAcct.h"
18
#include "mndAnode.h"
19
#include "mndArbGroup.h"
20
#include "mndBnode.h"
21
#include "mndCluster.h"
22
#include "mndCompact.h"
23
#include "mndCompactDetail.h"
24
#include "mndConfig.h"
25
#include "mndConsumer.h"
26
#include "mndDb.h"
27
#include "mndDnode.h"
28
#include "mndEncryptAlgr.h"
29
#include "mndFunc.h"
30
#include "mndGrant.h"
31
#include "mndIndex.h"
32
#include "mndInfoSchema.h"
33
#include "mndInstance.h"
34
#include "mndMnode.h"
35
#include "mndMount.h"
36
#include "mndPerfSchema.h"
37
#include "mndPrivilege.h"
38
#include "mndProfile.h"
39
#include "mndQnode.h"
40
#include "mndQuery.h"
41
#include "mndRetention.h"
42
#include "mndRetentionDetail.h"
43
#include "mndRsma.h"
44
#include "mndScan.h"
45
#include "mndScanDetail.h"
46
#include "mndShow.h"
47
#include "mndSma.h"
48
#include "mndSnode.h"
49
#include "mndSsMigrate.h"
50
#include "mndStb.h"
51
#include "mndStream.h"
52
#include "mndSubscribe.h"
53
#include "mndSync.h"
54
#include "mndTelem.h"
55
#include "mndTopic.h"
56
#include "mndTrans.h"
57
#include "mndUser.h"
58
#include "mndToken.h"
59
#include "mndVgroup.h"
60
#include "mndView.h"
61
#include "tencrypt.h"
62

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

82
static inline void mndReleaseRpc(SMnode *pMnode) {
232,173,901✔
83
  (void)taosThreadRwlockRdlock(&pMnode->lock);
232,173,901✔
84
#if 1
85
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
232,176,851✔
86
#else
87
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
88
  mTrace("mnode rpc is released, ref:%d", ref);
89
#endif
90
  (void)taosThreadRwlockUnlock(&pMnode->lock);
232,176,191✔
91
}
232,176,812✔
92

93
static void *mndBuildTimerMsg(int32_t *pContLen) {
57,443,447✔
94
  terrno = 0;
57,443,447✔
95
  SMTimerReq timerReq = {0};
57,443,548✔
96

97
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
57,443,244✔
98
  if (contLen <= 0) return NULL;
57,443,035✔
99
  void *pReq = rpcMallocCont(contLen);
57,443,035✔
100
  if (pReq == NULL) return NULL;
57,442,190✔
101

102
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
57,442,190✔
103
    mError("failed to serialize timer msg since %s", terrstr());
×
104
  }
105
  *pContLen = contLen;
57,442,946✔
106
  return pReq;
57,442,933✔
107
}
108

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

303
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
112,333✔
304
  SSdb *pSdb = pMnode->pSdb;
112,333✔
305

306
  void *pIter = NULL;
112,333✔
307
  while (1) {
379,015✔
308
    SVgObj *pVgroup = NULL;
491,348✔
309
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
491,348✔
310
    if (pIter == NULL) break;
491,348✔
311

312
    bool stateChanged = false;
379,015✔
313
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
961,501✔
314
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
727,288✔
315
      if (pGid->dnodeId == dnodeId) {
727,288✔
316
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
144,802✔
317
          mInfo(
57,325✔
318
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
319
              "restored:0 "
320
              "canRead:0",
321
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
322
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
57,325✔
323
          pGid->syncRestore = 0;
57,325✔
324
          pGid->syncCanRead = 0;
57,325✔
325
          pGid->startTimeMs = 0;
57,325✔
326
          stateChanged = true;
57,325✔
327
        }
328
        break;
144,802✔
329
      }
330
    }
331

332
    if (stateChanged) {
379,015✔
333
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
57,325✔
334
      if (pDb != NULL && pDb->stateTs != curMs) {
57,325✔
335
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
27,667✔
336
              curMs);
337
        pDb->stateTs = curMs;
27,667✔
338
      }
339
      mndReleaseDb(pMnode, pDb);
57,325✔
340
    }
341

342
    sdbRelease(pSdb, pVgroup);
379,015✔
343
  }
344
}
112,333✔
345

346
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,565,449✔
347
  mTrace("check dnode offline");
4,565,449✔
348
  if (mndAcquireRpc(pMnode) != 0) return;
4,565,449✔
349

350
  SSdb   *pSdb = pMnode->pSdb;
4,565,449✔
351
  int64_t curMs = taosGetTimestampMs();
4,565,449✔
352

353
  void *pIter = NULL;
4,565,449✔
354
  while (1) {
8,020,273✔
355
    SDnodeObj *pDnode = NULL;
12,585,722✔
356
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,585,722✔
357
    if (pIter == NULL) break;
12,585,722✔
358

359
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,020,273✔
360
    if (!online) {
8,020,273✔
361
      mInfo("dnode:%d, in offline state", pDnode->id);
112,333✔
362
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
112,333✔
363
    }
364

365
    sdbRelease(pSdb, pDnode);
8,020,273✔
366
  }
367

368
  mndReleaseRpc(pMnode);
4,565,449✔
369
}
370

371
static bool mnodeIsNotLeader(SMnode *pMnode) {
272,770,709✔
372
  terrno = 0;
272,770,709✔
373
  (void)taosThreadRwlockRdlock(&pMnode->lock);
272,771,263✔
374
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
272,774,408✔
375
  if (terrno != 0) {
272,775,901✔
376
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
377
    return true;
×
378
  }
379

380
  if (state.state != TAOS_SYNC_STATE_LEADER) {
272,775,159✔
381
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,831,856✔
382
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,831,856✔
383
    return true;
11,831,856✔
384
  }
385
  if (!state.restored || !pMnode->restored) {
260,943,303✔
386
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,221✔
387
    terrno = TSDB_CODE_SYN_RESTORING;
1,631✔
388
    return true;
1,631✔
389
  }
390
  (void)taosThreadRwlockUnlock(&pMnode->lock);
260,941,082✔
391
  return false;
260,942,241✔
392
}
393

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

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

408
  return min <= 1 ? 2 : min;
×
409
}
410
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
23,581,110✔
411
  int32_t code = 0;
23,581,110✔
412
#ifndef TD_ASTRA
413
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
23,581,110✔
414
    mndPullupGrant(pMnode);
1,117,569✔
415
  }
416
  if (sec % tsTtlPushIntervalSec == 0) {
23,581,110✔
417
    mndPullupTtl(pMnode);
2,275,868✔
418
  }
419

420
  if (sec % tsTrimVDbIntervalSec == 0) {
23,581,110✔
421
    mndPullupTrimDb(pMnode);
×
422
  }
423

424
  if (sec % tsQueryTrimIntervalSec == 0) {
23,581,110✔
425
    mndPullupQueryTrimDb(pMnode);
2,251,316✔
426
  }
427
#endif
428
#ifdef USE_SHARED_STORAGE
429
  if (tsSsEnabled) {
23,581,110✔
430
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
431
      mndPullupUpdateSsMigrateProgress(pMnode);
×
432
    }
433
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
434
      mndPullupSsMigrateDb(pMnode);
×
435
    }
436
  }
437
#endif
438
#ifdef TD_ENTERPRISE
439
  if (tsAuthReq) {
23,581,110✔
440
    if (sec % tsAuthReqHBInterval == 0) {
×
441
      mndPullupAuth(pMnode);
×
442
    }
443
  }
444
#endif
445
  if (sec % tsTransPullupInterval == 0) {
23,581,110✔
446
    mndPullupTrans(pMnode);
11,694,939✔
447
  }
448

449
  if (sec % tsCompactPullupInterval == 0) {
23,581,110✔
450
    mndPullupCompacts(pMnode);
2,191,374✔
451
  }
452

453
  if (sec % tsScanPullupInterval == 0) {
23,581,110✔
454
    mndPullupScans(pMnode);
2,191,374✔
455
  }
456
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
23,581,110✔
457
    mndPullupInstances(pMnode);
4,565,058✔
458
  }
459
#ifdef USE_TOPIC
460
  if (sec % tsMqRebalanceInterval == 0) {
23,581,110✔
461
    mndCalMqRebalance(pMnode);
11,694,775✔
462
  }
463
#endif
464
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
23,581,110✔
465
    mndPullupTelem(pMnode);
162✔
466
  }
467
  if (sec % tsUptimeInterval == 0) {
23,581,110✔
468
    mndIncreaseUpTime(pMnode);
18,380✔
469
  }
470
}
23,581,110✔
471

472
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
237,361,304✔
473
  int32_t code = 0;
237,361,304✔
474
#ifndef TD_ASTRA
475
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
237,361,304✔
476
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,692,494✔
477
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
478
    }
479
  }
480

481
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
237,361,304✔
482
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,750,628✔
483
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
484
    }
485
  }
486
#endif
487
}
237,361,304✔
488

489
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
237,361,304✔
490
  if (ms % (tsStatusTimeoutMs) == 0) {
237,361,304✔
491
    mndCheckDnodeOffline(pMnode);
4,565,449✔
492
  }
493
}
237,361,304✔
494

495
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
23,581,110✔
496
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
23,581,110✔
497
    mndSyncCheckTimeout(pMnode);
635,446✔
498
  }
499
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
23,581,110✔
500
    msmHealthCheck(pMnode);
7,752,097✔
501
  }
502
}
23,581,110✔
503

504
static void *mndThreadSecFp(void *param) {
401,587✔
505
  SMnode *pMnode = param;
401,587✔
506
  int64_t lastTime = 0;
401,587✔
507
  setThreadName("mnode-timer");
401,587✔
508

509
  while (1) {
248,166,416✔
510
    lastTime++;
248,568,003✔
511
    taosMsleep(100);
248,568,003✔
512

513
    if (mndGetStop(pMnode)) break;
248,568,003✔
514
    if (lastTime % 10 != 0) continue;
248,166,416✔
515

516
    if (mnodeIsNotLeader(pMnode)) {
24,641,521✔
517
      mTrace("timer not process since mnode is not leader");
1,060,411✔
518
      continue;
1,060,411✔
519
    }
520

521
    int64_t sec = lastTime / 10;
23,581,110✔
522
    mndDoTimerCheckSync(pMnode, sec);
23,581,110✔
523

524
    mndDoTimerPullupTask(pMnode, sec);
23,581,110✔
525
  }
526

527
  return NULL;
401,587✔
528
}
529

530
static void *mndThreadMsFp(void *param) {
401,587✔
531
  SMnode *pMnode = param;
401,587✔
532
  int64_t lastTime = 0;
401,587✔
533
  setThreadName("mnode-arb-timer");
401,587✔
534

535
  while (1) {
536
    lastTime += 100;
248,535,967✔
537
    taosMsleep(100);
248,535,967✔
538

539
    if (mndGetStop(pMnode)) break;
248,535,967✔
540
    if (lastTime % 10 != 0) continue;
248,134,380✔
541

542
    if (mnodeIsNotLeader(pMnode)) {
248,134,380✔
543
      mTrace("timer not process since mnode is not leader");
10,773,076✔
544
      continue;
10,773,076✔
545
    }
546

547
    mndDoTimerCheckStatus(pMnode, lastTime);
237,361,304✔
548

549
    mndDoArbTimerPullupTask(pMnode, lastTime);
237,361,304✔
550
  }
551

552
  return NULL;
401,587✔
553
}
554

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

568
  (void)taosThreadAttrDestroy(&thAttr);
401,587✔
569
  tmsgReportStartup("mnode-timer", "initialized");
401,587✔
570

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

582
  (void)taosThreadAttrDestroy(&arbAttr);
401,587✔
583
  tmsgReportStartup("mnode-timer", "initialized");
401,587✔
584
  TAOS_RETURN(code);
401,587✔
585
}
586

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

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

606
  if (taosMkDir(pMnode->path) != 0) {
401,651✔
607
    code = terrno;
×
608
    TAOS_RETURN(code);
×
609
  }
610

611
  TAOS_RETURN(code);
401,651✔
612
}
613

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

629
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
630
  if (taosWaitCfgKeyLoaded() != 0) {
401,651✔
NEW
631
    code = terrno;
×
NEW
632
    TAOS_RETURN(code);
×
633
  }
634
  if (tsMetaKey[0] != '\0') {
401,651✔
NEW
635
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
×
636
  }
637
#endif
638

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

647
  TAOS_RETURN(code);
401,651✔
648
}
649

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

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

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

671
  TAOS_RETURN(code);
401,651✔
672
}
673

674
static int32_t mndOpenSdb(SMnode *pMnode) {
401,651✔
675
  int32_t code = 0;
401,651✔
676
  if (!pMnode->deploy) {
401,651✔
677
    code = sdbReadFile(pMnode->pSdb);
113,801✔
678
  }
679

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

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

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

693
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,279,248✔
694
  SMnodeStep step = {0};
19,279,248✔
695
  step.name = name;
19,279,248✔
696
  step.initFp = initFp;
19,279,248✔
697
  step.cleanupFp = cleanupFp;
19,279,248✔
698
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
38,558,496✔
699
    TAOS_RETURN(terrno);
×
700
  }
701

702
  TAOS_RETURN(0);
19,279,248✔
703
}
704

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

759
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
401,587✔
760
  if (pMnode->pSteps == NULL) return;
401,587✔
761

762
  if (pos == -1) {
401,587✔
763
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
401,587✔
764
  }
765

766
  for (int32_t s = pos; s >= 0; s--) {
19,677,763✔
767
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,276,176✔
768
    mInfo("%s will cleanup", pStep->name);
19,276,176✔
769
    if (pStep->cleanupFp != NULL) {
19,276,176✔
770
      (*pStep->cleanupFp)(pMnode);
18,473,002✔
771
    }
772
  }
773

774
  taosArrayClear(pMnode->pSteps);
401,587✔
775
  taosArrayDestroy(pMnode->pSteps);
401,587✔
776
  pMnode->pSteps = NULL;
401,587✔
777
}
778

779
static int32_t mndExecSteps(SMnode *pMnode) {
401,651✔
780
  int32_t code = 0;
401,651✔
781
  int32_t size = taosArrayGetSize(pMnode->pSteps);
401,651✔
782
  for (int32_t pos = 0; pos < size; pos++) {
19,680,899✔
783
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,279,248✔
784
    if (pStep->initFp == NULL) continue;
19,279,248✔
785

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

796
  pMnode->clusterId = mndGetClusterId(pMnode);
401,651✔
797
  TAOS_RETURN(0);
401,651✔
798
}
799

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

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

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

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

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

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

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

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

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

876
  mInfo("mnode open successfully");
401,651✔
877
  return pMnode;
401,651✔
878
}
879

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

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

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

924
  return mndInitTimer(pMnode);
401,587✔
925
}
926

927
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
401,587✔
928

929
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
314,890✔
930

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

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

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

946
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
19,542,557✔
947

948
void mndStop(SMnode *pMnode) {
401,587✔
949
  mndSetStop(pMnode);
401,587✔
950
  mndSyncStop(pMnode);
401,587✔
951
  mndCleanupTimer(pMnode);
401,587✔
952
}
401,587✔
953

954
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
47,147,803✔
955
  SMnode    *pMnode = pMsg->info.node;
47,147,803✔
956
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
47,147,803✔
957

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

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

967
  return code;
47,147,803✔
968
}
969

970
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
230,510,457✔
971
  int32_t code = 0;
230,510,457✔
972
  if (!IsReq(pMsg)) TAOS_RETURN(code);
230,510,457✔
973
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
200,800,140✔
974
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
196,275,108✔
975
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
192,488,092✔
976
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
183,462,128✔
977
    TAOS_RETURN(code);
17,337,155✔
978
  }
979

980
  SMnode *pMnode = pMsg->info.node;
183,461,785✔
981
  (void)taosThreadRwlockRdlock(&pMnode->lock);
183,465,222✔
982
  if (pMnode->stopped) {
183,468,985✔
983
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,741✔
984
    code = TSDB_CODE_APP_IS_STOPPING;
2,741✔
985
    TAOS_RETURN(code);
2,741✔
986
  }
987

988
  terrno = 0;
183,457,597✔
989
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
183,462,832✔
990
  if (terrno != 0) {
183,469,764✔
991
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
992
    code = terrno;
×
993
    TAOS_RETURN(code);
×
994
  }
995

996
  if (state.state != TAOS_SYNC_STATE_LEADER) {
183,467,887✔
997
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,863,551✔
998
    code = TSDB_CODE_SYN_NOT_LEADER;
1,863,551✔
999
    goto _OVER;
1,863,551✔
1000
  }
1001

1002
  if (!state.restored || !pMnode->restored) {
181,604,336✔
1003
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,044,747✔
1004
    code = TSDB_CODE_SYN_RESTORING;
1,043,979✔
1005
    goto _OVER;
1,043,979✔
1006
  }
1007

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

1015
  (void)taosThreadRwlockUnlock(&pMnode->lock);
180,561,177✔
1016
  TAOS_RETURN(code);
180,560,119✔
1017

1018
_OVER:
2,907,530✔
1019
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,907,530✔
1020
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,907,530✔
1021
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,907,352✔
1022
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,907,373✔
1023
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,907,352✔
1024
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,907,270✔
1025
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,907,448✔
1026
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,907,448✔
1027
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,907,270✔
UNCOV
1028
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1029
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1030
    TAOS_RETURN(code);
×
1031
  }
1032

1033
  const STraceId *trace = &pMsg->info.traceId;
2,905,829✔
1034
  SEpSet          epSet = {0};
2,905,564✔
1035
  mndGetMnodeEpSet(pMnode, &epSet);
2,905,564✔
1036

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

1043
  if (epSet.numOfEps <= 0) return -1;
2,907,530✔
1044

1045
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
9,355,342✔
1046
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,447,812✔
1047
  }
1048

1049
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,907,530✔
1050
  pMsg->info.rsp = rpcMallocCont(contLen);
2,905,976✔
1051
  if (pMsg->info.rsp != NULL) {
2,906,707✔
1052
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,907,530✔
1053
      mError("failed to serialize ep set");
×
1054
    }
1055
    pMsg->info.hasEpSet = 1;
2,903,641✔
1056
    pMsg->info.rspLen = contLen;
2,902,999✔
1057
  }
1058

1059
  TAOS_RETURN(code);
2,905,364✔
1060
}
1061

1062
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
230,518,393✔
1063
  SMnode         *pMnode = pMsg->info.node;
230,518,393✔
1064
  const STraceId *trace = &pMsg->info.traceId;
230,521,118✔
1065
  int32_t         code = TSDB_CODE_SUCCESS;
230,518,971✔
1066

1067
#ifdef TD_ENTERPRISE
1068
  if (pMsg->info.conn.isToken) {
230,518,971✔
1069
    SCachedTokenInfo ti = {0};
×
1070
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
×
1071
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1072
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
×
1073
      TAOS_RETURN(code);
×
1074
    }
1075
    if (ti.enabled == 0) {
×
1076
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1077
      code = TSDB_CODE_MND_TOKEN_DISABLED;
×
1078
      TAOS_RETURN(code);
×
1079
    }
1080
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
×
1081
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1082
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1083
      TAOS_RETURN(code);
×
1084
    }
1085
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
×
1086
  }
1087
#endif
1088

1089
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
230,509,082✔
1090
  MndMsgFpExt fpExt = NULL;
230,517,643✔
1091
  if (fp == NULL) {
230,517,643✔
1092
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,338,997✔
1093
    if (fpExt == NULL) {
17,338,484✔
1094
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1095
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1096
      TAOS_RETURN(code);
×
1097
    }
1098
  }
1099

1100
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
230,517,130✔
1101

1102
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
227,603,720✔
1103
  if (fp)
227,601,189✔
1104
    code = (*fp)(pMsg);
210,262,705✔
1105
  else
1106
    code = (*fpExt)(pMsg, pQueueInfo);
17,338,484✔
1107
  mndReleaseRpc(pMnode);
227,610,320✔
1108

1109
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
227,611,896✔
1110
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,029,799✔
1111
  } else if (code == 0) {
200,582,097✔
1112
    mGTrace("msg:%p, successfully processed", pMsg);
198,093,485✔
1113
  } else {
1114
    // TODO removve this wrong set code
1115
    if (code == -1) {
2,488,612✔
1116
      code = terrno;
2,471✔
1117
    }
1118
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,488,612✔
1119
            TMSG_INFO(pMsg->msgType));
1120
  }
1121

1122
  TAOS_RETURN(code);
227,611,896✔
1123
}
1124

1125
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
84,748,361✔
1126
  tmsg_t type = TMSG_INDEX(msgType);
84,748,361✔
1127
  if (type < TDMT_MAX) {
84,748,361✔
1128
    pMnode->msgFp[type] = fp;
84,748,361✔
1129
  }
1130
}
84,748,361✔
1131

1132
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,213,208✔
1133
  tmsg_t type = TMSG_INDEX(msgType);
3,213,208✔
1134
  if (type < TDMT_MAX) {
3,213,208✔
1135
    pMnode->msgFpExt[type] = fp;
3,213,208✔
1136
  }
1137
}
3,213,208✔
1138

1139
// Note: uid 0 is reserved
1140
int64_t mndGenerateUid(const char *name, int32_t len) {
7,548,776✔
1141
  int32_t hashval = MurmurHash3_32(name, len);
7,548,776✔
1142
  do {
×
1143
    int64_t us = taosGetTimestampUs();
7,548,776✔
1144
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
7,548,776✔
1145
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
7,548,776✔
1146
    if (uuid) {
7,548,776✔
1147
      return llabs(uuid);
7,548,776✔
1148
    }
1149
  } while (true);
1150
}
1151

1152
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
82✔
1153
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1154
  int32_t code = mndAcquireRpc(pMnode);
82✔
1155
  if (code < 0) {
82✔
1156
    TAOS_RETURN(code);
×
1157
  } else if (code == 1) {
82✔
1158
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1159
  }
1160

1161
  SSdb   *pSdb = pMnode->pSdb;
82✔
1162
  int64_t ms = taosGetTimestampMs();
82✔
1163

1164
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
82✔
1165
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
82✔
1166
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
82✔
1167
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
82✔
1168
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
82✔
1169
      pStbInfo->stbs == NULL) {
82✔
1170
    mndReleaseRpc(pMnode);
×
1171
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1172
    if (terrno != 0) code = terrno;
×
1173
    TAOS_RETURN(code);
×
1174
  }
1175

1176
  // cluster info
1177
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
82✔
1178
  pClusterInfo->monitor_interval = tsMonitorInterval;
82✔
1179
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
82✔
1180
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
82✔
1181
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
82✔
1182
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
82✔
1183
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
82✔
1184

1185
  void *pIter = NULL;
82✔
1186
  while (1) {
82✔
1187
    SDnodeObj *pObj = NULL;
164✔
1188
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
164✔
1189
    if (pIter == NULL) break;
164✔
1190

1191
    SMonDnodeDesc desc = {0};
82✔
1192
    desc.dnode_id = pObj->id;
82✔
1193
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
82✔
1194
    if (mndIsDnodeOnline(pObj, ms)) {
82✔
1195
      tstrncpy(desc.status, "ready", sizeof(desc.status));
82✔
1196
    } else {
1197
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1198
    }
1199
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
164✔
1200
      mError("failed put dnode into array, but continue at this monitor report")
×
1201
    }
1202
    sdbRelease(pSdb, pObj);
82✔
1203
  }
1204

1205
  pIter = NULL;
82✔
1206
  while (1) {
82✔
1207
    SMnodeObj *pObj = NULL;
164✔
1208
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
164✔
1209
    if (pIter == NULL) break;
164✔
1210

1211
    SMonMnodeDesc desc = {0};
82✔
1212
    desc.mnode_id = pObj->id;
82✔
1213
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
82✔
1214

1215
    if (pObj->id == pMnode->selfDnodeId) {
82✔
1216
      pClusterInfo->first_ep_dnode_id = pObj->id;
82✔
1217
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
82✔
1218
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1219
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
82✔
1220
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1221
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
82✔
1222
      desc.syncState = TAOS_SYNC_STATE_LEADER;
82✔
1223
    } else {
1224
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1225
      desc.syncState = pObj->syncState;
×
1226
    }
1227
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
164✔
1228
      mError("failed to put mnode into array, but continue at this monitor report");
×
1229
    }
1230
    sdbRelease(pSdb, pObj);
82✔
1231
  }
1232

1233
  // vgroup info
1234
  pIter = NULL;
82✔
1235
  while (1) {
492✔
1236
    SVgObj *pVgroup = NULL;
574✔
1237
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
574✔
1238
    if (pIter == NULL) break;
574✔
1239

1240
    if (pVgroup->mountVgId) {
492✔
1241
      sdbRelease(pSdb, pVgroup);
×
1242
      continue;
×
1243
    }
1244

1245
    pClusterInfo->vgroups_total++;
492✔
1246
    pClusterInfo->tbs_total += pVgroup->numOfTables;
492✔
1247

1248
    SMonVgroupDesc desc = {0};
492✔
1249
    desc.vgroup_id = pVgroup->vgId;
492✔
1250

1251
    SName name = {0};
492✔
1252
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
492✔
1253
    if (code < 0) {
492✔
1254
      mError("failed to get db name since %s", tstrerror(code));
×
1255
      sdbRelease(pSdb, pVgroup);
×
1256
      TAOS_RETURN(code);
×
1257
    }
1258
    (void)tNameGetDbName(&name, desc.database_name);
492✔
1259

1260
    desc.tables_num = pVgroup->numOfTables;
492✔
1261
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
492✔
1262
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
492✔
1263
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
984✔
1264
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
492✔
1265
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
492✔
1266
      pVnDesc->dnode_id = pVgid->dnodeId;
492✔
1267
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
492✔
1268
      pVnDesc->syncState = pVgid->syncState;
492✔
1269
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
492✔
1270
        tstrncpy(desc.status, "ready", sizeof(desc.status));
492✔
1271
        pClusterInfo->vgroups_alive++;
492✔
1272
      }
1273
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
492✔
1274
        pClusterInfo->vnodes_alive++;
492✔
1275
      }
1276
      pClusterInfo->vnodes_total++;
492✔
1277
    }
1278

1279
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
984✔
1280
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1281
    }
1282
    sdbRelease(pSdb, pVgroup);
492✔
1283
  }
1284

1285
  // stb info
1286
  pIter = NULL;
82✔
1287
  while (1) {
82✔
1288
    SStbObj *pStb = NULL;
164✔
1289
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
164✔
1290
    if (pIter == NULL) break;
164✔
1291

1292
    SMonStbDesc desc = {0};
82✔
1293

1294
    SName name1 = {0};
82✔
1295
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
82✔
1296
    if (code < 0) {
82✔
1297
      mError("failed to get db name since %s", tstrerror(code));
×
1298
      sdbRelease(pSdb, pStb);
×
1299
      TAOS_RETURN(code);
×
1300
    }
1301
    (void)tNameGetDbName(&name1, desc.database_name);
82✔
1302

1303
    SName name2 = {0};
82✔
1304
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
82✔
1305
    if (code < 0) {
82✔
1306
      mError("failed to get table name since %s", tstrerror(code));
×
1307
      sdbRelease(pSdb, pStb);
×
1308
      TAOS_RETURN(code);
×
1309
    }
1310
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
82✔
1311

1312
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
164✔
1313
      mError("failed to put stb into array, but continue at this monitor report");
×
1314
    }
1315
    sdbRelease(pSdb, pStb);
82✔
1316
  }
1317

1318
  // grant info
1319
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
82✔
1320
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
82✔
1321
  if (pMnode->grant.expireTimeMS == 0) {
82✔
1322
    pGrantInfo->expire_time = 0;
×
1323
    pGrantInfo->timeseries_total = 0;
×
1324
  }
1325

1326
  mndReleaseRpc(pMnode);
82✔
1327
  TAOS_RETURN(code);
82✔
1328
}
1329

1330
int32_t mndResetTimer(SMnode *pMnode){
×
1331
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1332
}
1333

1334
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
24,660,269✔
1335
  mTrace("mnode get load");
24,660,269✔
1336
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
24,660,269✔
1337
  pLoad->syncState = state.state;
24,660,269✔
1338
  pLoad->syncRestore = state.restored;
24,660,269✔
1339
  pLoad->syncTerm = state.term;
24,660,269✔
1340
  pLoad->roleTimeMs = state.roleTimeMs;
24,660,269✔
1341
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
24,660,269✔
1342
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1343
  return 0;
24,660,269✔
1344
}
1345

1346
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,750,628✔
1347
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,750,628✔
1348
  return state.roleTimeMs;
7,750,628✔
1349
}
1350

1351
void mndSetRestored(SMnode *pMnode, bool restored) {
401,587✔
1352
  if (restored) {
401,587✔
1353
    (void)taosThreadRwlockWrlock(&pMnode->lock);
401,587✔
1354
    pMnode->restored = true;
401,587✔
1355
    (void)taosThreadRwlockUnlock(&pMnode->lock);
401,587✔
1356
    mInfo("mnode set restored:%d", restored);
401,587✔
1357
  } else {
1358
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1359
    pMnode->restored = false;
×
1360
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1361
    mInfo("mnode set restored:%d", restored);
×
1362
    while (1) {
1363
      if (pMnode->rpcRef <= 0) break;
×
1364
      taosMsleep(3);
×
1365
    }
1366
  }
1367
}
401,587✔
1368

1369
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1370

1371
void mndSetStop(SMnode *pMnode) {
401,587✔
1372
  (void)taosThreadRwlockWrlock(&pMnode->lock);
401,587✔
1373
  pMnode->stopped = true;
401,587✔
1374
  (void)taosThreadRwlockUnlock(&pMnode->lock);
401,587✔
1375
  mInfo("mnode set stopped");
401,587✔
1376
}
401,587✔
1377

1378
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
497,092,433✔
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