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

taosdata / TDengine / #3818

01 Apr 2025 07:46AM UTC coverage: 34.065% (-0.02%) from 34.08%
#3818

push

travis-ci

happyguoxy
test:alter gcda dir

148531 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222425 of 489446 relevant lines covered (45.44%)

762721.74 hits per line

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

53.71
/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 "mndCluster.h"
21
#include "mndCompact.h"
22
#include "mndCompactDetail.h"
23
#include "mndConfig.h"
24
#include "mndConsumer.h"
25
#include "mndDb.h"
26
#include "mndDnode.h"
27
#include "mndFunc.h"
28
#include "mndGrant.h"
29
#include "mndIndex.h"
30
#include "mndInfoSchema.h"
31
#include "mndMnode.h"
32
#include "mndPerfSchema.h"
33
#include "mndPrivilege.h"
34
#include "mndProfile.h"
35
#include "mndQnode.h"
36
#include "mndQuery.h"
37
#include "mndShow.h"
38
#include "mndSma.h"
39
#include "mndSnode.h"
40
#include "mndStb.h"
41
#include "mndStream.h"
42
#include "mndSubscribe.h"
43
#include "mndSync.h"
44
#include "mndTelem.h"
45
#include "mndTopic.h"
46
#include "mndTrans.h"
47
#include "mndUser.h"
48
#include "mndVgroup.h"
49
#include "mndView.h"
50

51
static inline int32_t mndAcquireRpc(SMnode *pMnode) {
364✔
52
  int32_t code = 0;
364✔
53
  (void)taosThreadRwlockRdlock(&pMnode->lock);
364✔
54
  if (pMnode->stopped) {
364!
55
    code = TSDB_CODE_APP_IS_STOPPING;
×
56
  } else if (!mndIsLeader(pMnode)) {
364!
57
    code = 1;
×
58
  } else {
59
#if 1
60
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
364✔
61
#else
62
    int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
63
    mTrace("mnode rpc is acquired, ref:%d", ref);
64
#endif
65
  }
66
  (void)taosThreadRwlockUnlock(&pMnode->lock);
364✔
67
  TAOS_RETURN(code);
364✔
68
}
69

70
static inline void mndReleaseRpc(SMnode *pMnode) {
17,577✔
71
  (void)taosThreadRwlockRdlock(&pMnode->lock);
17,577✔
72
#if 1
73
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
17,579✔
74
#else
75
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
76
  mTrace("mnode rpc is released, ref:%d", ref);
77
#endif
78
  (void)taosThreadRwlockUnlock(&pMnode->lock);
17,578✔
79
}
17,577✔
80

81
static void *mndBuildTimerMsg(int32_t *pContLen) {
4,339✔
82
  terrno = 0;
4,339✔
83
  SMTimerReq timerReq = {0};
4,339✔
84

85
  int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq);
4,339✔
86
  if (contLen <= 0) return NULL;
4,339!
87
  void *pReq = rpcMallocCont(contLen);
4,339✔
88
  if (pReq == NULL) return NULL;
4,339!
89

90
  if (tSerializeSMTimerMsg(pReq, contLen, &timerReq) < 0) {
4,339!
91
    mError("failed to serialize timer msg since %s", terrstr());
×
92
  }
93
  *pContLen = contLen;
4,339✔
94
  return pReq;
4,339✔
95
}
96

97
static void mndPullupTrans(SMnode *pMnode) {
938✔
98
  mTrace("pullup trans msg");
938✔
99
  int32_t contLen = 0;
938✔
100
  void   *pReq = mndBuildTimerMsg(&contLen);
938✔
101
  if (pReq != NULL) {
938!
102
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS_TIMER, .pCont = pReq, .contLen = contLen};
938✔
103
    // TODO check return value
104
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
938!
105
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
106
    }
107
  }
108
}
938✔
109

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

123
static void mndPullupTtl(SMnode *pMnode) {
172✔
124
  mTrace("pullup ttl");
172✔
125
  int32_t contLen = 0;
172✔
126
  void   *pReq = mndBuildTimerMsg(&contLen);
172✔
127
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
172✔
128
  // TODO check return value
129
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
172!
130
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
131
  }
132
}
172✔
133

134
static void mndPullupTrimDb(SMnode *pMnode) {
×
135
  mTrace("pullup s3migrate");
×
136
  int32_t contLen = 0;
×
137
  void   *pReq = mndBuildTimerMsg(&contLen);
×
138
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
139
  // TODO check return value
140
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
141
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
142
  }
143
}
×
144

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

156
static int32_t mndPullupArbHeartbeat(SMnode *pMnode) {
938✔
157
  mTrace("pullup arb hb");
938✔
158
  int32_t contLen = 0;
938✔
159
  void   *pReq = mndBuildTimerMsg(&contLen);
938✔
160
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
938✔
161
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
938✔
162
}
163

164
static int32_t mndPullupArbCheckSync(SMnode *pMnode) {
620✔
165
  mTrace("pullup arb sync");
620✔
166
  int32_t contLen = 0;
620✔
167
  void   *pReq = mndBuildTimerMsg(&contLen);
620✔
168
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_CHECK_SYNC_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
620✔
169
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
620✔
170
}
171

