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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

65.27
/source/dnode/mnode/impl/src/mndDnode.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 "mndDnode.h"
18
#include <stdio.h>
19
#include "audit.h"
20
#include "mndBnode.h"
21
#include "mndCluster.h"
22
#include "mndDb.h"
23
#include "mndMnode.h"
24
#include "mndMount.h"
25
#include "mndPrivilege.h"
26
#include "mndQnode.h"
27
#include "mndShow.h"
28
#include "mndSnode.h"
29
#include "mndTrans.h"
30
#include "mndUser.h"
31
#include "mndVgroup.h"
32
#include "taos_monitor.h"
33
#include "tconfig.h"
34
#include "tjson.h"
35
#include "tmisce.h"
36
#include "tunit.h"
37

38
#define TSDB_DNODE_VER_NUMBER   2
39
#define TSDB_DNODE_RESERVE_SIZE 40
40

41
static const char *offlineReason[] = {
42
    "",
43
    "status msg timeout",
44
    "status not received",
45
    "version not match",
46
    "dnodeId not match",
47
    "clusterId not match",
48
    "statusInterval not match",
49
    "timezone not match",
50
    "locale not match",
51
    "charset not match",
52
    "ttlChangeOnWrite not match",
53
    "enableWhiteList not match",
54
    "encryptionKey not match",
55
    "monitor not match",
56
    "monitor switch not match",
57
    "monitor interval not match",
58
    "monitor slow log threshold not match",
59
    "monitor slow log sql max len not match",
60
    "monitor slow log scope not match",
61
    "unknown",
62
};
63

64
enum {
65
  DND_ACTIVE_CODE,
66
  DND_CONN_ACTIVE_CODE,
67
};
68

69
enum {
70
  DND_CREATE,
71
  DND_ADD,
72
  DND_DROP,
73
};
74

75
static int32_t  mndCreateDefaultDnode(SMnode *pMnode);
76
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode);
77
static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw);
78
static int32_t  mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode);
79
static int32_t  mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode);
80
static int32_t  mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew);
81
static int32_t  mndProcessDnodeListReq(SRpcMsg *pReq);
82

83
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq);
84
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq);
85
static int32_t mndProcessStatusReq(SRpcMsg *pReq);
86
static int32_t mndProcessNotifyReq(SRpcMsg *pReq);
87
static int32_t mndProcessRestoreDnodeReq(SRpcMsg *pReq);
88
static int32_t mndProcessStatisReq(SRpcMsg *pReq);
89
static int32_t mndProcessAuditReq(SRpcMsg *pReq);
90
static int32_t mndProcessUpdateDnodeInfoReq(SRpcMsg *pReq);
91
static int32_t mndProcessCreateEncryptKeyReq(SRpcMsg *pRsp);
92
static int32_t mndProcessCreateEncryptKeyRsp(SRpcMsg *pRsp);
93

94
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
95
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
96
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
97
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
98

99
#ifdef _GRANT
100
int32_t mndUpdClusterInfo(SRpcMsg *pReq);
101
#else
102
static int32_t mndUpdClusterInfo(SRpcMsg *pReq) { return 0; }
103
#endif
104

105
int32_t mndInitDnode(SMnode *pMnode) {
1,338✔
106
  SSdbTable table = {
1,338✔
107
      .sdbType = SDB_DNODE,
108
      .keyType = SDB_KEY_INT32,
109
      .deployFp = (SdbDeployFp)mndCreateDefaultDnode,
110
      .encodeFp = (SdbEncodeFp)mndDnodeActionEncode,
111
      .decodeFp = (SdbDecodeFp)mndDnodeActionDecode,
112
      .insertFp = (SdbInsertFp)mndDnodeActionInsert,
113
      .updateFp = (SdbUpdateFp)mndDnodeActionUpdate,
114
      .deleteFp = (SdbDeleteFp)mndDnodeActionDelete,
115
  };
116

117
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
1,338✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
1,338✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
1,338✔
120
  mndSetMsgHandle(pMnode, TDMT_MND_NOTIFY, mndProcessNotifyReq);
1,338✔
121
  mndSetMsgHandle(pMnode, TDMT_MND_DNODE_LIST, mndProcessDnodeListReq);
1,338✔
122
  mndSetMsgHandle(pMnode, TDMT_MND_RESTORE_DNODE, mndProcessRestoreDnodeReq);
1,338✔
123
  mndSetMsgHandle(pMnode, TDMT_MND_STATIS, mndProcessStatisReq);
1,338✔
124
  mndSetMsgHandle(pMnode, TDMT_MND_AUDIT, mndProcessAuditReq);
1,338✔
125
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_ENCRYPT_KEY, mndProcessCreateEncryptKeyReq);
1,338✔
126
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_ENCRYPT_KEY_RSP, mndProcessCreateEncryptKeyRsp);
1,338✔
127
  mndSetMsgHandle(pMnode, TDMT_MND_UPDATE_DNODE_INFO, mndProcessUpdateDnodeInfoReq);
1,338✔
128

129
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
1,338✔
130
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
1,338✔
131
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
1,338✔
132
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);
1,338✔
133

134
  return sdbSetTable(pMnode->pSdb, table);
1,338✔
135
}
136

137
void mndCleanupDnode(SMnode *pMnode) {}
1,338✔
138

139
static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
882✔
140
  int32_t  code = -1;
882✔
141
  SSdbRaw *pRaw = NULL;
882✔
142
  STrans  *pTrans = NULL;
882✔
143

144
  SDnodeObj dnodeObj = {0};
882✔
145
  dnodeObj.id = 1;
882✔
146
  dnodeObj.createdTime = taosGetTimestampMs();
882✔
147
  dnodeObj.updateTime = dnodeObj.createdTime;
882✔
148
  dnodeObj.port = tsServerPort;
882✔
149
  tstrncpy(dnodeObj.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
882✔
150
  dnodeObj.fqdn[TSDB_FQDN_LEN - 1] = 0;
882✔
151
  (void)snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", tsLocalFqdn, tsServerPort);
882✔
152
  char *machineId = NULL;
882✔
153
  code = tGetMachineId(&machineId);
882✔
154
  if (machineId) {
882!
155
    (void)memcpy(dnodeObj.machineId, machineId, TSDB_MACHINE_ID_LEN);
882✔
156
    taosMemoryFreeClear(machineId);
882!
157
  } else {
158
#if defined(TD_ENTERPRISE) && !defined(GRANTS_CFG)
159
    terrno = TSDB_CODE_DNODE_NO_MACHINE_CODE;
×
160
    goto _OVER;
×
161
#endif
162
  }
163

164
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL, "create-dnode");
882✔
165
  if (pTrans == NULL) {
882!
166
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
167
    if (terrno != 0) code = terrno;
×
168
    goto _OVER;
×
169
  }
170
  mInfo("trans:%d, used to create dnode:%s on first deploy", pTrans->id, dnodeObj.ep);
882!
171

172
  pRaw = mndDnodeActionEncode(&dnodeObj);
882✔
173
  if (pRaw == NULL) {
882!
174
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
175
    if (terrno != 0) code = terrno;
×
176
    goto _OVER;
×
177
  }
178
  TAOS_CHECK_GOTO(mndTransAppendCommitlog(pTrans, pRaw), NULL, _OVER);
882!
179
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), NULL, _OVER);
882!
180
  pRaw = NULL;
882✔
181

182
  TAOS_CHECK_GOTO(mndTransPrepare(pMnode, pTrans), NULL, _OVER);
882!
183
  code = 0;
882✔
184
  (void)mndUpdateIpWhiteForAllUser(pMnode, TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD,
882✔
185
                                   1);  // TODO: check the return value
186

187
_OVER:
882✔
188
  mndTransDrop(pTrans);
882✔
189
  sdbFreeRaw(pRaw);
882✔
190
  return code;
882✔
191
}
192

193
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
8,557✔
194
  int32_t code = 0;
8,557✔
195
  int32_t lino = 0;
8,557✔
196
  terrno = TSDB_CODE_OUT_OF_MEMORY;
8,557✔
197

198
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
8,557✔
199
  if (pRaw == NULL) goto _OVER;
8,557!
200

201
  int32_t dataPos = 0;
8,557✔
202
  SDB_SET_INT32(pRaw, dataPos, pDnode->id, _OVER)
8,557!
203
  SDB_SET_INT64(pRaw, dataPos, pDnode->createdTime, _OVER)
8,557!
204
  SDB_SET_INT64(pRaw, dataPos, pDnode->updateTime, _OVER)
8,557!
205
  SDB_SET_INT16(pRaw, dataPos, pDnode->port, _OVER)
8,557!
206
  SDB_SET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, _OVER)
8,557!
207
  SDB_SET_BINARY(pRaw, dataPos, pDnode->machineId, TSDB_MACHINE_ID_LEN, _OVER)
8,557!
208
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, _OVER)
8,557!
209
  SDB_SET_INT16(pRaw, dataPos, 0, _OVER)  // forward/backward compatible
8,557!
210
  SDB_SET_INT16(pRaw, dataPos, 0, _OVER)  // forward/backward compatible
8,557!
211
  SDB_SET_DATALEN(pRaw, dataPos, _OVER);
8,557!
212

213
  terrno = 0;
8,557✔
214

215
_OVER:
8,557✔
216
  if (terrno != 0) {
8,557!
217
    mError("dnode:%d, failed to encode to raw:%p since %s", pDnode->id, pRaw, terrstr());
×
218
    sdbFreeRaw(pRaw);
×
219
    return NULL;
×
220
  }
221

222
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
8,557✔
223
  return pRaw;
8,557✔
224
}
225

226
static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
7,092✔
227
  int32_t code = 0;
7,092✔
228
  int32_t lino = 0;
7,092✔
229
  terrno = TSDB_CODE_OUT_OF_MEMORY;
7,092✔
230
  SSdbRow   *pRow = NULL;
7,092✔
231
  SDnodeObj *pDnode = NULL;
7,092✔
232

233
  int8_t sver = 0;
7,092✔
234
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
7,092!
235
  if (sver < 1 || sver > TSDB_DNODE_VER_NUMBER) {
7,092!
236
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
237
    goto _OVER;
×
238
  }
239

240
  pRow = sdbAllocRow(sizeof(SDnodeObj));
7,092✔
241
  if (pRow == NULL) goto _OVER;
7,092!
242

243
  pDnode = sdbGetRowObj(pRow);
7,092✔
244
  if (pDnode == NULL) goto _OVER;
7,092!
245

246
  int32_t dataPos = 0;
7,092✔
247
  SDB_GET_INT32(pRaw, dataPos, &pDnode->id, _OVER)
7,092!
248
  SDB_GET_INT64(pRaw, dataPos, &pDnode->createdTime, _OVER)
7,092!
249
  SDB_GET_INT64(pRaw, dataPos, &pDnode->updateTime, _OVER)
