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

taosdata / TDengine / #5043

29 Apr 2026 11:44AM UTC coverage: 73.107% (-0.06%) from 73.17%
#5043

push

travis-ci

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

1563 of 1828 new or added lines in 18 files covered. (85.5%)

7490 existing lines in 148 files now uncovered.

277321 of 379338 relevant lines covered (73.11%)

131116908.85 hits per line

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

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

16
#define _DEFAULT_SOURCE
17
#include "mndAcct.h"
18
#include "mndAnode.h"
19
#include "mndArbGroup.h"
20
#include "mndBnode.h"
21
#include "mndCluster.h"
22
#include "mndCompact.h"
23
#include "mndCompactDetail.h"
24
#include "mndConfig.h"
25
#include "mndConsumer.h"
26
#include "mndDb.h"
27
#include "mndDnode.h"
28
#include "mndEncryptAlgr.h"
29
#include "mndFunc.h"
30
#include "mndGrant.h"
31
#include "mndIndex.h"
32
#include "mndInfoSchema.h"
33
#include "mndInstance.h"
34
#include "mndMnode.h"
35
#include "mndMount.h"
36
#include "mndPerfSchema.h"
37
#include "mndPrivilege.h"
38
#include "mndProfile.h"
39
#include "mndQnode.h"
40
#include "mndQuery.h"
41
#include "mndRetention.h"
42
#include "mndRetentionDetail.h"
43
#include "mndRole.h"
44
#include "mndRsma.h"
45
#include "mndScan.h"
46
#include "mndScanDetail.h"
47
#include "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) {
6,773,063✔
67
  int32_t code = 0;
6,773,063✔
68
  (void)taosThreadRwlockRdlock(&pMnode->lock);
6,773,063✔
69
  if (pMnode->stopped) {
6,773,063✔
70
    code = TSDB_CODE_APP_IS_STOPPING;
×
71
  } else if (!mndIsLeader(pMnode)) {
6,773,063✔
72
    code = 1;
×
73
  } else {
74
#if 1
75
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
6,773,063✔
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);
6,773,063✔
82
  TAOS_RETURN(code);
6,773,063✔
83
}
84

85
static inline void mndReleaseRpc(SMnode *pMnode) {
338,889,084✔
86
  (void)taosThreadRwlockRdlock(&pMnode->lock);
338,889,084✔
87
#if 1
88
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
338,890,222✔
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);
338,887,710✔
94
}
338,890,638✔
95

96
static void *mndBuildTimerMsg(int32_t *pContLen) {
88,391,480✔
97
  terrno = 0;
88,391,480✔
98
  SMTimerReq timerReq = {0};
88,391,480✔
99

100
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
88,391,480✔
101
  if (contLen <= 0) return NULL;
88,391,195✔
102
  void *pReq = rpcMallocCont(contLen);
88,391,195✔
103
  if (pReq == NULL) return NULL;
88,390,897✔
104

105
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
88,390,897✔
106
    mError("failed to serialize timer msg since %s", terrstr());
×
107
  }
108
  *pContLen = contLen;
88,391,480✔
109
  return pReq;
88,390,993✔
110
}
111

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

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

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

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

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

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

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

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

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

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

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

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

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

309
  void *pIter = NULL;
148,916✔
310
  while (1) {
506,267✔
311
    SVgObj *pVgroup = NULL;
655,183✔
312
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
655,183✔
313
    if (pIter == NULL) break;
655,183✔
314

315
    bool stateChanged = false;
506,267✔
316
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,225,165✔
317
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
937,171✔
318
      if (pGid->dnodeId == dnodeId) {
937,171✔
319
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
218,273✔
320
          mInfo(
95,285✔
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;
95,285✔
326
          pGid->syncRestore = 0;
95,285✔
327
          pGid->syncCanRead = 0;
95,285✔
328
          pGid->startTimeMs = 0;
95,285✔
329
          pGid->learnerProgress = 0;
95,285✔
330
          pGid->snapSeq = -1;
95,285✔
331
          stateChanged = true;
95,285✔
332
        }
333
        break;
218,273✔
334
      }
335
    }
