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

taosdata / TDengine / #5015

03 Apr 2026 03:59PM UTC coverage: 72.289% (+0.03%) from 72.256%
#5015

push

travis-ci

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

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13044 existing lines in 149 files now uncovered.

257390 of 356056 relevant lines covered (72.29%)

130247228.09 hits per line

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

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

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
290,273,087✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
290,273,087✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
290,273,958✔
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);
290,271,934✔
93
}
290,274,522✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
76,360,236✔
96
  terrno = 0;
76,360,236✔
97
  SMTimerReq timerReq = {0};
76,360,236✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
76,360,236✔
100
  if (contLen <= 0) return NULL;
76,359,494✔
101
  void *pReq = rpcMallocCont(contLen);
76,359,494✔
102
  if (pReq == NULL) return NULL;
76,359,494✔
103

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

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

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

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

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

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

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

184
static void mndPullupQueryTrimDb(SMnode *pMnode) {
3,277,151✔
185
  mTrace("pullup trim query");
3,277,151✔
186
  int32_t contLen = 0;
3,277,151✔
187
  void   *pReq = mndBuildTimerMsg(&contLen);
3,277,151✔
188
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
3,277,151✔
189
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,277,151✔
190
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
  }
192
}
3,277,151✔
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) {
164,590✔
209
  mTrace("pullup update ssmigrate progress");
164,590✔
210
  int32_t contLen = 0;
164,590✔
211
  void   *pReq = mndBuildTimerMsg(&contLen);
164,590✔
212
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
164,590✔
213
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
164,590✔
214
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
215
  }
216
}
164,590✔
217

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

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

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

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

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

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

308
  void *pIter = NULL;
136,594✔
309
  while (1) {
398,356✔
310
    SVgObj *pVgroup = NULL;
534,950✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
534,950✔
312
    if (pIter == NULL) break;
534,950✔
313

314
    bool stateChanged = false;
398,356✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,022,636✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
801,835✔
317
      if (pGid->dnodeId == dnodeId) {
801,835✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
177,555✔
319
          mInfo(
52,371✔
320
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
321
              "restored:0 "
322
              "canRead:0",
323
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
324
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
52,371✔
325
          pGid->syncRestore = 0;
52,371✔
326
          pGid->syncCanRead = 0;
52,371✔
327
          pGid->startTimeMs = 0;
52,371✔
328
          pGid->learnerProgress = 0;
52,371✔
329
          pGid->snapSeq = -1;
52,371✔
330
          stateChanged = true;
52,371✔
331
        }
332
        break;
177,555✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
398,356✔
347
  }
348
}
136,594✔
349

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

354
  SSdb   *pSdb = pMnode->pSdb;
5,821,022✔
355
  int64_t curMs = taosGetTimestampMs();
5,821,022✔
356

357
  void *pIter = NULL;
5,821,022✔
358
  while (1) {
9,613,894✔
359
    SDnodeObj *pDnode = NULL;
15,434,916✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
15,434,916✔
361
    if (pIter == NULL) break;
15,434,916✔
362

363
    bool online = mndIsDnodeOnline(pDnode, curMs);
9,613,894✔
364
    if (!online) {
9,613,894✔
365
      mInfo("dnode:%d, in offline state", pDnode->id);
136,594✔
366
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
136,594✔
367
    }
368

369
    sdbRelease(pSdb, pDnode);
9,613,894✔
370
  }
371

372
  mndReleaseRpc(pMnode);
5,821,022✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
343,797,939✔
376
  terrno = 0;
343,797,939✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
343,797,939✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
343,798,380✔
379
  if (terrno != 0) {
343,798,331✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
343,797,782✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,483,795✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,483,795✔
387
    return true;
11,483,795✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
332,313,987✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
17,178✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
14,349✔
392
    return true;
14,349✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
332,296,982✔
395
  return false;
332,298,623✔
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) {
30,815,008✔
415
  int32_t code = 0;
30,815,008✔
416
#ifndef TD_ASTRA
417
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
30,815,008✔
418
    mndPullupGrant(pMnode);
1,696,703✔
419
  }
420
  if (sec % tsTtlPushIntervalSec == 0) {
30,815,008✔
421
    mndPullupTtl(pMnode);
3,230,880✔
422
  }
423

424
  if (sec % tsTrimVDbIntervalSec == 0) {
30,815,008✔
425
    mndPullupTrimDb(pMnode);
8,299✔
426
  }
427

428
  if (sec % tsQueryTrimIntervalSec == 0) {
30,815,008✔
429
    mndPullupQueryTrimDb(pMnode);
3,277,151✔
430
  }
431
#endif
432
#ifdef USE_SHARED_STORAGE
433
  if (tsSsEnabled) {
30,815,008✔
434
    if (sec % tsQuerySsMigrateIntervalSec == 0) {
226,500✔
435
      mndPullupUpdateSsMigrateProgress(pMnode);
164,590✔
436
    }
437
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
226,500✔
438
      mndPullupSsMigrateDb(pMnode);
×
439
    }
440
  }
