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

taosdata / TDengine / #4404

30 Jun 2025 02:45AM UTC coverage: 62.241% (-0.4%) from 62.635%
#4404

push

travis-ci

web-flow
Merge pull request #31480 from taosdata/docs/3.0/TD-34215

add stmt2 docs

153837 of 315978 branches covered (48.69%)

Branch coverage included in aggregate %.

238272 of 314005 relevant lines covered (75.88%)

6134648.6 hits per line

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

68.43
/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) {
9,937✔
53
  int32_t code = 0;
9,937✔
54
  (void)taosThreadRwlockRdlock(&pMnode->lock);
9,937✔
55
  if (pMnode->stopped) {
9,937!
56
    code = TSDB_CODE_APP_IS_STOPPING;
×
57
  } else if (!mndIsLeader(pMnode)) {
9,937!
58
    code = 1;
×
59
  } else {
60
#if 1
61
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
9,937✔
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);
9,937✔
68
  TAOS_RETURN(code);
9,937✔
69
}
70

71
static inline void mndReleaseRpc(SMnode *pMnode) {
618,122✔
72
  (void)taosThreadRwlockRdlock(&pMnode->lock);
618,122✔
73
#if 1
74
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
618,146✔
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);
618,136✔
80
}
618,139✔
81

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

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

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

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

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

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

135
static void mndPullupTrimDb(SMnode *pMnode) {
×
136
  mTrace("pullup s3migrate");
×
137
  int32_t contLen = 0;
×
138
  void   *pReq = mndBuildTimerMsg(&contLen);
×
139
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRIM_DB_TIMER, .pCont = pReq, .contLen = contLen};
×
140
  // TODO check return value
141
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
×
142
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
143
  }
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) {
26,335✔
158
  mTrace("pullup arb hb");
26,335✔
159
  int32_t contLen = 0;
26,335✔
160
  void   *pReq = mndBuildTimerMsg(&contLen);
26,335✔
161
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
26,335✔
162
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
26,335✔
163
}
164

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

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

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

196
static void mndStreamCheckNode(SMnode *pMnode) {
24✔
197
  int32_t contLen = 0;
24✔
198
  void   *pReq = mndBuildTimerMsg(&contLen);
24✔
199
  if (pReq != NULL) {
24!
200
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_NODECHECK_TIMER, .pCont = pReq, .contLen = contLen};
24✔
201
    // TODO check return value
202
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
24!
203
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
204
    }
205
  }
206
}
24✔
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,195✔
221
  int32_t contLen = 0;
1,195✔
222
  void   *pReq = mndBuildTimerMsg(&contLen);
1,195✔
223
  if (pReq != NULL) {
1,195!
224
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_CONSEN_TIMER, .pCont = pReq, .contLen = contLen};
1,195✔
225
    // TODO check return value
226
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,195!
227
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
228
    }
229
  }
230
}
1,195✔
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,021✔
246
  mTrace("pullup grant msg");
5,021✔
247
  int32_t contLen = 0;
5,021✔
248
  void   *pReq = mndBuildTimerMsg(&contLen);
5,021✔
249
  if (pReq != NULL) {
5,021!
250
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
5,021✔
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,021!
257
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
258
    }
259
  }
260
}
5,021✔
261

262
static void mndIncreaseUpTime(SMnode *pMnode) {
12✔
263
  mTrace("increate uptime");
12!
264
  int32_t contLen = 0;
12✔
265
  void   *pReq = mndBuildTimerMsg(&contLen);
12✔
266
  if (pReq != NULL) {
12!
267
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
12✔
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) {
12!
274
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
275
    }
276
  }
277
}
12✔
278

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

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

288
    bool stateChanged = false;
1,658✔
289
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
4,279✔
290
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
3,237✔
291
      if (pGid->dnodeId == dnodeId) {
3,237✔
292
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
616✔
293
          mInfo(
363!
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;
363✔
298
          pGid->syncRestore = 0;
363✔
299
          pGid->syncCanRead = 0;
363✔
300
          pGid->startTimeMs = 0;
363✔
301
          stateChanged = true;
363✔
302
        }
303
        break;
616✔
304
      }
305
    }
306

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

317
    sdbRelease(pSdb, pVgroup);
1,658✔
318
  }
319
}
414✔
320

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

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

328
  void *pIter = NULL;
9,921✔
329
  while (1) {
17,231✔
330
    SDnodeObj *pDnode = NULL;
27,152✔
331
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
27,152✔
332
    if (pIter == NULL) break;
27,152✔
333

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

340
    sdbRelease(pSdb, pDnode);
17,231✔
341
  }
342

343
  mndReleaseRpc(pMnode);
9,921✔
344
}
345

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

355
  if (state.state != TAOS_SYNC_STATE_LEADER) {
58,450✔
356
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,822✔
357
    terrno = TSDB_CODE_SYN_NOT_LEADER;
4,822✔
358
    return true;
4,822✔
359
  }
