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

taosdata / TDengine / #4893

20 Dec 2025 01:15PM UTC coverage: 65.57% (-0.001%) from 65.571%
#4893

push

travis-ci

web-flow
feat: support taos_connect_with func (#33952)

* feat: support taos_connect_with

* refactor: enhance connection options and add tests for taos_set_option and taos_connect_with

* fix: handle NULL keys and values in taos_connect_with options

* fix: revert TAOSWS_GIT_TAG to default value "main"

* docs: add TLS configuration options for WebSocket connections in documentation

* docs: modify zh docs and add en docs

* chore: update taos.cfg

* docs: add examples

* docs: add error handling for connection failure in example code

2 of 82 new or added lines in 3 files covered. (2.44%)

1158 existing lines in 109 files now uncovered.

182854 of 278870 relevant lines covered (65.57%)

105213271.06 hits per line

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

80.9
/source/dnode/mnode/impl/src/mndMain.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndAcct.h"
18
#include "mndAnode.h"
19
#include "mndArbGroup.h"
20
#include "mndBnode.h"
21
#include "mndCluster.h"
22
#include "mndCompact.h"
23
#include "mndCompactDetail.h"
24
#include "mndConfig.h"
25
#include "mndConsumer.h"
26
#include "mndDb.h"
27
#include "mndDnode.h"
28
#include "mndEncryptAlgr.h"
29
#include "mndFunc.h"
30
#include "mndGrant.h"
31
#include "mndIndex.h"
32
#include "mndInfoSchema.h"
33
#include "mndInstance.h"
34
#include "mndMnode.h"
35
#include "mndMount.h"
36
#include "mndPerfSchema.h"
37
#include "mndPrivilege.h"
38
#include "mndProfile.h"
39
#include "mndQnode.h"
40
#include "mndQuery.h"
41
#include "mndRetention.h"
42
#include "mndRetentionDetail.h"
43
#include "mndRsma.h"
44
#include "mndScan.h"
45
#include "mndScanDetail.h"
46
#include "mndShow.h"
47
#include "mndSma.h"
48
#include "mndSnode.h"
49
#include "mndSsMigrate.h"
50
#include "mndStb.h"
51
#include "mndStream.h"
52
#include "mndSubscribe.h"
53
#include "mndSync.h"
54
#include "mndTelem.h"
55
#include "mndTopic.h"
56
#include "mndTrans.h"
57
#include "mndUser.h"
58
#include "mndVgroup.h"
59
#include "mndView.h"
60

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

80
static inline void mndReleaseRpc(SMnode *pMnode) {
282,508,325✔
81
  (void)taosThreadRwlockRdlock(&pMnode->lock);
282,508,325✔
82
#if 1
83
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
282,509,207✔
84
#else
85
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
86
  mTrace("mnode rpc is released, ref:%d", ref);
87
#endif
88
  (void)taosThreadRwlockUnlock(&pMnode->lock);
282,508,918✔
89
}
282,508,072✔
90

91
static void *mndBuildTimerMsg(int32_t *pContLen) {
74,244,846✔
92
  terrno = 0;
74,244,846✔
93
  SMTimerReq timerReq = {0};
74,244,405✔
94

95
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
74,244,405✔
96
  if (contLen <= 0) return NULL;
74,242,144✔
97
  void *pReq = rpcMallocCont(contLen);
74,242,144✔
98
  if (pReq == NULL) return NULL;
74,243,831✔
99

100
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
74,243,831✔
101
    mError("failed to serialize timer msg since %s", terrstr());
×
102
  }
103
  *pContLen = contLen;
74,243,070✔
104
  return pReq;
74,243,837✔
105
}
106

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

301
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
235,774✔
302
  SSdb *pSdb = pMnode->pSdb;
235,774✔
303

304
  void *pIter = NULL;
