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

taosdata / TDengine / #4942

29 Jan 2026 06:55AM UTC coverage: 66.788% (-0.08%) from 66.868%
#4942

push

travis-ci

web-flow
fix: explain no response issue (#34435)

17 of 18 new or added lines in 3 files covered. (94.44%)

672 existing lines in 122 files now uncovered.

204165 of 305690 relevant lines covered (66.79%)

125991233.79 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
250,479,594✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
250,479,594✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
250,480,974✔
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);
250,480,410✔
93
}
250,479,975✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
64,981,960✔
96
  terrno = 0;
64,981,960✔
97
  SMTimerReq timerReq = {0};
64,981,960✔
98

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

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

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

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

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

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

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

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

184
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,708,704✔
185
  mTrace("pullup trim query");
2,708,704✔
186
  int32_t contLen = 0;
2,708,704✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
2,708,704✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,708,704✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,708,704✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
2,708,704✔
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) {
12,718,729✔
219
  mTrace("pullup arb hb");
12,718,729✔
220
  int32_t contLen = 0;
12,718,729✔
221
  void   *pReq = mndBuildTimerMsg(&contLen);
12,718,729✔
222
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
12,718,729✔
223
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
12,718,729✔
224
}
225

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

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

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

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

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

308
  void *pIter = NULL;
111,646✔
309
  while (1) {
363,013✔
310
    SVgObj *pVgroup = NULL;
474,659✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
474,659✔
312
    if (pIter == NULL) break;
474,659✔
313

314
    bool stateChanged = false;
363,013✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
937,376✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
716,730✔
317
      if (pGid->dnodeId == dnodeId) {
716,730✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
142,367✔
319
          mInfo(
48,420✔
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;
48,420✔
325
          pGid->syncRestore = 0;
48,420✔
326
          pGid->syncCanRead = 0;
48,420✔
327
          pGid->startTimeMs = 0;
48,420✔
328
          pGid->learnerProgress = 0;
48,420✔
329
          pGid->snapSeq = -1;
48,420✔
330
          stateChanged = true;
48,420✔
331
        }
332
        break;
142,367✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
363,013✔
347
  }
348
}
111,646✔
349

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

354
  SSdb   *pSdb = pMnode->pSdb;
4,978,833✔
355
  int64_t curMs = taosGetTimestampMs();
4,978,833✔
356

357
  void *pIter = NULL;
4,978,833✔
358
  while (1) {
8,323,750✔
359
    SDnodeObj *pDnode = NULL;
13,302,583✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,302,583✔
361
    if (pIter == NULL) break;
13,302,583✔
362

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

369
    sdbRelease(pSdb, pDnode);
8,323,750✔
370
  }
371

372
  mndReleaseRpc(pMnode);
4,978,833✔
373
}
374

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

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
294,747,947✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,631,329✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,631,404✔
387
    return true;
10,631,251✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
284,116,618✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,377✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
1,837✔
392
    return true;
1,837✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
284,114,241✔
395
  return false;
284,114,594✔
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) {
26,304,791✔
415
  int32_t code = 0;
26,304,791✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
26,304,791✔
418
    mndPullupGrant(pMnode);
1,428,739✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
26,304,791✔
421
    mndPullupTtl(pMnode);
2,757,020✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
26,304,791✔
425
    mndPullupTrimDb(pMnode);
10,582✔
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
26,304,791✔
429
    mndPullupQueryTrimDb(pMnode);
2,708,704✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
26,304,791✔
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) {
26,304,791✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
26,304,791✔
450
    mndPullupTrans(pMnode);
13,148,949✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
26,304,791✔
454
    mndPullupCompacts(pMnode);
2,642,970✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
26,304,791✔
458
    mndPullupScans(pMnode);
2,643,108✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
26,304,791✔
461
    mndPullupInstances(pMnode);
5,260,491✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
26,304,791✔
465
    mndCalMqRebalance(pMnode);
13,148,739✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
26,304,791✔
469
    mndPullupTelem(pMnode);
189✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
26,304,791✔
472
    mndIncreaseUpTime(pMnode);
87,567✔
473
  }
474
}
26,304,791✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
257,810,636✔
477
  int32_t code = 0;
257,810,636✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
257,810,636✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
12,718,729✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

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

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
257,810,636✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
257,810,636✔
495
    mndCheckDnodeOffline(pMnode);
4,978,833✔
496
  }
497
}
257,810,636✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
26,304,791✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
26,304,791✔
501
    mndSyncCheckTimeout(pMnode);
