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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

77.84
/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,372,450✔
64
  int32_t code = 0;
4,372,450✔
65
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,372,450✔
66
  if (pMnode->stopped) {
4,372,450✔
67
    code = TSDB_CODE_APP_IS_STOPPING;
×
68
  } else if (!mndIsLeader(pMnode)) {
4,372,450✔
69
    code = 1;
×
70
  } else {
71
#if 1
72
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
4,372,450✔
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,372,450✔
79
  TAOS_RETURN(code);
4,372,450✔
80
}
81

82
static inline void mndReleaseRpc(SMnode *pMnode) {
222,868,078✔
83
  (void)taosThreadRwlockRdlock(&pMnode->lock);
222,868,078✔
84
#if 1
85
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
222,871,168✔
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);
222,869,445✔
91
}
222,872,356✔
92

93
static void *mndBuildTimerMsg(int32_t *pContLen) {
54,973,511✔
94
  terrno = 0;
54,973,511✔
95
  SMTimerReq timerReq = {0};
54,973,985✔
96

97
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
54,973,985✔
98
  if (contLen <= 0) return NULL;
54,970,661✔
99
  void *pReq = rpcMallocCont(contLen);
54,970,661✔
100
  if (pReq == NULL) return NULL;
54,971,686✔
101

102
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
54,971,686✔
103
    mError("failed to serialize timer msg since %s", terrstr());
×
104
  }
105
  *pContLen = contLen;
54,970,734✔
106
  return pReq;
54,970,734✔
107
}
108

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

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

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

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

160
static void mndPullupTtl(SMnode *pMnode) {
2,183,438✔
161
  mTrace("pullup ttl");
2,183,438✔
162
  int32_t contLen = 0;
2,183,438✔
163
  void   *pReq = mndBuildTimerMsg(&contLen);
2,183,438✔
164
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
2,183,438✔
165
  // TODO check return value
166
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,183,438✔
167
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
168
  }
169
}
2,183,438✔
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,160,014✔
183
  mTrace("pullup trim query");
2,160,014✔
184
  int32_t contLen = 0;
2,160,014✔
185
  void   *pReq = mndBuildTimerMsg(&contLen);
2,160,014✔
186
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,160,014✔
187
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,160,014✔
188
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
189
  }
190
}
2,160,014✔
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,193,143✔
217
  mTrace("pullup arb hb");
11,193,143✔
218
  int32_t contLen = 0;
11,193,143✔
219
  void   *pReq = mndBuildTimerMsg(&contLen);
11,193,143✔
220
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,193,143✔
221
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,193,143✔
222
}
223

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

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

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

256
static void mndPullupGrant(SMnode *pMnode) {
1,061,255✔
257
  mTrace("pullup grant msg");
1,061,255✔
258
  int32_t contLen = 0;
1,061,255✔
259
  void   *pReq = mndBuildTimerMsg(&contLen);
1,061,255✔
260
  if (pReq != NULL) {
1,061,255✔
261
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,061,255✔
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,061,255✔
268
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
269
    }
270
  }
271
}
1,061,255✔
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) {
20,431✔
287
  mTrace("increate uptime");
20,431✔
288
  int32_t contLen = 0;
20,431✔
289
  void   *pReq = mndBuildTimerMsg(&contLen);
20,431✔
290
  if (pReq != NULL) {
20,431✔
291
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
20,431✔
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) {
20,431✔
298
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
299
    }
300
  }
301
}
20,431✔
302

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

306
  void *pIter = NULL;
115,948✔
307
  while (1) {
374,985✔
308
    SVgObj *pVgroup = NULL;
490,933✔
309
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
490,933✔
310
    if (pIter == NULL) break;
490,933✔
311

312
    bool stateChanged = false;
374,985✔
313
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
943,780✔
314
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
720,878✔
315
      if (pGid->dnodeId == dnodeId) {
720,878✔
316
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
152,083✔
317
          mInfo(
61,017✔
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;
61,017✔
323
          pGid->syncRestore = 0;
61,017✔
324
          pGid->syncCanRead = 0;
61,017✔
325
          pGid->startTimeMs = 0;
61,017✔
326
          stateChanged = true;
61,017✔
327
        }
328
        break;
152,083✔
329
      }
330
    }
331

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

342
    sdbRelease(pSdb, pVgroup);