235,774✔
305
  while (1) {
1,383,268✔
306
    SVgObj *pVgroup = NULL;
1,619,042✔
307
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,619,042✔
308
    if (pIter == NULL) break;
1,619,042✔
309

310
    bool stateChanged = false;
1,383,268✔
311
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
3,603,852✔
312
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
2,804,154✔
313
      if (pGid->dnodeId == dnodeId) {
2,804,154✔
314
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
583,570✔
315
          mInfo(
168,358✔
316
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
317
              "restored:0 "
318
              "canRead:0",
319
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
320
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
168,358✔
321
          pGid->syncRestore = 0;
168,358✔
322
          pGid->syncCanRead = 0;
168,358✔
323
          pGid->startTimeMs = 0;
168,358✔
324
          stateChanged = true;
168,358✔
325
        }
326
        break;
583,570✔
327
      }
328
    }
329

330
    if (stateChanged) {
1,383,268✔
331
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
168,358✔
332
      if (pDb != NULL && pDb->stateTs != curMs) {
168,358✔
333
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
102,009✔
334
              curMs);
335
        pDb->stateTs = curMs;
102,009✔
336
      }
337
      mndReleaseDb(pMnode, pDb);
168,358✔
338
    }
339

340
    sdbRelease(pSdb, pVgroup);
1,383,268✔
341
  }
342
}
235,774✔
343

344
static void mndCheckDnodeOffline(SMnode *pMnode) {
5,911,089✔
345
  mTrace("check dnode offline");
5,911,089✔
346
  if (mndAcquireRpc(pMnode) != 0) return;
5,911,089✔
347

348
  SSdb   *pSdb = pMnode->pSdb;
5,911,089✔
349
  int64_t curMs = taosGetTimestampMs();
5,911,089✔
350

351
  void *pIter = NULL;
5,911,089✔
352
  while (1) {
11,098,442✔
353
    SDnodeObj *pDnode = NULL;
17,009,531✔
354
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
17,009,531✔
355
    if (pIter == NULL) break;
17,009,531✔
356

357
    bool online = mndIsDnodeOnline(pDnode, curMs);
11,098,442✔
358
    if (!online) {
11,098,442✔
359
      mInfo("dnode:%d, in offline state", pDnode->id);
235,774✔
360
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
235,774✔
361
    }
362

363
    sdbRelease(pSdb, pDnode);
11,098,442✔
364
  }
365

366
  mndReleaseRpc(pMnode);
5,911,089✔
367
}
368

369
static bool mnodeIsNotLeader(SMnode *pMnode) {
368,583,026✔
370
  terrno = 0;
368,583,026✔
371
  (void)taosThreadRwlockRdlock(&pMnode->lock);
368,584,161✔
372
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
368,586,013✔
373
  if (terrno != 0) {
368,586,285✔
374
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
375
    return true;
×
376
  }
377

378
  if (state.state != TAOS_SYNC_STATE_LEADER) {
368,586,285✔
379
    (void)taosThreadRwlockUnlock(&pMnode->lock);
32,982,064✔
380
    terrno = TSDB_CODE_SYN_NOT_LEADER;
32,982,064✔
381
    return true;
32,982,064✔
382
  }
383
  if (!state.restored || !pMnode->restored) {
335,604,221✔
384
    (void)taosThreadRwlockUnlock(&pMnode->lock);
22,573✔
385
    terrno = TSDB_CODE_SYN_RESTORING;
22,573✔
386
    return true;
22,573✔
387
  }
388
  (void)taosThreadRwlockUnlock(&pMnode->lock);
335,581,648✔
389
  return false;
335,580,853✔
390
}
391

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

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

406
  return min <= 1 ? 2 : min;
×
407
}
408
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
30,343,889✔
409
  int32_t code = 0;
30,343,889✔
410
#ifndef TD_ASTRA
411
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
30,343,889✔
412
    mndPullupGrant(pMnode);
1,435,232✔
413
  }
414
  if (sec % tsTtlPushIntervalSec == 0) {
30,343,889✔
415
    mndPullupTtl(pMnode);
3,009,477✔
416
  }
417

