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

taosdata / TDengine / #4872

04 Dec 2025 01:55AM UTC coverage: 64.678% (+0.02%) from 64.654%
#4872

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

880 of 2219 new or added lines in 36 files covered. (39.66%)

6146 existing lines in 122 files now uncovered.

159679 of 246882 relevant lines covered (64.68%)

110947965.82 hits per line

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

79.77
/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,251,417✔
61
  int32_t code = 0;
4,251,417✔
62
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,251,417✔
63
  if (pMnode->stopped) {
4,251,417✔
UNCOV
64
    code = TSDB_CODE_APP_IS_STOPPING;
×
65
  } else if (!mndIsLeader(pMnode)) {
4,251,417✔
UNCOV
66
    code = 1;
×
67
  } else {
68
#if 1
69
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
4,251,417✔
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,251,417✔
76
  TAOS_RETURN(code);
4,251,417✔
77
}
78

79
static inline void mndReleaseRpc(SMnode *pMnode) {
240,220,802✔
80
  (void)taosThreadRwlockRdlock(&pMnode->lock);
240,220,802✔
81
#if 1
82
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
240,225,345✔
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);
240,224,378✔
88
}
240,225,010✔
89

90
static void *mndBuildTimerMsg(int32_t *pContLen) {
53,846,383✔
91
  terrno = 0;
53,846,383✔
92
  SMTimerReq timerReq = {0};
53,846,504✔
93

94
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
53,846,504✔
95
  if (contLen <= 0) return NULL;
53,845,534✔
96
  void *pReq = rpcMallocCont(contLen);
53,845,534✔
97
  if (pReq == NULL) return NULL;
53,843,767✔
98

99
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
53,843,767✔
UNCOV
100
    mError("failed to serialize timer msg since %s", terrstr());
×
101
  }
102
  *pContLen = contLen;
53,845,051✔
103
  return pReq;
53,845,267✔
104
}
105

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

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

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

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

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

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

179
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,128,512✔
180
  mTrace("pullup trim query");
2,128,512✔
181
  int32_t contLen = 0;
2,128,512✔
182
  void   *pReq = mndBuildTimerMsg(&contLen);
2,128,512✔
183
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,128,512✔
184
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,128,512✔
UNCOV
185
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
186
  }
187
}
2,128,512✔
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,939,503✔
214
  mTrace("pullup arb hb");
10,939,503✔
215
  int32_t contLen = 0;
10,939,503✔
216
  void   *pReq = mndBuildTimerMsg(&contLen);
10,939,503✔
217
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
10,939,503✔
218
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
10,939,503✔
219
}
220

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

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

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

253
static void mndPullupGrant(SMnode *pMnode) {
1,217,759✔
254
  mTrace("pullup grant msg");
1,217,759✔
255
  int32_t contLen = 0;
1,217,759✔
256
  void   *pReq = mndBuildTimerMsg(&contLen);
1,217,759✔
257
  if (pReq != NULL) {
1,217,759✔
258
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,217,759✔
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,217,759✔
UNCOV
265
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
266
    }
267
  }
268
}
1,217,759✔
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) {
17,601✔
284
  mTrace("increate uptime");
17,601✔
285
  int32_t contLen = 0;
17,601✔
286
  void   *pReq = mndBuildTimerMsg(&contLen);
17,601✔
287
  if (pReq != NULL) {
17,601✔
288
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
17,601✔
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) {
17,601✔
UNCOV
295
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
296
    }
297
  }
298
}
17,601✔
299

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

303
  void *pIter = NULL;
170,936✔
304
  while (1) {
799,664✔
305
    SVgObj *pVgroup = NULL;
970,600✔
306
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
970,600✔
307
    if (pIter == NULL) break;
970,600✔
308

309
    bool stateChanged = false;
799,664✔
310
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,994,580✔
311
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,536,314✔
312
      if (pGid->dnodeId == dnodeId) {
1,536,314✔
313
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
341,398✔
314
          mInfo(
169,155✔
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;
169,155✔
320
          pGid->syncRestore = 0;
169,155✔
321
          pGid->syncCanRead = 0;
169,155✔
322
          pGid->startTimeMs = 0;
169,155✔
323
          stateChanged = true;
169,155✔
324
        }
325
        break;
341,398✔
326
      }
327
    }
