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

taosdata / TDengine / #5042

29 Apr 2026 11:44AM UTC coverage: 73.17% (+0.06%) from 73.109%
#5042

push

travis-ci

web-flow
feat(statewindow): support multi columns (#35136)

1557 of 1828 new or added lines in 18 files covered. (85.18%)

7203 existing lines in 131 files now uncovered.

277560 of 379338 relevant lines covered (73.17%)

137140549.71 hits per line

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

81.92
/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 "mndSecurityPolicy.h"
48
#include "mndShow.h"
49
#include "mndSma.h"
50
#include "mndSnode.h"
51
#include "mndSsMigrate.h"
52
#include "mndStb.h"
53
#include "mndStream.h"
54
#include "mndSubscribe.h"
55
#include "mndSync.h"
56
#include "mndTelem.h"
57
#include "mndToken.h"
58
#include "mndTopic.h"
59
#include "mndTrans.h"
60
#include "mndUser.h"
61
#include "mndVgroup.h"
62
#include "mndView.h"
63
#include "mndXnode.h"
64
#include "tencrypt.h"
65

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

85
static inline void mndReleaseRpc(SMnode *pMnode) {
299,732,179✔
86
  (void)taosThreadRwlockRdlock(&pMnode->lock);
299,732,179✔
87
#if 1
88
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
299,732,287✔
89
#else
90
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
91
  mTrace("mnode rpc is released, ref:%d", ref);
92
#endif
93
  (void)taosThreadRwlockUnlock(&pMnode->lock);
299,729,667✔
94
}
299,732,764✔
95

96
static void *mndBuildTimerMsg(int32_t *pContLen) {
70,444,564✔
97
  terrno = 0;
70,444,564✔
98
  SMTimerReq timerReq = {0};
70,444,564✔
99

100
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
70,444,564✔
101
  if (contLen <= 0) return NULL;
70,444,564✔
102
  void *pReq = rpcMallocCont(contLen);
70,444,564✔
103
  if (pReq == NULL) return NULL;
70,444,440✔
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

306
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
106,268✔
307
  SSdb *pSdb = pMnode->pSdb;
106,268✔
308

309
  void *pIter = NULL;
106,268✔
310
  while (1) {
391,072✔
311
    SVgObj *pVgroup = NULL;
497,340✔
312
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
497,340✔
313
    if (pIter == NULL) break;
497,340✔
314

315
    bool stateChanged = false;
391,072✔
316
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,013,736✔
317
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
798,278✔
318
      if (pGid->dnodeId == dnodeId) {
798,278✔
319
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
175,614✔
320
          mInfo(
57,153✔
321
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
322
              "restored:0 "
323
              "canRead:0",
324
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
325
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
57,153✔
326
          pGid->syncRestore = 0;
57,153✔
327
          pGid->syncCanRead = 0;
57,153✔
328
          pGid->startTimeMs = 0;
57,153✔
329
          pGid->learnerProgress = 0;
57,153✔
330
          pGid->snapSeq = -1;
57,153✔
331
          stateChanged = true;
57,153✔
332
        }
333
        break;
175,614✔
334
      }
335
    }
336

337
    if (stateChanged) {
391,072✔
338
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
57,153✔
339
      if (pDb != NULL && pDb->stateTs != curMs) {
57,153✔
340
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
30,662✔
341
              curMs);
342
        pDb->stateTs = curMs;
30,662✔
343
      }
344
      mndReleaseDb(pMnode, pDb);
57,153✔
345
    }
346

347
    sdbRelease(pSdb, pVgroup);
391,072✔
348
  }
349
}
106,268✔
350

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

355
  SSdb   *pSdb = pMnode->pSdb;
5,327,843✔
356
  int64_t curMs = taosGetTimestampMs();
5,327,843✔
357

358
  void *pIter = NULL;
5,327,843✔
359
  while (1) {
9,154,630✔
360
    SDnodeObj *pDnode = NULL;
14,482,473✔
361
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
14,482,473✔
362
    if (pIter == NULL) break;
14,482,473✔
363

364
    bool online = mndIsDnodeOnline(pDnode, curMs);
9,154,630✔
365
    if (!online) {
9,154,630✔
366
      mInfo("dnode:%d, in offline state", pDnode->id);
106,268✔
367
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
106,268✔
368
    }
369

370
    sdbRelease(pSdb, pDnode);
9,154,630✔
371
  }
372

373
  mndReleaseRpc(pMnode);
5,327,843✔
374
}
375

376
static bool mnodeIsNotLeader(SMnode *pMnode) {
316,759,018✔
377
  terrno = 0;
316,759,018✔
378
  (void)taosThreadRwlockRdlock(&pMnode->lock);
316,759,018✔
379
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
316,758,366✔
380
  if (terrno != 0) {
316,759,018✔
381
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
382
    return true;
×
383
  }
384

385
  if (state.state != TAOS_SYNC_STATE_LEADER) {
316,757,278✔
386
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,744,591✔
387
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,744,591✔
388
    return true;
10,744,591✔
389
  }
390
  if (!state.restored || !pMnode->restored) {
306,012,687✔
391
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,904✔
392
    terrno = TSDB_CODE_SYN_RESTORING;
3,173✔
393
    return true;
3,173✔
394
  }
395
  (void)taosThreadRwlockUnlock(&pMnode->lock);
306,007,950✔
396
  return false;
306,010,302✔
397
}
398

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

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

