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

S
Shengliang Guan 已提交
25
#define TSDB_DNODE_VER_NUMBER 1
26
#define TSDB_DNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
27 28 29
#define TSDB_CONFIG_OPTION_LEN 16
#define TSDB_CONIIG_VALUE_LEN 48
#define TSDB_CONFIG_NUMBER 8
S
Shengliang Guan 已提交
30

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

S
Shengliang Guan 已提交
52 53 54 55 56 57
static int32_t mndProcessCreateDnodeReq(SMnodeMsg *pReq);
static int32_t mndProcessDropDnodeReq(SMnodeMsg *pReq);
static int32_t mndProcessConfigDnodeReq(SMnodeMsg *pReq);
static int32_t mndProcessConfigDnodeRsp(SMnodeMsg *pRsp);
static int32_t mndProcessStatusReq(SMnodeMsg *pReq);

S
Shengliang Guan 已提交
58
static int32_t mndGetConfigMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
S
Shengliang Guan 已提交
59
static int32_t mndRetrieveConfigs(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
60
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
61
static int32_t mndGetDnodeMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
S
Shengliang Guan 已提交
62
static int32_t mndRetrieveDnodes(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
63
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
64 65 66 67 68 69 70 71 72 73 74

int32_t mndInitDnode(SMnode *pMnode) {
  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 已提交
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);
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81 82 83 84 85 86 87
  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_VARIABLES, mndGetConfigMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_VARIABLES, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_VARIABLES, mndCancelGetNextConfig);
  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndGetDnodeMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
  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);

  SSdbRaw *pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
103
  if (sdbSetRawStatus(pRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
104

105
  mDebug("dnode:%d, will be created while deploy sdb, raw:%p", dnodeObj.id, pRaw);
S
Shengliang Guan 已提交
106 107 108
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
109
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
110 111
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
112
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
113
  if (pRaw == NULL) goto DNODE_ENCODE_OVER;
S
Shengliang Guan 已提交
114 115

  int32_t dataPos = 0;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  SDB_SET_INT32(pRaw, dataPos, pDnode->id, DNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pDnode->createdTime, DNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pDnode->updateTime, DNODE_ENCODE_OVER)
  SDB_SET_INT16(pRaw, dataPos, pDnode->port, DNODE_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, DNODE_ENCODE_OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, DNODE_ENCODE_OVER)
  SDB_SET_DATALEN(pRaw, dataPos, DNODE_ENCODE_OVER);

  terrno = 0;

DNODE_ENCODE_OVER:
  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 已提交
132

133
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
134 135 136 137
  return pRaw;
}

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
138 139
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
140
  int8_t sver = 0;
141
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto DNODE_DECODE_OVER;
S
Shengliang Guan 已提交
142

S
Shengliang Guan 已提交
143
  if (sver != TSDB_DNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
144
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
145
    goto DNODE_DECODE_OVER;
S
Shengliang Guan 已提交
146 147
  }

148 149 150
  SSdbRow *pRow = sdbAllocRow(sizeof(SDnodeObj));
  if (pRow == NULL) goto DNODE_DECODE_OVER;

S
Shengliang Guan 已提交
151
  SDnodeObj *pDnode = sdbGetRowObj(pRow);
152
  if (pDnode == NULL) goto DNODE_DECODE_OVER;
S
Shengliang Guan 已提交
153 154

  int32_t dataPos = 0;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  SDB_GET_INT32(pRaw, dataPos, &pDnode->id, DNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pDnode->createdTime, DNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pDnode->updateTime, DNODE_DECODE_OVER)
  SDB_GET_INT16(pRaw, dataPos, &pDnode->port, DNODE_DECODE_OVER)
  SDB_GET_BINARY(pRaw, dataPos, pDnode->fqdn, TSDB_FQDN_LEN, DNODE_DECODE_OVER)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_DNODE_RESERVE_SIZE, DNODE_DECODE_OVER)

  terrno = 0;

DNODE_DECODE_OVER:
  if (terrno != 0) {
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode->id, pRaw, terrstr());
    tfree(pRow);
    return NULL;
  }
S
Shengliang Guan 已提交
170

171
  mTrace("dnode:%d, decode from raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
172 173 174
  return pRow;
}

