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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

78.59
/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 "mndRsma.h"
44
#include "mndScan.h"
45
#include "mndScanDetail.h"
46
#include "mndShow.h"
47
#include "mndSma.h"
48
#include "mndSnode.h"
49
#include "mndSsMigrate.h"
50
#include "mndStb.h"
51
#include "mndStream.h"
52
#include "mndSubscribe.h"
53
#include "mndSync.h"
54
#include "mndTelem.h"
55
#include "mndTopic.h"
56
#include "mndTrans.h"
57
#include "mndUser.h"
58
#include "mndVgroup.h"
59
#include "mndView.h"
60

61
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
3,945,024✔
62
  int32_t code = 0;
3,945,024✔
63
  (void)taosThreadRwlockRdlock(&pMnode->lock);
3,945,024✔
64
  if (pMnode->stopped) {
3,945,024✔
65
    code = TSDB_CODE_APP_IS_STOPPING;
×
66
  } else if (!mndIsLeader(pMnode)) {
3,945,024✔
67
    code = 1;
×
68
  } else {
69
#if 1
70
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
3,945,024✔
71
#else
72
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
73
    mTrace("mnode rpc is acquired, ref:%d", ref);
74
#endif
75
  }
76
  (void)taosThreadRwlockUnlock(&pMnode->lock);
3,945,024✔
77
  TAOS_RETURN(code);
3,945,024✔
78
}
79

80
static inline void mndReleaseRpc(SMnode *pMnode) {
231,297,871✔
81
  (void)taosThreadRwlockRdlock(&pMnode->lock);
231,297,871✔
82
#if 1
83
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
231,300,414✔
84
#else
85
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
86
  mTrace("mnode rpc is released, ref:%d", ref);
87
#endif
88
  (void)taosThreadRwlockUnlock(&pMnode->lock);
231,299,102✔
89
}
231,300,109✔
90

91
static void *mndBuildTimerMsg(int32_t *pContLen) {
50,057,112✔
92
  terrno = 0;
50,057,112✔
93
  SMTimerReq timerReq = {0};
50,057,089✔
94

95
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
50,057,089✔
96
  if (contLen <= 0) return NULL;
50,053,448✔
97
  void *pReq = rpcMallocCont(contLen);
50,053,448✔
98
  if (pReq == NULL) return NULL;
50,053,857✔
99

100
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
50,053,857✔
101
    mError("failed to serialize timer msg since %s", terrstr());
×
102
  }
103
  *pContLen = contLen;
50,054,056✔
104
  return pReq;
50,055,974✔
105
}
106

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

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

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

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

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

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

180
static void mndPullupQueryTrimDb(SMnode *pMnode) {
1,991,495✔
181
  mTrace("pullup trim query");
1,991,495✔
182
  int32_t contLen = 0;
1,991,495✔
183
  void   *pReq = mndBuildTimerMsg(&contLen);
1,991,495✔
184
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
1,991,495✔
185
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,991,495✔
186
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
187
  }
188
}
1,991,495✔
189

190
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
191
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
192
    return;
×
193
  }
194

195
  mTrace("pullup ssmigrate db");
×
196
  int32_t contLen = 0;
×
197
  void   *pReq = mndBuildTimerMsg(&contLen);
×
198
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
199
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
200
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
201
  }
202
}
203

204
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
×
205
  mTrace("pullup update ssmigrate progress");
×
206
  int32_t contLen = 0;
×
207
  void   *pReq = mndBuildTimerMsg(&contLen);
×
208
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
209
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
210
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
211
  }
212
}
×
213

214
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
10,171,465✔
215
  mTrace("pullup arb hb");
10,171,465✔
216
  int32_t contLen = 0;
10,171,465✔
217
  void   *pReq = mndBuildTimerMsg(&contLen);
10,171,465✔
218
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
10,171,465✔
219
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
10,171,465✔
220
}
221

222
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
6,704,067✔
223
  mTrace("pullup arb sync");
6,704,067✔
224
  int32_t contLen = 0;
6,704,067✔
225
  void   *pReq = mndBuildTimerMsg(&contLen);
6,704,067✔
226
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
6,704,067✔
227
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
6,704,067✔
228
}
229

230
static void mndCalMqRebalance(SMnode *pMnode) {
10,169,926✔
231
  int32_t contLen = 0;
10,169,926✔
232
  void   *pReq = mndBuildTimerMsg(&contLen);
10,169,926✔
233
  if (pReq != NULL) {
10,169,926✔
234
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
10,169,926✔
235
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
10,169,926✔
236
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
237
    }
238
  }
239
}
10,169,926✔
240

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

254
static void mndPullupGrant(SMnode *pMnode) {
1,208,195✔
255
  mTrace("pullup grant msg");
1,208,195✔
256
  int32_t contLen = 0;
1,208,195✔
257
  void   *pReq = mndBuildTimerMsg(&contLen);
1,208,195✔
258
  if (pReq != NULL) {
1,208,195✔
259
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,208,195✔
260
                      .pCont = pReq,
261
                      .contLen = contLen,
262
                      .info.notFreeAhandle = 1,
263
                      .info.ahandle = 0};
264
    // TODO check return value
265
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,208,195✔
266
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
267
    }
268
  }
269
}
1,208,195✔
270

271
static void mndPullupAuth(SMnode *pMnode) {
×
272
  mTrace("pullup auth msg");
×
273
  int32_t contLen = 0;
×
274
  void   *pReq = mndBuildTimerMsg(&contLen);
×
275
  if (pReq != NULL) {
×
276
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_AUTH_HB_TIMER, .pCont = pReq, .contLen = contLen, .info.notFreeAhandle = 1, .info.ahandle = 0};
×
277
    // TODO check return value
278
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
279
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
280
    }
