mndDnode.c 25.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"
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
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
98 99
  dnodeObj.port = tsServerPort;
  memcpy(&dnodeObj.fqdn, tsLocalFqdn, 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
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
    if (pObj->state != statusReq.mload.syncState) {
S
Shengliang Guan 已提交
373
      mInfo("dnode:%d, mnode syncstate from %s to %s", pObj->id, syncStr(pObj->state), syncStr(statusReq.mload.syncState));
374 375 376 377 378 379
      pObj->state = statusReq.mload.syncState;
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
500 501
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
502 503 504 505
  int32_t         code = -1;
  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
  }

S
Shengliang Guan 已提交
511
  mInfo("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

525
  if (mndCheckOperAuth(pMnode, pReq->info.conn.user, MND_OPER_CREATE_DNODE) != 0) {
S
Shengliang Guan 已提交
526
    goto _OVER;
S
Shengliang Guan 已提交
527 528 529
  }

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

S
Shengliang Guan 已提交
532
_OVER:
S
Shengliang Guan 已提交
533
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
534
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
535 536
  }

S
Shengliang Guan 已提交
537 538
  mndReleaseDnode(pMnode, pDnode);
  return code;
S
Shengliang Guan 已提交
539 540
}

S
Shengliang Guan 已提交
541
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, int32_t numOfVnodes) {
S
Shengliang Guan 已提交
542 543 544 545 546 547
  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 已提交
548
  mndTransSetSerial(pTrans);
S
Shengliang Guan 已提交
549
  mInfo("trans:%d, used to drop dnode:%d", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
550

S
Shengliang Guan 已提交
551 552 553 554
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
  pRaw = NULL;
S
Shengliang Guan 已提交
555

S
Shengliang Guan 已提交
556 557 558 559 560
  pRaw = mndDnodeActionEncode(pDnode);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
  pRaw = NULL;

S
Shengliang Guan 已提交
561
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
562
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
563 564 565
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj) != 0) goto _OVER;
  }
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
566
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
S
Shengliang Guan 已提交
567 568
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id) != 0) goto _OVER;
  }
S
Shengliang Guan 已提交
569
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
570

S
Shengliang Guan 已提交
571 572 573
  code = 0;

_OVER:
S
Shengliang Guan 已提交
574
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
575 576
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
577 578
}

S
Shengliang Guan 已提交
579 580
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
581 582
  int32_t        code = -1;
  SDnodeObj     *pDnode = NULL;
583
  SMnodeObj     *pMObj = NULL;
S
Shengliang Guan 已提交
584
  SMDropMnodeReq dropReq = {0};
S
Shengliang Guan 已提交
585

S
Shengliang Guan 已提交
586
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
587
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
588
    goto _OVER;
S
Shengliang Guan 已提交
589
  }
S
Shengliang Guan 已提交
590

S
Shengliang Guan 已提交
591
  mInfo("dnode:%d, start to drop", dropReq.dnodeId);
S
Shengliang Guan 已提交
592

S
Shengliang Guan 已提交
593
  if (dropReq.dnodeId <= 0) {
594
    terrno = TSDB_CODE_MND_INVALID_DNODE_ID;
S
Shengliang Guan 已提交
595
    goto _OVER;
S
Shengliang Guan 已提交
596 597
  }

S
Shengliang Guan 已提交
598
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
599 600
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
601 602 603
    goto _OVER;
  }

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

626
  if (mndCheckOperAuth(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
627
    goto _OVER;
S
Shengliang Guan 已提交
628 629
  }

S
Shengliang Guan 已提交
630
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, numOfVnodes);
S
Shengliang Guan 已提交
631
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
632

S
Shengliang Guan 已提交
633
_OVER:
S
Shengliang Guan 已提交
634
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
635
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
636 637
  }

638
  mndReleaseDnode(pMnode, pDnode);
639
  mndReleaseMnode(pMnode, pMObj);
S
Shengliang Guan 已提交
640
  return code;
S
Shengliang Guan 已提交
641 642
}

S
Shengliang Guan 已提交
643 644
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
645 646

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
647
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
648 649 650
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
651

S
Shengliang Guan 已提交
652
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, cfgReq.dnodeId);
S
Shengliang Guan 已提交
653
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
654
    mError("dnode:%d, failed to config since %s ", cfgReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
655 656 657 658 659 660
    return -1;
  }

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

S
Shengliang Guan 已提交
661 662
  int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq);
  void   *pBuf = rpcMallocCont(bufLen);
S
Shengliang Guan 已提交
663 664

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

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

S
Shengliang Guan 已提交
672
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
673
  mDebug("config rsp from dnode, app:%p", pRsp->info.ahandle);
S
Shengliang Guan 已提交
674
  return 0;
675
}
S
Shengliang Guan 已提交
676

S
Shengliang Guan 已提交
677 678
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
679
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
680 681 682
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONIIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
683
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
684 685
  int32_t cols = 0;

S
Shengliang Guan 已提交
686 687 688
  cfgOpts[totalRows] = "statusInterval";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%d", tsStatusInterval);
  totalRows++;
S
Shengliang Guan 已提交
689

S
Shengliang Guan 已提交
690
  cfgOpts[totalRows] = "timezone";
wafwerar's avatar
wafwerar 已提交
691
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
692
  totalRows++;
S
Shengliang Guan 已提交
693

S
Shengliang Guan 已提交
694 695 696
  cfgOpts[totalRows] = "locale";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsLocale);
  totalRows++;
S
Shengliang Guan 已提交
697

S
Shengliang Guan 已提交
698 699 700
  cfgOpts[totalRows] = "charset";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsCharset);
  totalRows++;
S
Shengliang Guan 已提交
701

702 703 704
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
  char bufVal[TSDB_CONIIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};

S
Shengliang Guan 已提交
705
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
706 707
    cols = 0;

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

712 713
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONIIG_VALUE_LEN);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
M
Minghao Li 已提交
714
    colDataAppend(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
715 716

    numOfRows++;
S
Shengliang Guan 已提交
717 718
  }

719
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
720 721 722 723 724
  return numOfRows;
}

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

S
Shengliang Guan 已提交
725 726
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
727 728 729 730
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
731
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
732 733 734 735

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

    cols = 0;

M
Minghao Li 已提交
740 741
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->id, false);
742 743

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
744
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
745 746 747 748 749 750

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

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

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

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

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

767 768
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
769
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
770 771 772 773 774

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

775
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
776 777 778 779 780 781
  return numOfRows;
}

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