7,092!
250
  SDB_GET_INT16(pRaw, dataPos, &pDnode->port, _OVER)
7,092!
251
  SDB_GET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, _OVER)
7,092!
252
  SDB_GET_BINARY(pRaw, dataPos, pDnode->machineId, TSDB_MACHINE_ID_LEN, _OVER)
7,092!
253
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, _OVER)
7,092!
254
  if (sver > 1) {
7,092!
255
    int16_t keyLen = 0;
7,092✔
256
    SDB_GET_INT16(pRaw, dataPos, &keyLen, _OVER)
7,092!
257
    SDB_GET_BINARY(pRaw, dataPos, NULL, keyLen, _OVER)
7,092!
258
    SDB_GET_INT16(pRaw, dataPos, &keyLen, _OVER)
7,092!
259
    SDB_GET_BINARY(pRaw, dataPos, NULL, keyLen, _OVER)
7,092!
260
  }
261

262
  terrno = 0;
7,092✔
263
  if (tmsgUpdateDnodeInfo(&pDnode->id, NULL, pDnode->fqdn, &pDnode->port)) {
7,092!
264
    mInfo("dnode:%d, endpoint changed", pDnode->id);
×
265
  }
266

267
_OVER:
7,092✔
268
  if (terrno != 0) {
7,092!
269
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode == NULL ? 0 : pDnode->id, pRaw, terrstr());
×
270
    taosMemoryFreeClear(pRow);
×
271
    return NULL;
×
272
  }
273

274
  mTrace("dnode:%d, decode from raw:%p, row:%p ep:%s:%u", pDnode->id, pRaw, pDnode, pDnode->fqdn, pDnode->port);
7,092✔
275
  return pRow;
7,092✔
276
}
277

278
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
3,080✔
279
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
3,080✔
280
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
3,080✔
281

282
  char ep[TSDB_EP_LEN] = {0};
3,080✔
283
  (void)snprintf(ep, TSDB_EP_LEN - 1, "%s:%u", pDnode->fqdn, pDnode->port);
3,080✔
284
  tstrncpy(pDnode->ep, ep, TSDB_EP_LEN);
3,080✔
285
  return 0;
3,080✔
286
}
287

288
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
7,092✔
289
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
7,092✔
290
  return 0;
7,092✔
291
}
292

293
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
3,985✔
294
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
3,985✔
295
  pOld->updateTime = pNew->updateTime;
3,985✔
296
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
297
  tstrncpy(pOld->machineId, pNew->machineId, TSDB_MACHINE_ID_LEN + 1);
3,985✔
298
#endif
299
  return 0;
3,985✔
300
}
301

302
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
356,892✔
303
  SSdb      *pSdb = pMnode->pSdb;
356,892✔
304
  SDnodeObj *pDnode = sdbAcquire(pSdb, SDB_DNODE, &dnodeId);
356,892✔
305
  if (pDnode == NULL) {
356,893✔
306
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
606✔
307
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
210✔
308
    } else if (terrno == TSDB_CODE_SDB_OBJ_CREATING) {
396!
309
      terrno = TSDB_CODE_MND_DNODE_IN_CREATING;
×
310
    } else if (terrno == TSDB_CODE_SDB_OBJ_DROPPING) {
396!
311
      terrno = TSDB_CODE_MND_DNODE_IN_DROPPING;
396✔
312
    } else {
313
      terrno = TSDB_CODE_APP_ERROR;
×
314
      mFatal("dnode:%d, failed to acquire db since %s", dnodeId, terrstr());
×
315
    }
316
  }
317

318
  return pDnode;
356,893✔
319
}
320

321
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
360,626✔
322
  SSdb *pSdb = pMnode->pSdb;
360,626✔
323
  sdbRelease(pSdb, pDnode);
360,626✔
324
}
360,631✔
325

326
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
23,086✔
327
  SEpSet epSet = {0};
23,086✔
328
  terrno = addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
23,086✔
329
  return epSet;
23,086✔
330
}
331

332
SEpSet mndGetDnodeEpsetById(SMnode *pMnode, int32_t dnodeId) {
2,549✔
333
  SEpSet     epSet = {0};
2,549✔
334
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId);
2,549✔
335
  if (!pDnode) return epSet;
2,549!
336

337
  epSet = mndGetDnodeEpset(pDnode);
2,549✔
338

339
  mndReleaseDnode(pMnode, pDnode);
2,549✔
340
  return epSet;
2,549✔
341
}
342

343
static SDnodeObj *mndAcquireDnodeByEp(SMnode *pMnode, char *pEpStr) {
4,023✔
344
  SSdb *pSdb = pMnode->pSdb;
4,023✔
345

346
  void *pIter = NULL;
4,023✔
347
  while (1) {
7,860✔
348
    SDnodeObj *pDnode = NULL;
11,883✔
349
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
11,883✔
350
    if (pIter == NULL) break;
11,883✔
351

352
    if (taosStrncasecmp(pEpStr, pDnode->ep, TSDB_EP_LEN) == 0) {
9,672✔
353
      sdbCancelFetch(pSdb, pIter);
1,812✔
354
      return pDnode;
1,812✔
355
    }
356

357
    sdbRelease(pSdb, pDnode);
7,860✔
358
  }
359

360
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
2,211✔
361
  return NULL;
2,211✔
362
}
363

364
static SDnodeObj *mndAcquireDnodeAllStatusByEp(SMnode *pMnode, char *pEpStr) {
305✔
365
  SSdb *pSdb = pMnode->pSdb;
305✔
366

367
  void *pIter = NULL;
305✔
368
  while (1) {
345✔
369
    SDnodeObj *pDnode = NULL;
650✔
370
    ESdbStatus objStatus = 0;
650✔
371
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
650✔
372
    if (pIter == NULL) break;
650!
373

374
    if (taosStrncasecmp(pEpStr, pDnode->ep, TSDB_EP_LEN) == 0) {
650✔
375
      sdbCancelFetch(pSdb, pIter);
305✔
376
      return pDnode;
305✔
377
    }
378

379
    sdbRelease(pSdb, pDnode);
345✔
380
  }
381

382
  return NULL;
×
383
}
384

385
int32_t mndGetDnodeSize(SMnode *pMnode) {
152,488✔
386
  SSdb *pSdb = pMnode->pSdb;
152,488✔
387
  return sdbGetSize(pSdb, SDB_DNODE);
152,488✔
388
}
389

390
int32_t mndGetDbSize(SMnode *pMnode) {
×
391
  SSdb *pSdb = pMnode->pSdb;
×
392
  return sdbGetSize(pSdb, SDB_DB);
×
393
}
394

395
bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs) {
194,649✔
396
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
194,649✔
397
  if (interval > (int64_t)tsStatusTimeoutMs) {
194,649✔
398
    if (pDnode->rebootTime > 0 && pDnode->offlineReason == DND_REASON_ONLINE) {
6,367✔
399
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
102✔
400
    }
401
    return false;
6,367✔
402
  }
403
  return true;
188,282✔
404
}
405

