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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

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

79
static inline void mndReleaseRpc(SMnode *pMnode) {
249,030,397✔
80
  (void)taosThreadRwlockRdlock(&pMnode->lock);
249,030,397✔
81
#if 1
82
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
249,032,852✔
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);
249,032,488✔
88
}
249,033,654✔
89

90
static void *mndBuildTimerMsg(int32_t *pContLen) {
58,124,868✔
91
  terrno = 0;
58,124,868✔
92
  SMTimerReq timerReq = {0};
58,124,081✔
93

94
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
58,124,081✔
95
  if (contLen <= 0) return NULL;
58,121,715✔
96
  void *pReq = rpcMallocCont(contLen);
58,121,715✔
97
  if (pReq == NULL) return NULL;
58,123,996✔
98

99
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
58,123,996✔
UNCOV
100
    mError("failed to serialize timer msg since %s", terrstr());
×
101
  }
102
  *pContLen = contLen;
58,122,447✔
103
  return pReq;
58,122,090✔
104
}
105

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

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

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

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

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

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

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

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

229
static void mndCalMqRebalance(SMnode *pMnode) {
11,808,688✔
230
  int32_t contLen = 0;
11,808,688✔
231
  void   *pReq = mndBuildTimerMsg(&contLen);
11,808,688✔
232
  if (pReq != NULL) {
11,808,688✔
233
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
11,808,688✔
234
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,808,688✔
UNCOV
235
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
236
    }
237
  }
238
}
11,808,688✔
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✔
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,234,067✔
254
  mTrace("pullup grant msg");
1,234,067✔
255
  int32_t contLen = 0;
1,234,067✔
256
  void   *pReq = mndBuildTimerMsg(&contLen);
1,234,067✔
257
  if (pReq != NULL) {
1,234,067✔
258
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,234,067✔
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,234,067✔
UNCOV
265
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
266
    }
267
  }
268
}
1,234,067✔
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) {
19,358✔
284
  mTrace("increate uptime");
19,358✔
285
  int32_t contLen = 0;
19,358✔
286
  void   *pReq = mndBuildTimerMsg(&contLen);
19,358✔
287
  if (pReq != NULL) {
19,358✔
288
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
19,358✔
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) {
19,358✔
UNCOV
295
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
296
    }
297
  }
298
}
19,358✔
299

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

303
  void *pIter = NULL;
164,576✔
304
  while (1) {
793,120✔
305
    SVgObj *pVgroup = NULL;
957,696✔
306
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
957,696✔
307
    if (pIter == NULL) break;
957,696✔
308

309
    bool stateChanged = false;
793,120✔
310
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
2,006,735✔
311
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,546,184✔
312
      if (pGid->dnodeId == dnodeId) {
1,546,184✔
313
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
332,569✔
314
          mInfo(
136,369✔
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;
136,369✔
320
          pGid->syncRestore = 0;
136,369✔
321
          pGid->syncCanRead = 0;
136,369✔
322
          pGid->startTimeMs = 0;
136,369✔
323
          stateChanged = true;
136,369✔
324
        }
325
        break;
332,569✔
326
      }
327
    }
328

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

339
    sdbRelease(pSdb, pVgroup);
793,120✔
340
  }
341
}
164,576✔
342

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

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

350
  void *pIter = NULL;
4,610,326✔
351
  while (1) {
9,508,945✔
352
    SDnodeObj *pDnode = NULL;
14,119,271✔
353
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
14,119,271✔
354
    if (pIter == NULL) break;
14,119,271✔
355

356
    bool online = mndIsDnodeOnline(pDnode, curMs);
9,508,945✔
357
    if (!online) {
9,508,945✔
358
      mInfo("dnode:%d, in offline state", pDnode->id);
164,576✔
359
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
164,576✔
360
    }
361

362
    sdbRelease(pSdb, pDnode);
9,508,945✔
363
  }
364

365
  mndReleaseRpc(pMnode);
4,610,326✔
366
}
367

368
static bool mnodeIsNotLeader(SMnode *pMnode) {
287,641,509✔
369
  terrno = 0;
287,641,509✔
370
  (void)taosThreadRwlockRdlock(&pMnode->lock);
287,642,841✔
371
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
287,646,638✔
372
  if (terrno != 0) {
287,646,538✔
UNCOV
373
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
374
    return true;
×
375
  }
376

377
  if (state.state != TAOS_SYNC_STATE_LEADER) {
287,646,538✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
23,693,969✔
379
    terrno = TSDB_CODE_SYN_NOT_LEADER;
23,693,686✔
380
    return true;
23,693,686✔
381
  }
382
  if (!state.restored || !pMnode->restored) {
263,952,569✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
28,703✔
384
    terrno = TSDB_CODE_SYN_RESTORING;
27,352✔
385
    return true;
27,352✔
386
  }
387
  (void)taosThreadRwlockUnlock(&pMnode->lock);
263,924,531✔
388
  return false;
263,925,056✔
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) {
23,827,307✔
408
  int32_t code = 0;
23,827,307✔
409
#ifndef TD_ASTRA
410
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
23,827,307✔
411
    mndPullupGrant(pMnode);
1,234,067✔
412
  }
