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

taosdata / TDengine / #5054

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

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%)

771 existing lines in 142 files now uncovered.

281543 of 383795 relevant lines covered (73.36%)

133389099.84 hits per line

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

79.81
/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) {
7,031,327✔
68
  int32_t code = 0;
7,031,327✔
69
  (void)taosThreadRwlockRdlock(&pMnode->lock);
7,031,327✔
70
  if (pMnode->stopped) {
7,031,327✔
71
    code = TSDB_CODE_APP_IS_STOPPING;
×
72
  } else if (!mndIsLeader(pMnode)) {
7,031,327✔
73
    code = 1;
×
74
  } else {
75
#if 1
76
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
7,031,327✔
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);
7,031,327✔
83
  TAOS_RETURN(code);
7,031,327✔
84
}
85

86
static inline void mndReleaseRpc(SMnode *pMnode) {
361,382,207✔
87
  (void)taosThreadRwlockRdlock(&pMnode->lock);
361,382,207✔
88
#if 1
89
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
361,385,041✔
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);
361,385,418✔
95
}
361,384,431✔
96

97
static void *mndBuildTimerMsg(int32_t *pContLen) {
91,829,788✔
98
  terrno = 0;
91,829,788✔
99
  SMTimerReq timerReq = {0};
91,829,788✔
100

101
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
91,829,788✔
102
  if (contLen <= 0) return NULL;
91,829,102✔
103
  void *pReq = rpcMallocCont(contLen);
91,829,102✔
104
  if (pReq == NULL) return NULL;
91,829,079✔
105

106
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
91,829,079✔
107
    mError("failed to serialize timer msg since %s", terrstr());
×
108
  }
109
  *pContLen = contLen;
91,829,102✔
110
  return pReq;
91,829,102✔
111
}
112

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

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

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

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

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

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

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

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

203
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,921,888✔
204
  mTrace("pullup trim query");
3,921,888✔
205
  int32_t contLen = 0;
3,921,888✔
206
  void   *pReq = mndBuildTimerMsg(&contLen);
3,921,888✔
207
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,921,888✔
208
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,921,888✔
209
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
210
  }
211
}
3,921,888✔
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) {
185,024✔
228
  mTrace("pullup update ssmigrate progress");
185,024✔
229
  int32_t contLen = 0;
185,024✔
230
  void   *pReq = mndBuildTimerMsg(&contLen);
185,024✔
231
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
185,024✔
232
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
185,024✔
233
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
234
  }
235
}
185,024✔
236

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

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

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

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

277
static void mndPullupGrant(SMnode *pMnode) {
1,974,617✔
278
  mTrace("pullup grant msg");
1,974,617✔
279
  int32_t contLen = 0;
1,974,617✔
280
  void   *pReq = mndBuildTimerMsg(&contLen);
1,974,617✔
281
  if (pReq != NULL) {
1,974,617✔
282
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,974,617✔
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,974,617✔
289
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
290
    }
291
  }
292
}
1,974,617✔
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) {
132,522✔
320
  mTrace("increate uptime");
132,522✔
321
  int32_t contLen = 0;
132,522✔
322
  void   *pReq = mndBuildTimerMsg(&contLen);
132,522✔
323
  if (pReq != NULL) {
132,522✔
324
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
132,522✔
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) {
132,522✔
331
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
332
    }
333
  }
334
}
132,522✔
335

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

339
  void *pIter = NULL;
150,641✔
340
  while (1) {
489,931✔
341
    SVgObj *pVgroup = NULL;
640,572✔
342
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
640,572✔
343
    if (pIter == NULL) break;
640,572✔
344

345
    bool stateChanged = false;
489,931✔
346
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,246,029✔
347
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
962,475✔
348
      if (pGid->dnodeId == dnodeId) {
962,475✔
349
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
206,377✔
350
          mInfo(
77,198✔
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;
77,198✔
356
          pGid->syncRestore = 0;
77,198✔
357
          pGid->syncCanRead = 0;
77,198✔
358
          pGid->startTimeMs = 0;
77,198✔
359
          pGid->learnerProgress = 0;
77,198✔
360
          pGid->snapSeq = -1;
77,198✔
361
          stateChanged = true;
77,198✔
362
        }
363
        break;
206,377✔
364
      }
365
    }