418
  if (sec % tsTrimVDbIntervalSec == 0) {
30,343,889✔
419
    mndPullupTrimDb(pMnode);
118✔
420
  }
421

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

447
  if (sec % tsCompactPullupInterval == 0) {
30,343,889✔
448
    mndPullupCompacts(pMnode);
2,855,232✔
449
  }
450

451
  if (sec % tsScanPullupInterval == 0) {
30,343,889✔
452
    mndPullupScans(pMnode);
2,855,781✔
453
  }
454
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
30,343,889✔
455
    mndPullupInstances(pMnode);
5,913,710✔
456
  }
457
#ifdef USE_TOPIC
458
  if (sec % tsMqRebalanceInterval == 0) {
30,343,889✔
459
    mndCalMqRebalance(pMnode);
15,078,784✔
460
  }
461
#endif
462
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
30,343,889✔
463
    mndPullupTelem(pMnode);
393✔
464
  }
465
  if (sec % tsUptimeInterval == 0) {
30,343,889✔
466
    mndIncreaseUpTime(pMnode);
34,460✔
467
  }
468
}
30,343,889✔
469

470
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
305,237,759✔
471
  int32_t code = 0;
305,237,759✔
472
#ifndef TD_ASTRA
473
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
305,237,759✔
474
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
15,078,138✔
475
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
476
    }
477
  }
478

479
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
305,237,759✔
480
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
9,980,588✔
481
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
482
    }
483
  }
484
#endif
485
}
305,237,759✔
486

487
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
305,237,759✔
488
  if (ms % (tsStatusTimeoutMs) == 0) {
305,237,759✔
489
    mndCheckDnodeOffline(pMnode);
5,911,089✔
490
  }
491
}
305,237,759✔
492

493
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
30,343,889✔
494
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
30,343,889✔
495
    mndSyncCheckTimeout(pMnode);
854,707✔
496
  }
497
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
30,343,889✔
498
    msmHealthCheck(pMnode);
9,981,035✔
499
  }
500
}
30,343,889✔
501

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

507
  while (1) {
335,277,529✔
508
    lastTime++;
335,781,090✔
509
    taosMsleep(100);
335,781,090✔
510

511
    if (mndGetStop(pMnode)) break;
335,781,090✔
512
    if (lastTime % 10 != 0) continue;
335,277,529✔
513

514
    if (mnodeIsNotLeader(pMnode)) {
33,309,505✔
515
      mTrace("timer not process since mnode is not leader");
2,965,616✔
516
      continue;
2,965,616✔
517
    }
518

519
    int64_t sec = lastTime / 10;
30,343,889✔
520
    mndDoTimerCheckSync(pMnode, sec);
30,343,889✔
521

522
    mndDoTimerPullupTask(pMnode, sec);
30,343,889✔
523
  }
524

525
  return NULL;
503,561✔
526
}
527

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

533
  while (1) {
534
    lastTime += 100;
335,780,341✔
535
    taosMsleep(100);
335,780,341✔
536

537
    if (mndGetStop(pMnode)) break;
335,780,341✔
538
    if (lastTime % 10 != 0) continue;
335,276,780✔
539

540
    if (mnodeIsNotLeader(pMnode)) {
335,276,780✔
541
      mTrace("timer not process since mnode is not leader");
30,039,021✔
542
      continue;
30,039,021✔
543
    }
544

545
    mndDoTimerCheckStatus(pMnode, lastTime);
305,237,759✔
546

547
    mndDoArbTimerPullupTask(pMnode, lastTime);
305,237,759✔
548
  }
549

550
  return NULL;
503,561✔
551
}
552

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

566
  (void)taosThreadAttrDestroy(&thAttr);
503,561✔
567
  tmsgReportStartup("mnode-timer", "initialized");
503,561✔
568

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

580
  (void)taosThreadAttrDestroy(&arbAttr);
503,561✔
581
  tmsgReportStartup("mnode-timer", "initialized");
503,561✔
582
  TAOS_RETURN(code);
503,561✔
583
}
584

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

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

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