413
  return min <= 1 ? 2 : min;
×
414
}
415
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
28,311,582✔
416
  int32_t code = 0;
28,311,582✔
417
#ifndef TD_ASTRA
418
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
28,311,582✔
419
    mndPullupGrant(pMnode);
1,736,595✔
420
  }
421
  if (sec % tsTtlPushIntervalSec == 0) {
28,311,582✔
422
    mndPullupTtl(pMnode);
2,969,174✔
423
  }
424

425
  if (sec % tsTrimVDbIntervalSec == 0) {
28,311,582✔
426
    mndPullupTrimDb(pMnode);
15,257✔
427
  }
428

429
  if (sec % tsQueryTrimIntervalSec == 0) {
28,311,582✔
430
    mndPullupQueryTrimDb(pMnode);
3,049,150✔
431
  }
432
#endif
433
#ifdef USE_SHARED_STORAGE
434
  if (tsSsEnabled) {
28,311,582✔
435
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
234,612✔
436
      mndPullupUpdateSsMigrateProgress(pMnode);
166,782✔
437
    }
438
    if (tsSsEnabled == 2) {
234,612✔
439
      // By default, both tsTrimVDbIntervalSec and tsSsAutoMigrateIntervalSec are 3600 seconds,
440
      // so, delay half interval to do ss migrate to avoid conflict.
441
      //
442
      // NOTE: this solution is not perfect, there could still be conflict if user changes the
443
      // default value, but it is good enough as user is unlikely to change the default value.
444
      // The best solution is adding a new offset config to all cron tasks, but that would add
445
      // extra complexity.
446
      if ((sec % tsSsAutoMigrateIntervalSec) == (tsSsAutoMigrateIntervalSec / 2)) {
×
447
        mndPullupSsMigrateDb(pMnode);
×
448
      }
449
    }
450
  }
451
#endif
452
#ifdef TD_ENTERPRISE
453
  if (tsAuthReq) {
28,311,582✔
454
    if (sec % tsAuthReqHBInterval == 0) {
×
455
      mndPullupAuth(pMnode);
×
456
    }
457
  }
458
#endif
459
  if (sec % tsTransPullupInterval == 0) {
28,311,582✔
460
    mndPullupTrans(pMnode);
14,155,141✔
461
  }
462

463
  if (sec % tsCompactPullupInterval == 0) {
28,311,582✔
464
    mndPullupCompacts(pMnode);
2,840,872✔
465
  }
466

467
  if (sec % tsScanPullupInterval == 0) {
28,311,582✔
468
    mndPullupScans(pMnode);
2,840,872✔
469
  }
470
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
28,311,582✔
471
    mndPullupInstances(pMnode);
5,676,649✔
472
  }
473
#ifdef USE_TOPIC
474
  if (sec % tsMqRebalanceInterval == 0) {
28,311,582✔
475
    mndCalMqRebalance(pMnode);
14,155,011✔
476
  }
477
#endif
478
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
28,311,582✔
479
    mndPullupTelem(pMnode);
189✔
480
  }
481
  if (sec % tsUptimeInterval == 0) {
28,311,582✔
482
    mndIncreaseUpTime(pMnode);
109,246✔
483
  }
484
}
28,311,582✔
485

486
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
277,699,672✔
487
  int32_t code = 0;
277,699,672✔
488
#ifndef TD_ASTRA
489
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
277,699,672✔
490
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
13,668,246✔
491
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
492
    }
493
  }
494

495
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
277,699,672✔
496
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
9,061,380✔
497
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
498
    }
499
  }
500
#endif
501
}
277,699,672✔
502

503
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
277,699,672✔
504
  if (ms % (tsStatusTimeoutMs) == 0) {
277,699,672✔
505
    mndCheckDnodeOffline(pMnode);
5,327,843✔
506
  }
507
}
277,699,672✔
508

509
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
28,311,582✔
510
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
28,311,582✔
511
    mndSyncCheckTimeout(pMnode);
944,438✔
512
  }
513
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
28,311,582✔
514
    msmHealthCheck(pMnode);
9,440,807✔
515
  }
516
}
28,311,582✔
517

518
static void *mndThreadSecFp(void *param) {
487,107✔
519
  SMnode *pMnode = param;
487,107✔
520
  int64_t lastSec = 0;
487,107✔
521
  setThreadName("mnode-timer");
487,107✔
522

523
  while (1) {
287,891,125✔
524
    if (mndGetStop(pMnode)) break;
288,378,232✔
525

526
    int64_t nowSec = taosGetTimestampMs() / 1000;
287,891,125✔
527
    if (nowSec == lastSec) {
287,891,125✔
528
      taosMsleep(100);
258,556,601✔
529
      continue;
258,556,601✔
530
    }
531
    lastSec = nowSec;
29,334,524✔
532

533
    if (mnodeIsNotLeader(pMnode)) {
29,334,524✔
534
      taosMsleep(100);
1,022,942✔
535
      mTrace("timer not process since mnode is not leader");
1,022,942✔
536
      continue;
1,022,942✔
537
    }
538

539
    mndDoTimerCheckSync(pMnode, nowSec);
28,311,582✔
540

541
    mndDoTimerPullupTask(pMnode, nowSec);
28,311,582✔
542

543
    taosMsleep(100);
28,311,582✔
544
  }
545

546
  return NULL;
487,107✔
547
}
548

