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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 hits per line

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

78.83
/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 "mndFunc.h"
29
#include "mndGrant.h"
30
#include "mndIndex.h"
31
#include "mndInfoSchema.h"
32
#include "mndInstance.h"
33
#include "mndMnode.h"
34
#include "mndMount.h"
35
#include "mndPerfSchema.h"
36
#include "mndPrivilege.h"
37
#include "mndProfile.h"
38
#include "mndQnode.h"
39
#include "mndQuery.h"
40
#include "mndRetention.h"
41
#include "mndRetentionDetail.h"
42
#include "mndRsma.h"
43
#include "mndScan.h"
44
#include "mndScanDetail.h"
45
#include "mndShow.h"
46
#include "mndSma.h"
47
#include "mndSnode.h"
48
#include "mndSsMigrate.h"
49
#include "mndStb.h"
50
#include "mndStream.h"
51
#include "mndSubscribe.h"
52
#include "mndSync.h"
53
#include "mndTelem.h"
54
#include "mndTopic.h"
55
#include "mndTrans.h"
56
#include "mndUser.h"
57
#include "mndVgroup.h"
58
#include "mndView.h"
59

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

79
static inline void mndReleaseRpc(SMnode *pMnode) {
234,415,239✔
80
  (void)taosThreadRwlockRdlock(&pMnode->lock);
234,415,239✔
81
#if 1
82
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
234,418,174✔
83
#else
84
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
85
  mTrace("mnode rpc is released, ref:%d", ref);
86
#endif
87
  (void)taosThreadRwlockUnlock(&pMnode->lock);
234,416,606✔
88
}
234,418,134✔
89

90
static void *mndBuildTimerMsg(int32_t *pContLen) {
51,501,516✔
91
  terrno = 0;
51,501,516✔
92
  SMTimerReq timerReq = {0};
51,501,516✔
93

94
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
51,501,225✔
95
  if (contLen <= 0) return NULL;
51,494,145✔
96
  void *pReq = rpcMallocCont(contLen);
51,494,145✔
97
  if (pReq == NULL) return NULL;
51,498,654✔
98

99
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
51,498,654✔
UNCOV
100
    mError("failed to serialize timer msg since %s", terrstr());
×
101
  }
102
  *pContLen = contLen;
51,500,158✔
103
  return pReq;
51,500,694✔
104
}
105

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

300
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
158,867✔
301
  SSdb *pSdb = pMnode->pSdb;
158,867✔
302

303
  void *pIter = NULL;
158,867✔
304
  while (1) {
807,311✔
305
    SVgObj *pVgroup = NULL;
966,178✔
306
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
966,178✔
307
    if (pIter == NULL) break;
966,178✔
308

309
    bool stateChanged = false;
807,311✔
310
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
2,031,919✔
311
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,561,636✔
312
      if (pGid->dnodeId == dnodeId) {
1,561,636✔
313
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
337,028✔
314
          mInfo(
148,091✔
315
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
316
              "restored:0 "
317
              "canRead:0",
318
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
319
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
148,091✔
320
          pGid->syncRestore = 0;
148,091✔
321
          pGid->syncCanRead = 0;
148,091✔
322
          pGid->startTimeMs = 0;
148,091✔
323
          stateChanged = true;
148,091✔
324
        }
325
        break;
337,028✔
326
      }
327
    }
328

329
    if (stateChanged) {
807,311✔
330
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
148,091✔
331
      if (pDb != NULL && pDb->stateTs != curMs) {
148,091✔
332
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
83,891✔
333
              curMs);
334
        pDb->stateTs = curMs;
83,891✔
335
      }
336
      mndReleaseDb(pMnode, pDb);
148,091✔
337
    }
338

339
    sdbRelease(pSdb, pVgroup);
807,311✔
340
  }
341
}
158,867✔
342

343
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,063,132✔
344
  mTrace("check dnode offline");
4,063,132✔
345
  if (mndAcquireRpc(pMnode) != 0) return;
4,063,132✔
346

347
  SSdb   *pSdb = pMnode->pSdb;
