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

taosdata / TDengine / #4905

29 Dec 2025 02:08PM UTC coverage: 65.423% (-0.3%) from 65.734%
#4905

push

travis-ci

web-flow
enh: sign connect request (#34067)

23 of 29 new or added lines in 4 files covered. (79.31%)

11614 existing lines in 186 files now uncovered.

193476 of 295730 relevant lines covered (65.42%)

115752566.53 hits per line

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

78.8
/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 "tencrypt.h"
63

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

83
static inline void mndReleaseRpc(SMnode *pMnode) {
222,699,466✔
84
  (void)taosThreadRwlockRdlock(&pMnode->lock);
222,699,466✔
85
#if 1
86
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
222,701,295✔
87
#else
88
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
89
  mTrace("mnode rpc is released, ref:%d", ref);
90
#endif
91
  (void)taosThreadRwlockUnlock(&pMnode->lock);
222,700,479✔
92
}
222,701,839✔
93

94
static void *mndBuildTimerMsg(int32_t *pContLen) {
53,838,684✔
95
  terrno = 0;
53,838,684✔
96
  SMTimerReq timerReq = {0};
53,839,000✔
97

98
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
53,839,000✔
99
  if (contLen <= 0) return NULL;
53,837,765✔
100
  void *pReq = rpcMallocCont(contLen);
53,837,765✔
101
  if (pReq == NULL) return NULL;
53,837,508✔
102

103
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
53,837,508✔
UNCOV
104
    mError("failed to serialize timer msg since %s", terrstr());
×
105
  }
106
  *pContLen = contLen;
53,837,737✔
107
  return pReq;
53,838,183✔
108
}
109

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

304
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
113,549✔
305
  SSdb *pSdb = pMnode->pSdb;
113,549✔
306

307
  void *pIter = NULL;
113,549✔
308
  while (1) {
370,950✔
309
    SVgObj *pVgroup = NULL;
484,499✔
310
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
484,499✔
311
    if (pIter == NULL) break;
484,499✔
312

313
    bool stateChanged = false;
370,950✔
314
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
928,516✔
315
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
709,123✔
316
      if (pGid->dnodeId == dnodeId) {
709,123✔
317
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
151,557✔
318
          mInfo(
61,491✔
319
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
320
              "restored:0 "
321
              "canRead:0",
322
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
323
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
61,491✔
324
          pGid->syncRestore = 0;
61,491✔
325
          pGid->syncCanRead = 0;
61,491✔
326
          pGid->startTimeMs = 0;
61,491✔
327
          stateChanged = true;
61,491✔
328
        }
329
        break;
151,557✔
330
      }
331
    }
332

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

343
    sdbRelease(pSdb, pVgroup);
370,950✔
344
  }
345
}
113,549✔
346

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

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

354
  void *pIter = NULL;
4,274,565✔
355
  while (1) {
7,595,224✔
356
    SDnodeObj *pDnode = NULL;
11,869,789✔
357
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
11,869,789✔
358
    if (pIter == NULL) break;
11,869,789✔
359

360
    bool online = mndIsDnodeOnline(pDnode, curMs);
7,595,224✔
361
    if (!online) {
7,595,224✔
362
      mInfo("dnode:%d, in offline state", pDnode->id);
113,549✔
363
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
113,549✔
364
    }
365

366
    sdbRelease(pSdb, pDnode);
7,595,224✔
367
  }
368

369
  mndReleaseRpc(pMnode);
4,274,565✔
370
}
371

372
static bool mnodeIsNotLeader(SMnode *pMnode) {
255,000,892✔
373
  terrno = 0;
255,000,892✔
374
  (void)taosThreadRwlockRdlock(&pMnode->lock);
255,001,281✔
375
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
255,005,790✔
376
  if (terrno != 0) {
255,005,790✔
377
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
378
    return true;
×
379
  }
380

381
  if (state.state != TAOS_SYNC_STATE_LEADER) {
255,005,790✔
382
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,338,541✔
383
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,338,541✔
384
    return true;
10,338,541✔
385
  }
386
  if (!state.restored || !pMnode->restored) {
244,667,249✔
387
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,649✔
388
    terrno = TSDB_CODE_SYN_RESTORING;
2,618✔
389
    return true;
2,618✔
390
  }
391
  (void)taosThreadRwlockUnlock(&pMnode->lock);
244,664,600✔
392
  return false;
244,664,394✔
393
}
394

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

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

