mnodeDnode.c 33.9 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
#include "tutil.h"
#include "tsocket.h"
S
slguan 已提交
24
#include "tbalance.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 32 33
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeDnode.h"
#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

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
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 已提交
52
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg);
53 54 55 56 57 58 59 60
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
Shengliang Guan 已提交
64
  taosTFree(pOper->pObj);
S
slguan 已提交
65 66
  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
Shengliang Guan 已提交
72
    pDnode->lastAccess = tsAccessSquence;
S
slguan 已提交
73
  }
S
slguan 已提交
74

75
  mInfo("dnode:%d, fqdn:%s ep:%s port:%d, do insert action", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodeEp, pDnode->dnodePort);
S
slguan 已提交
76
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
77 78
}

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

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

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

S
slguan 已提交
101 102 103
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

S
slguan 已提交
132
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
133 134
}

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

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

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

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

S
Shengliang Guan 已提交
179
void mnodeCleanupDnodes() {
S
slguan 已提交
180
  sdbCloseTable(tsDnodeSdb);
181
  tsDnodeSdb = NULL;
S
slguan 已提交
182 183
}

184
void *mnodeGetNextDnode(void *pIter, SDnodeObj **pDnode) { 
S
Shengliang Guan 已提交
185
  return sdbFetchRow(tsDnodeSdb, pIter, (void **)pDnode); 
S
slguan 已提交
186 187
}

188
int32_t mnodeGetDnodesNum() {
S
slguan 已提交
189 190 191
  return sdbGetNumOfRows(tsDnodeSdb);
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
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);
  }

  sdbFreeIter(pIter);

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

212
int32_t mnodeGetOnlineDnodesNum() {
213 214 215 216 217
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    onlineDnodes = 0;

  while (1) {
218
    pIter = mnodeGetNextDnode(pIter, &pDnode);
219 220
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) onlineDnodes++;
221
    mnodeDecDnodeRef(pDnode);
222 223 224 225 226 227 228
  }

  sdbFreeIter(pIter);

  return onlineDnodes;
}

229
void *mnodeGetDnode(int32_t dnodeId) {
S
slguan 已提交
230 231 232
  return sdbGetRow(tsDnodeSdb, &dnodeId);
}

233
void *mnodeGetDnodeByEp(char *ep) {
S
slguan 已提交
234
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
235
  void *     pIter = NULL;
S
slguan 已提交
236 237

  while (1) {
238
    pIter = mnodeGetNextDnode(pIter, &pDnode);
S
slguan 已提交
239
    if (pDnode == NULL) break;
J
jtao1735 已提交
240
    if (strcmp(ep, pDnode->dnodeEp) == 0) {
S
Shengliang Guan 已提交
241
      sdbFreeIter(pIter);
S
slguan 已提交
242 243
      return pDnode;
    }
244
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
245 246
  }

S
Shengliang Guan 已提交
247 248
  sdbFreeIter(pIter);

S
slguan 已提交
249 250 251
  return NULL;
}

252
void mnodeIncDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
253 254 255
  sdbIncRef(tsDnodeSdb, pDnode);
}

256
void mnodeDecDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
257
  sdbDecRef(tsDnodeSdb, pDnode);
S
slguan 已提交
258 259
}

260
void mnodeUpdateDnode(SDnodeObj *pDnode) {
S
slguan 已提交
261
  SSdbOper oper = {
S
slguan 已提交
262 263
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
264
    .pObj = pDnode
S
slguan 已提交
265 266
  };

S
Shengliang Guan 已提交
267 268
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
269 270
    mError("dnodeId:%d, failed update", pDnode->dnodeId);
  }
S
slguan 已提交
271 272
}

273
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
274 275 276 277 278
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
    mError("failed to cfg dnode, no rights");
    return TSDB_CODE_MND_NO_RIGHTS;
  }
  
S
Shengliang Guan 已提交
279
  SCMCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
280
  if (pCmCfgDnode->ep[0] == 0) {
281
    tstrncpy(pCmCfgDnode->ep, tsLocalEp, TSDB_EP_LEN);
282
  }
283

