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

taosdata / TDengine / #4933

20 Jan 2026 10:44AM UTC coverage: 66.671% (+0.03%) from 66.646%
#4933

push

travis-ci

web-flow
merge: from main to 3.0 #34340

73 of 178 new or added lines in 9 files covered. (41.01%)

1199 existing lines in 124 files now uncovered.

203121 of 304663 relevant lines covered (66.67%)

132228377.94 hits per line

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

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
256,403,747✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
256,403,747✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
256,403,611✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
256,403,421✔
93
}
256,404,126✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
67,166,004✔
96
  terrno = 0;
67,166,004✔
97
  SMTimerReq timerReq = {0};
67,166,004✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
67,166,004✔
100
  if (contLen <= 0) return NULL;
67,166,004✔
101
  void *pReq = rpcMallocCont(contLen);
67,166,004✔
102
  if (pReq == NULL) return NULL;
67,166,004✔
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
  void *pIter = NULL;
116,769✔
309
  while (1) {
363,809✔
310
    SVgObj *pVgroup = NULL;
480,578✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
480,578✔
312
    if (pIter == NULL) break;
480,578✔
313

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

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

346
    sdbRelease(pSdb, pVgroup);
363,809✔
347
  }
348
}
116,769✔
349

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

354
  SSdb   *pSdb = pMnode->pSdb;
5,154,227✔
355
  int64_t curMs = taosGetTimestampMs();
5,154,227✔
356

357
  void *pIter = NULL;
5,154,227✔
358
  while (1) {
8,543,819✔
359
    SDnodeObj *pDnode = NULL;
13,698,046✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,698,046✔
361
    if (pIter == NULL) break;
13,698,046✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,543,819✔
364
    if (!online) {
8,543,819✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
116,769✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
116,769✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
8,543,819✔
370
  }
371

372
  mndReleaseRpc(pMnode);
5,154,227✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
304,603,819✔
376
  terrno = 0;
304,603,819✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
304,603,819✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
304,603,883✔
379
  if (terrno != 0) {
304,605,266✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
304,604,327✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,843,439✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,843,439✔
387
    return true;
10,843,439✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
293,760,888✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,896✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
4,717✔
392
    return true;
4,717✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
293,756,468✔
395
  return false;
293,756,089✔
396
}
397

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

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

412
  return min <= 1 ? 2 : min;
×
413
}
414
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
27,221,676✔
415
  int32_t code = 0;
27,221,676✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
27,221,676✔
418
    mndPullupGrant(pMnode);
1,476,609✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
27,221,676✔
421
    mndPullupTtl(pMnode);
2,844,655✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
27,221,676✔
425
    mndPullupTrimDb(pMnode);
11,587✔
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
27,221,676✔
429
    mndPullupQueryTrimDb(pMnode);
2,776,746✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
27,221,676✔
434
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
435
      mndPullupUpdateSsMigrateProgress(pMnode);
×
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
27,221,676✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
27,221,676✔
450
    mndPullupTrans(pMnode);
13,613,852✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
27,221,676✔
454
    mndPullupCompacts(pMnode);
2,722,605✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
27,221,676✔
458
    mndPullupScans(pMnode);
2,722,671✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
27,221,676✔
461
    mndPullupInstances(pMnode);
5,433,753✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
27,221,676✔
465
    mndCalMqRebalance(pMnode);
13,613,720✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
27,221,676✔
469
    mndPullupTelem(pMnode);
186✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
27,221,676✔
472
    mndIncreaseUpTime(pMnode);
83,149✔
473
  }
474
}
27,221,676✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
266,535,446✔
477
  int32_t code = 0;
266,535,446✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
266,535,446✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
13,150,933✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
266,535,446✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
8,715,538✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
266,535,446✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
266,535,446✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
266,535,446✔
495
    mndCheckDnodeOffline(pMnode);
5,154,227✔
496
  }
497
}
266,535,446✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
27,221,676✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
27,221,676✔
501
    mndSyncCheckTimeout(pMnode);
898,999✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
27,221,676✔
504
    msmHealthCheck(pMnode);
9,071,949✔
505
  }
506
}
27,221,676✔
507

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

