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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

71.54
/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 "mndArbGroup.h"
19
#include "mndAnode.h"
20
#include "mndCluster.h"
21
#include "mndCompact.h"
22
#include "mndCompactDetail.h"
23
#include "mndConsumer.h"
24
#include "mndDb.h"
25
#include "mndDnode.h"
26
#include "mndFunc.h"
27
#include "mndGrant.h"
28
#include "mndIndex.h"
29
#include "mndInfoSchema.h"
30
#include "mndMnode.h"
31
#include "mndPerfSchema.h"
32
#include "mndPrivilege.h"
33
#include "mndProfile.h"
34
#include "mndQnode.h"
35
#include "mndQuery.h"
36
#include "mndShow.h"
37
#include "mndSma.h"
38
#include "mndSnode.h"
39
#include "mndStb.h"
40
#include "mndStream.h"
41
#include "mndSubscribe.h"
42
#include "mndSync.h"
43
#include "mndTelem.h"
44
#include "mndTopic.h"
45
#include "mndTrans.h"
46
#include "mndUser.h"
47
#include "mndVgroup.h"
48
#include "mndView.h"
49

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

69
static inline void mndReleaseRpc(SMnode *pMnode) {
4,106,538✔
70
  (void)taosThreadRwlockRdlock(&pMnode->lock);
4,106,538✔
71
#if 1
72
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
4,107,150✔
73
#else
74
  int32_t ref = atomic_sub_fetch_32(&pMnode->rpcRef, 1);
75
  mTrace("mnode rpc is released, ref:%d", ref);
76
#endif
77
  (void)taosThreadRwlockUnlock(&pMnode->lock);
4,107,264✔
78
}
4,107,183✔
79

80
static void *mndBuildTimerMsg(int32_t *pContLen) {
117,122✔
81
  terrno = 0;
117,122✔
82
  SMTimerReq timerReq = {0};
117,122✔
83

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

268
  void *pIter = NULL;
153✔
269
  while (1) {
421✔
270
    SVgObj *pVgroup = NULL;
574✔
271
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
574✔
272
    if (pIter == NULL) break;
574✔
273

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

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

303
    sdbRelease(pSdb, pVgroup);
421✔
304
  }
305
}
153✔
306

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

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

314
  void *pIter = NULL;
12,385✔
315
  while (1) {
20,062✔
316
    SDnodeObj *pDnode = NULL;
32,447✔
317
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
32,447✔
318
    if (pIter == NULL) break;
32,447✔
319

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

326
    sdbRelease(pSdb, pDnode);
20,062✔
327
  }
328

329
  mndReleaseRpc(pMnode);
12,385✔
330
}
331

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

341
  if (state.state != TAOS_SYNC_STATE_LEADER) {
33,882✔
342
    (void)taosThreadRwlockUnlock(&pMnode->lock);
1,635✔
343
    terrno = TSDB_CODE_SYN_NOT_LEADER;
1,635✔
344
    return true;
1,635✔
345
  }
346
  if (!state.restored || !pMnode->restored) {
32,247!
347
    (void)taosThreadRwlockUnlock(&pMnode->lock);
13✔
348
    terrno = TSDB_CODE_SYN_RESTORING;
13✔
349
    return true;
13✔
350
  }
351
  (void)taosThreadRwlockUnlock(&pMnode->lock);
32,234✔
352
  return false;
32,234✔
353
}
354

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

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

373
  return min <= 1 ? 2 : min;
68,772✔
374
}
375
void mndDoTimerPullupTask(SMnode *pMnode, int64_t sec) {
67,124✔
376
  int32_t code = 0;
67,124✔
377
  if (sec % tsTtlPushIntervalSec == 0) {
67,124✔
378
    mndPullupTtl(pMnode);
6,246✔
379
  }
380

381
  if (sec % tsTrimVDbIntervalSec == 0) {
67,124✔
382
    mndPullupTrimDb(pMnode);
4✔
383
  }
384

385
  if (tsS3MigrateEnabled && sec % tsS3MigrateIntervalSec == 0) {
67,124!
386
    mndPullupS3MigrateDb(pMnode);
×
387
  }
388

389
  if (sec % tsTransPullupInterval == 0) {
67,124✔
390
    mndPullupTrans(pMnode);
32,902✔
391
  }
392

393
  if (sec % tsCompactPullupInterval == 0) {
67,124✔
394
    mndPullupCompacts(pMnode);
5,891✔
395
  }
396

397
  if (sec % tsMqRebalanceInterval == 0) {
67,124✔
398
    mndCalMqRebalance(pMnode);
32,234✔
399
  }
400

401
  if (sec % 30 == 0) {  // send the checkpoint info every 30 sec
67,124✔
402
    mndStreamCheckpointTimer(pMnode);
1,683✔
403
  }
404

405
  if (sec % tsStreamNodeCheckInterval == 0) {
67,124✔
406
    mndStreamCheckNode(pMnode);
2,703✔
407
  }
408

409
  if (sec % 5 == 0) {
67,124✔
410
    mndStreamConsensusChkpt(pMnode);
12,703✔
411
  }
412

413
  if (sec % tsTelemInterval == (TMIN(60, (tsTelemInterval - 1)))) {
67,124✔
414
    mndPullupTelem(pMnode);
235✔
415
  }
416

417
  if (sec % tsGrantHBInterval == 0) {
67,124✔
418
    mndPullupGrant(pMnode);
5,529✔
419
  }
420

421
  if (sec % tsUptimeInterval == 0) {
67,124✔
422
    mndIncreaseUpTime(pMnode);
81✔
423
  }
424

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

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

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

451
  while (1) {
697,391✔
452
    lastTime++;
699,402✔
453
    taosMsleep(100);
699,402✔
454
    if (mndGetStop(pMnode)) break;
699,402✔
455
    if (lastTime % 10 != 0) continue;
697,391✔
456

457
    int64_t sec = lastTime / 10;
68,772✔
458
    mndDoTimerCheckTask(pMnode, sec);
68,772✔
459

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

470
  return NULL;
2,011✔
471
}
472

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

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

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

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

503
  if (taosMkDir(pMnode->path) != 0) {
2,012!
504
    code = terrno;
×
505
    TAOS_RETURN(code);
×
506
  }
507

508
  TAOS_RETURN(code);
2,012✔
509
}
510

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

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

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

