mnodeDnode.c 36.7 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);
66
static char*   mnodeGetDnodeAlternativeRoleStr(int32_t alternativeRole);
S
TD-1762  
Shengliang Guan 已提交
67
static void    mnodeUpdateDnodeEps();
68

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return onlineDnodes;
}

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

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

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

S
Shengliang Guan 已提交
285

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
H
Hui Li 已提交
433 434
}

S
TD-1762  
Shengliang Guan 已提交
435 436 437 438 439 440 441
static int32_t mnodeGetDnodeEpsSize() {
  pthread_mutex_lock(&tsDnodeEpsMutex);
  int32_t size = tsDnodeEpsSize;
  pthread_mutex_unlock(&tsDnodeEpsMutex);
  return size;
}

S
TD-1762  
Shengliang Guan 已提交
442
static void mnodeGetDnodeEpsData(SDnodeEps *pEps, int32_t epsSize) {
S
TD-1762  
Shengliang Guan 已提交
443
  pthread_mutex_lock(&tsDnodeEpsMutex);
S
TD-1762  
Shengliang Guan 已提交
444 445 446
  if (epsSize == tsDnodeEpsSize) {
    memcpy(pEps, tsDnodeEps, tsDnodeEpsSize);
  }
S
TD-1762  
Shengliang Guan 已提交
447 448 449 450 451 452 453 454
  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 已提交
455
  free(tsDnodeEps);
S
TD-1762  
Shengliang Guan 已提交
456 457 458 459 460 461 462 463 464 465
  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 已提交
466 467 468 469
    if (dnodesNum >= totalDnodes) {
      mnodeCancelGetNextDnode(pIter);
      break;
    }
S
TD-1762  
Shengliang Guan 已提交
470 471 472 473 474 475 476 477 478 479 480 481

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

482
static int32_t mnodeProcessDnodeStatusMsg(SMnodeMsg *pMsg) {
S
TD-1473  
Shengliang Guan 已提交
483
  SDnodeObj *pDnode     = NULL;
S
TD-1732  
Shengliang Guan 已提交
484
  SStatusMsg *pStatus   = pMsg->rpcMsg.pCont;
S
slguan 已提交
485
  pStatus->dnodeId      = htonl(pStatus->dnodeId);
S
slguan 已提交
486
  pStatus->moduleStatus = htonl(pStatus->moduleStatus);
S
slguan 已提交
487 488
  pStatus->lastReboot   = htonl(pStatus->lastReboot);
  pStatus->numOfCores   = htons(pStatus->numOfCores);
S
TD-1473  
Shengliang Guan 已提交
489

S
slguan 已提交
490 491
  uint32_t version = htonl(pStatus->version);
  if (version != tsVersion) {
S
TD-1473  
Shengliang Guan 已提交
492 493 494 495 496
    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);
497
    return TSDB_CODE_MND_INVALID_MSG_VERSION;
S
slguan 已提交
498 499
  }

S
slguan 已提交
500
  if (pStatus->dnodeId == 0) {
501
    pDnode = mnodeGetDnodeByEp(pStatus->dnodeEp);
S
slguan 已提交
502
    if (pDnode == NULL) {
503
      mDebug("dnode %s not created", pStatus->dnodeEp);
504
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
505 506
    }
  } else {
507
    pDnode = mnodeGetDnode(pStatus->dnodeId);
S
slguan 已提交
508
    if (pDnode == NULL) {
S
TD-1473  
Shengliang Guan 已提交
509 510 511 512 513
      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);
514
      return TSDB_CODE_MND_DNODE_NOT_EXIST;
S
slguan 已提交
515 516
    }
  }
J
jtao1735 已提交
517

S
[TD-17]  
slguan 已提交
518 519
  pDnode->lastReboot       = pStatus->lastReboot;
  pDnode->numOfCores       = pStatus->numOfCores;
S
slguan 已提交
520 521
  pDnode->diskAvailable    = pStatus->diskAvailable;
  pDnode->alternativeRole  = pStatus->alternativeRole;