513
  while (1) {
276,812,831✔
514
    if (mndGetStop(pMnode)) break;
277,212,058✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
276,812,831✔
517
    if (nowSec == lastSec) {
276,812,831✔
518
      taosMsleep(100);
248,564,454✔
519
      continue;
248,564,454✔
520
    }
521
    lastSec = nowSec;
28,248,377✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
28,248,377✔
524
      taosMsleep(100);
1,026,701✔
525
      mTrace("timer not process since mnode is not leader");
1,026,701✔
526
      continue;
1,026,701✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
27,221,676✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
27,221,676✔
532

533
    taosMsleep(100);
27,221,676✔
534
  }
535

536
  return NULL;
399,227✔
537
}
538

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

544
  while (1) {
545
    lastTime += 100;
276,756,128✔
546
    taosMsleep(100);
276,756,128✔
547

548
    if (mndGetStop(pMnode)) break;
276,756,128✔
549
    if (lastTime % 10 != 0) continue;
276,356,901✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
276,356,901✔
552
      mTrace("timer not process since mnode is not leader");
9,821,455✔
553
      continue;
9,821,455✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
266,535,446✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
266,535,446✔
559
  }
560

561
  return NULL;
399,227✔
562
}
563

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

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

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

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

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

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

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

620
  TAOS_RETURN(code);
399,285✔
621
}
622

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

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

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

656
  TAOS_RETURN(code);
399,285✔
657
}
658

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

666
static int32_t mndInitSdb(SMnode *pMnode) {
399,285✔
667
  int32_t code = 0;
399,285✔
668
  SSdbOpt opt = {0};
399,285✔
669
  opt.path = pMnode->path;
399,285✔
670
  opt.pMnode = pMnode;
399,285✔
671
  opt.pWal = pMnode->pWal;
399,285✔
672

673
  pMnode->pSdb = sdbInit(&opt);
399,285✔
674
  if (pMnode->pSdb == NULL) {
399,285✔
675
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
676
    if (terrno != 0) code = terrno;
×
677
    TAOS_RETURN(code);
×
678
  }
679

680
  TAOS_RETURN(code);
399,285✔
681
}
682

683
static int32_t mndOpenSdb(SMnode *pMnode) {
399,285✔
684
  int32_t code = 0;
399,285✔
685
  if (!pMnode->deploy) {
399,285✔
686
    code = sdbReadFile(pMnode->pSdb);
112,856✔
687
  }
688

689
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
399,285✔
690

691
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
399,285✔
692
  return code;
399,285✔
693
}
694

695
static void mndCleanupSdb(SMnode *pMnode) {
399,227✔
696
  if (pMnode->pSdb) {
399,227✔
697
    sdbCleanup(pMnode->pSdb);
399,227✔
698
    pMnode->pSdb = NULL;
399,227✔
699
  }
700
}
399,227✔
701

702
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,964,250✔
703
  SMnodeStep step = {0};
19,964,250✔
704
  step.name = name;
19,964,250✔
705
  step.initFp = initFp;
19,964,250✔
706
  step.cleanupFp = cleanupFp;
19,964,250✔
707
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
39,928,500✔
708
    TAOS_RETURN(terrno);
×
709
  }
710

711
  TAOS_RETURN(0);
19,964,250✔
712
}
713

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

770
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
399,227✔
771
  if (pMnode->pSteps == NULL) return;
399,227✔
772

773
  if (pos == -1) {
399,227✔
774
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
399,227✔
775
  }
776

777
  for (int32_t s = pos; s >= 0; s--) {
20,360,577✔
778
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,961,350✔
779
    mInfo("%s will cleanup", pStep->name);
19,961,350✔
780
    if (pStep->cleanupFp != NULL) {
19,961,350✔
781
      (*pStep->cleanupFp)(pMnode);
19,162,896✔
782
    }
783
  }
784

785
  taosArrayClear(pMnode->pSteps);
399,227✔
786
  taosArrayDestroy(pMnode->pSteps);
399,227✔
787
  pMnode->pSteps = NULL;
399,227✔
788
}
789

790
static int32_t mndExecSteps(SMnode *pMnode) {
399,285✔
791
  int32_t code = 0;
399,285✔
792
  int32_t size = taosArrayGetSize(pMnode->pSteps);
399,285✔
793
  for (int32_t pos = 0; pos < size; pos++) {
20,363,535✔
794
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,964,250✔
795
    if (pStep->initFp == NULL) continue;
19,964,250✔
796

797
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
19,964,250✔
798
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
799
      mndCleanupSteps(pMnode, pos);
×
800
      TAOS_RETURN(code);
×
801
    } else {
802
      mInfo("%s is initialized", pStep->name);
19,964,250✔
803
      tmsgReportStartup(pStep->name, "initialized");
19,964,250✔
804
    }
805
  }