878,027✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
26,304,791✔
504
    msmHealthCheck(pMnode);
8,763,611✔
505
  }
506
}
26,304,791✔
507

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

513
  while (1) {
267,856,025✔
514
    if (mndGetStop(pMnode)) break;
268,252,107✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
267,856,025✔
517
    if (nowSec == lastSec) {
267,856,025✔
518
      taosMsleep(100);
240,542,013✔
519
      continue;
240,542,013✔
520
    }
521
    lastSec = nowSec;
27,314,012✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
27,314,012✔
524
      taosMsleep(100);
1,009,221✔
525
      mTrace("timer not process since mnode is not leader");
1,009,221✔
526
      continue;
1,009,221✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
26,304,791✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
26,304,791✔
532

533
    taosMsleep(100);
26,304,791✔
534
  }
535

536
  return NULL;
396,082✔
537
}
538

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

544
  while (1) {
545
    lastTime += 100;
267,830,816✔
546
    taosMsleep(100);
267,830,816✔
547

548
    if (mndGetStop(pMnode)) break;
267,830,816✔
549
    if (lastTime % 10 != 0) continue;
267,434,734✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
267,434,734✔
552
      mTrace("timer not process since mnode is not leader");
9,624,098✔
553
      continue;
9,624,098✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
257,810,636✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
257,810,636✔
559
  }
560

561
  return NULL;
396,082✔
562
}
563

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

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

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

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

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

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

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

620
  TAOS_RETURN(code);
396,141✔
621
}
622

623
static int32_t mndInitWal(SMnode *pMnode) {
396,141✔
624
  int32_t code = 0;
396,141✔
625
  char    path[PATH_MAX + 20] = {0};
396,141✔
626
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
396,141✔
627
  SWalCfg cfg = {.vgId = 1,
396,141✔
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) {
396,141✔
640
    code = terrno;
×
641
    TAOS_RETURN(code);
×
642
  }
643
  if (tsMetaKey[0] != '\0') {
396,141✔
644
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
3,743✔
645
  }
646
#endif
647

648
  pMnode->pWal = walOpen(path, &cfg);
396,141✔
649
  if (pMnode->pWal == NULL) {
396,141✔
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);
396,141✔
657
}
658

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

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

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

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

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

714
  TAOS_RETURN(code);
396,141✔
715
}
716

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

726
  if (!pMnode->deploy) {
396,141✔
727
    code = sdbReadFile(pMnode->pSdb);
113,519✔
728
  }
729

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

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

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

743
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,807,050✔
744
  SMnodeStep step = {0};
19,807,050✔
745
  step.name = name;
19,807,050✔
746
  step.initFp = initFp;
19,807,050✔
747
  step.cleanupFp = cleanupFp;
19,807,050✔
748
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
39,614,100✔
749
    TAOS_RETURN(terrno);
×
750
  }
751

752
  TAOS_RETURN(0);
19,807,050✔
753
}
754

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

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

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

818
  for (int32_t s = pos; s >= 0; s--) {
20,200,182✔
819
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,804,100✔
820
    mInfo("%s will cleanup", pStep->name);
19,804,100✔
821
    if (pStep->cleanupFp != NULL) {
19,804,100✔
822
      (*pStep->cleanupFp)(pMnode);
19,011,936✔
823
    }
824
  }
825

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

831
static int32_t mndExecSteps(SMnode *pMnode) {
396,141✔
832
  int32_t code = 0;
396,141✔
833
  int32_t size = taosArrayGetSize(pMnode->pSteps);
396,141✔
834
  for (int32_t pos = 0; pos < size; pos++) {
20,203,191✔
835
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,807,050✔
836
    if (pStep->initFp == NULL) continue;
19,807,050✔
837

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

980
  return mndInitTimer(pMnode);
396,082✔
981
}
982

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

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

987
int32_t mndIsCatchUp(SMnode *pMnode) {
193,380✔
988
  int64_t rid = pMnode->syncMgmt.sync;
193,380✔
989
  return syncIsCatchUp(rid);
193,380✔
990
}
991

992
ESyncRole mndGetRole(SMnode *pMnode) {
193,380✔
993
  int64_t rid = pMnode->syncMgmt.sync;
193,380✔
994
  return syncGetRole(rid);
193,380✔
995
}
996

997
int64_t mndGetTerm(SMnode *pMnode) {
8,531,497✔
998
  int64_t rid = pMnode->syncMgmt.sync;
8,531,497✔
999
  return syncGetTerm(rid);
8,531,497✔
1000
}
1001

1002
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,252,973✔
1003

1004
void mndStop(SMnode *pMnode) {
396,082✔
1005
  mndSetStop(pMnode);
396,082✔
1006
  mndSyncStop(pMnode);
396,082✔
1007
  mndCleanupTimer(pMnode);
396,082✔
1008
}
396,082✔
1009

1010
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
50,646,567✔
1011
  SMnode    *pMnode = pMsg->info.node;
50,646,567✔
1012
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
50,646,671✔
1013

1014
  const STraceId *trace = &pMsg->info.traceId;
50,646,671✔
1015
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
50,646,485✔
1016

1017
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
50,646,485✔
1018
  if (code != 0) {
50,646,671✔
1019
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
611✔
1020
            tstrerror(code), code);
1021
  }