406
static void mndGetDnodeEps(SMnode *pMnode, SArray *pDnodeEps) {
8,134✔
407
  SSdb *pSdb = pMnode->pSdb;
8,134✔
408

409
  int32_t numOfEps = 0;
8,134✔
410
  void   *pIter = NULL;
8,134✔
411
  while (1) {
30,471✔
412
    SDnodeObj *pDnode = NULL;
38,605✔
413
    ESdbStatus objStatus = 0;
38,605✔
414
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
38,605✔
415
    if (pIter == NULL) break;
38,605✔
416

417
    SDnodeEp dnodeEp = {0};
30,471✔
418
    dnodeEp.id = pDnode->id;
30,471✔
419
    dnodeEp.ep.port = pDnode->port;
30,471✔
420
    tstrncpy(dnodeEp.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
30,471✔
421
    sdbRelease(pSdb, pDnode);
30,471✔
422

423
    dnodeEp.isMnode = 0;
30,471✔
424
    if (mndIsMnode(pMnode, pDnode->id)) {
30,471✔
425
      dnodeEp.isMnode = 1;
13,153✔
426
    }
427
    if (taosArrayPush(pDnodeEps, &dnodeEp) == NULL) {
30,471!
428
      mError("failed to put ep into array, but continue at this call");
×
429
    }
430
  }
431
}
8,134✔
432

433
int32_t mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo) {
63,585✔
434
  SSdb   *pSdb = pMnode->pSdb;
63,585✔
435
  int32_t code = 0;
63,585✔
436

437
  int32_t numOfEps = 0;
63,585✔
438
  void   *pIter = NULL;
63,585✔
439
  while (1) {
293,870✔
440
    SDnodeObj *pDnode = NULL;
357,455✔
441
    ESdbStatus objStatus = 0;
357,455✔
442
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
357,455✔
443
    if (pIter == NULL) break;
357,455✔
444

445
    SDnodeInfo dInfo;
446
    dInfo.id = pDnode->id;
293,870✔
447
    dInfo.ep.port = pDnode->port;
293,870✔
448
    dInfo.offlineReason = pDnode->offlineReason;
293,870✔
449
    tstrncpy(dInfo.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
293,870✔
450
    sdbRelease(pSdb, pDnode);
293,870✔
451
    if (mndIsMnode(pMnode, pDnode->id)) {
293,870✔
452
      dInfo.isMnode = 1;
102,262✔
453
    } else {
454
      dInfo.isMnode = 0;
191,608✔
455
    }
456

457
    if (taosArrayPush(pDnodeInfo, &dInfo) == NULL) {
293,870!
458
      code = terrno;
×
459
      sdbCancelFetch(pSdb, pIter);
×
460
      break;
×
461
    }
462
  }
463
  TAOS_RETURN(code);
63,585✔
464
}
465

466
#define CHECK_MONITOR_PARA(para, err)                                                                    \
467
  if (pCfg->monitorParas.para != para) {                                                                 \
468
    mError("dnode:%d, para:%d inconsistent with cluster:%d", pDnode->id, pCfg->monitorParas.para, para); \
469
    terrno = err;                                                                                        \
470
    return err;                                                                                          \
471
  }
472

473
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
8,134✔
474
  CHECK_MONITOR_PARA(tsEnableMonitor, DND_REASON_STATUS_MONITOR_SWITCH_NOT_MATCH);
8,134!
475
  CHECK_MONITOR_PARA(tsMonitorInterval, DND_REASON_STATUS_MONITOR_INTERVAL_NOT_MATCH);
8,134!
476
  CHECK_MONITOR_PARA(tsSlowLogThreshold, DND_REASON_STATUS_MONITOR_SLOW_LOG_THRESHOLD_NOT_MATCH);
8,134!
477
  CHECK_MONITOR_PARA(tsSlowLogMaxLen, DND_REASON_STATUS_MONITOR_SLOW_LOG_SQL_MAX_LEN_NOT_MATCH);
8,134!
478
  CHECK_MONITOR_PARA(tsSlowLogScope, DND_REASON_STATUS_MONITOR_SLOW_LOG_SCOPE_NOT_MATCH);
8,134!
479

480
  if (0 != taosStrcasecmp(pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb)) {
8,134!
481
    mError("dnode:%d, tsSlowLogExceptDb:%s inconsistent with cluster:%s", pDnode->id,
×
482
           pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb);
483
    terrno = TSDB_CODE_DNODE_INVALID_MONITOR_PARAS;
×
484
    return DND_REASON_STATUS_MONITOR_NOT_MATCH;
×
485
  }
486

487
  /*
488
  if (pCfg->statusIntervalMs != tsStatusIntervalMs) {
489
    mError("dnode:%d, statusInterval:%d inconsistent with cluster:%d", pDnode->id, pCfg->statusIntervalMs,
490
           tsStatusIntervalMs);
491
    terrno = TSDB_CODE_DNODE_INVALID_STATUS_INTERVAL;
492
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
493
  }
494
  */
495

496
  if ((0 != taosStrcasecmp(pCfg->timezone, tsTimezoneStr)) && (pMnode->checkTime != pCfg->checkTime)) {
8,134!
497
    mError("dnode:%d, timezone:%s checkTime:%" PRId64 " inconsistent with cluster %s %" PRId64, pDnode->id,
×
498
           pCfg->timezone, pCfg->checkTime, tsTimezoneStr, pMnode->checkTime);
499
    terrno = TSDB_CODE_DNODE_INVALID_TIMEZONE;
×
500
    return DND_REASON_TIME_ZONE_NOT_MATCH;
×
501
  }
502

503
  if (0 != taosStrcasecmp(pCfg->locale, tsLocale)) {
8,134!
504
    mError("dnode:%d, locale:%s inconsistent with cluster:%s", pDnode->id, pCfg->locale, tsLocale);
×
505
    terrno = TSDB_CODE_DNODE_INVALID_LOCALE;
×
506
    return DND_REASON_LOCALE_NOT_MATCH;
×
507
  }
508

509
  if (0 != taosStrcasecmp(pCfg->charset, tsCharset)) {
8,134!
510
    mError("dnode:%d, charset:%s inconsistent with cluster:%s", pDnode->id, pCfg->charset, tsCharset);
×
511
    terrno = TSDB_CODE_DNODE_INVALID_CHARSET;
×
512
    return DND_REASON_CHARSET_NOT_MATCH;
×
513
  }
514

515
  if (pCfg->ttlChangeOnWrite != tsTtlChangeOnWrite) {
8,134!
516
    mError("dnode:%d, ttlChangeOnWrite:%d inconsistent with cluster:%d", pDnode->id, pCfg->ttlChangeOnWrite,
×
517
           tsTtlChangeOnWrite);
518
    terrno = TSDB_CODE_DNODE_INVALID_TTL_CHG_ON_WR;
×
519
    return DND_REASON_TTL_CHANGE_ON_WRITE_NOT_MATCH;
×
520
  }
521
  int8_t enable = tsEnableWhiteList ? 1 : 0;
8,134✔
522
  if (pCfg->enableWhiteList != enable) {
8,134!
523
    mError("dnode:%d, enableWhiteList:%d inconsistent with cluster:%d", pDnode->id, pCfg->enableWhiteList, enable);
×
524
    terrno = TSDB_CODE_DNODE_INVALID_EN_WHITELIST;
×
525
    return DND_REASON_ENABLE_WHITELIST_NOT_MATCH;
×
526
  }
527

528
  if (!atomic_load_8(&pMnode->encryptMgmt.encrypting) &&
8,134!
529
      (pCfg->encryptionKeyStat != tsEncryptionKeyStat || pCfg->encryptionKeyChksum != tsEncryptionKeyChksum)) {
8,134!
530
    mError("dnode:%d, encryptionKey:%" PRIi8 "-%u inconsistent with cluster:%" PRIi8 "-%u", pDnode->id,
×
531
           pCfg->encryptionKeyStat, pCfg->encryptionKeyChksum, tsEncryptionKeyStat, tsEncryptionKeyChksum);
532
    terrno = pCfg->encryptionKeyChksum ? TSDB_CODE_DNODE_INVALID_ENCRYPTKEY : TSDB_CODE_DNODE_NO_ENCRYPT_KEY;
×
533
    return DND_REASON_ENCRYPTION_KEY_NOT_MATCH;
×
534
  }
535

536
  return DND_REASON_ONLINE;
8,134✔
537
}
538

539
double calcAppliedRate(int64_t currentCount, int64_t lastCount, int64_t currentTimeMs, int64_t lastTimeMs) {
3✔
540
  if ((currentTimeMs <= lastTimeMs) || (currentCount <= lastCount)) {
3!
541
    return 0.0;
×
542
  }
543

544
  int64_t deltaCount = currentCount - lastCount;
3✔
545
  int64_t deltaMs = currentTimeMs - lastTimeMs;
3✔
546
  double  rate = (double)deltaCount / (double)deltaMs;
3✔
547
  return rate;
3✔
548
}
549

550
static bool mndUpdateVnodeState(int32_t vgId, SVnodeGid *pGid, SVnodeLoad *pVload) {
229,620✔
551
  bool stateChanged = false;
229,620✔
552
  bool roleChanged = pGid->syncState != pVload->syncState ||
684,461✔
553
                     (pVload->syncTerm != -1 && pGid->syncTerm != pVload->syncTerm) ||
447,335!
554
                     pGid->roleTimeMs != pVload->roleTimeMs;
217,715✔
555

556
  if (pVload->syncCommitIndex > pVload->syncAppliedIndex) {
229,620✔
557
    if (pGid->lastSyncAppliedIndexUpdateTime == 0) {
66✔
558
      pGid->lastSyncAppliedIndexUpdateTime = taosGetTimestampMs();
63✔
559
    } else if (pGid->syncAppliedIndex != pVload->syncAppliedIndex) {
3!
560
      int64_t currentTimeMs = taosGetTimestampMs();
3✔
561
      pGid->appliedRate = calcAppliedRate(pVload->syncAppliedIndex, pGid->syncAppliedIndex, currentTimeMs,
3✔
562
                                          pGid->lastSyncAppliedIndexUpdateTime);
563

564
      pGid->lastSyncAppliedIndexUpdateTime = currentTimeMs;
3✔
565
    }
566
  }
567

568
  pGid->syncAppliedIndex = pVload->syncAppliedIndex;
229,620✔
569
  pGid->syncCommitIndex = pVload->syncCommitIndex;
229,620✔
570
  pGid->bufferSegmentUsed = pVload->bufferSegmentUsed;
229,620✔
571
  pGid->bufferSegmentSize = pVload->bufferSegmentSize;
229,620✔
572
  if (roleChanged || pGid->syncRestore != pVload->syncRestore || pGid->syncCanRead != pVload->syncCanRead ||
229,620!
573
      pGid->startTimeMs != pVload->startTimeMs) {
216,633!
574
    mInfo(
12,987!
575
        "vgId:%d, state changed by status msg, old state:%s restored:%d canRead:%d new state:%s restored:%d "
576
        "canRead:%d, dnode:%d",
577
        vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead, syncStr(pVload->syncState),
578
        pVload->syncRestore, pVload->syncCanRead, pGid->dnodeId);
579
    pGid->syncState = pVload->syncState;
12,987✔
580
    pGid->syncTerm = pVload->syncTerm;
12,987✔
581
    pGid->syncRestore = pVload->syncRestore;
12,987✔
582
    pGid->syncCanRead = pVload->syncCanRead;
12,987✔
583
    pGid->startTimeMs = pVload->startTimeMs;
12,987✔
584
    pGid->roleTimeMs = pVload->roleTimeMs;
12,987✔
585
    stateChanged = true;
12,987✔
586
  }
587
  return stateChanged;
229,620✔
588
}
589

590
static bool mndUpdateMnodeState(SMnodeObj *pObj, SMnodeLoad *pMload) {
46,822✔
591
  bool stateChanged = false;
46,822✔
592
  bool roleChanged = pObj->syncState != pMload->syncState ||
139,092✔
593
                     (pMload->syncTerm != -1 && pObj->syncTerm != pMload->syncTerm) ||
92,204!
594
                     pObj->roleTimeMs != pMload->roleTimeMs;
45,382✔
595
  if (roleChanged || pObj->syncRestore != pMload->syncRestore) {
46,822!
596
    mInfo("dnode:%d, mnode syncState from %s to %s, restoreState from %d to %d, syncTerm from %" PRId64 " to %" PRId64,
1,462!
597
          pObj->id, syncStr(pObj->syncState), syncStr(pMload->syncState), pObj->syncRestore, pMload->syncRestore,
598
          pObj->syncTerm, pMload->syncTerm);
599
    pObj->syncState = pMload->syncState;
1,462✔
600
    pObj->syncTerm = pMload->syncTerm;
1,462✔
601
    pObj->syncRestore = pMload->syncRestore;
1,462✔
602
    pObj->roleTimeMs = pMload->roleTimeMs;
1,462✔
603
    stateChanged = true;
1,462✔
604
  }
605
  return stateChanged;
46,822✔
606
}
607

608
extern char   *tsMonFwUri;
609
extern char   *tsMonSlowLogUri;
610
static int32_t mndProcessStatisReq(SRpcMsg *pReq) {
1✔
611
  SMnode    *pMnode = pReq->info.node;
1✔
612
  SStatisReq statisReq = {0};
1✔
613
  int32_t    code = -1;
1✔
614

615
  TAOS_CHECK_RETURN(tDeserializeSStatisReq(pReq->pCont, pReq->contLen, &statisReq));
1!
616

617
  if (tsMonitorLogProtocol) {
1!
618
    mInfo("process statis req,\n %s", statisReq.pCont);
×
619
  }
620

621
  if (statisReq.type == MONITOR_TYPE_COUNTER) {
1!
622
    monSendContent(statisReq.pCont, tsMonFwUri);
1✔
623
  } else if (statisReq.type == MONITOR_TYPE_SLOW_LOG) {
×
624
    monSendContent(statisReq.pCont, tsMonSlowLogUri);
×
625
  }
626

627
  tFreeSStatisReq(&statisReq);
1✔
628
  return 0;
1✔
629
}
630