441
#endif
442
#ifdef TD_ENTERPRISE
443
  if (tsAuthReq) {
30,815,008✔
444
    if (sec % tsAuthReqHBInterval == 0) {
×
445
      mndPullupAuth(pMnode);
×
446
    }
447
  }
448
#endif
449
  if (sec % tsTransPullupInterval == 0) {
30,815,008✔
450
    mndPullupTrans(pMnode);
15,412,590✔
451
  }
452

453
  if (sec % tsCompactPullupInterval == 0) {
30,815,008✔
454
    mndPullupCompacts(pMnode);
3,076,321✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
30,815,008✔
458
    mndPullupScans(pMnode);
3,076,321✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
30,815,008✔
461
    mndPullupInstances(pMnode);
6,174,604✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
30,815,008✔
465
    mndCalMqRebalance(pMnode);
15,412,461✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
30,815,008✔
469
    mndPullupTelem(pMnode);
159✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
30,815,008✔
472
    mndIncreaseUpTime(pMnode);
96,754✔
473
  }
474
}
30,815,008✔
475

476
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
301,485,228✔
477
  int32_t code = 0;
301,485,228✔
478
#ifndef TD_ASTRA
479
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
301,485,228✔
480
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
14,876,811✔
481
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
482
    }
483
  }
484

485
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
301,485,228✔
486
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
9,856,592✔
487
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
488
    }
489
  }
490
#endif
491
}
301,485,228✔
492

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
301,485,228✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
301,485,228✔
495
    mndCheckDnodeOffline(pMnode);
5,821,022✔
496
  }
497
}
301,485,228✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
30,815,008✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
30,815,008✔
501
    mndSyncCheckTimeout(pMnode);
1,011,619✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
30,815,008✔
504
    msmHealthCheck(pMnode);
10,279,224✔
505
  }
506
}
30,815,008✔
507

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

513
  while (1) {
312,332,996✔
514
    if (mndGetStop(pMnode)) break;
312,791,512✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
312,332,996✔
517
    if (nowSec == lastSec) {
312,332,996✔
518
      taosMsleep(100);
280,426,477✔
519
      continue;
280,426,477✔
520
    }
521
    lastSec = nowSec;
31,906,519✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
31,906,519✔
524
      taosMsleep(100);
1,091,511✔
525
      mTrace("timer not process since mnode is not leader");
1,091,511✔
526
      continue;
1,091,511✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
30,815,008✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
30,815,008✔
532

533
    taosMsleep(100);
30,815,008✔
534
  }
535

536
  return NULL;
458,516✔
537
}
538

539
static void *mndThreadMsFp(void *param) {
458,516✔
540
  SMnode *pMnode = param;
458,516✔
541
  int64_t lastTime = 0;
458,516✔
542
  setThreadName("mnode-arb-timer");
458,516✔
543

544
  while (1) {
545
    lastTime += 100;
312,350,377✔
546
    taosMsleep(100);
312,350,377✔
547

548
    if (mndGetStop(pMnode)) break;
312,350,377✔
549
    if (lastTime % 10 != 0) continue;
311,891,861✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
311,891,861✔
552
      mTrace("timer not process since mnode is not leader");
10,406,633✔
553
      continue;
10,406,633✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
301,485,228✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
301,485,228✔
559
  }
560

561
  return NULL;
458,516✔
562
}
563

564
static int32_t mndInitTimer(SMnode *pMnode) {
458,516✔
565
  int32_t      code = 0;
458,516✔
566
  TdThreadAttr thAttr;
456,801✔
567
  (void)taosThreadAttrInit(&thAttr);
458,516✔
568
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
458,516✔
569
#ifdef TD_COMPACT_OS
570
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
571
#endif
572
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
458,516✔
573
    mError("failed to create timer thread since %s", tstrerror(code));
×
574
    TAOS_RETURN(code);
×
575
  }
576

577
  (void)taosThreadAttrDestroy(&thAttr);
458,516✔
578
  tmsgReportStartup("mnode-timer", "initialized");
458,516✔
579

580
  TdThreadAttr arbAttr;
456,801✔
581
  (void)taosThreadAttrInit(&arbAttr);
458,516✔
582
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
458,516✔
583
#ifdef TD_COMPACT_OS
584
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
585
#endif
586
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
458,516✔
587
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
588
    TAOS_RETURN(code);
×
589
  }
590

591
  (void)taosThreadAttrDestroy(&arbAttr);
458,516✔
592
  tmsgReportStartup("mnode-timer", "initialized");
458,516✔
593
  TAOS_RETURN(code);
458,516✔
594
}
595

596
static void mndCleanupTimer(SMnode *pMnode) {
458,516✔
597
  if (taosCheckPthreadValid(pMnode->thread)) {
458,516✔
598
    (void)taosThreadJoin(pMnode->thread, NULL);
458,516✔
599
    taosThreadClear(&pMnode->thread);
458,516✔
600
  }
601
  if (taosCheckPthreadValid(pMnode->arbThread)) {
458,516✔
602
    (void)taosThreadJoin(pMnode->arbThread, NULL);
458,516✔
603
    taosThreadClear(&pMnode->arbThread);
458,516✔
604
  }
605
}
458,516✔
606

