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

taosdata / TDengine / #4935

22 Jan 2026 06:38AM UTC coverage: 66.708% (+0.02%) from 66.691%
#4935

push

travis-ci

web-flow
merge: from main to 3.0 #34371

121 of 271 new or added lines in 17 files covered. (44.65%)

9066 existing lines in 149 files now uncovered.

203884 of 305637 relevant lines covered (66.71%)

125811266.68 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
247,845,651✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
247,845,651✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
247,844,558✔
88
#else
89
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
90
  mTrace("mnode rpc is released, ref:%d", ref);
91
#endif
92
  (void)taosThreadRwlockUnlock(&pMnode->lock);
247,843,782✔
93
}
247,845,510✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
63,068,559✔
96
  terrno = 0;
63,068,559✔
97
  SMTimerReq timerReq = {0};
63,068,559✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
63,068,559✔
100
  if (contLen <= 0) return NULL;
63,068,559✔
101
  void *pReq = rpcMallocCont(contLen);
63,068,559✔
102
  if (pReq == NULL) return NULL;
63,068,559✔
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
  void *pIter = NULL;
112,793✔
309
  while (1) {
365,487✔
310
    SVgObj *pVgroup = NULL;
478,280✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
478,280✔
312
    if (pIter == NULL) break;
478,280✔
313

314
    bool stateChanged = false;
365,487✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
956,547✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
745,687✔
317
      if (pGid->dnodeId == dnodeId) {
745,687✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
154,627✔
319
          mInfo(
56,765✔
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;
56,765✔
325
          pGid->syncRestore = 0;
56,765✔
326
          pGid->syncCanRead = 0;
56,765✔
327
          pGid->startTimeMs = 0;
56,765✔
328
          pGid->learnerProgress = 0;
56,765✔
329
          pGid->snapSeq = -1;
56,765✔
330
          stateChanged = true;
56,765✔
331
        }
332
        break;
154,627✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
365,487✔
347
  }
348
}
112,793✔
349

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

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

357
  void *pIter = NULL;
4,827,913✔
358
  while (1) {
8,259,233✔
359
    SDnodeObj *pDnode = NULL;
13,087,146✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,087,146✔
361
    if (pIter == NULL) break;
13,087,146✔
362

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

369
    sdbRelease(pSdb, pDnode);
8,259,233✔
370
  }
371

372
  mndReleaseRpc(pMnode);
4,827,913✔
373
}
374

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

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
286,732,580✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,912,981✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
10,912,981✔
387
    return true;
10,912,981✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
275,819,599✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,593✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
12,593✔
392
    return true;
12,593✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
275,807,006✔
395
  return false;
275,806,843✔
396
}
397

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

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

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

424
  if (sec % tsTrimVDbIntervalSec == 0) {
25,531,260✔
425
    mndPullupTrimDb(pMnode);
5,809✔
426
  }
427

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

453
  if (sec % tsCompactPullupInterval == 0) {
25,531,260✔
454
    mndPullupCompacts(pMnode);
2,549,751✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
25,531,260✔
458
    mndPullupScans(pMnode);
2,549,751✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
25,531,260✔
461
    mndPullupInstances(pMnode);
5,106,539✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
25,531,260✔
465
    mndCalMqRebalance(pMnode);
12,768,234✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
25,531,260✔
469
    mndPullupTelem(pMnode);
256✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
25,531,260✔
472
    mndIncreaseUpTime(pMnode);
82,624✔
473
  }
474
}
25,531,260✔
475

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

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

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
250,276,118✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
250,276,118✔
495
    mndCheckDnodeOffline(pMnode);
4,827,913✔
496
  }
497
}
250,276,118✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
25,531,260✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
25,531,260✔
501
    mndSyncCheckTimeout(pMnode);
840,491✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
25,531,260✔
504
    msmHealthCheck(pMnode);
8,517,316✔
505
  }
506
}
25,531,260✔
507

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

