mndDnode.c 41.3 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndDnode.h"
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndMnode.h"
20
#include "mndPrivilege.h"
D
dapan1121 已提交
21
#include "mndQnode.h"
S
Shengliang Guan 已提交
22
#include "mndShow.h"
23
#include "mndSnode.h"
S
Shengliang Guan 已提交
24
#include "mndTrans.h"
S
Shengliang Guan 已提交
25
#include "mndUser.h"
S
Shengliang Guan 已提交
26
#include "mndVgroup.h"
H
Haojun Liao 已提交
27
#include "tmisce.h"
C
cadem 已提交
28
#include "mndCluster.h"
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
#define TSDB_DNODE_VER_NUMBER   1
31
#define TSDB_DNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
32

S
Shengliang Guan 已提交
33
static const char *offlineReason[] = {
S
Shengliang Guan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
    "",
    "status msg timeout",
    "status not received",
    "version not match",
    "dnodeId not match",
    "clusterId not match",
    "interval not match",
    "timezone not match",
    "locale not match",
    "charset not match",
    "unknown",
};

S
Shengliang Guan 已提交
47 48 49 50 51
static int32_t  mndCreateDefaultDnode(SMnode *pMnode);
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode);
static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw);
static int32_t  mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode);
static int32_t  mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode);
52
static int32_t  mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew);
D
dapan1121 已提交
53
static int32_t  mndProcessDnodeListReq(SRpcMsg *pReq);
D
dapan1121 已提交
54
static int32_t  mndProcessShowVariablesReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56 57 58 59 60
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq);
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq);
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq);
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp);
static int32_t mndProcessStatusReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
63
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
64
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
65
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
66 67

int32_t mndInitDnode(SMnode *pMnode) {
68 69 70 71 72 73 74 75 76 77
  SSdbTable table = {
      .sdbType = SDB_DNODE,
      .keyType = SDB_KEY_INT32,
      .deployFp = (SdbDeployFp)mndCreateDefaultDnode,
      .encodeFp = (SdbEncodeFp)mndDnodeActionEncode,
      .decodeFp = (SdbDecodeFp)mndDnodeActionDecode,
      .insertFp = (SdbInsertFp)mndDnodeActionInsert,
      .updateFp = (SdbUpdateFp)mndDnodeActionUpdate,
      .deleteFp = (SdbDeleteFp)mndDnodeActionDelete,
  };
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79 80 81
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
H
Hongze Cheng 已提交
82
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
S
Shengliang Guan 已提交
83
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
D
dapan1121 已提交
84
  mndSetMsgHandle(pMnode, TDMT_MND_DNODE_LIST, mndProcessDnodeListReq);
D
dapan1121 已提交
85
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
S
Shengliang Guan 已提交
86

87 88
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
S
Shengliang Guan 已提交
89 90 91
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
92 93 94 95 96 97
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
S
Shengliang Guan 已提交
98 99 100 101
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
102 103 104 105
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
106
  dnodeObj.port = tsServerPort;
S
Shengliang Guan 已提交
107 108
  tstrncpy(dnodeObj.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
  dnodeObj.fqdn[TSDB_FQDN_LEN - 1] = 0;
S
Shengliang Guan 已提交
109
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", tsLocalFqdn, tsServerPort);
S
Shengliang Guan 已提交
110

111
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL, "create-dnode");
S
Shengliang Guan 已提交
112
  if (pTrans == NULL) goto _OVER;
113
  mInfo("trans:%d, used to create dnode:%s on first deploy", pTrans->id, dnodeObj.ep);
114

S
Shengliang Guan 已提交
115 116
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
117
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
118
  pRaw = NULL;
119

S
Shengliang Guan 已提交
120 121
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
122

S
Shengliang Guan 已提交
123
_OVER:
124
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
125 126
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
127 128
}

S
Shengliang Guan 已提交
129
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
130 131
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
132
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
S
Shengliang Guan 已提交
133
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
134 135

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
136 137 138 139 140 141 142
  SDB_SET_INT32(pRaw, dataPos, pDnode->id, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pDnode->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pDnode->updateTime, _OVER)
  SDB_SET_INT16(pRaw, dataPos, pDnode->port, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, _OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER);
143 144 145

  terrno = 0;

S
Shengliang Guan 已提交
146
_OVER:
147 148 149 150 151
  if (terrno != 0) {
    mError("dnode:%d, failed to encode to raw:%p since %s", pDnode->id, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
152

153
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
154 155 156 157
  return pRaw;
}

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
158
  terrno = TSDB_CODE_OUT_OF_MEMORY;
159 160
  SSdbRow   *pRow = NULL;
  SDnodeObj *pDnode = NULL;
161

S
Shengliang Guan 已提交
162
  int8_t sver = 0;
S
Shengliang Guan 已提交
163
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
164
  if (sver != TSDB_DNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
165
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
166
    goto _OVER;
S
Shengliang Guan 已提交
167 168
  }

S
Shengliang Guan 已提交
169 170
  pRow = sdbAllocRow(sizeof(SDnodeObj));
  if (pRow == NULL) goto _OVER;
171 172

  pDnode = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
173
  if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
174 175

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
176 177 178 179 180 181
  SDB_GET_INT32(pRaw, dataPos, &pDnode->id, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pDnode->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pDnode->updateTime, _OVER)
  SDB_GET_INT16(pRaw, dataPos, &pDnode->port, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, _OVER)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, _OVER)
182 183

  terrno = 0;
184 185 186
  if (tmsgUpdateDnodeInfo(&pDnode->id, NULL, pDnode->fqdn, &pDnode->port)) {
    mInfo("dnode:%d, endpoint changed", pDnode->id);
  }
187

S
Shengliang Guan 已提交
188
_OVER:
189
  if (terrno != 0) {
190
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode == NULL ? 0 : pDnode->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
191
    taosMemoryFreeClear(pRow);
192 193
    return NULL;
  }
S
Shengliang Guan 已提交
194

