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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
227,998,396✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
227,998,396✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
227,998,630✔
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);
227,998,607✔
93
}
227,998,896✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
55,560,092✔
96
  terrno = 0;
55,560,092✔
97
  SMTimerReq timerReq = {0};
55,560,049✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
55,560,049✔
100
  if (contLen <= 0) return NULL;
55,558,472✔
101
  void *pReq = rpcMallocCont(contLen);
55,558,472✔
102
  if (pReq == NULL) return NULL;
55,558,939✔
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
  void *pIter = NULL;
121,785✔
309
  while (1) {
348,092✔
310
    SVgObj *pVgroup = NULL;
469,877✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
469,877✔
312
    if (pIter == NULL) break;
469,877✔
313

314
    bool stateChanged = false;
348,092✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
902,338✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
699,069✔
317
      if (pGid->dnodeId == dnodeId) {
699,069✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
144,823✔
319
          mInfo(
57,702✔
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;
57,702✔
325
          pGid->syncRestore = 0;
57,702✔
326
          pGid->syncCanRead = 0;
57,702✔
327
          pGid->startTimeMs = 0;
57,702✔
328
          stateChanged = true;
57,702✔
329
        }
330
        break;
144,823✔
331
      }
332
    }
333

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

344
    sdbRelease(pSdb, pVgroup);
348,092✔
345
  }
346
}
121,785✔
347

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

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

355
  void *pIter = NULL;
4,421,154✔
356
  while (1) {
7,844,572✔
357
    SDnodeObj *pDnode = NULL;
12,265,726✔
358
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,265,726✔
359
    if (pIter == NULL) break;
12,265,726✔
360

361
    bool online = mndIsDnodeOnline(pDnode, curMs);
7,844,572✔
362
    if (!online) {
7,844,572✔
363
      mInfo("dnode:%d, in offline state", pDnode->id);
121,785✔
364
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
121,785✔
365
    }
366

367
    sdbRelease(pSdb, pDnode);
7,844,572✔
368
  }
369

370
  mndReleaseRpc(pMnode);
4,421,154✔
371
}
372

373
static bool mnodeIsNotLeader(SMnode *pMnode) {
263,781,362✔
374
  terrno = 0;
263,781,362✔
375
  (void)taosThreadRwlockRdlock(&pMnode->lock);
263,781,875✔
376
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
263,784,863✔
377
  if (terrno != 0) {
263,785,343✔
378
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
379
    return true;
×
380
  }
381

382
  if (state.state != TAOS_SYNC_STATE_LEADER) {
263,785,223✔
383
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,473,095✔
384
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,473,215✔
385
    return true;
11,473,215✔
386
  }
387
  if (!state.restored || !pMnode->restored) {
252,312,128✔
388
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,926✔
389
    terrno = TSDB_CODE_SYN_RESTORING;
1,884✔
390
    return true;
1,884✔
391
  }
392
  (void)taosThreadRwlockUnlock(&pMnode->lock);
252,310,202✔
393
  return false;
252,309,805✔
394
}
395

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

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

410
  return min <= 1 ? 2 : min;
×
411
}
412
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,789,038✔
413
  int32_t code = 0;
22,789,038✔
414
#ifndef TD_ASTRA
415
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,789,038✔
416
    mndPullupGrant(pMnode);
1,072,966✔
417
  }
418
  if (sec % tsTtlPushIntervalSec == 0) {
22,789,038✔
419
    mndPullupTtl(pMnode);
2,218,366✔
420
  }
421

422
  if (sec % tsTrimVDbIntervalSec == 0) {
22,789,038✔
UNCOV
423
    mndPullupTrimDb(pMnode);
×
424
  }
425

426
  if (sec % tsQueryTrimIntervalSec == 0) {
22,789,038✔
427
    mndPullupQueryTrimDb(pMnode);
2,183,701✔
428
  }
429
#endif
430
#ifdef USE_SHARED_STORAGE
431
  if (tsSsEnabled) {
22,789,038✔
432
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
433
      mndPullupUpdateSsMigrateProgress(pMnode);
×
434
    }
435
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
436
      mndPullupSsMigrateDb(pMnode);
×
437
    }
438
  }
439
#endif
440
#ifdef TD_ENTERPRISE
441
  if (tsAuthReq) {
22,789,038✔
442
    if (sec % tsAuthReqHBInterval == 0) {
×
443
      mndPullupAuth(pMnode);
×
444
    }
445
  }
446
#endif
447
  if (sec % tsTransPullupInterval == 0) {
22,789,038✔
448
    mndPullupTrans(pMnode);
11,306,798✔
449
  }
450

451
  if (sec % tsCompactPullupInterval == 0) {
22,789,038✔
452
    mndPullupCompacts(pMnode);
2,119,182✔
453
  }
454

