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

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

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

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

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

86
static inline void mndReleaseRpc(SMnode *pMnode) {
353,485,975✔
87
  (void)taosThreadRwlockRdlock(&pMnode->lock);
353,485,975✔
88
#if 1
89
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
353,487,190✔
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);
353,487,768✔
95
}
353,488,380✔
96

97
static void *mndBuildTimerMsg(int32_t *pContLen) {
90,215,770✔
98
  terrno = 0;
90,215,770✔
99
  SMTimerReq timerReq = {0};
90,216,448✔
100

101
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
90,216,448✔
102
  if (contLen <= 0) return NULL;
90,215,448✔
103
  void *pReq = rpcMallocCont(contLen);
90,215,448✔
104
  if (pReq == NULL) return NULL;
90,216,448✔
105

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

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

126
static void mndPullupUpgradeSdb(SMnode *pMnode) {
369,944✔
127
  if (sdbIsUpgraded(pMnode->pSdb)) {
369,944✔
128
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
328,914✔
129
    return;
328,914✔
130
  }
131

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

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

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

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

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

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

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

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

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

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

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

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

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

339
  void *pIter = NULL;
149,910✔
340
  while (1) {
461,365✔
341
    SVgObj *pVgroup = NULL;
611,275✔
342
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
611,275✔
343
    if (pIter == NULL) break;
611,275✔
344

345
    bool stateChanged = false;
461,365✔
346
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,140,992✔
347
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
873,231✔
348
      if (pGid->dnodeId == dnodeId) {
873,231✔
349
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
193,604✔
350
          mInfo(
78,896✔
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;
78,896✔
356
          pGid->syncRestore = 0;
78,896✔
357
          pGid->syncCanRead = 0;
78,896✔
358
          pGid->startTimeMs = 0;
78,896✔
359
          pGid->learnerProgress = 0;
78,896✔
360
          pGid->snapSeq = -1;
78,896✔
361
          stateChanged = true;
78,896✔
362
        }
363
        break;
193,604✔
364
      }
365
    }
366

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

377
    sdbRelease(pSdb, pVgroup);
461,365✔
378
  }
379
}
149,910✔
380

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

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

388
  void *pIter = NULL;
6,891,165✔
389
  while (1) {
11,394,215✔
390
    SDnodeObj *pDnode = NULL;
18,285,380✔
391
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
18,285,380✔
392
    if (pIter == NULL) break;
18,285,380✔
393

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

400
    sdbRelease(pSdb, pDnode);
11,394,215✔
401
  }
402

403
  mndReleaseRpc(pMnode);
6,891,165✔
404
}
405

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

415
  if (state.state != TAOS_SYNC_STATE_LEADER) {
408,127,274✔
416
    (void)taosThreadRwlockUnlock(&pMnode->lock);
15,009,748✔
417
    terrno = TSDB_CODE_SYN_NOT_LEADER;
15,009,654✔
418
    return true;
15,009,748✔
419
  }
420
  if (!state.restored || !pMnode->restored) {
393,117,526✔
421
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,712✔
422
    terrno = TSDB_CODE_SYN_RESTORING;
4,685✔
423
    return true;
4,685✔
424
  }
425
  (void)taosThreadRwlockUnlock(&pMnode->lock);
393,110,869✔
426
  return false;
393,112,145✔
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,453,738✔
449
  int32_t code = 0;
36,453,738✔
450
#ifndef TD_ASTRA
451
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
36,453,738✔
452
    mndPullupGrant(pMnode);
1,961,247✔
453
  }
454
  if (sec % tsTtlPushIntervalSec == 0) {
36,453,738✔
455
    mndPullupTtl(pMnode);
3,805,169✔
456
  }
457

458
  if (sec % tsTrimVDbIntervalSec == 0) {
36,453,738✔
459
    mndPullupTrimDb(pMnode);
9,663✔
460
  }
461

462
  if (sec % tsQueryTrimIntervalSec == 0) {
36,453,738✔
463
    mndPullupQueryTrimDb(pMnode);
3,864,272✔
464
  }