607
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
458,646✔
608
  int32_t code = 0;
458,646✔
609
  pMnode->path = taosStrdup(path);
458,646✔
610
  if (pMnode->path == NULL) {
458,646✔
611
    code = terrno;
×
612
    TAOS_RETURN(code);
×
613
  }
614

615
  if (taosMkDir(pMnode->path) != 0) {
458,646✔
616
    code = terrno;
×
617
    TAOS_RETURN(code);
×
618
  }
619

620
  TAOS_RETURN(code);
458,646✔
621
}
622

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

638
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
639
  if (taosWaitCfgKeyLoaded() != 0) {
458,646✔
640
    code = terrno;
×
641
    TAOS_RETURN(code);
×
642
  }
643
  if (tsMetaKey[0] != '\0') {
458,646✔
644
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
4,779✔
645
  }
646
#endif
647

648
  pMnode->pWal = walOpen(path, &cfg);
458,646✔
649
  if (pMnode->pWal == NULL) {
458,646✔
650
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
651
    if (terrno != 0) code = terrno;
×
652
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
653
    TAOS_RETURN(code);
×
654
  }
655

656
  TAOS_RETURN(code);
458,646✔
657
}
658

659
static void mndCloseWal(SMnode *pMnode) {
458,584✔
660
  if (pMnode->pWal != NULL) {
458,584✔
661
    walClose(pMnode->pWal);
458,584✔
662
    pMnode->pWal = NULL;
458,584✔
663
  }
664
}
458,584✔
665

666
// Forward declarations for mmFile.c functions
667
extern int32_t mmReadFile(const char *path, SMnodeOpt *pOption);
668
extern int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption);
669

670
// Callback function to persist encrypted flag to mnode.json
671
static int32_t mndPersistEncryptedFlag(void *param) {
6,446✔
672
  SMnode *pMnode = (SMnode *)param;
6,446✔
673
  if (pMnode == NULL) {
6,446✔
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  
677
  mInfo("persisting encrypted flag to mnode.json");
6,446✔
678
  
679
  SMnodeOpt option = {0};
6,446✔
680
  int32_t code = mmReadFile(pMnode->path, &option);
6,446✔
681
  if (code != 0) {
6,446✔
682
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
683
    return code;
×
684
  }
685
  
686
  option.encrypted = true;
6,446✔
687
  code = mmWriteFile(pMnode->path, &option);
6,446✔
688
  if (code != 0) {
6,446✔
689
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
690
    return code;
×
691
  }
692
  
693
  // Also update mnode's encrypted flag
694
  pMnode->encrypted = true;
6,446✔
695
  
696
  mInfo("successfully persisted encrypted flag to mnode.json");
6,446✔
697
  return 0;
6,446✔
698
}
699

700
static int32_t mndInitSdb(SMnode *pMnode) {
458,646✔
701
  int32_t code = 0;
458,646✔
702
  SSdbOpt opt = {0};
458,646✔
703
  opt.path = pMnode->path;
458,646✔
704
  opt.pMnode = pMnode;
458,646✔
705
  opt.pWal = pMnode->pWal;
458,646✔
706

707
  pMnode->pSdb = sdbInit(&opt);
458,646✔
708
  if (pMnode->pSdb == NULL) {
458,646✔
709
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
710
    if (terrno != 0) code = terrno;
×
711
    TAOS_RETURN(code);
×
712
  }
713

714
  TAOS_RETURN(code);
458,646✔
715
}
716

717
static int32_t mndOpenSdb(SMnode *pMnode) {
458,646✔
718
  int32_t code = 0;
458,646✔
719
  
720
  pMnode->pSdb->encrypted = pMnode->encrypted;
458,646✔
721
  
722
  // Set callback for persisting encrypted flag
723
  pMnode->pSdb->persistEncryptedFlagFp = mndPersistEncryptedFlag;
458,646✔
724
  pMnode->pSdb->pMnodeForCallback = pMnode;
458,646✔
725

726
  if (!pMnode->deploy) {
458,646✔
727
    code = sdbReadFile(pMnode->pSdb);
127,546✔
728
  }
729

730
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
458,646✔
731

732
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
458,646✔
733
  return code;
458,646✔
734
}
735

736
static void mndCleanupSdb(SMnode *pMnode) {
458,584✔
737
  if (pMnode->pSdb) {
458,584✔
738
    sdbCleanup(pMnode->pSdb);
458,584✔
739
    pMnode->pSdb = NULL;
458,584✔
740
  }
741
}
458,584✔
742

743
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
22,932,300✔
744
  SMnodeStep step = {0};
22,932,300✔
745
  step.name = name;
22,932,300✔
746
  step.initFp = initFp;
22,932,300✔
747
  step.cleanupFp = cleanupFp;
22,932,300✔
748
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
45,864,600✔
749
    TAOS_RETURN(terrno);
×
750
  }
751

752
  TAOS_RETURN(0);
22,932,300✔
753
}
754

