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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

78.41
/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 "mndFunc.h"
29
#include "mndGrant.h"
30
#include "mndIndex.h"
31
#include "mndInfoSchema.h"
32
#include "mndMnode.h"
33
#include "mndMount.h"
34
#include "mndPerfSchema.h"
35
#include "mndPrivilege.h"
36
#include "mndProfile.h"
37
#include "mndQnode.h"
38
#include "mndQuery.h"
39
#include "mndRetention.h"
40
#include "mndRetentionDetail.h"
41
#include "mndRsma.h"
42
#include "mndScan.h"
43
#include "mndScanDetail.h"
44
#include "mndShow.h"
45
#include "mndSma.h"
46
#include "mndSnode.h"
47
#include "mndSsMigrate.h"
48
#include "mndStb.h"
49
#include "mndStream.h"
50
#include "mndSubscribe.h"
51
#include "mndSync.h"
52
#include "mndTelem.h"
53
#include "mndTopic.h"
54
#include "mndTrans.h"
55
#include "mndUser.h"
56
#include "mndVgroup.h"
57
#include "mndView.h"
58

59
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
4,367,429✔
60
  int32_t code = 0;
4,367,429✔
61
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,367,429✔
62
  if (pMnode->stopped) {
4,367,429✔
63
    code = TSDB_CODE_APP_IS_STOPPING;
×
64
  } else if (!mndIsLeader(pMnode)) {
4,367,429✔
65
    code = 1;
×
66
  } else {
67
#if 1
68
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
4,367,429✔
69
#else
70
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
71
    mTrace("mnode rpc is acquired, ref:%d", ref);
72
#endif
73
  }
74
  (void)taosThreadRwlockUnlock(&pMnode->lock);
4,367,429✔
75
  TAOS_RETURN(code);
4,367,429✔
76
}
77

78
static inline void mndReleaseRpc(SMnode *pMnode) {
237,204,474✔
79
  (void)taosThreadRwlockRdlock(&pMnode->lock);
237,204,474✔
80
#if 1
81
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
237,205,201✔
82
#else
83
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
84
  mTrace("mnode rpc is released, ref:%d", ref);
85
#endif
86
  (void)taosThreadRwlockUnlock(&pMnode->lock);
237,205,126✔
87
}
237,205,967✔
88

89
static void *mndBuildTimerMsg(int32_t *pContLen) {
50,732,496✔
90
  terrno = 0;
50,732,496✔
91
  SMTimerReq timerReq = {0};
50,733,702✔
92

93
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
50,733,301✔
94
  if (contLen <= 0) return NULL;
50,729,596✔
95
  void *pReq = rpcMallocCont(contLen);
50,729,596✔
96
  if (pReq == NULL) return NULL;
50,731,723✔
97

98
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
50,731,723✔
99
    mError("failed to serialize timer msg since %s", terrstr());
×
100
  }
101
  *pContLen = contLen;
50,732,675✔
102
  return pReq;
50,731,777✔
103
}
104

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

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

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

144
static void mndPullupTtl(SMnode *pMnode) {
2,166,845✔
145
  mTrace("pullup ttl");
2,166,845✔
146
  int32_t contLen = 0;
2,166,845✔
147
  void   *pReq = mndBuildTimerMsg(&contLen);
2,166,845✔
148
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
2,166,845✔
149
  // TODO check return value
150
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,166,845✔
151
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
152
  }
153
}
2,166,845✔
154

UNCOV
155
static void mndPullupTrimDb(SMnode *pMnode) {
×
UNCOV
156
  mTrace("pullup trim");
×
UNCOV
157
  int32_t contLen = 0;
×
UNCOV
158
  void   *pReq = mndBuildTimerMsg(&contLen);
×
UNCOV
159
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
160
  // TODO check return value
UNCOV
161
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
162
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
163
  }
UNCOV
164
}
×
165

166
static void mndPullupQueryTrimDb(SMnode *pMnode) {
2,177,353✔
167
  mTrace("pullup trim query");
2,177,353✔
168
  int32_t contLen = 0;
2,177,353✔
169
  void   *pReq = mndBuildTimerMsg(&contLen);
2,177,353✔
170
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_QUERY_TRIM_TIMER, .pCont = pReq, .contLen = contLen};
2,177,353✔
171
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
2,177,353✔
172
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
173
  }
174
}
2,177,353✔
175

176
static void mndPullupSsMigrateDb(SMnode *pMnode) {
×
177
  if (grantCheck(TSDB_GRANT_SHARED_STORAGE) != TSDB_CODE_SUCCESS) {
×
178
    return;
×
179
  }
180

181
  mTrace("pullup ssmigrate db");
×
182
  int32_t contLen = 0;
×
183
  void   *pReq = mndBuildTimerMsg(&contLen);
×
184
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_SSMIGRATE_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
185
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
186
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
187
  }
188
}
189

190
static void mndPullupUpdateSsMigrateProgress(SMnode *pMnode) {
×
191
  mTrace("pullup update ssmigrate progress");
×
192
  int32_t contLen = 0;
×
193
  void   *pReq = mndBuildTimerMsg(&contLen);
×
194
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER, .pCont = pReq, .contLen = contLen};
×
195
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
196
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
197
  }
198
}
×
199

200
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
11,201,472✔
201
  mTrace("pullup arb hb");
11,201,472✔
202
  int32_t contLen = 0;
11,201,472✔
203
  void   *pReq = mndBuildTimerMsg(&contLen);
11,201,472✔
204
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
11,201,472✔
205
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
11,201,472✔
206
}
207

208
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
7,402,066✔
209
  mTrace("pullup arb sync");
7,402,066✔
210
  int32_t contLen = 0;
7,402,066✔
211
  void   *pReq = mndBuildTimerMsg(&contLen);
7,402,066✔
212
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
7,402,066✔
213
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
7,402,066✔
214
}
215

216
static void mndCalMqRebalance(SMnode *pMnode) {
11,199,271✔
217
  int32_t contLen = 0;
11,199,271✔
218
  void   *pReq = mndBuildTimerMsg(&contLen);
11,199,271✔
219
  if (pReq != NULL) {
11,199,271✔
220
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
11,199,271✔
221
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
11,199,271✔
222
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
223
    }
224
  }