366

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

377
    sdbRelease(pSdb, pVgroup);
489,931✔
378
  }
379
}
150,641✔
380

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

385
  SSdb   *pSdb = pMnode->pSdb;
7,031,228✔
386
  int64_t curMs = taosGetTimestampMs();
7,031,228✔
387

388
  void *pIter = NULL;
7,031,228✔
389
  while (1) {
11,783,880✔
390
    SDnodeObj *pDnode = NULL;
18,815,108✔
391
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
18,815,108✔
392
    if (pIter == NULL) break;
18,815,108✔
393

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

400
    sdbRelease(pSdb, pDnode);
11,783,880✔
401
  }
402

403
  mndReleaseRpc(pMnode);
7,031,228✔
404
}
405

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

415
  if (state.state != TAOS_SYNC_STATE_LEADER) {
416,224,726✔
416
    (void)taosThreadRwlockUnlock(&pMnode->lock);
15,364,436✔
417
    terrno = TSDB_CODE_SYN_NOT_LEADER;
15,364,107✔
418
    return true;
15,364,436✔
419
  }
420
  if (!state.restored || !pMnode->restored) {
400,860,290✔
421
    (void)taosThreadRwlockUnlock(&pMnode->lock);
20,320✔
422
    terrno = TSDB_CODE_SYN_RESTORING;
18,119✔
423
    return true;
18,119✔
424
  }
425
  (void)taosThreadRwlockUnlock(&pMnode->lock);
400,840,064✔
426
  return false;
400,841,284✔
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) {
37,088,463✔
449
  int32_t code = 0;
37,088,463✔
450
#ifndef TD_ASTRA
451
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
37,088,463✔
452
    mndPullupGrant(pMnode);
1,974,617✔
453
  }
454
  if (sec % tsTtlPushIntervalSec == 0) {
37,088,463✔
455
    mndPullupTtl(pMnode);
3,855,194✔
456
  }
457

458
  if (sec % tsTrimVDbIntervalSec == 0) {
37,088,463✔
459
    mndPullupTrimDb(pMnode);
12,995✔
460
  }
461

462
  if (sec % tsQueryTrimIntervalSec == 0) {
37,088,463✔
463
    mndPullupQueryTrimDb(pMnode);
3,921,888✔
464
  }
465
#endif
466
#ifdef USE_SHARED_STORAGE
467
  if (tsSsEnabled) {
37,088,463✔
468
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
254,408✔
469
      mndPullupUpdateSsMigrateProgress(pMnode);
185,024✔
470
    }
471
    if (tsSsEnabled == 2) {
254,408✔
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) {
37,088,463✔
487
    if (sec % tsAuthReqHBInterval == 0) {
×
488
      mndPullupAuth(pMnode);
×
489
    }
490
  }
491
  if (tsClsEnabled || tsClsRefreshInterval == GRANT_CLS_CLOSING || tsClsRefreshInterval == GRANT_CLS_OPENING) {
37,088,463✔
492
    if (sec % tsClsRefreshInterval == 0) {
×
493
      mndPullupCls(pMnode);
×
494
    }
495
  }
496
#endif
497
  if (sec % tsTransPullupInterval == 0) {
37,088,463✔
498
    mndPullupTrans(pMnode);
18,543,789✔
499
  }
500

501
  if (sec % tsCompactPullupInterval == 0) {
37,088,463✔
502
    mndPullupCompacts(pMnode);
3,696,390✔
503
  }
504

505
  if (sec % tsScanPullupInterval == 0) {
37,088,463✔
506
    mndPullupScans(pMnode);
3,696,390✔
507
  }
508
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
37,088,463✔
509
    mndPullupInstances(pMnode);
7,413,100✔
510
  }
511
#ifdef USE_TOPIC
512
  if (sec % tsMqRebalanceInterval == 0) {
37,088,463✔
513
    mndCalMqRebalance(pMnode);
18,543,667✔
514
  }
515
#endif
516
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
37,088,463✔
517
    mndPullupTelem(pMnode);
