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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

120594206.86 hits per line

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

77.66
/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 "tencrypt.h"
63

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

83
static inline void mndReleaseRpc(SMnode *pMnode) {
292,258,913✔
84
  (void)taosThreadRwlockRdlock(&pMnode->lock);
292,258,913✔
85
#if 1
86
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
292,260,369✔
87
#else
88
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
89
  mTrace("mnode rpc is released, ref:%d", ref);
90
#endif
91
  (void)taosThreadRwlockUnlock(&pMnode->lock);
292,260,148✔
92
}
292,259,456✔
93

94
static void *mndBuildTimerMsg(int32_t *pContLen) {
84,304,504✔
95
  terrno = 0;
84,304,504✔
96
  SMTimerReq timerReq = {0};
84,305,115✔
97

98
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
84,305,115✔
99
  if (contLen <= 0) return NULL;
84,304,700✔
100
  void *pReq = rpcMallocCont(contLen);
84,304,700✔
101
  if (pReq == NULL) return NULL;
84,304,416✔
102

103
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
84,304,416✔
104
    mError("failed to serialize timer msg since %s", terrstr());
×
105
  }
106
  *pContLen = contLen;
84,304,529✔
107
  return pReq;
84,303,976✔
108
}
109

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

304
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
143,994✔
305
  SSdb *pSdb = pMnode->pSdb;
143,994✔
306

307
  void *pIter = NULL;
143,994✔
308
  while (1) {
524,874✔
309
    SVgObj *pVgroup = NULL;
668,868✔
310
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
668,868✔
311
    if (pIter == NULL) break;
668,868✔
312

313
    bool stateChanged = false;
524,874✔
314
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,352,917✔
315
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,055,434✔
316
      if (pGid->dnodeId == dnodeId) {
1,055,434✔
317
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
227,391✔
318
          mInfo(
65,280✔
319
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
320
              "restored:0 "
321
              "canRead:0",
322
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
323
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
65,280✔
324
          pGid->syncRestore = 0;
65,280✔
325
          pGid->syncCanRead = 0;
65,280✔
326
          pGid->startTimeMs = 0;
65,280✔
327
          stateChanged = true;
65,280✔
328
        }
329
        break;
227,391✔
330
      }
331
    }
332

333
    if (stateChanged) {
524,874✔
334
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
65,280✔
335
      if (pDb != NULL && pDb->stateTs != curMs) {
65,280✔
336
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
37,987✔
337
              curMs);
338
        pDb->stateTs = curMs;
37,987✔
339
      }
340
      mndReleaseDb(pMnode, pDb);
65,280✔
341
    }
342

343
    sdbRelease(pSdb, pVgroup);
524,874✔
344
  }
345
}
143,994✔
346

347
static void mndCheckDnodeOffline(SMnode *pMnode) {
6,757,598✔
348
  mTrace("check dnode offline");
6,757,598✔
349
  if (mndAcquireRpc(pMnode) != 0) return;
6,757,598✔
350

351
  SSdb   *pSdb = pMnode->pSdb;
6,757,598✔
352
  int64_t curMs = taosGetTimestampMs();
6,757,598✔
353

354
  void *pIter = NULL;
6,757,598✔
355
  while (1) {
10,898,986✔
356
    SDnodeObj *pDnode = NULL;
17,656,584✔
357
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
17,656,584✔
358
    if (pIter == NULL) break;
17,656,584✔
359

360
    bool online = mndIsDnodeOnline(pDnode, curMs);
10,898,986✔
361
    if (!online) {
10,898,986✔
362
      mInfo("dnode:%d, in offline state", pDnode->id);
143,994✔
363
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
143,994✔
364
    }
365

366
    sdbRelease(pSdb, pDnode);
10,898,986✔
367
  }
368

369
  mndReleaseRpc(pMnode);
6,757,598✔
370
}
371

372
static bool mnodeIsNotLeader(SMnode *pMnode) {
394,927,466✔
373
  terrno = 0;
394,927,466✔
374
  (void)taosThreadRwlockRdlock(&pMnode->lock);
394,927,495✔
375
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
394,933,417✔
376
  if (terrno != 0) {
394,933,777✔
377
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
378
    return true;
×
379
  }
380

381
  if (state.state != TAOS_SYNC_STATE_LEADER) {
394,933,292✔
382
    (void)taosThreadRwlockUnlock(&pMnode->lock);
14,130,376✔
383
    terrno = TSDB_CODE_SYN_NOT_LEADER;
14,130,376✔
384
    return true;
14,130,376✔
385
  }
386
  if (!state.restored || !pMnode->restored) {
380,802,916✔
387
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,392✔
388
    terrno = TSDB_CODE_SYN_RESTORING;
1,388✔
389
    return true;
1,388✔
390
  }
391
  (void)taosThreadRwlockUnlock(&pMnode->lock);
380,801,009✔
392
  return false;
380,801,528✔
393
}
394

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

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