4,063,132✔
348
  int64_t curMs = taosGetTimestampMs();
4,063,132✔
349

350
  void *pIter = NULL;
4,063,132✔
351
  while (1) {
8,362,180✔
352
    SDnodeObj *pDnode = NULL;
12,425,312✔
353
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,425,312✔
354
    if (pIter == NULL) break;
12,425,312✔
355

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

362
    sdbRelease(pSdb, pDnode);
8,362,180✔
363
  }
364

365
  mndReleaseRpc(pMnode);
4,063,132✔
366
}
367

368
static bool mnodeIsNotLeader(SMnode *pMnode) {
258,504,829✔
369
  terrno = 0;
258,504,829✔
370
  (void)taosThreadRwlockRdlock(&pMnode->lock);
258,503,723✔
371
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
258,512,462✔
372
  if (terrno != 0) {
258,514,676✔
UNCOV
373
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
374
    return true;
×
375
  }
376

377
  if (state.state != TAOS_SYNC_STATE_LEADER) {
258,514,241✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
24,152,434✔
379
    terrno = TSDB_CODE_SYN_NOT_LEADER;
24,152,144✔
380
    return true;
24,152,434✔
381
  }
382
  if (!state.restored || !pMnode->restored) {
234,361,807✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
22,578✔
384
    terrno = TSDB_CODE_SYN_RESTORING;
20,421✔
385
    return true;
20,421✔
386
  }
387
  (void)taosThreadRwlockUnlock(&pMnode->lock);
234,339,229✔
388
  return false;
234,341,401✔
389
}
390

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

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

405
  return min <= 1 ? 2 : min;
×
406
}
407
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
21,140,174✔
408
  int32_t code = 0;
21,140,174✔
409
#ifndef TD_ASTRA
410
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
21,140,174✔
411
    mndPullupGrant(pMnode);
1,210,304✔
412
  }
413
  if (sec % tsTtlPushIntervalSec == 0) {
21,140,174✔
414
    mndPullupTtl(pMnode);
2,004,384✔
415
  }
416

417
  if (sec % tsTrimVDbIntervalSec == 0) {
21,140,174✔
418
    mndPullupTrimDb(pMnode);
×
419
  }
420

421
  if (sec % tsQueryTrimIntervalSec == 0) {
21,140,174✔
422
    mndPullupQueryTrimDb(pMnode);
2,041,438✔
423
  }
424
#endif
425
#ifdef USE_SHARED_STORAGE
426
  if (tsSsEnabled) {
21,140,174✔
UNCOV
427
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
UNCOV
428
      mndPullupUpdateSsMigrateProgress(pMnode);
×
429
    }
UNCOV
430
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
UNCOV
431
      mndPullupSsMigrateDb(pMnode);
×
432
    }
433
  }
434
#endif
435
#ifdef TD_ENTERPRISE
436
  if (tsAuthReq) {
21,140,174✔
UNCOV
437
    if (sec % tsAuthReqHBInterval == 0) {
×
UNCOV
438
      mndPullupAuth(pMnode);
×
439
    }
440
  }
441
#endif
442
  if (sec % tsTransPullupInterval == 0) {
21,140,174✔
443
    mndPullupTrans(pMnode);
10,468,552✔
444
  }
445

446
  if (sec % tsCompactPullupInterval == 0) {
21,140,174✔
447
    mndPullupCompacts(pMnode);
1,928,110✔
448
  }
449

450
  if (sec % tsScanPullupInterval == 0) {
21,140,174✔
451
    mndPullupScans(pMnode);
1,928,110✔
452
  }
453
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
21,140,174✔
454
    mndPullupInstances(pMnode);
4,064,455✔
455
  }
456
#ifdef USE_TOPIC
457
  if (sec % tsMqRebalanceInterval == 0) {
21,140,174✔
458
    mndCalMqRebalance(pMnode);
10,467,660✔
459
  }
460
#endif
461
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
21,140,174✔
462
    mndPullupTelem(pMnode);
292✔
463
  }