172
static void mndCalMqRebalance(SMnode *pMnode) {
938✔
173
  int32_t contLen = 0;
938✔
174
  void   *pReq = mndBuildTimerMsg(&contLen);
938✔
175
  if (pReq != NULL) {
938!
176
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TMQ_TIMER, .pCont = pReq, .contLen = contLen};
938✔
177
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
938!
178
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
179
    }
180
  }
181
}
938✔
182

183
static void mndStreamCheckpointTimer(SMnode *pMnode) {
45✔
184
  SMStreamDoCheckpointMsg *pMsg = rpcMallocCont(sizeof(SMStreamDoCheckpointMsg));
45✔
185
  if (pMsg != NULL) {
45!
186
    int32_t size = sizeof(SMStreamDoCheckpointMsg);
45✔
187
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_BEGIN_CHECKPOINT, .pCont = pMsg, .contLen = size};
45✔
188
    // TODO check return value
189
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
45!
190
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
191
    }
192
  }
193
}
45✔
194

195
static void mndStreamCheckNode(SMnode *pMnode) {
80✔
196
  int32_t contLen = 0;
80✔
197
  void   *pReq = mndBuildTimerMsg(&contLen);
80✔
198
  if (pReq != NULL) {
80!
199
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_NODECHECK_TIMER, .pCont = pReq, .contLen = contLen};
80✔
200
    // TODO check return value
201
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
80!
202
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
203
    }
204
  }
205
}
80✔
206

207
static void mndStreamCheckStatus(SMnode *pMnode) {
×
208
  int32_t contLen = 0;
×
209
  void   *pReq = mndBuildTimerMsg(&contLen);
×
210
  if (pReq != NULL) {
×
211
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_CHECK_STREAM_TIMER, .pCont = pReq, .contLen = contLen};
×
212
    // TODO check return value
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

219
static void mndStreamConsensusChkpt(SMnode *pMnode) {
364✔
220
  int32_t contLen = 0;
364✔
221
  void   *pReq = mndBuildTimerMsg(&contLen);
364✔
222
  if (pReq != NULL) {
364!
223
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_CONSEN_TIMER, .pCont = pReq, .contLen = contLen};
364✔
224
    // TODO check return value
225
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
364!
226
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
227
    }
228
  }
229
}
364✔
230

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

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

261
static void mndIncreaseUpTime(SMnode *pMnode) {
×
262
  mTrace("increate uptime");
×
263
  int32_t contLen = 0;
×
264
  void   *pReq = mndBuildTimerMsg(&contLen);
×
265
  if (pReq != NULL) {
×
266
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
×
267
                      .pCont = pReq,
268
                      .contLen = contLen,
269
                      .info.notFreeAhandle = 1,
270
                      .info.ahandle = 0};
271
    // TODO check return value
272
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
273
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
274
    }
275
  }
276
}
×
277

278
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
5✔
279
  SSdb *pSdb = pMnode->pSdb;
5✔
280

281
  void *pIter = NULL;
5✔
282
  while (1) {
2✔
283
    SVgObj *pVgroup = NULL;
7✔
284
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
7✔
285
    if (pIter == NULL) break;
7✔
286

287
    bool stateChanged = false;
2✔
288
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
4✔
289
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
2✔
290
      if (pGid->dnodeId == dnodeId) {
2!
291
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
×
292
          mInfo(
×
293
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:error restored:0 "
294
              "canRead:0",
295
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
296
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
×
297
          pGid->syncRestore = 0;
×
298
          pGid->syncCanRead = 0;
×
299
          pGid->startTimeMs = 0;
×
300
          stateChanged = true;
×
301
        }
302
        break;
×
303
      }
304
    }
305

306
    if (stateChanged) {
2!
307
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
×
308
      if (pDb != NULL && pDb->stateTs != curMs) {
×
309
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
×
310
              curMs);
311
        pDb->stateTs = curMs;
×
312
      }
313
      mndReleaseDb(pMnode, pDb);
×
314
    }
315

316
    sdbRelease(pSdb, pVgroup);
2✔
317
  }
318
}
5✔
319

320
static void mndCheckDnodeOffline(SMnode *pMnode) {
364✔
321
  mTrace("check dnode offline");
364✔
322
  if (mndAcquireRpc(pMnode) != 0) return;
364!
323

324
  SSdb   *pSdb = pMnode->pSdb;
364✔
325
  int64_t curMs = taosGetTimestampMs();
364✔
326

327
  void *pIter = NULL;
364✔
328
  while (1) {
697✔
329
    SDnodeObj *pDnode = NULL;
1,061✔
330
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
1,061✔
331
    if (pIter == NULL) break;
1,061✔
332

333
    bool online = mndIsDnodeOnline(pDnode, curMs);
697✔
334
    if (!online) {
697✔
335
      mInfo("dnode:%d, in offline state", pDnode->id);
5!
336
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
5✔
337
    }
338

339
    sdbRelease(pSdb, pDnode);
697✔
340
  }
341

342
  mndReleaseRpc(pMnode);
364✔
343
}
344