549
static void *mndThreadMsFp(void *param) {
487,107✔
550
  SMnode *pMnode = param;
487,107✔
551
  int64_t lastTime = 0;
487,107✔
552
  setThreadName("mnode-arb-timer");
487,107✔
553

554
  while (1) {
555
    lastTime += 100;
287,911,601✔
556
    taosMsleep(100);
287,911,601✔
557

558
    if (mndGetStop(pMnode)) break;
287,911,601✔
559
    if (lastTime % 10 != 0) continue;
287,424,494✔
560

561
    if (mnodeIsNotLeader(pMnode)) {
287,424,494✔
562
      mTrace("timer not process since mnode is not leader");
9,724,822✔
563
      continue;
9,724,822✔
564
    }
565

566
    mndDoTimerCheckStatus(pMnode, lastTime);
277,699,672✔
567

568
    mndDoArbTimerPullupTask(pMnode, lastTime);
277,699,672✔
569
  }
570

571
  return NULL;
487,107✔
572
}
573

574
static int32_t mndInitTimer(SMnode *pMnode) {
487,107✔
575
  int32_t      code = 0;
487,107✔
576
  TdThreadAttr thAttr;
486,146✔
577
  (void)taosThreadAttrInit(&thAttr);
487,107✔
578
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
487,107✔
579
#ifdef TD_COMPACT_OS
580
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
581
#endif
582
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
487,107✔
583
    mError("failed to create timer thread since %s", tstrerror(code));
×
584
    TAOS_RETURN(code);
×
585
  }
586

587
  (void)taosThreadAttrDestroy(&thAttr);
487,107✔
588
  tmsgReportStartup("mnode-timer", "initialized");
487,107✔
589

590
  TdThreadAttr arbAttr;
486,146✔
591
  (void)taosThreadAttrInit(&arbAttr);
487,107✔
592
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
487,107✔
593
#ifdef TD_COMPACT_OS
594
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
595
#endif
596
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
487,107✔
597
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
598
    TAOS_RETURN(code);
×
599
  }
600

601
  (void)taosThreadAttrDestroy(&arbAttr);
487,107✔
602
  tmsgReportStartup("mnode-timer", "initialized");
487,107✔
603
  TAOS_RETURN(code);
487,107✔
604
}
605

606
static void mndCleanupTimer(SMnode *pMnode) {
487,107✔
607
  if (taosCheckPthreadValid(pMnode->thread)) {
487,107✔
608
    (void)taosThreadJoin(pMnode->thread, NULL);
487,107✔
609
    taosThreadClear(&pMnode->thread);
487,107✔
610
  }
611
  if (taosCheckPthreadValid(pMnode->arbThread)) {
487,107✔
612
    (void)taosThreadJoin(pMnode->arbThread, NULL);
487,107✔
613
    taosThreadClear(&pMnode->arbThread);
487,107✔
614
  }
615
}
487,107✔
616

617
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
487,207✔
618
  int32_t code = 0;
487,207✔
619
  pMnode->path = taosStrdup(path);
487,207✔
620
  if (pMnode->path == NULL) {
487,207✔
621
    code = terrno;
×
622
    TAOS_RETURN(code);
×
623
  }
624

625
  if (taosMkDir(pMnode->path) != 0) {
487,207✔
626
    code = terrno;
×
627
    TAOS_RETURN(code);
×
628
  }
629

630
  TAOS_RETURN(code);
487,207✔
631
}
632

633
static int32_t mndInitWal(SMnode *pMnode) {
487,207✔
634
  int32_t code = 0;
487,207✔
635
  char    path[PATH_MAX + 20] = {0};
487,207✔
636
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
487,207✔
637
  SWalCfg cfg = {.vgId = 1,
487,207✔
638
                 .fsyncPeriod = 0,
639
                 .rollPeriod = -1,
640
                 .segSize = -1,
641
                 .committed = -1,
642
                 .retentionPeriod = 0,
643
                 .retentionSize = 0,
644
                 .level = TAOS_WAL_FSYNC,
645
                 .encryptAlgr = 0,
646
                 .encryptData = {0}};
647

648
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
649
  if (taosWaitCfgKeyLoaded() != 0) {
487,207✔
650
    code = terrno;
×
651
    TAOS_RETURN(code);
×
652
  }
653
  if (tsMetaKey[0] != '\0') {
487,207✔
654
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
5,266✔
655
  }
656
#endif
657

658
  pMnode->pWal = walOpen(path, &cfg);
487,207✔
659
  if (pMnode->pWal == NULL) {
487,207✔
660
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
661
    if (terrno != 0) code = terrno;
×
662
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
663
    TAOS_RETURN(code);
×
664
  }
665

666
  TAOS_RETURN(code);
487,207✔
667
}
668

669
static void mndCloseWal(SMnode *pMnode) {
487,176✔
670
  if (pMnode->pWal != NULL) {
487,176✔
671
    walClose(pMnode->pWal);
487,176✔
672
    pMnode->pWal = NULL;
487,176✔
673
  }
674
}
487,176✔
675

676
// Forward declarations for mmFile.c functions
677
extern int32_t mmReadFile(const char *path, SMnodeOpt *pOption);
678
extern int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption);
679