328

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

339
    sdbRelease(pSdb, pVgroup);
799,664✔
340
  }
341
}
170,936✔
342

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

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

350
  void *pIter = NULL;
4,251,286✔
351
  while (1) {
8,550,683✔
352
    SDnodeObj *pDnode = NULL;
12,801,969✔
353
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,801,969✔
354
    if (pIter == NULL) break;
12,801,969✔
355

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

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

365
  mndReleaseRpc(pMnode);
4,251,286✔
366
}
367

368
static bool mnodeIsNotLeader(SMnode *pMnode) {
268,745,290✔
369
  terrno = 0;
268,745,290✔
370
  (void)taosThreadRwlockRdlock(&pMnode->lock);
268,746,112✔
371
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
268,748,620✔
372
  if (terrno != 0) {
268,750,155✔
UNCOV
373
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
374
    return true;
×
375
  }
376

377
  if (state.state != TAOS_SYNC_STATE_LEADER) {
268,750,021✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
24,050,622✔
379
    terrno = TSDB_CODE_SYN_NOT_LEADER;
24,050,622✔
380
    return true;
24,050,622✔
381
  }
382
  if (!state.restored || !pMnode->restored) {
244,699,399✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,290✔
384
    terrno = TSDB_CODE_SYN_RESTORING;
5,066✔
385
    return true;
5,066✔
386
  }
387
  (void)taosThreadRwlockUnlock(&pMnode->lock);
244,694,109✔
388
  return false;
244,693,528✔
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) {
22,075,391✔
408
  int32_t code = 0;
22,075,391✔
409
#ifndef TD_ASTRA
410
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,075,391✔
411
    mndPullupGrant(pMnode);
1,217,759✔
412
  }
413
  if (sec % tsTtlPushIntervalSec == 0) {
22,075,391✔
414
    mndPullupTtl(pMnode);
2,129,989✔
415
  }
416

417
  if (sec % tsTrimVDbIntervalSec == 0) {
22,075,391✔
418
    mndPullupTrimDb(pMnode);
116✔
419
  }
420

421
  if (sec % tsQueryTrimIntervalSec == 0) {
22,075,391✔
422
    mndPullupQueryTrimDb(pMnode);
2,128,512✔
423
  }
424
#endif
425
#ifdef USE_SHARED_STORAGE
426
  if (tsSsEnabled) {
22,075,391✔
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) {
22,075,391✔
UNCOV
437
    if (sec % tsAuthReqHBInterval == 0) {
×
UNCOV
438
      mndPullupAuth(pMnode);
×
439
    }
440
  }
441
#endif
442
  if (sec % tsTransPullupInterval == 0) {
22,075,391✔
443
    mndPullupTrans(pMnode);
10,937,263✔
444
  }
445

446
  if (sec % tsCompactPullupInterval == 0) {
22,075,391✔
447
    mndPullupCompacts(pMnode);
2,025,134✔
448
  }
449

450
  if (sec % tsScanPullupInterval == 0) {
22,075,391✔
451
    mndPullupScans(pMnode);
2,025,134✔
452
  }
453
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,075,391✔
454
    mndPullupInstances(pMnode);
4,253,905✔
455
  }
456
#ifdef USE_TOPIC
457
  if (sec % tsMqRebalanceInterval == 0) {
22,075,391✔
458
    mndCalMqRebalance(pMnode);
10,936,554✔
459
  }
460
#endif
461
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,075,391✔
462
    mndPullupTelem(pMnode);
264✔
463
  }
464
  if (sec % tsUptimeInterval == 0) {
22,075,391✔
465
    mndIncreaseUpTime(pMnode);
17,601✔
466
  }
467
}
22,075,391✔
468

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

478
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
222,619,076✔
479
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,234,770✔
UNCOV
480
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
481
    }
482
  }
483
#endif
484
}
222,619,076✔
485

