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

#define _DEFAULT_SOURCE
17
#include "os.h"
S
slguan 已提交
18
#include "tgrant.h"
S
Shengliang Guan 已提交
19
#include "tbn.h"
S
slguan 已提交
20 21
#include "tglobal.h"
#include "tconfig.h"
S
slguan 已提交
22 23
#include "tutil.h"
#include "tsocket.h"
S
Shengliang Guan 已提交
24
#include "tbn.h"
S
slguan 已提交
25
#include "tsync.h"
26
#include "tdataformat.h"
27
#include "mnode.h"
S
slguan 已提交
28
#include "dnode.h"
S
Shengliang Guan 已提交
29 30 31
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeDnode.h"
32
#include "mnodeDb.h"
S
Shengliang Guan 已提交
33 34
#include "mnodeMnode.h"
#include "mnodeSdb.h"
35
#include "mnodeShow.h"
S
Shengliang Guan 已提交
36 37
#include "mnodeUser.h"
#include "mnodeVgroup.h"
38 39
#include "mnodeWrite.h"
#include "mnodePeer.h"
S
Shengliang Guan 已提交
40
#include "mnodeCluster.h"
S
slguan 已提交
41

42 43
int64_t        tsAccessSquence = 0;
int64_t        tsDnodeRid = -1;
S
TD-1762  
Shengliang Guan 已提交
44
static void *  tsDnodeSdb = NULL;
45
static int32_t tsDnodeUpdateSize = 0;
S
slguan 已提交
46
extern void *  tsMnodeSdb;
S
slguan 已提交
47 48
extern void *  tsVgroupSdb;

S
TD-1762  
Shengliang Guan 已提交
49 50 51 52
static SDnodeEps*tsDnodeEps;
static int32_t   tsDnodeEpsSize;
static pthread_mutex_t tsDnodeEpsMutex;

53
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg);
54 55 56 57
static int32_t mnodeProcessCreateDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessDropDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) ;
H
Haojun Liao 已提交
58
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg);
59 60 61 62 63 64 65 66
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
TD-1762  
Shengliang Guan 已提交
67
static void    mnodeUpdateDnodeEps();
68

S
TD-1473  
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static char* offlineReason[] = {
  "",
  "status msg timeout",
  "status not received",
  "status reset by mnode",
  "version not match",
  "dnodeId not match",
  "clusterId not match",
  "numOfMnodes not match",
  "balance not match",
  "mnEqualVn not match",
  "offThreshold not match",
  "interval not match",
  "maxTabPerVn not match",
  "maxVgPerDb not match",
  "arbitrator not match",
  "timezone not match",
  "locale not match",
  "charset not match",
  "unknown",
};