680
// Callback function to persist encrypted flag to mnode.json
681
static int32_t mndPersistEncryptedFlag(void *param) {
7,339✔
682
  SMnode *pMnode = (SMnode *)param;
7,339✔
683
  if (pMnode == NULL) {
7,339✔
684
    return TSDB_CODE_INVALID_PARA;
×
685
  }
686
  
687
  mInfo("persisting encrypted flag to mnode.json");
7,339✔
688
  
689
  SMnodeOpt option = {0};
7,339✔
690
  int32_t code = mmReadFile(pMnode->path, &option);
7,339✔
691
  if (code != 0) {
7,339✔
692
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
693
    return code;
×
694
  }
695
  
696
  option.encrypted = true;
7,339✔
697
  code = mmWriteFile(pMnode->path, &option);
7,339✔
698
  if (code != 0) {
7,339✔
699
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
700
    return code;
×
701
  }
702
  
703
  // Also update mnode's encrypted flag
704
  pMnode->encrypted = true;
7,339✔
705
  
706
  mInfo("successfully persisted encrypted flag to mnode.json");
7,339✔
707
  return 0;
7,339✔
708
}
709

710
static int32_t mndInitSdb(SMnode *pMnode) {
487,207✔
711
  int32_t code = 0;
487,207✔
712
  SSdbOpt opt = {0};
487,207✔
713
  opt.path = pMnode->path;
487,207✔
714
  opt.pMnode = pMnode;
487,207✔
715
  opt.pWal = pMnode->pWal;
487,207✔
716

717
  pMnode->pSdb = sdbInit(&opt);
487,207✔
718
  if (pMnode->pSdb == NULL) {
487,207✔
719
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
720
    if (terrno != 0) code = terrno;
×
721
    TAOS_RETURN(code);
×
722
  }
723

724
  TAOS_RETURN(code);
487,207✔
725
}
726

727
static int32_t mndOpenSdb(SMnode *pMnode) {
487,207✔
728
  int32_t code = 0;
487,207✔
729
  
730
  pMnode->pSdb->encrypted = pMnode->encrypted;
487,207✔
731
  
732
  // Set callback for persisting encrypted flag
733
  pMnode->pSdb->persistEncryptedFlagFp = mndPersistEncryptedFlag;
487,207✔
734
  pMnode->pSdb->pMnodeForCallback = pMnode;
487,207✔
735

736
  if (!pMnode->deploy) {
487,207✔
737
    code = sdbReadFile(pMnode->pSdb);
134,087✔
738
  }
739

740
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
487,207✔
741

742
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
487,207✔
743
  return code;
487,207✔
744
}
745

746
static void mndCleanupSdb(SMnode *pMnode) {
487,176✔
747
  if (pMnode->pSdb) {
487,176✔
748
    sdbCleanup(pMnode->pSdb);
487,176✔
749
    pMnode->pSdb = NULL;
487,176✔
750
  }
751
}
487,176✔
752

753
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
24,847,557✔
754
  SMnodeStep step = {0};
24,847,557✔
755
  step.name = name;
24,847,557✔
756
  step.initFp = initFp;
24,847,557✔
757
  step.cleanupFp = cleanupFp;
24,847,557✔
758
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
49,695,114✔
759
    TAOS_RETURN(terrno);
×
760
  }
761

762
  TAOS_RETURN(0);
24,847,557✔
763
}
764

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

822
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
487,176✔
823
  if (pMnode->pSteps == NULL) return;
487,176✔
824

825
  if (pos == -1) {
487,176✔
826
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
487,176✔
827
  }
828

829
  for (int32_t s = pos; s >= 0; s--) {
25,333,152✔
830
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
24,845,976✔
831
    mInfo("%s will cleanup", pStep->name);
24,845,976✔
832
    if (pStep->cleanupFp != NULL) {
24,845,976✔
833
      (*pStep->cleanupFp)(pMnode);
23,871,624✔
834
    }
835
  }
836

837
  taosArrayClear(pMnode->pSteps);
487,176✔
838
  taosArrayDestroy(pMnode->pSteps);
487,176✔
839
  pMnode->pSteps = NULL;
487,176✔
840
}
841

842
static int32_t mndExecSteps(SMnode *pMnode) {
487,207✔
843
  int32_t code = 0;
487,207✔
844
  int32_t size = taosArrayGetSize(pMnode->pSteps);
487,207✔
845
  for (int32_t pos = 0; pos < size; pos++) {
25,334,764✔
846
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
24,847,557✔
847
    if (pStep->initFp == NULL) continue;
24,847,557✔
848

849
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
24,847,557✔
850
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
851
      mndCleanupSteps(pMnode, pos);
×
852
      TAOS_RETURN(code);
×
853
    } else {
854
      mInfo("%s is initialized", pStep->name);
24,847,557✔
855
      tmsgReportStartup(pStep->name, "initialized");
24,847,557✔
856
    }
857
  }
858

859
  pMnode->clusterId = mndGetClusterId(pMnode);
487,207✔
860
  TAOS_RETURN(0);
487,207✔
861
}
862

863
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
487,207✔
864
  pMnode->msgCb = pOption->msgCb;
487,207✔
865
  pMnode->selfDnodeId = pOption->dnodeId;
