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

taosdata / TDengine / #4926

13 Jan 2026 05:43AM UTC coverage: 66.053% (-0.05%) from 66.107%
#4926

push

travis-ci

web-flow
feat: [6654385780] show snap progress (#34203)

48 of 59 new or added lines in 7 files covered. (81.36%)

582 existing lines in 124 files now uncovered.

200362 of 303334 relevant lines covered (66.05%)

132283104.31 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
247,227,264✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
247,227,264✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
247,229,526✔
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);
247,228,776✔
93
}
247,230,981✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
62,247,264✔
96
  terrno = 0;
62,247,264✔
97
  SMTimerReq timerReq = {0};
62,247,264✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
62,246,744✔
100
  if (contLen <= 0) return NULL;
62,245,830✔
101
  void *pReq = rpcMallocCont(contLen);
62,245,830✔
102
  if (pReq == NULL) return NULL;
62,244,184✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
62,244,184✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
62,245,667✔
108
  return pReq;
62,245,590✔
109
}
110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

258
static void mndPullupGrant(SMnode *pMnode) {
1,133,504✔
259
  mTrace("pullup grant msg");
1,133,504✔
260
  int32_t contLen = 0;
1,133,504✔
261
  void   *pReq = mndBuildTimerMsg(&contLen);
1,133,504✔
262
  if (pReq != NULL) {
1,133,504✔
263
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,133,504✔
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,133,504✔
270
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
271
    }
272
  }
273
}
1,133,504✔
274

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

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

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

308
  void *pIter = NULL;
114,469✔
309
  while (1) {
369,769✔
310
    SVgObj *pVgroup = NULL;
484,238✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
484,238✔
312
    if (pIter == NULL) break;
484,238✔
313

314
    bool stateChanged = false;
369,769✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
974,141✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
756,797✔
317
      if (pGid->dnodeId == dnodeId) {
756,797✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
152,425✔
319
          mInfo(
46,514✔
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;
46,514✔
325
          pGid->syncRestore = 0;
46,514✔
326
          pGid->syncCanRead = 0;
46,514✔
327
          pGid->startTimeMs = 0;
46,514✔
328
          pGid->learnerProgress = 0;
46,514✔
329
          pGid->snapSeq = -1;
46,514✔
330
          stateChanged = true;
46,514✔
331
        }
332
        break;
152,425✔
333
      }
334
    }
335

336
    if (stateChanged) {
369,769✔
337
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
46,514✔
338
      if (pDb != NULL && pDb->stateTs != curMs) {
46,514✔
339
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
28,939✔
340
              curMs);
341
        pDb->stateTs = curMs;
28,939✔
342
      }
343
      mndReleaseDb(pMnode, pDb);
46,514✔
344
    }
345

346
    sdbRelease(pSdb, pVgroup);
369,769✔
347
  }
348
}
114,469✔
349

350
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,959,592✔
351
  mTrace("check dnode offline");
4,959,592✔
352
  if (mndAcquireRpc(pMnode) != 0) return;
4,959,592✔
353

354
  SSdb   *pSdb = pMnode->pSdb;
4,959,592✔
355
  int64_t curMs = taosGetTimestampMs();
4,959,592✔
356

357
  void *pIter = NULL;
4,959,592✔
358
  while (1) {
8,234,831✔
359
    SDnodeObj *pDnode = NULL;
13,194,423✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,194,423✔
361
    if (pIter == NULL) break;
13,194,423✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,234,831✔
364
    if (!online) {
8,234,831✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
114,469✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
114,469✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
8,234,831✔
370
  }
371

372
  mndReleaseRpc(pMnode);
4,959,592✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
292,733,787✔
376
  terrno = 0;
292,733,787✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
292,735,144✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
292,737,321✔
379
  if (terrno != 0) {
292,738,002✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
292,737,872✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,380,131✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,380,048✔
387
    return true;
10,379,960✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
282,357,741✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,225✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
1,892✔
392
    return true;
1,892✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
282,353,516✔
395
  return false;
282,354,056✔
396
}
397

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

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

412
  return min <= 1 ? 2 : min;
×
413
}
414
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
25,525,639✔
415
  int32_t code = 0;
25,525,639✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
25,525,639✔
418
    mndPullupGrant(pMnode);
1,133,504✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
25,525,639✔
421
    mndPullupTtl(pMnode);
2,492,129✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
25,525,639✔
425
    mndPullupTrimDb(pMnode);
×
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
25,525,639✔
429
    mndPullupQueryTrimDb(pMnode);
2,441,199✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
25,525,639✔
434
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
435
      mndPullupUpdateSsMigrateProgress(pMnode);
×
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
25,525,639✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
25,525,639✔
450
    mndPullupTrans(pMnode);