464
  if (sec % tsUptimeInterval == 0) {
21,140,174✔
465
    mndIncreaseUpTime(pMnode);
13,726✔
466
  }
467
}
21,140,174✔
468

469
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
213,201,647✔
470
  int32_t code = 0;
213,201,647✔
471
#ifndef TD_ASTRA
472
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
213,201,647✔
473
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
10,469,165✔
UNCOV
474
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
475
    }
476
  }
477

478
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
213,201,647✔
479
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
6,905,320✔
UNCOV
480
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
481
    }
482
  }
483
#endif
484
}
213,201,647✔
485

486
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
213,201,647✔
487
  if (ms % (tsStatusTimeoutMs) == 0) {
213,201,647✔
488
    mndCheckDnodeOffline(pMnode);
4,063,132✔
489
  }
490
}
213,201,647✔
491

492
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
21,140,174✔
493
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
21,140,174✔
494
    mndSyncCheckTimeout(pMnode);
548,392✔
495
  }
496
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
21,140,174✔
497
    msmHealthCheck(pMnode);
6,905,035✔
498
  }
499
}
21,140,174✔
500

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

506
  while (1) {
235,261,102✔
507
    lastTime++;
235,759,709✔
508
    taosMsleep(100);
235,759,709✔
509

510
    if (mndGetStop(pMnode)) break;
235,759,709✔
511
    if (lastTime % 10 != 0) continue;
235,261,102✔
512

513
    if (mnodeIsNotLeader(pMnode)) {
23,300,801✔
514
      mTrace("timer not process since mnode is not leader");
2,160,627✔
515
      continue;
2,160,627✔
516
    }
517

518
    int64_t sec = lastTime / 10;
21,140,174✔
519
    mndDoTimerCheckSync(pMnode, sec);
21,140,174✔
520

521
    mndDoTimerPullupTask(pMnode, sec);
21,140,174✔
522
  }
523

524
  return NULL;
498,607✔
525
}
526

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

532
  while (1) {
533
    lastTime += 100;
235,712,482✔
534
    taosMsleep(100);
235,712,482✔
535

536
    if (mndGetStop(pMnode)) break;
235,712,482✔
537
    if (lastTime % 10 != 0) continue;
235,213,875✔
538

539
    if (mnodeIsNotLeader(pMnode)) {
235,213,875✔
540
      mTrace("timer not process since mnode is not leader");
22,012,228✔
541
      continue;
22,012,228✔
542
    }
543

544
    mndDoTimerCheckStatus(pMnode, lastTime);
213,201,647✔
545

546
    mndDoArbTimerPullupTask(pMnode, lastTime);
213,201,647✔
547
  }
548

549
  return NULL;
498,607✔
550
}
551

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

565
  (void)taosThreadAttrDestroy(&thAttr);
498,607✔
566
  tmsgReportStartup("mnode-timer", "initialized");
498,607✔
567

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

579
  (void)taosThreadAttrDestroy(&arbAttr);
498,607✔
580
  tmsgReportStartup("mnode-timer", "initialized");
498,607✔
581
  TAOS_RETURN(code);
498,607✔
582
}
583

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

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

603
  if (taosMkDir(pMnode->path) != 0) {
499,407✔
UNCOV
604
    code = terrno;
×
UNCOV
605
    TAOS_RETURN(code);
×
606
  }
607

608
  TAOS_RETURN(code);
499,407✔
609
}
610

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

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

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

646
  TAOS_RETURN(code);
499,407✔
647
}
648

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

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

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

670
  TAOS_RETURN(code);
499,407✔
671
}
672

673
static int32_t mndOpenSdb(SMnode *pMnode) {
499,407✔
674
  int32_t code = 0;
499,407✔
675
  if (!pMnode->deploy) {
499,407✔
676
    code = sdbReadFile(pMnode->pSdb);
174,355✔
677
  }
678

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

681
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
499,407✔
682
  return code;
499,407✔
683
}
684

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

692
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
22,972,722✔
693
  SMnodeStep step = {0};
22,972,722✔
694
  step.name = name;
22,972,722✔
695
  step.initFp = initFp;