S
slguan 已提交
522
  pDnode->moduleStatus     = pStatus->moduleStatus;
S
Shengliang Guan 已提交
523

S
slguan 已提交
524
  if (pStatus->dnodeId == 0) {
525
    mDebug("dnode:%d %s, first access, set clusterId %s", pDnode->dnodeId, pDnode->dnodeEp, mnodeGetClusterId());
S
slguan 已提交
526
  } else {
527
    if (strncmp(pStatus->clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN - 1) != 0) {
S
TD-1473  
Shengliang Guan 已提交
528 529 530
      if (pDnode != NULL && pDnode->status != TAOS_DN_STATUS_READY) {
        pDnode->offlineReason = TAOS_DN_OFF_CLUSTER_ID_NOT_MATCH;
      }
531
      mError("dnode:%d, input clusterId %s not match with exist %s", pDnode->dnodeId, pStatus->clusterId,
S
Shengliang Guan 已提交
532 533 534
             mnodeGetClusterId());
      return TSDB_CODE_MND_INVALID_CLUSTER_ID;
    } else {
S
TD-1725  
Shengliang Guan 已提交
535 536
      mTrace("dnode:%d, status received, access times %d openVnodes:%d:%d", pDnode->dnodeId, pDnode->lastAccess,
             htons(pStatus->openVnodes), pDnode->openVnodes);
S
Shengliang Guan 已提交
537
    }
S
slguan 已提交
538
  }
S
Shengliang Guan 已提交
539

540
  int32_t openVnodes = htons(pStatus->openVnodes);
S
TD-1762  
Shengliang Guan 已提交
541
  int32_t epsSize = mnodeGetDnodeEpsSize();
S
TD-1732  
Shengliang Guan 已提交
542 543
  int32_t vgAccessSize = openVnodes * sizeof(SVgroupAccess);
  int32_t contLen = sizeof(SStatusRsp) + vgAccessSize + epsSize;
S
TD-1762  
Shengliang Guan 已提交
544

S
TD-1732  
Shengliang Guan 已提交
545
  SStatusRsp *pRsp = rpcMallocCont(contLen);
546 547 548 549 550 551 552 553
  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);
554
  tstrncpy(pRsp->dnodeCfg.clusterId, mnodeGetClusterId(), TSDB_CLUSTER_ID_LEN);
S
TD-1732  
Shengliang Guan 已提交
555
  SVgroupAccess *pAccess = (SVgroupAccess *)((char *)pRsp + sizeof(SStatusRsp));
S
TD-1762  
Shengliang Guan 已提交
556
  
557
  for (int32_t j = 0; j < openVnodes; ++j) {
558
    SVnodeLoad *pVload = &pStatus->load[j];
S
slguan 已提交
559
    pVload->vgId = htonl(pVload->vgId);
S
slguan 已提交
560
    pVload->cfgVersion = htonl(pVload->cfgVersion);
S
slguan 已提交
561

562
    SVgObj *pVgroup = mnodeGetVgroup(pVload->vgId);
563
    if (pVgroup == NULL) {
564
      SRpcEpSet epSet = mnodeGetEpSetFromIp(pDnode->dnodeEp);
565
      mInfo("dnode:%d, vgId:%d not exist in mnode, drop it", pDnode->dnodeId, pVload->vgId);
566
      mnodeSendDropVnodeMsg(pVload->vgId, &epSet, NULL);
567
    } else {
568
      mnodeUpdateVgroupStatus(pVgroup, pDnode, pVload);
569 570
      pAccess->vgId = htonl(pVload->vgId);
      pAccess->accessState = pVgroup->accessState;
H
Haojun Liao 已提交
571
      pAccess++;
572
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
573 574 575
    }
  }