513
  while (1) {
260,621,609✔
514
    if (mndGetStop(pMnode)) break;
261,017,830✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
260,621,609✔
517
    if (nowSec == lastSec) {
260,621,609✔
518
      taosMsleep(100);
234,049,686✔
519
      continue;
234,049,686✔
520
    }
521
    lastSec = nowSec;
26,571,923✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
26,571,923✔
524
      taosMsleep(100);
1,040,663✔
525
      mTrace("timer not process since mnode is not leader");
1,040,663✔
526
      continue;
1,040,663✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
25,531,260✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
25,531,260✔
532

533
    taosMsleep(100);
25,531,260✔
534
  }
535

536
  return NULL;
396,221✔
537
}
538

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

544
  while (1) {
545
    lastTime += 100;
260,557,250✔
546
    taosMsleep(100);
260,557,250✔
547

548
    if (mndGetStop(pMnode)) break;
260,557,250✔
549
    if (lastTime % 10 != 0) continue;
260,161,029✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
260,161,029✔
552
      mTrace("timer not process since mnode is not leader");
9,884,911✔
553
      continue;
9,884,911✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
250,276,118✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
250,276,118✔
559
  }
560

561
  return NULL;
396,221✔
562
}
563

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

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

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

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

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

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

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

620
  TAOS_RETURN(code);
396,279✔
621
}
622

623
static int32_t mndInitWal(SMnode *pMnode) {
396,279✔
624
  int32_t code = 0;
396,279✔
625
  char    path[PATH_MAX + 20] = {0};
396,279✔
626
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
396,279✔
627
  SWalCfg cfg = {.vgId = 1,
396,279✔
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) {
396,279✔
640
    code = terrno;
×
641
    TAOS_RETURN(code);
×
642
  }
643
  if (tsMetaKey[0] != '\0') {
396,279✔
644
    tstrncpy(cfg.encryptData.encryptKey, tsMetaKey, ENCRYPT_KEY_LEN + 1);
3,730✔
645
  }
646
#endif
647

648
  pMnode->pWal = walOpen(path, &cfg);
396,279✔
649
  if (pMnode->pWal == NULL) {
396,279✔
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);
396,279✔
657
}
658

659
static void mndCloseWal(SMnode *pMnode) {
396,221✔
660
  if (pMnode->pWal != NULL) {
396,221✔
661
    walClose(pMnode->pWal);
396,221✔
662
    pMnode->pWal = NULL;
396,221✔
663
  }
664
}
396,221✔
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) {
4,528✔
672
  SMnode *pMnode = (SMnode *)param;
4,528✔
673
  if (pMnode == NULL) {
4,528✔
NEW
674
    return TSDB_CODE_INVALID_PARA;
×
675
  }
676
  
677
  mInfo("persisting encrypted flag to mnode.json");
4,528✔
678
  
679
  SMnodeOpt option = {0};
4,528✔
680
  int32_t code = mmReadFile(pMnode->path, &option);
4,528✔
681
  if (code != 0) {
4,528✔
NEW
682
    mError("failed to read mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
NEW
683
    return code;
×
684
  }
685
  
686
  option.encrypted = true;
4,528✔
687
  code = mmWriteFile(pMnode->path, &option);
4,528✔
688
  if (code != 0) {
4,528✔
NEW
689
    mError("failed to write mnode.json for persisting encrypted flag since %s", tstrerror(code));
×
NEW
690
    return code;
×
691
  }
692
  
693
  // Also update mnode's encrypted flag
694
  pMnode->encrypted = true;
4,528✔
695
  
696
  mInfo("successfully persisted encrypted flag to mnode.json");
4,528✔
697
  return 0;
4,528✔
698
}
699

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

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

714
  TAOS_RETURN(code);
396,279✔
715
}
716

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

726
  if (!pMnode->deploy) {
396,279✔
727
    code = sdbReadFile(pMnode->pSdb);
113,818✔
728
  }
729

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

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

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

743
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
19,813,950✔
744
  SMnodeStep step = {0};
19,813,950✔
745
  step.name = name;
19,813,950✔
746
  step.initFp = initFp;
19,813,950✔
747
  step.cleanupFp = cleanupFp;