S
TD-2046  
Shengliang Guan 已提交
91 92
static int32_t mnodeDnodeActionDestroy(SSdbRow *pRow) {
  tfree(pRow->pObj);
S
slguan 已提交
93 94
  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
95

S
TD-2046  
Shengliang Guan 已提交
96 97
static int32_t mnodeDnodeActionInsert(SSdbRow *pRow) {
  SDnodeObj *pDnode = pRow->pObj;
S
slguan 已提交
98 99
  if (pDnode->status != TAOS_DN_STATUS_DROPPING) {
    pDnode->status = TAOS_DN_STATUS_OFFLINE;
S
Shengliang Guan 已提交
100
    pDnode->lastAccess = tsAccessSquence;
S
TD-1473  
Shengliang Guan 已提交
101
    pDnode->offlineReason = TAOS_DN_OFF_STATUS_NOT_RECEIVED;
S
slguan 已提交
102
  }
S
slguan 已提交
103

104 105
  pDnode->customScore = 0;

S
TD-1762  
Shengliang Guan 已提交
106 107 108
  dnodeUpdateEp(pDnode->dnodeId, pDnode->dnodeEp, pDnode->dnodeFqdn, &pDnode->dnodePort);
  mnodeUpdateDnodeEps();

Z
zyyang 已提交
109
  mInfo("dnode:%d, fqdn:%s ep:%s port:%d is created", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodeEp, pDnode->dnodePort);
S
slguan 已提交
110
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
111 112
}

S
TD-2046  
Shengliang Guan 已提交
113 114
static int32_t mnodeDnodeActionDelete(SSdbRow *pRow) {
  SDnodeObj *pDnode = pRow->pObj;
115
 
116
  mnodeDropMnodeLocal(pDnode->dnodeId);
S
TD-2270  
Shengliang Guan 已提交
117
  bnNotify();
S
TD-1762  
Shengliang Guan 已提交
118
  mnodeUpdateDnodeEps();
S
slguan 已提交
119

120
  mDebug("dnode:%d, all vgroups is dropped from sdb", pDnode->dnodeId);
S
slguan 已提交
121 122 123
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
124 125
static int32_t mnodeDnodeActionUpdate(SSdbRow *pRow) {
  SDnodeObj *pNew = pRow->pObj;
126 127
  SDnodeObj *pDnode = mnodeGetDnode(pNew->dnodeId);
  if (pDnode != NULL && pNew != pDnode) {
S
TD-2046  
Shengliang Guan 已提交
128
    memcpy(pDnode, pNew, pRow->rowSize);
129
    free(pNew);
S
slguan 已提交
130
  }
131
  mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
132

S
TD-1762  
Shengliang Guan 已提交
133
  mnodeUpdateDnodeEps();
S
slguan 已提交
134 135 136
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
137 138 139 140
static int32_t mnodeDnodeActionEncode(SSdbRow *pRow) {
  SDnodeObj *pDnode = pRow->pObj;
  memcpy(pRow->rowData, pDnode, tsDnodeUpdateSize);
  pRow->rowSize = tsDnodeUpdateSize;
S
slguan 已提交
141
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
142 143
}

S
TD-2046  
Shengliang Guan 已提交
144
static int32_t mnodeDnodeActionDecode(SSdbRow *pRow) {
S
slguan 已提交
145
  SDnodeObj *pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj));
146
  if (pDnode == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
147

S
TD-2046  
Shengliang Guan 已提交
148 149
  memcpy(pDnode, pRow->rowData, tsDnodeUpdateSize);
  pRow->pObj = pDnode;
S
slguan 已提交
150 151
  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
152

153
static int32_t mnodeDnodeActionRestored() {
S
TD-1207  
Shengliang Guan 已提交
154
  int64_t numOfRows = sdbGetNumOfRows(tsDnodeSdb);
S
slguan 已提交
155
  if (numOfRows <= 0 && dnodeIsFirstDeploy()) {
156
    mInfo("dnode first deploy, create dnode:%s", tsLocalEp);
157
    mnodeCreateDnode(tsLocalEp, NULL);
158
    SDnodeObj *pDnode = mnodeGetDnodeByEp(tsLocalEp);
S
Shengliang Guan 已提交
159
    if (pDnode != NULL) {
S
TD-1671  
Shengliang Guan 已提交
160
      mnodeCreateMnode(pDnode->dnodeId, pDnode->dnodeEp, false);
S
Shengliang Guan 已提交
161 162
      mnodeDecDnodeRef(pDnode);
    }
S
slguan 已提交
163 164
  }

S
TD-1762  
Shengliang Guan 已提交
165
  mnodeUpdateDnodeEps();
S
slguan 已提交
166
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
167 168
}

169
int32_t mnodeInitDnodes() {
S
slguan 已提交
170
  SDnodeObj tObj;
S
TD-1207  
Shengliang Guan 已提交
171
  tsDnodeUpdateSize = (int32_t)((int8_t *)tObj.updateEnd - (int8_t *)&tObj);
S
TD-1762  
Shengliang Guan 已提交
172
  pthread_mutex_init(&tsDnodeEpsMutex, NULL);
S
slguan 已提交
173

S
TD-2046  
Shengliang Guan 已提交
174 175 176
  SSdbTableDesc desc = {
    .id           = SDB_TABLE_DNODE,
    .name         = "dnodes",
S
Shengliang Guan 已提交
177
    .hashSessions = TSDB_DEFAULT_DNODES_HASH_SIZE,
S
slguan 已提交
178
    .maxRowSize   = tsDnodeUpdateSize,
S
TD-1207  
Shengliang Guan 已提交
179
    .refCountPos  = (int32_t)((int8_t *)(&tObj.refCount) - (int8_t *)&tObj),
S
slguan 已提交
180
    .keyType      = SDB_KEY_AUTO,
S
TD-2046  
Shengliang Guan 已提交
181 182 183 184 185 186
    .fpInsert     = mnodeDnodeActionInsert,
    .fpDelete     = mnodeDnodeActionDelete,
    .fpUpdate     = mnodeDnodeActionUpdate,
    .fpEncode     = mnodeDnodeActionEncode,
    .fpDecode     = mnodeDnodeActionDecode,
    .fpDestroy    = mnodeDnodeActionDestroy,
S
TD-2046  
Shengliang Guan 已提交
187
    .fpRestored   = mnodeDnodeActionRestored
S
slguan 已提交
188 189
  };

S
TD-2264  
Shengliang Guan 已提交
190 191
  tsDnodeRid = sdbOpenTable(&desc);
  tsDnodeSdb = sdbGetTableByRid(tsDnodeRid);
S
slguan 已提交
192 193 194 195 196
  if (tsDnodeSdb == NULL) {
    mError("failed to init dnodes data");
    return -1;
  }

197 198 199 200 201 202 203
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CREATE_DNODE, mnodeProcessCreateDnodeMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_DROP_DNODE, mnodeProcessDropDnodeMsg); 
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CONFIG_DNODE, mnodeProcessCfgDnodeMsg);
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP, mnodeProcessCfgDnodeMsgRsp);
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_STATUS, mnodeProcessDnodeStatusMsg);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_MODULE, mnodeGetModuleMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_MODULE, mnodeRetrieveModules);
H
Haojun Liao 已提交
204 205
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_VARIABLES, mnodeGetConfigMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_VARIABLES, mnodeRetrieveConfigs);
206 207 208 209
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_VNODES, mnodeGetVnodeMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_VNODES, mnodeRetrieveVnodes);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_DNODE, mnodeGetDnodeMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_DNODE, mnodeRetrieveDnodes);
S
Shengliang Guan 已提交
210
  mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_DNODE, mnodeCancelGetNextDnode);
S
slguan 已提交
211
 
212
  mDebug("table:dnodes table is created");
S
slguan 已提交
213 214 215
  return 0;
}

S
Shengliang Guan 已提交
216
void mnodeCleanupDnodes() {
S
TD-2264  
Shengliang Guan 已提交
217
  sdbCloseTable(tsDnodeRid);
S
TD-1762  
Shengliang Guan 已提交
218 219 220
  pthread_mutex_destroy(&tsDnodeEpsMutex);
  free(tsDnodeEps);
  tsDnodeEps = NULL;
221
  tsDnodeSdb = NULL;
S
slguan 已提交
222 223
}

224
void *mnodeGetNextDnode(void *pIter, SDnodeObj **pDnode) { 
S
Shengliang Guan 已提交
225
  return sdbFetchRow(tsDnodeSdb, pIter, (void **)pDnode); 
S
slguan 已提交
226 227
}

S
Shengliang Guan 已提交
228 229 230 231
void mnodeCancelGetNextDnode(void *pIter) {
  sdbFreeIter(tsDnodeSdb, pIter);
}

232
int32_t mnodeGetDnodesNum() {
S
TD-1207  
Shengliang Guan 已提交
233
  return (int32_t)sdbGetNumOfRows(tsDnodeSdb);
S
slguan 已提交
234 235
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
int32_t mnodeGetOnlinDnodesCpuCoreNum() {
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    cpuCores = 0;

  while (1) {
    pIter = mnodeGetNextDnode(pIter, &pDnode);
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) {
      cpuCores += pDnode->numOfCores;
    }
    mnodeDecDnodeRef(pDnode);
  }

  if (cpuCores < 2) cpuCores = 2;
  return cpuCores;
}

254
int32_t mnodeGetOnlineDnodesNum() {
255 256 257 258 259
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    onlineDnodes = 0;

  while (1) {
260
    pIter = mnodeGetNextDnode(pIter, &pDnode);
261 262
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) onlineDnodes++;
263
    mnodeDecDnodeRef(pDnode);
264 265 266 267 268
  }

  return onlineDnodes;
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
void mnodeGetOnlineAndTotalDnodesNum(int32_t *onlineNum, int32_t *totalNum) {
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    onlineDnodes = 0, totalDnodes = 0;

  while (1) {
    pIter = mnodeGetNextDnode(pIter, &pDnode);
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) ++onlineDnodes;
    ++totalDnodes;
    mnodeDecDnodeRef(pDnode);
  }

  if (onlineNum) {
    *onlineNum = onlineDnodes;
  }

  if (totalNum) {
    *totalNum = totalDnodes;
  }
}