UNCOV
409
  return min <= 1 ? 2 : min;
×
410
}
411
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,101,786✔
412
  int32_t code = 0;
22,101,786✔
413
#ifndef TD_ASTRA
414
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,101,786✔
415
    mndPullupGrant(pMnode);
1,065,026✔
416
  }
417
  if (sec % tsTtlPushIntervalSec == 0) {
22,101,786✔
418
    mndPullupTtl(pMnode);
2,133,961✔
419
  }
420

421
  if (sec % tsTrimVDbIntervalSec == 0) {
22,101,786✔
UNCOV
422
    mndPullupTrimDb(pMnode);
×
423
  }
424

425
  if (sec % tsQueryTrimIntervalSec == 0) {
22,101,786✔
426
    mndPullupQueryTrimDb(pMnode);
2,116,400✔
427
  }
428
#endif
429
#ifdef USE_SHARED_STORAGE
430
  if (tsSsEnabled) {
22,101,786✔
431
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
UNCOV
432
      mndPullupUpdateSsMigrateProgress(pMnode);
×
433
    }
434
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
UNCOV
435
      mndPullupSsMigrateDb(pMnode);
×
436
    }
437
  }
438
#endif
439
#ifdef TD_ENTERPRISE
440
  if (tsAuthReq) {
22,101,786✔
441
    if (sec % tsAuthReqHBInterval == 0) {
×
UNCOV
442
      mndPullupAuth(pMnode);
×
443
    }
444
  }
445
#endif
446
  if (sec % tsTransPullupInterval == 0) {
22,101,786✔
447
    mndPullupTrans(pMnode);
10,958,009✔
448
  }
449

450
  if (sec % tsCompactPullupInterval == 0) {
22,101,786✔
451
    mndPullupCompacts(pMnode);
2,048,112✔
452
  }
453

454
  if (sec % tsScanPullupInterval == 0) {
22,101,786✔
455
    mndPullupScans(pMnode);
2,048,112✔
456
  }
457
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,101,786✔
458
    mndPullupInstances(pMnode);
4,275,117✔
459
  }
460
#ifdef USE_TOPIC
461
  if (sec % tsMqRebalanceInterval == 0) {
22,101,786✔
462
    mndCalMqRebalance(pMnode);
10,957,989✔
463
  }
464
#endif
465
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,101,786✔
466
    mndPullupTelem(pMnode);
130✔
467
  }
468
  if (sec % tsUptimeInterval == 0) {
22,101,786✔
469
    mndIncreaseUpTime(pMnode);
19,072✔
470
  }
471
}
22,101,786✔
472

473
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
222,562,845✔
474
  int32_t code = 0;
222,562,845✔
475
#ifndef TD_ASTRA
476
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
222,562,845✔
477
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
10,959,337✔
UNCOV
478
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
479
    }
480
  }
481

482
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
222,562,845✔
483
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,257,735✔
UNCOV
484
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
485
    }
486
  }
487
#endif
488
}
222,562,845✔
489

490
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
222,562,845✔
491
  if (ms % (tsStatusTimeoutMs) == 0) {
222,562,845✔
492
    mndCheckDnodeOffline(pMnode);
4,274,565✔
493
  }
494
}
222,562,845✔
495

496
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,101,786✔
497
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,101,786✔
498
    mndSyncCheckTimeout(pMnode);
606,419✔
499
  }
500
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,101,786✔
501
    msmHealthCheck(pMnode);
7,259,368✔
502
  }
503
}
22,101,786✔
504

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