S
slguan 已提交
576
  if (pDnode->status == TAOS_DN_STATUS_OFFLINE) {
H
Hui Li 已提交
577
    // Verify whether the cluster parameters are consistent when status change from offline to ready
S
TD-1473  
Shengliang Guan 已提交
578 579 580
    int32_t ret = mnodeCheckClusterCfgPara(&(pStatus->clusterCfg));
    if (0 != ret) {
      pDnode->offlineReason = ret;
H
Hui Li 已提交
581
      mnodeDecDnodeRef(pDnode);
S
Shengliang Guan 已提交
582
      rpcFreeCont(pRsp);
S
TD-1473  
Shengliang Guan 已提交
583 584
      mError("dnode:%d, %s cluster cfg parameters inconsistent, reason:%s", pDnode->dnodeId, pStatus->dnodeEp,
             offlineReason[ret]);
H
Hui Li 已提交
585 586
      return TSDB_CODE_MND_CLUSTER_CFG_INCONSISTENT;
    }
S
TD-1473  
Shengliang Guan 已提交
587

S
add log  
Shengliang Guan 已提交
588
    mInfo("dnode:%d, from offline to online", pDnode->dnodeId);
S
slguan 已提交
589
    pDnode->status = TAOS_DN_STATUS_READY;
S
TD-1473  
Shengliang Guan 已提交
590
    pDnode->offlineReason = TAOS_DN_OFF_ONLINE;
S
TD-2270  
Shengliang Guan 已提交
591 592
    bnCheckModules();
    bnNotify();
S
slguan 已提交
593 594
  }

S
Shengliang Guan 已提交
595 596 597 598
  if (openVnodes != pDnode->openVnodes) {
    mnodeCheckUnCreatedVgroup(pDnode, pStatus->load, openVnodes);
  }

H
Hui Li 已提交
599
  pDnode->lastAccess = tsAccessSquence;
S
Shengliang Guan 已提交
600 601 602 603

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

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

609 610
  pMsg->rpcRsp.len = contLen;
  pMsg->rpcRsp.rsp =  pRsp;
S
slguan 已提交
611

612
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
613
}
614

615
static int32_t mnodeCreateDnode(char *ep, SMnodeMsg *pMsg) {
S
slguan 已提交
616 617 618 619 620
  int32_t grantCode = grantCheck(TSDB_GRANT_DNODE);
  if (grantCode != TSDB_CODE_SUCCESS) {
    return grantCode;
  }

S
Shengliang Guan 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633
  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;

634
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
635
  if (pDnode != NULL) {
636
    mnodeDecDnodeRef(pDnode);
637
    mError("dnode:%d, already exist, %s:%d", pDnode->dnodeId, pDnode->dnodeFqdn, pDnode->dnodePort);
638
    return TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
slguan 已提交
639 640 641 642 643
  }

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

S
TD-2046  
Shengliang Guan 已提交
648
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
649 650
    .type    = SDB_OPER_GLOBAL,
    .pTable  = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
651
    .pObj    = pDnode,
652
    .rowSize = sizeof(SDnodeObj),
S
TD-2046  
Shengliang Guan 已提交
653
    .pMsg    = pMsg
S
slguan 已提交
654 655
  };

S
TD-2046  
Shengliang Guan 已提交
656
  int32_t code = sdbInsertRow(&row);
S
Shengliang Guan 已提交
657
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
slguan 已提交
658
    int dnodeId = pDnode->dnodeId;
S
TD-1848  
Shengliang Guan 已提交
659
    tfree(pDnode);
S
Shengliang Guan 已提交
660
    mError("failed to create dnode:%d, reason:%s", dnodeId, tstrerror(code));
661
  } else {
S
Shengliang Guan 已提交
662
    mLInfo("dnode:%d is created", pDnode->dnodeId);
S
slguan 已提交
663 664 665 666 667
  }

  return code;
}

668
int32_t mnodeDropDnode(SDnodeObj *pDnode, void *pMsg) {
S
TD-2046  
Shengliang Guan 已提交
669
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
670 671
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsDnodeSdb,
S
TD-2046  
Shengliang Guan 已提交
672
    .pObj   = pDnode,
S
TD-2046  
Shengliang Guan 已提交
673
    .pMsg   = pMsg
S
slguan 已提交
674 675
  };

S
TD-2046  
Shengliang Guan 已提交
676
  int32_t code = sdbDeleteRow(&row);