195
  mTrace("dnode:%d, decode from raw:%p, row:%p ep:%s:%u", pDnode->id, pRaw, pDnode, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
196 197 198
  return pRow;
}

199
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
200
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
201
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
S
Shengliang Guan 已提交
202 203 204 205

  char ep[TSDB_EP_LEN] = {0};
  snprintf(ep, TSDB_EP_LEN - 1, "%s:%u", pDnode->fqdn, pDnode->port);
  tstrncpy(pDnode->ep, ep, TSDB_EP_LEN);
S
Shengliang Guan 已提交
206 207 208
  return 0;
}

S
Shengliang Guan 已提交
209
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
210
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
211 212
  return 0;
}
S
Shengliang Guan 已提交
213

214
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
S
Shengliang Guan 已提交
215
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
216
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
217
  return 0;
S
Shengliang Guan 已提交
218 219
}

S
Shengliang Guan 已提交
220
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
221 222 223
  SSdb      *pSdb = pMnode->pSdb;
  SDnodeObj *pDnode = sdbAcquire(pSdb, SDB_DNODE, &dnodeId);
  if (pDnode == NULL) {
224 225 226 227 228 229 230 231 232 233
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    } else if (terrno == TSDB_CODE_SDB_OBJ_CREATING) {
      terrno = TSDB_CODE_MND_DNODE_IN_CREATING;
    } else if (terrno == TSDB_CODE_SDB_OBJ_DROPPING) {
      terrno = TSDB_CODE_MND_DNODE_IN_DROPPING;
    } else {
      terrno = TSDB_CODE_APP_ERROR;
      mFatal("dnode:%d, failed to acquire db since %s", dnodeId, terrstr());
    }
S
Shengliang Guan 已提交
234
  }
235

S
Shengliang Guan 已提交
236
  return pDnode;
S
Shengliang Guan 已提交
237
}
S
Shengliang Guan 已提交
238

S
Shengliang Guan 已提交
239 240 241
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
242 243
}

S
Shengliang Guan 已提交
244
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
H
Haojun Liao 已提交
245 246
  SEpSet epSet = {0};
  addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
247 248 249
  return epSet;
}

S
Shengliang Guan 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262
static SDnodeObj *mndAcquireDnodeByEp(SMnode *pMnode, char *pEpStr) {
  SSdb *pSdb = pMnode->pSdb;

  void *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;

    if (strncasecmp(pEpStr, pDnode->ep, TSDB_EP_LEN) == 0) {
      sdbCancelFetch(pSdb, pIter);
      return pDnode;
    }
S
Shengliang Guan 已提交
263 264

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
265 266
  }

S
Shengliang Guan 已提交
267
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
268 269 270
  return NULL;
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static SDnodeObj *mndAcquireDnodeAllStatusByEp(SMnode *pMnode, char *pEpStr) {
  SSdb *pSdb = pMnode->pSdb;

  void *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
    if (pIter == NULL) break;

    if (strncasecmp(pEpStr, pDnode->ep, TSDB_EP_LEN) == 0) {
      sdbCancelFetch(pSdb, pIter);
      return pDnode;
    }

    sdbRelease(pSdb, pDnode);
  }

  return NULL;
}

S
Shengliang Guan 已提交
292
int32_t mndGetDnodeSize(SMnode *pMnode) {
S
Shengliang Guan 已提交
293 294 295 296
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DNODE);
}

C
cadem 已提交
297 298 299 300 301
int32_t mndGetDbSize(SMnode *pMnode) {
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DB);
}

S
Shengliang Guan 已提交
302
bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs) {
dengyihao's avatar
dengyihao 已提交
303
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
S
Shengliang Guan 已提交
304
  if (interval > 5000 * (int64_t)tsStatusInterval) {
S
Shengliang Guan 已提交
305 306 307
    if (pDnode->rebootTime > 0) {
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
    }
S
Shengliang Guan 已提交
308 309 310 311 312
    return false;
  }
  return true;
}

C
Cary Xu 已提交
313
void mndGetDnodeData(SMnode *pMnode, SArray *pDnodeEps) {
S
Shengliang Guan 已提交
314 315
  SSdb *pSdb = pMnode->pSdb;

316
  int32_t numOfEps = 0;
S
Shengliang Guan 已提交
317 318 319
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
320 321
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
322 323
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
324 325 326
    SDnodeEp dnodeEp = {0};
    dnodeEp.id = pDnode->id;
    dnodeEp.ep.port = pDnode->port;
S
Shengliang Guan 已提交
327
    tstrncpy(dnodeEp.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
328
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
329

S
Shengliang Guan 已提交
330
    dnodeEp.isMnode = 0;
S
Shengliang Guan 已提交
331
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
332
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
333
    }
S
Shengliang Guan 已提交
334
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
335 336 337
  }
}

S
Shengliang Guan 已提交
338
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
S
Shengliang Guan 已提交
339
  if (pCfg->statusInterval != tsStatusInterval) {
S
Shengliang Guan 已提交
340 341
    mError("dnode:%d, statusInterval:%d inconsistent with cluster:%d", pDnode->id, pCfg->statusInterval,
           tsStatusInterval);
S
Shengliang Guan 已提交
342 343 344
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
  }

