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

taosdata / TDengine / #4922

09 Jan 2026 08:13AM UTC coverage: 65.161% (-0.4%) from 65.541%
#4922

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

2171 existing lines in 120 files now uncovered.

197632 of 303297 relevant lines covered (65.16%)

117870313.81 hits per line

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

78.48
/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 "mndRole.h"
44
#include "mndRsma.h"
45
#include "mndScan.h"
46
#include "mndScanDetail.h"
47
#include "mndShow.h"
48
#include "mndSma.h"
49
#include "mndSnode.h"
50
#include "mndSsMigrate.h"
51
#include "mndStb.h"
52
#include "mndStream.h"
53
#include "mndSubscribe.h"
54
#include "mndSync.h"
55
#include "mndTelem.h"
56
#include "mndTopic.h"
57
#include "mndTrans.h"
58
#include "mndUser.h"
59
#include "mndToken.h"
60
#include "mndVgroup.h"
61
#include "mndView.h"
62
#include "mndXnode.h"
63
#include "tencrypt.h"
64

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
232,390,273✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
232,390,273✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
232,391,227✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
232,391,195✔
93
}
232,391,772✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
55,681,689✔
96
  terrno = 0;
55,681,689✔
97
  SMTimerReq timerReq = {0};
55,681,689✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
55,681,436✔
100
  if (contLen <= 0) return NULL;
55,676,804✔
101
  void *pReq = rpcMallocCont(contLen);
55,676,804✔
102
  if (pReq == NULL) return NULL;
55,678,912✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
55,678,912✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
55,680,110✔
108
  return pReq;
55,679,456✔
109
}
110

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

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

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

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

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

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

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

194
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
195
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
196
    return;
×
197
  }
198

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

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

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

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

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

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

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

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

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

305
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
105,445✔
306
  SSdb *pSdb = pMnode->pSdb;
105,445✔
307

308
  void *pIter = NULL;
105,445✔
309
  while (1) {
395,211✔
310
    SVgObj *pVgroup = NULL;
500,656✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
500,656✔
312
    if (pIter == NULL) break;
500,656✔
313

314
    bool stateChanged = false;
395,211✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,007,453✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
779,012✔
317
      if (pGid->dnodeId == dnodeId) {
779,012✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
166,770✔
319
          mInfo(
57,033✔
320
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
321
              "restored:0 "
322
              "canRead:0",
323
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
324
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
57,033✔
325
          pGid->syncRestore = 0;
57,033✔
326
          pGid->syncCanRead = 0;
57,033✔
327
          pGid->startTimeMs = 0;
57,033✔
328
          stateChanged = true;
57,033✔
329
        }
330
        break;
166,770✔
331
      }
332
    }
333

334
    if (stateChanged) {
395,211✔
335
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
57,033✔
336
      if (pDb != NULL && pDb->stateTs != curMs) {
57,033✔
337
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
31,140✔
338
              curMs);
339
        pDb->stateTs = curMs;
31,140✔
340
      }
341
      mndReleaseDb(pMnode, pDb);
57,033✔
342
    }
343

344
    sdbRelease(pSdb, pVgroup);
395,211✔
345
  }
346
}
105,445✔
347

348
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,424,749✔
349
  mTrace("check dnode offline");
4,424,749✔
350
  if (mndAcquireRpc(pMnode) != 0) return;
4,424,749✔
351

352
  SSdb   *pSdb = pMnode->pSdb;
4,424,749✔
353
  int64_t curMs = taosGetTimestampMs();
4,424,749✔
354

355
  void *pIter = NULL;
4,424,749✔
356
  while (1) {
7,866,968✔
357
    SDnodeObj *pDnode = NULL;
12,291,717✔
358
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,291,717✔
359
    if (pIter == NULL) break;
12,291,717✔
360

361
    bool online = mndIsDnodeOnline(pDnode, curMs);
7,866,968✔
362
    if (!online) {
7,866,968✔
363
      mInfo("dnode:%d, in offline state", pDnode->id);
105,445✔
364
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
105,445✔
365
    }
366

367
    sdbRelease(pSdb, pDnode);
7,866,968✔
368
  }
369

370
  mndReleaseRpc(pMnode);