12,673,691✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
25,525,639✔
454
    mndPullupCompacts(pMnode);
2,388,289✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
25,525,639✔
458
    mndPullupScans(pMnode);
2,388,289✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
25,525,639✔
461
    mndPullupInstances(pMnode);
4,959,429✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
25,525,639✔
465
    mndCalMqRebalance(pMnode);
12,673,529✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
25,525,639✔
469
    mndPullupTelem(pMnode);
130✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
25,525,639✔
472
    mndIncreaseUpTime(pMnode);
24,721✔
473
  }
474
}
25,525,639✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
256,830,210✔
477
  int32_t code = 0;
256,830,210✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
256,830,210✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
12,675,588✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
256,830,210✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
8,397,257✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
256,830,210✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
256,830,210✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
256,830,210✔
495
    mndCheckDnodeOffline(pMnode);
4,959,592✔
496
  }
497
}
256,830,210✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
25,525,639✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
25,525,639✔
501
    mndSyncCheckTimeout(pMnode);
711,457✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
25,525,639✔
504
    msmHealthCheck(pMnode);
8,396,035✔
505
  }
506
}
25,525,639✔
507

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

513
  while (1) {
266,324,109✔
514
    lastTime++;
266,721,933✔
515
    taosMsleep(100);
266,721,933✔
516

517
    if (mndGetStop(pMnode)) break;
266,721,933✔
518
    if (lastTime % 10 != 0) continue;
266,324,109✔
519

520
    if (mnodeIsNotLeader(pMnode)) {
26,453,091✔
521
      mTrace("timer not process since mnode is not leader");
927,452✔
522
      continue;
927,452✔
523
    }
524

525
    int64_t sec = lastTime / 10;
25,525,639✔
526
    mndDoTimerCheckSync(pMnode, sec);
25,525,639✔
527

528
    mndDoTimerPullupTask(pMnode, sec);
25,525,639✔
529
  }
530

531
  return NULL;
397,824✔
532
}
533

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

539
  while (1) {
540
    lastTime += 100;
266,682,735✔
541
    taosMsleep(100);
266,682,735✔
542

543
    if (mndGetStop(pMnode)) break;
266,682,735✔
544
    if (lastTime % 10 != 0) continue;
266,284,911✔
545

546
    if (mnodeIsNotLeader(pMnode)) {
266,284,911✔
547
      mTrace("timer not process since mnode is not leader");
9,454,701✔
548
      continue;
9,454,701✔
549
    }
550

551
    mndDoTimerCheckStatus(pMnode, lastTime);
256,830,210✔
552

553
    mndDoArbTimerPullupTask(pMnode, lastTime);
256,830,210✔
554
  }
555

556
  return NULL;
397,824✔
557
}
558

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

572
  (void)taosThreadAttrDestroy(&thAttr);
397,824✔
573
  tmsgReportStartup("mnode-timer", "initialized");
397,824✔
574

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

586
  (void)taosThreadAttrDestroy(&arbAttr);
397,824✔
587
  tmsgReportStartup("mnode-timer", "initialized");
397,824✔
588
  TAOS_RETURN(code);
397,824✔
589
}
590

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

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

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

615
  TAOS_RETURN(code);
397,883✔
616
}
617

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

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

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

651
  TAOS_RETURN(code);
397,883✔
652
}
653

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

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

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

675
  TAOS_RETURN(code);
397,883✔
676
}
677

678
static int32_t mndOpenSdb(SMnode *pMnode) {
397,883✔
679
  int32_t code = 0;
397,883✔
680
  if (!pMnode->deploy) {
397,883✔
681
    code = sdbReadFile(pMnode->pSdb);
112,994✔
682
  }
683

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

686
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
397,883✔
687
  return code;
397,883✔
688
}
689

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

697
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,894,150✔
698
  SMnodeStep step = {0};
19,894,150✔
699
  step.name = name;
19,894,150✔
700
  step.initFp = initFp;
19,894,150✔
701
  step.cleanupFp = cleanupFp;
19,894,150✔
702
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
39,788,300✔
703
    TAOS_RETURN(terrno);
×
704
  }
705

706
  TAOS_RETURN(0);
19,894,150✔
707
}
708

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

765
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
397,824✔
766
  if (pMnode->pSteps == NULL) return;
397,824✔
767

768
  if (pos == -1) {
397,824✔
769
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
397,824✔
770
  }
771

772
  for (int32_t s = pos; s >= 0; s--) {
20,289,024✔
773
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,891,200✔
774
    mInfo("%s will cleanup", pStep->name);
19,891,200✔
775
    if (pStep->cleanupFp != NULL) {
19,891,200✔
776
      (*pStep->cleanupFp)(pMnode);
19,095,552✔
777
    }
778
  }
779

