mndDnode.c 27.5 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"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
S
Shengliang Guan 已提交
19
#include "mndMnode.h"
D
dapan1121 已提交
20
#include "mndQnode.h"
S
Shengliang Guan 已提交
21
#include "mndShow.h"
22
#include "mndSnode.h"
S
Shengliang Guan 已提交
23
#include "mndTrans.h"
S
Shengliang Guan 已提交
24
#include "mndUser.h"
S
Shengliang Guan 已提交
25
#include "mndVgroup.h"
S
Shengliang Guan 已提交
26

S
Shengliang Guan 已提交
27
#define TSDB_DNODE_VER_NUMBER   1
28
#define TSDB_DNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
static const char *offlineReason[] = {
S
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
    "",
    "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 已提交
44 45 46 47 48
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);
49
static int32_t  mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew);
D
dapan1121 已提交
50
static int32_t  mndProcessDnodeListReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
51

S
Shengliang Guan 已提交
52 53 54 55 56
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 已提交
57

S
Shengliang Guan 已提交
58
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
59
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
60
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
61
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
62 63

int32_t mndInitDnode(SMnode *pMnode) {
64 65 66 67 68 69 70 71 72 73
  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 已提交
74

S
Shengliang Guan 已提交
75 76 77
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
H
Hongze Cheng 已提交
78
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
S
Shengliang Guan 已提交
79
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
D
dapan1121 已提交
80
  mndSetMsgHandle(pMnode, TDMT_MND_DNODE_LIST, mndProcessDnodeListReq);
S
Shengliang Guan 已提交
81

82 83
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
S
Shengliang Guan 已提交
84 85 86
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
87 88 89 90 91 92
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
S
Shengliang Guan 已提交
93 94 95 96
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
97 98 99 100
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
101 102
  dnodeObj.port = tsServerPort;
  memcpy(&dnodeObj.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
103
  snprintf(dnodeObj.ep, TSDB_EP_LEN, "%s:%u", dnodeObj.fqdn, dnodeObj.port);
S
Shengliang Guan 已提交
104

S
Shengliang Guan 已提交
105 106 107
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL);
  if (pTrans == NULL) goto _OVER;
  mDebug("trans:%d, used to create dnode:%s on first deploy", pTrans->id, dnodeObj.ep);
108

S
Shengliang Guan 已提交
109 110
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
111
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
112
  pRaw = NULL;
113

S
Shengliang Guan 已提交
114 115
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
116

S
Shengliang Guan 已提交
117
_OVER:
118
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
119 120
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
121 122
}

S
Shengliang Guan 已提交
123
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
124 125
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
126
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
S
Shengliang Guan 已提交
127
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
128 129

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
130 131 132 133 134 135 136
  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);
137 138 139

  terrno = 0;

S
Shengliang Guan 已提交
140
_OVER:
141 142 143 144 145
  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 已提交
146

147
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
148 149 150 151
  return pRaw;
}

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
152
  SSdbRow *pRow = NULL;
153 154
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
155
  int8_t sver = 0;
S
Shengliang Guan 已提交
156
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
157
  if (sver != TSDB_DNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
158
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
159
    goto _OVER;
S
Shengliang Guan 已提交
160 161
  }

S
Shengliang Guan 已提交
162 163
  pRow = sdbAllocRow(sizeof(SDnodeObj));
  if (pRow == NULL) goto _OVER;
S
Shengliang Guan 已提交
164
  SDnodeObj *pDnode = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
165
  if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
166 167

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
168 169 170 171 172 173
  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)
174 175 176

  terrno = 0;

S
Shengliang Guan 已提交
177
_OVER:
178 179
  if (terrno != 0) {
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
180
    taosMemoryFreeClear(pRow);
181 182
    return NULL;
  }
S
Shengliang Guan 已提交
183

184
  mTrace("dnode:%d, decode from raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
185 186 187
  return pRow;
}

188
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
189
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
190
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
S
Shengliang Guan 已提交
191 192 193 194
  snprintf(pDnode->ep, TSDB_EP_LEN, "%s:%u", pDnode->fqdn, pDnode->port);
  return 0;
}

S
Shengliang Guan 已提交
195
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
196
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
197 198
  return 0;
}
S
Shengliang Guan 已提交
199

200
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
S
Shengliang Guan 已提交
201
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
202
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
203
  return 0;
S
Shengliang Guan 已提交
204 205
}

