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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

web-flow
merge: from main to 3.0 branch #35319

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 hits per line

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

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

86
static inline void mndReleaseRpc(SMnode *pMnode) {
323,497,753✔
87
  (void)taosThreadRwlockRdlock(&pMnode->lock);
323,497,753✔
88
#if 1
89
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
323,500,106✔
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);
323,500,726✔
95
}
323,500,283✔
96

97
static void *mndBuildTimerMsg(int32_t *pContLen) {
75,021,549✔
98
  terrno = 0;
75,021,549✔
99
  SMTimerReq timerReq = {0};
75,021,549✔
100

101
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
75,021,549✔
102
  if (contLen <= 0) return NULL;
75,021,549✔
103
  void *pReq = rpcMallocCont(contLen);
75,021,549✔
104
  if (pReq == NULL) return NULL;
75,021,549✔
105

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

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

126
static void mndPullupUpgradeSdb(SMnode *pMnode) {
355,344✔
127
  if (sdbIsUpgraded(pMnode->pSdb)) {
355,344✔
128
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
326,551✔
129
    return;
326,551✔
130
  }
131

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

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

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

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

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

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

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

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

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

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

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

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

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

339
  void *pIter = NULL;
113,158✔
340
  while (1) {
482,751✔
341
    SVgObj *pVgroup = NULL;
595,909✔
342
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
595,909✔
343
    if (pIter == NULL) break;
595,909✔
344

345
    bool stateChanged = false;
482,751✔
346
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,197,362✔
347
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
910,891✔
348
      if (pGid->dnodeId == dnodeId) {
910,891✔
349
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
196,280✔
350
          mInfo(
73,107✔
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;
73,107✔
356
          pGid->syncRestore = 0;
73,107✔
357
          pGid->syncCanRead = 0;
73,107✔
358
          pGid->startTimeMs = 0;
73,107✔
359
          pGid->learnerProgress = 0;
73,107✔
360
          pGid->snapSeq = -1;
73,107✔
361
          stateChanged = true;
73,107✔
362
        }
363
        break;
196,280✔
364
      }
365
    }
366

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

377
    sdbRelease(pSdb, pVgroup);
482,751✔
378
  }
379
}
113,158✔
380

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

385
  SSdb   *pSdb = pMnode->pSdb;
5,685,520✔
386
  int64_t curMs = taosGetTimestampMs();
5,685,520✔
387

388
  void *pIter = NULL;
5,685,520✔
389
  while (1) {
9,679,230✔
390
    SDnodeObj *pDnode = NULL;
15,364,750✔
391
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
15,364,750✔
392
    if (pIter == NULL) break;
15,364,750✔
393

394
    bool online = mndIsDnodeOnline(pDnode, curMs);
9,679,230✔
395
    if (!online) {
9,679,230✔
396
      mInfo("dnode:%d, in offline state", pDnode->id);
113,158✔
397
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
113,158✔
398
    }
399

400
    sdbRelease(pSdb, pDnode);
9,679,230✔
401
  }
402

403
  mndReleaseRpc(pMnode);
5,685,520✔
404
}
405

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

415
  if (state.state != TAOS_SYNC_STATE_LEADER) {
338,062,099✔
416
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,750,806✔
417
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,750,806✔
418
    return true;
11,750,806✔
419
  }
420
  if (!state.restored || !pMnode->restored) {
326,311,293✔
421
    (void)taosThreadRwlockUnlock(&pMnode->lock);
14,567✔
422
    terrno = TSDB_CODE_SYN_RESTORING;
14,567✔
423
    return true;
14,567✔
424
  }
425
  (void)taosThreadRwlockUnlock(&pMnode->lock);
326,296,726✔
426
  return false;
326,296,122✔
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) {
30,173,340✔
449
  int32_t code = 0;
30,173,340✔
450
#ifndef TD_ASTRA
451
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
30,173,340✔
452
    mndPullupGrant(pMnode);
1,870,979✔
453
  }
454
  if (sec % tsTtlPushIntervalSec == 0) {
30,173,340✔
455
    mndPullupTtl(pMnode);
3,148,652✔
456
  }
457

458
  if (sec % tsTrimVDbIntervalSec == 0) {
30,173,340✔
459
    mndPullupTrimDb(pMnode);
8,261✔
460
  }
