mndDnode.c 25.2 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"
S
Shengliang Guan 已提交
22
#include "mndTrans.h"
S
Shengliang Guan 已提交
23
#include "mndUser.h"
S
Shengliang Guan 已提交
24
#include "mndVgroup.h"
S
Shengliang Guan 已提交
25

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

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

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

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

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

S
Shengliang Guan 已提交
73 74 75
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
H
Hongze Cheng 已提交
76
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
S
Shengliang Guan 已提交
77
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
S
Shengliang Guan 已提交
78

79 80
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
S
Shengliang Guan 已提交
81 82 83
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
84 85 86 87 88 89
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

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

S
Shengliang Guan 已提交
94 95 96 97 98 99
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
  dnodeObj.port = pMnode->replicas[0].port;
  memcpy(&dnodeObj.fqdn, pMnode->replicas[0].fqdn, TSDB_FQDN_LEN);
100
  snprintf(dnodeObj.ep, TSDB_EP_LEN, "%s:%u", dnodeObj.fqdn, dnodeObj.port);
S
Shengliang Guan 已提交
101

S
Shengliang Guan 已提交
102 103 104
  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);
105

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

S
Shengliang Guan 已提交
111 112
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
113

S
Shengliang Guan 已提交
114
_OVER:
115
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
116 117
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
118 119
}

S
Shengliang Guan 已提交
120
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
121 122
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

  terrno = 0;

S
Shengliang Guan 已提交
137
_OVER:
138 139 140 141 142
  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 已提交
143

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

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
149
  SSdbRow *pRow = NULL;
150 151
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

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

  terrno = 0;

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

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

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

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

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

S
Shengliang Guan 已提交
203
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
204 205 206 207 208 209
  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 已提交
210
}
S
Shengliang Guan 已提交
211

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

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

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

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
238 239
  }

S
Shengliang Guan 已提交
240
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
241 242 243
  return NULL;
}

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

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

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

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

S
Shengliang Guan 已提交
270 271 272 273 274 275
    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 已提交
276
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
277
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
278
    }
S
Shengliang Guan 已提交
279

280
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
281
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
282 283 284
  }
}

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

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

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

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

  return 0;
}

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

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

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

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

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

    mndReleaseVgroup(pMnode, pVgroup);
  }

370 371 372 373 374 375 376 377 378
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
    if (pObj->state != statusReq.mload.syncState) {
      pObj->state = statusReq.mload.syncState;
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

D
dapan1121 已提交
379 380 381 382 383 384
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
  if (pQnode != NULL) {
    pQnode->load = statusReq.qload;
    mndReleaseQnode(pMnode, pQnode);
  }

385
  int64_t dnodeVer = sdbGetTableVer(pMnode->pSdb, SDB_DNODE) + sdbGetTableVer(pMnode->pSdb, SDB_MNODE);
S
Shengliang Guan 已提交
386
  int64_t curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
387
  bool    online = mndIsDnodeOnline(pDnode, curMs);
388
  bool    dnodeChanged = (statusReq.dnodeVer != dnodeVer);
S
Shengliang Guan 已提交
389
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
390
  bool    needCheck = !online || dnodeChanged || reboot;
S
Shengliang Guan 已提交
391

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

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

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

427 428 429
    if (!online) {
      mInfo("dnode:%d, from offline to online", pDnode->id);
    } else {
S
Shengliang Guan 已提交
430
      mDebug("dnode:%d, send dnode epset, online:%d dnode_ver:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
431
             statusReq.dnodeVer, dnodeVer, reboot);
432
    }
S
Shengliang Guan 已提交
433

S
Shengliang Guan 已提交
434 435 436
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
S
Shengliang Guan 已提交
437

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

S
Shengliang Guan 已提交
448 449
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

S
Shengliang Guan 已提交
450
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
451
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
452
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
453
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
454

S
Shengliang Guan 已提交
455 456
    pReq->info.rspLen = contLen;
    pReq->info.rsp = pHead;
S
Shengliang Guan 已提交
457
  }