S
Shengliang Guan 已提交
677 678 679 680
  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 已提交
681 682 683 684 685
  }

  return code;
}

686
static int32_t mnodeDropDnodeByEp(char *ep, SMnodeMsg *pMsg) {
687
  SDnodeObj *pDnode = mnodeGetDnodeByEp(ep);
S
slguan 已提交
688
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
689 690 691 692 693 694
    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 已提交
695 696
  }

H
Hui Li 已提交
697
  if (strcmp(pDnode->dnodeEp, mnodeGetMnodeMasterEp()) == 0) {
J
jtao1735 已提交
698
    mError("dnode:%d, can't drop dnode:%s which is master", pDnode->dnodeId, ep);
699
    mnodeDecDnodeRef(pDnode);
700
    return TSDB_CODE_MND_NO_REMOVE_MASTER;
S
slguan 已提交
701 702
  }

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

S
TD-2270  
Shengliang Guan 已提交
705
  int32_t code = bnDropDnode(pDnode);
706 707
  mnodeDecDnodeRef(pDnode);
  return code;
S
slguan 已提交
708 709
}

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

713
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
714
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
715
  } else {
716
    return mnodeCreateDnode(pCreate->ep, pMsg);
S
slguan 已提交
717 718 719
  }
}

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

723
  if (strcmp(pMsg->pUser->user, TSDB_DEFAULT_USER) != 0) {
724
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
725
  } else {
726
    return mnodeDropDnodeByEp(pDrop->ep, pMsg);
727
  }
S
slguan 已提交
728 729
}

730 731
static int32_t mnodeGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
732 733
  if (pUser == NULL) return 0;

734
  if (strcmp(pUser->pAcct->user, TSDB_DEFAULT_USER) != 0) {
735
    mnodeDecUserRef(pUser);
736
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
737
  }
738 739 740 741 742 743 744 745 746 747

  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 已提交
748
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
749
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
750
  strcpy(pSchema[cols].name, "end_point");
751 752 753 754 755
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
756
  strcpy(pSchema[cols].name, "vnodes");
757 758 759 760 761
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
762
  strcpy(pSchema[cols].name, "cores");
763 764 765
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
766
  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
767
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
768 769 770 771
  strcpy(pSchema[cols].name, "status");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
772
  pShow->bytes[cols] = 5 + VARSTR_HEADER_SIZE;
773
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
774
  strcpy(pSchema[cols].name, "role");
775 776 777
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
778 779
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
780
  strcpy(pSchema[cols].name, "create_time");
781 782 783
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
TD-1473  
Shengliang Guan 已提交
784 785 786 787 788 789
  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++;

790 791 792 793 794 795 796 797
  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];
  }

798
  pShow->numOfRows = mnodeGetDnodesNum();
799
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
800
  pShow->pIter = NULL;
801

802
  mnodeDecUserRef(pUser);
S
slguan 已提交
803

804 805 806
  return 0;
}

807
static int32_t mnodeRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
808 809 810 811 812 813
  int32_t    numOfRows = 0;
  int32_t    cols      = 0;
  SDnodeObj *pDnode   = NULL;
  char      *pWrite;

  while (numOfRows < rows) {
814
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, &pDnode);
815 816 817 818 819 820 821 822 823
    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 已提交
824
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols]);
825 826 827 828 829 830 831
    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 已提交
832
    *(int16_t *)pWrite = pDnode->numOfCores;
833
    cols++;
S
slguan 已提交
834
    
835
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
836
    char* status = mnodeGetDnodeStatusStr(pDnode->status);
837
    STR_TO_VARSTR(pWrite, status);
838 839
    cols++;

840 841 842 843 844
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;  
    char* role = mnodeGetDnodeAlternativeRoleStr(pDnode->alternativeRole);
    STR_TO_VARSTR(pWrite, role);
    cols++;

S
slguan 已提交
845 846 847 848
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

S
TD-1473  
Shengliang Guan 已提交
849 850 851 852 853
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, offlineReason[pDnode->offlineReason]);
    cols++;

     numOfRows++;