225
}
11,199,271✔
226

227
static void mndPullupTelem(SMnode *pMnode) {
266✔
228
  mTrace("pullup telem msg");
266✔
229
  int32_t contLen = 0;
266✔
230
  void   *pReq = mndBuildTimerMsg(&contLen);
266✔
231
  if (pReq != NULL) {
266✔
232
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
266✔
233
    // TODO check return value
234
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
266✔
235
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
236
    }
237
  }
238
}
266✔
239

240
static void mndPullupGrant(SMnode *pMnode) {
1,225,113✔
241
  mTrace("pullup grant msg");
1,225,113✔
242
  int32_t contLen = 0;
1,225,113✔
243
  void   *pReq = mndBuildTimerMsg(&contLen);
1,225,113✔
244
  if (pReq != NULL) {
1,225,113✔
245
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
1,225,113✔
246
                      .pCont = pReq,
247
                      .contLen = contLen,
248
                      .info.notFreeAhandle = 1,
249
                      .info.ahandle = 0};
250
    // TODO check return value
251
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,225,113✔
252
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
253
    }
254
  }
255
}
1,225,113✔
256

257
static void mndPullupAuth(SMnode *pMnode) {
×
258
  mTrace("pullup auth msg");
×
259
  int32_t contLen = 0;
×
260
  void   *pReq = mndBuildTimerMsg(&contLen);
×
261
  if (pReq != NULL) {
×
262
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_AUTH_HB_TIMER, .pCont = pReq, .contLen = contLen, .info.notFreeAhandle = 1, .info.ahandle = 0};
×
263
    // TODO check return value
264
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
265
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
266
    }
267
  }
268
}
×
269

270
static void mndIncreaseUpTime(SMnode *pMnode) {
18,419✔
271
  mTrace("increate uptime");
18,419✔
272
  int32_t contLen = 0;
18,419✔
273
  void   *pReq = mndBuildTimerMsg(&contLen);
18,419✔
274
  if (pReq != NULL) {
18,419✔
275
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
18,419✔
276
                      .pCont = pReq,
277
                      .contLen = contLen,
278
                      .info.notFreeAhandle = 1,
279
                      .info.ahandle = 0};
280
    // TODO check return value
281
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
18,419✔
282
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
283
    }
284
  }
285
}
18,419✔
286

287
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
155,097✔
288
  SSdb *pSdb = pMnode->pSdb;
155,097✔
289

290
  void *pIter = NULL;
155,097✔
291
  while (1) {
745,236✔
292
    SVgObj *pVgroup = NULL;
900,333✔
293
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
900,333✔
294
    if (pIter == NULL) break;
900,333✔
295

296
    bool stateChanged = false;
745,236✔
297
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
1,932,886✔
298
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
1,522,823✔
299
      if (pGid->dnodeId == dnodeId) {
1,522,823✔
300
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
335,173✔
301
          mInfo(
138,837✔
302
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:offline "
303
              "restored:0 "
304
              "canRead:0",
305
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
306
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
138,837✔
307
          pGid->syncRestore = 0;
138,837✔
308
          pGid->syncCanRead = 0;
138,837✔
309
          pGid->startTimeMs = 0;
138,837✔
310
          stateChanged = true;
138,837✔
311
        }
312
        break;
335,173✔
313
      }
314
    }
315

316
    if (stateChanged) {
745,236✔
317
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
138,837✔
318
      if (pDb != NULL && pDb->stateTs != curMs) {
138,837✔
319
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
75,239✔
320
              curMs);
321
        pDb->stateTs = curMs;
75,239✔
322
      }
323
      mndReleaseDb(pMnode, pDb);
138,837✔
324
    }
325

326
    sdbRelease(pSdb, pVgroup);
745,236✔
327
  }
328
}
155,097✔
329

330
static void mndCheckDnodeOffline(SMnode *pMnode) {
4,367,296✔
331
  mTrace("check dnode offline");
4,367,296✔
332
  if (mndAcquireRpc(pMnode) != 0) return;
4,367,296✔
333

334
  SSdb   *pSdb = pMnode->pSdb;
4,367,296✔
335
  int64_t curMs = taosGetTimestampMs();
4,367,296✔
336

337
  void *pIter = NULL;
4,367,296✔
338
  while (1) {
8,220,996✔
339
    SDnodeObj *pDnode = NULL;
12,588,292✔
340
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
12,588,292✔
341
    if (pIter == NULL) break;
12,588,292✔
342

343
    bool online = mndIsDnodeOnline(pDnode, curMs);
8,220,996✔
344
    if (!online) {
8,220,996✔
345
      mInfo("dnode:%d, in offline state", pDnode->id);
155,097✔
346
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
155,097✔
347
    }
348

349
    sdbRelease(pSdb, pDnode);
8,220,996✔
350
  }
351

352
  mndReleaseRpc(pMnode);
4,367,296✔
353
}
354

355
static bool mnodeIsNotLeader(SMnode *pMnode) {
272,762,400✔
356
  terrno = 0;
272,762,400✔
357
  (void)taosThreadRwlockRdlock(&pMnode->lock);
272,763,838✔
358
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
272,765,762✔
359
  if (terrno != 0) {
272,765,295✔
360
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
361
    return true;
×
362
  }
363

364
  if (state.state != TAOS_SYNC_STATE_LEADER) {
272,765,504✔
365
    (void)taosThreadRwlockUnlock(&pMnode->lock);
22,288,511✔
366
    terrno = TSDB_CODE_SYN_NOT_LEADER;
22,288,207✔
367
    return true;
22,288,511✔
368
  }
369
  if (!state.restored || !pMnode->restored) {
250,476,993✔
370
    (void)taosThreadRwlockUnlock(&pMnode->lock);
38,528✔
371
    terrno = TSDB_CODE_SYN_RESTORING;
37,660✔
372
    return true;
37,660✔
373
  }
374
  (void)taosThreadRwlockUnlock(&pMnode->lock);
250,438,932✔
375
  return false;
250,438,964✔
376
}
377