609
  TAOS_RETURN(code);
503,681✔
610
}
611

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

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

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

647
  TAOS_RETURN(code);
503,681✔
648
}
649

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

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

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

671
  TAOS_RETURN(code);
503,681✔
672
}
673

674
static int32_t mndOpenSdb(SMnode *pMnode) {
503,681✔
675
  int32_t code = 0;
503,681✔
676
  if (!pMnode->deploy) {
503,681✔
677
    code = sdbReadFile(pMnode->pSdb);
179,294✔
678
  }
679

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

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

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

693
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
23,673,007✔
694
  SMnodeStep step = {0};
23,673,007✔
695
  step.name = name;
23,673,007✔
696
  step.initFp = initFp;
23,673,007✔
697
  step.cleanupFp = cleanupFp;
23,673,007✔
698
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
47,346,014✔
699
    TAOS_RETURN(terrno);
×
700
  }
701

702
  TAOS_RETURN(0);
23,673,007✔
703
}
704

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

758
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
503,561✔
759
  if (pMnode->pSteps == NULL) return;
503,561✔
760

761
  if (pos == -1) {
503,561✔
762
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
503,561✔
763
  }
764

765
  for (int32_t s = pos; s >= 0; s--) {
24,170,928✔
766
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
23,667,367✔
767
    mInfo("%s will cleanup", pStep->name);
23,667,367✔
768
    if (pStep->cleanupFp != NULL) {
23,667,367✔
769
      (*pStep->cleanupFp)(pMnode);
22,660,245✔
770
    }
771
  }
772

773
  taosArrayClear(pMnode->pSteps);
503,561✔
774
  taosArrayDestroy(pMnode->pSteps);
503,561✔
775
  pMnode->pSteps = NULL;
503,561✔
776
}
777

778
static int32_t mndExecSteps(SMnode *pMnode) {
503,681✔
779
  int32_t code = 0;
503,681✔
780
  int32_t size = taosArrayGetSize(pMnode->pSteps);
503,681✔
781
  for (int32_t pos = 0; pos < size; pos++) {
24,176,688✔
782
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
23,673,007✔
783
    if (pStep->initFp == NULL) continue;
23,673,007✔
784

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

795
  pMnode->clusterId = mndGetClusterId(pMnode);
503,681✔
796
  TAOS_RETURN(0);
503,681✔
797
}
798

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

810
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
503,681✔
811
  terrno = 0;
503,681✔
812
  mInfo("start to open mnode in %s", path);
503,681✔
813

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

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

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

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

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

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

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

875
  mInfo("mnode open successfully");
503,681✔
876
  return pMnode;
503,681✔
877
}
878

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

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

905
int32_t mndStart(SMnode *pMnode) {
503,561✔
906
  mndSyncStart(pMnode);
503,561✔
907
  if (pMnode->deploy) {
503,561✔
908
    if (sdbDeploy(pMnode->pSdb) != 0) {
324,387✔
909
      mError("failed to deploy sdb while start mnode");
×
910
      return -1;
×
911
    }
912
    mndSetRestored(pMnode, true);
324,387✔
913
  }
914
  if (mndIsLeader(pMnode)) {
503,561✔
915
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
406,511✔
916
      mError("failed to upgrade sdb while start mnode");
×
917
      return -1;
×
918
    }
919
  }
920
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
503,561✔
921
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
503,561✔
922

923
  return mndInitTimer(pMnode);
503,561✔
924
}
925

926
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
503,561✔
927

928
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
388,487✔
929

930
int32_t mndIsCatchUp(SMnode *pMnode) {
398,082✔
931
  int64_t rid = pMnode->syncMgmt.sync;
398,082✔
932
  return syncIsCatchUp(rid);
398,082✔
933
}
934

935
ESyncRole mndGetRole(SMnode *pMnode) {
398,082✔
936
  int64_t rid = pMnode->syncMgmt.sync;
398,082✔
937
  return syncGetRole(rid);
398,082✔
938
}
939