631
static int32_t mndProcessAuditReq(SRpcMsg *pReq) {
5,304✔
632
  mTrace("process audit req:%p", pReq);
5,304!
633
  if (tsEnableAudit && tsEnableAuditDelete) {
5,304!
634
    SMnode   *pMnode = pReq->info.node;
5,304✔
635
    SAuditReq auditReq = {0};
5,304✔
636

637
    TAOS_CHECK_RETURN(tDeserializeSAuditReq(pReq->pCont, pReq->contLen, &auditReq));
5,304!
638

639
    mDebug("received audit req:%s, %s, %s, %s", auditReq.operation, auditReq.db, auditReq.table, auditReq.pSql);
5,304✔
640

641
    auditAddRecord(pReq, pMnode->clusterId, auditReq.operation, auditReq.db, auditReq.table, auditReq.pSql,
5,304✔
642
                   auditReq.sqlLen);
643

644
    tFreeSAuditReq(&auditReq);
5,304✔
645
  }
646
  return 0;
5,304✔
647
}
648

649
static int32_t mndUpdateDnodeObj(SMnode *pMnode, SDnodeObj *pDnode) {
2,717✔
650
  int32_t       code = 0, lino = 0;
2,717✔
651
  SDnodeInfoReq infoReq = {0};
2,717✔
652
  int32_t       contLen = 0;
2,717✔
653
  void         *pReq = NULL;
2,717✔
654

655
  infoReq.dnodeId = pDnode->id;
2,717✔
656
  tstrncpy(infoReq.machineId, pDnode->machineId, TSDB_MACHINE_ID_LEN + 1);
2,717✔
657

658
  if ((contLen = tSerializeSDnodeInfoReq(NULL, 0, &infoReq)) <= 0) {
2,717!
659
    TAOS_RETURN(contLen ? contLen : TSDB_CODE_OUT_OF_MEMORY);
×
660
  }
661
  pReq = rpcMallocCont(contLen);
2,717✔
662
  if (pReq == NULL) {
2,717!
663
    TAOS_RETURN(terrno);
×
664
  }
665

666
  if ((contLen = tSerializeSDnodeInfoReq(pReq, contLen, &infoReq)) <= 0) {
2,717!
667
    code = contLen;
×
668
    goto _exit;
×
669
  }
670

671
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPDATE_DNODE_INFO, .pCont = pReq, .contLen = contLen};
2,717✔
672
  TAOS_CHECK_EXIT(tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg));
2,717!
673
_exit:
2,717✔
674
  if (code < 0) {
2,717!
675
    mError("dnode:%d, failed to update dnode info since %s", pDnode->id, tstrerror(code));
×
676
  }
677
  TAOS_RETURN(code);
2,717✔
678
}
679

680
static int32_t mndProcessUpdateDnodeInfoReq(SRpcMsg *pReq) {
2,717✔
681
  int32_t       code = 0, lino = 0;
2,717✔
682
  SMnode       *pMnode = pReq->info.node;
2,717✔
683
  SDnodeInfoReq infoReq = {0};
2,717✔
684
  SDnodeObj    *pDnode = NULL;
2,717✔
685
  STrans       *pTrans = NULL;
2,717✔
686
  SSdbRaw      *pCommitRaw = NULL;
2,717✔
687

688
  TAOS_CHECK_EXIT(tDeserializeSDnodeInfoReq(pReq->pCont, pReq->contLen, &infoReq));
2,717!
689

690
  pDnode = mndAcquireDnode(pMnode, infoReq.dnodeId);
2,717✔
691
  if (pDnode == NULL) {
2,717✔
692
    TAOS_CHECK_EXIT(terrno);
1!
693
  }
694

695
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, NULL, "update-dnode-obj");
2,716✔
696
  if (pTrans == NULL) {
2,716!
697
    TAOS_CHECK_EXIT(terrno);
×
698
  }
699

700
  pDnode->updateTime = taosGetTimestampMs();
2,716✔
701

702
  if ((pCommitRaw = mndDnodeActionEncode(pDnode)) == NULL) {
2,716!
703
    TAOS_CHECK_EXIT(terrno);
×
704
  }
705
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
2,716!
706
    mError("trans:%d, failed to append commit log since %s", pTrans->id, tstrerror(code));
×
707
    TAOS_CHECK_EXIT(code);
×
708
  }
709
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
2,716!
710
  pCommitRaw = NULL;
2,716✔
711

712
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
2,716!
713
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
714
    TAOS_CHECK_EXIT(code);
×
715
  }
716

717
_exit:
2,716✔
718
  mndReleaseDnode(pMnode, pDnode);
2,717✔
719
  if (code != 0) {
2,717✔
720
    mError("dnode:%d, failed to update dnode info at line %d since %s", infoReq.dnodeId, lino, tstrerror(code));
1!
721
  }
722
  mndTransDrop(pTrans);
2,717✔
723
  sdbFreeRaw(pCommitRaw);
2,717✔
724
  TAOS_RETURN(code);
2,717✔
725
}
726

727
static int32_t mndProcessStatusReq(SRpcMsg *pReq) {
86,163✔
728
  SMnode    *pMnode = pReq->info.node;
86,163✔
729
  SStatusReq statusReq = {0};
86,163✔
730
  SDnodeObj *pDnode = NULL;
86,163✔
731
  int32_t    code = -1;
86,163✔
732

733
  TAOS_CHECK_GOTO(tDeserializeSStatusReq(pReq->pCont, pReq->contLen, &statusReq), NULL, _OVER);
86,163!
734

735
  int64_t clusterid = mndGetClusterId(pMnode);
86,163✔
736
  if (statusReq.clusterId != 0 && statusReq.clusterId != clusterid) {
86,163!
737
    code = TSDB_CODE_MND_DNODE_DIFF_CLUSTER;
×
738
    mWarn("dnode:%d, %s, its clusterid:%" PRId64 " differ from current clusterid:%" PRId64 ", code:0x%x",
×
739
          statusReq.dnodeId, statusReq.dnodeEp, statusReq.clusterId, clusterid, code);
740
    goto _OVER;
×
741
  }
742

743
  if (statusReq.dnodeId == 0) {
86,163✔
744
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
3,040✔
745
    if (pDnode == NULL) {
3,040✔
746
      mInfo("dnode:%s, not created yet", statusReq.dnodeEp);
1,240!
747
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
1,240✔
748
      if (terrno != 0) code = terrno;
1,240!
749
      goto _OVER;
1,240✔
750
    }
751
  } else {
752
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
83,123✔
753
    if (pDnode == NULL) {
83,123✔
754
      int32_t err = terrno;
483✔
755
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
483✔
756
      if (pDnode != NULL) {
483✔
757
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
12✔
758
        terrno = err;
12✔
759
        goto _OVER;
12✔
760
      }
761

762
      mError("dnode:%d, %s not exist, code:0x%x", statusReq.dnodeId, statusReq.dnodeEp, err);
471!
763
      if (err == TSDB_CODE_MND_DNODE_NOT_EXIST) {
471✔
764
        terrno = err;
166✔
765
        goto _OVER;
166✔
766
      } else {
767
        pDnode = mndAcquireDnodeAllStatusByEp(pMnode, statusReq.dnodeEp);
305✔
768
        if (pDnode == NULL) goto _OVER;
305!
769
      }
770
    }
771
  }
772

773
  pMnode->ipWhiteVer = mndGetIpWhiteVer(pMnode);
84,745✔
774

775
  int64_t analVer = sdbGetTableVer(pMnode->pSdb, SDB_ANODE);
84,745✔
776
  int64_t dnodeVer = sdbGetTableVer(pMnode->pSdb, SDB_DNODE) + sdbGetTableVer(pMnode->pSdb, SDB_MNODE);
84,745✔
777
  int64_t curMs = taosGetTimestampMs();
84,745✔
778
  bool    online = mndIsDnodeOnline(pDnode, curMs);
84,745✔
779
  bool    dnodeChanged = (statusReq.dnodeVer == 0) || (statusReq.dnodeVer != dnodeVer);
84,745✔
780
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
84,745✔
781
  bool    supportVnodesChanged = pDnode->numOfSupportVnodes != statusReq.numOfSupportVnodes;
84,745✔
782
  bool    encryptKeyChanged = pDnode->encryptionKeyChksum != statusReq.clusterCfg.encryptionKeyChksum;
84,745✔
783
  bool    enableWhiteListChanged = statusReq.clusterCfg.enableWhiteList != (tsEnableWhiteList ? 1 : 0);
84,745✔
784
  bool    analVerChanged = (analVer != statusReq.analVer);
84,745✔
785
  bool    needCheck = !online || dnodeChanged || reboot || supportVnodesChanged || analVerChanged ||
82,675!
786
                   pMnode->ipWhiteVer != statusReq.ipWhiteVer || encryptKeyChanged || enableWhiteListChanged;
167,420!
787
  const STraceId *trace = &pReq->info.traceId;
84,745✔
788
  char            timestamp[TD_TIME_STR_LEN] = {0};
84,745✔
789
  if (mDebugFlag & DEBUG_TRACE) (void)formatTimestampLocal(timestamp, statusReq.timestamp, TSDB_TIME_PRECISION_MILLI);
84,745✔
790
  mGTrace(
84,745!
791
      "dnode:%d, status received, accessTimes:%d check:%d online:%d reboot:%d changed:%d statusSeq:%d "
792
      "timestamp:%s",
793
      pDnode->id, pDnode->accessTimes, needCheck, online, reboot, dnodeChanged, statusReq.statusSeq, timestamp);
794

795
  if (reboot) {
84,745✔
796
    tsGrantHBInterval = GRANT_HEART_BEAT_MIN;
2,181✔
797
  }
798

799
  for (int32_t v = 0; v < taosArrayGetSize(statusReq.pVloads); ++v) {
316,969✔
800
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);
232,224✔
801

802
    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
232,224✔
803
    if (pVgroup != NULL) {
232,224✔
804
      if (pVload->syncState == TAOS_SYNC_STATE_LEADER || pVload->syncState == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
229,696✔
805
        pVgroup->cacheUsage = pVload->cacheUsage;
153,649✔
806
        pVgroup->numOfCachedTables = pVload->numOfCachedTables;
153,649✔
807
        pVgroup->numOfTables = pVload->numOfTables;
153,649✔
808
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
153,649✔
809
        pVgroup->totalStorage = pVload->totalStorage;
153,649✔
810
        pVgroup->compStorage = pVload->compStorage;
153,649✔
811
        pVgroup->pointsWritten = pVload->pointsWritten;
153,649✔
812
      }
813
      bool stateChanged = false;
229,696✔
814
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
336,088✔
815
        SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
336,012✔
816
        if (pGid->dnodeId == statusReq.dnodeId) {
336,012✔
817
          if (pVload->startTimeMs == 0) {
229,620!
818
            pVload->startTimeMs = statusReq.rebootTime;
×
819
          }
820
          if (pVload->roleTimeMs == 0) {
229,620!
821
            pVload->roleTimeMs = statusReq.rebootTime;
×
822
          }
823
          stateChanged = mndUpdateVnodeState(pVgroup->vgId, pGid, pVload);
229,620✔
824
          break;
229,620✔
825
        }
826
      }
827
      if (stateChanged) {
229,696✔
828
        SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
12,987✔
829
        if (pDb != NULL && pDb->stateTs != curMs) {
12,987✔
830
          mInfo("db:%s, stateTs changed by status msg, old stateTs:%" PRId64 " new stateTs:%" PRId64, pDb->name,
8,718!
831
                pDb->stateTs, curMs);
832
          pDb->stateTs = curMs;
8,718✔
833
        }
834
        mndReleaseDb(pMnode, pDb);
12,987✔
835
      }
836
    }