806

807
  pMnode->clusterId = mndGetClusterId(pMnode);
399,285✔
808
  TAOS_RETURN(0);
399,285✔
809
}
810

811
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
399,285✔
812
  pMnode->msgCb = pOption->msgCb;
399,285✔
813
  pMnode->selfDnodeId = pOption->dnodeId;
399,285✔
814
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
399,285✔
815
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
399,285✔
816
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
399,285✔
817
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
399,285✔
818
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
399,285✔
819
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
399,285✔
820
}
399,285✔
821

822
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
399,285✔
823
  terrno = 0;
399,285✔
824
  mInfo("start to open mnode in %s", path);
399,285✔
825

826
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
399,285✔
827
  if (pMnode == NULL) {
399,285✔
828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
829
    mError("failed to open mnode in step 1, since %s", terrstr());
×
830
    return NULL;
×
831
  }
832
  (void)memset(pMnode, 0, sizeof(SMnode));
399,285✔
833

834
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
399,285✔
835
  if (code != 0) {
399,285✔
836
    taosMemoryFree(pMnode);
×
837
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
838
    terrno = code;
×
839
    return NULL;
×
840
  }
841

842
  char timestr[24] = "1970-01-01 00:00:00.00";
399,285✔
843
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
399,285✔
844
  if (code < 0) {
399,285✔
845
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
846
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
847
    taosMemoryFree(pMnode);
×
848
    terrno = code;
×
849
    return NULL;
×
850
  }
851

852
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
399,285✔
853
        pOption->numOfTotalReplicas);
854
  mndSetOptions(pMnode, pOption);
399,285✔
855

856
  pMnode->deploy = pOption->deploy;
399,285✔
857
  pMnode->version = pOption->version;
399,285✔
858
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
399,285✔
859
  if (pMnode->pSteps == NULL) {
399,285✔
860
    taosMemoryFree(pMnode);
×
861
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
862
    mError("failed to open mnode in step 4, since %s", terrstr());
×
863
    return NULL;
×
864
  }
865

866
  code = mndCreateDir(pMnode, path);
399,285✔
867
  if (code != 0) {
399,285✔
868
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
869
    mndClose(pMnode);
×
870
    terrno = code;
×
871
    return NULL;
×
872
  }
873

874
  code = mndInitSteps(pMnode);
399,285✔
875
  if (code != 0) {
399,285✔
876
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
877
    mndClose(pMnode);
×
878
    terrno = code;
×
879
    return NULL;
×
880
  }
881

882
  code = mndExecSteps(pMnode);
399,285✔
883
  if (code != 0) {
399,285✔
884
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
885
    mndClose(pMnode);
×
886
    terrno = code;
×
887
    return NULL;
×
888
  }
889

890
  mInfo("mnode open successfully");
399,285✔
891
  return pMnode;
399,285✔
892
}
893

894
void mndPreClose(SMnode *pMnode) {
399,227✔
895
  if (pMnode != NULL) {
399,227✔
896
    int32_t code = 0;
399,227✔
897
    // TODO check return value
898
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
399,227✔
899
    if (code < 0) {
399,227✔
900
      mError("failed to transfer leader since %s", tstrerror(code));
×
901
    }
902
    syncPreStop(pMnode->syncMgmt.sync);
399,227✔
903
    code = sdbWriteFile(pMnode->pSdb, 0);
399,227✔
904
    if (code < 0) {
399,227✔
905
      mError("failed to write sdb since %s", tstrerror(code));
713✔
906
    }
907
  }
908
}
399,227✔
909

910
void mndClose(SMnode *pMnode) {
399,227✔
911
  if (pMnode != NULL) {
399,227✔
912
    mInfo("start to close mnode");
399,227✔
913
    mndCleanupSteps(pMnode, -1);
399,227✔
914
    taosMemoryFreeClear(pMnode->path);
399,227✔
915
    taosMemoryFreeClear(pMnode);
399,227✔
916
    mInfo("mnode is closed");
399,227✔
917
  }
918
}
399,227✔
919