487,207✔
866
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
487,207✔
867
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
487,207✔
868
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
487,207✔
869
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
487,207✔
870
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
487,207✔
871
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
487,207✔
872
  pMnode->encrypted = pOption->encrypted;
487,207✔
873
}
487,207✔
874

875
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
487,207✔
876
  terrno = 0;
487,207✔
877
  mInfo("start to open mnode in %s", path);
487,207✔
878

879
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
487,207✔
880
  if (pMnode == NULL) {
487,207✔
881
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
882
    mError("failed to open mnode in step 1, since %s", terrstr());
×
883
    return NULL;
×
884
  }
885
  (void)memset(pMnode, 0, sizeof(SMnode));
487,207✔
886

887
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
487,207✔
888
  if (code != 0) {
487,207✔
889
    taosMemoryFree(pMnode);
×
890
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
891
    terrno = code;
×
892
    return NULL;
×
893
  }
894

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

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

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

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

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

933
  mInfo("mnode open successfully");
487,207✔
934
  return pMnode;
487,207✔
935
}
936

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

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

963
int32_t mndStart(SMnode *pMnode) {
487,107✔
964
  int32_t code = 0;
487,107✔
965
  mndSyncStart(pMnode);
487,107✔
966
  if (pMnode->deploy) {
487,107✔
967
    if (sdbDeploy(pMnode->pSdb) != 0) {
353,120✔
968
      mError("failed to deploy sdb while start mnode");
×
969
      return -1;
×
970
    }
971
    mndSetRestored(pMnode, true);
353,120✔
972
  }
973
  if (mndIsLeader(pMnode)) {
487,107✔
974
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
442,040✔
975
      mError("failed to upgrade sdb while start mnode");
×
976
      return -1;
×
977
    }
978
#ifdef TD_ENTERPRISE
979
    if (tsSodEnforceMode) {
442,040✔
980
      if ((code = mndProcessEnforceSod(pMnode)) != 0) {
×
981
        if (code == TSDB_CODE_MND_ROLE_NO_VALID_SYSDBA || code == TSDB_CODE_MND_ROLE_NO_VALID_SYSSEC ||
×
982
            code == TSDB_CODE_MND_ROLE_NO_VALID_SYSAUDIT) {
983
          mInfo("enter SoD pending mode. Enforce SoD by command line failed since %s", tstrerror(code));
×
984
        } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
985
          mInfo("enter SoD pending mode. Enforce SoD is in progress");
×
986
        } else {
987
          mError("failed to enforce SoD by command line since %s", tstrerror(code));
×
988
          TAOS_RETURN(code);
×
989
        }
990
      } else {
991
        mndSetSoDPhase(pMnode, TSDB_SOD_PHASE_STABLE);
×
992
      }
993
    }
994
#endif
995
  }
996
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
487,107✔
997
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
487,107✔
998

999
  return mndInitTimer(pMnode);
487,107✔
1000
}
1001

1002
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
487,107✔
1003

1004
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
385,312✔
1005

1006
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
385,312✔
1007

1008
int32_t mndIsCatchUp(SMnode *pMnode) {
210,096✔
1009
  int64_t rid = pMnode->syncMgmt.sync;
210,096✔
1010
  return syncIsCatchUp(rid);
210,096✔
1011
}
1012

1013
ESyncRole mndGetRole(SMnode *pMnode) {
210,096✔
1014
  int64_t rid = pMnode->syncMgmt.sync;
210,096✔
1015
  return syncGetRole(rid);
210,096✔
1016
}
1017

1018
int64_t mndGetTerm(SMnode *pMnode) {
9,184,347✔
1019
  int64_t rid = pMnode->syncMgmt.sync;
9,184,347✔
1020
  return syncGetTerm(rid);
9,184,347✔
1021
}
1022

1023
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
22,854,152✔
1024

1025
void mndStop(SMnode *pMnode) {
487,107✔
1026
  mndSetStop(pMnode);
487,107✔
1027
  mndSyncStop(pMnode);
487,107✔
1028
  mndCleanupTimer(pMnode);
487,107✔
1029
}
487,107✔
1030

1031
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
61,642,773✔
1032
  SMnode    *pMnode = pMsg->info.node;
61,642,773✔
1033
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
61,642,773✔
1034

1035
  const STraceId *trace = &pMsg->info.traceId;
61,642,773✔
1036
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
61,642,773✔
1037

1038
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
61,642,773✔
1039
  if (code != 0) {
61,642,773✔
1040
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
828✔
1041
            tstrerror(code), code);
1042
  }
1043

1044
  return code;
61,642,773✔
1045
}
1046

1047
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
296,457,278✔
1048
  int32_t code = 0;
296,457,278✔
1049
  if (!IsReq(pMsg)) TAOS_RETURN(code);
296,457,278✔
1050
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
259,441,843✔
1051
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
253,091,904✔
1052
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
247,647,552✔
1053
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
235,107,367✔
1054
    TAOS_RETURN(code);
24,342,223✔
1055
  }
1056

1057
  SMnode *pMnode = pMsg->info.node;
235,103,129✔
1058
  (void)taosThreadRwlockRdlock(&pMnode->lock);
235,105,090✔
1059
  if (pMnode->stopped) {
235,111,551✔
1060
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,359✔
1061
    code = TSDB_CODE_APP_IS_STOPPING;
2,359✔
1062
    TAOS_RETURN(code);
2,359✔
1063
  }