837

838
    mndReleaseVgroup(pMnode, pVgroup);
232,224✔
839
  }
840

841
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
84,745✔
842
  if (pObj != NULL) {
84,745✔
843
    if (statusReq.mload.roleTimeMs == 0) {
46,822✔
844
      statusReq.mload.roleTimeMs = statusReq.rebootTime;
1,046✔
845
    }
846
    (void)mndUpdateMnodeState(pObj, &statusReq.mload);
46,822✔
847
    mndReleaseMnode(pMnode, pObj);
46,822✔
848
  }
849

850
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
84,745✔
851
  if (pQnode != NULL) {
84,745✔
852
    pQnode->load = statusReq.qload;
665✔
853
    mndReleaseQnode(pMnode, pQnode);
665✔
854
  }
855

856
  if (needCheck) {
84,745✔
857
    if (statusReq.sver != tsVersion) {
8,134!
858
      if (pDnode != NULL) {
×
859
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
×
860
      }
861
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver, tsVersion);
×
862
      terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
863
      goto _OVER;
×
864
    }
865

866
    if (statusReq.dnodeId == 0) {
8,134✔
867
      mInfo("dnode:%d, %s first access, clusterId:%" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
1,800!
868
    } else {
869
      if (statusReq.clusterId != pMnode->clusterId) {
6,334!
870
        if (pDnode != NULL) {
×
871
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
×
872
        }
873
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
×
874
               pMnode->clusterId);
875
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
×
876
        goto _OVER;
×
877
      }
878
    }
879

880
    // Verify whether the cluster parameters are consistent when status change from offline to ready
881
    pDnode->offlineReason = mndCheckClusterCfgPara(pMnode, pDnode, &statusReq.clusterCfg);
8,134✔
882
    if (pDnode->offlineReason != 0) {
8,134!
883
      mError("dnode:%d, cluster cfg inconsistent since:%s", pDnode->id, offlineReason[pDnode->offlineReason]);
×
884
      if (terrno == 0) terrno = TSDB_CODE_MND_INVALID_CLUSTER_CFG;
×
885
      goto _OVER;
×
886
    }
887

888
    if (!online) {
8,134✔
889
      mInfo("dnode:%d, from offline to online, memory avail:%" PRId64 " total:%" PRId64 " cores:%.2f", pDnode->id,
2,070!
890
            statusReq.memAvail, statusReq.memTotal, statusReq.numOfCores);
891
    } else {
892
      mInfo("dnode:%d, send dnode epset, online:%d dnodeVer:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
6,064!
893
            statusReq.dnodeVer, dnodeVer, reboot);
894
    }
895

896
    pDnode->rebootTime = statusReq.rebootTime;
8,134✔
897
    pDnode->numOfCores = statusReq.numOfCores;
8,134✔
898
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
8,134✔
899
    pDnode->numOfDiskCfg = statusReq.numOfDiskCfg;
8,134✔
900
    pDnode->memAvail = statusReq.memAvail;
8,134✔
901
    pDnode->memTotal = statusReq.memTotal;
8,134✔
902
    pDnode->encryptionKeyStat = statusReq.clusterCfg.encryptionKeyStat;
8,134✔
903
    pDnode->encryptionKeyChksum = statusReq.clusterCfg.encryptionKeyChksum;
8,134✔
904
    if (memcmp(pDnode->machineId, statusReq.machineId, TSDB_MACHINE_ID_LEN) != 0) {
8,134✔
905
      tstrncpy(pDnode->machineId, statusReq.machineId, TSDB_MACHINE_ID_LEN + 1);
2,717✔
906
      if ((terrno = mndUpdateDnodeObj(pMnode, pDnode)) != 0) {
2,717!
907
        goto _OVER;
×
908
      }
909
    }
910

911
    SStatusRsp statusRsp = {0};
8,134✔
912
    statusRsp.statusSeq++;
8,134✔
913
    statusRsp.analVer = analVer;
8,134✔
914
    statusRsp.dnodeVer = dnodeVer;
8,134✔
915
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
8,134✔
916
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
8,134✔
917
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
8,134✔
918
    if (statusRsp.pDnodeEps == NULL) {
8,134!
919
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
920
      goto _OVER;
×
921
    }
922

923
    mndGetDnodeEps(pMnode, statusRsp.pDnodeEps);
8,134✔
924
    statusRsp.ipWhiteVer = pMnode->ipWhiteVer;
8,134✔
925

926
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
8,134✔
927
    void   *pHead = rpcMallocCont(contLen);
8,134✔
928
    contLen = tSerializeSStatusRsp(pHead, contLen, &statusRsp);
8,134✔
929
    taosArrayDestroy(statusRsp.pDnodeEps);
8,134✔
930
    if (contLen < 0) {
8,134!
931
      code = contLen;
×
932
      goto _OVER;
×
933
    }
934

935
    pReq->info.rspLen = contLen;
8,134✔
936
    pReq->info.rsp = pHead;
8,134✔
937
  }
938

939
  pDnode->accessTimes++;
84,745✔
940
  pDnode->lastAccessTime = curMs;
84,745✔
941
  if ((DND_REASON_ONLINE != pDnode->offlineReason) && (online || mndIsDnodeOnline(pDnode, curMs))) {
84,745!
942
    pDnode->offlineReason = DND_REASON_ONLINE;
×
943
  }
944
  code = 0;
84,745✔
945

946
_OVER:
86,163✔
947
  mndReleaseDnode(pMnode, pDnode);
86,163✔
948
  taosArrayDestroy(statusReq.pVloads);
86,163✔
949
  return mndUpdClusterInfo(pReq);
86,163✔
950
}
951

952
static int32_t mndProcessNotifyReq(SRpcMsg *pReq) {
×
953
  SMnode    *pMnode = pReq->info.node;
×
954
  SNotifyReq notifyReq = {0};
×
955
  int32_t    code = 0;
×
956

957
  if ((code = tDeserializeSNotifyReq(pReq->pCont, pReq->contLen, &notifyReq)) != 0) {
×
958
    terrno = code;
×
959
    goto _OVER;
×
960
  }
961

962
  int64_t clusterid = mndGetClusterId(pMnode);
×
963
  if (notifyReq.clusterId != 0 && notifyReq.clusterId != clusterid) {
×
964
    code = TSDB_CODE_MND_DNODE_DIFF_CLUSTER;
×
965
    mWarn("dnode:%d, its clusterid:%" PRId64 " differ from current cluster:%" PRId64 " since %s", notifyReq.dnodeId,
×
966
          notifyReq.clusterId, clusterid, tstrerror(code));
967
    goto _OVER;
×
968
  }
969

970
  int32_t nVgroup = taosArrayGetSize(notifyReq.pVloads);
×
971
  for (int32_t v = 0; v < nVgroup; ++v) {
×
972
    SVnodeLoadLite *pVload = taosArrayGet(notifyReq.pVloads, v);
×
973

974
    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
×
975
    if (pVgroup != NULL) {
×
976
      pVgroup->numOfTimeSeries = pVload->nTimeSeries;
×
977
      mndReleaseVgroup(pMnode, pVgroup);
×
978
    }
979
  }
980
  code = mndUpdClusterInfo(pReq);
×
981
_OVER:
×
982
  tFreeSNotifyReq(&notifyReq);
×
983
  return code;
×
984
}
985

986
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
500✔
987
  int32_t  code = -1;
500✔
988
  SSdbRaw *pRaw = NULL;
500✔
989
  STrans  *pTrans = NULL;
500✔
990

991
  SDnodeObj dnodeObj = {0};
500✔
992
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
500✔
993
  dnodeObj.createdTime = taosGetTimestampMs();
500✔
994
  dnodeObj.updateTime = dnodeObj.createdTime;
500✔
995
  dnodeObj.port = pCreate->port;
500✔
996
  tstrncpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
500✔
997
  (void)snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", pCreate->fqdn, pCreate->port);
500✔
998

999
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_GLOBAL, pReq, "create-dnode");
500✔
1000
  if (pTrans == NULL) {
500!
1001
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1002
    if (terrno != 0) code = terrno;
×
1003
    goto _OVER;
×
1004
  }
1005
  mInfo("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
500!
1006
  TAOS_CHECK_GOTO(mndTransCheckConflict(pMnode, pTrans), NULL, _OVER);
500!
1007

1008
  pRaw = mndDnodeActionEncode(&dnodeObj);
500✔
1009
  if (pRaw == NULL) {
500!
1010
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1011
    if (terrno != 0) code = terrno;
×
1012
    goto _OVER;
×
1013
  }
1014
  TAOS_CHECK_GOTO(mndTransAppendCommitlog(pTrans, pRaw), NULL, _OVER);
500!
1015
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), NULL, _OVER);
500!
1016
  pRaw = NULL;
500✔
1017

1018
  TAOS_CHECK_GOTO(mndTransPrepare(pMnode, pTrans), NULL, _OVER);
500!
1019
  code = 0;
500✔
1020

1021
  (void)mndUpdateIpWhiteForAllUser(pMnode, TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD,
500✔
1022
                                   1);  // TODO: check the return value
1023
_OVER:
500✔
1024
  mndTransDrop(pTrans);
500✔
1025
  sdbFreeRaw(pRaw);
500✔
1026
  return code;
500✔
1027
}
1028

