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

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

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

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static int32_t mnodeCreateDnode(char *ep);
static int32_t mnodeProcessCreateDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessDropDnodeMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) ;
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *rpcMsg);
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn);

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

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

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
74 75
}

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

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

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

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

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

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

116
static int32_t mnodeDnodeActionRestored() {
S
slguan 已提交
117
  int32_t numOfRows = sdbGetNumOfRows(tsDnodeSdb);
S
slguan 已提交
118
  if (numOfRows <= 0 && dnodeIsFirstDeploy()) {
119 120 121 122
    mnodeCreateDnode(tsLocalEp);
    SDnodeObj *pDnode = mnodeGetDnodeByEp(tsLocalEp);
    mnodeAddMnode(pDnode->dnodeId);
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
123 124
  }

S
slguan 已提交
125
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
126 127
}

128
int32_t mnodeInitDnodes() {
S
slguan 已提交
129 130 131 132 133 134
  SDnodeObj tObj;
  tsDnodeUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj;

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

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

154 155 156 157 158 159 160 161 162 163 164 165 166
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CREATE_DNODE, mnodeProcessCreateDnodeMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_DROP_DNODE, mnodeProcessDropDnodeMsg); 
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CONFIG_DNODE, mnodeProcessCfgDnodeMsg);
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP, mnodeProcessCfgDnodeMsgRsp);
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_STATUS, mnodeProcessDnodeStatusMsg);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_MODULE, mnodeGetModuleMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_MODULE, mnodeRetrieveModules);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_CONFIGS, mnodeGetConfigMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_CONFIGS, mnodeRetrieveConfigs);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_VNODES, mnodeGetVnodeMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_VNODES, mnodeRetrieveVnodes);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_DNODE, mnodeGetDnodeMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_DNODE, mnodeRetrieveDnodes);
S
slguan 已提交
167
 
S
slguan 已提交
168
  mTrace("table:dnodes table is created");
S
slguan 已提交
169 170 171 172 173 174 175
  return 0;
}

void mgmtCleanupDnodes() {
  sdbCloseTable(tsDnodeSdb);
}

176
void *mnodeGetNextDnode(void *pIter, SDnodeObj **pDnode) { 
S
Shengliang Guan 已提交
177
  return sdbFetchRow(tsDnodeSdb, pIter, (void **)pDnode); 
S
slguan 已提交
178 179
}

180
int32_t mnodeGetDnodesNum() {
S
slguan 已提交
181 182 183
  return sdbGetNumOfRows(tsDnodeSdb);
}

184
int32_t mnodeGetOnlinDnodesNum(char *ep) {
185 186 187 188 189
  SDnodeObj *pDnode = NULL;
  void *     pIter = NULL;
  int32_t    onlineDnodes = 0;

  while (1) {
190
    pIter = mnodeGetNextDnode(pIter, &pDnode);
191 192
    if (pDnode == NULL) break;
    if (pDnode->status != TAOS_DN_STATUS_OFFLINE) onlineDnodes++;
193
    mnodeDecDnodeRef(pDnode);
194 195 196 197 198 199 200
  }

  sdbFreeIter(pIter);

  return onlineDnodes;
}

201
void *mnodeGetDnode(int32_t dnodeId) {
S
slguan 已提交
202 203 204
  return sdbGetRow(tsDnodeSdb, &dnodeId);
}

205
void *mnodeGetDnodeByEp(char *ep) {
S
slguan 已提交
206
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
207
  void *     pIter = NULL;
S
slguan 已提交
208 209

  while (1) {
210
    pIter = mnodeGetNextDnode(pIter, &pDnode);
S
slguan 已提交
211
    if (pDnode == NULL) break;
J
jtao1735 已提交
212
    if (strcmp(ep, pDnode->dnodeEp) == 0) {
S
Shengliang Guan 已提交
213
      sdbFreeIter(pIter);
S
slguan 已提交
214 215
      return pDnode;
    }
216
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
217 218
  }

S
Shengliang Guan 已提交
219 220
  sdbFreeIter(pIter);

S
slguan 已提交
221 222 223
  return NULL;
}

224
void mnodeIncDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
225 226 227
  sdbIncRef(tsDnodeSdb, pDnode);
}

228
void mnodeDecDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
229
  sdbDecRef(tsDnodeSdb, pDnode);
S
slguan 已提交
230 231
}

232
void mnodeUpdateDnode(SDnodeObj *pDnode) {
S
slguan 已提交
233
  SSdbOper oper = {
S
slguan 已提交
234 235
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
236
    .pObj = pDnode
S
slguan 已提交
237 238 239
  };

  sdbUpdateRow(&oper);
S
slguan 已提交
240 241
}