281
  }
282
}
×
283

284
static void mndIncreaseUpTime(SMnode *pMnode) {
11,853✔
285
  mTrace("increate uptime");
11,853✔
286
  int32_t contLen = 0;
11,853✔
287
  void   *pReq = mndBuildTimerMsg(&contLen);
11,853✔
288
  if (pReq != NULL) {
11,853✔
289
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
11,853✔
290
                      .pCont = pReq,
291
                      .contLen = contLen,
292
                      .info.notFreeAhandle = 1,
293
                      .info.ahandle = 0};
294
    // TODO check return value
295
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,853✔
296
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
297
    }
298
  }
299
}
11,853✔
300

301
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
145,914✔
302
  SSdb *pSdb = pMnode->pSdb;
145,914✔
303

304
  void *pIter = NULL;
145,914✔
305
  while (1) {
751,274✔
306
    SVgObj *pVgroup = NULL;
897,188✔
307
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
897,188✔
308
    if (pIter == NULL) break;
897,188✔
309

310
    bool stateChanged = false;
751,274✔
311
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,918,927✔
312
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,475,545✔
313
      if (pGid->dnodeId == dnodeId) {
1,475,545✔
314
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
307,892✔
315
          mInfo(
139,902✔
316
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
317
              "restored:0 "
318
              "canRead:0",
319
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
320
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
139,902✔
321
          pGid->syncRestore = 0;
139,902✔
322
          pGid->syncCanRead = 0;
139,902✔
323
          pGid->startTimeMs = 0;
139,902✔
324
          stateChanged = true;
139,902✔
325
        }
326
        break;
307,892✔
327
      }
328
    }
329

330
    if (stateChanged) {
751,274✔
331
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
139,902✔
332
      if (pDb != NULL && pDb->stateTs != curMs) {
139,902✔
333
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
74,083✔
334
              curMs);
335
        pDb->stateTs = curMs;
74,083✔
336
      }
337
      mndReleaseDb(pMnode, pDb);
139,902✔
338
    }
339

340
    sdbRelease(pSdb, pVgroup);
751,274✔
341
  }
342
}
145,914✔
343

344
static void mndCheckDnodeOffline(SMnode *pMnode) {
3,944,880✔
345
  mTrace("check dnode offline");
3,944,880✔
346
  if (mndAcquireRpc(pMnode) != 0) return;
3,944,880✔
347

348
  SSdb   *pSdb = pMnode->pSdb;
3,944,880✔
349
  int64_t curMs = taosGetTimestampMs();
3,944,880✔
350

351
  void *pIter = NULL;
3,944,880✔
352
  while (1) {
8,056,769✔
353
    SDnodeObj *pDnode = NULL;
12,001,649✔
354
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,001,649✔
355
    if (pIter == NULL) break;
12,001,649✔
356

357
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,056,769✔
358
    if (!online) {
8,056,769✔
359
      mInfo("dnode:%d, in offline state", pDnode->id);
145,914✔
360
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
145,914✔
361
    }
362

363
    sdbRelease(pSdb, pDnode);
8,056,769✔
364
  }
365

366
  mndReleaseRpc(pMnode);
3,944,880✔
367
}
368

369
static bool mnodeIsNotLeader(SMnode *pMnode) {
252,219,053✔
370
  terrno = 0;
252,219,053✔
371
  (void)taosThreadRwlockRdlock(&pMnode->lock);
252,219,137✔
372
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
252,227,958✔
373
  if (terrno != 0) {
252,227,958✔
374
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
375
    return true;
×
376
  }
377

378
  if (state.state != TAOS_SYNC_STATE_LEADER) {
252,225,962✔
379
    (void)taosThreadRwlockUnlock(&pMnode->lock);
24,394,568✔
380
    terrno = TSDB_CODE_SYN_NOT_LEADER;
24,394,236✔
381
    return true;
24,394,236✔
382
  }
383
  if (!state.restored || !pMnode->restored) {
227,831,394✔
384
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,383✔
385
    terrno = TSDB_CODE_SYN_RESTORING;
3,362✔
386
    return true;
3,362✔
387
  }
388
  (void)taosThreadRwlockUnlock(&pMnode->lock);
227,826,645✔
389
  return false;
227,829,240✔
390
}
391

392
static int32_t minCronTime() {
×
393
  int32_t min = INT32_MAX;
×
394
  min = TMIN(min, tsTtlPushIntervalSec);
×
395
  min = TMIN(min, tsTrimVDbIntervalSec);
×
396
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
397
  min = TMIN(min, tsTransPullupInterval);
×
398
  min = TMIN(min, tsCompactPullupInterval);
×
399
  min = TMIN(min, tsMqRebalanceInterval);
×
400

401
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
402
  min = TMIN(min, telemInt);
×
403
  min = TMIN(min, tsGrantHBInterval);
×
404
  min = TMIN(min, tsUptimeInterval);
×
405

406
  return min <= 1 ? 2 : min;
×
407
}
408
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
20,542,332✔
409
  int32_t code = 0;
20,542,332✔
410
#ifndef TD_ASTRA
411
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
20,542,332✔
412
    mndPullupGrant(pMnode);
1,208,195✔
413
  }
414
  if (sec % tsTtlPushIntervalSec == 0) {
20,542,332✔
415
    mndPullupTtl(pMnode);
1,943,520✔
416
  }
417

418
  if (sec % tsTrimVDbIntervalSec == 0) {
20,542,332✔
419
    mndPullupTrimDb(pMnode);
×
420
  }
421

422
  if (sec % tsQueryTrimIntervalSec == 0) {
20,542,332✔
423
    mndPullupQueryTrimDb(pMnode);
1,991,495✔
424
  }