336

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

347
    sdbRelease(pSdb, pVgroup);
506,267✔
348
  }
349
}
148,916✔
350

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

355
  SSdb   *pSdb = pMnode->pSdb;
6,772,997✔
356
  int64_t curMs = taosGetTimestampMs();
6,772,997✔
357

358
  void *pIter = NULL;
6,772,997✔
359
  while (1) {
11,263,387✔
360
    SDnodeObj *pDnode = NULL;
18,036,384✔
361
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
18,036,384✔
362
    if (pIter == NULL) break;
18,036,384✔
363

364
    bool online = mndIsDnodeOnline(pDnode, curMs);
11,263,387✔
365
    if (!online) {
11,263,387✔
366
      mInfo("dnode:%d, in offline state", pDnode->id);
148,916✔
367
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
148,916✔
368
    }
369

370
    sdbRelease(pSdb, pDnode);
11,263,387✔
371
  }
372

373
  mndReleaseRpc(pMnode);
6,772,997✔
374
}
375

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

385
  if (state.state != TAOS_SYNC_STATE_LEADER) {
400,262,467✔
386
    (void)taosThreadRwlockUnlock(&pMnode->lock);
14,467,832✔
387
    terrno = TSDB_CODE_SYN_NOT_LEADER;
14,467,832✔
388
    return true;
14,467,832✔
389
  }
390
  if (!state.restored || !pMnode->restored) {
385,794,635✔
391
    (void)taosThreadRwlockUnlock(&pMnode->lock);
16,857✔
392
    terrno = TSDB_CODE_SYN_RESTORING;
16,504✔
393
    return true;
16,504✔
394
  }
395
  (void)taosThreadRwlockUnlock(&pMnode->lock);
385,777,913✔
396
  return false;
385,780,110✔
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) {
35,726,447✔
416
  int32_t code = 0;
35,726,447✔
417
#ifndef TD_ASTRA
418
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
35,726,447✔
419
    mndPullupGrant(pMnode);
1,834,946✔
420
  }
421
  if (sec % tsTtlPushIntervalSec == 0) {
35,726,447✔
422
    mndPullupTtl(pMnode);
3,719,491✔
423
  }
424

425
  if (sec % tsTrimVDbIntervalSec == 0) {
35,726,447✔
426
    mndPullupTrimDb(pMnode);
16,873✔
427
  }
428

429
  if (sec % tsQueryTrimIntervalSec == 0) {
35,726,447✔
430
    mndPullupQueryTrimDb(pMnode);
3,777,672✔
431
  }
432
#endif
433
#ifdef USE_SHARED_STORAGE
434
  if (tsSsEnabled) {
35,726,447✔
435
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
242,964✔
436
      mndPullupUpdateSsMigrateProgress(pMnode);
177,062✔
437
    }
438
    if (tsSsEnabled == 2) {
242,964✔
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) {
35,726,447✔
454
    if (sec % tsAuthReqHBInterval == 0) {
×
455
      mndPullupAuth(pMnode);
×
456
    }
457
  }
458
#endif
459
  if (sec % tsTransPullupInterval == 0) {
35,726,447✔
460
    mndPullupTrans(pMnode);
17,862,846✔
461
  }
462

463
  if (sec % tsCompactPullupInterval == 0) {
35,726,447✔
464
    mndPullupCompacts(pMnode);
3,561,704✔
465
  }
466

467
  if (sec % tsScanPullupInterval == 0) {
35,726,447✔
468
    mndPullupScans(pMnode);
3,561,704✔
469
  }
470
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
35,726,447✔
471
    mndPullupInstances(pMnode);
7,144,682✔
472
  }
473
#ifdef USE_TOPIC
474
  if (sec % tsMqRebalanceInterval == 0) {
35,726,447✔
475
    mndCalMqRebalance(pMnode);
17,862,688✔
476
  }
