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

taosdata / TDengine / #4927

14 Jan 2026 07:42AM UTC coverage: 66.12% (+0.07%) from 66.053%
#4927

push

travis-ci

web-flow
fix memleak decimal (#34283)

2 of 16 new or added lines in 1 file covered. (12.5%)

476 existing lines in 124 files now uncovered.

200574 of 303348 relevant lines covered (66.12%)

130242324.59 hits per line

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

78.42
/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 "mndShow.h"
48
#include "mndSma.h"
49
#include "mndSnode.h"
50
#include "mndSsMigrate.h"
51
#include "mndStb.h"
52
#include "mndStream.h"
53
#include "mndSubscribe.h"
54
#include "mndSync.h"
55
#include "mndTelem.h"
56
#include "mndTopic.h"
57
#include "mndTrans.h"
58
#include "mndUser.h"
59
#include "mndToken.h"
60
#include "mndVgroup.h"
61
#include "mndView.h"
62
#include "mndXnode.h"
63
#include "tencrypt.h"
64

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
248,052,001✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
248,052,001✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
248,055,247✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
248,053,896✔
93
}
248,054,375✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
63,188,988✔
96
  terrno = 0;
63,188,988✔
97
  SMTimerReq timerReq = {0};
63,188,839✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
63,188,839✔
100
  if (contLen <= 0) return NULL;
63,188,783✔
101
  void *pReq = rpcMallocCont(contLen);
63,188,783✔
102
  if (pReq == NULL) return NULL;
63,187,797✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
63,187,797✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
63,187,694✔
108
  return pReq;
63,187,347✔
109
}
110

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

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

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

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

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

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

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

194
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
195
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
196
    return;
×
197
  }
198

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

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

218
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
12,861,996✔
219
  mTrace("pullup arb hb");
12,861,996✔
220
  int32_t contLen = 0;
12,861,996✔
221
  void   *pReq = mndBuildTimerMsg(&contLen);
12,861,996✔
222
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
12,861,996✔
223
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
12,861,996✔
224
}
225

226
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
8,520,839✔
227
  mTrace("pullup arb sync");
8,520,839✔
228
  int32_t contLen = 0;
8,520,839✔
229
  void   *pReq = mndBuildTimerMsg(&contLen);
8,520,839✔
230
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
8,520,839✔
231
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
8,520,839✔
232
}
233

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

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

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

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

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

305
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
109,918✔
306
  SSdb *pSdb = pMnode->pSdb;
109,918✔
307

308
  void *pIter = NULL;
109,918✔
309
  while (1) {
396,957✔
310
    SVgObj *pVgroup = NULL;
506,875✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
506,875✔
312
    if (pIter == NULL) break;
506,875✔
313

314
    bool stateChanged = false;
396,957✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,017,830✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
777,426✔
317
      if (pGid->dnodeId == dnodeId) {
777,426✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
156,553✔
319
          mInfo(
56,787✔
320
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
321
              "restored:0 "
322
              "canRead:0",
323
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
324
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
56,787✔
325
          pGid->syncRestore = 0;
56,787✔
326
          pGid->syncCanRead = 0;
56,787✔
327
          pGid->startTimeMs = 0;
56,787✔
328
          pGid->learnerProgress = 0;
56,787✔
329
          pGid->snapSeq = -1;
56,787✔
330
          stateChanged = true;
56,787✔
331
        }
332
        break;
156,553✔
333
      }
334
    }
335

336
    if (stateChanged) {
396,957✔
337
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
56,787✔
338
      if (pDb != NULL && pDb->stateTs != curMs) {
56,787✔
339
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
31,510✔
340
              curMs);
341
        pDb->stateTs = curMs;
31,510✔
342
      }
343
      mndReleaseDb(pMnode, pDb);
56,787✔
344
    }
345

346
    sdbRelease(pSdb, pVgroup);
396,957✔
347
  }
348
}
109,918✔
349

