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

taosdata / TDengine / #3522

07 Nov 2024 05:59AM UTC coverage: 58.216% (+1.3%) from 56.943%
#3522

push

travis-ci

web-flow
Merge pull request #28663 from taosdata/fix/3_liaohj

fix(stream): stop the underlying scan operations for stream

111884 of 248391 branches covered (45.04%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

1164 existing lines in 134 files now uncovered.

191720 of 273118 relevant lines covered (70.2%)

13088725.13 hits per line

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

70.94
/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) {
6,415✔
51
  int32_t code = 0;
6,415✔
52
  (void)taosThreadRwlockRdlock(&pMnode->lock);
6,415✔
53
  if (pMnode->stopped) {
6,415!
54
    code = TSDB_CODE_APP_IS_STOPPING;
×
55
  } else if (!mndIsLeader(pMnode)) {
6,415✔
56
    code = -1;
182✔
57
  } else {
58
#if 1
59
    (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
6,233✔
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);
6,415✔
66
  TAOS_RETURN(code);
6,415✔
67
}
68

69
static inline void mndReleaseRpc(SMnode *pMnode) {
5,961,635✔
70
  (void)taosThreadRwlockRdlock(&pMnode->lock);
5,961,635✔
71
#if 1
72
  (void)atomic_sub_fetch_32(&pMnode->rpcRef, 1);
5,962,253✔
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);
5,962,282✔
78
}
5,962,215✔
79

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

303
    sdbRelease(pSdb, pVgroup);
238✔
304
  }
305
}
110✔
306

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

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

314
  void *pIter = NULL;
6,222✔
315
  while (1) {
10,795✔
316
    SDnodeObj *pDnode = NULL;
17,017✔
317
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
17,017✔
318
    if (pIter == NULL) break;
17,017✔
319

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

326
    sdbRelease(pSdb, pDnode);
10,795✔
327
  }
328

329
  mndReleaseRpc(pMnode);
6,222✔
330
}
331

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

341
  if (state.state != TAOS_SYNC_STATE_LEADER) {
16,620✔
342
    (void)taosThreadRwlockUnlock(&pMnode->lock);
576✔
343
    terrno = TSDB_CODE_SYN_NOT_LEADER;
576✔
344
    return true;
576✔
345
  }
346
  if (!state.restored || !pMnode->restored) {
16,044!
347
    (void)taosThreadRwlockUnlock(&pMnode->lock);
4✔
348
    terrno = TSDB_CODE_SYN_RESTORING;
4✔
349
    return true;
4✔
350
  }
351
  (void)taosThreadRwlockUnlock(&pMnode->lock);
16,040✔
352
  return false;
16,040✔
353
}
354

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

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

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

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

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

389
  if (sec % tsTransPullupInterval == 0) {
33,124✔
390
    mndPullupTrans(pMnode);
16,711✔
391
  }
392

393
  if (sec % tsCompactPullupInterval == 0) {
33,124✔
394
    mndPullupCompacts(pMnode);
3,031✔
395
  }
396

397
  if (sec % tsMqRebalanceInterval == 0) {
33,124✔
398
    mndCalMqRebalance(pMnode);
16,040✔
399
  }
400

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

405
  if (sec % tsStreamNodeCheckInterval == 0) {
33,124✔
406
    mndStreamCheckNode(pMnode);
1,435✔
407
  }
408

409
  if (sec % 5 == 0) {
33,124✔
410
    mndStreamConsensusChkpt(pMnode);
6,332✔
411
  }
412

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

417
  if (sec % tsGrantHBInterval == 0) {
33,124✔
418
    mndPullupGrant(pMnode);
2,283✔
419
  }
420

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

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

431
  if (sec % (tsArbCheckSyncIntervalSec) == 0) {
33,124✔
432
    if ((code = mndPullupArbCheckSync(pMnode)) != 0) {
3,031!
433
      mError("failed to pullup arb check sync, since:%s", tstrerror(code));
×
434
    }
435
  }