920
int32_t mndStart(SMnode *pMnode) {
399,227✔
921
  mndSyncStart(pMnode);
399,227✔
922
  if (pMnode->deploy) {
399,227✔
923
    if (sdbDeploy(pMnode->pSdb) != 0) {
286,429✔
924
      mError("failed to deploy sdb while start mnode");
×
925
      return -1;
×
926
    }
927
    mndSetRestored(pMnode, true);
286,429✔
928
  }
929
  if (mndIsLeader(pMnode)) {
399,227✔
930
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
359,615✔
931
      mError("failed to upgrade sdb while start mnode");
×
932
      return -1;
×
933
    }
934
  }
935
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
399,227✔
936
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
399,227✔
937

938
  return mndInitTimer(pMnode);
399,227✔
939
}
940

941
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
399,227✔
942

943
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
314,751✔
944

945
int32_t mndIsCatchUp(SMnode *pMnode) {
168,032✔
946
  int64_t rid = pMnode->syncMgmt.sync;
168,032✔
947
  return syncIsCatchUp(rid);
168,032✔
948
}
949

950
ESyncRole mndGetRole(SMnode *pMnode) {
168,032✔
951
  int64_t rid = pMnode->syncMgmt.sync;
168,032✔
952
  return syncGetRole(rid);
168,032✔
953
}
954

955
int64_t mndGetTerm(SMnode *pMnode) {
8,815,066✔
956
  int64_t rid = pMnode->syncMgmt.sync;
8,815,066✔
957
  return syncGetTerm(rid);
8,815,066✔
958
}
959

960
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,966,281✔
961

962
void mndStop(SMnode *pMnode) {
399,227✔
963
  mndSetStop(pMnode);
399,227✔
964
  mndSyncStop(pMnode);
399,227✔
965
  mndCleanupTimer(pMnode);
399,227✔
966
}
399,227✔
967

968
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
50,749,578✔
969
  SMnode    *pMnode = pMsg->info.node;
50,749,578✔
970
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
50,749,578✔
971

972
  const STraceId *trace = &pMsg->info.traceId;
50,749,578✔
973
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
50,749,578✔
974

975
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
50,749,578✔
976
  if (code != 0) {
50,749,578✔
977
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
463✔
978
            tstrerror(code), code);
979
  }
980

981
  return code;
50,749,498✔
982
}
983

984
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
254,166,823✔
985
  int32_t code = 0;
254,166,823✔
986
  if (!IsReq(pMsg)) TAOS_RETURN(code);
254,166,823✔
987
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
223,385,419✔
988
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
218,688,389✔
989
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
214,818,136✔
990
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
205,597,263✔
991
    TAOS_RETURN(code);
17,785,270✔
992
  }
993

994
  SMnode *pMnode = pMsg->info.node;
205,600,836✔
995
  (void)taosThreadRwlockRdlock(&pMnode->lock);
205,602,207✔
996
  if (pMnode->stopped) {
205,603,835✔
997
    (void)taosThreadRwlockUnlock(&pMnode->lock);
720✔
998
    code = TSDB_CODE_APP_IS_STOPPING;
720✔
999
    TAOS_RETURN(code);
720✔
1000
  }
1001

1002
  terrno = 0;
205,600,391✔
1003
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
205,600,102✔
1004
  if (terrno != 0) {
205,605,269✔
1005
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1006
    code = terrno;
×
1007
    TAOS_RETURN(code);
×
1008
  }
1009

1010
  if (state.state != TAOS_SYNC_STATE_LEADER) {
205,604,292✔
1011
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,085,978✔
1012
    code = TSDB_CODE_SYN_NOT_LEADER;
2,085,978✔
1013
    goto _OVER;
2,085,978✔
1014
  }
1015

1016
  if (!state.restored || !pMnode->restored) {
203,518,314✔
1017
    (void)taosThreadRwlockUnlock(&pMnode->lock);
840,710✔
1018
    code = TSDB_CODE_SYN_RESTORING;
839,299✔
1019
    goto _OVER;
839,299✔
1020
  }
1021

1022
#if 1
1023
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
202,678,082✔
1024
#else
1025
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1026
  mTrace("mnode rpc is acquired, ref:%d", ref);
1027
#endif
1028

1029
  (void)taosThreadRwlockUnlock(&pMnode->lock);
202,679,212✔
1030
  TAOS_RETURN(code);
202,679,202✔
1031