284 285 286
  SDnodeObj *pDnode = mnodeGetDnodeByEp(pCmCfgDnode->ep);
  if (pDnode == NULL) {
    int32_t dnodeId = strtol(pCmCfgDnode->ep, NULL, 10);
287
    if (dnodeId <= 0 || dnodeId > 65536) {
288
      mError("failed to cfg dnode, invalid dnodeEp:%s", pCmCfgDnode->ep);
289 290
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
291

292
    pDnode = mnodeGetDnode(dnodeId);
293 294 295 296
    if (pDnode == NULL) {
      mError("failed to cfg dnode, invalid dnodeId:%d", dnodeId);
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
297 298
  }

299
  SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
300

301
  if (strncasecmp(pCmCfgDnode->config, "balance", 7) == 0) {
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309 310 311 312
    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;
    }

    int32_t code = balanceAlterDnode(pDnode, vnodeId, dnodeId);
    mnodeDecDnodeRef(pDnode);
    return code;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  } else {
    SMDCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SMDCfgDnodeMsg));
    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,
      .contLen = sizeof(SMDCfgDnodeMsg)
    };

    mInfo("dnode:%s, is configured by %s", pCmCfgDnode->ep, pMsg->pUser->user);
    dnodeSendMsgToDnode(&epSet, &rpcMdCfgDnodeMsg);
S
Shengliang Guan 已提交
328
    mnodeDecDnodeRef(pDnode);
329 330
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
331
}
S
slguan 已提交
332

333
static void mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) {
334
  mInfo("cfg dnode rsp is received");
S
slguan 已提交
335 336
}

H
Hui Li 已提交
337
static bool mnodeCheckClusterCfgPara(const SClusterCfg *clusterCfg) {
H
Hui Li 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  if (clusterCfg->numOfMnodes != htonl(tsNumOfMnodes)) {
    mError("\"numOfMnodes\"[%d - %d] cfg parameters inconsistent", clusterCfg->numOfMnodes, htonl(tsNumOfMnodes));
    return false;
  }  
  if (clusterCfg->enableBalance      != htonl(tsEnableBalance)) {
    mError("\"balance\"[%d - %d] cfg parameters inconsistent", clusterCfg->enableBalance, htonl(tsEnableBalance));
    return false;
  }
  if (clusterCfg->mnodeEqualVnodeNum != htonl(tsMnodeEqualVnodeNum)) {
    mError("\"mnodeEqualVnodeNum\"[%d - %d] cfg parameters inconsistent", clusterCfg->mnodeEqualVnodeNum, htonl(tsMnodeEqualVnodeNum));
    return false;
  }
  if (clusterCfg->offlineThreshold   != htonl(tsOfflineThreshold)) {
    mError("\"offlineThreshold\"[%d - %d] cfg parameters inconsistent", clusterCfg->offlineThreshold, htonl(tsOfflineThreshold));
    return false;
  }
  if (clusterCfg->statusInterval     != htonl(tsStatusInterval)) {
    mError("\"statusInterval\"[%d - %d] cfg parameters inconsistent", clusterCfg->statusInterval, htonl(tsStatusInterval));
    return false;
  }
  if (clusterCfg->maxtablesPerVnode  != htonl(tsMaxTablePerVnode)) {
    mError("\"maxTablesPerVnode\"[%d - %d] cfg parameters inconsistent", clusterCfg->maxtablesPerVnode, htonl(tsMaxTablePerVnode));
    return false;
  }
  if (clusterCfg->maxVgroupsPerDb    != htonl(tsMaxVgroupsPerDb)) {
    mError("\"maxVgroupsPerDb\"[%d - %d]  cfg parameters inconsistent", clusterCfg->maxVgroupsPerDb, htonl(tsMaxVgroupsPerDb));
    return false;
  }
  if (0 != strncasecmp(clusterCfg->arbitrator, tsArbitrator, strlen(tsArbitrator))) {
    mError("\"arbitrator\"[%s - %s]  cfg parameters inconsistent", clusterCfg->arbitrator, tsArbitrator);
    return false;
  }
H
Hui Li 已提交
370 371 372 373 374 375

  int64_t checkTime = 0;
  char timestr[32] = "1970-01-01 00:00:00.00";
  (void)taosParseTime(timestr, &checkTime, strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
  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);
H
Hui Li 已提交
376 377
    return false;
  }
H
Hui Li 已提交
378

H
Hui Li 已提交
379 380 381 382 383 384 385 386
  if (0 != strncasecmp(clusterCfg->locale, tsLocale, strlen(tsLocale))) {
    mError("\"locale\"[%s - %s]  cfg parameters inconsistent", clusterCfg->locale, tsLocale);
    return false;
  }
  if (0 != strncasecmp(clusterCfg->charset, tsCharset, strlen(tsCharset))) {
    mError("\"charset\"[%s - %s] cfg parameters inconsistent.", clusterCfg->charset, tsCharset);
    return false;
  }