425
#endif
426
#ifdef USE_SHARED_STORAGE
427
  if (tsSsEnabled) {
20,542,332✔
428
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
429
      mndPullupUpdateSsMigrateProgress(pMnode);
×
430
    }
431
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
432
      mndPullupSsMigrateDb(pMnode);
×
433
    }
434
  }
435
#endif
436
#ifdef TD_ENTERPRISE
437
  if (tsAuthReq) {
20,542,332✔
438
    if (sec % tsAuthReqHBInterval == 0) {
×
439
      mndPullupAuth(pMnode);
×
440
    }
441
  }
442
#endif
443
  if (sec % tsTransPullupInterval == 0) {
20,542,332✔
444
    mndPullupTrans(pMnode);
10,170,522✔
445
  }
446

447
  if (sec % tsCompactPullupInterval == 0) {
20,542,332✔
448
    mndPullupCompacts(pMnode);
1,870,931✔
449
  }
450

451
  if (sec % tsScanPullupInterval == 0) {
20,542,332✔
452
    mndPullupScans(pMnode);
1,870,931✔
453
  }
454
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
20,542,332✔
455
    mndPullupInstances(pMnode);
3,944,317✔
456
  }
457
#ifdef USE_TOPIC
458
  if (sec % tsMqRebalanceInterval == 0) {
20,542,332✔
459
    mndCalMqRebalance(pMnode);
10,169,926✔
460
  }
461
#endif
462
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
20,542,332✔
463
    mndPullupTelem(pMnode);
288✔
464
  }
465
  if (sec % tsUptimeInterval == 0) {
20,542,332✔
466
    mndIncreaseUpTime(pMnode);
11,853✔
467
  }
468
}
20,542,332✔
469

470
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
207,287,696✔
471
  int32_t code = 0;
207,287,696✔
472
#ifndef TD_ASTRA
473
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
207,287,696✔
474
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
10,171,465✔
475
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
476
    }
477
  }
478

479
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
207,287,696✔
480
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
6,704,067✔
481
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
482
    }
483
  }
484
#endif
485
}
207,287,696✔
486

487
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
207,287,696✔
488
  if (ms % (tsStatusTimeoutMs) == 0) {
207,287,696✔
489
    mndCheckDnodeOffline(pMnode);
3,944,880✔
490
  }
491
}
207,287,696✔
492

493
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
20,542,332✔
494
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
20,542,332✔
495
    mndSyncCheckTimeout(pMnode);
525,700✔
496
  }
497
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
20,542,332✔
498
    msmHealthCheck(pMnode);
6,704,713✔
499
  }
500
}
20,542,332✔
501

502
static void *mndThreadSecFp(void *param) {
502,305✔
503
  SMnode *pMnode = param;
502,305✔
504
  int64_t lastTime = 0;
502,305✔
505
  setThreadName("mnode-timer");
502,305✔
506

507
  while (1) {
229,526,161✔
508
    lastTime++;
230,028,466✔
509
    taosMsleep(100);
230,028,466✔
510

511
    if (mndGetStop(pMnode)) break;
230,028,466✔
512
    if (lastTime % 10 != 0) continue;
229,526,161✔
513

514
    if (mnodeIsNotLeader(pMnode)) {
22,725,753✔
515
      mTrace("timer not process since mnode is not leader");
2,183,421✔
516
      continue;
2,183,421✔
517
    }
518

519
    int64_t sec = lastTime / 10;
20,542,332✔
520
    mndDoTimerCheckSync(pMnode, sec);
20,542,332✔
521

522
    mndDoTimerPullupTask(pMnode, sec);
20,542,332✔
523
  }
524

525
  return NULL;
502,305✔
526
}
527

528
static void *mndThreadMsFp(void *param) {
502,305✔
529
  SMnode *pMnode = param;
502,305✔
530
  int64_t lastTime = 0;
502,305✔
531
  setThreadName("mnode-arb-timer");
502,305✔
532

533
  while (1) {
534
    lastTime += 100;
230,004,510✔
535
    taosMsleep(100);
230,004,510✔
536

537
    if (mndGetStop(pMnode)) break;
230,004,510✔
538
    if (lastTime % 10 != 0) continue;
229,502,205✔
539

540
    if (mnodeIsNotLeader(pMnode)) {
229,502,205✔
541
      mTrace("timer not process since mnode is not leader");
22,214,509✔
542
      continue;
22,214,509✔
543
    }
544

545
    mndDoTimerCheckStatus(pMnode, lastTime);
207,287,696✔
546

547
    mndDoArbTimerPullupTask(pMnode, lastTime);
207,287,696✔
548
  }
549

550
  return NULL;
502,305✔
551
}
552

553
static int32_t mndInitTimer(SMnode *pMnode) {
502,305✔
554
  int32_t      code = 0;
502,305✔
555
  TdThreadAttr thAttr;
499,567✔
556
  (void)taosThreadAttrInit(&thAttr);
502,305✔
557
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
502,305✔
558
#ifdef TD_COMPACT_OS
559
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
560
#endif
561
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
502,305✔
562
    mError("failed to create timer thread since %s", tstrerror(code));
×
563
    TAOS_RETURN(code);
×
564
  }
565

566
  (void)taosThreadAttrDestroy(&thAttr);
502,305✔
567
  tmsgReportStartup("mnode-timer", "initialized");
502,305✔
568

569
  TdThreadAttr arbAttr;
499,567✔
570
  (void)taosThreadAttrInit(&arbAttr);
502,305✔
571
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
502,305✔
572
#ifdef TD_COMPACT_OS
573
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
574
#endif
575
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
502,305✔
576
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
577
    TAOS_RETURN(code);
×
578
  }
579

580
  (void)taosThreadAttrDestroy(&arbAttr);
