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

taosdata / TDengine / #5051

13 May 2026 12:00PM UTC coverage: 73.358% (-0.04%) from 73.398%
#5051

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

714 existing lines in 146 files now uncovered.

281543 of 383795 relevant lines covered (73.36%)

135448694.71 hits per line

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

80.02
/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
#define UPGRADE_INTERVAL 10
67
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
6,871,668✔
68
  int32_t code = 0;
6,871,668✔
69
  (void)taosThreadRwlockRdlock(&pMnode->lock);
6,871,668✔
70
  if (pMnode->stopped) {
6,871,668✔
71
    code = TSDB_CODE_APP_IS_STOPPING;
×
72
  } else if (!mndIsLeader(pMnode)) {
6,871,668✔
73
    code = 1;
×
74
  } else {
75
#if 1
76
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
6,871,668✔
77
#else
78
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
79
    mTrace("mnode rpc is acquired, ref:%d", ref);
80
#endif
81
  }
82
  (void)taosThreadRwlockUnlock(&pMnode->lock);
6,871,668✔
83
  TAOS_RETURN(code);
6,871,668✔
84
}
85

86
static inline void mndReleaseRpc(SMnode *pMnode) {
358,956,479✔
87
  (void)taosThreadRwlockRdlock(&pMnode->lock);
358,956,479✔
88
#if 1
89
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
358,957,358✔
90
#else
91
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
92
  mTrace("mnode rpc is released, ref:%d", ref);
93
#endif
94
  (void)taosThreadRwlockUnlock(&pMnode->lock);
358,956,282✔
95
}
358,957,870✔
96

97
static void *mndBuildTimerMsg(int32_t *pContLen) {
89,961,935✔
98
  terrno = 0;
89,961,935✔
99
  SMTimerReq timerReq = {0};
89,961,935✔
100

101
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
89,961,935✔
102
  if (contLen <= 0) return NULL;
89,961,558✔
103
  void *pReq = rpcMallocCont(contLen);
89,961,558✔
104
  if (pReq == NULL) return NULL;
89,961,935✔
105

106
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
89,961,935✔
107
    mError("failed to serialize timer msg since %s", terrstr());
×
108
  }
109
  *pContLen = contLen;
89,960,503✔
110
  return pReq;
89,960,750✔
111
}
112

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

126
static void mndPullupUpgradeSdb(SMnode *pMnode) {
378,683✔
127
  if (sdbIsUpgraded(pMnode->pSdb)) {
378,683✔
128
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
345,387✔
129
    return;
345,387✔
130
  }
131

132
  if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION && mndIsLeader(pMnode)) {
33,296✔
133
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
33,296✔
134
      mError("failed to upgrade sdb while start mnode");
×
135
      return;
×
136
    }
137
    if (sdbIsUpgraded(pMnode->pSdb)) {
33,296✔
138
      pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
2,804✔
139
    }
140
  }
141
}
142

143
static void mndPullupCompacts(SMnode *pMnode) {
3,635,697✔
144
  mTrace("pullup compact timer msg");
3,635,697✔
145
  int32_t contLen = 0;
3,635,697✔
146
  void   *pReq = mndBuildTimerMsg(&contLen);
3,635,697✔
147
  if (pReq != NULL) {
3,635,697✔
148
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_COMPACT_TIMER, .pCont = pReq, .contLen = contLen};
3,635,697✔
149
    // TODO check return value
150
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,635,697✔
151
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
152
    }
153
  }
154
}
3,635,697✔
155

156
static void mndPullupScans(SMnode *pMnode) {
3,635,801✔
157
  mTrace("pullup scan timer msg");
3,635,801✔
158
  int32_t contLen = 0;
3,635,801✔
159
  void   *pReq = mndBuildTimerMsg(&contLen);
3,635,801✔
160
  if (pReq != NULL) {
3,635,801✔
161
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_SCAN_TIMER, .pCont = pReq, .contLen = contLen};
3,635,801✔
162
    // TODO check return value
163
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,635,801✔
164
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
165
    }
166
  }
167
}
3,635,801✔
168

169
static void mndPullupInstances(SMnode *pMnode) {
7,262,250✔
170
  mTrace("pullup instance timer msg");
7,262,250✔
171
  int32_t contLen = 0;
7,262,250✔
172
  void   *pReq = mndBuildTimerMsg(&contLen);
7,262,250✔
173
  if (pReq != NULL) {
7,262,250✔
174
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_INSTANCE_TIMER, .pCont = pReq, .contLen = contLen};
7,262,250✔
175
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
7,262,250✔
176
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
177
    }
178
  }
179
}
7,262,250✔
180

181
static void mndPullupTtl(SMnode *pMnode) {
3,794,074✔
182
  mTrace("pullup ttl");
3,794,074✔
183
  int32_t contLen = 0;
3,794,074✔
184
  void   *pReq = mndBuildTimerMsg(&contLen);
3,794,074✔
185
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
3,794,074✔
186
  // TODO check return value
187
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,794,074✔
188
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
189
  }
190
}
3,794,074✔
191

192
static void mndPullupTrimDb(SMnode *pMnode) {
11,413✔
193
  mTrace("pullup trim");
11,413✔
194
  int32_t contLen = 0;
11,413✔
195
  void   *pReq = mndBuildTimerMsg(&contLen);
11,413✔
196
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
11,413✔
197
  // TODO check return value
198
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,413✔
199
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
200
  }
201
}
11,413✔
202

203
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,860,473✔
204
  mTrace("pullup trim query");
3,860,473✔
205
  int32_t contLen = 0;
3,860,473✔
206
  void   *pReq = mndBuildTimerMsg(&contLen);
3,860,473✔
207
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,860,473✔
208
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,860,473✔
209
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
210
  }
211
}
3,860,473✔
212

213
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
214
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
215
    return;
×
216
  }
217

218
  mTrace("pullup ssmigrate db");
×
219
  int32_t contLen = 0;
×
220
  void   *pReq = mndBuildTimerMsg(&contLen);
×
221
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
222
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
223
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
224
  }
225
}
226

227
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
182,546✔
228
  mTrace("pullup update ssmigrate progress");
182,546✔
229
  int32_t contLen = 0;
182,546✔
230
  void   *pReq = mndBuildTimerMsg(&contLen);
182,546✔
231
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
182,546✔
232
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
182,546✔
233
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
234
  }
235
}
182,546✔
236

237
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
17,549,576✔
238
  mTrace("pullup arb hb");
17,549,576✔
239
  int32_t contLen = 0;
17,549,576✔
240
  void   *pReq = mndBuildTimerMsg(&contLen);
17,549,576✔
241
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
17,549,576✔
242
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
17,549,576✔
243
}
244

245
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
11,626,326✔
246
  mTrace("pullup arb sync");
11,626,326✔
247
  int32_t contLen = 0;
11,626,326✔
248
  void   *pReq = mndBuildTimerMsg(&contLen);
11,626,326✔
249
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,626,326✔
250
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,626,326✔
251
}
252