1022

1023
  return code;
50,646,671✔
1024
}
1025

1026
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
249,418,135✔
1027
  int32_t code = 0;
249,418,135✔
1028
  if (!IsReq(pMsg)) TAOS_RETURN(code);
249,418,135✔
1029
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
218,871,115✔
1030
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
214,196,333✔
1031
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
210,334,397✔
1032
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
201,141,661✔
1033
    TAOS_RETURN(code);
17,735,812✔
1034
  }
1035

1036
  SMnode *pMnode = pMsg->info.node;
201,153,941✔
1037
  (void)taosThreadRwlockRdlock(&pMnode->lock);
201,150,036✔
1038
  if (pMnode->stopped) {
201,156,086✔
1039
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,659✔
1040
    code = TSDB_CODE_APP_IS_STOPPING;
12,659✔
1041
    TAOS_RETURN(code);
12,659✔
1042
  }
1043

1044
  terrno = 0;
201,135,641✔
1045
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
201,138,921✔
1046
  if (terrno != 0) {
201,145,822✔
1047
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1048
    code = terrno;
×
1049
    TAOS_RETURN(code);
×
1050
  }
1051

1052
  if (state.state != TAOS_SYNC_STATE_LEADER) {
201,144,132✔
1053
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,880,479✔
1054
    code = TSDB_CODE_SYN_NOT_LEADER;
1,880,479✔
1055
    goto _OVER;
1,880,479✔
1056
  }
1057

1058
  if (!state.restored || !pMnode->restored) {
199,263,653✔
1059
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,034,541✔
1060
    code = TSDB_CODE_SYN_RESTORING;
2,033,435✔
1061
    goto _OVER;
2,033,435✔
1062
  }
1063

1064
#if 1
1065
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
197,229,713✔
1066
#else
1067
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1068
  mTrace("mnode rpc is acquired, ref:%d", ref);
1069
#endif
1070

1071
  (void)taosThreadRwlockUnlock(&pMnode->lock);
197,231,674✔
1072
  TAOS_RETURN(code);
197,231,253✔
1073

1074
_OVER:
3,913,914✔
1075
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,913,914✔
1076
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,912,444✔
1077
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,913,097✔
1078
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,911,751✔
1079
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,913,914✔
1080
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,913,179✔
1081
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,912,359✔
1082
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,913,139✔
1083
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,913,179✔
1084
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
2,415✔
1085
           pMnode->stopped, state.restored, syncStr(state.state));
1086
    TAOS_RETURN(code);
2,415✔
1087
  }
1088

1089
  const STraceId *trace = &pMsg->info.traceId;
3,910,578✔
1090
  SEpSet          epSet = {0};
3,911,669✔
1091
  mndGetMnodeEpSet(pMnode, &epSet);
3,912,404✔
1092

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

1099
  if (epSet.numOfEps <= 0) return -1;
3,913,914✔
1100

1101
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
11,381,238✔
1102
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
7,467,324✔
1103
  }
1104

1105
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,913,914✔
1106
  pMsg->info.rsp = rpcMallocCont(contLen);
3,910,750✔
1107
  if (pMsg->info.rsp != NULL) {
3,912,298✔
1108
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,912,298✔
1109
      mError("failed to serialize ep set");
×
1110
    }
1111
    pMsg->info.hasEpSet = 1;
3,911,166✔
1112
    pMsg->info.rspLen = contLen;
3,910,416✔
1113
  }
1114

1115
  TAOS_RETURN(code);
3,913,312✔
1116
}
1117

1118
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
249,430,699✔
1119
  SMnode         *pMnode = pMsg->info.node;
249,430,699✔
1120
  const STraceId *trace = &pMsg->info.traceId;
249,430,707✔
1121
  int32_t         code = TSDB_CODE_SUCCESS;