350
static void mndCheckDnodeOffline(SMnode *pMnode) {
5,033,293✔
351
  mTrace("check dnode offline");
5,033,293✔
352
  if (mndAcquireRpc(pMnode) != 0) return;
5,033,293✔
353

354
  SSdb   *pSdb = pMnode->pSdb;
5,033,293✔
355
  int64_t curMs = taosGetTimestampMs();
5,033,293✔
356

357
  void *pIter = NULL;
5,033,293✔
358
  while (1) {
8,365,313✔
359
    SDnodeObj *pDnode = NULL;
13,398,606✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,398,606✔
361
    if (pIter == NULL) break;
13,398,606✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,365,313✔
364
    if (!online) {
8,365,313✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
109,918✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
109,918✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
8,365,313✔
370
  }
371

372
  mndReleaseRpc(pMnode);
5,033,293✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
297,375,895✔
376
  terrno = 0;
297,375,895✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
297,376,110✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
297,377,767✔
379
  if (terrno != 0) {
297,377,989✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
297,377,327✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,777,168✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,777,168✔
387
    return true;
10,777,168✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
286,600,159✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
9,555✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
7,679✔
392
    return true;
7,679✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
286,591,266✔
395
  return false;
286,592,359✔
396
}
397

398
static int32_t minCronTime() {
×
399
  int32_t min = INT32_MAX;
×
400
  min = TMIN(min, tsTtlPushIntervalSec);
×
401
  min = TMIN(min, tsTrimVDbIntervalSec);
×
402
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
403
  min = TMIN(min, tsTransPullupInterval);
×
404
  min = TMIN(min, tsCompactPullupInterval);
×
405
  min = TMIN(min, tsMqRebalanceInterval);
×
406

407
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
408
  min = TMIN(min, telemInt);
×
409
  min = TMIN(min, tsGrantHBInterval);
×
410
  min = TMIN(min, tsUptimeInterval);
×
411

412
  return min <= 1 ? 2 : min;
×
413
}
414
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
25,911,216✔
415
  int32_t code = 0;
25,911,216✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
25,911,216✔
418
    mndPullupGrant(pMnode);
1,148,050✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
25,911,216✔
421
    mndPullupTtl(pMnode);
2,520,796✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
25,911,216✔
425
    mndPullupTrimDb(pMnode);
×
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
25,911,216✔
429
    mndPullupQueryTrimDb(pMnode);
2,490,796✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
25,911,216✔
434
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
435
      mndPullupUpdateSsMigrateProgress(pMnode);
×
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
25,911,216✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
25,911,216✔
450
    mndPullupTrans(pMnode);
12,861,359✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
25,911,216✔
454
    mndPullupCompacts(pMnode);
2,431,451✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
25,911,216✔
458
    mndPullupScans(pMnode);
2,431,451✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
25,911,216✔
461
    mndPullupInstances(pMnode);
5,034,073✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
25,911,216✔
465
    mndCalMqRebalance(pMnode);
12,861,189✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
25,911,216✔
469
    mndPullupTelem(pMnode);
132✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
25,911,216✔
472
    mndIncreaseUpTime(pMnode);
26,977✔
473
  }
474
}
25,911,216✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
260,681,926✔
477
  int32_t code = 0;
260,681,926✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
260,681,926✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
12,861,996✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
260,681,926✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
8,520,839✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
260,681,926✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
260,681,926✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
260,681,926✔
495
    mndCheckDnodeOffline(pMnode);
5,033,293✔
496
  }
497
}
260,681,926✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
25,911,216✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
25,911,216✔
501
    mndSyncCheckTimeout(pMnode);
728,570✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
25,911,216✔
504
    msmHealthCheck(pMnode);
8,522,067✔
505
  }
506
}
25,911,216✔
507

508
static void *mndThreadSecFp(void *param) {
397,275✔
509
  SMnode *pMnode = param;
397,275✔
510
  int64_t lastTime = 0;
397,275✔
511
  setThreadName("mnode-timer");
397,275✔
512

513
  while (1) {
270,546,188✔
514
    lastTime++;
270,943,463✔
515
    taosMsleep(100);
270,943,463✔
516

517
    if (mndGetStop(pMnode)) break;
270,943,463✔
518
    if (lastTime % 10 != 0) continue;
270,546,188✔
519

520
    if (mnodeIsNotLeader(pMnode)) {
26,877,794✔
521
      mTrace("timer not process since mnode is not leader");
966,578✔
522
      continue;
966,578✔
523
    }
524

525
    int64_t sec = lastTime / 10;
25,911,216✔
526
    mndDoTimerCheckSync(pMnode, sec);
25,911,216✔
527

528
    mndDoTimerPullupTask(pMnode, sec);
25,911,216✔
529
  }
530

531
  return NULL;
397,275✔
532
}
533

534
static void *mndThreadMsFp(void *param) {
397,275✔
535
  SMnode *pMnode = param;
397,275✔
536
  int64_t lastTime = 0;
397,275✔
537
  setThreadName("mnode-arb-timer");
397,275✔
538

539
  while (1) {
540
    lastTime += 100;
270,897,470✔
541
    taosMsleep(100);
270,897,470✔
542

543
    if (mndGetStop(pMnode)) break;
270,897,470✔
544
    if (lastTime % 10 != 0) continue;
270,500,195✔
545

546
    if (mnodeIsNotLeader(pMnode)) {
270,500,195✔
547
      mTrace("timer not process since mnode is not leader");
9,818,269✔
548
      continue;
9,818,269✔
549
    }
550

551
    mndDoTimerCheckStatus(pMnode, lastTime);
260,681,926✔
552

553
    mndDoArbTimerPullupTask(pMnode, lastTime);
260,681,926✔
554
  }
555

556
  return NULL;
397,275✔
557
}
558

