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

taosdata / TDengine / #4914

06 Jan 2026 01:30AM UTC coverage: 64.876% (-0.008%) from 64.884%
#4914

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

3475 existing lines in 124 files now uncovered.

194993 of 300563 relevant lines covered (64.88%)

116239151.85 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
236,466,113✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
236,466,113✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
236,469,015✔
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);
236,468,815✔
93
}
236,469,324✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
57,722,515✔
96
  terrno = 0;
57,722,515✔
97
  SMTimerReq timerReq = {0};
57,723,341✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
57,722,811✔
100
  if (contLen <= 0) return NULL;
57,721,827✔
101
  void *pReq = rpcMallocCont(contLen);
57,721,827✔
102
  if (pReq == NULL) return NULL;
57,720,004✔
103

104
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
57,720,004✔
105
    mError("failed to serialize timer msg since %s", terrstr());
×
106
  }
107
  *pContLen = contLen;
57,720,549✔
108
  return pReq;
57,719,604✔
109
}
110

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

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

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

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

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

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

184
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,263,814✔
185
  mTrace("pullup trim query");
2,263,814✔
186
  int32_t contLen = 0;
2,263,814✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
2,263,814✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,263,814✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,263,814✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
2,263,814✔
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,755,803✔
219
  mTrace("pullup arb hb");
11,755,803✔
220
  int32_t contLen = 0;
11,755,803✔
221
  void   *pReq = mndBuildTimerMsg(&contLen);
11,755,803✔
222
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,755,803✔
223
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,755,803✔
224
}
225

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

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

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

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

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

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

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

308
  void *pIter = NULL;
103,490✔
309
  while (1) {
360,381✔
310
    SVgObj *pVgroup = NULL;
463,871✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
463,871✔
312
    if (pIter == NULL) break;
463,871✔
313

314
    bool stateChanged = false;
360,381✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
924,436✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
705,595✔
317
      if (pGid->dnodeId == dnodeId) {
705,595✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
141,540✔
319
          mInfo(
53,956✔
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;
53,956✔
325
          pGid->syncRestore = 0;
53,956✔
326
          pGid->syncCanRead = 0;
53,956✔
327
          pGid->startTimeMs = 0;
53,956✔
328
          stateChanged = true;
53,956✔
329
        }
330
        break;
141,540✔
331
      }
332
    }
333

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

344
    sdbRelease(pSdb, pVgroup);
360,381✔
345
  }
346
}
103,490✔
347

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

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

355
  void *pIter = NULL;
4,588,677✔
356
  while (1) {
8,120,655✔
357
    SDnodeObj *pDnode = NULL;
12,709,332✔
358
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,709,332✔
359
    if (pIter == NULL) break;
12,709,332✔
360

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

367
    sdbRelease(pSdb, pDnode);
8,120,655✔
368
  }
369

370
  mndReleaseRpc(pMnode);
4,588,677✔
371
}
372

373
static bool mnodeIsNotLeader(SMnode *pMnode) {
274,246,339✔
374
  terrno = 0;
274,246,339✔
375
  (void)taosThreadRwlockRdlock(&pMnode->lock);
274,245,938✔
376
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
274,248,605✔
377
  if (terrno != 0) {
274,249,558✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
379
    return true;
×
380
  }
381

382
  if (state.state != TAOS_SYNC_STATE_LEADER) {
274,248,792✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,966,723✔
384
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,966,723✔
385
    return true;
11,966,723✔
386
  }
387
  if (!state.restored || !pMnode->restored) {
262,282,069✔
388
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,747✔
389
    terrno = TSDB_CODE_SYN_RESTORING;
802✔
390
    return true;
802✔
391
  }
392
  (void)taosThreadRwlockUnlock(&pMnode->lock);
262,280,111✔
393
  return false;
262,281,447✔
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,700,258✔
413
  int32_t code = 0;
23,700,258✔
414
#ifndef TD_ASTRA
415
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
23,700,258✔
416
    mndPullupGrant(pMnode);
1,104,047✔
417
  }
