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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

68.22
/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
#include "mndBnode.h"
51

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

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

82
static void *mndBuildTimerMsg(int32_t *pContLen) {
126,038✔
83
  terrno = 0;
126,038✔
84
  SMTimerReq timerReq = {0};
126,038✔
85

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

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

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

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

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

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

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

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

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

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

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

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

208
static void mndStreamCheckStatus(SMnode *pMnode) {
1✔
209
  int32_t contLen = 0;
1✔
210
  void   *pReq = mndBuildTimerMsg(&contLen);
1✔
211
  if (pReq != NULL) {
1!
212
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_CHECK_STREAM_TIMER, .pCont = pReq, .contLen = contLen};
1✔
213
    // TODO check return value
214
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1!
215
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
216
    }
217
  }
218
}
1✔
219

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

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

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

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

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

282
  void *pIter = NULL;
606✔
283
  while (1) {
2,188✔
284
    SVgObj *pVgroup = NULL;
2,794✔
285
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
2,794✔
286
    if (pIter == NULL) break;
2,794✔
287

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

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

317
    sdbRelease(pSdb, pVgroup);
2,188✔
318
  }
319
}
606✔
320

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

325
  SSdb   *pSdb = pMnode->pSdb;
11,134✔
326
  int64_t curMs = taosGetTimestampMs();
11,134✔
327

328
  void *pIter = NULL;
11,134✔
329
  while (1) {
20,628✔
330
    SDnodeObj *pDnode = NULL;
31,762✔
331
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
31,762✔
332
    if (pIter == NULL) break;
31,762✔
333

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

340
    sdbRelease(pSdb, pDnode);
20,628✔
341
  }
342

343
  mndReleaseRpc(pMnode);
11,134✔
344
}
345

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

355
  if (state.state != TAOS_SYNC_STATE_LEADER) {
64,994✔
356
    (void)taosThreadRwlockUnlock(&pMnode->lock);
5,082✔
357
    terrno = TSDB_CODE_SYN_NOT_LEADER;
5,082✔
358
    return true;
5,082✔
359
  }
360
  if (!state.restored || !pMnode->restored) {
59,912!
361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
38✔
362
    terrno = TSDB_CODE_SYN_RESTORING;
38✔
363
    return true;
38✔
364
  }
365
  (void)taosThreadRwlockUnlock(&pMnode->lock);
59,874✔
366
  return false;
59,874✔
367
}
368

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

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

387
  return min <= 1 ? 2 : min;
×
388
}
389
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
59,874✔
390
  int32_t code = 0;
59,874✔
391
#ifndef TD_ASTRA
392
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
59,874✔
393
    mndPullupGrant(pMnode);
5,757✔
394
  }
395
  if (sec % tsTtlPushIntervalSec == 0) {
59,874✔
396
    mndPullupTtl(pMnode);
5,643✔
397
  }
398

399
  if (sec % tsTrimVDbIntervalSec == 0) {
59,874!
UNCOV
400
    mndPullupTrimDb(pMnode);
×
401
  }
402
#endif
403
#ifdef USE_S3
404
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
59,874!
405
    mndPullupS3MigrateDb(pMnode);
×
406
  }
407
#endif
408
  if (sec % tsTransPullupInterval == 0) {
59,874✔
409
    mndPullupTrans(pMnode);
30,196✔
410
  }
411

412
  if (sec % tsCompactPullupInterval == 0) {
59,874✔
413
    mndPullupCompacts(pMnode);
5,056✔
414
  }
415
#ifdef USE_TOPIC
416
  if (sec % tsMqRebalanceInterval == 0) {
59,874✔
417
    mndCalMqRebalance(pMnode);
29,376✔
418
  }
419
#endif
420
#ifdef USE_STREAM
421
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
59,874✔
422
    mndStreamCheckpointTimer(pMnode);
1,333✔
423
  }
424

425
  if (sec % tsStreamNodeCheckInterval == 0) {
59,874✔
426
    mndStreamCheckNode(pMnode);
34✔
427
  }
428

429
  if (sec % (tsStreamFailedTimeout / 1000) == 0) {
59,874✔
430
    mndStreamCheckStatus(pMnode);
1✔
431
  }