wafwerar's avatar
wafwerar 已提交
345
  if ((0 != strcasecmp(pCfg->timezone, tsTimezoneStr)) && (pMnode->checkTime != pCfg->checkTime)) {
S
Shengliang Guan 已提交
346 347
    mError("dnode:%d, timezone:%s checkTime:%" PRId64 " inconsistent with cluster %s %" PRId64, pDnode->id,
           pCfg->timezone, pCfg->checkTime, tsTimezoneStr, pMnode->checkTime);
S
Shengliang Guan 已提交
348 349 350
    return DND_REASON_TIME_ZONE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
351
  if (0 != strcasecmp(pCfg->locale, tsLocale)) {
S
Shengliang Guan 已提交
352
    mError("dnode:%d, locale:%s inconsistent with cluster:%s", pDnode->id, pCfg->locale, tsLocale);
S
Shengliang Guan 已提交
353 354 355
    return DND_REASON_LOCALE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
356
  if (0 != strcasecmp(pCfg->charset, tsCharset)) {
S
Shengliang Guan 已提交
357
    mError("dnode:%d, charset:%s inconsistent with cluster:%s", pDnode->id, pCfg->charset, tsCharset);
S
Shengliang Guan 已提交
358 359 360 361 362 363
    return DND_REASON_CHARSET_NOT_MATCH;
  }

  return 0;
}

S
Shengliang Guan 已提交
364 365
static int32_t mndProcessStatusReq(SRpcMsg *pReq) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
366 367 368
  SStatusReq statusReq = {0};
  SDnodeObj *pDnode = NULL;
  int32_t    code = -1;
S
Shengliang Guan 已提交
369

S
Shengliang Guan 已提交
370
  if (tDeserializeSStatusReq(pReq->pCont, pReq->contLen, &statusReq) != 0) {
S
Shengliang Guan 已提交
371
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
372
    goto _OVER;
S
Shengliang Guan 已提交
373
  }
S
Shengliang Guan 已提交
374

C
cadem 已提交
375
  int64_t clusterid = mndGetClusterId(pMnode);
S
Shengliang Guan 已提交
376
  if (statusReq.clusterId != 0 && statusReq.clusterId != clusterid) {
C
cadem 已提交
377
    code = TSDB_CODE_MND_DNODE_DIFF_CLUSTER;
S
Shengliang Guan 已提交
378 379
    mWarn("dnode:%d, %s, its clusterid:%" PRId64 " differ from current cluster:%" PRId64 ", code:0x%x",
          statusReq.dnodeId, statusReq.dnodeEp, statusReq.clusterId, clusterid, code);
C
cadem 已提交
380 381 382
    goto _OVER;
  }

S
Shengliang Guan 已提交
383 384
  if (statusReq.dnodeId == 0) {
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
385
    if (pDnode == NULL) {
386
      mInfo("dnode:%s, not created yet", statusReq.dnodeEp);
S
Shengliang Guan 已提交
387
      goto _OVER;
S
Shengliang Guan 已提交
388 389
    }
  } else {
S
Shengliang Guan 已提交
390
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
S
Shengliang Guan 已提交
391
    if (pDnode == NULL) {
392
      int32_t err = terrno;
S
Shengliang Guan 已提交
393
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
394
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
395
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
396 397 398 399 400 401 402 403 404 405 406
        terrno = err;
        goto _OVER;
      }

      mError("dnode:%d, %s not exist, code:0x%x", statusReq.dnodeId, statusReq.dnodeEp, err);
      if (err == TSDB_CODE_MND_DNODE_NOT_EXIST) {
        terrno = err;
        goto _OVER;
      } else {
        pDnode = mndAcquireDnodeAllStatusByEp(pMnode, statusReq.dnodeEp);
        if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
407 408 409 410
      }
    }
  }

411 412 413 414 415 416 417 418 419
  int64_t dnodeVer = sdbGetTableVer(pMnode->pSdb, SDB_DNODE) + sdbGetTableVer(pMnode->pSdb, SDB_MNODE);
  int64_t curMs = taosGetTimestampMs();
  bool    online = mndIsDnodeOnline(pDnode, curMs);
  bool    dnodeChanged = (statusReq.dnodeVer == 0) || (statusReq.dnodeVer != dnodeVer);
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
  bool    needCheck = !online || dnodeChanged || reboot;

  const STraceId *trace = &pReq->info.traceId;
  mGTrace("dnode:%d, status received, accessTimes:%d check:%d online:%d reboot:%d changed:%d statusSeq:%d", pDnode->id,
420
          pDnode->accessTimes, needCheck, online, reboot, dnodeChanged, statusReq.statusSeq);
421

S
Shengliang Guan 已提交
422
  for (int32_t v = 0; v < taosArrayGetSize(statusReq.pVloads); ++v) {
S
Shengliang Guan 已提交
423 424 425 426
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);

    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
    if (pVgroup != NULL) {
S
Shengliang Guan 已提交
427
      if (pVload->syncState == TAOS_SYNC_STATE_LEADER) {
428
        pVgroup->cacheUsage = pVload->cacheUsage;
D
dapan1121 已提交
429
        pVgroup->numOfCachedTables = pVload->numOfCachedTables;
S
Shengliang Guan 已提交
430 431 432 433 434 435
        pVgroup->numOfTables = pVload->numOfTables;
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
        pVgroup->totalStorage = pVload->totalStorage;
        pVgroup->compStorage = pVload->compStorage;
        pVgroup->pointsWritten = pVload->pointsWritten;
      }
L
Liu Jicong 已提交
436
      bool roleChanged = false;
S
Shengliang Guan 已提交
437
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
438 439 440 441 442 443
        SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
        if (pGid->dnodeId == statusReq.dnodeId) {
          if (pGid->syncState != pVload->syncState || pGid->syncRestore != pVload->syncRestore ||
              pGid->syncCanRead != pVload->syncCanRead) {
            mInfo(
                "vgId:%d, state changed by status msg, old state:%s restored:%d canRead:%d new state:%s restored:%d "
444
                "canRead:%d, dnode:%d",
445
                pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead,
446
                syncStr(pVload->syncState), pVload->syncRestore, pVload->syncCanRead, pDnode->id);
447 448 449
            pGid->syncState = pVload->syncState;
            pGid->syncRestore = pVload->syncRestore;
            pGid->syncCanRead = pVload->syncCanRead;
M
Minghao Li 已提交
450 451
            roleChanged = true;
          }
S
Shengliang Guan 已提交
452
          break;
L
Liu Jicong 已提交
453
        }
S
Shengliang Guan 已提交
454
      }
