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

taosdata / TDengine / #4929

16 Jan 2026 02:32AM UTC coverage: 66.723% (+0.02%) from 66.708%
#4929

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

539 existing lines in 127 files now uncovered.

203225 of 304580 relevant lines covered (66.72%)

128590777.46 hits per line

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

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

84
static inline void mndReleaseRpc(SMnode *pMnode) {
253,511,165✔
85
  (void)taosThreadRwlockRdlock(&pMnode->lock);
253,511,165✔
86
#if 1
87
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
253,512,408✔
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);
253,512,251✔
93
}
253,511,974✔
94

95
static void *mndBuildTimerMsg(int32_t *pContLen) {
64,830,140✔
96
  terrno = 0;
64,830,140✔
97
  SMTimerReq timerReq = {0};
64,830,140✔
98

99
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
64,830,140✔
100
  if (contLen <= 0) return NULL;
64,829,745✔
101
  void *pReq = rpcMallocCont(contLen);
64,829,745✔
102
  if (pReq == NULL) return NULL;
64,830,140✔
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
  void *pIter = NULL;
92,102✔
309
  while (1) {
358,944✔
310
    SVgObj *pVgroup = NULL;
451,046✔
311
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
451,046✔
312
    if (pIter == NULL) break;
451,046✔
313

314
    bool stateChanged = false;
358,944✔
315
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
907,214✔
316
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
704,783✔
317
      if (pGid->dnodeId == dnodeId) {
704,783✔
318
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
156,513✔
319
          mInfo(
57,951✔
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,951✔
325
          pGid->syncRestore = 0;
57,951✔
326
          pGid->syncCanRead = 0;
57,951✔
327
          pGid->startTimeMs = 0;
57,951✔
328
          pGid->learnerProgress = 0;
57,951✔
329
          pGid->snapSeq = -1;
57,951✔
330
          stateChanged = true;
57,951✔
331
        }
332
        break;
156,513✔
333
      }
334
    }
335

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

346
    sdbRelease(pSdb, pVgroup);
358,944✔
347
  }
348
}
92,102✔
349

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

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

357
  void *pIter = NULL;
4,952,820✔
358
  while (1) {
8,310,500✔
359
    SDnodeObj *pDnode = NULL;
13,263,320✔
360
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
13,263,320✔
361
    if (pIter == NULL) break;
13,263,320✔
362

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

369
    sdbRelease(pSdb, pDnode);
8,310,500✔
370
  }
371

372
  mndReleaseRpc(pMnode);
4,952,820✔
373
}
374

375
static bool mnodeIsNotLeader(SMnode *pMnode) {
294,420,381✔
376
  terrno = 0;
294,420,381✔
377
  (void)taosThreadRwlockRdlock(&pMnode->lock);
294,421,332✔
378
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
294,422,850✔
379
  if (terrno != 0) {
294,422,545✔
380
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
381
    return true;
×
382
  }
383

384
  if (state.state != TAOS_SYNC_STATE_LEADER) {
294,422,410✔
385
    (void)taosThreadRwlockUnlock(&pMnode->lock);
11,285,257✔
386
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,285,257✔
387
    return true;
11,285,257✔
388
  }
389
  if (!state.restored || !pMnode->restored) {
283,137,153✔
390
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,840✔
391
    terrno = TSDB_CODE_SYN_RESTORING;
1,161✔
392
    return true;
1,161✔
393
  }
394
  (void)taosThreadRwlockUnlock(&pMnode->lock);
283,135,654✔
395
  return false;
283,135,383✔
396
}
397

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

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

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

424
  if (sec % tsTrimVDbIntervalSec == 0) {
26,218,140✔
425
    mndPullupTrimDb(pMnode);
4,713✔
426
  }
427

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

453
  if (sec % tsCompactPullupInterval == 0) {
26,218,140✔
454
    mndPullupCompacts(pMnode);
2,638,846✔
455
  }
456

457
  if (sec % tsScanPullupInterval == 0) {
26,218,140✔
458
    mndPullupScans(pMnode);
2,638,926✔
459
  }
460
  if (tsInstancePullupInterval > 0 && sec % tsInstancePullupInterval == 0) {  // check instance expired
26,218,140✔
461
    mndPullupInstances(pMnode);
5,247,648✔
462
  }