1064

1065
  terrno = 0;
235,105,041✔
1066
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
235,104,406✔
1067
  if (terrno != 0) {
235,110,967✔
1068
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1069
    code = terrno;
×
1070
    TAOS_RETURN(code);
×
1071
  }
1072

1073
  if (state.state != TAOS_SYNC_STATE_LEADER) {
235,110,172✔
1074
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,848,998✔
1075
    code = TSDB_CODE_SYN_NOT_LEADER;
1,848,998✔
1076
    goto _OVER;
1,848,998✔
1077
  }
1078

1079
  if (!state.restored || !pMnode->restored) {
233,261,174✔
1080
    (void)taosThreadRwlockUnlock(&pMnode->lock);
209,524✔
1081
    code = TSDB_CODE_SYN_RESTORING;
208,812✔
1082
    goto _OVER;
208,812✔
1083
  }
1084

1085
#if 1
1086
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
233,051,694✔
1087
#else
1088
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1089
  mTrace("mnode rpc is acquired, ref:%d", ref);
1090
#endif
1091

1092
  (void)taosThreadRwlockUnlock(&pMnode->lock);
233,051,997✔
1093
  TAOS_RETURN(code);
233,050,896✔
1094

1095
_OVER:
2,057,810✔
1096
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,057,810✔
1097
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,057,578✔
1098
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,057,276✔
1099
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,057,200✔
1100
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,057,200✔
1101
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,057,122✔
1102
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,057,276✔
1103
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,057,354✔
1104
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,057,656✔
UNCOV
1105
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1106
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1107
    TAOS_RETURN(code);
×
1108
  }
1109

1110
  const STraceId *trace = &pMsg->info.traceId;
2,057,031✔
1111
  SEpSet          epSet = {0};
2,057,124✔
1112
  mndGetMnodeEpSet(pMnode, &epSet);
2,057,487✔
1113

1114
  mGDebug(
2,057,810✔
1115
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1116
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1117
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1118
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1119

1120
  if (epSet.numOfEps <= 0) return -1;
2,057,810✔
1121

1122
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
7,603,051✔
1123
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
5,545,241✔
1124
  }
1125

1126
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,057,810✔
1127
  pMsg->info.rsp = rpcMallocCont(contLen);
2,057,213✔
1128
  if (pMsg->info.rsp != NULL) {
2,057,720✔
1129
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,057,732✔
1130
      mError("failed to serialize ep set");
×
1131
    }
1132
    pMsg->info.hasEpSet = 1;
2,056,679✔
1133
    pMsg->info.rspLen = contLen;
2,056,415✔
1134
  }
1135

1136
  TAOS_RETURN(code);
2,056,909✔
1137
}
1138

1139
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
296,470,291✔
1140
  SMnode         *pMnode = pMsg->info.node;
296,470,291✔
1141
  const STraceId *trace = &pMsg->info.traceId;
296,470,785✔
1142
  int32_t         code = TSDB_CODE_SUCCESS;
296,469,139✔
1143

1144
#ifdef TD_ENTERPRISE
1145
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
296,469,139✔
1146
    SCachedTokenInfo ti = {0};
19,721✔
1147
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
19,721✔
1148
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,270✔
1149
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
2,270✔
1150
      TAOS_RETURN(code);
2,270✔
1151
    }
1152
    if (ti.enabled == 0) {
17,451✔
1153
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,795✔
1154
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,795✔
1155
      TAOS_RETURN(code);
2,795✔
1156
    }
1157
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
14,656✔
1158
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1159
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1160
      TAOS_RETURN(code);
×
1161
    }
1162
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
14,656✔
1163
  }
1164
#endif
1165

1166
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
296,460,026✔
1167
  MndMsgFpExt fpExt = NULL;
296,464,260✔
1168
  if (fp == NULL) {
296,464,260✔
1169
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
24,337,222✔
1170
    if (fpExt == NULL) {
24,337,222✔
1171
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1172
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1173
      TAOS_RETURN(code);
×
1174
    }
1175
  }
1176

1177
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
296,464,260✔
1178

1179
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
294,404,147✔
1180
  if (fp)
294,403,713✔
1181
    code = (*fp)(pMsg);
270,066,491✔
1182
  else
1183
    code = (*fpExt)(pMsg, pQueueInfo);
24,337,222✔
1184
  mndReleaseRpc(pMnode);
294,402,246✔
1185

1186
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
294,405,637✔
1187
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
37,464,162✔
1188
  } else if (code == 0) {
256,941,475✔
1189
    mGTrace("msg:%p, successfully processed", pMsg);
245,511,869✔
1190
  } else {
1191
    // TODO removve this wrong set code
1192
    if (code == -1) {
11,429,606✔
1193
      code = terrno;
×
1194
    }
1195
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
11,429,606✔
1196
            TMSG_INFO(pMsg->msgType));
1197
  }
1198

1199
  TAOS_RETURN(code);
294,405,957✔
1200
}
1201

1202
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
117,416,887✔
1203
  tmsg_t type = TMSG_INDEX(msgType);
117,416,887✔
1204
  if (type < TDMT_MAX) {
117,416,887✔
1205
    pMnode->msgFp[type] = fp;
117,416,887✔
1206
  }