S
Shengliang Guan 已提交
206
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
207 208 209 210 211 212
  SSdb      *pSdb = pMnode->pSdb;
  SDnodeObj *pDnode = sdbAcquire(pSdb, SDB_DNODE, &dnodeId);
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
  }
  return pDnode;
S
Shengliang Guan 已提交
213
}
S
Shengliang Guan 已提交
214

S
Shengliang Guan 已提交
215 216 217
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
218 219
}

S
Shengliang Guan 已提交
220
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
H
Haojun Liao 已提交
221 222
  SEpSet epSet = {0};
  addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
223 224 225
  return epSet;
}

S
Shengliang Guan 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
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 已提交
239 240

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
241 242
  }

S
Shengliang Guan 已提交
243
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
244 245 246
  return NULL;
}

S
Shengliang Guan 已提交
247
int32_t mndGetDnodeSize(SMnode *pMnode) {
S
Shengliang Guan 已提交
248 249 250 251
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DNODE);
}

S
Shengliang Guan 已提交
252
bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs) {
dengyihao's avatar
dengyihao 已提交
253
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
254
  if (interval > 5000 * tsStatusInterval) {
S
Shengliang Guan 已提交
255 256 257
    if (pDnode->rebootTime > 0) {
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
    }
S
Shengliang Guan 已提交
258 259 260 261 262
    return false;
  }
  return true;
}

S
Shengliang Guan 已提交
263
static void mndGetDnodeData(SMnode *pMnode, SArray *pDnodeEps) {
S
Shengliang Guan 已提交
264 265
  SSdb *pSdb = pMnode->pSdb;

266
  int32_t numOfEps = 0;
S
Shengliang Guan 已提交
267 268 269 270 271 272
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
273 274 275 276 277 278
    SDnodeEp dnodeEp = {0};
    dnodeEp.id = pDnode->id;
    dnodeEp.isMnode = 0;
    dnodeEp.ep.port = pDnode->port;
    memcpy(dnodeEp.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);

S
Shengliang Guan 已提交
279
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
280
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
281
    }
S
Shengliang Guan 已提交
282

283
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
284
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
285 286 287
  }
}

S
Shengliang Guan 已提交
288
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
S
Shengliang Guan 已提交
289
  if (pCfg->statusInterval != tsStatusInterval) {
S
Shengliang Guan 已提交
290 291
    mError("dnode:%d, statusInterval:%d inconsistent with cluster:%d", pDnode->id, pCfg->statusInterval,
           tsStatusInterval);
S
Shengliang Guan 已提交
292 293 294
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
  }

wafwerar's avatar
wafwerar 已提交
295
  if ((0 != strcasecmp(pCfg->timezone, tsTimezoneStr)) && (pMnode->checkTime != pCfg->checkTime)) {
S
Shengliang Guan 已提交
296 297
    mError("dnode:%d, timezone:%s checkTime:%" PRId64 " inconsistent with cluster %s %" PRId64, pDnode->id,
           pCfg->timezone, pCfg->checkTime, tsTimezoneStr, pMnode->checkTime);
S
Shengliang Guan 已提交
298 299 300
    return DND_REASON_TIME_ZONE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
301
  if (0 != strcasecmp(pCfg->locale, tsLocale)) {
S
Shengliang Guan 已提交
302
    mError("dnode:%d, locale:%s inconsistent with cluster:%s", pDnode->id, pCfg->locale, tsLocale);
S
Shengliang Guan 已提交
303 304 305
    return DND_REASON_LOCALE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
306
  if (0 != strcasecmp(pCfg->charset, tsCharset)) {
S
Shengliang Guan 已提交
307
    mError("dnode:%d, charset:%s inconsistent with cluster:%s", pDnode->id, pCfg->charset, tsCharset);
S
Shengliang Guan 已提交
308 309 310 311 312 313
    return DND_REASON_CHARSET_NOT_MATCH;
  }

  return 0;
}

S
Shengliang Guan 已提交
314 315
static int32_t mndProcessStatusReq(SRpcMsg *pReq) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
316 317 318
  SStatusReq statusReq = {0};
  SDnodeObj *pDnode = NULL;
  int32_t    code = -1;
S
Shengliang Guan 已提交
319

S
Shengliang Guan 已提交
320
  if (tDeserializeSStatusReq(pReq->pCont, pReq->contLen, &statusReq) != 0) {
S
Shengliang Guan 已提交
321
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
322
    goto _OVER;
S
Shengliang Guan 已提交
323
  }
S
Shengliang Guan 已提交
324

S
Shengliang Guan 已提交
325 326
  if (statusReq.dnodeId == 0) {
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
327
    if (pDnode == NULL) {
S
Shengliang Guan 已提交
328
      mDebug("dnode:%s, not created yet", statusReq.dnodeEp);
S
Shengliang Guan 已提交
329
      goto _OVER;
S
Shengliang Guan 已提交
330 331
    }
  } else {
S
Shengliang Guan 已提交
332
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
S
Shengliang Guan 已提交
333
    if (pDnode == NULL) {
S
Shengliang Guan 已提交
334
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
335
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
336 337
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
      }
S
Shengliang Guan 已提交
338
      mError("dnode:%d, %s not exist", statusReq.dnodeId, statusReq.dnodeEp);
S
Shengliang Guan 已提交
339
      goto _OVER;
S
Shengliang Guan 已提交
340 341 342
    }
  }