436
}
33,124✔
437
void mndDoTimerCheckTask(SMnode *pMnode, int64_t sec) {
33,704✔
438
  if (sec % (tsStatusInterval * 5) == 0) {
33,704✔
439
    mndCheckDnodeOffline(pMnode);
6,404✔
440
  }
441
  if (sec % (MNODE_TIMEOUT_SEC / 2) == 0) {
33,704✔
442
    mndSyncCheckTimeout(pMnode);
906✔
443
  }
444
}
33,704✔
445

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

451
  while (1) {
341,409✔
452
    lastTime++;
342,318✔
453
    taosMsleep(100);
342,318✔
454
    if (mndGetStop(pMnode)) break;
342,318✔
455
    if (lastTime % 10 != 0) continue;
341,409✔
456

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

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

470
  return NULL;
909✔
471
}
472

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

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

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

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

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

508
  TAOS_RETURN(code);
910✔
509
}
510

511
static int32_t mndInitWal(SMnode *pMnode) {
910✔
512
  int32_t code = 0;
910✔
513
  char    path[PATH_MAX + 20] = {0};
910✔
514
  (void)snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
910✔
515
  SWalCfg cfg = {.vgId = 1,
910✔
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) {
910!
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);
910✔
539
  if (pMnode->pWal == NULL) {
910!
UNCOV
540
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
541
    if (terrno != 0) code = terrno;
×
UNCOV
542
    mError("failed to open wal since %s. wal:%s", tstrerror(code), path);
×
UNCOV
543
    TAOS_RETURN(code);
×
544
  }
545

546
  TAOS_RETURN(code);
910✔
547
}
548

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

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

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

570
  TAOS_RETURN(code);
910✔
571
}
572

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

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

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

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

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

601
  TAOS_RETURN(0);
31,850✔
602
}
603

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

641
  return 0;
910✔
642
}
643

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

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

651
  for (int32_t s = pos; s >= 0; s--) {
32,724✔
652
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, s);
31,815✔
653
    mInfo("%s will cleanup", pStep->name);
31,815!
654
    if (pStep->cleanupFp != NULL) {
31,815✔
655
      (*pStep->cleanupFp)(pMnode);
30,906✔
656
    }
657
  }
658

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

