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

taosdata / TDengine / #4925

12 Jan 2026 09:34AM UTC coverage: 66.107% (+0.8%) from 65.354%
#4925

push

travis-ci

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

103 of 129 new or added lines in 9 files covered. (79.84%)

891 existing lines in 139 files now uncovered.

200488 of 303278 relevant lines covered (66.11%)

129810096.48 hits per line

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

78.37
/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) {
5,074,099✔
66
  int32_t code = 0;
5,074,099✔
67
  (void)taosThreadRwlockRdlock(&pMnode->lock);
5,074,099✔
68
  if (pMnode->stopped) {
5,074,099✔
69
    code = TSDB_CODE_APP_IS_STOPPING;
×
70
  } else if (!mndIsLeader(pMnode)) {
5,074,099✔
71
    code = 1;
×
72
  } else {
73
#if 1
74
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
5,074,099✔
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);
5,074,099✔
81
  TAOS_RETURN(code);
5,074,099✔
82
}
83

84
static inline void mndReleaseRpc(SMnode *pMnode) {
249,401,353✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
249,401,353✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
249,402,911✔
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);
249,402,749✔
93
}
249,403,253✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
63,616,444✔
96
  terrno = 0;
63,616,444✔
97
  SMTimerReq timerReq = {0};
63,616,454✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
63,616,454✔
100
  if (contLen <= 0) return NULL;
63,614,439✔
101
  void *pReq = rpcMallocCont(contLen);
63,614,439✔
102
  if (pReq == NULL) return NULL;
63,614,439✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
63,614,439✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
63,614,878✔
108
  return pReq;
63,614,878✔
109
}
110

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

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

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

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

162
static void mndPullupTtl(SMnode *pMnode) {
2,534,663✔
163
  mTrace("pullup ttl");
2,534,663✔
164
  int32_t contLen = 0;
2,534,663✔
165
  void   *pReq = mndBuildTimerMsg(&contLen);
2,534,663✔
166
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
2,534,663✔
167
  // TODO check return value
168
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,534,663✔
169
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
170
  }
171
}
2,534,663✔
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,515,126✔
185
  mTrace("pullup trim query");
2,515,126✔
186
  int32_t contLen = 0;
2,515,126✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
2,515,126✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,515,126✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,515,126✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
2,515,126✔
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) {
12,955,156✔
219
  mTrace("pullup arb hb");
12,955,156✔
220
  int32_t contLen = 0;
12,955,156✔
221
  void   *pReq = mndBuildTimerMsg(&contLen);
12,955,156✔
222
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
12,955,156✔
223
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
12,955,156✔
224
}
225

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

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

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

258
static void mndPullupGrant(SMnode *pMnode) {
1,136,538✔
259
  mTrace("pullup grant msg");
1,136,538✔
260
  int32_t contLen = 0;
1,136,538✔
261
  void   *pReq = mndBuildTimerMsg(&contLen);
1,136,538✔
262
  if (pReq != NULL) {
1,136,538✔
263
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,136,538✔
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,136,538✔
270
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
271
    }
272
  }
273
}
1,136,538✔
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) {
24,768✔
289
  mTrace("increate uptime");
24,768✔
290
  int32_t contLen = 0;
24,768✔
291
  void   *pReq = mndBuildTimerMsg(&contLen);
24,768✔
292
  if (pReq != NULL) {
24,768✔
293
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
24,768✔
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) {
24,768✔
300
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
301
    }
302
  }
303
}
24,768✔
304

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

308
  void *pIter = NULL;
107,346✔
309
  while (1) {
360,774✔
310
    SVgObj *pVgroup = NULL;
468,120✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
468,120✔
312
    if (pIter == NULL) break;
468,120✔
313

314
    bool stateChanged = false;
360,774✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
925,682✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
717,187✔
317
      if (pGid->dnodeId == dnodeId) {
717,187✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
152,279✔
319
          mInfo(
50,282✔
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;
50,282✔
325
          pGid->syncRestore = 0;
50,282✔
326
          pGid->syncCanRead = 0;
50,282✔
327
          pGid->startTimeMs = 0;
50,282✔
328
          stateChanged = true;
50,282✔
329
        }
330
        break;
152,279✔
331
      }
332
    }
333

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

344
    sdbRelease(pSdb, pVgroup);
360,774✔
345
  }
346
}
107,346✔
347

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