510
  while (1) {
231,998,900✔
511
    lastTime++;
232,383,151✔
512
    taosMsleep(100);
232,383,151✔
513

514
    if (mndGetStop(pMnode)) break;
232,383,151✔
515
    if (lastTime % 10 != 0) continue;
231,998,900✔
516

517
    if (mnodeIsNotLeader(pMnode)) {
23,028,959✔
518
      mTrace("timer not process since mnode is not leader");
927,173✔
519
      continue;
927,173✔
520
    }
521

522
    int64_t sec = lastTime / 10;
22,101,786✔
523
    mndDoTimerCheckSync(pMnode, sec);
22,101,786✔
524

525
    mndDoTimerPullupTask(pMnode, sec);
22,101,786✔
526
  }
527

528
  return NULL;
384,251✔
529
}
530

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

536
  while (1) {
537
    lastTime += 100;
232,361,082✔
538
    taosMsleep(100);
232,361,082✔
539

540
    if (mndGetStop(pMnode)) break;
232,361,082✔
541
    if (lastTime % 10 != 0) continue;
231,976,831✔
542

543
    if (mnodeIsNotLeader(pMnode)) {
231,976,831✔
544
      mTrace("timer not process since mnode is not leader");
9,413,986✔
545
      continue;
9,413,986✔
546
    }
547

548
    mndDoTimerCheckStatus(pMnode, lastTime);
222,562,845✔
549

550
    mndDoArbTimerPullupTask(pMnode, lastTime);
222,562,845✔
551
  }
552

553
  return NULL;
384,251✔
554
}
555

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

569
  (void)taosThreadAttrDestroy(&thAttr);
384,251✔
570
  tmsgReportStartup("mnode-timer", "initialized");
384,251✔
571

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

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

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

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

607
  if (taosMkDir(pMnode->path) != 0) {
384,314✔
608
    code = terrno;
×
UNCOV
609
    TAOS_RETURN(code);
×
610
  }
611

612
  TAOS_RETURN(code);
384,314✔
613
}
614

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

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

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

648
  TAOS_RETURN(code);
384,314✔
649
}
650

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

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

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

672
  TAOS_RETURN(code);
384,314✔
673
}
674

675
static int32_t mndOpenSdb(SMnode *pMnode) {
384,314✔
676
  int32_t code = 0;
384,314✔
677
  if (!pMnode->deploy) {
384,314✔
678
    code = sdbReadFile(pMnode->pSdb);
107,767✔
679
  }
680

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

683
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
384,314✔
684
  return code;
384,314✔
685
}
686

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

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

703
  TAOS_RETURN(0);
18,831,386✔
704
}
705

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

761
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
384,251✔
762
  if (pMnode->pSteps == NULL) return;
384,251✔
763

764
  if (pos == -1) {
384,251✔
765
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
384,251✔
766
  }
767

768
  for (int32_t s = pos; s >= 0; s--) {
19,212,550✔
769
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
18,828,299✔
770
    mInfo("%s will cleanup", pStep->name);
18,828,299✔
771
    if (pStep->cleanupFp != NULL) {
18,828,299✔
772
      (*pStep->cleanupFp)(pMnode);
18,059,797✔
773
    }
774
  }
775

776
  taosArrayClear(pMnode->pSteps);
384,251✔
777
  taosArrayDestroy(pMnode->pSteps);
384,251✔
778
  pMnode->pSteps = NULL;
384,251✔
779
}
780

781
static int32_t mndExecSteps(SMnode *pMnode) {
384,314✔
782
  int32_t code = 0;
384,314✔
783
  int32_t size = taosArrayGetSize(pMnode->pSteps);
384,314✔
784
  for (int32_t pos = 0; pos < size; pos++) {
19,215,700✔
785
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
18,831,386✔
786
    if (pStep->initFp == NULL) continue;
18,831,386✔
787

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

798
  pMnode->clusterId = mndGetClusterId(pMnode);
384,314✔
799
  TAOS_RETURN(0);
384,314✔
800
}
801

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

813
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
384,314✔
814
  terrno = 0;
384,314✔
815
  mInfo("start to open mnode in %s", path);
384,314✔
816

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

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

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

843
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
384,314✔
844
        pOption->numOfTotalReplicas);