755
static int32_t mndInitSteps(SMnode *pMnode) {
458,646✔
756
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
458,646✔
757
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
458,646✔
758
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
458,646✔
759
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
458,646✔
760
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-encrypt-algorithms", mndInitEncryptAlgr, mndCleanupEncryptAlgr));
458,646✔
761
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
458,646✔
762
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
458,646✔
763
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
458,646✔
764
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
458,646✔
765
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
458,646✔
766
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-xnode", mndInitXnode, mndCleanupXnode));
458,646✔
767
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
458,646✔
768
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
458,646✔
769
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
458,646✔
770
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-role", mndInitRole, mndCleanupRole));
458,646✔
771
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
458,646✔
772
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-token", mndInitToken, mndCleanupToken));
458,646✔
773
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
458,646✔
774
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
458,646✔
775
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
458,646✔
776
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
458,646✔
777
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-instance", mndInitInstance, mndCleanupInstance));
458,646✔
778
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
458,646✔
779
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
458,646✔
780
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
458,646✔
781
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
458,646✔
782
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
458,646✔
783
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
458,646✔
784
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
458,646✔
785
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
458,646✔
786
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
458,646✔
787
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
458,646✔
788
#ifdef USE_MOUNT
789
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
458,646✔
790
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
458,646✔
791
#endif
792
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
458,646✔
793
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
458,646✔
794
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
458,646✔
795
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
458,646✔
796
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
458,646✔
797
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
458,646✔
798
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
458,646✔
799
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
458,646✔
800
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
458,646✔
801
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
458,646✔
802
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
458,646✔
803
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
458,646✔
804
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
458,646✔
805
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
458,646✔
806
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
458,646✔
807
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
458,646✔
808
  return 0;
458,646✔
809
}
810

811
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
458,584✔
812
  if (pMnode->pSteps == NULL) return;
458,584✔
813

814
  if (pos == -1) {
458,584✔
815
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
458,584✔
816
  }
817

818
  for (int32_t s = pos; s >= 0; s--) {
23,387,784✔
819
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
22,929,200✔
820
    mInfo("%s will cleanup", pStep->name);
22,929,200✔
821
    if (pStep->cleanupFp != NULL) {
22,929,200✔
822
      (*pStep->cleanupFp)(pMnode);
22,012,032✔
823
    }
824
  }
825

826
  taosArrayClear(pMnode->pSteps);
458,584✔
827
  taosArrayDestroy(pMnode->pSteps);
458,584✔
828
  pMnode->pSteps = NULL;
458,584✔
829
}
830

831
static int32_t mndExecSteps(SMnode *pMnode) {
458,646✔
832
  int32_t code = 0;
458,646✔
833
  int32_t size = taosArrayGetSize(pMnode->pSteps);
458,646✔
834
  for (int32_t pos = 0; pos < size; pos++) {
23,390,946✔
835
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
22,932,300✔
836
    if (pStep->initFp == NULL) continue;
22,932,300✔
837

838
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
22,932,300✔
839
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
840
      mndCleanupSteps(pMnode, pos);
×
841
      TAOS_RETURN(code);
×
842
    } else {
843
      mInfo("%s is initialized", pStep->name);
22,932,300✔
844
      tmsgReportStartup(pStep->name, "initialized");
22,932,300✔
845
    }
846
  }
847

848
  pMnode->clusterId = mndGetClusterId(pMnode);
458,646✔
849
  TAOS_RETURN(0);
458,646✔
850
}
851

852
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
458,646✔
853
  pMnode->msgCb = pOption->msgCb;
458,646✔
854
  pMnode->selfDnodeId = pOption->dnodeId;
458,646✔
855
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
458,646✔
856
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
458,646✔
857
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
458,646✔
858
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
458,646✔
859
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
458,646✔
860
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
458,646✔
861
  pMnode->encrypted = pOption->encrypted;
458,646✔
862
}
458,646✔
863

864
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
458,646✔
865
  terrno = 0;
458,646✔
866
  mInfo("start to open mnode in %s", path);
458,646✔
867

868
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
458,646✔
869
  if (pMnode == NULL) {
458,646✔
870
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
871
    mError("failed to open mnode in step 1, since %s", terrstr());
×
872
    return NULL;
×
873
  }
874
  (void)memset(pMnode, 0, sizeof(SMnode));
458,646✔
875

876
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
458,646✔
877
  if (code != 0) {
458,646✔
878
    taosMemoryFree(pMnode);
×
879
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
880
    terrno = code;
×
881
    return NULL;
×
882
  }
883

884
  char timestr[24] = "1970-01-01 00:00:00.00";
458,646✔
885
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
458,646✔
886
  if (code < 0) {
458,646✔
887
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
888
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
889
    taosMemoryFree(pMnode);
×
890
    terrno = code;
×
891
    return NULL;
×
892
  }
893

894
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
458,646✔
895
        pOption->numOfTotalReplicas);
896
  mndSetOptions(pMnode, pOption);
458,646✔
897

898
  pMnode->deploy = pOption->deploy;
458,646✔
899
  pMnode->version = pOption->version;