352
  SSdb   *pSdb = pMnode->pSdb;
5,074,034✔
353
  int64_t curMs = taosGetTimestampMs();
5,074,034✔
354

355
  void *pIter = NULL;
5,074,034✔
356
  while (1) {
8,452,650✔
357
    SDnodeObj *pDnode = NULL;
13,526,684✔
358
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,526,684✔
359
    if (pIter == NULL) break;
13,526,684✔
360

361
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,452,650✔
362
    if (!online) {
8,452,650✔
363
      mInfo("dnode:%d, in offline state", pDnode->id);
107,346✔
364
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
107,346✔
365
    }
366

367
    sdbRelease(pSdb, pDnode);
8,452,650✔
368
  }
369

370
  mndReleaseRpc(pMnode);
5,074,034✔
371
}
372

373
static bool mnodeIsNotLeader(SMnode *pMnode) {
299,104,747✔
374
  terrno = 0;
299,104,747✔
375
  (void)taosThreadRwlockRdlock(&pMnode->lock);
299,104,827✔
376
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
299,108,167✔
377
  if (terrno != 0) {
299,108,196✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
379
    return true;
×
380
  }
381

382
  if (state.state != TAOS_SYNC_STATE_LEADER) {
299,108,120✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,493,016✔
384
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,493,092✔
385
    return true;
10,493,016✔
386
  }
387
  if (!state.restored || !pMnode->restored) {
288,615,104✔
388
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,453✔
389
    terrno = TSDB_CODE_SYN_RESTORING;
977✔
390
    return true;
977✔
391
  }
392
  (void)taosThreadRwlockUnlock(&pMnode->lock);
288,612,941✔
393
  return false;
288,613,426✔
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) {
26,095,684✔
413
  int32_t code = 0;
26,095,684✔
414
#ifndef TD_ASTRA
415
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
26,095,684✔
416
    mndPullupGrant(pMnode);
1,136,538✔
417
  }
418
  if (sec % tsTtlPushIntervalSec == 0) {
26,095,684✔
419
    mndPullupTtl(pMnode);
2,534,663✔
420
  }
421

422
  if (sec % tsTrimVDbIntervalSec == 0) {
26,095,684✔
UNCOV
423
    mndPullupTrimDb(pMnode);
×
424
  }
425

426
  if (sec % tsQueryTrimIntervalSec == 0) {
26,095,684✔
427
    mndPullupQueryTrimDb(pMnode);
2,515,126✔
428
  }
429
#endif
430
#ifdef USE_SHARED_STORAGE
431
  if (tsSsEnabled) {
26,095,684✔
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) {
26,095,684✔
442
    if (sec % tsAuthReqHBInterval == 0) {
×
443
      mndPullupAuth(pMnode);
×
444
    }
445
  }
446
#endif
447
  if (sec % tsTransPullupInterval == 0) {
26,095,684✔
448
    mndPullupTrans(pMnode);
12,956,716✔
449
  }
450

451
  if (sec % tsCompactPullupInterval == 0) {
26,095,684✔
452
    mndPullupCompacts(pMnode);
2,442,911✔
453
  }
454

455
  if (sec % tsScanPullupInterval == 0) {
26,095,684✔
456
    mndPullupScans(pMnode);
2,442,911✔
457
  }
458
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
26,095,684✔
459
    mndPullupInstances(pMnode);
5,073,451✔
460
  }
461
#ifdef USE_TOPIC
462
  if (sec % tsMqRebalanceInterval == 0) {
26,095,684✔
463
    mndCalMqRebalance(pMnode);
12,956,606✔
464
  }
465
#endif
466
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
26,095,684✔
467
    mndPullupTelem(pMnode);
128✔
468
  }
469
  if (sec % tsUptimeInterval == 0) {
26,095,684✔
470
    mndIncreaseUpTime(pMnode);
24,768✔
471
  }
472
}
26,095,684✔
473

474
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
262,518,443✔
475
  int32_t code = 0;
262,518,443✔
476
#ifndef TD_ASTRA
477
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
262,518,443✔
478
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
12,955,156✔
479
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
480
    }
481
  }
482

483
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
262,518,443✔
484
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
8,577,843✔
485
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
486
    }
487
  }
488
#endif
489
}
262,518,443✔
490

491
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
262,518,443✔
492
  if (ms % (tsStatusTimeoutMs) == 0) {
262,518,443✔
493
    mndCheckDnodeOffline(pMnode);
5,074,034✔
494
  }