L
Liu Jicong 已提交
455
      if (roleChanged) {
456
        SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
457
        if (pDb != NULL && pDb->stateTs != curMs) {
dengyihao's avatar
dengyihao 已提交
458 459
          mInfo("db:%s, stateTs changed by status msg, old stateTs:%" PRId64 " new stateTs:%" PRId64, pDb->name,
                pDb->stateTs, curMs);
460 461 462
          pDb->stateTs = curMs;
        }
        mndReleaseDb(pMnode, pDb);
L
Liu Jicong 已提交
463
      }
S
Shengliang Guan 已提交
464 465 466 467 468
    }

    mndReleaseVgroup(pMnode, pVgroup);
  }

469 470
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
471 472 473 474 475
    if (pObj->syncState != statusReq.mload.syncState || pObj->syncRestore != statusReq.mload.syncRestore) {
      mInfo("dnode:%d, mnode syncState from %s to %s, restoreState from %d to %d", pObj->id, syncStr(pObj->syncState),
            syncStr(statusReq.mload.syncState), pObj->syncRestore, statusReq.mload.syncRestore);
      pObj->syncState = statusReq.mload.syncState;
      pObj->syncRestore = statusReq.mload.syncRestore;
476 477 478 479 480
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

D
dapan1121 已提交
481 482 483 484 485 486
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
  if (pQnode != NULL) {
    pQnode->load = statusReq.qload;
    mndReleaseQnode(pMnode, pQnode);
  }

487
  if (needCheck) {
S
Shengliang Guan 已提交
488
    if (statusReq.sver != tsVersion) {
S
Shengliang Guan 已提交
489
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
490
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
S
Shengliang Guan 已提交
491
      }
S
Shengliang Guan 已提交
492
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver, tsVersion);
493
      terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
S
Shengliang Guan 已提交
494
      goto _OVER;
S
Shengliang Guan 已提交
495 496
    }

S
Shengliang Guan 已提交
497
    if (statusReq.dnodeId == 0) {
S
Shengliang Guan 已提交
498
      mInfo("dnode:%d, %s first access, clusterId:%" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
S
Shengliang Guan 已提交
499
    } else {
S
Shengliang Guan 已提交
500
      if (statusReq.clusterId != pMnode->clusterId) {
S
Shengliang Guan 已提交
501 502 503
        if (pDnode != NULL) {
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
        }
S
Shengliang Guan 已提交
504
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
S
Shengliang Guan 已提交
505 506
               pMnode->clusterId);
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
S
Shengliang Guan 已提交
507
        goto _OVER;
S
Shengliang Guan 已提交
508
      }
S
Shengliang Guan 已提交
509 510 511
    }

    // Verify whether the cluster parameters are consistent when status change from offline to ready
S
Shengliang Guan 已提交
512 513 514
    pDnode->offlineReason = mndCheckClusterCfgPara(pMnode, pDnode, &statusReq.clusterCfg);
    if (pDnode->offlineReason != 0) {
      mError("dnode:%d, cluster cfg inconsistent since:%s", pDnode->id, offlineReason[pDnode->offlineReason]);
515
      terrno = TSDB_CODE_MND_INVALID_CLUSTER_CFG;
S
Shengliang Guan 已提交
516
      goto _OVER;
S
Shengliang Guan 已提交
517 518
    }

519
    if (!online) {
520 521
      mInfo("dnode:%d, from offline to online, memory avail:%" PRId64 " total:%" PRId64 " cores:%.2f", pDnode->id,
            statusReq.memAvail, statusReq.memTotal, statusReq.numOfCores);
522
    } else {
523
      mInfo("dnode:%d, send dnode epset, online:%d dnodeVer:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
H
Hongze Cheng 已提交
524
            statusReq.dnodeVer, dnodeVer, reboot);
525
    }
S
Shengliang Guan 已提交
526

S
Shengliang Guan 已提交
527 528 529
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
530 531
    pDnode->memAvail = statusReq.memAvail;
    pDnode->memTotal = statusReq.memTotal;
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533
    SStatusRsp statusRsp = {0};
534
    statusRsp.statusSeq++;
535
    statusRsp.dnodeVer = dnodeVer;
S
Shengliang Guan 已提交
536 537 538 539
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
    if (statusRsp.pDnodeEps == NULL) {
S
Shengliang Guan 已提交
540
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
541
      goto _OVER;
S
Shengliang Guan 已提交
542 543
    }

S
Shengliang Guan 已提交
544 545
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

S
Shengliang Guan 已提交
546
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
547
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
548
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
549
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
550

S
Shengliang Guan 已提交
551 552
    pReq->info.rspLen = contLen;
    pReq->info.rsp = pHead;
S
Shengliang Guan 已提交
553
  }
S
Shengliang Guan 已提交
554

555 556
  pDnode->accessTimes++;
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
557 558
  code = 0;

S
Shengliang Guan 已提交
559
_OVER:
S
Shengliang Guan 已提交
560
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
561
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
562
  return code;
S
Shengliang Guan 已提交
563 564
}

S
Shengliang Guan 已提交
565
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
566 567 568 569
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
570
  SDnodeObj dnodeObj = {0};
S
Shengliang Guan 已提交
571
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
S
Shengliang Guan 已提交
572 573
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
574
  dnodeObj.port = pCreate->port;
S
Shengliang Guan 已提交
575
  tstrncpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
576
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", pCreate->fqdn, pCreate->port);
S
Shengliang Guan 已提交
577

578
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_GLOBAL, pReq, "create-dnode");
S
Shengliang Guan 已提交
579
  if (pTrans == NULL) goto _OVER;
580
  mInfo("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
581
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
582

S
Shengliang Guan 已提交
583 584
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
585
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
586
  pRaw = NULL;
S
Shengliang Guan 已提交
587

S
Shengliang Guan 已提交
588 589
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
S
Shengliang Guan 已提交
590

S
Shengliang Guan 已提交
591
_OVER:
S
Shengliang Guan 已提交
592
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
593 594
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
595 596
}

