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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

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

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

69.64
/source/dnode/mnode/impl/src/mndMain.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndAcct.h"
18
#include "mndAnode.h"
19
#include "mndArbGroup.h"
20
#include "mndCluster.h"
21
#include "mndCompact.h"
22
#include "mndCompactDetail.h"
23
#include "mndConfig.h"
24
#include "mndConsumer.h"
25
#include "mndDb.h"
26
#include "mndDnode.h"
27
#include "mndFunc.h"
28
#include "mndGrant.h"
29
#include "mndIndex.h"
30
#include "mndInfoSchema.h"
31
#include "mndMnode.h"
32
#include "mndPerfSchema.h"
33
#include "mndPrivilege.h"
34
#include "mndProfile.h"
35
#include "mndQnode.h"
36
#include "mndQuery.h"
37
#include "mndShow.h"
38
#include "mndSma.h"
39
#include "mndSnode.h"
40
#include "mndStb.h"
41
#include "mndStream.h"
42
#include "mndSubscribe.h"
43
#include "mndSync.h"
44
#include "mndTelem.h"
45
#include "mndTopic.h"
46
#include "mndTrans.h"
47
#include "mndUser.h"
48
#include "mndVgroup.h"
49
#include "mndView.h"
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

232
static void mndPullupGrant(SMnode *pMnode) {
3,143✔
233
  mTrace("pullup grant msg");
3,143✔
234
  int32_t contLen = 0;
3,143✔
235
  void   *pReq = mndBuildTimerMsg(&contLen);
3,143✔
236
  if (pReq != NULL) {
3,143!
237
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
3,143✔
238
                      .pCont = pReq,
239
                      .contLen = contLen,
240
                      .info.notFreeAhandle = 1,
241
                      .info.ahandle = 0};
242
    // TODO check return value
243
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
3,143!
244
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
245
    }
246
  }
247
}
3,143✔
248

249
static void mndIncreaseUpTime(SMnode *pMnode) {
32✔
250
  mTrace("increate uptime");
32✔
251
  int32_t contLen = 0;
32✔
252
  void   *pReq = mndBuildTimerMsg(&contLen);
32✔
253
  if (pReq != NULL) {
32!
254
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
32✔
255
                      .pCont = pReq,
256
                      .contLen = contLen,
257
                      .info.notFreeAhandle = 1,
258
                      .info.ahandle = 0};
259
    // TODO check return value
260
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
32!
261
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
262
    }
263
  }
264
}
32✔
265

266
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
109✔
267
  SSdb *pSdb = pMnode->pSdb;
109✔
268

269
  void *pIter = NULL;
109✔
270
  while (1) {
248✔
271
    SVgObj *pVgroup = NULL;
357✔
272
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
357✔
273
    if (pIter == NULL) break;
357✔
274

275
    bool stateChanged = false;
248✔
276
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
644✔
277
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
484✔
278
      if (pGid->dnodeId == dnodeId) {
484✔
279
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
88✔
280
          mInfo(
47!
281
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:error restored:0 "
282
              "canRead:0",
283
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
284
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
47✔
285
          pGid->syncRestore = 0;
47✔
286
          pGid->syncCanRead = 0;
47✔
287
          pGid->startTimeMs = 0;
47✔
288
          stateChanged = true;
47✔
289
        }
290
        break;
88✔
291
      }
292
    }
293

294
    if (stateChanged) {
248✔
295
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
47✔
296
      if (pDb != NULL && pDb->stateTs != curMs) {
47!
297
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
36!
298
              curMs);
299
        pDb->stateTs = curMs;
36✔
300
      }
301
      mndReleaseDb(pMnode, pDb);
47✔
302
    }
303

304
    sdbRelease(pSdb, pVgroup);
248✔
305
  }
306
}
109✔
307

308
static void mndCheckDnodeOffline(SMnode *pMnode) {
7,931✔
309
  mTrace("check dnode offline");
7,931✔
310
  if (mndAcquireRpc(pMnode) != 0) return;
7,931✔
311

312
  SSdb   *pSdb = pMnode->pSdb;
7,697✔
313
  int64_t curMs = taosGetTimestampMs();
7,697✔
314

315
  void *pIter = NULL;
7,697✔
316
  while (1) {
13,957✔
317
    SDnodeObj *pDnode = NULL;
21,654✔
318
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
21,654✔
319
    if (pIter == NULL) break;
21,654✔
320

321
    bool online = mndIsDnodeOnline(pDnode, curMs);
13,957✔
322
    if (!online) {
13,957✔
323
      mInfo("dnode:%d, in offline state", pDnode->id);
109!
324
      mndSetVgroupOffline(pMnode, pDnode->id, curMs);
109✔
325
    }
326

327
    sdbRelease(pSdb, pDnode);
13,957✔
328
  }
329

330
  mndReleaseRpc(pMnode);
7,697✔
331
}
332