458,646✔
900
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
458,646✔
901
  if (pMnode->pSteps == NULL) {
458,646✔
902
    taosMemoryFree(pMnode);
×
903
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
904
    mError("failed to open mnode in step 4, since %s", terrstr());
×
905
    return NULL;
×
906
  }
907

908
  code = mndCreateDir(pMnode, path);
458,646✔
909
  if (code != 0) {
458,646✔
910
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
911
    mndClose(pMnode);
×
912
    terrno = code;
×
913
    return NULL;
×
914
  }
915

916
  code = mndInitSteps(pMnode);
458,646✔
917
  if (code != 0) {
458,646✔
918
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
919
    mndClose(pMnode);
×
920
    terrno = code;
×
921
    return NULL;
×
922
  }
923

924
  code = mndExecSteps(pMnode);
458,646✔
925
  if (code != 0) {
458,646✔
926
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
927
    mndClose(pMnode);
×
928
    terrno = code;
×
929
    return NULL;
×
930
  }
931

932
  mInfo("mnode open successfully");
458,646✔
933
  return pMnode;
458,646✔
934
}
935

936
void mndPreClose(SMnode *pMnode) {
458,516✔
937
  if (pMnode != NULL) {
458,516✔
938
    int32_t code = 0;
458,516✔
939
    // TODO check return value
940
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
458,516✔
941
    if (code < 0) {
458,516✔
942
      mError("failed to transfer leader since %s", tstrerror(code));
×
943
    }
944
    syncPreStop(pMnode->syncMgmt.sync);
458,516✔
945
    code = sdbWriteFile(pMnode->pSdb, 0);
458,516✔
946
    if (code < 0) {
458,516✔
947
      mError("failed to write sdb since %s", tstrerror(code));
742✔
948
    }
949
  }
950
}
458,516✔
951

952
void mndClose(SMnode *pMnode) {
458,584✔
953
  if (pMnode != NULL) {
458,584✔
954
    mInfo("start to close mnode");
458,584✔
955
    mndCleanupSteps(pMnode, -1);
458,584✔
956
    taosMemoryFreeClear(pMnode->path);
458,584✔
957
    taosMemoryFreeClear(pMnode);
458,584✔
958
    mInfo("mnode is closed");
458,584✔
959
  }
960
}
458,584✔
961

962
int32_t mndStart(SMnode *pMnode) {
458,516✔
963
  mndSyncStart(pMnode);
458,516✔
964
  if (pMnode->deploy) {
458,516✔
965
    if (sdbDeploy(pMnode->pSdb) != 0) {
331,100✔
966
      mError("failed to deploy sdb while start mnode");
×
967
      return -1;
×
968
    }
969
    mndSetRestored(pMnode, true);
331,100✔
970
  }
971
  if (mndIsLeader(pMnode)) {
458,516✔
972
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
415,745✔
973
      mError("failed to upgrade sdb while start mnode");
×
974
      return -1;
×
975
    }
976
  }
977
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
458,516✔
978
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
458,516✔
979

980
  return mndInitTimer(pMnode);
458,516✔
981
}
982

983
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
458,516✔
984

985
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
361,800✔
986

987
int32_t mndGetEncryptedFlag(SMnode *pMnode) { return pMnode->encrypted; }
361,800✔
988

989
int32_t mndIsCatchUp(SMnode *pMnode) {
169,051✔
990
  int64_t rid = pMnode->syncMgmt.sync;
169,051✔
991
  return syncIsCatchUp(rid);
169,051✔
992
}
993

994
ESyncRole mndGetRole(SMnode *pMnode) {
169,051✔
995
  int64_t rid = pMnode->syncMgmt.sync;
169,051✔
996
  return syncGetRole(rid);
169,051✔
997
}
998

999
int64_t mndGetTerm(SMnode *pMnode) {
9,957,462✔
1000
  int64_t rid = pMnode->syncMgmt.sync;
9,957,462✔
1001
  return syncGetTerm(rid);
9,957,462✔
1002
}
1003

1004
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
24,836,085✔
1005

1006
void mndStop(SMnode *pMnode) {
458,516✔
1007
  mndSetStop(pMnode);
458,516✔
1008
  mndSyncStop(pMnode);
458,516✔
1009
  mndCleanupTimer(pMnode);
458,516✔
1010
}
458,516✔
1011

1012
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
57,995,981✔
1013
  SMnode    *pMnode = pMsg->info.node;
57,995,981✔
1014
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
57,995,981✔
1015

1016
  const STraceId *trace = &pMsg->info.traceId;
57,995,981✔
1017
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
57,995,981✔
1018

1019
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
57,995,981✔
1020
  if (code != 0) {
57,995,981✔
1021
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
308✔
1022
            tstrerror(code), code);
1023
  }
1024

1025
  return code;
57,995,981✔
1026
}
1027

1028
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
287,184,940✔
1029
  int32_t code = 0;
287,184,940✔
1030
  if (!IsReq(pMsg)) TAOS_RETURN(code);
287,184,940✔
1031
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
251,823,568✔
1032
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
246,297,603✔
1033
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
241,640,347✔
1034
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
230,684,955✔
1035
    TAOS_RETURN(code);
