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

taosdata / TDengine / #5005

26 Mar 2026 12:51PM UTC coverage: 72.152% (-0.2%) from 72.338%
#5005

push

travis-ci

web-flow
merge: from main to 3.0 branch #34951

512 of 851 new or added lines in 47 files covered. (60.16%)

6189 existing lines in 147 files now uncovered.

253282 of 351039 relevant lines covered (72.15%)

132156710.33 hits per line

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

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

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
281,682,312✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
281,682,312✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
281,682,260✔
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);
281,681,078✔
93
}
281,683,080✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
73,157,193✔
96
  terrno = 0;
73,157,193✔
97
  SMTimerReq timerReq = {0};
73,157,193✔
98

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

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

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

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

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

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

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

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

184
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,157,473✔
185
  mTrace("pullup trim query");
3,157,473✔
186
  int32_t contLen = 0;
3,157,473✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
3,157,473✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,157,473✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,157,473✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
3,157,473✔
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) {
171,228✔
209
  mTrace("pullup update ssmigrate progress");
171,228✔
210
  int32_t contLen = 0;
171,228✔
211
  void   *pReq = mndBuildTimerMsg(&contLen);
171,228✔
212
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
171,228✔
213
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
171,228✔
214
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
215
  }
216
}
171,228✔
217

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

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

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

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

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

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

308
  void *pIter = NULL;
125,513✔
309
  while (1) {
419,521✔
310
    SVgObj *pVgroup = NULL;
545,034✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
545,034✔
312
    if (pIter == NULL) break;
545,034✔
313

314
    bool stateChanged = false;
419,521✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,054,152✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
825,687✔
317
      if (pGid->dnodeId == dnodeId) {
825,687✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
191,056✔
319
          mInfo(
62,940✔
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;
62,940✔
325
          pGid->syncRestore = 0;
62,940✔
326
          pGid->syncCanRead = 0;
62,940✔
327
          pGid->startTimeMs = 0;
62,940✔
328
          pGid->learnerProgress = 0;
62,940✔
329
          pGid->snapSeq = -1;
62,940✔
330
          stateChanged = true;
62,940✔
331
        }
332
        break;
191,056✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
419,521✔
347
  }
348
}
125,513✔
349

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

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

357
  void *pIter = NULL;
5,560,012✔
358
  while (1) {
9,285,855✔
359
    SDnodeObj *pDnode = NULL;
14,845,867✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
14,845,867✔
361
    if (pIter == NULL) break;
14,845,867✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
9,285,855✔
364
    if (!online) {
9,285,855✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
125,513✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
125,513✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
9,285,855✔
370
  }
371

372
  mndReleaseRpc(pMnode);
5,560,012✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
329,362,533✔
376
  terrno = 0;
329,362,533✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
329,363,030✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
329,362,692✔
379
  if (terrno != 0) {
329,363,045✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
329,362,363✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,265,399✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,265,399✔
387
    return true;
11,265,399✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
318,096,964✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
9,050✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
7,421✔
392
    return true;
7,421✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
318,088,596✔
395
  return false;
318,089,407✔
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) {
29,505,537✔
415
  int32_t code = 0;
29,505,537✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
29,505,537✔
418
    mndPullupGrant(pMnode);
1,662,873✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
29,505,537✔
421
    mndPullupTtl(pMnode);
3,089,509✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
29,505,537✔
425
    mndPullupTrimDb(pMnode);
10,468✔
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
29,505,537✔
429
    mndPullupQueryTrimDb(pMnode);
3,157,473✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
29,505,537✔
434
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
235,814✔
435
      mndPullupUpdateSsMigrateProgress(pMnode);
171,228✔
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
235,814✔
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
29,505,537✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
29,505,537✔
450
    mndPullupTrans(pMnode);
14,755,498✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
29,505,537✔
454
    mndPullupCompacts(pMnode);
2,948,624✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
29,505,537✔
458
    mndPullupScans(pMnode);
2,948,695✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
29,505,537✔
461
    mndPullupInstances(pMnode);
5,903,605✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
29,505,537✔
465
    mndCalMqRebalance(pMnode);
14,755,407✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
29,505,537✔
469
    mndPullupTelem(pMnode);
159✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
29,505,537✔
472
    mndIncreaseUpTime(pMnode);
98,237✔
473
  }
474
}
29,505,537✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
288,585,064✔
477
  int32_t code = 0;