175
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
176
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
177
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
S
Shengliang Guan 已提交
178 179 180 181
  snprintf(pDnode->ep, TSDB_EP_LEN, "%s:%u", pDnode->fqdn, pDnode->port);
  return 0;
}

S
Shengliang Guan 已提交
182
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
183
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
184 185
  return 0;
}
S
Shengliang Guan 已提交
186

187
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
S
Shengliang Guan 已提交
188
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
189
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
190
  return 0;
S
Shengliang Guan 已提交
191 192
}

S
Shengliang Guan 已提交
193
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
194 195 196 197 198 199
  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 已提交
200
}
S
Shengliang Guan 已提交
201

S
Shengliang Guan 已提交
202 203 204
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
205 206
}

S
Shengliang Guan 已提交
207
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
H
Haojun Liao 已提交
208 209
  SEpSet epSet = {0};
  addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
210 211 212
  return epSet;
}

S
Shengliang Guan 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225
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 已提交
226 227

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
228 229 230 231 232
  }

  return NULL;
}

S
Shengliang Guan 已提交
233
int32_t mndGetDnodeSize(SMnode *pMnode) {
S
Shengliang Guan 已提交
234 235 236 237
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DNODE);
}

S
Shengliang Guan 已提交
238
bool mndIsDnodeOnline(SMnode *pMnode, SDnodeObj *pDnode, int64_t curMs) {
dengyihao's avatar
dengyihao 已提交
239
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
S
Shengliang Guan 已提交
240
  if (interval > 3500 * pMnode->cfg.statusInterval) {
S
Shengliang Guan 已提交
241 242 243
    if (pDnode->rebootTime > 0) {
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
    }
S
Shengliang Guan 已提交
244 245 246 247 248
    return false;
  }
  return true;
}

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

252
  int32_t numOfEps = 0;
S
Shengliang Guan 已提交
253 254 255 256 257 258
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
259 260 261 262 263 264
    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 已提交
265
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
266
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
267
    }
S
Shengliang Guan 已提交
268

269
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
270
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
271 272 273 274
  }
}

static int32_t mndCheckClusterCfgPara(SMnode *pMnode, const SClusterCfg *pCfg) {
S
Shengliang Guan 已提交
275
  if (pCfg->statusInterval != pMnode->cfg.statusInterval) {
276
    mError("statusInterval [%d - %d] cfg inconsistent", pCfg->statusInterval, pMnode->cfg.statusInterval);
S
Shengliang Guan 已提交
277 278 279
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
  }

S
charset  
Shengliang Guan 已提交
280 281
  if ((0 != strcasecmp(pCfg->timezone, osTimezone())) && (pMnode->checkTime != pCfg->checkTime)) {
    mError("timezone [%s - %s] [%" PRId64 " - %" PRId64 "] cfg inconsistent", pCfg->timezone, osTimezone(),
S
Shengliang Guan 已提交
282
           pCfg->checkTime, pMnode->checkTime);
S
Shengliang Guan 已提交
283 284 285
    return DND_REASON_TIME_ZONE_NOT_MATCH;
  }

S
charset  
Shengliang Guan 已提交
286 287
  if (0 != strcasecmp(pCfg->locale, osLocale())) {
    mError("locale [%s - %s]  cfg inconsistent", pCfg->locale, osLocale());
S
Shengliang Guan 已提交
288 289 290
    return DND_REASON_LOCALE_NOT_MATCH;
  }

S
charset  
Shengliang Guan 已提交
291 292
  if (0 != strcasecmp(pCfg->charset, osCharset())) {
    mError("charset [%s - %s] cfg inconsistent.", pCfg->charset, osCharset());
S
Shengliang Guan 已提交
293 294 295 296 297 298
    return DND_REASON_CHARSET_NOT_MATCH;
  }

  return 0;
}

S
Shengliang Guan 已提交
299
static int32_t mndProcessStatusReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
300 301 302 303
  SMnode    *pMnode = pReq->pMnode;
  SStatusReq statusReq = {0};
  SDnodeObj *pDnode = NULL;
  int32_t    code = -1;
S
Shengliang Guan 已提交
304

S
Shengliang Guan 已提交
305 306 307 308
  if (tDeserializeSStatusReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &statusReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto PROCESS_STATUS_MSG_OVER;
  }
S
Shengliang Guan 已提交
309