845
  mndSetOptions(pMnode, pOption);
384,314✔
846

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

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

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

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

881
  mInfo("mnode open successfully");
384,314✔
882
  return pMnode;
384,314✔
883
}
884

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

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

911
int32_t mndStart(SMnode *pMnode) {
384,251✔
912
  mndSyncStart(pMnode);
384,251✔
913
  if (pMnode->deploy) {
384,251✔
914
    if (sdbDeploy(pMnode->pSdb) != 0) {
276,547✔
UNCOV
915
      mError("failed to deploy sdb while start mnode");
×
UNCOV
916
      return -1;
×
917
    }
918
    mndSetRestored(pMnode, true);
276,547✔
919
  }
920
  if (mndIsLeader(pMnode)) {
384,251✔
921
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
348,181✔
UNCOV
922
      mError("failed to upgrade sdb while start mnode");
×
UNCOV
923
      return -1;
×
924
    }
925
  }
926
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
384,251✔
927
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
384,251✔
928

929
  return mndInitTimer(pMnode);
384,251✔
930
}
931

932
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
384,251✔
933

934
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
301,447✔
935

936
int32_t mndIsCatchUp(SMnode *pMnode) {
145,865✔
937
  int64_t rid = pMnode->syncMgmt.sync;
145,865✔
938
  return syncIsCatchUp(rid);
145,865✔
939
}
940

941
ESyncRole mndGetRole(SMnode *pMnode) {
145,865✔
942
  int64_t rid = pMnode->syncMgmt.sync;
145,865✔
943
  return syncGetRole(rid);
145,865✔
944
}
945

946
int64_t mndGetTerm(SMnode *pMnode) {
7,362,569✔
947
  int64_t rid = pMnode->syncMgmt.sync;
7,362,569✔
948
  return syncGetTerm(rid);
7,362,569✔
949
}
950

951
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,322,061✔
952

953
void mndStop(SMnode *pMnode) {
384,251✔
954
  mndSetStop(pMnode);
384,251✔
955
  mndSyncStop(pMnode);
384,251✔
956
  mndCleanupTimer(pMnode);
384,251✔
957
}
384,251✔
958

959
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
47,433,849✔
960
  SMnode    *pMnode = pMsg->info.node;
47,433,849✔
961
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
47,433,849✔
962

963
  const STraceId *trace = &pMsg->info.traceId;
47,433,849✔
964
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
47,433,849✔
965

966
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
47,433,849✔
967
  if (code != 0) {
47,433,849✔
968
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
449✔
969
            tstrerror(code), code);
970
  }
971

972
  return code;
47,433,849✔
973
}
974

975
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
221,960,995✔
976
  int32_t code = 0;
221,960,995✔
977
  if (!IsReq(pMsg)) TAOS_RETURN(code);
221,960,995✔
978
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
194,157,757✔
979
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
189,796,492✔
980
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
186,139,683✔
981
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
177,464,461✔
982
    TAOS_RETURN(code);
16,704,122✔
983
  }
984

985
  SMnode *pMnode = pMsg->info.node;
177,466,171✔
986
  (void)taosThreadRwlockRdlock(&pMnode->lock);
177,460,685✔
987
  if (pMnode->stopped) {
177,471,997✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,957✔
989
    code = TSDB_CODE_APP_IS_STOPPING;
3,957✔
990
    TAOS_RETURN(code);
3,957✔
991
  }
992

993
  terrno = 0;
177,457,898✔
994
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
177,462,460✔
995
  if (terrno != 0) {
177,467,610✔
996
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
997
    code = terrno;
×
UNCOV
998
    TAOS_RETURN(code);
×
999
  }
1000