253
static void mndCalMqRebalance(SMnode *pMnode) {
18,151,284✔
254
  int32_t contLen = 0;
18,151,284✔
255
  void   *pReq = mndBuildTimerMsg(&contLen);
18,151,284✔
256
  if (pReq != NULL) {
18,151,284✔
257
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
18,151,284✔
258
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
18,151,284✔
259
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
260
    }
261
  }
262
}
18,151,284✔
263

264
static void mndPullupTelem(SMnode *pMnode) {
291✔
265
  mTrace("pullup telem msg");
291✔
266
  int32_t contLen = 0;
291✔
267
  void   *pReq = mndBuildTimerMsg(&contLen);
291✔
268
  if (pReq != NULL) {
291✔
269
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
291✔
270
    // TODO check return value
271
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
291✔
272
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
273
    }
274
  }
275
}
291✔
276

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

294
static void mndPullupAuth(SMnode *pMnode) {
×
295
  mTrace("pullup auth msg");
×
296
  int32_t contLen = 0;
×
297
  void   *pReq = mndBuildTimerMsg(&contLen);
×
298
  if (pReq != NULL) {
×
299
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_AUTH_HB_TIMER, .pCont = pReq, .contLen = contLen, .info.notFreeAhandle = 1, .info.ahandle = 0};
×
300
    // TODO check return value
301
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
302
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
303
    }
304
  }
305
}
×
306

307
static void mndPullupCls(SMnode *pMnode) {
×
308
  mTrace("pullup cls msg");
×
309
  int32_t contLen = 0;
×
310
  void   *pReq = mndBuildTimerMsg(&contLen);
×
311
  if (pReq != NULL) {
×
312
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_CLS_HB_TIMER, .pCont = pReq, .contLen = contLen, .info.notFreeAhandle = 1, .info.ahandle = 0};
×
313
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
314
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
315
    }
316
  }
317
}
×
318

319
static void mndIncreaseUpTime(SMnode *pMnode) {
125,494✔
320
  mTrace("increate uptime");
125,494✔
321
  int32_t contLen = 0;
125,494✔
322
  void   *pReq = mndBuildTimerMsg(&contLen);
125,494✔
323
  if (pReq != NULL) {
125,494✔
324
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
125,494✔
325
                      .pCont = pReq,
326
                      .contLen = contLen,
327
                      .info.notFreeAhandle = 1,
328
                      .info.ahandle = 0};
329
    // TODO check return value
330
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
125,494✔
331
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
332
    }
333
  }
334
}
125,494✔
335

336
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
137,561✔
337
  SSdb *pSdb = pMnode->pSdb;
137,561✔
338

339
  void *pIter = NULL;
137,561✔
340
  while (1) {
472,529✔
341
    SVgObj *pVgroup = NULL;
610,090✔
342
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
610,090✔
343
    if (pIter == NULL) break;
610,090✔
344

345
    bool stateChanged = false;
472,529✔
346
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,195,530✔
347
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
928,246✔
348
      if (pGid->dnodeId == dnodeId) {
928,246✔
349
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
205,245✔
350
          mInfo(
84,642✔
351
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
352
              "restored:0 "
353
              "canRead:0",
354
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
355
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
84,642✔
356
          pGid->syncRestore = 0;
84,642✔
357
          pGid->syncCanRead = 0;
84,642✔
358
          pGid->startTimeMs = 0;
84,642✔
359
          pGid->learnerProgress = 0;
84,642✔
360
          pGid->snapSeq = -1;
84,642✔
361
          stateChanged = true;
84,642✔
362
        }
363
        break;
205,245✔
364
      }
365
    }
366

367
    if (stateChanged) {
472,529✔
368
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
84,642✔
369
      if (pDb != NULL && pDb->stateTs != curMs) {
84,642✔
370
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
48,395✔
371
              curMs);
372
        pDb->stateTs = curMs;
48,395✔
373
      }
374
      mndReleaseDb(pMnode, pDb);
84,642✔
375
    }
376

377
    sdbRelease(pSdb, pVgroup);
472,529✔
378
  }
379
}
137,561✔
380

381
static void mndCheckDnodeOffline(SMnode *pMnode) {
6,871,568✔
382
  mTrace("check dnode offline");
6,871,568✔
383
  if (mndAcquireRpc(pMnode) != 0) return;
6,871,568✔
384

385
  SSdb   *pSdb = pMnode->pSdb;
6,871,568✔
386
  int64_t curMs = taosGetTimestampMs();
6,871,568✔
387

388
  void *pIter = NULL;
6,871,568✔
389
  while (1) {
11,403,011✔
390
    SDnodeObj *pDnode = NULL;
18,274,579✔
391
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
18,274,579✔
392
    if (pIter == NULL) break;
18,274,579✔
393

394
    bool online = mndIsDnodeOnline(pDnode, curMs);
11,403,011✔
395
    if (!online) {
11,403,011✔
396
      mInfo("dnode:%d, in offline state", pDnode->id);
137,561✔
397
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
137,561✔
398
    }
399

400
    sdbRelease(pSdb, pDnode);
11,403,011✔
401
  }
402

403
  mndReleaseRpc(pMnode);
6,871,568✔
404
}
405

406
static bool mnodeIsNotLeader(SMnode *pMnode) {
406,984,317✔
407
  terrno = 0;
406,984,317✔
408
  (void)taosThreadRwlockRdlock(&pMnode->lock);
406,984,317✔
409
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
406,984,557✔
410
  if (terrno != 0) {
406,984,508✔
411
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
412
    return true;
×
413
  }
414

415
  if (state.state != TAOS_SYNC_STATE_LEADER) {
406,982,846✔
416
    (void)taosThreadRwlockUnlock(&pMnode->lock);
15,020,190✔
417
    terrno = TSDB_CODE_SYN_NOT_LEADER;
15,020,190✔
418
    return true;
15,020,190✔
419
  }
420
  if (!state.restored || !pMnode->restored) {
391,962,656✔
421
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,931✔
422
    terrno = TSDB_CODE_SYN_RESTORING;
1,293✔
423
    return true;
1,293✔
424
  }
425
  (void)taosThreadRwlockUnlock(&pMnode->lock);
391,960,388✔
426
  return false;
391,962,392✔
427
}
428

429
static int32_t minCronTime() {
×
430
  int32_t min = INT32_MAX;
×
431
  min = TMIN(min, tsTtlPushIntervalSec);
×
432
  min = TMIN(min, tsTrimVDbIntervalSec);
×
433
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
434
  min = TMIN(min, tsTransPullupInterval);
×
435
  min = TMIN(min, tsCompactPullupInterval);
×
436
  min = TMIN(min, tsMqRebalanceInterval);
×
437

438
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
439
  min = TMIN(min, telemInt);
×
440
  min = TMIN(min, tsGrantHBInterval);
×
441
  min = TMIN(min, tsUptimeInterval);
×
442
#ifdef TD_ENTERPRISE
443
  if (tsClsEnabled) min = TMIN(min, tsClsRefreshInterval);
×
444
#endif
445

446
  return min <= 1 ? 2 : min;
×
447
}
448
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
36,297,672✔
449
  int32_t code = 0;