559
static int32_t mndInitTimer(SMnode *pMnode) {
397,275✔
560
  int32_t      code = 0;
397,275✔
561
  TdThreadAttr thAttr;
397,141✔
562
  (void)taosThreadAttrInit(&thAttr);
397,275✔
563
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
397,275✔
564
#ifdef TD_COMPACT_OS
565
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
566
#endif
567
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
397,275✔
568
    mError("failed to create timer thread since %s", tstrerror(code));
×
569
    TAOS_RETURN(code);
×
570
  }
571

572
  (void)taosThreadAttrDestroy(&thAttr);
397,275✔
573
  tmsgReportStartup("mnode-timer", "initialized");
397,275✔
574

575
  TdThreadAttr arbAttr;
397,141✔
576
  (void)taosThreadAttrInit(&arbAttr);
397,275✔
577
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
397,275✔
578
#ifdef TD_COMPACT_OS
579
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
580
#endif
581
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
397,275✔
582
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
583
    TAOS_RETURN(code);
×
584
  }
585

586
  (void)taosThreadAttrDestroy(&arbAttr);
397,275✔
587
  tmsgReportStartup("mnode-timer", "initialized");
397,275✔
588
  TAOS_RETURN(code);
397,275✔
589
}
590

591
static void mndCleanupTimer(SMnode *pMnode) {
397,275✔
592
  if (taosCheckPthreadValid(pMnode->thread)) {
397,275✔
593
    (void)taosThreadJoin(pMnode->thread, NULL);
397,275✔
594
    taosThreadClear(&pMnode->thread);
397,275✔
595
  }
596
  if (taosCheckPthreadValid(pMnode->arbThread)) {
397,275✔
597
    (void)taosThreadJoin(pMnode->arbThread, NULL);
397,275✔
598
    taosThreadClear(&pMnode->arbThread);
397,275✔
599
  }
600
}
397,275✔
601

602
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
397,334✔
603
  int32_t code = 0;
397,334✔
604
  pMnode->path = taosStrdup(path);
397,334✔
605
  if (pMnode->path == NULL) {
397,334✔
606
    code = terrno;
×
607
    TAOS_RETURN(code);
×
608
  }
609

610
  if (taosMkDir(pMnode->path) != 0) {
397,334✔
611
    code = terrno;
×
612
    TAOS_RETURN(code);
×
613
  }
614

615
  TAOS_RETURN(code);
397,334✔
616
}
617

618
static int32_t mndInitWal(SMnode *pMnode) {
397,334✔
619
  int32_t code = 0;
397,334✔
620
  char    path[PATH_MAX + 20] = {0};
397,334✔
621
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
397,334✔
622
  SWalCfg cfg = {.vgId = 1,
397,334✔
623
                 .fsyncPeriod = 0,
624
                 .rollPeriod = -1,
625
                 .segSize = -1,
626
                 .committed = -1,
627
                 .retentionPeriod = 0,
628
                 .retentionSize = 0,
629
                 .level = TAOS_WAL_FSYNC,
630
                 .encryptAlgr = 0,
631
                 .encryptData = {0}};
632

633
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
634
  if (taosWaitCfgKeyLoaded() != 0) {
397,334✔
635
    code = terrno;
×
636
    TAOS_RETURN(code);
×
637
  }
638
  if (tsMetaKey[0] != '\0') {
397,334✔
639
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
3,686✔
640
  }
641
#endif
642

643
  pMnode->pWal = walOpen(path, &cfg);
397,334✔
644
  if (pMnode->pWal == NULL) {
397,334✔
645
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
646
    if (terrno != 0) code = terrno;
×
647
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
648
    TAOS_RETURN(code);
×
649
  }
650

651
  TAOS_RETURN(code);
397,334✔
652
}
653

654
static void mndCloseWal(SMnode *pMnode) {
397,275✔
655
  if (pMnode->pWal != NULL) {
397,275✔
656
    walClose(pMnode->pWal);
397,275✔
657
    pMnode->pWal = NULL;
397,275✔
658
  }
659
}
397,275✔
660

661
static int32_t mndInitSdb(SMnode *pMnode) {
397,334✔
662
  int32_t code = 0;
397,334✔
663
  SSdbOpt opt = {0};
397,334✔
664
  opt.path = pMnode->path;
397,334✔
665
  opt.pMnode = pMnode;
397,334✔
666
  opt.pWal = pMnode->pWal;
397,334✔
667

668
  pMnode->pSdb = sdbInit(&opt);
397,334✔
669
  if (pMnode->pSdb == NULL) {
397,334✔
670
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
671
    if (terrno != 0) code = terrno;
×
672
    TAOS_RETURN(code);
×
673
  }
674

675
  TAOS_RETURN(code);
397,334✔
676
}
677

