mnodeDnode.c 28.2 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
slguan 已提交
19
#include "tbalance.h"
S
slguan 已提交
20 21
#include "tglobal.h"
#include "tconfig.h"
S
slguan 已提交
22 23 24
#include "ttime.h"
#include "tutil.h"
#include "tsocket.h"
S
slguan 已提交
25
#include "tbalance.h"
S
slguan 已提交
26
#include "tsync.h"
27
#include "tdataformat.h"
28
#include "mnode.h"
S
slguan 已提交
29
#include "dnode.h"
S
Shengliang Guan 已提交
30 31 32 33 34
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeDnode.h"
#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
slguan 已提交
40

S
slguan 已提交
41
int32_t tsAccessSquence = 0;
42 43
static void   *tsDnodeSdb = NULL;
static int32_t tsDnodeUpdateSize = 0;
S
slguan 已提交
44
extern void *  tsMnodeSdb;
S
slguan 已提交
45 46
extern void *  tsVgroupSdb;

47
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg);
48 49 50 51 52 53 54 55 56 57 58 59 60
static int32_t mnodeProcessCreateDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessDropDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) ;
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *rpcMsg);
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);
61
static char*   mnodeGetDnodeAlternativeRoleStr(int32_t alternativeRole);
62 63

static int32_t mnodeDnodeActionDestroy(SSdbOper *pOper) {
S
slguan 已提交
64 65 66
  tfree(pOper->pObj);
  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
67

68
static int32_t mnodeDnodeActionInsert(SSdbOper *pOper) {
S
slguan 已提交
69 70 71
  SDnodeObj *pDnode = pOper->pObj;
  if (pDnode->status != TAOS_DN_STATUS_DROPPING) {
    pDnode->status = TAOS_DN_STATUS_OFFLINE;
S
slguan 已提交
72
  }
S
slguan 已提交
73 74

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
75 76
}

77
static int32_t mnodeDnodeActionDelete(SSdbOper *pOper) {
S
slguan 已提交
78
  SDnodeObj *pDnode = pOper->pObj;
79
 
S
slguan 已提交
80
#ifndef _SYNC 
81
  mnodeDropAllDnodeVgroups(pDnode);
S
slguan 已提交
82
#endif  
83
  mnodeDropMnodeLocal(pDnode->dnodeId);
S
Shengliang Guan 已提交
84
  balanceAsyncNotify();
S
slguan 已提交
85

86
  mTrace("dnode:%d, all vgroups is dropped from sdb", pDnode->dnodeId);
S
slguan 已提交
87 88 89
  return TSDB_CODE_SUCCESS;
}

90
static int32_t mnodeDnodeActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
91
  SDnodeObj *pDnode = pOper->pObj;
92
  SDnodeObj *pSaved = mnodeGetDnode(pDnode->dnodeId);
S
Shengliang Guan 已提交
93
  if (pSaved != NULL && pDnode != pSaved) {
S
slguan 已提交
94 95
    memcpy(pSaved, pDnode, pOper->rowSize);
    free(pDnode);
S
Shengliang Guan 已提交
96
    mnodeDecDnodeRef(pSaved);
S
slguan 已提交
97
  }
S
Shengliang Guan 已提交
98

S
slguan 已提交
99 100 101
  return TSDB_CODE_SUCCESS;
}

102
static int32_t mnodeDnodeActionEncode(SSdbOper *pOper) {
S
slguan 已提交
103 104 105 106
  SDnodeObj *pDnode = pOper->pObj;
  memcpy(pOper->rowData, pDnode, tsDnodeUpdateSize);
  pOper->rowSize = tsDnodeUpdateSize;
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
107 108
}

109
static int32_t mnodeDnodeActionDecode(SSdbOper *pOper) {
S
slguan 已提交
110
  SDnodeObj *pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj));