463
#ifdef USE_TOPIC
464
  if (sec % tsMqRebalanceInterval == 0) {
26,218,140✔
465
    mndCalMqRebalance(pMnode);
13,118,778✔
466
  }
467
#endif
468
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
26,218,140✔
469
    mndPullupTelem(pMnode);
240✔
470
  }
471
  if (sec % tsUptimeInterval == 0) {
26,218,140✔
472
    mndIncreaseUpTime(pMnode);
90,138✔
473
  }
474
}
26,218,140✔
475

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

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

493
void mndDoTimerCheckStatus(SMnode *pMnode, int64_t ms) {
256,918,292✔
494
  if (ms % (tsStatusTimeoutMs) == 0) {
256,918,292✔
495
    mndCheckDnodeOffline(pMnode);
4,952,820✔
496
  }
497
}
256,918,292✔
498

499
void mndDoTimerCheckSync(SMnode *pMnode, int64_t sec) {
26,218,140✔
500
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
26,218,140✔
501
    mndSyncCheckTimeout(pMnode);
885,653✔
502
  }
503
  if (!tsDisableStream && (sec % MND_STREAM_HEALTH_CHECK_PERIOD_SEC == 0)) {
26,218,140✔
504
    msmHealthCheck(pMnode);
8,749,464✔
505
  }
506
}
26,218,140✔
507

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

513
  while (1) {
267,592,114✔
514
    if (mndGetStop(pMnode)) break;
268,005,915✔
515

516
    int64_t nowSec = taosGetTimestampMs() / 1000;
267,592,114✔
517
    if (nowSec == lastSec) {
267,592,114✔
518
      taosMsleep(100);
240,300,283✔
519
      continue;
240,300,283✔
520
    }
521
    lastSec = nowSec;
27,291,831✔
522

523
    if (mnodeIsNotLeader(pMnode)) {
27,291,831✔
524
      taosMsleep(100);
1,073,691✔
525
      mTrace("timer not process since mnode is not leader");
1,073,691✔
526
      continue;
1,073,691✔
527
    }
528

529
    mndDoTimerCheckSync(pMnode, nowSec);
26,218,140✔
530

531
    mndDoTimerPullupTask(pMnode, nowSec);
26,218,140✔
532

533
    taosMsleep(100);
26,218,140✔
534
  }
535

536
  return NULL;
413,801✔
537
}
538

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

544
  while (1) {
545
    lastTime += 100;
267,544,820✔
546
    taosMsleep(100);
267,544,820✔
547

548
    if (mndGetStop(pMnode)) break;
267,544,820✔
549
    if (lastTime % 10 != 0) continue;
267,131,019✔
550

551
    if (mnodeIsNotLeader(pMnode)) {
267,131,019✔
552
      mTrace("timer not process since mnode is not leader");
10,212,727✔
553
      continue;
10,212,727✔
554
    }
555

556
    mndDoTimerCheckStatus(pMnode, lastTime);
256,918,292✔
557

558
    mndDoArbTimerPullupTask(pMnode, lastTime);
256,918,292✔
559
  }
560

561
  return NULL;
413,801✔
562
}
563

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

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

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

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

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

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

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

620
  TAOS_RETURN(code);
413,861✔
621
}
622

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

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

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

656
  TAOS_RETURN(code);
413,861✔
657
}
658

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

666
static int32_t mndInitSdb(SMnode *pMnode) {
413,861✔
667
  int32_t code = 0;
413,861✔
668
  SSdbOpt opt = {0};
413,861✔
669
  opt.path = pMnode->path;
413,861✔
670
  opt.pMnode = pMnode;
413,861✔
671
  opt.pWal = pMnode->pWal;
413,861✔
672

673
  pMnode->pSdb = sdbInit(&opt);
413,861✔
674
  if (pMnode->pSdb == NULL) {
413,861✔
675
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
676
    if (terrno != 0) code = terrno;
×
677
    TAOS_RETURN(code);
×
678
  }
679

680
  TAOS_RETURN(code);
413,861✔
681
}
682

683
static int32_t mndOpenSdb(SMnode *pMnode) {
413,861✔
684
  int32_t code = 0;
413,861✔
685
  if (!pMnode->deploy) {
413,861✔
686
    code = sdbReadFile(pMnode->pSdb);
119,487✔
687
  }
688

689
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
413,861✔
690

691
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
413,861✔
692
  return code;
413,861✔
693
}
694