333
static bool mnodeIsNotLeader(SMnode *pMnode) {
20,816✔
334
  terrno = 0;
20,816✔
335
  (void)taosThreadRwlockRdlock(&pMnode->lock);
20,816✔
336
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
20,816✔
337
  if (terrno != 0) {
20,816!
338
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
339
    return true;
×
340
  }
341

342
  if (state.state != TAOS_SYNC_STATE_LEADER) {
20,816✔
343
    (void)taosThreadRwlockUnlock(&pMnode->lock);
651✔
344
    terrno = TSDB_CODE_SYN_NOT_LEADER;
651✔
345
    return true;
651✔
346
  }
347
  if (!state.restored || !pMnode->restored) {
20,165!
348
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1✔
349
    terrno = TSDB_CODE_SYN_RESTORING;
1✔
350
    return true;
1✔
351
  }
352
  (void)taosThreadRwlockUnlock(&pMnode->lock);
20,164✔
353
  return false;
20,164✔
354
}
355

356
static int32_t minCronTime() {
42,318✔
357
  int32_t min = INT32_MAX;
42,318✔
358
  min = TMIN(min, tsTtlPushIntervalSec);
42,318✔
359
  min = TMIN(min, tsTrimVDbIntervalSec);
42,318✔
360
  min = TMIN(min, tsS3MigrateIntervalSec);
42,318✔
361
  min = TMIN(min, tsTransPullupInterval);
42,318✔
362
  min = TMIN(min, tsCompactPullupInterval);
42,318✔
363
  min = TMIN(min, tsMqRebalanceInterval);
42,318✔
364
  min = TMIN(min, tsStreamCheckpointInterval);
42,318✔
365
  min = TMIN(min, tsStreamNodeCheckInterval);
42,318✔
366
  min = TMIN(min, tsArbHeartBeatIntervalSec);
42,318✔
367
  min = TMIN(min, tsArbCheckSyncIntervalSec);
42,318✔
368

369
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
42,318✔
370
  min = TMIN(min, telemInt);
42,318✔
371
  min = TMIN(min, tsGrantHBInterval);
42,318✔
372
  min = TMIN(min, tsUptimeInterval);
42,318✔
373

374
  return min <= 1 ? 2 : min;
42,318✔
375
}
376
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
41,666✔
377
  int32_t code = 0;
41,666✔
378
  if (sec % tsTtlPushIntervalSec == 0) {
41,666✔
379
    mndPullupTtl(pMnode);
3,966✔
380
  }
381

382
  if (sec % tsTrimVDbIntervalSec == 0) {
41,666!
383
    mndPullupTrimDb(pMnode);
×
384
  }
385

386
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
41,666!
387
    mndPullupS3MigrateDb(pMnode);
×
388
  }
389

390
  if (sec % tsTransPullupInterval == 0) {
41,666✔
391
    mndPullupTrans(pMnode);
20,972✔
392
  }
393

394
  if (sec % tsCompactPullupInterval == 0) {
41,666✔
395
    mndPullupCompacts(pMnode);
3,640✔
396
  }
397

398
  if (sec % tsMqRebalanceInterval == 0) {
41,666✔
399
    mndCalMqRebalance(pMnode);
20,161✔
400
  }
401

402
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
41,666✔
403
    mndStreamCheckpointTimer(pMnode);
1,013✔
404
  }
405

406
  if (sec % tsStreamNodeCheckInterval == 0) {
41,666✔
407
    mndStreamCheckNode(pMnode);
1,658✔
408
  }
409

410
  if (sec % 5 == 0) {
41,666✔
411
    mndStreamConsensusChkpt(pMnode);
7,827✔
412
  }
413

414
  if (sec % tsTelemInterval == (TMIN(86400, (tsTelemInterval - 1)))) {
41,666✔
415
    mndPullupTelem(pMnode);
2✔
416
  }
417

418
  if (sec % tsGrantHBInterval == 0) {
41,666✔
419
    mndPullupGrant(pMnode);
3,143✔
420
  }
421

422
  if (sec % tsUptimeInterval == 0) {
41,666✔
423
    mndIncreaseUpTime(pMnode);
32✔
424
  }
425

426
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
41,666✔
427
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
7,827!
428
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
429
    }
430
  }
431

432
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
41,666✔
433
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
3,640!
434
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
435
    }
436
  }
437
}
41,666✔
438
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
42,318✔
439
  if (sec % (tsStatusInterval * 5) == 0) {
42,318✔
440
    mndCheckDnodeOffline(pMnode);
7,931✔
441
  }
442
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
42,318✔
443
    mndSyncCheckTimeout(pMnode);
1,039✔
444
  }
445
}
42,318✔
446