345
static bool mnodeIsNotLeader(SMnode *pMnode) {
2,241✔
346
  terrno = 0;
2,241✔
347
  (void)taosThreadRwlockRdlock(&pMnode->lock);
2,241✔
348
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
2,241✔
349
  if (terrno != 0) {
2,241!
350
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
351
    return true;
×
352
  }
353

354
  if (state.state != TAOS_SYNC_STATE_LEADER) {
2,241✔
355
    (void)taosThreadRwlockUnlock(&pMnode->lock);
333✔
356
    terrno = TSDB_CODE_SYN_NOT_LEADER;
333✔
357
    return true;
333✔
358
  }
359
  if (!state.restored || !pMnode->restored) {
1,908!
360
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
361
    terrno = TSDB_CODE_SYN_RESTORING;
×
362
    return true;
×
363
  }
364
  (void)taosThreadRwlockUnlock(&pMnode->lock);
1,908✔
365
  return false;
1,908✔
366
}
367

368
static int32_t minCronTime() {
×
369
  int32_t min = INT32_MAX;
×
370
  min = TMIN(min, tsTtlPushIntervalSec);
×
371
  min = TMIN(min, tsTrimVDbIntervalSec);
×
372
  min = TMIN(min, tsS3MigrateIntervalSec);
×
373
  min = TMIN(min, tsTransPullupInterval);
×
374
  min = TMIN(min, tsCompactPullupInterval);
×
375
  min = TMIN(min, tsMqRebalanceInterval);
×
376
  min = TMIN(min, tsStreamCheckpointInterval);
×
377
  min = TMIN(min, tsStreamNodeCheckInterval);
×
378
  min = TMIN(min, tsArbHeartBeatIntervalSec);
×
379
  min = TMIN(min, tsArbCheckSyncIntervalSec);
×
380

381
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
382
  min = TMIN(min, telemInt);
×
383
  min = TMIN(min, tsGrantHBInterval);
×
384
  min = TMIN(min, tsUptimeInterval);
×
385

386
  return min <= 1 ? 2 : min;
×
387
}
388
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
1,908✔
389
  int32_t code = 0;
1,908✔
390
#ifndef TD_ASTRA  
391
  if (sec % tsTtlPushIntervalSec == 0) {
1,908✔
392
    mndPullupTtl(pMnode);
172✔
393
  }
394

395
  if (sec % tsTrimVDbIntervalSec == 0) {
1,908!
396
    mndPullupTrimDb(pMnode);
×
397
  }
398
#endif
399
#ifdef USE_S3
400
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
1,908!
401
    mndPullupS3MigrateDb(pMnode);
×
402
  }
403
#endif
404
  if (sec % tsTransPullupInterval == 0) {
1,908✔
405
    mndPullupTrans(pMnode);
938✔
406
  }
407

408
  if (sec % tsCompactPullupInterval == 0) {
1,908✔
409
    mndPullupCompacts(pMnode);
172✔
410
  }
411
#ifdef USE_TOPIC
412
  if (sec % tsMqRebalanceInterval == 0) {
1,908✔
413
    mndCalMqRebalance(pMnode);
938✔
414
  }
415
#endif
416
#ifdef USE_STREAM
417
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
1,908✔
418
    mndStreamCheckpointTimer(pMnode);
45✔
419
  }
420

421
  if (sec % tsStreamNodeCheckInterval == 0) {
1,908✔
422
    mndStreamCheckNode(pMnode);
80✔
423
  }
424

425
  if (sec % (tsStreamFailedTimeout/1000) == 0) {
1,908!
426
    mndStreamCheckStatus(pMnode);
×
427
  }
428

429
  if (sec % 5 == 0) {
1,908✔
430
    mndStreamConsensusChkpt(pMnode);
364✔
431
  }
432

433
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
1,908!
434
    mndPullupTelem(pMnode);
×
435
  }
436
#endif
437
#ifndef TD_ASTRA
438
  if (sec % tsGrantHBInterval == 0) {
1,908✔
439
    mndPullupGrant(pMnode);
117✔
440
  }
441
#endif
442
  if (sec % tsUptimeInterval == 0) {
1,908!
443
    mndIncreaseUpTime(pMnode);
×
444
  }
445
#ifndef TD_ASTRA
446
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
1,908✔
447
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
938!
448
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
449
    }
450
  }
451

452
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
1,908✔
453
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
620!
454
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
455
    }
456
  }
457
#endif
458
}
1,908✔
459
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
1,908✔
460
  if (sec % (tsStatusInterval * 5) == 0) {
1,908✔
461
    mndCheckDnodeOffline(pMnode);
364✔
462
  }
463
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
1,908✔
464
    mndSyncCheckTimeout(pMnode);
45✔
465
  }
466
}
1,908✔
467

468
static void *mndThreadFp(void *param) {
60✔
469
  SMnode *pMnode = param;
60✔
470
  int64_t lastTime = 0;
60✔
471
  setThreadName("mnode-timer");
60✔
472

473
  while (1) {
22,663✔
474
    lastTime++;
22,723✔
475
    taosMsleep(100);
22,723✔
476

477
    if (mndGetStop(pMnode)) break;
22,723✔
478
    if (lastTime % 10 != 0) continue;
22,663✔
479

480
    if (mnodeIsNotLeader(pMnode)) {
2,241✔
481
      mTrace("timer not process since mnode is not leader");
333!
482
      continue;
333✔
483
    }
484

485
    int64_t sec = lastTime / 10;
1,908✔
486
    mndDoTimerCheckTask(pMnode, sec);
1,908✔
487

488
    mndDoTimerPullupTask(pMnode, sec);
1,908✔
489
  }
490

491
  return NULL;
60✔
492
}
493