374,985✔
343
  }
344
}
115,948✔
345

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

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

353
  void *pIter = NULL;
4,372,384✔
354
  while (1) {
7,683,376✔
355
    SDnodeObj *pDnode = NULL;
12,055,760✔
356
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,055,760✔
357
    if (pIter == NULL) break;
12,055,760✔
358

359
    bool online = mndIsDnodeOnline(pDnode, curMs);
7,683,376✔
360
    if (!online) {
7,683,376✔
361
      mInfo("dnode:%d, in offline state", pDnode->id);
115,948✔
362
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
115,948✔
363
    }
364

365
    sdbRelease(pSdb, pDnode);
7,683,376✔
366
  }
367

368
  mndReleaseRpc(pMnode);
4,372,384✔
369
}
370

371
static bool mnodeIsNotLeader(SMnode *pMnode) {
260,447,362✔
372
  terrno = 0;
260,447,362✔
373
  (void)taosThreadRwlockRdlock(&pMnode->lock);
260,446,595✔
374
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
260,451,282✔
375
  if (terrno != 0) {
260,451,583✔
376
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
377
    return true;
×
378
  }
379

380
  if (state.state != TAOS_SYNC_STATE_LEADER) {
260,451,583✔
381
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,690,932✔
382
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,691,046✔
383
    return true;
10,690,932✔
384
  }
385
  if (!state.restored || !pMnode->restored) {
249,760,651✔
386
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,207✔
387
    terrno = TSDB_CODE_SYN_RESTORING;
2,519✔
388
    return true;
2,519✔
389
  }
390
  (void)taosThreadRwlockUnlock(&pMnode->lock);
249,757,809✔
391
  return false;
249,757,338✔
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) {
22,565,157✔
411
  int32_t code = 0;
22,565,157✔
412
#ifndef TD_ASTRA
413
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,565,157✔
414
    mndPullupGrant(pMnode);
1,061,255✔
415
  }
416
  if (sec % tsTtlPushIntervalSec == 0) {
22,565,157✔
417
    mndPullupTtl(pMnode);
2,183,438✔
418
  }
419

420
  if (sec % tsTrimVDbIntervalSec == 0) {
22,565,157✔
421
    mndPullupTrimDb(pMnode);
×
422
  }
423

424
  if (sec % tsQueryTrimIntervalSec == 0) {
22,565,157✔
425
    mndPullupQueryTrimDb(pMnode);
2,160,014✔
426
  }
427
#endif
428
#ifdef USE_SHARED_STORAGE
429
  if (tsSsEnabled) {
22,565,157✔
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) {
22,565,157✔
440
    if (sec % tsAuthReqHBInterval == 0) {
×
441
      mndPullupAuth(pMnode);
×
442
    }
443
  }
444
#endif
445
  if (sec % tsTransPullupInterval == 0) {
22,565,157✔
446
    mndPullupTrans(pMnode);
11,193,022✔
447
  }
448

449
  if (sec % tsCompactPullupInterval == 0) {
22,565,157✔
450
    mndPullupCompacts(pMnode);
2,094,786✔
451
  }
452

453
  if (sec % tsScanPullupInterval == 0) {
22,565,157✔
454
    mndPullupScans(pMnode);
2,094,786✔
455
  }
456
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,565,157✔
457
    mndPullupInstances(pMnode);
4,371,073✔
458
  }
459
#ifdef USE_TOPIC
460
  if (sec % tsMqRebalanceInterval == 0) {
22,565,157✔
461
    mndCalMqRebalance(pMnode);
11,192,876✔
462
  }
463
#endif
464
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,565,157✔
465
    mndPullupTelem(pMnode);
126✔
466
  }
467
  if (sec % tsUptimeInterval == 0) {
22,565,157✔
468
    mndIncreaseUpTime(pMnode);
20,431✔
469
  }
470
}
22,565,157✔
471

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

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

489
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
227,193,340✔
490
  if (ms % (tsStatusTimeoutMs) == 0) {
227,193,340✔
491
    mndCheckDnodeOffline(pMnode);
4,372,384✔
492
  }
493
}
227,193,340✔
494

495
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,565,157✔
496
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,565,157✔
497
    mndSyncCheckTimeout(pMnode);
613,051✔
498
  }
499
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,565,157✔
500
    msmHealthCheck(pMnode);