477
#endif
478
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
35,726,447✔
479
    mndPullupTelem(pMnode);
248✔
480
  }
481
  if (sec % tsUptimeInterval == 0) {
35,726,447✔
482
    mndIncreaseUpTime(pMnode);
115,093✔
483
  }
484
}
35,726,447✔
485

486
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
350,053,663✔
487
  int32_t code = 0;
350,053,663✔
488
#ifndef TD_ASTRA
489
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
350,053,663✔
490
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
17,294,653✔
491
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
492
    }
493
  }
494

495
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
350,053,663✔
496
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
11,462,103✔
497
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
498
    }
499
  }
500
#endif
501
}
350,053,663✔
502

503
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
350,053,663✔
504
  if (ms % (tsStatusTimeoutMs) == 0) {
350,053,663✔
505
    mndCheckDnodeOffline(pMnode);
6,772,997✔
506
  }
507
}
350,053,663✔
508

509
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
35,726,447✔
510
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
35,726,447✔
511
    mndSyncCheckTimeout(pMnode);
1,191,185✔
512
  }
513
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
35,726,447✔
514
    msmHealthCheck(pMnode);
11,906,965✔
515
  }
516
}
35,726,447✔
517

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

523
  while (1) {
363,757,250✔
524
    if (mndGetStop(pMnode)) break;
364,247,304✔
525

526
    int64_t nowSec = taosGetTimestampMs() / 1000;
363,757,250✔
527
    if (nowSec == lastSec) {
363,757,250✔
528
      taosMsleep(100);
326,652,033✔
529
      continue;
326,652,033✔
530
    }
531
    lastSec = nowSec;
37,105,217✔
532

533
    if (mnodeIsNotLeader(pMnode)) {
37,105,217✔
534
      taosMsleep(100);
1,378,770✔
535
      mTrace("timer not process since mnode is not leader");
1,378,770✔
536
      continue;
1,378,770✔
537
    }
538

539
    mndDoTimerCheckSync(pMnode, nowSec);
35,726,447✔
540

541
    mndDoTimerPullupTask(pMnode, nowSec);
35,726,447✔
542

543
    taosMsleep(100);
35,726,447✔
544
  }
545

546
  return NULL;
490,054✔
547
}
548

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

554
  while (1) {
555
    lastTime += 100;
363,649,377✔
556
    taosMsleep(100);
363,649,377✔
557

558
    if (mndGetStop(pMnode)) break;
363,649,377✔
559
    if (lastTime % 10 != 0) continue;
363,159,323✔
560

561
    if (mnodeIsNotLeader(pMnode)) {
363,159,323✔
562
      mTrace("timer not process since mnode is not leader");
13,105,660✔
563
      continue;
13,105,660✔
564
    }
565

566
    mndDoTimerCheckStatus(pMnode, lastTime);
350,053,663✔
567

568
    mndDoArbTimerPullupTask(pMnode, lastTime);
350,053,663✔
569
  }
570

571
  return NULL;
490,054✔
572
}
573

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

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

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

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

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

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

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

630
  TAOS_RETURN(code);
490,192✔
631
}
632

633
static int32_t mndInitWal(SMnode *pMnode) {
490,192✔
634
  int32_t code = 0;
490,192✔
635
  char    path[PATH_MAX + 20] = {0};
490,192✔
636
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
490,192✔
637
  SWalCfg cfg = {.vgId = 1,
490,192✔
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) {
490,192✔
650
    code = terrno;
×
651
    TAOS_RETURN(code);
×
652
  }
653
  if (tsMetaKey[0] != '\0') {
490,192✔
654
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
5,406✔
655
  }
656
#endif
657

658
  pMnode->pWal = walOpen(path, &cfg);
490,192✔
659
  if (pMnode->pWal == NULL) {
490,192✔
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);
490,192✔
667
}
668