486
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
222,619,076✔
487
  if (ms % (tsStatusTimeoutMs) == 0) {
222,619,076✔
488
    mndCheckDnodeOffline(pMnode);
4,251,286✔
489
  }
490
}
222,619,076✔
491

492
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,075,391✔
493
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,075,391✔
494
    mndSyncCheckTimeout(pMnode);
589,259✔
495
  }
496
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,075,391✔
497
    msmHealthCheck(pMnode);
7,232,588✔
498
  }
499
}
22,075,391✔
500

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

506
  while (1) {
244,544,098✔
507
    lastTime++;
245,034,012✔
508
    taosMsleep(100);
245,034,012✔
509

510
    if (mndGetStop(pMnode)) break;
245,034,012✔
511
    if (lastTime % 10 != 0) continue;
244,544,098✔
512

513
    if (mnodeIsNotLeader(pMnode)) {
24,228,645✔
514
      mTrace("timer not process since mnode is not leader");
2,153,254✔
515
      continue;
2,153,254✔
516
    }
517

518
    int64_t sec = lastTime / 10;
22,075,391✔
519
    mndDoTimerCheckSync(pMnode, sec);
22,075,391✔
520

521
    mndDoTimerPullupTask(pMnode, sec);
22,075,391✔
522
  }
523

524
  return NULL;
489,914✔
525
}
526

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

532
  while (1) {
533
    lastTime += 100;
245,011,424✔
534
    taosMsleep(100);
245,011,424✔
535

536
    if (mndGetStop(pMnode)) break;
245,011,424✔
537
    if (lastTime % 10 != 0) continue;
244,521,510✔
538

539
    if (mnodeIsNotLeader(pMnode)) {
244,521,510✔
540
      mTrace("timer not process since mnode is not leader");
21,902,434✔
541
      continue;
21,902,434✔
542
    }
543

544
    mndDoTimerCheckStatus(pMnode, lastTime);
222,619,076✔
545

546
    mndDoArbTimerPullupTask(pMnode, lastTime);
222,619,076✔
547
  }
548

549
  return NULL;
489,914✔
550
}
551

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

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

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

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

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

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

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

608
  TAOS_RETURN(code);
490,686✔
609
}
610

611
static int32_t mndInitWal(SMnode *pMnode) {
490,686✔
612
  int32_t code = 0;
490,686✔
613
  char    path[PATH_MAX + 20] = {0};
490,686✔
614
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
490,686✔
615
  SWalCfg cfg = {.vgId = 1,
490,686✔
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) {
490,686✔
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);
490,686✔
639
  if (pMnode->pWal == NULL) {
490,686✔
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);
490,686✔
647
}
648

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

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

663
  pMnode->pSdb = sdbInit(&opt);
490,686✔
664
  if (pMnode->pSdb == NULL) {
490,686✔
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);
490,686✔
671
}
672

673
static int32_t mndOpenSdb(SMnode *pMnode) {
490,686✔
674
  int32_t code = 0;
490,686✔
675
  if (!pMnode->deploy) {
490,686✔
676
    code = sdbReadFile(pMnode->pSdb);
171,786✔
677
  }
678

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

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

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

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

701
  TAOS_RETURN(0);
22,571,556✔
702
}
703

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

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

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

763
  for (int32_t s = pos; s >= 0; s--) {
23,025,958✔
764
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,536,044✔
765
    mInfo("%s will cleanup", pStep->name);
22,536,044✔
766
    if (pStep->cleanupFp != NULL) {
22,536,044✔
767
      (*pStep->cleanupFp)(pMnode);
21,556,216✔
768
    }
769
  }
770

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

776
static int32_t mndExecSteps(SMnode *pMnode) {
490,686✔
777
  int32_t code = 0;
490,686✔
778
  int32_t size = taosArrayGetSize(pMnode->pSteps);
490,686✔
779
  for (int32_t pos = 0; pos < size; pos++) {
23,062,242✔
780
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,571,556✔
781
    if (pStep->initFp == NULL) continue;
22,571,556✔
782

783
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
22,571,556✔
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,571,556✔
789
      tmsgReportStartup(pStep->name, "initialized");
22,571,556✔
790
    }
791
  }
792

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

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

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