695
static void mndCleanupSdb(SMnode *pMnode) {
413,801✔
696
  if (pMnode->pSdb) {
413,801✔
697
    sdbCleanup(pMnode->pSdb);
413,801✔
698
    pMnode->pSdb = NULL;
413,801✔
699
  }
700
}
413,801✔
701

702
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
20,693,050✔
703
  SMnodeStep step = {0};
20,693,050✔
704
  step.name = name;
20,693,050✔
705
  step.initFp = initFp;
20,693,050✔
706
  step.cleanupFp = cleanupFp;
20,693,050✔
707
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
41,386,100✔
708
    TAOS_RETURN(terrno);
×
709
  }
710

711
  TAOS_RETURN(0);
20,693,050✔
712
}
713

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

770
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
413,801✔
771
  if (pMnode->pSteps == NULL) return;
413,801✔
772

773
  if (pos == -1) {
413,801✔
774
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
413,801✔
775
  }
776

777
  for (int32_t s = pos; s >= 0; s--) {
21,103,851✔
778
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
20,690,050✔
779
    mInfo("%s will cleanup", pStep->name);
20,690,050✔
780
    if (pStep->cleanupFp != NULL) {
20,690,050✔
781
      (*pStep->cleanupFp)(pMnode);
19,862,448✔
782
    }
783
  }
784

785
  taosArrayClear(pMnode->pSteps);
413,801✔
786
  taosArrayDestroy(pMnode->pSteps);
413,801✔
787
  pMnode->pSteps = NULL;
413,801✔
788
}
789

790
static int32_t mndExecSteps(SMnode *pMnode) {
413,861✔
791
  int32_t code = 0;
413,861✔
792
  int32_t size = taosArrayGetSize(pMnode->pSteps);
413,861✔
793
  for (int32_t pos = 0; pos < size; pos++) {
21,106,911✔
794
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
20,693,050✔
795
    if (pStep->initFp == NULL) continue;
20,693,050✔
796

797
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
20,693,050✔
798
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
799
      mndCleanupSteps(pMnode, pos);
×
800
      TAOS_RETURN(code);
×
801
    } else {
802
      mInfo("%s is initialized", pStep->name);
20,693,050✔
803
      tmsgReportStartup(pStep->name, "initialized");
20,693,050✔
804
    }
805
  }
806

807
  pMnode->clusterId = mndGetClusterId(pMnode);
413,861✔
808
  TAOS_RETURN(0);
413,861✔
809
}
810

811
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
413,861✔
812
  pMnode->msgCb = pOption->msgCb;
413,861✔
813
  pMnode->selfDnodeId = pOption->dnodeId;
413,861✔
814
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
413,861✔
815
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
413,861✔
816
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
413,861✔
817
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
413,861✔
818
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
413,861✔
819
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
413,861✔
820
}
413,861✔
821

822
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
413,861✔
823
  terrno = 0;
413,861✔
824
  mInfo("start to open mnode in %s", path);
413,861✔
825

826
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
413,861✔
827
  if (pMnode == NULL) {
413,861✔
828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
829
    mError("failed to open mnode in step 1, since %s", terrstr());
×
830
    return NULL;
×
831
  }
832
  (void)memset(pMnode, 0, sizeof(SMnode));
413,861✔
833

834
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
413,861✔
835
  if (code != 0) {
413,861✔
836
    taosMemoryFree(pMnode);
×
837
    mError("failed to open mnode in step 2, add lock, since %s", tstrerror(code));
×
838
    terrno = code;
×
839
    return NULL;
×
840
  }
841

842
  char timestr[24] = "1970-01-01 00:00:00.00";
413,861✔
843
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
413,861✔
844
  if (code < 0) {
413,861✔
845
    mError("failed to open mnode in step 3, parse time, since %s", tstrerror(code));
×
846
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
847
    taosMemoryFree(pMnode);
×
848
    terrno = code;
×
849
    return NULL;
×
850
  }
851

852
  mInfo("vgId:1, mnode set options to syncMgmt, dnodeId:%d, numOfTotalReplicas:%d", pOption->selfIndex,
413,861✔
853
        pOption->numOfTotalReplicas);
854
  mndSetOptions(pMnode, pOption);
413,861✔
855

856
  pMnode->deploy = pOption->deploy;
413,861✔
857
  pMnode->version = pOption->version;