H
Hui Li 已提交
387 388 389 390
    
  return true;
}

391
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
392
  SDMStatusMsg *pStatus = pMsg->rpcMsg.pCont;
S
slguan 已提交
393
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
394
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
395 396
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
Shengliang Guan 已提交
397
  
S
slguan 已提交
398 399 400
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
    mError("status msg version:%d not equal with mnode:%d", version, tsVersion);
401
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
402 403
  }

S
slguan 已提交
404 405
  SDnodeObj *pDnode = NULL;
  if (pStatus->dnodeId == 0) {
406
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
407
    if (pDnode == NULL) {
408
      mDebug("dnode %s not created", pStatus->dnodeEp);
409
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
410 411
    }
  } else {
412
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
413
    if (pDnode == NULL) {
J
jtao1735 已提交
414
      mError("dnode id:%d, %s not exist", pStatus->dnodeId, pStatus->dnodeEp);
415
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
416 417
    }
  }
J
jtao1735 已提交
418

S
[TD-17]  
slguan 已提交
419 420
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
421 422
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
423
  pDnode->moduleStatus     = pStatus->moduleStatus;
S
Shengliang Guan 已提交
424

S
slguan 已提交
425
  if (pStatus->dnodeId == 0) {
426
    mDebug("dnode:%d %s, first access, set clusterId %s", pDnode->dnodeId, pDnode->dnodeEp, mnodeGetClusterId());
S
slguan 已提交
427
  } else {
428 429
    if (strncmp(pStatus->clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN - 1) != 0) {
      mError("dnode:%d, input clusterId %s not match with exist %s", pDnode->dnodeId, pStatus->clusterId,
S
Shengliang Guan 已提交
430 431 432 433 434
             mnodeGetClusterId());
      return TSDB_CODE_MND_INVALID_CLUSTER_ID;
    } else {
      mTrace("dnode:%d, status received, access times %d", pDnode->dnodeId, pDnode->lastAccess);
    }
S
slguan 已提交
435
  }
S
Shengliang Guan 已提交
436

437
  int32_t openVnodes = htons(pStatus->openVnodes);
438 439 440 441 442 443 444 445 446 447
  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);
448
  tstrncpy(pRsp->dnodeCfg.clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN);
449 450
  SDMVgroupAccess *pAccess = (SDMVgroupAccess *)((char *)pRsp + sizeof(SDMStatusRsp));

451
  for (int32_t j = 0; j < openVnodes; ++j) {
452
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
453
    pVload->vgId = htonl(pVload->vgId);
S
slguan 已提交
454
    pVload->cfgVersion = htonl(pVload->cfgVersion);
S
slguan 已提交
455

456
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
457
    if (pVgroup == NULL) {
458
      SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
459
      mInfo("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
460
      mnodeSendDropVnodeMsg(pVload->vgId, &epSet, NULL);
461
    } else {
462
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
463 464
      pAccess->vgId = htonl(pVload->vgId);
      pAccess->accessState = pVgroup->accessState;
H
Haojun Liao 已提交
465
      pAccess++;
466
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
467 468 469
    }
  }

S
slguan 已提交
470
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
H
Hui Li 已提交
471
    // Verify whether the cluster parameters are consistent when status change from offline to ready
H
Hui Li 已提交
472
    bool ret = mnodeCheckClusterCfgPara(&(pStatus->clusterCfg));
H
Hui Li 已提交
473 474
    if (false == ret) {
      mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
475
      rpcFreeCont(pRsp);
S
scripts  
Shengliang Guan 已提交
476
      mError("dnode:%d, %s cluster cfg parameters inconsistent", pDnode->dnodeId, pStatus->dnodeEp);
H
Hui Li 已提交
477 478 479
      return TSDB_CODE_MND_CLUSTER_CFG_INCONSISTENT;
    }
    
480
    mDebug("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
481
    pDnode->status = TAOS_DN_STATUS_READY;
S
Shengliang Guan 已提交
482 483
    balanceSyncNotify();
    balanceAsyncNotify();
S
slguan 已提交
484 485
  }

S
Shengliang Guan 已提交
486 487 488 489
  if (openVnodes != pDnode->openVnodes) {
    mnodeCheckUnCreatedVgroup(pDnode, pStatus->load, openVnodes);
  }

H
Hui Li 已提交
490
  pDnode->lastAccess = tsAccessSquence;
S
Shengliang Guan 已提交
491 492 493 494

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

497 498
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
499

500
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
501
}
502