664
static int32_t mndExecSteps(SMnode *pMnode) {
910✔
665
  int32_t code = 0;
910✔
666
  int32_t size = taosArrayGetSize(pMnode->pSteps);
910✔
667
  for (int32_t pos = 0; pos < size; pos++) {
32,760✔
668
    SMnodeStep *pStep = taosArrayGet(pMnode->pSteps, pos);
31,850✔
669
    if (pStep->initFp == NULL) continue;
31,850!
670

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

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

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

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

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

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

725
  pMnode->deploy = pOption->deploy;
910✔
726
  pMnode->pSteps = taosArrayInit(24, sizeof(SMnodeStep));
910✔
727
  if (pMnode->pSteps == NULL) {
910!
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);
910✔
735
  if (code != 0) {
910!
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);
910✔
744
  if (code != 0) {
910!
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);
910✔
753
  if (code != 0) {
910!
UNCOV
754
    code = terrno;
×
UNCOV
755
    mError("failed to open mnode since %s", tstrerror(code));
×
UNCOV
756
    mndClose(pMnode);
×
UNCOV
757
    terrno = code;
×
UNCOV
758
    return NULL;
×
759
  }
760

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

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

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

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

801
  grantReset(pMnode, TSDB_GRANT_ALL, 0);
909✔
802

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

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

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

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

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

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

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

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

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

842
  return code;
58,961✔
843
}
844

845
static int32_t mndCheckMnodeState(SRpcMsg *pMsg) {
5,966,865✔
846
  int32_t code = 0;
5,966,865✔
847
  if (!IsReq(pMsg)) TAOS_RETURN(code);
5,966,865✔
848
  if (pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_SCH_MERGE_QUERY ||
5,842,284!
849
      pMsg->msgType == TDMT_SCH_QUERY_CONTINUE || pMsg->msgType == TDMT_SCH_QUERY_HEARTBEAT ||
5,835,884!
850
      pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_SCH_MERGE_FETCH || pMsg->msgType == TDMT_SCH_DROP_TASK ||
5,830,592✔
851
      pMsg->msgType == TDMT_SCH_TASK_NOTIFY) {
5,817,875!
852
    TAOS_RETURN(code);
24,406✔
853
  }
854

855
  SMnode *pMnode = pMsg->info.node;
5,817,878✔
856
  (void)taosThreadRwlockRdlock(&pMnode->lock);
5,817,878✔
857
  if (pMnode->stopped) {
5,817,932✔
858
    (void)taosThreadRwlockUnlock(&pMnode->lock);
44✔
859
    code = TSDB_CODE_APP_IS_STOPPING;
44✔
860
    TAOS_RETURN(code);
44✔
861
  }
862

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

871
  if (state.state != TAOS_SYNC_STATE_LEADER) {
5,817,972✔
872
    (void)taosThreadRwlockUnlock(&pMnode->lock);
10,589✔
873
    code = TSDB_CODE_SYN_NOT_LEADER;
10,586✔
874
    goto _OVER;
10,586✔
875
  }
876

877
  if (!state.restored || !pMnode->restored) {
5,807,383✔
878
    (void)taosThreadRwlockUnlock(&pMnode->lock);
348✔
879
    code = TSDB_CODE_SYN_RESTORING;
363✔
880
    goto _OVER;
363✔
881
  }
882

883
#if 1
884
  (void)atomic_add_fetch_32(&pMnode->rpcRef, 1);
5,807,035✔
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);
5,807,028✔
891
  TAOS_RETURN(code);
5,806,759✔
892

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

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

910
  mGDebug(
10,170!
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;
10,170!
917

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

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

932
  TAOS_RETURN(code);
10,170✔
933
}
934

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

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

953
  mGTrace("msg:%p, start to process in mnode, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
5,955,661!
954
  if (fp)
5,955,661✔
955
    code = (*fp)(pMsg);
5,931,176✔
956
  else
957
    code = (*fpExt)(pMsg, pQueueInfo);
24,485✔
958
  mndReleaseRpc(pMnode);
5,955,657✔
959

960
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
5,955,955✔
961
    mGTrace("msg:%p, won't response immediately since in progress", pMsg);
35,844!
962
  } else if (code == 0) {
5,920,111✔
963
    mGTrace("msg:%p, successfully processed", pMsg);
5,918,164!
964
  } else {
965
    // TODO removve this wrong set code
966
    if (code == -1) {
1,947✔
967
      code = terrno;
4✔
968
    }
969
    mGError("msg:%p, failed to process since %s, app:%p type:%s", pMsg, tstrerror(code), pMsg->info.ahandle,
1,947!
970
            TMSG_INFO(pMsg->msgType));
971
  }
972

973
  TAOS_RETURN(code);
5,955,955✔
974
}
975

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

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

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

