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

#define _DEFAULT_SOURCE
17
#include "os.h"
S
slguan 已提交
18
#include "tgrant.h"
S
Shengliang Guan 已提交
19
#include "tbn.h"
S
slguan 已提交
20 21
#include "tglobal.h"
#include "tconfig.h"
S
slguan 已提交
22 23
#include "tutil.h"
#include "tsocket.h"
S
Shengliang Guan 已提交
24
#include "tbn.h"
S
slguan 已提交
25
#include "tsync.h"
26
#include "tdataformat.h"
27
#include "mnode.h"
S
slguan 已提交
28
#include "dnode.h"
S
Shengliang Guan 已提交
29 30 31 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;
S
TD-2264  
Shengliang Guan 已提交
42
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

229
int32_t mnodeGetDnodesNum() {
S
slguan 已提交
230 231 232
  return sdbGetNumOfRows(tsDnodeSdb);
}

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

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

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

  return onlineDnodes;
}

266
void *mnodeGetDnode(int32_t dnodeId) {
S
slguan 已提交
267 268 269
  return sdbGetRow(tsDnodeSdb, &dnodeId);
}

270
void *mnodeGetDnodeByEp(char *ep) {
S
slguan 已提交
271
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
272
  void *     pIter = NULL;
S
slguan 已提交
273 274

  while (1) {
275
    pIter = mnodeGetNextDnode(pIter, &pDnode);
S
slguan 已提交
276
    if (pDnode == NULL) break;
J
jtao1735 已提交
277
    if (strcmp(ep, pDnode->dnodeEp) == 0) {
S
Shengliang Guan 已提交
278
      mnodeCancelGetNextDnode(pIter);
S
slguan 已提交
279 280
      return pDnode;
    }
281
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
282 283
  }

S
Shengliang Guan 已提交
284

S
slguan 已提交
285 286 287
  return NULL;
}

288
void mnodeIncDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
289 290 291
  sdbIncRef(tsDnodeSdb, pDnode);
}

292
void mnodeDecDnodeRef(SDnodeObj *pDnode) {
S
slguan 已提交
293
  sdbDecRef(tsDnodeSdb, pDnode);
S
slguan 已提交
294 295
}

296
void mnodeUpdateDnode(SDnodeObj *pDnode) {
S
TD-2046  
Shengliang Guan 已提交
297
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
298 299
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
300
    .pObj   = pDnode
S
slguan 已提交
301 302
  };

S
TD-2046  
Shengliang Guan 已提交
303
  int32_t code = sdbUpdateRow(&row);
S
Shengliang Guan 已提交
304
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
TD-2415  
Shengliang Guan 已提交
305
    mError("dnode:%d, failed update", pDnode->dnodeId);
306
  }
S
slguan 已提交
307 308
}

309
static int32_t mnodeProcessCfgDnodeMsg(SMnodeMsg *pMsg) {
310 311 312 313 314
  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 已提交
315
  SCfgDnodeMsg *pCmCfgDnode = pMsg->rpcMsg.pCont;
J
jtao1735 已提交
316
  if (pCmCfgDnode->ep[0] == 0) {
317
    tstrncpy(pCmCfgDnode->ep, tsLocalEp, TSDB_EP_LEN);
318
  }
319

320 321 322
  SDnodeObj *pDnode = mnodeGetDnodeByEp(pCmCfgDnode->ep);
  if (pDnode == NULL) {
    int32_t dnodeId = strtol(pCmCfgDnode->ep, NULL, 10);
323
    if (dnodeId <= 0 || dnodeId > 65536) {
324
      mError("failed to cfg dnode, invalid dnodeEp:%s", pCmCfgDnode->ep);
325 326
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
327

328
    pDnode = mnodeGetDnode(dnodeId);
329 330 331 332
    if (pDnode == NULL) {
      mError("failed to cfg dnode, invalid dnodeId:%d", dnodeId);
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
    }
S
slguan 已提交
333 334
  }

335
  SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
336

337
  if (strncasecmp(pCmCfgDnode->config, "balance", 7) == 0) {
S
Shengliang Guan 已提交
338 339 340 341 342 343 344 345
    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 已提交
346
    int32_t code = bnAlterDnode(pDnode, vnodeId, dnodeId);
S
Shengliang Guan 已提交
347 348
    mnodeDecDnodeRef(pDnode);
    return code;
349
  } else {
S
TD-1732  
Shengliang Guan 已提交
350
    SCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SCfgDnodeMsg));
351 352 353 354 355 356 357 358
    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 已提交
359
      .contLen = sizeof(SCfgDnodeMsg)
360 361 362 363
    };

    mInfo("dnode:%s, is configured by %s", pCmCfgDnode->ep, pMsg->pUser->user);
    dnodeSendMsgToDnode(&epSet, &rpcMdCfgDnodeMsg);