940
int64_t mndGetTerm(SMnode *pMnode) {
10,109,992✔
941
  int64_t rid = pMnode->syncMgmt.sync;
10,109,992✔
942
  return syncGetTerm(rid);
10,109,992✔
943
}
944

945
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
25,190,008✔
946

947
void mndStop(SMnode *pMnode) {
503,561✔
948
  mndSetStop(pMnode);
503,561✔
949
  mndSyncStop(pMnode);
503,561✔
950
  mndCleanupTimer(pMnode);
503,561✔
951
}
503,561✔
952

953
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
71,720,062✔
954
  SMnode    *pMnode = pMsg->info.node;
71,720,062✔
955
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
71,720,062✔
956

957
  const STraceId *trace = &pMsg->info.traceId;
71,720,062✔
958
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
71,720,062✔
959

960
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
71,720,062✔
961
  if (code != 0) {
71,719,770✔
962
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,617✔
963
            tstrerror(code), code);
964
  }
965

966
  return code;
71,720,062✔
967
}
968

969
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
282,063,598✔
970
  int32_t code = 0;
282,063,598✔
971
  if (!IsReq(pMsg)) TAOS_RETURN(code);
282,063,598✔
972
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
248,087,022✔
973
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
242,710,241✔
974
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
237,677,459✔
975
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
226,911,367✔
976
    TAOS_RETURN(code);
21,174,438✔
977
  }
978

979
  SMnode *pMnode = pMsg->info.node;
226,910,598✔
980
  (void)taosThreadRwlockRdlock(&pMnode->lock);
226,910,827✔
981
  if (pMnode->stopped) {
226,914,492✔
982
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,925✔
983
    code = TSDB_CODE_APP_IS_STOPPING;
12,925✔
984
    TAOS_RETURN(code);
12,925✔
985
  }
986

987
  terrno = 0;
226,898,860✔
988
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
226,897,112✔
989
  if (terrno != 0) {
226,903,988✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
991
    code = terrno;
×
992
    TAOS_RETURN(code);
×
993
  }
994

995
  if (state.state != TAOS_SYNC_STATE_LEADER) {
226,903,629✔
996
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,552,450✔
997
    code = TSDB_CODE_SYN_NOT_LEADER;
4,552,450✔
998
    goto _OVER;
4,552,450✔
999
  }
1000

1001
  if (!state.restored || !pMnode->restored) {
222,351,179✔
1002
    (void)taosThreadRwlockUnlock(&pMnode->lock);
915,807✔
1003
    code = TSDB_CODE_SYN_RESTORING;
913,820✔
1004
    goto _OVER;
913,820✔
1005
  }
1006

1007
#if 1
1008
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
221,436,235✔
1009
#else
1010
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1011
  mTrace("mnode rpc is acquired, ref:%d", ref);
1012
#endif
1013

1014
  (void)taosThreadRwlockUnlock(&pMnode->lock);
221,437,308✔
1015
  TAOS_RETURN(code);
221,435,339✔
1016

1017
_OVER:
5,466,270✔
1018
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
5,466,270✔
1019
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
5,466,270✔
1020
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
5,466,270✔
1021
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
5,466,270✔
1022
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
5,465,985✔
1023
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
5,466,270✔
1024
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
5,465,699✔
1025
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
5,465,984✔
1026
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
5,465,985✔
UNCOV
1027
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1028
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1029
    TAOS_RETURN(code);
×
1030
  }
1031

1032
  const STraceId *trace = &pMsg->info.traceId;
5,466,270✔
1033
  SEpSet          epSet = {0};
5,466,270✔
1034
  mndGetMnodeEpSet(pMnode, &epSet);
5,466,270✔
1035

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

1042
  if (epSet.numOfEps <= 0) return -1;
5,466,270✔
1043

1044
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
19,793,751✔
1045
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
14,327,481✔
1046
  }
1047

1048
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
5,466,270✔
1049
  pMsg->info.rsp = rpcMallocCont(contLen);