378
static int32_t minCronTime() {
×
379
  int32_t min = INT32_MAX;
×
380
  min = TMIN(min, tsTtlPushIntervalSec);
×
381
  min = TMIN(min, tsTrimVDbIntervalSec);
×
382
  min = TMIN(min, tsSsAutoMigrateIntervalSec);
×
383
  min = TMIN(min, tsTransPullupInterval);
×
384
  min = TMIN(min, tsCompactPullupInterval);
×
385
  min = TMIN(min, tsMqRebalanceInterval);
×
386

387
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
388
  min = TMIN(min, telemInt);
×
389
  min = TMIN(min, tsGrantHBInterval);
×
390
  min = TMIN(min, tsUptimeInterval);
×
391

392
  return min <= 1 ? 2 : min;
×
393
}
394
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
22,608,577✔
395
  int32_t code = 0;
22,608,577✔
396
#ifndef TD_ASTRA
397
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
22,608,577✔
398
    mndPullupGrant(pMnode);
1,225,113✔
399
  }
400
  if (sec % tsTtlPushIntervalSec == 0) {
22,608,577✔
401
    mndPullupTtl(pMnode);
2,166,845✔
402
  }
403

404
  if (sec % tsTrimVDbIntervalSec == 0) {
22,608,577✔
UNCOV
405
    mndPullupTrimDb(pMnode);
×
406
  }
407

408
  if (sec % tsQueryTrimIntervalSec == 0) {
22,608,577✔
409
    mndPullupQueryTrimDb(pMnode);
2,177,353✔
410
  }
411
#endif
412
#ifdef USE_SHARED_STORAGE
413
  if (tsSsEnabled) {
22,608,577✔
414
    if (sec % 10 == 0) { // TODO: make 10 to be configurable
×
415
      mndPullupUpdateSsMigrateProgress(pMnode);
×
416
    }
417
    if (tsSsEnabled == 2 && sec % tsSsAutoMigrateIntervalSec == 0) {
×
418
      mndPullupSsMigrateDb(pMnode);
×
419
    }
420
  }
421
#endif
422
#ifdef TD_ENTERPRISE
423
  if (tsAuthReq) {
22,608,577✔
424
    if (sec % tsAuthReqHBInterval == 0) {
×
425
      mndPullupAuth(pMnode);
×
426
    }
427
  }
428
#endif
429
  if (sec % tsTransPullupInterval == 0) {
22,608,577✔
430
    mndPullupTrans(pMnode);
11,200,135✔
431
  }
432

433
  if (sec % tsCompactPullupInterval == 0) {
22,608,577✔
434
    mndPullupCompacts(pMnode);
2,071,435✔
435
  }
436

437
  if (sec % tsScanPullupInterval == 0) {
22,608,577✔
438
    mndPullupScans(pMnode);
2,071,435✔
439
  }
440
#ifdef USE_TOPIC
441
  if (sec % tsMqRebalanceInterval == 0) {
22,608,577✔
442
    mndCalMqRebalance(pMnode);
11,199,271✔
443
  }
444
#endif
445
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
22,608,577✔
446
    mndPullupTelem(pMnode);
266✔
447
  }
448
  if (sec % tsUptimeInterval == 0) {
22,608,577✔
449
    mndIncreaseUpTime(pMnode);
18,419✔
450
  }
451
}
22,608,577✔
452

453
void mndDoArbTimerPullupTask(SMnode *pMnode, int64_t ms) {
227,831,477✔
454
  int32_t code = 0;
227,831,477✔
455
#ifndef TD_ASTRA
456
  if (ms % (tsArbHeartBeatIntervalMs) == 0) {
227,831,477✔
457
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
11,201,472✔
458
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
459
    }
460
  }
461

462
  if (ms % (tsArbCheckSyncIntervalMs) == 0) {
227,831,477✔
463
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
7,402,066✔
464
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
465
    }
466
  }
467
#endif
468
}
227,831,477✔
469

470
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
227,831,477✔
471
  if (ms % (tsStatusTimeoutMs) == 0) {
227,831,477✔
472
    mndCheckDnodeOffline(pMnode);
4,367,296✔
473
  }
474
}
227,831,477✔
475

476
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
22,608,577✔
477
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
22,608,577✔
478
    mndSyncCheckTimeout(pMnode);
602,529✔
479
  }
480
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
22,608,577✔
481
    msmHealthCheck(pMnode);
7,403,845✔
482
  }
483
}
22,608,577✔
484

485
static void *mndThreadSecFp(void *param) {
483,289✔
486
  SMnode *pMnode = param;
483,289✔
487
  int64_t lastTime = 0;
483,289✔
488
  setThreadName("mnode-timer");
483,289✔
489

490
  while (1) {
248,220,276✔
491
    lastTime++;
248,703,565✔
492
    taosMsleep(100);
248,703,565✔
493

494
    if (mndGetStop(pMnode)) break;
248,703,565✔
495
    if (lastTime % 10 != 0) continue;
248,220,276✔
496

497
    if (mnodeIsNotLeader(pMnode)) {
24,605,132✔
498
      mTrace("timer not process since mnode is not leader");
1,996,555✔
499
      continue;
1,996,555✔
500
    }
501

502
    int64_t sec = lastTime / 10;
22,608,577✔
503
    mndDoTimerCheckSync(pMnode, sec);
22,608,577✔
504

505
    mndDoTimerPullupTask(pMnode, sec);
22,608,577✔
506
  }
507

508
  return NULL;
483,289✔
509
}
510

511
static void *mndThreadMsFp(void *param) {
483,289✔
512
  SMnode *pMnode = param;
483,289✔
513
  int64_t lastTime = 0;
483,289✔
514
  setThreadName("mnode-arb-timer");
483,289✔
515

516
  while (1) {
517
    lastTime += 100;
248,644,382✔
518
    taosMsleep(100);
248,644,382✔
519

520
    if (mndGetStop(pMnode)) break;
248,644,382✔
521
    if (lastTime % 10 != 0) continue;
248,161,093✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
248,161,093✔
524
      mTrace("timer not process since mnode is not leader");
20,329,616✔
525
      continue;
20,329,616✔
526
    }
527

528
    mndDoTimerCheckStatus(pMnode, lastTime);
227,831,477✔
529

530
    mndDoArbTimerPullupTask(pMnode, lastTime);
227,831,477✔
531
  }
