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

taosdata / TDengine / #4380

25 Jun 2025 06:58AM UTC coverage: 62.307% (-0.09%) from 62.393%
#4380

push

travis-ci

web-flow
feat(mqtt): mqtt subscription (#30127)

* feat(mqtt): Initial commit for mqtt

* chore(xnode/mnd): xnode message handlers for mnode

* chore(mnd/xnode): mnode part for xnode

* chore(xnode/translater): fix show commands

* fix(ast/creater): fix xnode create option

* fix(xnode/ci): fix ci & doc's error codes

* chore(xnode/sql): make create/drop/show work properly

* fix(xnode/sql): commit new files

* fix(xnode/sql): commit cmake files

* fix: fix testing cases

* fix(xnode/tsc): fix tokens

* fix(ast/anode): fix anode update decl.

* fix(xnode/error): fix xnode error codes

* fix: xnode make/destroy

* chore: xnode with option & dnode id

* chore: use taosmqtt for xnode

* chore: new error code for xnode launching

* chore(xnode): new error code

* chore: header for _xnode_mgmt_mqtt

* chore: source for _xnode_mgmt_mqtt

* chore: remove test directory from cmake

* chore: remove taosmqtt for ci to compile

* chore: remove taosudf header from xnode

* chore: new window macro

* chore: remove xnode mgmt mqtt for windows compilation

* Revert "chore: remove xnode mgmt mqtt for windows compilation"

This reverts commit 197e1640c.

* chore: cleanup code

* chore: xnode mgmt comment windows part out

* chore: mgmt/mqtt, move uv head toppest

* xnode/mnode: create xnode once per dnode

* fix(xnode/systable/test): fix column count

* xnode/sdb: renumber sdb type for xnode to make start/stop order correct

* xnode/mqtt: new param mqttPort

* fix SXnode's struct type

* transfer dnode id to mqtt subscription

* tmqtt: remove uv_a linking

* tmqtt/tools: sources for tools

* tools: fix windows compilation

* tools/producer: fix windows sleep param

* tools/producer: fix uninited var rc

* make tools only for linux

* test/mnodes: wail 1 or 2 seconds for offline to be leader

* update topic producer tool for geometry data type testing

* format tool sql statements

* show xnodes' ep

* make shell auto complete xnodes

* use usleep... (continued)

156642 of 320746 branches covered (48.84%)

Branch coverage included in aggregate %.

61 of 1020 new or added lines in 21 files covered. (5.98%)

1736 existing lines in 172 files now uncovered.

242538 of 319922 relevant lines covered (75.81%)

6277604.48 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

232
static void mndPullupTelem(SMnode *pMnode) {
2✔
233
  mTrace("pullup telem msg");
2!
234
  int32_t contLen = 0;
2✔
235
  void   *pReq = mndBuildTimerMsg(&contLen);
2✔
236
  if (pReq != NULL) {
2!
237
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen};
2✔
238
    // TODO check return value
239
    if (tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg) < 0) {
2!
240
      mError("failed to put into read-queue since %s, line:%d", terrstr(), __LINE__);
×
241
    }
242
  }
243
}
2✔
244

245
static void mndPullupGrant(SMnode *pMnode) {
4,735✔
246
  mTrace("pullup grant msg");
4,735✔
247
  int32_t contLen = 0;
4,735✔
248
  void   *pReq = mndBuildTimerMsg(&contLen);
4,735✔
249
  if (pReq != NULL) {
4,735!
250
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_GRANT_HB_TIMER,
4,735✔
251
                      .pCont = pReq,
252
                      .contLen = contLen,
253
                      .info.notFreeAhandle = 1,
254
                      .info.ahandle = 0};
255
    // TODO check return value
256
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
4,735!
257
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
258
    }
259
  }
260
}
4,735✔
261

262
static void mndIncreaseUpTime(SMnode *pMnode) {
15✔
263
  mTrace("increate uptime");
15!
264
  int32_t contLen = 0;
15✔
265
  void   *pReq = mndBuildTimerMsg(&contLen);
15✔
266
  if (pReq != NULL) {
15!
267
    SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPTIME_TIMER,
15✔
268
                      .pCont = pReq,
269
                      .contLen = contLen,
270
                      .info.notFreeAhandle = 1,
271
                      .info.ahandle = 0};
272
    // TODO check return value
273
    if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
15!
274
      mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
×
275
    }
276
  }
277
}
15✔
278