S
Shengliang Guan 已提交
364
    mnodeDecDnodeRef(pDnode);
365 366
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
367
}
S
slguan 已提交
368

369
static void mnodeProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) {
370
  mInfo("cfg dnode rsp is received");
S
slguan 已提交
371 372
}

S
TD-1473  
Shengliang Guan 已提交
373
static int32_t mnodeCheckClusterCfgPara(const SClusterCfg *clusterCfg) {
H
Hui Li 已提交
374 375
  if (clusterCfg->numOfMnodes != htonl(tsNumOfMnodes)) {
    mError("\"numOfMnodes\"[%d - %d] cfg parameters inconsistent", clusterCfg->numOfMnodes, htonl(tsNumOfMnodes));
S
TD-1473  
Shengliang Guan 已提交
376 377
    return TAOS_DN_OFF_NUM_OF_MNODES_NOT_MATCH;
  }
H
Hui Li 已提交
378
  if (clusterCfg->mnodeEqualVnodeNum != htonl(tsMnodeEqualVnodeNum)) {
S
TD-1473  
Shengliang Guan 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    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 已提交
402 403 404
  }
  if (0 != strncasecmp(clusterCfg->arbitrator, tsArbitrator, strlen(tsArbitrator))) {
    mError("\"arbitrator\"[%s - %s]  cfg parameters inconsistent", clusterCfg->arbitrator, tsArbitrator);
S
TD-1473  
Shengliang Guan 已提交
405
    return TAOS_DN_OFF_ARBITRATOR_NOT_MATCH;
H
Hui Li 已提交
406
  }
H
Hui Li 已提交
407 408

  int64_t checkTime = 0;
S
TD-1473  
Shengliang Guan 已提交
409
  char    timestr[32] = "1970-01-01 00:00:00.00";
H
Hui Li 已提交
410
  (void)taosParseTime(timestr, &checkTime, strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
S
TD-1473  
Shengliang Guan 已提交
411 412 413 414 415
  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 已提交
416
  }
H
Hui Li 已提交
417

H
Hui Li 已提交
418 419
  if (0 != strncasecmp(clusterCfg->locale, tsLocale, strlen(tsLocale))) {
    mError("\"locale\"[%s - %s]  cfg parameters inconsistent", clusterCfg->locale, tsLocale);
S
TD-1473  
Shengliang Guan 已提交
420
    return TAOS_DN_OFF_LOCALE_NOT_MATCH;
H
Hui Li 已提交
421 422 423
  }
  if (0 != strncasecmp(clusterCfg->charset, tsCharset, strlen(tsCharset))) {
    mError("\"charset\"[%s - %s] cfg parameters inconsistent.", clusterCfg->charset, tsCharset);
S
TD-1473  
Shengliang Guan 已提交
424
    return TAOS_DN_OFF_CHARSET_NOT_MATCH;
H
Hui Li 已提交
425
  }
S
TD-1473  
Shengliang Guan 已提交
426

S
Shengliang Guan 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  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 已提交
444
  return 0;
H
Hui Li 已提交
445 446
}

S
TD-1762  
Shengliang Guan 已提交
447 448 449 450 451 452 453
static int32_t mnodeGetDnodeEpsSize() {
  pthread_mutex_lock(&tsDnodeEpsMutex);
  int32_t size = tsDnodeEpsSize;
  pthread_mutex_unlock(&tsDnodeEpsMutex);
  return size;
}