291
void *mnodeGetDnode(int32_t dnodeId) {
S
slguan 已提交
292 293 294
  return sdbGetRow(tsDnodeSdb, &dnodeId);
}

295
void *mnodeGetDnodeByEp(char *ep) {
S
slguan 已提交
296
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
297
  void *     pIter = NULL;
S
slguan 已提交
298 299

  while (1) {
300
    pIter = mnodeGetNextDnode(pIter, &pDnode);
S
slguan 已提交
301
    if (pDnode == NULL) break;
J
jtao1735 已提交
302
    if (strcmp(ep, pDnode->dnodeEp) == 0) {
S
Shengliang Guan 已提交
303
      mnodeCancelGetNextDnode(pIter);
S
slguan 已提交
304 305
      return pDnode;
    }
306
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
307 308
  }

S
Shengliang Guan 已提交
309

S
slguan 已提交
310 311 312
  return NULL;
}

313
void mnodeIncDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
314 315 316
  sdbIncRef(tsDnodeSdb, pDnode);
}

317
void mnodeDecDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
318
  sdbDecRef(tsDnodeSdb, pDnode);
S
slguan 已提交
319 320
}

321
void mnodeUpdateDnode(SDnodeObj *pDnode) {
S
TD-2046  
Shengliang Guan 已提交
322
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
323 324
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
325
    .pObj   = pDnode
S
slguan 已提交
326 327
  };

S
TD-2046  
Shengliang Guan 已提交
328
  int32_t code = sdbUpdateRow(&row);
S
Shengliang Guan 已提交
329
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
TD-2415  
Shengliang Guan 已提交
330
    mError("dnode:%d, failed update", pDnode->dnodeId);
331
  }
S
slguan 已提交
332 333
}

334
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
335 336 337 338 339
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
    mError("failed to cfg dnode, no rights");
    return TSDB_CODE_MND_NO_RIGHTS;
  }
  
S
TD-1732  
Shengliang Guan 已提交
340
  SCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
341
  if (pCmCfgDnode->ep[0] == 0) {
342
    tstrncpy(pCmCfgDnode->ep, tsLocalEp, TSDB_EP_LEN);
343
  }
344

345 346 347
  SDnodeObj *pDnode = mnodeGetDnodeByEp(pCmCfgDnode->ep);
  if (pDnode == NULL) {
    int32_t dnodeId = strtol(pCmCfgDnode->ep, NULL, 10);
348
    if (dnodeId <= 0 || dnodeId > 65536) {
349
      mError("failed to cfg dnode, invalid dnodeEp:%s", pCmCfgDnode->ep);
350 351
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
352

353
    pDnode = mnodeGetDnode(dnodeId);
354 355 356 357
    if (pDnode == NULL) {
      mError("failed to cfg dnode, invalid dnodeId:%d", dnodeId);
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
358 359
  }

360
  SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
361

362
  if (strncasecmp(pCmCfgDnode->config, "balance", 7) == 0) {
S
Shengliang Guan 已提交
363 364 365 366 367 368 369 370
    int32_t vnodeId = 0;
    int32_t dnodeId = 0;
    bool parseOk = taosCheckBalanceCfgOptions(pCmCfgDnode->config + 8, &vnodeId, &dnodeId);
    if (!parseOk) {
      mnodeDecDnodeRef(pDnode);
      return TSDB_CODE_MND_INVALID_DNODE_CFG_OPTION;
    }

S
TD-2270  
Shengliang Guan 已提交
371
    int32_t code = bnAlterDnode(pDnode, vnodeId, dnodeId);
S
Shengliang Guan 已提交
372 373
    mnodeDecDnodeRef(pDnode);
    return code;
374
  } else {
S
TD-1732  
Shengliang Guan 已提交
375
    SCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SCfgDnodeMsg));
376 377 378 379 380 381 382 383
    strcpy(pMdCfgDnode->ep, pCmCfgDnode->ep);
    strcpy(pMdCfgDnode->config, pCmCfgDnode->config);

    SRpcMsg rpcMdCfgDnodeMsg = {
      .ahandle = 0,
      .code = 0,
      .msgType = TSDB_MSG_TYPE_MD_CONFIG_DNODE,
      .pCont = pMdCfgDnode,
S
TD-1732  
Shengliang Guan 已提交
384
      .contLen = sizeof(SCfgDnodeMsg)
385 386 387 388
    };

    mInfo("dnode:%s, is configured by %s", pCmCfgDnode->ep, pMsg->pUser->user);
    dnodeSendMsgToDnode(&epSet, &rpcMdCfgDnodeMsg);
S
Shengliang Guan 已提交
389
    mnodeDecDnodeRef(pDnode);
390 391
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
392
}
S
slguan 已提交
393

394
static void mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) {
395
  mInfo("cfg dnode rsp is received");
S
slguan 已提交
396 397
}