279
static void mndSetVgroupOffline(SMnode *pMnode, int32_t dnodeId, int64_t curMs) {
362✔
280
  SSdb *pSdb = pMnode->pSdb;
362✔
281

282
  void *pIter = NULL;
362✔
283
  while (1) {
1,740✔
284
    SVgObj *pVgroup = NULL;
2,102✔
285
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
2,102✔
286
    if (pIter == NULL) break;
2,102✔
287

288
    bool stateChanged = false;
1,740✔
289
    for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
4,154✔
290
      SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
2,952✔
291
      if (pGid->dnodeId == dnodeId) {
2,952✔
292
        if (pGid->syncState != TAOS_SYNC_STATE_OFFLINE) {
538✔
293
          mInfo(
327!
294
              "vgId:%d, state changed by offline check, old state:%s restored:%d canRead:%d new state:error restored:0 "
295
              "canRead:0",
296
              pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead);
297
          pGid->syncState = TAOS_SYNC_STATE_OFFLINE;
327✔
298
          pGid->syncRestore = 0;
327✔
299
          pGid->syncCanRead = 0;
327✔
300
          pGid->startTimeMs = 0;
327✔
301
          stateChanged = true;
327✔
302
        }
303
        break;
538✔
304
      }
305
    }
306

307
    if (stateChanged) {
1,740✔
308
      SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
327✔
309
      if (pDb != NULL && pDb->stateTs != curMs) {
327!
310
        mInfo("db:%s, stateTs changed by offline check, old newTs:%" PRId64 " newTs:%" PRId64, pDb->name, pDb->stateTs,
234!
311
              curMs);
312
        pDb->stateTs = curMs;
234✔
313
      }
314
      mndReleaseDb(pMnode, pDb);
327✔
315
    }
316

317
    sdbRelease(pSdb, pVgroup);
1,740✔
318
  }
319
}
362✔
320

321
static void mndCheckDnodeOffline(SMnode *pMnode) {
9,881✔
322
  mTrace("check dnode offline");
9,881✔
323
  if (mndAcquireRpc(pMnode) != 0) return;
9,881!
324

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

328
  void *pIter = NULL;
9,881✔
329
  while (1) {
16,935✔
330
    SDnodeObj *pDnode = NULL;
26,816✔
331
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
26,816✔
332
    if (pIter == NULL) break;
26,816✔
333

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

340
    sdbRelease(pSdb, pDnode);
16,935✔
341
  }
342

343
  mndReleaseRpc(pMnode);
9,881✔
344
}
345

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

355
  if (state.state != TAOS_SYNC_STATE_LEADER) {
57,698✔
356
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4,523✔
357
    terrno = TSDB_CODE_SYN_NOT_LEADER;
4,523✔
358
    return true;
4,523✔
359
  }
360
  if (!state.restored || !pMnode->restored) {
53,175!
361
    (void)taosThreadRwlockUnlock(&pMnode->lock);
52✔
362
    terrno = TSDB_CODE_SYN_RESTORING;
52✔
363
    return true;
52✔
364
  }
365
  (void)taosThreadRwlockUnlock(&pMnode->lock);
53,123✔
366
  return false;
53,123✔
367
}
368

369
static int32_t minCronTime() {
×
370
  int32_t min = INT32_MAX;
×
371
  min = TMIN(min, tsTtlPushIntervalSec);
×
372
  min = TMIN(min, tsTrimVDbIntervalSec);
×
373
  min = TMIN(min, tsS3MigrateIntervalSec);
×
374
  min = TMIN(min, tsTransPullupInterval);
×
375
  min = TMIN(min, tsCompactPullupInterval);
×
376
  min = TMIN(min, tsMqRebalanceInterval);
×
377
  min = TMIN(min, tsStreamCheckpointInterval);
×
378
  min = TMIN(min, tsStreamNodeCheckInterval);
×
379
  min = TMIN(min, tsArbHeartBeatIntervalSec);
×
380
  min = TMIN(min, tsArbCheckSyncIntervalSec);
×
381

382
  int64_t telemInt = TMIN(60, (tsTelemInterval - 1));
×
383
  min = TMIN(min, telemInt);
×
384
  min = TMIN(min, tsGrantHBInterval);
×
385
  min = TMIN(min, tsUptimeInterval);
×
386

387
  return min <= 1 ? 2 : min;
×
388
}
389
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
53,123✔
390
  int32_t code = 0;
53,123✔
391
#ifndef TD_ASTRA  
392
  if (sec % tsTtlPushIntervalSec == 0) {
53,123✔
393
    mndPullupTtl(pMnode);
5,139✔
394
  }
395