678
static int32_t mndOpenSdb(SMnode *pMnode) {
397,334✔
679
  int32_t code = 0;
397,334✔
680
  if (!pMnode->deploy) {
397,334✔
681
    code = sdbReadFile(pMnode->pSdb);
113,473✔
682
  }
683

684
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
397,334✔
685

686
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
397,334✔
687
  return code;
397,334✔
688
}
689

690
static void mndCleanupSdb(SMnode *pMnode) {
397,275✔
691
  if (pMnode->pSdb) {
397,275✔
692
    sdbCleanup(pMnode->pSdb);
397,275✔
693
    pMnode->pSdb = NULL;
397,275✔
694
  }
695
}
397,275✔
696

697
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,866,700✔
698
  SMnodeStep step = {0};
19,866,700✔
699
  step.name = name;
19,866,700✔
700
  step.initFp = initFp;
19,866,700✔
701
  step.cleanupFp = cleanupFp;
19,866,700✔
702
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
39,733,400✔
703
    TAOS_RETURN(terrno);
×
704
  }
705

706
  TAOS_RETURN(0);
19,866,700✔
707
}
708

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

765
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
397,275✔
766
  if (pMnode->pSteps == NULL) return;
397,275✔
767

768
  if (pos == -1) {
397,275✔
769
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
397,275✔
770
  }
771

772
  for (int32_t s = pos; s >= 0; s--) {
20,261,025✔
773
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,863,750✔
774
    mInfo("%s will cleanup", pStep->name);
19,863,750✔
775
    if (pStep->cleanupFp != NULL) {
19,863,750✔
776
      (*pStep->cleanupFp)(pMnode);
19,069,200✔
777
    }
778
  }
779

780
  taosArrayClear(pMnode->pSteps);
397,275✔
781
  taosArrayDestroy(pMnode->pSteps);
397,275✔
782
  pMnode->pSteps = NULL;
397,275✔
783
}
784

785
static int32_t mndExecSteps(SMnode *pMnode) {
397,334✔
786
  int32_t code = 0;
397,334✔
787
  int32_t size = taosArrayGetSize(pMnode->pSteps);
397,334✔
788
  for (int32_t pos = 0; pos < size; pos++) {
20,264,034✔
789
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,866,700✔
790
    if (pStep->initFp == NULL) continue;
19,866,700✔
791

792
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
19,866,700✔
793
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
794
      mndCleanupSteps(pMnode, pos);
×
795
      TAOS_RETURN(code);
×
796
    } else {
797
      mInfo("%s is initialized", pStep->name);
19,866,700✔
798
      tmsgReportStartup(pStep->name, "initialized");
19,866,700✔
799
    }
800
  }
801

802
  pMnode->clusterId = mndGetClusterId(pMnode);
397,334✔
803
  TAOS_RETURN(0);
397,334✔
804
}
805

806
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
397,334✔
807
  pMnode->msgCb = pOption->msgCb;
397,334✔
808
  pMnode->selfDnodeId = pOption->dnodeId;
397,334✔
809
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
397,334✔
810
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
397,334✔
811
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
397,334✔
812
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
397,334✔
813
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
397,334✔
814
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
397,334✔
815
}
397,334✔
816

817
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
397,334✔
818
  terrno = 0;
397,334✔
819
  mInfo("start to open mnode in %s", path);
397,334✔
820

821
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
397,334✔
822
  if (pMnode == NULL) {
397,334✔
823
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
824
    mError("failed to open mnode in step 1, since %s", terrstr());
×
825
    return NULL;
×
826
  }
827
  (void)memset(pMnode, 0, sizeof(SMnode));
397,334✔
828

829
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
397,334✔
830
  if (code != 0) {
397,334✔
831
    taosMemoryFree(pMnode);
×
832
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
833
    terrno = code;
×
834
    return NULL;
×
835
  }
836

837
  char timestr[24] = "1970-01-01 00:00:00.00";
397,334✔
838
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
397,334✔
839
  if (code < 0) {
397,334✔
840
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
841
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
842
    taosMemoryFree(pMnode);
×
843
    terrno = code;
×
844
    return NULL;
×
845
  }
846

847
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
397,334✔
848
        pOption->numOfTotalReplicas);
849
  mndSetOptions(pMnode, pOption);
397,334✔
850

851
  pMnode->deploy = pOption->deploy;
397,334✔
852
  pMnode->version = pOption->version;
397,334✔
853
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
397,334✔
854
  if (pMnode->pSteps == NULL) {
397,334✔
855
    taosMemoryFree(pMnode);
×
856
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
857
    mError("failed to open mnode in step 4, since %s", terrstr());
×
858
    return NULL;
×
859
  }
860

861
  code = mndCreateDir(pMnode, path);
397,334✔
862
  if (code != 0) {
397,334✔
863
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
864
    mndClose(pMnode);
×
865
    terrno = code;
×
866
    return NULL;
×
867
  }
868

869
  code = mndInitSteps(pMnode);