503
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg) {
S
slguan 已提交
504 505 506 507 508
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

S
Shengliang Guan 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521
  char dnodeEp[TSDB_EP_LEN] = {0};
  tstrncpy(dnodeEp, ep, TSDB_EP_LEN);
  strtrim(dnodeEp);

  char *temp = strchr(dnodeEp, ':');
  if (!temp) {
    int len = strlen(dnodeEp);
    if (dnodeEp[len - 1] == ';') dnodeEp[len - 1] = 0;
    len = strlen(dnodeEp);
    snprintf(dnodeEp + len, TSDB_EP_LEN - len, ":%d", tsServerPort);
  }
  ep = dnodeEp;

522
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
523
  if (pDnode != NULL) {
524
    mnodeDecDnodeRef(pDnode);
525
    mError("dnode:%d, already exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
526
    return TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
slguan 已提交
527 528 529 530 531
  }

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

S
slguan 已提交
535
  SSdbOper oper = {
S
slguan 已提交
536 537 538
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
    .pObj = pDnode,
539 540
    .rowSize = sizeof(SDnodeObj),
    .pMsg = pMsg
S
slguan 已提交
541 542 543
  };

  int32_t code = sdbInsertRow(&oper);
S
Shengliang Guan 已提交
544
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
slguan 已提交
545
    int dnodeId = pDnode->dnodeId;
S
Shengliang Guan 已提交
546
    taosTFree(pDnode);
S
Shengliang Guan 已提交
547
    mError("failed to create dnode:%d, reason:%s", dnodeId, tstrerror(code));
548
  } else {
S
Shengliang Guan 已提交
549
    mLInfo("dnode:%d is created", pDnode->dnodeId);
S
slguan 已提交
550 551 552 553 554
  }

  return code;
}

555
int32_t mnodeDropDnode(SDnodeObj *pDnode, void *pMsg) {
S
slguan 已提交
556
  SSdbOper oper = {
S
slguan 已提交
557 558
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
559 560
    .pObj = pDnode,
    .pMsg = pMsg
S
slguan 已提交
561 562
  };

563
  int32_t code = sdbDeleteRow(&oper);
S
Shengliang Guan 已提交
564 565 566 567
  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 已提交
568 569 570 571 572
  }

  return code;
}

573
static int32_t mnodeDropDnodeByEp(char *ep, SMnodeMsg *pMsg) {
574
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
575
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
576 577 578 579 580 581
    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 已提交
582 583
  }

H
Hui Li 已提交
584
  if (strcmp(pDnode->dnodeEp, mnodeGetMnodeMasterEp()) == 0) {
J
jtao1735 已提交
585
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
586
    mnodeDecDnodeRef(pDnode);
587
    return TSDB_CODE_MND_NO_REMOVE_MASTER;
S
slguan 已提交
588 589
  }

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

S
TD-1310  
Shengliang Guan 已提交
592 593 594
#ifndef _SYNC
  int32_t code = mnodeDropDnode(pDnode, pMsg);
#else
595
  int32_t code = balanceDropDnode(pDnode);
S
TD-1310  
Shengliang Guan 已提交
596
#endif  
597 598
  mnodeDecDnodeRef(pDnode);
  return code;
S
slguan 已提交
599 600
}

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

604
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
605
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
606
  } else {
607
    return mnodeCreateDnode(pCreate->ep, pMsg);
S
slguan 已提交
608 609 610
  }
}

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

614
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
615
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
616
  } else {
617
    return mnodeDropDnodeByEp(pDrop->ep, pMsg);
618
  }
S
slguan 已提交
619 620
}

621 622
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
623 624
  if (pUser == NULL) return 0;

625
  if (strcmp(pUser->pAcct->user, TSDB_DEFAULT_USER) != 0) {
626
    mnodeDecUserRef(pUser);
627
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
628
  }
629 630 631 632 633 634 635 636 637 638

  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 已提交
639
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
640
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
641
  strcpy(pSchema[cols].name, "end_point");
642 643 644 645 646
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
647
  strcpy(pSchema[cols].name, "vnodes");
648 649 650 651 652
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
653
  strcpy(pSchema[cols].name, "cores");
654 655 656
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

663 664
  pShow->bytes[cols] = 6 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
665
  strcpy(pSchema[cols].name, "role");