413,861✔
858
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
413,861✔
859
  if (pMnode->pSteps == NULL) {
413,861✔
860
    taosMemoryFree(pMnode);
×
861
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
862
    mError("failed to open mnode in step 4, since %s", terrstr());
×
863
    return NULL;
×
864
  }
865

866
  code = mndCreateDir(pMnode, path);
413,861✔
867
  if (code != 0) {
413,861✔
868
    mError("failed to open mnode in step 5, since %s", tstrerror(code));
×
869
    mndClose(pMnode);
×
870
    terrno = code;
×
871
    return NULL;
×
872
  }
873

874
  code = mndInitSteps(pMnode);
413,861✔
875
  if (code != 0) {
413,861✔
876
    mError("failed to open mnode in step 6, since %s", tstrerror(code));
×
877
    mndClose(pMnode);
×
878
    terrno = code;
×
879
    return NULL;
×
880
  }
881

882
  code = mndExecSteps(pMnode);
413,861✔
883
  if (code != 0) {
413,861✔
884
    mError("failed to open mnode in step 7, since %s", tstrerror(code));
×
885
    mndClose(pMnode);
×
886
    terrno = code;
×
887
    return NULL;
×
888
  }
889

890
  mInfo("mnode open successfully");
413,861✔
891
  return pMnode;
413,861✔
892
}
893

894
void mndPreClose(SMnode *pMnode) {
413,801✔
895
  if (pMnode != NULL) {
413,801✔
896
    int32_t code = 0;
413,801✔
897
    // TODO check return value
898
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
413,801✔
899
    if (code < 0) {
413,801✔
900
      mError("failed to transfer leader since %s", tstrerror(code));
×
901
    }
902
    syncPreStop(pMnode->syncMgmt.sync);
413,801✔
903
    code = sdbWriteFile(pMnode->pSdb, 0);
413,801✔
904
    if (code < 0) {
413,801✔
905
      mError("failed to write sdb since %s", tstrerror(code));
716✔
906
    }
907
  }
908
}
413,801✔
909

910
void mndClose(SMnode *pMnode) {
413,801✔
911
  if (pMnode != NULL) {
413,801✔
912
    mInfo("start to close mnode");
413,801✔
913
    mndCleanupSteps(pMnode, -1);
413,801✔
914
    taosMemoryFreeClear(pMnode->path);
413,801✔
915
    taosMemoryFreeClear(pMnode);
413,801✔
916
    mInfo("mnode is closed");
413,801✔
917
  }
918
}
413,801✔
919

920
int32_t mndStart(SMnode *pMnode) {
413,801✔
921
  mndSyncStart(pMnode);
413,801✔
922
  if (pMnode->deploy) {
413,801✔
923
    if (sdbDeploy(pMnode->pSdb) != 0) {
294,374✔
924
      mError("failed to deploy sdb while start mnode");
×
925
      return -1;
×
926
    }
927
    mndSetRestored(pMnode, true);
294,374✔
928
  }
929
  if (mndIsLeader(pMnode)) {
413,801✔
930
    if (sdbUpgrade(pMnode->pSdb, pMnode->version) != 0) {
370,059✔
931
      mError("failed to upgrade sdb while start mnode");
×
932
      return -1;
×
933
    }
934
  }
935
  pMnode->version = TSDB_MNODE_BUILTIN_DATA_VERSION;
413,801✔
936
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
413,801✔
937

938
  return mndInitTimer(pMnode);
413,801✔
939
}
940

941
bool mndNeedUpgrade(SMnode *pMnode, int32_t version) { return pMnode->version > version; }
413,801✔
942

943
int32_t mndGetVersion(SMnode *pMnode) { return pMnode->version; }
325,288✔
944

945
int32_t mndIsCatchUp(SMnode *pMnode) {
179,314✔
946
  int64_t rid = pMnode->syncMgmt.sync;
179,314✔
947
  return syncIsCatchUp(rid);
179,314✔
948
}
949

950
ESyncRole mndGetRole(SMnode *pMnode) {
179,314✔
951
  int64_t rid = pMnode->syncMgmt.sync;
179,314✔
952
  return syncGetRole(rid);
179,314✔
953
}
954

955
int64_t mndGetTerm(SMnode *pMnode) {
8,493,859✔
956
  int64_t rid = pMnode->syncMgmt.sync;
8,493,859✔
957
  return syncGetTerm(rid);
8,493,859✔
958
}
959