396
  if (sec % tsTrimVDbIntervalSec == 0) {
53,123!
UNCOV
397
    mndPullupTrimDb(pMnode);
×
398
  }
399
#endif
400
#ifdef USE_S3
401
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
53,123!
402
    mndPullupS3MigrateDb(pMnode);
×
403
  }
404
#endif
405
  if (sec % tsTransPullupInterval == 0) {
53,123✔
406
    mndPullupTrans(pMnode);
26,924✔
407
  }
408

409
  if (sec % tsCompactPullupInterval == 0) {
53,123✔
410
    mndPullupCompacts(pMnode);
4,569✔
411
  }
412
#ifdef USE_TOPIC
413
  if (sec % tsMqRebalanceInterval == 0) {
53,123✔
414
    mndCalMqRebalance(pMnode);
26,121✔
415
  }
416
#endif
417
#ifdef USE_STREAM
418
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
53,123✔
419
    mndStreamCheckpointTimer(pMnode);
1,218✔
420
  }
421

422
  if (sec % tsStreamNodeCheckInterval == 0) {
53,123✔
423
    mndStreamCheckNode(pMnode);
28✔
424
  }
425

426
  if (sec % (tsStreamFailedTimeout/1000) == 0) {
53,123✔
427
    mndStreamCheckStatus(pMnode);
1✔
428
  }
429

430
  if (sec % 30 == 0) {
53,123✔
431
    mndStreamConsensusChkpt(pMnode);
1,218✔
432
  }
433

434
  if (tsTelemInterval > 0 && sec % tsTelemInterval == 0) {
53,123!
435
    mndPullupTelem(pMnode);
2✔
436
  }
437
#endif
438
#ifndef TD_ASTRA
439
  if (sec % tsGrantHBInterval == 0) {
53,123✔
440
    mndPullupGrant(pMnode);
4,735✔
441
  }
442
#endif
443
  if (sec % tsUptimeInterval == 0) {
53,123✔
444
    mndIncreaseUpTime(pMnode);
15✔
445
  }
446
#ifndef TD_ASTRA
447
  if (sec % (tsArbHeartBeatIntervalSec) == 0) {
53,123✔
448
    if ((code = mndPullupArbHeartbeat(pMnode)) != 0) {
26,122!
449
      mError("failed to pullup arb heartbeat, since:%s", tstrerror(code));
×
450
    }
451
  }
452

453
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
53,123✔
454
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
17,153!
455
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
456
    }
457
  }
458
#endif
459
}
53,123✔
460
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
53,123✔
461
  if (sec % (tsStatusInterval * 5) == 0) {
53,123✔
462
    mndCheckDnodeOffline(pMnode);
9,881✔
463
  }
464
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
53,123✔
465
    mndSyncCheckTimeout(pMnode);
1,218✔
466
  }
467
}
53,123✔
468

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

474
  while (1) {
585,871✔
475
    lastTime++;
587,902✔
476
    taosMsleep(100);
587,902✔
477

478
    if (mndGetStop(pMnode)) break;
587,902✔
479
    if (lastTime % 10 != 0) continue;
585,871✔
480

481
    if (mnodeIsNotLeader(pMnode)) {
57,698✔
482
      mTrace("timer not process since mnode is not leader");
4,575!
483
      continue;
4,575✔
484
    }
485

486
    int64_t sec = lastTime / 10;
53,123✔
487
    mndDoTimerCheckTask(pMnode, sec);
53,123✔
488

489
    mndDoTimerPullupTask(pMnode, sec);
53,123✔
490
  }
491

492
  return NULL;
2,031✔
493
}
494

495
static int32_t mndInitTimer(SMnode *pMnode) {
2,031✔
496
  int32_t      code = 0;
2,031✔
497
  TdThreadAttr thAttr;
498
  (void)taosThreadAttrInit(&thAttr);
2,031✔
499
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,031✔
500
#ifdef TD_COMPACT_OS
501
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
502
#endif
503
  if ((code = taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode)) != 0) {
2,031!
504
    mError("failed to create timer thread since %s", tstrerror(code));
×
505
    TAOS_RETURN(code);
×
506
  }
507

508
  (void)taosThreadAttrDestroy(&thAttr);
2,031✔
509
  tmsgReportStartup("mnode-timer", "initialized");
2,031✔
510
  TAOS_RETURN(code);
2,031✔
511
}
512

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

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

528
  if (taosMkDir(pMnode->path) != 0) {
2,032!
529
    code = terrno;
×
530
    TAOS_RETURN(code);
×
531
  }