111
  if (pDnode == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
112

S
slguan 已提交
113 114 115 116
  memcpy(pDnode, pOper->rowData, tsDnodeUpdateSize);
  pOper->pObj = pDnode;
  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
117

118
static int32_t mnodeDnodeActionRestored() {
S
slguan 已提交
119
  int32_t numOfRows = sdbGetNumOfRows(tsDnodeSdb);
S
slguan 已提交
120
  if (numOfRows <= 0 && dnodeIsFirstDeploy()) {
121
    mPrint("dnode first deploy, create dnode:%s", tsLocalEp);
122
    mnodeCreateDnode(tsLocalEp, NULL);
123
    SDnodeObj *pDnode = mnodeGetDnodeByEp(tsLocalEp);
S
Shengliang Guan 已提交
124 125 126 127
    if (pDnode != NULL) {
      mnodeAddMnode(pDnode->dnodeId);
      mnodeDecDnodeRef(pDnode);
    }
S
slguan 已提交
128 129
  }

S
slguan 已提交
130
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
131 132
}

133
int32_t mnodeInitDnodes() {
S
slguan 已提交
134 135 136 137 138 139
  SDnodeObj tObj;
  tsDnodeUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj;

  SSdbTableDesc tableDesc = {
    .tableId      = SDB_TABLE_DNODE,
    .tableName    = "dnodes",
S
Shengliang Guan 已提交
140
    .hashSessions = TSDB_DEFAULT_DNODES_HASH_SIZE,
S
slguan 已提交
141 142 143
    .maxRowSize   = tsDnodeUpdateSize,
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
    .keyType      = SDB_KEY_AUTO,
144 145 146 147 148 149 150
    .insertFp     = mnodeDnodeActionInsert,
    .deleteFp     = mnodeDnodeActionDelete,
    .updateFp     = mnodeDnodeActionUpdate,
    .encodeFp     = mnodeDnodeActionEncode,
    .decodeFp     = mnodeDnodeActionDecode,
    .destroyFp    = mnodeDnodeActionDestroy,
    .restoredFp   = mnodeDnodeActionRestored
S
slguan 已提交
151 152 153 154 155 156 157 158
  };

  tsDnodeSdb = sdbOpenTable(&tableDesc);
  if (tsDnodeSdb == NULL) {
    mError("failed to init dnodes data");
    return -1;
  }

159 160 161 162 163 164 165 166 167 168 169 170 171
  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);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_CONFIGS, mnodeGetConfigMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_CONFIGS, mnodeRetrieveConfigs);
  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
slguan 已提交
172
 
S
slguan 已提交
173
  mTrace("table:dnodes table is created");
S
slguan 已提交
174 175 176
  return 0;
}

S
Shengliang Guan 已提交
177
void mnodeCleanupDnodes() {
S
slguan 已提交
178 179 180
  sdbCloseTable(tsDnodeSdb);
}

181
void *mnodeGetNextDnode(void *pIter, SDnodeObj **pDnode) { 
S
Shengliang Guan 已提交
182
  return sdbFetchRow(tsDnodeSdb, pIter, (void **)pDnode); 
S
slguan 已提交
183 184
}

185
int32_t mnodeGetDnodesNum() {
S
slguan 已提交
186 187 188
  return sdbGetNumOfRows(tsDnodeSdb);
}

189
int32_t mnodeGetOnlinDnodesNum(char *ep) {
190 191 192 193 194
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    onlineDnodes = 0;

  while (1) {
195
    pIter = mnodeGetNextDnode(pIter, &pDnode);
196 197
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) onlineDnodes++;
198
    mnodeDecDnodeRef(pDnode);
199 200 201 202 203 204 205
  }

  sdbFreeIter(pIter);

  return onlineDnodes;
}

206
void *mnodeGetDnode(int32_t dnodeId) {
S
slguan 已提交
207 208 209
  return sdbGetRow(tsDnodeSdb, &dnodeId);
}

210
void *mnodeGetDnodeByEp(char *ep) {
S
slguan 已提交
211
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
212
  void *     pIter = NULL;
S
slguan 已提交
213 214

  while (1) {
215
    pIter = mnodeGetNextDnode(pIter, &pDnode);
S
slguan 已提交
216
    if (pDnode == NULL) break;
J
jtao1735 已提交
217
    if (strcmp(ep, pDnode->dnodeEp) == 0) {
S
Shengliang Guan 已提交
218
      sdbFreeIter(pIter);
S
slguan 已提交
219 220
      return pDnode;
    }
221
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
222 223
  }

S
Shengliang Guan 已提交
224 225
  sdbFreeIter(pIter);

S
slguan 已提交
226 227 228
  return NULL;
}