960
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
21,159,001✔
961

962
void mndStop(SMnode *pMnode) {
413,801✔
963
  mndSetStop(pMnode);
413,801✔
964
  mndSyncStop(pMnode);
413,801✔
965
  mndCleanupTimer(pMnode);
413,801✔
966
}
413,801✔
967

968
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
52,222,269✔
969
  SMnode    *pMnode = pMsg->info.node;
52,222,269✔
970
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
52,222,269✔
971

972
  const STraceId *trace = &pMsg->info.traceId;
52,222,269✔
973
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
52,222,269✔
974

975
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
52,222,269✔
976
  if (code != 0) {
52,222,176✔
977
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
454✔
978
            tstrerror(code), code);
979
  }
980

981
  return code;
52,222,269✔
982
}
983

984
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
251,107,248✔
985
  int32_t code = 0;
251,107,248✔
986
  if (!IsReq(pMsg)) TAOS_RETURN(code);
251,107,248✔
987
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
220,412,134✔
988
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
215,528,164✔
989
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
211,494,693✔
990
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
201,904,012✔
991
    TAOS_RETURN(code);
18,505,963✔
992
  }
993

994
  SMnode *pMnode = pMsg->info.node;
201,904,188✔
995
  (void)taosThreadRwlockRdlock(&pMnode->lock);
201,901,654✔
996
  if (pMnode->stopped) {
201,910,271✔
997
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,863✔
998
    code = TSDB_CODE_APP_IS_STOPPING;
1,863✔
999
    TAOS_RETURN(code);
1,863✔
1000
  }
1001

1002
  terrno = 0;
201,902,920✔
1003
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
201,897,259✔
1004
  if (terrno != 0) {
201,909,813✔
1005
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1006
    code = terrno;
×
1007
    TAOS_RETURN(code);
×
1008
  }
1009

1010
  if (state.state != TAOS_SYNC_STATE_LEADER) {
201,908,668✔
1011
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,854,164✔
1012
    code = TSDB_CODE_SYN_NOT_LEADER;
1,854,163✔
1013
    goto _OVER;
1,854,163✔
1014
  }
1015

1016
  if (!state.restored || !pMnode->restored) {
200,054,504✔
1017
    (void)taosThreadRwlockUnlock(&pMnode->lock);
711,825✔
1018
    code = TSDB_CODE_SYN_RESTORING;
710,374✔
1019
    goto _OVER;
710,374✔
1020
  }
1021

1022
#if 1
1023
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
199,342,704✔
1024
#else
1025
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
1026
  mTrace("mnode rpc is acquired, ref:%d", ref);
1027
#endif
1028

1029
  (void)taosThreadRwlockUnlock(&pMnode->lock);
199,345,028✔
1030
  TAOS_RETURN(code);
199,345,889✔
1031

1032
_OVER:
2,564,537✔
1033
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
2,564,537✔
1034
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
2,563,793✔
1035
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
2,563,793✔
1036
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
2,564,453✔
1037
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
2,564,453✔
1038
      pMsg->msgType == TDMT_MND_SSMIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
2,564,560✔
1039
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER ||
2,564,642✔
1040
      pMsg->msgType == TDMT_MND_UPDATE_SSMIGRATE_PROGRESS_TIMER || pMsg->msgType == TDMT_MND_SCAN_TIMER ||
2,563,070✔
1041
      pMsg->msgType == TDMT_MND_QUERY_TRIM_TIMER || pMsg->msgType == TDMT_MND_AUTH_HB_TIMER) {
2,563,154✔
1042
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
339✔
1043
           pMnode->stopped, state.restored, syncStr(state.state));
1044
    TAOS_RETURN(code);
339✔
1045
  }
1046

1047
  const STraceId *trace = &pMsg->info.traceId;
2,562,653✔
1048
  SEpSet          epSet = {0};
2,562,758✔
1049
  mndGetMnodeEpSet(pMnode, &epSet);
2,563,502✔
1050

1051
  mGDebug(
2,564,642✔
1052
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
1053
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
1054
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
1055
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
1056

1057
  if (epSet.numOfEps <= 0) return -1;
2,564,642✔
1058

1059
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
8,669,874✔
1060
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
6,105,232✔
1061
  }
1062

1063
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
2,564,642✔
1064
  pMsg->info.rsp = rpcMallocCont(contLen);