455
  if (sec % tsScanPullupInterval == 0) {
22,789,038✔
456
    mndPullupScans(pMnode);
2,119,182✔
457
  }
458
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
22,789,038✔
459
    mndPullupInstances(pMnode);
4,420,983✔
460
  }
461
#ifdef USE_TOPIC
462
  if (sec % tsMqRebalanceInterval == 0) {
22,789,038✔
463
    mndCalMqRebalance(pMnode);
11,306,666✔
464
  }
465
#endif
466
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,789,038✔
467
    mndPullupTelem(pMnode);
120✔
468
  }
469
  if (sec % tsUptimeInterval == 0) {
22,789,038✔
470
    mndIncreaseUpTime(pMnode);
19,202✔
471
  }
472
}
22,789,038✔
473

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

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

491
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
229,521,206✔
492
  if (ms % (tsStatusTimeoutMs) == 0) {
229,521,206✔
493
    mndCheckDnodeOffline(pMnode);
4,421,154✔
494
  }
495
}
229,521,206✔
496

497
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,789,038✔
498
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,789,038✔
499
    mndSyncCheckTimeout(pMnode);
619,995✔
500
  }
501
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,789,038✔
502
    msmHealthCheck(pMnode);
7,484,322✔
503
  }
504
}
22,789,038✔
505

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

511
  while (1) {
240,010,352✔
512
    lastTime++;
240,396,127✔
513
    taosMsleep(100);
240,396,127✔
514

515
    if (mndGetStop(pMnode)) break;
240,396,127✔
516
    if (lastTime % 10 != 0) continue;
240,010,352✔
517

518
    if (mnodeIsNotLeader(pMnode)) {
23,819,441✔
519
      mTrace("timer not process since mnode is not leader");
1,030,403✔
520
      continue;
1,030,403✔
521
    }
522

523
    int64_t sec = lastTime / 10;
22,789,038✔
524
    mndDoTimerCheckSync(pMnode, sec);
22,789,038✔
525

526
    mndDoTimerPullupTask(pMnode, sec);
22,789,038✔
527
  }
528

529
  return NULL;
385,775✔
530
}
531

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

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

541
    if (mndGetStop(pMnode)) break;
240,351,677✔
542
    if (lastTime % 10 != 0) continue;
239,965,902✔
543

544
    if (mnodeIsNotLeader(pMnode)) {
239,965,902✔
545
      mTrace("timer not process since mnode is not leader");
10,444,696✔
546
      continue;
10,444,696✔
547
    }
548

549
    mndDoTimerCheckStatus(pMnode, lastTime);
229,521,206✔
550

551
    mndDoArbTimerPullupTask(pMnode, lastTime);
229,521,206✔
552
  }
553

554
  return NULL;
385,775✔
555
}
556

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

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

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

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

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

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

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

613
  TAOS_RETURN(code);
385,837✔
614
}
615

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

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

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

649
  TAOS_RETURN(code);
385,837✔
650
}
651

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

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

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

673
  TAOS_RETURN(code);
385,837✔
674
}
675

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

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

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

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

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

704
  TAOS_RETURN(0);
19,291,850✔
705
}
706

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

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

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

770
  for (int32_t s = pos; s >= 0; s--) {
19,674,525✔
771
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
19,288,750✔
772
    mInfo("%s will cleanup", pStep->name);
19,288,750✔
773
    if (pStep->cleanupFp != NULL) {
19,288,750✔
774
      (*pStep->cleanupFp)(pMnode);
18,517,200✔
775
    }
776
  }
777

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

783
static int32_t mndExecSteps(SMnode *pMnode) {
385,837✔
784
  int32_t code = 0;
385,837✔
785
  int32_t size = taosArrayGetSize(pMnode->pSteps);
385,837✔
786
  for (int32_t pos = 0; pos < size; pos++) {
19,677,687✔
787
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
19,291,850✔
788
    if (pStep->initFp == NULL) continue;
19,291,850✔
789

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

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

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

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

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

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

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

845
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
385,837✔
846
        pOption->numOfTotalReplicas);
847
  mndSetOptions(pMnode, pOption);
385,837✔
848

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

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

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

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

883
  mInfo("mnode open successfully");
385,837✔
884
  return pMnode;
385,837✔
885
}
886

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

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

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

931
  return mndInitTimer(pMnode);
385,775✔
932
}
933

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

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

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

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

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

953
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,902,796✔
954

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

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

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

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

974
  return code;
48,874,939✔
975
}
976

977
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
227,269,731✔
978
  int32_t code = 0;
227,269,731✔
979
  if (!IsReq(pMsg)) TAOS_RETURN(code);
227,269,731✔
980
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
198,715,478✔
981
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
194,331,477✔
982
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
190,654,475✔
983
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
181,957,500✔
984
    TAOS_RETURN(code);
16,765,083✔
985
  }
986