21,147,890✔
1036
  }
1037

1038
  SMnode *pMnode = pMsg->info.node;
230,681,678✔
1039
  (void)taosThreadRwlockRdlock(&pMnode->lock);
230,690,045✔
1040
  if (pMnode->stopped) {
230,692,397✔
1041
    (void)taosThreadRwlockUnlock(&pMnode->lock);
3,175✔
1042
    code = TSDB_CODE_APP_IS_STOPPING;
3,175✔
1043
    TAOS_RETURN(code);
3,175✔
1044
  }
1045

1046
  terrno = 0;
230,686,053✔
1047
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
230,683,345✔
1048
  if (terrno != 0) {
230,690,774✔
1049
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1050
    code = terrno;
×
1051
    TAOS_RETURN(code);
×
1052
  }
1053

1054
  if (state.state != TAOS_SYNC_STATE_LEADER) {
230,688,615✔
1055
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,354,372✔
1056
    code = TSDB_CODE_SYN_NOT_LEADER;
2,354,463✔
1057
    goto _OVER;
2,354,463✔
1058
  }
1059

1060
  if (!state.restored || !pMnode->restored) {
228,334,243✔
1061
    (void)taosThreadRwlockUnlock(&pMnode->lock);
394,378✔
1062
    code = TSDB_CODE_SYN_RESTORING;
392,576✔
1063
    goto _OVER;
392,576✔
1064
  }
1065

1066
#if 1
1067
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
227,939,985✔
1068
#else
1069
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1070
  mTrace("mnode rpc is acquired, ref:%d", ref);
1071
#endif
1072

1073
  (void)taosThreadRwlockUnlock(&pMnode->lock);
227,943,611✔
1074
  TAOS_RETURN(code);
227,939,258✔
1075

1076
_OVER:
2,747,039✔
1077
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,747,039✔
1078
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,747,039✔
1079
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,746,948✔
1080
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,747,039✔
1081
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,746,948✔
1082
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,747,039✔
1083
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,747,039✔
1084
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,747,039✔
1085
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,747,039✔
UNCOV
1086
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
1087
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
1088
    TAOS_RETURN(code);
×
1089
  }
1090

1091
  const STraceId *trace = &pMsg->info.traceId;
2,746,862✔
1092
  SEpSet          epSet = {0};
2,746,862✔
1093
  mndGetMnodeEpSet(pMnode, &epSet);
2,746,953✔
1094

1095
  mGDebug(
2,747,039✔
1096
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1097
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1098
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1099
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1100

1101
  if (epSet.numOfEps <= 0) return -1;
2,747,039✔
1102

1103
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
10,084,080✔
1104
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
7,337,129✔
1105
  }
1106

1107
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,746,951✔
1108
  pMsg->info.rsp = rpcMallocCont(contLen);
2,746,522✔
1109
  if (pMsg->info.rsp != NULL) {
2,747,039✔
1110
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,747,039✔
1111
      mError("failed to serialize ep set");
×
1112
    }
1113
    pMsg->info.hasEpSet = 1;
2,746,620✔
1114
    pMsg->info.rspLen = contLen;
2,746,634✔
1115
  }
1116

1117
  TAOS_RETURN(code);
2,746,537✔
1118
}
1119

1120
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
287,205,187✔
1121
  SMnode         *pMnode = pMsg->info.node;
287,205,187✔
1122
  const STraceId *trace = &pMsg->info.traceId;
287,206,641✔
1123
  int32_t         code = TSDB_CODE_SUCCESS;
287,205,586✔
1124

1125
#ifdef TD_ENTERPRISE
1126
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
287,205,586✔
1127
    SCachedTokenInfo ti = {0};
18,974✔
1128
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
18,974✔
1129
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,091✔
1130
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
2,091✔
1131
      TAOS_RETURN(code);
2,091✔
1132
    }
1133
    if (ti.enabled == 0) {
16,883✔
1134
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
2,898✔
1135
      code = TSDB_CODE_MND_TOKEN_DISABLED;
2,898✔
1136
      TAOS_RETURN(code);
2,898✔
1137
    }
1138
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
13,985✔
1139
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1140
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1141
      TAOS_RETURN(code);
×
1142
    }
1143
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
13,985✔
1144
  }
1145
#endif
1146

1147
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
287,188,038✔
1148
  MndMsgFpExt fpExt = NULL;
287,196,402✔
1149
  if (fp == NULL) {
287,196,402✔
1150
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
21,146,233✔
1151
    if (fpExt == NULL) {
21,146,233✔
1152
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1153
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1154
      TAOS_RETURN(code);
×
1155
    }
1156
  }
1157

1158
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
287,196,402✔
1159

1160
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
284,444,532✔
1161
  if (fp)
284,445,425✔
1162
    code = (*fp)(pMsg);
263,300,246✔
1163
  else
1164
    code = (*fpExt)(pMsg, pQueueInfo);
21,145,179✔
1165
  mndReleaseRpc(pMnode);
284,450,104✔
1166