242
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
243
  SCMCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
244 245
  if (pCmCfgDnode->ep[0] == 0) {
    strcpy(pCmCfgDnode->ep, tsLocalEp);
S
slguan 已提交
246
  } else {
sangshuduo's avatar
sangshuduo 已提交
247
    // TODO temporary disabled for compiling: strcpy(pCmCfgDnode->ep, pCmCfgDnode->ep); 
S
slguan 已提交
248
  }
S
slguan 已提交
249

S
slguan 已提交
250
  if (strcmp(pMsg->pUser->user, "root") != 0) {
251
    return TSDB_CODE_NO_RIGHTS;
S
slguan 已提交
252 253
  }

254 255 256 257 258 259 260 261 262 263 264 265 266
  SRpcIpSet ipSet = mnodeGetIpSetFromIp(pCmCfgDnode->ep);
  SMDCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SMDCfgDnodeMsg));
  strcpy(pMdCfgDnode->ep, pCmCfgDnode->ep);
  strcpy(pMdCfgDnode->config, pCmCfgDnode->config);

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

268 269 270
  mPrint("dnode:%s, is configured by %s", pCmCfgDnode->ep, pMsg->pUser->user);

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
271
}
S
slguan 已提交
272

273
static void mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) {
guanshengliang's avatar
guanshengliang 已提交
274
  mPrint("cfg dnode rsp is received");
S
slguan 已提交
275 276
}

277
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
278
  SDMStatusMsg *pStatus = pMsg->rpcMsg.pCont;
S
slguan 已提交
279
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
280
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
281 282
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
[TD-17]  
slguan 已提交
283
  pStatus->numOfTotalVnodes = htons(pStatus->numOfTotalVnodes);
S
slguan 已提交
284

S
slguan 已提交
285 286 287
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
    mError("status msg version:%d not equal with mnode:%d", version, tsVersion);
288
    return TSDB_CODE_INVALID_MSG_VERSION;
S
slguan 已提交
289 290
  }

S
slguan 已提交
291 292
  SDnodeObj *pDnode = NULL;
  if (pStatus->dnodeId == 0) {
293
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
294
    if (pDnode == NULL) {
J
jtao1735 已提交
295
      mTrace("dnode %s not created", pStatus->dnodeEp);
296
      return TSDB_CODE_DNODE_NOT_EXIST;
S
slguan 已提交
297 298
    }
  } else {
299
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
300
    if (pDnode == NULL) {
J
jtao1735 已提交
301
      mError("dnode id:%d, %s not exist", pStatus->dnodeId, pStatus->dnodeEp);
302
      return TSDB_CODE_DNODE_NOT_EXIST;
S
slguan 已提交
303 304
    }
  }
J
jtao1735 已提交
305

S
[TD-17]  
slguan 已提交
306 307
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
308 309
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
310
  pDnode->totalVnodes      = pStatus->numOfTotalVnodes; 
S
slguan 已提交
311
  pDnode->moduleStatus     = pStatus->moduleStatus;
S
slguan 已提交
312
  pDnode->lastAccess       = tsAccessSquence;
313
  
S
slguan 已提交
314
  if (pStatus->dnodeId == 0) {
J
jtao1735 已提交
315
    mTrace("dnode:%d %s, first access", pDnode->dnodeId, pDnode->dnodeEp);
S
slguan 已提交
316
  } else {
S
Shengliang Guan 已提交
317
    //mTrace("dnode:%d, status received, access times %d", pDnode->dnodeId, pDnode->lastAccess);
S
slguan 已提交
318 319
  }
 
320 321
  int32_t openVnodes = htons(pStatus->openVnodes);
  for (int32_t j = 0; j < openVnodes; ++j) {
322
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
323
    pVload->vgId = htonl(pVload->vgId);
S
slguan 已提交
324
    pVload->cfgVersion = htonl(pVload->cfgVersion);
S
slguan 已提交
325

326
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
327
    if (pVgroup == NULL) {
328
      SRpcIpSet ipSet = mnodeGetIpSetFromIp(pDnode->dnodeEp);
S
slguan 已提交
329
      mPrint("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
330
      mnodeSendDropVnodeMsg(pVload->vgId, &ipSet, NULL);
331
    } else {
332 333
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
334 335 336
    }
  }

S
slguan 已提交
337
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
S
slguan 已提交
338
    mTrace("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
339
    pDnode->status = TAOS_DN_STATUS_READY;
340
    balanceUpdateMnode();
S
slguan 已提交
341
    balanceNotify();
S
slguan 已提交
342 343
  }

344
  mnodeDecDnodeRef(pDnode);
S
slguan 已提交
345

S
slguan 已提交
346
  int32_t contLen = sizeof(SDMStatusRsp) + TSDB_MAX_VNODES * sizeof(SDMVgroupAccess);
S
slguan 已提交
347 348
  SDMStatusRsp *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
349
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
350 351
  }