447
static void *mndThreadFp(void *param) {
1,365✔
448
  SMnode *pMnode = param;
1,365✔
449
  int64_t lastTime = 0;
1,365✔
450
  setThreadName("mnode-timer");
1,365✔
451

452
  while (1) {
429,711✔
453
    lastTime++;
431,076✔
454
    taosMsleep(100);
431,076✔
455
    if (mndGetStop(pMnode)) break;
431,076✔
456
    if (lastTime % 10 != 0) continue;
429,711✔
457

458
    int64_t sec = lastTime / 10;
42,318✔
459
    mndDoTimerCheckTask(pMnode, sec);
42,318✔
460

461
    int64_t minCron = minCronTime();
42,318✔
462
    if (sec % minCron == 0 && mnodeIsNotLeader(pMnode)) {
42,318✔
463
      // not leader, do nothing
464
      mTrace("timer not process since mnode is not leader, reason: %s", tstrerror(terrno));
652!
465
      terrno = 0;
652✔
466
      continue;
652✔
467
    }
468
    mndDoTimerPullupTask(pMnode, sec);
41,666✔
469
  }
470

471
  return NULL;
1,365✔
472
}
473

474
static int32_t mndInitTimer(SMnode *pMnode) {
1,365✔
475
  int32_t      code = 0;
1,365✔
476
  TdThreadAttr thAttr;
477
  (void)taosThreadAttrInit(&thAttr);
1,365✔
478
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,365✔
479
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode)) != 0) {
1,365!
480
    mError("failed to create timer thread since %s", tstrerror(code));
×
481
    TAOS_RETURN(code);
×
482
  }
483

484
  (void)taosThreadAttrDestroy(&thAttr);
1,365✔
485
  tmsgReportStartup("mnode-timer", "initialized");
1,365✔
486
  TAOS_RETURN(code);
1,365✔
487
}
488

489
static void mndCleanupTimer(SMnode *pMnode) {
1,365✔
490
  if (taosCheckPthreadValid(pMnode->thread)) {
1,365!
491
    (void)taosThreadJoin(pMnode->thread, NULL);
1,365✔
492
    taosThreadClear(&pMnode->thread);
1,365✔
493
  }
494
}
1,365✔
495

496
static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
1,366✔
497
  int32_t code = 0;
1,366✔
498
  pMnode->path = taosStrdup(path);
1,366!
499
  if (pMnode->path == NULL) {
1,366!
500
    code = terrno;
×
501
    TAOS_RETURN(code);
×
502
  }
503

504
  if (taosMkDir(pMnode->path) != 0) {
1,366!
505
    code = terrno;
×
506
    TAOS_RETURN(code);
×
507
  }
508

509
  TAOS_RETURN(code);
1,366✔
510
}
511

512
static int32_t mndInitWal(SMnode *pMnode) {
1,366✔
513
  int32_t code = 0;
1,366✔
514
  char    path[PATH_MAX + 20] = {0};
1,366✔
515
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
1,366✔
516
  SWalCfg cfg = {.vgId = 1,
1,366✔
517
                 .fsyncPeriod = 0,
518
                 .rollPeriod = -1,
519
                 .segSize = -1,
520
                 .committed = -1,
521
                 .retentionPeriod = 0,
522
                 .retentionSize = 0,
523
                 .level = TAOS_WAL_FSYNC,
524
                 .encryptAlgorithm = 0,
525
                 .encryptKey = {0}};
526

527
#if defined(TD_ENTERPRISE)
528
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
1,366!
529
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
530
    if (tsEncryptKey[0] == '\0') {
×
531
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
532
      TAOS_RETURN(code);
×
533
    } else {
534
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
535
    }
536
  }
537
#endif
538

539
  pMnode->pWal = walOpen(path, &cfg);
1,366✔
540
  if (pMnode->pWal == NULL) {
1,366!
541
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
542
    if (terrno != 0) code = terrno;
×
543
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
544
    TAOS_RETURN(code);
×
545
  }
546

547
  TAOS_RETURN(code);
1,366✔
548
}
549

550
static void mndCloseWal(SMnode *pMnode) {
1,365✔
551
  if (pMnode->pWal != NULL) {
1,365!
552
    walClose(pMnode->pWal);
1,365✔
553
    pMnode->pWal = NULL;
1,365✔
554
  }
555
}
1,365✔
556

557
static int32_t mndInitSdb(SMnode *pMnode) {
1,366✔
558
  int32_t code = 0;
1,366✔
559
  SSdbOpt opt = {0};
1,366✔
560
  opt.path = pMnode->path;
1,366✔
561
  opt.pMnode = pMnode;
1,366✔
562
  opt.pWal = pMnode->pWal;
1,366✔
563

564
  pMnode->pSdb = sdbInit(&opt);
1,366✔
565
  if (pMnode->pSdb == NULL) {
1,366!
566
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
567
    if (terrno != 0) code = terrno;
×
568
    TAOS_RETURN(code);
×
569
  }
570

571
  TAOS_RETURN(code);
1,366✔
572
}
573

574
static int32_t mndOpenSdb(SMnode *pMnode) {
1,366✔
575
  int32_t code = 0;
1,366✔
576
  if (!pMnode->deploy) {
1,366✔
577
    code = sdbReadFile(pMnode->pSdb);
279✔
578
  }
579

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

582
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
1,366✔
583
  return code;
1,366✔
584
}
585