1001
  if (state.state != TAOS_SYNC_STATE_LEADER) {
177,465,639✔
1002
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,737,391✔
1003
    code = TSDB_CODE_SYN_NOT_LEADER;
1,737,391✔
1004
    goto _OVER;
1,737,391✔
1005
  }
1006

1007
  if (!state.restored || !pMnode->restored) {
175,728,248✔
1008
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,810,184✔
1009
    code = TSDB_CODE_SYN_RESTORING;
1,811,163✔
1010
    goto _OVER;
1,811,163✔
1011
  }
1012

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

1020
  (void)taosThreadRwlockUnlock(&pMnode->lock);
173,918,808✔
1021
  TAOS_RETURN(code);
173,919,528✔
1022

1023
_OVER:
3,548,554✔
1024
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,548,554✔
1025
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,548,554✔
1026
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,548,554✔
1027
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,547,748✔
1028
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,547,829✔
1029
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,548,554✔
1030
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,546,379✔
1031
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,548,554✔
1032
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,546,769✔
UNCOV
1033
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1034
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1035
    TAOS_RETURN(code);
×
1036
  }
1037

1038
  const STraceId *trace = &pMsg->info.traceId;
3,545,252✔
1039
  SEpSet          epSet = {0};
3,547,104✔
1040
  mndGetMnodeEpSet(pMnode, &epSet);
3,546,379✔
1041

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

1048
  if (epSet.numOfEps <= 0) return -1;
3,548,554✔
1049

1050
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
10,419,527✔
1051
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,870,973✔
1052
  }
1053

1054
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,548,554✔
1055
  pMsg->info.rsp = rpcMallocCont(contLen);
3,547,126✔
1056
  if (pMsg->info.rsp != NULL) {
3,547,104✔
1057
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,547,829✔
UNCOV
1058
      mError("failed to serialize ep set");
×
1059
    }
1060
    pMsg->info.hasEpSet = 1;
3,540,218✔
1061
    pMsg->info.rspLen = contLen;
3,541,587✔
1062
  }
1063

1064
  TAOS_RETURN(code);
3,544,606✔
1065
}
1066

1067
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
221,976,147✔
1068
  SMnode         *pMnode = pMsg->info.node;
221,976,147✔
1069
  const STraceId *trace = &pMsg->info.traceId;
221,977,452✔
1070
  int32_t         code = TSDB_CODE_SUCCESS;
221,976,006✔
1071

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

1094
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
221,957,491✔
1095
  MndMsgFpExt fpExt = NULL;
221,973,076✔
1096
  if (fp == NULL) {
221,973,076✔
1097
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
16,703,280✔
1098
    if (fpExt == NULL) {
16,702,890✔
1099
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
1100
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
UNCOV
1101
      TAOS_RETURN(code);
×
1102
    }
1103
  }
1104

1105
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
221,972,686✔
1106

1107
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
218,422,774✔
1108
  if (fp)
218,424,665✔
1109
    code = (*fp)(pMsg);
201,721,385✔
1110
  else
1111
    code = (*fpExt)(pMsg, pQueueInfo);
16,703,280✔
1112
  mndReleaseRpc(pMnode);
218,426,086✔
1113

1114
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
218,427,206✔
1115
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
26,602,117✔
1116
  } else if (code == 0) {
191,825,089✔
1117
    mGTrace("msg:%p, successfully processed", pMsg);
189,428,781✔
1118
  } else {
1119
    // TODO removve this wrong set code
1120
    if (code == -1) {
2,396,308✔
UNCOV
1121
      code = terrno;
×
1122
    }
1123
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,396,308✔
1124
            TMSG_INFO(pMsg->msgType));
1125
  }
1126

1127
  TAOS_RETURN(code);
218,427,206✔
1128
}
1129

1130
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
82,243,196✔
1131
  tmsg_t type = TMSG_INDEX(msgType);
82,243,196✔
1132
  if (type < TDMT_MAX) {
82,243,196✔
1133
    pMnode->msgFp[type] = fp;
82,243,196✔
1134
  }