5,466,270✔
1050
  if (pMsg->info.rsp != NULL) {
5,466,270✔
1051
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
5,466,270✔
1052
      mError("failed to serialize ep set");
×
1053
    }
1054
    pMsg->info.hasEpSet = 1;
5,465,938✔
1055
    pMsg->info.rspLen = contLen;
5,465,938✔
1056
  }
1057

1058
  TAOS_RETURN(code);
5,465,606✔
1059
}
1060

1061
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
282,072,493✔
1062
  SMnode         *pMnode = pMsg->info.node;
282,072,493✔
1063
  const STraceId *trace = &pMsg->info.traceId;
282,074,296✔
1064
  int32_t         code = TSDB_CODE_SUCCESS;
282,069,700✔
1065

1066
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
282,069,700✔
1067
  MndMsgFpExt fpExt = NULL;
282,071,494✔
1068
  if (fp == NULL) {
282,071,494✔
1069
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,182,846✔
1070
    if (fpExt == NULL) {
21,182,846✔
1071
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1072
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1073
      TAOS_RETURN(code);
×
1074
    }
1075
  }
1076

1077
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
282,071,494✔
1078

1079
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
276,593,715✔
1080
  if (fp)
276,591,601✔
1081
    code = (*fp)(pMsg);
255,409,683✔
1082
  else
1083
    code = (*fpExt)(pMsg, pQueueInfo);
21,181,918✔
1084
  mndReleaseRpc(pMnode);
276,597,056✔
1085

1086
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
276,596,054✔
1087
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
33,063,185✔
1088
  } else if (code == 0) {
243,532,869✔
1089
    mGTrace("msg:%p, successfully processed", pMsg);
240,465,155✔
1090
  } else {
1091
    // TODO removve this wrong set code
1092
    if (code == -1) {
3,067,714✔
1093
      code = terrno;
3,200✔
1094
    }
1095
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
3,067,714✔
1096
            TMSG_INFO(pMsg->msgType));
1097
  }
1098

1099
  TAOS_RETURN(code);
276,596,054✔
1100
}
1101

1102
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
103,254,605✔
1103
  tmsg_t type = TMSG_INDEX(msgType);
103,254,605✔
1104
  if (type < TDMT_MAX) {
103,254,605✔
1105
    pMnode->msgFp[type] = fp;
103,254,605✔
1106
  }
1107
}
103,254,605✔
1108

1109
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
4,029,448✔
1110
  tmsg_t type = TMSG_INDEX(msgType);
4,029,448✔
1111
  if (type < TDMT_MAX) {
4,029,448✔
1112
    pMnode->msgFpExt[type] = fp;
4,029,448✔
1113
  }
1114
}
4,029,448✔
1115

1116
// Note: uid 0 is reserved
1117
int64_t mndGenerateUid(const char *name, int32_t len) {
9,332,292✔
1118
  int32_t hashval = MurmurHash3_32(name, len);
9,332,292✔
1119
  do {
×
1120
    int64_t us = taosGetTimestampUs();
9,332,292✔
1121
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,332,292✔
1122
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,332,292✔
1123
    if (uuid) {
9,332,292✔
1124
      return llabs(uuid);
9,332,292✔
1125
    }
1126
  } while (true);
1127
}
1128

1129
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
258✔
1130
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1131
  int32_t code = mndAcquireRpc(pMnode);
258✔
1132
  if (code < 0) {
258✔
1133
    TAOS_RETURN(code);
×
1134
  } else if (code == 1) {
258✔
1135
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1136
  }
1137

1138
  SSdb   *pSdb = pMnode->pSdb;
258✔
1139
  int64_t ms = taosGetTimestampMs();
258✔
1140

1141
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
258✔
1142
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
258✔
1143
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
258✔
1144
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
258✔
1145
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
258✔
1146
      pStbInfo->stbs == NULL) {
258✔
1147
    mndReleaseRpc(pMnode);
×
1148
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1149
    if (terrno != 0) code = terrno;
×
1150
    TAOS_RETURN(code);
×
1151
  }