C
cadem 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static int32_t mndRestoreDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, int8_t restoreType) {
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "restore-dnode");
  if (pTrans == NULL) goto _OVER;

  mndTransSetSerial(pTrans);

  mInfo("trans:%d, used to restore dnode:%s", pTrans->id, pDnode->ep);

  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;

  if(restoreType == RESTORE_TYPE__ALL || restoreType == RESTORE_TYPE__MNODE)
  {
    SMnodeObj *mnodeObj = mndAcquireMnode(pMnode, pDnode->id);
    if(mnodeObj == NULL){
      mError("trans:%d, no mnode exist on dnode:%s", pTrans->id, pDnode->ep);
    }
    else
    {
      SMnodeObj newMnodeObj = {0};
      newMnodeObj.id = pDnode->id;
      newMnodeObj.createdTime = taosGetTimestampMs();
      newMnodeObj.updateTime = newMnodeObj.createdTime;
      newMnodeObj.role = TAOS_SYNC_ROLE_LEARNER;
      newMnodeObj.lastIndex = pMnode->applied;
      if (mndSetRestoreCreateMnodeRedoActions(pMnode, pTrans, pDnode, &newMnodeObj) != 0) goto _OVER;
      if (mndSetRestoreCreateMnodeRedoLogs(pMnode, pTrans, &newMnodeObj) != 0) goto _OVER;

      SMnodeObj mnodeLeaderObj = {0};
      mnodeLeaderObj.id = pDnode->id;
      mnodeLeaderObj.createdTime = taosGetTimestampMs();
      mnodeLeaderObj.updateTime = mnodeLeaderObj.createdTime;
      mnodeLeaderObj.role = TAOS_SYNC_ROLE_VOTER;
      mnodeLeaderObj.lastIndex = pMnode->applied + 1;
      if (mndSetRestoreAlterMnodeTypeRedoActions(pMnode, pTrans, pDnode, &mnodeLeaderObj) != 0) goto _OVER;
      if (mndSetRestoreCreateMnodeRedoLogs(pMnode, pTrans, &mnodeLeaderObj) != 0) goto _OVER;

      if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeLeaderObj) != 0) goto _OVER;

      mndReleaseMnode(pMnode, mnodeObj);
    }
  }

  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL; 

  if(restoreType == RESTORE_TYPE__ALL || restoreType == RESTORE_TYPE__VNODE){
    while (1) {
      SVgObj *pVgroup = NULL;
      pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
      if (pIter == NULL) break;

      if (mndVgroupInDnode(pVgroup, pDnode->id)) {
        SDbObj *db = mndAcquireDb(pMnode, pVgroup->dbName);
        if(db == NULL){
          sdbCancelFetch(pSdb, pIter);
          sdbRelease(pSdb, pVgroup);
          goto _OVER;
        }
        if (mndBuildRestoreAlterVgroupAction(pMnode, pTrans, db, pVgroup, pDnode) != 0) {
          sdbCancelFetch(pSdb, pIter);
          mndReleaseDb(pMnode, db);
          sdbRelease(pSdb, pVgroup);
          goto _OVER;
        }
        mndReleaseDb(pMnode, db);
      }

      sdbRelease(pSdb, pVgroup);
    }
  }
  
  if(restoreType == RESTORE_TYPE__ALL || restoreType == RESTORE_TYPE__QNODE){
    pIter = NULL;
    while (1) {
      SQnodeObj *pQnode = NULL;
      pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pQnode);
      if (pIter == NULL) break;

      if (mndQnodeInDnode(pQnode, pDnode->id)) {
        if (mndSetCreateQnodeCommitLogs(pTrans, pQnode) != 0) goto _OVER;
        if (mndSetCreateQnodeRedoActions(pTrans, pDnode, pQnode) != 0) goto _OVER;
      }

      sdbRelease(pSdb, pQnode);
    }
  }

  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;

_OVER:

  mndTransDrop(pTrans);
  sdbFreeRaw(pRaw);
  return code;
}

D
dapan1121 已提交
698
static int32_t mndProcessDnodeListReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
699 700 701 702
  SMnode       *pMnode = pReq->info.node;
  SSdb         *pSdb = pMnode->pSdb;
  SDnodeObj    *pObj = NULL;
  void         *pIter = NULL;
D
dapan1121 已提交
703
  SDnodeListRsp rsp = {0};
S
Shengliang Guan 已提交
704 705
  int32_t       code = -1;

D
dapan1121 已提交
706 707 708 709 710 711
  rsp.dnodeList = taosArrayInit(5, sizeof(SEpSet));
  if (NULL == rsp.dnodeList) {
    mError("failed to alloc epSet while process dnode list req");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }
S
Shengliang Guan 已提交
712

D
dapan1121 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  while (1) {
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SEpSet epSet = {0};
    epSet.numOfEps = 1;
    tstrncpy(epSet.eps[0].fqdn, pObj->fqdn, TSDB_FQDN_LEN);
    epSet.eps[0].port = pObj->port;

    (void)taosArrayPush(rsp.dnodeList, &epSet);

    sdbRelease(pSdb, pObj);
  }

  int32_t rspLen = tSerializeSDnodeListRsp(NULL, 0, &rsp);
  void   *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSDnodeListRsp(pRsp, rspLen, &rsp);

  pReq->info.rspLen = rspLen;
  pReq->info.rsp = pRsp;
  code = 0;

_OVER:

  if (code != 0) {
    mError("failed to get dnode list since %s", terrstr());
  }

  tFreeSDnodeListRsp(&rsp);

  return code;
}

D
dapan1121 已提交
751 752
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
  SShowVariablesRsp rsp = {0};
753 754 755 756 757
  int32_t           code = -1;

  if (mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIBALES) != 0) {
    goto _OVER;
  }
D
dapan1121 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

  rsp.variables = taosArrayInit(4, sizeof(SVariablesInfo));
  if (NULL == rsp.variables) {
    mError("failed to alloc SVariablesInfo array while process show variables req");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  SVariablesInfo info = {0};

  strcpy(info.name, "statusInterval");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "timezone");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
  taosArrayPush(rsp.variables, &info);
