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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

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

70
static inline void mndReleaseRpc(SMnode *pMnode) {
25,779✔
71
  (void)taosThreadRwlockRdlock(&pMnode->lock);
25,779✔
72
#if 1
73
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
25,782✔
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);
25,781✔
79
}
25,781✔
80

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

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

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

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

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

123
static void mndPullupTtl(SMnode *pMnode) {
158✔
124
  mTrace("pullup ttl");
158!
125
  int32_t contLen = 0;
158✔
126
  void   *pReq = mndBuildTimerMsg(&contLen);
158✔
127
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_TTL_TIMER, .pCont = pReq, .contLen = contLen};
158✔
128
  // TODO check return value
129
  if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
158!
130
    mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
131
  }
132
}
158✔
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) {
321✔
157
  mTrace("pullup arb hb");
321✔
158
  int32_t contLen = 0;
321✔
159
  void   *pReq = mndBuildTimerMsg(&contLen);
321✔
160
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_ARB_HEARTBEAT_TIMER, .pCont = pReq, .contLen = contLen, .info.noResp = 1};
321✔
161
  return tmsgPutToQueue(&pMnode->msgCb, ARB_QUEUE, &rpcMsg);
321✔
162
}
163

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

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

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

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

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

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

232
static void mndPullupGrant(SMnode *pMnode) {
53✔
233
  mTrace("pullup grant msg");
53✔
234
  int32_t contLen = 0;
53✔
235
  void   *pReq = mndBuildTimerMsg(&contLen);
53✔
236
  if (pReq != NULL) {
53!
237
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
53✔
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) {
53!
244
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
245
    }
246
  }
247
}
53✔
248

249
static void mndIncreaseUpTime(SMnode *pMnode) {
1✔
250
  mTrace("increate uptime");
1!
251
  int32_t contLen = 0;
1✔
252
  void   *pReq = mndBuildTimerMsg(&contLen);
1✔
253
  if (pReq != NULL) {
1!
254
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
1✔
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) {
1!
261
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
262
    }
263
  }
264
}
1✔
265

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

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

275
    bool stateChanged = false;
10✔
276
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
40✔
277
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
30✔
278
      if (pGid->dnodeId == dnodeId) {
30!
UNCOV
279
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
×
UNCOV
280
          mInfo(
×
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);
UNCOV
284
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
×
UNCOV
285
          pGid->syncRestore = 0;
×
UNCOV
286
          pGid->syncCanRead = 0;
×
UNCOV
287
          pGid->startTimeMs = 0;
×
UNCOV
288
          stateChanged = true;
×
289
        }
UNCOV
290
        break;
×
291
      }
292
    }
293

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

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

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

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

315
  void *pIter = NULL;
321✔
316
  while (1) {
1,354✔
317
    SDnodeObj *pDnode = NULL;
1,675✔
318
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
1,675✔
319
    if (pIter == NULL) break;
1,675✔
320

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

327
    sdbRelease(pSdb, pDnode);
1,354✔
328
  }
329

330
  mndReleaseRpc(pMnode);
321✔
331
}
332

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

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

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

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

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

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

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

390
  if (sec % tsTransPullupInterval == 0) {
1,627✔
391
    mndPullupTrans(pMnode);
1,246✔
392
  }
393

394
  if (sec % tsCompactPullupInterval == 0) {
1,627✔
395
    mndPullupCompacts(pMnode);
158✔
396
  }
397

398
  if (sec % tsMqRebalanceInterval == 0) {
1,627✔
399
    mndCalMqRebalance(pMnode);
809✔
400
  }
401

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

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

410
  if (sec % 5 == 0) {
1,627✔
411
    mndStreamConsensusChkpt(pMnode);
321✔
412
  }
413

414
  if (sec % tsTelemInterval == (TMIN(86400, (tsTelemInterval - 1)))) {
1,627!
UNCOV
415
    mndPullupTelem(pMnode);
×
416
  }
417

418
  if (sec % tsGrantHBInterval == 0) {
1,627✔
419
    mndPullupGrant(pMnode);
53✔
420
  }
421

422
  if (sec % tsUptimeInterval == 0) {
1,627✔
423
    mndIncreaseUpTime(pMnode);
1✔
424
  }
425

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

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

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

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

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

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

471
  return NULL;
13✔
472
}
473

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

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

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

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

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

509
  TAOS_RETURN(code);
13✔
510
}
511

512
static int32_t mndInitWal(SMnode *pMnode) {
13✔
513
  int32_t code = 0;
13✔
514
  char    path[PATH_MAX + 20] = {0};
13✔
515
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
13✔
516
  SWalCfg cfg = {.vgId = 1,
13✔
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) {
13!
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);
13✔
540
  if (pMnode->pWal == NULL) {
13!
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);
13✔
548
}
549

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

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

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