S
Shengliang Guan 已提交
343
  for (int32_t v = 0; v < taosArrayGetSize(statusReq.pVloads); ++v) {
S
Shengliang Guan 已提交
344 345 346 347
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);

    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
    if (pVgroup != NULL) {
S
Shengliang Guan 已提交
348
      if (pVload->syncState == TAOS_SYNC_STATE_LEADER) {
S
Shengliang Guan 已提交
349 350 351 352 353 354
        pVgroup->numOfTables = pVload->numOfTables;
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
        pVgroup->totalStorage = pVload->totalStorage;
        pVgroup->compStorage = pVload->compStorage;
        pVgroup->pointsWritten = pVload->pointsWritten;
      }
L
Liu Jicong 已提交
355
      bool roleChanged = false;
S
Shengliang Guan 已提交
356
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
M
Minghao Li 已提交
357
        if (pVgroup->vnodeGid[vg].dnodeId == statusReq.dnodeId) {
M
Minghao Li 已提交
358
          if (pVgroup->vnodeGid[vg].role != pVload->syncState) {
M
Minghao Li 已提交
359 360
            roleChanged = true;
          }
M
Minghao Li 已提交
361
          pVgroup->vnodeGid[vg].role = pVload->syncState;
S
Shengliang Guan 已提交
362
          break;
L
Liu Jicong 已提交
363
        }
S
Shengliang Guan 已提交
364
      }
L
Liu Jicong 已提交
365 366 367
      if (roleChanged) {
        // notify scheduler role has changed
      }
S
Shengliang Guan 已提交
368 369 370 371 372
    }

    mndReleaseVgroup(pMnode, pVgroup);
  }

373 374 375
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
    if (pObj->state != statusReq.mload.syncState) {
376 377
      mInfo("dnode:%d, mnode syncstate from %s to %s", pObj->id, syncStr(pObj->state),
            syncStr(statusReq.mload.syncState));
378 379 380 381 382 383
      pObj->state = statusReq.mload.syncState;
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

D
dapan1121 已提交
384 385 386 387 388 389
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
  if (pQnode != NULL) {
    pQnode->load = statusReq.qload;
    mndReleaseQnode(pMnode, pQnode);
  }

390
  int64_t dnodeVer = sdbGetTableVer(pMnode->pSdb, SDB_DNODE) + sdbGetTableVer(pMnode->pSdb, SDB_MNODE);
S
Shengliang Guan 已提交
391
  int64_t curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
392
  bool    online = mndIsDnodeOnline(pDnode, curMs);
393
  bool    dnodeChanged = (statusReq.dnodeVer == 0) || (statusReq.dnodeVer != dnodeVer);
S
Shengliang Guan 已提交
394
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
395
  bool    needCheck = !online || dnodeChanged || reboot;
S
Shengliang Guan 已提交
396

397
  if (needCheck) {
S
Shengliang Guan 已提交
398
    if (statusReq.sver != tsVersion) {
S
Shengliang Guan 已提交
399
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
400
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
S
Shengliang Guan 已提交
401
      }
S
Shengliang Guan 已提交
402
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver, tsVersion);
403
      terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
S
Shengliang Guan 已提交
404
      goto _OVER;
S
Shengliang Guan 已提交
405 406
    }