532

533
  TAOS_RETURN(code);
2,032✔
534
}
535

536
static int32_t mndInitWal(SMnode *pMnode) {
2,032✔
537
  int32_t code = 0;
2,032✔
538
  char    path[PATH_MAX + 20] = {0};
2,032✔
539
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
2,032✔
540
  SWalCfg cfg = {.vgId = 1,
2,032✔
541
                 .fsyncPeriod = 0,
542
                 .rollPeriod = -1,
543
                 .segSize = -1,
544
                 .committed = -1,
545
                 .retentionPeriod = 0,
546
                 .retentionSize = 0,
547
                 .level = TAOS_WAL_FSYNC,
548
                 .encryptAlgorithm = 0,
549
                 .encryptKey = {0}};
550

551
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
552
  if (tsiEncryptAlgorithm == DND_CA_SM4 && (tsiEncryptScope & DND_CS_MNODE_WAL) == DND_CS_MNODE_WAL) {
2,032!
553
    cfg.encryptAlgorithm = (tsiEncryptScope & DND_CS_MNODE_WAL) ? tsiEncryptAlgorithm : 0;
×
554
    if (tsEncryptKey[0] == '\0') {
×
555
      code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
556
      TAOS_RETURN(code);
×
557
    } else {
558
      tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
×
559
    }
560
  }
561
#endif
562

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

571
  TAOS_RETURN(code);
2,032✔
572
}
573

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

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

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

595
  TAOS_RETURN(code);
2,032✔
596
}
597

598
static int32_t mndOpenSdb(SMnode *pMnode) {
2,032✔
599
  int32_t code = 0;
2,032✔
600
  if (!pMnode->deploy) {
2,032✔
601
    code = sdbReadFile(pMnode->pSdb);
493✔
602
  }
603

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

606
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
2,032✔
607
  return code;
2,032✔
608
}
609

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

617
static int32_t mndAllocStep(SMnode *pMnode, char *name, MndInitFp initFp, MndCleanupFp cleanupFp) {
75,184✔
618
  SMnodeStep step = {0};
75,184✔
619
  step.name = name;
75,184✔
620
  step.initFp = initFp;
75,184✔
621
  step.cleanupFp = cleanupFp;
75,184✔
622
  if (taosArrayPush(pMnode->pSteps, &step) == NULL) {
150,368!
623
    TAOS_RETURN(terrno);
×
624
  }
625

626
  TAOS_RETURN(0);
75,184✔
627
}
628

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

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

673
  if (pos == -1) {
2,031!
674
    pos = taosArrayGetSize(pMnode->pSteps) - 1;
2,031✔
675
  }
676

677
  for (int32_t s = pos; s >= 0; s--) {
77,178✔
678
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
75,147✔
679
    mInfo("%s will cleanup", pStep->name);
75,147!
680
    if (pStep->cleanupFp != NULL) {
75,147✔
681
      (*pStep->cleanupFp)(pMnode);
71,085✔
682
    }
683
  }
684

685
  taosArrayClear(pMnode->pSteps);
2,031✔
686
  taosArrayDestroy(pMnode->pSteps);
2,031✔
687
  pMnode->pSteps = NULL;
2,031✔
688
}
689

690
static int32_t mndExecSteps(SMnode *pMnode) {
2,032✔
691
  int32_t code = 0;
2,032✔
692
  int32_t size = taosArrayGetSize(pMnode->pSteps);
2,032✔
693
  for (int32_t pos = 0; pos < size; pos++) {
77,216✔
694
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
75,184✔
695
    if (pStep->initFp == NULL) continue;
75,184!
696

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

707
  pMnode->clusterId = mndGetClusterId(pMnode);
2,032✔
708
  TAOS_RETURN(0);
2,032✔
709
}
710

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

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

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

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

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

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

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

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

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

787
  mInfo("mnode open successfully");
2,032!
788
  return pMnode;
2,032✔
789
}
790

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

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

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

828
  return mndInitTimer(pMnode);
2,031✔
829
}
830

831
int32_t mndIsCatchUp(SMnode *pMnode) {
892✔
832
  int64_t rid = pMnode->syncMgmt.sync;
892✔
833
  return syncIsCatchUp(rid);
892✔
834
}
835

836
ESyncRole mndGetRole(SMnode *pMnode) {
892✔
837
  int64_t rid = pMnode->syncMgmt.sync;
892✔
838
  return syncGetRole(rid);
892✔
839
}
840