36,297,672✔
450
#ifndef TD_ASTRA
451
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
36,297,672✔
452
    mndPullupGrant(pMnode);
1,975,219✔
453
  }
454
  if (sec % tsTtlPushIntervalSec == 0) {
36,297,672✔
455
    mndPullupTtl(pMnode);
3,794,074✔
456
  }
457

458
  if (sec % tsTrimVDbIntervalSec == 0) {
36,297,672✔
459
    mndPullupTrimDb(pMnode);
11,413✔
460
  }
461

462
  if (sec % tsQueryTrimIntervalSec == 0) {
36,297,672✔
463
    mndPullupQueryTrimDb(pMnode);
3,860,473✔
464
  }
465
#endif
466
#ifdef USE_SHARED_STORAGE
467
  if (tsSsEnabled) {
36,297,672✔
468
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
252,756✔
469
      mndPullupUpdateSsMigrateProgress(pMnode);
182,546✔
470
    }
471
    if (tsSsEnabled == 2) {
252,756✔
472
      // By default, both tsTrimVDbIntervalSec and tsSsAutoMigrateIntervalSec are 3600 seconds,
473
      // so, delay half interval to do ss migrate to avoid conflict.
474
      //
475
      // NOTE: this solution is not perfect, there could still be conflict if user changes the
476
      // default value, but it is good enough as user is unlikely to change the default value.
477
      // The best solution is adding a new offset config to all cron tasks, but that would add
478
      // extra complexity.
479
      if ((sec % tsSsAutoMigrateIntervalSec) == (tsSsAutoMigrateIntervalSec / 2)) {
×
480
        mndPullupSsMigrateDb(pMnode);
×
481
      }
482
    }
483
  }
484
#endif
485
#ifdef TD_ENTERPRISE
486
  if (tsAuthReq) {
36,297,672✔
487
    if (sec % tsAuthReqHBInterval == 0) {
×
488
      mndPullupAuth(pMnode);
×
489
    }
490
  }
491
  if (tsClsEnabled || tsClsRefreshInterval == GRANT_CLS_CLOSING || tsClsRefreshInterval == GRANT_CLS_OPENING) {
36,297,672✔
492
    if (sec % tsClsRefreshInterval == 0) {
×
493
      mndPullupCls(pMnode);
×
494
    }
495
  }
496
#endif
497
  if (sec % tsTransPullupInterval == 0) {
36,297,672✔
498
    mndPullupTrans(pMnode);
18,151,491✔
499
  }
500

501
  if (sec % tsCompactPullupInterval == 0) {
36,297,672✔
502
    mndPullupCompacts(pMnode);
3,635,697✔
503
  }
504

505
  if (sec % tsScanPullupInterval == 0) {
36,297,672✔
506
    mndPullupScans(pMnode);
3,635,801✔
507
  }
508
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
36,297,672✔
509
    mndPullupInstances(pMnode);
7,262,250✔
510
  }
511
#ifdef USE_TOPIC
512
  if (sec % tsMqRebalanceInterval == 0) {
36,297,672✔
513
    mndCalMqRebalance(pMnode);
18,151,284✔
514
  }
515
#endif
516
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
36,297,672✔
517
    mndPullupTelem(pMnode);
291✔
518
  }
519
  if (sec % tsUptimeInterval == 0) {
36,297,672✔
520
    mndIncreaseUpTime(pMnode);
125,494✔
521
  }
522

523
  if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION && sec % UPGRADE_INTERVAL == 0) {
36,297,672✔
524
    mndPullupUpgradeSdb(pMnode);
378,683✔
525
  }
526
}
36,297,672✔
527

528
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
355,666,065✔
529
  int32_t code = 0;
355,666,065✔
530
#ifndef TD_ASTRA
531
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
355,666,065✔
532
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
17,549,576✔
533
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
534
    }
535
  }
536

537
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
355,666,065✔
538
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
11,626,326✔
539
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
540
    }
541
  }
542
#endif
543
}
355,666,065✔
544

545
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
355,666,065✔
546
  if (ms % (tsStatusTimeoutMs) == 0) {
355,666,065✔
547
    mndCheckDnodeOffline(pMnode);
6,871,568✔
548
  }
549
}
355,666,065✔
550

551
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
36,297,672✔
552
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
36,297,672✔
553
    mndSyncCheckTimeout(pMnode);
1,223,683✔
554
  }
555
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
36,297,672✔
556
    msmHealthCheck(pMnode);
12,093,260✔
557
  }
558
}
36,297,672✔
559

560
static void *mndThreadSecFp(void *param) {
543,985✔
561
  SMnode *pMnode = param;
543,985✔
562
  int64_t lastSec = 0;
543,985✔
563
  setThreadName("mnode-timer");
543,985✔
564
  taosSetCpuAffinity(THREAD_CAT_MANAGEMENT);
543,985✔
565

566
  while (1) {
369,764,340✔
567
    if (mndGetStop(pMnode)) break;
370,308,325✔
568

569
    int64_t nowSec = taosGetTimestampMs() / 1000;
369,764,340✔
570
    if (nowSec == lastSec) {
369,764,340✔
571
      taosMsleep(100);
332,036,748✔
572
      continue;
332,036,748✔
573
    }
574
    lastSec = nowSec;
37,727,592✔
575

576
    if (mnodeIsNotLeader(pMnode)) {
37,727,592✔
577
      taosMsleep(100);
1,429,920✔
578
      mTrace("timer not process since mnode is not leader");
1,429,920✔
579
      continue;
1,429,920✔
580
    }
581

582
    mndDoTimerCheckSync(pMnode, nowSec);
36,297,672✔
583

584
    mndDoTimerPullupTask(pMnode, nowSec);
36,297,672✔
585

586
    taosMsleep(100);
36,297,672✔
587
  }
588

589
  return NULL;
543,985✔
590
}
591

592
static void *mndThreadMsFp(void *param) {
543,985✔
593
  SMnode *pMnode = param;
543,985✔
594
  int64_t lastTime = 0;
543,985✔
595
  setThreadName("mnode-arb-timer");
543,985✔
596
  taosSetCpuAffinity(THREAD_CAT_MANAGEMENT);
543,985✔
597

598
  while (1) {
599
    lastTime += 100;
369,801,613✔
600
    taosMsleep(100);
369,801,613✔
601

602
    if (mndGetStop(pMnode)) break;
369,801,613✔
603
    if (lastTime % 10 != 0) continue;
369,257,628✔
604

605
    if (mnodeIsNotLeader(pMnode)) {
369,257,628✔
606
      mTrace("timer not process since mnode is not leader");
13,591,563✔
607
      continue;
13,591,563✔
608
    }
609

610
    mndDoTimerCheckStatus(pMnode, lastTime);
355,666,065✔
611

612
    mndDoArbTimerPullupTask(pMnode, lastTime);
355,666,065✔
613
  }
614

615
  return NULL;
543,985✔
616
}
617

