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

taosdata / TDengine / #3560

18 Dec 2024 12:59AM UTC coverage: 60.124% (+0.3%) from 59.805%
#3560

push

travis-ci

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

merge: main to 3.0 branch

132787 of 286174 branches covered (46.4%)

Branch coverage included in aggregate %.

86 of 94 new or added lines in 18 files covered. (91.49%)

2446 existing lines in 10 files now uncovered.

209637 of 283352 relevant lines covered (73.98%)

8248618.71 hits per line

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

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

70
static inline void mndReleaseRpc(SMnode *pMnode) {
530,996✔
71
  (void)taosThreadRwlockRdlock(&pMnode->lock);
530,996✔
72
#if 1
73
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
531,005✔
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);
531,006✔
79
}
531,007✔
80

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

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

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

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

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

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

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

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

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

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

207
static void mndStreamConsensusChkpt(SMnode *pMnode) {
8,296✔
208
  int32_t contLen = 0;
8,296✔
209
  void   *pReq = mndBuildTimerMsg(&contLen);
8,296✔
210
  if (pReq != NULL) {
8,296!
211
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_CONSEN_TIMER, .pCont = pReq, .contLen = contLen};
8,296✔
212
    // TODO check return value
213
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
8,296!
214
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
215
    }
216
  }
217
}
8,296✔
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) {
4,073✔
233
  mTrace("pullup grant msg");
4,073✔
234
  int32_t contLen = 0;
4,073✔
235
  void   *pReq = mndBuildTimerMsg(&contLen);
4,073✔
236
  if (pReq != NULL) {
4,073!
237
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
4,073✔
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) {
4,073!
244
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
245
    }
246
  }
247
}
4,073✔
248

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

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

269
  void *pIter = NULL;
482✔
270
  while (1) {
3,729✔
271
    SVgObj *pVgroup = NULL;
4,211✔
272
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
4,211✔
273
    if (pIter == NULL) break;
4,211✔
274

275
    bool stateChanged = false;
3,729✔
276
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
6,658✔
277
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
4,063✔
278
      if (pGid->dnodeId == dnodeId) {
4,063✔
279
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
1,134✔
280
          mInfo(
51!
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;
51✔
285
          pGid->syncRestore = 0;
51✔
286
          pGid->syncCanRead = 0;
51✔
287
          pGid->startTimeMs = 0;
51✔
288
          stateChanged = true;
51✔
289
        }
290
        break;
1,134✔
291
      }
292
    }
293

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

304
    sdbRelease(pSdb, pVgroup);
3,729✔
305
  }
306
}
482✔
307

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

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

315
  void *pIter = NULL;
8,024✔
316
  while (1) {
15,960✔
317
    SDnodeObj *pDnode = NULL;
23,984✔
318
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
23,984✔
319
    if (pIter == NULL) break;
23,984✔
320

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

327
    sdbRelease(pSdb, pDnode);
15,960✔
328
  }
329

330
  mndReleaseRpc(pMnode);
8,024✔
331
}
332

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

342
  if (state.state != TAOS_SYNC_STATE_LEADER) {
22,361✔
343
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,348✔
344
    terrno = TSDB_CODE_SYN_NOT_LEADER;
1,348✔
345
    return true;
1,348✔
346
  }
347
  if (!state.restored || !pMnode->restored) {
21,013!
348
    (void)taosThreadRwlockUnlock(&pMnode->lock);
9✔
349
    terrno = TSDB_CODE_SYN_RESTORING;
9✔
350
    return true;
9✔
351
  }
352
  (void)taosThreadRwlockUnlock(&pMnode->lock);
21,004✔
353
  return false;
21,004✔
354
}
355

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

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

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

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

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

390
  if (sec % tsTransPullupInterval == 0) {
44,113✔
391
    mndPullupTrans(pMnode);
21,773✔
392
  }
393

394
  if (sec % tsCompactPullupInterval == 0) {
44,113✔
395
    mndPullupCompacts(pMnode);
3,789✔
396
  }
397

398
  if (sec % tsMqRebalanceInterval == 0) {
44,113✔
399
    mndCalMqRebalance(pMnode);
21,002✔
400
  }
401

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

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

410
  if (sec % 5 == 0) {
44,113✔
411
    mndStreamConsensusChkpt(pMnode);
8,296✔
412
  }