397,334✔
870
  if (code != 0) {
397,334✔
871
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
872
    mndClose(pMnode);
×
873
    terrno = code;
×
874
    return NULL;
×
875
  }
876

877
  code = mndExecSteps(pMnode);
397,334✔
878
  if (code != 0) {
397,334✔
879
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
880
    mndClose(pMnode);
×
881
    terrno = code;
×
882
    return NULL;
×
883
  }
884

885
  mInfo("mnode open successfully");
397,334✔
886
  return pMnode;
397,334✔
887
}
888

889
void mndPreClose(SMnode *pMnode) {
397,275✔
890
  if (pMnode != NULL) {
397,275✔
891
    int32_t code = 0;
397,275✔
892
    // TODO check return value
893
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
397,275✔
894
    if (code < 0) {
397,275✔
895
      mError("failed to transfer leader since %s", tstrerror(code));
×
896
    }
897
    syncPreStop(pMnode->syncMgmt.sync);
397,275✔
898
    code = sdbWriteFile(pMnode->pSdb, 0);
397,275✔
899
    if (code < 0) {
397,275✔
900
      mError("failed to write sdb since %s", tstrerror(code));
707✔
901
    }
902
  }
903
}
397,275✔
904

905
void mndClose(SMnode *pMnode) {
397,275✔
906
  if (pMnode != NULL) {
397,275✔
907
    mInfo("start to close mnode");
397,275✔
908
    mndCleanupSteps(pMnode, -1);
397,275✔
909
    taosMemoryFreeClear(pMnode->path);
397,275✔
910
    taosMemoryFreeClear(pMnode);
397,275✔
911
    mInfo("mnode is closed");
397,275✔
912
  }
913
}
397,275✔
914

915
int32_t mndStart(SMnode *pMnode) {
397,275✔
916
  mndSyncStart(pMnode);
397,275✔
917
  if (pMnode->deploy) {
397,275✔
918
    if (sdbDeploy(pMnode->pSdb) != 0) {
283,861✔
919
      mError("failed to deploy sdb while start mnode");
×
920
      return -1;
×
921
    }
922
    mndSetRestored(pMnode, true);
283,861✔
923
  }
924
  if (mndIsLeader(pMnode)) {
397,275✔
925
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
357,099✔
926
      mError("failed to upgrade sdb while start mnode");
×
927
      return -1;
×
928
    }
929
  }
930
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
397,275✔
931
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
397,275✔
932

933
  return mndInitTimer(pMnode);
397,275✔
934
}
935

936
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
397,275✔
937

938
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
312,471✔
939

940
int32_t mndIsCatchUp(SMnode *pMnode) {
226,802✔
941
  int64_t rid = pMnode->syncMgmt.sync;
226,802✔
942
  return syncIsCatchUp(rid);
226,802✔
943
}
944

945
ESyncRole mndGetRole(SMnode *pMnode) {
226,802✔
946
  int64_t rid = pMnode->syncMgmt.sync;
226,802✔
947
  return syncGetRole(rid);
226,802✔
948
}
949

950
int64_t mndGetTerm(SMnode *pMnode) {
8,618,573✔
951
  int64_t rid = pMnode->syncMgmt.sync;
8,618,573✔
952
  return syncGetTerm(rid);
8,618,573✔
953
}
954

955
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,480,580✔
956

957
void mndStop(SMnode *pMnode) {
397,275✔
958
  mndSetStop(pMnode);
397,275✔
959
  mndSyncStop(pMnode);
397,275✔
960
  mndCleanupTimer(pMnode);
397,275✔
961
}
397,275✔
962

963
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
49,851,428✔
964
  SMnode    *pMnode = pMsg->info.node;
49,851,428✔
965
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
49,851,428✔
966

967
  const STraceId *trace = &pMsg->info.traceId;
49,851,428✔
968
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
49,851,428✔
969

970
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
49,851,428✔
971
  if (code != 0) {
49,851,428✔
972
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
471✔
973
            tstrerror(code), code);
974
  }
975

976
  return code;
49,851,428✔
977
}
978

979
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
246,178,486✔
980
  int32_t code = 0;
246,178,486✔
981
  if (!IsReq(pMsg)) TAOS_RETURN(code);
246,178,486✔
982
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
216,553,740✔
983
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
211,963,761✔
984
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
208,194,419✔
985
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
199,174,835✔
986
    TAOS_RETURN(code);
17,379,630✔
987
  }
988

989
  SMnode *pMnode = pMsg->info.node;
199,176,983✔
990
  (void)taosThreadRwlockRdlock(&pMnode->lock);
199,173,778✔
991
  if (pMnode->stopped) {
199,176,989✔
992
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,100✔
993
    code = TSDB_CODE_APP_IS_STOPPING;
6,100✔
994
    TAOS_RETURN(code);
6,100✔
995
  }
996

997
  terrno = 0;