413
  if (sec % tsTtlPushIntervalSec == 0) {
23,827,307✔
414
    mndPullupTtl(pMnode);
2,307,858✔
415
  }
416

417
  if (sec % tsTrimVDbIntervalSec == 0) {
23,827,307✔
418
    mndPullupTrimDb(pMnode);
121✔
419
  }
420

421
  if (sec % tsQueryTrimIntervalSec == 0) {
23,827,307✔
422
    mndPullupQueryTrimDb(pMnode);
2,309,162✔
423
  }
424
#endif
425
#ifdef USE_SHARED_STORAGE
426
  if (tsSsEnabled) {
23,827,307✔
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) {
23,827,307✔
UNCOV
437
    if (sec % tsAuthReqHBInterval == 0) {
×
UNCOV
438
      mndPullupAuth(pMnode);
×
439
    }
440
  }
441
#endif
442
  if (sec % tsTransPullupInterval == 0) {
23,827,307✔
443
    mndPullupTrans(pMnode);
11,809,544✔
444
  }
445

446
  if (sec % tsCompactPullupInterval == 0) {
23,827,307✔
447
    mndPullupCompacts(pMnode);
2,202,353✔
448
  }
449

450
  if (sec % tsScanPullupInterval == 0) {
23,827,307✔
451
    mndPullupScans(pMnode);
2,202,626✔
452
  }
453
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
23,827,307✔
454
    mndPullupInstances(pMnode);
4,612,107✔
455
  }
456
#ifdef USE_TOPIC
457
  if (sec % tsMqRebalanceInterval == 0) {
23,827,307✔
458
    mndCalMqRebalance(pMnode);
11,808,688✔
459
  }
460
#endif
461
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
23,827,307✔
462
    mndPullupTelem(pMnode);
264✔
463
  }
464
  if (sec % tsUptimeInterval == 0) {
23,827,307✔
465
    mndIncreaseUpTime(pMnode);
19,358✔
466
  }
467
}
23,827,307✔
468

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

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

486
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
240,098,575✔
487
  if (ms % (tsStatusTimeoutMs) == 0) {
240,098,575✔
488
    mndCheckDnodeOffline(pMnode);
4,610,326✔
489
  }
490
}
240,098,575✔
491

492
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
23,827,307✔
493
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
23,827,307✔
494
    mndSyncCheckTimeout(pMnode);
643,766✔
495
  }
496
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
23,827,307✔
497
    msmHealthCheck(pMnode);
7,811,284✔
498
  }
499
}
23,827,307✔
500

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

506
  while (1) {
261,746,280✔
507
    lastTime++;
262,234,779✔
508
    taosMsleep(100);
262,234,779✔
509

510
    if (mndGetStop(pMnode)) break;
262,234,779✔
511
    if (lastTime % 10 != 0) continue;
261,746,280✔
512

513
    if (mnodeIsNotLeader(pMnode)) {
25,952,068✔
514
      mTrace("timer not process since mnode is not leader");
2,124,761✔
515
      continue;
2,124,761✔
516
    }
517

518
    int64_t sec = lastTime / 10;
23,827,307✔
519
    mndDoTimerCheckSync(pMnode, sec);
23,827,307✔
520

521
    mndDoTimerPullupTask(pMnode, sec);
23,827,307✔
522
  }
523

524
  return NULL;
488,499✔
525
}
526

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

532
  while (1) {
533
    lastTime += 100;
262,183,634✔
534
    taosMsleep(100);
262,183,634✔
535

536
    if (mndGetStop(pMnode)) break;
262,183,634✔
537
    if (lastTime % 10 != 0) continue;
261,695,135✔
538

539
    if (mnodeIsNotLeader(pMnode)) {
261,695,135✔
540
      mTrace("timer not process since mnode is not leader");
21,596,560✔
541
      continue;
21,596,560✔
542
    }
543

544
    mndDoTimerCheckStatus(pMnode, lastTime);
240,098,575✔
545

546
    mndDoArbTimerPullupTask(pMnode, lastTime);
240,098,575✔
547
  }
548

549
  return NULL;
488,499✔
550
}
551

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

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

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

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

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

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

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

608
  TAOS_RETURN(code);
489,281✔
609
}
610

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

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

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

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

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

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

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

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

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

701
  TAOS_RETURN(0);
22,506,926✔
702
}
703

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

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

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

763
  for (int32_t s = pos; s >= 0; s--) {
22,959,453✔
764
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,470,954✔
765
    mInfo("%s will cleanup", pStep->name);
22,470,954✔
766
    if (pStep->cleanupFp != NULL) {
22,470,954✔
767
      (*pStep->cleanupFp)(pMnode);
21,493,956✔
768
    }
769
  }
770

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

