mndDnode.c 26.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndDnode.h"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
S
Shengliang Guan 已提交
19
#include "mndMnode.h"
D
dapan1121 已提交
20
#include "mndQnode.h"
S
Shengliang Guan 已提交
21
#include "mndShow.h"
22
#include "mndSnode.h"
S
Shengliang Guan 已提交
23
#include "mndTrans.h"
S
Shengliang Guan 已提交
24
#include "mndUser.h"
S
Shengliang Guan 已提交
25
#include "mndVgroup.h"
S
Shengliang Guan 已提交
26

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

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

S
Shengliang Guan 已提交
44 45 46 47 48
static int32_t  mndCreateDefaultDnode(SMnode *pMnode);
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode);
static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw);
static int32_t  mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode);
static int32_t  mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode);
49
static int32_t  mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew);
S
Shengliang Guan 已提交
50

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

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

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

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

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

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

void mndCleanupDnode(SMnode *pMnode) {}

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

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

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

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

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

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

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

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

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

  terrno = 0;

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

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

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

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

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

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

  terrno = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

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

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

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

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

    mndReleaseVgroup(pMnode, pVgroup);
  }

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
451 452
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
564
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
565
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
566 567
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj) != 0) goto _OVER;
  }
568 569 570 571 572 573 574 575 576 577 578

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

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

S
Shengliang Guan 已提交
579
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
580
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
S
Shengliang Guan 已提交
581 582
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id) != 0) goto _OVER;
  }
S
Shengliang Guan 已提交
583
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
584

S
Shengliang Guan 已提交
585 586 587
  code = 0;

_OVER:
S
Shengliang Guan 已提交
588
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
589 590
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
591 592
}

S
Shengliang Guan 已提交
593 594
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
595 596
  int32_t        code = -1;
  SDnodeObj     *pDnode = NULL;
597
  SMnodeObj     *pMObj = NULL;
598 599
  SQnodeObj     *pQObj = NULL;
  SSnodeObj     *pSObj = NULL;
S
Shengliang Guan 已提交
600
  SMDropMnodeReq dropReq = {0};
S
Shengliang Guan 已提交
601

S
Shengliang Guan 已提交
602
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
603
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
604
    goto _OVER;
S
Shengliang Guan 已提交
605
  }
S
Shengliang Guan 已提交
606

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

S
Shengliang Guan 已提交
609
  if (dropReq.dnodeId <= 0) {
610
    terrno = TSDB_CODE_MND_INVALID_DNODE_ID;
S
Shengliang Guan 已提交
611
    goto _OVER;
S
Shengliang Guan 已提交
612 613
  }

S
Shengliang Guan 已提交
614
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
615 616
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
617 618 619
    goto _OVER;
  }

620 621
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
622 623
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
624 625 626 627 628
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
629 630 631 632 633 634 635 636 637 638 639
      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 已提交
640 641
      goto _OVER;
    }
642 643
  }

644
  if (mndCheckOperAuth(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
645
    goto _OVER;
S
Shengliang Guan 已提交
646 647
  }

648
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, numOfVnodes);
S
Shengliang Guan 已提交
649
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
650

S
Shengliang Guan 已提交
651
_OVER:
S
Shengliang Guan 已提交
652
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
653
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
654 655
  }

656
  mndReleaseDnode(pMnode, pDnode);
657
  mndReleaseMnode(pMnode, pMObj);
658 659
  mndReleaseQnode(pMnode, pQObj);
  mndReleaseSnode(pMnode, pSObj);
S
Shengliang Guan 已提交
660
  return code;
S
Shengliang Guan 已提交
661 662
}

S
Shengliang Guan 已提交
663 664
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
665 666

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
667
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
668 669 670
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
671

S
Shengliang Guan 已提交
672
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, cfgReq.dnodeId);
S
Shengliang Guan 已提交
673
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
674
    mError("dnode:%d, failed to config since %s ", cfgReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
675 676 677 678 679 680
    return -1;
  }

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

S
Shengliang Guan 已提交
681 682
  int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq);
  void   *pBuf = rpcMallocCont(bufLen);
S
Shengliang Guan 已提交
683 684

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

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

S
Shengliang Guan 已提交
692
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
693
  mDebug("config rsp from dnode, app:%p", pRsp->info.ahandle);
S
Shengliang Guan 已提交
694
  return 0;
695
}
S
Shengliang Guan 已提交
696

S
Shengliang Guan 已提交
697 698
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
699
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
700 701 702
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONIIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
703
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
704 705
  int32_t cols = 0;

S
Shengliang Guan 已提交
706 707 708
  cfgOpts[totalRows] = "statusInterval";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%d", tsStatusInterval);
  totalRows++;
S
Shengliang Guan 已提交
709

S
Shengliang Guan 已提交
710
  cfgOpts[totalRows] = "timezone";
wafwerar's avatar
wafwerar 已提交
711
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
712
  totalRows++;
S
Shengliang Guan 已提交
713

S
Shengliang Guan 已提交
714 715 716
  cfgOpts[totalRows] = "locale";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsLocale);
  totalRows++;
S
Shengliang Guan 已提交
717

S
Shengliang Guan 已提交
718 719 720
  cfgOpts[totalRows] = "charset";
  snprintf(cfgVals[totalRows], TSDB_CONIIG_VALUE_LEN, "%s", tsCharset);
  totalRows++;
S
Shengliang Guan 已提交
721

722 723 724
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
  char bufVal[TSDB_CONIIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};

S
Shengliang Guan 已提交
725
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
726 727
    cols = 0;

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

732 733
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONIIG_VALUE_LEN);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
M
Minghao Li 已提交
734
    colDataAppend(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
735 736

    numOfRows++;
S
Shengliang Guan 已提交
737 738
  }

739
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
740 741 742 743 744
  return numOfRows;
}

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

S
Shengliang Guan 已提交
745 746
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
747 748 749 750
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
751
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
752 753 754 755

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

    cols = 0;

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

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
764
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
765 766 767 768 769 770

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

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

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

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

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

787 788
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
789
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
790 791 792 793 794

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

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

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