586
static void mndCleanupSdb(SMnode *pMnode) {
1,365✔
587
  if (pMnode->pSdb) {
1,365!
588
    sdbCleanup(pMnode->pSdb);
1,365✔
589
    pMnode->pSdb = NULL;
1,365✔
590
  }
591
}
1,365✔
592

593
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
49,176✔
594
  SMnodeStep step = {0};
49,176✔
595
  step.name = name;
49,176✔
596
  step.initFp = initFp;
49,176✔
597
  step.cleanupFp = cleanupFp;
49,176✔
598
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
98,352!
599
    TAOS_RETURN(terrno);
×
600
  }
601

602
  TAOS_RETURN(0);
49,176✔
603
}
604

605
static int32_t mndInitSteps(SMnode *pMnode) {
1,366✔
606
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-wal", mndInitWal, mndCloseWal));
1,366!
607
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb));
1,366!
608
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans));
1,366!
609
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster));
1,366!
610
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode));
1,366!
611
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-qnode", mndInitQnode, mndCleanupQnode));
1,366!
612
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-snode", mndInitSnode, mndCleanupSnode));
1,366!
613
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-anode", mndInitAnode, mndCleanupAnode));
1,366!
614
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-arbgroup", mndInitArbGroup, mndCleanupArbGroup));
1,366!
615
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-config", mndInitConfig, NULL));
1,366!
616
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode));
1,366!
617
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser));
1,366!
618
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-grant", mndInitGrant, mndCleanupGrant));
1,366!
619
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-privilege", mndInitPrivilege, mndCleanupPrivilege));
1,366!
620
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct));
1,366!
621
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stream", mndInitStream, mndCleanupStream));
1,366!
622
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic));
1,366!
623
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-consumer", mndInitConsumer, mndCleanupConsumer));
1,366!
624
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-subscribe", mndInitSubscribe, mndCleanupSubscribe));
1,366!
625
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup));
1,366!
626
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb));
1,366!
627
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sma", mndInitSma, mndCleanupSma));
1,366!
628
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-idx", mndInitIdx, mndCleanupIdx));
1,366!
629
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-infos", mndInitInfos, mndCleanupInfos));
1,366!
630
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-perfs", mndInitPerfs, mndCleanupPerfs));
1,366!
631
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb));
1,366!
632
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc));
1,366!
633
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-view", mndInitView, mndCleanupView));
1,366!
634
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact", mndInitCompact, mndCleanupCompact));
1,366!
635
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-compact-detail", mndInitCompactDetail, mndCleanupCompactDetail));
1,366!
636
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sdb", mndOpenSdb, NULL));
1,366!
637
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-profile", mndInitProfile, mndCleanupProfile));
1,366!
638
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-show", mndInitShow, mndCleanupShow));
1,366!
639
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-query", mndInitQuery, mndCleanupQuery));
1,366!
640
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-sync", mndInitSync, mndCleanupSync));
1,366!
641
  TAOS_CHECK_RETURN(mndAllocStep(pMnode, "mnode-telem", mndInitTelem, mndCleanupTelem));
1,366!
642
  return 0;
1,366✔
643
}
644

645
static void mndCleanupSteps(SMnode *pMnode, int32_t pos) {
1,365✔
646
  if (pMnode->pSteps == NULL) return;
1,365!
647

648
  if (pos == -1) {
1,365!
649
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
1,365✔
650
  }
651

652
  for (int32_t s = pos; s >= 0; s--) {
50,505✔
653
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
49,140✔
654
    mInfo("%s will cleanup", pStep->name);
49,140!
655
    if (pStep->cleanupFp != NULL) {
49,140✔
656
      (*pStep->cleanupFp)(pMnode);
46,410✔
657
    }
658
  }
659

660
  taosArrayClear(pMnode->pSteps);
1,365✔
661
  taosArrayDestroy(pMnode->pSteps);
1,365✔
662
  pMnode->pSteps = NULL;
1,365✔
663
}
664

665
static int32_t mndExecSteps(SMnode *pMnode) {
1,366✔
666
  int32_t code = 0;
1,366✔
667
  int32_t size = taosArrayGetSize(pMnode->pSteps);
1,366✔
668
  for (int32_t pos = 0; pos < size; pos++) {
50,542✔
669
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
49,176✔
670
    if (pStep->initFp == NULL) continue;
49,176!
671

672
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
49,176!
673
      mError("%s exec failed since %s, start to cleanup", pStep->name, tstrerror(code));
×
674
      mndCleanupSteps(pMnode, pos);
×
675
      TAOS_RETURN(code);
×
676
    } else {
677
      mInfo("%s is initialized", pStep->name);
49,176!
678
      tmsgReportStartup(pStep->name, "initialized");
49,176✔
679
    }
680
  }
681

682
  pMnode->clusterId = mndGetClusterId(pMnode);
1,366✔
683
  TAOS_RETURN(0);
1,366✔
684
}
685