841
int64_t mndGetTerm(SMnode *pMnode) {
17,217✔
842
  int64_t rid = pMnode->syncMgmt.sync;
17,217✔
843
  return syncGetTerm(rid);
17,217✔
844
}
845

846
int32_t mndGetArbToken(SMnode *pMnode, char *outToken) { return syncGetArbToken(pMnode->syncMgmt.sync, outToken); }
43,341✔
847

848
void mndStop(SMnode *pMnode) {
2,031✔
849
  mndSetStop(pMnode);
2,031✔
850
  mndSyncStop(pMnode);
2,031✔
851
  mndCleanupTimer(pMnode);
2,031✔
852
}
2,031✔
853

854
int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
160,828✔
855
  SMnode    *pMnode = pMsg->info.node;
160,828✔
856
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
160,828✔
857

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

861
  int32_t code = syncProcessMsg(pMgmt->sync, pMsg);
160,828✔
862
  if (code != 0) {
160,827✔
863
    mGError("vgId:1, failed to process sync msg:%p type:%s since %s, code:0x%x", pMsg, TMSG_INFO(pMsg->msgType),
4!
864
            tstrerror(code), code);
865
  }
866

867
  return code;
160,827✔
868
}
869

870
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
624,239✔
871
  int32_t code = 0;
624,239✔
872
  if (!IsReq(pMsg)) TAOS_RETURN(code);
624,239✔
873
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
534,300✔
874
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
517,328!
875
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
506,067✔
876
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
472,322!
877
    TAOS_RETURN(code);
61,962✔
878
  }
879

880
  SMnode *pMnode = pMsg->info.node;
472,338✔
881
  (void)taosThreadRwlockRdlock(&pMnode->lock);
472,338✔
882
  if (pMnode->stopped) {
472,930✔
883
    (void)taosThreadRwlockUnlock(&pMnode->lock);
89✔
884
    code = TSDB_CODE_APP_IS_STOPPING;
89✔
885
    TAOS_RETURN(code);
89✔
886
  }
887

888
  terrno = 0;
472,841✔
889
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
472,436✔
890
  if (terrno != 0) {
472,896!
891
    (void)taosThreadRwlockUnlock(&pMnode->lock);
×
892
    code = terrno;
×
893
    TAOS_RETURN(code);
×
894
  }
895

896
  if (state.state != TAOS_SYNC_STATE_LEADER) {
472,888✔
897
    (void)taosThreadRwlockUnlock(&pMnode->lock);
24,126✔
898
    code = TSDB_CODE_SYN_NOT_LEADER;
24,124✔
899
    goto _OVER;
24,124✔
900
  }
901

902
  if (!state.restored || !pMnode->restored) {
448,762✔
903
    (void)taosThreadRwlockUnlock(&pMnode->lock);
2,234✔
904
    code = TSDB_CODE_SYN_RESTORING;
2,220✔
905
    goto _OVER;
2,220✔
906
  }
907

908
#if 1
909
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
446,528✔
910
#else
911
  int32_t ref = atomic_add_fetch_32(&pMnode->rpcRef, 1);
912
  mTrace("mnode rpc is acquired, ref:%d", ref);
913
#endif
914

915
  (void)taosThreadRwlockUnlock(&pMnode->lock);
446,550✔
916
  TAOS_RETURN(code);
446,536✔
917

918
_OVER:
26,344✔
919
  if (pMsg->msgType == TDMT_MND_TMQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER ||
26,344!
920
      pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER ||
26,346!
921
      pMsg->msgType == TDMT_MND_TRIM_DB_TIMER || pMsg->msgType == TDMT_MND_UPTIME_TIMER ||
26,346!
922
      pMsg->msgType == TDMT_MND_COMPACT_TIMER || pMsg->msgType == TDMT_MND_NODECHECK_TIMER ||
26,347!
923
      pMsg->msgType == TDMT_MND_GRANT_HB_TIMER || pMsg->msgType == TDMT_MND_STREAM_REQ_CHKPT ||
26,347!
924
      pMsg->msgType == TDMT_MND_S3MIGRATE_DB_TIMER || pMsg->msgType == TDMT_MND_ARB_HEARTBEAT_TIMER ||
26,347!
925
      pMsg->msgType == TDMT_MND_ARB_CHECK_SYNC_TIMER || pMsg->msgType == TDMT_MND_CHECK_STREAM_TIMER) {
26,347!
926
    mTrace("timer not process since mnode restored:%d stopped:%d, sync restored:%d role:%s ", pMnode->restored,
×
927
           pMnode->stopped, state.restored, syncStr(state.state));
928
    TAOS_RETURN(code);
×
929
  }
