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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

77.61
/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 "mndToken.h"
59
#include "mndVgroup.h"
60
#include "mndView.h"
61

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

81
static inline void mndReleaseRpc(SMnode *pMnode) {
237,972,606✔
82
  (void)taosThreadRwlockRdlock(&pMnode->lock);
237,972,606✔
83
#if 1
84
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
237,973,510✔
85
#else
86
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
87
  mTrace("mnode rpc is released, ref:%d", ref);
88
#endif
89
  (void)taosThreadRwlockUnlock(&pMnode->lock);
237,972,645✔
90
}
237,972,832✔
91

92
static void *mndBuildTimerMsg(int32_t *pContLen) {
54,356,573✔
93
  terrno = 0;
54,356,573✔
94
  SMTimerReq timerReq = {0};
54,356,706✔
95

96
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
54,356,706✔
97
  if (contLen <= 0) return NULL;
54,354,631✔
98
  void *pReq = rpcMallocCont(contLen);
54,354,631✔
99
  if (pReq == NULL) return NULL;
54,354,365✔
100

101
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
54,354,365✔
102
    mError("failed to serialize timer msg since %s", terrstr());
×
103
  }
104
  *pContLen = contLen;
54,353,323✔
105
  return pReq;
54,354,520✔
106
}
107

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

302
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
180,535✔
303
  SSdb *pSdb = pMnode->pSdb;
180,535✔
304

305
  void *pIter = NULL;
180,535✔
306
  while (1) {
849,393✔
307
    SVgObj *pVgroup = NULL;
1,029,928✔
308
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,029,928✔
309
    if (pIter == NULL) break;
1,029,928✔
310

311
    bool stateChanged = false;
849,393✔
312
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
2,181,859✔
313
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,665,890✔
314
      if (pGid->dnodeId == dnodeId) {
1,665,890✔
315
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
333,424✔
316
          mInfo(
143,487✔
317
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
318
              "restored:0 "
319
              "canRead:0",
320
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
321
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
143,487✔
322
          pGid->syncRestore = 0;
143,487✔
323
          pGid->syncCanRead = 0;
143,487✔
324
          pGid->startTimeMs = 0;
143,487✔
325
          stateChanged = true;
143,487✔
326
        }
327
        break;
333,424✔
328
      }
329
    }
330

331
    if (stateChanged) {
849,393✔
332
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
143,487✔
333
      if (pDb != NULL && pDb->stateTs != curMs) {
143,487✔
334
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
99,722✔
335
              curMs);
336
        pDb->stateTs = curMs;
99,722✔
337
      }
338
      mndReleaseDb(pMnode, pDb);
143,487✔
339
    }
340

341
    sdbRelease(pSdb, pVgroup);
849,393✔
342
  }
343
}
180,535✔
344

345
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,293,023✔
346
  mTrace("check dnode offline");
4,293,023✔
347
  if (mndAcquireRpc(pMnode) != 0) return;
4,293,023✔
348

349
  SSdb   *pSdb = pMnode->pSdb;
4,293,023✔
350
  int64_t curMs = taosGetTimestampMs();
4,293,023✔
351

352
  void *pIter = NULL;
4,293,023✔
353
  while (1) {
8,308,822✔
354
    SDnodeObj *pDnode = NULL;
12,601,845✔
355
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,601,845✔
356
    if (pIter == NULL) break;
12,601,845✔
357

358
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,308,822✔
359
    if (!online) {
8,308,822✔
360
      mInfo("dnode:%d, in offline state", pDnode->id);
180,535✔
361
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
180,535✔
362
    }
363

364
    sdbRelease(pSdb, pDnode);
8,308,822✔
365
  }
366

367
  mndReleaseRpc(pMnode);
4,293,023✔
368
}
369

370
static bool mnodeIsNotLeader(SMnode *pMnode) {
273,403,684✔
371
  terrno = 0;
273,403,684✔
372
  (void)taosThreadRwlockRdlock(&pMnode->lock);
273,405,151✔
373
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
273,409,750✔
374
  if (terrno != 0) {
273,410,140✔
375
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
376
    return true;
×
377
  }
378

379
  if (state.state != TAOS_SYNC_STATE_LEADER) {
273,410,114✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
26,180,667✔
381
    terrno = TSDB_CODE_SYN_NOT_LEADER;
26,180,667✔
382
    return true;
26,180,667✔
383
  }
384
  if (!state.restored || !pMnode->restored) {
247,229,447✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6,927✔
386
    terrno = TSDB_CODE_SYN_RESTORING;
3,508✔
387
    return true;
3,508✔
388
  }
389
  (void)taosThreadRwlockUnlock(&pMnode->lock);
247,222,520✔
390
  return false;
247,223,556✔
391
}
392

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

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