418
  if (sec % tsTtlPushIntervalSec == 0) {
23,700,258✔
419
    mndPullupTtl(pMnode);
2,296,058✔
420
  }
421

422
  if (sec % tsTrimVDbIntervalSec == 0) {
23,700,258✔
423
    mndPullupTrimDb(pMnode);
79✔
424
  }
425

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

451
  if (sec % tsCompactPullupInterval == 0) {
23,700,258✔
452
    mndPullupCompacts(pMnode);
2,198,755✔
453
  }
454

455
  if (sec % tsScanPullupInterval == 0) {
23,700,258✔
456
    mndPullupScans(pMnode);
2,198,755✔
457
  }
458
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
23,700,258✔
459
    mndPullupInstances(pMnode);
4,588,862✔
460
  }
461
#ifdef USE_TOPIC
462
  if (sec % tsMqRebalanceInterval == 0) {
23,700,258✔
463
    mndCalMqRebalance(pMnode);
11,757,806✔
464
  }
465
#endif
466
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
23,700,258✔
467
    mndPullupTelem(pMnode);
164✔
468
  }
469
  if (sec % tsUptimeInterval == 0) {
23,700,258✔
470
    mndIncreaseUpTime(pMnode);
18,773✔
471
  }
472
}
23,700,258✔
473

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

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

491
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
238,581,775✔
492
  if (ms % (tsStatusTimeoutMs) == 0) {
238,581,775✔
493
    mndCheckDnodeOffline(pMnode);
4,588,677✔
494
  }
495
}
238,581,775✔
496

497
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
23,700,258✔
498
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
23,700,258✔
499
    mndSyncCheckTimeout(pMnode);
644,904✔
500
  }
501
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
23,700,258✔
502
    msmHealthCheck(pMnode);
7,788,294✔
503
  }
504
}
23,700,258✔
505

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

511
  while (1) {
249,515,559✔
512
    lastTime++;
249,917,118✔
513
    taosMsleep(100);
249,917,118✔
514

515
    if (mndGetStop(pMnode)) break;
249,917,118✔
516
    if (lastTime % 10 != 0) continue;
249,515,559✔
517

518
    if (mnodeIsNotLeader(pMnode)) {
24,772,745✔
519
      mTrace("timer not process since mnode is not leader");
1,072,487✔
520
      continue;
1,072,487✔
521
    }
522

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

526
    mndDoTimerPullupTask(pMnode, sec);
23,700,258✔
527
  }
528

529
  return NULL;
401,559✔
530
}
531

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

537
  while (1) {
538
    lastTime += 100;
249,878,372✔
539
    taosMsleep(100);
249,878,372✔
540

541
    if (mndGetStop(pMnode)) break;
249,878,372✔
542
    if (lastTime % 10 != 0) continue;
249,476,813✔
543

544
    if (mnodeIsNotLeader(pMnode)) {
249,476,813✔
545
      mTrace("timer not process since mnode is not leader");
10,895,038✔
546
      continue;
10,895,038✔
547
    }
548

549
    mndDoTimerCheckStatus(pMnode, lastTime);
238,581,775✔
550

551
    mndDoArbTimerPullupTask(pMnode, lastTime);
238,581,775✔
552
  }
553

554
  return NULL;
401,559✔
555
}
556

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

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

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

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

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

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

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

613
  TAOS_RETURN(code);
401,620✔
614
}
615

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

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

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

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

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

673
  TAOS_RETURN(code);
401,620✔
674
}
675

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

931
  return mndInitTimer(pMnode);
401,559✔
932
}
933

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

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

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

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

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

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

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

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

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

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

974
  return code;
50,475,510✔
975
}
976

977
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
235,658,059✔
978
  int32_t code = 0;
235,658,059✔
979
  if (!IsReq(pMsg)) TAOS_RETURN(code);
235,658,059✔
980
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
206,559,314✔
981
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
201,978,253✔
982
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
198,142,012✔
983
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
189,046,898✔
984
    TAOS_RETURN(code);
17,501,845✔
985
  }
986

987
  SMnode *pMnode = pMsg->info.node;
189,045,889✔
988
  (void)taosThreadRwlockRdlock(&pMnode->lock);