409
  return min <= 1 ? 2 : min;
×
410
}
411
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
34,480,848✔
412
  int32_t code = 0;
34,480,848✔
413
#ifndef TD_ASTRA
414
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
34,480,848✔
415
    mndPullupGrant(pMnode);
1,316,922✔
416
  }
417
  if (sec % tsTtlPushIntervalSec == 0) {
34,480,848✔
418
    mndPullupTtl(pMnode);
3,418,073✔
419
  }
420

421
  if (sec % tsTrimVDbIntervalSec == 0) {
34,480,848✔
422
    mndPullupTrimDb(pMnode);
×
423
  }
424

425
  if (sec % tsQueryTrimIntervalSec == 0) {
34,480,848✔
426
    mndPullupQueryTrimDb(pMnode);
3,348,027✔
427
  }
428
#endif
429
#ifdef USE_SHARED_STORAGE
430
  if (tsSsEnabled) {
34,480,848✔
431
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
432
      mndPullupUpdateSsMigrateProgress(pMnode);
×
433
    }
434
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
435
      mndPullupSsMigrateDb(pMnode);
×
436
    }
437
  }
438
#endif
439
#ifdef TD_ENTERPRISE
440
  if (tsAuthReq) {
34,480,848✔
441
    if (sec % tsAuthReqHBInterval == 0) {
×
442
      mndPullupAuth(pMnode);
×
443
    }
444
  }
445
#endif
446
  if (sec % tsTransPullupInterval == 0) {
34,480,848✔
447
    mndPullupTrans(pMnode);
17,150,842✔
448
  }
449

450
  if (sec % tsCompactPullupInterval == 0) {
34,480,848✔
451
    mndPullupCompacts(pMnode);
3,291,187✔
452
  }
453

454
  if (sec % tsScanPullupInterval == 0) {
34,480,848✔
455
    mndPullupScans(pMnode);
3,291,321✔
456
  }
457
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
34,480,848✔
458
    mndPullupInstances(pMnode);
6,755,955✔
459
  }
460
#ifdef USE_TOPIC
461
  if (sec % tsMqRebalanceInterval == 0) {
34,480,848✔
462
    mndCalMqRebalance(pMnode);
17,150,625✔
463
  }
464
#endif
465
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
34,480,848✔
466
    mndPullupTelem(pMnode);
118✔
467
  }
468
  if (sec % tsUptimeInterval == 0) {
34,480,848✔
469
    mndIncreaseUpTime(pMnode);
46,819✔
470
  }
471
}
34,480,848✔
472

473
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
346,321,165✔
474
  int32_t code = 0;
346,321,165✔
475
#ifndef TD_ASTRA
476
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
346,321,165✔
477
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
17,151,075✔
478
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
479
    }
480
  }
481

482
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
346,321,165✔
483
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
11,384,151✔
484
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
485
    }
486
  }
487
#endif
488
}
346,321,165✔
489

490
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
346,321,165✔
491
  if (ms % (tsStatusTimeoutMs) == 0) {
346,321,165✔
492
    mndCheckDnodeOffline(pMnode);
6,757,598✔
493
  }
494
}
346,321,165✔
495

496
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
34,480,848✔
497
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
34,480,848✔
498
    mndSyncCheckTimeout(pMnode);
1,012,176✔
499
  }
500
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
34,480,848✔
501
    msmHealthCheck(pMnode);
11,384,050✔
502
  }
503
}
34,480,848✔
504

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

510
  while (1) {
359,201,990✔
511
    lastTime++;
359,591,124✔
512
    taosMsleep(100);
359,591,124✔
513

514
    if (mndGetStop(pMnode)) break;
359,591,124✔
515
    if (lastTime % 10 != 0) continue;
359,201,990✔
516

517
    if (mnodeIsNotLeader(pMnode)) {
35,753,099✔
518
      mTrace("timer not process since mnode is not leader");
1,272,251✔
519
      continue;
1,272,251✔
520
    }
521

522
    int64_t sec = lastTime / 10;
34,480,848✔
523
    mndDoTimerCheckSync(pMnode, sec);
34,480,848✔
524

525
    mndDoTimerPullupTask(pMnode, sec);
34,480,848✔
526
  }