4,424,749✔
371
}
372

373
static bool mnodeIsNotLeader(SMnode *pMnode) {
264,476,178✔
374
  terrno = 0;
264,476,178✔
375
  (void)taosThreadRwlockRdlock(&pMnode->lock);
264,476,752✔
376
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
264,480,039✔
377
  if (terrno != 0) {
264,480,581✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
379
    return true;
×
380
  }
381

382
  if (state.state != TAOS_SYNC_STATE_LEADER) {
264,479,476✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,496,415✔
384
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,496,415✔
385
    return true;
11,496,415✔
386
  }
387
  if (!state.restored || !pMnode->restored) {
252,983,061✔
388
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,177✔
389
    terrno = TSDB_CODE_SYN_RESTORING;
8,504✔
390
    return true;
8,504✔
391
  }
392
  (void)taosThreadRwlockUnlock(&pMnode->lock);
252,972,720✔
393
  return false;
252,973,679✔
394
}
395

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

405
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
406
  min = TMIN(min, telemInt);
×
407
  min = TMIN(min, tsGrantHBInterval);
×
408
  min = TMIN(min, tsUptimeInterval);
×
409

410
  return min <= 1 ? 2 : min;
×
411
}
412
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,857,501✔
413
  int32_t code = 0;
22,857,501✔
414
#ifndef TD_ASTRA
415
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,857,501✔
416
    mndPullupGrant(pMnode);
1,092,161✔
417
  }
418
  if (sec % tsTtlPushIntervalSec == 0) {
22,857,501✔
419
    mndPullupTtl(pMnode);
2,220,347✔
420
  }
421

422
  if (sec % tsTrimVDbIntervalSec == 0) {
22,857,501✔
UNCOV
423
    mndPullupTrimDb(pMnode);
×
424
  }
425

426
  if (sec % tsQueryTrimIntervalSec == 0) {
22,857,501✔
427
    mndPullupQueryTrimDb(pMnode);
2,179,767✔
428
  }
429
#endif
430
#ifdef USE_SHARED_STORAGE
431
  if (tsSsEnabled) {
22,857,501✔
432
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
433
      mndPullupUpdateSsMigrateProgress(pMnode);
×
434
    }
435
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
436
      mndPullupSsMigrateDb(pMnode);
×
437
    }
438
  }
439
#endif
440
#ifdef TD_ENTERPRISE
441
  if (tsAuthReq) {
22,857,501✔
442
    if (sec % tsAuthReqHBInterval == 0) {
×
443
      mndPullupAuth(pMnode);
×
444
    }
445
  }
446
#endif
447
  if (sec % tsTransPullupInterval == 0) {
22,857,501✔
448
    mndPullupTrans(pMnode);
11,334,473✔
449
  }
450

451
  if (sec % tsCompactPullupInterval == 0) {
22,857,501✔
452
    mndPullupCompacts(pMnode);
2,121,781✔
453
  }
454

455
  if (sec % tsScanPullupInterval == 0) {
22,857,501✔
456
    mndPullupScans(pMnode);
2,121,781✔
457
  }
458
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,857,501✔
459
    mndPullupInstances(pMnode);
4,424,519✔
460
  }
461
#ifdef USE_TOPIC
462
  if (sec % tsMqRebalanceInterval == 0) {
22,857,501✔
463
    mndCalMqRebalance(pMnode);
11,334,279✔
464
  }
465
#endif
466
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,857,501✔
467
    mndPullupTelem(pMnode);
160✔
468
  }
469
  if (sec % tsUptimeInterval == 0) {
22,857,501✔
470
    mndIncreaseUpTime(pMnode);
18,003✔
471
  }
472
}
22,857,501✔
473

474
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
230,118,292✔
475
  int32_t code = 0;
230,118,292✔
476
#ifndef TD_ASTRA
477
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
230,118,292✔
478
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,335,780✔
479
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
480
    }
481
  }
482

483
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
230,118,292✔
484
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,498,638✔
485
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
486
    }
487
  }
488
#endif
489
}
230,118,292✔
490

491
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
230,118,292✔
492
  if (ms % (tsStatusTimeoutMs) == 0) {
230,118,292✔
493
    mndCheckDnodeOffline(pMnode);
4,424,749✔
494
  }