360
  if (!state.restored || !pMnode->restored) {
53,628!
361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
42✔
362
    terrno = TSDB_CODE_SYN_RESTORING;
42✔
363
    return true;
42✔
364
  }
365
  (void)taosThreadRwlockUnlock(&pMnode->lock);
53,586✔
366
  return false;
53,586✔
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) {
53,586✔
390
  int32_t code = 0;
53,586✔
391
#ifndef TD_ASTRA  
392
  if (sec % tsTtlPushIntervalSec == 0) {
53,586✔
393
    mndPullupTtl(pMnode);
5,125✔
394
  }
395

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

409
  if (sec % tsCompactPullupInterval == 0) {
53,586✔
410
    mndPullupCompacts(pMnode);
4,539✔
411
  }
412
#ifdef USE_TOPIC
413
  if (sec % tsMqRebalanceInterval == 0) {
53,586✔
414
    mndCalMqRebalance(pMnode);
26,334✔
415
  }
416
#endif
417
#ifdef USE_STREAM
418
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
53,586✔
419
    mndStreamCheckpointTimer(pMnode);
1,195✔
420
  }
421

422
  if (sec % tsStreamNodeCheckInterval == 0) {
53,586✔
423
    mndStreamCheckNode(pMnode);
24✔
424
  }
425

426
  if (sec % (tsStreamFailedTimeout/1000) == 0) {
53,586✔
427
    mndStreamCheckStatus(pMnode);
1✔
428
  }
429

430
  if (sec % 30 == 0) {
53,586✔
431
    mndStreamConsensusChkpt(pMnode);
1,195✔
432
  }
433

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

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

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

474
  while (1) {
594,211✔
475
    lastTime++;
596,372✔
476
    taosMsleep(100);
596,372✔
477

478
    if (mndGetStop(pMnode)) break;
596,372✔
479
    if (lastTime % 10 != 0) continue;
594,211✔
480

481
    if (mnodeIsNotLeader(pMnode)) {
58,450✔
482
      mTrace("timer not process since mnode is not leader");
4,864!
483
      continue;
4,864✔
484
    }
485

486
    int64_t sec = lastTime / 10;
53,586✔
487
    mndDoTimerCheckTask(pMnode, sec);
53,586✔
488

489
    mndDoTimerPullupTask(pMnode, sec);
53,586✔
490
  }
491

492
  return NULL;
2,161✔
493
}
494

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

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

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

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

528
  if (taosMkDir(pMnode->path) != 0) {
2,162!
529
    code = terrno;
×
530
    TAOS_RETURN(code);
×
531
  }
532

533
  TAOS_RETURN(code);
2,162✔
534
}
535

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

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

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

571
  TAOS_RETURN(code);
2,162✔
572
}
573

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

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

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

595
  TAOS_RETURN(code);
2,162✔
596
}
597

598
static int32_t mndOpenSdb(SMnode *pMnode) {
2,162✔
599
  int32_t code = 0;
2,162✔
600
  if (!pMnode->deploy) {
2,162✔
601
    code = sdbReadFile(pMnode->pSdb);
538✔
602
  }
603

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

606
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
2,162✔
607
  return code;
2,162✔
608
}
609

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

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

626
  TAOS_RETURN(0);
79,994✔
627
}
628

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

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

673
  if (pos == -1) {
2,161!
674
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
2,161✔
675
  }
676

677
  for (int32_t s = pos; s >= 0; s--) {
82,118✔
678
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
79,957✔
679
    mInfo("%s will cleanup", pStep->name);
79,957!
680
    if (pStep->cleanupFp != NULL) {
79,957✔
681
      (*pStep->cleanupFp)(pMnode);
75,635✔
682
    }
683
  }
684

685
  taosArrayClear(pMnode->pSteps);
2,161✔
686
  taosArrayDestroy(pMnode->pSteps);
2,161✔
687
  pMnode->pSteps = NULL;
2,161✔
688
}
689

690
static int32_t mndExecSteps(SMnode *pMnode) {
2,162✔
691
  int32_t code = 0;
2,162✔
692
  int32_t size = taosArrayGetSize(pMnode->pSteps);
2,162✔
693
  for (int32_t pos = 0; pos < size; pos++) {
82,156✔
694
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
79,994✔
695
    if (pStep->initFp == NULL) continue;
79,994!
696

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

707
  pMnode->clusterId = mndGetClusterId(pMnode);
2,162✔
708
  TAOS_RETURN(0);
2,162✔
709
}
710

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

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

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

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

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

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

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

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

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

787
  mInfo("mnode open successfully");
2,162!
788
  return pMnode;
2,162✔
789
}
790

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

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

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

828
  return mndInitTimer(pMnode);
