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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

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

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

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
232,136,950✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
232,136,950✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
232,138,200✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
232,136,796✔
93
}
232,139,459✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
56,261,426✔
96
  terrno = 0;
56,261,426✔
97
  SMTimerReq timerReq = {0};
56,261,748✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
56,261,357✔
100
  if (contLen <= 0) return NULL;
56,258,640✔
101
  void *pReq = rpcMallocCont(contLen);
56,258,640✔
102
  if (pReq == NULL) return NULL;
56,260,057✔
103

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

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

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

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

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

162
static void mndPullupTtl(SMnode *pMnode) {
2,244,370✔
163
  mTrace("pullup ttl");
2,244,370✔
164
  int32_t contLen = 0;
2,244,370✔
165
  void   *pReq = mndBuildTimerMsg(&contLen);
2,244,370✔
166
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
2,244,370✔
167
  // TODO check return value
168
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,244,370✔
169
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
170
  }
171
}
2,244,370✔
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,196,477✔
185
  mTrace("pullup trim query");
2,196,477✔
186
  int32_t contLen = 0;
2,196,477✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
2,196,477✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,196,477✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,196,477✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
2,196,477✔
193

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

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

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

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

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

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

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

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

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

308
  void *pIter = NULL;
120,924✔
309
  while (1) {
355,922✔
310
    SVgObj *pVgroup = NULL;
476,846✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
476,846✔
312
    if (pIter == NULL) break;
476,846✔
313

314
    bool stateChanged = false;
355,922✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
904,258✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
696,689✔
317
      if (pGid->dnodeId == dnodeId) {
696,689✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
148,353✔
319
          mInfo(
52,485✔
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;
52,485✔
325
          pGid->syncRestore = 0;
52,485✔
326
          pGid->syncCanRead = 0;
52,485✔
327
          pGid->startTimeMs = 0;
52,485✔
328
          stateChanged = true;
52,485✔
329
        }
330
        break;
148,353✔
331
      }
332
    }
333

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

344
    sdbRelease(pSdb, pVgroup);
355,922✔
345
  }
346
}
120,924✔
347

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

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

355
  void *pIter = NULL;
4,470,237✔
356
  while (1) {
8,032,942✔
357
    SDnodeObj *pDnode = NULL;
12,503,179✔
358
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,503,179✔
359
    if (pIter == NULL) break;
12,503,179✔
360

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

367
    sdbRelease(pSdb, pDnode);
8,032,942✔
368
  }
369

370
  mndReleaseRpc(pMnode);
4,470,237✔
371
}
372

373
static bool mnodeIsNotLeader(SMnode *pMnode) {
266,344,949✔
374
  terrno = 0;
266,344,949✔
375
  (void)taosThreadRwlockRdlock(&pMnode->lock);
266,344,323✔
376
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
266,348,202✔
377
  if (terrno != 0) {
266,348,324✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
379
    return true;
×
380
  }
381

382
  if (state.state != TAOS_SYNC_STATE_LEADER) {
266,346,201✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,005,369✔
384
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,005,369✔
385
    return true;
11,005,369✔
386
  }
387
  if (!state.restored || !pMnode->restored) {
255,340,832✔
388
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,010✔
389
    terrno = TSDB_CODE_SYN_RESTORING;
1,073✔
390
    return true;
1,073✔
391
  }
392
  (void)taosThreadRwlockUnlock(&pMnode->lock);
255,339,020✔
393
  return false;
255,341,060✔
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) {
23,072,523✔
413
  int32_t code = 0;
23,072,523✔
414
#ifndef TD_ASTRA
415
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
23,072,523✔
416
    mndPullupGrant(pMnode);
1,093,016✔
417
  }
418
  if (sec % tsTtlPushIntervalSec == 0) {
23,072,523✔
419
    mndPullupTtl(pMnode);
2,244,370✔
420
  }
421

422
  if (sec % tsTrimVDbIntervalSec == 0) {
23,072,523✔
UNCOV
423
    mndPullupTrimDb(pMnode);
×
424
  }
425

426
  if (sec % tsQueryTrimIntervalSec == 0) {
23,072,523✔
427
    mndPullupQueryTrimDb(pMnode);
2,196,477✔
428
  }
429
#endif
430
#ifdef USE_SHARED_STORAGE
431
  if (tsSsEnabled) {
23,072,523✔
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) {
23,072,523✔
442
    if (sec % tsAuthReqHBInterval == 0) {
×
443
      mndPullupAuth(pMnode);
×
444
    }
445
  }