1207
}
117,416,887✔
1208

1209
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,897,656✔
1210
  tmsg_t type = TMSG_INDEX(msgType);
3,897,656✔
1211
  if (type < TDMT_MAX) {
3,897,656✔
1212
    pMnode->msgFpExt[type] = fp;
3,897,656✔
1213
  }
1214
}
3,897,656✔
1215

1216
// Note: uid 0 is reserved
1217
int64_t mndGenerateUid(const char *name, int32_t len) {
12,508,441✔
1218
  int32_t hashval = MurmurHash3_32(name, len);
12,508,441✔
1219
  do {
×
1220
    int64_t us = taosGetTimestampUs();
12,508,495✔
1221
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
12,508,495✔
1222
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
12,508,495✔
1223
    if (uuid) {
12,508,495✔
1224
      return llabs(uuid);
12,508,495✔
1225
    }
1226
  } while (true);
1227
}
1228

1229
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
63✔
1230
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1231
  int32_t code = mndAcquireRpc(pMnode);
63✔
1232
  if (code < 0) {
63✔
1233
    TAOS_RETURN(code);
×
1234
  } else if (code == 1) {
63✔
1235
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1236
  }
1237

1238
  SSdb   *pSdb = pMnode->pSdb;
63✔
1239
  int64_t ms = taosGetTimestampMs();
63✔
1240

1241
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
63✔
1242
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
63✔
1243
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
63✔
1244
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
63✔
1245
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
63✔
1246
      pStbInfo->stbs == NULL) {
63✔
1247
    mndReleaseRpc(pMnode);
×
1248
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1249
    if (terrno != 0) code = terrno;
×
1250
    TAOS_RETURN(code);
×
1251
  }
1252

1253
  // cluster info
1254
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
63✔
1255
  pClusterInfo->monitor_interval = tsMonitorInterval;
63✔
1256
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
63✔
1257
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
63✔
1258
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
63✔
1259
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
63✔
1260
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
63✔
1261

1262
  void *pIter = NULL;
63✔
1263
  while (1) {
63✔
1264
    SDnodeObj *pObj = NULL;
126✔
1265
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
126✔
1266
    if (pIter == NULL) break;
126✔
1267

1268
    SMonDnodeDesc desc = {0};
63✔
1269
    desc.dnode_id = pObj->id;
63✔
1270
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
63✔
1271
    if (mndIsDnodeOnline(pObj, ms)) {
63✔
1272
      tstrncpy(desc.status, "ready", sizeof(desc.status));
63✔
1273
    } else {
1274
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1275
    }
1276
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
126✔
1277
      mError("failed put dnode into array, but continue at this monitor report")
×
1278
    }
1279
    sdbRelease(pSdb, pObj);
63✔
1280
  }
1281

1282
  pIter = NULL;
63✔
1283
  while (1) {
63✔
1284
    SMnodeObj *pObj = NULL;
126✔
1285
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
126✔
1286
    if (pIter == NULL) break;
126✔
1287

1288
    SMonMnodeDesc desc = {0};
63✔
1289
    desc.mnode_id = pObj->id;
63✔
1290
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
63✔
1291

1292
    if (pObj->id == pMnode->selfDnodeId) {
63✔
1293
      pClusterInfo->first_ep_dnode_id = pObj->id;
63✔
1294
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
63✔
1295
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1296
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
63✔
1297
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1298
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
63✔
1299
      desc.syncState = TAOS_SYNC_STATE_LEADER;
63✔
1300
    } else {
1301
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1302
      desc.syncState = pObj->syncState;
×
1303
    }
1304
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
126✔
1305
      mError("failed to put mnode into array, but continue at this monitor report");
×
1306
    }
1307
    sdbRelease(pSdb, pObj);
63✔
1308
  }
1309

1310
  // vgroup info
1311
  pIter = NULL;
63✔
1312
  while (1) {
378✔
1313
    SVgObj *pVgroup = NULL;
441✔
1314
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
441✔
1315
    if (pIter == NULL) break;
441✔
1316

1317
    if (pVgroup->mountVgId) {
378✔
1318
      sdbRelease(pSdb, pVgroup);
×
1319
      continue;
×
1320
    }
1321

1322
    pClusterInfo->vgroups_total++;
378✔
1323
    pClusterInfo->tbs_total += pVgroup->numOfTables;
378✔
1324

1325
    SMonVgroupDesc desc = {0};
378✔
1326
    desc.vgroup_id = pVgroup->vgId;
378✔
1327

1328
    SName name = {0};
378✔
1329
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
378✔
1330
    if (code < 0) {
378✔
1331
      mError("failed to get db name since %s", tstrerror(code));
×
1332
      sdbCancelFetch(pSdb, pIter);
×
1333
      sdbRelease(pSdb, pVgroup);
×
1334
      TAOS_RETURN(code);
×
1335
    }
1336
    (void)tNameGetDbName(&name, desc.database_name);
378✔
1337

1338
    desc.tables_num = pVgroup->numOfTables;
378✔
1339
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
378✔
1340
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
378✔
1341
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
756✔
1342
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
378✔
1343
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
378✔
1344
      pVnDesc->dnode_id = pVgid->dnodeId;
378✔
1345
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
378✔
1346
      pVnDesc->syncState = pVgid->syncState;
378✔
1347
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
378✔
1348
        tstrncpy(desc.status, "ready", sizeof(desc.status));
378✔
1349
        pClusterInfo->vgroups_alive++;
378✔
1350
      }