502,305✔
581
  tmsgReportStartup("mnode-timer", "initialized");
502,305✔
582
  TAOS_RETURN(code);
502,305✔
583
}
584

585
static void mndCleanupTimer(SMnode *pMnode) {
502,305✔
586
  if (taosCheckPthreadValid(pMnode->thread)) {
502,305✔
587
    (void)taosThreadJoin(pMnode->thread, NULL);
502,305✔
588
    taosThreadClear(&pMnode->thread);
502,305✔
589
  }
590
  if (taosCheckPthreadValid(pMnode->arbThread)) {
502,305✔
591
    (void)taosThreadJoin(pMnode->arbThread, NULL);
502,305✔
592
    taosThreadClear(&pMnode->arbThread);
502,305✔
593
  }
594
}
502,305✔
595

596
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
503,116✔
597
  int32_t code = 0;
503,116✔
598
  pMnode->path = taosStrdup(path);
503,116✔
599
  if (pMnode->path == NULL) {
503,116✔
600
    code = terrno;
×
601
    TAOS_RETURN(code);
×
602
  }
603

604
  if (taosMkDir(pMnode->path) != 0) {
503,116✔
605
    code = terrno;
×
606
    TAOS_RETURN(code);
×
607
  }
608

609
  TAOS_RETURN(code);
503,116✔
610
}
611

612
static int32_t mndInitWal(SMnode *pMnode) {
503,116✔
613
  int32_t code = 0;
503,116✔
614
  char    path[PATH_MAX + 20] = {0};
503,116✔
615
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
503,116✔
616
  SWalCfg cfg = {.vgId = 1,
503,116✔
617
                 .fsyncPeriod = 0,
618
                 .rollPeriod = -1,
619
                 .segSize = -1,
620
                 .committed = -1,
621
                 .retentionPeriod = 0,
622
                 .retentionSize = 0,
623
                 .level = TAOS_WAL_FSYNC,
624
                 .encryptAlgr = 0,
625
                 .encryptData = {0}};
626

627
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
628
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
503,116✔
629
    cfg.encryptAlgr = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
630
    if (tsEncryptKey[0] == '\0') {
×
631
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
632
      TAOS_RETURN(code);
×
633
    } else {
634
      tstrncpy(cfg.encryptData.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
635
    }
636
  }
637
#endif
638

639
  pMnode->pWal = walOpen(path, &cfg);
503,116✔
640
  if (pMnode->pWal == NULL) {
503,116✔
641
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
642
    if (terrno != 0) code = terrno;
×
643
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
644
    TAOS_RETURN(code);
×
645
  }
646

647
  TAOS_RETURN(code);
503,116✔
648
}
649

650
static void mndCloseWal(SMnode *pMnode) {
502,305✔
651
  if (pMnode->pWal != NULL) {
502,305✔
652
    walClose(pMnode->pWal);
502,305✔
653
    pMnode->pWal = NULL;
502,305✔
654
  }
655
}
502,305✔
656

657
static int32_t mndInitSdb(SMnode *pMnode) {
503,116✔
658
  int32_t code = 0;
503,116✔
659
  SSdbOpt opt = {0};
503,116✔
660
  opt.path = pMnode->path;
503,116✔
661
  opt.pMnode = pMnode;
503,116✔
662
  opt.pWal = pMnode->pWal;
503,116✔
663

664
  pMnode->pSdb = sdbInit(&opt);
503,116✔
665
  if (pMnode->pSdb == NULL) {
503,116✔
666
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
667
    if (terrno != 0) code = terrno;
×
668
    TAOS_RETURN(code);
×
669
  }
670

671
  TAOS_RETURN(code);
503,116✔
672
}
673

674
static int32_t mndOpenSdb(SMnode *pMnode) {
503,116✔
675
  int32_t code = 0;
503,116✔
676
  if (!pMnode->deploy) {
503,116✔
677
    code = sdbReadFile(pMnode->pSdb);
175,325✔
678
  }
679

680
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
503,116✔
681

682
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
503,116✔
683
  return code;
503,116✔
684
}
685

686
static void mndCleanupSdb(SMnode *pMnode) {
502,305✔
687
  if (pMnode->pSdb) {
502,305✔
688
    sdbCleanup(pMnode->pSdb);
502,305✔
689
    pMnode->pSdb = NULL;
502,305✔
690
  }
691
}
502,305✔
692

693
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
23,646,452✔
694
  SMnodeStep step = {0};
23,646,452✔
695
  step.name = name;
23,646,452✔
696
  step.initFp = initFp;
23,646,452✔
697
  step.cleanupFp = cleanupFp;
23,646,452✔
698
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
47,292,904✔
699
    TAOS_RETURN(terrno);
×
700
  }
701

702
  TAOS_RETURN(0);
23,646,452✔
703
}
704

705
static int32_t mndInitSteps(SMnode *pMnode) {
503,116✔
706
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
503,116✔
707
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
503,116✔
708
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
503,116✔
709
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
503,116✔
710
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-encrypt-algorithms", mndInitEncryptAlgr, mndCleanupEncryptAlgr));
503,116✔
711
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
503,116✔
712
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
503,116✔
713
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
503,116✔
714
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
503,116✔
715
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
503,116✔
716
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
503,116✔
717
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
503,116✔
718
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
503,116✔
719
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
503,116✔
720
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
503,116✔
721
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
503,116✔
722
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
503,116✔
723
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
503,116✔
724
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-instance", mndInitInstance, mndCleanupInstance));
503,116✔
725
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
503,116✔
726
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
503,116✔
727
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
503,116✔
728
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
503,116✔
729
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
503,116✔
730
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
503,116✔
731
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
503,116✔
732
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
503,116✔
733
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
503,116✔
734
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
503,116✔
735
#ifdef USE_MOUNT
736
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
503,116✔
737
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
503,116✔
738
#endif
739
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
503,116✔
740
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
503,116✔
741
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
503,116✔
742
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
503,116✔
743
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
503,116✔
744
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
503,116✔
745
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
503,116✔
746
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
503,116✔
747
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
503,116✔
748
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
503,116✔
749
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
503,116✔
750
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
503,116✔
751
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
503,116✔
752
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
503,116✔
753
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
503,116✔
754
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
503,116✔
755
  return 0;
503,116✔
756
}
757