352
  mnodeGetMnodeInfos(&pRsp->mnodes);
S
slguan 已提交
353

S
slguan 已提交
354 355 356
  pRsp->dnodeCfg.dnodeId = htonl(pDnode->dnodeId);
  pRsp->dnodeCfg.moduleStatus = htonl((int32_t)pDnode->isMgmt);
  pRsp->dnodeCfg.numOfVnodes = 0;
S
slguan 已提交
357 358 359 360 361
  
  contLen = sizeof(SDMStatusRsp);

  //TODO: set vnode access
  
362 363
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
364

365
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
366
}
367

368
static int32_t mnodeCreateDnode(char *ep) {
S
slguan 已提交
369 370 371 372 373
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

374
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
375
  if (pDnode != NULL) {
376
    mnodeDecDnodeRef(pDnode);
J
jtao1735 已提交
377
    mError("dnode:%d is alredy exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
S
slguan 已提交
378 379 380 381 382 383 384
    return TSDB_CODE_DNODE_ALREADY_EXIST;
  }

  pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj));
  pDnode->createdTime = taosGetTimestampMs();
  pDnode->status = TAOS_DN_STATUS_OFFLINE; 
  pDnode->totalVnodes = TSDB_INVALID_VNODE_NUM; 
J
jtao1735 已提交
385 386
  strcpy(pDnode->dnodeEp, ep);
  taosGetFqdnPortFromEp(ep, pDnode->dnodeFqdn, &pDnode->dnodePort);
S
slguan 已提交
387

S
slguan 已提交
388
  SSdbOper oper = {
S
slguan 已提交
389 390 391 392 393 394 395 396
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
    .pObj = pDnode,
    .rowSize = sizeof(SDnodeObj)
  };

  int32_t code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
397
    int dnodeId = pDnode->dnodeId;
S
slguan 已提交
398
    tfree(pDnode);
S
slguan 已提交
399 400
    mError("failed to create dnode:%d, result:%s", dnodeId, tstrerror(code));
    return TSDB_CODE_SDB_ERROR;
S
slguan 已提交
401 402 403 404 405 406
  }

  mPrint("dnode:%d is created, result:%s", pDnode->dnodeId, tstrerror(code));
  return code;
}

407
int32_t mnodeDropDnode(SDnodeObj *pDnode) {
S
slguan 已提交
408
  SSdbOper oper = {
S
slguan 已提交
409 410 411 412 413 414 415 416 417 418
    .type = SDB_OPER_GLOBAL,
    .table = tsDnodeSdb,
    .pObj = pDnode
  };

  int32_t code = sdbDeleteRow(&oper); 
  if (code != TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_SDB_ERROR;
  }

419
  mLPrint("dnode:%d, is dropped from cluster, result:%s", pDnode->dnodeId, tstrerror(code));
S
slguan 已提交
420 421 422
  return code;
}

423 424
static int32_t mnodeDropDnodeByEp(char *ep) {
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
425
  if (pDnode == NULL) {
J
jtao1735 已提交
426
    mError("dnode:%s, is not exist", ep);
S
slguan 已提交
427
    return TSDB_CODE_DNODE_NOT_EXIST;
S
slguan 已提交
428 429
  }

430
  mnodeDecDnodeRef(pDnode);
J
jtao1735 已提交
431 432
  if (strcmp(pDnode->dnodeEp, dnodeGetMnodeMasterEp()) == 0) {
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
S
slguan 已提交
433 434 435
    return TSDB_CODE_NO_REMOVE_MASTER;
  }

S
add log  
slguan 已提交
436
  mPrint("dnode:%d, start to drop it", pDnode->dnodeId);
S
slguan 已提交
437
#ifndef _SYNC
438
  return mnodeDropDnode(pDnode);
S
slguan 已提交
439 440 441 442 443
#else
  return balanceDropDnode(pDnode);
#endif
}

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