495
}
262,518,443✔
496

497
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
26,095,684✔
498
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
26,095,684✔
499
    mndSyncCheckTimeout(pMnode);
734,533✔
500
  }
501
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
26,095,684✔
502
    msmHealthCheck(pMnode);
8,577,709✔
503
  }
504
}
26,095,684✔
505

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

511
  while (1) {
272,099,690✔
512
    lastTime++;
272,497,465✔
513
    taosMsleep(100);
272,497,465✔
514

515
    if (mndGetStop(pMnode)) break;
272,497,465✔
516
    if (lastTime % 10 != 0) continue;
272,099,690✔
517

518
    if (mnodeIsNotLeader(pMnode)) {
27,034,454✔
519
      mTrace("timer not process since mnode is not leader");
938,770✔
520
      continue;
938,770✔
521
    }
522

523
    int64_t sec = lastTime / 10;
26,095,684✔
524
    mndDoTimerCheckSync(pMnode, sec);
26,095,684✔
525

526
    mndDoTimerPullupTask(pMnode, sec);
26,095,684✔
527
  }
528

529
  return NULL;
397,775✔
530
}
531

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

537
  while (1) {
538
    lastTime += 100;
272,471,517✔
539
    taosMsleep(100);
272,471,517✔
540

541
    if (mndGetStop(pMnode)) break;
272,471,517✔
542
    if (lastTime % 10 != 0) continue;
272,073,742✔
543

544
    if (mnodeIsNotLeader(pMnode)) {
272,073,742✔
545
      mTrace("timer not process since mnode is not leader");
9,555,299✔
546
      continue;
9,555,299✔
547
    }
548

549
    mndDoTimerCheckStatus(pMnode, lastTime);
262,518,443✔
550

551
    mndDoArbTimerPullupTask(pMnode, lastTime);
262,518,443✔
552
  }
553

554
  return NULL;
397,775✔
555
}
556

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

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

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

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

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

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

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

613
  TAOS_RETURN(code);
397,834✔
614
}
615

616
static int32_t mndInitWal(SMnode *pMnode) {
397,834✔
617
  int32_t code = 0;
397,834✔
618
  char    path[PATH_MAX + 20] = {0};
397,834✔
619
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
397,834✔
620
  SWalCfg cfg = {.vgId = 1,
397,834✔
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) {
397,834✔
633
    code = terrno;
×
634
    TAOS_RETURN(code);
×
635
  }
636
  if (tsMetaKey[0] != '\0') {
397,834✔
637
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
3,749✔
638
  }
639
#endif
640

641
  pMnode->pWal = walOpen(path, &cfg);
397,834✔
642
  if (pMnode->pWal == NULL) {
397,834✔
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);
397,834✔
650
}
651

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

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

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

673
  TAOS_RETURN(code);
397,834✔
674
}
675

676
static int32_t mndOpenSdb(SMnode *pMnode) {
397,834✔
677
  int32_t code = 0;
397,834✔
678
  if (!pMnode->deploy) {
397,834✔
679
    code = sdbReadFile(pMnode->pSdb);
113,692✔
680
  }
681

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

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

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

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

704
  TAOS_RETURN(0);
19,891,700✔
705
}
706

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

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

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

770
  for (int32_t s = pos; s >= 0; s--) {
20,286,525✔
771
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,888,750✔
772
    mInfo("%s will cleanup", pStep->name);
19,888,750✔
773
    if (pStep->cleanupFp != NULL) {
19,888,750✔
774
      (*pStep->cleanupFp)(pMnode);
19,093,200✔
775
    }
776
  }
777

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

783
static int32_t mndExecSteps(SMnode *pMnode) {
397,834✔
784
  int32_t code = 0;
397,834✔
785
  int32_t size = taosArrayGetSize(pMnode->pSteps);
397,834✔
786
  for (int32_t pos = 0; pos < size; pos++) {
20,289,534✔
787
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,891,700✔
788
    if (pStep->initFp == NULL) continue;
19,891,700✔
789

790
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
19,891,700✔
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);
19,891,700✔
796
      tmsgReportStartup(pStep->name, "initialized");
19,891,700✔
797
    }
798
  }
799

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

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

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

819
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
397,834✔
820
  if (pMnode == NULL) {
397,834✔
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));
397,834✔
826