S
TD-1473  
Shengliang Guan 已提交
398
static int32_t mnodeCheckClusterCfgPara(const SClusterCfg *clusterCfg) {
H
Hui Li 已提交
399 400
  if (clusterCfg->numOfMnodes != htonl(tsNumOfMnodes)) {
    mError("\"numOfMnodes\"[%d - %d] cfg parameters inconsistent", clusterCfg->numOfMnodes, htonl(tsNumOfMnodes));
S
TD-1473  
Shengliang Guan 已提交
401 402
    return TAOS_DN_OFF_NUM_OF_MNODES_NOT_MATCH;
  }
H
Hui Li 已提交
403
  if (clusterCfg->mnodeEqualVnodeNum != htonl(tsMnodeEqualVnodeNum)) {
S
TD-1473  
Shengliang Guan 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    mError("\"mnodeEqualVnodeNum\"[%d - %d] cfg parameters inconsistent", clusterCfg->mnodeEqualVnodeNum,
           htonl(tsMnodeEqualVnodeNum));
    return TAOS_DN_OFF_MN_EQUAL_VN_NOT_MATCH;
  }
  if (clusterCfg->offlineThreshold != htonl(tsOfflineThreshold)) {
    mError("\"offlineThreshold\"[%d - %d] cfg parameters inconsistent", clusterCfg->offlineThreshold,
           htonl(tsOfflineThreshold));
    return TAOS_DN_OFF_OFFLINE_THRESHOLD_NOT_MATCH;
  }
  if (clusterCfg->statusInterval != htonl(tsStatusInterval)) {
    mError("\"statusInterval\"[%d - %d] cfg parameters inconsistent", clusterCfg->statusInterval,
           htonl(tsStatusInterval));
    return TAOS_DN_OFF_STATUS_INTERVAL_NOT_MATCH;
  }
  if (clusterCfg->maxtablesPerVnode != htonl(tsMaxTablePerVnode)) {
    mError("\"maxTablesPerVnode\"[%d - %d] cfg parameters inconsistent", clusterCfg->maxtablesPerVnode,
           htonl(tsMaxTablePerVnode));
    return TAOS_DN_OFF_MAX_TAB_PER_VN_NOT_MATCH;
  }
  if (clusterCfg->maxVgroupsPerDb != htonl(tsMaxVgroupsPerDb)) {
    mError("\"maxVgroupsPerDb\"[%d - %d]  cfg parameters inconsistent", clusterCfg->maxVgroupsPerDb,
           htonl(tsMaxVgroupsPerDb));
    return TAOS_DN_OFF_MAX_VG_PER_DB_NOT_MATCH;
H
Hui Li 已提交
427 428 429
  }
  if (0 != strncasecmp(clusterCfg->arbitrator, tsArbitrator, strlen(tsArbitrator))) {
    mError("\"arbitrator\"[%s - %s]  cfg parameters inconsistent", clusterCfg->arbitrator, tsArbitrator);
S
TD-1473  
Shengliang Guan 已提交
430
    return TAOS_DN_OFF_ARBITRATOR_NOT_MATCH;
H
Hui Li 已提交
431
  }
H
Hui Li 已提交
432 433

  int64_t checkTime = 0;
S
TD-1473  
Shengliang Guan 已提交
434
  char    timestr[32] = "1970-01-01 00:00:00.00";
S
TD-1207  
Shengliang Guan 已提交
435
  (void)taosParseTime(timestr, &checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
S
TD-1473  
Shengliang Guan 已提交
436 437 438 439 440
  if ((0 != strncasecmp(clusterCfg->timezone, tsTimezone, strlen(tsTimezone))) &&
      (checkTime != clusterCfg->checkTime)) {
    mError("\"timezone\"[%s - %s] [%" PRId64 " - %" PRId64 "] cfg parameters inconsistent", clusterCfg->timezone,
           tsTimezone, clusterCfg->checkTime, checkTime);
    return TAOS_DN_OFF_TIME_ZONE_NOT_MATCH;
H
Hui Li 已提交
441
  }
H
Hui Li 已提交
442

443 444 445 446 447 448 449 450
  // if (0 != strncasecmp(clusterCfg->locale, tsLocale, strlen(tsLocale))) {
  //   mError("\"locale\"[%s - %s]  cfg parameters inconsistent", clusterCfg->locale, tsLocale);
  //   return TAOS_DN_OFF_LOCALE_NOT_MATCH;
  // }
  // if (0 != strncasecmp(clusterCfg->charset, tsCharset, strlen(tsCharset))) {
  //   mError("\"charset\"[%s - %s] cfg parameters inconsistent.", clusterCfg->charset, tsCharset);
  //   return TAOS_DN_OFF_CHARSET_NOT_MATCH;
  // }
S
TD-1473  
Shengliang Guan 已提交
451

S
Shengliang Guan 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  if (clusterCfg->enableBalance != tsEnableBalance) {
    mError("\"balance\"[%d - %d] cfg parameters inconsistent", clusterCfg->enableBalance, tsEnableBalance);
    return TAOS_DN_OFF_ENABLE_BALANCE_NOT_MATCH;
  }
  if (clusterCfg->flowCtrl != tsEnableFlowCtrl) {
    mError("\"flowCtrl\"[%d - %d] cfg parameters inconsistent", clusterCfg->flowCtrl, tsEnableFlowCtrl);
    return TAOS_DN_OFF_FLOW_CTRL_NOT_MATCH;
  }
  if (clusterCfg->slaveQuery != tsEnableSlaveQuery) {
    mError("\"slaveQuery\"[%d - %d] cfg parameters inconsistent", clusterCfg->slaveQuery, tsEnableSlaveQuery);
    return TAOS_DN_OFF_SLAVE_QUERY_NOT_MATCH;
  }
  if (clusterCfg->adjustMaster != tsEnableAdjustMaster) {
    mError("\"adjustMaster\"[%d - %d] cfg parameters inconsistent", clusterCfg->adjustMaster, tsEnableAdjustMaster);
    return TAOS_DN_OFF_ADJUST_MASTER_NOT_MATCH;
  }

S
TD-1473  
Shengliang Guan 已提交
469
  return 0;
H
Hui Li 已提交
470 471
}

S
TD-1762  
Shengliang Guan 已提交
472 473 474 475 476 477 478
static int32_t mnodeGetDnodeEpsSize() {
  pthread_mutex_lock(&tsDnodeEpsMutex);
  int32_t size = tsDnodeEpsSize;
  pthread_mutex_unlock(&tsDnodeEpsMutex);
  return size;
}

S
TD-1762  
Shengliang Guan 已提交
479
static void mnodeGetDnodeEpsData(SDnodeEps *pEps, int32_t epsSize) {
S
TD-1762  
Shengliang Guan 已提交
480
  pthread_mutex_lock(&tsDnodeEpsMutex);
S
TD-1762  
Shengliang Guan 已提交
481 482 483
  if (epsSize == tsDnodeEpsSize) {
    memcpy(pEps, tsDnodeEps, tsDnodeEpsSize);
  }
S
TD-1762  
Shengliang Guan 已提交
484 485 486 487 488 489 490 491
  pthread_mutex_unlock(&tsDnodeEpsMutex);
}

static void mnodeUpdateDnodeEps() {
  pthread_mutex_lock(&tsDnodeEpsMutex);

  int32_t totalDnodes = mnodeGetDnodesNum();
  tsDnodeEpsSize = sizeof(SDnodeEps) + totalDnodes * sizeof(SDnodeEp);
S
TD-1762  
Shengliang Guan 已提交
492
  free(tsDnodeEps);
S
TD-1762  
Shengliang Guan 已提交
493 494 495 496 497 498 499 500 501 502
  tsDnodeEps = calloc(1, tsDnodeEpsSize);
  tsDnodeEps->dnodeNum = htonl(totalDnodes);

  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    dnodesNum = 0;

  while (1) {
    pIter = mnodeGetNextDnode(pIter, &pDnode);
    if (pDnode == NULL) break;
S
Shengliang Guan 已提交
503 504 505 506
    if (dnodesNum >= totalDnodes) {
      mnodeCancelGetNextDnode(pIter);
      break;
    }
S
TD-1762  
Shengliang Guan 已提交
507 508 509 510 511 512 513 514 515 516 517 518

    SDnodeEp *pEp = &tsDnodeEps->dnodeEps[dnodesNum];
    dnodesNum++;
    pEp->dnodeId = htonl(pDnode->dnodeId);
    pEp->dnodePort = htons(pDnode->dnodePort);
    tstrncpy(pEp->dnodeFqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN);
    mnodeDecDnodeRef(pDnode);
  }

  pthread_mutex_unlock(&tsDnodeEpsMutex);
}

519
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
TD-1473  
Shengliang Guan 已提交
520
  SDnodeObj *pDnode     = NULL;
S
TD-1732  
Shengliang Guan 已提交
521
  SStatusMsg *pStatus   = pMsg->rpcMsg.pCont;
S
slguan 已提交
522
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
523
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
524 525
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
TD-1473  
Shengliang Guan 已提交
526

527 528
  uint32_t _version = htonl(pStatus->version);
  if (_version != tsVersion) {
S
TD-1473  
Shengliang Guan 已提交
529 530 531 532
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
    if (pDnode != NULL && pDnode->status != TAOS_DN_STATUS_READY) {
      pDnode->offlineReason = TAOS_DN_OFF_VERSION_NOT_MATCH;
    }
533
    mError("dnode:%d, status msg version:%d not equal with cluster:%d", pStatus->dnodeId, _version, tsVersion);
534
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
535 536
  }

S
slguan 已提交
537
  if (pStatus->dnodeId == 0) {
538
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
539
    if (pDnode == NULL) {
540
      mDebug("dnode %s not created", pStatus->dnodeEp);
541
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
542 543
    }
  } else {
544
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
545
    if (pDnode == NULL) {
S
TD-1473  
Shengliang Guan 已提交
546 547 548 549 550
      pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
      if (pDnode != NULL && pDnode->status != TAOS_DN_STATUS_READY) {
        pDnode->offlineReason = TAOS_DN_OFF_DNODE_ID_NOT_MATCH;
      }
      mError("dnode:%d, %s not exist", pStatus->dnodeId, pStatus->dnodeEp);
551
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
552 553
    }
  }
J
jtao1735 已提交
554

S
[TD-17]  
slguan 已提交
555 556
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
557 558
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
559
  pDnode->moduleStatus     = pStatus->moduleStatus;
S
Shengliang Guan 已提交
560

S
slguan 已提交
561
  if (pStatus->dnodeId == 0) {
562
    mDebug("dnode:%d %s, first access, set clusterId %s", pDnode->dnodeId, pDnode->dnodeEp, mnodeGetClusterId());
S
slguan 已提交
563
  } else {
564
    if (strncmp(pStatus->clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN - 1) != 0) {
S
TD-1473  
Shengliang Guan 已提交
565 566 567
      if (pDnode != NULL && pDnode->status != TAOS_DN_STATUS_READY) {
        pDnode->offlineReason = TAOS_DN_OFF_CLUSTER_ID_NOT_MATCH;
      }
568
      mError("dnode:%d, input clusterId %s not match with exist %s", pDnode->dnodeId, pStatus->clusterId,
S
Shengliang Guan 已提交
569 570 571
             mnodeGetClusterId());
      return TSDB_CODE_MND_INVALID_CLUSTER_ID;
    } else {
572
      mTrace("dnode:%d, status received, access times %" PRId64 " openVnodes:%d:%d", pDnode->dnodeId, pDnode->lastAccess,
S
TD-1725  
Shengliang Guan 已提交
573
             htons(pStatus->openVnodes), pDnode->openVnodes);
S
Shengliang Guan 已提交
574
    }
S
slguan 已提交
575
  }
S
Shengliang Guan 已提交
576

577
  int32_t openVnodes = htons(pStatus->openVnodes);
S
TD-1762  
Shengliang Guan 已提交
578
  int32_t epsSize = mnodeGetDnodeEpsSize();
S
TD-1732  
Shengliang Guan 已提交
579 580
  int32_t vgAccessSize = openVnodes * sizeof(SVgroupAccess);
  int32_t contLen = sizeof(SStatusRsp) + vgAccessSize + epsSize;
S
TD-1762  
Shengliang Guan 已提交
581

S
TD-1732  
Shengliang Guan 已提交
582
  SStatusRsp *pRsp = rpcMallocCont(contLen);
583 584 585 586 587 588 589 590
  if (pRsp == NULL) {
    mnodeDecDnodeRef(pDnode);
    return TSDB_CODE_MND_OUT_OF_MEMORY;
  }

  pRsp->dnodeCfg.dnodeId = htonl(pDnode->dnodeId);
  pRsp->dnodeCfg.moduleStatus = htonl((int32_t)pDnode->isMgmt);
  pRsp->dnodeCfg.numOfVnodes = htonl(openVnodes);
591
  tstrncpy(pRsp->dnodeCfg.clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN);
S
TD-1732  
Shengliang Guan 已提交
592
  SVgroupAccess *pAccess = (SVgroupAccess *)((char *)pRsp + sizeof(SStatusRsp));
S
TD-1762  
Shengliang Guan 已提交
593
  
594
  for (int32_t j = 0; j < openVnodes; ++j) {
595
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
596
    pVload->vgId = htonl(pVload->vgId);
S
TD-2270  
Shengliang Guan 已提交
597 598
    pVload->dbCfgVersion = htonl(pVload->dbCfgVersion);
    pVload->vgCfgVersion = htonl(pVload->vgCfgVersion);
S
TD-2429  
Shengliang Guan 已提交
599
    pVload->vnodeVersion = htobe64(pVload->vnodeVersion);
S
slguan 已提交
600

601
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
602
    if (pVgroup == NULL) {
603
      SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
604
      mInfo("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
605
      mnodeSendDropVnodeMsg(pVload->vgId, &epSet, NULL);
606
    } else {
607
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
608 609
      pAccess->vgId = htonl(pVload->vgId);
      pAccess->accessState = pVgroup->accessState;
H
Haojun Liao 已提交
610
      pAccess++;
611
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
612 613 614
    }
  }

S
slguan 已提交
615
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
H
Hui Li 已提交
616
    // Verify whether the cluster parameters are consistent when status change from offline to ready
S
TD-1473  
Shengliang Guan 已提交
617 618 619
    int32_t ret = mnodeCheckClusterCfgPara(&(pStatus->clusterCfg));
    if (0 != ret) {
      pDnode->offlineReason = ret;
H
Hui Li 已提交
620
      mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
621
      rpcFreeCont(pRsp);
S
TD-1473  
Shengliang Guan 已提交
622 623
      mError("dnode:%d, %s cluster cfg parameters inconsistent, reason:%s", pDnode->dnodeId, pStatus->dnodeEp,
             offlineReason[ret]);
H
Hui Li 已提交
624 625
      return TSDB_CODE_MND_CLUSTER_CFG_INCONSISTENT;
    }
S
TD-1473  
Shengliang Guan 已提交
626

S
add log  
Shengliang Guan 已提交
627
    mInfo("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
628
    pDnode->status = TAOS_DN_STATUS_READY;
S
TD-1473  
Shengliang Guan 已提交
629
    pDnode->offlineReason = TAOS_DN_OFF_ONLINE;
S
TD-2270  
Shengliang Guan 已提交
630 631
    bnCheckModules();
    bnNotify();
S
slguan 已提交
632 633
  }

634
  int32_t numOfMnodes = mnodeGetMnodesNum();
635
  if (numOfMnodes < tsNumOfMnodes && numOfMnodes < mnodeGetOnlineDnodesNum() && !pDnode->isMgmt) {
636
    bnNotify();
637 638
  }

S
Shengliang Guan 已提交
639 640 641 642
  if (openVnodes != pDnode->openVnodes) {
    mnodeCheckUnCreatedVgroup(pDnode, pStatus->load, openVnodes);
  }

H
Hui Li 已提交
643
  pDnode->lastAccess = tsAccessSquence;
S
Shengliang Guan 已提交
644 645 646 647

  //this func should be called after sdb replica changed
  mnodeGetMnodeInfos(&pRsp->mnodes);
  
648
  mnodeDecDnodeRef(pDnode);
H
Hui Li 已提交
649

S
TD-1732  
Shengliang Guan 已提交
650
  SDnodeEps *pEps = (SDnodeEps *)((char *)pRsp + sizeof(SStatusRsp) + vgAccessSize);
S
TD-1762  
Shengliang Guan 已提交
651
  mnodeGetDnodeEpsData(pEps, epsSize);
S
TD-1762  
Shengliang Guan 已提交
652

653 654
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
655

656
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
657
}
658

659
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg) {
S
slguan 已提交
660 661 662 663 664
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

S
Shengliang Guan 已提交
665 666 667 668 669 670
  char dnodeEp[TSDB_EP_LEN] = {0};
  tstrncpy(dnodeEp, ep, TSDB_EP_LEN);
  strtrim(dnodeEp);

  char *temp = strchr(dnodeEp, ':');
  if (!temp) {
S
TD-1207  
Shengliang Guan 已提交
671
    int32_t len = (int32_t)strlen(dnodeEp);
S
Shengliang Guan 已提交
672
    if (dnodeEp[len - 1] == ';') dnodeEp[len - 1] = 0;
S
TD-1207  
Shengliang Guan 已提交
673
    len = (int32_t)strlen(dnodeEp);
S
Shengliang Guan 已提交
674 675 676 677
    snprintf(dnodeEp + len, TSDB_EP_LEN - len, ":%d", tsServerPort);
  }
  ep = dnodeEp;

678
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
679
  if (pDnode != NULL) {
680
    mnodeDecDnodeRef(pDnode);
681
    mError("dnode:%d, already exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
682
    return TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
slguan 已提交
683 684 685 686 687
  }

  pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj));
  pDnode->createdTime = taosGetTimestampMs();
  pDnode->status = TAOS_DN_STATUS_OFFLINE; 