1003
int32_t mndGetMonitorInfo(SMnode *pMnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo,
11✔
1004
                          SMonStbInfo *pStbInfo, SMonGrantInfo *pGrantInfo) {
1005
  int32_t code = 0;
11✔
1006
  TAOS_CHECK_RETURN(mndAcquireRpc(pMnode));
11!
1007

1008
  SSdb   *pSdb = pMnode->pSdb;
11✔
1009
  int64_t ms = taosGetTimestampMs();
11✔
1010

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

1023
  // cluster info
1024
  tstrncpy(pClusterInfo->version, td_version, sizeof(pClusterInfo->version));
11✔
1025
  pClusterInfo->monitor_interval = tsMonitorInterval;
11✔
1026
  pClusterInfo->connections_total = mndGetNumOfConnections(pMnode);
11✔
1027
  pClusterInfo->dbs_total = sdbGetSize(pSdb, SDB_DB);
11✔
1028
  pClusterInfo->stbs_total = sdbGetSize(pSdb, SDB_STB);
11✔
1029
  pClusterInfo->topics_toal = sdbGetSize(pSdb, SDB_TOPIC);
11✔
1030
  pClusterInfo->streams_total = sdbGetSize(pSdb, SDB_STREAM);
11✔
1031

1032
  void *pIter = NULL;
11✔
1033
  while (1) {
11✔
1034
    SDnodeObj *pObj = NULL;
22✔
1035
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
22✔
1036
    if (pIter == NULL) break;
22✔
1037

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

1052
  pIter = NULL;
11✔
1053
  while (1) {
11✔
1054
    SMnodeObj *pObj = NULL;
22✔
1055
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
22✔
1056
    if (pIter == NULL) break;
22✔
1057

1058
    SMonMnodeDesc desc = {0};
11✔
1059
    desc.mnode_id = pObj->id;
11✔
1060
    tstrncpy(desc.mnode_ep, pObj->pDnode->ep, sizeof(desc.mnode_ep));
11✔
1061

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

1080
  // vgroup info
1081
  pIter = NULL;
11✔
1082
  while (1) {
20✔
1083
    SVgObj *pVgroup = NULL;
31✔
1084
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
31✔
1085
    if (pIter == NULL) break;
31✔
1086

1087
    pClusterInfo->vgroups_total++;
20✔
1088
    pClusterInfo->tbs_total += pVgroup->numOfTables;
20✔
1089

1090
    SMonVgroupDesc desc = {0};
20✔
1091
    desc.vgroup_id = pVgroup->vgId;
20✔
1092

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

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

1121
    if (taosArrayPush(pVgroupInfo->vgroups, &desc) == NULL) {
40!
1122
      mError("failed to put vgroup into array, but continue at this monitor report")
×
1123
    }
1124
    sdbRelease(pSdb, pVgroup);
20✔
1125
  }
1126

1127
  // stb info
1128
  pIter = NULL;
11✔
1129
  while (1) {
10✔
1130
    SStbObj *pStb = NULL;
21✔
1131
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
21✔
1132
    if (pIter == NULL) break;
21✔
1133

1134
    SMonStbDesc desc = {0};
10✔
1135

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

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

1154
    if (taosArrayPush(pStbInfo->stbs, &desc) == NULL) {
20!
1155
      mError("failed to put stb into array, but continue at this monitor report");
×
1156
    }
1157
    sdbRelease(pSdb, pStb);
10✔
1158
  }
1159

1160
  // grant info
1161
  pGrantInfo->expire_time = (pMnode->grant.expireTimeMS - ms) / 1000;
11✔
1162
  pGrantInfo->timeseries_total = pMnode->grant.timeseriesAllowed;
11✔
1163
  if (pMnode->grant.expireTimeMS == 0) {
11!
1164
    pGrantInfo->expire_time = 0;
×
1165
    pGrantInfo->timeseries_total = 0;
×
1166
  }
1167

1168
  mndReleaseRpc(pMnode);
11✔
1169
  TAOS_RETURN(code);
11✔
1170
}
1171

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

1183
int64_t mndGetRoleTimeMs(SMnode *pMnode) {
3,031✔
1184
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
3,031✔
1185
  return state.roleTimeMs;
3,031✔
1186
}
1187

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

1206
bool mndGetRestored(SMnode *pMnode) { return pMnode->restored; }
×
1207

1208
void mndSetStop(SMnode *pMnode) {
909✔
1209
  (void)taosThreadRwlockWrlock(&pMnode->lock);
909✔
1210
  pMnode->stopped = true;
909✔
1211
  (void)taosThreadRwlockUnlock(&pMnode->lock);
909✔
1212
  mInfo("mnode set stopped");
909!
1213
}
909✔
1214

1215
bool mndGetStop(SMnode *pMnode) { return pMnode->stopped; }
342,318✔
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