S
Shengliang Guan 已提交
310 311
  if (statusReq.dnodeId == 0) {
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
312
    if (pDnode == NULL) {
S
Shengliang Guan 已提交
313
      mDebug("dnode:%s, not created yet", statusReq.dnodeEp);
314
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
315
      goto PROCESS_STATUS_MSG_OVER;
S
Shengliang Guan 已提交
316 317
    }
  } else {
S
Shengliang Guan 已提交
318
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
S
Shengliang Guan 已提交
319
    if (pDnode == NULL) {
S
Shengliang Guan 已提交
320
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
321
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
322 323
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
      }
S
Shengliang Guan 已提交
324
      mError("dnode:%d, %s not exist", statusReq.dnodeId, statusReq.dnodeEp);
325
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
326
      goto PROCESS_STATUS_MSG_OVER;
S
Shengliang Guan 已提交
327 328 329
    }
  }

S
Shengliang Guan 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  int32_t numOfVloads = (int32_t)taosArrayGetSize(statusReq.pVloads);
  for (int32_t v = 0; v < numOfVloads; ++v) {
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);

    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
    if (pVgroup != NULL) {
      if (pVload->role == TAOS_SYNC_STATE_LEADER) {
        pVgroup->numOfTables = pVload->numOfTables;
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
        pVgroup->totalStorage = pVload->totalStorage;
        pVgroup->compStorage = pVload->compStorage;
        pVgroup->pointsWritten = pVload->pointsWritten;
      }
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
        pVgroup->vnodeGid[vg].role = pVload->role;
      }
    }

    mndReleaseVgroup(pMnode, pVgroup);
  }

S
Shengliang Guan 已提交
351 352
  int64_t curMs = taosGetTimestampMs();
  bool    online = mndIsDnodeOnline(pMnode, pDnode, curMs);
S
Shengliang Guan 已提交
353 354
  bool    dnodeChanged = (statusReq.dver != sdbGetTableVer(pMnode->pSdb, SDB_DNODE));
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
355
  bool    needCheck = !online || dnodeChanged || reboot;
S
Shengliang Guan 已提交
356

357
  if (needCheck) {
S
Shengliang Guan 已提交
358
    if (statusReq.sver != pMnode->cfg.sver) {
S
Shengliang Guan 已提交
359
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
360
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
S
Shengliang Guan 已提交
361
      }
S
Shengliang Guan 已提交
362 363
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver,
             pMnode->cfg.sver);
S
Shengliang Guan 已提交
364
      terrno = TSDB_CODE_MND_INVALID_MSG_VERSION;
S
Shengliang Guan 已提交
365
      goto PROCESS_STATUS_MSG_OVER;
S
Shengliang Guan 已提交
366 367
    }

S
Shengliang Guan 已提交
368
    if (statusReq.dnodeId == 0) {
S
Shengliang Guan 已提交
369
      mDebug("dnode:%d, %s first access, set clusterId %" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
S
Shengliang Guan 已提交
370
    } else {
S
Shengliang Guan 已提交
371
      if (statusReq.clusterId != pMnode->clusterId) {
S
Shengliang Guan 已提交
372 373 374
        if (pDnode != NULL) {
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
        }
S
Shengliang Guan 已提交
375
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
S
Shengliang Guan 已提交
376 377 378 379 380 381 382
               pMnode->clusterId);
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
        goto PROCESS_STATUS_MSG_OVER;
      } else {
        pDnode->accessTimes++;
        mTrace("dnode:%d, status received, access times %d", pDnode->id, pDnode->accessTimes);
      }
S
Shengliang Guan 已提交
383 384 385
    }

    // Verify whether the cluster parameters are consistent when status change from offline to ready
S
Shengliang Guan 已提交
386
    int32_t ret = mndCheckClusterCfgPara(pMnode, &statusReq.clusterCfg);
S
Shengliang Guan 已提交
387 388 389
    if (0 != ret) {
      pDnode->offlineReason = ret;
      mError("dnode:%d, cluster cfg inconsistent since:%s", pDnode->id, offlineReason[ret]);
390
      terrno = TSDB_CODE_MND_INVALID_CLUSTER_CFG;
S
Shengliang Guan 已提交
391
      goto PROCESS_STATUS_MSG_OVER;
S
Shengliang Guan 已提交
392 393
    }