1351
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
378✔
1352
        pClusterInfo->vnodes_alive++;
378✔
1353
      }
1354
      pClusterInfo->vnodes_total++;
378✔
1355
    }
1356

1357
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
756✔
1358
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1359
    }
1360
    sdbRelease(pSdb, pVgroup);
378✔
1361
  }
1362

1363
  // stb info
1364
  pIter = NULL;
63✔
1365
  while (1) {
63✔
1366
    SStbObj *pStb = NULL;
126✔
1367
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
126✔
1368
    if (pIter == NULL) break;
126✔
1369

1370
    SMonStbDesc desc = {0};
63✔
1371

1372
    SName name1 = {0};
63✔
1373
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
63✔
1374
    if (code < 0) {
63✔
1375
      mError("failed to get db name since %s", tstrerror(code));
×
1376
      sdbRelease(pSdb, pStb);
×
1377
      TAOS_RETURN(code);
×
1378
    }
1379
    (void)tNameGetDbName(&name1, desc.database_name);
63✔
1380

1381
    SName name2 = {0};
63✔
1382
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
63✔
1383
    if (code < 0) {
63✔
1384
      mError("failed to get table name since %s", tstrerror(code));
×
1385
      sdbRelease(pSdb, pStb);
×
1386
      TAOS_RETURN(code);
×
1387
    }
1388
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
63✔
1389

1390
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
126✔
1391
      mError("failed to put stb into array, but continue at this monitor report");
×
1392
    }
1393
    sdbRelease(pSdb, pStb);
63✔
1394
  }
1395

1396
  // grant info
1397
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
63✔
1398
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
63✔
1399
  if (pMnode->grant.expireTimeMS == 0) {
63✔
1400
    pGrantInfo->expire_time = 0;
×
1401
    pGrantInfo->timeseries_total = 0;
×
1402
  }
1403

1404
  mndReleaseRpc(pMnode);
63✔
1405
  TAOS_RETURN(code);
63✔
1406
}
1407

1408
int32_t mndResetTimer(SMnode *pMnode){
×
1409
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1410
}
1411

1412
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
28,427,057✔
1413
  mTrace("mnode get load");
28,427,057✔
1414
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
28,427,057✔
1415
  pLoad->syncState = state.state;
28,427,057✔
1416
  pLoad->syncRestore = state.restored;
28,427,057✔
1417
  pLoad->syncTerm = state.term;
28,427,057✔
1418
  pLoad->roleTimeMs = state.roleTimeMs;
28,427,057✔
1419
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
28,427,057✔
1420
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1421
  return 0;
28,427,057✔
1422
}
1423

1424
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
9,061,380✔
1425
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
9,061,380✔
1426
  return state.roleTimeMs;
9,061,380✔
1427
}
1428

1429
void mndSetRestored(SMnode *pMnode, bool restored) {
487,107✔
1430
  if (restored) {
487,107✔
1431
    (void)taosThreadRwlockWrlock(&pMnode->lock);
487,107✔
1432
    pMnode->restored = true;
487,107✔
1433
    (void)taosThreadRwlockUnlock(&pMnode->lock);
487,107✔
1434
    mInfo("mnode set restored:%d", restored);
487,107✔
1435
  } else {
1436
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1437
    pMnode->restored = false;
×
1438
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1439
    mInfo("mnode set restored:%d", restored);
×
1440
    while (1) {
1441
      if (pMnode->rpcRef <= 0) break;
×
1442
      taosMsleep(3);
×
1443
    }
1444
  }
1445
}
487,107✔
1446

1447
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1448

1449
void mndSetStop(SMnode *pMnode) {
487,107✔
1450
  (void)taosThreadRwlockWrlock(&pMnode->lock);
487,107✔
1451
  pMnode->stopped = true;
487,107✔
1452
  (void)taosThreadRwlockUnlock(&pMnode->lock);
487,107✔
1453
  mInfo("mnode set stopped");
487,107✔
1454
}
487,107✔
1455

1456
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
576,275,308✔
1457

1458
void mndSetSoDPhase(SMnode *pMnode, int8_t phase) {
328✔
1459
  (void)taosThreadRwlockWrlock(&pMnode->lock);
328✔
1460
  pMnode->sodPhase = phase;
328✔
1461
  (void)taosThreadRwlockUnlock(&pMnode->lock);
328✔
1462
}
328✔
1463

1464
int8_t mndGetSoDPhase(SMnode *pMnode) {
70,108✔
1465
  int8_t result = TSDB_SOD_PHASE_STABLE;
70,108✔
1466
  (void)taosThreadRwlockRdlock(&pMnode->lock);
70,108✔
1467
  result = pMnode->sodPhase;
70,108✔
1468
  (void)taosThreadRwlockUnlock(&pMnode->lock);
70,108✔
1469
  if (result < TSDB_SOD_PHASE_STABLE || result > TSDB_SOD_PHASE_ENFORCE) {
70,108✔
1470
    mWarn("invalid SoD phase:%d, reset to stable", result);
×
1471
    result = TSDB_SOD_PHASE_STABLE;
×
1472
  }
1473
  return result;
70,108✔
1474
}
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