618
static int32_t mndInitTimer(SMnode *pMnode) {
543,985✔
619
  int32_t      code = 0;
543,985✔
620
  TdThreadAttr thAttr;
542,221✔
621
  (void)taosThreadAttrInit(&thAttr);
543,985✔
622
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
543,985✔
623
#ifdef TD_COMPACT_OS
624
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
625
#endif
626
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
543,985✔
627
    mError("failed to create timer thread since %s", tstrerror(code));
×
628
    TAOS_RETURN(code);
×
629
  }
630

631
  (void)taosThreadAttrDestroy(&thAttr);
543,985✔
632
  tmsgReportStartup("mnode-timer", "initialized");
543,985✔
633

634
  TdThreadAttr arbAttr;
542,221✔
635
  (void)taosThreadAttrInit(&arbAttr);
543,985✔
636
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
543,985✔
637
#ifdef TD_COMPACT_OS
638
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
639
#endif
640
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
543,985✔
641
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
642
    TAOS_RETURN(code);
×
643
  }
644

645
  (void)taosThreadAttrDestroy(&arbAttr);
543,985✔
646
  tmsgReportStartup("mnode-timer", "initialized");
543,985✔
647
  TAOS_RETURN(code);
543,985✔
648
}
649

650
static void mndCleanupTimer(SMnode *pMnode) {
543,985✔
651
  if (taosCheckPthreadValid(pMnode->thread)) {
543,985✔
652
    (void)taosThreadJoin(pMnode->thread, NULL);
543,985✔
653
    taosThreadClear(&pMnode->thread);
543,985✔
654
  }
655
  if (taosCheckPthreadValid(pMnode->arbThread)) {
543,985✔
656
    (void)taosThreadJoin(pMnode->arbThread, NULL);
543,985✔
657
    taosThreadClear(&pMnode->arbThread);
543,985✔
658
  }
659
}
543,985✔
660

661
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
544,163✔
662
  int32_t code = 0;
544,163✔
663
  pMnode->path = taosStrdup(path);
544,163✔
664
  if (pMnode->path == NULL) {
544,163✔
665
    code = terrno;
×
666
    TAOS_RETURN(code);
×
667
  }
668

669
  if (taosMkDir(pMnode->path) != 0) {
544,163✔
670
    code = terrno;
×
671
    TAOS_RETURN(code);
×
672
  }
673

674
  TAOS_RETURN(code);
544,163✔
675
}
676

677
static int32_t mndInitWal(SMnode *pMnode) {
544,163✔
678
  int32_t code = 0;
544,163✔
679
  char    path[PATH_MAX + 20] = {0};
544,163✔
680
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
544,163✔
681
  SWalCfg cfg = {.vgId = 1,
544,163✔
682
                 .fsyncPeriod = 0,
683
                 .rollPeriod = -1,
684
                 .segSize = -1,
685
                 .committed = -1,
686
                 .retentionPeriod = 0,
687
                 .retentionSize = 0,
688
                 .level = TAOS_WAL_FSYNC,
689
                 .encryptAlgr = 0,
690
                 .encryptData = {0}};
691

692
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
693
  if (taosWaitCfgKeyLoaded() != 0) {
544,163✔
694
    code = terrno;
×
695
    TAOS_RETURN(code);
×
696
  }
697
  if (tsMetaKey[0] != '\0') {
544,163✔
698
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
5,995✔
699
  }
700
#endif
701

702
  pMnode->pWal = walOpen(path, &cfg);
544,163✔
703
  if (pMnode->pWal == NULL) {
544,163✔
704
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
705
    if (terrno != 0) code = terrno;
×
706
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
707
    TAOS_RETURN(code);
×
708
  }
709

710
  TAOS_RETURN(code);
544,163✔
711
}
712

713
static void mndCloseWal(SMnode *pMnode) {
544,095✔
714
  if (pMnode->pWal != NULL) {
544,095✔
715
    walClose(pMnode->pWal);
544,095✔
716
    pMnode->pWal = NULL;
544,095✔
717
  }
718
}
544,095✔
719

720
// Forward declarations for mmFile.c functions
721
extern int32_t mmReadFile(const char *path, SMnodeOpt *pOption);
722
extern int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption);
723

724
// Callback function to persist encrypted flag to mnode.json
725
static int32_t mndPersistEncryptedFlag(void *param) {
8,597✔
726
  SMnode *pMnode = (SMnode *)param;
8,597✔
727
  if (pMnode == NULL) {
8,597✔
728
    return TSDB_CODE_INVALID_PARA;
×
729
  }
730
  
731
  mInfo("persisting encrypted flag to mnode.json");
8,597✔
732
  
733
  SMnodeOpt option = {0};
8,597✔
734
  int32_t code = mmReadFile(pMnode->path, &option);
8,597✔
735
  if (code != 0) {
8,597✔
736
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
737
    return code;
×
738
  }
739
  
740
  option.encrypted = true;
8,597✔
741
  code = mmWriteFile(pMnode->path, &option);
8,597✔
742
  if (code != 0) {
8,597✔
743
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
744
    return code;
×
745
  }
746
  
747
  // Also update mnode's encrypted flag
748
  pMnode->encrypted = true;
8,597✔
749
  
750
  mInfo("successfully persisted encrypted flag to mnode.json");
8,597✔
751
  return 0;
8,597✔
752
}
753

754
static int32_t mndInitSdb(SMnode *pMnode) {
544,163✔
755
  int32_t code = 0;
544,163✔
756
  SSdbOpt opt = {0};
544,163✔
757
  opt.path = pMnode->path;
544,163✔
758
  opt.pMnode = pMnode;
544,163✔
759
  opt.pWal = pMnode->pWal;
544,163✔
760

761
  pMnode->pSdb = sdbInit(&opt);
544,163✔
762
  if (pMnode->pSdb == NULL) {
544,163✔
763
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
764
    if (terrno != 0) code = terrno;
×
765
    TAOS_RETURN(code);
×
766
  }
767

768
  TAOS_RETURN(code);
544,163✔
769
}
770

771
static int32_t mndOpenSdb(SMnode *pMnode) {
544,163✔
772
  int32_t code = 0;
544,163✔
773
  
774
  pMnode->pSdb->encrypted = pMnode->encrypted;
544,163✔
775
  
776
  // Set callback for persisting encrypted flag
777
  pMnode->pSdb->persistEncryptedFlagFp = mndPersistEncryptedFlag;
544,163✔
778
  pMnode->pSdb->pMnodeForCallback = pMnode;
544,163✔
779

780
  if (!pMnode->deploy) {
544,163✔
781
    code = sdbReadFile(pMnode->pSdb);
154,993✔
782
  }
783

784
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
544,163✔
785

786
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
544,163✔
787
  return code;
544,163✔
788
}
789

790
static void mndCleanupSdb(SMnode *pMnode) {
544,095✔
791
  if (pMnode->pSdb) {
544,095✔
792
    sdbCleanup(pMnode->pSdb);
544,095✔
793
    pMnode->pSdb = NULL;
544,095✔
794
  }
795
}
544,095✔
796

797
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
27,752,313✔
798
  SMnodeStep step = {0};