465
#endif
466
#ifdef USE_SHARED_STORAGE
467
  if (tsSsEnabled) {
36,453,738✔
468
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
261,525✔
469
      mndPullupUpdateSsMigrateProgress(pMnode);
189,750✔
470
    }
471
    if (tsSsEnabled == 2) {
261,525✔
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,453,738✔
487
    if (sec % tsAuthReqHBInterval == 0) {
×
488
      mndPullupAuth(pMnode);
×
489
    }
490
  }
491
  if (tsClsEnabled || tsClsRefreshInterval == GRANT_CLS_CLOSING || tsClsRefreshInterval == GRANT_CLS_OPENING) {
36,453,738✔
492
    if (sec % tsClsRefreshInterval == 0) {
×
493
      mndPullupCls(pMnode);
×
494
    }
495
  }
496
#endif
497
  if (sec % tsTransPullupInterval == 0) {
36,453,738✔
498
    mndPullupTrans(pMnode);
18,217,677✔
499
  }
500

501
  if (sec % tsCompactPullupInterval == 0) {
36,453,738✔
502
    mndPullupCompacts(pMnode);
3,632,353✔
503
  }
504

505
  if (sec % tsScanPullupInterval == 0) {
36,453,738✔
506
    mndPullupScans(pMnode);
3,632,447✔
507
  }
508
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
36,453,738✔
509
    mndPullupInstances(pMnode);
7,296,263✔
510
  }
511
#ifdef USE_TOPIC
512
  if (sec % tsMqRebalanceInterval == 0) {
36,453,738✔
513
    mndCalMqRebalance(pMnode);
18,217,476✔
514
  }
515
#endif
516
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
36,453,738✔
517
    mndPullupTelem(pMnode);
246✔
518
  }
519
  if (sec % tsUptimeInterval == 0) {
36,453,738✔
520
    mndIncreaseUpTime(pMnode);
122,547✔
521
  }
522

523
  if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION && sec % UPGRADE_INTERVAL == 0) {
36,453,738✔
524
    mndPullupUpgradeSdb(pMnode);
369,944✔
525
  }
526
}
36,453,738✔
527

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

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

545
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
356,659,820✔
546
  if (ms % (tsStatusTimeoutMs) == 0) {
356,659,820✔
547
    mndCheckDnodeOffline(pMnode);
6,891,165✔
548
  }
549
}
356,659,820✔
550

551
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
36,453,738✔
552
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
36,453,738✔
553
    mndSyncCheckTimeout(pMnode);
1,220,344✔
554
  }
555
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
36,453,738✔
556
    msmHealthCheck(pMnode);
12,155,930✔
557
  }
558
}
36,453,738✔
559

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

566
  while (1) {
370,738,535✔
567
    if (mndGetStop(pMnode)) break;
371,269,502✔
568

569
    int64_t nowSec = taosGetTimestampMs() / 1000;
370,738,535✔
570
    if (nowSec == lastSec) {
370,738,535✔
571
      taosMsleep(100);
332,858,983✔
572
      continue;
332,858,983✔
573
    }
574
    lastSec = nowSec;
37,879,552✔
575

576
    if (mnodeIsNotLeader(pMnode)) {
37,879,552✔
577
      taosMsleep(100);
1,425,814✔
578
      mTrace("timer not process since mnode is not leader");
1,425,814✔
579
      continue;
1,425,814✔
580
    }
581

582
    mndDoTimerCheckSync(pMnode, nowSec);
36,453,738✔
583

584
    mndDoTimerPullupTask(pMnode, nowSec);
36,453,738✔
585

586
    taosMsleep(100);
36,453,738✔
587
  }
588

589
  return NULL;
530,967✔
590
}
591

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

598
  while (1) {
599
    lastTime += 100;
370,779,406✔
600
    taosMsleep(100);
370,779,406✔
601

602
    if (mndGetStop(pMnode)) break;
370,779,406✔
603
    if (lastTime % 10 != 0) continue;
370,248,439✔
604

605
    if (mnodeIsNotLeader(pMnode)) {
370,248,439✔
606
      mTrace("timer not process since mnode is not leader");
13,588,619✔
607
      continue;
13,588,619✔
608
    }
609

610
    mndDoTimerCheckStatus(pMnode, lastTime);
356,659,820✔
611

612
    mndDoArbTimerPullupTask(pMnode, lastTime);
356,659,820✔
613
  }