407
  return min <= 1 ? 2 : min;
×
408
}
409
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,307,423✔
410
  int32_t code = 0;
22,307,423✔
411
#ifndef TD_ASTRA
412
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,307,423✔
413
    mndPullupGrant(pMnode);
1,259,376✔
414
  }
415
  if (sec % tsTtlPushIntervalSec == 0) {
22,307,423✔
416
    mndPullupTtl(pMnode);
2,132,438✔
417
  }
418

419
  if (sec % tsTrimVDbIntervalSec == 0) {
22,307,423✔
420
    mndPullupTrimDb(pMnode);
×
421
  }
422

423
  if (sec % tsQueryTrimIntervalSec == 0) {
22,307,423✔
424
    mndPullupQueryTrimDb(pMnode);
2,107,186✔
425
  }
426
#endif
427
#ifdef USE_SHARED_STORAGE
428
  if (tsSsEnabled) {
22,307,423✔
429
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
430
      mndPullupUpdateSsMigrateProgress(pMnode);
×
431
    }
432
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
433
      mndPullupSsMigrateDb(pMnode);
×
434
    }
435
  }
436
#endif
437
#ifdef TD_ENTERPRISE
438
  if (tsAuthReq) {
22,307,423✔
439
    if (sec % tsAuthReqHBInterval == 0) {
×
440
      mndPullupAuth(pMnode);
×
441
    }
442
  }
443
#endif
444
  if (sec % tsTransPullupInterval == 0) {
22,307,423✔
445
    mndPullupTrans(pMnode);
11,051,275✔
446
  }
447

448
  if (sec % tsCompactPullupInterval == 0) {
22,307,423✔
449
    mndPullupCompacts(pMnode);
2,047,222✔
450
  }
451

452
  if (sec % tsScanPullupInterval == 0) {
22,307,423✔
453
    mndPullupScans(pMnode);
2,047,222✔
454
  }
455
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,307,423✔
456
    mndPullupInstances(pMnode);
4,294,593✔
457
  }
458
#ifdef USE_TOPIC
459
  if (sec % tsMqRebalanceInterval == 0) {
22,307,423✔
460
    mndCalMqRebalance(pMnode);
11,050,397✔
461
  }
462
#endif
463
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,307,423✔
464
    mndPullupTelem(pMnode);
268✔
465
  }
466
  if (sec % tsUptimeInterval == 0) {
22,307,423✔
467
    mndIncreaseUpTime(pMnode);
18,223✔
468
  }
469
}
22,307,423✔
470

471
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
224,918,923✔
472
  int32_t code = 0;
224,918,923✔
473
#ifndef TD_ASTRA
474
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
224,918,923✔
475
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,049,049✔
476
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
477
    }
478
  }
479

480
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
224,918,923✔
481
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,299,969✔
482
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
483
    }
484
  }
485
#endif
486
}
224,918,923✔
487

488
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
224,918,923✔
489
  if (ms % (tsStatusTimeoutMs) == 0) {
224,918,923✔
490
    mndCheckDnodeOffline(pMnode);
4,293,023✔
491
  }
492
}
224,918,923✔
493

494
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,307,423✔
495
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,307,423✔
496
    mndSyncCheckTimeout(pMnode);
591,553✔
497
  }
498
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,307,423✔
499
    msmHealthCheck(pMnode);
7,301,593✔
500
  }
501
}
22,307,423✔
502

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

508
  while (1) {
248,806,701✔
509
    lastTime++;
249,320,680✔
510
    taosMsleep(100);
249,320,680✔
511

512
    if (mndGetStop(pMnode)) break;
249,320,680✔
513
    if (lastTime % 10 != 0) continue;
248,806,701✔
514

515
    if (mnodeIsNotLeader(pMnode)) {
24,649,121✔
516
      mTrace("timer not process since mnode is not leader");
2,341,698✔
517
      continue;
2,341,698✔
518
    }
519

520
    int64_t sec = lastTime / 10;
22,307,423✔
521
    mndDoTimerCheckSync(pMnode, sec);
22,307,423✔
522

523
    mndDoTimerPullupTask(pMnode, sec);
22,307,423✔
524
  }