812
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
490,686✔
813
  if (pMnode == NULL) {
490,686✔
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));
490,686✔
819

820
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
490,686✔
821
  if (code != 0) {
490,686✔
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";
490,686✔
829
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
490,686✔
830
  if (code < 0) {
490,686✔
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);
490,686✔
838

839
  pMnode->deploy = pOption->deploy;
490,686✔
840
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
490,686✔
841
  if (pMnode->pSteps == NULL) {
490,686✔
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);
490,686✔
849
  if (code != 0) {
490,686✔
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);
490,686✔
857
  if (code != 0) {
490,686✔
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);
490,686✔
865
  if (code != 0) {
490,686✔
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");
490,686✔
873
  return pMnode;
490,686✔
874
}
875

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

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

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

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

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

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

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

931
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,286,397✔
932

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

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

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

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

952
  return code;
64,734,629✔
953
}
954

955
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
240,719,034✔
956
  int32_t code = 0;
240,719,034✔
957
  if (!IsReq(pMsg)) TAOS_RETURN(code);
240,719,034✔
958
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
209,105,245✔
959
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
203,633,668✔
960
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
198,497,586✔
961
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
187,533,681✔
962
    TAOS_RETURN(code);
21,574,355✔
963
  }
964

965
  SMnode *pMnode = pMsg->info.node;
187,532,958✔
966
  (void)taosThreadRwlockRdlock(&pMnode->lock);
187,535,421✔
967
  if (pMnode->stopped) {
187,535,365✔
968
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,185✔
969
    code = TSDB_CODE_APP_IS_STOPPING;
12,185✔
970
    TAOS_RETURN(code);
12,185✔
971
  }
972

973
  terrno = 0;
187,520,491✔
974
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
187,520,065✔
975
  if (terrno != 0) {
187,526,165✔
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) {
187,524,540✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,069,188✔
983
    code = TSDB_CODE_SYN_NOT_LEADER;
4,068,913✔
984
    goto _OVER;
4,068,913✔
985
  }
986

987
  if (!state.restored || !pMnode->restored) {
183,455,352✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
683,886✔
989
    code = TSDB_CODE_SYN_RESTORING;
681,606✔
990
    goto _OVER;
681,606✔
991
  }
992

993
#if 1
994
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
182,772,293✔
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);
182,774,367✔
1001
  TAOS_RETURN(code);
182,772,378✔
1002

1003
_OVER:
4,750,519✔
1004
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
4,750,519✔
1005
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
4,748,747✔
1006
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
4,746,980✔
1007
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
4,746,980✔
1008
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
4,746,276✔
1009
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
4,745,489✔
1010
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
4,745,983✔
1011
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
4,746,959✔
1012
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
4,746,944✔
1013
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
7,267✔
1014
           pMnode->stopped, state.restored, syncStr(state.state));
1015
    TAOS_RETURN(code);
7,267✔
1016
  }
1017

1018
  const STraceId *trace = &pMsg->info.traceId;
4,743,988✔
1019
  SEpSet          epSet = {0};
4,746,077✔
1020
  mndGetMnodeEpSet(pMnode, &epSet);
4,746,077✔
1021

1022
  mGDebug(
4,746,646✔
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;
4,746,646✔
1029

1030
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
17,236,448✔
1031
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
12,489,802✔
1032
  }
1033

1034
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
4,746,646✔
1035
  pMsg->info.rsp = rpcMallocCont(contLen);
4,745,072✔
1036
  if (pMsg->info.rsp != NULL) {
4,746,646✔
1037
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
4,745,859✔
UNCOV
1038
      mError("failed to serialize ep set");
×
1039
    }
1040
    pMsg->info.hasEpSet = 1;
4,746,360✔
1041
    pMsg->info.rspLen = contLen;
4,746,646✔
1042
  }
1043

1044
  TAOS_RETURN(code);
4,746,526✔
1045
}
1046

1047
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
240,733,327✔
1048
  SMnode         *pMnode = pMsg->info.node;
240,733,327✔
1049
  const STraceId *trace = &pMsg->info.traceId;