614

615
  return NULL;
530,967✔
616
}
617

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

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

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

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

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

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

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

674
  TAOS_RETURN(code);
531,129✔
675
}
676

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

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

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

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

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

768
  TAOS_RETURN(code);
531,129✔
769
}
770

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

780
  if (!pMnode->deploy) {
531,129✔
781
    code = sdbReadFile(pMnode->pSdb);
149,846✔
782
  }
783

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

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

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

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

806
  TAOS_RETURN(0);
27,087,579✔
807
}
808

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

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

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

873
  for (int32_t s = pos; s >= 0; s--) {
27,615,328✔
874
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
27,084,264✔
875
    mInfo("%s will cleanup", pStep->name);
27,084,264✔
876
    if (pStep->cleanupFp != NULL) {
27,084,264✔
877
      (*pStep->cleanupFp)(pMnode);
26,022,136✔
878
    }
879
  }
880

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

886
static int32_t mndExecSteps(SMnode *pMnode) {
531,129✔
887
  int32_t code = 0;
531,129✔
888
  int32_t size = taosArrayGetSize(pMnode->pSteps);
531,129✔
889
  for (int32_t pos = 0; pos < size; pos++) {
27,618,708✔
890
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
27,087,579✔
891
    if (pStep->initFp == NULL) continue;
27,087,579✔
892

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

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

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

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

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

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

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

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

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

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

1018
  if (sdbIsUpgraded(pMnode->pSdb)) {
530,967✔
1019
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
130,617✔
1020
  } else if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION) {
400,350✔
1021
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
400,350✔
1022
      mError("failed to upgrade sdb while start mnode");
×
1023
      return -1;
×
1024
    }
1025
    if (sdbIsUpgraded(pMnode->pSdb)) {
400,350✔
1026
      pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
6,798✔
1027
    }
1028
  }
1029

1030
#ifdef TD_ENTERPRISE
1031
  if (mndIsLeader(pMnode)) {
530,967✔
1032
    if (tsSodEnforceMode) {
477,796✔
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);
530,967✔
1050

1051
  return mndInitTimer(pMnode);
530,967✔
1052
}
1053

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

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

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

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

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

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

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

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

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

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

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

1096
  return code;
70,308,884✔
1097
}
1098

1099
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
349,917,373✔
1100
  int32_t code = 0;
349,917,373✔
1101
  if (!IsReq(pMsg)) TAOS_RETURN(code);
349,917,373✔
1102
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
308,493,965✔
1103
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
301,894,374✔
1104
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
296,297,443✔
1105
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
283,267,649✔
1106
    TAOS_RETURN(code);
25,235,945✔
1107
  }
1108

1109
  SMnode *pMnode = pMsg->info.node;
283,258,862✔
1110
  (void)taosThreadRwlockRdlock(&pMnode->lock);
283,272,901✔
1111
  if (pMnode->stopped) {
283,265,898✔
1112
    (void)taosThreadRwlockUnlock(&pMnode->lock);
7,708✔
1113
    code = TSDB_CODE_APP_IS_STOPPING;
7,708✔
1114
    TAOS_RETURN(code);
7,708✔
1115
  }
1116

1117
  terrno = 0;
283,262,417✔
1118
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
283,262,103✔
1119
  if (terrno != 0) {
283,268,333✔
1120
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1121
    code = terrno;
×
1122
    TAOS_RETURN(code);
×
1123
  }
1124

1125
  if (state.state != TAOS_SYNC_STATE_LEADER) {
283,266,970✔
1126
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,745,920✔
1127
    code = TSDB_CODE_SYN_NOT_LEADER;
2,745,920✔
1128
    goto _OVER;
2,745,920✔
1129
  }
1130