1029
static int32_t mndProcessDnodeListReq(SRpcMsg *pReq) {
287✔
1030
  SMnode       *pMnode = pReq->info.node;
287✔
1031
  SSdb         *pSdb = pMnode->pSdb;
287✔
1032
  SDnodeObj    *pObj = NULL;
287✔
1033
  void         *pIter = NULL;
287✔
1034
  SDnodeListRsp rsp = {0};
287✔
1035
  int32_t       code = -1;
287✔
1036

1037
  rsp.dnodeList = taosArrayInit(5, sizeof(SDNodeAddr));
287✔
1038
  if (NULL == rsp.dnodeList) {
287!
1039
    mError("failed to alloc epSet while process dnode list req");
×
1040
    code = terrno;
×
1041
    goto _OVER;
×
1042
  }
1043

1044
  while (1) {
608✔
1045
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
895✔
1046
    if (pIter == NULL) break;
895✔
1047

1048
    SDNodeAddr dnodeAddr = {0};
608✔
1049
    dnodeAddr.nodeId = pObj->id;
608✔
1050
    dnodeAddr.epSet.numOfEps = 1;
608✔
1051
    tstrncpy(dnodeAddr.epSet.eps[0].fqdn, pObj->fqdn, TSDB_FQDN_LEN);
608✔
1052
    dnodeAddr.epSet.eps[0].port = pObj->port;
608✔
1053

1054
    if (taosArrayPush(rsp.dnodeList, &dnodeAddr) == NULL) {
1,216!
1055
      if (terrno != 0) code = terrno;
×
1056
      sdbRelease(pSdb, pObj);
×
1057
      sdbCancelFetch(pSdb, pIter);
×
1058
      goto _OVER;
×
1059
    }
1060

1061
    sdbRelease(pSdb, pObj);
608✔
1062
  }
1063

1064
  int32_t rspLen = tSerializeSDnodeListRsp(NULL, 0, &rsp);
287✔
1065
  void   *pRsp = rpcMallocCont(rspLen);
287✔
1066
  if (pRsp == NULL) {
287!
1067
    code = terrno;
×
1068
    goto _OVER;
×
1069
  }
1070

1071
  if ((rspLen = tSerializeSDnodeListRsp(pRsp, rspLen, &rsp)) <= 0) {
287!
1072
    code = rspLen;
×
1073
    goto _OVER;
×
1074
  }
1075

1076
  pReq->info.rspLen = rspLen;
287✔
1077
  pReq->info.rsp = pRsp;
287✔
1078
  code = 0;
287✔
1079

1080
_OVER:
287✔
1081

1082
  if (code != 0) {
287!
1083
    mError("failed to get dnode list since %s", tstrerror(code));
×
1084
  }
1085

1086
  tFreeSDnodeListRsp(&rsp);
287✔
1087

1088
  TAOS_RETURN(code);
287✔
1089
}
1090

1091
void getSlowLogScopeString(int32_t scope, char *result) {
7✔
1092
  if (scope == SLOW_LOG_TYPE_NULL) {
7!
1093
    (void)strncat(result, "NONE", 64);
×
1094
    return;
×
1095
  }
1096
  while (scope > 0) {
14✔
1097
    if (scope & SLOW_LOG_TYPE_QUERY) {
7!
1098
      (void)strncat(result, "QUERY", 64);
7✔
1099
      scope &= ~SLOW_LOG_TYPE_QUERY;
7✔
1100
    } else if (scope & SLOW_LOG_TYPE_INSERT) {
×
1101
      (void)strncat(result, "INSERT", 64);
×
1102
      scope &= ~SLOW_LOG_TYPE_INSERT;
×
1103
    } else if (scope & SLOW_LOG_TYPE_OTHERS) {
×
1104
      (void)strncat(result, "OTHERS", 64);
×
1105
      scope &= ~SLOW_LOG_TYPE_OTHERS;
×
1106
    } else {
1107
      (void)printf("invalid slow log scope:%d", scope);
×
1108
      return;
×
1109
    }
1110

1111
    if (scope > 0) {
7!
1112
      (void)strncat(result, "|", 64);
×
1113
    }
1114
  }
1115
}
1116

1117
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
501✔
1118
  SMnode         *pMnode = pReq->info.node;
501✔
1119
  int32_t         code = -1;
501✔
1120
  SDnodeObj      *pDnode = NULL;
501✔
1121
  SCreateDnodeReq createReq = {0};
501✔
1122
  int32_t         lino = 0;
501✔
1123

1124
  if ((code = grantCheck(TSDB_GRANT_DNODE)) != 0 || (code = grantCheck(TSDB_GRANT_CPU_CORES)) != 0) {
501!
1125
    goto _OVER;
×
1126
  }
1127

1128
  code = tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq);
501✔
1129
  TAOS_CHECK_GOTO(code, &lino, _OVER);
501!
1130

1131
  mInfo("dnode:%s:%d, start to create", createReq.fqdn, createReq.port);
501!
1132
  code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_DNODE);
501✔
1133
  TAOS_CHECK_GOTO(code, &lino, _OVER);
501✔
1134

1135
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
500!
1136
    code = TSDB_CODE_MND_INVALID_DNODE_EP;
×
1137
    goto _OVER;
×
1138
  }
1139
  // code = taosValidFqdn(tsEnableIpv6, createReq.fqdn);
1140
  // if (code != 0) {
1141
  //   mError("ipv6 flag %d, the local FQDN %s does not resolve to the ip address since %s", tsEnableIpv6, tsLocalFqdn,
1142
  //          tstrerror(code));
1143
  //   goto _OVER;
1144
  // }
1145

1146
  char ep[TSDB_EP_LEN];
1147
  (void)snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
500✔
1148
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
500✔
1149
  if (pDnode != NULL) {
500!
1150
    code = TSDB_CODE_MND_DNODE_ALREADY_EXIST;
×
1151
    goto _OVER;
×
1152
  }
1153

1154
  code = mndCreateDnode(pMnode, pReq, &createReq);
500✔
1155
  if (code == 0) {
500!
1156
    code = TSDB_CODE_ACTION_IN_PROGRESS;
500✔
1157
    tsGrantHBInterval = 5;
500✔
1158
  }
1159

1160
  char obj[200] = {0};
500✔
1161
  (void)tsnprintf(obj, sizeof(obj), "%s:%d", createReq.fqdn, createReq.port);
500✔
1162

1163
  auditRecord(pReq, pMnode->clusterId, "createDnode", "", obj, createReq.sql, createReq.sqlLen);
500✔
1164

1165
_OVER:
501✔
1166
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
501!
1167
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, tstrerror(code));
1!
1168
  }
1169

1170
  mndReleaseDnode(pMnode, pDnode);
501✔
1171
  tFreeSCreateDnodeReq(&createReq);
501✔
1172
  TAOS_RETURN(code);
501✔
1173
}
1174

1175
extern int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq);
1176

1177
int32_t mndProcessRestoreDnodeReq(SRpcMsg *pReq) { return mndProcessRestoreDnodeReqImpl(pReq); }
8✔
1178

1179
#ifndef TD_ENTERPRISE
1180
int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq) { return 0; }
1181
#endif
1182

1183
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, SQnodeObj *pQObj,
26✔
1184
                            SSnodeObj *pSObj, SBnodeObj *pBObj, int32_t numOfVnodes, bool force, bool unsafe) {
1185
  int32_t  code = -1;
26✔
1186
  SSdbRaw *pRaw = NULL;
26✔
1187
  STrans  *pTrans = NULL;
26✔
1188
  int32_t  lino = 0;
26✔
1189

1190
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-dnode");
26✔
1191
  if (pTrans == NULL) {
26!
1192
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1193
    if (terrno != 0) code = terrno;
×
1194
    goto _OVER;
×
1195
  }
1196
  mndTransSetGroupParallel(pTrans);
26✔
1197
  mInfo("trans:%d, used to drop dnode:%d, force:%d", pTrans->id, pDnode->id, force);
26!
1198
  TAOS_CHECK_GOTO(mndTransCheckConflict(pMnode, pTrans), &lino, _OVER);
26!
1199

1200
  pRaw = mndDnodeActionEncode(pDnode);
26✔
1201
  if (pRaw == NULL) {
26!
1202
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1203
    if (terrno != 0) code = terrno;
×
1204
    goto _OVER;
×
1205
  }
1206
  TAOS_CHECK_GOTO(mndTransAppendGroupRedolog(pTrans, pRaw, -1), &lino, _OVER);
26!
1207
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING), &lino, _OVER);
26!
1208
  pRaw = NULL;
26✔
1209

1210
  pRaw = mndDnodeActionEncode(pDnode);
26✔
1211
  if (pRaw == NULL) {
26!
1212
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1213
    if (terrno != 0) code = terrno;
×
1214
    goto _OVER;
×
1215
  }
1216
  TAOS_CHECK_GOTO(mndTransAppendCommitlog(pTrans, pRaw), &lino, _OVER);
26!
1217
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED), &lino, _OVER);
26!
1218
  pRaw = NULL;
26✔
1219

1220
  if (pSObj != NULL) {
26✔
1221
    mInfo("trans:%d, snode on dnode:%d will be dropped", pTrans->id, pDnode->id);
3!
1222
    TAOS_CHECK_GOTO(mndDropSnodeImpl(pMnode, pReq, pSObj, pTrans, force), &lino, _OVER);
3!
1223
  }
1224

1225
  if (pMObj != NULL) {
26✔
1226
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
2!
1227
    TAOS_CHECK_GOTO(mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj, force), &lino, _OVER);
2!
1228
  }
1229

1230
  if (pQObj != NULL) {
26✔
1231
    mInfo("trans:%d, qnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
1!
1232
    TAOS_CHECK_GOTO(mndSetDropQnodeInfoToTrans(pMnode, pTrans, pQObj, force), &lino, _OVER);
1!
1233
  }
1234

1235
  if (pBObj != NULL) {
26✔
1236
    mInfo("trans:%d, bnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
3!
1237
    TAOS_CHECK_GOTO(mndSetDropBnodeInfoToTrans(pMnode, pTrans, pBObj, force), &lino, _OVER);
3!
1238
  }
1239

1240
  if (numOfVnodes > 0) {
23✔
1241
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
14!
1242
    TAOS_CHECK_GOTO(mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id, force, unsafe), &lino, _OVER);
14!
1243
  }
1244

1245
  TAOS_CHECK_GOTO(mndTransPrepare(pMnode, pTrans), &lino, _OVER);
23!
1246

1247
  code = 0;
23✔
1248

1249
_OVER:
26✔
1250
  if (code != 0) mError("dnode:%d, failed to drop dnode at line:%d since %s", pDnode->id, lino, tstrerror(code));
26!
1251
  mndTransDrop(pTrans);
26✔
1252
  sdbFreeRaw(pRaw);
26✔
1253
  TAOS_RETURN(code);
26✔
1254
}
1255

1256
static bool mndIsEmptyDnode(SMnode *pMnode, int32_t dnodeId) {
×
1257
  bool       isEmpty = false;
×
1258
  SMnodeObj *pMObj = NULL;
×
1259
  SQnodeObj *pQObj = NULL;
×
1260
  SSnodeObj *pSObj = NULL;
×
1261

1262
  pQObj = mndAcquireQnode(pMnode, dnodeId);
×
1263
  if (pQObj) goto _OVER;
×
1264

1265
  pSObj = mndAcquireSnode(pMnode, dnodeId);
×
1266
  if (pSObj) goto _OVER;
×
1267

1268
  pMObj = mndAcquireMnode(pMnode, dnodeId);
×
1269
  if (pMObj) goto _OVER;
×
1270

1271
  int32_t numOfVnodes = mndGetVnodesNum(pMnode, dnodeId);
×
1272
  if (numOfVnodes > 0) goto _OVER;
×
1273

1274
  isEmpty = true;
×
1275
_OVER:
×
1276
  mndReleaseMnode(pMnode, pMObj);
×
1277
  mndReleaseQnode(pMnode, pQObj);
×
1278
  mndReleaseSnode(pMnode, pSObj);
×
1279
  return isEmpty;
×
1280
}
1281