686
static void mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) {
1,366✔
687
  pMnode->msgCb = pOption->msgCb;
1,366✔
688
  pMnode->selfDnodeId = pOption->dnodeId;
1,366✔
689
  pMnode->syncMgmt.selfIndex = pOption->selfIndex;
1,366✔
690
  pMnode->syncMgmt.numOfReplicas = pOption->numOfReplicas;
1,366✔
691
  pMnode->syncMgmt.numOfTotalReplicas = pOption->numOfTotalReplicas;
1,366✔
692
  pMnode->syncMgmt.lastIndex = pOption->lastIndex;
1,366✔
693
  (void)memcpy(pMnode->syncMgmt.replicas, pOption->replicas, sizeof(pOption->replicas));
1,366✔
694
  (void)memcpy(pMnode->syncMgmt.nodeRoles, pOption->nodeRoles, sizeof(pOption->nodeRoles));
1,366✔
695
}
1,366✔
696

697
SMnode *mndOpen(const char *path, const SMnodeOpt *pOption) {
1,366✔
698
  terrno = 0;
1,366✔
699
  mInfo("start to open mnode in %s", path);
1,366!
700

701
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
1,366!
702
  if (pMnode == NULL) {
1,366!
703
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
704
    mError("failed to open mnode since %s", terrstr());
×
705
    return NULL;
×
706
  }
707
  (void)memset(pMnode, 0, sizeof(SMnode));
1,366✔
708

709
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
1,366✔
710
  if (code != 0) {
1,366!
711
    taosMemoryFree(pMnode);
×
712
    mError("failed to open mnode lock since %s", tstrerror(code));
×
713
    return NULL;
×
714
  }
715

716
  char timestr[24] = "1970-01-01 00:00:00.00";
1,366✔
717
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
1,366✔
718
  if (code < 0) {
1,366!
719
    mError("failed to parse time since %s", tstrerror(code));
×
720
    (void)taosThreadRwlockDestroy(&pMnode->lock);
×
721
    taosMemoryFree(pMnode);
×
722
    return NULL;
×
723
  }
724
  mndSetOptions(pMnode, pOption);
1,366✔
725

726
  pMnode->deploy = pOption->deploy;
1,366✔
727
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
1,366✔
728
  if (pMnode->pSteps == NULL) {
1,366!
729
    taosMemoryFree(pMnode);
×
730
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
731
    mError("failed to open mnode since %s", terrstr());
×
732
    return NULL;
×
733
  }
734

735
  code = mndCreateDir(pMnode, path);
1,366✔
736
  if (code != 0) {
1,366!
737
    code = terrno;
×
738
    mError("failed to open mnode since %s", tstrerror(code));
×
739
    mndClose(pMnode);
×
740
    terrno = code;
×
741
    return NULL;
×
742
  }
743

744
  code = mndInitSteps(pMnode);
1,366✔
745
  if (code != 0) {
1,366!
746
    code = terrno;
×
747
    mError("failed to open mnode since %s", tstrerror(code));
×
748
    mndClose(pMnode);
×
749
    terrno = code;
×
750
    return NULL;
×
751
  }
752

753
  code = mndExecSteps(pMnode);
1,366✔
754
  if (code != 0) {
1,366!
755
    code = terrno;
×
756
    mError("failed to open mnode since %s", tstrerror(code));
×
757
    mndClose(pMnode);
×
758
    terrno = code;
×
759
    return NULL;
×
760
  }
761

762
  mInfo("mnode open successfully");
1,366!
763
  return pMnode;
1,366✔
764
}
765

766
void mndPreClose(SMnode *pMnode) {
1,365✔
767
  if (pMnode != NULL) {
1,365!
768
    int32_t code = 0;
1,365✔
769
    // TODO check return value
770
    code = syncLeaderTransfer(pMnode->syncMgmt.sync);
1,365✔
771
    if (code < 0) {
1,365!
772
      mError("failed to transfer leader since %s", tstrerror(code));
×
773
    }
774
    syncPreStop(pMnode->syncMgmt.sync);
1,365✔
775
    code = sdbWriteFile(pMnode->pSdb, 0);
1,365✔
776
    if (code < 0) {
1,365!
777
      mError("failed to write sdb since %s", tstrerror(code));
×
778
    }
779
  }
780
}
1,365✔
781

782
void mndClose(SMnode *pMnode) {
1,365✔
783
  if (pMnode != NULL) {
1,365!
784
    mInfo("start to close mnode");
1,365!
785
    mndCleanupSteps(pMnode, -1);
1,365✔
786
    taosMemoryFreeClear(pMnode->path);
1,365!
787
    taosMemoryFreeClear(pMnode);
1,365!
788
    mInfo("mnode is closed");
1,365!
789
  }
790
}
1,365✔
791

792
int32_t mndStart(SMnode *pMnode) {
1,365✔
793
  mndSyncStart(pMnode);
1,365✔
794
  if (pMnode->deploy) {
1,365✔
795
    if (sdbDeploy(pMnode->pSdb) != 0) {
1,087!
796
      mError("failed to deploy sdb while start mnode");
×
797
      return -1;
×
798
    }
799
    mndSetRestored(pMnode, true);
1,087✔
800
  }
801
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
1,365✔
802

803
  return mndInitTimer(pMnode);
1,365✔
804
}
805