930

931
  const STraceId *trace = &pMsg->info.traceId;
26,344✔
932
  SEpSet          epSet = {0};
26,344✔
933
  mndGetMnodeEpSet(pMnode, &epSet);
26,344✔
934

935
  mGDebug(
26,347!
936
      "msg:%p, type:%s failed to process since %s, mnode restored:%d stopped:%d, sync restored:%d "
937
      "role:%s, redirect numOfEps:%d inUse:%d, type:%s",
938
      pMsg, TMSG_INFO(pMsg->msgType), tstrerror(code), pMnode->restored, pMnode->stopped, state.restored,
939
      syncStr(state.state), epSet.numOfEps, epSet.inUse, TMSG_INFO(pMsg->msgType));
940

941
  if (epSet.numOfEps <= 0) return -1;
26,347!
942

943
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
102,623✔
944
    mDebug("mnode index:%d, ep:%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
76,276✔
945
  }
946

947
  int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
26,347✔
948
  pMsg->info.rsp = rpcMallocCont(contLen);
26,347✔
949
  if (pMsg->info.rsp != NULL) {
26,346!
950
    if (tSerializeSEpSet(pMsg->info.rsp, contLen, &epSet) < 0) {
26,347!
951
      mError("failed to serialize ep set");
×
952
    }
953
    pMsg->info.hasEpSet = 1;
26,345✔
954
    pMsg->info.rspLen = contLen;
26,345✔
955
  }
956

957
  TAOS_RETURN(code);
26,344✔
958
}
959

960
int32_t mndProcessRpcMsg(SRpcMsg *pMsg, SQueueInfo *pQueueInfo) {
624,214✔
961
  SMnode         *pMnode = pMsg->info.node;
624,214✔
962
  const STraceId *trace = &pMsg->info.traceId;
624,214✔
963
  int32_t         code = TSDB_CODE_SUCCESS;
624,214✔
964

965
  MndMsgFp    fp = pMnode->msgFp[TMSG_INDEX(pMsg->msgType)];
624,214✔
966
  MndMsgFpExt fpExt = NULL;
624,214✔
967
  if (fp == NULL) {
624,214✔
968
    fpExt = pMnode->msgFpExt[TMSG_INDEX(pMsg->msgType)];
62,173✔
969
    if (fpExt == NULL) {
62,173!
UNCOV
970
      mGError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
×
UNCOV
971
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
UNCOV
972
      TAOS_RETURN(code);
×
973
    }
974
  }
975

976
  TAOS_CHECK_RETURN(mndCheckMnodeState(pMsg));
624,214✔
977

978
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
598,531!
979
  if (fp)
598,531✔
980
    code = (*fp)(pMsg);
536,358✔
981
  else
982
    code = (*fpExt)(pMsg, pQueueInfo);
62,173✔
983
  mndReleaseRpc(pMnode);
598,642✔
984

985
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
598,658✔
986
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
85,923!
987
  } else if (code == 0) {
512,735✔
988
    mGTrace("msg:%p, successfully processed", pMsg);
504,417!
989
  } else {
990
    // TODO removve this wrong set code
991
    if (code == -1) {
8,318✔
992
      code = terrno;
6✔
993
    }
994
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
8,318!
995
            TMSG_INFO(pMsg->msgType));
996
  }
997

998
  TAOS_RETURN(code);
598,658✔
999
}
1000

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

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

1015
// Note: uid 0 is reserved
1016
int64_t mndGenerateUid(const char *name, int32_t len) {
48,229✔
1017
  int32_t hashval = MurmurHash3_32(name, len);
48,229✔
1018
  do {
×
1019
    int64_t us = taosGetTimestampUs();
48,231✔
1020
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
48,231✔
1021
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
48,231✔
1022
    if (uuid) {
48,231!
1023
      return llabs(uuid);
48,231✔
1024
    }
1025
  } while (true);
1026
}
1027

1028
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
15✔
1029
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1030
  int32_t code = mndAcquireRpc(pMnode);
15✔
1031
  if (code < 0) {
15!
1032
    TAOS_RETURN(code);
×
1033
  } else if (code == 1) {
15!
1034
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1035
  }
1036

1037
  SSdb   *pSdb = pMnode->pSdb;
15✔
1038
  int64_t ms = taosGetTimestampMs();
15✔
1039

1040
  pClusterInfo->dnodes = taosArrayInit(sdbGetSize(pSdb, SDB_DNODE), sizeof(SMonDnodeDesc));