666 667 668
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
669 670
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
671
  strcpy(pSchema[cols].name, "create_time");
672 673 674 675 676 677 678 679 680 681 682
  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];
  }

683
  pShow->numOfRows = mnodeGetDnodesNum();
684
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
685
  pShow->pIter = NULL;
686

687
  mnodeDecUserRef(pUser);
S
slguan 已提交
688

689 690 691
  return 0;
}

692
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
693 694 695 696 697 698
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
  SDnodeObj *pDnode   = NULL;
  char      *pWrite;

  while (numOfRows < rows) {
699
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
700 701 702 703 704 705 706 707 708
    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 已提交
709
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols]);
710 711 712 713 714 715 716
    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 已提交
717
    *(int16_t *)pWrite = pDnode->numOfCores;
718
    cols++;
S
slguan 已提交
719
    
720
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
721
    char* status = mnodeGetDnodeStatusStr(pDnode->status);
722
    STR_TO_VARSTR(pWrite, status);
723 724
    cols++;

725 726 727 728 729
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
    char* role = mnodeGetDnodeAlternativeRoleStr(pDnode->alternativeRole);
    STR_TO_VARSTR(pWrite, role);
    cols++;

S
slguan 已提交
730 731 732 733 734
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

 
735
    numOfRows++;
736
    mnodeDecDnodeRef(pDnode);
737 738 739 740 741 742
  }

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

743
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
H
Haojun Liao 已提交
744
  uint32_t status = pDnode->moduleStatus & (1u << moduleType);
745 746 747
  return status > 0;
}

748
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
749 750
  int32_t cols = 0;

751
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
752 753
  if (pUser == NULL) return 0;

754
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
755
    mnodeDecUserRef(pUser);
756
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
757
  }
758 759 760

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
761 762 763 764 765 766
  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 已提交
767
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
768
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
769
  strcpy(pSchema[cols].name, "end_point");
770 771 772
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
773
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
774
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
775
  strcpy(pSchema[cols].name, "module");
776 777 778
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
779
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
780
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
781
  strcpy(pSchema[cols].name, "status");
782 783 784 785 786 787 788 789 790 791 792
  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];
  }

793
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
794
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
795
  pShow->pIter = NULL;
796
  mnodeDecUserRef(pUser);
797 798 799 800

  return 0;
}

801
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
802
  int32_t numOfRows = 0;
H
Haojun Liao 已提交
803 804 805

  char* pWrite;
  char* moduleName[5] = { "MNODE", "HTTP", "MONITOR", "MQTT", "UNKNOWN" };
806 807

  while (numOfRows < rows) {
S
slguan 已提交
808
    SDnodeObj *pDnode = NULL;
809
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
810 811 812
    if (pDnode == NULL) break;

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

S
slguan 已提交
815 816 817
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
818 819

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
820
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols] - 1);
821 822 823
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
824
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, moduleName[moduleType], pShow->bytes[cols]);
825 826 827
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
828
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
H
Haojun Liao 已提交
829 830 831

      char* v = enable? "enable":"disable";
      STR_TO_VARSTR(pWrite, v);
832 833 834 835
      cols++;

      numOfRows++;
    }
S
slguan 已提交
836

837
    mnodeDecDnodeRef(pDnode);
838 839 840 841 842 843
  }

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

844
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
845 846 847 848 849
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

850
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
851 852
  int32_t cols = 0;

853
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
854 855
  if (pUser == NULL) return 0;

856
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
857
    mnodeDecUserRef(pUser);
858
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
859
  }
860 861 862

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
863
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
864
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
865
  tstrncpy(pSchema[cols].name, "name", sizeof(pSchema[cols].name));
866 867 868
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
869
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
870
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
871
  tstrncpy(pSchema[cols].name, "value", sizeof(pSchema[cols].name));
872 873 874 875 876 877 878 879 880 881 882
  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 已提交
883
    SGlobalCfg *cfg = tsGlobalConfig + i;
884
    if (!mnodeCheckConfigShow(cfg)) continue;
885 886 887 888
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
889
  pShow->pIter = NULL;
890
  mnodeDecUserRef(pUser);
891 892 893 894

  return 0;
}

895
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
896 897 898
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
899
    SGlobalCfg *cfg = tsGlobalConfig + i;
900
    if (!mnodeCheckConfigShow(cfg)) continue;
901 902 903 904 905

    char *pWrite;
    int32_t   cols = 0;

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

908
    cols++;
909
    int32_t t = 0;