495
}
230,118,292✔
496

497
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,857,501✔
498
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,857,501✔
499
    mndSyncCheckTimeout(pMnode);
627,433✔
500
  }
501
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,857,501✔
502
    msmHealthCheck(pMnode);
7,498,656✔
503
  }
504
}
22,857,501✔
505

506
static void *mndThreadSecFp(void *param) {
405,819✔
507
  SMnode *pMnode = param;
405,819✔
508
  int64_t lastTime = 0;
405,819✔
509
  setThreadName("mnode-timer");
405,819✔
510

511
  while (1) {
240,599,370✔
512
    lastTime++;
241,005,189✔
513
    taosMsleep(100);
241,005,189✔
514

515
    if (mndGetStop(pMnode)) break;
241,005,189✔
516
    if (lastTime % 10 != 0) continue;
240,599,370✔
517

518
    if (mnodeIsNotLeader(pMnode)) {
23,886,554✔
519
      mTrace("timer not process since mnode is not leader");
1,029,053✔
520
      continue;
1,029,053✔
521
    }
522

523
    int64_t sec = lastTime / 10;
22,857,501✔
524
    mndDoTimerCheckSync(pMnode, sec);
22,857,501✔
525

526
    mndDoTimerPullupTask(pMnode, sec);
22,857,501✔
527
  }
528

529
  return NULL;
405,819✔
530
}
531

532
static void *mndThreadMsFp(void *param) {
405,819✔
533
  SMnode *pMnode = param;
405,819✔
534
  int64_t lastTime = 0;
405,819✔
535
  setThreadName("mnode-arb-timer");
405,819✔
536

537
  while (1) {
538
    lastTime += 100;
240,999,977✔
539
    taosMsleep(100);
240,999,977✔
540

541
    if (mndGetStop(pMnode)) break;
240,999,977✔
542
    if (lastTime % 10 != 0) continue;
240,594,158✔
543

544
    if (mnodeIsNotLeader(pMnode)) {
240,594,158✔
545
      mTrace("timer not process since mnode is not leader");
10,475,866✔
546
      continue;
10,475,866✔
547
    }
548

549
    mndDoTimerCheckStatus(pMnode, lastTime);
230,118,292✔
550

551
    mndDoArbTimerPullupTask(pMnode, lastTime);
230,118,292✔
552
  }
553

554
  return NULL;
405,819✔
555
}
556

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

570
  (void)taosThreadAttrDestroy(&thAttr);
405,819✔
571
  tmsgReportStartup("mnode-timer", "initialized");
405,819✔
572

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

584
  (void)taosThreadAttrDestroy(&arbAttr);
405,819✔
585
  tmsgReportStartup("mnode-timer", "initialized");
405,819✔
586
  TAOS_RETURN(code);
405,819✔
587
}
588

589
static void mndCleanupTimer(SMnode *pMnode) {
405,819✔
590
  if (taosCheckPthreadValid(pMnode->thread)) {
405,819✔
591
    (void)taosThreadJoin(pMnode->thread, NULL);
405,819✔
592
    taosThreadClear(&pMnode->thread);
405,819✔
593
  }
594
  if (taosCheckPthreadValid(pMnode->arbThread)) {
405,819✔
595
    (void)taosThreadJoin(pMnode->arbThread, NULL);
405,819✔
596
    taosThreadClear(&pMnode->arbThread);
405,819✔
597
  }
598
}
405,819✔
599

600
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
405,880✔
601
  int32_t code = 0;
405,880✔
602
  pMnode->path = taosStrdup(path);
405,880✔
603
  if (pMnode->path == NULL) {
405,880✔
604
    code = terrno;
×
605
    TAOS_RETURN(code);
×
606
  }
607

608
  if (taosMkDir(pMnode->path) != 0) {
405,880✔
609
    code = terrno;
×
610
    TAOS_RETURN(code);
×
611
  }
612

613
  TAOS_RETURN(code);
405,880✔
614
}
615

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

631
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
632
  if (taosWaitCfgKeyLoaded() != 0) {
405,880✔
633
    code = terrno;
×
634
    TAOS_RETURN(code);
×
635
  }