571
  TAOS_RETURN(code);
13✔
572
}
573

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

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

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

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

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

602
  TAOS_RETURN(0);
468✔
603
}
604

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

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

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

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

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

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

672
    if ((code = (*pStep->initFp)(pMnode)) != 0) {
468!
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);
468!
678
      tmsgReportStartup(pStep->name, "initialized");
468✔
679
    }
680
  }
681

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

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

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

701
  SMnode *pMnode = taosMemoryCalloc(1, sizeof(SMnode));
13!
702
  if (pMnode == NULL) {
13!
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));
13✔
708

709
  int32_t code = taosThreadRwlockInit(&pMnode->lock, NULL);
13✔
710
  if (code != 0) {
13!
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";
13✔
717
  code = taosParseTime(timestr, &pMnode->checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL);
13✔
718
  if (code < 0) {
13!
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);
13✔
725

726
  pMnode->deploy = pOption->deploy;
13✔
727
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
13✔
728
  if (pMnode->pSteps == NULL) {
13!
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);
13✔
736
  if (code != 0) {
13!
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);
13✔
745
  if (code != 0) {
13!
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);
13✔
754
  if (code != 0) {
13!
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");
13!
763
  return pMnode;
13✔
764
}
765

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

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

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

803
  return mndInitTimer(pMnode);
13✔
804
}
805

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

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

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

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

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

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

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

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

842
  return code;
1,599✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
25,446✔
846
  int32_t code = 0;
25,446✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
25,446✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
23,389!
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
23,323!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
23,254!
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
23,118!
852
    TAOS_RETURN(code);
271✔
853
  }
854

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

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

871
  if (state.state != TAOS_SYNC_STATE_LEADER) {
23,130!
UNCOV
872
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
873
    code = TSDB_CODE_SYN_NOT_LEADER;
×
UNCOV
874
    goto _OVER;
×
875
  }
876

877
  if (!state.restored || !pMnode->restored) {
23,130!
UNCOV
878
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
UNCOV
879
    code = TSDB_CODE_SYN_RESTORING;
×
UNCOV
880
    goto _OVER;
×
881
  }
882

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
23,130✔
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);
23,130✔
891
  TAOS_RETURN(code);
23,129✔
892

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

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

UNCOV
910
  mGDebug(
×
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

UNCOV
916
  if (epSet.numOfEps <= 0) return -1;
×
917

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

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

UNCOV
932
  TAOS_RETURN(code);
×
933
}
934

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

940
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
25,448✔
941
  MndMsgFpExt fpExt = NULL;
25,448✔
942
  if (fp == NULL) {
25,448✔
943
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
274✔
944
    if (fpExt == NULL) {
274!
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));
25,448!
952

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
25,459!
954
  if (fp)
25,459✔
955
    code = (*fp)(pMsg);
25,185✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
274✔
958
  mndReleaseRpc(pMnode);
25,461✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
25,460✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
408!
962
  } else if (code == 0) {
25,052✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
24,996!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
56!
UNCOV
967
      code = terrno;
×
968
    }
969
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
56!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
25,460✔
974
}
975

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

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

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

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

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

UNCOV
1015
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
×
UNCOV
1016
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
×
UNCOV
1017
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
×
UNCOV
1018
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
×
UNCOV
1019
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
×
UNCOV
1020
      pStbInfo->stbs == NULL) {
×
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
UNCOV
1028
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
×
UNCOV
1029
  pClusterInfo->monitor_interval = tsMonitorInterval;
×
UNCOV
1030
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
×
UNCOV
1031
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
×
UNCOV
1032
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
×
UNCOV
1033
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
×
UNCOV
1034
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
×
1035

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

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

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

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

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

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

UNCOV
1091
    pClusterInfo->vgroups_total++;
×
UNCOV
1092
    pClusterInfo->tbs_total += pVgroup->numOfTables;
×
1093

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

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

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

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

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

UNCOV
1138
    SMonStbDesc desc = {0};
×
1139

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

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

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

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

UNCOV
1172
  mndReleaseRpc(pMnode);
×
UNCOV
1173
  TAOS_RETURN(code);
×
1174
}
1175

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

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

1192
void mndSetRestored(SMnode *pMnode, bool restored) {
13✔
1193
  if (restored) {
13!
1194
    (void)taosThreadRwlockWrlock(&pMnode->lock);
13✔
1195
    pMnode->restored = true;
13✔
1196
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13✔
1197
    mInfo("mnode set restored:%d", restored);
13!
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
}
13✔
1209

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

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

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