1167
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
284,453,442✔
1168
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
33,384,894✔
1169
  } else if (code == 0) {
251,068,548✔
1170
    mGTrace("msg:%p, successfully processed", pMsg);
248,173,485✔
1171
  } else {
1172
    // TODO removve this wrong set code
1173
    if (code == -1) {
2,895,063✔
1174
      code = terrno;
×
1175
    }
1176
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,895,063✔
1177
            TMSG_INFO(pMsg->msgType));
1178
  }
1179

1180
  TAOS_RETURN(code);
284,453,442✔
1181
}
1182

1183
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
110,075,040✔
1184
  tmsg_t type = TMSG_INDEX(msgType);
110,075,040✔
1185
  if (type < TDMT_MAX) {
110,075,040✔
1186
    pMnode->msgFp[type] = fp;
110,075,040✔
1187
  }
1188
}
110,075,040✔
1189

1190
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,669,168✔
1191
  tmsg_t type = TMSG_INDEX(msgType);
3,669,168✔
1192
  if (type < TDMT_MAX) {
3,669,168✔
1193
    pMnode->msgFpExt[type] = fp;
3,669,168✔
1194
  }
1195
}
3,669,168✔
1196

1197
// Note: uid 0 is reserved
1198
int64_t mndGenerateUid(const char *name, int32_t len) {
11,575,918✔
1199
  int32_t hashval = MurmurHash3_32(name, len);
11,575,918✔
1200
  do {
×
1201
    int64_t us = taosGetTimestampUs();
11,576,163✔
1202
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
11,576,163✔
1203
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
11,576,163✔
1204
    if (uuid) {
11,576,167✔
1205
      return llabs(uuid);
11,576,167✔
1206
    }
1207
  } while (true);
1208
}
1209

1210
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
58✔
1211
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1212
  int32_t code = mndAcquireRpc(pMnode);
58✔
1213
  if (code < 0) {
58✔
1214
    TAOS_RETURN(code);
×
1215
  } else if (code == 1) {
58✔
1216
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1217
  }
1218

1219
  SSdb   *pSdb = pMnode->pSdb;
58✔
1220
  int64_t ms = taosGetTimestampMs();
58✔
1221

1222
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
58✔
1223
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
58✔
1224
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
58✔
1225
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
58✔
1226
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
58✔
1227
      pStbInfo->stbs == NULL) {
58✔
1228
    mndReleaseRpc(pMnode);
×
1229
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1230
    if (terrno != 0) code = terrno;
×
1231
    TAOS_RETURN(code);
×
1232
  }
1233

1234
  // cluster info
1235
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
58✔
1236
  pClusterInfo->monitor_interval = tsMonitorInterval;
58✔
1237
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
58✔
1238
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
58✔
1239
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
58✔
1240
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
58✔
1241
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
58✔
1242

1243
  void *pIter = NULL;
58✔
1244
  while (1) {
58✔
1245
    SDnodeObj *pObj = NULL;
116✔
1246
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
116✔
1247
    if (pIter == NULL) break;
116✔
1248

1249
    SMonDnodeDesc desc = {0};
58✔
1250
    desc.dnode_id = pObj->id;
58✔
1251
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
58✔
1252
    if (mndIsDnodeOnline(pObj, ms)) {
58✔
1253
      tstrncpy(desc.status, "ready", sizeof(desc.status));
58✔
1254
    } else {
1255
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1256
    }
1257
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
116✔
1258
      mError("failed put dnode into array, but continue at this monitor report")
×
1259
    }
1260
    sdbRelease(pSdb, pObj);
58✔
1261
  }
1262

1263
  pIter = NULL;
58✔
1264
  while (1) {
58✔
1265
    SMnodeObj *pObj = NULL;
116✔
1266
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
116✔
1267
    if (pIter == NULL) break;
116✔
1268

1269
    SMonMnodeDesc desc = {0};
58✔
1270
    desc.mnode_id = pObj->id;
58✔
1271
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
58✔
1272

1273
    if (pObj->id == pMnode->selfDnodeId) {
58✔
1274
      pClusterInfo->first_ep_dnode_id = pObj->id;
58✔
1275
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
58✔
1276
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1277
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
58✔
1278
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1279
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
58✔
1280
      desc.syncState = TAOS_SYNC_STATE_LEADER;
58✔
1281
    } else {
1282
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1283
      desc.syncState = pObj->syncState;
×
1284
    }
1285
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
116✔
1286
      mError("failed to put mnode into array, but continue at this monitor report");
×
1287
    }
1288
    sdbRelease(pSdb, pObj);
58✔
1289
  }
1290

1291
  // vgroup info
1292
  pIter = NULL;