240,737,147✔
1050
  int32_t         code = TSDB_CODE_SUCCESS;
240,719,014✔
1051

1052
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
240,719,014✔
1053
  MndMsgFpExt fpExt = NULL;
240,731,324✔
1054
  if (fp == NULL) {
240,731,324✔
1055
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,583,175✔
1056
    if (fpExt == NULL) {
21,583,175✔
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));
240,731,324✔
1064

1065
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
235,970,706✔
1066
  if (fp)
235,970,267✔
1067
    code = (*fp)(pMsg);
214,387,092✔
1068
  else
1069
    code = (*fpExt)(pMsg, pQueueInfo);
21,583,175✔
1070
  mndReleaseRpc(pMnode);
235,970,835✔
1071

1072
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
235,970,103✔
1073
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
32,800,910✔
1074
  } else if (code == 0) {
203,169,193✔
1075
    mGTrace("msg:%p, successfully processed", pMsg);
200,576,456✔
1076
  } else {
1077
    // TODO removve this wrong set code
1078
    if (code == -1) {
2,592,737✔
1079
      code = terrno;
3,155✔
1080
    }
1081
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,592,737✔
1082
            TMSG_INFO(pMsg->msgType));
1083
  }
1084

1085
  TAOS_RETURN(code);
235,970,103✔
1086
}
1087

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

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

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

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

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

1127
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
131✔
1128
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
131✔
1129
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
131✔
1130
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
131✔
1131
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
131✔
1132
      pStbInfo->stbs == NULL) {
131✔
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));
131✔
1141
  pClusterInfo->monitor_interval = tsMonitorInterval;
131✔
1142
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
131✔
1143
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
131✔
1144
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
131✔
1145
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
131✔
1146
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
131✔
1147

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

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

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

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

1178
    if (pObj->id == pMnode->selfDnodeId) {
131✔
1179
      pClusterInfo->first_ep_dnode_id = pObj->id;
131✔
1180
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
131✔
1181
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1182
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
131✔
1183
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1184
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
131✔
1185
      desc.syncState = TAOS_SYNC_STATE_LEADER;
131✔
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) {
262✔
UNCOV
1191
      mError("failed to put mnode into array, but continue at this monitor report");
×
1192
    }
1193
    sdbRelease(pSdb, pObj);
131✔
1194
  }
1195

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

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

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

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

1214
    SName name = {0};
262✔
1215
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
262✔
1216
    if (code < 0) {
262✔
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);
262✔
1222

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

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

1248
  // stb info
1249
  pIter = NULL;
131✔
UNCOV
1250
  while (1) {
×
1251
    SStbObj *pStb = NULL;
131✔
1252
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
131✔
1253
    if (pIter == NULL) break;
131✔
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;
131✔
1283
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
131✔
1284
  if (pMnode->grant.expireTimeMS == 0) {
131✔
1285
    pGrantInfo->expire_time = 0;
×
1286
    pGrantInfo->timeseries_total = 0;
×
1287
  }
1288

1289
  mndReleaseRpc(pMnode);
131✔
1290
  TAOS_RETURN(code);
131✔
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) {
24,224,756✔
1298
  mTrace("mnode get load");
24,224,756✔
1299
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
24,224,756✔
1300
  pLoad->syncState = state.state;
24,224,756✔
1301
  pLoad->syncRestore = state.restored;
24,224,756✔
1302
  pLoad->syncTerm = state.term;
24,224,756✔
1303
  pLoad->roleTimeMs = state.roleTimeMs;
24,224,756✔
1304
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
24,224,756✔
1305
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1306
  return 0;
24,224,756✔
1307
}
1308

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

1314
void mndSetRestored(SMnode *pMnode, bool restored) {
489,914✔
1315
  if (restored) {
489,914✔
1316
    (void)taosThreadRwlockWrlock(&pMnode->lock);
489,914✔
1317
    pMnode->restored = true;
489,914✔
1318
    (void)taosThreadRwlockUnlock(&pMnode->lock);
489,914✔
1319
    mInfo("mnode set restored:%d", restored);
489,914✔
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
}
489,914✔
1331

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

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

1341
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
490,032,482✔
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