199,167,735✔
998
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
199,167,149✔
999
  if (terrno != 0) {
199,172,567✔
1000
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1001
    code = terrno;
×
1002
    TAOS_RETURN(code);
×
1003
  }
1004

1005
  if (state.state != TAOS_SYNC_STATE_LEADER) {
199,169,859✔
1006
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,882,493✔
1007
    code = TSDB_CODE_SYN_NOT_LEADER;
1,882,493✔
1008
    goto _OVER;
1,882,493✔
1009
  }
1010

1011
  if (!state.restored || !pMnode->restored) {
197,287,366✔
1012
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,279,535✔
1013
    code = TSDB_CODE_SYN_RESTORING;
1,278,084✔
1014
    goto _OVER;
1,278,084✔
1015
  }
1016

1017
#if 1
1018
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
196,009,214✔
1019
#else
1020
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1021
  mTrace("mnode rpc is acquired, ref:%d", ref);
1022
#endif
1023

1024
  (void)taosThreadRwlockUnlock(&pMnode->lock);
196,012,362✔
1025
  TAOS_RETURN(code);
196,011,861✔
1026

1027
_OVER:
3,160,577✔
1028
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,160,577✔
1029
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,160,577✔
1030
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,160,494✔
1031
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,160,577✔
1032
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,160,497✔
1033
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,160,577✔
1034
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,160,414✔
1035
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,160,497✔
1036
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,160,497✔
1037
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
992✔
1038
           pMnode->stopped, state.restored, syncStr(state.state));
1039
    TAOS_RETURN(code);
992✔
1040
  }
1041

1042
  const STraceId *trace = &pMsg->info.traceId;
3,159,674✔
1043
  SEpSet          epSet = {0};
3,159,733✔
1044
  mndGetMnodeEpSet(pMnode, &epSet);
3,159,863✔
1045

1046
  mGDebug(
3,160,492✔
1047
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1048
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1049
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1050
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1051

1052
  if (epSet.numOfEps <= 0) return -1;
3,160,577✔
1053

1054
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
9,953,736✔
1055
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,793,159✔
1056
  }
1057

1058
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,160,577✔
1059
  pMsg->info.rsp = rpcMallocCont(contLen);
3,159,840✔
1060
  if (pMsg->info.rsp != NULL) {
3,159,661✔
1061
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,159,840✔
1062
      mError("failed to serialize ep set");
×
1063
    }
1064
    pMsg->info.hasEpSet = 1;
3,158,369✔
1065
    pMsg->info.rspLen = contLen;
3,159,508✔
1066
  }
1067

1068
  TAOS_RETURN(code);
3,160,674✔
1069
}
1070

1071
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
246,188,382✔
1072
  SMnode         *pMnode = pMsg->info.node;
246,188,382✔
1073
  const STraceId *trace = &pMsg->info.traceId;
246,188,382✔
1074
  int32_t         code = TSDB_CODE_SUCCESS;
246,187,149✔
1075

1076
#ifdef TD_ENTERPRISE
1077
  if (pMsg->info.conn.isToken) {
246,187,149✔
1078
    SCachedTokenInfo ti = {0};
546✔
1079
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
546✔
1080
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1081
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
×
1082
      TAOS_RETURN(code);
×
1083
    }
1084
    if (ti.enabled == 0) {
546✔
1085
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1086
      code = TSDB_CODE_MND_TOKEN_DISABLED;
×
1087
      TAOS_RETURN(code);
×
1088
    }
1089
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
546✔
1090
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1091
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1092
      TAOS_RETURN(code);
×
1093
    }
1094
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
546✔
1095
  }
1096
#endif
1097

1098
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
246,179,234✔
1099
  MndMsgFpExt fpExt = NULL;
246,187,218✔
1100
  if (fp == NULL) {
246,187,218✔
1101
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,383,978✔
1102
    if (fpExt == NULL) {
17,384,123✔
1103
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1104
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1105
      TAOS_RETURN(code);
×
1106
    }
1107
  }
1108

1109
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
246,187,363✔
1110

1111
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
243,016,334✔
1112
  if (fp)
243,017,557✔
1113
    code = (*fp)(pMsg);
225,633,596✔
1114
  else
1115
    code = (*fpExt)(pMsg, pQueueInfo);
17,383,961✔
1116
  mndReleaseRpc(pMnode);
243,018,630✔
1117

1118
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
243,021,729✔
1119
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,647,515✔
1120
  } else if (code == 0) {
215,374,214✔
1121
    mGTrace("msg:%p, successfully processed", pMsg);
212,935,053✔
1122
  } else {
1123
    // TODO removve this wrong set code
1124
    if (code == -1) {
2,439,161✔
1125
      code = terrno;
×
1126
    }
1127
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,439,161✔
1128
            TMSG_INFO(pMsg->msgType));
1129
  }
1130

1131
  TAOS_RETURN(code);
243,021,729✔
1132
}
1133