22,972,722✔
696
  step.cleanupFp = cleanupFp;
22,972,722✔
697
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
45,945,444✔
UNCOV
698
    TAOS_RETURN(terrno);
×
699
  }
700

701
  TAOS_RETURN(0);
22,972,722✔
702
}
703

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

756
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
498,607✔
757
  if (pMnode->pSteps == NULL) return;
498,607✔
758

759
  if (pos == -1) {
498,607✔
760
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
498,607✔
761
  }
762

763
  for (int32_t s = pos; s >= 0; s--) {
23,434,529✔
764
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,935,922✔
765
    mInfo("%s will cleanup", pStep->name);
22,935,922✔
766
    if (pStep->cleanupFp != NULL) {
22,935,922✔
767
      (*pStep->cleanupFp)(pMnode);
21,938,708✔
768
    }
769
  }
770

771
  taosArrayClear(pMnode->pSteps);
498,607✔
772
  taosArrayDestroy(pMnode->pSteps);
498,607✔
773
  pMnode->pSteps = NULL;
498,607✔
774
}
775

776
static int32_t mndExecSteps(SMnode *pMnode) {
499,407✔
777
  int32_t code = 0;
499,407✔
778
  int32_t size = taosArrayGetSize(pMnode->pSteps);
499,407✔
779
  for (int32_t pos = 0; pos < size; pos++) {
23,472,129✔
780
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,972,722✔
781
    if (pStep->initFp == NULL) continue;
22,972,722✔
782

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

793
  pMnode->clusterId = mndGetClusterId(pMnode);
499,407✔
794
  TAOS_RETURN(0);
499,407✔
795
}
796

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

808
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
499,407✔
809
  terrno = 0;
499,407✔
810
  mInfo("start to open mnode in %s", path);
499,407✔
811

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

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

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

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

848
  code = mndCreateDir(pMnode, path);
499,407✔
849
  if (code != 0) {
499,407✔
850
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
851
    mndClose(pMnode);
×
852
    terrno = code;
×
UNCOV
853
    return NULL;
×
854
  }
855

856
  code = mndInitSteps(pMnode);
499,407✔
857
  if (code != 0) {
499,407✔
UNCOV
858
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
859
    mndClose(pMnode);
×
860
    terrno = code;
×
861
    return NULL;
×
862
  }
863

864
  code = mndExecSteps(pMnode);
499,407✔
865
  if (code != 0) {
499,407✔
UNCOV
866
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
867
    mndClose(pMnode);
×
868
    terrno = code;
×
869
    return NULL;
×
870
  }
871

872
  mInfo("mnode open successfully");
499,407✔
873
  return pMnode;
499,407✔
874
}
875

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

892
void mndClose(SMnode *pMnode) {
498,607✔
893
  if (pMnode != NULL) {
498,607✔
894
    mInfo("start to close mnode");
498,607✔
895
    mndCleanupSteps(pMnode, -1);
498,607✔
896
    taosMemoryFreeClear(pMnode->path);
498,607✔
897
    taosMemoryFreeClear(pMnode);
498,607✔
898
    mInfo("mnode is closed");
498,607✔
899
  }
900
}
498,607✔
901

902
int32_t mndStart(SMnode *pMnode) {
498,607✔
903
  mndSyncStart(pMnode);
498,607✔
904
  if (pMnode->deploy) {
498,607✔
905
    if (sdbDeploy(pMnode->pSdb) != 0) {
325,052✔
UNCOV
906
      mError("failed to deploy sdb while start mnode");
×
UNCOV
907
      return -1;
×
908
    }
909
    mndSetRestored(pMnode, true);
325,052✔
910
  }
911
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
498,607✔
912

913
  return mndInitTimer(pMnode);
498,607✔
914
}
915

916
int32_t mndIsCatchUp(SMnode *pMnode) {
344,526✔
917
  int64_t rid = pMnode->syncMgmt.sync;
344,526✔
918
  return syncIsCatchUp(rid);
344,526✔
919
}
920