461

462
  if (sec % tsQueryTrimIntervalSec == 0) {
30,173,340✔
463
    mndPullupQueryTrimDb(pMnode);
3,233,740✔
464
  }
465
#endif
466
#ifdef USE_SHARED_STORAGE
467
  if (tsSsEnabled) {
30,173,340✔
468
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
244,608✔
469
      mndPullupUpdateSsMigrateProgress(pMnode);
173,056✔
470
    }
471
    if (tsSsEnabled == 2) {
244,608✔
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) {
30,173,340✔
487
    if (sec % tsAuthReqHBInterval == 0) {
×
488
      mndPullupAuth(pMnode);
×
489
    }
490
  }
491
  if (tsClsEnabled || tsClsRefreshInterval == GRANT_CLS_CLOSING || tsClsRefreshInterval == GRANT_CLS_OPENING) {
30,173,340✔
492
    if (sec % tsClsRefreshInterval == 0) {
×
493
      mndPullupCls(pMnode);
×
494
    }
495
  }
496
#endif
497
  if (sec % tsTransPullupInterval == 0) {
30,173,340✔
498
    mndPullupTrans(pMnode);
15,091,725✔
499
  }
500

501
  if (sec % tsCompactPullupInterval == 0) {
30,173,340✔
502
    mndPullupCompacts(pMnode);
3,016,504✔
503
  }
504

505
  if (sec % tsScanPullupInterval == 0) {
30,173,340✔
506
    mndPullupScans(pMnode);
3,016,588✔
507
  }
508
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
30,173,340✔
509
    mndPullupInstances(pMnode);
6,047,752✔
510
  }
511
#ifdef USE_TOPIC
512
  if (sec % tsMqRebalanceInterval == 0) {
30,173,340✔
513
    mndCalMqRebalance(pMnode);
15,091,557✔
514
  }
515
#endif
516
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
30,173,340✔
517
    mndPullupTelem(pMnode);
249✔
518
  }
519
  if (sec % tsUptimeInterval == 0) {
30,173,340✔
520
    mndIncreaseUpTime(pMnode);
99,741✔
521
  }
522

523
  if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION && sec % UPGRADE_INTERVAL == 0) {
30,173,340✔
524
    mndPullupUpgradeSdb(pMnode);
355,344✔
525
  }
526
}
30,173,340✔
527

528
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
296,123,561✔
529
  int32_t code = 0;
296,123,561✔
530
#ifndef TD_ASTRA
531
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
296,123,561✔
532
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
14,568,131✔
533
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
534
    }
535
  }
536

537
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
296,123,561✔
538
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
9,654,614✔
539
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
540
    }
541
  }
542
#endif
543
}
296,123,561✔
544

545
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
296,123,561✔
546
  if (ms % (tsStatusTimeoutMs) == 0) {
296,123,561✔
547
    mndCheckDnodeOffline(pMnode);
5,685,520✔
548
  }
549
}
296,123,561✔
550

551
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
30,173,340✔
552
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
30,173,340✔
553
    mndSyncCheckTimeout(pMnode);
1,017,697✔
554
  }
555
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
30,173,340✔
556
    msmHealthCheck(pMnode);
10,055,977✔
557
  }
558
}
30,173,340✔
559

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

566
  while (1) {
307,283,853✔
567
    if (mndGetStop(pMnode)) break;
307,810,321✔
568

569
    int64_t nowSec = taosGetTimestampMs() / 1000;
307,283,853✔
570
    if (nowSec == lastSec) {
307,283,853✔
571
      taosMsleep(100);
275,991,986✔
572
      continue;
275,991,986✔
573
    }
574
    lastSec = nowSec;
31,291,867✔
575

576
    if (mnodeIsNotLeader(pMnode)) {
31,291,867✔
577
      taosMsleep(100);
1,118,527✔
578
      mTrace("timer not process since mnode is not leader");
1,118,527✔
579
      continue;
1,118,527✔
580
    }
581

582
    mndDoTimerCheckSync(pMnode, nowSec);
30,173,340✔
583

584
    mndDoTimerPullupTask(pMnode, nowSec);
30,173,340✔
585

586
    taosMsleep(100);
30,173,340✔
587
  }