294✔
518
  }
519
  if (sec % tsUptimeInterval == 0) {
37,088,463✔
520
    mndIncreaseUpTime(pMnode);
132,522✔
521
  }
522

523
  if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION && sec % UPGRADE_INTERVAL == 0) {
37,088,463✔
524
    mndPullupUpgradeSdb(pMnode);
378,906✔
525
  }
526
}
37,088,463✔
527

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

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

545
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
363,754,274✔
546
  if (ms % (tsStatusTimeoutMs) == 0) {
363,754,274✔
547
    mndCheckDnodeOffline(pMnode);
7,031,228✔
548
  }
549
}
363,754,274✔
550

551
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
37,088,463✔
552
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
37,088,463✔
553
    mndSyncCheckTimeout(pMnode);
1,237,864✔
554
  }
555
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
37,088,463✔
556
    msmHealthCheck(pMnode);
12,357,758✔
557
  }
558
}
37,088,463✔
559

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

566
  while (1) {
378,295,538✔
567
    if (mndGetStop(pMnode)) break;
378,838,961✔
568

569
    int64_t nowSec = taosGetTimestampMs() / 1000;
378,295,538✔
570
    if (nowSec == lastSec) {
378,295,538✔
571
      taosMsleep(100);
339,748,084✔
572
      continue;
339,748,084✔
573
    }
574
    lastSec = nowSec;
38,547,454✔
575

576
    if (mnodeIsNotLeader(pMnode)) {
38,547,454✔
577
      taosMsleep(100);
1,458,991✔
578
      mTrace("timer not process since mnode is not leader");
1,458,991✔
579
      continue;
1,458,991✔
580
    }
581

582
    mndDoTimerCheckSync(pMnode, nowSec);
37,088,463✔
583

584
    mndDoTimerPullupTask(pMnode, nowSec);
37,088,463✔
585

586
    taosMsleep(100);
37,088,463✔
587
  }
588

589
  return NULL;
543,423✔
590
}
591

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

598
  while (1) {
599
    lastTime += 100;
378,221,261✔
600
    taosMsleep(100);
378,221,261✔
601

602
    if (mndGetStop(pMnode)) break;
378,221,261✔
603
    if (lastTime % 10 != 0) continue;
377,677,838✔
604

605
    if (mnodeIsNotLeader(pMnode)) {
377,677,838✔
606
      mTrace("timer not process since mnode is not leader");
13,923,564✔
607
      continue;
13,923,564✔
608
    }
609

610
    mndDoTimerCheckStatus(pMnode, lastTime);
363,754,274✔
611

612
    mndDoArbTimerPullupTask(pMnode, lastTime);
363,754,274✔
613
  }
614

615
  return NULL;
543,423✔
616
}
617

618
static int32_t mndInitTimer(SMnode *pMnode) {
543,423✔
619
  int32_t      code = 0;
543,423✔
620
  TdThreadAttr thAttr;
542,441✔
621
  (void)taosThreadAttrInit(&thAttr);
543,423✔
622
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
543,423✔
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,423✔
627
    mError("failed to create timer thread since %s", tstrerror(code));
×
628
    TAOS_RETURN(code);
×
629
  }
630

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

634
  TdThreadAttr arbAttr;
542,441✔
635
  (void)taosThreadAttrInit(&arbAttr);
543,423✔
636
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
543,423✔
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,423✔
641
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
642
    TAOS_RETURN(code);
×
643
  }
644

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

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

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

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

674
  TAOS_RETURN(code);
543,601✔
675
}
676

677
static int32_t mndInitWal(SMnode *pMnode) {
543,601✔
678
  int32_t code = 0;
543,601✔
679
  char    path[PATH_MAX + 20] = {0};
543,601✔
680
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
543,601✔
681
  SWalCfg cfg = {.vgId = 1,
543,601✔
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) {
543,601✔
694
    code = terrno;
×
695
    TAOS_RETURN(code);
×
696
  }
697
  if (tsMetaKey[0] != '\0') {
543,601✔
698
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
5,980✔
699
  }
700
#endif
701

702
  pMnode->pWal = walOpen(path, &cfg);
543,601✔
703
  if (pMnode->pWal == NULL) {
543,601✔
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);
543,601✔
711
}
712