S
TD-1473  
Shengliang Guan 已提交
688
  pDnode->offlineReason = TAOS_DN_OFF_STATUS_NOT_RECEIVED;
689
  tstrncpy(pDnode->dnodeEp, ep, TSDB_EP_LEN);
J
jtao1735 已提交
690
  taosGetFqdnPortFromEp(ep, pDnode->dnodeFqdn, &pDnode->dnodePort);
S
slguan 已提交
691

S
TD-2046  
Shengliang Guan 已提交
692
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
693 694
    .type    = SDB_OPER_GLOBAL,
    .pTable  = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
695
    .pObj    = pDnode,
696
    .rowSize = sizeof(SDnodeObj),
S
TD-2046  
Shengliang Guan 已提交
697
    .pMsg    = pMsg
S
slguan 已提交
698 699
  };

S
TD-2046  
Shengliang Guan 已提交
700
  int32_t code = sdbInsertRow(&row);
S
Shengliang Guan 已提交
701
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
slguan 已提交
702
    int dnodeId = pDnode->dnodeId;
S
TD-1848  
Shengliang Guan 已提交
703
    tfree(pDnode);
S
Shengliang Guan 已提交
704
    mError("failed to create dnode:%d, reason:%s", dnodeId, tstrerror(code));
705
  } else {
S
Shengliang Guan 已提交
706
    mLInfo("dnode:%d is created", pDnode->dnodeId);
S
slguan 已提交
707 708 709 710 711
  }

  return code;
}