15✔
1041
  pClusterInfo->mnodes = taosArrayInit(sdbGetSize(pSdb, SDB_MNODE), sizeof(SMonMnodeDesc));
15✔
1042
  pVgroupInfo->vgroups = taosArrayInit(sdbGetSize(pSdb, SDB_VGROUP), sizeof(SMonVgroupDesc));
15✔
1043
  pStbInfo->stbs = taosArrayInit(sdbGetSize(pSdb, SDB_STB), sizeof(SMonStbDesc));
15✔
1044
  if (pClusterInfo->dnodes == NULL || pClusterInfo->mnodes == NULL || pVgroupInfo->vgroups == NULL ||
15!
1045
      pStbInfo->stbs == NULL) {
15!
1046
    mndReleaseRpc(pMnode);
×
1047
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1048
    if (terrno != 0) code = terrno;
×
1049
    TAOS_RETURN(code);
×
1050
  }
1051

1052
  // cluster info
1053
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
15✔
1054
  pClusterInfo->monitor_interval = tsMonitorInterval;
15✔
1055
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
15✔
1056
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
15✔
1057
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
15✔
1058
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
15✔
1059
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
15✔
1060

1061
  void *pIter = NULL;
15✔
1062
  while (1) {
15✔
1063
    SDnodeObj *pObj = NULL;
30✔
1064
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
30✔
1065
    if (pIter == NULL) break;
30✔
1066

1067
    SMonDnodeDesc desc = {0};
15✔
1068
    desc.dnode_id = pObj->id;
15✔
1069
    tstrncpy(desc.dnode_ep, pObj->ep, sizeof(desc.dnode_ep));
15✔
1070
    if (mndIsDnodeOnline(pObj, ms)) {
15✔
1071
      tstrncpy(desc.status, "ready", sizeof(desc.status));
14✔
1072
    } else {
1073
      tstrncpy(desc.status, "offline", sizeof(desc.status));
1✔
1074
    }
1075
    if (taosArrayPush(pClusterInfo->dnodes, &desc) == NULL) {
30!
1076
      mError("failed put dnode into array, but continue at this monitor report")
×
1077
    }
1078
    sdbRelease(pSdb, pObj);
15✔
1079
  }
1080

1081
  pIter = NULL;
15✔
1082
  while (1) {
15✔
1083
    SMnodeObj *pObj = NULL;
30✔
1084
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
30✔
1085
    if (pIter == NULL) break;
30✔
1086

1087
    SMonMnodeDesc desc = {0};
15✔
1088
    desc.mnode_id = pObj->id;
15✔
1089
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
15✔
1090

1091
    if (pObj->id == pMnode->selfDnodeId) {
15!
1092
      pClusterInfo->first_ep_dnode_id = pObj->id;
15✔
1093
      tstrncpy(pClusterInfo->first_ep, pObj->pDnode->ep, sizeof(pClusterInfo->first_ep));
15✔
1094
      // pClusterInfo->master_uptime = (float)mndGetClusterUpTime(pMnode) / 86400.0f;
1095
      pClusterInfo->master_uptime = mndGetClusterUpTime(pMnode);
15✔
1096
      // pClusterInfo->master_uptime = (ms - pObj->stateStartTime) / (86400000.0f);
1097
      tstrncpy(desc.role, syncStr(TAOS_SYNC_STATE_LEADER), sizeof(desc.role));
15✔
1098
      desc.syncState = TAOS_SYNC_STATE_LEADER;
15✔
1099
    } else {
1100
      tstrncpy(desc.role, syncStr(pObj->syncState), sizeof(desc.role));
×
1101
      desc.syncState = pObj->syncState;
×
1102
    }
1103
    if (taosArrayPush(pClusterInfo->mnodes, &desc) == NULL) {
30!
1104
      mError("failed to put mnode into array, but continue at this monitor report");
×
1105
    }
1106
    sdbRelease(pSdb, pObj);
15✔
1107
  }
1108

1109
  // vgroup info
1110
  pIter = NULL;