713
static void mndCloseWal(SMnode *pMnode) {
543,532✔
714
  if (pMnode->pWal != NULL) {
543,532✔
715
    walClose(pMnode->pWal);
543,532✔
716
    pMnode->pWal = NULL;
543,532✔
717
  }
718
}
543,532✔
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,567✔
726
  SMnode *pMnode = (SMnode *)param;
8,567✔
727
  if (pMnode == NULL) {
8,567✔
728
    return TSDB_CODE_INVALID_PARA;
×
729
  }
730
  
731
  mInfo("persisting encrypted flag to mnode.json");
8,567✔
732
  
733
  SMnodeOpt option = {0};
8,567✔
734
  int32_t code = mmReadFile(pMnode->path, &option);
8,567✔
735
  if (code != 0) {
8,567✔
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,567✔
741
  code = mmWriteFile(pMnode->path, &option);
8,567✔
742
  if (code != 0) {
8,567✔
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,567✔
749
  
750
  mInfo("successfully persisted encrypted flag to mnode.json");
8,567✔
751
  return 0;
8,567✔
752
}
753

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

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

768
  TAOS_RETURN(code);
543,601✔
769
}
770

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

780
  if (!pMnode->deploy) {
543,601✔
781
    code = sdbReadFile(pMnode->pSdb);
154,956✔
782
  }
783

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

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

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

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

806
  TAOS_RETURN(0);
27,723,651✔
807
}
808

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

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

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

873
  for (int32_t s = pos; s >= 0; s--) {
28,263,664✔
874
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
27,720,132✔
875
    mInfo("%s will cleanup", pStep->name);
27,720,132✔
876
    if (pStep->cleanupFp != NULL) {
27,720,132✔
877
      (*pStep->cleanupFp)(pMnode);
26,633,068✔
878
    }
879
  }
880

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

886
static int32_t mndExecSteps(SMnode *pMnode) {
543,601✔
887
  int32_t code = 0;
543,601✔
888
  int32_t size = taosArrayGetSize(pMnode->pSteps);
543,601✔
889
  for (int32_t pos = 0; pos < size; pos++) {
28,267,252✔
890
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
27,723,651✔
891
    if (pStep->initFp == NULL) continue;
27,723,651✔
892

893
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
27,723,651✔
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,723,651✔
899
      tmsgReportStartup(pStep->name, "initialized");
27,723,651✔
900
    }
901
  }
902

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

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

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

923
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
543,601✔
924
  if (pMnode == NULL) {
543,601✔
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));
543,601✔
930

931
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
543,601✔
932
  if (code != 0) {
543,601✔
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,
543,601✔
940
        pOption->numOfTotalReplicas);
941
  mndSetOptions(pMnode, pOption);
543,601✔
942

943
  pMnode->deploy = pOption->deploy;
543,601✔
944
  pMnode->version = pOption->version;
543,601✔
945
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
543,601✔
946
  if (pMnode->pSteps == NULL) {
543,601✔
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);
543,601✔
954
  if (code != 0) {
543,601✔
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);
543,601✔
962
  if (code != 0) {
543,601✔
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);
543,601✔
970
  if (code != 0) {
543,601✔
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");
543,601✔
978
  return pMnode;
543,601✔
979
}
980

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

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

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

1018
  if (sdbIsUpgraded(pMnode->pSdb)) {
543,423✔
1019
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
134,672✔
1020
  } else if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION) {
408,751✔
1021
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
408,751✔
1022
      mError("failed to upgrade sdb while start mnode");
×
1023
      return -1;
×
1024
    }
1025
    if (sdbIsUpgraded(pMnode->pSdb)) {
408,751✔
1026
      pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
5,902✔
1027
    }
1028
  }
1029