1152

1153
  // cluster info
1154
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
258✔
1155
  pClusterInfo->monitor_interval = tsMonitorInterval;
258✔
1156
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
258✔
1157
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
258✔
1158
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
258✔
1159
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
258✔
1160
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
258✔
1161

1162
  void *pIter = NULL;
258✔
1163
  while (1) {
258✔
1164
    SDnodeObj *pObj = NULL;
516✔
1165
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
516✔
1166
    if (pIter == NULL) break;
516✔
1167

1168
    SMonDnodeDesc desc = {0};
258✔
1169
    desc.dnode_id = pObj->id;
258✔
1170
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
258✔
1171
    if (mndIsDnodeOnline(pObj, ms)) {
258✔
1172
      tstrncpy(desc.status, "ready", sizeof(desc.status));
258✔
1173
    } else {
1174
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1175
    }
1176
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
516✔
1177
      mError("failed put dnode into array, but continue at this monitor report")
×
1178
    }
1179
    sdbRelease(pSdb, pObj);
258✔
1180
  }
1181

1182
  pIter = NULL;
258✔
1183
  while (1) {
258✔
1184
    SMnodeObj *pObj = NULL;
516✔
1185
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
516✔
1186
    if (pIter == NULL) break;
516✔
1187

1188
    SMonMnodeDesc desc = {0};
258✔
1189
    desc.mnode_id = pObj->id;
258✔
1190
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
258✔
1191

1192
    if (pObj->id == pMnode->selfDnodeId) {
258✔
1193
      pClusterInfo->first_ep_dnode_id = pObj->id;
258✔
1194
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
258✔
1195
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1196
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
258✔
1197
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1198
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
258✔
1199
      desc.syncState = TAOS_SYNC_STATE_LEADER;
258✔
1200
    } else {
1201
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1202
      desc.syncState = pObj->syncState;
×
1203
    }
1204
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
516✔
1205
      mError("failed to put mnode into array, but continue at this monitor report");
×
1206
    }
1207
    sdbRelease(pSdb, pObj);
258✔
1208
  }
1209

1210
  // vgroup info
1211
  pIter = NULL;
258✔
1212
  while (1) {
1,032✔
1213
    SVgObj *pVgroup = NULL;
1,290✔
1214
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,290✔
1215
    if (pIter == NULL) break;
1,290✔
1216

1217
    if (pVgroup->mountVgId) {
1,032✔
1218
      sdbRelease(pSdb, pVgroup);
×
1219
      continue;
×
1220
    }
1221

1222
    pClusterInfo->vgroups_total++;
1,032✔
1223
    pClusterInfo->tbs_total += pVgroup->numOfTables;
1,032✔
1224

1225
    SMonVgroupDesc desc = {0};
1,032✔
1226
    desc.vgroup_id = pVgroup->vgId;
1,032✔
1227

1228
    SName name = {0};
1,032✔
1229
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,032✔
1230
    if (code < 0) {
1,032✔
1231
      mError("failed to get db name since %s", tstrerror(code));
×
1232
      sdbRelease(pSdb, pVgroup);
×
1233
      TAOS_RETURN(code);
×
1234
    }
1235
    (void)tNameGetDbName(&name, desc.database_name);
1,032✔
1236

1237
    desc.tables_num = pVgroup->numOfTables;
1,032✔
1238
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
1,032✔
1239
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
1,032✔
1240
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
2,064✔
1241
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
1,032✔
1242
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
1,032✔
1243
      pVnDesc->dnode_id = pVgid->dnodeId;
1,032✔
1244
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
1,032✔
1245
      pVnDesc->syncState = pVgid->syncState;
1,032✔
1246
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
1,032✔
1247
        tstrncpy(desc.status, "ready", sizeof(desc.status));
1,032✔
1248
        pClusterInfo->vgroups_alive++;
1,032✔
1249
      }
1250
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
1,032✔
1251
        pClusterInfo->vnodes_alive++;