432

433
  if (sec % 30 == 0) {
59,874✔
434
    mndStreamConsensusChkpt(pMnode);
1,333✔
435
  }
436

437
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
59,874!
438
    mndPullupTelem(pMnode);
2✔
439
  }
440
#endif
441
  if (sec % tsUptimeInterval == 0) {
59,874✔
442
    mndIncreaseUpTime(pMnode);
14✔
443
  }
444
#ifndef TD_ASTRA
445
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
59,874✔
446
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
29,377!
UNCOV
447
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
448
    }
449
  }
450

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

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

472
  while (1) {
661,562✔
473
    lastTime++;
664,028✔
474
    taosMsleep(100);
664,028✔
475

476
    if (mndGetStop(pMnode)) break;
664,028✔
477
    if (lastTime % 10 != 0) continue;
661,562✔
478

479
    if (mnodeIsNotLeader(pMnode)) {
64,994✔
480
      mTrace("timer not process since mnode is not leader");
5,120!
481
      continue;
5,120✔
482
    }
483

484
    int64_t sec = lastTime / 10;
59,874✔
485
    mndDoTimerCheckTask(pMnode, sec);
59,874✔
486

487
    mndDoTimerPullupTask(pMnode, sec);
59,874✔
488
  }
489

490
  return NULL;
2,466✔
491
}
492

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

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

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

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

526
  if (taosMkDir(pMnode->path) != 0) {
2,467!
527
    code = terrno;
×
528
    TAOS_RETURN(code);
×
529
  }
530

531
  TAOS_RETURN(code);
2,467✔
532
}
533

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

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

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

569
  TAOS_RETURN(code);
2,467✔
570
}
571

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

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

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

593
  TAOS_RETURN(code);
2,467✔
594
}
595

596
static int32_t mndOpenSdb(SMnode *pMnode) {
2,467✔
597
  int32_t code = 0;
2,467✔
598
  if (!pMnode->deploy) {
2,467✔
599
    code = sdbReadFile(pMnode->pSdb);
607✔
600
  }
601

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

604
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
2,467✔
605
  return code;
2,467✔
606
}
607

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

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

624
  TAOS_RETURN(0);
91,279✔
625
}
626

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

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

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

675
  for (int32_t s = pos; s >= 0; s--) {
93,708✔
676
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
91,242✔
677
    mInfo("%s will cleanup", pStep->name);
91,242!
678
    if (pStep->cleanupFp != NULL) {
91,242✔
679
      (*pStep->cleanupFp)(pMnode);
86,310✔
680
    }
681
  }
682

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

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

695
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
91,279!
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);
91,279!
701
      tmsgReportStartup(pStep->name, "initialized");
91,279✔
702
    }
703
  }
704

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

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

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

724
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
2,467!
725
  if (pMnode == NULL) {
2,467!
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));
2,467✔
731

732
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
2,467✔
733
  if (code != 0) {
2,467!
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";
2,467✔
740
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
2,467✔
741
  if (code < 0) {
2,467!
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);
2,467✔
748

749
  pMnode->deploy = pOption->deploy;
2,467✔
750
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
2,467✔
751
  if (pMnode->pSteps == NULL) {
2,467!
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);
2,467✔
759
  if (code != 0) {
2,467!
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);
2,467✔
768
  if (code != 0) {
2,467!
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);
2,467✔
777
  if (code != 0) {
2,467!
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");
2,467!
786
  return pMnode;
2,467✔
787
}
788

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

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

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

826
  return mndInitTimer(pMnode);
2,466✔
827
}
828

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

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

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

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

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

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

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

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

865
  return code;
184,663✔
866
}
867

868
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
698,238✔
869
  int32_t code = 0;
698,238✔
870
  if (!IsReq(pMsg)) TAOS_RETURN(code);
698,238✔
871
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
598,545✔
872
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
579,169!
873
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
565,421✔
874
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
526,828✔
875
    TAOS_RETURN(code);
71,725✔
876
  }
877

878
  SMnode *pMnode = pMsg->info.node;
526,820✔
879
  (void)taosThreadRwlockRdlock(&pMnode->lock);