827
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
397,834✔
828
  if (code != 0) {
397,834✔
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";
397,834✔
836
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
397,834✔
837
  if (code < 0) {
397,834✔
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,
397,834✔
846
        pOption->numOfTotalReplicas);
847
  mndSetOptions(pMnode, pOption);
397,834✔
848

849
  pMnode->deploy = pOption->deploy;
397,834✔
850
  pMnode->version = pOption->version;
397,834✔
851
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
397,834✔
852
  if (pMnode->pSteps == NULL) {
397,834✔
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);
397,834✔
860
  if (code != 0) {
397,834✔
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);
397,834✔
868
  if (code != 0) {
397,834✔
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);
397,834✔
876
  if (code != 0) {
397,834✔
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");
397,834✔
884
  return pMnode;
397,834✔
885
}
886

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

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

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

931
  return mndInitTimer(pMnode);
397,775✔
932
}
933

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

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

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

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

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

953
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,646,035✔
954

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

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

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

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

974
  return code;
50,943,205✔
975
}
976

977
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
248,111,270✔
978
  int32_t code = 0;
248,111,270✔
979
  if (!IsReq(pMsg)) TAOS_RETURN(code);
248,111,270✔
980
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
218,293,745✔
981
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
213,641,693✔
982
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
209,818,293✔
983
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
200,681,245✔
984
    TAOS_RETURN(code);
17,623,675✔
985
  }
986

987
  SMnode *pMnode = pMsg->info.node;
200,682,470✔
988
  (void)taosThreadRwlockRdlock(&pMnode->lock);
200,677,630✔
989
  if (pMnode->stopped) {
200,687,067✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,656✔
991
    code = TSDB_CODE_APP_IS_STOPPING;
2,656✔
992
    TAOS_RETURN(code);
2,656✔
993
  }
994

995
  terrno = 0;
200,670,769✔
996
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
200,679,525✔
997
  if (terrno != 0) {
200,686,064✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
999
    code = terrno;
×
1000
    TAOS_RETURN(code);
×
1001
  }
1002

1003
  if (state.state != TAOS_SYNC_STATE_LEADER) {
200,684,922✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,894,202✔
1005
    code = TSDB_CODE_SYN_NOT_LEADER;
1,894,202✔
1006
    goto _OVER;
1,894,202✔
1007
  }
1008

1009
  if (!state.restored || !pMnode->restored) {
198,790,720✔
1010
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,898,919✔
1011
    code = TSDB_CODE_SYN_RESTORING;
1,896,334✔
1012
    goto _OVER;
1,896,334✔
1013
  }
1014

1015
#if 1
1016
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
196,892,218✔
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);
196,894,281✔
1023
  TAOS_RETURN(code);
196,894,394✔
1024

1025
_OVER:
3,790,536✔
1026
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,790,536✔
1027
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,789,716✔
1028
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,789,638✔
1029
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,790,375✔
1030
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,788,730✔
1031
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,789,306✔
1032
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,788,814✔
1033
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,788,155✔
1034
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,787,749✔
1035
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
6,445✔
1036
           pMnode->stopped, state.restored, syncStr(state.state));
1037
    TAOS_RETURN(code);
6,445✔
1038
  }
1039

1040
  const STraceId *trace = &pMsg->info.traceId;
3,789,551✔
1041
  SEpSet          epSet = {0};
3,788,144✔
1042
  mndGetMnodeEpSet(pMnode, &epSet);
3,788,227✔
1043

1044
  mGDebug(
3,790,536✔
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,790,536✔
1051

1052
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
11,190,880✔
1053
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
7,400,344✔
1054
  }
1055

1056
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,790,536✔
1057
  pMsg->info.rsp = rpcMallocCont(contLen);
3,788,812✔
1058
  if (pMsg->info.rsp != NULL) {
3,790,439✔
1059
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,789,714✔
1060
      mError("failed to serialize ep set");
×
1061
    }
1062
    pMsg->info.hasEpSet = 1;
3,788,578✔
1063
    pMsg->info.rspLen = contLen;
3,787,710✔
1064
  }
1065

1066
  TAOS_RETURN(code);
3,788,911✔
1067
}
1068

1069
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
248,119,256✔
1070
  SMnode         *pMnode = pMsg->info.node;
248,119,256✔
1071
  const STraceId *trace = &pMsg->info.traceId;