532

533
  return NULL;
483,289✔
534
}
535

536
static int32_t mndInitTimer(SMnode *pMnode) {
483,289✔
537
  int32_t      code = 0;
483,289✔
538
  TdThreadAttr thAttr;
480,677✔
539
  (void)taosThreadAttrInit(&thAttr);
483,289✔
540
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
483,289✔
541
#ifdef TD_COMPACT_OS
542
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
543
#endif
544
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadSecFp, pMnode)) != 0) {
483,289✔
545
    mError("failed to create timer thread since %s", tstrerror(code));
×
546
    TAOS_RETURN(code);
×
547
  }
548

549
  (void)taosThreadAttrDestroy(&thAttr);
483,289✔
550
  tmsgReportStartup("mnode-timer", "initialized");
483,289✔
551

552
  TdThreadAttr arbAttr;
480,677✔
553
  (void)taosThreadAttrInit(&arbAttr);
483,289✔
554
  (void)taosThreadAttrSetDetachState(&arbAttr, PTHREAD_CREATE_JOINABLE);
483,289✔
555
#ifdef TD_COMPACT_OS
556
  (void)taosThreadAttrSetStackSize(&arbAttr, STACK_SIZE_SMALL);
557
#endif
558
  if ((code = taosThreadCreate(&pMnode->arbThread, &arbAttr, mndThreadMsFp, pMnode)) != 0) {
483,289✔
559
    mError("failed to create arb timer thread since %s", tstrerror(code));
×
560
    TAOS_RETURN(code);
×
561
  }
562

563
  (void)taosThreadAttrDestroy(&arbAttr);
483,289✔
564
  tmsgReportStartup("mnode-timer", "initialized");
483,289✔
565
  TAOS_RETURN(code);
483,289✔
566
}
567

568
static void mndCleanupTimer(SMnode *pMnode) {
483,289✔
569
  if (taosCheckPthreadValid(pMnode->thread)) {
483,289✔
570
    (void)taosThreadJoin(pMnode->thread, NULL);
483,289✔
571
    taosThreadClear(&pMnode->thread);
483,289✔
572
  }
573
  if (taosCheckPthreadValid(pMnode->arbThread)) {
483,289✔
574
    (void)taosThreadJoin(pMnode->arbThread, NULL);
483,289✔
575
    taosThreadClear(&pMnode->arbThread);
483,289✔
576
  }
577
}
483,289✔
578

579
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
484,071✔
580
  int32_t code = 0;
484,071✔
581
  pMnode->path = taosStrdup(path);
484,071✔
582
  if (pMnode->path == NULL) {
484,071✔
583
    code = terrno;
×
584
    TAOS_RETURN(code);
×
585
  }
586

587
  if (taosMkDir(pMnode->path) != 0) {
484,071✔
588
    code = terrno;
×
589
    TAOS_RETURN(code);
×
590
  }
591

592
  TAOS_RETURN(code);
484,071✔
593
}
594

595
static int32_t mndInitWal(SMnode *pMnode) {
484,071✔
596
  int32_t code = 0;
484,071✔
597
  char    path[PATH_MAX + 20] = {0};
484,071✔
598
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
484,071✔
599
  SWalCfg cfg = {.vgId = 1,
484,071✔
600
                 .fsyncPeriod = 0,
601
                 .rollPeriod = -1,
602
                 .segSize = -1,
603
                 .committed = -1,
604
                 .retentionPeriod = 0,
605
                 .retentionSize = 0,
606
                 .level = TAOS_WAL_FSYNC,
607
                 .encryptAlgorithm = 0,
608
                 .encryptKey = {0}};
609

610
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
611
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
484,071✔
612
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
613
    if (tsEncryptKey[0] == '\0') {
×
614
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
615
      TAOS_RETURN(code);
×
616
    } else {
617
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
618
    }
619
  }
620
#endif
621

622
  pMnode->pWal = walOpen(path, &cfg);
484,071✔
623
  if (pMnode->pWal == NULL) {
484,071✔
624
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
625
    if (terrno != 0) code = terrno;
×
626
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
627
    TAOS_RETURN(code);
×
628
  }
629

630
  TAOS_RETURN(code);
484,071✔
631
}
632

633
static void mndCloseWal(SMnode *pMnode) {
483,289✔
634
  if (pMnode->pWal != NULL) {
483,289✔
635
    walClose(pMnode->pWal);
483,289✔
636
    pMnode->pWal = NULL;
483,289✔
637
  }
638
}
483,289✔
639

640
static int32_t mndInitSdb(SMnode *pMnode) {
484,071✔
641
  int32_t code = 0;
484,071✔
642
  SSdbOpt opt = {0};
484,071✔
643
  opt.path = pMnode->path;
484,071✔
644
  opt.pMnode = pMnode;
484,071✔
645
  opt.pWal = pMnode->pWal;
484,071✔
646

647
  pMnode->pSdb = sdbInit(&opt);
484,071✔
648
  if (pMnode->pSdb == NULL) {
484,071✔
649
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
650
    if (terrno != 0) code = terrno;
×
651
    TAOS_RETURN(code);
×
652
  }
653

654
  TAOS_RETURN(code);
484,071✔
655
}
656

657
static int32_t mndOpenSdb(SMnode *pMnode) {
484,071✔
658
  int32_t code = 0;
484,071✔
659
  if (!pMnode->deploy) {
484,071✔
660
    code = sdbReadFile(pMnode->pSdb);
170,036✔
661
  }
662

663
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
484,071✔
664

665
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
484,071✔
666
  return code;
484,071✔
667
}
668

669
static void mndCleanupSdb(SMnode *pMnode) {
483,289✔
670
  if (pMnode->pSdb) {
483,289✔
671
    sdbCleanup(pMnode->pSdb);
483,289✔
672
    pMnode->pSdb = NULL;
483,289✔
673
  }
674
}
483,289✔
675