636
  if (tsMetaKey[0] != '\0') {
405,880✔
637
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
3,750✔
638
  }
639
#endif
640

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

649
  TAOS_RETURN(code);
405,880✔
650
}
651

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

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

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

673
  TAOS_RETURN(code);
405,880✔
674
}
675

676
static int32_t mndOpenSdb(SMnode *pMnode) {
405,880✔
677
  int32_t code = 0;
405,880✔
678
  if (!pMnode->deploy) {
405,880✔
679
    code = sdbReadFile(pMnode->pSdb);
117,171✔
680
  }
681

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

684
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
405,880✔
685
  return code;
405,880✔
686
}
687

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

695
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
20,294,000✔
696
  SMnodeStep step = {0};
20,294,000✔
697
  step.name = name;
20,294,000✔
698
  step.initFp = initFp;
20,294,000✔
699
  step.cleanupFp = cleanupFp;
20,294,000✔
700
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
40,588,000✔
701
    TAOS_RETURN(terrno);
×
702
  }
703

704
  TAOS_RETURN(0);
20,294,000✔
705
}
706

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

763
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
405,819✔
764
  if (pMnode->pSteps == NULL) return;
405,819✔
765

766
  if (pos == -1) {
405,819✔
767
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
405,819✔
768
  }
769

770
  for (int32_t s = pos; s >= 0; s--) {
20,696,769✔
771
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
20,290,950✔
772
    mInfo("%s will cleanup", pStep->name);
20,290,950✔
773
    if (pStep->cleanupFp != NULL) {
20,290,950✔
774
      (*pStep->cleanupFp)(pMnode);
19,479,312✔
775
    }
776
  }
777

778
  taosArrayClear(pMnode->pSteps);
405,819✔
779
  taosArrayDestroy(pMnode->pSteps);
405,819✔
780
  pMnode->pSteps = NULL;
405,819✔
781
}
782

783
static int32_t mndExecSteps(SMnode *pMnode) {
405,880✔
784
  int32_t code = 0;
405,880✔
785
  int32_t size = taosArrayGetSize(pMnode->pSteps);
405,880✔
786
  for (int32_t pos = 0; pos < size; pos++) {
20,699,880✔
787
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
20,294,000✔
788
    if (pStep->initFp == NULL) continue;
20,294,000✔
789

790
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
20,294,000✔
791
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
792
      mndCleanupSteps(pMnode, pos);
×
793
      TAOS_RETURN(code);
×
794
    } else {
795
      mInfo("%s is initialized", pStep->name);
20,294,000✔
796
      tmsgReportStartup(pStep->name, "initialized");
20,294,000✔
797
    }
798
  }
799

800
  pMnode->clusterId = mndGetClusterId(pMnode);
405,880✔
801
  TAOS_RETURN(0);
405,880✔
802
}
803

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

815
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
405,880✔
816
  terrno = 0;
405,880✔
817
  mInfo("start to open mnode in %s", path);
405,880✔
818

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

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

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

845
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
405,880✔
846
        pOption->numOfTotalReplicas);
847
  mndSetOptions(pMnode, pOption);
405,880✔
848

849
  pMnode->deploy = pOption->deploy;
405,880✔
850
  pMnode->version = pOption->version;
405,880✔
851
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
405,880✔
852
  if (pMnode->pSteps == NULL) {
405,880✔
853
    taosMemoryFree(pMnode);
×
854
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
855
    mError("failed to open mnode in step 4, since %s", terrstr());
×
856
    return NULL;
×
857
  }
858

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

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

875
  code = mndExecSteps(pMnode);
405,880✔
876
  if (code != 0) {
405,880✔
877
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
878
    mndClose(pMnode);
×
879
    terrno = code;
×
880
    return NULL;
×
881
  }
882

883
  mInfo("mnode open successfully");
405,880✔
884
  return pMnode;
405,880✔
885
}
886

887
void mndPreClose(SMnode *pMnode) {
405,819✔
888
  if (pMnode != NULL) {
405,819✔
889
    int32_t code = 0;
405,819✔
890
    // TODO check return value
891
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
405,819✔
892
    if (code < 0) {
405,819✔
893
      mError("failed to transfer leader since %s", tstrerror(code));
×
894
    }
895
    syncPreStop(pMnode->syncMgmt.sync);
405,819✔
896
    code = sdbWriteFile(pMnode->pSdb, 0);
405,819✔
897
    if (code < 0) {
405,819✔
898
      mError("failed to write sdb since %s", tstrerror(code));
708✔
899
    }
900
  }
901
}
405,819✔
902