712
int32_t mnodeDropDnode(SDnodeObj *pDnode, void *pMsg) {
S
TD-2046  
Shengliang Guan 已提交
713
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
714 715
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
716
    .pObj   = pDnode,
S
TD-2046  
Shengliang Guan 已提交
717
    .pMsg   = pMsg
S
slguan 已提交
718 719
  };

S
TD-2046  
Shengliang Guan 已提交
720
  int32_t code = sdbDeleteRow(&row);
S
Shengliang Guan 已提交
721 722 723 724
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("dnode:%d, failed to drop from cluster, result:%s", pDnode->dnodeId, tstrerror(code));
  } else {
    mLInfo("dnode:%d, is dropped from cluster", pDnode->dnodeId);
S
slguan 已提交
725 726 727 728 729
  }

  return code;
}

730
static int32_t mnodeDropDnodeByEp(char *ep, SMnodeMsg *pMsg) {
731
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
732
  if (pDnode == NULL) {
733 734 735 736
    if (strspn(ep, "0123456789 ;") != strlen(ep)) {
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }

S
Shengliang Guan 已提交
737 738 739 740 741 742
    int32_t dnodeId = (int32_t)strtol(ep, NULL, 10);
    pDnode = mnodeGetDnode(dnodeId);
    if (pDnode == NULL) {
      mError("dnode:%s, is not exist", ep);
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
743 744
  }

H
Hui Li 已提交
745
  if (strcmp(pDnode->dnodeEp, mnodeGetMnodeMasterEp()) == 0) {
J
jtao1735 已提交
746
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
747
    mnodeDecDnodeRef(pDnode);
748
    return TSDB_CODE_MND_NO_REMOVE_MASTER;
S
slguan 已提交
749 750
  }

751 752 753 754 755 756 757 758
  int32_t maxReplica = mnodeGetDbMaxReplica();
  int32_t dnodesNum = mnodeGetDnodesNum();
  if (dnodesNum <= maxReplica) {
    mError("dnode:%d, can't drop dnode:%s, #dnodes: %d, replia: %d", pDnode->dnodeId, ep, dnodesNum, maxReplica);
    mnodeDecDnodeRef(pDnode);
    return TSDB_CODE_MND_NO_ENOUGH_DNODES;
  }

759
  mInfo("dnode:%d, start to drop it", pDnode->dnodeId);
760

S
TD-2270  
Shengliang Guan 已提交
761
  int32_t code = bnDropDnode(pDnode);
762 763
  mnodeDecDnodeRef(pDnode);
  return code;
S
slguan 已提交
764 765
}

766
static int32_t mnodeProcessCreateDnodeMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
767
  SCreateDnodeMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
768

769
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
770
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
771
  } else {
772
    return mnodeCreateDnode(pCreate->ep, pMsg);
S
slguan 已提交
773 774 775
  }
}