1282
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
32✔
1283
  SMnode       *pMnode = pReq->info.node;
32✔
1284
  int32_t       code = -1;
32✔
1285
  SDnodeObj    *pDnode = NULL;
32✔
1286
  SMnodeObj    *pMObj = NULL;
32✔
1287
  SQnodeObj    *pQObj = NULL;
32✔
1288
  SSnodeObj    *pSObj = NULL;
32✔
1289
  SBnodeObj    *pBObj = NULL;
32✔
1290
  SDropDnodeReq dropReq = {0};
32✔
1291

1292
  TAOS_CHECK_GOTO(tDeserializeSDropDnodeReq(pReq->pCont, pReq->contLen, &dropReq), NULL, _OVER);
32!
1293

1294
  mInfo("dnode:%d, start to drop, ep:%s:%d, force:%s, unsafe:%s", dropReq.dnodeId, dropReq.fqdn, dropReq.port,
32!
1295
        dropReq.force ? "true" : "false", dropReq.unsafe ? "true" : "false");
1296
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE), NULL, _OVER);
32✔
1297

1298
  bool force = dropReq.force;
31✔
1299
  if (dropReq.unsafe) {
31!
1300
    force = true;
×
1301
  }
1302

1303
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
31✔
1304
  if (pDnode == NULL) {
31!
1305
    int32_t err = terrno;
×
1306
    char    ep[TSDB_EP_LEN + 1] = {0};
×
1307
    (void)snprintf(ep, sizeof(ep), dropReq.fqdn, dropReq.port);
×
1308
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
×
1309
    if (pDnode == NULL) {
×
1310
      code = err;
×
1311
      goto _OVER;
×
1312
    }
1313
  }
1314

1315
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
31✔
1316
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
31✔
1317
  pBObj = mndAcquireBnode(pMnode, dropReq.dnodeId);
31✔
1318
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
31✔
1319
  if (pMObj != NULL) {
31✔
1320
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
7✔
1321
      code = TSDB_CODE_MND_TOO_FEW_MNODES;
3✔
1322
      goto _OVER;
3✔
1323
    }
1324
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
4✔
1325
      code = TSDB_CODE_MND_CANT_DROP_LEADER;
2✔
1326
      goto _OVER;
2✔
1327
    }
1328
  }
1329

1330
#ifdef USE_MOUNT
1331
  if (mndHasMountOnDnode(pMnode, dropReq.dnodeId) && !force) {
26!
1332
    code = TSDB_CODE_MND_MOUNT_NOT_EMPTY;
×
1333
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, tstrerror(code));
×
1334
    goto _OVER;
×
1335
  }
1336
#endif
1337

1338
  int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);
26✔
1339
  bool    isonline = mndIsDnodeOnline(pDnode, taosGetTimestampMs());
26✔
1340

1341
  if (isonline && force) {
26!
1342
    code = TSDB_CODE_DNODE_ONLY_USE_WHEN_OFFLINE;
×
1343
    mError("dnode:%d, failed to drop since %s, vnodes:%d mnode:%d qnode:%d snode:%d bnode:%d", pDnode->id,
×
1344
           tstrerror(code), numOfVnodes, pMObj != NULL, pQObj != NULL, pSObj != NULL, pBObj != NULL);
1345
    goto _OVER;
×
1346
  }
1347

1348
  mError("vnode num:%d", numOfVnodes);
26!
1349

1350
  bool    vnodeOffline = false;
26✔
1351
  void   *pIter = NULL;
26✔
1352
  int32_t vgId = -1;
26✔
1353
  while (1) {
39✔
1354
    SVgObj *pVgroup = NULL;
65✔
1355
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
65✔
1356
    if (pIter == NULL) break;
65✔
1357

1358
    for (int32_t i = 0; i < pVgroup->replica; ++i) {
120✔
1359
      mError("vnode dnodeId:%d state:%d", pVgroup->vnodeGid[i].dnodeId, pVgroup->vnodeGid[i].syncState);
81!
1360
      if (pVgroup->vnodeGid[i].dnodeId == pDnode->id) {
81✔
1361
        if (pVgroup->vnodeGid[i].syncState == TAOS_SYNC_STATE_OFFLINE) {
29!
1362
          vgId = pVgroup->vgId;
×
1363
          vnodeOffline = true;
×
1364
          break;
×
1365
        }
1366
      }
1367
    }
1368

1369
    sdbRelease(pMnode->pSdb, pVgroup);
39✔
1370

1371
    if (vnodeOffline) {
39!
1372
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1373
      break;
×
1374
    }
1375
  }
1376

1377
  if (vnodeOffline && !force) {
26!
1378
    code = TSDB_CODE_VND_VNODE_OFFLINE;
×
1379
    mError("dnode:%d, failed to drop since vgId:%d is offline, vnodes:%d mnode:%d qnode:%d snode:%d", pDnode->id, vgId,
×
1380
           numOfVnodes, pMObj != NULL, pQObj != NULL, pSObj != NULL);
1381
    goto _OVER;
×
1382
  }
1383

1384
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, pBObj, numOfVnodes, force, dropReq.unsafe);
26✔
1385
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
26✔
1386

1387
  char obj1[30] = {0};
26✔
1388
  (void)tsnprintf(obj1, sizeof(obj1), "%d", dropReq.dnodeId);
26✔
1389

1390
  auditRecord(pReq, pMnode->clusterId, "dropDnode", "", obj1, dropReq.sql, dropReq.sqlLen);
26✔
1391

1392
_OVER:
32✔
1393
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
32!
1394
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, tstrerror(code));
9!
1395
  }
1396

1397
  mndReleaseDnode(pMnode, pDnode);
32✔
1398
  mndReleaseMnode(pMnode, pMObj);
32✔
1399
  mndReleaseQnode(pMnode, pQObj);
32✔
1400
  mndReleaseBnode(pMnode, pBObj);
32✔
1401
  mndReleaseSnode(pMnode, pSObj);
32✔
1402
  tFreeSDropDnodeReq(&dropReq);
32✔
1403
  TAOS_RETURN(code);
32✔
1404
}
1405

1406
static int32_t mndProcessCreateEncryptKeyReqImpl(SRpcMsg *pReq, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
3✔
1407
  int32_t code = 0;
3✔
1408
  SMnode *pMnode = pReq->info.node;
3✔
1409
  SSdb   *pSdb = pMnode->pSdb;
3✔
1410
  void   *pIter = NULL;
3✔
1411
  int8_t  encrypting = 0;
3✔
1412

1413
  const STraceId *trace = &pReq->info.traceId;
3✔
1414

1415
  int32_t klen = strlen(pDcfgReq->value);
3✔
1416
  if (klen > ENCRYPT_KEY_LEN || klen < ENCRYPT_KEY_LEN_MIN) {
3!
1417
    code = TSDB_CODE_DNODE_INVALID_ENCRYPT_KLEN;
×
1418
    mGError("msg:%p, failed to create encrypt_key since invalid key length:%d, valid range:[%d, %d]", pReq, klen,
×
1419
            ENCRYPT_KEY_LEN_MIN, ENCRYPT_KEY_LEN);
1420
    goto _exit;
×
1421
  }
1422

1423
  if (0 != (encrypting = atomic_val_compare_exchange_8(&pMnode->encryptMgmt.encrypting, 0, 1))) {
3!
1424
    code = TSDB_CODE_QRY_DUPLICATED_OPERATION;
×
1425
    mGWarn("msg:%p, failed to create encrypt key since %s, encrypting:%" PRIi8, pReq, tstrerror(code), encrypting);
×
1426
    goto _exit;
×
1427
  }
1428

1429
  if (tsEncryptionKeyStat == ENCRYPT_KEY_STAT_SET || tsEncryptionKeyStat == ENCRYPT_KEY_STAT_LOADED) {
3!
1430
    atomic_store_8(&pMnode->encryptMgmt.encrypting, 0);
×
1431
    code = TSDB_CODE_QRY_DUPLICATED_OPERATION;
×
1432
    mGWarn("msg:%p, failed to create encrypt key since %s, stat:%" PRIi8 ", checksum:%u", pReq, tstrerror(code),
×
1433
           tsEncryptionKeyStat, tsEncryptionKeyChksum);
1434
    goto _exit;
×
1435
  }
1436

1437
  atomic_store_16(&pMnode->encryptMgmt.nEncrypt, 0);
3✔
1438
  atomic_store_16(&pMnode->encryptMgmt.nSuccess, 0);
3✔
1439
  atomic_store_16(&pMnode->encryptMgmt.nFailed, 0);
3✔
1440

1441
  while (1) {
5✔
1442
    SDnodeObj *pDnode = NULL;
8✔
1443
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
8✔
1444
    if (pIter == NULL) break;
8✔
1445
    if (pDnode->offlineReason != DND_REASON_ONLINE) {
5!
1446
      mGWarn("msg:%p, don't send create encrypt_key req since dnode:%d in offline state:%s", pReq, pDnode->id,
×
1447
             offlineReason[pDnode->offlineReason]);
1448
      sdbRelease(pSdb, pDnode);
×
1449
      continue;
×
1450
    }
1451

1452
    if (dnodeId == -1 || pDnode->id == dnodeId || dnodeId == 0) {
5!
1453
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
5✔
1454
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
5✔
1455
      void   *pBuf = rpcMallocCont(bufLen);
5✔
1456

1457
      if (pBuf != NULL) {
5!
1458
        if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
5!
1459
          code = bufLen;
×
1460
          sdbRelease(pSdb, pDnode);
×
1461
          goto _exit;
×
1462
        }
1463
        SRpcMsg rpcMsg = {.msgType = TDMT_DND_CREATE_ENCRYPT_KEY, .pCont = pBuf, .contLen = bufLen};
5✔
1464
        if (0 == tmsgSendReq(&epSet, &rpcMsg)) {
5!
1465
          (void)atomic_add_fetch_16(&pMnode->encryptMgmt.nEncrypt, 1);
5✔
1466
        }
1467
      }
1468
    }
1469

1470
    sdbRelease(pSdb, pDnode);
5✔
1471
  }
1472

1473
  if (atomic_load_16(&pMnode->encryptMgmt.nEncrypt) <= 0) {
3!
1474
    atomic_store_8(&pMnode->encryptMgmt.encrypting, 0);
×
1475
  }
1476

1477
_exit:
3✔
1478
  if (code != 0) {
3!
1479
    if (terrno == 0) terrno = code;
×
1480
  }
1481
  return code;
3✔
1482
}
1483