S
slguan 已提交
447
  if (strcmp(pMsg->pUser->user, "root") != 0) {
448
    return TSDB_CODE_NO_RIGHTS;
S
slguan 已提交
449
  } else {
450 451 452 453
    int32_t code = mnodeCreateDnode(pCreate->ep);

    if (code == TSDB_CODE_SUCCESS) {
      SDnodeObj *pDnode = mnodeGetDnodeByEp(pCreate->ep);
J
jtao1735 已提交
454
      mLPrint("dnode:%d, %s is created by %s", pDnode->dnodeId, pCreate->ep, pMsg->pUser->user);
455
      mnodeDecDnodeRef(pDnode);
S
slguan 已提交
456
    } else {
457
      mError("failed to create dnode:%s, reason:%s", pCreate->ep, tstrerror(code));
S
slguan 已提交
458
    }
459 460

    return code;
S
slguan 已提交
461 462 463
  }
}

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

S
slguan 已提交
467
  if (strcmp(pMsg->pUser->user, "root") != 0) {
468
    return TSDB_CODE_NO_RIGHTS;
S
slguan 已提交
469
  } else {
470 471 472
    int32_t code = mnodeDropDnodeByEp(pDrop->ep);

    if (code == TSDB_CODE_SUCCESS) {
J
jtao1735 已提交
473
      mLPrint("dnode:%s is dropped by %s", pDrop->ep, pMsg->pUser->user);
S
slguan 已提交
474
    } else {
475
      mError("failed to drop dnode:%s, reason:%s", pDrop->ep, tstrerror(code));
S
slguan 已提交
476 477
    }

478 479
    return code;
  }
S
slguan 已提交
480 481
}

482 483
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
484 485
  if (pUser == NULL) return 0;

S
slguan 已提交
486
  if (strcmp(pUser->pAcct->user, "root") != 0) {
487
    mnodeDecUserRef(pUser);
S
slguan 已提交
488 489
    return TSDB_CODE_NO_RIGHTS;
  }
490 491 492 493 494 495 496 497 498 499

  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 已提交
500
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
501
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
502
  strcpy(pSchema[cols].name, "end_point");
503 504 505 506 507
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
508
  strcpy(pSchema[cols].name, "open_vnodes");
509 510 511 512 513
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
514
  strcpy(pSchema[cols].name, "total_vnodes");
515 516 517
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
518
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
519
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
520 521 522 523 524 525
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
526
  strcpy(pSchema[cols].name, "create_time");
527 528 529 530 531 532 533 534 535 536 537
  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];
  }

538
  pShow->numOfRows = mnodeGetDnodesNum();
539
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
540
  pShow->pIter = NULL;
541

542
  mnodeDecUserRef(pUser);
S
slguan 已提交
543

544 545 546
  return 0;
}

547
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
548 549 550 551 552 553
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
  SDnodeObj *pDnode   = NULL;
  char      *pWrite;

  while (numOfRows < rows) {
554
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
555 556 557 558 559 560 561 562 563
    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;
564
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols] - VARSTR_HEADER_SIZE);
565 566 567 568 569 570 571
    cols++;

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

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
slguan 已提交
572
    *(int16_t *)pWrite = pDnode->totalVnodes;
573
    cols++;
S
slguan 已提交
574
    
575
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
576
    
577
    char* status = mnodeGetDnodeStatusStr(pDnode->status);
578
    STR_TO_VARSTR(pWrite, status);
579 580
    cols++;

S
slguan 已提交
581 582 583 584 585
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

 
586
    numOfRows++;
587
    mnodeDecDnodeRef(pDnode);
588 589 590 591 592 593
  }

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

594
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
595 596 597 598
  uint32_t status = pDnode->moduleStatus & (1 << moduleType);
  return status > 0;
}

599
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
600 601
  int32_t cols = 0;

602
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
603 604
  if (pUser == NULL) return 0;

S
slguan 已提交
605
  if (strcmp(pUser->user, "root") != 0)  {
606
    mnodeDecUserRef(pUser);
S
slguan 已提交
607 608
    return TSDB_CODE_NO_RIGHTS;
  }
609 610 611

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
612 613 614 615 616 617
  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 已提交
618
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
619
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
J
jtao1735 已提交
620
  strcpy(pSchema[cols].name, "end point");
621 622 623
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
624
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
625
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
626
  strcpy(pSchema[cols].name, "module");
627 628 629
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
630
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
631
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
632
  strcpy(pSchema[cols].name, "status");
633 634 635 636 637 638 639 640 641 642 643
  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];
  }

644
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
645
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
646
  pShow->pIter = NULL;
647
  mnodeDecUserRef(pUser);
648 649 650 651

  return 0;
}

652
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
653 654
  int32_t numOfRows = 0;
  char *  pWrite;