669
static void mndCloseWal(SMnode *pMnode) {
490,128✔
670
  if (pMnode->pWal != NULL) {
490,128✔
671
    walClose(pMnode->pWal);
490,128✔
672
    pMnode->pWal = NULL;
490,128✔
673
  }
674
}
490,128✔
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,548✔
682
  SMnode *pMnode = (SMnode *)param;
7,548✔
683
  if (pMnode == NULL) {
7,548✔
684
    return TSDB_CODE_INVALID_PARA;
×
685
  }
686
  
687
  mInfo("persisting encrypted flag to mnode.json");
7,548✔
688
  
689
  SMnodeOpt option = {0};
7,548✔
690
  int32_t code = mmReadFile(pMnode->path, &option);
7,548✔
691
  if (code != 0) {
7,548✔
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,548✔
697
  code = mmWriteFile(pMnode->path, &option);
7,548✔
698
  if (code != 0) {
7,548✔
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,548✔
705
  
706
  mInfo("successfully persisted encrypted flag to mnode.json");
7,548✔
707
  return 0;
7,548✔
708
}
709

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

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

724
  TAOS_RETURN(code);
490,192✔
725
}
726

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

736
  if (!pMnode->deploy) {
490,192✔
737
    code = sdbReadFile(pMnode->pSdb);
135,404✔
738
  }
739

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

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

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

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

762
  TAOS_RETURN(0);
24,999,792✔
763
}
764

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

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

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

829
  for (int32_t s = pos; s >= 0; s--) {
25,486,656✔
830
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
24,996,528✔
831
    mInfo("%s will cleanup", pStep->name);
24,996,528✔
832
    if (pStep->cleanupFp != NULL) {
24,996,528✔
833
      (*pStep->cleanupFp)(pMnode);
24,016,272✔
834
    }
835
  }
836

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

842
static int32_t mndExecSteps(SMnode *pMnode) {
490,192✔
843
  int32_t code = 0;
490,192✔
844
  int32_t size = taosArrayGetSize(pMnode->pSteps);
490,192✔
845
  for (int32_t pos = 0; pos < size; pos++) {
25,489,984✔
846
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
24,999,792✔
847
    if (pStep->initFp == NULL) continue;
24,999,792✔
848

849
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
24,999,792✔
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,999,792✔
855
      tmsgReportStartup(pStep->name, "initialized");
24,999,792✔
856
    }
857
  }
858

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

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

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

879
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
490,192✔
880
  if (pMnode == NULL) {
490,192✔
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));
490,192✔
886

887
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
490,192✔
888
  if (code != 0) {
490,192✔
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,
490,192✔
896
        pOption->numOfTotalReplicas);
897
  mndSetOptions(pMnode, pOption);
490,192✔
898

899
  pMnode->deploy = pOption->deploy;
490,192✔
900
  pMnode->version = pOption->version;
490,192✔
901
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
490,192✔
902
  if (pMnode->pSteps == NULL) {
490,192✔
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);
490,192✔
910
  if (code != 0) {
490,192✔
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);
490,192✔
918
  if (code != 0) {
490,192✔
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);
490,192✔
926
  if (code != 0) {
490,192✔
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");
490,192✔
934
  return pMnode;
490,192✔
935
}
936

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

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

963
int32_t mndStart(SMnode *pMnode) {
490,054✔
964
  int32_t code = 0;
490,054✔
965
  mndSyncStart(pMnode);
490,054✔
966
  if (pMnode->deploy) {
490,054✔
967
    if (sdbDeploy(pMnode->pSdb) != 0) {
354,788✔
968
      mError("failed to deploy sdb while start mnode");
×
969
      return -1;
×
970
    }
971
    mndSetRestored(pMnode, true);
354,788✔
972
  }
973
  if (mndIsLeader(pMnode)) {
490,054✔
974
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
444,272✔
975
      mError("failed to upgrade sdb while start mnode");
×
976
      return -1;
×
977
    }
978
#ifdef TD_ENTERPRISE
979
    if (tsSodEnforceMode) {
444,272✔
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;
490,054✔
997
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
490,054✔
998

999
  return mndInitTimer(pMnode);
490,054✔
1000
}
1001

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

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

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

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

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

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

1023
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
28,873,510✔
1024

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

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

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

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

1044
  return code;
65,975,082✔
1045
}
1046