776
static int32_t mnodeProcessDropDnodeMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
777
  SDropDnodeMsg *pDrop = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
778

779
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
780
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
781
  } else {
782
    return mnodeDropDnodeByEp(pDrop->ep, pMsg);
783
  }
S
slguan 已提交
784 785
}

786 787
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
788 789
  if (pUser == NULL) return 0;

790
  if (strcmp(pUser->pAcct->user, TSDB_DEFAULT_USER) != 0) {
791
    mnodeDecUserRef(pUser);
792
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
793
  }
794 795 796 797 798 799 800 801 802 803

  int32_t  cols = 0;
  SSchema *pSchema = pMeta->schema;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

D
dapan1121 已提交
804
  pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE;
805
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
806
  strcpy(pSchema[cols].name, "end_point");
807 808 809 810 811
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
812
  strcpy(pSchema[cols].name, "vnodes");
813 814 815 816 817
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
818
  strcpy(pSchema[cols].name, "cores");
819 820 821
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
822
  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
823
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
824 825 826 827
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
828
  pShow->bytes[cols] = 5 + VARSTR_HEADER_SIZE;
829
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
830
  strcpy(pSchema[cols].name, "role");
831 832 833
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
834 835
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
836
  strcpy(pSchema[cols].name, "create_time");
837 838 839
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
840 841 842 843 844 845
  pShow->bytes[cols] = 24 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "offline reason");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

846 847 848 849 850 851 852 853
  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

854
  pShow->numOfRows = mnodeGetDnodesNum();
S
TD-2680  
Shengliang Guan 已提交
855 856 857 858
  if (tsArbitrator[0] != 0) {
    pShow->numOfRows++;
  }

859
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
860
  pShow->pIter = NULL;
861

862
  mnodeDecUserRef(pUser);
S
slguan 已提交
863

864 865 866
  return 0;
}

867
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
868 869
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
S
TD-2680  
Shengliang Guan 已提交
870
  SDnodeObj *pDnode    = NULL;
871 872 873
  char      *pWrite;

  while (numOfRows < rows) {
874
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
875 876 877 878 879 880 881 882 883
    if (pDnode == NULL) break;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pDnode->dnodeId;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Hui Li 已提交
884
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols]);
885 886 887 888 889 890 891
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pDnode->openVnodes;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
892
    *(int16_t *)pWrite = pDnode->numOfCores;
893
    cols++;
S
slguan 已提交
894
    
895
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
S
TD-2270  
Shengliang Guan 已提交
896
    char* status = dnodeStatus[pDnode->status];
897
    STR_TO_VARSTR(pWrite, status);
898 899
    cols++;

900
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
S
TD-2270  
Shengliang Guan 已提交
901
    char* role = dnodeRoles[pDnode->alternativeRole];
902 903 904
    STR_TO_VARSTR(pWrite, role);
    cols++;

S
slguan 已提交
905 906 907 908
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

S
TD-1473  
Shengliang Guan 已提交
909 910 911 912
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, offlineReason[pDnode->offlineReason]);
    cols++;

S
TD-2680  
Shengliang Guan 已提交
913
    numOfRows++;
914
    mnodeDecDnodeRef(pDnode);
915 916
  }

S
TD-2680  
Shengliang Guan 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
  if (tsArbitrator[0] != 0) {
    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = 0;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tsArbitrator, pShow->bytes[cols]);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = 0;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = 0;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    char *status = dnodeStatus[tsArbOnline > 0 ? TAOS_DN_STATUS_READY : TAOS_DN_STATUS_OFFLINE];
    STR_TO_VARSTR(pWrite, status);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, "arb");
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
946
    *(int64_t *)pWrite = tsArbOnlineTimestamp;
S
TD-2680  
Shengliang Guan 已提交
947 948 949 950 951 952 953 954 955
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, "-");
    cols++;

    numOfRows++;
  }

956
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
957 958 959 960
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

961
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
H
Haojun Liao 已提交
962
  uint32_t status = pDnode->moduleStatus & (1u << moduleType);
963 964 965
  return status > 0;
}

966
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
967 968
  int32_t cols = 0;

969
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
970 971
  if (pUser == NULL) return 0;

972
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
973
    mnodeDecUserRef(pUser);
974
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
975
  }
976 977 978

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
979 980 981 982 983 984
  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
985
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
986
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
987
  strcpy(pSchema[cols].name, "end_point");
988 989 990
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
991
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
992
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
993
  strcpy(pSchema[cols].name, "module");
994 995 996
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
997
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
998
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
999
  strcpy(pSchema[cols].name, "status");
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

1011
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
1012
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1013
  pShow->pIter = NULL;
1014
  mnodeDecUserRef(pUser);
1015 1016 1017 1018

  return 0;
}

1019
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1020
  int32_t numOfRows = 0;
H
Haojun Liao 已提交
1021 1022 1023

  char* pWrite;
  char* moduleName[5] = { "MNODE", "HTTP", "MONITOR", "MQTT", "UNKNOWN" };