1484
static int32_t mndProcessCreateEncryptKeyReq(SRpcMsg *pReq) {
3✔
1485
  int32_t code = 0;
3✔
1486

1487
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
1488
  SMnode       *pMnode = pReq->info.node;
3✔
1489
  SMCfgDnodeReq cfgReq = {0};
3✔
1490
  TAOS_CHECK_RETURN(tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq));
3!
1491

1492
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE)) != 0) {
3!
1493
    tFreeSMCfgDnodeReq(&cfgReq);
×
1494
    TAOS_RETURN(code);
×
1495
  }
1496
  const STraceId *trace = &pReq->info.traceId;
3✔
1497
  SDCfgDnodeReq   dcfgReq = {0};
3✔
1498
  if (strncasecmp(cfgReq.config, "encrypt_key", 12) == 0) {
3!
1499
    tstrncpy(dcfgReq.config, cfgReq.config, sizeof(dcfgReq.config));
3✔
1500
    tstrncpy(dcfgReq.value, cfgReq.value, sizeof(dcfgReq.value));
3✔
1501
    tFreeSMCfgDnodeReq(&cfgReq);
3✔
1502
    return mndProcessCreateEncryptKeyReqImpl(pReq, cfgReq.dnodeId, &dcfgReq);
3✔
1503
  } else {
1504
    code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
1505
    tFreeSMCfgDnodeReq(&cfgReq);
×
1506
    TAOS_RETURN(code);
×
1507
  }
1508

1509
#else
1510
  TAOS_RETURN(code);
1511
#endif
1512
}
1513

1514
static int32_t mndProcessCreateEncryptKeyRsp(SRpcMsg *pRsp) {
5✔
1515
  SMnode *pMnode = pRsp->info.node;
5✔
1516
  int16_t nSuccess = 0;
5✔
1517
  int16_t nFailed = 0;
5✔
1518

1519
  if (0 == pRsp->code) {
5!
1520
    nSuccess = atomic_add_fetch_16(&pMnode->encryptMgmt.nSuccess, 1);
5✔
1521
  } else {
1522
    nFailed = atomic_add_fetch_16(&pMnode->encryptMgmt.nFailed, 1);
×
1523
  }
1524

1525
  int16_t nReq = atomic_load_16(&pMnode->encryptMgmt.nEncrypt);
5✔
1526
  bool    finished = nSuccess + nFailed >= nReq;
5✔
1527

1528
  if (finished) {
5✔
1529
    atomic_store_8(&pMnode->encryptMgmt.encrypting, 0);
3✔
1530
  }
1531

1532
  const STraceId *trace = &pRsp->info.traceId;
5✔
1533
  mGInfo("msg:%p, create encrypt key rsp, nReq:%" PRIi16 ", nSucess:%" PRIi16 ", nFailed:%" PRIi16 ", %s", pRsp, nReq,
5!
1534
         nSuccess, nFailed, finished ? "encrypt done" : "in encrypting");
1535

1536
  return 0;
5✔
1537
}
1538

1539
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7✔
1540
  SMnode *pMnode = pReq->info.node;
7✔
1541
  int32_t totalRows = 0;
7✔
1542
  int32_t numOfRows = 0;
7✔
1543
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
7✔
1544
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONFIG_VALUE_LEN + 1] = {0};
7✔
1545
  char   *pWrite = NULL;
7✔
1546
  int32_t cols = 0;
7✔
1547
  int32_t code = 0;
7✔
1548
  int32_t lino = 0;
7✔
1549

1550
  cfgOpts[totalRows] = "statusIntervalMs";
7✔
1551
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsStatusIntervalMs);
7✔
1552
  totalRows++;
7✔
1553

1554
  cfgOpts[totalRows] = "timezone";
7✔
1555
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
7✔
1556
  totalRows++;
7✔
1557

1558
  cfgOpts[totalRows] = "locale";
7✔
1559
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
7✔
1560
  totalRows++;
7✔
1561

1562
  cfgOpts[totalRows] = "charset";
7✔
1563
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
7✔
1564
  totalRows++;
7✔
1565

1566
  cfgOpts[totalRows] = "monitor";
7✔
1567
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsEnableMonitor);
7✔
1568
  totalRows++;
7✔
1569

1570
  cfgOpts[totalRows] = "monitorInterval";
7✔
1571
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsMonitorInterval);
7✔
1572
  totalRows++;
7✔
1573

1574
  cfgOpts[totalRows] = "slowLogThreshold";
7✔
1575
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsSlowLogThreshold);
7✔
1576
  totalRows++;
7✔
1577

1578
  cfgOpts[totalRows] = "slowLogMaxLen";
7✔
1579
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsSlowLogMaxLen);
7✔
1580
  totalRows++;
7✔
1581

1582
  char scopeStr[64] = {0};
7✔
1583
  getSlowLogScopeString(tsSlowLogScope, scopeStr);
7✔
1584
  cfgOpts[totalRows] = "slowLogScope";
7✔
1585
  (void)snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", scopeStr);
7✔
1586
  totalRows++;
7✔
1587

1588
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
7✔
1589
  char bufVal[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
7✔
1590

1591
  for (int32_t i = 0; i < totalRows; i++) {
70✔
1592
    cols = 0;
63✔
1593

1594
    STR_WITH_MAXSIZE_TO_VARSTR(buf, cfgOpts[i], TSDB_CONFIG_OPTION_LEN);
63✔
1595
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
63✔
1596
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)buf, false), &lino, _OVER);
63!
1597

1598
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONFIG_VALUE_LEN);
63✔
1599
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
63✔
1600
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)bufVal, false), &lino, _OVER);
63!
1601

1602
    numOfRows++;
63✔
1603
  }
1604

1605
_OVER:
7✔
1606
  if (code != 0) mError("failed to retrieve configs at line:%d since %s", lino, tstrerror(code));
7!
1607
  pShow->numOfRows += numOfRows;
7✔
1608
  return numOfRows;
7✔
1609
}
1610

1611
static void mndCancelGetNextConfig(SMnode *pMnode, void *pIter) {}
×
1612

1613
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1,939✔
1614
  SMnode    *pMnode = pReq->info.node;
1,939✔
1615
  SSdb      *pSdb = pMnode->pSdb;
1,939✔
1616
  int32_t    numOfRows = 0;
1,939✔
1617
  int32_t    cols = 0;
1,939✔
1618
  ESdbStatus objStatus = 0;
1,939✔
1619
  SDnodeObj *pDnode = NULL;
1,939✔
1620
  int64_t    curMs = taosGetTimestampMs();
1,939✔
1621
  char       buf[TSDB_EP_LEN + VARSTR_HEADER_SIZE];
1622
  int32_t    code = 0;
1,939✔
1623
  int32_t    lino = 0;
1,939✔
1624

1625
  while (numOfRows < rows) {
6,997!
1626
    pShow->pIter = sdbFetchAll(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode, &objStatus, true);
6,997✔
1627
    if (pShow->pIter == NULL) break;
6,997✔
1628
    bool online = mndIsDnodeOnline(pDnode, curMs);
5,058✔
1629

1630
    cols = 0;
5,058✔
1631

1632
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1633
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->id, false), pDnode, &lino, _OVER);
5,058!
1634

1635
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
5,058✔
1636

1637
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1638
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, buf, false), pDnode, &lino, _OVER);
5,058!
1639

1640
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1641
    int16_t id = mndGetVnodesNum(pMnode, pDnode->id);
5,058✔
1642
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&id, false), pDnode, &lino, _OVER);
5,058!
1643

1644
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1645
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->numOfSupportVnodes, false), pDnode,
5,058!
1646
                        &lino, _OVER);
1647

1648
    const char *status = "ready";
5,058✔
1649
    if (objStatus == SDB_STATUS_CREATING) status = "creating";
5,058!
1650
    if (objStatus == SDB_STATUS_DROPPING) status = "dropping";
5,058!
1651
    if (!online) {
5,058✔
1652
      if (objStatus == SDB_STATUS_CREATING)
457!
1653
        status = "creating*";
×
1654
      else if (objStatus == SDB_STATUS_DROPPING)
457!
1655
        status = "dropping*";
×
1656
      else
1657
        status = "offline";
457✔
1658
    }
1659

1660
    STR_TO_VARSTR(buf, status);
5,058✔
1661
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1662
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, buf, false), pDnode, &lino, _OVER);
5,058!
1663

1664
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1665
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->createdTime, false), pDnode, &lino,
5,058!
1666
                        _OVER);
1667

1668
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1669
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->rebootTime, false), pDnode, &lino,
5,058!
1670
                        _OVER);
1671

1672
    char *b = taosMemoryCalloc(VARSTR_HEADER_SIZE + strlen(offlineReason[pDnode->offlineReason]) + 1, 1);
5,058!
1673
    STR_TO_VARSTR(b, online ? "" : offlineReason[pDnode->offlineReason]);
5,058✔
1674

1675
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1676
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, b, false), pDnode, &lino, _OVER);
5,058!
1677
    taosMemoryFreeClear(b);
5,058!
1678

1679
#ifdef TD_ENTERPRISE
1680
    STR_TO_VARSTR(buf, pDnode->machineId);
5,058✔
1681
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5,058✔
1682
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, buf, false), pDnode, &lino, _OVER);
5,058!
1683
#endif
1684

1685
    numOfRows++;
5,058✔
1686
    sdbRelease(pSdb, pDnode);
5,058✔
1687
  }
1688

1689
_OVER:
×
1690
  if (code != 0) mError("failed to retrieve dnodes at line:%d since %s", lino, tstrerror(code));
1,939!
1691

1692
  pShow->numOfRows += numOfRows;
1,939✔
1693
  return numOfRows;
1,939✔
1694
}
1695

1696
static void mndCancelGetNextDnode(SMnode *pMnode, void *pIter) {
×
1697
  SSdb *pSdb = pMnode->pSdb;
×
1698
  sdbCancelFetchByType(pSdb, pIter, SDB_DNODE);
×
1699
}
×
1700

1701
SArray *mndGetAllDnodeFqdns(SMnode *pMnode) {
4,061✔
1702
  SDnodeObj *pObj = NULL;
4,061✔
1703
  void      *pIter = NULL;
4,061✔
1704
  SSdb      *pSdb = pMnode->pSdb;
4,061✔
1705
  SArray    *fqdns = taosArrayInit(4, sizeof(void *));
4,061✔
1706
  while (1) {
4,633✔
1707
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
8,694✔
1708
    if (pIter == NULL) break;
8,694✔
1709

1710
    char *fqdn = taosStrdup(pObj->fqdn);
4,633!
1711
    if (taosArrayPush(fqdns, &fqdn) == NULL) {
4,633!
1712
      mError("failed to fqdn into array, but continue at this time");
×
1713
    }
1714
    sdbRelease(pSdb, pObj);
4,633✔
1715
  }
1716
  return fqdns;
4,061✔
1717
}
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