987
  SMnode *pMnode = pMsg->info.node;
181,952,411✔
988
  (void)taosThreadRwlockRdlock(&pMnode->lock);
181,961,463✔
989
  if (pMnode->stopped) {
181,959,320✔
990
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,485✔
991
    code = TSDB_CODE_APP_IS_STOPPING;
2,485✔
992
    TAOS_RETURN(code);
2,485✔
993
  }
994

995
  terrno = 0;
181,957,133✔
996
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
181,956,524✔
997
  if (terrno != 0) {
181,961,540✔
998
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
999
    code = terrno;
×
1000
    TAOS_RETURN(code);
×
1001
  }
1002

1003
  if (state.state != TAOS_SYNC_STATE_LEADER) {
181,961,540✔
1004
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,593,242✔
1005
    code = TSDB_CODE_SYN_NOT_LEADER;
1,593,242✔
1006
    goto _OVER;
1,593,242✔
1007
  }
1008

1009
  if (!state.restored || !pMnode->restored) {
180,368,298✔
1010
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,103,882✔
1011
    code = TSDB_CODE_SYN_RESTORING;
2,101,043✔
1012
    goto _OVER;
2,101,043✔
1013
  }
1014

1015
#if 1
1016
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
178,264,416✔
1017
#else
1018
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1019
  mTrace("mnode rpc is acquired, ref:%d", ref);
1020
#endif
1021

1022
  (void)taosThreadRwlockUnlock(&pMnode->lock);
178,266,543✔
1023
  TAOS_RETURN(code);
178,264,027✔
1024

1025
_OVER:
3,694,285✔
1026
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
3,694,285✔
1027
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
3,694,027✔
1028
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
3,693,769✔
1029
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
3,692,965✔
1030
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
3,693,683✔
1031
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,692,225✔
1032
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
3,693,683✔
1033
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
3,692,879✔
1034
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
3,693,597✔
1035
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
249✔
1036
           pMnode->stopped, state.restored, syncStr(state.state));
1037
    TAOS_RETURN(code);
249✔
1038
  }
1039

1040
  const STraceId *trace = &pMsg->info.traceId;
3,691,939✔
1041
  SEpSet          epSet = {0};
3,692,612✔
1042
  mndGetMnodeEpSet(pMnode, &epSet);
3,692,868✔
1043

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

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

1052
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
10,416,473✔
1053
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,722,876✔
1054
  }
1055

1056
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,693,597✔
1057
  pMsg->info.rsp = rpcMallocCont(contLen);
3,693,441✔
1058
  if (pMsg->info.rsp != NULL) {
3,693,202✔
1059
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,693,464✔
1060
      mError("failed to serialize ep set");
×
1061
    }
1062
    pMsg->info.hasEpSet = 1;
3,691,797✔
1063
    pMsg->info.rspLen = contLen;
3,692,255✔
1064
  }
1065

1066
  TAOS_RETURN(code);
3,692,966✔
1067
}
1068

1069
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
227,275,116✔
1070
  SMnode         *pMnode = pMsg->info.node;
227,275,116✔
1071
  const STraceId *trace = &pMsg->info.traceId;
227,275,364✔
1072
  int32_t         code = TSDB_CODE_SUCCESS;
227,275,238✔
1073

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

1096
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
227,258,184✔
1097
  MndMsgFpExt fpExt = NULL;
227,274,838✔
1098
  if (fp == NULL) {
227,274,838✔
1099
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
16,756,031✔
1100
    if (fpExt == NULL) {
16,755,900✔
1101
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1102
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1103
      TAOS_RETURN(code);
×
1104
    }
1105
  }
1106

1107
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
227,274,707✔
1108

1109
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
223,574,790✔
1110
  if (fp)
223,574,633✔
1111
    code = (*fp)(pMsg);
206,817,886✔
1112
  else
1113
    code = (*fpExt)(pMsg, pQueueInfo);
16,756,747✔
1114
  mndReleaseRpc(pMnode);
223,577,859✔
1115

1116
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
223,574,840✔
1117
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
26,794,591✔
1118
  } else if (code == 0) {
196,780,249✔
1119
    mGTrace("msg:%p, successfully processed", pMsg);
194,381,501✔
1120
  } else {
1121
    // TODO removve this wrong set code
1122
    if (code == -1) {
2,398,748✔
1123
      code = terrno;
×
1124
    }
1125
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,398,748✔
1126
            TMSG_INFO(pMsg->msgType));
1127
  }
1128

1129
  TAOS_RETURN(code);
223,574,840✔
1130
}
1131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1300
    SMonStbDesc desc = {0};
×
1301

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

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

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

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

1334
  mndReleaseRpc(pMnode);
60✔
1335
  TAOS_RETURN(code);
60✔
1336
}
1337

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

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

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

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

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

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

1386
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
480,737,803✔
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