2,563,102✔
1065
  if (pMsg->info.rsp != NULL) {
2,562,875✔
1066
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
2,563,604✔
1067
      mError("failed to serialize ep set");
×
1068
    }
1069
    pMsg->info.hasEpSet = 1;
2,562,977✔
1070
    pMsg->info.rspLen = contLen;
2,563,044✔
1071
  }
1072

1073
  TAOS_RETURN(code);
2,562,871✔
1074
}
1075

1076
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
251,123,054✔
1077
  SMnode         *pMnode = pMsg->info.node;
251,123,054✔
1078
  const STraceId *trace = &pMsg->info.traceId;
251,124,718✔
1079
  int32_t         code = TSDB_CODE_SUCCESS;
251,124,178✔
1080

1081
#ifdef TD_ENTERPRISE
1082
  if (pMsg->msgType != TDMT_MND_HEARTBEAT && pMsg->info.conn.isToken) {
251,124,178✔
1083
    SCachedTokenInfo ti = {0};
8,111✔
1084
    if (mndGetCachedTokenInfo(pMsg->info.conn.identifier, &ti) == NULL) {
8,111✔
1085
      mGError("msg:%p, failed to get token info, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
165✔
1086
      code = TSDB_CODE_MND_TOKEN_NOT_EXIST;
165✔
1087
      TAOS_RETURN(code);
165✔
1088
    }
1089
    if (ti.enabled == 0) {
7,946✔
1090
      mGError("msg:%p, token is disabled, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
330✔
1091
      code = TSDB_CODE_MND_TOKEN_DISABLED;
330✔
1092
      TAOS_RETURN(code);
330✔
1093
    }
1094
    if (ti.expireTime > 0 && taosGetTimestampSec() > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
7,616✔
1095
      mGError("msg:%p, token is expired, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1096
      code = TSDB_CODE_MND_TOKEN_EXPIRED;
×
1097
      TAOS_RETURN(code);
×
1098
    }
1099
    tstrncpy(pMsg->info.conn.user, ti.user, sizeof(pMsg->info.conn.user));
7,616✔
1100
  }
1101
#endif
1102

1103
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
251,114,942✔
1104
  MndMsgFpExt fpExt = NULL;
251,121,832✔
1105
  if (fp == NULL) {
251,121,832✔
1106
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
18,518,205✔
1107
    if (fpExt == NULL) {
18,518,205✔
1108
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
1109
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
1110
      TAOS_RETURN(code);
×
1111
    }
1112
  }
1113

1114
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
251,121,832✔
1115

1116
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
248,555,186✔
1117
  if (fp)
248,554,891✔
1118
    code = (*fp)(pMsg);
230,036,972✔
1119
  else
1120
    code = (*fpExt)(pMsg, pQueueInfo);
18,517,919✔
1121
  mndReleaseRpc(pMnode);
248,558,378✔
1122

1123
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
248,559,074✔
1124
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
29,166,119✔
1125
  } else if (code == 0) {
219,392,955✔
1126
    mGTrace("msg:%p, successfully processed", pMsg);
216,878,253✔
1127
  } else {
1128
    // TODO removve this wrong set code
1129
    if (code == -1) {
2,514,702✔
1130
      code = terrno;
×
1131
    }
1132
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
2,514,702✔
1133
            TMSG_INFO(pMsg->msgType));
1134
  }
1135

1136
  TAOS_RETURN(code);
248,559,074✔
1137
}
1138

1139
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
96,429,613✔
1140
  tmsg_t type = TMSG_INDEX(msgType);
96,429,613✔
1141
  if (type < TDMT_MAX) {
96,429,613✔
1142
    pMnode->msgFp[type] = fp;
96,429,613✔
1143
  }
1144
}
96,429,613✔
1145

1146
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
3,310,888✔
1147
  tmsg_t type = TMSG_INDEX(msgType);
3,310,888✔
1148
  if (type < TDMT_MAX) {
3,310,888✔
1149
    pMnode->msgFpExt[type] = fp;
3,310,888✔
1150
  }
1151
}
3,310,888✔
1152