S
Shengliang Guan 已提交
407
    if (statusReq.dnodeId == 0) {
S
Shengliang Guan 已提交
408
      mInfo("dnode:%d, %s first access, set clusterId %" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
S
Shengliang Guan 已提交
409
    } else {
S
Shengliang Guan 已提交
410
      if (statusReq.clusterId != pMnode->clusterId) {
S
Shengliang Guan 已提交
411 412 413
        if (pDnode != NULL) {
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
        }
S
Shengliang Guan 已提交
414
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
S
Shengliang Guan 已提交
415 416
               pMnode->clusterId);
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
S
Shengliang Guan 已提交
417
        goto _OVER;
S
Shengliang Guan 已提交
418 419 420 421
      } else {
        pDnode->accessTimes++;
        mTrace("dnode:%d, status received, access times %d", pDnode->id, pDnode->accessTimes);
      }
S
Shengliang Guan 已提交
422 423 424
    }

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

432 433 434
    if (!online) {
      mInfo("dnode:%d, from offline to online", pDnode->id);
    } else {
435
      mDebug("dnode:%d, send dnode epset, online:%d dnodeVer:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
436
             statusReq.dnodeVer, dnodeVer, reboot);
437
    }
S
Shengliang Guan 已提交
438

S
Shengliang Guan 已提交
439 440 441
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
S
Shengliang Guan 已提交
442

S
Shengliang Guan 已提交
443
    SStatusRsp statusRsp = {0};
444
    statusRsp.dnodeVer = dnodeVer;
S
Shengliang Guan 已提交
445 446 447 448
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
    if (statusRsp.pDnodeEps == NULL) {
S
Shengliang Guan 已提交
449
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
450
      goto _OVER;
S
Shengliang Guan 已提交
451 452
    }

S
Shengliang Guan 已提交
453 454
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

S
Shengliang Guan 已提交
455
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
456
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
457
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
458
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
459

S
Shengliang Guan 已提交
460 461
    pReq->info.rspLen = contLen;
    pReq->info.rsp = pHead;
S
Shengliang Guan 已提交
462
  }
S
Shengliang Guan 已提交
463

S
Shengliang Guan 已提交
464
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
465 466
  code = 0;

S
Shengliang Guan 已提交
467
_OVER:
S
Shengliang Guan 已提交
468
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
469
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
470
  return code;
S
Shengliang Guan 已提交
471 472
}

S
Shengliang Guan 已提交
473
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
474 475 476 477
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
478
  SDnodeObj dnodeObj = {0};
S
Shengliang Guan 已提交
479
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
S
Shengliang Guan 已提交
480 481
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
482 483
  dnodeObj.port = pCreate->port;
  memcpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
484
  snprintf(dnodeObj.ep, TSDB_EP_LEN, "%s:%u", dnodeObj.fqdn, dnodeObj.port);
S
Shengliang Guan 已提交
485

S
Shengliang Guan 已提交
486 487
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_GLOBAL, pReq);
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
488
  mDebug("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
S
Shengliang Guan 已提交
489

S
Shengliang Guan 已提交
490 491 492 493
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
  pRaw = NULL;
S
Shengliang Guan 已提交
494

S
Shengliang Guan 已提交
495 496
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
S
Shengliang Guan 已提交
497

S
Shengliang Guan 已提交
498
_OVER:
S
Shengliang Guan 已提交
499
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
500 501
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
502 503
}

D
dapan1121 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
static int32_t mndProcessDnodeListReq(SRpcMsg *pReq) {
  SMnode *pMnode = pReq->info.node;
  SSdb   *pSdb = pMnode->pSdb;
  SDnodeObj *pObj = NULL;
  void *pIter = NULL;
  SDnodeListRsp rsp = {0};
  int32_t code = -1;
  
  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;
  }
  
  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;
}


S
Shengliang Guan 已提交
558 559
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
560 561 562 563
  int32_t         code = -1;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

S
Shengliang Guan 已提交
564
  if (tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
565
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
566
    goto _OVER;
S
Shengliang Guan 已提交
567 568
  }

S
Shengliang Guan 已提交
569
  mInfo("dnode:%s:%d, start to create", createReq.fqdn, createReq.port);
S
Shengliang Guan 已提交
570

S
Shengliang Guan 已提交
571
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
572
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
573
    goto _OVER;
S
Shengliang Guan 已提交
574 575
  }

S
Shengliang Guan 已提交
576
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
577 578
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
579
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
580
    goto _OVER;
S
Shengliang Guan 已提交
581
  }
