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

taosdata / TDengine / #3564

24 Dec 2024 05:40AM UTC coverage: 62.21% (+1.2%) from 61.045%
#3564

push

travis-ci

web-flow
Merge pull request #29289 from taosdata/fix/TD-33270-2

ci(stream):add stream unit test

138331 of 285924 branches covered (48.38%)

Branch coverage included in aggregate %.

215800 of 283329 relevant lines covered (76.17%)

19198660.97 hits per line

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

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

70
static inline void mndReleaseRpc(SMnode *pMnode) {
4,089,907✔
71
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,089,907✔
72
#if 1
73
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
4,090,610✔
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);
4,090,637✔
79
}
4,090,638✔
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

315
  void *pIter = NULL;
12,031✔
316
  while (1) {
19,619✔
317
    SDnodeObj *pDnode = NULL;
31,650✔
318
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
31,650✔
319
    if (pIter == NULL) break;
31,650✔
320

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

327
    sdbRelease(pSdb, pDnode);
19,619✔
328
  }
329

330
  mndReleaseRpc(pMnode);
12,031✔
331
}
332

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

342
  if (state.state != TAOS_SYNC_STATE_LEADER) {
33,051✔
343
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,857✔
344
    terrno = TSDB_CODE_SYN_NOT_LEADER;
1,857✔
345
    return true;
1,857✔
346
  }
347
  if (!state.restored || !pMnode->restored) {
31,194!
348
    (void)taosThreadRwlockUnlock(&pMnode->lock);
6✔
349
    terrno = TSDB_CODE_SYN_RESTORING;
6✔
350
    return true;
6✔
351
  }
352
  (void)taosThreadRwlockUnlock(&pMnode->lock);
31,188✔
353
  return false;
31,188✔
354
}
355

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

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

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

382
  if (sec % tsTrimVDbIntervalSec == 0) {
65,103✔
383
    mndPullupTrimDb(pMnode);
4✔
384
  }
385

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

390
  if (sec % tsTransPullupInterval == 0) {
65,103✔
391
    mndPullupTrans(pMnode);
31,958✔
392
  }
393

394
  if (sec % tsCompactPullupInterval == 0) {
65,103✔
395
    mndPullupCompacts(pMnode);
5,759✔
396
  }
397

398
  if (sec % tsMqRebalanceInterval == 0) {
65,103✔
399
    mndCalMqRebalance(pMnode);
31,185✔
400
  }
401

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

406
  if (sec % tsStreamNodeCheckInterval == 0) {
65,103✔
407
    mndStreamCheckNode(pMnode);
2,676✔
408
  }
409

410
  if (sec % 5 == 0) {
65,103✔
411
    mndStreamConsensusChkpt(pMnode);
12,397✔
412
  }
413

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

418
  if (sec % tsGrantHBInterval == 0) {
65,103✔
419
    mndPullupGrant(pMnode);
5,296✔
420
  }
421

422
  if (sec % tsUptimeInterval == 0) {
65,103✔
423
    mndIncreaseUpTime(pMnode);
87✔
424
  }
425

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

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

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

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

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

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

471
  return NULL;
1,773✔
472
}
473

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

602
  TAOS_RETURN(0);
63,864✔
603
}
604

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

836
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
157,737✔
837
  if (code != 0) {
157,734✔
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;
157,734✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
4,107,158✔
846
  int32_t code = 0;
4,107,158✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
4,107,158✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
3,914,862✔
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
3,686,587!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
3,549,670✔
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
3,093,033✔
852
    TAOS_RETURN(code);
821,835✔
853
  }
854

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

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

871
  if (state.state != TAOS_SYNC_STATE_LEADER) {
3,093,265✔
872
    (void)taosThreadRwlockUnlock(&pMnode->lock);
28,409✔
873
    code = TSDB_CODE_SYN_NOT_LEADER;
28,410✔
874
    goto _OVER;
28,410✔
875
  }
876

877
  if (!state.restored || !pMnode->restored) {
3,064,856✔
878
    (void)taosThreadRwlockUnlock(&pMnode->lock);
654✔
879
    code = TSDB_CODE_SYN_RESTORING;
664✔
880
    goto _OVER;
664✔
881
  }
882

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
3,064,202✔
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);
3,064,213✔
891
  TAOS_RETURN(code);
3,064,197✔
892

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

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

910
  mGDebug(
26,716!
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;
26,716!
917

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

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

932
  TAOS_RETURN(code);
26,716✔
933
}
934

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

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

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
4,078,208!
954
  if (fp)
4,078,208✔
955
    code = (*fp)(pMsg);
3,256,290✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
821,918✔
958
  mndReleaseRpc(pMnode);
4,078,124✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
4,078,576✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
849,493!
962
  } else if (code == 0) {
3,229,083✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
3,221,916!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
7,167✔
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,167!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
4,078,576✔
974
}
975

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1138
    SMonStbDesc desc = {0};
11✔
1139

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

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

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

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

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

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

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

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

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

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

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