7,409,660✔
501
  }
502
}
22,565,157✔
503

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

509
  while (1) {
236,941,508✔
510
    lastTime++;
237,327,532✔
511
    taosMsleep(100);
237,327,532✔
512

513
    if (mndGetStop(pMnode)) break;
237,327,532✔
514
    if (lastTime % 10 != 0) continue;
236,941,508✔
515

516
    if (mnodeIsNotLeader(pMnode)) {
23,523,107✔
517
      mTrace("timer not process since mnode is not leader");
957,950✔
518
      continue;
957,950✔
519
    }
520

521
    int64_t sec = lastTime / 10;
22,565,157✔
522
    mndDoTimerCheckSync(pMnode, sec);
22,565,157✔
523

524
    mndDoTimerPullupTask(pMnode, sec);
22,565,157✔
525
  }
526

527
  return NULL;
386,024✔
528
}
529

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

535
  while (1) {
536
    lastTime += 100;
237,314,979✔
537
    taosMsleep(100);
237,314,979✔
538

539
    if (mndGetStop(pMnode)) break;
237,314,979✔
540
    if (lastTime % 10 != 0) continue;
236,928,955✔
541

542
    if (mnodeIsNotLeader(pMnode)) {
236,928,955✔
543
      mTrace("timer not process since mnode is not leader");
9,735,615✔
544
      continue;
9,735,615✔
545
    }
546

547
    mndDoTimerCheckStatus(pMnode, lastTime);
227,193,340✔
548

549
    mndDoArbTimerPullupTask(pMnode, lastTime);
227,193,340✔
550
  }
551

552
  return NULL;
386,024✔
553
}
554

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

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

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

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

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

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

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

611
  TAOS_RETURN(code);
386,087✔
612
}
613

614
static int32_t mndInitWal(SMnode *pMnode) {
386,087✔
615
  int32_t code = 0;
386,087✔
616
  char    path[PATH_MAX + 20] = {0};
386,087✔
617
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
386,087✔
618
  SWalCfg cfg = {.vgId = 1,
386,087✔
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) {
386,087✔
631
    code = terrno;
×
632
    TAOS_RETURN(code);
×
633
  }
634
  if (tsMetaKey[0] != '\0') {
386,087✔
635
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
×
636
  }
637
#endif
638

639
  pMnode->pWal = walOpen(path, &cfg);
386,087✔
640
  if (pMnode->pWal == NULL) {
386,087✔
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);
386,087✔
648
}
649

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

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

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

671
  TAOS_RETURN(code);
386,087✔
672
}
673

674
static int32_t mndOpenSdb(SMnode *pMnode) {
386,087✔
675
  int32_t code = 0;
386,087✔
676
  if (!pMnode->deploy) {
386,087✔
677
    code = sdbReadFile(pMnode->pSdb);
108,019✔
678
  }
679

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

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

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

693
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
18,532,176✔
694
  SMnodeStep step = {0};
18,532,176✔
695
  step.name = name;
18,532,176✔
696
  step.initFp = initFp;
18,532,176✔
697
  step.cleanupFp = cleanupFp;
18,532,176✔
698
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
37,064,352✔
699
    TAOS_RETURN(terrno);
×
700
  }
701

702
  TAOS_RETURN(0);
18,532,176✔
703
}
704

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

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

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

766
  for (int32_t s = pos; s >= 0; s--) {
18,915,176✔
767
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
18,529,152✔
768
    mInfo("%s will cleanup", pStep->name);
18,529,152✔
769
    if (pStep->cleanupFp != NULL) {
18,529,152✔
770
      (*pStep->cleanupFp)(pMnode);
17,757,104✔
771
    }
772
  }
773

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

779
static int32_t mndExecSteps(SMnode *pMnode) {
386,087✔
780
  int32_t code = 0;
386,087✔
781
  int32_t size = taosArrayGetSize(pMnode->pSteps);
386,087✔
782
  for (int32_t pos = 0; pos < size; pos++) {
18,918,263✔
783
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
18,532,176✔
784
    if (pStep->initFp == NULL) continue;
18,532,176✔
785

786
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
18,532,176✔
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);
18,532,176✔
792
      tmsgReportStartup(pStep->name, "initialized");
18,532,176✔
793
    }
794
  }
795

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

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

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