903
void mndClose(SMnode *pMnode) {
405,819✔
904
  if (pMnode != NULL) {
405,819✔
905
    mInfo("start to close mnode");
405,819✔
906
    mndCleanupSteps(pMnode, -1);
405,819✔
907
    taosMemoryFreeClear(pMnode->path);
405,819✔
908
    taosMemoryFreeClear(pMnode);
405,819✔
909
    mInfo("mnode is closed");
405,819✔
910
  }
911
}
405,819✔
912

913
int32_t mndStart(SMnode *pMnode) {
405,819✔
914
  mndSyncStart(pMnode);
405,819✔
915
  if (pMnode->deploy) {
405,819✔
916
    if (sdbDeploy(pMnode->pSdb) != 0) {
288,709✔
917
      mError("failed to deploy sdb while start mnode");
×
918
      return -1;
×
919
    }
920
    mndSetRestored(pMnode, true);
288,709✔
921
  }
922
  if (mndIsLeader(pMnode)) {
405,819✔
923
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
362,788✔
924
      mError("failed to upgrade sdb while start mnode");
×
925
      return -1;
×
926
    }
927
  }
928
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
405,819✔
929
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
405,819✔
930

931
  return mndInitTimer(pMnode);
405,819✔
932
}
933

934
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
405,819✔
935

936
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
319,149✔
937

938
int32_t mndIsCatchUp(SMnode *pMnode) {
158,108✔
939
  int64_t rid = pMnode->syncMgmt.sync;
158,108✔
940
  return syncIsCatchUp(rid);
158,108✔
941
}
942

943
ESyncRole mndGetRole(SMnode *pMnode) {
158,108✔
944
  int64_t rid = pMnode->syncMgmt.sync;
158,108✔
945
  return syncGetRole(rid);
158,108✔
946
}
947

948
int64_t mndGetTerm(SMnode *pMnode) {
7,599,380✔
949
  int64_t rid = pMnode->syncMgmt.sync;
7,599,380✔
950
  return syncGetTerm(rid);
7,599,380✔
951
}
952

953
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,936,817✔
954

955
void mndStop(SMnode *pMnode) {
405,819✔
956
  mndSetStop(pMnode);
405,819✔
957
  mndSyncStop(pMnode);
405,819✔
958
  mndCleanupTimer(pMnode);
405,819✔
959
}
405,819✔
960

961
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
50,651,509✔
962
  SMnode    *pMnode = pMsg->info.node;
50,651,509✔
963
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
50,651,509✔
964

965
  const STraceId *trace = &pMsg->info.traceId;
50,651,509✔
966
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
50,651,509✔
967

968
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
50,651,509✔
969
  if (code != 0) {
50,651,509✔
970
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
410✔
971
            tstrerror(code), code);
972
  }
973

974
  return code;
50,651,509✔
975
}
976

977
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
231,129,758✔
978
  int32_t code = 0;
231,129,758✔
979
  if (!IsReq(pMsg)) TAOS_RETURN(code);
231,129,758✔
980
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
202,235,277✔
981
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
197,649,538✔
982
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
193,807,851✔
983
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
184,704,872✔
984
    TAOS_RETURN(code);
17,528,035✔
985
  }
986

987
  SMnode *pMnode = pMsg->info.node;
184,702,498✔
988
  (void)taosThreadRwlockRdlock(&pMnode->lock);
184,701,811✔
989
  if (pMnode->stopped) {
184,708,703✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,836✔
991
    code = TSDB_CODE_APP_IS_STOPPING;
4,836✔
992
    TAOS_RETURN(code);
4,836✔
993
  }
994

995
  terrno = 0;
184,699,108✔
996
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
184,699,663✔
997
  if (terrno != 0) {
184,708,954✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
999
    code = terrno;
×
1000
    TAOS_RETURN(code);
×
1001
  }
1002