394 395 396 397 398
    if (!online) {
      mInfo("dnode:%d, from offline to online", pDnode->id);
    } else {
      mDebug("dnode:%d, send dnode eps", pDnode->id);
    }
S
Shengliang Guan 已提交
399

S
Shengliang Guan 已提交
400 401 402
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
S
Shengliang Guan 已提交
403

S
Shengliang Guan 已提交
404 405 406 407 408 409
    SStatusRsp statusRsp = {0};
    statusRsp.dver = sdbGetTableVer(pMnode->pSdb, SDB_DNODE);
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
    if (statusRsp.pDnodeEps == NULL) {
S
Shengliang Guan 已提交
410 411 412 413
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto PROCESS_STATUS_MSG_OVER;
    }

S
Shengliang Guan 已提交
414 415
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

S
Shengliang Guan 已提交
416
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
417
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
418
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
419
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
420

S
Shengliang Guan 已提交
421
    pReq->contLen = contLen;
S
Shengliang Guan 已提交
422
    pReq->pCont = pHead;
S
Shengliang Guan 已提交
423
  }
S
Shengliang Guan 已提交
424

S
Shengliang Guan 已提交
425
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
426 427 428 429
  code = 0;

PROCESS_STATUS_MSG_OVER:
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
430
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
431
  return code;
S
Shengliang Guan 已提交
432 433
}

S
Shengliang Guan 已提交
434
static int32_t mndCreateDnode(SMnode *pMnode, SMnodeMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
435
  SDnodeObj dnodeObj = {0};
S
Shengliang Guan 已提交
436
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
S
Shengliang Guan 已提交
437 438
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
439 440
  dnodeObj.port = pCreate->port;
  memcpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
441
  snprintf(dnodeObj.ep, TSDB_EP_LEN, "%s:%u", dnodeObj.fqdn, dnodeObj.port);
S
Shengliang Guan 已提交
442

S
Shengliang Guan 已提交
443
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_DNODE, &pReq->rpcMsg);
S
Shengliang Guan 已提交
444
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
445
    mError("dnode:%s, failed to create since %s", dnodeObj.ep, terrstr());
S
Shengliang Guan 已提交
446 447
    return -1;
  }
S
Shengliang Guan 已提交
448
  mDebug("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
S
Shengliang Guan 已提交
449 450 451 452 453 454 455

  SSdbRaw *pRedoRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
456
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
457

S
Shengliang Guan 已提交
458
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
459 460 461 462 463 464 465 466 467
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
468
static int32_t mndProcessCreateDnodeReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
469 470 471 472 473 474 475 476 477 478 479 480
  SMnode         *pMnode = pReq->pMnode;
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

  if (tDeserializeSCreateDnodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_DNODE_OVER;
  }

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

S
Shengliang Guan 已提交
482
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
483
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
484
    goto CREATE_DNODE_OVER;
S
Shengliang Guan 已提交
485 486
  }

S
Shengliang Guan 已提交
487
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
488 489
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
490 491
  if (pDnode != NULL) {
    terrno = TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
Shengliang Guan 已提交
492
    goto CREATE_DNODE_OVER;
S
Shengliang Guan 已提交
493
  }
S
Shengliang Guan 已提交
494

S
Shengliang Guan 已提交
495 496 497 498 499
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto CREATE_DNODE_OVER;
  }
S
Shengliang Guan 已提交
500

S
Shengliang Guan 已提交
501
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
502 503 504 505 506 507 508 509 510
    goto CREATE_DNODE_OVER;
  }

  code = mndCreateDnode(pMnode, pReq, &createReq);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

CREATE_DNODE_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
511 512
  }

S
Shengliang Guan 已提交
513 514 515
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
516 517
}

S
Shengliang Guan 已提交
518
static int32_t mndDropDnode(SMnode *pMnode, SMnodeMsg *pReq, SDnodeObj *pDnode) {
S
Shengliang Guan 已提交
519
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_DNODE, &pReq->rpcMsg);
S
Shengliang Guan 已提交
520 521 522 523
  if (pTrans == NULL) {
    mError("dnode:%d, failed to drop since %s", pDnode->id, terrstr());
    return -1;
  }