2,161✔
829
}
830

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

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

841
int64_t mndGetTerm(SMnode *pMnode) {
17,314✔
842
  int64_t rid = pMnode->syncMgmt.sync;
17,314✔
843
  return syncGetTerm(rid);
17,314✔
844
}
845

846
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
43,651✔
847

848
void mndStop(SMnode *pMnode) {
2,161✔
849
  mndSetStop(pMnode);
2,161✔
850
  mndSyncStop(pMnode);
2,161✔
851
  mndCleanupTimer(pMnode);
2,161✔
852
}
2,161✔
853

854
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
164,422✔
855
  SMnode    *pMnode = pMsg->info.node;
164,422✔
856
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
164,422✔
857

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

861
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
164,422✔
862
  if (code != 0) {
164,421✔
863
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
4!
864
            tstrerror(code), code);
865
  }
866

867
  return code;
164,421✔
868
}
869

870
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
638,287✔
871
  int32_t code = 0;
638,287✔
872
  if (!IsReq(pMsg)) TAOS_RETURN(code);
638,287✔
873
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
547,570✔
874
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
530,219!
875
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
518,579✔
876
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
484,133✔
877
    TAOS_RETURN(code);
63,440✔
878
  }
879

880
  SMnode *pMnode = pMsg->info.node;
484,130✔
881
  (void)taosThreadRwlockRdlock(&pMnode->lock);
484,130✔
882
  if (pMnode->stopped) {
484,659✔
883
    (void)taosThreadRwlockUnlock(&pMnode->lock);
45✔
884
    code = TSDB_CODE_APP_IS_STOPPING;
45✔
885
    TAOS_RETURN(code);
45✔
886
  }
887

888
  terrno = 0;
484,614✔
889
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
484,346✔
890
  if (terrno != 0) {
484,641!
891
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
892
    code = terrno;
×
893
    TAOS_RETURN(code);
×
894
  }
895

896
  if (state.state != TAOS_SYNC_STATE_LEADER) {
484,633✔
897
    (void)taosThreadRwlockUnlock(&pMnode->lock);
29,099✔
898
    code = TSDB_CODE_SYN_NOT_LEADER;
29,101✔
899
    goto _OVER;
29,101✔
900
  }
901

902
  if (!state.restored || !pMnode->restored) {
455,534✔
903
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,665✔
904
    code = TSDB_CODE_SYN_RESTORING;
1,663✔
905
    goto _OVER;
1,663✔
906
  }
907

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

915
  (void)taosThreadRwlockUnlock(&pMnode->lock);
453,853✔
916
  TAOS_RETURN(code);
453,851✔
917

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

931
  const STraceId *trace = &pMsg->info.traceId;
30,764✔
932
  SEpSet          epSet = {0};
30,764✔
933
  mndGetMnodeEpSet(pMnode, &epSet);
30,764✔
934

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

941
  if (epSet.numOfEps <= 0) return -1;
30,764!
942

943
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
120,052✔
944
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
89,288✔
945
  }
946

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

957
  TAOS_RETURN(code);
30,764✔
958
}
959

960
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
638,231✔
961
  SMnode         *pMnode = pMsg->info.node;
638,231✔
962
  const STraceId *trace = &pMsg->info.traceId;
638,231✔
963
  int32_t         code = TSDB_CODE_SUCCESS;
638,231✔
964

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

976
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
638,231✔
977

978
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
608,106!
979
  if (fp)
608,106✔
980
    code = (*fp)(pMsg);
544,494✔
981
  else
982
    code = (*fpExt)(pMsg, pQueueInfo);
63,612✔
983
  mndReleaseRpc(pMnode);
608,188✔
984

985
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
608,203✔
986
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
87,819!
987
  } else if (code == 0) {
520,384✔
988
    mGTrace("msg:%p, successfully processed", pMsg);
512,020!
989
  } else {
990
    // TODO removve this wrong set code
991
    if (code == -1) {
8,364✔
992
      code = terrno;
8✔
993
    }
994
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
8,364!
995
            TMSG_INFO(pMsg->msgType));
996
  }
997

998
  TAOS_RETURN(code);
608,203✔
999
}
1000

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

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

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

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

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

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

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

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

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

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

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

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

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

1116
    pClusterInfo->vgroups_total++;
38✔
1117
    pClusterInfo->tbs_total += pVgroup->numOfTables;
38✔
1118

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

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

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

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

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

1163
    SMonStbDesc desc = {0};
15✔
1164

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

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

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

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

1197
  mndReleaseRpc(pMnode);
16✔
1198
  TAOS_RETURN(code);
16✔
1199
}
1200

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

1213
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
17,259✔
1214
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
17,259✔
1215
  return state.roleTimeMs;
17,259✔
1216
}
1217

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

1236
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1237

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

1245
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
596,372✔
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