S
Shengliang Guan 已提交
458

S
Shengliang Guan 已提交
459
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
460 461
  code = 0;

S
Shengliang Guan 已提交
462
_OVER:
S
Shengliang Guan 已提交
463
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
464
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
465
  return code;
S
Shengliang Guan 已提交
466 467
}

S
Shengliang Guan 已提交
468
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
469 470 471 472
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

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

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

S
Shengliang Guan 已提交
485 486 487 488
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
  pRaw = NULL;
S
Shengliang Guan 已提交
489

S
Shengliang Guan 已提交
490 491
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
S
Shengliang Guan 已提交
492

S
Shengliang Guan 已提交
493
_OVER:
S
Shengliang Guan 已提交
494
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
495 496
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
497 498
}

S
Shengliang Guan 已提交
499 500
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
501 502 503 504 505
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

S
Shengliang Guan 已提交
506
  if (tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
507
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
508
    goto _OVER;
S
Shengliang Guan 已提交
509 510 511
  }

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

S
Shengliang Guan 已提交
513
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
514
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
515
    goto _OVER;
S
Shengliang Guan 已提交
516 517
  }

S
Shengliang Guan 已提交
518
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
519 520
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
521
  if (pDnode != NULL) {
S
Shengliang Guan 已提交
522
    goto _OVER;
S
Shengliang Guan 已提交
523
  }
S
Shengliang Guan 已提交
524

S
Shengliang Guan 已提交
525
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
526 527
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
S
Shengliang Guan 已提交
528
    goto _OVER;
S
Shengliang Guan 已提交
529
  }
S
Shengliang Guan 已提交
530

S
Shengliang Guan 已提交
531
  if (mndCheckNodeAuth(pUser) != 0) {
S
Shengliang Guan 已提交
532
    goto _OVER;
S
Shengliang Guan 已提交
533 534 535
  }

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

S
Shengliang Guan 已提交
538
_OVER:
S
Shengliang Guan 已提交
539
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
540
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
541 542
  }

S
Shengliang Guan 已提交
543 544 545
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
546 547
}

S
Shengliang Guan 已提交
548
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, int32_t numOfVnodes) {
S
Shengliang Guan 已提交
549 550 551 552 553 554
  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 已提交
555
  mndTransSetSerial(pTrans);
556
  mDebug("trans:%d, used to drop dnode:%d", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
557

S
Shengliang Guan 已提交
558 559 560 561
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
  pRaw = NULL;
S
Shengliang Guan 已提交
562

S
Shengliang Guan 已提交
563 564 565 566 567
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
  pRaw = NULL;

S
Shengliang Guan 已提交
568 569 570 571 572 573
  if (pMObj != NULL) {
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj) != 0) goto _OVER;
  }
  if (numOfVnodes > 0) {
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id) != 0) goto _OVER;
  }
S
Shengliang Guan 已提交
574
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
575

S
Shengliang Guan 已提交
576 577 578
  code = 0;

_OVER:
S
Shengliang Guan 已提交
579
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
580 581
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
582 583
}

S
Shengliang Guan 已提交
584 585
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
586 587 588
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SDnodeObj     *pDnode = NULL;
589
  SMnodeObj     *pMObj = NULL;
S
Shengliang Guan 已提交
590
  SMDropMnodeReq dropReq = {0};
S
Shengliang Guan 已提交
591

S
Shengliang Guan 已提交
592
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
593
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
594
    goto _OVER;
S
Shengliang Guan 已提交
595
  }
S
Shengliang Guan 已提交
596

S
Shengliang Guan 已提交
597
  mDebug("dnode:%d, start to drop", dropReq.dnodeId);
S
Shengliang Guan 已提交
598

S
Shengliang Guan 已提交
599
  if (dropReq.dnodeId <= 0) {
600
    terrno = TSDB_CODE_MND_INVALID_DNODE_ID;
S
Shengliang Guan 已提交
601
    goto _OVER;
S
Shengliang Guan 已提交
602 603
  }