27,752,313✔
799
  step.name = name;
27,752,313✔
800
  step.initFp = initFp;
27,752,313✔
801
  step.cleanupFp = cleanupFp;
27,752,313✔
802
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
55,504,626✔
803
    TAOS_RETURN(terrno);
×
804
  }
805

806
  TAOS_RETURN(0);
27,752,313✔
807
}
808

809
static int32_t mndInitSteps(SMnode *pMnode) {
544,163✔
810
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
544,163✔
811
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
544,163✔
812
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
544,163✔
813
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
544,163✔
814
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-security-policy", mndInitSecurityPolicy, mndCleanupSecurityPolicy));
544,163✔
815
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-encrypt-algorithms", mndInitEncryptAlgr, mndCleanupEncryptAlgr));
544,163✔
816
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
544,163✔
817
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
544,163✔
818
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
544,163✔
819
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
544,163✔
820
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
544,163✔
821
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-xnode", mndInitXnode, mndCleanupXnode));
544,163✔
822
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
544,163✔
823
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
544,163✔
824
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
544,163✔
825
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-role", mndInitRole, mndCleanupRole));
544,163✔
826
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
544,163✔
827
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-token", mndInitToken, mndCleanupToken));
544,163✔
828
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
544,163✔
829
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
544,163✔
830
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
544,163✔
831
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
544,163✔
832
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-instance", mndInitInstance, mndCleanupInstance));
544,163✔
833
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
544,163✔
834
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
544,163✔
835
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
544,163✔
836
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
544,163✔
837
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
544,163✔
838
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
544,163✔
839
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
544,163✔
840
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
544,163✔
841
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
544,163✔
842
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
544,163✔
843
#ifdef USE_MOUNT
844
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
544,163✔
845
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
544,163✔
846
#endif
847
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
544,163✔
848
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
544,163✔
849
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
544,163✔
850
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
544,163✔
851
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
544,163✔
852
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
544,163✔
853
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
544,163✔
854
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
544,163✔
855
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
544,163✔
856
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
544,163✔
857
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
544,163✔
858
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
544,163✔
859
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
544,163✔
860
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
544,163✔
861
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
544,163✔
862
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
544,163✔
863
  return 0;
544,163✔
864
}
865

866
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
544,095✔
867
  if (pMnode->pSteps == NULL) return;
544,095✔
868

869
  if (pos == -1) {
544,095✔
870
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
544,095✔
871
  }
872

873
  for (int32_t s = pos; s >= 0; s--) {
28,292,940✔
874
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
27,748,845✔
875
    mInfo("%s will cleanup", pStep->name);
27,748,845✔
876
    if (pStep->cleanupFp != NULL) {
27,748,845✔
877
      (*pStep->cleanupFp)(pMnode);
26,660,655✔
878
    }
879
  }
880

881
  taosArrayClear(pMnode->pSteps);
544,095✔
882
  taosArrayDestroy(pMnode->pSteps);
544,095✔
883
  pMnode->pSteps = NULL;
544,095✔
884
}
885

886
static int32_t mndExecSteps(SMnode *pMnode) {
544,163✔
887
  int32_t code = 0;
544,163✔
888
  int32_t size = taosArrayGetSize(pMnode->pSteps);
544,163✔
889
  for (int32_t pos = 0; pos < size; pos++) {
28,296,476✔
890
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
27,752,313✔
891
    if (pStep->initFp == NULL) continue;
27,752,313✔
892

893
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
27,752,313✔
894
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
895
      mndCleanupSteps(pMnode, pos);
×
896
      TAOS_RETURN(code);
×
897
    } else {
898
      mInfo("%s is initialized", pStep->name);
27,752,313✔
899
      tmsgReportStartup(pStep->name, "initialized");
27,752,313✔
900
    }
901
  }
902

903
  pMnode->clusterId = mndGetClusterId(pMnode);
544,163✔
904
  TAOS_RETURN(0);
544,163✔
905
}
906

907
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
544,163✔
908
  pMnode->msgCb = pOption->msgCb;
544,163✔
909
  pMnode->selfDnodeId = pOption->dnodeId;
544,163✔
910
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
544,163✔
911
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
544,163✔
912
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
544,163✔
913
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
544,163✔
914
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
544,163✔
915
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
544,163✔
916
  pMnode->encrypted = pOption->encrypted;
544,163✔
917
}
544,163✔
918

919
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
544,163✔
920
  terrno = 0;
544,163✔
921
  mInfo("start to open mnode in %s", path);
544,163✔
922

923
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
544,163✔
924
  if (pMnode == NULL) {
544,163✔
925
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
926
    mError("failed to open mnode in step 1, since %s", terrstr());
×
927
    return NULL;
×
928
  }
929
  (void)memset(pMnode, 0, sizeof(SMnode));
544,163✔
930

931
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
544,163✔
932
  if (code != 0) {
544,163✔
933
    taosMemoryFree(pMnode);
×
934
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
935
    terrno = code;
×
936
    return NULL;
×
937
  }
938

939
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
544,163✔
940
        pOption->numOfTotalReplicas);
941
  mndSetOptions(pMnode, pOption);
544,163✔
942

943
  pMnode->deploy = pOption->deploy;
544,163✔
944
  pMnode->version = pOption->version;
544,163✔
945
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
544,163✔
946
  if (pMnode->pSteps == NULL) {
544,163✔
947
    taosMemoryFree(pMnode);
×
948
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
949
    mError("failed to open mnode in step 4, since %s", terrstr());
×
950
    return NULL;
×
951
  }
952

953
  code = mndCreateDir(pMnode, path);
544,163✔
954
  if (code != 0) {
544,163✔
955
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
956
    mndClose(pMnode);
×
957
    terrno = code;
×
958
    return NULL;
×
959
  }
960

961
  code = mndInitSteps(pMnode);
544,163✔
962
  if (code != 0) {
544,163✔
963
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
964
    mndClose(pMnode);
×
965
    terrno = code;
×
966
    return NULL;
×
967
  }
968

969
  code = mndExecSteps(pMnode);
544,163✔
970
  if (code != 0) {
544,163✔
971
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
972
    mndClose(pMnode);
×
973
    terrno = code;
×
974
    return NULL;
×
975
  }
976

977
  mInfo("mnode open successfully");
544,163✔
978
  return pMnode;
544,163✔
979
}
980

981
void mndPreClose(SMnode *pMnode) {
543,985✔
982
  if (pMnode != NULL) {
543,985✔
983
    int32_t code = 0;
543,985✔
984
    // TODO check return value
985
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
543,985✔
986
    if (code < 0) {
543,985✔
UNCOV
987
      mError("failed to transfer leader since %s", tstrerror(code));
×
988
    }
989
    syncPreStop(pMnode->syncMgmt.sync);
543,985✔
990
    code = sdbWriteFile(pMnode->pSdb, 0);
543,985✔
991
    if (code < 0) {
543,985✔
992
      mError("failed to write sdb since %s", tstrerror(code));
831✔
993
    }
994
  }
995
}
543,985✔
996