546
  TAOS_RETURN(code);
2,012✔
547
}
548

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

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

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

570
  TAOS_RETURN(code);
2,012✔
571
}
572

573
static int32_t mndOpenSdb(SMnode *pMnode) {
2,012✔
574
  int32_t code = 0;
2,012✔
575
  if (!pMnode->deploy) {
2,012✔
576
    code = sdbReadFile(pMnode->pSdb);
516✔
577
  }
578

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

581
  atomic_store_64(&pMnode->applied, pMnode->pSdb->commitIndex);
2,012✔
582
  return code;
2,012✔
583
}
584

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

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

601
  TAOS_RETURN(0);
70,420✔
602
}
603

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

641
  return 0;
2,012✔
642
}
643

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

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

651
  for (int32_t s = pos; s >= 0; s--) {
72,396✔
652
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
70,385✔
653
    mInfo("%s will cleanup", pStep->name);
70,385!
654
    if (pStep->cleanupFp != NULL) {
70,385✔
655
      (*pStep->cleanupFp)(pMnode);
68,374✔
656
    }
657
  }
658

659
  taosArrayClear(pMnode->pSteps);
2,011✔
660
  taosArrayDestroy(pMnode->pSteps);
2,011✔
661
  pMnode->pSteps = NULL;
2,011✔
662
}
663

664
static int32_t mndExecSteps(SMnode *pMnode) {
2,012✔
665
  int32_t code = 0;
2,012✔
666
  int32_t size = taosArrayGetSize(pMnode->pSteps);
2,012✔
667
  for (int32_t pos = 0; pos < size; pos++) {
72,432✔
668
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
70,420✔
669
    if (pStep->initFp == NULL) continue;
70,420!
670

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

681
  pMnode->clusterId = mndGetClusterId(pMnode);
2,012✔
682
  TAOS_RETURN(0);
2,012✔
683
}
684

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

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

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

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

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

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

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

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

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

761
  mInfo("mnode open successfully");
2,012!
762
  return pMnode;
2,012✔
763
}
764

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

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

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

801
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
2,011✔
802

803
  return mndInitTimer(pMnode);
2,011✔
804
}
805

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

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

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

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

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

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

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

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

842
  return code;
145,735✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
4,117,806✔
846
  int32_t code = 0;
4,117,806✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
4,117,806✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
3,919,594✔
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
3,695,287!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
3,559,711✔
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
3,111,304!
852
    TAOS_RETURN(code);
808,275✔
853
  }
854

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

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

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

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

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
3,088,061✔
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,088,068✔
891
  TAOS_RETURN(code);
3,088,045✔
892

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

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

910
  mGDebug(
21,372!
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;
21,372!
917

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

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

932
  TAOS_RETURN(code);
21,371✔
933
}
934

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

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

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
4,094,533!
954
  if (fp)
4,094,533✔
955
    code = (*fp)(pMsg);
3,286,092✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
808,441✔
958
  mndReleaseRpc(pMnode);
4,094,410✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
4,094,785✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
838,833!
962
  } else if (code == 0) {
3,255,952✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
3,246,132!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
9,820✔
967
      code = terrno;
5✔
968
    }
969
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
9,820!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
4,094,785✔
974
}
975

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

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

990
// Note: uid 0 is reserved
991
int64_t mndGenerateUid(const char *name, int32_t len) {
123,371✔
992
  int32_t hashval = MurmurHash3_32(name, len);
123,371✔
993
  do {
×
994
    int64_t us = taosGetTimestampUs();
123,371✔
995
    int64_t x = (us & 0x000000FFFFFFFFFF) << 24;
123,371✔
996
    int64_t uuid = x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
123,371✔
997
    if (uuid) {
123,371!
998
      return llabs(uuid);
123,371✔
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) {
68,114✔
1177
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
68,114✔
1178
  pLoad->syncState = state.state;
68,114✔
1179
  pLoad->syncRestore = state.restored;
68,114✔
1180
  pLoad->syncTerm = state.term;
68,114✔
1181
  pLoad->roleTimeMs = state.roleTimeMs;
68,114✔
1182
  mTrace("mnode current syncState is %s, syncRestore:%d, syncTerm:%" PRId64 " ,roleTimeMs:%" PRId64,
68,114✔
1183
         syncStr(pLoad->syncState), pLoad->syncRestore, pLoad->syncTerm, pLoad->roleTimeMs);
1184
  return 0;
68,114✔
1185
}
1186

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

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

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

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

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

© 2025 Coveralls, Inc