58✔
1293
  while (1) {
116✔
1294
    SVgObj *pVgroup = NULL;
174✔
1295
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
174✔
1296
    if (pIter == NULL) break;
174✔
1297

1298
    if (pVgroup->mountVgId) {
116✔
1299
      sdbRelease(pSdb, pVgroup);
×
1300
      continue;
×
1301
    }
1302

1303
    pClusterInfo->vgroups_total++;
116✔
1304
    pClusterInfo->tbs_total += pVgroup->numOfTables;
116✔
1305

1306
    SMonVgroupDesc desc = {0};
116✔
1307
    desc.vgroup_id = pVgroup->vgId;
116✔
1308

1309
    SName name = {0};
116✔
1310
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
116✔
1311
    if (code < 0) {
116✔
1312
      mError("failed to get db name since %s", tstrerror(code));
×
1313
      sdbCancelFetch(pSdb, pIter);
×
1314
      sdbRelease(pSdb, pVgroup);
×
1315
      TAOS_RETURN(code);
×
1316
    }
1317
    (void)tNameGetDbName(&name, desc.database_name);
116✔
1318

1319
    desc.tables_num = pVgroup->numOfTables;
116✔
1320
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
116✔
1321
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
116✔
1322
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
232✔
1323
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
116✔
1324
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
116✔
1325
      pVnDesc->dnode_id = pVgid->dnodeId;
116✔
1326
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
116✔
1327
      pVnDesc->syncState = pVgid->syncState;
116✔
1328
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
116✔
1329
        tstrncpy(desc.status, "ready", sizeof(desc.status));
116✔
1330
        pClusterInfo->vgroups_alive++;
116✔
1331
      }
1332
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
116✔
1333
        pClusterInfo->vnodes_alive++;
116✔
1334
      }
1335
      pClusterInfo->vnodes_total++;
116✔
1336
    }
1337

1338
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
232✔
1339
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1340
    }
1341
    sdbRelease(pSdb, pVgroup);
116✔
1342
  }
1343

1344
  // stb info
1345
  pIter = NULL;
58✔
1346
  while (1) {
×
1347
    SStbObj *pStb = NULL;
58✔
1348
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
58✔
1349
    if (pIter == NULL) break;
58✔
1350

1351
    SMonStbDesc desc = {0};
×
1352

1353
    SName name1 = {0};
×
1354
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1355
    if (code < 0) {
×
1356
      mError("failed to get db name since %s", tstrerror(code));
×
1357
      sdbRelease(pSdb, pStb);
×
1358
      TAOS_RETURN(code);
×
1359
    }
1360
    (void)tNameGetDbName(&name1, desc.database_name);
×
1361

1362
    SName name2 = {0};
×
1363
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1364
    if (code < 0) {
×
1365
      mError("failed to get table name since %s", tstrerror(code));
×
1366
      sdbRelease(pSdb, pStb);
×
1367
      TAOS_RETURN(code);
×
1368
    }
1369
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1370

1371
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1372
      mError("failed to put stb into array, but continue at this monitor report");
×
1373
    }
1374
    sdbRelease(pSdb, pStb);
×
1375
  }
1376

1377
  // grant info
1378
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
58✔
1379
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
58✔
1380
  if (pMnode->grant.expireTimeMS == 0) {
58✔
1381
    pGrantInfo->expire_time = 0;
×
1382
    pGrantInfo->timeseries_total = 0;
×
1383
  }
1384

1385
  mndReleaseRpc(pMnode);
58✔
1386
  TAOS_RETURN(code);
58✔
1387
}
1388

1389
int32_t mndResetTimer(SMnode *pMnode){
×
1390
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1391
}
1392

1393
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
30,841,707✔
1394
  mTrace("mnode get load");
30,841,707✔
1395
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
30,841,707✔
1396
  pLoad->syncState = state.state;
30,841,707✔
1397
  pLoad->syncRestore = state.restored;
30,841,707✔
1398
  pLoad->syncTerm = state.term;
30,841,707✔
1399
  pLoad->roleTimeMs = state.roleTimeMs;
30,841,707✔
1400
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
30,841,707✔
1401
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1402
  return 0;
30,841,707✔
1403
}
1404

1405
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
9,856,592✔
1406
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
9,856,592✔
1407
  return state.roleTimeMs;
9,856,592✔
1408
}
1409

1410
void mndSetRestored(SMnode *pMnode, bool restored) {
458,516✔
1411
  if (restored) {
458,516✔
1412
    (void)taosThreadRwlockWrlock(&pMnode->lock);
458,516✔
1413
    pMnode->restored = true;
458,516✔
1414
    (void)taosThreadRwlockUnlock(&pMnode->lock);
458,516✔
1415
    mInfo("mnode set restored:%d", restored);
458,516✔
1416
  } else {
1417
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1418
    pMnode->restored = false;
×
1419
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1420
    mInfo("mnode set restored:%d", restored);
×
1421
    while (1) {
1422
      if (pMnode->rpcRef <= 0) break;
×
1423
      taosMsleep(3);
×
1424
    }
1425
  }
1426
}
458,516✔
1427

1428
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1429

1430
void mndSetStop(SMnode *pMnode) {
458,516✔
1431
  (void)taosThreadRwlockWrlock(&pMnode->lock);
458,516✔
1432
  pMnode->stopped = true;
458,516✔
1433
  (void)taosThreadRwlockUnlock(&pMnode->lock);
458,516✔
1434
  mInfo("mnode set stopped");
458,516✔
1435
}
458,516✔
1436

1437
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
625,133,959✔
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