525

526
  return NULL;
513,979✔
527
}
528

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

534
  while (1) {
535
    lastTime += 100;
249,275,379✔
536
    taosMsleep(100);
249,275,379✔
537

538
    if (mndGetStop(pMnode)) break;
249,275,379✔
539
    if (lastTime % 10 != 0) continue;
248,761,400✔
540

541
    if (mnodeIsNotLeader(pMnode)) {
248,761,400✔
542
      mTrace("timer not process since mnode is not leader");
23,842,477✔
543
      continue;
23,842,477✔
544
    }
545

546
    mndDoTimerCheckStatus(pMnode, lastTime);
224,918,923✔
547

548
    mndDoArbTimerPullupTask(pMnode, lastTime);
224,918,923✔
549
  }
550

551
  return NULL;
513,979✔
552
}
553

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

567
  (void)taosThreadAttrDestroy(&thAttr);
513,979✔
568
  tmsgReportStartup("mnode-timer", "initialized");
513,979✔
569

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

581
  (void)taosThreadAttrDestroy(&arbAttr);
513,979✔
582
  tmsgReportStartup("mnode-timer", "initialized");
513,979✔
583
  TAOS_RETURN(code);
513,979✔
584
}
585

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

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

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

610
  TAOS_RETURN(code);
514,100✔
611
}
612

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

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

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

648
  TAOS_RETURN(code);
514,100✔
649
}
650

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

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

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

672
  TAOS_RETURN(code);
514,100✔
673
}
674

675
static int32_t mndOpenSdb(SMnode *pMnode) {
514,100✔
676
  int32_t code = 0;
514,100✔
677
  if (!pMnode->deploy) {
514,100✔
678
    code = sdbReadFile(pMnode->pSdb);
178,821✔
679
  }
680

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

683
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
514,100✔
684
  return code;
514,100✔
685
}
686

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

694
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
24,676,800✔
695
  SMnodeStep step = {0};
24,676,800✔
696
  step.name = name;
24,676,800✔
697
  step.initFp = initFp;
24,676,800✔
698
  step.cleanupFp = cleanupFp;
24,676,800✔
699
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
49,353,600✔
700
    TAOS_RETURN(terrno);
×
701
  }
702

703
  TAOS_RETURN(0);
24,676,800✔
704
}
705

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

760
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
513,979✔
761
  if (pMnode->pSteps == NULL) return;
513,979✔
762

763
  if (pos == -1) {
513,979✔
764
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
513,979✔
765
  }
766

767
  for (int32_t s = pos; s >= 0; s--) {
25,184,971✔
768
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
24,670,992✔
769
    mInfo("%s will cleanup", pStep->name);
24,670,992✔
770
    if (pStep->cleanupFp != NULL) {
24,670,992✔
771
      (*pStep->cleanupFp)(pMnode);
23,643,034✔
772
    }
773
  }
774

775
  taosArrayClear(pMnode->pSteps);
513,979✔
776
  taosArrayDestroy(pMnode->pSteps);
513,979✔
777
  pMnode->pSteps = NULL;
513,979✔
778
}
779

780
static int32_t mndExecSteps(SMnode *pMnode) {
514,100✔
781
  int32_t code = 0;
514,100✔
782
  int32_t size = taosArrayGetSize(pMnode->pSteps);
514,100✔
783
  for (int32_t pos = 0; pos < size; pos++) {
25,190,900✔
784
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
24,676,800✔
785
    if (pStep->initFp == NULL) continue;
24,676,800✔
786

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

797
  pMnode->clusterId = mndGetClusterId(pMnode);
514,100✔
798
  TAOS_RETURN(0);
514,100✔
799
}
800

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

812
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
514,100✔
813
  terrno = 0;
514,100✔
814
  mInfo("start to open mnode in %s", path);
514,100✔
815

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

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

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

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

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

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

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

877
  mInfo("mnode open successfully");
514,100✔
878
  return pMnode;
514,100✔
879
}
880

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

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