S
TD-1762  
Shengliang Guan 已提交
454
static void mnodeGetDnodeEpsData(SDnodeEps *pEps, int32_t epsSize) {
S
TD-1762  
Shengliang Guan 已提交
455
  pthread_mutex_lock(&tsDnodeEpsMutex);
S
TD-1762  
Shengliang Guan 已提交
456 457 458
  if (epsSize == tsDnodeEpsSize) {
    memcpy(pEps, tsDnodeEps, tsDnodeEpsSize);
  }
S
TD-1762  
Shengliang Guan 已提交
459 460 461 462 463 464 465 466
  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 已提交
467
  free(tsDnodeEps);
S
TD-1762  
Shengliang Guan 已提交
468 469 470 471 472 473 474 475 476 477
  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 已提交
478 479 480 481
    if (dnodesNum >= totalDnodes) {
      mnodeCancelGetNextDnode(pIter);
      break;
    }
S
TD-1762  
Shengliang Guan 已提交
482 483 484 485 486 487 488 489 490 491 492 493

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

494
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
TD-1473  
Shengliang Guan 已提交
495
  SDnodeObj *pDnode     = NULL;
S
TD-1732  
Shengliang Guan 已提交
496
  SStatusMsg *pStatus   = pMsg->rpcMsg.pCont;
S
slguan 已提交
497
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
498
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
499 500
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
TD-1473  
Shengliang Guan 已提交
501

S
slguan 已提交
502 503
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
S
TD-1473  
Shengliang Guan 已提交
504 505 506 507 508
    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);
509
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
510 511
  }

S
slguan 已提交
512
  if (pStatus->dnodeId == 0) {
513
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
514
    if (pDnode == NULL) {
515
      mDebug("dnode %s not created", pStatus->dnodeEp);
516
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
517 518
    }
  } else {
519
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
520
    if (pDnode == NULL) {
S
TD-1473  
Shengliang Guan 已提交
521 522 523 524 525
      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);
526
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
527 528
    }
  }
J
jtao1735 已提交
529

S
[TD-17]  
slguan 已提交
530 531
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
532 533
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
534
  pDnode->moduleStatus     = pStatus->moduleStatus;
S
Shengliang Guan 已提交
535

S
slguan 已提交
536
  if (pStatus->dnodeId == 0) {
537
    mDebug("dnode:%d %s, first access, set clusterId %s", pDnode->dnodeId, pDnode->dnodeEp, mnodeGetClusterId());
S
slguan 已提交
538
  } else {
539
    if (strncmp(pStatus->clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN - 1) != 0) {
S
TD-1473  
Shengliang Guan 已提交
540 541 542
      if (pDnode != NULL && pDnode->status != TAOS_DN_STATUS_READY) {
        pDnode->offlineReason = TAOS_DN_OFF_CLUSTER_ID_NOT_MATCH;
      }
543
      mError("dnode:%d, input clusterId %s not match with exist %s", pDnode->dnodeId, pStatus->clusterId,
S
Shengliang Guan 已提交
544 545 546
             mnodeGetClusterId());
      return TSDB_CODE_MND_INVALID_CLUSTER_ID;
    } else {
S
TD-1725  
Shengliang Guan 已提交
547 548
      mTrace("dnode:%d, status received, access times %d openVnodes:%d:%d", pDnode->dnodeId, pDnode->lastAccess,
             htons(pStatus->openVnodes), pDnode->openVnodes);
S
Shengliang Guan 已提交
549
    }
S
slguan 已提交
550
  }
S
Shengliang Guan 已提交
551

552
  int32_t openVnodes = htons(pStatus->openVnodes);
S
TD-1762  
Shengliang Guan 已提交
553
  int32_t epsSize = mnodeGetDnodeEpsSize();
S
TD-1732  
Shengliang Guan 已提交
554 555
  int32_t vgAccessSize = openVnodes * sizeof(SVgroupAccess);
  int32_t contLen = sizeof(SStatusRsp) + vgAccessSize + epsSize;
S
TD-1762  
Shengliang Guan 已提交
556

S
TD-1732  
Shengliang Guan 已提交
557
  SStatusRsp *pRsp = rpcMallocCont(contLen);
558 559 560 561 562 563 564 565
  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);
566
  tstrncpy(pRsp->dnodeCfg.clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN);
S
TD-1732  
Shengliang Guan 已提交
567
  SVgroupAccess *pAccess = (SVgroupAccess *)((char *)pRsp + sizeof(SStatusRsp));
S
TD-1762  
Shengliang Guan 已提交
568
  