997
void mndClose(SMnode *pMnode) {
544,095✔
998
  if (pMnode != NULL) {
544,095✔
999
    mInfo("start to close mnode");
544,095✔
1000
    mndCleanupSteps(pMnode, -1);
544,095✔
1001
    taosMemoryFreeClear(pMnode->path);
544,095✔
1002
    taosMemoryFreeClear(pMnode);
544,095✔
1003
    mInfo("mnode is closed");
544,095✔
1004
  }
1005
}
544,095✔
1006

1007
int32_t mndStart(SMnode *pMnode) {
543,985✔
1008
  int32_t code = 0;
543,985✔
1009
  mndSyncStart(pMnode);
543,985✔
1010
  if (pMnode->deploy) {
543,985✔
1011
    if (sdbDeploy(pMnode->pSdb) != 0) {
389,170✔
1012
      mError("failed to deploy sdb while start mnode");
×
1013
      return -1;
×
1014
    }
1015
    mndSetRestored(pMnode, true);
389,170✔
1016
  }
1017

1018
  if (sdbIsUpgraded(pMnode->pSdb)) {
543,985✔
1019
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
134,721✔
1020
  } else if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION) {
409,264✔
1021
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
409,264✔
1022
      mError("failed to upgrade sdb while start mnode");
×
1023
      return -1;
×
1024
    }
1025
    if (sdbIsUpgraded(pMnode->pSdb)) {
409,264✔
1026
      pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
8,790✔
1027
    }
1028
  }
1029

1030
#ifdef TD_ENTERPRISE
1031
  if (mndIsLeader(pMnode)) {
543,985✔
1032
    if (tsSodEnforceMode) {
487,563✔
1033
      if ((code = mndProcessEnforceSod(pMnode)) != 0) {
×
1034
        if (code == TSDB_CODE_MND_ROLE_NO_VALID_SYSDBA || code == TSDB_CODE_MND_ROLE_NO_VALID_SYSSEC ||
×
1035
            code == TSDB_CODE_MND_ROLE_NO_VALID_SYSAUDIT) {
1036
          mInfo("enter SoD pending mode. Enforce SoD by command line failed since %s", tstrerror(code));
×
1037
        } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
1038
          mInfo("enter SoD pending mode. Enforce SoD is in progress");
×
1039
        } else {
1040
          mError("failed to enforce SoD by command line since %s", tstrerror(code));
×
1041
          TAOS_RETURN(code);
×
1042
        }
1043
      } else {
1044
        mndSetSoDPhase(pMnode, TSDB_SOD_PHASE_STABLE);
×
1045
      }
1046
    }
1047
  }
1048
#endif
1049
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
543,985✔
1050

1051
  return mndInitTimer(pMnode);
543,985✔
1052
}
1053

1054
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
543,985✔
1055

1056
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
81,818✔
1057

1058
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
81,818✔
1059

1060
int32_t mndIsCatchUp(SMnode *pMnode) {
126,637✔
1061
  int64_t rid = pMnode->syncMgmt.sync;
126,637✔
1062
  return syncIsCatchUp(rid);
126,637✔
1063
}
1064

1065
ESyncRole mndGetRole(SMnode *pMnode) {
126,637✔
1066
  int64_t rid = pMnode->syncMgmt.sync;
126,637✔
1067
  return syncGetRole(rid);
126,637✔
1068
}
1069

1070
int64_t mndGetTerm(SMnode *pMnode) {
11,755,958✔
1071
  int64_t rid = pMnode->syncMgmt.sync;
11,755,958✔
1072
  return syncGetTerm(rid);
11,755,958✔
1073
}
1074

1075
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
29,305,497✔
1076

1077
void mndStop(SMnode *pMnode) {
543,985✔
1078
  mndSetStop(pMnode);
543,985✔
1079
  mndSyncStop(pMnode);
543,985✔
1080
  mndCleanupTimer(pMnode);
543,985✔
1081
}
543,985✔
1082

1083
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
71,642,772✔
1084
  SMnode    *pMnode = pMsg->info.node;
71,642,772✔
1085
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
71,642,772✔
1086

1087
  const STraceId *trace = &pMsg->info.traceId;
71,642,772✔
1088
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
71,642,553✔
1089

1090
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
71,642,553✔
1091
  if (code != 0) {
71,642,772✔
1092
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,058✔
1093
            tstrerror(code), code);
1094
  }
1095

1096
  return code;
71,642,772✔
1097
}
1098

1099
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
355,058,737✔
1100
  int32_t code = 0;
355,058,737✔
1101
  if (!IsReq(pMsg)) TAOS_RETURN(code);
355,058,737✔
1102
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
312,891,725✔
1103
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
306,148,209✔
1104
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
300,429,569✔
1105
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
287,126,496✔
1106
    TAOS_RETURN(code);
25,762,919✔
1107
  }
1108

1109
  SMnode *pMnode = pMsg->info.node;
287,119,105✔
1110
  (void)taosThreadRwlockRdlock(&pMnode->lock);
287,125,141✔
1111
  if (pMnode->stopped) {
287,133,309✔
1112
    (void)taosThreadRwlockUnlock(&pMnode->lock);
19,016✔
1113
    code = TSDB_CODE_APP_IS_STOPPING;
19,016✔
1114
    TAOS_RETURN(code);
19,016✔
1115
  }
1116

1117
  terrno = 0;
287,110,937✔
1118
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
287,111,548✔
1119
  if (terrno != 0) {
287,117,325✔
1120
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1121
    code = terrno;
×
1122
    TAOS_RETURN(code);
×
1123
  }
1124

1125
  if (state.state != TAOS_SYNC_STATE_LEADER) {
287,116,139✔
1126
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,544,538✔
1127
    code = TSDB_CODE_SYN_NOT_LEADER;
2,544,431✔
1128
    goto _OVER;
2,544,431✔
1129
  }
1130

1131
  if (!state.restored || !pMnode->restored) {
284,571,601✔
1132
    (void)taosThreadRwlockUnlock(&pMnode->lock);
420,613✔
1133
    code = TSDB_CODE_SYN_RESTORING;
417,230✔
1134
    goto _OVER;
417,230✔
1135
  }
1136

1137
#if 1
1138
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
284,152,203✔
1139
#else
1140
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1141
  mTrace("mnode rpc is acquired, ref:%d", ref);
1142
#endif
1143

1144
  (void)taosThreadRwlockUnlock(&pMnode->lock);
284,153,708✔
1145
  TAOS_RETURN(code);
284,153,586✔
1146

1147
_OVER:
2,961,661✔
1148
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,961,661✔
1149
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,960,262✔
1150
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,959,169✔
1151
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,959,596✔
1152
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,959,330✔
1153
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,958,551✔
1154
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,959,223✔
1155
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,959,330✔
1156
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER ||
2,957,830✔
1157
      pMsg->msgType == TDMT_MND_CLS_HB_TIMER) {
2,959,186✔
1158
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
2,988✔
1159
           pMnode->stopped, state.restored, syncStr(state.state));
1160
    TAOS_RETURN(code);
2,988✔
1161
  }
1162