815
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
386,087✔
816
  if (pMnode == NULL) {
386,087✔
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));
386,087✔
822

823
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
386,087✔
824
  if (code != 0) {
386,087✔
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";
386,087✔
832
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
386,087✔
833
  if (code < 0) {
386,087✔
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

841
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
386,087✔
842
        pOption->numOfTotalReplicas);
843
  mndSetOptions(pMnode, pOption);
386,087✔
844

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

855
  code = mndCreateDir(pMnode, path);
386,087✔
856
  if (code != 0) {
386,087✔
857
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
858
    mndClose(pMnode);
×
859
    terrno = code;
×
860
    return NULL;
×
861
  }
862

863
  code = mndInitSteps(pMnode);
386,087✔
864
  if (code != 0) {
386,087✔
865
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
866
    mndClose(pMnode);
×
867
    terrno = code;
×
868
    return NULL;
×
869
  }
870

871
  code = mndExecSteps(pMnode);
386,087✔
872
  if (code != 0) {
386,087✔
873
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
874
    mndClose(pMnode);
×
875
    terrno = code;
×
876
    return NULL;
×
877
  }
878

879
  mInfo("mnode open successfully");
386,087✔
880
  return pMnode;
386,087✔
881
}
882

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

899
void mndClose(SMnode *pMnode) {
386,024✔
900
  if (pMnode != NULL) {
386,024✔
901
    mInfo("start to close mnode");
386,024✔
902
    mndCleanupSteps(pMnode, -1);
386,024✔
903
    taosMemoryFreeClear(pMnode->path);
386,024✔
904
    taosMemoryFreeClear(pMnode);
386,024✔
905
    mInfo("mnode is closed");
386,024✔
906
  }
907
}
386,024✔
908

909
int32_t mndStart(SMnode *pMnode) {
386,024✔
910
  mndSyncStart(pMnode);
386,024✔
911
  if (pMnode->deploy) {
386,024✔
912
    if (sdbDeploy(pMnode->pSdb) != 0) {
278,068✔
913
      mError("failed to deploy sdb while start mnode");
×
914
      return -1;
×
915
    }
916
    mndSetRestored(pMnode, true);
278,068✔
917
  }
918
  if (mndIsLeader(pMnode)) {
386,024✔
919
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
350,055✔
920
      mError("failed to upgrade sdb while start mnode");
×
921
      return -1;
×
922
    }
923
  }
924
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
386,024✔
925
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
386,024✔
926

927
  return mndInitTimer(pMnode);
386,024✔
928
}
929

930
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
386,024✔
931

932
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
302,940✔
933

934
int32_t mndIsCatchUp(SMnode *pMnode) {
148,546✔
935
  int64_t rid = pMnode->syncMgmt.sync;
148,546✔
936
  return syncIsCatchUp(rid);
148,546✔
937
}
938

939
ESyncRole mndGetRole(SMnode *pMnode) {
148,546✔
940
  int64_t rid = pMnode->syncMgmt.sync;
148,546✔
941
  return syncGetRole(rid);
148,546✔
942
}
943

944
int64_t mndGetTerm(SMnode *pMnode) {
7,498,251✔
945
  int64_t rid = pMnode->syncMgmt.sync;
7,498,251✔
946
  return syncGetTerm(rid);
7,498,251✔
947
}
948

949
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,693,008✔
950

951
void mndStop(SMnode *pMnode) {
386,024✔
952
  mndSetStop(pMnode);
386,024✔
953
  mndSyncStop(pMnode);
386,024✔
954
  mndCleanupTimer(pMnode);
386,024✔
955
}
386,024✔
956

957
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
44,407,809✔
958
  SMnode    *pMnode = pMsg->info.node;
44,407,809✔
959
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
44,407,809✔
960

961
  const STraceId *trace = &pMsg->info.traceId;
44,407,809✔
962
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
44,407,725✔
963

964
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
44,407,725✔
965
  if (code != 0) {
44,407,809✔
966
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
714✔
967
            tstrerror(code), code);
968
  }
969

970
  return code;
44,407,809✔
971
}
972

973
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
222,034,585✔
974
  int32_t code = 0;
222,034,585✔
975
  if (!IsReq(pMsg)) TAOS_RETURN(code);