19,813,950✔
748
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
39,627,900✔
749
    TAOS_RETURN(terrno);
×
750
  }
751

752
  TAOS_RETURN(0);
19,813,950✔
753
}
754

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

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

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

818
  for (int32_t s = pos; s >= 0; s--) {
20,207,271✔
819
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,811,050✔
820
    mInfo("%s will cleanup", pStep->name);
19,811,050✔
821
    if (pStep->cleanupFp != NULL) {
19,811,050✔
822
      (*pStep->cleanupFp)(pMnode);
19,018,608✔
823
    }
824
  }
825

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

831
static int32_t mndExecSteps(SMnode *pMnode) {
396,279✔
832
  int32_t code = 0;
396,279✔
833
  int32_t size = taosArrayGetSize(pMnode->pSteps);
396,279✔
834
  for (int32_t pos = 0; pos < size; pos++) {
20,210,229✔
835
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,813,950✔
836
    if (pStep->initFp == NULL) continue;
19,813,950✔
837

838
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
19,813,950✔
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);
19,813,950✔
844
      tmsgReportStartup(pStep->name, "initialized");
19,813,950✔
845
    }
846
  }
847

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

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

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

868
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
396,279✔
869
  if (pMnode == NULL) {
396,279✔
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));
396,279✔
875

876
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
396,279✔
877
  if (code != 0) {
396,279✔
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";
396,279✔
885
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
396,279✔
886
  if (code < 0) {
396,279✔
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,
396,279✔
895
        pOption->numOfTotalReplicas);
896
  mndSetOptions(pMnode, pOption);
396,279✔
897

898
  pMnode->deploy = pOption->deploy;
396,279✔
899
  pMnode->version = pOption->version;
396,279✔
900
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
396,279✔
901
  if (pMnode->pSteps == NULL) {
396,279✔
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);
396,279✔
909
  if (code != 0) {
396,279✔
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);
396,279✔
917
  if (code != 0) {
396,279✔
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);
396,279✔
925
  if (code != 0) {
396,279✔
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");
396,279✔
933
  return pMnode;
396,279✔
934
}
935

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

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

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

980
  return mndInitTimer(pMnode);
396,221✔
981
}
982

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

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

987
int32_t mndIsCatchUp(SMnode *pMnode) {
179,445✔
988
  int64_t rid = pMnode->syncMgmt.sync;
179,445✔
989
  return syncIsCatchUp(rid);
179,445✔
990
}
991

992
ESyncRole mndGetRole(SMnode *pMnode) {
179,445✔
993
  int64_t rid = pMnode->syncMgmt.sync;
179,445✔
994
  return syncGetRole(rid);
179,445✔
995
}
996

997
int64_t mndGetTerm(SMnode *pMnode) {
8,281,006✔
998
  int64_t rid = pMnode->syncMgmt.sync;
8,281,006✔
999
  return syncGetTerm(rid);
8,281,006✔
1000
}
1001

1002
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
20,632,490✔
1003

1004
void mndStop(SMnode *pMnode) {
396,221✔
1005
  mndSetStop(pMnode);
396,221✔
1006
  mndSyncStop(pMnode);
396,221✔
1007
  mndCleanupTimer(pMnode);
396,221✔
1008
}
396,221✔
1009

1010
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
50,742,017✔
1011
  SMnode    *pMnode = pMsg->info.node;
50,742,017✔
1012
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
50,742,120✔
1013

1014
  const STraceId *trace = &pMsg->info.traceId;
50,742,120✔
1015
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
50,742,120✔
1016

1017
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
50,742,120✔
1018
  if (code != 0) {
50,742,120✔
1019
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
474✔
1020
            tstrerror(code), code);
1021
  }
1022

1023
  return code;
50,742,120✔
1024
}
1025

1026
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
246,481,329✔
1027
  int32_t code = 0;
246,481,329✔
1028
  if (!IsReq(pMsg)) TAOS_RETURN(code);
246,481,329✔
1029
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
216,265,301✔
1030
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
211,614,622✔
1031
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
207,777,514✔
1032
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
198,639,673✔
1033
    TAOS_RETURN(code);