780
  taosArrayClear(pMnode->pSteps);
397,824✔
781
  taosArrayDestroy(pMnode->pSteps);
397,824✔
782
  pMnode->pSteps = NULL;
397,824✔
783
}
784

785
static int32_t mndExecSteps(SMnode *pMnode) {
397,883✔
786
  int32_t code = 0;
397,883✔
787
  int32_t size = taosArrayGetSize(pMnode->pSteps);
397,883✔
788
  for (int32_t pos = 0; pos < size; pos++) {
20,292,033✔
789
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,894,150✔
790
    if (pStep->initFp == NULL) continue;
19,894,150✔
791

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

802
  pMnode->clusterId = mndGetClusterId(pMnode);
397,883✔
803
  TAOS_RETURN(0);
397,883✔
804
}
805

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

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

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

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

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

847
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
397,883✔
848
        pOption->numOfTotalReplicas);
849
  mndSetOptions(pMnode, pOption);
397,883✔
850

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

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

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

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

885
  mInfo("mnode open successfully");
397,883✔
886
  return pMnode;
397,883✔
887
}
888

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

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

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

933
  return mndInitTimer(pMnode);
397,824✔
934
}
935

936
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
397,824✔
937

938
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
313,497✔
939

940
int32_t mndIsCatchUp(SMnode *pMnode) {
160,822✔
941
  int64_t rid = pMnode->syncMgmt.sync;
160,822✔
942
  return syncIsCatchUp(rid);
160,822✔
943
}
944

945
ESyncRole mndGetRole(SMnode *pMnode) {
160,822✔
946
  int64_t rid = pMnode->syncMgmt.sync;
160,822✔
947
  return syncGetRole(rid);
160,822✔
948
}
949

950
int64_t mndGetTerm(SMnode *pMnode) {
8,501,325✔
951
  int64_t rid = pMnode->syncMgmt.sync;
8,501,325✔
952
  return syncGetTerm(rid);
8,501,325✔
953
}
954

955
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,176,778✔
956

957
void mndStop(SMnode *pMnode) {
397,824✔
958
  mndSetStop(pMnode);
397,824✔
959
  mndSyncStop(pMnode);
397,824✔
960
  mndCleanupTimer(pMnode);
397,824✔
961
}
397,824✔
962

963
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
50,212,885✔
964
  SMnode    *pMnode = pMsg->info.node;
50,212,885✔
965
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
50,212,885✔
966

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

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

976
  return code;
50,212,885✔
977
}
978

979
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
244,538,649✔
980
  int32_t code = 0;
244,538,649✔
981
  if (!IsReq(pMsg)) TAOS_RETURN(code);
244,538,649✔
982
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
214,401,484✔
983
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
209,798,021✔
984
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
206,017,389✔
985
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
196,972,575✔
986
    TAOS_RETURN(code);
17,430,276✔
987
  }
988

989
  SMnode *pMnode = pMsg->info.node;
196,975,172✔
990
  (void)taosThreadRwlockRdlock(&pMnode->lock);
196,971,106✔
991
  if (pMnode->stopped) {
196,975,377✔
992
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,096✔
993
    code = TSDB_CODE_APP_IS_STOPPING;
2,096✔
994
    TAOS_RETURN(code);
2,096✔
995
  }
996

997
  terrno = 0;
196,969,700✔
998
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
196,970,251✔
999
  if (terrno != 0) {
196,975,648✔
1000
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1001
    code = terrno;
×
1002
    TAOS_RETURN(code);
×
1003
  }
1004

1005
  if (state.state != TAOS_SYNC_STATE_LEADER) {
196,974,205✔
1006
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,683,410✔
1007
    code = TSDB_CODE_SYN_NOT_LEADER;
1,683,455✔
1008
    goto _OVER;
1,683,455✔
1009
  }
1010

1011
  if (!state.restored || !pMnode->restored) {
195,290,795✔
1012
    (void)taosThreadRwlockUnlock(&pMnode->lock);
592,790✔
1013
    code = TSDB_CODE_SYN_RESTORING;
590,920✔
1014
    goto _OVER;
590,920✔
1015
  }
1016

1017
#if 1
1018
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
194,698,826✔
1019
#else
1020
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1021
  mTrace("mnode rpc is acquired, ref:%d", ref);
1022
#endif
1023

1024
  (void)taosThreadRwlockUnlock(&pMnode->lock);
194,701,310✔
1025
  TAOS_RETURN(code);
194,700,372✔
1026

1027
_OVER:
2,274,375✔
1028
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,274,375✔
1029
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,274,375✔
1030
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,274,457✔
1031
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,274,457✔
1032
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,274,390✔
1033
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,274,390✔
1034
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,273,720✔
1035
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,274,457✔
1036
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,274,374✔
UNCOV
1037
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1038
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1039
    TAOS_RETURN(code);