569
  for (int32_t j = 0; j < openVnodes; ++j) {
570
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
571
    pVload->vgId = htonl(pVload->vgId);
S
TD-2270  
Shengliang Guan 已提交
572 573
    pVload->dbCfgVersion = htonl(pVload->dbCfgVersion);
    pVload->vgCfgVersion = htonl(pVload->vgCfgVersion);
S
slguan 已提交
574

575
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
576
    if (pVgroup == NULL) {
577
      SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
578
      mInfo("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
579
      mnodeSendDropVnodeMsg(pVload->vgId, &epSet, NULL);
580
    } else {
581
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
582 583
      pAccess->vgId = htonl(pVload->vgId);
      pAccess->accessState = pVgroup->accessState;
H
Haojun Liao 已提交
584
      pAccess++;
585
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
586 587 588
    }
  }

S
slguan 已提交
589
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
H
Hui Li 已提交
590
    // Verify whether the cluster parameters are consistent when status change from offline to ready
S
TD-1473  
Shengliang Guan 已提交
591 592 593
    int32_t ret = mnodeCheckClusterCfgPara(&(pStatus->clusterCfg));
    if (0 != ret) {
      pDnode->offlineReason = ret;
H
Hui Li 已提交
594
      mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
595
      rpcFreeCont(pRsp);
S
TD-1473  
Shengliang Guan 已提交
596 597
      mError("dnode:%d, %s cluster cfg parameters inconsistent, reason:%s", pDnode->dnodeId, pStatus->dnodeEp,
             offlineReason[ret]);
H
Hui Li 已提交
598 599
      return TSDB_CODE_MND_CLUSTER_CFG_INCONSISTENT;
    }
S
TD-1473  
Shengliang Guan 已提交
600

S
add log  
Shengliang Guan 已提交
601
    mInfo("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
602
    pDnode->status = TAOS_DN_STATUS_READY;
S
TD-1473  
Shengliang Guan 已提交
603
    pDnode->offlineReason = TAOS_DN_OFF_ONLINE;
S
TD-2270  
Shengliang Guan 已提交
604 605
    bnCheckModules();
    bnNotify();
S
slguan 已提交
606 607
  }

S
Shengliang Guan 已提交
608 609 610 611
  if (openVnodes != pDnode->openVnodes) {
    mnodeCheckUnCreatedVgroup(pDnode, pStatus->load, openVnodes);
  }

H
Hui Li 已提交
612
  pDnode->lastAccess = tsAccessSquence;
S
Shengliang Guan 已提交
613 614 615 616

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

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

622 623
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
624

625
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
626
}
627

628
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg) {
S
slguan 已提交
629 630 631 632 633
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

S
Shengliang Guan 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646
  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;

647
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
648
  if (pDnode != NULL) {
649
    mnodeDecDnodeRef(pDnode);
650
    mError("dnode:%d, already exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
651
    return TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
slguan 已提交
652 653 654 655 656
  }

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

S
TD-2046  
Shengliang Guan 已提交
661
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
662 663
    .type    = SDB_OPER_GLOBAL,
    .pTable  = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
664
    .pObj    = pDnode,
665
    .rowSize = sizeof(SDnodeObj),
S
TD-2046  
Shengliang Guan 已提交
666
    .pMsg    = pMsg
S
slguan 已提交
667 668
  };

S
TD-2046  
Shengliang Guan 已提交
669
  int32_t code = sdbInsertRow(&row);
S
Shengliang Guan 已提交
670
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
slguan 已提交
671
    int dnodeId = pDnode->dnodeId;
S
TD-1848  
Shengliang Guan 已提交
672
    tfree(pDnode);
S
Shengliang Guan 已提交
673
    mError("failed to create dnode:%d, reason:%s", dnodeId, tstrerror(code));
674
  } else {
S
Shengliang Guan 已提交
675
    mLInfo("dnode:%d is created", pDnode->dnodeId);
S
slguan 已提交
676 677 678 679 680
  }

  return code;
}

681
int32_t mnodeDropDnode(SDnodeObj *pDnode, void *pMsg) {
S
TD-2046  
Shengliang Guan 已提交
682
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
683 684
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
685
    .pObj   = pDnode,
S
TD-2046  
Shengliang Guan 已提交
686
    .pMsg   = pMsg
S
slguan 已提交
687 688
  };

S
TD-2046  
Shengliang Guan 已提交
689
  int32_t code = sdbDeleteRow(&row);
S
Shengliang Guan 已提交
690 691 692 693
  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 已提交
694 695 696 697 698
  }

  return code;
}

699
static int32_t mnodeDropDnodeByEp(char *ep, SMnodeMsg *pMsg) {
700
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
701
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
702 703 704 705 706 707
    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 已提交
708 709
  }

