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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

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

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

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

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

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

71
static inline void mndReleaseRpc(SMnode *pMnode) {
4,087,078✔
72
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,087,078✔
73
#if 1
74
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
4,087,542✔
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);
4,087,635✔
80
}
4,087,489✔
81

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

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

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

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

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

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

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

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

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

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

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

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

220
static void mndStreamConsensusChkpt(SMnode *pMnode) {
1,926✔
221
  int32_t contLen = 0;
1,926✔
222
  void   *pReq = mndBuildTimerMsg(&contLen);
1,926✔
223
  if (pReq != NULL) {
1,926!
224
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_CONSEN_TIMER, .pCont = pReq, .contLen = contLen};
1,926✔
225
    // TODO check return value
226
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
1,926!
227
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
228
    }
229
  }
230
}
1,926✔
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) {
6,111✔
246
  mTrace("pullup grant msg");
6,111✔
247
  int32_t contLen = 0;
6,111✔
248
  void   *pReq = mndBuildTimerMsg(&contLen);
6,111✔
249
  if (pReq != NULL) {
6,111!
250
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
6,111✔
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) {
6,111!
257
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
258
    }
259
  }
260
}
6,111✔
261

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

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

328
  void *pIter = NULL;
14,750✔
329
  while (1) {
26,063✔
330
    SDnodeObj *pDnode = NULL;
40,813✔
331
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
40,813✔
332
    if (pIter == NULL) break;
40,813✔
333

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

340
    sdbRelease(pSdb, pDnode);
26,063✔
341
  }
342

343
  mndReleaseRpc(pMnode);
14,750✔
344
}
345

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

355
  if (state.state != TAOS_SYNC_STATE_LEADER) {
83,104✔
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) {
78,022!
361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
38✔
362
    terrno = TSDB_CODE_SYN_RESTORING;
38✔
363
    return true;
38✔
364
  }
365
  (void)taosThreadRwlockUnlock(&pMnode->lock);
77,984✔
366
  return false;
77,984✔
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) {
77,984✔
390
  int32_t code = 0;
77,984✔
391
#ifndef TD_ASTRA
392
  if (sec % tsGrantHBInterval == 0) {  // put in the 1st place as to take effect ASAP
77,984✔
393
    mndPullupGrant(pMnode);
6,111✔
394
  }
395
  if (sec % tsTtlPushIntervalSec == 0) {
77,984✔
396
    mndPullupTtl(pMnode);
7,446✔
397
  }
398

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

412
  if (sec % tsCompactPullupInterval == 0) {
77,984✔
413
    mndPullupCompacts(pMnode);
6,859✔
414
  }
415
#ifdef USE_TOPIC
416
  if (sec % tsMqRebalanceInterval == 0) {
77,984✔
417
    mndCalMqRebalance(pMnode);
38,424✔
418
  }
419
#endif
420
#ifdef USE_STREAM
421
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
77,984✔
422
    mndStreamCheckpointTimer(pMnode);
1,926✔
423
  }
424

425
  if (sec % tsStreamNodeCheckInterval == 0) {
77,984✔
426
    mndStreamCheckNode(pMnode);
102✔
427
  }
428

429
  if (sec % (tsStreamFailedTimeout / 1000) == 0) {
77,984✔
430
    mndStreamCheckStatus(pMnode);
8✔
431
  }
432

433
  if (sec % 30 == 0) {
77,984✔
434
    mndStreamConsensusChkpt(pMnode);
1,926✔
435
  }
436

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

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

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

472
  while (1) {
842,770✔
473
    lastTime++;
845,260✔
474
    taosMsleep(100);
845,260✔
475

476
    if (mndGetStop(pMnode)) break;
845,260✔
477
    if (lastTime % 10 != 0) continue;
842,770✔
478

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

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

487
    mndDoTimerPullupTask(pMnode, sec);
77,984✔
488
  }
489

490
  return NULL;
2,490✔
491
}
492

493
static int32_t mndInitTimer(SMnode *pMnode) {
2,490✔
494
  int32_t      code = 0;
2,490✔
495
  TdThreadAttr thAttr;
496
  (void)taosThreadAttrInit(&thAttr);
2,490✔
497
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,490✔
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,490!
UNCOV
502
    mError("failed to create timer thread since %s", tstrerror(code));
×
UNCOV
503
    TAOS_RETURN(code);
×
504
  }
505

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

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

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

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

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