229
void mnodeIncDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
230 231 232
  sdbIncRef(tsDnodeSdb, pDnode);
}

233
void mnodeDecDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
234
  sdbDecRef(tsDnodeSdb, pDnode);
S
slguan 已提交
235 236
}

237
void mnodeUpdateDnode(SDnodeObj *pDnode) {
S
slguan 已提交
238
  SSdbOper oper = {
S
slguan 已提交
239 240
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
241
    .pObj = pDnode
S
slguan 已提交
242 243
  };

244 245 246
  if (sdbUpdateRow(&oper) != 0) {
    mError("dnodeId:%d, failed update", pDnode->dnodeId);
  }
S
slguan 已提交
247 248
}

249
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
250
  SCMCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
251 252
  if (pCmCfgDnode->ep[0] == 0) {
    strcpy(pCmCfgDnode->ep, tsLocalEp);
S
slguan 已提交
253
  } else {
sangshuduo's avatar
sangshuduo 已提交
254
    // TODO temporary disabled for compiling: strcpy(pCmCfgDnode->ep, pCmCfgDnode->ep); 
S
slguan 已提交
255
  }
S
slguan 已提交
256

257
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
258
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
259 260
  }

261 262 263 264 265 266 267 268 269 270 271 272 273
  SRpcIpSet ipSet = mnodeGetIpSetFromIp(pCmCfgDnode->ep);
  SMDCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SMDCfgDnodeMsg));
  strcpy(pMdCfgDnode->ep, pCmCfgDnode->ep);
  strcpy(pMdCfgDnode->config, pCmCfgDnode->config);

  SRpcMsg rpcMdCfgDnodeMsg = {
    .handle = 0,
    .code = 0,
    .msgType = TSDB_MSG_TYPE_MD_CONFIG_DNODE,
    .pCont = pMdCfgDnode,
    .contLen = sizeof(SMDCfgDnodeMsg)
  };
  dnodeSendMsgToDnode(&ipSet, &rpcMdCfgDnodeMsg);
S
slguan 已提交
274

275 276 277
  mPrint("dnode:%s, is configured by %s", pCmCfgDnode->ep, pMsg->pUser->user);

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
278
}
S
slguan 已提交
279

280
static void mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) {
guanshengliang's avatar
guanshengliang 已提交
281
  mPrint("cfg dnode rsp is received");
S
slguan 已提交
282 283
}

H
Hui Li 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297
static bool mnodeCheckClusterCfgPara(const SClusterCfg *clusterCfg) {
  if (clusterCfg->numOfMnodes        != tsNumOfMnodes)        return false;
  if (clusterCfg->mnodeEqualVnodeNum != tsMnodeEqualVnodeNum) return false;
  if (clusterCfg->offlineThreshold   != tsOfflineThreshold)   return false;
  if (clusterCfg->statusInterval     != tsStatusInterval)     return false;

  if (0 != strncasecmp(clusterCfg->arbitrator, tsArbitrator, strlen(tsArbitrator))) return false;
  if (0 != strncasecmp(clusterCfg->timezone, tsTimezone, strlen(tsTimezone)))       return false;
  if (0 != strncasecmp(clusterCfg->locale, tsLocale, strlen(tsLocale)))              return false;
  if (0 != strncasecmp(clusterCfg->charset, tsCharset, strlen(tsCharset)))           return false;
    
  return true;
}

298
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
299
  SDMStatusMsg *pStatus = pMsg->rpcMsg.pCont;
S
slguan 已提交
300
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
301
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
302 303
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
[TD-17]  
slguan 已提交
304
  pStatus->numOfTotalVnodes = htons(pStatus->numOfTotalVnodes);
S
slguan 已提交
305

S
slguan 已提交
306 307 308
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
    mError("status msg version:%d not equal with mnode:%d", version, tsVersion);