907
int32_t mndStart(SMnode *pMnode) {
513,979✔
908
  mndSyncStart(pMnode);
513,979✔
909
  if (pMnode->deploy) {
513,979✔
910
    if (sdbDeploy(pMnode->pSdb) != 0) {
335,279✔
911
      mError("failed to deploy sdb while start mnode");
×
912
      return -1;
×
913
    }
914
    mndSetRestored(pMnode, true);
335,279✔
915
  }
916
  if (mndIsLeader(pMnode)) {
513,979✔
917
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
417,154✔
918
      mError("failed to upgrade sdb while start mnode");
×
919
      return -1;
×
920
    }
921
  }
922
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
513,979✔
923
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
513,979✔
924

925
  return mndInitTimer(pMnode);
513,979✔
926
}
927

928
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
513,979✔
929

930
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
399,389✔
931

932
int32_t mndIsCatchUp(SMnode *pMnode) {
325,643✔
933
  int64_t rid = pMnode->syncMgmt.sync;
325,643✔
934
  return syncIsCatchUp(rid);
325,643✔
935
}
936

937
ESyncRole mndGetRole(SMnode *pMnode) {
325,643✔
938
  int64_t rid = pMnode->syncMgmt.sync;
325,643✔
939
  return syncGetRole(rid);
325,643✔
940
}
941

942
int64_t mndGetTerm(SMnode *pMnode) {
7,416,247✔
943
  int64_t rid = pMnode->syncMgmt.sync;
7,416,247✔
944
  return syncGetTerm(rid);
7,416,247✔
945
}
946

947
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,464,990✔
948

949
void mndStop(SMnode *pMnode) {
513,979✔
950
  mndSetStop(pMnode);
513,979✔
951
  mndSyncStop(pMnode);
513,979✔
952
  mndCleanupTimer(pMnode);
513,979✔
953
}
513,979✔
954

955
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
65,847,102✔
956
  SMnode    *pMnode = pMsg->info.node;
65,847,102✔
957
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
65,847,102✔
958

959
  const STraceId *trace = &pMsg->info.traceId;
65,847,102✔
960
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
65,847,102✔
961

962
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
65,847,102✔
963
  if (code != 0) {
65,847,102✔
964
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,871✔
965
            tstrerror(code), code);
966
  }
967

968
  return code;
65,847,102✔
969
}
970

971
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
238,705,419✔
972
  int32_t code = 0;
238,705,419✔
973
  if (!IsReq(pMsg)) TAOS_RETURN(code);
238,705,419✔
974
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
207,640,730✔
975
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
202,357,733✔
976
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
197,417,972✔
977
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
186,817,797✔
978
    TAOS_RETURN(code);
20,825,051✔
979
  }
980

981
  SMnode *pMnode = pMsg->info.node;
186,814,578✔
982
  (void)taosThreadRwlockRdlock(&pMnode->lock);
186,816,403✔
983
  if (pMnode->stopped) {
186,826,598✔
984
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,360✔
985
    code = TSDB_CODE_APP_IS_STOPPING;
5,360✔
986
    TAOS_RETURN(code);
5,360✔
987
  }
988

989
  terrno = 0;
186,813,697✔
990
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
186,814,777✔
991
  if (terrno != 0) {
186,823,048✔
992
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
993
    code = terrno;
×
994
    TAOS_RETURN(code);
×
995
  }
996

997
  if (state.state != TAOS_SYNC_STATE_LEADER) {
186,821,103✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,374,489✔
999
    code = TSDB_CODE_SYN_NOT_LEADER;
4,374,489✔
1000
    goto _OVER;
4,374,489✔
1001
  }
1002

1003
  if (!state.restored || !pMnode->restored) {
182,446,614✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
658,372✔
1005
    code = TSDB_CODE_SYN_RESTORING;
658,114✔
1006
    goto _OVER;
658,114✔
1007
  }
1008

1009
#if 1
1010
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
181,788,969✔
1011
#else
1012
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1013
  mTrace("mnode rpc is acquired, ref:%d", ref);
1014
#endif
1015

1016
  (void)taosThreadRwlockUnlock(&pMnode->lock);
181,790,373✔
1017
  TAOS_RETURN(code);
181,790,102✔
1018

1019
_OVER:
5,032,603✔
1020
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,032,603✔
1021
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,031,419✔
1022
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,029,719✔
1023
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,030,531✔
1024
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,029,719✔
1025
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,030,531✔
1026
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,029,719✔
1027
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,029,719✔
1028
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,028,095✔
1029
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
1,179✔
1030
           pMnode->stopped, state.restored, syncStr(state.state));