1047
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
337,459,945✔
1048
  int32_t code = 0;
337,459,945✔
1049
  if (!IsReq(pMsg)) TAOS_RETURN(code);
337,459,945✔
1050
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
297,694,084✔
1051
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
291,313,327✔
1052
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
285,876,359✔
1053
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
273,246,870✔
1054
    TAOS_RETURN(code);
24,457,679✔
1055
  }
1056

1057
  SMnode *pMnode = pMsg->info.node;
273,241,949✔
1058
  (void)taosThreadRwlockRdlock(&pMnode->lock);
273,245,955✔
1059
  if (pMnode->stopped) {
273,251,726✔
1060
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,919✔
1061
    code = TSDB_CODE_APP_IS_STOPPING;
6,919✔
1062
    TAOS_RETURN(code);
6,919✔
1063
  }
1064

1065
  terrno = 0;
273,240,421✔
1066
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
273,236,578✔
1067
  if (terrno != 0) {
273,245,843✔
1068
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1069
    code = terrno;
×
1070
    TAOS_RETURN(code);
×
1071
  }
1072

1073
  if (state.state != TAOS_SYNC_STATE_LEADER) {
273,242,612✔
1074
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,274,685✔
1075
    code = TSDB_CODE_SYN_NOT_LEADER;
3,274,982✔
1076
    goto _OVER;
3,274,982✔
1077
  }
1078

1079
  if (!state.restored || !pMnode->restored) {
269,967,927✔
1080
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,077,027✔
1081
    code = TSDB_CODE_SYN_RESTORING;
2,076,761✔
1082
    goto _OVER;
2,076,761✔
1083
  }
1084

1085
#if 1
1086
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
267,891,526✔
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);
267,892,809✔
1093
  TAOS_RETURN(code);
267,889,941✔
1094

1095
_OVER:
5,351,743✔
1096
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,351,743✔
1097
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,351,743✔
1098
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,351,013✔
1099
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,350,609✔
1100
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,349,928✔
1101
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,350,089✔
1102
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,350,004✔
1103
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,349,793✔
1104
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,350,791✔
1105
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
782✔
1106
           pMnode->stopped, state.restored, syncStr(state.state));
1107
    TAOS_RETURN(code);
782✔
1108
  }
1109

1110
  const STraceId *trace = &pMsg->info.traceId;
5,346,281✔
1111
  SEpSet          epSet = {0};
5,349,271✔
1112
  mndGetMnodeEpSet(pMnode, &epSet);
5,349,229✔
1113

1114
  mGDebug(
5,351,539✔
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;
5,351,649✔
1121

1122
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
17,043,452✔
1123
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
11,691,803✔
1124
  }
1125

1126
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,351,649✔
1127
  pMsg->info.rsp = rpcMallocCont(contLen);
5,348,224✔
1128
  if (pMsg->info.rsp != NULL) {
5,351,409✔
1129
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,351,468✔
1130
      mError("failed to serialize ep set");
×
1131
    }
1132
    pMsg->info.hasEpSet = 1;
5,349,448✔
1133
    pMsg->info.rspLen = contLen;
5,350,127✔
1134
  }
1135

1136
  TAOS_RETURN(code);
5,350,534✔
1137
}
1138

1139
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
337,477,720✔
1140
  SMnode         *pMnode = pMsg->info.node;
337,477,720✔
1141
  const STraceId *trace = &pMsg->info.traceId;
337,479,544✔
1142
  int32_t         code = TSDB_CODE_SUCCESS;
337,478,520✔
1143

1144
#ifdef TD_ENTERPRISE
1145
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
337,478,520✔
1146
    SCachedTokenInfo ti = {0};
20,628✔
1147
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
20,628✔
1148
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,260✔
1149
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
2,260✔
1150
      TAOS_RETURN(code);
2,260✔
1151
    }