527

528
  return NULL;
389,134✔
529
}
530

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

536
  while (1) {
537
    lastTime += 100;
359,569,812✔
538
    taosMsleep(100);
359,569,812✔
539

540
    if (mndGetStop(pMnode)) break;
359,569,812✔
541
    if (lastTime % 10 != 0) continue;
359,180,678✔
542

543
    if (mnodeIsNotLeader(pMnode)) {
359,180,678✔
544
      mTrace("timer not process since mnode is not leader");
12,859,513✔
545
      continue;
12,859,513✔
546
    }
547

548
    mndDoTimerCheckStatus(pMnode, lastTime);
346,321,165✔
549

550
    mndDoArbTimerPullupTask(pMnode, lastTime);
346,321,165✔
551
  }
552

553
  return NULL;
389,134✔
554
}
555

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

569
  (void)taosThreadAttrDestroy(&thAttr);
389,134✔
570
  tmsgReportStartup("mnode-timer", "initialized");
389,134✔
571

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

583
  (void)taosThreadAttrDestroy(&arbAttr);
389,134✔
584
  tmsgReportStartup("mnode-timer", "initialized");
389,134✔
585
  TAOS_RETURN(code);
389,134✔
586
}
587

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

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

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

612
  TAOS_RETURN(code);
389,196✔
613
}
614

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

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

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

648
  TAOS_RETURN(code);
389,196✔
649
}
650

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

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

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

672
  TAOS_RETURN(code);
389,196✔
673
}
674

675
static int32_t mndOpenSdb(SMnode *pMnode) {
389,196✔
676
  int32_t code = 0;
389,196✔
677
  if (!pMnode->deploy) {
389,196✔
678
    code = sdbReadFile(pMnode->pSdb);
110,110✔
679
  }
680

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

683
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
389,196✔
684
  return code;
389,196✔
685
}
686

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

694
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,070,604✔
695
  SMnodeStep step = {0};
19,070,604✔
696
  step.name = name;
19,070,604✔
697
  step.initFp = initFp;
19,070,604✔
698
  step.cleanupFp = cleanupFp;
19,070,604✔
699
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
38,141,208✔
700
    TAOS_RETURN(terrno);
×
701
  }
702

703
  TAOS_RETURN(0);
19,070,604✔
704
}
705

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

761
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
389,134✔
762
  if (pMnode->pSteps == NULL) return;
389,134✔
763

764
  if (pos == -1) {
389,134✔
765
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
389,134✔
766
  }
767

768
  for (int32_t s = pos; s >= 0; s--) {
19,456,700✔
769
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,067,566✔
770
    mInfo("%s will cleanup", pStep->name);
19,067,566✔
771
    if (pStep->cleanupFp != NULL) {
19,067,566✔
772
      (*pStep->cleanupFp)(pMnode);
18,289,298✔
773
    }
774
  }
775

776
  taosArrayClear(pMnode->pSteps);
389,134✔
777
  taosArrayDestroy(pMnode->pSteps);
389,134✔
778
  pMnode->pSteps = NULL;
389,134✔
779
}
780

781
static int32_t mndExecSteps(SMnode *pMnode) {
389,196✔
782
  int32_t code = 0;
389,196✔
783
  int32_t size = taosArrayGetSize(pMnode->pSteps);
389,196✔
784
  for (int32_t pos = 0; pos < size; pos++) {
19,459,800✔
785
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,070,604✔
786
    if (pStep->initFp == NULL) continue;
19,070,604✔
787

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

798
  pMnode->clusterId = mndGetClusterId(pMnode);
389,196✔
799
  TAOS_RETURN(0);
389,196✔
800
}
801

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

813
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
389,196✔
814
  terrno = 0;
389,196✔
815
  mInfo("start to open mnode in %s", path);
389,196✔
816

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

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

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

843
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
389,196✔
844
        pOption->numOfTotalReplicas);
845
  mndSetOptions(pMnode, pOption);
389,196✔
846

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

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

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

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

881
  mInfo("mnode open successfully");
389,196✔
882
  return pMnode;
389,196✔
883
}
884

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

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