524
  mDebug("trans:%d, used to drop dnode:%d", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
525 526 527 528 529 530 531

  SSdbRaw *pRedoRaw = mndDnodeActionEncode(pDnode);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
532
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
533

S
Shengliang Guan 已提交
534
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
535 536 537 538 539 540 541 542 543
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
544
static int32_t mndProcessDropDnodeReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
545 546 547 548 549
  SMnode        *pMnode = pReq->pMnode;
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SDnodeObj     *pDnode = NULL;
  SMDropMnodeReq dropReq = {0};
S
Shengliang Guan 已提交
550

S
Shengliang Guan 已提交
551
  if (tDeserializeSMCreateDropMnodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
552 553 554
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_DNODE_OVER;
  }
S
Shengliang Guan 已提交
555

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

S
Shengliang Guan 已提交
558
  if (dropReq.dnodeId <= 0) {
559
    terrno = TSDB_CODE_MND_INVALID_DNODE_ID;
S
Shengliang Guan 已提交
560
    goto DROP_DNODE_OVER;
S
Shengliang Guan 已提交
561 562
  }

S
Shengliang Guan 已提交
563
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
564 565
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
566 567 568 569 570 571 572
    goto DROP_DNODE_OVER;
  }

  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto DROP_DNODE_OVER;
S
Shengliang Guan 已提交
573 574
  }

S
Shengliang Guan 已提交
575
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
576 577 578 579 580 581 582 583 584
    goto DROP_DNODE_OVER;
  }

  code = mndDropDnode(pMnode, pReq, pDnode);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

DROP_DNODE_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
585 586
  }

587
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
588 589 590
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
591 592
}

S
Shengliang Guan 已提交
593
static int32_t mndProcessConfigDnodeReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
594 595 596 597 598 599 600
  SMnode *pMnode = pReq->pMnode;

  SMCfgDnodeReq cfgReq = {0};
  if (tDeserializeSMCfgDnodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &cfgReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
601

S
Shengliang Guan 已提交
602
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, cfgReq.dnodeId);
S
Shengliang Guan 已提交
603 604
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
605
    mError("dnode:%d, failed to config since %s ", cfgReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
606 607 608 609 610 611
    return -1;
  }

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

S
Shengliang Guan 已提交
612 613 614 615 616 617
  int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq);
  void   *pBuf = rpcMallocCont(bufLen);
  tSerializeSMCfgDnodeReq(pBuf, bufLen, &cfgReq);

  SRpcMsg rpcMsg = {
      .msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen, .ahandle = pReq->rpcMsg.ahandle};
S
Shengliang Guan 已提交
618

S
Shengliang Guan 已提交
619
  mInfo("dnode:%d, app:%p config:%s req send to dnode", cfgReq.dnodeId, rpcMsg.ahandle, cfgReq.config);
S
Shengliang Guan 已提交
620
  mndSendReqToDnode(pMnode, &epSet, &rpcMsg);
S
Shengliang Guan 已提交
621 622 623 624

  return 0;
}

S
Shengliang Guan 已提交
625 626
static int32_t mndProcessConfigDnodeRsp(SMnodeMsg *pRsp) {
  mInfo("app:%p config rsp from dnode", pRsp->rpcMsg.ahandle);
627
}
S
Shengliang Guan 已提交
628

S
Shengliang Guan 已提交
629
static int32_t mndGetConfigMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
S
Shengliang Guan 已提交
630
  int32_t  cols = 0;
S
Shengliang Guan 已提交
631
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
632 633 634 635

  pShow->bytes[cols] = TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  tstrncpy(pSchema[cols].name, "name", sizeof(pSchema[cols].name));
S
Shengliang Guan 已提交
636
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
637 638 639 640 641
  cols++;

  pShow->bytes[cols] = TSDB_CONIIG_VALUE_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  tstrncpy(pSchema[cols].name, "value", sizeof(pSchema[cols].name));
S
Shengliang Guan 已提交
642
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
643 644
  cols++;

S
Shengliang Guan 已提交
645
  pMeta->numOfColumns = cols;
S
Shengliang Guan 已提交
646 647 648 649 650 651 652 653 654
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->numOfRows = TSDB_CONFIG_NUMBER;
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
655
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
656 657 658 659

  return 0;
}