248,121,904✔
1072
  int32_t         code = TSDB_CODE_SUCCESS;
248,120,846✔
1073

1074
#ifdef TD_ENTERPRISE
1075
  if (pMsg->info.conn.isToken) {
248,120,846✔
1076
    SCachedTokenInfo ti = {0};
518✔
1077
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
518✔
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) {
518✔
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)) {
518✔
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));
518✔
1093
  }
1094
#endif
1095

1096
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
248,103,957✔
1097
  MndMsgFpExt fpExt = NULL;
248,115,905✔
1098
  if (fp == NULL) {
248,115,905✔
1099
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,616,495✔
1100
    if (fpExt == NULL) {
17,616,200✔
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));
248,115,610✔
1108

1109
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
244,324,605✔
1110
  if (fp)
244,325,568✔
1111
    code = (*fp)(pMsg);
226,709,368✔
1112
  else
1113
    code = (*fpExt)(pMsg, pQueueInfo);
17,616,200✔
1114
  mndReleaseRpc(pMnode);
244,325,857✔
1115

1116
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
244,328,790✔
1117
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,905,434✔
1118
  } else if (code == 0) {
216,423,356✔
1119
    mGTrace("msg:%p, successfully processed", pMsg);
213,967,555✔
1120
  } else {
1121
    // TODO removve this wrong set code
1122
    if (code == -1) {
2,455,801✔
1123
      code = terrno;
×
1124
    }
1125
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,455,801✔
1126
            TMSG_INFO(pMsg->msgType));
1127
  }
1128

1129
  TAOS_RETURN(code);
244,328,790✔
1130
}
1131

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

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

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

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

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

1171
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
65✔
1172
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
65✔
1173
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
65✔
1174
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
65✔
1175
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
65✔
1176
      pStbInfo->stbs == NULL) {
65✔
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));
65✔
1185
  pClusterInfo->monitor_interval = tsMonitorInterval;
65✔
1186
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
65✔
1187
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
65✔
1188
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
65✔
1189
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
65✔
1190
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
65✔
1191

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

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

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

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

1222
    if (pObj->id == pMnode->selfDnodeId) {
65✔
1223
      pClusterInfo->first_ep_dnode_id = pObj->id;
65✔
1224
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
65✔
1225
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1226
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
65✔
1227
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1228
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
65✔
1229
      desc.syncState = TAOS_SYNC_STATE_LEADER;
65✔
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) {
130✔
1235
      mError("failed to put mnode into array, but continue at this monitor report");
×
1236
    }
1237
    sdbRelease(pSdb, pObj);
65✔
1238
  }
1239

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

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

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

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

1258
    SName name = {0};
130✔
1259
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
130✔
1260
    if (code < 0) {
130✔
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);
130✔
1267

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

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

1293
  // stb info
1294
  pIter = NULL;
65✔
1295
  while (1) {
×
1296
    SStbObj *pStb = NULL;
65✔
1297
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
65✔
1298
    if (pIter == NULL) break;
65✔
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;
65✔
1328
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
65✔
1329
  if (pMnode->grant.expireTimeMS == 0) {
65✔
1330
    pGrantInfo->expire_time = 0;
×
1331
    pGrantInfo->timeseries_total = 0;
×
1332
  }
1333

1334
  mndReleaseRpc(pMnode);
65✔
1335
  TAOS_RETURN(code);
65✔
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) {
27,187,146✔
1343
  mTrace("mnode get load");
27,187,146✔
1344
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
27,187,146✔
1345
  pLoad->syncState = state.state;
27,187,146✔
1346
  pLoad->syncRestore = state.restored;
27,187,146✔
1347
  pLoad->syncTerm = state.term;
27,187,146✔
1348
  pLoad->roleTimeMs = state.roleTimeMs;
27,187,146✔
1349
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
27,187,146✔
1350
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1351
  return 0;
27,187,146✔
1352
}
1353

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

1359
void mndSetRestored(SMnode *pMnode, bool restored) {
397,775✔
1360
  if (restored) {
397,775✔
1361
    (void)taosThreadRwlockWrlock(&pMnode->lock);
397,775✔
1362
    pMnode->restored = true;
397,775✔
1363
    (void)taosThreadRwlockUnlock(&pMnode->lock);
397,775✔
1364
    mInfo("mnode set restored:%d", restored);
397,775✔
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
}
397,775✔
1376

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

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

1386
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
544,960,215✔
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