806
int32_t mndIsCatchUp(SMnode *pMnode) {
303✔
807
  int64_t rid = pMnode->syncMgmt.sync;
303✔
808
  return syncIsCatchUp(rid);
303✔
809
}
810

811
ESyncRole mndGetRole(SMnode *pMnode) {
303✔
812
  int64_t rid = pMnode->syncMgmt.sync;
303✔
813
  return syncGetRole(rid);
303✔
814
}
815

816
int64_t mndGetTerm(SMnode *pMnode) {
3,649✔
817
  int64_t rid = pMnode->syncMgmt.sync;
3,649✔
818
  return syncGetTerm(rid);
3,649✔
819
}
820

821
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
11,346✔
822

823
void mndStop(SMnode *pMnode) {
1,365✔
824
  mndSetStop(pMnode);
1,365✔
825
  mndSyncStop(pMnode);
1,365✔
826
  mndCleanupTimer(pMnode);
1,365✔
827
}
1,365✔
828

829
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
105,733✔
830
  SMnode    *pMnode = pMsg->info.node;
105,733✔
831
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
105,733✔
832

833
  const STraceId *trace = &pMsg->info.traceId;
105,733✔
834
  mGTrace("vgId:1, sync msg:%p will be processed, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
105,733!
835

836
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
105,733✔
837
  if (code != 0) {
105,732✔
838
    mGError("vgId:1, failed to process sync msg:%p type:%s, reason: %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
2!
839
            tstrerror(code), code);
840
  }
841

842
  return code;
105,732✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
485,610✔
846
  int32_t code = 0;
485,610✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
485,610✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
394,218✔
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
379,808!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
371,964✔
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
343,146✔
852
    TAOS_RETURN(code);
51,077✔
853
  }
854

855
  SMnode *pMnode = pMsg->info.node;
343,141✔
856
  (void)taosThreadRwlockRdlock(&pMnode->lock);
343,141✔
857
  if (pMnode->stopped) {
343,249!
UNCOV
858
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
859
    code = TSDB_CODE_APP_IS_STOPPING;
×
UNCOV
860
    TAOS_RETURN(code);
×
861
  }
862

863
  terrno = 0;
343,249✔
864
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
343,136✔
865
  if (terrno != 0) {
343,264!
866
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
867
    code = terrno;
×
868
    TAOS_RETURN(code);
×
869
  }
870

871
  if (state.state != TAOS_SYNC_STATE_LEADER) {
343,265✔
872
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,152✔
873
    code = TSDB_CODE_SYN_NOT_LEADER;
4,152✔
874
    goto _OVER;
4,152✔
875
  }
876

877
  if (!state.restored || !pMnode->restored) {
339,113✔
878
    (void)taosThreadRwlockUnlock(&pMnode->lock);
107✔
879
    code = TSDB_CODE_SYN_RESTORING;
107✔
880
    goto _OVER;
107✔
881
  }
882

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
339,006✔
885
#else
886
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
887
  mTrace("mnode rpc is acquired, ref:%d", ref);
888
#endif
889

890
  (void)taosThreadRwlockUnlock(&pMnode->lock);
339,007✔
891
  TAOS_RETURN(code);
339,002✔
892

893
_OVER:
4,259✔
894
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
4,259!
895
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
4,259!
896
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
4,218!
897
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
4,218!
898
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
4,218!
899
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
3,545!
900
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER) {
3,415!
901
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
844!
902
           pMnode->stopped, state.restored, syncStr(state.state));
903
    TAOS_RETURN(code);
844✔
904
  }
905

906
  const STraceId *trace = &pMsg->info.traceId;
3,415✔
907
  SEpSet          epSet = {0};
3,415✔
908
  mndGetMnodeEpSet(pMnode, &epSet);
3,415✔
909

910
  mGDebug(
3,415!
911
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
912
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
913
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
914
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
915

916
  if (epSet.numOfEps <= 0) return -1;
3,415!
917

918
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
13,234✔
919
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
9,819!
920
  }
921

922
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
3,415✔
923
  pMsg->info.rsp = rpcMallocCont(contLen);
3,415✔
924
  if (pMsg->info.rsp != NULL) {
3,415!
925
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
3,415!
926
      mError("failed to serialize ep set");
×
927
    }
928
    pMsg->info.hasEpSet = 1;
3,415✔
929
    pMsg->info.rspLen = contLen;
3,415✔
930
  }
931

932
  TAOS_RETURN(code);
3,415✔
933
}
934

935
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
485,612✔
936
  SMnode         *pMnode = pMsg->info.node;
485,612✔
937
  const STraceId *trace = &pMsg->info.traceId;
485,612✔
938
  int32_t         code = TSDB_CODE_SUCCESS;
485,612✔
939

940
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
485,612✔
941
  MndMsgFpExt fpExt = NULL;