534
static int32_t mndInitWal(SMnode *pMnode) {
2,491✔
535
  int32_t code = 0;
2,491✔
536
  char    path[PATH_MAX + 20] = {0};
2,491✔
537
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
2,491✔
538
  SWalCfg cfg = {.vgId = 1,
2,491✔
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,491!
551
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
552
    if (tsEncryptKey[0] == '\0') {
×
UNCOV
553
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
554
      TAOS_RETURN(code);
×
555
    } else {
UNCOV
556
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
557
    }
558
  }
559
#endif
560

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

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

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

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

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

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

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

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

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

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

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

624
  TAOS_RETURN(0);
92,167✔
625
}
626

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

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

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

675
  for (int32_t s = pos; s >= 0; s--) {
94,620✔
676
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
92,130✔
677
    mInfo("%s will cleanup", pStep->name);
92,130!
678
    if (pStep->cleanupFp != NULL) {
92,130✔
679
      (*pStep->cleanupFp)(pMnode);
87,150✔
680
    }
681
  }
682

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

826
  return mndInitTimer(pMnode);
2,490✔
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) {
25,359✔
840
  int64_t rid = pMnode->syncMgmt.sync;
25,359✔
841
  return syncGetTerm(rid);
25,359✔
842
}
843

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

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

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

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

859
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
208,038✔
860
  if (code != 0) {
208,037✔
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;
208,037✔
866
}
867

868
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
4,096,693✔
869
  int32_t code = 0;
4,096,693✔
870
  if (!IsReq(pMsg)) TAOS_RETURN(code);
4,096,693✔
871
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
3,923,992✔
872
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
3,677,052!
873
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
3,526,571✔
874
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
3,033,455✔
875
    TAOS_RETURN(code);
890,544✔
876
  }
877

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

886
  terrno = 0;
3,034,125✔
887
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
3,033,687✔
888
  if (terrno != 0) {
3,034,176!
889
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
890
    code = terrno;
×
UNCOV
891
    TAOS_RETURN(code);
×
892
  }
893

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

900
  if (!state.restored || !pMnode->restored) {
3,010,843✔
901
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,883✔
902
    code = TSDB_CODE_SYN_RESTORING;
1,859✔
903
    goto _OVER;
1,859✔
904
  }
905

906
#if 1
907
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
3,008,960✔
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);
3,009,008✔
914
  TAOS_RETURN(code);
3,008,968✔
915

916
_OVER:
25,185✔
917
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
25,185!
918
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
25,188!
919
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
25,188!
920
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
25,189!
921
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
25,189!
922
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
25,189!
923
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER) {
25,189!
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));
UNCOV
926
    TAOS_RETURN(code);
×
927
  }
928

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

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

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

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

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

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

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

974
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
4,096,902✔
975

976
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
4,072,405!
977
  if (fp)
4,072,405✔
978
    code = (*fp)(pMsg);
3,181,538✔
979
  else
980
    code = (*fpExt)(pMsg, pQueueInfo);
890,867✔
981
  mndReleaseRpc(pMnode);
4,072,508✔
982

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

996
  TAOS_RETURN(code);
4,072,520✔
997
}
998

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

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

1013
// Note: uid 0 is reserved
1014
int64_t mndGenerateUid(const char *name, int32_t len) {
139,765✔
1015
  int32_t hashval = MurmurHash3_32(name, len);
139,765✔
1016
  do {
1✔
1017
    int64_t us = taosGetTimestampUs();
139,772✔
1018
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
139,772✔
1019
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
139,772✔
1020
    if (uuid) {
139,775✔
1021
      return llabs(uuid);
139,774✔
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!
UNCOV
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;
×
UNCOV
1046
    if (terrno != 0) code = terrno;
×
UNCOV
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!
UNCOV
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 {
UNCOV
1098
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
UNCOV
1099
      desc.syncState = pObj->syncState;
×
1100
    }
1101
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
32!
UNCOV
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));
×
UNCOV
1124
      sdbRelease(pSdb, pVgroup);
×
UNCOV
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!
UNCOV
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));
×
UNCOV
1167
      sdbRelease(pSdb, pStb);
×
UNCOV
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));
×
UNCOV
1176
      sdbRelease(pSdb, pStb);
×
UNCOV
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!
UNCOV
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!
UNCOV
1191
    pGrantInfo->expire_time = 0;
×
UNCOV
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) {
83,114✔
1200
  mTrace("mnode get load");
83,114✔
1201
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
83,114✔
1202
  pLoad->syncState = state.state;
83,114✔
1203
  pLoad->syncRestore = state.restored;
83,114✔
1204
  pLoad->syncTerm = state.term;
83,114✔
1205
  pLoad->roleTimeMs = state.roleTimeMs;
83,114✔
1206
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
83,114✔
1207
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1208
  return 0;
83,114✔
1209
}
1210

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

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

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

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

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

© 2025 Coveralls, Inc