921
ESyncRole mndGetRole(SMnode *pMnode) {
344,526✔
922
  int64_t rid = pMnode->syncMgmt.sync;
344,526✔
923
  return syncGetRole(rid);
344,526✔
924
}
925

926
int64_t mndGetTerm(SMnode *pMnode) {
7,034,606✔
927
  int64_t rid = pMnode->syncMgmt.sync;
7,034,606✔
928
  return syncGetTerm(rid);
7,034,606✔
929
}
930

931
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
17,504,582✔
932

933
void mndStop(SMnode *pMnode) {
498,607✔
934
  mndSetStop(pMnode);
498,607✔
935
  mndSyncStop(pMnode);
498,607✔
936
  mndCleanupTimer(pMnode);
498,607✔
937
}
498,607✔
938

939
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
63,721,436✔
940
  SMnode    *pMnode = pMsg->info.node;
63,721,436✔
941
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
63,721,436✔
942

943
  const STraceId *trace = &pMsg->info.traceId;
63,721,436✔
944
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
63,721,436✔
945

946
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
63,721,436✔
947
  if (code != 0) {
63,721,121✔
948
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,673✔
949
            tstrerror(code), code);
950
  }
951

952
  return code;
63,721,436✔
953
}
954

955
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
235,844,879✔
956
  int32_t code = 0;
235,844,879✔
957
  if (!IsReq(pMsg)) TAOS_RETURN(code);
235,844,879✔
958
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
204,928,997✔
959
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
199,496,477✔
960
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
194,417,779✔
961
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
183,541,772✔
962
    TAOS_RETURN(code);
21,376,303✔
963
  }
964

965
  SMnode *pMnode = pMsg->info.node;
183,541,319✔
966
  (void)taosThreadRwlockRdlock(&pMnode->lock);
183,540,697✔
967
  if (pMnode->stopped) {
183,545,550✔
968
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,634✔
969
    code = TSDB_CODE_APP_IS_STOPPING;
5,634✔
970
    TAOS_RETURN(code);
5,634✔
971
  }
972

973
  terrno = 0;
183,539,048✔
974
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
183,537,650✔
975
  if (terrno != 0) {
183,543,373✔
UNCOV
976
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
977
    code = terrno;
×
UNCOV
978
    TAOS_RETURN(code);
×
979
  }
980

981
  if (state.state != TAOS_SYNC_STATE_LEADER) {
183,542,674✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,346,649✔
983
    code = TSDB_CODE_SYN_NOT_LEADER;
4,346,649✔
984
    goto _OVER;
4,346,649✔
985
  }
986

987
  if (!state.restored || !pMnode->restored) {
179,196,025✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,155,321✔
989
    code = TSDB_CODE_SYN_RESTORING;
1,150,311✔
990
    goto _OVER;
1,150,311✔
991
  }
992

993
#if 1
994
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
178,040,909✔
995
#else
996
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
997
  mTrace("mnode rpc is acquired, ref:%d", ref);
998
#endif
999

1000
  (void)taosThreadRwlockUnlock(&pMnode->lock);
178,045,724✔
1001
  TAOS_RETURN(code);
178,043,508✔
1002

1003
_OVER:
5,496,960✔
1004
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,496,960✔
1005
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,496,024✔
1006
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,494,776✔
1007
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,494,776✔
1008
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,494,464✔
1009
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,493,195✔
1010
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,493,195✔
1011
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,493,862✔
1012
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,493,550✔
1013
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
4,766✔
1014
           pMnode->stopped, state.restored, syncStr(state.state));
1015
    TAOS_RETURN(code);
4,766✔
1016
  }
1017

1018
  const STraceId *trace = &pMsg->info.traceId;
5,492,281✔
1019
  SEpSet          epSet = {0};
5,492,571✔
1020
  mndGetMnodeEpSet(pMnode, &epSet);
5,492,571✔
1021

1022
  mGDebug(
5,493,528✔
1023
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1024
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1025
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1026
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1027

1028
  if (epSet.numOfEps <= 0) return -1;
5,493,528✔
1029

1030
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
19,380,104✔
1031
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
13,886,576✔
1032
  }
1033