1134
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
92,181,488✔
1135
  tmsg_t type = TMSG_INDEX(msgType);
92,181,488✔
1136
  if (type < TDMT_MAX) {
92,181,488✔
1137
    pMnode->msgFp[type] = fp;
92,181,488✔
1138
  }
1139
}
92,181,488✔
1140

1141
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,178,672✔
1142
  tmsg_t type = TMSG_INDEX(msgType);
3,178,672✔
1143
  if (type < TDMT_MAX) {
3,178,672✔
1144
    pMnode->msgFpExt[type] = fp;
3,178,672✔
1145
  }
1146
}
3,178,672✔
1147

1148
// Note: uid 0 is reserved
1149
int64_t mndGenerateUid(const char *name, int32_t len) {
9,545,119✔
1150
  int32_t hashval = MurmurHash3_32(name, len);
9,545,119✔
1151
  do {
×
1152
    int64_t us = taosGetTimestampUs();
9,545,148✔
1153
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,545,148✔
1154
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,545,148✔
1155
    if (uuid) {
9,545,148✔
1156
      return llabs(uuid);
9,545,148✔
1157
    }
1158
  } while (true);
1159
}
1160

1161
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
68✔
1162
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1163
  int32_t code = mndAcquireRpc(pMnode);
68✔
1164
  if (code < 0) {
68✔
1165
    TAOS_RETURN(code);
×
1166
  } else if (code == 1) {
68✔
1167
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1168
  }
1169

1170
  SSdb   *pSdb = pMnode->pSdb;
68✔
1171
  int64_t ms = taosGetTimestampMs();
68✔
1172

1173
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
68✔
1174
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
68✔
1175
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
68✔
1176
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
68✔
1177
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
68✔
1178
      pStbInfo->stbs == NULL) {
68✔
1179
    mndReleaseRpc(pMnode);
×
1180
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1181
    if (terrno != 0) code = terrno;
×
1182
    TAOS_RETURN(code);
×
1183
  }
1184

1185
  // cluster info
1186
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
68✔
1187
  pClusterInfo->monitor_interval = tsMonitorInterval;
68✔
1188
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
68✔
1189
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
68✔
1190
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
68✔
1191
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
68✔
1192
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
68✔
1193

1194
  void *pIter = NULL;
68✔
1195
  while (1) {
68✔
1196
    SDnodeObj *pObj = NULL;
136✔
1197
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
136✔
1198
    if (pIter == NULL) break;
136✔
1199

1200
    SMonDnodeDesc desc = {0};
68✔
1201
    desc.dnode_id = pObj->id;
68✔
1202
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
68✔
1203
    if (mndIsDnodeOnline(pObj, ms)) {
68✔
1204
      tstrncpy(desc.status, "ready", sizeof(desc.status));
68✔
1205
    } else {
1206
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1207
    }
1208
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
136✔
1209
      mError("failed put dnode into array, but continue at this monitor report")
×
1210
    }
1211
    sdbRelease(pSdb, pObj);
68✔
1212
  }
1213

1214
  pIter = NULL;
68✔
1215
  while (1) {
68✔
1216
    SMnodeObj *pObj = NULL;
136✔
1217
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
136✔
1218
    if (pIter == NULL) break;
136✔
1219

1220
    SMonMnodeDesc desc = {0};
68✔
1221
    desc.mnode_id = pObj->id;
68✔
1222
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
68✔
1223

1224
    if (pObj->id == pMnode->selfDnodeId) {
68✔
1225
      pClusterInfo->first_ep_dnode_id = pObj->id;
68✔
1226
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
68✔
1227
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1228
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
68✔
1229
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1230
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
68✔
1231
      desc.syncState = TAOS_SYNC_STATE_LEADER;
68✔
1232
    } else {
1233
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1234
      desc.syncState = pObj->syncState;
×
1235
    }
1236
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
136✔
1237
      mError("failed to put mnode into array, but continue at this monitor report");
×
1238
    }
1239
    sdbRelease(pSdb, pObj);
68✔
1240
  }
1241

1242
  // vgroup info
1243
  pIter = NULL;
68✔
1244
  while (1) {
136✔
1245
    SVgObj *pVgroup = NULL;
204✔
1246
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
204✔
1247
    if (pIter == NULL) break;
204✔
1248

1249
    if (pVgroup->mountVgId) {
136✔
1250
      sdbRelease(pSdb, pVgroup);
×
1251
      continue;
×
1252
    }
1253

1254
    pClusterInfo->vgroups_total++;
136✔
1255
    pClusterInfo->tbs_total += pVgroup->numOfTables;
136✔
1256

1257
    SMonVgroupDesc desc = {0};
136✔
1258
    desc.vgroup_id = pVgroup->vgId;
136✔
1259

1260
    SName name = {0};
136✔
1261
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
136✔
1262
    if (code < 0) {
136✔
1263
      mError("failed to get db name since %s", tstrerror(code));
×
1264
      sdbCancelFetch(pSdb, pIter);
×
1265
      sdbRelease(pSdb, pVgroup);
×
1266
      TAOS_RETURN(code);
×
1267
    }
1268
    (void)tNameGetDbName(&name, desc.database_name);
136✔
1269

1270
    desc.tables_num = pVgroup->numOfTables;
136✔
1271
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
136✔
1272
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
136✔
1273
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
272✔
1274
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
136✔
1275
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
136✔
1276
      pVnDesc->dnode_id = pVgid->dnodeId;
136✔
1277
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
136✔
1278
      pVnDesc->syncState = pVgid->syncState;
136✔
1279
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
136✔
1280
        tstrncpy(desc.status, "ready", sizeof(desc.status));
136✔
1281
        pClusterInfo->vgroups_alive++;
136✔
1282
      }