1003
  if (state.state != TAOS_SYNC_STATE_LEADER) {
184,708,043✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,846,136✔
1005
    code = TSDB_CODE_SYN_NOT_LEADER;
1,846,227✔
1006
    goto _OVER;
1,846,227✔
1007
  }
1008

1009
  if (!state.restored || !pMnode->restored) {
182,861,907✔
1010
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,327,777✔
1011
    code = TSDB_CODE_SYN_RESTORING;
1,322,249✔
1012
    goto _OVER;
1,322,249✔
1013
  }
1014

1015
#if 1
1016
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
181,534,130✔
1017
#else
1018
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1019
  mTrace("mnode rpc is acquired, ref:%d", ref);
1020
#endif
1021

1022
  (void)taosThreadRwlockUnlock(&pMnode->lock);
181,536,293✔
1023
  TAOS_RETURN(code);
181,536,058✔
1024

1025
_OVER:
3,168,476✔
1026
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,168,476✔
1027
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,167,651✔
1028
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,167,742✔
1029
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,167,651✔
1030
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,166,735✔
1031
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,167,187✔
1032
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,168,296✔
1033
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,167,467✔
1034
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,166,728✔
1035
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
2,457✔
1036
           pMnode->stopped, state.restored, syncStr(state.state));
1037
    TAOS_RETURN(code);
2,457✔
1038
  }
1039

1040
  const STraceId *trace = &pMsg->info.traceId;
3,166,253✔
1041
  SEpSet          epSet = {0};
3,167,106✔
1042
  mndGetMnodeEpSet(pMnode, &epSet);
3,166,274✔
1043

1044
  mGDebug(
3,168,476✔
1045
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1046
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1047
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1048
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1049

1050
  if (epSet.numOfEps <= 0) return -1;
3,168,476✔
1051

1052
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
9,910,256✔
1053
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,741,780✔
1054
  }
1055

1056
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,168,476✔
1057
  pMsg->info.rsp = rpcMallocCont(contLen);
3,164,755✔
1058
  if (pMsg->info.rsp != NULL) {
3,166,174✔
1059
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,167,642✔
1060
      mError("failed to serialize ep set");
×
1061
    }
1062
    pMsg->info.hasEpSet = 1;
3,164,645✔
1063
    pMsg->info.rspLen = contLen;
3,163,773✔
1064
  }
1065

1066
  TAOS_RETURN(code);
3,166,809✔
1067
}
1068

1069
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
231,136,922✔
1070
  SMnode         *pMnode = pMsg->info.node;
231,136,922✔
1071
  const STraceId *trace = &pMsg->info.traceId;
231,139,093✔
1072
  int32_t         code = TSDB_CODE_SUCCESS;
231,139,371✔
1073

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

1096
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
231,129,697✔
1097
  MndMsgFpExt fpExt = NULL;
231,137,391✔
1098
  if (fp == NULL) {
231,137,391✔
1099
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,531,402✔
1100
    if (fpExt == NULL) {
17,531,563✔
1101
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1102
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1103
      TAOS_RETURN(code);
×
1104
    }
1105
  }
1106

1107
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
231,137,552✔
1108

1109
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
227,961,495✔
1110
  if (fp)
227,961,901✔
1111
    code = (*fp)(pMsg);
210,430,235✔
1112
  else
1113
    code = (*fpExt)(pMsg, pQueueInfo);
17,531,666✔
1114
  mndReleaseRpc(pMnode);
227,966,120✔
1115

1116
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
227,964,995✔
1117
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,892,379✔
1118
  } else if (code == 0) {
200,072,616✔
1119
    mGTrace("msg:%p, successfully processed", pMsg);
197,632,973✔
1120
  } else {
1121
    // TODO removve this wrong set code
1122
    if (code == -1) {
2,439,643✔
1123
      code = terrno;
×
1124
    }
1125
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,439,643✔
1126
            TMSG_INFO(pMsg->msgType));
1127
  }
1128

1129
  TAOS_RETURN(code);
227,964,995✔
1130
}
1131

1132
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
94,164,160✔
1133
  tmsg_t type = TMSG_INDEX(msgType);