911
int32_t mndStart(SMnode *pMnode) {
389,134✔
912
  mndSyncStart(pMnode);
389,134✔
913
  if (pMnode->deploy) {
389,134✔
914
    if (sdbDeploy(pMnode->pSdb) != 0) {
279,086✔
915
      mError("failed to deploy sdb while start mnode");
×
916
      return -1;
×
917
    }
918
    mndSetRestored(pMnode, true);
279,086✔
919
  }
920
  if (mndIsLeader(pMnode)) {
389,134✔
921
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
352,736✔
922
      mError("failed to upgrade sdb while start mnode");
×
923
      return -1;
×
924
    }
925
  }
926
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
389,134✔
927
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
389,134✔
928

929
  return mndInitTimer(pMnode);
389,134✔
930
}
931

932
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
389,134✔
933

934
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
304,130✔
935

936
int32_t mndIsCatchUp(SMnode *pMnode) {
218,488✔
937
  int64_t rid = pMnode->syncMgmt.sync;
218,488✔
938
  return syncIsCatchUp(rid);
218,488✔
939
}
940

941
ESyncRole mndGetRole(SMnode *pMnode) {
218,488✔
942
  int64_t rid = pMnode->syncMgmt.sync;
218,488✔
943
  return syncGetRole(rid);
218,488✔
944
}
945

946
int64_t mndGetTerm(SMnode *pMnode) {
11,497,374✔
947
  int64_t rid = pMnode->syncMgmt.sync;
11,497,374✔
948
  return syncGetTerm(rid);
11,497,374✔
949
}
950

951
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
28,650,454✔
952

953
void mndStop(SMnode *pMnode) {
389,134✔
954
  mndSetStop(pMnode);
389,134✔
955
  mndSyncStop(pMnode);
389,134✔
956
  mndCleanupTimer(pMnode);
389,134✔
957
}
389,134✔
958

959
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
52,708,616✔
960
  SMnode    *pMnode = pMsg->info.node;
52,708,616✔
961
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
52,708,616✔
962

963
  const STraceId *trace = &pMsg->info.traceId;
52,708,616✔
964
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
52,708,616✔
965

966
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
52,708,616✔
967
  if (code != 0) {
52,708,616✔
968
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
451✔
969
            tstrerror(code), code);
970
  }
971

972
  return code;
52,708,511✔
973
}
974

975
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
288,008,879✔
976
  int32_t code = 0;
288,008,879✔
977
  if (!IsReq(pMsg)) TAOS_RETURN(code);
288,008,879✔
978
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
255,493,411✔
979
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
251,112,110✔
980
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
247,427,957✔
981
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
238,738,777✔
982
    TAOS_RETURN(code);
16,758,739✔
983
  }
984

985
  SMnode *pMnode = pMsg->info.node;
238,737,126✔
986
  (void)taosThreadRwlockRdlock(&pMnode->lock);
238,738,083✔
987
  if (pMnode->stopped) {
238,742,576✔
988
    (void)taosThreadRwlockUnlock(&pMnode->lock);
200,646✔
989
    code = TSDB_CODE_APP_IS_STOPPING;
200,646✔
990
    TAOS_RETURN(code);
200,646✔
991
  }
992

993
  terrno = 0;
238,541,058✔
994
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
238,539,087✔
995
  if (terrno != 0) {
238,542,341✔
996
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
997
    code = terrno;
×
998
    TAOS_RETURN(code);
×
999
  }
1000

1001
  if (state.state != TAOS_SYNC_STATE_LEADER) {
238,541,797✔
1002
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,896,835✔
1003
    code = TSDB_CODE_SYN_NOT_LEADER;
1,896,835✔
1004
    goto _OVER;
1,896,835✔
1005
  }
1006

1007
  if (!state.restored || !pMnode->restored) {
236,644,962✔
1008
    (void)taosThreadRwlockUnlock(&pMnode->lock);
418,272✔
1009
    code = TSDB_CODE_SYN_RESTORING;
417,350✔
1010
    goto _OVER;
417,350✔
1011
  }
1012

1013
#if 1
1014
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
236,226,772✔
1015
#else
1016
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1017
  mTrace("mnode rpc is acquired, ref:%d", ref);
1018
#endif
1019

1020
  (void)taosThreadRwlockUnlock(&pMnode->lock);
236,227,711✔
1021
  TAOS_RETURN(code);
236,227,220✔
1022