S
Shengliang Guan 已提交
582

583
  if (mndCheckOperAuth(pMnode, pReq->info.conn.user, MND_OPER_CREATE_DNODE) != 0) {
S
Shengliang Guan 已提交
584
    goto _OVER;
S
Shengliang Guan 已提交
585 586 587
  }

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

S
Shengliang Guan 已提交
590
_OVER:
S
Shengliang Guan 已提交
591
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
592
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
593 594
  }

S
Shengliang Guan 已提交
595 596
  mndReleaseDnode(pMnode, pDnode);
  return code;
S
Shengliang Guan 已提交
597 598
}

599 600
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, SQnodeObj *pQObj,
                            SSnodeObj *pSObj, int32_t numOfVnodes) {
S
Shengliang Guan 已提交
601 602 603 604 605 606
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq);
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
607
  mndTransSetSerial(pTrans);
S
Shengliang Guan 已提交
608
  mInfo("trans:%d, used to drop dnode:%d", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
609

S
Shengliang Guan 已提交
610 611 612 613
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
  pRaw = NULL;
S
Shengliang Guan 已提交
614

S
Shengliang Guan 已提交
615 616 617 618 619
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
  pRaw = NULL;

S
Shengliang Guan 已提交
620
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
621
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
622 623
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj) != 0) goto _OVER;
  }
624 625 626 627 628 629 630 631 632 633 634

  if (pQObj != NULL) {
    mInfo("trans:%d, qnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
    if (mndSetDropQnodeInfoToTrans(pMnode, pTrans, pQObj) != 0) goto _OVER;
  }

  if (pSObj != NULL) {
    mInfo("trans:%d, snode on dnode:%d will be dropped", pTrans->id, pDnode->id);
    if (mndSetDropSnodeInfoToTrans(pMnode, pTrans, pSObj) != 0) goto _OVER;
  }

S
Shengliang Guan 已提交
635
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
636
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
S
Shengliang Guan 已提交
637 638
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id) != 0) goto _OVER;
  }
S
Shengliang Guan 已提交
639

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

S
Shengliang Guan 已提交
642 643 644
  code = 0;

_OVER:
S
Shengliang Guan 已提交
645
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
646 647
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
648 649
}

S
Shengliang Guan 已提交
650
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
651 652 653 654 655 656 657 658 659
  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 已提交
660
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
661
    goto _OVER;
S
Shengliang Guan 已提交
662
  }
S
Shengliang Guan 已提交
663

664
  mInfo("dnode:%d, start to drop, ep:%s:%d", dropReq.dnodeId, dropReq.fqdn, dropReq.port);
S
Shengliang Guan 已提交
665

S
Shengliang Guan 已提交
666
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
667
  if (pDnode == NULL) {
668 669 670 671 672 673 674
    char ep[TSDB_EP_LEN + 1] = {0};
    snprintf(ep, sizeof(ep), dropReq.fqdn, dropReq.port);
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
    if (pDnode == NULL) {
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
      goto _OVER;
    }
S
Shengliang Guan 已提交
675 676
  }

677 678
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
679 680
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
681 682 683 684 685
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
686 687 688 689 690 691 692 693 694 695 696
      terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
      goto _OVER;
    }
  }

  int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);
  if (numOfVnodes > 0 || pMObj != NULL) {
    if (!mndIsDnodeOnline(pDnode, taosGetTimestampMs())) {
      terrno = TSDB_CODE_NODE_OFFLINE;
      mError("dnode:%d, failed to drop since %s, has_mnode:%d numOfVnodes:%d", pDnode->id, terrstr(), pMObj != NULL,
             numOfVnodes);
S
Shengliang Guan 已提交
697 698
      goto _OVER;
    }
699 700
  }

701
  if (mndCheckOperAuth(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
702
    goto _OVER;
S
Shengliang Guan 已提交
703 704
  }

705
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, numOfVnodes);
S
Shengliang Guan 已提交
706
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
707

S
Shengliang Guan 已提交
708
_OVER:
S
Shengliang Guan 已提交
709
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
710
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
711 712
  }

713
  mndReleaseDnode(pMnode, pDnode);
714
  mndReleaseMnode(pMnode, pMObj);
715 716
  mndReleaseQnode(pMnode, pQObj);
  mndReleaseSnode(pMnode, pSObj);
S
Shengliang Guan 已提交
717
  return code;