758
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
502,305✔
759
  if (pMnode->pSteps == NULL) return;
502,305✔
760

761
  if (pos == -1) {
502,305✔
762
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
502,305✔
763
  }
764

765
  for (int32_t s = pos; s >= 0; s--) {
24,110,640✔
766
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
23,608,335✔
767
    mInfo("%s will cleanup", pStep->name);
23,608,335✔
768
    if (pStep->cleanupFp != NULL) {
23,608,335✔
769
      (*pStep->cleanupFp)(pMnode);
22,603,725✔
770
    }
771
  }
772

773
  taosArrayClear(pMnode->pSteps);
502,305✔
774
  taosArrayDestroy(pMnode->pSteps);
502,305✔
775
  pMnode->pSteps = NULL;
502,305✔
776
}
777

778
static int32_t mndExecSteps(SMnode *pMnode) {
503,116✔
779
  int32_t code = 0;
503,116✔
780
  int32_t size = taosArrayGetSize(pMnode->pSteps);
503,116✔
781
  for (int32_t pos = 0; pos < size; pos++) {
24,149,568✔
782
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
23,646,452✔
783
    if (pStep->initFp == NULL) continue;
23,646,452✔
784

785
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
23,646,452✔
786
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
787
      mndCleanupSteps(pMnode, pos);
×
788
      TAOS_RETURN(code);
×
789
    } else {
790
      mInfo("%s is initialized", pStep->name);
23,646,452✔
791
      tmsgReportStartup(pStep->name, "initialized");
23,646,452✔
792
    }
793
  }
794

795
  pMnode->clusterId = mndGetClusterId(pMnode);
503,116✔
796
  TAOS_RETURN(0);
503,116✔
797
}
798

799
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
503,116✔
800
  pMnode->msgCb = pOption->msgCb;
503,116✔
801
  pMnode->selfDnodeId = pOption->dnodeId;
503,116✔
802
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
503,116✔
803
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
503,116✔
804
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
503,116✔
805
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
503,116✔
806
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
503,116✔
807
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
503,116✔
808
}
503,116✔
809

810
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
503,116✔
811
  terrno = 0;
503,116✔
812
  mInfo("start to open mnode in %s", path);
503,116✔
813

814
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
503,116✔
815
  if (pMnode == NULL) {
503,116✔
816
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
817
    mError("failed to open mnode in step 1, since %s", terrstr());
×
818
    return NULL;
×
819
  }
820
  (void)memset(pMnode, 0, sizeof(SMnode));
503,116✔
821

822
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
503,116✔
823
  if (code != 0) {
503,116✔
824
    taosMemoryFree(pMnode);
×
825
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
826
    terrno = code;
×
827
    return NULL;
×
828
  }
829

830
  char timestr[24] = "1970-01-01 00:00:00.00";
503,116✔
831
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
503,116✔
832
  if (code < 0) {
503,116✔
833
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
834
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
835
    taosMemoryFree(pMnode);
×
836
    terrno = code;
×
837
    return NULL;
×
838
  }
839
  mndSetOptions(pMnode, pOption);
503,116✔
840

841
  pMnode->deploy = pOption->deploy;
503,116✔
842
  pMnode->version = pOption->version;
503,116✔
843
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
503,116✔
844
  if (pMnode->pSteps == NULL) {
503,116✔
845
    taosMemoryFree(pMnode);
×
846
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
847
    mError("failed to open mnode in step 4, since %s", terrstr());
×
848
    return NULL;
×
849
  }
850

851
  code = mndCreateDir(pMnode, path);
503,116✔
852
  if (code != 0) {
503,116✔
853
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
854
    mndClose(pMnode);
×
855
    terrno = code;
×
856
    return NULL;
×
857
  }
858

859
  code = mndInitSteps(pMnode);
503,116✔
860
  if (code != 0) {
503,116✔
861
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
862
    mndClose(pMnode);
×
863
    terrno = code;
×
864
    return NULL;
×
865
  }
866

867
  code = mndExecSteps(pMnode);
503,116✔
868
  if (code != 0) {
503,116✔
869
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
870
    mndClose(pMnode);
×
871
    terrno = code;
×
872
    return NULL;
×
873
  }
874

875
  mInfo("mnode open successfully");
503,116✔
876
  return pMnode;
503,116✔
877
}
878

879
void mndPreClose(SMnode *pMnode) {
502,305✔
880
  if (pMnode != NULL) {
502,305✔
881
    int32_t code = 0;
502,305✔
882
    // TODO check return value
883
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
502,305✔
884
    if (code < 0) {
502,305✔
885
      mError("failed to transfer leader since %s", tstrerror(code));
×
886
    }
887
    syncPreStop(pMnode->syncMgmt.sync);
502,305✔
888
    code = sdbWriteFile(pMnode->pSdb, 0);
502,305✔
889
    if (code < 0) {
502,305✔
890
      mError("failed to write sdb since %s", tstrerror(code));
741✔
891
    }
892
  }
893
}
502,305✔
894