S
Shengliang Guan 已提交
604
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
605 606
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
607 608 609
    goto _OVER;
  }

610 611
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
612 613 614 615 616
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
617 618 619 620 621 622 623 624 625 626 627
      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 已提交
628 629
      goto _OVER;
    }
630 631
  }

S
Shengliang Guan 已提交
632
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
633 634
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
S
Shengliang Guan 已提交
635
    goto _OVER;
S
Shengliang Guan 已提交
636 637
  }

S
Shengliang Guan 已提交
638
  if (mndCheckNodeAuth(pUser) != 0) {
S
Shengliang Guan 已提交
639
    goto _OVER;
S
Shengliang Guan 已提交
640 641
  }

S
Shengliang Guan 已提交
642
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, numOfVnodes);
S
Shengliang Guan 已提交
643
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
644

S
Shengliang Guan 已提交
645
_OVER:
S
Shengliang Guan 已提交
646
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
647
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
648 649
  }

650
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
651
  mndReleaseUser(pMnode, pUser);
652
  mndReleaseMnode(pMnode, pMObj);
S
Shengliang Guan 已提交
653
  return code;
S
Shengliang Guan 已提交
654 655
}

S
Shengliang Guan 已提交
656 657
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
658 659

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
660
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
661 662 663
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
664

S
Shengliang Guan 已提交
665
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, cfgReq.dnodeId);
S
Shengliang Guan 已提交
666
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
667
    mError("dnode:%d, failed to config since %s ", cfgReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
668 669 670 671 672 673
    return -1;
  }

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

S
Shengliang Guan 已提交
674 675
  int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq);
  void   *pBuf = rpcMallocCont(bufLen);
S
Shengliang Guan 已提交
676 677

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

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

S
Shengliang Guan 已提交
685
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
686
  mDebug("config rsp from dnode, app:%p", pRsp->info.ahandle);
S
Shengliang Guan 已提交
687
  return 0;
688
}
S
Shengliang Guan 已提交
689

S
Shengliang Guan 已提交
690 691
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
692
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
693 694 695
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONIIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
696
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
697 698
  int32_t cols = 0;

S
Shengliang Guan 已提交
699 700 701
  cfgOpts[totalRows] = "statusInterval";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%d", tsStatusInterval);
  totalRows++;
S
Shengliang Guan 已提交
702

S
Shengliang Guan 已提交
703
  cfgOpts[totalRows] = "timezone";
wafwerar's avatar
wafwerar 已提交
704
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
705
  totalRows++;
S
Shengliang Guan 已提交
706

S
Shengliang Guan 已提交
707 708 709
  cfgOpts[totalRows] = "locale";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsLocale);
  totalRows++;
S
Shengliang Guan 已提交
710

S
Shengliang Guan 已提交
711 712 713
  cfgOpts[totalRows] = "charset";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsCharset);
  totalRows++;
S
Shengliang Guan 已提交
714

715 716 717
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
  char bufVal[TSDB_CONIIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};

S
Shengliang Guan 已提交
718
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
719 720
    cols = 0;

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

725 726
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONIIG_VALUE_LEN);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
M
Minghao Li 已提交
727
    colDataAppend(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
728 729

    numOfRows++;
S
Shengliang Guan 已提交
730 731
  }

732
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
733 734 735 736 737
  return numOfRows;
}

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

S
Shengliang Guan 已提交
738 739
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
740 741 742 743
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
744
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
745 746 747 748

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

    cols = 0;

M
Minghao Li 已提交
753 754
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->id, false);
755 756

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
757
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
758 759 760 761 762 763

    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 已提交
764
    colDataAppend(pColInfo, numOfRows, (const char *)&id, false);
S
Shengliang Guan 已提交
765

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

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

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

777 778
    char b[tListLen(offlineReason) + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(b, online ? "" : offlineReason[pDnode->offlineReason]);
S
Shengliang Guan 已提交
779

780 781
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b, false);
S
Shengliang Guan 已提交
782 783 784 785 786

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

787
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
788 789 790 791 792 793
  return numOfRows;
}

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