1034
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,493,528✔
1035
  pMsg->info.rsp = rpcMallocCont(contLen);
5,491,427✔
1036
  if (pMsg->info.rsp != NULL) {
5,492,555✔
1037
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,492,760✔
UNCOV
1038
      mError("failed to serialize ep set");
×
1039
    }
1040
    pMsg->info.hasEpSet = 1;
5,490,419✔
1041
    pMsg->info.rspLen = contLen;
5,489,036✔
1042
  }
1043

1044
  TAOS_RETURN(code);
5,488,559✔
1045
}
1046

1047
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
235,856,050✔
1048
  SMnode         *pMnode = pMsg->info.node;
235,856,050✔
1049
  const STraceId *trace = &pMsg->info.traceId;
235,857,189✔
1050
  int32_t         code = TSDB_CODE_SUCCESS;
235,850,946✔
1051

1052
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
235,850,946✔
1053
  MndMsgFpExt fpExt = NULL;
235,854,190✔
1054
  if (fp == NULL) {
235,854,190✔
1055
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,392,922✔
1056
    if (fpExt == NULL) {
21,392,922✔
UNCOV
1057
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
1058
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
UNCOV
1059
      TAOS_RETURN(code);
×
1060
    }
1061
  }
1062

1063
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
235,854,190✔
1064

1065
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
230,353,138✔
1066
  if (fp)
230,351,813✔
1067
    code = (*fp)(pMsg);
208,959,594✔
1068
  else
1069
    code = (*fpExt)(pMsg, pQueueInfo);
21,392,219✔
1070
  mndReleaseRpc(pMnode);
230,354,309✔
1071

1072
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
230,353,255✔
1073
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
32,644,113✔
1074
  } else if (code == 0) {
197,709,142✔
1075
    mGTrace("msg:%p, successfully processed", pMsg);
195,103,523✔
1076
  } else {
1077
    // TODO removve this wrong set code
1078
    if (code == -1) {
2,605,619✔
1079
      code = terrno;
3,257✔
1080
    }
1081
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,605,619✔
1082
            TMSG_INFO(pMsg->msgType));
1083
  }
1084

1085
  TAOS_RETURN(code);
230,353,255✔
1086
}
1087

1088
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
97,883,772✔
1089
  tmsg_t type = TMSG_INDEX(msgType);
97,883,772✔
1090
  if (type < TDMT_MAX) {
97,883,772✔
1091
    pMnode->msgFp[type] = fp;
97,883,772✔
1092
  }
1093
}
97,883,772✔
1094

1095
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,995,256✔
1096
  tmsg_t type = TMSG_INDEX(msgType);
3,995,256✔
1097
  if (type < TDMT_MAX) {
3,995,256✔
1098
    pMnode->msgFpExt[type] = fp;
3,995,256✔
1099
  }
1100
}
3,995,256✔
1101

1102
// Note: uid 0 is reserved
1103
int64_t mndGenerateUid(const char *name, int32_t len) {
9,373,029✔
1104
  int32_t hashval = MurmurHash3_32(name, len);
9,373,029✔
UNCOV
1105
  do {
×
1106
    int64_t us = taosGetTimestampUs();
9,373,806✔
1107
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,373,806✔
1108
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,373,806✔
1109
    if (uuid) {
9,373,806✔
1110
      return llabs(uuid);
9,373,806✔
1111
    }
1112
  } while (true);
1113
}
1114

1115
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
145✔
1116
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1117
  int32_t code = mndAcquireRpc(pMnode);
145✔
1118
  if (code < 0) {
145✔
UNCOV
1119
    TAOS_RETURN(code);
×
1120
  } else if (code == 1) {
145✔
UNCOV
1121
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1122
  }
1123

1124
  SSdb   *pSdb = pMnode->pSdb;
145✔
1125
  int64_t ms = taosGetTimestampMs();
145✔
1126

1127
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
145✔
1128
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
145✔
1129
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
145✔
1130
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
145✔
1131
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
145✔
1132
      pStbInfo->stbs == NULL) {
145✔
UNCOV
1133
    mndReleaseRpc(pMnode);
×
UNCOV
1134
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1135
    if (terrno != 0) code = terrno;
×
1136
    TAOS_RETURN(code);
×
1137
  }