895
void mndClose(SMnode *pMnode) {
502,305✔
896
  if (pMnode != NULL) {
502,305✔
897
    mInfo("start to close mnode");
502,305✔
898
    mndCleanupSteps(pMnode, -1);
502,305✔
899
    taosMemoryFreeClear(pMnode->path);
502,305✔
900
    taosMemoryFreeClear(pMnode);
502,305✔
901
    mInfo("mnode is closed");
502,305✔
902
  }
903
}
502,305✔
904

905
int32_t mndStart(SMnode *pMnode) {
502,305✔
906
  mndSyncStart(pMnode);
502,305✔
907
  if (pMnode->deploy) {
502,305✔
908
    if (sdbDeploy(pMnode->pSdb) != 0) {
327,791✔
909
      mError("failed to deploy sdb while start mnode");
×
910
      return -1;
×
911
    }
912
    mndSetRestored(pMnode, true);
327,791✔
913
  }
914
  if (mndIsLeader(pMnode)) {
502,305✔
915
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
411,562✔
916
      mError("failed to upgrade sdb while start mnode");
×
917
      return -1;
×
918
    }
919
  }
920
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
502,305✔
921
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
502,305✔
922

923
  return mndInitTimer(pMnode);
502,305✔
924
}
925

926
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
502,305✔
927

928
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
388,353✔
929

930
int32_t mndIsCatchUp(SMnode *pMnode) {
368,166✔
931
  int64_t rid = pMnode->syncMgmt.sync;
368,166✔
932
  return syncIsCatchUp(rid);
368,166✔
933
}
934

935
ESyncRole mndGetRole(SMnode *pMnode) {
368,166✔
936
  int64_t rid = pMnode->syncMgmt.sync;
368,166✔
937
  return syncGetRole(rid);
368,166✔
938
}
939

940
int64_t mndGetTerm(SMnode *pMnode) {
6,804,367✔
941
  int64_t rid = pMnode->syncMgmt.sync;
6,804,367✔
942
  return syncGetTerm(rid);
6,804,367✔
943
}
944

945
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
16,975,908✔
946

947
void mndStop(SMnode *pMnode) {
502,305✔
948
  mndSetStop(pMnode);
502,305✔
949
  mndSyncStop(pMnode);
502,305✔
950
  mndCleanupTimer(pMnode);
502,305✔
951
}
502,305✔
952

953
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
64,543,093✔
954
  SMnode    *pMnode = pMsg->info.node;
64,543,093✔
955
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
64,543,093✔
956

957
  const STraceId *trace = &pMsg->info.traceId;
64,543,093✔
958
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
64,543,093✔
959

960
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
64,543,093✔
961
  if (code != 0) {
64,543,093✔
962
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,033✔
963
            tstrerror(code), code);
964
  }
965

966
  return code;
64,543,093✔
967
}
968

969
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
233,086,835✔
970
  int32_t code = 0;
233,086,835✔
971
  if (!IsReq(pMsg)) TAOS_RETURN(code);
233,086,835✔
972
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
202,177,677✔
973
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
196,721,214✔
974
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
191,626,604✔
975
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
180,704,653✔
976
    TAOS_RETURN(code);
21,465,679✔
977
  }
978

979
  SMnode *pMnode = pMsg->info.node;
180,703,896✔
980
  (void)taosThreadRwlockRdlock(&pMnode->lock);
180,704,977✔
981
  if (pMnode->stopped) {
180,715,435✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,539✔
983
    code = TSDB_CODE_APP_IS_STOPPING;
11,539✔
984
    TAOS_RETURN(code);
11,539✔
985
  }
986

987
  terrno = 0;
180,695,462✔
988
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
180,701,850✔
989
  if (terrno != 0) {
180,706,810✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
991
    code = terrno;
×
992
    TAOS_RETURN(code);
×
993
  }
994

995
  if (state.state != TAOS_SYNC_STATE_LEADER) {
180,705,176✔
996
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,519,065✔
997
    code = TSDB_CODE_SYN_NOT_LEADER;
4,520,007✔
998
    goto _OVER;
4,520,007✔
999
  }
1000

1001
  if (!state.restored || !pMnode->restored) {
176,186,111✔
1002
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,214,707✔
1003
    code = TSDB_CODE_SYN_RESTORING;
1,213,348✔
1004
    goto _OVER;
1,213,348✔
1005
  }
1006

1007
#if 1
1008
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
174,971,404✔
1009
#else
1010
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1011
  mTrace("mnode rpc is acquired, ref:%d", ref);
1012
#endif
1013

1014
  (void)taosThreadRwlockUnlock(&pMnode->lock);
174,972,062✔
1015
  TAOS_RETURN(code);
174,972,754✔
1016

1017
_OVER:
5,733,355✔
1018
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,733,355✔
1019
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,732,729✔
1020
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,731,805✔
1021
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,730,816✔
1022
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,730,492✔
1023
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,731,796✔
1024
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,730,820✔
1025
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,731,796✔
1026
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,730,284✔
1027
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1028
           pMnode->stopped, state.restored, syncStr(state.state));
1029
    TAOS_RETURN(code);
×
1030
  }
1031

1032
  const STraceId *trace = &pMsg->info.traceId;
5,730,231✔
1033
  SEpSet          epSet = {0};
5,727,796✔
1034
  mndGetMnodeEpSet(pMnode, &epSet);
5,728,128✔
1035

1036
  mGDebug(
5,732,109✔
1037
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1038
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1039
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1040
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1041

1042
  if (epSet.numOfEps <= 0) return -1;
5,732,109✔
1043

1044
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
20,157,960✔
1045
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
14,425,851✔
1046
  }
1047

1048
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,732,109✔
1049
  pMsg->info.rsp = rpcMallocCont(contLen);
5,730,675✔
1050
  if (pMsg->info.rsp != NULL) {
5,730,533✔
1051
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,731,820✔
1052
      mError("failed to serialize ep set");
×
1053
    }