1032
_OVER:
2,925,277✔
1033
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,925,277✔
1034
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,925,277✔
1035
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,924,536✔
1036
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,925,277✔
1037
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,925,277✔
1038
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,925,277✔
1039
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,924,536✔
1040
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,925,277✔
1041
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,925,277✔
UNCOV
1042
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1043
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1044
    TAOS_RETURN(code);
×
1045
  }
1046

1047
  const STraceId *trace = &pMsg->info.traceId;
2,925,277✔
1048
  SEpSet          epSet = {0};
2,925,277✔
1049
  mndGetMnodeEpSet(pMnode, &epSet);
2,925,277✔
1050

1051
  mGDebug(
2,925,277✔
1052
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1053
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1054
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1055
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1056

1057
  if (epSet.numOfEps <= 0) return -1;
2,925,277✔
1058

1059
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
9,868,362✔
1060
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,943,085✔
1061
  }
1062

1063
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,925,277✔
1064
  pMsg->info.rsp = rpcMallocCont(contLen);
2,922,312✔
1065
  if (pMsg->info.rsp != NULL) {
2,924,886✔
1066
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,924,886✔
1067
      mError("failed to serialize ep set");
×
1068
    }
1069
    pMsg->info.hasEpSet = 1;
2,924,807✔
1070
    pMsg->info.rspLen = contLen;
2,924,702✔
1071
  }
1072

1073
  TAOS_RETURN(code);
2,924,973✔
1074
}
1075

1076
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
254,174,557✔
1077
  SMnode         *pMnode = pMsg->info.node;
254,174,557✔
1078
  const STraceId *trace = &pMsg->info.traceId;
254,175,914✔
1079
  int32_t         code = TSDB_CODE_SUCCESS;
254,174,249✔
1080

1081
#ifdef TD_ENTERPRISE
1082
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
254,174,249✔
1083
    SCachedTokenInfo ti = {0};
7,414✔
1084
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
7,414✔
1085
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
152✔
1086
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
152✔
1087
      TAOS_RETURN(code);
152✔
1088
    }
1089
    if (ti.enabled == 0) {
7,262✔
1090
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
304✔
1091
      code = TSDB_CODE_MND_TOKEN_DISABLED;
304✔
1092
      TAOS_RETURN(code);
304✔
1093
    }
1094
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
6,958✔
1095
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1096
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1097
      TAOS_RETURN(code);
×
1098
    }
1099
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
6,958✔
1100
  }
1101
#endif
1102

1103
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
254,160,639✔
1104
  MndMsgFpExt fpExt = NULL;
254,169,664✔
1105
  if (fp == NULL) {
254,169,664✔
1106
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,786,989✔
1107
    if (fpExt == NULL) {
17,787,702✔
1108
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1109
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1110
      TAOS_RETURN(code);
×
1111
    }
1112
  }
1113

1114
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
254,170,377✔
1115

1116
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
251,245,303✔
1117
  if (fp)
251,245,199✔
1118
    code = (*fp)(pMsg);
233,457,497✔
1119
  else
1120
    code = (*fpExt)(pMsg, pQueueInfo);
17,787,702✔
1121
  mndReleaseRpc(pMnode);
251,249,769✔
1122

1123
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
251,249,836✔
1124
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
28,210,767✔
1125
  } else if (code == 0) {
223,039,069✔
1126
    mGTrace("msg:%p, successfully processed", pMsg);
220,590,046✔
1127
  } else {
1128
    // TODO removve this wrong set code
1129
    if (code == -1) {
2,449,023✔
1130
      code = terrno;
×
1131
    }
1132
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,449,023✔
1133
            TMSG_INFO(pMsg->msgType));
1134
  }
1135

1136
  TAOS_RETURN(code);
251,249,836✔
1137
}
1138

1139
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
93,033,405✔
1140
  tmsg_t type = TMSG_INDEX(msgType);
93,033,405✔
1141
  if (type < TDMT_MAX) {
93,033,405✔
1142
    pMnode->msgFp[type] = fp;
93,033,405✔
1143
  }
1144
}
93,033,405✔
1145

1146
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,194,280✔
1147
  tmsg_t type = TMSG_INDEX(msgType);
3,194,280✔
1148
  if (type < TDMT_MAX) {
3,194,280✔
1149
    pMnode->msgFpExt[type] = fp;
3,194,280✔
1150
  }