446
#endif
447
  if (sec % tsTransPullupInterval == 0) {
23,072,523✔
448
    mndPullupTrans(pMnode);
11,454,840✔
449
  }
450

451
  if (sec % tsCompactPullupInterval == 0) {
23,072,523✔
452
    mndPullupCompacts(pMnode);
2,146,707✔
453
  }
454

455
  if (sec % tsScanPullupInterval == 0) {
23,072,523✔
456
    mndPullupScans(pMnode);
2,146,707✔
457
  }
458
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
23,072,523✔
459
    mndPullupInstances(pMnode);
4,471,371✔
460
  }
461
#ifdef USE_TOPIC
462
  if (sec % tsMqRebalanceInterval == 0) {
23,072,523✔
463
    mndCalMqRebalance(pMnode);
11,454,758✔
464
  }
465
#endif
466
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
23,072,523✔
467
    mndPullupTelem(pMnode);
120✔
468
  }
469
  if (sec % tsUptimeInterval == 0) {
23,072,523✔
470
    mndIncreaseUpTime(pMnode);
20,333✔
471
  }
472
}
23,072,523✔
473

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

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

491
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
232,269,359✔
492
  if (ms % (tsStatusTimeoutMs) == 0) {
232,269,359✔
493
    mndCheckDnodeOffline(pMnode);
4,470,237✔
494
  }
495
}
232,269,359✔
496

497
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
23,072,523✔
498
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
23,072,523✔
499
    mndSyncCheckTimeout(pMnode);
638,893✔
500
  }
501
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
23,072,523✔
502
    msmHealthCheck(pMnode);
7,579,322✔
503
  }
504
}
23,072,523✔
505

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

511
  while (1) {
242,340,067✔
512
    lastTime++;
242,727,679✔
513
    taosMsleep(100);
242,727,679✔
514

515
    if (mndGetStop(pMnode)) break;
242,727,679✔
516
    if (lastTime % 10 != 0) continue;
242,340,067✔
517

518
    if (mnodeIsNotLeader(pMnode)) {
24,058,680✔
519
      mTrace("timer not process since mnode is not leader");
986,157✔
520
      continue;
986,157✔
521
    }
522

523
    int64_t sec = lastTime / 10;
23,072,523✔
524
    mndDoTimerCheckSync(pMnode, sec);
23,072,523✔
525

526
    mndDoTimerPullupTask(pMnode, sec);
23,072,523✔
527
  }
528

529
  return NULL;
387,612✔
530
}
531

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

537
  while (1) {
538
    lastTime += 100;
242,677,256✔
539
    taosMsleep(100);
242,677,256✔
540

541
    if (mndGetStop(pMnode)) break;
242,677,256✔
542
    if (lastTime % 10 != 0) continue;
242,289,644✔
543

544
    if (mnodeIsNotLeader(pMnode)) {
242,289,644✔
545
      mTrace("timer not process since mnode is not leader");
10,020,285✔
546
      continue;
10,020,285✔
547
    }
548

549
    mndDoTimerCheckStatus(pMnode, lastTime);
232,269,359✔
550

551
    mndDoArbTimerPullupTask(pMnode, lastTime);
232,269,359✔
552
  }
553

554
  return NULL;
387,612✔
555
}
556

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

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

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

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

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

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

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

613
  TAOS_RETURN(code);
387,671✔
614
}
615

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

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

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

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

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

673
  TAOS_RETURN(code);
387,671✔
674
}
675

676
static int32_t mndOpenSdb(SMnode *pMnode) {
387,671✔
677
  int32_t code = 0;
387,671✔
678
  if (!pMnode->deploy) {
387,671✔
679
    code = sdbReadFile(pMnode->pSdb);
108,806✔
680
  }
681

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

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

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

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

704
  TAOS_RETURN(0);
19,383,550✔
705
}
706

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

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

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

770
  for (int32_t s = pos; s >= 0; s--) {
19,768,212✔
771
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,380,600✔
772
    mInfo("%s will cleanup", pStep->name);
19,380,600✔
773
    if (pStep->cleanupFp != NULL) {
19,380,600✔
774
      (*pStep->cleanupFp)(pMnode);
18,605,376✔
775
    }
776
  }
777

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

783
static int32_t mndExecSteps(SMnode *pMnode) {
387,671✔
784
  int32_t code = 0;
387,671✔
785
  int32_t size = taosArrayGetSize(pMnode->pSteps);
387,671✔
786
  for (int32_t pos = 0; pos < size; pos++) {
19,771,221✔
787
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,383,550✔
788
    if (pStep->initFp == NULL) continue;
19,383,550✔
789

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

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

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

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

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

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

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

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

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

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

931
  return mndInitTimer(pMnode);
387,612✔
932
}
933

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

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

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

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

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

953
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
19,140,365✔
954

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

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

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

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

974
  return code;
48,926,411✔
975
}
976