288,585,064✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
288,585,064✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
14,234,207✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
288,585,064✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
9,421,210✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
288,585,064✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
288,585,064✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
288,585,064✔
495
    mndCheckDnodeOffline(pMnode);
5,560,012✔
496
  }
497
}
288,585,064✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
29,505,537✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
29,505,537✔
501
    mndSyncCheckTimeout(pMnode);
969,174✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
29,505,537✔
504
    msmHealthCheck(pMnode);
9,830,400✔
505
  }
506
}
29,505,537✔
507

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

513
  while (1) {
299,279,975✔
514
    if (mndGetStop(pMnode)) break;
299,734,082✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
299,279,975✔
517
    if (nowSec == lastSec) {
299,279,975✔
518
      taosMsleep(100);
268,705,042✔
519
      continue;
268,705,042✔
520
    }
521
    lastSec = nowSec;
30,574,933✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
30,574,933✔
524
      taosMsleep(100);
1,069,396✔
525
      mTrace("timer not process since mnode is not leader");
1,069,396✔
526
      continue;
1,069,396✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
29,505,537✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
29,505,537✔
532

533
    taosMsleep(100);
29,505,537✔
534
  }
535

536
  return NULL;
454,107✔
537
}
538

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

544
  while (1) {
545
    lastTime += 100;
299,242,595✔
546
    taosMsleep(100);
299,242,595✔
547

548
    if (mndGetStop(pMnode)) break;
299,242,595✔
549
    if (lastTime % 10 != 0) continue;
298,788,488✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
298,788,488✔
552
      mTrace("timer not process since mnode is not leader");
10,203,424✔
553
      continue;
10,203,424✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
288,585,064✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
288,585,064✔
559
  }
560

561
  return NULL;
454,107✔
562
}
563

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

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

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

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

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

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

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

620
  TAOS_RETURN(code);
454,244✔
621
}
622

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

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

659
static void mndCloseWal(SMnode *pMnode) {
454,180✔
660
  if (pMnode->pWal != NULL) {
454,180✔
661
    walClose(pMnode->pWal);
454,180✔
662
    pMnode->pWal = NULL;
454,180✔
663
  }
664
}
454,180✔
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) {
6,410✔
672
  SMnode *pMnode = (SMnode *)param;
6,410✔
673
  if (pMnode == NULL) {
6,410✔
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  
677
  mInfo("persisting encrypted flag to mnode.json");
6,410✔
678
  
679
  SMnodeOpt option = {0};
6,410✔
680
  int32_t code = mmReadFile(pMnode->path, &option);
6,410✔
681
  if (code != 0) {
6,410✔
682
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
683
    return code;
×
684
  }
685
  
686
  option.encrypted = true;
6,410✔
687
  code = mmWriteFile(pMnode->path, &option);
6,410✔
688
  if (code != 0) {
6,410✔
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;
6,410✔
695
  
696
  mInfo("successfully persisted encrypted flag to mnode.json");
6,410✔
697
  return 0;
6,410✔
698
}
699

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

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

714
  TAOS_RETURN(code);
454,244✔
715
}
716

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

726
  if (!pMnode->deploy) {
454,244✔
727
    code = sdbReadFile(pMnode->pSdb);
127,325✔
728
  }
729

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

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

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

743
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
22,712,200✔
744
  SMnodeStep step = {0};
22,712,200✔
745
  step.name = name;
22,712,200✔
746
  step.initFp = initFp;
22,712,200✔
747
  step.cleanupFp = cleanupFp;
22,712,200✔
748
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
45,424,400✔
749
    TAOS_RETURN(terrno);
×
750
  }
751

752
  TAOS_RETURN(0);