526,820✔
880
  if (pMnode->stopped) {
527,332✔
881
    (void)taosThreadRwlockUnlock(&pMnode->lock);
42✔
882
    code = TSDB_CODE_APP_IS_STOPPING;
42✔
883
    TAOS_RETURN(code);
42✔
884
  }
885

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

894
  if (state.state != TAOS_SYNC_STATE_LEADER) {
527,323✔
895
    (void)taosThreadRwlockUnlock(&pMnode->lock);
23,326✔
896
    code = TSDB_CODE_SYN_NOT_LEADER;
23,322✔
897
    goto _OVER;
23,322✔
898
  }
899

900
  if (!state.restored || !pMnode->restored) {
503,997✔
901
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,838✔
902
    code = TSDB_CODE_SYN_RESTORING;
1,834✔
903
    goto _OVER;
1,834✔
904
  }
905

906
#if 1
907
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
502,159✔
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);
502,168✔
914
  TAOS_RETURN(code);
502,158✔
915

916
_OVER:
25,156✔
917
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
25,156!
918
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
25,159!
919
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
25,159!
920
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
25,160!
921
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
25,160!
922
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
25,160!
923
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER) {
25,160!
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;
25,160✔
930
  SEpSet          epSet = {0};
25,160✔
931
  mndGetMnodeEpSet(pMnode, &epSet);
25,160✔
932

933
  mGDebug(
25,158!
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;
25,158!
940

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

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

955
  TAOS_RETURN(code);
25,157✔
956
}
957

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

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

974
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
698,215✔
975

976
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
673,771!
977
  if (fp)
673,771✔
978
    code = (*fp)(pMsg);
601,808✔
979
  else
980
    code = (*fpExt)(pMsg, pQueueInfo);
71,963✔
981
  mndReleaseRpc(pMnode);
673,802✔
982

983
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
673,828✔
984
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
98,320!
985
  } else if (code == 0) {
575,508✔
986
    mGTrace("msg:%p, successfully processed", pMsg);
566,398!
987
  } else {
988
    // TODO removve this wrong set code
989
    if (code == -1) {
9,110✔
990
      code = terrno;
8✔
991
    }
992
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
9,110!
993
            TMSG_INFO(pMsg->msgType));
994
  }
995

996
  TAOS_RETURN(code);
673,828✔
997
}
998

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

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

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

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

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

1038
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
16✔
1039
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
16✔
1040
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
16✔
1041
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
16✔
1042
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
16!
1043
      pStbInfo->stbs == NULL) {
16!
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));
16✔
1052
  pClusterInfo->monitor_interval = tsMonitorInterval;
16✔
1053
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
16✔
1054
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
16✔
1055
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
16✔
1056
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
16✔
1057
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
16✔
1058

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

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

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

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

1089
    if (pObj->id == pMnode->selfDnodeId) {
16!
1090
      pClusterInfo->first_ep_dnode_id = pObj->id;
16✔
1091
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
16✔
1092
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1093
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
16✔
1094
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1095
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
16✔
1096
      desc.syncState = TAOS_SYNC_STATE_LEADER;
16✔
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) {
32!
1102
      mError("failed to put mnode into array, but continue at this monitor report");
×
1103
    }
1104
    sdbRelease(pSdb, pObj);
16✔
1105
  }
1106

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

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

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

1120
    SName name = {0};
38✔
1121
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
38✔
1122
    if (code < 0) {
38!
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);
38✔
1128

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

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

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

1161
    SMonStbDesc desc = {0};
15✔
1162

1163
    SName name1 = {0};
15✔
1164
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
15✔
1165
    if (code < 0) {
15!
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);
15✔
1171

1172
    SName name2 = {0};
15✔
1173
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
15✔
1174
    if (code < 0) {
15!
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);
15✔
1180

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

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

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

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

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

1216
void mndSetRestored(SMnode *pMnode, bool restored) {
2,466✔
1217
  if (restored) {
2,466!
1218
    (void)taosThreadRwlockWrlock(&pMnode->lock);
2,466✔
1219
    pMnode->restored = true;
2,466✔
1220
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,466✔
1221
    mInfo("mnode set restored:%d", restored);
2,466!
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
}
2,466✔
1233

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

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

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