1153
// Note: uid 0 is reserved
1154
int64_t mndGenerateUid(const char *name, int32_t len) {
9,979,131✔
1155
  int32_t hashval = MurmurHash3_32(name, len);
9,979,131✔
1156
  do {
×
1157
    int64_t us = taosGetTimestampUs();
9,979,156✔
1158
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
9,979,156✔
1159
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
9,979,156✔
1160
    if (uuid) {
9,979,156✔
1161
      return llabs(uuid);
9,979,156✔
1162
    }
1163
  } while (true);
1164
}
1165

1166
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
80✔
1167
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1168
  int32_t code = mndAcquireRpc(pMnode);
80✔
1169
  if (code < 0) {
80✔
1170
    TAOS_RETURN(code);
×
1171
  } else if (code == 1) {
80✔
1172
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1173
  }
1174

1175
  SSdb   *pSdb = pMnode->pSdb;
80✔
1176
  int64_t ms = taosGetTimestampMs();
80✔
1177

1178
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
80✔
1179
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
80✔
1180
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
80✔
1181
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
80✔
1182
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
80✔
1183
      pStbInfo->stbs == NULL) {
80✔
1184
    mndReleaseRpc(pMnode);
×
1185
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1186
    if (terrno != 0) code = terrno;
×
1187
    TAOS_RETURN(code);
×
1188
  }
1189

1190
  // cluster info
1191
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
80✔
1192
  pClusterInfo->monitor_interval = tsMonitorInterval;
80✔
1193
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
80✔
1194
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
80✔
1195
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
80✔
1196
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
80✔
1197
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
80✔
1198

1199
  void *pIter = NULL;
80✔
1200
  while (1) {
80✔
1201
    SDnodeObj *pObj = NULL;
160✔
1202
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
160✔
1203
    if (pIter == NULL) break;
160✔
1204

1205
    SMonDnodeDesc desc = {0};
80✔
1206
    desc.dnode_id = pObj->id;
80✔
1207
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
80✔
1208
    if (mndIsDnodeOnline(pObj, ms)) {
80✔
1209
      tstrncpy(desc.status, "ready", sizeof(desc.status));
80✔
1210
    } else {
1211
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1212
    }
1213
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
160✔
1214
      mError("failed put dnode into array, but continue at this monitor report")
×
1215
    }
1216
    sdbRelease(pSdb, pObj);
80✔
1217
  }
1218

1219
  pIter = NULL;
80✔
1220
  while (1) {
80✔
1221
    SMnodeObj *pObj = NULL;
160✔
1222
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
160✔
1223
    if (pIter == NULL) break;
160✔
1224

1225
    SMonMnodeDesc desc = {0};
80✔
1226
    desc.mnode_id = pObj->id;
80✔
1227
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
80✔
1228

1229
    if (pObj->id == pMnode->selfDnodeId) {
80✔
1230
      pClusterInfo->first_ep_dnode_id = pObj->id;
80✔
1231
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
80✔
1232
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1233
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
80✔
1234
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1235
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
80✔
1236
      desc.syncState = TAOS_SYNC_STATE_LEADER;
80✔
1237
    } else {
1238
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1239
      desc.syncState = pObj->syncState;
×
1240
    }
1241
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
160✔
1242
      mError("failed to put mnode into array, but continue at this monitor report");
×
1243
    }
1244
    sdbRelease(pSdb, pObj);
80✔
1245
  }
1246

1247
  // vgroup info
1248
  pIter = NULL;
80✔
1249
  while (1) {
160✔
1250
    SVgObj *pVgroup = NULL;
240✔
1251
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
240✔
1252
    if (pIter == NULL) break;
240✔
1253

1254
    if (pVgroup->mountVgId) {
160✔
1255
      sdbRelease(pSdb, pVgroup);
×
1256
      continue;
×
1257
    }
1258

1259
    pClusterInfo->vgroups_total++;
160✔
1260
    pClusterInfo->tbs_total += pVgroup->numOfTables;
160✔
1261

1262
    SMonVgroupDesc desc = {0};
160✔
1263
    desc.vgroup_id = pVgroup->vgId;
160✔
1264

1265
    SName name = {0};
160✔
1266
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
160✔
1267
    if (code < 0) {
160✔
1268
      mError("failed to get db name since %s", tstrerror(code));
×
1269
      sdbCancelFetch(pSdb, pIter);
×
1270
      sdbRelease(pSdb, pVgroup);
×
1271
      TAOS_RETURN(code);
×
1272
    }
1273
    (void)tNameGetDbName(&name, desc.database_name);
160✔
1274

1275
    desc.tables_num = pVgroup->numOfTables;
160✔
1276
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
160✔
1277
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
160✔
1278
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
320✔
1279
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
160✔
1280
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
160✔
1281
      pVnDesc->dnode_id = pVgid->dnodeId;
160✔
1282
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
160✔
1283
      pVnDesc->syncState = pVgid->syncState;
160✔
1284
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
160✔
1285
        tstrncpy(desc.status, "ready", sizeof(desc.status));
160✔
1286
        pClusterInfo->vgroups_alive++;
160✔
1287
      }