22,712,200✔
753
}
754

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

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

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

818
  for (int32_t s = pos; s >= 0; s--) {
23,163,180✔
819
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,709,000✔
820
    mInfo("%s will cleanup", pStep->name);
22,709,000✔
821
    if (pStep->cleanupFp != NULL) {
22,709,000✔
822
      (*pStep->cleanupFp)(pMnode);
21,800,640✔
823
    }
824
  }
825

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

831
static int32_t mndExecSteps(SMnode *pMnode) {
454,244✔
832
  int32_t code = 0;
454,244✔
833
  int32_t size = taosArrayGetSize(pMnode->pSteps);
454,244✔
834
  for (int32_t pos = 0; pos < size; pos++) {
23,166,444✔
835
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,712,200✔
836
    if (pStep->initFp == NULL) continue;
22,712,200✔
837

838
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
22,712,200✔
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);
22,712,200✔
844
      tmsgReportStartup(pStep->name, "initialized");
22,712,200✔
845
    }
846
  }
847

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

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

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

868
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
454,244✔
869
  if (pMnode == NULL) {
454,244✔
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));
454,244✔
875

876
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
454,244✔
877
  if (code != 0) {
454,244✔
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";
454,244✔
885
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
454,244✔
886
  if (code < 0) {
454,244✔
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,
454,244✔
895
        pOption->numOfTotalReplicas);
896
  mndSetOptions(pMnode, pOption);
454,244✔
897

898
  pMnode->deploy = pOption->deploy;
454,244✔
899
  pMnode->version = pOption->version;
454,244✔
900
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
454,244✔
901
  if (pMnode->pSteps == NULL) {
454,244✔
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);
454,244✔
909
  if (code != 0) {
454,244✔
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);
454,244✔
917
  if (code != 0) {
454,244✔
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);
454,244✔
925
  if (code != 0) {
454,244✔
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");
454,244✔
933
  return pMnode;
454,244✔
934
}
935

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

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

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

980
  return mndInitTimer(pMnode);
454,107✔
981
}
982

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

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

987
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
357,859✔
988

989
int32_t mndIsCatchUp(SMnode *pMnode) {
182,217✔
990
  int64_t rid = pMnode->syncMgmt.sync;
182,217✔
991
  return syncIsCatchUp(rid);
182,217✔
992
}
993

994
ESyncRole mndGetRole(SMnode *pMnode) {
182,217✔
995
  int64_t rid = pMnode->syncMgmt.sync;
182,217✔
996
  return syncGetRole(rid);
182,217✔
997
}
998

999
int64_t mndGetTerm(SMnode *pMnode) {
9,524,130✔
1000
  int64_t rid = pMnode->syncMgmt.sync;
9,524,130✔
1001
  return syncGetTerm(rid);
9,524,130✔
1002
}
1003

1004
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
23,759,342✔
1005

1006
void mndStop(SMnode *pMnode) {
454,107✔
1007
  mndSetStop(pMnode);
454,107✔
1008
  mndSyncStop(pMnode);
454,107✔
1009
  mndCleanupTimer(pMnode);
454,107✔
1010
}
454,107✔
1011

1012
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
57,038,069✔
1013
  SMnode    *pMnode = pMsg->info.node;
57,038,069✔
1014
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
57,038,069✔
1015

1016
  const STraceId *trace = &pMsg->info.traceId;
57,038,069✔
1017
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
57,038,069✔
1018

1019
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
57,038,069✔
1020
  if (code != 0) {
57,038,069✔
1021
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
494✔
1022
            tstrerror(code), code);
1023
  }
1024

1025
  return code;
57,038,069✔
1026
}
1027

1028
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
279,455,791✔
1029
  int32_t code = 0;
279,455,791✔
1030
  if (!IsReq(pMsg)) TAOS_RETURN(code);
279,455,791✔
1031
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
245,284,552✔
1032
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
239,826,757✔
1033
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
235,232,404✔
1034
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
224,430,327✔
1035
    TAOS_RETURN(code);