309
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
310 311
  }

S
slguan 已提交
312 313
  SDnodeObj *pDnode = NULL;
  if (pStatus->dnodeId == 0) {
314
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
315
    if (pDnode == NULL) {
J
jtao1735 已提交
316
      mTrace("dnode %s not created", pStatus->dnodeEp);
317
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
318 319
    }
  } else {
320
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
321
    if (pDnode == NULL) {
J
jtao1735 已提交
322
      mError("dnode id:%d, %s not exist", pStatus->dnodeId, pStatus->dnodeEp);
323
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
324 325
    }
  }
J
jtao1735 已提交
326

S
[TD-17]  
slguan 已提交
327 328
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
329 330
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
331
  pDnode->totalVnodes      = pStatus->numOfTotalVnodes; 
S
slguan 已提交
332
  pDnode->moduleStatus     = pStatus->moduleStatus;
333
  
S
slguan 已提交
334
  if (pStatus->dnodeId == 0) {
J
jtao1735 已提交
335
    mTrace("dnode:%d %s, first access", pDnode->dnodeId, pDnode->dnodeEp);
S
slguan 已提交
336
  } else {
S
Shengliang Guan 已提交
337
    //mTrace("dnode:%d, status received, access times %d", pDnode->dnodeId, pDnode->lastAccess);
S
slguan 已提交
338 339
  }
 
340
  int32_t openVnodes = htons(pStatus->openVnodes);
341 342 343 344 345 346 347 348 349 350 351 352
  int32_t contLen = sizeof(SDMStatusRsp) + openVnodes * sizeof(SDMVgroupAccess);
  SDMStatusRsp *pRsp = rpcMallocCont(contLen);
  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);
  SDMVgroupAccess *pAccess = (SDMVgroupAccess *)((char *)pRsp + sizeof(SDMStatusRsp));

353
  for (int32_t j = 0; j < openVnodes; ++j) {
354
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
355
    pVload->vgId = htonl(pVload->vgId);
S
slguan 已提交
356
    pVload->cfgVersion = htonl(pVload->cfgVersion);
S
slguan 已提交
357

358
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
359
    if (pVgroup == NULL) {
360
      SRpcIpSet ipSet = mnodeGetIpSetFromIp(pDnode->dnodeEp);
S
slguan 已提交
361
      mPrint("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
362
      mnodeSendDropVnodeMsg(pVload->vgId, &ipSet, NULL);
363
    } else {
364
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
365 366
      pAccess->vgId = htonl(pVload->vgId);
      pAccess->accessState = pVgroup->accessState;
H
Haojun Liao 已提交
367
      pAccess++;
368
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
369
    }
H
Haojun Liao 已提交
370

S
slguan 已提交
371 372
  }

S
slguan 已提交
373
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
H
Hui Li 已提交
374
    // Verify whether the cluster parameters are consistent when status change from offline to ready
H
Hui Li 已提交
375
    bool ret = mnodeCheckClusterCfgPara(&(pStatus->clusterCfg));
H
Hui Li 已提交
376 377
    if (false == ret) {
      mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
378
      rpcFreeCont(pRsp);
H
Hui Li 已提交
379 380 381 382
      mError("dnode %s cluster cfg parameters inconsistent", pStatus->dnodeEp);
      return TSDB_CODE_MND_CLUSTER_CFG_INCONSISTENT;
    }
    
S
slguan 已提交
383
    mTrace("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
384
    pDnode->status = TAOS_DN_STATUS_READY;
S
Shengliang Guan 已提交
385 386
    balanceSyncNotify();
    balanceAsyncNotify();
S
slguan 已提交
387 388
  }

S
Shengliang Guan 已提交
389 390 391 392
  if (openVnodes != pDnode->openVnodes) {
    mnodeCheckUnCreatedVgroup(pDnode, pStatus->load, openVnodes);
  }

H
Hui Li 已提交
393
  pDnode->lastAccess = tsAccessSquence;
S
Shengliang Guan 已提交
394 395 396 397

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

400 401
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
402

403
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
404
}
405