1152
    if (ti.enabled == 0) {
18,368✔
1153
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
3,132✔
1154
      code = TSDB_CODE_MND_TOKEN_DISABLED;
3,132✔
1155
      TAOS_RETURN(code);
3,132✔
1156
    }
1157
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
15,236✔
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));
15,236✔
1163
  }
1164
#endif
1165

1166
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
337,458,434✔
1167
  MndMsgFpExt fpExt = NULL;
337,474,934✔
1168
  if (fp == NULL) {
337,474,934✔
1169
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
24,456,506✔
1170
    if (fpExt == NULL) {
24,457,075✔
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));
337,475,503✔
1178

1179
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
332,112,126✔
1180
  if (fp)
332,111,678✔
1181
    code = (*fp)(pMsg);
307,654,603✔
1182
  else
1183
    code = (*fpExt)(pMsg, pQueueInfo);
24,457,075✔
1184
  mndReleaseRpc(pMnode);
332,115,026✔
1185

1186
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
332,117,717✔
1187
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
37,608,736✔
1188
  } else if (code == 0) {
294,508,981✔
1189
    mGTrace("msg:%p, successfully processed", pMsg);
282,725,630✔
1190
  } else {
1191
    // TODO removve this wrong set code
1192
    if (code == -1) {
11,783,351✔
1193
      code = terrno;
×
1194
    }
1195
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
11,783,351✔
1196
            TMSG_INFO(pMsg->msgType));
1197
  }
1198

1199
  TAOS_RETURN(code);
332,117,717✔
1200
}
1201

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

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

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

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

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

1241
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
66✔
1242
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
66✔
1243
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
66✔
1244
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
66✔
1245
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
66✔
1246
      pStbInfo->stbs == NULL) {
66✔
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));
66✔
1255
  pClusterInfo->monitor_interval = tsMonitorInterval;
66✔
1256
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
66✔
1257
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
66✔
1258
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
66✔
1259
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
66✔
1260
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
66✔
1261

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

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

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

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

1292
    if (pObj->id == pMnode->selfDnodeId) {
66✔
1293
      pClusterInfo->first_ep_dnode_id = pObj->id;
66✔
1294
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
66✔
1295
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1296
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
66✔
1297
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1298
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
66✔
1299
      desc.syncState = TAOS_SYNC_STATE_LEADER;
66✔
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) {
132✔
1305
      mError("failed to put mnode into array, but continue at this monitor report");
×
1306
    }
1307
    sdbRelease(pSdb, pObj);
66✔
1308
  }
1309

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

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

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

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

1328
    SName name = {0};
132✔
1329
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
132✔
1330
    if (code < 0) {
132✔
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);
132✔
1337

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

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

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

UNCOV
1370
    SMonStbDesc desc = {0};
×
1371

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

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

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

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

1404
  mndReleaseRpc(pMnode);
66✔
1405
  TAOS_RETURN(code);
66✔
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) {
36,111,065✔
1413
  mTrace("mnode get load");
36,111,065✔
1414
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
36,111,065✔
1415
  pLoad->syncState = state.state;
36,111,065✔
1416
  pLoad->syncRestore = state.restored;
36,111,065✔
1417
  pLoad->syncTerm = state.term;
36,111,065✔
1418
  pLoad->roleTimeMs = state.roleTimeMs;
36,111,065✔
1419
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
36,111,065✔
1420
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1421
  return 0;
36,111,065✔
1422
}
1423

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

1429
void mndSetRestored(SMnode *pMnode, bool restored) {
489,855✔
1430
  if (restored) {
489,855✔
1431
    (void)taosThreadRwlockWrlock(&pMnode->lock);
489,855✔
1432
    pMnode->restored = true;
489,855✔
1433
    (void)taosThreadRwlockUnlock(&pMnode->lock);
489,855✔
1434
    mInfo("mnode set restored:%d", restored);
489,855✔
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
}
489,855✔
1446

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

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

1456
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
727,883,773✔
1457

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

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