20,869,288✔
1036
  }
1037

1038
  SMnode *pMnode = pMsg->info.node;
224,424,720✔
1039
  (void)taosThreadRwlockRdlock(&pMnode->lock);
224,431,698✔
1040
  if (pMnode->stopped) {
224,433,274✔
1041
    (void)taosThreadRwlockUnlock(&pMnode->lock);
19,394✔
1042
    code = TSDB_CODE_APP_IS_STOPPING;
19,394✔
1043
    TAOS_RETURN(code);
19,394✔
1044
  }
1045

1046
  terrno = 0;
224,410,765✔
1047
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
224,407,282✔
1048
  if (terrno != 0) {
224,414,946✔
1049
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1050
    code = terrno;
×
1051
    TAOS_RETURN(code);
×
1052
  }
1053

1054
  if (state.state != TAOS_SYNC_STATE_LEADER) {
224,413,061✔
1055
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,483,302✔
1056
    code = TSDB_CODE_SYN_NOT_LEADER;
2,483,302✔
1057
    goto _OVER;
2,483,302✔
1058
  }
1059

1060
  if (!state.restored || !pMnode->restored) {
221,929,759✔
1061
    (void)taosThreadRwlockUnlock(&pMnode->lock);
844,847✔
1062
    code = TSDB_CODE_SYN_RESTORING;
843,575✔
1063
    goto _OVER;
843,575✔
1064
  }
1065

1066
#if 1
1067
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
221,084,912✔
1068
#else
1069
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1070
  mTrace("mnode rpc is acquired, ref:%d", ref);
1071
#endif
1072

1073
  (void)taosThreadRwlockUnlock(&pMnode->lock);
221,087,293✔
1074
  TAOS_RETURN(code);
221,084,637✔
1075

1076
_OVER:
3,326,877✔
1077
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,326,877✔
1078
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,326,115✔
1079
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,326,115✔
1080
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,326,115✔
1081
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,326,115✔
1082
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,326,877✔
1083
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,326,828✔
1084
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,325,938✔
1085
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,326,877✔
1086
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
1,755✔
1087
           pMnode->stopped, state.restored, syncStr(state.state));
1088
    TAOS_RETURN(code);
1,755✔
1089
  }
1090

1091
  const STraceId *trace = &pMsg->info.traceId;
3,325,865✔
1092
  SEpSet          epSet = {0};
3,326,532✔
1093
  mndGetMnodeEpSet(pMnode, &epSet);
3,325,889✔
1094

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

1101
  if (epSet.numOfEps <= 0) return -1;
3,326,877✔
1102

1103
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
11,466,365✔
1104
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
8,139,566✔
1105
  }
1106

1107
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,326,799✔
1108
  pMsg->info.rsp = rpcMallocCont(contLen);
3,326,405✔
1109
  if (pMsg->info.rsp != NULL) {
3,326,877✔
1110
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,326,854✔
1111
      mError("failed to serialize ep set");
×
1112
    }
1113
    pMsg->info.hasEpSet = 1;
3,325,523✔
1114
    pMsg->info.rspLen = contLen;
3,326,335✔
1115
  }
1116

1117
  TAOS_RETURN(code);
3,325,681✔
1118
}
1119

1120
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
279,470,445✔
1121
  SMnode         *pMnode = pMsg->info.node;
279,470,445✔
1122
  const STraceId *trace = &pMsg->info.traceId;
279,470,777✔
1123
  int32_t         code = TSDB_CODE_SUCCESS;
279,470,907✔
1124

1125
#ifdef TD_ENTERPRISE
1126
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
279,470,907✔
1127
    SCachedTokenInfo ti = {0};
18,798✔
1128
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
18,798✔
1129
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,813✔
1130
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,813✔
1131
      TAOS_RETURN(code);
1,813✔
1132
    }
1133
    if (ti.enabled == 0) {
16,985✔
1134
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,966✔
1135
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,966✔
1136
      TAOS_RETURN(code);
2,966✔
1137
    }