222,034,585✔
976
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
193,914,778✔
977
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
189,585,564✔
978
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
185,969,015✔
979
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
177,329,207✔
980
    TAOS_RETURN(code);
16,593,163✔
981
  }
982

983
  SMnode *pMnode = pMsg->info.node;
177,333,235✔
984
  (void)taosThreadRwlockRdlock(&pMnode->lock);
177,329,201✔
985
  if (pMnode->stopped) {
177,335,220✔
986
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,497✔
987
    code = TSDB_CODE_APP_IS_STOPPING;
12,497✔
988
    TAOS_RETURN(code);
12,497✔
989
  }
990

991
  terrno = 0;
177,316,109✔
992
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
177,315,487✔
993
  if (terrno != 0) {
177,324,621✔
994
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
995
    code = terrno;
×
996
    TAOS_RETURN(code);
×
997
  }
998

999
  if (state.state != TAOS_SYNC_STATE_LEADER) {
177,321,893✔
1000
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,846,162✔
1001
    code = TSDB_CODE_SYN_NOT_LEADER;
1,846,237✔
1002
    goto _OVER;
1,846,237✔
1003
  }
1004

1005
  if (!state.restored || !pMnode->restored) {
175,475,731✔
1006
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,690,706✔
1007
    code = TSDB_CODE_SYN_RESTORING;
1,688,192✔
1008
    goto _OVER;
1,688,192✔
1009
  }
1010

1011
#if 1
1012
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
173,785,503✔
1013
#else
1014
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1015
  mTrace("mnode rpc is acquired, ref:%d", ref);
1016
#endif
1017

1018
  (void)taosThreadRwlockUnlock(&pMnode->lock);
173,790,034✔
1019
  TAOS_RETURN(code);
173,790,405✔
1020

1021
_OVER:
3,534,429✔
1022
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,534,429✔
1023
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,533,941✔
1024
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,534,354✔
1025
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,534,429✔
1026
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,534,271✔
1027
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,534,354✔
1028
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,533,135✔
1029
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,533,439✔
1030
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,533,623✔
1031
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
2,254✔
1032
           pMnode->stopped, state.restored, syncStr(state.state));
1033
    TAOS_RETURN(code);
2,254✔
1034
  }
1035

1036
  const STraceId *trace = &pMsg->info.traceId;
3,531,331✔
1037
  SEpSet          epSet = {0};
3,532,620✔
1038
  mndGetMnodeEpSet(pMnode, &epSet);
3,532,148✔
1039

1040
  mGDebug(
3,534,429✔
1041
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1042
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1043
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1044
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1045

1046
  if (epSet.numOfEps <= 0) return -1;
3,534,429✔
1047

1048
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
10,560,434✔
1049
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
7,026,005✔
1050
  }
1051

1052
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,534,429✔
1053
  pMsg->info.rsp = rpcMallocCont(contLen);
3,532,509✔
1054
  if (pMsg->info.rsp != NULL) {
3,533,496✔
1055
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,532,840✔
1056
      mError("failed to serialize ep set");
×
1057
    }
1058
    pMsg->info.hasEpSet = 1;
3,531,140✔
1059
    pMsg->info.rspLen = contLen;
3,530,988✔
1060
  }
1061

1062
  TAOS_RETURN(code);
3,531,777✔
1063
}
1064

1065
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
222,046,028✔
1066
  SMnode         *pMnode = pMsg->info.node;
222,046,028✔
1067
  const STraceId *trace = &pMsg->info.traceId;
222,046,026✔
1068
  int32_t         code = TSDB_CODE_SUCCESS;
222,042,818✔
1069

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

1092
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
222,033,108✔
1093
  MndMsgFpExt fpExt = NULL;
222,042,016✔
1094
  if (fp == NULL) {
222,042,016✔
1095
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
16,589,208✔
1096
    if (fpExt == NULL) {
16,589,101✔
1097
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1098
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1099
      TAOS_RETURN(code);
×
1100
    }
1101
  }
1102

1103
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
222,041,909✔
1104

1105
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
218,493,560✔
1106
  if (fp)
218,494,841✔
1107
    code = (*fp)(pMsg);
201,905,529✔
1108
  else
1109
    code = (*fpExt)(pMsg, pQueueInfo);
16,589,312✔
1110
  mndReleaseRpc(pMnode);
218,494,775✔
1111