×
1040
  }
1041

1042
  const STraceId *trace = &pMsg->info.traceId;
2,273,553✔
1043
  SEpSet          epSet = {0};
2,273,593✔
1044
  mndGetMnodeEpSet(pMnode, &epSet);
2,273,593✔
1045

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

1052
  if (epSet.numOfEps <= 0) return -1;
2,274,457✔
1053

1054
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
7,763,818✔
1055
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
5,489,361✔
1056
  }
1057

1058
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,274,457✔
1059
  pMsg->info.rsp = rpcMallocCont(contLen);
2,272,416✔
1060
  if (pMsg->info.rsp != NULL) {
2,272,852✔
1061
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,274,387✔
1062
      mError("failed to serialize ep set");
×
1063
    }
1064
    pMsg->info.hasEpSet = 1;
2,273,519✔
1065
    pMsg->info.rspLen = contLen;
2,274,056✔
1066
  }
1067

1068
  TAOS_RETURN(code);
2,271,264✔
1069
}
1070

1071
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
244,544,393✔
1072
  SMnode         *pMnode = pMsg->info.node;
244,544,393✔
1073
  const STraceId *trace = &pMsg->info.traceId;
244,545,923✔
1074
  int32_t         code = TSDB_CODE_SUCCESS;
244,545,968✔
1075

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

1098
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
244,533,302✔
1099
  MndMsgFpExt fpExt = NULL;
244,545,945✔
1100
  if (fp == NULL) {
244,545,945✔
1101
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,431,968✔
1102
    if (fpExt == NULL) {
17,432,458✔
1103
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1104
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1105
      TAOS_RETURN(code);
×
1106
    }
1107
  }
1108

1109
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
244,546,435✔
1110

1111
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
242,267,294✔
1112
  if (fp)
242,268,063✔
1113
    code = (*fp)(pMsg);
224,836,094✔
1114
  else
1115
    code = (*fpExt)(pMsg, pQueueInfo);
17,431,969✔
1116
  mndReleaseRpc(pMnode);
242,268,247✔
1117

1118
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
242,271,322✔
1119
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,770,077✔
1120
  } else if (code == 0) {
214,501,245✔
1121
    mGTrace("msg:%p, successfully processed", pMsg);
212,096,600✔
1122
  } else {
1123
    // TODO removve this wrong set code
1124
    if (code == -1) {
2,404,645✔
1125
      code = terrno;
×
1126
    }
1127
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,404,645✔
1128
            TMSG_INFO(pMsg->msgType));
1129
  }
1130

1131
  TAOS_RETURN(code);
242,271,322✔
1132
}
1133

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

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

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

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

1170
  SSdb   *pSdb = pMnode->pSdb;
67✔
1171
  int64_t ms = taosGetTimestampMs();
67✔
1172

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

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

1194
  void *pIter = NULL;
67✔
1195
  while (1) {
67✔
1196
    SDnodeObj *pObj = NULL;
134✔
1197
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
134✔
1198
    if (pIter == NULL) break;
134✔
1199

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

1214
  pIter = NULL;
67✔
1215
  while (1) {
67✔
1216
    SMnodeObj *pObj = NULL;
134✔
1217
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
134✔
1218
    if (pIter == NULL) break;
134✔
1219

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

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

1242
  // vgroup info
1243
  pIter = NULL;
67✔
1244
  while (1) {
402✔
1245
    SVgObj *pVgroup = NULL;
469✔
1246
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
469✔
1247
    if (pIter == NULL) break;
469✔
1248

1249
    if (pVgroup->mountVgId) {
402✔
1250
      sdbRelease(pSdb, pVgroup);
×
1251
      continue;
×
1252
    }
1253

1254
    pClusterInfo->vgroups_total++;
402✔
1255
    pClusterInfo->tbs_total += pVgroup->numOfTables;
402✔
1256

1257
    SMonVgroupDesc desc = {0};
402✔
1258
    desc.vgroup_id = pVgroup->vgId;
402✔
1259

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

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

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

1295
  // stb info
1296
  pIter = NULL;
67✔
1297
  while (1) {
67✔
1298
    SStbObj *pStb = NULL;
134✔
1299
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
134✔
1300
    if (pIter == NULL) break;
134✔
1301

1302
    SMonStbDesc desc = {0};
67✔
1303

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

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

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

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

1336
  mndReleaseRpc(pMnode);
67✔
1337
  TAOS_RETURN(code);
67✔
1338
}
1339

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

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

1356
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,397,257✔
1357
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,397,257✔
1358
  return state.roleTimeMs;
8,397,257✔
1359
}
1360

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

1379
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1380

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

1388
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
533,394,720✔
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