1138

1139
  // cluster info
1140
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
145✔
1141
  pClusterInfo->monitor_interval = tsMonitorInterval;
145✔
1142
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
145✔
1143
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
145✔
1144
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
145✔
1145
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
145✔
1146
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
145✔
1147

1148
  void *pIter = NULL;
145✔
1149
  while (1) {
145✔
1150
    SDnodeObj *pObj = NULL;
290✔
1151
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
290✔
1152
    if (pIter == NULL) break;
290✔
1153

1154
    SMonDnodeDesc desc = {0};
145✔
1155
    desc.dnode_id = pObj->id;
145✔
1156
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
145✔
1157
    if (mndIsDnodeOnline(pObj, ms)) {
145✔
1158
      tstrncpy(desc.status, "ready", sizeof(desc.status));
145✔
1159
    } else {
UNCOV
1160
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1161
    }
1162
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
290✔
UNCOV
1163
      mError("failed put dnode into array, but continue at this monitor report")
×
1164
    }
1165
    sdbRelease(pSdb, pObj);
145✔
1166
  }
1167

1168
  pIter = NULL;
145✔
1169
  while (1) {
145✔
1170
    SMnodeObj *pObj = NULL;
290✔
1171
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
290✔
1172
    if (pIter == NULL) break;
290✔
1173

1174
    SMonMnodeDesc desc = {0};
145✔
1175
    desc.mnode_id = pObj->id;
145✔
1176
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
145✔
1177

1178
    if (pObj->id == pMnode->selfDnodeId) {
145✔
1179
      pClusterInfo->first_ep_dnode_id = pObj->id;
145✔
1180
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
145✔
1181
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1182
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
145✔
1183
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1184
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
145✔
1185
      desc.syncState = TAOS_SYNC_STATE_LEADER;
145✔
1186
    } else {
UNCOV
1187
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
UNCOV
1188
      desc.syncState = pObj->syncState;
×
1189
    }
1190
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
290✔
UNCOV
1191
      mError("failed to put mnode into array, but continue at this monitor report");
×
1192
    }
1193
    sdbRelease(pSdb, pObj);
145✔
1194
  }
1195

1196
  // vgroup info
1197
  pIter = NULL;
145✔
1198
  while (1) {
290✔
1199
    SVgObj *pVgroup = NULL;
435✔
1200
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
435✔
1201
    if (pIter == NULL) break;
435✔
1202

1203
    if (pVgroup->mountVgId) {
290✔
1204
      sdbRelease(pSdb, pVgroup);
×
1205
      continue;
×
1206
    }
1207

1208
    pClusterInfo->vgroups_total++;
290✔
1209
    pClusterInfo->tbs_total += pVgroup->numOfTables;
290✔
1210

1211
    SMonVgroupDesc desc = {0};
290✔
1212
    desc.vgroup_id = pVgroup->vgId;
290✔
1213

1214
    SName name = {0};
290✔
1215
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
290✔
1216
    if (code < 0) {
290✔
UNCOV
1217
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
1218
      sdbRelease(pSdb, pVgroup);
×
UNCOV
1219
      TAOS_RETURN(code);
×
1220
    }
1221
    (void)tNameGetDbName(&name, desc.database_name);
290✔
1222

1223
    desc.tables_num = pVgroup->numOfTables;
290✔
1224
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
290✔
1225
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
290✔
1226
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
580✔
1227
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
290✔
1228
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
290✔
1229
      pVnDesc->dnode_id = pVgid->dnodeId;
290✔
1230
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
290✔
1231
      pVnDesc->syncState = pVgid->syncState;
290✔
1232
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
290✔
1233
        tstrncpy(desc.status, "ready", sizeof(desc.status));
290✔
1234
        pClusterInfo->vgroups_alive++;
290✔
1235
      }
1236
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
290✔
1237
        pClusterInfo->vnodes_alive++;