1151
}
3,194,280✔
1152

1153
// Note: uid 0 is reserved
1154
int64_t mndGenerateUid(const char *name, int32_t len) {
9,678,616✔
1155
  int32_t hashval = MurmurHash3_32(name, len);
9,678,616✔
1156
  do {
×
1157
    int64_t us = taosGetTimestampUs();
9,678,616✔
1158
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,678,616✔
1159
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,678,616✔
1160
    if (uuid) {
9,679,045✔
1161
      return llabs(uuid);
9,679,045✔
1162
    }
1163
  } while (true);
1164
}
1165

1166
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
63✔
1167
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1168
  int32_t code = mndAcquireRpc(pMnode);
63✔
1169
  if (code < 0) {
63✔
1170
    TAOS_RETURN(code);
×
1171
  } else if (code == 1) {
63✔
1172
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1173
  }
1174

1175
  SSdb   *pSdb = pMnode->pSdb;
63✔
1176
  int64_t ms = taosGetTimestampMs();
63✔
1177

1178
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
63✔
1179
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
63✔
1180
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
63✔
1181
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
63✔
1182
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
63✔
1183
      pStbInfo->stbs == NULL) {
63✔
1184
    mndReleaseRpc(pMnode);
×
1185
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1186
    if (terrno != 0) code = terrno;
×
1187
    TAOS_RETURN(code);
×
1188
  }
1189

1190
  // cluster info
1191
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
63✔
1192
  pClusterInfo->monitor_interval = tsMonitorInterval;
63✔
1193
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
63✔
1194
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
63✔
1195
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
63✔
1196
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
63✔
1197
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
63✔
1198

1199
  void *pIter = NULL;
63✔
1200
  while (1) {
63✔
1201
    SDnodeObj *pObj = NULL;
126✔
1202
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
126✔
1203
    if (pIter == NULL) break;
126✔
1204

1205
    SMonDnodeDesc desc = {0};
63✔
1206
    desc.dnode_id = pObj->id;
63✔
1207
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
63✔
1208
    if (mndIsDnodeOnline(pObj, ms)) {
63✔
1209
      tstrncpy(desc.status, "ready", sizeof(desc.status));
63✔
1210
    } else {
1211
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1212
    }
1213
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
126✔
1214
      mError("failed put dnode into array, but continue at this monitor report")
×
1215
    }
1216
    sdbRelease(pSdb, pObj);
63✔
1217
  }
1218

1219
  pIter = NULL;
63✔
1220
  while (1) {
63✔
1221
    SMnodeObj *pObj = NULL;
126✔
1222
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
126✔
1223
    if (pIter == NULL) break;
126✔
1224

1225
    SMonMnodeDesc desc = {0};
63✔
1226
    desc.mnode_id = pObj->id;
63✔
1227
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
63✔
1228

1229
    if (pObj->id == pMnode->selfDnodeId) {
63✔
1230
      pClusterInfo->first_ep_dnode_id = pObj->id;
63✔
1231
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
63✔
1232
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1233
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
63✔
1234
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1235
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
63✔
1236
      desc.syncState = TAOS_SYNC_STATE_LEADER;
63✔
1237
    } else {
1238
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1239
      desc.syncState = pObj->syncState;
×
1240
    }
1241
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
126✔
1242
      mError("failed to put mnode into array, but continue at this monitor report");
×
1243
    }
1244
    sdbRelease(pSdb, pObj);
63✔
1245
  }
1246

1247
  // vgroup info
1248
  pIter = NULL;
63✔
1249
  while (1) {
126✔
1250
    SVgObj *pVgroup = NULL;
189✔
1251
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
189✔
1252
    if (pIter == NULL) break;
189✔
1253

1254
    if (pVgroup->mountVgId) {
126✔
1255
      sdbRelease(pSdb, pVgroup);
×
1256
      continue;
×
1257
    }
1258

1259
    pClusterInfo->vgroups_total++;
126✔
1260
    pClusterInfo->tbs_total += pVgroup->numOfTables;
126✔
1261

1262
    SMonVgroupDesc desc = {0};
126✔
1263
    desc.vgroup_id = pVgroup->vgId;
126✔
1264

1265
    SName name = {0};
126✔
1266
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
126✔
1267
    if (code < 0) {
126✔
1268
      mError("failed to get db name since %s", tstrerror(code));
×
1269
      sdbCancelFetch(pSdb, pIter);
×
1270
      sdbRelease(pSdb, pVgroup);
×
1271
      TAOS_RETURN(code);
×
1272
    }
1273
    (void)tNameGetDbName(&name, desc.database_name);
126✔
1274

1275
    desc.tables_num = pVgroup->numOfTables;
126✔
1276
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
126✔
1277
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
126✔
1278
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
252✔
1279
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
126✔
1280
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
126✔
1281
      pVnDesc->dnode_id = pVgid->dnodeId;
126✔
1282
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
126✔
1283
      pVnDesc->syncState = pVgid->syncState;
126✔
1284
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
126✔
1285
        tstrncpy(desc.status, "ready", sizeof(desc.status));
126✔
1286
        pClusterInfo->vgroups_alive++;
126✔
1287
      }