17,627,702✔
1034
  }
1035

1036
  SMnode *pMnode = pMsg->info.node;
198,644,899✔
1037
  (void)taosThreadRwlockRdlock(&pMnode->lock);
198,642,293✔
1038
  if (pMnode->stopped) {
198,648,292✔
1039
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,575✔
1040
    code = TSDB_CODE_APP_IS_STOPPING;
1,575✔
1041
    TAOS_RETURN(code);
1,575✔
1042
  }
1043

1044
  terrno = 0;
198,644,201✔
1045
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
198,643,122✔
1046
  if (terrno != 0) {
198,648,971✔
1047
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1048
    code = terrno;
×
1049
    TAOS_RETURN(code);
×
1050
  }
1051

1052
  if (state.state != TAOS_SYNC_STATE_LEADER) {
198,648,756✔
1053
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,369,597✔
1054
    code = TSDB_CODE_SYN_NOT_LEADER;
2,369,681✔
1055
    goto _OVER;
2,369,681✔
1056
  }
1057

1058
  if (!state.restored || !pMnode->restored) {
196,279,159✔
1059
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,106,045✔
1060
    code = TSDB_CODE_SYN_RESTORING;
1,101,792✔
1061
    goto _OVER;
1,101,792✔
1062
  }
1063

1064
#if 1
1065
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
195,173,766✔
1066
#else
1067
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1068
  mTrace("mnode rpc is acquired, ref:%d", ref);
1069
#endif
1070

1071
  (void)taosThreadRwlockUnlock(&pMnode->lock);
195,176,234✔
1072
  TAOS_RETURN(code);
195,177,773✔
1073

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

1089
  const STraceId *trace = &pMsg->info.traceId;
3,471,289✔
1090
  SEpSet          epSet = {0};
3,471,177✔
1091
  mndGetMnodeEpSet(pMnode, &epSet);
3,471,219✔
1092

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

1099
  if (epSet.numOfEps <= 0) return -1;
3,471,473✔
1100

1101
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
11,557,569✔
1102
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
8,086,829✔
1103
  }
1104

1105
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,470,740✔
1106
  pMsg->info.rsp = rpcMallocCont(contLen);
3,469,561✔
1107
  if (pMsg->info.rsp != NULL) {
3,471,392✔
1108
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,471,473✔
1109
      mError("failed to serialize ep set");
×
1110
    }
1111
    pMsg->info.hasEpSet = 1;
3,470,433✔
1112
    pMsg->info.rspLen = contLen;
3,469,853✔
1113
  }
1114

1115
  TAOS_RETURN(code);
3,470,656✔
1116
}
1117

1118
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
246,497,317✔
1119
  SMnode         *pMnode = pMsg->info.node;
246,497,317✔
1120
  const STraceId *trace = &pMsg->info.traceId;
246,498,472✔
1121
  int32_t         code = TSDB_CODE_SUCCESS;
246,495,514✔
1122

1123
#ifdef TD_ENTERPRISE
1124
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
246,495,514✔
1125
    SCachedTokenInfo ti = {0};
25,062✔
1126
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
25,062✔
1127
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
3,604✔
1128
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
3,604✔
1129
      TAOS_RETURN(code);
3,604✔
1130
    }
1131
    if (ti.enabled == 0) {
21,458✔
1132
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
4,600✔
1133
      code = TSDB_CODE_MND_TOKEN_DISABLED;
4,600✔
1134
      TAOS_RETURN(code);
4,600✔
1135
    }
1136
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
16,858✔
1137
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1138
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1139
      TAOS_RETURN(code);
×
1140
    }
1141
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
16,858✔
1142
  }
1143
#endif
1144

1145
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
246,483,386✔
1146
  MndMsgFpExt fpExt = NULL;
246,485,923✔
1147
  if (fp == NULL) {
246,485,923✔
1148
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
17,624,237✔
1149
    if (fpExt == NULL) {
17,624,237✔
1150
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1151
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1152
      TAOS_RETURN(code);
×
1153
    }
1154
  }