406
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg) {
S
slguan 已提交
407 408 409 410 411
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

412
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
413
  if (pDnode != NULL) {
414
    mnodeDecDnodeRef(pDnode);
J
jtao1735 已提交
415
    mError("dnode:%d is alredy exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
416
    return TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
slguan 已提交
417 418 419 420 421 422
  }

  pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj));
  pDnode->createdTime = taosGetTimestampMs();
  pDnode->status = TAOS_DN_STATUS_OFFLINE; 
  pDnode->totalVnodes = TSDB_INVALID_VNODE_NUM; 
423
  tstrncpy(pDnode->dnodeEp, ep, TSDB_EP_LEN);
J
jtao1735 已提交
424
  taosGetFqdnPortFromEp(ep, pDnode->dnodeFqdn, &pDnode->dnodePort);
S
slguan 已提交
425

S
slguan 已提交
426
  SSdbOper oper = {
S
slguan 已提交
427 428 429
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
    .pObj = pDnode,
430 431
    .rowSize = sizeof(SDnodeObj),
    .pMsg = pMsg
S
slguan 已提交
432 433 434 435
  };

  int32_t code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
436
    int dnodeId = pDnode->dnodeId;
S
slguan 已提交
437
    tfree(pDnode);
S
slguan 已提交
438
    mError("failed to create dnode:%d, result:%s", dnodeId, tstrerror(code));
439 440 441
  } else {
    mPrint("dnode:%d is created, result:%s", pDnode->dnodeId, tstrerror(code));
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
442 443 444 445 446
  }

  return code;
}

447
int32_t mnodeDropDnode(SDnodeObj *pDnode, void *pMsg) {
S
slguan 已提交
448
  SSdbOper oper = {
S
slguan 已提交
449 450
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
451 452
    .pObj = pDnode,
    .pMsg = pMsg
S
slguan 已提交
453 454
  };

455 456 457 458
  int32_t code = sdbDeleteRow(&oper);
  if (code == TSDB_CODE_SUCCESS) {
    mLPrint("dnode:%d, is dropped from cluster, result:%s", pDnode->dnodeId, tstrerror(code));
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
459 460 461 462 463
  }

  return code;
}

464
static int32_t mnodeDropDnodeByEp(char *ep, SMnodeMsg *pMsg) {
465
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
466
  if (pDnode == NULL) {
J
jtao1735 已提交
467
    mError("dnode:%s, is not exist", ep);
468
    return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
469 470
  }

471
  mnodeDecDnodeRef(pDnode);
J
jtao1735 已提交
472 473
  if (strcmp(pDnode->dnodeEp, dnodeGetMnodeMasterEp()) == 0) {
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
474
    return TSDB_CODE_MND_NO_REMOVE_MASTER;
S
slguan 已提交
475 476
  }

S
add log  
slguan 已提交
477
  mPrint("dnode:%d, start to drop it", pDnode->dnodeId);
S
slguan 已提交
478
#ifndef _SYNC
479
  return mnodeDropDnode(pDnode, pMsg);
S
slguan 已提交
480 481 482 483 484
#else
  return balanceDropDnode(pDnode);
#endif
}

485
static int32_t mnodeProcessCreateDnodeMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
486
  SCMCreateDnodeMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
487

488
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
489
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
490
  } else {
491
    return mnodeCreateDnode(pCreate->ep, pMsg);
S
slguan 已提交
492 493 494
  }
}

495
static int32_t mnodeProcessDropDnodeMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
496
  SCMDropDnodeMsg *pDrop = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
497

498
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
499
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
500
  } else {
501
    return mnodeDropDnodeByEp(pDrop->ep, pMsg);
502
  }
S
slguan 已提交
503 504
}

505 506
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
507 508
  if (pUser == NULL) return 0;

509
  if (strcmp(pUser->pAcct->user, TSDB_DEFAULT_USER) != 0) {
510
    mnodeDecUserRef(pUser);
511
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
512
  }
513 514 515 516 517 518 519 520 521 522

  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++;

S
slguan 已提交
523
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
524
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
525
  strcpy(pSchema[cols].name, "end_point");
526 527 528 529 530
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
531
  strcpy(pSchema[cols].name, "open_vnodes");