676
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
21,783,195✔
677
  SMnodeStep step = {0};
21,783,195✔
678
  step.name = name;
21,783,195✔
679
  step.initFp = initFp;
21,783,195✔
680
  step.cleanupFp = cleanupFp;
21,783,195✔
681
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
43,566,390✔
682
    TAOS_RETURN(terrno);
×
683
  }
684

685
  TAOS_RETURN(0);
21,783,195✔
686
}
687

688
static int32_t mndInitSteps(SMnode *pMnode) {
484,071✔
689
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
484,071✔
690
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
484,071✔
691
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
484,071✔
692
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
484,071✔
693
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
484,071✔
694
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
484,071✔
695
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
484,071✔
696
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
484,071✔
697
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-bnode", mndInitBnode, mndCleanupBnode));
484,071✔
698
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
484,071✔
699
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
484,071✔
700
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
484,071✔
701
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
484,071✔
702
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
484,071✔
703
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
484,071✔
704
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
484,071✔
705
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
484,071✔
706
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
484,071✔
707
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
484,071✔
708
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
484,071✔
709
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
484,071✔
710
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
484,071✔
711
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
484,071✔
712
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
484,071✔
713
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
484,071✔
714
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
484,071✔
715
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
484,071✔
716
#ifdef USE_MOUNT
717
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount", mndInitMount, mndCleanupMount));
484,071✔
718
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mount-log", mndInitMountLog, mndCleanupMountLog));
484,071✔
719
#endif
720
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-rsma", mndInitRsma, mndCleanupRsma));
484,071✔
721
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
484,071✔
722
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
484,071✔
723
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
484,071✔
724
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan", mndInitScan, mndCleanupScan));
484,071✔
725
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention", mndInitRetention, mndCleanupRetention));
484,071✔
726
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
484,071✔
727
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-scan-detail", mndInitScanDetail, mndCleanupScanDetail));
484,071✔
728
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-retention-detail", mndInitRetentionDetail, mndCleanupRetentionDetail));
484,071✔
729
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-ssmigrate", mndInitSsMigrate, mndCleanupSsMigrate));
484,071✔
730
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
484,071✔
731
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
484,071✔
732
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
484,071✔
733
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
484,071✔
734
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
484,071✔
735
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
484,071✔
736
  return 0;
484,071✔
737
}
738

739
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
483,289✔
740
  if (pMnode->pSteps == NULL) return;
483,289✔
741

742
  if (pos == -1) {
483,289✔
743
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
483,289✔
744
  }
745

746
  for (int32_t s = pos; s >= 0; s--) {
22,231,294✔
747
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
21,748,005✔
748
    mInfo("%s will cleanup", pStep->name);
21,748,005✔
749
    if (pStep->cleanupFp != NULL) {
21,748,005✔
750
      (*pStep->cleanupFp)(pMnode);
20,781,427✔
751
    }
752
  }
753

754
  taosArrayClear(pMnode->pSteps);
483,289✔
755
  taosArrayDestroy(pMnode->pSteps);
483,289✔
756
  pMnode->pSteps = NULL;
483,289✔
757
}
758

759
static int32_t mndExecSteps(SMnode *pMnode) {
484,071✔
760
  int32_t code = 0;
484,071✔
761
  int32_t size = taosArrayGetSize(pMnode->pSteps);
484,071✔
762
  for (int32_t pos = 0; pos < size; pos++) {
22,267,266✔
763
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
21,783,195✔
764
    if (pStep->initFp == NULL) continue;
21,783,195✔
765

766
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
21,783,195✔
767
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
768
      mndCleanupSteps(pMnode, pos);
×
769
      TAOS_RETURN(code);
×
770
    } else {
771
      mInfo("%s is initialized", pStep->name);
21,783,195✔
772
      tmsgReportStartup(pStep->name, "initialized");
21,783,195✔
773
    }
774
  }
775

776
  pMnode->clusterId = mndGetClusterId(pMnode);
484,071✔
777
  TAOS_RETURN(0);
484,071✔
778
}
779

780
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
484,071✔
781
  pMnode->msgCb = pOption->msgCb;
484,071✔
782
  pMnode->selfDnodeId = pOption->dnodeId;
484,071✔
783
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
484,071✔
784
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
484,071✔
785
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
484,071✔
786
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
484,071✔
787
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
484,071✔
788
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
484,071✔
789
}
484,071✔
790

791
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
484,071✔
792
  terrno = 0;
484,071✔
793
  mInfo("start to open mnode in %s", path);
484,071✔
794

795
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
484,071✔
796
  if (pMnode == NULL) {
484,071✔
797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
798
    mError("failed to open mnode in step 1, since %s", terrstr());
×
799
    return NULL;
×
800
  }
801
  (void)memset(pMnode, 0, sizeof(SMnode));
484,071✔
802

803
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
484,071✔
804
  if (code != 0) {
484,071✔
805
    taosMemoryFree(pMnode);
×
806
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
807
    terrno = code;
×
808
    return NULL;
×
809
  }
810

811
  char timestr[24] = "1970-01-01 00:00:00.00";
484,071✔
812
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
484,071✔
813
  if (code < 0) {
484,071✔
814
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
815
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
816
    taosMemoryFree(pMnode);
×
817
    terrno = code;
×
818
    return NULL;
×
819
  }
820
  mndSetOptions(pMnode, pOption);
484,071✔
821

822
  pMnode->deploy = pOption->deploy;
484,071✔
823
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
484,071✔
824
  if (pMnode->pSteps == NULL) {
484,071✔
825
    taosMemoryFree(pMnode);
×
826
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
827
    mError("failed to open mnode in step 4, since %s", terrstr());
×
828
    return NULL;
×
829
  }
830

831
  code = mndCreateDir(pMnode, path);
484,071✔
832
  if (code != 0) {
484,071✔
833
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
834
    mndClose(pMnode);
×
835
    terrno = code;
×
836
    return NULL;
×
837
  }
838

839
  code = mndInitSteps(pMnode);