1031
    TAOS_RETURN(code);
1,179✔
1032
  }
1033

1034
  const STraceId *trace = &pMsg->info.traceId;
5,028,058✔
1035
  SEpSet          epSet = {0};
5,028,281✔
1036
  mndGetMnodeEpSet(pMnode, &epSet);
5,028,907✔
1037

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

1044
  if (epSet.numOfEps <= 0) return -1;
5,030,531✔
1045

1046
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
18,505,661✔
1047
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
13,475,130✔
1048
  }
1049

1050
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,030,531✔
1051
  pMsg->info.rsp = rpcMallocCont(contLen);
5,029,265✔
1052
  if (pMsg->info.rsp != NULL) {
5,029,719✔
1053
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,029,719✔
1054
      mError("failed to serialize ep set");
×
1055
    }
1056
    pMsg->info.hasEpSet = 1;
5,028,603✔
1057
    pMsg->info.rspLen = contLen;
5,027,512✔
1058
  }
1059

1060
  TAOS_RETURN(code);
5,029,719✔
1061
}
1062

1063
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
238,713,999✔
1064
  SMnode         *pMnode = pMsg->info.node;
238,713,999✔
1065
  const STraceId *trace = &pMsg->info.traceId;
238,716,210✔
1066
  int32_t         code = TSDB_CODE_SUCCESS;
238,711,530✔
1067

1068
#ifdef TD_ENTERPRISE
1069
  if (pMsg->info.conn.isToken) {
238,711,530✔
1070
    SCachedTokenInfo ti = {0};
×
1071
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
×
1072
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1073
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
×
1074
      TAOS_RETURN(code);
×
1075
    }
1076
    if (ti.enabled == 0) {
×
1077
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1078
      code = TSDB_CODE_MND_TOKEN_DISABLED;
×
1079
      TAOS_RETURN(code);
×
1080
    }
1081
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
×
1082
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1083
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1084
      TAOS_RETURN(code);
×
1085
    }
1086
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
×
1087
  }
1088
#endif
1089

1090
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
238,699,672✔
1091
  MndMsgFpExt fpExt = NULL;
238,713,744✔
1092
  if (fp == NULL) {
238,713,744✔
1093
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,824,362✔
1094
    if (fpExt == NULL) {
20,824,362✔
1095
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1096
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1097
      TAOS_RETURN(code);
×
1098
    }
1099
  }
1100

1101
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
238,713,744✔
1102

1103
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
233,674,411✔
1104
  if (fp)
233,672,958✔
1105
    code = (*fp)(pMsg);
212,848,117✔
1106
  else
1107
    code = (*fpExt)(pMsg, pQueueInfo);
20,824,841✔
1108
  mndReleaseRpc(pMnode);
233,679,697✔
1109

1110
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
233,680,421✔
1111
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
31,997,835✔
1112
  } else if (code == 0) {
201,682,586✔
1113
    mGTrace("msg:%p, successfully processed", pMsg);
198,910,170✔
1114
  } else {
1115
    // TODO removve this wrong set code
1116
    if (code == -1) {
2,772,416✔
1117
      code = terrno;
3,198✔
1118
    }
1119
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,772,416✔
1120
            TMSG_INFO(pMsg->msgType));
1121
  }
1122

1123
  TAOS_RETURN(code);
233,680,421✔
1124
}
1125

1126
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
106,932,800✔
1127
  tmsg_t type = TMSG_INDEX(msgType);
106,932,800✔
1128
  if (type < TDMT_MAX) {
106,932,800✔
1129
    pMnode->msgFp[type] = fp;
106,932,800✔
1130
  }
1131
}
106,932,800✔
1132

1133
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,112,800✔
1134
  tmsg_t type = TMSG_INDEX(msgType);
4,112,800✔
1135
  if (type < TDMT_MAX) {
4,112,800✔
1136
    pMnode->msgFpExt[type] = fp;
4,112,800✔
1137
  }
1138
}
4,112,800✔
1139

1140
// Note: uid 0 is reserved
1141
int64_t mndGenerateUid(const char *name, int32_t len) {
8,896,692✔
1142
  int32_t hashval = MurmurHash3_32(name, len);
8,896,692✔
1143
  do {
×
1144
    int64_t us = taosGetTimestampUs();
8,897,315✔
1145
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
8,897,315✔
1146
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
8,897,315✔
1147
    if (uuid) {
8,897,315✔
1148
      return llabs(uuid);
8,897,315✔
1149
    }
1150
  } while (true);
1151
}
1152