1135
}
82,243,196✔
1136

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

1144
// Note: uid 0 is reserved
1145
int64_t mndGenerateUid(const char *name, int32_t len) {
8,944,856✔
1146
  int32_t hashval = MurmurHash3_32(name, len);
8,944,856✔
UNCOV
1147
  do {
×
1148
    int64_t us = taosGetTimestampUs();
8,945,088✔
1149
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
8,945,088✔
1150
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
8,945,088✔
1151
    if (uuid) {
8,945,279✔
1152
      return llabs(uuid);
8,945,279✔
1153
    }
1154
  } while (true);
1155
}
1156

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

1166
  SSdb   *pSdb = pMnode->pSdb;
68✔
1167
  int64_t ms = taosGetTimestampMs();
68✔
1168

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

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

1190
  void *pIter = NULL;
68✔
1191
  while (1) {
68✔
1192
    SDnodeObj *pObj = NULL;
136✔
1193
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
136✔
1194
    if (pIter == NULL) break;
136✔
1195

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

1210
  pIter = NULL;
68✔
1211
  while (1) {
68✔
1212
    SMnodeObj *pObj = NULL;
136✔
1213
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
136✔
1214
    if (pIter == NULL) break;
136✔
1215

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

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

1238
  // vgroup info
1239
  pIter = NULL;
68✔
1240
  while (1) {
408✔
1241
    SVgObj *pVgroup = NULL;
476✔
1242
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
476✔
1243
    if (pIter == NULL) break;
476✔
1244

1245
    if (pVgroup->mountVgId) {
408✔
UNCOV
1246
      sdbRelease(pSdb, pVgroup);
×
UNCOV
1247
      continue;
×
1248
    }
1249

1250
    pClusterInfo->vgroups_total++;
408✔
1251
    pClusterInfo->tbs_total += pVgroup->numOfTables;
408✔
1252

1253
    SMonVgroupDesc desc = {0};
408✔
1254
    desc.vgroup_id = pVgroup->vgId;
408✔
1255

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

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

1285
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
816✔
UNCOV
1286
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1287
    }
1288
    sdbRelease(pSdb, pVgroup);
408✔
1289
  }
1290

1291
  // stb info
1292
  pIter = NULL;
68✔
1293
  while (1) {
68✔
1294
    SStbObj *pStb = NULL;
136✔
1295
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
136✔
1296
    if (pIter == NULL) break;
136✔
1297

1298
    SMonStbDesc desc = {0};
68✔
1299

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

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

1318
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
136✔
UNCOV
1319
      mError("failed to put stb into array, but continue at this monitor report");
×
1320
    }
1321
    sdbRelease(pSdb, pStb);
68✔
1322
  }
1323

1324
  // grant info
1325
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
68✔
1326
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
68✔
1327
  if (pMnode->grant.expireTimeMS == 0) {
68✔
UNCOV
1328
    pGrantInfo->expire_time = 0;
×
UNCOV
1329
    pGrantInfo->timeseries_total = 0;
×
1330
  }
1331

1332
  mndReleaseRpc(pMnode);
68✔
1333
  TAOS_RETURN(code);
68✔
1334
}
1335

UNCOV
1336
int32_t mndResetTimer(SMnode *pMnode){
×
UNCOV
1337
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1338
}
1339

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

1352
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,257,735✔
1353
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,257,735✔
1354
  return state.roleTimeMs;
7,257,735✔
1355
}
1356

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

UNCOV
1375
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1376

1377
void mndSetStop(SMnode *pMnode) {
384,251✔
1378
  (void)taosThreadRwlockWrlock(&pMnode->lock);
384,251✔
1379
  pMnode->stopped = true;
384,251✔
1380
  (void)taosThreadRwlockUnlock(&pMnode->lock);
384,251✔
1381
  mInfo("mnode set stopped");
384,251✔
1382
}
384,251✔
1383

1384
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
464,733,093✔
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