484,071✔
840
  if (code != 0) {
484,071✔
841
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
842
    mndClose(pMnode);
×
843
    terrno = code;
×
844
    return NULL;
×
845
  }
846

847
  code = mndExecSteps(pMnode);
484,071✔
848
  if (code != 0) {
484,071✔
849
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
850
    mndClose(pMnode);
×
851
    terrno = code;
×
852
    return NULL;
×
853
  }
854

855
  mInfo("mnode open successfully");
484,071✔
856
  return pMnode;
484,071✔
857
}
858

859
void mndPreClose(SMnode *pMnode) {
483,289✔
860
  if (pMnode != NULL) {
483,289✔
861
    int32_t code = 0;
483,289✔
862
    // TODO check return value
863
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
483,289✔
864
    if (code < 0) {
483,289✔
865
      mError("failed to transfer leader since %s", tstrerror(code));
×
866
    }
867
    syncPreStop(pMnode->syncMgmt.sync);
483,289✔
868
    code = sdbWriteFile(pMnode->pSdb, 0);
483,289✔
869
    if (code < 0) {
483,289✔
870
      mError("failed to write sdb since %s", tstrerror(code));
734✔
871
    }
872
  }
873
}
483,289✔
874

875
void mndClose(SMnode *pMnode) {
483,289✔
876
  if (pMnode != NULL) {
483,289✔
877
    mInfo("start to close mnode");
483,289✔
878
    mndCleanupSteps(pMnode, -1);
483,289✔
879
    taosMemoryFreeClear(pMnode->path);
483,289✔
880
    taosMemoryFreeClear(pMnode);
483,289✔
881
    mInfo("mnode is closed");
483,289✔
882
  }
883
}
483,289✔
884

885
int32_t mndStart(SMnode *pMnode) {
483,289✔
886
  mndSyncStart(pMnode);
483,289✔
887
  if (pMnode->deploy) {
483,289✔
888
    if (sdbDeploy(pMnode->pSdb) != 0) {
314,035✔
889
      mError("failed to deploy sdb while start mnode");
×
890
      return -1;
×
891
    }
892
    mndSetRestored(pMnode, true);
314,035✔
893
  }
894
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
483,289✔
895

896
  return mndInitTimer(pMnode);
483,289✔
897
}
898

899
int32_t mndIsCatchUp(SMnode *pMnode) {
366,201✔
900
  int64_t rid = pMnode->syncMgmt.sync;
366,201✔
901
  return syncIsCatchUp(rid);
366,201✔
902
}
903

904
ESyncRole mndGetRole(SMnode *pMnode) {
366,201✔
905
  int64_t rid = pMnode->syncMgmt.sync;
366,201✔
906
  return syncGetRole(rid);
366,201✔
907
}
908

909
int64_t mndGetTerm(SMnode *pMnode) {
7,483,617✔
910
  int64_t rid = pMnode->syncMgmt.sync;
7,483,617✔
911
  return syncGetTerm(rid);
7,483,617✔
912
}
913

914
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
18,685,876✔
915

916
void mndStop(SMnode *pMnode) {
483,289✔
917
  mndSetStop(pMnode);
483,289✔
918
  mndSyncStop(pMnode);
483,289✔
919
  mndCleanupTimer(pMnode);
483,289✔
920
}
483,289✔
921

922
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
61,959,432✔
923
  SMnode    *pMnode = pMsg->info.node;
61,959,432✔
924
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
61,959,432✔
925

926
  const STraceId *trace = &pMsg->info.traceId;
61,959,432✔
927
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
61,959,432✔
928

929
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
61,959,432✔
930
  if (code != 0) {
61,959,432✔
931
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
1,619✔
932
            tstrerror(code), code);
933
  }
934

935
  return code;
61,959,432✔
936
}
937

938
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
237,508,876✔
939
  int32_t code = 0;
237,508,876✔
940
  if (!IsReq(pMsg)) TAOS_RETURN(code);
237,508,876✔
941
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
205,966,274✔
942
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
200,731,160✔
943
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
195,836,801✔
944
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
185,146,805✔
945
    TAOS_RETURN(code);
20,821,772✔
946
  }
947

948
  SMnode *pMnode = pMsg->info.node;
185,147,302✔
949
  (void)taosThreadRwlockRdlock(&pMnode->lock);
185,150,524✔
950
  if (pMnode->stopped) {
185,151,640✔
951
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,400✔
952
    code = TSDB_CODE_APP_IS_STOPPING;
5,400✔
953
    TAOS_RETURN(code);
5,400✔
954
  }
955

956
  terrno = 0;
185,146,711✔
957
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
185,145,000✔
958
  if (terrno != 0) {
185,148,743✔
959
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
960
    code = terrno;
×
961
    TAOS_RETURN(code);
×
962
  }
963

964
  if (state.state != TAOS_SYNC_STATE_LEADER) {
185,148,727✔
965
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,353,388✔
966
    code = TSDB_CODE_SYN_NOT_LEADER;
4,353,388✔
967
    goto _OVER;
4,353,388✔
968
  }
969

970
  if (!state.restored || !pMnode->restored) {
180,795,339✔
971
    (void)taosThreadRwlockUnlock(&pMnode->lock);
324,869✔
972
    code = TSDB_CODE_SYN_RESTORING;
323,787✔
973
    goto _OVER;
323,787✔
974
  }
975

976
#if 1
977
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
180,470,470✔
978
#else
979
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
980
  mTrace("mnode rpc is acquired, ref:%d", ref);
981
#endif
982

983
  (void)taosThreadRwlockUnlock(&pMnode->lock);
180,470,417✔
984
  TAOS_RETURN(code);
180,470,555✔
985

986
_OVER:
4,677,175✔
987
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
4,677,175✔
988
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
4,677,175✔
989
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
4,677,175✔
990
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
4,677,175✔
991
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
4,675,025✔
992
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
4,677,175✔
993
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
4,677,175✔
994
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
4,676,108✔
995
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
4,676,887✔
UNCOV
996
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
997
           pMnode->stopped, state.restored, syncStr(state.state));
UNCOV
998
    TAOS_RETURN(code);
×
999
  }
1000