1030
#ifdef TD_ENTERPRISE
1031
  if (mndIsLeader(pMnode)) {
543,423✔
1032
    if (tsSodEnforceMode) {
486,946✔
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,423✔
1050

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

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

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

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

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

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

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

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

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

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

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

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

1096
  return code;
72,772,117✔
1097
}
1098

1099
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
359,544,851✔
1100
  int32_t code = 0;
359,544,851✔
1101
  if (!IsReq(pMsg)) TAOS_RETURN(code);
359,544,851✔
1102
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
317,537,781✔
1103
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
310,664,336✔
1104
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
304,803,448✔
1105
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
291,223,715✔
1106
    TAOS_RETURN(code);
26,306,456✔
1107
  }
1108

1109
  SMnode *pMnode = pMsg->info.node;
291,215,848✔
1110
  (void)taosThreadRwlockRdlock(&pMnode->lock);
291,219,111✔
1111
  if (pMnode->stopped) {
291,227,361✔
1112
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,644✔
1113
    code = TSDB_CODE_APP_IS_STOPPING;
3,644✔
1114
    TAOS_RETURN(code);
3,644✔
1115
  }
1116

1117
  terrno = 0;
291,217,959✔
1118
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
291,218,869✔
1119
  if (terrno != 0) {
291,228,473✔
1120
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1121
    code = terrno;
×
1122
    TAOS_RETURN(code);
×
1123
  }
1124

1125
  if (state.state != TAOS_SYNC_STATE_LEADER) {
291,225,994✔
1126
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,035,611✔
1127
    code = TSDB_CODE_SYN_NOT_LEADER;
3,035,151✔
1128
    goto _OVER;
3,035,151✔
1129
  }
1130

1131
  if (!state.restored || !pMnode->restored) {
288,190,383✔
1132
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,168,067✔
1133
    code = TSDB_CODE_SYN_RESTORING;
2,163,891✔
1134
    goto _OVER;
2,163,891✔
1135
  }
1136

1137
#if 1
1138
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
286,022,885✔
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);
286,028,865✔
1145
  TAOS_RETURN(code);
286,023,352✔
1146

1147
_OVER:
5,199,042✔
1148
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,199,042✔
1149
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,199,502✔
1150
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,199,153✔
1151
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,199,153✔
1152
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,199,042✔
1153
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,198,446✔
1154
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,199,145✔
1155
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,198,927✔
1156
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER ||
5,198,927✔
1157
      pMsg->msgType == TDMT_MND_CLS_HB_TIMER) {
5,199,030✔
UNCOV
1158
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1159
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1160
    TAOS_RETURN(code);
×
1161
  }
1162

1163
  const STraceId *trace = &pMsg->info.traceId;
5,198,339✔
1164
  SEpSet          epSet = {0};
5,197,048✔
1165
  mndGetMnodeEpSet(pMnode, &epSet);
5,198,201✔
1166

1167
  mGDebug(
5,199,502✔
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;
5,199,502✔
1174

1175
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
16,287,987✔
1176
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
11,088,834✔
1177
  }
1178

1179
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,199,153✔
1180
  pMsg->info.rsp = rpcMallocCont(contLen);
5,197,376✔
1181
  if (pMsg->info.rsp != NULL) {
5,198,351✔
1182
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,198,567✔
1183
      mError("failed to serialize ep set");
×
1184
    }
1185
    pMsg->info.hasEpSet = 1;
5,195,479✔
1186
    pMsg->info.rspLen = contLen;
5,196,056✔
1187
  }
1188

1189
  TAOS_RETURN(code);
5,197,507✔
1190
}
1191

1192
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
359,559,498✔
1193
  SMnode         *pMnode = pMsg->info.node;
359,559,498✔
1194
  const STraceId *trace = &pMsg->info.traceId;
359,561,294✔
1195
  int32_t         code = TSDB_CODE_SUCCESS;
359,557,987✔
1196

1197
#ifdef TD_ENTERPRISE
1198
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
359,557,987✔
1199
    SCachedTokenInfo ti = {0};
22,238✔
1200
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
22,238✔
1201
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,737✔
1202
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,737✔
1203
      TAOS_RETURN(code);
1,737✔
1204
    }
1205
    if (ti.enabled == 0) {
20,501✔
1206
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
3,474✔
1207
      code = TSDB_CODE_MND_TOKEN_DISABLED;
3,474✔
1208
      TAOS_RETURN(code);
3,474✔
1209
    }