532 533 534 535 536
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
537
  strcpy(pSchema[cols].name, "total_vnodes");
538 539 540
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
541
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
542
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
543 544 545 546
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

547 548 549 550 551 552
  pShow->bytes[cols] = 6 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "alternativeRole");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
553 554
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
555
  strcpy(pSchema[cols].name, "create_time");
556 557 558 559 560 561 562 563 564 565 566
  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];
  }

567
  pShow->numOfRows = mnodeGetDnodesNum();
568
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
569
  pShow->pIter = NULL;
570

571
  mnodeDecUserRef(pUser);
S
slguan 已提交
572

573 574 575
  return 0;
}

576
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
577 578 579 580 581 582
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
  SDnodeObj *pDnode   = NULL;
  char      *pWrite;

  while (numOfRows < rows) {
583
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
584 585 586 587 588 589 590 591 592
    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 已提交
593
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols]);
594 595 596 597 598 599 600
    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
slguan 已提交
601
    *(int16_t *)pWrite = pDnode->totalVnodes;
602
    cols++;
S
slguan 已提交
603
    
604
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
605
    char* status = mnodeGetDnodeStatusStr(pDnode->status);
606
    STR_TO_VARSTR(pWrite, status);
607 608
    cols++;

609 610 611 612 613
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
    char* role = mnodeGetDnodeAlternativeRoleStr(pDnode->alternativeRole);
    STR_TO_VARSTR(pWrite, role);
    cols++;

S
slguan 已提交
614 615 616 617 618
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

 
619
    numOfRows++;
620
    mnodeDecDnodeRef(pDnode);
621 622 623 624 625 626
  }

  pShow->numOfReads += numOfRows;
  return numOfRows;
}

627
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
628 629 630 631
  uint32_t status = pDnode->moduleStatus & (1 << moduleType);
  return status > 0;
}

632
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
633 634
  int32_t cols = 0;

635
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
636 637
  if (pUser == NULL) return 0;

638
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
639
    mnodeDecUserRef(pUser);
640
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
641
  }
642 643 644

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
645 646 647 648 649 650
  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 已提交
651
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
652
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
J
jtao1735 已提交
653
  strcpy(pSchema[cols].name, "end point");
654 655 656
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
657
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
658
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
659
  strcpy(pSchema[cols].name, "module");
660 661 662
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
663
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
664
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
665
  strcpy(pSchema[cols].name, "status");
666 667 668 669 670 671 672 673 674 675 676
  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];
  }

677
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
678
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
679
  pShow->pIter = NULL;
680
  mnodeDecUserRef(pUser);
681 682 683 684

  return 0;
}

685
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
686 687
  int32_t numOfRows = 0;
  char *  pWrite;
688 689

  while (numOfRows < rows) {
S
slguan 已提交
690
    SDnodeObj *pDnode = NULL;
691
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
692 693 694
    if (pDnode == NULL) break;

    for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) {
S
slguan 已提交
695
      int32_t cols = 0;
696

S
slguan 已提交
697 698 699
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
700 701

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
J
jtao1735 已提交
702
      strncpy(pWrite, pDnode->dnodeEp, pShow->bytes[cols]-1);
703 704 705
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
slguan 已提交
706
      switch (moduleType) {
S
Shengliang Guan 已提交
707 708
        case TSDB_MOD_MNODE:
          strcpy(pWrite, "mnode");
S
slguan 已提交
709 710 711 712 713 714 715 716 717 718
          break;
        case TSDB_MOD_HTTP:
          strcpy(pWrite, "http");
          break;
        case TSDB_MOD_MONITOR:
          strcpy(pWrite, "monitor");
          break;
        default:
          strcpy(pWrite, "unknown");
      }
719 720 721
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
722
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
S
slguan 已提交
723
      strcpy(pWrite, enable ? "enable" : "disable");
724 725 726 727
      cols++;

      numOfRows++;
    }
S
slguan 已提交
728

729
    mnodeDecDnodeRef(pDnode);
730 731 732 733 734 735
  }

  pShow->numOfReads += numOfRows;
  return numOfRows;
}