1288
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
160✔
1289
        pClusterInfo->vnodes_alive++;
160✔
1290
      }
1291
      pClusterInfo->vnodes_total++;
160✔
1292
    }
1293

1294
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
320✔
1295
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1296
    }
1297
    sdbRelease(pSdb, pVgroup);
160✔
1298
  }
1299

1300
  // stb info
1301
  pIter = NULL;
80✔
UNCOV
1302
  while (1) {
×
1303
    SStbObj *pStb = NULL;
80✔
1304
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
80✔
1305
    if (pIter == NULL) break;
80✔
1306

UNCOV
1307
    SMonStbDesc desc = {0};
×
1308

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

UNCOV
1318
    SName name2 = {0};
×
UNCOV
1319
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
UNCOV
1320
    if (code < 0) {
×
1321
      mError("failed to get table name since %s", tstrerror(code));
×
1322
      sdbRelease(pSdb, pStb);
×
1323
      TAOS_RETURN(code);
×
1324
    }
UNCOV
1325
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1326

UNCOV
1327
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1328
      mError("failed to put stb into array, but continue at this monitor report");
×
1329
    }
UNCOV
1330
    sdbRelease(pSdb, pStb);
×
1331
  }
1332

1333
  // grant info
1334
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
80✔
1335
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
80✔
1336
  if (pMnode->grant.expireTimeMS == 0) {
80✔
1337
    pGrantInfo->expire_time = 0;
×
1338
    pGrantInfo->timeseries_total = 0;
×
1339
  }
1340

1341
  mndReleaseRpc(pMnode);
80✔
1342
  TAOS_RETURN(code);
80✔
1343
}
1344

1345
int32_t mndResetTimer(SMnode *pMnode){
×
1346
  return syncResetTimer(pMnode->syncMgmt.sync, tsMnodeElectIntervalMs, tsMnodeHeartbeatIntervalMs);
×
1347
}
1348

1349
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
26,502,270✔
1350
  mTrace("mnode get load");
26,502,270✔
1351
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
26,502,270✔
1352
  pLoad->syncState = state.state;
26,502,270✔
1353
  pLoad->syncRestore = state.restored;
26,502,270✔
1354
  pLoad->syncTerm = state.term;
26,502,270✔
1355
  pLoad->roleTimeMs = state.roleTimeMs;
26,502,270✔
1356
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
26,502,270✔
1357
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1358
  return 0;
26,502,270✔
1359
}
1360

1361
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
8,388,219✔
1362
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
8,388,219✔
1363
  return state.roleTimeMs;
8,388,219✔
1364
}
1365

1366
void mndSetRestored(SMnode *pMnode, bool restored) {
413,801✔
1367
  if (restored) {
413,801✔
1368
    (void)taosThreadRwlockWrlock(&pMnode->lock);
413,801✔
1369
    pMnode->restored = true;
413,801✔
1370
    (void)taosThreadRwlockUnlock(&pMnode->lock);
413,801✔
1371
    mInfo("mnode set restored:%d", restored);
413,801✔
1372
  } else {
1373
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1374
    pMnode->restored = false;
×
1375
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1376
    mInfo("mnode set restored:%d", restored);
×
1377
    while (1) {
1378
      if (pMnode->rpcRef <= 0) break;
×
1379
      taosMsleep(3);
×
1380
    }
1381
  }
1382
}
413,801✔
1383

1384
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1385

1386
void mndSetStop(SMnode *pMnode) {
413,801✔
1387
  (void)taosThreadRwlockWrlock(&pMnode->lock);
413,801✔
1388
  pMnode->stopped = true;
413,801✔
1389
  (void)taosThreadRwlockUnlock(&pMnode->lock);
413,801✔
1390
  mInfo("mnode set stopped");
413,801✔
1391
}
413,801✔
1392

1393
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
535,540,270✔
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