485,612✔
942
  if (fp == NULL) {
485,612✔
943
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
51,181✔
944
    if (fpExt == NULL) {
51,181!
945
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
946
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
947
      TAOS_RETURN(code);
×
948
    }
949
  }
950

951
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
485,612✔
952

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
481,559!
954
  if (fp)
481,559✔
955
    code = (*fp)(pMsg);
430,379✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
51,180✔
958
  mndReleaseRpc(pMnode);
481,572✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
481,579✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
71,822!
962
  } else if (code == 0) {
409,757✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
403,537!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
6,220✔
967
      code = terrno;
4✔
968
    }
969
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
6,220!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
481,579✔
974
}
975

976
void mndSetMsgHandle(SMnode *pMnode, tmsg_t msgType, MndMsgFp fp) {
234,952✔
977
  tmsg_t type = TMSG_INDEX(msgType);
234,952✔
978
  if (type < TDMT_MAX) {
234,952!
979
    pMnode->msgFp[type] = fp;
234,952✔
980
  }
981
}
234,952✔
982

983
void mndSetMsgHandleExt(SMnode *pMnode, tmsg_t msgType, MndMsgFpExt fp) {
10,928✔
984
  tmsg_t type = TMSG_INDEX(msgType);
10,928✔
985
  if (type < TDMT_MAX) {
10,928!
986
    pMnode->msgFpExt[type] = fp;
10,928✔
987
  }
988
}
10,928✔
989

990
// Note: uid 0 is reserved
991
int64_t mndGenerateUid(const char *name, int32_t len) {
29,396✔
992
  int32_t hashval = MurmurHash3_32(name, len);
29,396✔
993
  do {
×
994
    int64_t us = taosGetTimestampUs();
29,398✔
995
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
29,398✔
996
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
29,398✔
997
    if (uuid) {
29,398!
998
      return llabs(uuid);
29,398✔
999
    }
1000
  } while (true);
1001
}
1002

1003
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
12✔
1004
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1005
  int32_t code = mndAcquireRpc(pMnode);
12✔
1006
  if (code < 0) {
12!
1007
    TAOS_RETURN(code);
×
1008
  } else if (code == 1) {
12!
1009
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1010
  }
1011

1012
  SSdb   *pSdb = pMnode->pSdb;
12✔
1013
  int64_t ms = taosGetTimestampMs();
12✔
1014

1015
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
12✔
1016
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
12✔
1017
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
12✔
1018
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
12✔
1019
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
12!
1020
      pStbInfo->stbs == NULL) {
12!
1021
    mndReleaseRpc(pMnode);
×
1022
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1023
    if (terrno != 0) code = terrno;
×
1024
    TAOS_RETURN(code);
×
1025
  }
1026

1027
  // cluster info
1028
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
12✔
1029
  pClusterInfo->monitor_interval = tsMonitorInterval;
12✔
1030
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
12✔
1031
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
12✔
1032
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
12✔
1033
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
12✔
1034
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
12✔
1035

1036
  void *pIter = NULL;
12✔
1037
  while (1) {
12✔
1038
    SDnodeObj *pObj = NULL;
24✔
1039
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
24✔
1040
    if (pIter == NULL) break;
24✔
1041

1042
    SMonDnodeDesc desc = {0};
12✔
1043
    desc.dnode_id = pObj->id;
12✔
1044
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
12✔
1045
    if (mndIsDnodeOnline(pObj, ms)) {
12✔
1046
      tstrncpy(desc.status, "ready", sizeof(desc.status));
11✔
1047
    } else {
1048
      tstrncpy(desc.status, "offline", sizeof(desc.status));
1✔
1049
    }
1050
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
24!
1051
      mError("failed put dnode into array, but continue at this monitor report")
×
1052
    }
1053
    sdbRelease(pSdb, pObj);
12✔
1054
  }
1055

1056
  pIter = NULL;
12✔
1057
  while (1) {
12✔
1058
    SMnodeObj *pObj = NULL;
24✔
1059
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
24✔
1060
    if (pIter == NULL) break;
24✔
1061

1062
    SMonMnodeDesc desc = {0};
12✔
1063
    desc.mnode_id = pObj->id;
12✔
1064
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
12✔
1065

1066
    if (pObj->id == pMnode->selfDnodeId) {
12!
1067
      pClusterInfo->first_ep_dnode_id = pObj->id;
12✔
1068
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
12✔
1069
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1070
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
12✔
1071
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1072
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
12✔
1073
      desc.syncState = TAOS_SYNC_STATE_LEADER;
12✔
1074
    } else {
1075
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1076
      desc.syncState = pObj->syncState;
×
1077
    }
1078
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
24!
1079
      mError("failed to put mnode into array, but continue at this monitor report");
×
1080
    }
1081
    sdbRelease(pSdb, pObj);
12✔
1082
  }
1083

1084
  // vgroup info
1085
  pIter = NULL;