655 656

  while (numOfRows < rows) {
S
slguan 已提交
657
    SDnodeObj *pDnode = NULL;
658
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
659 660 661
    if (pDnode == NULL) break;

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

S
slguan 已提交
664 665 666
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
667 668

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
J
jtao1735 已提交
669
      strncpy(pWrite, pDnode->dnodeEp, pShow->bytes[cols]-1);
670 671 672
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
slguan 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685
      switch (moduleType) {
        case TSDB_MOD_MGMT:
          strcpy(pWrite, "mgmt");
          break;
        case TSDB_MOD_HTTP:
          strcpy(pWrite, "http");
          break;
        case TSDB_MOD_MONITOR:
          strcpy(pWrite, "monitor");
          break;
        default:
          strcpy(pWrite, "unknown");
      }
686 687 688
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
689
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
S
slguan 已提交
690
      strcpy(pWrite, enable ? "enable" : "disable");
691 692 693 694
      cols++;

      numOfRows++;
    }
S
slguan 已提交
695

696
    mnodeDecDnodeRef(pDnode);
697 698 699 700 701 702
  }

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

703
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
704 705 706 707 708
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

709
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
710 711
  int32_t cols = 0;

712
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
713 714
  if (pUser == NULL) return 0;

S
slguan 已提交
715
  if (strcmp(pUser->user, "root") != 0)  {
716
    mnodeDecUserRef(pUser);
S
slguan 已提交
717 718
    return TSDB_CODE_NO_RIGHTS;
  }
719 720 721

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
722
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
723 724 725 726 727
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "config name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
728
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
729 730 731 732 733 734 735 736 737 738 739 740 741
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "config value");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

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

  pShow->numOfRows = 0;
  for (int32_t i = tsGlobalConfigNum - 1; i >= 0; --i) {
S
slguan 已提交
742
    SGlobalCfg *cfg = tsGlobalConfig + i;
743
    if (!mnodeCheckConfigShow(cfg)) continue;
744 745 746 747
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
748
  pShow->pIter = NULL;
749
  mnodeDecUserRef(pUser);
750 751 752 753

  return 0;
}

754
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
755 756 757
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
758
    SGlobalCfg *cfg = tsGlobalConfig + i;
759
    if (!mnodeCheckConfigShow(cfg)) continue;
760 761 762 763 764 765 766 767 768 769

    char *pWrite;
    int32_t   cols = 0;

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

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
slguan 已提交
770
      case TAOS_CFG_VTYPE_INT16:
771 772 773
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
774
      case TAOS_CFG_VTYPE_INT32:
775 776 777
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
778
      case TAOS_CFG_VTYPE_FLOAT:
779 780 781
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        numOfRows++;
        break;
S
slguan 已提交
782 783 784
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
785 786 787 788 789 790 791 792 793 794 795 796
        snprintf(pWrite, TSDB_CFG_VALUE_LEN, "%s", (char *)cfg->ptr);
        numOfRows++;
        break;
      default:
        break;
    }
  }

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

797
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
798
  int32_t cols = 0;
799
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
800
  if (pUser == NULL) return 0;
S
slguan 已提交
801 802
  
  if (strcmp(pUser->user, "root") != 0)  {
803
    mnodeDecUserRef(pUser);
S
slguan 已提交
804 805
    return TSDB_CODE_NO_RIGHTS;
  }
806 807 808 809 810 811 812 813 814

  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 已提交
815
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
816 817 818 819 820 821 822 823 824 825 826 827 828
  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 ) {
829
    pDnode = mnodeGetDnodeByEp(pShow->payload);
830
  } else {
831
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
832
    sdbFreeIter(pIter);
S
slguan 已提交
833
  }
834

S
slguan 已提交
835 836
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
837
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
838
  }
839 840

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
841
  pShow->pIter = pDnode;
842
  mnodeDecUserRef(pUser);
843 844 845 846

  return 0;
}

847
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
848 849 850 851 852 853 854
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
855
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
856
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
857
    void *pIter = NULL;
S
slguan 已提交
858 859
    SVgObj *pVgroup;
    while (1) {
860
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
861 862 863 864 865 866 867 868 869 870 871 872
      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;
873
          strcpy(pWrite, mnodeGetMnodeRoleStr(pVgid->role));
S
slguan 已提交
874
          cols++;
875 876
        }
      }
S
slguan 已提交
877

878
      mnodeDecVgroupRef(pVgroup);
879
    }
S
Shengliang Guan 已提交
880
    sdbFreeIter(pIter);
881 882 883
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
884

885 886
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
887 888
}

889
char* mnodeGetDnodeStatusStr(int32_t dnodeStatus) {
S
slguan 已提交
890
  switch (dnodeStatus) {
S
slguan 已提交
891 892 893 894
    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 已提交
895 896 897
    default:                       return "undefined";
  }
}