15✔
1111
  while (1) {
36✔
1112
    SVgObj *pVgroup = NULL;
51✔
1113
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
51✔
1114
    if (pIter == NULL) break;
51✔
1115

1116
    pClusterInfo->vgroups_total++;
36✔
1117
    pClusterInfo->tbs_total += pVgroup->numOfTables;
36✔
1118

1119
    SMonVgroupDesc desc = {0};
36✔
1120
    desc.vgroup_id = pVgroup->vgId;
36✔
1121

1122
    SName name = {0};
36✔
1123
    code = tNameFromString(&name, pVgroup->dbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
36✔
1124
    if (code < 0) {
36!
1125
      mError("failed to get db name since %s", tstrerror(code));
×
1126
      sdbRelease(pSdb, pVgroup);
×
1127
      TAOS_RETURN(code);
×
1128
    }
1129
    (void)tNameGetDbName(&name, desc.database_name);
36✔
1130

1131
    desc.tables_num = pVgroup->numOfTables;
36✔
1132
    pGrantInfo->timeseries_used += pVgroup->numOfTimeSeries;
36✔
1133
    tstrncpy(desc.status, "unsynced", sizeof(desc.status));
36✔
1134
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
72✔
1135
      SVnodeGid     *pVgid = &pVgroup->vnodeGid[i];
36✔
1136
      SMonVnodeDesc *pVnDesc = &desc.vnodes[i];
36✔
1137
      pVnDesc->dnode_id = pVgid->dnodeId;
36✔
1138
      tstrncpy(pVnDesc->vnode_role, syncStr(pVgid->syncState), sizeof(pVnDesc->vnode_role));
36✔
1139
      pVnDesc->syncState = pVgid->syncState;
36✔
1140
      if (pVgid->syncState == TAOS_SYNC_STATE_LEADER || pVgid->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
36!
1141
        tstrncpy(desc.status, "ready", sizeof(desc.status));
36✔
1142
        pClusterInfo->vgroups_alive++;
36✔
1143
      }
1144
      if (pVgid->syncState != TAOS_SYNC_STATE_ERROR && pVgid->syncState != TAOS_SYNC_STATE_OFFLINE) {
36!
1145
        pClusterInfo->vnodes_alive++;
36✔
1146
      }
1147
      pClusterInfo->vnodes_total++;
36✔
1148
    }
1149

1150
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
72!
1151
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1152
    }
1153
    sdbRelease(pSdb, pVgroup);
36✔
1154
  }
1155

1156
  // stb info
1157
  pIter = NULL;
15✔
1158
  while (1) {
14✔
1159
    SStbObj *pStb = NULL;
29✔
1160
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
29✔
1161
    if (pIter == NULL) break;
29✔
1162

1163
    SMonStbDesc desc = {0};
14✔
1164

1165
    SName name1 = {0};
14✔
1166
    code = tNameFromString(&name1, pStb->db, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
14✔
1167
    if (code < 0) {
14!
1168
      mError("failed to get db name since %s", tstrerror(code));
×
1169
      sdbRelease(pSdb, pStb);
×
1170
      TAOS_RETURN(code);
×
1171
    }
1172
    (void)tNameGetDbName(&name1, desc.database_name);
14✔
1173

1174
    SName name2 = {0};
14✔
1175
    code = tNameFromString(&name2, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
14✔
1176
    if (code < 0) {
14!
1177
      mError("failed to get table name since %s", tstrerror(code));
×
1178
      sdbRelease(pSdb, pStb);
×
1179
      TAOS_RETURN(code);
×
1180
    }
1181
    tstrncpy(desc.stb_name, tNameGetTableName(&name2), TSDB_TABLE_NAME_LEN);
14✔
1182

1183
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
28!
1184
      mError("failed to put stb into array, but continue at this monitor report");
×
1185
    }
1186
    sdbRelease(pSdb, pStb);
14✔
1187
  }
1188

1189
  // grant info
1190
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
15✔
1191
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
15✔
1192
  if (pMnode->grant.expireTimeMS == 0) {
15!
1193
    pGrantInfo->expire_time = 0;
×
1194
    pGrantInfo->timeseries_total = 0;
×
1195
  }
1196

1197
  mndReleaseRpc(pMnode);
15✔
1198
  TAOS_RETURN(code);
15✔
1199
}
1200

1201
int32_t mndGetLoad(SMnode *pMnode, SMnodeLoad *pLoad) {
57,647✔
1202
  mTrace("mnode get load");
57,647✔
1203
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
57,647✔
1204
  pLoad->syncState = state.state;
57,647✔
1205
  pLoad->syncRestore = state.restored;
57,647✔
1206
  pLoad->syncTerm = state.term;
57,647✔
1207
  pLoad->roleTimeMs = state.roleTimeMs;
57,647✔
1208
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
57,647✔
1209
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1210
  return 0;
57,647✔
1211
}
1212

1213
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
17,153✔
1214
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
17,153✔
1215
  return state.roleTimeMs;
17,153✔
1216
}
1217

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

1236
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1237

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

1245
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
587,902✔
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