854
    mnodeDecDnodeRef(pDnode);
855 856
  }

857
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
858 859 860 861
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

862
static bool mnodeCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) {
H
Haojun Liao 已提交
863
  uint32_t status = pDnode->moduleStatus & (1u << moduleType);
864 865 866
  return status > 0;
}

867
static int32_t mnodeGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
868 869
  int32_t cols = 0;

870
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
871 872
  if (pUser == NULL) return 0;

873
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
874
    mnodeDecUserRef(pUser);
875
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
876
  }
877 878 879

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
880 881 882 883 884 885
  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 已提交
886
  pShow->bytes[cols] = 40 + VARSTR_HEADER_SIZE;
887
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
888
  strcpy(pSchema[cols].name, "end_point");
889 890 891
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
892
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
893
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
894
  strcpy(pSchema[cols].name, "module");
895 896 897
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
898
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
899
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
900
  strcpy(pSchema[cols].name, "status");
901 902 903 904 905 906 907 908 909 910 911
  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];
  }

912
  pShow->numOfRows = mnodeGetDnodesNum() * TSDB_MOD_MAX;
913
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
914
  pShow->pIter = NULL;
915
  mnodeDecUserRef(pUser);
916 917 918 919

  return 0;
}

920
int32_t mnodeRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
921
  int32_t numOfRows = 0;
H
Haojun Liao 已提交
922 923 924

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

  while (numOfRows < rows) {
S
slguan 已提交
928
    SDnodeObj *pDnode = NULL;
929
    pShow->pIter = mnodeGetNextDnode(pShow->pIter, (SDnodeObj **)&pDnode);
930 931 932
    if (pDnode == NULL) break;

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

S
slguan 已提交
935 936 937
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *)pWrite = pDnode->dnodeId;
      cols++;
938 939

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
940
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->dnodeEp, pShow->bytes[cols] - 1);
941 942 943
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
944
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, moduleName[moduleType], pShow->bytes[cols]);
945 946 947
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
948
      bool enable = mnodeCheckModuleInDnode(pDnode, moduleType);
H
Haojun Liao 已提交
949 950 951

      char* v = enable? "enable":"disable";
      STR_TO_VARSTR(pWrite, v);
952 953 954 955
      cols++;

      numOfRows++;
    }
S
slguan 已提交
956

957
    mnodeDecDnodeRef(pDnode);
958 959
  }

960
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
961 962 963 964
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

965
static bool mnodeCheckConfigShow(SGlobalCfg *cfg) {
966 967 968 969 970
  if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW))
    return false;
  return true;
}

971
static int32_t mnodeGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
972 973
  int32_t cols = 0;

974
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
975 976
  if (pUser == NULL) return 0;

977
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
978
    mnodeDecUserRef(pUser);
979
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
980
  }
981 982 983

  SSchema *pSchema = pMeta->schema;

S
slguan 已提交
984
  pShow->bytes[cols] = TSDB_CFG_OPTION_LEN + VARSTR_HEADER_SIZE;
985
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
986
  tstrncpy(pSchema[cols].name, "name", sizeof(pSchema[cols].name));
987 988 989
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

S
slguan 已提交
990
  pShow->bytes[cols] = TSDB_CFG_VALUE_LEN + VARSTR_HEADER_SIZE;
991
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
992
  tstrncpy(pSchema[cols].name, "value", sizeof(pSchema[cols].name));
993 994 995 996 997 998 999 1000 1001 1002 1003
  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 已提交
1004
    SGlobalCfg *cfg = tsGlobalConfig + i;
1005
    if (!mnodeCheckConfigShow(cfg)) continue;
1006 1007 1008 1009
    pShow->numOfRows++;
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1010
  pShow->pIter = NULL;
1011
  mnodeDecUserRef(pUser);
1012 1013 1014 1015

  return 0;
}