1163
  const STraceId *trace = &pMsg->info.traceId;
2,957,974✔
1164
  SEpSet          epSet = {0};
2,958,103✔
1165
  mndGetMnodeEpSet(pMnode, &epSet);
2,957,171✔
1166

1167
  mGDebug(
2,959,289✔
1168
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1169
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1170
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1171
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1172

1173
  if (epSet.numOfEps <= 0) return -1;
2,959,289✔
1174

1175
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
10,742,950✔
1176
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
7,783,901✔
1177
  }
1178

1179
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,959,049✔
1180
  pMsg->info.rsp = rpcMallocCont(contLen);
2,959,289✔
1181
  if (pMsg->info.rsp != NULL) {
2,959,289✔
1182
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,959,289✔
1183
      mError("failed to serialize ep set");
×
1184
    }
1185
    pMsg->info.hasEpSet = 1;
2,957,803✔
1186
    pMsg->info.rspLen = contLen;
2,957,753✔
1187
  }
1188

1189
  TAOS_RETURN(code);
2,958,569✔
1190
}
1191

1192
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
355,071,625✔
1193
  SMnode         *pMnode = pMsg->info.node;
355,071,625✔
1194
  const STraceId *trace = &pMsg->info.traceId;
355,072,105✔
1195
  int32_t         code = TSDB_CODE_SUCCESS;
355,070,996✔
1196

1197
#ifdef TD_ENTERPRISE
1198
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
355,070,996✔
1199
    SCachedTokenInfo ti = {0};
21,936✔
1200
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
21,936✔
1201
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,049✔
1202
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
2,049✔
1203
      TAOS_RETURN(code);
2,049✔
1204
    }
1205
    if (ti.enabled == 0) {
19,887✔
1206
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
3,366✔
1207
      code = TSDB_CODE_MND_TOKEN_DISABLED;
3,366✔
1208
      TAOS_RETURN(code);
3,366✔
1209
    }
1210
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
16,521✔
1211
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1212
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1213
      TAOS_RETURN(code);
×
1214
    }
1215
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
16,521✔
1216
  }
1217
#endif
1218

1219
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
355,057,091✔
1220
  MndMsgFpExt fpExt = NULL;
355,063,001✔
1221
  if (fp == NULL) {
355,063,001✔
1222
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
25,763,637✔
1223
    if (fpExt == NULL) {
25,763,403✔
1224
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1225
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1226
      TAOS_RETURN(code);
×
1227
    }
1228
  }
1229

1230
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
355,062,767✔
1231

1232
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
352,080,267✔
1233
  if (fp)
352,082,264✔
1234
    code = (*fp)(pMsg);
326,318,642✔
1235
  else
1236
    code = (*fpExt)(pMsg, pQueueInfo);
25,763,622✔
1237
  mndReleaseRpc(pMnode);
352,085,149✔
1238

1239
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
352,086,202✔
1240
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
40,096,672✔
1241
  } else if (code == 0) {
311,989,530✔
1242
    mGTrace("msg:%p, successfully processed", pMsg);
293,618,998✔
1243
  } else {
1244
    // TODO removve this wrong set code
1245
    if (code == -1) {
18,370,532✔
1246
      code = terrno;
×
1247
    }
1248
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
18,370,532✔
1249
            TMSG_INFO(pMsg->msgType));
1250
  }
1251

1252
  TAOS_RETURN(code);
352,086,202✔
1253
}
1254

1255
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
131,687,446✔
1256
  tmsg_t type = TMSG_INDEX(msgType);
131,687,446✔
1257
  if (type < TDMT_MAX) {
131,687,446✔
1258
    pMnode->msgFp[type] = fp;
131,687,446✔
1259
  }
1260
}
131,687,446✔
1261

1262
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,353,304✔
1263
  tmsg_t type = TMSG_INDEX(msgType);
4,353,304✔
1264
  if (type < TDMT_MAX) {
4,353,304✔
1265
    pMnode->msgFpExt[type] = fp;
4,353,304✔
1266
  }
1267
}
4,353,304✔
1268

1269
// Note: uid 0 is reserved
1270
int64_t mndGenerateUid(const char *name, int32_t len) {
13,820,210✔
1271
  int32_t hashval = MurmurHash3_32(name, len);
13,820,210✔
1272
  do {
×
1273
    int64_t us = taosGetTimestampUs();
13,820,224✔
1274
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
13,820,224✔
1275
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
13,820,224✔
1276
    if (uuid) {
13,820,224✔
1277
      return llabs(uuid);
13,820,224✔
1278
    }
1279
  } while (true);
1280
}
1281

1282
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
100✔
1283
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1284
  int32_t code = mndAcquireRpc(pMnode);
100✔
1285
  if (code < 0) {
100✔
1286
    TAOS_RETURN(code);
×
1287
  } else if (code == 1) {
100✔
1288
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1289
  }
1290

1291
  SSdb   *pSdb = pMnode->pSdb;
100✔
1292
  int64_t ms = taosGetTimestampMs();
100✔
1293

1294
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
100✔
1295
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
100✔
1296
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
100✔
1297
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
100✔
1298
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
100✔
1299
      pStbInfo->stbs == NULL) {
100✔
1300
    mndReleaseRpc(pMnode);
×
1301
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1302
    if (terrno != 0) code = terrno;
×
1303
    TAOS_RETURN(code);
×
1304
  }
1305

1306
  // cluster info
1307
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
100✔
1308
  pClusterInfo->monitor_interval = tsMonitorInterval;
100✔
1309
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
100✔
1310
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
100✔
1311
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
100✔
1312
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
100✔
1313
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
100✔
1314

1315
  void *pIter = NULL;
100✔
1316
  while (1) {
100✔
1317
    SDnodeObj *pObj = NULL;
200✔
1318
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
200✔
1319
    if (pIter == NULL) break;
200✔
1320

1321
    SMonDnodeDesc desc = {0};
100✔
1322
    desc.dnode_id = pObj->id;
100✔
1323
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
100✔
1324
    if (mndIsDnodeOnline(pObj, ms)) {
100✔
1325
      tstrncpy(desc.status, "ready", sizeof(desc.status));
100✔
1326
    } else {
1327
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1328
    }
1329
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
200✔
1330
      mError("failed put dnode into array, but continue at this monitor report")
×
1331
    }
1332
    sdbRelease(pSdb, pObj);
100✔
1333
  }
1334

1335
  pIter = NULL;
100✔
1336
  while (1) {
100✔
1337
    SMnodeObj *pObj = NULL;
200✔
1338
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
200✔
1339
    if (pIter == NULL) break;
200✔
1340

1341
    SMonMnodeDesc desc = {0};
100✔
1342
    desc.mnode_id = pObj->id;
100✔
1343
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
100✔
1344

1345
    if (pObj->id == pMnode->selfDnodeId) {
100✔
1346
      pClusterInfo->first_ep_dnode_id = pObj->id;
100✔
1347
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
100✔
1348
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1349
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
100✔
1350
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1351
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
100✔
1352
      desc.syncState = TAOS_SYNC_STATE_LEADER;
100✔
1353
    } else {
1354
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1355
      desc.syncState = pObj->syncState;
×
1356
    }
1357
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
200✔
1358
      mError("failed to put mnode into array, but continue at this monitor report");
×
1359
    }