588

589
  return NULL;
526,468✔
590
}
591

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

598
  while (1) {
599
    lastTime += 100;
307,296,875✔
600
    taosMsleep(100);
307,296,875✔
601

602
    if (mndGetStop(pMnode)) break;
307,296,875✔
603
    if (lastTime % 10 != 0) continue;
306,770,407✔
604

605
    if (mnodeIsNotLeader(pMnode)) {
306,770,407✔
606
      mTrace("timer not process since mnode is not leader");
10,646,846✔
607
      continue;
10,646,846✔
608
    }
609

610
    mndDoTimerCheckStatus(pMnode, lastTime);
296,123,561✔
611

612
    mndDoArbTimerPullupTask(pMnode, lastTime);
296,123,561✔
613
  }
614

615
  return NULL;
526,468✔
616
}
617

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

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

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

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

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

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

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

674
  TAOS_RETURN(code);
526,620✔
675
}
676

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

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

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

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

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

768
  TAOS_RETURN(code);
526,620✔
769
}
770

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

780
  if (!pMnode->deploy) {
526,620✔
781
    code = sdbReadFile(pMnode->pSdb);
147,560✔
782
  }
783

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

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

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

797
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
26,857,620✔
798
  SMnodeStep step = {0};
26,857,620✔
799
  step.name = name;
26,857,620✔
800
  step.initFp = initFp;
26,857,620✔
801
  step.cleanupFp = cleanupFp;
26,857,620✔
802
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
53,715,240✔
803
    TAOS_RETURN(terrno);
×
804
  }
805

806
  TAOS_RETURN(0);
26,857,620✔
807
}
808

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

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

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

873
  for (int32_t s = pos; s >= 0; s--) {
27,380,912✔
874
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
26,854,356✔
875
    mInfo("%s will cleanup", pStep->name);
26,854,356✔
876
    if (pStep->cleanupFp != NULL) {
26,854,356✔
877
      (*pStep->cleanupFp)(pMnode);
25,801,244✔
878
    }
879
  }
880

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

886
static int32_t mndExecSteps(SMnode *pMnode) {
526,620✔
887
  int32_t code = 0;
526,620✔
888
  int32_t size = taosArrayGetSize(pMnode->pSteps);
526,620✔
889
  for (int32_t pos = 0; pos < size; pos++) {
27,384,240✔
890
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
26,857,620✔
891
    if (pStep->initFp == NULL) continue;
26,857,620✔
892

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

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

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

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

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

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

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

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

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

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

1018
  if (sdbIsUpgraded(pMnode->pSdb)) {
526,468✔
1019
    pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
128,725✔
1020
  } else if (pMnode->version < TSDB_MNODE_BUILTIN_DATA_VERSION) {
397,743✔
1021
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
397,743✔
1022
      mError("failed to upgrade sdb while start mnode");
×
1023
      return -1;
×
1024
    }
1025
    if (sdbIsUpgraded(pMnode->pSdb)) {
397,743✔
1026
      pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
1,497✔
1027
    }
1028
  }
1029

1030
#ifdef TD_ENTERPRISE
1031
  if (mndIsLeader(pMnode)) {
526,468✔
1032
    if (tsSodEnforceMode) {
474,464✔
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);
526,468✔
1050

1051
  return mndInitTimer(pMnode);
526,468✔
1052
}
1053

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

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

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

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

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

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

1075
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
24,352,165✔
1076

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

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

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

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

1096
  return code;
67,895,544✔
1097
}
1098

1099
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
320,944,804✔
1100
  int32_t code = 0;
320,944,804✔
1101
  if (!IsReq(pMsg)) TAOS_RETURN(code);
320,944,804✔
1102
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
281,965,565✔
1103
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
275,365,351✔
1104
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
269,746,824✔
1105
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
256,690,404✔
1106
    TAOS_RETURN(code);
25,269,416✔
1107
  }
1108

1109
  SMnode *pMnode = pMsg->info.node;
256,689,796✔
1110
  (void)taosThreadRwlockRdlock(&pMnode->lock);