775

D
dapan1121 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
  strcpy(info.name, "locale");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "charset");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
  taosArrayPush(rsp.variables, &info);

  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
  void   *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp);

  pReq->info.rspLen = rspLen;
  pReq->info.rsp = pRsp;
  code = 0;

_OVER:

  if (code != 0) {
    mError("failed to get show variables info since %s", terrstr());
  }

  tFreeSShowVariablesRsp(&rsp);

  return code;
}

S
Shengliang Guan 已提交
808 809
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
810 811 812 813
  int32_t         code = -1;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

C
Cary Xu 已提交
814 815 816 817
  if ((terrno = grantCheck(TSDB_GRANT_DNODE)) != 0) {
    code = terrno;
    goto _OVER;
  }
818

S
Shengliang Guan 已提交
819
  if (tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
820
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
821
    goto _OVER;
S
Shengliang Guan 已提交
822 823
  }

S
Shengliang Guan 已提交
824
  mInfo("dnode:%s:%d, start to create", createReq.fqdn, createReq.port);
825
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_DNODE) != 0) {
S
Shengliang Guan 已提交
826 827
    goto _OVER;
  }
S
Shengliang Guan 已提交
828

S
Shengliang Guan 已提交
829
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
830
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
831
    goto _OVER;
S
Shengliang Guan 已提交
832 833
  }

S
Shengliang Guan 已提交
834
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
835 836
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
837
  if (pDnode != NULL) {
838
    terrno = TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
Shengliang Guan 已提交
839
    goto _OVER;
S
Shengliang Guan 已提交
840
  }
S
Shengliang Guan 已提交
841

S
Shengliang Guan 已提交
842
  code = mndCreateDnode(pMnode, pReq, &createReq);
S
Shengliang Guan 已提交
843
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
844

S
Shengliang Guan 已提交
845
_OVER:
S
Shengliang Guan 已提交
846
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
847
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
848 849
  }

S
Shengliang Guan 已提交
850 851
  mndReleaseDnode(pMnode, pDnode);
  return code;
S
Shengliang Guan 已提交
852 853
}

C
cadem 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
extern int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq);

int32_t mndProcessRestoreDnodeReq(SRpcMsg *pReq){
  return mndProcessRestoreDnodeReqImpl(pReq);
}

#ifndef TD_ENTERPRISE
int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq){
  return 0;
}
#endif

/*
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
  int32_t       code = -1;
  SDnodeObj    *pDnode = NULL;
  SMnodeObj    *pMObj = NULL;
  SQnodeObj    *pQObj = NULL;
  SSnodeObj    *pSObj = NULL;
  SRestoreDnodeReq restoreReq = {0};

  if (tDeserializeSRestoreDnodeReq(pReq->pCont, pReq->contLen, &restoreReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  mInfo("dnode:%d, start to restore, ep:%s:%d", restoreReq.dnodeId, restoreReq.fqdn, restoreReq.port);
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
    goto _OVER;
  }

  pDnode = mndAcquireDnode(pMnode, restoreReq.dnodeId);
  if (pDnode == NULL) {
    int32_t err = terrno;
    char    ep[TSDB_EP_LEN + 1] = {0};
    snprintf(ep, sizeof(ep), restoreReq.fqdn, restoreReq.port);
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
    if (pDnode == NULL) {
      terrno = err;
      goto _OVER;
    }
  }

  code = mndRestoreDnode(pMnode, pReq, pDnode, restoreReq.restoreType);
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;

_OVER:
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
    mError("dnode:%d, failed to restore since %s", restoreReq.dnodeId, terrstr());
  }

  mndReleaseDnode(pMnode, pDnode);
  return code;
}
*/

911
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, SQnodeObj *pQObj,
S
Shengliang Guan 已提交
912
                            SSnodeObj *pSObj, int32_t numOfVnodes, bool force) {
S
Shengliang Guan 已提交
913 914 915 916
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

917
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-dnode");
S
Shengliang Guan 已提交
918
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
919
  mndTransSetSerial(pTrans);
S
Shengliang Guan 已提交
920
  mInfo("trans:%d, used to drop dnode:%d, force:%d", pTrans->id, pDnode->id, force);
921
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
922

S
Shengliang Guan 已提交
923
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
924 925
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
926
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
S
Shengliang Guan 已提交
927
  pRaw = NULL;
S
Shengliang Guan 已提交
928

S
Shengliang Guan 已提交
929
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
930 931
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
932
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
933 934
  pRaw = NULL;

S
Shengliang Guan 已提交
935
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
936
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
937
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj, force) != 0) goto _OVER;
S
Shengliang Guan 已提交
938
  }
939 940 941

  if (pQObj != NULL) {
    mInfo("trans:%d, qnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
942
    if (mndSetDropQnodeInfoToTrans(pMnode, pTrans, pQObj, force) != 0) goto _OVER;
943 944 945 946
  }

  if (pSObj != NULL) {
    mInfo("trans:%d, snode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
947
    if (mndSetDropSnodeInfoToTrans(pMnode, pTrans, pSObj, force) != 0) goto _OVER;
948 949
  }

S
Shengliang Guan 已提交
950
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
951
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
S
Shengliang Guan 已提交
952
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id, force) != 0) goto _OVER;
S
Shengliang Guan 已提交
953
  }
S
Shengliang Guan 已提交
954

S
Shengliang Guan 已提交
955
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
956

S
Shengliang Guan 已提交
957 958 959
  code = 0;

_OVER:
S
Shengliang Guan 已提交
960
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
961 962
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
963 964
}

S
Shengliang Guan 已提交
965
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
966 967 968 969 970 971 972 973 974
  SMnode       *pMnode = pReq->info.node;
  int32_t       code = -1;
  SDnodeObj    *pDnode = NULL;
  SMnodeObj    *pMObj = NULL;
  SQnodeObj    *pQObj = NULL;
  SSnodeObj    *pSObj = NULL;
  SDropDnodeReq dropReq = {0};

  if (tDeserializeSDropDnodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
975
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
976
    goto _OVER;
S
Shengliang Guan 已提交
977
  }