94,164,160✔
1134
  if (type < TDMT_MAX) {
94,164,160✔
1135
    pMnode->msgFp[type] = fp;
94,164,160✔
1136
  }
1137
}
94,164,160✔
1138

1139
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,247,040✔
1140
  tmsg_t type = TMSG_INDEX(msgType);
3,247,040✔
1141
  if (type < TDMT_MAX) {
3,247,040✔
1142
    pMnode->msgFpExt[type] = fp;
3,247,040✔
1143
  }
1144
}
3,247,040✔
1145

1146
// Note: uid 0 is reserved
1147
int64_t mndGenerateUid(const char *name, int32_t len) {
9,792,416✔
1148
  int32_t hashval = MurmurHash3_32(name, len);
9,792,416✔
1149
  do {
×
1150
    int64_t us = taosGetTimestampUs();
9,792,416✔
1151
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,792,416✔
1152
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,792,416✔
1153
    if (uuid) {
9,792,462✔
1154
      return llabs(uuid);
9,792,462✔
1155
    }
1156
  } while (true);
1157
}
1158

1159
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
81✔
1160
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1161
  int32_t code = mndAcquireRpc(pMnode);
81✔
1162
  if (code < 0) {
81✔
1163
    TAOS_RETURN(code);
×
1164
  } else if (code == 1) {
81✔
1165
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1166
  }
1167

1168
  SSdb   *pSdb = pMnode->pSdb;
81✔
1169
  int64_t ms = taosGetTimestampMs();
81✔
1170

1171
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
81✔
1172
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
81✔
1173
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
81✔
1174
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
81✔
1175
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
81✔
1176
      pStbInfo->stbs == NULL) {
81✔
1177
    mndReleaseRpc(pMnode);
×
1178
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1179
    if (terrno != 0) code = terrno;
×
1180
    TAOS_RETURN(code);
×
1181
  }
1182

1183
  // cluster info
1184
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
81✔
1185
  pClusterInfo->monitor_interval = tsMonitorInterval;
81✔
1186
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
81✔
1187
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
81✔
1188
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
81✔
1189
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
81✔
1190
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
81✔
1191

1192
  void *pIter = NULL;
81✔
1193
  while (1) {
81✔
1194
    SDnodeObj *pObj = NULL;
162✔
1195
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
162✔
1196
    if (pIter == NULL) break;
162✔
1197

1198
    SMonDnodeDesc desc = {0};
81✔
1199
    desc.dnode_id = pObj->id;
81✔
1200
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
81✔
1201
    if (mndIsDnodeOnline(pObj, ms)) {
81✔
1202
      tstrncpy(desc.status, "ready", sizeof(desc.status));
81✔
1203
    } else {
1204
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1205
    }
1206
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
162✔
1207
      mError("failed put dnode into array, but continue at this monitor report")
×
1208
    }
1209
    sdbRelease(pSdb, pObj);
81✔
1210
  }
1211

1212
  pIter = NULL;
81✔
1213
  while (1) {
81✔
1214
    SMnodeObj *pObj = NULL;
162✔
1215
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
162✔
1216
    if (pIter == NULL) break;
162✔
1217

1218
    SMonMnodeDesc desc = {0};
81✔
1219
    desc.mnode_id = pObj->id;
81✔
1220
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
81✔
1221

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

1240
  // vgroup info
1241
  pIter = NULL;
81✔
1242
  while (1) {
162✔
1243
    SVgObj *pVgroup = NULL;
243✔
1244
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
243✔
1245
    if (pIter == NULL) break;
243✔
1246

1247
    if (pVgroup->mountVgId) {
162✔
1248
      sdbRelease(pSdb, pVgroup);
×
1249
      continue;
×
1250
    }
1251

1252
    pClusterInfo->vgroups_total++;
162✔
1253
    pClusterInfo->tbs_total += pVgroup->numOfTables;
162✔
1254

1255
    SMonVgroupDesc desc = {0};
162✔
1256
    desc.vgroup_id = pVgroup->vgId;
162✔
1257

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

1268
    desc.tables_num = pVgroup->numOfTables;
162✔
1269
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
162✔
1270
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
162✔
1271
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
324✔
1272
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
162✔
1273
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
162✔
1274
      pVnDesc->dnode_id = pVgid->dnodeId;
162✔
1275
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
162✔
1276
      pVnDesc->syncState = pVgid->syncState;
162✔
1277
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
162✔
1278
        tstrncpy(desc.status, "ready", sizeof(desc.status));
162✔
1279
        pClusterInfo->vgroups_alive++;
162✔
1280
      }
1281
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
162✔
1282
        pClusterInfo->vnodes_alive++;
162✔
1283
      }