1288
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
126✔
1289
        pClusterInfo->vnodes_alive++;
126✔
1290
      }
1291
      pClusterInfo->vnodes_total++;
126✔
1292
    }
1293

1294
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
252✔
1295
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1296
    }
1297
    sdbRelease(pSdb, pVgroup);
126✔
1298
  }
1299

1300
  // stb info
1301
  pIter = NULL;
63✔
1302
  while (1) {
×
1303
    SStbObj *pStb = NULL;
63✔
1304
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
63✔
1305
    if (pIter == NULL) break;
63✔
1306

1307
    SMonStbDesc desc = {0};
×
1308

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

1318
    SName name2 = {0};
×
1319
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1320
    if (code < 0) {
×
1321
      mError("failed to get table name since %s", tstrerror(code));
×
1322
      sdbRelease(pSdb, pStb);
×
1323
      TAOS_RETURN(code);
×
1324
    }
1325
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1326

1327
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1328
      mError("failed to put stb into array, but continue at this monitor report");
×
1329
    }
1330
    sdbRelease(pSdb, pStb);
×
1331
  }
1332

1333
  // grant info
1334
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
63✔
1335
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
63✔
1336
  if (pMnode->grant.expireTimeMS == 0) {
63✔
1337
    pGrantInfo->expire_time = 0;
×
1338
    pGrantInfo->timeseries_total = 0;
×
1339
  }
1340

1341
  mndReleaseRpc(pMnode);
63✔
1342
  TAOS_RETURN(code);
63✔
1343
}
1344

1345
int32_t mndResetTimer(SMnode *pMnode){
×
1346
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1347
}
1348

1349
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
27,470,830✔
1350
  mTrace("mnode get load");
27,470,830✔
1351
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
27,470,830✔
1352
  pLoad->syncState = state.state;
27,470,830✔
1353
  pLoad->syncRestore = state.restored;
27,470,830✔
1354
  pLoad->syncTerm = state.term;
27,470,830✔
1355
  pLoad->roleTimeMs = state.roleTimeMs;
27,470,830✔
1356
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
27,470,830✔
1357
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1358
  return 0;
27,470,830✔
1359
}
1360

1361
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,715,538✔
1362
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,715,538✔
1363
  return state.roleTimeMs;
8,715,538✔
1364
}
1365

1366
void mndSetRestored(SMnode *pMnode, bool restored) {
399,227✔
1367
  if (restored) {
399,227✔
1368
    (void)taosThreadRwlockWrlock(&pMnode->lock);
399,227✔
1369
    pMnode->restored = true;
399,227✔
1370
    (void)taosThreadRwlockUnlock(&pMnode->lock);
399,227✔
1371
    mInfo("mnode set restored:%d", restored);
399,227✔
1372
  } else {
1373
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1374
    pMnode->restored = false;
×
1375
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1376
    mInfo("mnode set restored:%d", restored);
×
1377
    while (1) {
1378
      if (pMnode->rpcRef <= 0) break;
×
1379
      taosMsleep(3);
×
1380
    }
1381
  }
1382
}
399,227✔
1383

1384
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1385

1386
void mndSetStop(SMnode *pMnode) {
399,227✔
1387
  (void)taosThreadRwlockWrlock(&pMnode->lock);
399,227✔
1388
  pMnode->stopped = true;
399,227✔
1389
  (void)taosThreadRwlockUnlock(&pMnode->lock);
399,227✔
1390
  mInfo("mnode set stopped");
399,227✔
1391
}
399,227✔
1392

1393
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
553,960,012✔
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