S
Shengliang Guan 已提交
978

979
  mInfo("dnode:%d, start to drop, ep:%s:%d", dropReq.dnodeId, dropReq.fqdn, dropReq.port);
980
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
981 982
    goto _OVER;
  }
S
Shengliang Guan 已提交
983

S
Shengliang Guan 已提交
984
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
985
  if (pDnode == NULL) {
986 987
    int32_t err = terrno;
    char    ep[TSDB_EP_LEN + 1] = {0};
988 989 990
    snprintf(ep, sizeof(ep), dropReq.fqdn, dropReq.port);
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
    if (pDnode == NULL) {
991
      terrno = err;
992 993
      goto _OVER;
    }
S
Shengliang Guan 已提交
994 995
  }

996 997
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
998 999
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
1000 1001 1002 1003 1004
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
1005 1006 1007 1008 1009 1010
      terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
      goto _OVER;
    }
  }

  int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);
S
Shengliang Guan 已提交
1011
  if ((numOfVnodes > 0 || pMObj != NULL || pSObj != NULL || pQObj != NULL) && !dropReq.force) {
S
Shengliang Guan 已提交
1012
    if (!mndIsDnodeOnline(pDnode, taosGetTimestampMs())) {
1013
      terrno = TSDB_CODE_DNODE_OFFLINE;
S
Shengliang Guan 已提交
1014 1015
      mError("dnode:%d, failed to drop since %s, vnodes:%d mnode:%d qnode:%d snode:%d", pDnode->id, terrstr(),
             numOfVnodes, pMObj != NULL, pQObj != NULL, pSObj != NULL);
S
Shengliang Guan 已提交
1016 1017
      goto _OVER;
    }
1018 1019
  }

S
Shengliang Guan 已提交
1020
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, numOfVnodes, dropReq.force);
S
Shengliang Guan 已提交
1021
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1022

S
Shengliang Guan 已提交
1023
_OVER:
S
Shengliang Guan 已提交
1024
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1025
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
1026 1027
  }

1028
  mndReleaseDnode(pMnode, pDnode);
1029
  mndReleaseMnode(pMnode, pMObj);
1030 1031
  mndReleaseQnode(pMnode, pQObj);
  mndReleaseSnode(pMnode, pSObj);
S
Shengliang Guan 已提交
1032
  return code;
S
Shengliang Guan 已提交
1033 1034
}

S
Shengliang Guan 已提交
1035
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
1036 1037
  SMnode     *pMnode = pReq->info.node;
  const char *options[] = {
1038 1039 1040
      "debugFlag",   "dDebugFlag",   "vDebugFlag",   "mDebugFlag",   "wDebugFlag",    "sDebugFlag",   "tsdbDebugFlag",
      "tqDebugFlag", "fsDebugFlag",  "udfDebugFlag", "smaDebugFlag", "idxDebugFlag",  "tdbDebugFlag", "tmrDebugFlag",
      "uDebugFlag",  "smaDebugFlag", "rpcDebugFlag", "qDebugFlag",   "metaDebugFlag",
1041 1042
  };
  int32_t optionSize = tListLen(options);
S
Shengliang Guan 已提交
1043 1044

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
1045
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
1046 1047 1048
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
1049

1050
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
1051
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE) != 0) {
S
Shengliang Guan 已提交
1052 1053 1054
    return -1;
  }

S
Shengliang Guan 已提交
1055
  SDCfgDnodeReq dcfgReq = {0};
1056 1057 1058
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
    strcpy(dcfgReq.config, "resetlog");
  } else if (strncasecmp(cfgReq.config, "monitor", 7) == 0) {
1059 1060 1061 1062 1063 1064
    if (' ' != cfgReq.config[7] && 0 != cfgReq.config[7]) {
      mError("dnode:%d, failed to config monitor since invalid conf:%s", cfgReq.dnodeId, cfgReq.config);
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

S
Shengliang Guan 已提交
1065 1066 1067
    const char *value = cfgReq.value;
    int32_t     flag = atoi(value);
    if (flag <= 0) {
1068
      flag = atoi(cfgReq.config + 8);
S
Shengliang Guan 已提交
1069
    }
1070 1071
    if (flag < 0 || flag > 2) {
      mError("dnode:%d, failed to config monitor since value:%d", cfgReq.dnodeId, flag);
S
Shengliang Guan 已提交
1072 1073 1074 1075
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

1076
    strcpy(dcfgReq.config, "monitor");
S
Shengliang Guan 已提交
1077 1078
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
  } else {
1079 1080 1081 1082 1083 1084
    bool findOpt = false;
    for (int32_t d = 0; d < optionSize; ++d) {
      const char *optName = options[d];
      int32_t     optLen = strlen(optName);
      if (strncasecmp(cfgReq.config, optName, optLen) != 0) continue;

1085 1086 1087 1088 1089 1090
      if (' ' != cfgReq.config[optLen] && 0 != cfgReq.config[optLen]) {
        mError("dnode:%d, failed to config since invalid conf:%s", cfgReq.dnodeId, cfgReq.config);
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
      }

1091
      const char *value = cfgReq.value;
M
Minglei Jin 已提交
1092
      int32_t     flag = atoi(value);
1093 1094 1095
      if (flag <= 0) {
        flag = atoi(cfgReq.config + optLen + 1);
      }
1096
      if (flag < 0 || flag > 255) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        mError("dnode:%d, failed to config %s since value:%d", cfgReq.dnodeId, optName, flag);
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
      }

      tstrncpy(dcfgReq.config, optName, optLen + 1);
      snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
      findOpt = true;
    }

    if (!findOpt) {
      terrno = TSDB_CODE_INVALID_CFG;
      mError("dnode:%d, failed to config since %s", cfgReq.dnodeId, terrstr());
      return -1;
    }
S
Shengliang Guan 已提交
1112 1113
  }

S
Shengliang Guan 已提交
1114
  int32_t code = -1;
1115 1116
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1117 1118 1119 1120
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1121

S
Shengliang Guan 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130
    if (pDnode->id == cfgReq.dnodeId || cfgReq.dnodeId == -1 || cfgReq.dnodeId == 0) {
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, &dcfgReq);
      void   *pBuf = rpcMallocCont(bufLen);

      if (pBuf != NULL) {
        tSerializeSDCfgDnodeReq(pBuf, bufLen, &dcfgReq);
        mInfo("dnode:%d, send config req to dnode, app:%p config:%s value:%s", cfgReq.dnodeId, pReq->info.ahandle,
              dcfgReq.config, dcfgReq.value);
dengyihao's avatar
dengyihao 已提交
1131
        SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen, .info = pReq->info};
S
Shengliang Guan 已提交
1132 1133 1134 1135
        tmsgSendReq(&epSet, &rpcMsg);
        code = 0;
      }
    }