1001
  const STraceId *trace = &pMsg->info.traceId;
4,675,547✔
1002
  SEpSet          epSet = {0};
4,676,396✔
1003
  mndGetMnodeEpSet(pMnode, &epSet);
4,676,396✔
1004

1005
  mGDebug(
4,677,175✔
1006
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1007
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1008
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1009
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1010

1011
  if (epSet.numOfEps <= 0) return -1;
4,677,175✔
1012

1013
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
17,969,331✔
1014
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
13,292,156✔
1015
  }
1016

1017
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
4,677,175✔
1018
  pMsg->info.rsp = rpcMallocCont(contLen);
4,676,323✔
1019
  if (pMsg->info.rsp != NULL) {
4,676,887✔
1020
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
4,677,175✔
1021
      mError("failed to serialize ep set");
×
1022
    }
1023
    pMsg->info.hasEpSet = 1;
4,674,902✔
1024
    pMsg->info.rspLen = contLen;
4,675,478✔
1025
  }
1026

1027
  TAOS_RETURN(code);
4,675,660✔
1028
}
1029

1030
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
237,516,627✔
1031
  SMnode         *pMnode = pMsg->info.node;
237,516,627✔
1032
  const STraceId *trace = &pMsg->info.traceId;
237,518,479✔
1033
  int32_t         code = TSDB_CODE_SUCCESS;
237,509,309✔
1034

1035
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
237,509,309✔
1036
  MndMsgFpExt fpExt = NULL;
237,517,095✔
1037
  if (fp == NULL) {
237,517,095✔
1038
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
20,824,081✔
1039
    if (fpExt == NULL) {
20,824,081✔
1040
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1041
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1042
      TAOS_RETURN(code);
×
1043
    }
1044
  }
1045

1046
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
237,517,095✔
1047

1048
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
232,837,856✔
1049
  if (fp)
232,837,749✔
1050
    code = (*fp)(pMsg);
212,014,024✔
1051
  else
1052
    code = (*fpExt)(pMsg, pQueueInfo);
20,823,725✔
1053
  mndReleaseRpc(pMnode);
232,837,376✔
1054

1055
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
232,836,132✔
1056
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
31,795,254✔
1057
  } else if (code == 0) {
201,040,878✔
1058
    mGTrace("msg:%p, successfully processed", pMsg);
198,397,487✔
1059
  } else {
1060
    // TODO removve this wrong set code
1061
    if (code == -1) {
2,643,391✔
1062
      code = terrno;
3,148✔
1063
    }
1064
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,643,391✔
1065
            TMSG_INFO(pMsg->msgType));
1066
  }
1067

1068
  TAOS_RETURN(code);
232,836,132✔
1069
}
1070

1071
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
93,425,703✔
1072
  tmsg_t type = TMSG_INDEX(msgType);
93,425,703✔
1073
  if (type < TDMT_MAX) {
93,425,703✔
1074
    pMnode->msgFp[type] = fp;
93,425,703✔
1075
  }
1076
}
93,425,703✔
1077

1078
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,872,568✔
1079
  tmsg_t type = TMSG_INDEX(msgType);
3,872,568✔
1080
  if (type < TDMT_MAX) {
3,872,568✔
1081
    pMnode->msgFpExt[type] = fp;
3,872,568✔
1082
  }
1083
}
3,872,568✔
1084

1085
// Note: uid 0 is reserved
1086
int64_t mndGenerateUid(const char *name, int32_t len) {
9,602,923✔
1087
  int32_t hashval = MurmurHash3_32(name, len);
9,602,923✔
1088
  do {
×
1089
    int64_t us = taosGetTimestampUs();
9,602,923✔
1090
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,602,923✔
1091
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,602,923✔
1092
    if (uuid) {
9,602,923✔
1093
      return llabs(uuid);
9,602,923✔
1094
    }
1095
  } while (true);
1096
}
1097

1098
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
133✔
1099
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1100
  int32_t code = mndAcquireRpc(pMnode);
133✔
1101
  if (code < 0) {
133✔
1102
    TAOS_RETURN(code);
×
1103
  } else if (code == 1) {
133✔
1104
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1105
  }
1106

1107
  SSdb   *pSdb = pMnode->pSdb;
133✔
1108
  int64_t ms = taosGetTimestampMs();
133✔
1109

1110
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
133✔
1111
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
133✔
1112
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
133✔
1113
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
133✔
1114
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
133✔
1115
      pStbInfo->stbs == NULL) {
133✔
1116
    mndReleaseRpc(pMnode);
×
1117
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1118
    if (terrno != 0) code = terrno;
×
1119
    TAOS_RETURN(code);
×
1120
  }
1121

1122
  // cluster info
1123
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
133✔
1124
  pClusterInfo->monitor_interval = tsMonitorInterval;
133✔
1125
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
133✔
1126
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
133✔
1127
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
133✔
1128
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
133✔
1129
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
133✔
1130

1131
  void *pIter = NULL;
133✔
1132
  while (1) {
133✔
1133
    SDnodeObj *pObj = NULL;
266✔
1134
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
266✔
1135
    if (pIter == NULL) break;
266✔
1136

1137
    SMonDnodeDesc desc = {0};
133✔
1138
    desc.dnode_id = pObj->id;
133✔
1139
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
133✔
1140
    if (mndIsDnodeOnline(pObj, ms)) {
133✔
1141
      tstrncpy(desc.status, "ready", sizeof(desc.status));
133✔
1142
    } else {
1143
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1144
    }
1145
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
266✔
1146
      mError("failed put dnode into array, but continue at this monitor report")
×
1147
    }
1148
    sdbRelease(pSdb, pObj);
133✔
1149
  }
1150

1151
  pIter = NULL;