1360
    sdbRelease(pSdb, pObj);
100✔
1361
  }
1362

1363
  // vgroup info
1364
  pIter = NULL;
100✔
1365
  while (1) {
200✔
1366
    SVgObj *pVgroup = NULL;
300✔
1367
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
300✔
1368
    if (pIter == NULL) break;
300✔
1369

1370
    if (pVgroup->mountVgId) {
200✔
1371
      sdbRelease(pSdb, pVgroup);
×
1372
      continue;
×
1373
    }
1374

1375
    pClusterInfo->vgroups_total++;
200✔
1376
    pClusterInfo->tbs_total += pVgroup->numOfTables;
200✔
1377

1378
    SMonVgroupDesc desc = {0};
200✔
1379
    desc.vgroup_id = pVgroup->vgId;
200✔
1380

1381
    SName name = {0};
200✔
1382
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
200✔
1383
    if (code < 0) {
200✔
1384
      mError("failed to get db name since %s", tstrerror(code));
×
1385
      sdbCancelFetch(pSdb, pIter);
×
1386
      sdbRelease(pSdb, pVgroup);
×
1387
      TAOS_RETURN(code);
×
1388
    }
1389
    (void)tNameGetDbName(&name, desc.database_name);
200✔
1390

1391
    desc.tables_num = pVgroup->numOfTables;
200✔
1392
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
200✔
1393
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
200✔
1394
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
400✔
1395
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
200✔
1396
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
200✔
1397
      pVnDesc->dnode_id = pVgid->dnodeId;
200✔
1398
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
200✔
1399
      pVnDesc->syncState = pVgid->syncState;
200✔
1400
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
200✔
1401
        tstrncpy(desc.status, "ready", sizeof(desc.status));
200✔
1402
        pClusterInfo->vgroups_alive++;
200✔
1403
      }
1404
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
200✔
1405
        pClusterInfo->vnodes_alive++;
200✔
1406
      }
1407
      pClusterInfo->vnodes_total++;
200✔
1408
    }
1409

1410
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
400✔
1411
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1412
    }
1413
    sdbRelease(pSdb, pVgroup);
200✔
1414
  }
1415

1416
  // stb info
1417
  pIter = NULL;
100✔
1418
  while (1) {
×
1419
    SStbObj *pStb = NULL;
100✔
1420
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
100✔
1421
    if (pIter == NULL) break;
100✔
1422

1423
    SMonStbDesc desc = {0};
×
1424

1425
    SName name1 = {0};
×
1426
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1427
    if (code < 0) {
×
1428
      mError("failed to get db name since %s", tstrerror(code));
×
1429
      sdbRelease(pSdb, pStb);
×
1430
      TAOS_RETURN(code);
×
1431
    }
1432
    (void)tNameGetDbName(&name1, desc.database_name);
×
1433

1434
    SName name2 = {0};
×
1435
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1436
    if (code < 0) {
×
1437
      mError("failed to get table name since %s", tstrerror(code));
×
1438
      sdbRelease(pSdb, pStb);
×
1439
      TAOS_RETURN(code);
×
1440
    }
1441
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1442

1443
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1444
      mError("failed to put stb into array, but continue at this monitor report");
×
1445
    }
1446
    sdbRelease(pSdb, pStb);
×
1447
  }
1448

1449
  // grant info
1450
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
100✔
1451
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
100✔
1452
  if (pMnode->grant.expireTimeMS == 0) {
100✔
1453
    pGrantInfo->expire_time = 0;
×
1454
    pGrantInfo->timeseries_total = 0;
×
1455
  }
1456

1457
  mndReleaseRpc(pMnode);
100✔
1458
  TAOS_RETURN(code);
100✔
1459
}
1460

1461
int32_t mndResetTimer(SMnode *pMnode){
×
1462
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1463
}
1464

1465
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
36,531,389✔
1466
  mTrace("mnode get load");
36,531,389✔
1467
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
36,531,389✔
1468
  pLoad->syncState = state.state;
36,531,389✔
1469
  pLoad->syncRestore = state.restored;
36,531,389✔
1470
  pLoad->syncTerm = state.term;
36,531,389✔
1471
  pLoad->roleTimeMs = state.roleTimeMs;
36,531,389✔
1472
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
36,531,389✔
1473
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1474
  return 0;
36,531,389✔
1475
}
1476

1477
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
11,626,028✔
1478
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
11,626,028✔
1479
  return state.roleTimeMs;
11,626,028✔
1480
}
1481

1482
void mndSetRestored(SMnode *pMnode, bool restored) {
543,985✔
1483
  if (restored) {
543,985✔
1484
    (void)taosThreadRwlockWrlock(&pMnode->lock);
543,985✔
1485
    pMnode->restored = true;
543,985✔
1486
    (void)taosThreadRwlockUnlock(&pMnode->lock);
543,985✔
1487
    mInfo("mnode set restored:%d", restored);
543,985✔
1488
  } else {
1489
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1490
    pMnode->restored = false;
×
1491
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1492
    mInfo("mnode set restored:%d", restored);
×
1493
    while (1) {
1494
      if (pMnode->rpcRef <= 0) break;
×
1495
      taosMsleep(3);
×
1496
    }
1497
  }
1498
}
543,985✔
1499

1500
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1501

1502
void mndSetStop(SMnode *pMnode) {
543,985✔
1503
  (void)taosThreadRwlockWrlock(&pMnode->lock);
543,985✔
1504
  pMnode->stopped = true;
543,985✔
1505
  (void)taosThreadRwlockUnlock(&pMnode->lock);
543,985✔
1506
  mInfo("mnode set stopped");
543,985✔
1507
}
543,985✔
1508

1509
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
740,103,530✔
1510

1511
void mndSetSoDPhase(SMnode *pMnode, int8_t phase) {
412✔
1512
  (void)taosThreadRwlockWrlock(&pMnode->lock);
412✔
1513
  pMnode->sodPhase = phase;
412✔
1514
  (void)taosThreadRwlockUnlock(&pMnode->lock);
412✔
1515
}
412✔
1516

1517
int8_t mndGetSoDPhase(SMnode *pMnode) {
87,527✔
1518
  int8_t result = TSDB_SOD_PHASE_STABLE;
87,527✔
1519
  (void)taosThreadRwlockRdlock(&pMnode->lock);
87,527✔
1520
  result = pMnode->sodPhase;
87,527✔
1521
  (void)taosThreadRwlockUnlock(&pMnode->lock);
87,527✔
1522
  if (result < TSDB_SOD_PHASE_STABLE || result > TSDB_SOD_PHASE_ENFORCE) {
87,527✔
1523
    mWarn("invalid SoD phase:%d, reset to stable", result);
×
1524
    result = TSDB_SOD_PHASE_STABLE;
×
1525
  }
1526
  return result;
87,527✔
1527
}
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