1023
_OVER:
2,314,185✔
1024
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,314,185✔
1025
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,314,185✔
1026
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,314,120✔
1027
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,314,185✔
1028
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,314,185✔
1029
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,314,185✔
1030
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,314,185✔
1031
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,314,120✔
1032
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,314,185✔
1033
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
123✔
1034
           pMnode->stopped, state.restored, syncStr(state.state));
1035
    TAOS_RETURN(code);
123✔
1036
  }
1037

1038
  const STraceId *trace = &pMsg->info.traceId;
2,313,997✔
1039
  SEpSet          epSet = {0};
2,313,997✔
1040
  mndGetMnodeEpSet(pMnode, &epSet);
2,313,997✔
1041

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

1048
  if (epSet.numOfEps <= 0) return -1;
2,314,185✔
1049

1050
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
8,250,938✔
1051
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
5,936,753✔
1052
  }
1053

1054
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,314,185✔
1055
  pMsg->info.rsp = rpcMallocCont(contLen);
2,313,483✔
1056
  if (pMsg->info.rsp != NULL) {
2,314,185✔
1057
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,313,483✔
1058
      mError("failed to serialize ep set");
×
1059
    }
1060
    pMsg->info.hasEpSet = 1;
2,314,185✔
1061
    pMsg->info.rspLen = contLen;
2,314,185✔
1062
  }
1063

1064
  TAOS_RETURN(code);
2,314,185✔
1065
}
1066

1067
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
288,016,316✔
1068
  SMnode         *pMnode = pMsg->info.node;
288,016,316✔
1069
  const STraceId *trace = &pMsg->info.traceId;
288,017,932✔
1070
  int32_t         code = TSDB_CODE_SUCCESS;
288,015,807✔
1071

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

1094
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
288,007,862✔
1095
  MndMsgFpExt fpExt = NULL;
288,015,430✔
1096
  if (fp == NULL) {
288,015,430✔
1097
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
16,759,103✔
1098
    if (fpExt == NULL) {
16,759,103✔
1099
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1100
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1101
      TAOS_RETURN(code);
×
1102
    }
1103
  }
1104

1105
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
288,015,430✔
1106

1107
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
285,498,631✔
1108
  if (fp)
285,498,791✔
1109
    code = (*fp)(pMsg);
268,739,688✔
1110
  else
1111
    code = (*fpExt)(pMsg, pQueueInfo);
16,759,103✔
1112
  mndReleaseRpc(pMnode);
285,500,260✔
1113

1114
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
285,501,190✔
1115
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
26,668,565✔
1116
  } else if (code == 0) {
258,832,625✔
1117
    mGTrace("msg:%p, successfully processed", pMsg);
256,352,515✔
1118
  } else {
1119
    // TODO removve this wrong set code
1120
    if (code == -1) {
2,480,110✔
1121
      code = terrno;
×
1122
    }
1123
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,480,110✔
1124
            TMSG_INFO(pMsg->msgType));
1125
  }
1126

1127
  TAOS_RETURN(code);
285,501,190✔
1128
}
1129

1130
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
83,287,944✔
1131
  tmsg_t type = TMSG_INDEX(msgType);
83,287,944✔
1132
  if (type < TDMT_MAX) {
83,287,944✔
1133
    pMnode->msgFp[type] = fp;
83,287,944✔
1134
  }
1135
}
83,287,944✔
1136

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

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

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

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

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

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

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

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

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

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

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

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

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

1250
    pClusterInfo->vgroups_total++;
130✔
1251
    pClusterInfo->tbs_total += pVgroup->numOfTables;
130✔
1252

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

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

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

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

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

UNCOV
1298
    SMonStbDesc desc = {0};
×
1299

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

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

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

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

1332
  mndReleaseRpc(pMnode);
65✔
1333
  TAOS_RETURN(code);
65✔
1334
}
1335

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

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

1352
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
11,384,151✔
1353
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
11,384,151✔
1354
  return state.roleTimeMs;
11,384,151✔
1355
}
1356

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

1375
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1376

1377
void mndSetStop(SMnode *pMnode) {
389,134✔
1378
  (void)taosThreadRwlockWrlock(&pMnode->lock);
389,134✔
1379
  pMnode->stopped = true;
389,134✔
1380
  (void)taosThreadRwlockUnlock(&pMnode->lock);
389,134✔
1381
  mInfo("mnode set stopped");
389,134✔
1382
}
389,134✔
1383

1384
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
719,148,975✔
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