1153
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
133✔
1154
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1155
  int32_t code = mndAcquireRpc(pMnode);
133✔
1156
  if (code < 0) {
133✔
1157
    TAOS_RETURN(code);
×
1158
  } else if (code == 1) {
133✔
1159
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1160
  }
1161

1162
  SSdb   *pSdb = pMnode->pSdb;
133✔
1163
  int64_t ms = taosGetTimestampMs();
133✔
1164

1165
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
133✔
1166
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
133✔
1167
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
133✔
1168
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
133✔
1169
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
133✔
1170
      pStbInfo->stbs == NULL) {
133✔
1171
    mndReleaseRpc(pMnode);
×
1172
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1173
    if (terrno != 0) code = terrno;
×
1174
    TAOS_RETURN(code);
×
1175
  }
1176

1177
  // cluster info
1178
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
133✔
1179
  pClusterInfo->monitor_interval = tsMonitorInterval;
133✔
1180
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
133✔
1181
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
133✔
1182
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
133✔
1183
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
133✔
1184
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
133✔
1185

1186
  void *pIter = NULL;
133✔
1187
  while (1) {
133✔
1188
    SDnodeObj *pObj = NULL;
266✔
1189
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
266✔
1190
    if (pIter == NULL) break;
266✔
1191

1192
    SMonDnodeDesc desc = {0};
133✔
1193
    desc.dnode_id = pObj->id;
133✔
1194
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
133✔
1195
    if (mndIsDnodeOnline(pObj, ms)) {
133✔
1196
      tstrncpy(desc.status, "ready", sizeof(desc.status));
133✔
1197
    } else {
1198
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1199
    }
1200
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
266✔
1201
      mError("failed put dnode into array, but continue at this monitor report")
×
1202
    }
1203
    sdbRelease(pSdb, pObj);
133✔
1204
  }
1205

1206
  pIter = NULL;
133✔
1207
  while (1) {
133✔
1208
    SMnodeObj *pObj = NULL;
266✔
1209
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
266✔
1210
    if (pIter == NULL) break;
266✔
1211

1212
    SMonMnodeDesc desc = {0};
133✔
1213
    desc.mnode_id = pObj->id;
133✔
1214
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
133✔
1215

1216
    if (pObj->id == pMnode->selfDnodeId) {
133✔
1217
      pClusterInfo->first_ep_dnode_id = pObj->id;
133✔
1218
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
133✔
1219
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1220
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
133✔
1221
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1222
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
133✔
1223
      desc.syncState = TAOS_SYNC_STATE_LEADER;
133✔
1224
    } else {
1225
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1226
      desc.syncState = pObj->syncState;
×
1227
    }
1228
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
266✔
1229
      mError("failed to put mnode into array, but continue at this monitor report");
×
1230
    }
1231
    sdbRelease(pSdb, pObj);
133✔
1232
  }
1233

1234
  // vgroup info
1235
  pIter = NULL;
133✔
1236
  while (1) {
266✔
1237
    SVgObj *pVgroup = NULL;
399✔
1238
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
399✔
1239
    if (pIter == NULL) break;
399✔
1240

1241
    if (pVgroup->mountVgId) {
266✔
1242
      sdbRelease(pSdb, pVgroup);
×
1243
      continue;
×
1244
    }
1245

1246
    pClusterInfo->vgroups_total++;
266✔
1247
    pClusterInfo->tbs_total += pVgroup->numOfTables;
266✔
1248

1249
    SMonVgroupDesc desc = {0};
266✔
1250
    desc.vgroup_id = pVgroup->vgId;
266✔
1251

1252
    SName name = {0};
266✔
1253
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
266✔
1254
    if (code < 0) {
266✔
1255
      mError("failed to get db name since %s", tstrerror(code));
×
1256
      sdbRelease(pSdb, pVgroup);
×
1257
      TAOS_RETURN(code);
×
1258
    }
1259
    (void)tNameGetDbName(&name, desc.database_name);
266✔
1260

1261
    desc.tables_num = pVgroup->numOfTables;
266✔
1262
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
266✔
1263
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
266✔
1264
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
532✔
1265
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
266✔
1266
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
266✔
1267
      pVnDesc->dnode_id = pVgid->dnodeId;
266✔
1268
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
266✔
1269
      pVnDesc->syncState = pVgid->syncState;
266✔
1270
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
266✔
1271
        tstrncpy(desc.status, "ready", sizeof(desc.status));
266✔
1272
        pClusterInfo->vgroups_alive++;
266✔
1273
      }