494
static int32_t mndInitTimer(SMnode *pMnode) {
60✔
495
  int32_t      code = 0;
60✔
496
  TdThreadAttr thAttr;
497
  (void)taosThreadAttrInit(&thAttr);
60✔
498
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
60✔
499
#ifdef TD_COMPACT_OS
500
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
501
#endif
502
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode)) != 0) {
60!
503
    mError("failed to create timer thread since %s", tstrerror(code));
×
504
    TAOS_RETURN(code);
×
505
  }
506

507
  (void)taosThreadAttrDestroy(&thAttr);
60✔
508
  tmsgReportStartup("mnode-timer", "initialized");
60✔
509
  TAOS_RETURN(code);
60✔
510
}
511

512
static void mndCleanupTimer(SMnode *pMnode) {
60✔
513
  if (taosCheckPthreadValid(pMnode->thread)) {
60!
514
    (void)taosThreadJoin(pMnode->thread, NULL);
60✔
515
    taosThreadClear(&pMnode->thread);
60✔
516
  }
517
}
60✔
518

519
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
63✔
520
  int32_t code = 0;
63✔
521
  pMnode->path = taosStrdup(path);
63!
522
  if (pMnode->path == NULL) {
63!
523
    code = terrno;
×
524
    TAOS_RETURN(code);
×
525
  }
526

527
  if (taosMkDir(pMnode->path) != 0) {
63!
528
    code = terrno;
×
529
    TAOS_RETURN(code);
×
530
  }
531

532
  TAOS_RETURN(code);
63✔
533
}
534

535
static int32_t mndInitWal(SMnode *pMnode) {
63✔
536
  int32_t code = 0;
63✔
537
  char    path[PATH_MAX + 20] = {0};
63✔
538
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
63✔
539
  SWalCfg cfg = {.vgId = 1,
63✔
540
                 .fsyncPeriod = 0,
541
                 .rollPeriod = -1,
542
                 .segSize = -1,
543
                 .committed = -1,
544
                 .retentionPeriod = 0,
545
                 .retentionSize = 0,
546
                 .level = TAOS_WAL_FSYNC,
547
                 .encryptAlgorithm = 0,
548
                 .encryptKey = {0}};
549

550
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
551
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
63!
552
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
553
    if (tsEncryptKey[0] == '\0') {
×
554
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
555
      TAOS_RETURN(code);
×
556
    } else {
557
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
558
    }
559
  }
560
#endif
561

562
  pMnode->pWal = walOpen(path, &cfg);
63✔
563
  if (pMnode->pWal == NULL) {
63!
564
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
565
    if (terrno != 0) code = terrno;
×
566
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
567
    TAOS_RETURN(code);
×
568
  }
569

570
  TAOS_RETURN(code);
63✔
571
}
572

573
static void mndCloseWal(SMnode *pMnode) {
60✔
574
  if (pMnode->pWal != NULL) {
60!
575
    walClose(pMnode->pWal);
60✔
576
    pMnode->pWal = NULL;
60✔
577
  }
578
}
60✔
579

580
static int32_t mndInitSdb(SMnode *pMnode) {
63✔
581
  int32_t code = 0;
63✔
582
  SSdbOpt opt = {0};
63✔
583
  opt.path = pMnode->path;
63✔
584
  opt.pMnode = pMnode;
63✔
585
  opt.pWal = pMnode->pWal;
63✔
586

587
  pMnode->pSdb = sdbInit(&opt);
63✔
588
  if (pMnode->pSdb == NULL) {
63!
589
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
590
    if (terrno != 0) code = terrno;
×
591
    TAOS_RETURN(code);
×
592
  }
593

594
  TAOS_RETURN(code);
63✔
595
}
596

597
static int32_t mndOpenSdb(SMnode *pMnode) {
63✔
598
  int32_t code = 0;
63✔
599
  if (!pMnode->deploy) {
63✔
600
    code = sdbReadFile(pMnode->pSdb);
20✔
601
  }
602

603
  mInfo("vgId:1, mnode sdb is opened, with applied index:%" PRId64, pMnode->pSdb->commitIndex);
63!
604

605
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
63✔
606
  return code;
63✔
607
}
608

609
static void mndCleanupSdb(SMnode *pMnode) {
60✔
610
  if (pMnode->pSdb) {
60!
611
    sdbCleanup(pMnode->pSdb);
60✔
612
    pMnode->pSdb = NULL;
60✔
613
  }
614
}
60✔
615

616
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
2,268✔
617
  SMnodeStep step = {0};
2,268✔
618
  step.name = name;
2,268✔
619
  step.initFp = initFp;
2,268✔
620
  step.cleanupFp = cleanupFp;
2,268✔
621
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
4,536!
622
    TAOS_RETURN(terrno);
×
623
  }
624

625
  TAOS_RETURN(0);
2,268✔
626
}
627