1016
static int32_t mnodeRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1017 1018 1019
  int32_t numOfRows = 0;

  for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) {
S
slguan 已提交
1020
    SGlobalCfg *cfg = tsGlobalConfig + i;
1021
    if (!mnodeCheckConfigShow(cfg)) continue;
1022 1023 1024 1025 1026

    char *pWrite;
    int32_t   cols = 0;

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

1029
    cols++;
1030
    int32_t t = 0;
1031 1032 1033

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    switch (cfg->valType) {
S
slguan 已提交
1034
      case TAOS_CFG_VTYPE_INT16:
1035 1036
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int16_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1037 1038
        numOfRows++;
        break;
S
slguan 已提交
1039
      case TAOS_CFG_VTYPE_INT32:
1040 1041
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%d", *((int32_t *)cfg->ptr));
        varDataSetLen(pWrite, t);
1042 1043
        numOfRows++;
        break;
S
slguan 已提交
1044
      case TAOS_CFG_VTYPE_FLOAT:
1045 1046
        t = snprintf(varDataVal(pWrite), TSDB_CFG_VALUE_LEN, "%f", *((float *)cfg->ptr));
        varDataSetLen(pWrite, t);
1047 1048
        numOfRows++;
        break;
S
slguan 已提交
1049 1050 1051
      case TAOS_CFG_VTYPE_STRING:
      case TAOS_CFG_VTYPE_IPSTR:
      case TAOS_CFG_VTYPE_DIRECTORY:
1052
        STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfg->ptr, TSDB_CFG_VALUE_LEN);
1053 1054 1055 1056 1057 1058 1059
        numOfRows++;
        break;
      default:
        break;
    }
  }

1060
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1061 1062 1063 1064
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

1065
static int32_t mnodeGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
1066
  int32_t cols = 0;
1067
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
1068
  if (pUser == NULL) return 0;
S
slguan 已提交
1069
  
1070
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) != 0)  {
1071
    mnodeDecUserRef(pUser);
1072
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
1073
  }
1074 1075 1076 1077 1078 1079 1080 1081 1082

  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 已提交
1083
  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  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 ) {
1097
    pDnode = mnodeGetDnodeByEp(pShow->payload);
1098
  } else {
1099
    void *pIter = mnodeGetNextDnode(NULL, (SDnodeObj **)&pDnode);
S
Shengliang Guan 已提交
1100
    mnodeCancelGetNextDnode(pIter);
S
slguan 已提交
1101
  }
1102

S
slguan 已提交
1103 1104
  if (pDnode != NULL) {
    pShow->numOfRows += pDnode->openVnodes;
1105
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1106
  }
1107 1108

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
S
Shengliang Guan 已提交
1109
  pShow->pIter = pDnode;
1110
  mnodeDecUserRef(pUser);
1111 1112 1113 1114

  return 0;
}

1115
static int32_t mnodeRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
1116 1117 1118 1119 1120 1121 1122
  int32_t    numOfRows = 0;
  SDnodeObj *pDnode = NULL;
  char *     pWrite;
  int32_t    cols = 0;

  if (0 == rows) return 0;

S
Shengliang Guan 已提交
1123
  pDnode = (SDnodeObj *)(pShow->pIter);
S
slguan 已提交
1124
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
1125
    void *pIter = NULL;
S
slguan 已提交
1126 1127
    SVgObj *pVgroup;
    while (1) {
1128
      pIter = mnodeGetNextVgroup(pIter, &pVgroup);
S
slguan 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
      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 已提交
1141
          strcpy(pWrite, syncRole[pVgid->role]);
S
slguan 已提交
1142
          cols++;
1143 1144
        }
      }
S
slguan 已提交
1145

1146
      mnodeDecVgroupRef(pVgroup);
1147 1148 1149 1150
    }
  } else {
    numOfRows = 0;
  }
S
slguan 已提交
1151

1152
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1153 1154
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
slguan 已提交
1155 1156
}

1157
char* mnodeGetDnodeStatusStr(int32_t dnodeStatus) {
S
slguan 已提交
1158
  switch (dnodeStatus) {
S
slguan 已提交
1159 1160 1161 1162
    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 已提交
1163 1164 1165
    default:                       return "undefined";
  }
}
1166 1167 1168 1169 1170 1171 1172 1173 1174

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