290✔
1238
      }
1239
      pClusterInfo->vnodes_total++;
290✔
1240
    }
1241

1242
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
580✔
UNCOV
1243
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1244
    }
1245
    sdbRelease(pSdb, pVgroup);
290✔
1246
  }
1247

1248
  // stb info
1249
  pIter = NULL;
145✔
UNCOV
1250
  while (1) {
×
1251
    SStbObj *pStb = NULL;
145✔
1252
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
145✔
1253
    if (pIter == NULL) break;
145✔
1254

UNCOV
1255
    SMonStbDesc desc = {0};
×
1256

UNCOV
1257
    SName name1 = {0};
×
UNCOV
1258
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1259
    if (code < 0) {
×
1260
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
1261
      sdbRelease(pSdb, pStb);
×
UNCOV
1262
      TAOS_RETURN(code);
×
1263
    }
UNCOV
1264
    (void)tNameGetDbName(&name1, desc.database_name);
×
1265

UNCOV
1266
    SName name2 = {0};
×
1267
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1268
    if (code < 0) {
×
UNCOV
1269
      mError("failed to get table name since %s", tstrerror(code));
×
UNCOV
1270
      sdbRelease(pSdb, pStb);
×
UNCOV
1271
      TAOS_RETURN(code);
×
1272
    }
UNCOV
1273
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1274

1275
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1276
      mError("failed to put stb into array, but continue at this monitor report");
×
1277
    }
1278
    sdbRelease(pSdb, pStb);
×
1279
  }
1280

1281
  // grant info
1282
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
145✔
1283
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
145✔
1284
  if (pMnode->grant.expireTimeMS == 0) {
145✔
1285
    pGrantInfo->expire_time = 0;
×
1286
    pGrantInfo->timeseries_total = 0;
×
1287
  }
1288

1289
  mndReleaseRpc(pMnode);
145✔
1290
  TAOS_RETURN(code);
145✔
1291
}
1292

1293
int32_t mndResetTimer(SMnode *pMnode){
×
UNCOV
1294
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1295
}
1296

1297
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
23,376,354✔
1298
  mTrace("mnode get load");
23,376,354✔
1299
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
23,376,354✔
1300
  pLoad->syncState = state.state;
23,376,354✔
1301
  pLoad->syncRestore = state.restored;
23,376,354✔
1302
  pLoad->syncTerm = state.term;
23,376,354✔
1303
  pLoad->roleTimeMs = state.roleTimeMs;
23,376,354✔
1304
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
23,376,354✔
1305
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1306
  return 0;
23,376,354✔
1307
}
1308

1309
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
6,905,320✔
1310
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
6,905,320✔
1311
  return state.roleTimeMs;
6,905,320✔
1312
}
1313

1314
void mndSetRestored(SMnode *pMnode, bool restored) {
498,607✔
1315
  if (restored) {
498,607✔
1316
    (void)taosThreadRwlockWrlock(&pMnode->lock);
498,607✔
1317
    pMnode->restored = true;
498,607✔
1318
    (void)taosThreadRwlockUnlock(&pMnode->lock);
498,607✔
1319
    mInfo("mnode set restored:%d", restored);
498,607✔
1320
  } else {
UNCOV
1321
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
UNCOV
1322
    pMnode->restored = false;
×
UNCOV
1323
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
1324
    mInfo("mnode set restored:%d", restored);
×
1325
    while (1) {
UNCOV
1326
      if (pMnode->rpcRef <= 0) break;
×
UNCOV
1327
      taosMsleep(3);
×
1328
    }
1329
  }
1330
}
498,607✔
1331

UNCOV
1332
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1333

1334
void mndSetStop(SMnode *pMnode) {
498,607✔
1335
  (void)taosThreadRwlockWrlock(&pMnode->lock);
498,607✔
1336
  pMnode->stopped = true;
498,607✔
1337
  (void)taosThreadRwlockUnlock(&pMnode->lock);
498,607✔
1338
  mInfo("mnode set stopped");
498,607✔
1339
}
498,607✔
1340

1341
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
471,457,602✔
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