H
Hui Li 已提交
710
  if (strcmp(pDnode->dnodeEp, mnodeGetMnodeMasterEp()) == 0) {
J
jtao1735 已提交
711
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
712
    mnodeDecDnodeRef(pDnode);
713
    return TSDB_CODE_MND_NO_REMOVE_MASTER;
S
slguan 已提交
714 715
  }

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

S
TD-2270  
Shengliang Guan 已提交
718
  int32_t code = bnDropDnode(pDnode);
719 720
  mnodeDecDnodeRef(pDnode);
  return code;
S
slguan 已提交
721 722
}

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

726
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
727
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
728
  } else {
729
    return mnodeCreateDnode(pCreate->ep, pMsg);
S
slguan 已提交
730 731 732
  }
}

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

736
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
737
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
738
  } else {
739
    return mnodeDropDnodeByEp(pDrop->ep, pMsg);
740
  }
S
slguan 已提交
741 742
}

743 744
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
745 746
  if (pUser == NULL) return 0;

747
  if (strcmp(pUser->pAcct->user, TSDB_DEFAULT_USER) != 0) {
748
    mnodeDecUserRef(pUser);
749
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
750
  }
751 752 753 754 755 756 757 758 759 760

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

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
769
  strcpy(pSchema[cols].name, "vnodes");
770 771 772 773 774
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
775
  strcpy(pSchema[cols].name, "cores");
776 777 778
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
779
  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
780
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
781 782 783 784
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
785
  pShow->bytes[cols] = 5 + VARSTR_HEADER_SIZE;
786
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
787
  strcpy(pSchema[cols].name, "role");
788 789 790
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
791 792
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
793
  strcpy(pSchema[cols].name, "create_time");
794 795 796
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
797 798 799 800 801 802
  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++;

803 804 805 806 807 808 809 810
  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];
  }

811
  pShow->numOfRows = mnodeGetDnodesNum();
812
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
813
  pShow->pIter = NULL;
814

815
  mnodeDecUserRef(pUser);
S
slguan 已提交
816

817 818 819
  return 0;
}

820
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
821 822 823 824 825 826
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
  SDnodeObj *pDnode   = NULL;
  char      *pWrite;

  while (numOfRows < rows) {
827
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
828 829 830 831 832 833 834 835 836
    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 已提交
837
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols]);
838 839 840 841 842 843 844
    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 已提交
845
    *(int16_t *)pWrite = pDnode->numOfCores;
846
    cols++;
S
slguan 已提交
847
    
848
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
S
TD-2270  
Shengliang Guan 已提交
849
    char* status = dnodeStatus[pDnode->status];
850
    STR_TO_VARSTR(pWrite, status);
851 852
    cols++;

853
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
S
TD-2270  
Shengliang Guan 已提交
854
    char* role = dnodeRoles[pDnode->alternativeRole];
855 856 857
    STR_TO_VARSTR(pWrite, role);
    cols++;

S
slguan 已提交
858 859 860 861
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

S
TD-1473  
Shengliang Guan 已提交
862 863 864 865 866
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, offlineReason[pDnode->offlineReason]);
    cols++;

     numOfRows++;
867
    mnodeDecDnodeRef(pDnode);
868 869
  }

870
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
871 872 873 874
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

875
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
H
Haojun Liao 已提交
876
  uint32_t status = pDnode->moduleStatus & (1u << moduleType);
877 878 879
  return status > 0;
}

880
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
881 882
  int32_t cols = 0;

883
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
884 885
  if (pUser == NULL) return 0;

886
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
887
    mnodeDecUserRef(pUser);
888
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
889
  }
890 891 892

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
893 894 895 896 897 898
  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 已提交
899
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
900
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
901
  strcpy(pSchema[cols].name, "end_point");
902 903 904
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
905
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
906
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
907
  strcpy(pSchema[cols].name, "module");
908 909 910
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
911
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
912
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
913
  strcpy(pSchema[cols].name, "status");
914 915 916 917 918 919 920 921 922 923 924
  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];
  }

925
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
926
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
927
  pShow->pIter = NULL;
928
  mnodeDecUserRef(pUser);
929 930 931 932

  return 0;
}

933
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
934
  int32_t numOfRows = 0;
H
Haojun Liao 已提交
935 936 937

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

  while (numOfRows < rows) {
S
slguan 已提交
941
    SDnodeObj *pDnode = NULL;
942
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
943 944 945
    if (pDnode == NULL) break;

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

S
slguan 已提交
948 949 950
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
951 952

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
953
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols] - 1);
954 955 956
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
957
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, moduleName[moduleType], pShow->bytes[cols]);
958 959 960
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
961
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
H
Haojun Liao 已提交
962 963 964

      char* v = enable? "enable":"disable";
      STR_TO_VARSTR(pWrite, v);