1210
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
17,027✔
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));
17,027✔
1216
  }
1217
#endif
1218

1219
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
359,546,079✔
1220
  MndMsgFpExt fpExt = NULL;
359,550,954✔
1221
  if (fp == NULL) {
359,550,954✔
1222
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
26,317,692✔
1223
    if (fpExt == NULL) {
26,317,006✔
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));
359,550,268✔
1231

1232
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
354,346,271✔
1233
  if (fp)
354,346,017✔
1234
    code = (*fp)(pMsg);
328,028,325✔
1235
  else
1236
    code = (*fpExt)(pMsg, pQueueInfo);
26,317,692✔
1237
  mndReleaseRpc(pMnode);
354,351,613✔
1238

1239
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
354,353,449✔
1240
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
40,722,753✔
1241
  } else if (code == 0) {
313,630,696✔
1242
    mGTrace("msg:%p, successfully processed", pMsg);
297,962,032✔
1243
  } else {
1244
    // TODO removve this wrong set code
1245
    if (code == -1) {
15,668,664✔
1246
      code = terrno;
×
1247
    }
1248
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
15,668,664✔
1249
            TMSG_INFO(pMsg->msgType));
1250
  }
1251

1252
  TAOS_RETURN(code);
354,353,885✔
1253
}
1254

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

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

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

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

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

1294
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
99✔
1295
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
99✔
1296
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
99✔
1297
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
99✔
1298
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
99✔
1299
      pStbInfo->stbs == NULL) {
99✔
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));
99✔
1308
  pClusterInfo->monitor_interval = tsMonitorInterval;
99✔
1309
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
99✔
1310
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
99✔
1311
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
99✔
1312
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
99✔
1313
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
99✔
1314

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

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

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

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

1345
    if (pObj->id == pMnode->selfDnodeId) {
99✔
1346
      pClusterInfo->first_ep_dnode_id = pObj->id;
99✔
1347
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
99✔
1348
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1349
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
99✔
1350
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1351
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
99✔
1352
      desc.syncState = TAOS_SYNC_STATE_LEADER;
99✔
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) {
198✔
1358
      mError("failed to put mnode into array, but continue at this monitor report");
×
1359
    }
1360
    sdbRelease(pSdb, pObj);
99✔
1361
  }
1362

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

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

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

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

1381
    SName name = {0};
198✔
1382
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
198✔
1383
    if (code < 0) {
198✔
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);
198✔
1390

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

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

1416
  // stb info
1417
  pIter = NULL;
99✔
1418
  while (1) {
×
1419
    SStbObj *pStb = NULL;
99✔
1420
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
99✔
1421
    if (pIter == NULL) break;
99✔
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;
99✔
1451
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
99✔
1452
  if (pMnode->grant.expireTimeMS == 0) {
99✔
1453
    pGrantInfo->expire_time = 0;
×
1454
    pGrantInfo->timeseries_total = 0;
×
1455
  }
1456

1457
  mndReleaseRpc(pMnode);
99✔
1458
  TAOS_RETURN(code);
99✔
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) {
37,553,234✔
1466
  mTrace("mnode get load");
37,553,234✔
1467
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
37,553,234✔
1468
  pLoad->syncState = state.state;
37,553,234✔
1469
  pLoad->syncRestore = state.restored;
37,553,234✔
1470
  pLoad->syncTerm = state.term;
37,553,234✔
1471
  pLoad->roleTimeMs = state.roleTimeMs;
37,553,234✔
1472
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
37,553,234✔
1473
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1474
  return 0;
37,553,234✔
1475
}
1476

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

1482
void mndSetRestored(SMnode *pMnode, bool restored) {
543,294✔
1483
  if (restored) {
543,294✔
1484
    (void)taosThreadRwlockWrlock(&pMnode->lock);
543,294✔
1485
    pMnode->restored = true;
543,294✔
1486
    (void)taosThreadRwlockUnlock(&pMnode->lock);
543,294✔
1487
    mInfo("mnode set restored:%d", restored);
543,294✔
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,294✔
1499

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

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

1509
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
757,052,434✔
1510

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

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