1054
    pMsg->info.hasEpSet = 1;
5,726,497✔
1055
    pMsg->info.rspLen = contLen;
5,727,887✔
1056
  }
1057

1058
  TAOS_RETURN(code);
5,725,237✔
1059
}
1060

1061
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
233,097,105✔
1062
  SMnode         *pMnode = pMsg->info.node;
233,097,105✔
1063
  const STraceId *trace = &pMsg->info.traceId;
233,097,251✔
1064
  int32_t         code = TSDB_CODE_SUCCESS;
233,087,774✔
1065

1066
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
233,087,774✔
1067
  MndMsgFpExt fpExt = NULL;
233,097,649✔
1068
  if (fp == NULL) {
233,097,649✔
1069
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,472,790✔
1070
    if (fpExt == NULL) {
21,472,790✔
1071
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1072
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1073
      TAOS_RETURN(code);
×
1074
    }
1075
  }
1076

1077
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
233,097,649✔
1078

1079
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
227,353,019✔
1080
  if (fp)
227,353,258✔
1081
    code = (*fp)(pMsg);
205,881,203✔
1082
  else
1083
    code = (*fpExt)(pMsg, pQueueInfo);
21,472,055✔
1084
  mndReleaseRpc(pMnode);
227,349,262✔
1085

1086
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
227,355,005✔
1087
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
33,508,469✔
1088
  } else if (code == 0) {
193,846,536✔
1089
    mGTrace("msg:%p, successfully processed", pMsg);
191,252,489✔
1090
  } else {
1091
    // TODO removve this wrong set code
1092
    if (code == -1) {
2,594,047✔
1093
      code = terrno;
3,253✔
1094
    }
1095
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,594,047✔
1096
            TMSG_INFO(pMsg->msgType));
1097
  }
1098

1099
  TAOS_RETURN(code);
227,355,005✔
1100
}
1101

1102
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
102,635,664✔
1103
  tmsg_t type = TMSG_INDEX(msgType);
102,635,664✔
1104
  if (type < TDMT_MAX) {
102,635,664✔
1105
    pMnode->msgFp[type] = fp;
102,635,664✔
1106
  }
1107
}
102,635,664✔
1108

1109
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,024,928✔
1110
  tmsg_t type = TMSG_INDEX(msgType);
4,024,928✔
1111
  if (type < TDMT_MAX) {
4,024,928✔
1112
    pMnode->msgFpExt[type] = fp;
4,024,928✔
1113
  }
1114
}
4,024,928✔
1115

1116
// Note: uid 0 is reserved
1117
int64_t mndGenerateUid(const char *name, int32_t len) {
9,118,703✔
1118
  int32_t hashval = MurmurHash3_32(name, len);
9,118,703✔
1119
  do {
×
1120
    int64_t us = taosGetTimestampUs();
9,119,491✔
1121
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,119,491✔
1122
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,119,491✔
1123
    if (uuid) {
9,119,491✔
1124
      return llabs(uuid);
9,119,491✔
1125
    }
1126
  } while (true);
1127
}
1128

1129
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
144✔
1130
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1131
  int32_t code = mndAcquireRpc(pMnode);
144✔
1132
  if (code < 0) {
144✔
1133
    TAOS_RETURN(code);
×
1134
  } else if (code == 1) {
144✔
1135
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1136
  }
1137

1138
  SSdb   *pSdb = pMnode->pSdb;
144✔
1139
  int64_t ms = taosGetTimestampMs();
144✔
1140

1141
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
144✔
1142
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
144✔
1143
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
144✔
1144
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
144✔
1145
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
144✔
1146
      pStbInfo->stbs == NULL) {
144✔
1147
    mndReleaseRpc(pMnode);
×
1148
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1149
    if (terrno != 0) code = terrno;
×
1150
    TAOS_RETURN(code);
×
1151
  }
1152

1153
  // cluster info
1154
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
144✔
1155
  pClusterInfo->monitor_interval = tsMonitorInterval;
144✔
1156
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
144✔
1157
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
144✔
1158
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
144✔
1159
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
144✔
1160
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
144✔
1161

1162
  void *pIter = NULL;
144✔
1163
  while (1) {
144✔
1164
    SDnodeObj *pObj = NULL;
288✔
1165
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
288✔
1166
    if (pIter == NULL) break;
288✔
1167

1168
    SMonDnodeDesc desc = {0};
144✔
1169
    desc.dnode_id = pObj->id;
144✔
1170
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
144✔
1171
    if (mndIsDnodeOnline(pObj, ms)) {
144✔
1172
      tstrncpy(desc.status, "ready", sizeof(desc.status));
144✔
1173
    } else {
1174
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1175
    }
1176
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
288✔
1177
      mError("failed put dnode into array, but continue at this monitor report")
×
1178
    }
1179
    sdbRelease(pSdb, pObj);
144✔
1180
  }
1181

1182
  pIter = NULL;
144✔
1183
  while (1) {
144✔
1184
    SMnodeObj *pObj = NULL;
288✔
1185
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
288✔
1186
    if (pIter == NULL) break;
288✔
1187

1188
    SMonMnodeDesc desc = {0};
144✔
1189
    desc.mnode_id = pObj->id;
144✔
1190
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
144✔
1191

1192
    if (pObj->id == pMnode->selfDnodeId) {
144✔
1193
      pClusterInfo->first_ep_dnode_id = pObj->id;
144✔
1194
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
144✔
1195
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1196
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
144✔
1197
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1198
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
144✔
1199
      desc.syncState = TAOS_SYNC_STATE_LEADER;
144✔
1200
    } else {
1201
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1202
      desc.syncState = pObj->syncState;
×
1203
    }
1204
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
288✔
1205
      mError("failed to put mnode into array, but continue at this monitor report");
×
1206
    }