S
Shengliang Guan 已提交
660 661
static int32_t mndRetrieveConfigs(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
662 663 664 665 666 667 668 669 670 671 672
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONIIG_VALUE_LEN + 1] = {0};
  char   *pWrite;
  int32_t cols = 0;

  cfgOpts[numOfRows] = "statusInterval";
  snprintf(cfgVals[numOfRows], TSDB_CONIIG_VALUE_LEN, "%d", pMnode->cfg.statusInterval);
  numOfRows++;

  cfgOpts[numOfRows] = "timezone";
S
charset  
Shengliang Guan 已提交
673
  snprintf(cfgVals[numOfRows], TSDB_CONIIG_VALUE_LEN, "%s", osTimezone());
S
Shengliang Guan 已提交
674 675 676
  numOfRows++;

  cfgOpts[numOfRows] = "locale";
S
charset  
Shengliang Guan 已提交
677
  snprintf(cfgVals[numOfRows], TSDB_CONIIG_VALUE_LEN, "%s", osLocale());
S
Shengliang Guan 已提交
678 679 680
  numOfRows++;

  cfgOpts[numOfRows] = "charset";
S
charset  
Shengliang Guan 已提交
681
  snprintf(cfgVals[numOfRows], TSDB_CONIIG_VALUE_LEN, "%s", osCharset());
S
Shengliang Guan 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695
  numOfRows++;

  for (int32_t i = 0; i < numOfRows; i++) {
    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfgOpts[i], TSDB_CONFIG_OPTION_LEN);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, cfgVals[i], TSDB_CONIIG_VALUE_LEN);
    cols++;
  }

S
Shengliang Guan 已提交
696
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
697 698 699 700 701 702
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

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

S
Shengliang Guan 已提交
703
static int32_t mndGetDnodeMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
S
Shengliang Guan 已提交
704
  SMnode *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
705 706 707
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
708
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
709 710 711 712

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
S
Shengliang Guan 已提交
713
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
714 715 716 717
  cols++;

  pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
718
  strcpy(pSchema[cols].name, "endpoint");
S
Shengliang Guan 已提交
719
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
720 721 722 723 724
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "vnodes");
S
Shengliang Guan 已提交
725
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
726 727 728 729
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
S
Shengliang Guan 已提交
730
  strcpy(pSchema[cols].name, "support_vnodes");
S
Shengliang Guan 已提交
731
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
732 733 734 735 736
  cols++;

  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "status");
S
Shengliang Guan 已提交
737
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
738 739 740 741
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
742
  strcpy(pSchema[cols].name, "create_time");
S
Shengliang Guan 已提交
743
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
744 745 746 747
  cols++;

  pShow->bytes[cols] = 24 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
748
  strcpy(pSchema[cols].name, "offline_reason");
S
Shengliang Guan 已提交
749
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
750 751
  cols++;

S
Shengliang Guan 已提交
752
  pMeta->numOfColumns = cols;
S
Shengliang Guan 已提交
753 754 755 756 757 758 759 760 761
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->numOfRows = sdbGetSize(pSdb, SDB_DNODE);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
762
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
763 764 765 766

  return 0;
}

S
Shengliang Guan 已提交
767 768
static int32_t mndRetrieveDnodes(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
769 770 771 772 773
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SDnodeObj *pDnode = NULL;
  char      *pWrite;
S
Shengliang Guan 已提交
774
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
775 776 777 778

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
779
    bool online = mndIsDnodeOnline(pMnode, pDnode, curMs);
S
Shengliang Guan 已提交
780 781 782 783 784 785 786 787 788 789 790 791

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pDnode->id;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDnode->ep, pShow->bytes[cols]);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
792
    *(int16_t *)pWrite = mndGetVnodesNum(pMnode, pDnode->id);
S
Shengliang Guan 已提交
793 794 795
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
796
    *(int16_t *)pWrite = pDnode->numOfSupportVnodes;
S
Shengliang Guan 已提交
797 798 799
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
800
    STR_TO_VARSTR(pWrite, online ? "ready" : "offline");
S
Shengliang Guan 已提交
801 802 803 804 805 806 807
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pDnode->createdTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
808
    STR_TO_VARSTR(pWrite, online ? "" : offlineReason[pDnode->offlineReason]);
S
Shengliang Guan 已提交
809 810 811 812 813 814
    cols++;

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

S
Shengliang Guan 已提交
815
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
816
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
817

S
Shengliang Guan 已提交
818 819 820 821 822 823 824
  return numOfRows;
}

static void mndCancelGetNextDnode(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}