S
Shengliang Guan 已提交
1136

S
Shengliang Guan 已提交
1137 1138
    sdbRelease(pSdb, pDnode);
  }
1139

S
Shengliang Guan 已提交
1140 1141 1142 1143
  if (code == -1) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
  }
  return code;
S
Shengliang Guan 已提交
1144 1145
}

S
Shengliang Guan 已提交
1146
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
1147
  mInfo("config rsp from dnode");
S
Shengliang Guan 已提交
1148
  return 0;
1149
}
S
Shengliang Guan 已提交
1150

S
Shengliang Guan 已提交
1151 1152
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1153
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
1154 1155
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
D
dapan1121 已提交
1156
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONFIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
1157
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
1158 1159
  int32_t cols = 0;

S
Shengliang Guan 已提交
1160
  cfgOpts[totalRows] = "statusInterval";
D
dapan1121 已提交
1161
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
S
Shengliang Guan 已提交
1162
  totalRows++;
S
Shengliang Guan 已提交
1163

S
Shengliang Guan 已提交
1164
  cfgOpts[totalRows] = "timezone";
D
dapan1121 已提交
1165
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
1166
  totalRows++;
S
Shengliang Guan 已提交
1167

S
Shengliang Guan 已提交
1168
  cfgOpts[totalRows] = "locale";
D
dapan1121 已提交
1169
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
S
Shengliang Guan 已提交
1170
  totalRows++;
S
Shengliang Guan 已提交
1171

S
Shengliang Guan 已提交
1172
  cfgOpts[totalRows] = "charset";
D
dapan1121 已提交
1173
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
S
Shengliang Guan 已提交
1174
  totalRows++;
S
Shengliang Guan 已提交
1175

1176
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
1177
  char bufVal[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
1178

S
Shengliang Guan 已提交
1179
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
1180 1181
    cols = 0;

1182
    STR_WITH_MAXSIZE_TO_VARSTR(buf, cfgOpts[i], TSDB_CONFIG_OPTION_LEN);
M
Minghao Li 已提交
1183
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1184
    colDataSetVal(pColInfo, numOfRows, (const char *)buf, false);
S
Shengliang Guan 已提交
1185

D
dapan1121 已提交
1186
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONFIG_VALUE_LEN);
1187
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1188
    colDataSetVal(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
1189 1190

    numOfRows++;
S
Shengliang Guan 已提交
1191 1192
  }

1193
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1194 1195 1196 1197 1198
  return numOfRows;
}

static void mndCancelGetNextConfig(SMnode *pMnode, void *pIter) {}

S
Shengliang Guan 已提交
1199 1200
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1201 1202 1203
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
1204
  ESdbStatus objStatus = 0;
S
Shengliang Guan 已提交
1205
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
1206
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
1207 1208

  while (numOfRows < rows) {
1209
    pShow->pIter = sdbFetchAll(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
1210
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
1211
    bool online = mndIsDnodeOnline(pDnode, curMs);
S
Shengliang Guan 已提交
1212 1213 1214

    cols = 0;

M
Minghao Li 已提交
1215
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1216
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->id, false);
1217 1218

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
1219
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
1220 1221

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1222
    colDataSetVal(pColInfo, numOfRows, buf, false);
1223 1224 1225

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    int16_t id = mndGetVnodesNum(pMnode, pDnode->id);
1226
    colDataSetVal(pColInfo, numOfRows, (const char *)&id, false);
S
Shengliang Guan 已提交
1227

1228
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1229
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->numOfSupportVnodes, false);
S
Shengliang Guan 已提交
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    const char *status = "ready";
    if (objStatus == SDB_STATUS_CREATING) status = "creating";
    if (objStatus == SDB_STATUS_DROPPING) status = "dropping";
    if (!online) {
      if (objStatus == SDB_STATUS_CREATING)
        status = "creating*";
      else if (objStatus == SDB_STATUS_DROPPING)
        status = "dropping*";
      else
        status = "offline";
    }

1243
    char b1[16] = {0};
1244
    STR_TO_VARSTR(b1, status);
1245
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1246
    colDataSetVal(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
1247

1248
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1249
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->createdTime, false);
S
Shengliang Guan 已提交
1250

C
cadem 已提交
1251 1252 1253
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->rebootTime, false);

wafwerar's avatar
wafwerar 已提交
1254
    char *b = taosMemoryCalloc(VARSTR_HEADER_SIZE + strlen(offlineReason[pDnode->offlineReason]) + 1, 1);
1255
    STR_TO_VARSTR(b, online ? "" : offlineReason[pDnode->offlineReason]);
S
Shengliang Guan 已提交
1256

1257
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1258
    colDataSetVal(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
1259
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
1260 1261 1262 1263 1264

    numOfRows++;
    sdbRelease(pSdb, pDnode);
  }

1265
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1266 1267 1268 1269 1270 1271
  return numOfRows;
}

static void mndCancelGetNextDnode(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
1272
}