1207
    sdbRelease(pSdb, pObj);
144✔
1208
  }
1209

1210
  // vgroup info
1211
  pIter = NULL;
144✔
1212
  while (1) {
288✔
1213
    SVgObj *pVgroup = NULL;
432✔
1214
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
432✔
1215
    if (pIter == NULL) break;
432✔
1216

1217
    if (pVgroup->mountVgId) {
288✔
1218
      sdbRelease(pSdb, pVgroup);
×
1219
      continue;
×
1220
    }
1221

1222
    pClusterInfo->vgroups_total++;
288✔
1223
    pClusterInfo->tbs_total += pVgroup->numOfTables;
288✔
1224

1225
    SMonVgroupDesc desc = {0};
288✔
1226
    desc.vgroup_id = pVgroup->vgId;
288✔
1227

1228
    SName name = {0};
288✔
1229
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
288✔
1230
    if (code < 0) {
288✔
1231
      mError("failed to get db name since %s", tstrerror(code));
×
1232
      sdbRelease(pSdb, pVgroup);
×
1233
      TAOS_RETURN(code);
×
1234
    }
1235
    (void)tNameGetDbName(&name, desc.database_name);
288✔
1236

1237
    desc.tables_num = pVgroup->numOfTables;
288✔
1238
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
288✔
1239
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
288✔
1240
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
576✔
1241
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
288✔
1242
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
288✔
1243
      pVnDesc->dnode_id = pVgid->dnodeId;
288✔
1244
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
288✔
1245
      pVnDesc->syncState = pVgid->syncState;
288✔
1246
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
288✔
1247
        tstrncpy(desc.status, "ready", sizeof(desc.status));
288✔
1248
        pClusterInfo->vgroups_alive++;
288✔
1249
      }
1250
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
288✔
1251
        pClusterInfo->vnodes_alive++;
288✔
1252
      }
1253
      pClusterInfo->vnodes_total++;
288✔
1254
    }
1255

1256
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
576✔
1257
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1258
    }
1259
    sdbRelease(pSdb, pVgroup);
288✔
1260
  }
1261

1262
  // stb info
1263
  pIter = NULL;
144✔
1264
  while (1) {
×
1265
    SStbObj *pStb = NULL;
144✔
1266
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
144✔
1267
    if (pIter == NULL) break;
144✔
1268

1269
    SMonStbDesc desc = {0};
×
1270

1271
    SName name1 = {0};
×
1272
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1273
    if (code < 0) {
×
1274
      mError("failed to get db name since %s", tstrerror(code));
×
1275
      sdbRelease(pSdb, pStb);
×
1276
      TAOS_RETURN(code);
×
1277
    }
1278
    (void)tNameGetDbName(&name1, desc.database_name);
×
1279

1280
    SName name2 = {0};
×
1281
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1282
    if (code < 0) {
×
1283
      mError("failed to get table name since %s", tstrerror(code));
×
1284
      sdbRelease(pSdb, pStb);
×
1285
      TAOS_RETURN(code);
×
1286
    }
1287
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1288

1289
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1290
      mError("failed to put stb into array, but continue at this monitor report");
×
1291
    }
1292
    sdbRelease(pSdb, pStb);
×
1293
  }
1294

1295
  // grant info
1296
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
144✔
1297
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
144✔
1298
  if (pMnode->grant.expireTimeMS == 0) {
144✔
1299
    pGrantInfo->expire_time = 0;
×
1300
    pGrantInfo->timeseries_total = 0;
×
1301
  }
1302

1303
  mndReleaseRpc(pMnode);
144✔
1304
  TAOS_RETURN(code);
144✔
1305
}
1306

1307
int32_t mndResetTimer(SMnode *pMnode){
×
1308
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1309
}
1310

1311
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
22,800,850✔
1312
  mTrace("mnode get load");
22,800,850✔
1313
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
22,800,850✔
1314
  pLoad->syncState = state.state;
22,800,850✔
1315
  pLoad->syncRestore = state.restored;
22,800,850✔
1316
  pLoad->syncTerm = state.term;
22,800,850✔
1317
  pLoad->roleTimeMs = state.roleTimeMs;
22,800,850✔
1318
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
22,800,850✔
1319
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1320
  return 0;
22,800,850✔
1321
}
1322

1323
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
6,704,067✔
1324
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
6,704,067✔
1325
  return state.roleTimeMs;
6,704,067✔
1326
}
1327

1328
void mndSetRestored(SMnode *pMnode, bool restored) {
501,707✔
1329
  if (restored) {
501,707✔
1330
    (void)taosThreadRwlockWrlock(&pMnode->lock);
501,707✔
1331
    pMnode->restored = true;
501,707✔
1332
    (void)taosThreadRwlockUnlock(&pMnode->lock);
501,707✔
1333
    mInfo("mnode set restored:%d", restored);
501,707✔
1334
  } else {
1335
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1336
    pMnode->restored = false;
×
1337
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1338
    mInfo("mnode set restored:%d", restored);
×
1339
    while (1) {
1340
      if (pMnode->rpcRef <= 0) break;
×
1341
      taosMsleep(3);
×
1342
    }
1343
  }
1344
}
501,707✔
1345

1346
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1347

1348
void mndSetStop(SMnode *pMnode) {
502,305✔
1349
  (void)taosThreadRwlockWrlock(&pMnode->lock);
502,305✔
1350
  pMnode->stopped = true;
502,305✔
1351
  (void)taosThreadRwlockUnlock(&pMnode->lock);
502,305✔
1352
  mInfo("mnode set stopped");
502,305✔
1353
}
502,305✔
1354

1355
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
460,015,807✔
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