977
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
230,141,812✔
978
  int32_t code = 0;
230,141,812✔
979
  if (!IsReq(pMsg)) TAOS_RETURN(code);
230,141,812✔
980
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
201,210,549✔
981
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
196,565,287✔
982
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
192,635,847✔
983
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
183,404,831✔
984
    TAOS_RETURN(code);
17,807,472✔
985
  }
986

987
  SMnode *pMnode = pMsg->info.node;
183,404,225✔
988
  (void)taosThreadRwlockRdlock(&pMnode->lock);
183,400,782✔
989
  if (pMnode->stopped) {
183,406,435✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,857✔
991
    code = TSDB_CODE_APP_IS_STOPPING;
1,857✔
992
    TAOS_RETURN(code);
1,857✔
993
  }
994

995
  terrno = 0;
183,398,090✔
996
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
183,402,650✔
997
  if (terrno != 0) {
183,406,169✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
999
    code = terrno;
×
1000
    TAOS_RETURN(code);
×
1001
  }
1002

1003
  if (state.state != TAOS_SYNC_STATE_LEADER) {
183,405,344✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,647,467✔
1005
    code = TSDB_CODE_SYN_NOT_LEADER;
1,647,467✔
1006
    goto _OVER;
1,647,467✔
1007
  }
1008

1009
  if (!state.restored || !pMnode->restored) {
181,757,877✔
1010
    (void)taosThreadRwlockUnlock(&pMnode->lock);
836,129✔
1011
    code = TSDB_CODE_SYN_RESTORING;
835,280✔
1012
    goto _OVER;
835,280✔
1013
  }
1014

1015
#if 1
1016
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
180,921,904✔
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);
180,922,454✔
1023
  TAOS_RETURN(code);
180,922,391✔
1024

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

1040
  const STraceId *trace = &pMsg->info.traceId;
2,482,472✔
1041
  SEpSet          epSet = {0};
2,482,607✔
1042
  mndGetMnodeEpSet(pMnode, &epSet);
2,482,607✔
1043

1044
  mGDebug(
2,482,747✔
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;
2,482,747✔
1051

1052
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
8,086,556✔
1053
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
5,603,809✔
1054
  }
1055

1056
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,482,747✔
1057
  pMsg->info.rsp = rpcMallocCont(contLen);
2,481,395✔
1058
  if (pMsg->info.rsp != NULL) {
2,482,672✔
1059
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,482,591✔
1060
      mError("failed to serialize ep set");
×
1061
    }
1062
    pMsg->info.hasEpSet = 1;
2,482,382✔
1063
    pMsg->info.rspLen = contLen;
2,481,807✔
1064
  }
1065

1066
  TAOS_RETURN(code);
2,482,016✔
1067
}
1068

1069
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
230,151,717✔
1070
  SMnode         *pMnode = pMsg->info.node;
230,151,717✔
1071
  const STraceId *trace = &pMsg->info.traceId;
230,151,441✔
1072
  int32_t         code = TSDB_CODE_SUCCESS;
230,152,826✔
1073

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

1096
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
230,135,907✔
1097
  MndMsgFpExt fpExt = NULL;
230,149,042✔
1098
  if (fp == NULL) {
230,149,042✔
1099
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,814,598✔
1100
    if (fpExt == NULL) {
17,814,598✔
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));
230,149,042✔
1108

1109
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
227,668,193✔
1110
  if (fp)
227,667,402✔
1111
    code = (*fp)(pMsg);
209,852,804✔
1112
  else
1113
    code = (*fpExt)(pMsg, pQueueInfo);
17,814,598✔
1114
  mndReleaseRpc(pMnode);
227,667,731✔
1115

1116
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
227,668,681✔
1117
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,969,584✔
1118
  } else if (code == 0) {
199,699,097✔
1119
    mGTrace("msg:%p, successfully processed", pMsg);
197,320,828✔
1120
  } else {
1121
    // TODO removve this wrong set code
1122
    if (code == -1) {
2,378,269✔
1123
      code = terrno;
×
1124
    }
1125
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,378,269✔
1126
            TMSG_INFO(pMsg->msgType));
1127
  }
1128

1129
  TAOS_RETURN(code);
227,668,681✔
1130
}
1131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1386
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
485,392,971✔
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