1284
      pClusterInfo->vnodes_total++;
162✔
1285
    }
1286

1287
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
324✔
1288
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1289
    }
1290
    sdbRelease(pSdb, pVgroup);
162✔
1291
  }
1292

1293
  // stb info
1294
  pIter = NULL;
81✔
1295
  while (1) {
×
1296
    SStbObj *pStb = NULL;
81✔
1297
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
81✔
1298
    if (pIter == NULL) break;
81✔
1299

1300
    SMonStbDesc desc = {0};
×
1301

1302
    SName name1 = {0};
×
1303
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1304
    if (code < 0) {
×
1305
      mError("failed to get db name since %s", tstrerror(code));
×
1306
      sdbRelease(pSdb, pStb);
×
1307
      TAOS_RETURN(code);
×
1308
    }
1309
    (void)tNameGetDbName(&name1, desc.database_name);
×
1310

1311
    SName name2 = {0};
×
1312
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1313
    if (code < 0) {
×
1314
      mError("failed to get table name since %s", tstrerror(code));
×
1315
      sdbRelease(pSdb, pStb);
×
1316
      TAOS_RETURN(code);
×
1317
    }
1318
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1319

1320
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1321
      mError("failed to put stb into array, but continue at this monitor report");
×
1322
    }
1323
    sdbRelease(pSdb, pStb);
×
1324
  }
1325

1326
  // grant info
1327
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
81✔
1328
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
81✔
1329
  if (pMnode->grant.expireTimeMS == 0) {
81✔
1330
    pGrantInfo->expire_time = 0;
×
1331
    pGrantInfo->timeseries_total = 0;
×
1332
  }
1333

1334
  mndReleaseRpc(pMnode);
81✔
1335
  TAOS_RETURN(code);
81✔
1336
}
1337

1338
int32_t mndResetTimer(SMnode *pMnode){
×
1339
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1340
}
1341

1342
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
23,936,046✔
1343
  mTrace("mnode get load");
23,936,046✔
1344
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
23,936,046✔
1345
  pLoad->syncState = state.state;
23,936,046✔
1346
  pLoad->syncRestore = state.restored;
23,936,046✔
1347
  pLoad->syncTerm = state.term;
23,936,046✔
1348
  pLoad->roleTimeMs = state.roleTimeMs;
23,936,046✔
1349
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
23,936,046✔
1350
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1351
  return 0;
23,936,046✔
1352
}
1353

1354
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,498,638✔
1355
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,498,638✔
1356
  return state.roleTimeMs;
7,498,638✔
1357
}
1358

1359
void mndSetRestored(SMnode *pMnode, bool restored) {
405,819✔
1360
  if (restored) {
405,819✔
1361
    (void)taosThreadRwlockWrlock(&pMnode->lock);
405,819✔
1362
    pMnode->restored = true;
405,819✔
1363
    (void)taosThreadRwlockUnlock(&pMnode->lock);
405,819✔
1364
    mInfo("mnode set restored:%d", restored);
405,819✔
1365
  } else {
1366
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1367
    pMnode->restored = false;
×
1368
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1369
    mInfo("mnode set restored:%d", restored);
×
1370
    while (1) {
1371
      if (pMnode->rpcRef <= 0) break;
×
1372
      taosMsleep(3);
×
1373
    }
1374
  }
1375
}
405,819✔
1376

1377
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1378

1379
void mndSetStop(SMnode *pMnode) {
405,819✔
1380
  (void)taosThreadRwlockWrlock(&pMnode->lock);
405,819✔
1381
  pMnode->stopped = true;
405,819✔
1382
  (void)taosThreadRwlockUnlock(&pMnode->lock);
405,819✔
1383
  mInfo("mnode set stopped");
405,819✔
1384
}
405,819✔
1385

1386
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
481,985,323✔
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