628
static int32_t mndInitSteps(SMnode *pMnode) {
63✔
629
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
63!
630
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
63!
631
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
63!
632
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
63!
633
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
63!
634
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
63!
635
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
63!
636
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
63!
637
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
63!
638
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
63!
639
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
63!
640
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
63!
641
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
63!
642
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
63!
643
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
63!
644
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
63!
645
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
63!
646
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
63!
647
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
63!
648
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
63!
649
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
63!
650
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
63!
651
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
63!
652
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
63!
653
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
63!
654
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
63!
655
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
63!
656
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
63!
657
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
63!
658
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
63!
659
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
63!
660
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
63!
661
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
63!
662
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
63!
663
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
63!
664
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
63!
665
  return 0;
63✔
666
}
667

668
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
60✔
669
  if (pMnode->pSteps == NULL) return;
60!
670

671
  if (pos == -1) {
60!
672
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
60✔
673
  }
674

675
  for (int32_t s = pos; s >= 0; s--) {
2,220✔
676
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
2,160✔
677
    mInfo("%s will cleanup", pStep->name);
2,160!
678
    if (pStep->cleanupFp != NULL) {
2,160✔
679
      (*pStep->cleanupFp)(pMnode);
2,040✔
680
    }
681
  }
682

683
  taosArrayClear(pMnode->pSteps);
60✔
684
  taosArrayDestroy(pMnode->pSteps);
60✔
685
  pMnode->pSteps = NULL;
60✔
686
}
687

688
static int32_t mndExecSteps(SMnode *pMnode) {
63✔
689
  int32_t code = 0;
63✔
690
  int32_t size = taosArrayGetSize(pMnode->pSteps);
63✔
691
  for (int32_t pos = 0; pos < size; pos++) {
2,331✔
692
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
2,268✔
693
    if (pStep->initFp == NULL) continue;
2,268!
694

695
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
2,268!
696
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
697
      mndCleanupSteps(pMnode, pos);
×
698
      TAOS_RETURN(code);
×
699
    } else {
700
      mInfo("%s is initialized", pStep->name);
2,268!
701
      tmsgReportStartup(pStep->name, "initialized");
2,268✔
702
    }
703
  }
704

705
  pMnode->clusterId = mndGetClusterId(pMnode);
63✔
706
  TAOS_RETURN(0);
63✔
707
}
708

709
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
63✔
710
  pMnode->msgCb = pOption->msgCb;
63✔
711
  pMnode->selfDnodeId = pOption->dnodeId;
63✔
712
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
63✔
713
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
63✔
714
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
63✔
715
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
63✔
716
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
63✔
717
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
63✔
718
}
63✔
719

720
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
63✔
721
  terrno = 0;
63✔
722
  mInfo("start to open mnode in %s", path);
63!
723

724
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
63!
725
  if (pMnode == NULL) {
63!
726
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
727
    mError("failed to open mnode since %s", terrstr());
×
728
    return NULL;
×
729
  }
730
  (void)memset(pMnode, 0, sizeof(SMnode));
63✔
731

732
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
63✔
733
  if (code != 0) {
63!
734
    taosMemoryFree(pMnode);
×
735
    mError("failed to open mnode lock since %s", tstrerror(code));
×
736
    return NULL;
×
737
  }
738

739
  char timestr[24] = "1970-01-01 00:00:00.00";
63✔
740
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
63✔
741
  if (code < 0) {
63!
742
    mError("failed to parse time since %s", tstrerror(code));
×
743
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
744
    taosMemoryFree(pMnode);
×
745
    return NULL;
×
746
  }
747
  mndSetOptions(pMnode, pOption);
63✔
748

749
  pMnode->deploy = pOption->deploy;
63✔
750
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
63✔
751
  if (pMnode->pSteps == NULL) {
63!
752
    taosMemoryFree(pMnode);
×
753
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
754
    mError("failed to open mnode since %s", terrstr());
×
755
    return NULL;
×
756
  }
757

758
  code = mndCreateDir(pMnode, path);
63✔
759
  if (code != 0) {
63!
760
    code = terrno;
×
761
    mError("failed to open mnode since %s", tstrerror(code));
×
762
    mndClose(pMnode);
×
763
    terrno = code;
×
764
    return NULL;
×
765
  }
766

767
  code = mndInitSteps(pMnode);
63✔
768
  if (code != 0) {
63!
769
    code = terrno;
×
770
    mError("failed to open mnode since %s", tstrerror(code));
×
771
    mndClose(pMnode);
×
772
    terrno = code;
×
773
    return NULL;
×
774
  }
775

776
  code = mndExecSteps(pMnode);
63✔
777
  if (code != 0) {
63!
778
    code = terrno;
×
779
    mError("failed to open mnode since %s", tstrerror(code));
×
780
    mndClose(pMnode);
×
781
    terrno = code;
×
782
    return NULL;
×
783
  }
784

785
  mInfo("mnode open successfully");
63!
786
  return pMnode;
63✔
787
}
788

789
void mndPreClose(SMnode *pMnode) {
60✔
790
  if (pMnode != NULL) {
60!
791
    int32_t code = 0;
60✔
792
    // TODO check return value
793
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
60✔
794
    if (code < 0) {
60!
795
      mError("failed to transfer leader since %s", tstrerror(code));
×
796
    }
797
    syncPreStop(pMnode->syncMgmt.sync);
60✔
798
    code = sdbWriteFile(pMnode->pSdb, 0);
60✔
799
    if (code < 0) {
60!
800
      mError("failed to write sdb since %s", tstrerror(code));
×
801
    }
802
  }
803
}
60✔
804