1274
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
266✔
1275
        pClusterInfo->vnodes_alive++;
266✔
1276
      }
1277
      pClusterInfo->vnodes_total++;
266✔
1278
    }
1279

1280
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
532✔
1281
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1282
    }
1283
    sdbRelease(pSdb, pVgroup);
266✔
1284
  }
1285

1286
  // stb info
1287
  pIter = NULL;
133✔
1288
  while (1) {
×
1289
    SStbObj *pStb = NULL;
133✔
1290
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
133✔
1291
    if (pIter == NULL) break;
133✔
1292

1293
    SMonStbDesc desc = {0};
×
1294

1295
    SName name1 = {0};
×
1296
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1297
    if (code < 0) {
×
1298
      mError("failed to get db name since %s", tstrerror(code));
×
1299
      sdbRelease(pSdb, pStb);
×
1300
      TAOS_RETURN(code);
×
1301
    }
1302
    (void)tNameGetDbName(&name1, desc.database_name);
×
1303

1304
    SName name2 = {0};
×
1305
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1306
    if (code < 0) {
×
1307
      mError("failed to get table name since %s", tstrerror(code));
×
1308
      sdbRelease(pSdb, pStb);
×
1309
      TAOS_RETURN(code);
×
1310
    }
1311
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1312

1313
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1314
      mError("failed to put stb into array, but continue at this monitor report");
×
1315
    }
1316
    sdbRelease(pSdb, pStb);
×
1317
  }
1318

1319
  // grant info
1320
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
133✔
1321
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
133✔
1322
  if (pMnode->grant.expireTimeMS == 0) {
133✔
1323
    pGrantInfo->expire_time = 0;
×
1324
    pGrantInfo->timeseries_total = 0;
×
1325
  }
1326

1327
  mndReleaseRpc(pMnode);
133✔
1328
  TAOS_RETURN(code);
133✔
1329
}
1330

1331
int32_t mndResetTimer(SMnode *pMnode){
×
1332
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1333
}
1334

1335
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
24,661,549✔
1336
  mTrace("mnode get load");
24,661,549✔
1337
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
24,661,549✔
1338
  pLoad->syncState = state.state;
24,661,549✔
1339
  pLoad->syncRestore = state.restored;
24,661,549✔
1340
  pLoad->syncTerm = state.term;
24,661,549✔
1341
  pLoad->roleTimeMs = state.roleTimeMs;
24,661,549✔
1342
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
24,661,549✔
1343
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1344
  return 0;
24,661,549✔
1345
}
1346

1347
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,299,969✔
1348
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,299,969✔
1349
  return state.roleTimeMs;
7,299,969✔
1350
}
1351

1352
void mndSetRestored(SMnode *pMnode, bool restored) {
513,979✔
1353
  if (restored) {
513,979✔
1354
    (void)taosThreadRwlockWrlock(&pMnode->lock);
513,979✔
1355
    pMnode->restored = true;
513,979✔
1356
    (void)taosThreadRwlockUnlock(&pMnode->lock);
513,979✔
1357
    mInfo("mnode set restored:%d", restored);
513,979✔
1358
  } else {
1359
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1360
    pMnode->restored = false;
×
1361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1362
    mInfo("mnode set restored:%d", restored);
×
1363
    while (1) {
1364
      if (pMnode->rpcRef <= 0) break;
×
1365
      taosMsleep(3);
×
1366
    }
1367
  }
1368
}
513,979✔
1369

1370
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1371

1372
void mndSetStop(SMnode *pMnode) {
513,979✔
1373
  (void)taosThreadRwlockWrlock(&pMnode->lock);
513,979✔
1374
  pMnode->stopped = true;
513,979✔
1375
  (void)taosThreadRwlockUnlock(&pMnode->lock);
513,979✔
1376
  mInfo("mnode set stopped");
513,979✔
1377
}
513,979✔
1378

1379
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
498,578,874✔
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