1155

1156
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
246,485,923✔
1157

1158
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
243,013,767✔
1159
  if (fp)
243,012,890✔
1160
    code = (*fp)(pMsg);
225,388,928✔
1161
  else
1162
    code = (*fpExt)(pMsg, pQueueInfo);
17,623,962✔
1163
  mndReleaseRpc(pMnode);
243,018,006✔
1164

1165
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
243,017,533✔
1166
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
28,000,768✔
1167
  } else if (code == 0) {
215,016,765✔
1168
    mGTrace("msg:%p, successfully processed", pMsg);
212,526,106✔
1169
  } else {
1170
    // TODO removve this wrong set code
1171
    if (code == -1) {
2,490,659✔
1172
      code = terrno;
×
1173
    }
1174
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,490,659✔
1175
            TMSG_INFO(pMsg->msgType));
1176
  }
1177

1178
  TAOS_RETURN(code);
243,017,533✔
1179
}
1180

1181
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
92,333,007✔
1182
  tmsg_t type = TMSG_INDEX(msgType);
92,333,007✔
1183
  if (type < TDMT_MAX) {
92,333,007✔
1184
    pMnode->msgFp[type] = fp;
92,333,007✔
1185
  }
1186
}
92,333,007✔
1187

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

1195
// Note: uid 0 is reserved
1196
int64_t mndGenerateUid(const char *name, int32_t len) {
9,662,855✔
1197
  int32_t hashval = MurmurHash3_32(name, len);
9,662,855✔
1198
  do {
×
1199
    int64_t us = taosGetTimestampUs();
9,662,868✔
1200
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,662,868✔
1201
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,662,868✔
1202
    if (uuid) {
9,663,003✔
1203
      return llabs(uuid);
9,663,003✔
1204
    }
1205
  } while (true);
1206
}
1207

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

1217
  SSdb   *pSdb = pMnode->pSdb;
64✔
1218
  int64_t ms = taosGetTimestampMs();
64✔
1219

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

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

1241
  void *pIter = NULL;
64✔
1242
  while (1) {
64✔
1243
    SDnodeObj *pObj = NULL;
128✔
1244
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
128✔
1245
    if (pIter == NULL) break;
128✔
1246

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

1261
  pIter = NULL;
64✔
1262
  while (1) {
64✔
1263
    SMnodeObj *pObj = NULL;
128✔
1264
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
128✔
1265
    if (pIter == NULL) break;
128✔
1266

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

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

1289
  // vgroup info
1290
  pIter = NULL;
64✔
1291
  while (1) {
128✔
1292
    SVgObj *pVgroup = NULL;
192✔
1293
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
192✔
1294
    if (pIter == NULL) break;
192✔
1295

1296
    if (pVgroup->mountVgId) {
128✔
1297
      sdbRelease(pSdb, pVgroup);
×
1298
      continue;
×
1299
    }
1300

1301
    pClusterInfo->vgroups_total++;
128✔
1302
    pClusterInfo->tbs_total += pVgroup->numOfTables;
128✔
1303

1304
    SMonVgroupDesc desc = {0};
128✔
1305
    desc.vgroup_id = pVgroup->vgId;
128✔
1306

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

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

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

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

UNCOV
1349
    SMonStbDesc desc = {0};
×
1350

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

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

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

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

1383
  mndReleaseRpc(pMnode);
64✔
1384
  TAOS_RETURN(code);
64✔
1385
}
1386

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

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

1403
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,175,476✔
1404
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,175,476✔
1405
  return state.roleTimeMs;
8,175,476✔
1406
}
1407

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

1426
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1427

1428
void mndSetStop(SMnode *pMnode) {
396,221✔
1429
  (void)taosThreadRwlockWrlock(&pMnode->lock);
396,221✔
1430
  pMnode->stopped = true;
396,221✔
1431
  (void)taosThreadRwlockUnlock(&pMnode->lock);
396,221✔
1432
  mInfo("mnode set stopped");
396,221✔
1433
}
396,221✔
1434

1435
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
521,567,203✔
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