12✔
1086
  while (1) {
26✔
1087
    SVgObj *pVgroup = NULL;
38✔
1088
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
38✔
1089
    if (pIter == NULL) break;
38✔
1090

1091
    pClusterInfo->vgroups_total++;
26✔
1092
    pClusterInfo->tbs_total += pVgroup->numOfTables;
26✔
1093

1094
    SMonVgroupDesc desc = {0};
26✔
1095
    desc.vgroup_id = pVgroup->vgId;
26✔
1096

1097
    SName name = {0};
26✔
1098
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
26✔
1099
    if (code < 0) {
26!
1100
      mError("failed to get db name since %s", tstrerror(code));
×
1101
      sdbRelease(pSdb, pVgroup);
×
1102
      TAOS_RETURN(code);
×
1103
    }
1104
    (void)tNameGetDbName(&name, desc.database_name);
26✔
1105

1106
    desc.tables_num = pVgroup->numOfTables;
26✔
1107
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
26✔
1108
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
26✔
1109
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
52✔
1110
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
26✔
1111
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
26✔
1112
      pVnDesc->dnode_id = pVgid->dnodeId;
26✔
1113
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
26✔
1114
      pVnDesc->syncState = pVgid->syncState;
26✔
1115
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
26!
1116
        tstrncpy(desc.status, "ready", sizeof(desc.status));
26✔
1117
        pClusterInfo->vgroups_alive++;
26✔
1118
      }
1119
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
26!
1120
        pClusterInfo->vnodes_alive++;
26✔
1121
      }
1122
      pClusterInfo->vnodes_total++;
26✔
1123
    }
1124

1125
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
52!
1126
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1127
    }
1128
    sdbRelease(pSdb, pVgroup);
26✔
1129
  }
1130

1131
  // stb info
1132
  pIter = NULL;
12✔
1133
  while (1) {
11✔
1134
    SStbObj *pStb = NULL;
23✔
1135
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
23✔
1136
    if (pIter == NULL) break;
23✔
1137

1138
    SMonStbDesc desc = {0};
11✔
1139

1140
    SName name1 = {0};
11✔
1141
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
11✔
1142
    if (code < 0) {
11!
1143
      mError("failed to get db name since %s", tstrerror(code));
×
1144
      sdbRelease(pSdb, pStb);
×
1145
      TAOS_RETURN(code);
×
1146
    }
1147
    (void)tNameGetDbName(&name1, desc.database_name);
11✔
1148

1149
    SName name2 = {0};
11✔
1150
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
11✔
1151
    if (code < 0) {
11!
1152
      mError("failed to get table name since %s", tstrerror(code));
×
1153
      sdbRelease(pSdb, pStb);
×
1154
      TAOS_RETURN(code);
×
1155
    }
1156
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
11✔
1157

1158
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
22!
1159
      mError("failed to put stb into array, but continue at this monitor report");
×
1160
    }
1161
    sdbRelease(pSdb, pStb);
11✔
1162
  }
1163

1164
  // grant info
1165
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
12✔
1166
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
12✔
1167
  if (pMnode->grant.expireTimeMS == 0) {
12!
1168
    pGrantInfo->expire_time = 0;
×
1169
    pGrantInfo->timeseries_total = 0;
×
1170
  }
1171

1172
  mndReleaseRpc(pMnode);
12✔
1173
  TAOS_RETURN(code);
12✔
1174
}
1175

1176
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
42,123✔
1177
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
42,123✔
1178
  pLoad->syncState = state.state;
42,123✔
1179
  pLoad->syncRestore = state.restored;
42,123✔
1180
  pLoad->syncTerm = state.term;
42,123✔
1181
  pLoad->roleTimeMs = state.roleTimeMs;
42,123✔
1182
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
42,123✔
1183
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1184
  return 0;
42,123✔
1185
}
1186

1187
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
3,640✔
1188
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
3,640✔
1189
  return state.roleTimeMs;
3,640✔
1190
}
1191

1192
void mndSetRestored(SMnode *pMnode, bool restored) {
1,363✔
1193
  if (restored) {
1,363!
1194
    (void)taosThreadRwlockWrlock(&pMnode->lock);
1,363✔
1195
    pMnode->restored = true;
1,363✔
1196
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,363✔
1197
    mInfo("mnode set restored:%d", restored);
1,363!
1198
  } else {
1199
    (void)taosThreadRwlockWrlock(&pMnode->lock);
×
1200
    pMnode->restored = false;
×
1201
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
1202
    mInfo("mnode set restored:%d", restored);
×
1203
    while (1) {
1204
      if (pMnode->rpcRef <= 0) break;
×
1205
      taosMsleep(3);
×
1206
    }
1207
  }
1208
}
1,363✔
1209

1210
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1211

1212
void mndSetStop(SMnode *pMnode) {
1,365✔
1213
  (void)taosThreadRwlockWrlock(&pMnode->lock);
1,365✔
1214
  pMnode->stopped = true;
1,365✔
1215
  (void)taosThreadRwlockUnlock(&pMnode->lock);
1,365✔
1216
  mInfo("mnode set stopped");
1,365!
1217
}
1,365✔
1218

1219
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
431,076✔
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