1283
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
136✔
1284
        pClusterInfo->vnodes_alive++;
136✔
1285
      }
1286
      pClusterInfo->vnodes_total++;
136✔
1287
    }
1288

1289
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
272✔
1290
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1291
    }
1292
    sdbRelease(pSdb, pVgroup);
136✔
1293
  }
1294

1295
  // stb info
1296
  pIter = NULL;
68✔
UNCOV
1297
  while (1) {
×
1298
    SStbObj *pStb = NULL;
68✔
1299
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
68✔
1300
    if (pIter == NULL) break;
68✔
1301

UNCOV
1302
    SMonStbDesc desc = {0};
×
1303

UNCOV
1304
    SName name1 = {0};
×
UNCOV
1305
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1306
    if (code < 0) {
×
1307
      mError("failed to get db name since %s", tstrerror(code));
×
1308
      sdbRelease(pSdb, pStb);
×
1309
      TAOS_RETURN(code);
×
1310
    }
UNCOV
1311
    (void)tNameGetDbName(&name1, desc.database_name);
×
1312

UNCOV
1313
    SName name2 = {0};
×
UNCOV
1314
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1315
    if (code < 0) {
×
1316
      mError("failed to get table name since %s", tstrerror(code));
×
1317
      sdbRelease(pSdb, pStb);
×
1318
      TAOS_RETURN(code);
×
1319
    }
UNCOV
1320
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1321

UNCOV
1322
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1323
      mError("failed to put stb into array, but continue at this monitor report");
×
1324
    }
UNCOV
1325
    sdbRelease(pSdb, pStb);
×
1326
  }
1327

1328
  // grant info
1329
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
68✔
1330
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
68✔
1331
  if (pMnode->grant.expireTimeMS == 0) {
68✔
1332
    pGrantInfo->expire_time = 0;
×
1333
    pGrantInfo->timeseries_total = 0;
×
1334
  }
1335

1336
  mndReleaseRpc(pMnode);
68✔
1337
  TAOS_RETURN(code);
68✔
1338
}
1339

1340
int32_t mndResetTimer(SMnode *pMnode){
×
1341
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1342
}
1343

1344
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
26,922,050✔
1345
  mTrace("mnode get load");
26,922,050✔
1346
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
26,922,050✔
1347
  pLoad->syncState = state.state;
26,922,050✔
1348
  pLoad->syncRestore = state.restored;
26,922,050✔
1349
  pLoad->syncTerm = state.term;
26,922,050✔
1350
  pLoad->roleTimeMs = state.roleTimeMs;
26,922,050✔
1351
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
26,922,050✔
1352
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1353
  return 0;
26,922,050✔
1354
}
1355

1356
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,520,839✔
1357
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,520,839✔
1358
  return state.roleTimeMs;
8,520,839✔
1359
}
1360

1361
void mndSetRestored(SMnode *pMnode, bool restored) {
397,275✔
1362
  if (restored) {
397,275✔
1363
    (void)taosThreadRwlockWrlock(&pMnode->lock);
397,275✔
1364
    pMnode->restored = true;
397,275✔
1365
    (void)taosThreadRwlockUnlock(&pMnode->lock);
397,275✔
1366
    mInfo("mnode set restored:%d", restored);
397,275✔
1367
  } else {
1368
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1369
    pMnode->restored = false;
×
1370
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1371
    mInfo("mnode set restored:%d", restored);
×
1372
    while (1) {
1373
      if (pMnode->rpcRef <= 0) break;
×
1374
      taosMsleep(3);
×
1375
    }
1376
  }
1377
}
397,275✔
1378

1379
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1380

1381
void mndSetStop(SMnode *pMnode) {
397,275✔
1382
  (void)taosThreadRwlockWrlock(&pMnode->lock);
397,275✔
1383
  pMnode->stopped = true;
397,275✔
1384
  (void)taosThreadRwlockUnlock(&pMnode->lock);
397,275✔
1385
  mInfo("mnode set stopped");
397,275✔
1386
}
397,275✔
1387

1388
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
541,828,449✔
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