S
Shengliang Guan 已提交
718 719
}

S
Shengliang Guan 已提交
720 721
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
722 723

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
724
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
725 726 727
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
728

729
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
S
Shengliang Guan 已提交
730
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, cfgReq.dnodeId);
S
Shengliang Guan 已提交
731
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
732
    mError("dnode:%d, failed to config since %s ", cfgReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
733 734 735 736 737 738
    return -1;
  }

  SEpSet epSet = mndGetDnodeEpset(pDnode);
  mndReleaseDnode(pMnode, pDnode);

S
Shengliang Guan 已提交
739 740
  int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq);
  void   *pBuf = rpcMallocCont(bufLen);
S
Shengliang Guan 已提交
741 742

  if (pBuf == NULL) return -1;
S
Shengliang Guan 已提交
743 744
  tSerializeSMCfgDnodeReq(pBuf, bufLen, &cfgReq);

S
Shengliang Guan 已提交
745
  mDebug("dnode:%d, send config req to dnode, app:%p", cfgReq.dnodeId, pReq->info.ahandle);
746
  SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
S
Shengliang Guan 已提交
747
  return tmsgSendReq(&epSet, &rpcMsg);
S
Shengliang Guan 已提交
748 749
}

S
Shengliang Guan 已提交
750
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
751
  mDebug("config rsp from dnode, app:%p", pRsp->info.ahandle);
S
Shengliang Guan 已提交
752
  return 0;
753
}
S
Shengliang Guan 已提交
754

S
Shengliang Guan 已提交
755 756
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
757
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
758 759
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
D
dapan1121 已提交
760
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONFIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
761
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
762 763
  int32_t cols = 0;

S
Shengliang Guan 已提交
764
  cfgOpts[totalRows] = "statusInterval";
D
dapan1121 已提交
765
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
S
Shengliang Guan 已提交
766
  totalRows++;
S
Shengliang Guan 已提交
767

S
Shengliang Guan 已提交
768
  cfgOpts[totalRows] = "timezone";
D
dapan1121 已提交
769
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
770
  totalRows++;
S
Shengliang Guan 已提交
771

S
Shengliang Guan 已提交
772
  cfgOpts[totalRows] = "locale";
D
dapan1121 已提交
773
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
S
Shengliang Guan 已提交
774
  totalRows++;
S
Shengliang Guan 已提交
775

S
Shengliang Guan 已提交
776
  cfgOpts[totalRows] = "charset";
D
dapan1121 已提交
777
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
S
Shengliang Guan 已提交
778
  totalRows++;
S
Shengliang Guan 已提交
779

780
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
781
  char bufVal[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
782

S
Shengliang Guan 已提交
783
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
784 785
    cols = 0;

786
    STR_WITH_MAXSIZE_TO_VARSTR(buf, cfgOpts[i], TSDB_CONFIG_OPTION_LEN);
M
Minghao Li 已提交
787 788
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)buf, false);
S
Shengliang Guan 已提交
789

D
dapan1121 已提交
790
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONFIG_VALUE_LEN);
791
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
M
Minghao Li 已提交
792
    colDataAppend(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
793 794

    numOfRows++;
S
Shengliang Guan 已提交
795 796
  }

797
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
798 799 800 801 802
  return numOfRows;
}

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

S
Shengliang Guan 已提交
803 804
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
805 806 807 808
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
809
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
810 811 812 813

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
814
    bool online = mndIsDnodeOnline(pDnode, curMs);
S
Shengliang Guan 已提交
815 816 817

    cols = 0;

M
Minghao Li 已提交
818 819
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->id, false);
820 821

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
822
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
823 824 825 826 827 828

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, buf, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    int16_t id = mndGetVnodesNum(pMnode, pDnode->id);
M
Minghao Li 已提交
829
    colDataAppend(pColInfo, numOfRows, (const char *)&id, false);
S
Shengliang Guan 已提交
830

831 832
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->numOfSupportVnodes, false);
S
Shengliang Guan 已提交
833

834
    char b1[9] = {0};
M
Minghao Li 已提交
835
    STR_TO_VARSTR(b1, online ? "ready" : "offline");
836 837
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
838

839
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
M
Minghao Li 已提交
840
    colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->createdTime, false);
S
Shengliang Guan 已提交
841

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

845 846
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
847
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
848 849 850 851 852

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

853
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
854 855 856 857 858 859
  return numOfRows;
}

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