805
void mndClose(SMnode *pMnode) {
60✔
806
  if (pMnode != NULL) {
60!
807
    mInfo("start to close mnode");
60!
808
    mndCleanupSteps(pMnode, -1);
60✔
809
    taosMemoryFreeClear(pMnode->path);
60!
810
    taosMemoryFreeClear(pMnode);
60!
811
    mInfo("mnode is closed");
60!
812
  }
813
}
60✔
814

815
int32_t mndStart(SMnode *pMnode) {
60✔
816
  mndSyncStart(pMnode);
60✔
817
  if (pMnode->deploy) {
60✔
818
    if (sdbDeploy(pMnode->pSdb) != 0) {
43!
819
      mError("failed to deploy sdb while start mnode");
×
820
      return -1;
×
821
    }
822
    mndSetRestored(pMnode, true);
43✔
823
  }
824
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
60✔
825

826
  return mndInitTimer(pMnode);
60✔
827
}
828

829
int32_t mndIsCatchUp(SMnode *pMnode) {
27✔
830
  int64_t rid = pMnode->syncMgmt.sync;
27✔
831
  return syncIsCatchUp(rid);
27✔
832
}
833

834
ESyncRole mndGetRole(SMnode *pMnode) {
27✔
835
  int64_t rid = pMnode->syncMgmt.sync;
27✔
836
  return syncGetRole(rid);
27✔
837
}
838

839
int64_t mndGetTerm(SMnode *pMnode) {
620✔
840
  int64_t rid = pMnode->syncMgmt.sync;
620✔
841
  return syncGetTerm(rid);
620✔
842
}
843

844
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
1,558✔
845

846
void mndStop(SMnode *pMnode) {
60✔
847
  mndSetStop(pMnode);
60✔
848
  mndSyncStop(pMnode);
60✔
849
  mndCleanupTimer(pMnode);
60✔
850
}
60✔
851

852
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
5,629✔
853
  SMnode    *pMnode = pMsg->info.node;
5,629✔
854
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
5,629✔
855

856
  const STraceId *trace = &pMsg->info.traceId;
5,629✔
857
  mGTrace("vgId:1, process sync msg:%p, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
5,629!
858

859
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
5,629✔
860
  if (code != 0) {
5,629!
861
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
×
862
            tstrerror(code), code);
863
  }
864

865
  return code;
5,629✔
866
}
867

868
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
17,623✔
869
  int32_t code = 0;
17,623✔
870
  if (!IsReq(pMsg)) TAOS_RETURN(code);
17,623✔
871
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
15,692✔
872
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
15,444!
873
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
15,199✔
874
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
14,704!
875
    TAOS_RETURN(code);
988✔
876
  }
877

878
  SMnode *pMnode = pMsg->info.node;
14,704✔
879
  (void)taosThreadRwlockRdlock(&pMnode->lock);
14,704✔
880
  if (pMnode->stopped) {
14,712!
881
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
882
    code = TSDB_CODE_APP_IS_STOPPING;
×
883
    TAOS_RETURN(code);
×
884
  }
885

886
  terrno = 0;
14,712✔
887
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
14,711✔
888
  if (terrno != 0) {
14,712!
889
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
890
    code = terrno;
×
891
    TAOS_RETURN(code);
×
892
  }
893

894
  if (state.state != TAOS_SYNC_STATE_LEADER) {
14,711✔
895
    (void)taosThreadRwlockUnlock(&pMnode->lock);
239✔
896
    code = TSDB_CODE_SYN_NOT_LEADER;
239✔
897
    goto _OVER;
239✔
898
  }
899

900
  if (!state.restored || !pMnode->restored) {
14,472✔
901
    (void)taosThreadRwlockUnlock(&pMnode->lock);
180✔
902
    code = TSDB_CODE_SYN_RESTORING;
181✔
903
    goto _OVER;
181✔
904
  }
905

906
#if 1
907
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
14,292✔
908
#else
909
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
910
  mTrace("mnode rpc is acquired, ref:%d", ref);
911
#endif
912

913
  (void)taosThreadRwlockUnlock(&pMnode->lock);
14,296✔
914
  TAOS_RETURN(code);
14,295✔
915

916
_OVER:
420✔
917
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
420!
918
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
420!
919
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
420!
920
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
420!
921
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
420!
922
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
420!
923
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER) {
420!
924
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
925
           pMnode->stopped, state.restored, syncStr(state.state));
926
    TAOS_RETURN(code);
×
927
  }
928

929
  const STraceId *trace = &pMsg->info.traceId;
420✔
930
  SEpSet          epSet = {0};
420✔
931
  mndGetMnodeEpSet(pMnode, &epSet);
420✔
932

933
  mGDebug(
420!
934
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
935
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
936
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
937
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
938

939
  if (epSet.numOfEps <= 0) return -1;
420!
940

941
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
1,226✔
942
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
806✔
943
  }
944

945
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
420✔
946
  pMsg->info.rsp = rpcMallocCont(contLen);