413

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

418
  if (sec % tsGrantHBInterval == 0) {
44,113✔
419
    mndPullupGrant(pMnode);
4,073✔
420
  }
421

422
  if (sec % tsUptimeInterval == 0) {
44,113✔
423
    mndIncreaseUpTime(pMnode);
33✔
424
  }
425

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

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

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

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

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

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

471
  return NULL;
1,517✔
472
}
473

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

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

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

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

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

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

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

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

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

564
  pMnode->pSdb = sdbInit(&opt);
1,518✔
565
  if (pMnode->pSdb == NULL) {
1,518!
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,518✔
572
}
573

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

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

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

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

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

602
  TAOS_RETURN(0);
54,648✔
603
}
604

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

842
  return code;
130,689✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
535,803✔
846
  int32_t code = 0;
535,803✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
535,803✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
438,291✔
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
423,493!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
415,506✔
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
385,969✔
852
    TAOS_RETURN(code);
52,324✔
853
  }
854

855
  SMnode *pMnode = pMsg->info.node;
385,967✔
856
  (void)taosThreadRwlockRdlock(&pMnode->lock);
385,967✔
857
  if (pMnode->stopped) {
386,085✔
858
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13✔
859
    code = TSDB_CODE_APP_IS_STOPPING;
13✔
860
    TAOS_RETURN(code);
13✔
861
  }
862

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

871
  if (state.state != TAOS_SYNC_STATE_LEADER) {
386,111✔
872
    (void)taosThreadRwlockUnlock(&pMnode->lock);
12,765✔
873
    code = TSDB_CODE_SYN_NOT_LEADER;
12,765✔
874
    goto _OVER;
12,765✔
875
  }
876

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

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
373,023✔
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);
373,019✔
891
  TAOS_RETURN(code);
373,020✔
892

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

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

910
  mGDebug(
11,359!
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;
11,359!
917

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

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

932
  TAOS_RETURN(code);
11,359✔
933
}
934

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

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

951
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
535,798✔
952

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
522,952!
954
  if (fp)
522,952✔
955
    code = (*fp)(pMsg);
470,516✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
52,436✔
958
  mndReleaseRpc(pMnode);
522,964✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
522,969✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
74,367!
962
  } else if (code == 0) {
448,602✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
441,530!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
7,072✔
967
      code = terrno;
4✔
968
    }
969
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
7,072!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
522,969✔
974
}
975

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

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

990
// Note: uid 0 is reserved
991
int64_t mndGenerateUid(const char *name, int32_t len) {
32,133✔
992
  int32_t hashval = MurmurHash3_32(name, len);
32,133✔
UNCOV
993
  do {
×
994
    int64_t us = taosGetTimestampUs();
32,133✔
995
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
32,133✔
996
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
32,133✔
997
    if (uuid) {
32,133!
998
      return llabs(uuid);
32,133✔
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!
UNCOV
1007
    TAOS_RETURN(code);
×
1008
  } else if (code == 1) {
12!
UNCOV
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!
UNCOV
1021
    mndReleaseRpc(pMnode);
×
UNCOV
1022
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1023
    if (terrno != 0) code = terrno;
×
UNCOV
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!
UNCOV
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 {
UNCOV
1075
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
UNCOV
1076
      desc.syncState = pObj->syncState;
×
1077
    }
1078
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
24!
UNCOV
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!
UNCOV
1100
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
1101
      sdbRelease(pSdb, pVgroup);
×
UNCOV
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!
UNCOV
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!
UNCOV
1143
      mError("failed to get db name since %s", tstrerror(code));
×
UNCOV
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));
×
UNCOV
1153
      sdbRelease(pSdb, pStb);
×
UNCOV
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!
UNCOV
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!
UNCOV
1168
    pGrantInfo->expire_time = 0;
×
UNCOV
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) {
45,078✔
1177
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
45,078✔
1178
  pLoad->syncState = state.state;
45,078✔
1179
  pLoad->syncRestore = state.restored;
45,078✔
1180
  pLoad->syncTerm = state.term;
45,078✔
1181
  pLoad->roleTimeMs = state.roleTimeMs;
45,078✔
1182
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
45,078✔
1183
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1184
  return 0;
45,078✔
1185
}
1186

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

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

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

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

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