965 966 967 968
      cols++;

      numOfRows++;
    }
S
slguan 已提交
969

970
    mnodeDecDnodeRef(pDnode);
971 972
  }

973
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
974 975 976 977
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

978
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
979 980 981 982 983
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

984
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
985 986
  int32_t cols = 0;

987
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
988 989
  if (pUser == NULL) return 0;

990
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
991
    mnodeDecUserRef(pUser);
992
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
993
  }
994 995 996

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
997
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
998
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
999
  tstrncpy(pSchema[cols].name, "name", sizeof(pSchema[cols].name));
1000 1001 1002
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
1003
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
1004
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
1005
  tstrncpy(pSchema[cols].name, "value", sizeof(pSchema[cols].name));
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  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 已提交
1017
    SGlobalCfg *cfg = tsGlobalConfig + i;
1018
    if (!mnodeCheckConfigShow(cfg)) continue;
1019 1020 1021 1022
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1023
  pShow->pIter = NULL;
1024
  mnodeDecUserRef(pUser);
1025 1026 1027 1028

  return 0;
}

1029
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1030 1031 1032
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
1033
    SGlobalCfg *cfg = tsGlobalConfig + i;
1034
    if (!mnodeCheckConfigShow(cfg)) continue;
1035 1036 1037 1038 1039

    char *pWrite;
    int32_t   cols = 0;

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

1042
    cols++;
1043
    int32_t t = 0;
1044 1045 1046

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
Shengliang Guan 已提交
1047 1048 1049 1050 1051
      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 已提交
1052
      case TAOS_CFG_VTYPE_INT16:
1053 1054
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1055 1056
        numOfRows++;
        break;
S
slguan 已提交
1057
      case TAOS_CFG_VTYPE_INT32:
1058 1059
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1060 1061
        numOfRows++;
        break;
S
slguan 已提交
1062
      case TAOS_CFG_VTYPE_FLOAT:
1063 1064
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        varDataSetLen(pWrite, t);
1065 1066
        numOfRows++;
        break;
S
slguan 已提交
1067 1068 1069
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
1070
        STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfg->ptr, TSDB_CFG_VALUE_LEN);
1071 1072 1073 1074 1075 1076 1077
        numOfRows++;
        break;
      default:
        break;
    }
  }

1078
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1079 1080 1081 1082
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

1083
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
1084
  int32_t cols = 0;
1085
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
1086
  if (pUser == NULL) return 0;
S
slguan 已提交
1087
  
1088
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
1089
    mnodeDecUserRef(pUser);
1090
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
1091
  }
1092 1093 1094 1095 1096 1097 1098 1099 1100

  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 已提交
1101
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  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 ) {
1115
    pDnode = mnodeGetDnodeByEp(pShow->payload);
1116
  } else {
1117
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
1118
    mnodeCancelGetNextDnode(pIter);
S
slguan 已提交
1119
  }
1120

S
slguan 已提交
1121 1122
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
1123
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1124
  }
1125 1126

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1127
  pShow->pIter = pDnode;
1128
  mnodeDecUserRef(pUser);
1129 1130 1131 1132

  return 0;
}

1133
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1134 1135 1136 1137 1138 1139 1140
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
1141
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
1142
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
1143
    void *pIter = NULL;
S
slguan 已提交
1144 1145
    SVgObj *pVgroup;
    while (1) {
1146
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
      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 已提交
1159
          strcpy(pWrite, syncRole[pVgid->role]);
S
slguan 已提交
1160
          cols++;
1161 1162
        }
      }
S
slguan 已提交
1163

1164
      mnodeDecVgroupRef(pVgroup);
1165 1166 1167 1168
    }
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
1169

1170
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1171 1172
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
1173 1174
}

S
TD-2270  
Shengliang Guan 已提交
1175 1176 1177 1178 1179 1180 1181
char* dnodeStatus[] = {
  "offline",
  "dropping",
  "balancing",
  "ready",
  "undefined"
};
1182

S
TD-2270  
Shengliang Guan 已提交
1183 1184 1185 1186 1187 1188
char* dnodeRoles[] = {
  "any",
  "mnode",
  "vnode",
  "any"
};