776
static int32_t mndExecSteps(SMnode *pMnode) {
489,281✔
777
  int32_t code = 0;
489,281✔
778
  int32_t size = taosArrayGetSize(pMnode->pSteps);
489,281✔
779
  for (int32_t pos = 0; pos < size; pos++) {
22,996,207✔
780
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,506,926✔
781
    if (pStep->initFp == NULL) continue;
22,506,926✔
782

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

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

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

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

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

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

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

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

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

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

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

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

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

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

931
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
19,734,836✔
932

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

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

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

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

952
  return code;
63,262,226✔
953
}
954

955
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
251,720,216✔
956
  int32_t code = 0;
251,720,216✔
957
  if (!IsReq(pMsg)) TAOS_RETURN(code);
251,720,216✔
958
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
220,893,112✔
959
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
215,337,580✔
960
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
210,111,934✔
961
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
198,956,787✔
962
    TAOS_RETURN(code);
21,935,267✔
963
  }
964

965
  SMnode *pMnode = pMsg->info.node;
198,952,349✔
966
  (void)taosThreadRwlockRdlock(&pMnode->lock);
198,956,298✔
967
  if (pMnode->stopped) {
198,957,249✔
968
    (void)taosThreadRwlockUnlock(&pMnode->lock);
32,036✔
969
    code = TSDB_CODE_APP_IS_STOPPING;
32,036✔
970
    TAOS_RETURN(code);
32,036✔
971
  }
972

973
  terrno = 0;
198,922,552✔
974
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
198,920,772✔
975
  if (terrno != 0) {
198,928,014✔
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) {
198,926,458✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,744,493✔
983
    code = TSDB_CODE_SYN_NOT_LEADER;
4,745,067✔
984
    goto _OVER;
4,745,067✔
985
  }
986

987
  if (!state.restored || !pMnode->restored) {
194,181,965✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,539,171✔
989
    code = TSDB_CODE_SYN_RESTORING;
2,539,060✔
990
    goto _OVER;
2,539,060✔
991
  }
992

993
#if 1
994
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
191,643,644✔
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);
191,645,051✔
1001
  TAOS_RETURN(code);
191,642,175✔
1002

1003
_OVER:
7,284,127✔
1004
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
7,284,127✔
1005
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
7,284,127✔
1006
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
7,284,127✔
1007
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
7,284,127✔
1008
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
7,283,812✔
1009
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
7,281,521✔
1010
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
7,282,487✔
1011
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
7,281,105✔
1012
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
7,281,216✔
1013
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
14✔
1014
           pMnode->stopped, state.restored, syncStr(state.state));
1015
    TAOS_RETURN(code);
14✔
1016
  }
1017

1018
  const STraceId *trace = &pMsg->info.traceId;
7,279,002✔
1019
  SEpSet          epSet = {0};
7,278,639✔
1020
  mndGetMnodeEpSet(pMnode, &epSet);
7,280,151✔
1021

1022
  mGDebug(
7,284,127✔
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;
7,284,127✔
1029

1030
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
23,645,450✔
1031
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
16,361,323✔
1032
  }
1033

1034
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
7,284,127✔
1035
  pMsg->info.rsp = rpcMallocCont(contLen);
7,279,214✔
1036
  if (pMsg->info.rsp != NULL) {
7,281,902✔
1037
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
7,283,149✔
UNCOV
1038
      mError("failed to serialize ep set");
×
1039
    }
1040
    pMsg->info.hasEpSet = 1;
7,275,793✔
1041
    pMsg->info.rspLen = contLen;
7,277,323✔
1042
  }
1043

1044
  TAOS_RETURN(code);
7,277,996✔
1045
}
1046

1047
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
251,734,340✔
1048
  SMnode         *pMnode = pMsg->info.node;
251,734,340✔
1049
  const STraceId *trace = &pMsg->info.traceId;
251,736,709✔
1050
  int32_t         code = TSDB_CODE_SUCCESS;
251,719,456✔
1051

1052
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
251,719,456✔
1053
  MndMsgFpExt fpExt = NULL;
251,732,844✔
1054
  if (fp == NULL) {
251,732,844✔
1055
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,951,848✔
1056
    if (fpExt == NULL) {
21,952,049✔
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));
251,733,045✔
1064

1065
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
244,418,645✔
1066
  if (fp)
244,417,150✔
1067
    code = (*fp)(pMsg);
222,466,193✔
1068
  else
1069
    code = (*fpExt)(pMsg, pQueueInfo);
21,950,957✔
1070
  mndReleaseRpc(pMnode);
244,420,042✔
1071

1072
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
244,420,193✔
1073
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
32,905,670✔
1074
  } else if (code == 0) {
211,514,523✔
1075
    mGTrace("msg:%p, successfully processed", pMsg);
208,826,118✔
1076
  } else {
1077
    // TODO removve this wrong set code
1078
    if (code == -1) {
2,688,405✔
1079
      code = terrno;
3,160✔
1080
    }
1081
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,688,405✔
1082
            TMSG_INFO(pMsg->msgType));
1083
  }
1084

1085
  TAOS_RETURN(code);
244,420,193✔
1086
}
1087

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1341
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
524,399,540✔
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