256,690,330✔
1111
  if (pMnode->stopped) {
256,696,766✔
1112
    (void)taosThreadRwlockUnlock(&pMnode->lock);
684✔
1113
    code = TSDB_CODE_APP_IS_STOPPING;
684✔
1114
    TAOS_RETURN(code);
684✔
1115
  }
1116

1117
  terrno = 0;
256,690,450✔
1118
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
256,689,516✔
1119
  if (terrno != 0) {
256,696,880✔
1120
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1121
    code = terrno;
×
1122
    TAOS_RETURN(code);
×
1123
  }
1124

1125
  if (state.state != TAOS_SYNC_STATE_LEADER) {
256,695,750✔
1126
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,563,988✔
1127
    code = TSDB_CODE_SYN_NOT_LEADER;
2,563,793✔
1128
    goto _OVER;
2,563,793✔
1129
  }
1130

1131
  if (!state.restored || !pMnode->restored) {
254,131,762✔
1132
    (void)taosThreadRwlockUnlock(&pMnode->lock);
576,064✔
1133
    code = TSDB_CODE_SYN_RESTORING;
574,691✔
1134
    goto _OVER;
574,691✔
1135
  }
1136

1137
#if 1
1138
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
253,556,289✔
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);
253,557,133✔
1145
  TAOS_RETURN(code);
253,556,079✔
1146

1147
_OVER:
3,138,484✔
1148
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,138,484✔
1149
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,138,973✔
1150
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,138,484✔
1151
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,138,501✔
1152
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,138,750✔
1153
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,138,862✔
1154
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,138,278✔
1155
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,137,960✔
1156
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER ||
3,137,731✔
1157
      pMsg->msgType == TDMT_MND_CLS_HB_TIMER) {
3,138,475✔
1158
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
6✔
1159
           pMnode->stopped, state.restored, syncStr(state.state));
1160
    TAOS_RETURN(code);
6✔
1161
  }
1162

1163
  const STraceId *trace = &pMsg->info.traceId;
3,137,833✔
1164
  SEpSet          epSet = {0};
3,137,719✔
1165
  mndGetMnodeEpSet(pMnode, &epSet);
3,137,909✔
1166

1167
  mGDebug(
3,138,945✔
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,139,057✔
1174

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

1179
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,139,057✔
1180
  pMsg->info.rsp = rpcMallocCont(contLen);
3,137,652✔
1181
  if (pMsg->info.rsp != NULL) {
3,138,597✔
1182
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,138,720✔
1183
      mError("failed to serialize ep set");
×
1184
    }
1185
    pMsg->info.hasEpSet = 1;
3,138,418✔
1186
    pMsg->info.rspLen = contLen;
3,138,427✔
1187
  }
1188

1189
  TAOS_RETURN(code);
3,137,672✔
1190
}
1191

1192
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
320,957,501✔
1193
  SMnode         *pMnode = pMsg->info.node;
320,957,501✔
1194
  const STraceId *trace = &pMsg->info.traceId;
320,959,179✔
1195
  int32_t         code = TSDB_CODE_SUCCESS;
320,956,868✔
1196

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

1219
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
320,947,704✔
1220
  MndMsgFpExt fpExt = NULL;
320,953,836✔
1221
  if (fp == NULL) {
320,953,836✔
1222
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
25,277,467✔
1223
    if (fpExt == NULL) {
25,277,467✔
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));
320,953,836✔
1231

1232
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
317,812,382✔
1233
  if (fp)
317,811,428✔
1234
    code = (*fp)(pMsg);
292,534,201✔
1235
  else
1236
    code = (*fpExt)(pMsg, pQueueInfo);
25,277,227✔
1237
  mndReleaseRpc(pMnode);
317,811,040✔
1238

1239
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
317,814,679✔
1240
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
39,398,474✔
1241
  } else if (code == 0) {
278,416,205✔
1242
    mGTrace("msg:%p, successfully processed", pMsg);
260,639,656✔
1243
  } else {
1244
    // TODO removve this wrong set code
1245
    if (code == -1) {
17,776,549✔
1246
      code = terrno;
×
1247
    }
1248
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
17,776,549✔
1249
            TMSG_INFO(pMsg->msgType));
1250
  }
1251

1252
  TAOS_RETURN(code);
317,814,679✔
1253
}
1254

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1509
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
615,086,804✔
1510

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

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