1112
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
218,499,831✔
1113
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
25,954,556✔
1114
  } else if (code == 0) {
192,545,275✔
1115
    mGTrace("msg:%p, successfully processed", pMsg);
190,149,640✔
1116
  } else {
1117
    // TODO removve this wrong set code
1118
    if (code == -1) {
2,395,635✔
1119
      code = terrno;
2,397✔
1120
    }
1121
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,395,635✔
1122
            TMSG_INFO(pMsg->msgType));
1123
  }
1124

1125
  TAOS_RETURN(code);
218,499,831✔
1126
}
1127

1128
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
81,464,357✔
1129
  tmsg_t type = TMSG_INDEX(msgType);
81,464,357✔
1130
  if (type < TDMT_MAX) {
81,464,357✔
1131
    pMnode->msgFp[type] = fp;
81,464,357✔
1132
  }
1133
}
81,464,357✔
1134

1135
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,088,696✔
1136
  tmsg_t type = TMSG_INDEX(msgType);
3,088,696✔
1137
  if (type < TDMT_MAX) {
3,088,696✔
1138
    pMnode->msgFpExt[type] = fp;
3,088,696✔
1139
  }
1140
}
3,088,696✔
1141

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

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

1164
  SSdb   *pSdb = pMnode->pSdb;
66✔
1165
  int64_t ms = taosGetTimestampMs();
66✔
1166

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

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

1188
  void *pIter = NULL;
66✔
1189
  while (1) {
66✔
1190
    SDnodeObj *pObj = NULL;
132✔
1191
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
132✔
1192
    if (pIter == NULL) break;
132✔
1193

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

1208
  pIter = NULL;
66✔
1209
  while (1) {
66✔
1210
    SMnodeObj *pObj = NULL;
132✔
1211
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
132✔
1212
    if (pIter == NULL) break;
132✔
1213

1214
    SMonMnodeDesc desc = {0};
66✔
1215
    desc.mnode_id = pObj->id;
66✔
1216
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
66✔
1217

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

1236
  // vgroup info
1237
  pIter = NULL;
66✔
1238
  while (1) {
132✔
1239
    SVgObj *pVgroup = NULL;
198✔
1240
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
198✔
1241
    if (pIter == NULL) break;
198✔
1242

1243
    if (pVgroup->mountVgId) {
132✔
1244
      sdbRelease(pSdb, pVgroup);
×
1245
      continue;
×
1246
    }
1247

1248
    pClusterInfo->vgroups_total++;
132✔
1249
    pClusterInfo->tbs_total += pVgroup->numOfTables;
132✔
1250

1251
    SMonVgroupDesc desc = {0};
132✔
1252
    desc.vgroup_id = pVgroup->vgId;
132✔
1253

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

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

1282
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
264✔
1283
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1284
    }
1285
    sdbRelease(pSdb, pVgroup);
132✔
1286
  }
1287

1288
  // stb info
1289
  pIter = NULL;
66✔
1290
  while (1) {
×
1291
    SStbObj *pStb = NULL;
66✔
1292
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
66✔
1293
    if (pIter == NULL) break;
66✔
1294

1295
    SMonStbDesc desc = {0};
×
1296

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

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

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

1321
  // grant info
1322
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
66✔
1323
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
66✔
1324
  if (pMnode->grant.expireTimeMS == 0) {
66✔
1325
    pGrantInfo->expire_time = 0;
×
1326
    pGrantInfo->timeseries_total = 0;
×
1327
  }
1328

1329
  mndReleaseRpc(pMnode);
66✔
1330
  TAOS_RETURN(code);
66✔
1331
}
1332

1333
int32_t mndResetTimer(SMnode *pMnode){
×
1334
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1335
}
1336

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

1349
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,409,035✔
1350
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,409,035✔
1351
  return state.roleTimeMs;
7,409,035✔
1352
}
1353

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

1372
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1373

1374
void mndSetStop(SMnode *pMnode) {
386,024✔
1375
  (void)taosThreadRwlockWrlock(&pMnode->lock);
386,024✔
1376
  pMnode->stopped = true;
386,024✔
1377
  (void)taosThreadRwlockUnlock(&pMnode->lock);
386,024✔
1378
  mInfo("mnode set stopped");
386,024✔
1379
}
386,024✔
1380

1381
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
474,619,095✔
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