249,431,252✔
1122

1123
#ifdef TD_ENTERPRISE
1124
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
249,431,252✔
1125
    SCachedTokenInfo ti = {0};
17,100✔
1126
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
17,100✔
1127
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,999✔
1128
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,999✔
1129
      TAOS_RETURN(code);
1,999✔
1130
    }
1131
    if (ti.enabled == 0) {
15,101✔
1132
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,770✔
1133
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,770✔
1134
      TAOS_RETURN(code);
2,770✔
1135
    }
1136
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
12,331✔
1137
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1138
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1139
      TAOS_RETURN(code);
×
1140
    }
1141
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
12,331✔
1142
  }
1143
#endif
1144

1145
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
249,413,806✔
1146
  MndMsgFpExt fpExt = NULL;
249,423,814✔
1147
  if (fp == NULL) {
249,423,814✔
1148
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,721,873✔
1149
    if (fpExt == NULL) {
17,721,873✔
1150
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1151
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1152
      TAOS_RETURN(code);
×
1153
    }
1154
  }
1155

1156
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
249,423,814✔
1157

1158
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
245,498,082✔
1159
  if (fp)
245,498,560✔
1160
    code = (*fp)(pMsg);
227,777,333✔
1161
  else
1162
    code = (*fpExt)(pMsg, pQueueInfo);
17,721,227✔
1163
  mndReleaseRpc(pMnode);
245,499,992✔
1164

1165
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
245,500,277✔
1166
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
28,072,277✔
1167
  } else if (code == 0) {
217,428,000✔
1168
    mGTrace("msg:%p, successfully processed", pMsg);
214,936,204✔
1169
  } else {
1170
    // TODO removve this wrong set code
1171
    if (code == -1) {
2,491,796✔
1172
      code = terrno;
×
1173
    }
1174
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,491,796✔
1175
            TMSG_INFO(pMsg->msgType));
1176
  }
1177

1178
  TAOS_RETURN(code);
245,500,277✔
1179
}
1180

1181
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
92,300,853✔
1182
  tmsg_t type = TMSG_INDEX(msgType);
92,300,853✔
1183
  if (type < TDMT_MAX) {
92,300,853✔
1184
    pMnode->msgFp[type] = fp;
92,300,853✔
1185
  }
1186
}
92,300,853✔
1187

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

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

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

1217
  SSdb   *pSdb = pMnode->pSdb;
69✔
1218
  int64_t ms = taosGetTimestampMs();
69✔
1219

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

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

1241
  void *pIter = NULL;
69✔
1242
  while (1) {
69✔
1243
    SDnodeObj *pObj = NULL;
138✔
1244
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
138✔
1245
    if (pIter == NULL) break;
138✔
1246

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

1261
  pIter = NULL;
69✔
1262
  while (1) {
69✔
1263
    SMnodeObj *pObj = NULL;
138✔
1264
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
138✔
1265
    if (pIter == NULL) break;
138✔
1266

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

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

1289
  // vgroup info
1290
  pIter = NULL;
69✔
1291
  while (1) {
138✔
1292
    SVgObj *pVgroup = NULL;
207✔
1293
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
207✔
1294
    if (pIter == NULL) break;
207✔
1295

1296
    if (pVgroup->mountVgId) {
138✔
1297
      sdbRelease(pSdb, pVgroup);
×
1298
      continue;
×
1299
    }
1300

1301
    pClusterInfo->vgroups_total++;
138✔
1302
    pClusterInfo->tbs_total += pVgroup->numOfTables;
138✔
1303

1304
    SMonVgroupDesc desc = {0};
138✔
1305
    desc.vgroup_id = pVgroup->vgId;
138✔
1306

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

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

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

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

UNCOV
1349
    SMonStbDesc desc = {0};
×
1350

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

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

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

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

1383
  mndReleaseRpc(pMnode);
69✔
1384
  TAOS_RETURN(code);
69✔
1385
}
1386

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

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

1403
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,426,173✔
1404
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,426,173✔
1405
  return state.roleTimeMs;
8,426,173✔
1406
}
1407

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

1426
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1427

1428
void mndSetStop(SMnode *pMnode) {
396,082✔
1429
  (void)taosThreadRwlockWrlock(&pMnode->lock);
396,082✔
1430
  pMnode->stopped = true;
396,082✔
1431
  (void)taosThreadRwlockUnlock(&pMnode->lock);
396,082✔
1432
  mInfo("mnode set stopped");
396,082✔
1433
}
396,082✔
1434

1435
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
536,069,072✔
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