Y
TD-1659  
yihaoDeng 已提交
1024
  int32_t cols;
1025 1026

  while (numOfRows < rows) {
S
slguan 已提交
1027
    SDnodeObj *pDnode = NULL;
1028
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
1029 1030 1031
    if (pDnode == NULL) break;

    for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) {
Y
TD-1659  
yihaoDeng 已提交
1032
      cols = 0;
1033

S
slguan 已提交
1034 1035 1036
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
1037 1038

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
1039
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols] - 1);
1040 1041 1042
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
1043
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, moduleName[moduleType], pShow->bytes[cols]);
1044 1045 1046
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
1047
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
H
Haojun Liao 已提交
1048 1049 1050

      char* v = enable? "enable":"disable";
      STR_TO_VARSTR(pWrite, v);
1051 1052 1053 1054
      cols++;

      numOfRows++;
    }
S
slguan 已提交
1055

1056
    mnodeDecDnodeRef(pDnode);
1057 1058
  }

1059
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1060 1061 1062 1063
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

1064
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
1065 1066 1067 1068 1069
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

1070
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
1071 1072
  int32_t cols = 0;

1073
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
1074 1075
  if (pUser == NULL) return 0;

1076
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
1077
    mnodeDecUserRef(pUser);
1078
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
1079
  }
1080 1081 1082

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
1083
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
1084
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
1085
  tstrncpy(pSchema[cols].name, "name", sizeof(pSchema[cols].name));
1086 1087 1088
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
1089
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
1090
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
1091
  tstrncpy(pSchema[cols].name, "value", sizeof(pSchema[cols].name));
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];

  pShow->numOfRows = 0;
  for (int32_t i = tsGlobalConfigNum - 1; i >= 0; --i) {
S
slguan 已提交
1103
    SGlobalCfg *cfg = tsGlobalConfig + i;
1104
    if (!mnodeCheckConfigShow(cfg)) continue;
1105 1106 1107 1108
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1109
  pShow->pIter = NULL;
1110
  mnodeDecUserRef(pUser);
1111 1112 1113 1114

  return 0;
}

1115
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1116 1117 1118
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
1119
    SGlobalCfg *cfg = tsGlobalConfig + i;
1120
    if (!mnodeCheckConfigShow(cfg)) continue;
1121 1122 1123 1124 1125

    char *pWrite;
    int32_t   cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
1126 1127
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfg->option, TSDB_CFG_OPTION_LEN);

1128
    cols++;
1129
    int32_t t = 0;
1130 1131 1132

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
Shengliang Guan 已提交
1133 1134 1135 1136 1137
      case TAOS_CFG_VTYPE_INT8:
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int8_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
        numOfRows++;
        break;
S
slguan 已提交
1138
      case TAOS_CFG_VTYPE_INT16:
1139 1140
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1141 1142
        numOfRows++;
        break;
S
slguan 已提交
1143
      case TAOS_CFG_VTYPE_INT32:
1144 1145
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1146 1147
        numOfRows++;
        break;
S
slguan 已提交
1148
      case TAOS_CFG_VTYPE_FLOAT:
1149 1150
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        varDataSetLen(pWrite, t);
1151 1152
        numOfRows++;
        break;
S
slguan 已提交
1153 1154 1155
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
1156
        STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfg->ptr, TSDB_CFG_VALUE_LEN);
1157 1158 1159 1160 1161 1162 1163
        numOfRows++;
        break;
      default:
        break;
    }
  }

1164
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1165 1166 1167 1168
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

1169
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
1170
  int32_t cols = 0;
1171
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
1172
  if (pUser == NULL) return 0;
S
slguan 已提交
1173
  
1174
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
1175
    mnodeDecUserRef(pUser);
1176
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
1177
  }
1178 1179 1180 1181 1182 1183 1184 1185 1186

  SSchema *pSchema = pMeta->schema;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vnode");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
1187
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];

  SDnodeObj *pDnode = NULL;
  if (pShow->payloadLen > 0 ) {
1201
    pDnode = mnodeGetDnodeByEp(pShow->payload);
1202
  } else {
1203
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
1204
    mnodeCancelGetNextDnode(pIter);
S
slguan 已提交
1205
  }
1206

S
slguan 已提交
1207 1208
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
1209
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1210
  }
1211 1212

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1213
  pShow->pIter = pDnode;
1214
  mnodeDecUserRef(pUser);
1215 1216 1217 1218

  return 0;
}

1219
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1220 1221 1222 1223 1224 1225 1226
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
1227
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
1228
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
1229
    void *pIter = NULL;
S
slguan 已提交
1230 1231
    SVgObj *pVgroup;
    while (1) {
1232
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
      if (pVgroup == NULL) break;

      for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
        SVnodeGid *pVgid = &pVgroup->vnodeGid[i];
        if (pVgid->pDnode == pDnode) {
          cols = 0;

          pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
          *(uint32_t *)pWrite = pVgroup->vgId;
          cols++;

          pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
TD-2046  
Shengliang Guan 已提交
1245
          strcpy(pWrite, syncRole[pVgid->role]);
S
slguan 已提交
1246
          cols++;
1247 1248
        }
      }
S
slguan 已提交
1249

1250
      mnodeDecVgroupRef(pVgroup);
1251 1252 1253 1254
    }
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
1255

1256
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1257 1258
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
1259 1260
}

S
TD-2270  
Shengliang Guan 已提交
1261 1262 1263 1264 1265 1266 1267
char* dnodeStatus[] = {
  "offline",
  "dropping",
  "balancing",
  "ready",
  "undefined"
};
1268

S
TD-2270  
Shengliang Guan 已提交
1269 1270 1271 1272 1273 1274
char* dnodeRoles[] = {
  "any",
  "mnode",
  "vnode",
  "any"
};
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

int32_t mnodeCompactDnodes() {
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;

  mInfo("start to compact dnodes table...");

  while (1) {
    pIter = mnodeGetNextDnode(pIter, &pDnode);
    if (pDnode == NULL) break;

    SSdbRow row = {
      .type    = SDB_OPER_GLOBAL,
      .pTable  = tsDnodeSdb,
      .pObj    = pDnode,
      .rowSize = sizeof(SDnodeObj),
    };

    mInfo("compact dnode %d", pDnode->dnodeId);
    
    sdbInsertCompactRow(&row);
  }

  mInfo("end to compact dnodes table...");

  return 0;
1301
}