420✔
947
  if (pMsg->info.rsp != NULL) {
420!
948
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
420!
949
      mError("failed to serialize ep set");
×
950
    }
951
    pMsg->info.hasEpSet = 1;
420✔
952
    pMsg->info.rspLen = contLen;
420✔
953
  }
954

955
  TAOS_RETURN(code);
420✔
956
}
957

958
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
17,623✔
959
  SMnode         *pMnode = pMsg->info.node;
17,623✔
960
  const STraceId *trace = &pMsg->info.traceId;
17,623✔
961
  int32_t         code = TSDB_CODE_SUCCESS;
17,623✔
962

963
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
17,623✔
964
  MndMsgFpExt fpExt = NULL;
17,623✔
965
  if (fp == NULL) {
17,623✔
966
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
988✔
967
    if (fpExt == NULL) {
988!
968
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
969
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
970
      TAOS_RETURN(code);
×
971
    }
972
  }
973

974
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
17,623✔
975

976
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
17,211!
977
  if (fp)
17,211✔
978
    code = (*fp)(pMsg);
16,223✔
979
  else
980
    code = (*fpExt)(pMsg, pQueueInfo);
988✔
981
  mndReleaseRpc(pMnode);
17,214✔
982

983
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
17,213✔
984
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
1,520!
985
  } else if (code == 0) {
15,693✔
986
    mGTrace("msg:%p, successfully processed", pMsg);
15,378!
987
  } else {
988
    // TODO removve this wrong set code
989
    if (code == -1) {
315!
990
      code = terrno;
×
991
    }
992
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
315!
993
            TMSG_INFO(pMsg->msgType));
994
  }
995

996
  TAOS_RETURN(code);
17,213✔
997
}
998

999
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
11,466✔
1000
  tmsg_t type = TMSG_INDEX(msgType);
11,466✔
1001
  if (type < TDMT_MAX) {
11,466!
1002
    pMnode->msgFp[type] = fp;
11,466✔
1003
  }
1004
}
11,466✔
1005

1006
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
504✔
1007
  tmsg_t type = TMSG_INDEX(msgType);
504✔
1008
  if (type < TDMT_MAX) {
504!
1009
    pMnode->msgFpExt[type] = fp;
504✔
1010
  }
1011
}
504✔
1012

1013
// Note: uid 0 is reserved
1014
int64_t mndGenerateUid(const char *name, int32_t len) {
1,201✔
1015
  int32_t hashval = MurmurHash3_32(name, len);
1,201✔
1016
  do {
×
1017
    int64_t us = taosGetTimestampUs();
1,201✔
1018
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
1,201✔
1019
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
1,201✔
1020
    if (uuid) {
1,201!
1021
      return llabs(uuid);
1,201✔
1022
    }
1023
  } while (true);
1024
}
1025

1026
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
×
1027
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1028
  int32_t code = mndAcquireRpc(pMnode);
×
1029
  if (code < 0) {
×
1030
    TAOS_RETURN(code);
×
1031
  } else if (code == 1) {
×
1032
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1033
  }
1034

1035
  SSdb   *pSdb = pMnode->pSdb;
×
1036
  int64_t ms = taosGetTimestampMs();
×
1037

1038
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
×
1039
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
×
1040
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
×
1041
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
×
1042
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
×
1043
      pStbInfo->stbs == NULL) {
×
1044
    mndReleaseRpc(pMnode);
×
1045
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1046
    if (terrno != 0) code = terrno;
×
1047
    TAOS_RETURN(code);
×
1048
  }
1049

1050
  // cluster info
1051
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
×
1052
  pClusterInfo->monitor_interval = tsMonitorInterval;
×
1053
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
×
1054
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
×
1055
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
×
1056
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
×
1057
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
×
1058

1059
  void *pIter = NULL;
×
1060
  while (1) {
×
1061
    SDnodeObj *pObj = NULL;
×
1062
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
×
1063
    if (pIter == NULL) break;
×
1064

1065
    SMonDnodeDesc desc = {0};
×
1066
    desc.dnode_id = pObj->id;
×
1067
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
×
1068
    if (mndIsDnodeOnline(pObj, ms)) {
×
1069
      tstrncpy(desc.status, "ready", sizeof(desc.status));
×
1070
    } else {
1071
      tstrncpy(desc.status, "offline", sizeof(desc.status));
×
1072
    }
1073
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
×
1074
      mError("failed put dnode into array, but continue at this monitor report")
×
1075
    }
1076
    sdbRelease(pSdb, pObj);
×
1077
  }
1078

1079
  pIter = NULL;
×
1080
  while (1) {
×
1081
    SMnodeObj *pObj = NULL;
×
1082
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
×
1083
    if (pIter == NULL) break;
×
1084

1085
    SMonMnodeDesc desc = {0};
×
1086
    desc.mnode_id = pObj->id;
×
1087
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
×
1088

1089
    if (pObj->id == pMnode->selfDnodeId) {
×
1090
      pClusterInfo->first_ep_dnode_id = pObj->id;
×
1091
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
×
1092
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1093
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
×
1094
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1095
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
×
1096
      desc.syncState = TAOS_SYNC_STATE_LEADER;
×
1097
    } else {
1098
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1099
      desc.syncState = pObj->syncState;
×
1100
    }
1101
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
×
1102
      mError("failed to put mnode into array, but continue at this monitor report");
×
1103
    }