910 911 912

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
slguan 已提交
913
      case TAOS_CFG_VTYPE_INT16:
914 915
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
916 917
        numOfRows++;
        break;
S
slguan 已提交
918
      case TAOS_CFG_VTYPE_INT32:
919 920
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
921 922
        numOfRows++;
        break;
S
slguan 已提交
923
      case TAOS_CFG_VTYPE_FLOAT:
924 925
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        varDataSetLen(pWrite, t);
926 927
        numOfRows++;
        break;
S
slguan 已提交
928 929 930
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
931
        STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfg->ptr, TSDB_CFG_VALUE_LEN);
932 933 934 935 936 937 938 939 940 941 942
        numOfRows++;
        break;
      default:
        break;
    }
  }

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

943
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
944
  int32_t cols = 0;
945
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
946
  if (pUser == NULL) return 0;
S
slguan 已提交
947
  
948
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
949
    mnodeDecUserRef(pUser);
950
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
951
  }
952 953 954 955 956 957 958 959 960

  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 已提交
961
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
962 963 964 965 966 967 968 969 970 971 972 973 974
  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 ) {
975
    pDnode = mnodeGetDnodeByEp(pShow->payload);
976
  } else {
977
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
978
    sdbFreeIter(pIter);
S
slguan 已提交
979
  }
980

S
slguan 已提交
981 982
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
983
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
984
  }
985 986

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
987
  pShow->pIter = pDnode;
988
  mnodeDecUserRef(pUser);
989 990 991 992

  return 0;
}

993
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
994 995 996 997 998 999 1000
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
1001
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
1002
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
1003
    void *pIter = NULL;
S
slguan 已提交
1004 1005
    SVgObj *pVgroup;
    while (1) {
1006
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
      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;
1019
          strcpy(pWrite, mnodeGetMnodeRoleStr(pVgid->role));
S
slguan 已提交
1020
          cols++;
1021 1022
        }
      }
S
slguan 已提交
1023

1024
      mnodeDecVgroupRef(pVgroup);
1025
    }
S
Shengliang Guan 已提交
1026
    sdbFreeIter(pIter);
1027 1028 1029
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
1030

1031 1032
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
1033 1034
}

1035
char* mnodeGetDnodeStatusStr(int32_t dnodeStatus) {
S
slguan 已提交
1036
  switch (dnodeStatus) {
S
slguan 已提交
1037 1038 1039 1040
    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 已提交
1041 1042 1043
    default:                       return "undefined";
  }
}
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

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

S
TD-1310  
Shengliang Guan 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
#ifndef _SYNC

int32_t balanceInit() { return TSDB_CODE_SUCCESS; }
void    balanceCleanUp() {}
void    balanceAsyncNotify() {}
void    balanceSyncNotify() {}
void    balanceReset() {}
int32_t balanceAlterDnode(struct SDnodeObj *pDnode, int32_t vnodeId, int32_t dnodeId) { return TSDB_CODE_SYN_NOT_ENABLED; }

char* syncRole[] = {
  "offline",
  "unsynced",
  "syncing",
  "slave",
  "master"
};

int32_t balanceAllocVnodes(SVgObj *pVgroup) {
  void *     pIter = NULL;
  SDnodeObj *pDnode = NULL;
  SDnodeObj *pSelDnode = NULL;
  float      vnodeUsage = 1000.0;

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

    if (pDnode->numOfCores > 0 && pDnode->openVnodes < TSDB_MAX_VNODES) {
      float openVnodes = pDnode->openVnodes;
      if (pDnode->isMgmt) openVnodes += tsMnodeEqualVnodeNum;

      float usage = openVnodes / pDnode->numOfCores;
      if (usage <= vnodeUsage) {
        pSelDnode = pDnode;
        vnodeUsage = usage;
      }
    }
    mnodeDecDnodeRef(pDnode);
  }

  sdbFreeIter(pIter);

  if (pSelDnode == NULL) {
    mError("failed to alloc vnode to vgroup");
    return TSDB_CODE_MND_NO_ENOUGH_DNODES;
  }

  pVgroup->vnodeGid[0].dnodeId = pSelDnode->dnodeId;
  pVgroup->vnodeGid[0].pDnode = pSelDnode;

  mDebug("dnode:%d, alloc one vnode to vgroup, openVnodes:%d", pSelDnode->dnodeId, pSelDnode->openVnodes);
  return TSDB_CODE_SUCCESS;
}

#endif