189,044,838✔
989
  if (pMnode->stopped) {
189,055,354✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,509✔
991
    code = TSDB_CODE_APP_IS_STOPPING;
1,509✔
992
    TAOS_RETURN(code);
1,509✔
993
  }
994

995
  terrno = 0;
189,038,564✔
996
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
189,047,861✔
997
  if (terrno != 0) {
189,053,893✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
999
    code = terrno;
×
1000
    TAOS_RETURN(code);
×
1001
  }
1002

1003
  if (state.state != TAOS_SYNC_STATE_LEADER) {
189,052,464✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,861,563✔
1005
    code = TSDB_CODE_SYN_NOT_LEADER;
1,861,563✔
1006
    goto _OVER;
1,861,563✔
1007
  }
1008

1009
  if (!state.restored || !pMnode->restored) {
187,190,901✔
1010
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,933,615✔
1011
    code = TSDB_CODE_SYN_RESTORING;
1,931,315✔
1012
    goto _OVER;
1,931,315✔
1013
  }
1014

1015
#if 1
1016
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
185,257,902✔
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);
185,259,726✔
1023
  TAOS_RETURN(code);
185,260,368✔
1024

1025
_OVER:
3,792,878✔
1026
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,792,878✔
1027
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,792,575✔
1028
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,792,373✔
1029
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,791,642✔
1030
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,790,180✔
1031
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,790,820✔
1032
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,791,551✔
1033
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,789,356✔
1034
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,787,803✔
1035
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
192✔
1036
           pMnode->stopped, state.restored, syncStr(state.state));
1037
    TAOS_RETURN(code);
192✔
1038
  }
1039

1040
  const STraceId *trace = &pMsg->info.traceId;
3,788,534✔
1041
  SEpSet          epSet = {0};
3,787,805✔
1042
  mndGetMnodeEpSet(pMnode, &epSet);
3,788,536✔
1043

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

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

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

1056
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,792,373✔
1057
  pMsg->info.rsp = rpcMallocCont(contLen);
3,788,193✔
1058
  if (pMsg->info.rsp != NULL) {
3,790,726✔
1059
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,789,669✔
1060
      mError("failed to serialize ep set");
×
1061
    }
1062
    pMsg->info.hasEpSet = 1;
3,788,661✔
1063
    pMsg->info.rspLen = contLen;
3,787,930✔
1064
  }
1065

1066
  TAOS_RETURN(code);
3,791,269✔
1067
}
1068

1069
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
235,673,076✔
1070
  SMnode         *pMnode = pMsg->info.node;
235,673,076✔
1071
  const STraceId *trace = &pMsg->info.traceId;
235,673,409✔
1072
  int32_t         code = TSDB_CODE_SUCCESS;
235,673,046✔
1073

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

1096
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
235,663,284✔
1097
  MndMsgFpExt fpExt = NULL;
235,673,770✔
1098
  if (fp == NULL) {
235,673,770✔
1099
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,523,299✔
1100
    if (fpExt == NULL) {
17,523,299✔
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));
235,673,770✔
1108

1109
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
231,877,967✔
1110
  if (fp)
231,879,234✔
1111
    code = (*fp)(pMsg);
214,355,774✔
1112
  else
1113
    code = (*fpExt)(pMsg, pQueueInfo);
17,523,460✔
1114
  mndReleaseRpc(pMnode);
231,880,218✔
1115

1116
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
231,880,778✔
1117
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
27,855,577✔
1118
  } else if (code == 0) {
204,025,201✔
1119
    mGTrace("msg:%p, successfully processed", pMsg);
201,586,722✔
1120
  } else {
1121
    // TODO removve this wrong set code
1122
    if (code == -1) {
2,438,479✔
1123
      code = terrno;
×
1124
    }
1125
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,438,479✔
1126
            TMSG_INFO(pMsg->msgType));
1127
  }
1128

1129
  TAOS_RETURN(code);
231,880,778✔
1130
}
1131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1300
    SMonStbDesc desc = {0};
×
1301

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

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

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

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

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

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

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

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

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

1386
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
499,783,298✔
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