1138
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
14,019✔
1139
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1140
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1141
      TAOS_RETURN(code);
×
1142
    }
1143
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
14,019✔
1144
  }
1145
#endif
1146

1147
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
279,450,707✔
1148
  MndMsgFpExt fpExt = NULL;
279,464,936✔
1149
  if (fp == NULL) {
279,464,936✔
1150
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,861,561✔
1151
    if (fpExt == NULL) {
20,861,561✔
1152
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1153
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1154
      TAOS_RETURN(code);
×
1155
    }
1156
  }
1157

1158
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
279,464,936✔
1159

1160
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
276,118,307✔
1161
  if (fp)
276,113,489✔
1162
    code = (*fp)(pMsg);
255,251,928✔
1163
  else
1164
    code = (*fpExt)(pMsg, pQueueInfo);
20,861,561✔
1165
  mndReleaseRpc(pMnode);
276,120,528✔
1166

1167
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
276,122,579✔
1168
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
32,965,000✔
1169
  } else if (code == 0) {
243,157,579✔
1170
    mGTrace("msg:%p, successfully processed", pMsg);
240,328,840✔
1171
  } else {
1172
    // TODO removve this wrong set code
1173
    if (code == -1) {
2,828,739✔
1174
      code = terrno;
×
1175
    }
1176
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,828,739✔
1177
            TMSG_INFO(pMsg->msgType));
1178
  }
1179

1180
  TAOS_RETURN(code);
276,122,579✔
1181
}
1182

1183
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
108,564,316✔
1184
  tmsg_t type = TMSG_INDEX(msgType);
108,564,316✔
1185
  if (type < TDMT_MAX) {
108,564,316✔
1186
    pMnode->msgFp[type] = fp;
108,564,316✔
1187
  }
1188
}
108,564,316✔
1189

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

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

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

1219
  SSdb   *pSdb = pMnode->pSdb;
59✔
1220
  int64_t ms = taosGetTimestampMs();
59✔
1221

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

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

1243
  void *pIter = NULL;
59✔
1244
  while (1) {
59✔
1245
    SDnodeObj *pObj = NULL;
118✔
1246
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
118✔
1247
    if (pIter == NULL) break;
118✔
1248

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

1263
  pIter = NULL;
59✔
1264
  while (1) {
59✔
1265
    SMnodeObj *pObj = NULL;
118✔
1266
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
118✔
1267
    if (pIter == NULL) break;
118✔
1268

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

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

1291
  // vgroup info
1292
  pIter = NULL;
59✔
1293
  while (1) {
118✔
1294
    SVgObj *pVgroup = NULL;
177✔
1295
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
177✔
1296
    if (pIter == NULL) break;
177✔
1297

1298
    if (pVgroup->mountVgId) {
118✔
1299
      sdbRelease(pSdb, pVgroup);
×
1300
      continue;
×
1301
    }
1302

1303
    pClusterInfo->vgroups_total++;
118✔
1304
    pClusterInfo->tbs_total += pVgroup->numOfTables;
118✔
1305

1306
    SMonVgroupDesc desc = {0};
118✔
1307
    desc.vgroup_id = pVgroup->vgId;
118✔
1308

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

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

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

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

UNCOV
1351
    SMonStbDesc desc = {0};
×
1352

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

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

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

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

1385
  mndReleaseRpc(pMnode);
59✔
1386
  TAOS_RETURN(code);
59✔
1387
}
1388

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

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

1405
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
9,421,210✔
1406
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
9,421,210✔
1407
  return state.roleTimeMs;
9,421,210✔
1408
}
1409

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

1428
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1429

1430
void mndSetStop(SMnode *pMnode) {
454,107✔
1431
  (void)taosThreadRwlockWrlock(&pMnode->lock);
454,107✔
1432
  pMnode->stopped = true;
454,107✔
1433
  (void)taosThreadRwlockUnlock(&pMnode->lock);
454,107✔
1434
  mInfo("mnode set stopped");
454,107✔
1435
}
454,107✔
1436

1437
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
598,965,314✔
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