mnodeDnode.c 39.5 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 20
#include "tglobal.h"
#include "tconfig.h"
S
slguan 已提交
21 22
#include "tutil.h"
#include "tsocket.h"
S
Shengliang Guan 已提交
23
#include "tbn.h"
S
slguan 已提交
24
#include "tsync.h"
25
#include "tdataformat.h"
26
#include "mnode.h"
S
slguan 已提交
27
#include "dnode.h"
S
Shengliang Guan 已提交
28 29 30
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeDnode.h"
31
#include "mnodeDb.h"
S
Shengliang Guan 已提交
32 33
#include "mnodeMnode.h"
#include "mnodeSdb.h"
34
#include "mnodeShow.h"
S
Shengliang Guan 已提交
35 36
#include "mnodeUser.h"
#include "mnodeVgroup.h"
37 38
#include "mnodeWrite.h"
#include "mnodePeer.h"
S
Shengliang Guan 已提交
39
#include "mnodeCluster.h"
S
slguan 已提交
40

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

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

52
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg);
53 54 55 56
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 已提交
57
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg);
58 59 60 61 62 63 64 65
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 已提交
66
static void    mnodeUpdateDnodeEps();
67

S
TD-1473  
Shengliang Guan 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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 已提交
90 91
static int32_t mnodeDnodeActionDestroy(SSdbRow *pRow) {
  tfree(pRow->pObj);
S
slguan 已提交
92 93
  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
94

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

103 104
  pDnode->customScore = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196 197 198 199 200 201 202
  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 已提交
203 204
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_VARIABLES, mnodeGetConfigMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_VARIABLES, mnodeRetrieveConfigs);
205 206 207 208
  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 已提交
209
  mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_DNODE, mnodeCancelGetNextDnode);
S
slguan 已提交
210
 
211
  mDebug("table:dnodes table is created");
S
slguan 已提交
212 213 214
  return 0;
}

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

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

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

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

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
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;
}

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

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

  return onlineDnodes;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
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;
  }
}

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

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

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

S
Shengliang Guan 已提交
308

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

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

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

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

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

333
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
334 335 336 337 338
  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 已提交
339
  SCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
340
  if (pCmCfgDnode->ep[0] == 0) {
341
    tstrncpy(pCmCfgDnode->ep, tsLocalEp, TSDB_EP_LEN);
342
  }
343

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

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

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

361
  if (strncasecmp(pCmCfgDnode->config, "balance", 7) == 0) {
S
Shengliang Guan 已提交
362 363 364 365 366 367 368 369
    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 已提交
370
    int32_t code = bnAlterDnode(pDnode, vnodeId, dnodeId);
S
Shengliang Guan 已提交
371 372
    mnodeDecDnodeRef(pDnode);
    return code;
373
  } else {
S
TD-1732  
Shengliang Guan 已提交
374
    SCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SCfgDnodeMsg));
375 376 377 378 379 380 381 382
    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 已提交
383
      .contLen = sizeof(SCfgDnodeMsg)
384 385 386 387
    };

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

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

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

  int64_t checkTime = 0;
S
TD-1473  
Shengliang Guan 已提交
433
  char    timestr[32] = "1970-01-01 00:00:00.00";
S
TD-1207  
Shengliang Guan 已提交
434
  (void)taosParseTime(timestr, &checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
S
TD-1473  
Shengliang Guan 已提交
435 436 437 438 439
  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 已提交
440
  }
H
Hui Li 已提交
441

442 443 444 445 446 447 448 449
  // 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 已提交
450

S
Shengliang Guan 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  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 已提交
468
  return 0;
H
Hui Li 已提交
469 470
}

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

S
TD-1762  
Shengliang Guan 已提交
478
static void mnodeGetDnodeEpsData(SDnodeEps *pEps, int32_t epsSize) {
S
TD-1762  
Shengliang Guan 已提交
479
  pthread_mutex_lock(&tsDnodeEpsMutex);
S
TD-1762  
Shengliang Guan 已提交
480 481 482
  if (epsSize == tsDnodeEpsSize) {
    memcpy(pEps, tsDnodeEps, tsDnodeEpsSize);
  }
S
TD-1762  
Shengliang Guan 已提交
483 484 485 486 487 488 489 490
  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 已提交
491
  free(tsDnodeEps);
S
TD-1762  
Shengliang Guan 已提交
492 493 494 495 496 497 498 499 500 501
  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 已提交
502 503 504 505
    if (dnodesNum >= totalDnodes) {
      mnodeCancelGetNextDnode(pIter);
      break;
    }
S
TD-1762  
Shengliang Guan 已提交
506 507 508 509 510 511 512 513 514 515 516 517

    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);
}

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

S
slguan 已提交
526 527
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
S
TD-1473  
Shengliang Guan 已提交
528 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;
    }
    mError("dnode:%d, status msg version:%d not equal with cluster:%d", pStatus->dnodeId, version, tsVersion);
533
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
534 535
  }

S
slguan 已提交
536
  if (pStatus->dnodeId == 0) {
537
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
538
    if (pDnode == NULL) {
539
      mDebug("dnode %s not created", pStatus->dnodeEp);
540
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
541 542
    }
  } else {
543
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
544
    if (pDnode == NULL) {
S
TD-1473  
Shengliang Guan 已提交
545 546 547 548 549
      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);
550
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
551 552
    }
  }
J
jtao1735 已提交
553

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

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

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

S
TD-1732  
Shengliang Guan 已提交
581
  SStatusRsp *pRsp = rpcMallocCont(contLen);
582 583 584 585 586 587 588 589
  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);
590
  tstrncpy(pRsp->dnodeCfg.clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN);
S
TD-1732  
Shengliang Guan 已提交
591
  SVgroupAccess *pAccess = (SVgroupAccess *)((char *)pRsp + sizeof(SStatusRsp));
S
TD-1762  
Shengliang Guan 已提交
592
  
593
  for (int32_t j = 0; j < openVnodes; ++j) {
594
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
595
    pVload->vgId = htonl(pVload->vgId);
S
TD-2270  
Shengliang Guan 已提交
596 597
    pVload->dbCfgVersion = htonl(pVload->dbCfgVersion);
    pVload->vgCfgVersion = htonl(pVload->vgCfgVersion);
S
TD-2429  
Shengliang Guan 已提交
598
    pVload->vnodeVersion = htobe64(pVload->vnodeVersion);
S
slguan 已提交
599

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

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

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

633
  int32_t numOfMnodes = mnodeGetMnodesNum();
634 635
  if (numOfMnodes < tsNumOfMnodes && numOfMnodes < mnodeGetOnlineDnodesNum()
      && bnDnodeCanCreateMnode(pDnode)) {
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"
};