133✔
1152
  while (1) {
133✔
1153
    SMnodeObj *pObj = NULL;
266✔
1154
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
266✔
1155
    if (pIter == NULL) break;
266✔
1156

1157
    SMonMnodeDesc desc = {0};
133✔
1158
    desc.mnode_id = pObj->id;
133✔
1159
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
133✔
1160

1161
    if (pObj->id == pMnode->selfDnodeId) {
133✔
1162
      pClusterInfo->first_ep_dnode_id = pObj->id;
133✔
1163
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
133✔
1164
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1165
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
133✔
1166
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1167
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
133✔
1168
      desc.syncState = TAOS_SYNC_STATE_LEADER;
133✔
1169
    } else {
1170
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1171
      desc.syncState = pObj->syncState;
×
1172
    }
1173
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
266✔
1174
      mError("failed to put mnode into array, but continue at this monitor report");
×
1175
    }
1176
    sdbRelease(pSdb, pObj);
133✔
1177
  }
1178

1179
  // vgroup info
1180
  pIter = NULL;
133✔
1181
  while (1) {
266✔
1182
    SVgObj *pVgroup = NULL;
399✔
1183
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
399✔
1184
    if (pIter == NULL) break;
399✔
1185

1186
    if (pVgroup->mountVgId) {
266✔
1187
      sdbRelease(pSdb, pVgroup);
×
1188
      continue;
×
1189
    }
1190

1191
    pClusterInfo->vgroups_total++;
266✔
1192
    pClusterInfo->tbs_total += pVgroup->numOfTables;
266✔
1193

1194
    SMonVgroupDesc desc = {0};
266✔
1195
    desc.vgroup_id = pVgroup->vgId;
266✔
1196

1197
    SName name = {0};
266✔
1198
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
266✔
1199
    if (code < 0) {
266✔
1200
      mError("failed to get db name since %s", tstrerror(code));
×
1201
      sdbRelease(pSdb, pVgroup);
×
1202
      TAOS_RETURN(code);
×
1203
    }
1204
    (void)tNameGetDbName(&name, desc.database_name);
266✔
1205

1206
    desc.tables_num = pVgroup->numOfTables;
266✔
1207
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
266✔
1208
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
266✔
1209
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
532✔
1210
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
266✔
1211
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
266✔
1212
      pVnDesc->dnode_id = pVgid->dnodeId;
266✔
1213
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
266✔
1214
      pVnDesc->syncState = pVgid->syncState;
266✔
1215
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
266✔
1216
        tstrncpy(desc.status, "ready", sizeof(desc.status));
266✔
1217
        pClusterInfo->vgroups_alive++;
266✔
1218
      }
1219
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
266✔
1220
        pClusterInfo->vnodes_alive++;
266✔
1221
      }
1222
      pClusterInfo->vnodes_total++;
266✔
1223
    }
1224

1225
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
532✔
1226
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1227
    }
1228
    sdbRelease(pSdb, pVgroup);
266✔
1229
  }
1230

1231
  // stb info
1232
  pIter = NULL;
133✔
1233
  while (1) {
×
1234
    SStbObj *pStb = NULL;
133✔
1235
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
133✔
1236
    if (pIter == NULL) break;
133✔
1237

1238
    SMonStbDesc desc = {0};
×
1239

1240
    SName name1 = {0};
×
1241
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1242
    if (code < 0) {
×
1243
      mError("failed to get db name since %s", tstrerror(code));
×
1244
      sdbRelease(pSdb, pStb);
×
1245
      TAOS_RETURN(code);
×
1246
    }
1247
    (void)tNameGetDbName(&name1, desc.database_name);
×
1248

1249
    SName name2 = {0};
×
1250
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1251
    if (code < 0) {
×
1252
      mError("failed to get table name since %s", tstrerror(code));
×
1253
      sdbRelease(pSdb, pStb);
×
1254
      TAOS_RETURN(code);
×
1255
    }
1256
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1257

1258
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1259
      mError("failed to put stb into array, but continue at this monitor report");
×
1260
    }
1261
    sdbRelease(pSdb, pStb);
×
1262
  }
1263

1264
  // grant info
1265
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
133✔
1266
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
133✔
1267
  if (pMnode->grant.expireTimeMS == 0) {
133✔
1268
    pGrantInfo->expire_time = 0;
×
1269
    pGrantInfo->timeseries_total = 0;
×
1270
  }
1271

1272
  mndReleaseRpc(pMnode);
133✔
1273
  TAOS_RETURN(code);
133✔
1274
}
1275

1276
int32_t mndResetTimer(SMnode *pMnode){
×
1277
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1278
}
1279

1280
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
24,534,628✔
1281
  mTrace("mnode get load");
24,534,628✔
1282
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
24,534,628✔
1283
  pLoad->syncState = state.state;
24,534,628✔
1284
  pLoad->syncRestore = state.restored;
24,534,628✔
1285
  pLoad->syncTerm = state.term;
24,534,628✔
1286
  pLoad->roleTimeMs = state.roleTimeMs;
24,534,628✔
1287
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
24,534,628✔
1288
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1289
  return 0;
24,534,628✔
1290
}
1291

1292
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
7,402,066✔
1293
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
7,402,066✔
1294
  return state.roleTimeMs;
7,402,066✔
1295
}
1296

1297
void mndSetRestored(SMnode *pMnode, bool restored) {
483,289✔
1298
  if (restored) {
483,289✔
1299
    (void)taosThreadRwlockWrlock(&pMnode->lock);
483,289✔
1300
    pMnode->restored = true;
483,289✔
1301
    (void)taosThreadRwlockUnlock(&pMnode->lock);
483,289✔
1302
    mInfo("mnode set restored:%d", restored);
483,289✔
1303
  } else {
1304
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1305
    pMnode->restored = false;
×
1306
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1307
    mInfo("mnode set restored:%d", restored);
×
1308
    while (1) {
1309
      if (pMnode->rpcRef <= 0) break;
×
1310
      taosMsleep(3);
×
1311
    }
1312
  }
1313
}
483,289✔
1314

1315
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1316

1317
void mndSetStop(SMnode *pMnode) {
483,289✔
1318
  (void)taosThreadRwlockWrlock(&pMnode->lock);
483,289✔
1319
  pMnode->stopped = true;
483,289✔
1320
  (void)taosThreadRwlockUnlock(&pMnode->lock);
483,289✔
1321
  mInfo("mnode set stopped");
483,289✔
1322
}
483,289✔
1323

1324
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
497,336,458✔
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