1131
  if (!state.restored || !pMnode->restored) {
280,521,050✔
1132
    (void)taosThreadRwlockUnlock(&pMnode->lock);
581,916✔
1133
    code = TSDB_CODE_SYN_RESTORING;
578,809✔
1134
    goto _OVER;
578,809✔
1135
  }
1136

1137
#if 1
1138
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
279,939,752✔
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);
279,938,772✔
1145
  TAOS_RETURN(code);
279,936,976✔
1146

1147
_OVER:
3,324,729✔
1148
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,324,729✔
1149
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,324,710✔
1150
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,324,829✔
1151
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,324,509✔
1152
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,324,816✔
1153
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,324,478✔
1154
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,324,707✔
1155
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,324,593✔
1156
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER ||
3,324,673✔
1157
      pMsg->msgType == TDMT_MND_CLS_HB_TIMER) {
3,324,804✔
1158
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
199✔
1159
           pMnode->stopped, state.restored, syncStr(state.state));
1160
    TAOS_RETURN(code);
199✔
1161
  }
1162

1163
  const STraceId *trace = &pMsg->info.traceId;
3,323,703✔
1164
  SEpSet          epSet = {0};
3,324,274✔
1165
  mndGetMnodeEpSet(pMnode, &epSet);
3,324,228✔
1166

1167
  mGDebug(
3,324,625✔
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;
3,324,933✔
1174

1175
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
11,876,239✔
1176
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
8,551,306✔
1177
  }
1178

1179
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,324,933✔
1180
  pMsg->info.rsp = rpcMallocCont(contLen);
3,323,348✔
1181
  if (pMsg->info.rsp != NULL) {
3,324,180✔
1182
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,324,651✔
1183
      mError("failed to serialize ep set");
×
1184
    }
1185
    pMsg->info.hasEpSet = 1;
3,323,252✔
1186
    pMsg->info.rspLen = contLen;
3,323,350✔
1187
  }
1188

1189
  TAOS_RETURN(code);
3,323,378✔
1190
}
1191

1192
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
349,931,214✔
1193
  SMnode         *pMnode = pMsg->info.node;
349,931,214✔
1194
  const STraceId *trace = &pMsg->info.traceId;
349,931,833✔
1195
  int32_t         code = TSDB_CODE_SUCCESS;
349,932,452✔
1196

1197
#ifdef TD_ENTERPRISE
1198
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
349,932,452✔
1199
    SCachedTokenInfo ti = {0};
21,458✔
1200
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
21,458✔
1201
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
1,669✔
1202
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
1,669✔
1203
      TAOS_RETURN(code);
1,669✔
1204
    }
1205
    if (ti.enabled == 0) {
19,789✔
1206
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,972✔
1207
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,972✔
1208
      TAOS_RETURN(code);
2,972✔
1209
    }
1210
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
16,817✔
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,817✔
1216
  }
1217
#endif
1218

1219
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
349,914,962✔
1220
  MndMsgFpExt fpExt = NULL;
349,929,845✔
1221
  if (fp == NULL) {
349,929,845✔
1222
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
25,229,851✔
1223
    if (fpExt == NULL) {
25,229,851✔
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));
349,929,845✔
1231

1232
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
346,591,724✔
1233
  if (fp)
346,591,431✔
1234
    code = (*fp)(pMsg);
321,363,454✔
1235
  else
1236
    code = (*fpExt)(pMsg, pQueueInfo);
25,227,977✔
1237
  mndReleaseRpc(pMnode);
346,593,929✔
1238

1239
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
346,596,706✔
1240
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
39,230,051✔
1241
  } else if (code == 0) {
307,366,655✔
1242
    mGTrace("msg:%p, successfully processed", pMsg);
291,280,640✔
1243
  } else {
1244
    // TODO removve this wrong set code
1245
    if (code == -1) {
16,086,015✔
1246
      code = terrno;
×
1247
    }
1248
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
16,086,015✔
1249
            TMSG_INFO(pMsg->msgType));
1250
  }
1251

1252
  TAOS_RETURN(code);
346,596,706✔
1253
}
1254

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1423
    SMonStbDesc desc = {0};
×
1424

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

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

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

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

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

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

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

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

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

1509
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
742,042,771✔
1510

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

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