1104
    sdbRelease(pSdb, pObj);
×
1105
  }
1106

1107
  // vgroup info
1108
  pIter = NULL;
×
1109
  while (1) {
×
1110
    SVgObj *pVgroup = NULL;
×
1111
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
×
1112
    if (pIter == NULL) break;
×
1113

1114
    pClusterInfo->vgroups_total++;
×
1115
    pClusterInfo->tbs_total += pVgroup->numOfTables;
×
1116

1117
    SMonVgroupDesc desc = {0};
×
1118
    desc.vgroup_id = pVgroup->vgId;
×
1119

1120
    SName name = {0};
×
1121
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1122
    if (code < 0) {
×
1123
      mError("failed to get db name since %s", tstrerror(code));
×
1124
      sdbRelease(pSdb, pVgroup);
×
1125
      TAOS_RETURN(code);
×
1126
    }
1127
    (void)tNameGetDbName(&name, desc.database_name);
×
1128

1129
    desc.tables_num = pVgroup->numOfTables;
×
1130
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
×
1131
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
×
1132
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
×
1133
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
×
1134
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
×
1135
      pVnDesc->dnode_id = pVgid->dnodeId;
×
1136
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
×
1137
      pVnDesc->syncState = pVgid->syncState;
×
1138
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
×
1139
        tstrncpy(desc.status, "ready", sizeof(desc.status));
×
1140
        pClusterInfo->vgroups_alive++;
×
1141
      }
1142
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
×
1143
        pClusterInfo->vnodes_alive++;
×
1144
      }
1145
      pClusterInfo->vnodes_total++;
×
1146
    }
1147

1148
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
×
1149
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1150
    }
1151
    sdbRelease(pSdb, pVgroup);
×
1152
  }
1153

1154
  // stb info
1155
  pIter = NULL;
×
1156
  while (1) {
×
1157
    SStbObj *pStb = NULL;
×
1158
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
×
1159
    if (pIter == NULL) break;
×
1160

1161
    SMonStbDesc desc = {0};
×
1162

1163
    SName name1 = {0};
×
1164
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1165
    if (code < 0) {
×
1166
      mError("failed to get db name since %s", tstrerror(code));
×
1167
      sdbRelease(pSdb, pStb);
×
1168
      TAOS_RETURN(code);
×
1169
    }
1170
    (void)tNameGetDbName(&name1, desc.database_name);
×
1171

1172
    SName name2 = {0};
×
1173
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1174
    if (code < 0) {
×
1175
      mError("failed to get table name since %s", tstrerror(code));
×
1176
      sdbRelease(pSdb, pStb);
×
1177
      TAOS_RETURN(code);
×
1178
    }
1179
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
×
1180

1181
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
×
1182
      mError("failed to put stb into array, but continue at this monitor report");
×
1183
    }
1184
    sdbRelease(pSdb, pStb);
×
1185
  }
1186

1187
  // grant info
1188
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
×
1189
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
×
1190
  if (pMnode->grant.expireTimeMS == 0) {
×
1191
    pGrantInfo->expire_time = 0;
×
1192
    pGrantInfo->timeseries_total = 0;
×
1193
  }
1194

1195
  mndReleaseRpc(pMnode);
×
1196
  TAOS_RETURN(code);
×
1197
}
1198

1199
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
2,347✔
1200
  mTrace("mnode get load");
2,347✔
1201
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
2,347✔
1202
  pLoad->syncState = state.state;
2,347✔
1203
  pLoad->syncRestore = state.restored;
2,347✔
1204
  pLoad->syncTerm = state.term;
2,347✔
1205
  pLoad->roleTimeMs = state.roleTimeMs;
2,347✔
1206
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
2,347✔
1207
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1208
  return 0;
2,347✔
1209
}
1210

1211
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
620✔
1212
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
620✔
1213
  return state.roleTimeMs;
620✔
1214
}
1215

1216
void mndSetRestored(SMnode *pMnode, bool restored) {
60✔
1217
  if (restored) {
60!
1218
    (void)taosThreadRwlockWrlock(&pMnode->lock);
60✔
1219
    pMnode->restored = true;
60✔
1220
    (void)taosThreadRwlockUnlock(&pMnode->lock);
60✔
1221
    mInfo("mnode set restored:%d", restored);
60!
1222
  } else {
1223
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1224
    pMnode->restored = false;
×
1225
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1226
    mInfo("mnode set restored:%d", restored);
×
1227
    while (1) {
1228
      if (pMnode->rpcRef <= 0) break;
×
1229
      taosMsleep(3);
×
1230
    }
1231
  }
1232
}
60✔
1233

1234
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1235

1236
void mndSetStop(SMnode *pMnode) {
60✔
1237
  (void)taosThreadRwlockWrlock(&pMnode->lock);
60✔
1238
  pMnode->stopped = true;
60✔
1239
  (void)taosThreadRwlockUnlock(&pMnode->lock);
60✔
1240
  mInfo("mnode set stopped");
60!
1241
}
60✔
1242

1243
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
22,723✔
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