1,032✔
1252
      }
1253
      pClusterInfo->vnodes_total++;
1,032✔
1254
    }
1255

1256
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
2,064✔
1257
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1258
    }
1259
    sdbRelease(pSdb, pVgroup);
1,032✔
1260
  }
1261

1262
  // stb info
1263
  pIter = NULL;
258✔
1264
  while (1) {
129✔
1265
    SStbObj *pStb = NULL;
387✔
1266
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
387✔
1267
    if (pIter == NULL) break;
387✔
1268

1269
    SMonStbDesc desc = {0};
129✔
1270

1271
    SName name1 = {0};
129✔
1272
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
129✔
1273
    if (code < 0) {
129✔
1274
      mError("failed to get db name since %s", tstrerror(code));
×
1275
      sdbRelease(pSdb, pStb);
×
1276
      TAOS_RETURN(code);
×
1277
    }
1278
    (void)tNameGetDbName(&name1, desc.database_name);
129✔
1279

1280
    SName name2 = {0};
129✔
1281
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
129✔
1282
    if (code < 0) {
129✔
1283
      mError("failed to get table name since %s", tstrerror(code));
×
1284
      sdbRelease(pSdb, pStb);
×
1285
      TAOS_RETURN(code);
×
1286
    }
1287
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
129✔
1288

1289
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
258✔
1290
      mError("failed to put stb into array, but continue at this monitor report");
×
1291
    }
1292
    sdbRelease(pSdb, pStb);
129✔
1293
  }
1294

1295
  // grant info
1296
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
258✔
1297
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
258✔
1298
  if (pMnode->grant.expireTimeMS == 0) {
258✔
1299
    pGrantInfo->expire_time = 0;
×
1300
    pGrantInfo->timeseries_total = 0;
×
1301
  }
1302

1303
  mndReleaseRpc(pMnode);
258✔
1304
  TAOS_RETURN(code);
258✔
1305
}
1306

1307
int32_t mndResetTimer(SMnode *pMnode){
×
1308
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1309
}
1310

1311
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
33,322,834✔
1312
  mTrace("mnode get load");
33,322,834✔
1313
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
33,322,834✔
1314
  pLoad->syncState = state.state;
33,322,834✔
1315
  pLoad->syncRestore = state.restored;
33,322,834✔
1316
  pLoad->syncTerm = state.term;
33,322,834✔
1317
  pLoad->roleTimeMs = state.roleTimeMs;
33,322,834✔
1318
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
33,322,834✔
1319
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1320
  return 0;
33,322,834✔
1321
}
1322

1323
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
9,980,588✔
1324
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
9,980,588✔
1325
  return state.roleTimeMs;
9,980,588✔
1326
}
1327

1328
void mndSetRestored(SMnode *pMnode, bool restored) {
503,263✔
1329
  if (restored) {
503,263✔
1330
    (void)taosThreadRwlockWrlock(&pMnode->lock);
503,263✔
1331
    pMnode->restored = true;
503,263✔
1332
    (void)taosThreadRwlockUnlock(&pMnode->lock);
503,263✔
1333
    mInfo("mnode set restored:%d", restored);
503,263✔
1334
  } else {
1335
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1336
    pMnode->restored = false;
×
1337
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1338
    mInfo("mnode set restored:%d", restored);
×
1339
    while (1) {
1340
      if (pMnode->rpcRef <= 0) break;
×
1341
      taosMsleep(3);
×
1342
    }
1343
  }
1344
}
503,263✔
1345

1346
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1347

1348
void mndSetStop(SMnode *pMnode) {
503,561✔
1349
  (void)taosThreadRwlockWrlock(&pMnode->lock);
503,561✔
1350
  pMnode->stopped = true;
503,561✔
1351
  (void)taosThreadRwlockUnlock(&pMnode->lock);
503,561✔
1352
  mInfo("mnode set stopped");
503,561✔
1353
}
503,561✔
1354

1355
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
671,544,919✔
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