736
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
737 738 739 740 741
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

742
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
743 744
  int32_t cols = 0;

745
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
746 747
  if (pUser == NULL) return 0;

748
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
749
    mnodeDecUserRef(pUser);
750
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
751
  }
752 753 754

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
755
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
756 757 758 759 760
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "config name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
761
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
762 763 764 765 766 767 768 769 770 771 772 773 774
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "config value");
  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 已提交
775
    SGlobalCfg *cfg = tsGlobalConfig + i;
776
    if (!mnodeCheckConfigShow(cfg)) continue;
777 778 779 780
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
781
  pShow->pIter = NULL;
782
  mnodeDecUserRef(pUser);
783 784 785 786

  return 0;
}

787
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
788 789 790
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
791
    SGlobalCfg *cfg = tsGlobalConfig + i;
792
    if (!mnodeCheckConfigShow(cfg)) continue;
793 794 795 796 797 798 799 800 801 802

    char *pWrite;
    int32_t   cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    snprintf(pWrite, TSDB_CFG_OPTION_LEN, "%s", cfg->option);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
slguan 已提交
803
      case TAOS_CFG_VTYPE_INT16:
804 805 806
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
807
      case TAOS_CFG_VTYPE_INT32:
808 809 810
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
811
      case TAOS_CFG_VTYPE_FLOAT:
812 813 814
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
815 816 817
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
818 819 820 821 822 823 824 825 826 827 828 829
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%s", (char *)cfg->ptr);
        numOfRows++;
        break;
      default:
        break;
    }
  }

  pShow->numOfReads += numOfRows;
  return numOfRows;
}

830
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
831
  int32_t cols = 0;
832
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
833
  if (pUser == NULL) return 0;
S
slguan 已提交
834
  
835
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
836
    mnodeDecUserRef(pUser);
837
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
838
  }
839 840 841 842 843 844 845 846 847

  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 已提交
848
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
849 850 851 852 853 854 855 856 857 858 859 860 861
  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 ) {
862
    pDnode = mnodeGetDnodeByEp(pShow->payload);
863
  } else {
864
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
865
    sdbFreeIter(pIter);
S
slguan 已提交
866
  }
867

S
slguan 已提交
868 869
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
870
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
871
  }
872 873

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
874
  pShow->pIter = pDnode;
875
  mnodeDecUserRef(pUser);
876 877 878 879

  return 0;
}

880
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
881 882 883 884 885 886 887
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
888
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
889
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
890
    void *pIter = NULL;
S
slguan 已提交
891 892
    SVgObj *pVgroup;
    while (1) {
893
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
894 895 896 897 898 899 900 901 902 903 904 905
      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;
906
          strcpy(pWrite, mnodeGetMnodeRoleStr(pVgid->role));
S
slguan 已提交
907
          cols++;
908 909
        }
      }
S
slguan 已提交
910

911
      mnodeDecVgroupRef(pVgroup);
912
    }
S
Shengliang Guan 已提交
913
    sdbFreeIter(pIter);
914 915 916
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
917

918 919
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
920 921
}

922
char* mnodeGetDnodeStatusStr(int32_t dnodeStatus) {
S
slguan 已提交
923
  switch (dnodeStatus) {
S
slguan 已提交
924 925 926 927
    case TAOS_DN_STATUS_OFFLINE:   return "offline";
    case TAOS_DN_STATUS_DROPPING:  return "dropping";
    case TAOS_DN_STATUS_BALANCING: return "balancing";
    case TAOS_DN_STATUS_READY:     return "ready";
S
slguan 已提交
928 929 930
    default:                       return "undefined";
  }
}
931 932 933 934 935 936 937 938 939 940

static char* mnodeGetDnodeAlternativeRoleStr(int32_t alternativeRole) {
  switch (alternativeRole) {
    case TAOS_DN_ALTERNATIVE_ROLE_ANY: return "any";
    case TAOS_DN_ALTERNATIVE_ROLE_MNODE: return "mnode";
    case TAOS_DN_ALTERNATIVE_ROLE_VNODE: return "vnode";
    default:return "any";
  }
}