mndDnode.c 44.1 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"
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndMnode.h"
20
#include "mndPrivilege.h"
D
dapan1121 已提交
21
#include "mndQnode.h"
S
Shengliang Guan 已提交
22
#include "mndShow.h"
23
#include "mndSnode.h"
S
Shengliang Guan 已提交
24
#include "mndTrans.h"
S
Shengliang Guan 已提交
25
#include "mndUser.h"
S
Shengliang Guan 已提交
26
#include "mndVgroup.h"
H
Haojun Liao 已提交
27
#include "tmisce.h"
C
cadem 已提交
28
#include "mndCluster.h"
S
Shengliang Guan 已提交
29

K
kailixu 已提交
30
#define TSDB_DNODE_VER_NUMBER   2
31
#define TSDB_DNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
32

S
Shengliang Guan 已提交
33
static const char *offlineReason[] = {
S
Shengliang Guan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
    "",
    "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",
};

K
kailixu 已提交
47 48 49 50 51
enum {
  DND_ACTIVE_CODE,
  DND_CONN_ACTIVE_CODE,
};

S
Shengliang Guan 已提交
52 53 54 55 56
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);
57
static int32_t  mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew);
D
dapan1121 已提交
58
static int32_t  mndProcessDnodeListReq(SRpcMsg *pReq);
D
dapan1121 已提交
59
static int32_t  mndProcessShowVariablesReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61 62 63 64 65
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);
D
dmchen 已提交
66
static int32_t mndProcessRestoreDnodeReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
69
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
70
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
71
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
72

73 74
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pInMCfgReq, int32_t opLen, int32_t *pOutValue);

S
Shengliang Guan 已提交
75
int32_t mndInitDnode(SMnode *pMnode) {
76 77 78 79 80 81 82 83 84 85
  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 已提交
86

S
Shengliang Guan 已提交
87 88 89
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
H
Hongze Cheng 已提交
90
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
S
Shengliang Guan 已提交
91
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
D
dapan1121 已提交
92
  mndSetMsgHandle(pMnode, TDMT_MND_DNODE_LIST, mndProcessDnodeListReq);
D
dapan1121 已提交
93
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
D
dmchen 已提交
94
  mndSetMsgHandle(pMnode, TDMT_MND_RESTORE_DNODE, mndProcessRestoreDnodeReq);
S
Shengliang Guan 已提交
95

96 97
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
S
Shengliang Guan 已提交
98 99 100
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
101 102 103 104 105 106
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
S
Shengliang Guan 已提交
107 108 109 110
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
111 112 113 114
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
115
  dnodeObj.port = tsServerPort;
S
Shengliang Guan 已提交
116 117
  tstrncpy(dnodeObj.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
  dnodeObj.fqdn[TSDB_FQDN_LEN - 1] = 0;
S
Shengliang Guan 已提交
118
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", tsLocalFqdn, tsServerPort);
S
Shengliang Guan 已提交
119

120
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL, "create-dnode");
S
Shengliang Guan 已提交
121
  if (pTrans == NULL) goto _OVER;
122
  mInfo("trans:%d, used to create dnode:%s on first deploy", pTrans->id, dnodeObj.ep);
123

S
Shengliang Guan 已提交
124 125
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
126
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
127
  pRaw = NULL;
128

S
Shengliang Guan 已提交
129 130
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
131

S
Shengliang Guan 已提交
132
_OVER:
133
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
134 135
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
136 137
}

S
Shengliang Guan 已提交
138
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
139 140
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
141
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
S
Shengliang Guan 已提交
142
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
143 144

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
145 146 147 148 149 150
  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)
K
kailixu 已提交
151
  SDB_SET_INT16(pRaw, dataPos, TSDB_ACTIVE_KEY_LEN, _OVER)
K
kailixu 已提交
152
  SDB_SET_BINARY(pRaw, dataPos, pDnode->active, TSDB_ACTIVE_KEY_LEN, _OVER)
K
kailixu 已提交
153
  SDB_SET_INT16(pRaw, dataPos, TSDB_CONN_ACTIVE_KEY_LEN, _OVER)
K
kailixu 已提交
154
  SDB_SET_BINARY(pRaw, dataPos, pDnode->connActive, TSDB_CONN_ACTIVE_KEY_LEN, _OVER)
S
Shengliang Guan 已提交
155
  SDB_SET_DATALEN(pRaw, dataPos, _OVER);
156 157 158

  terrno = 0;

S
Shengliang Guan 已提交
159
_OVER:
160 161 162 163 164
  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 已提交
165

166
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
167 168 169 170
  return pRaw;
}

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
171
  terrno = TSDB_CODE_OUT_OF_MEMORY;
172 173
  SSdbRow   *pRow = NULL;
  SDnodeObj *pDnode = NULL;
174

S
Shengliang Guan 已提交
175
  int8_t sver = 0;
S
Shengliang Guan 已提交
176
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
K
kailixu 已提交
177
  if (sver < 1 || sver > TSDB_DNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
178
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
179
    goto _OVER;
S
Shengliang Guan 已提交
180 181
  }

S
Shengliang Guan 已提交
182 183
  pRow = sdbAllocRow(sizeof(SDnodeObj));
  if (pRow == NULL) goto _OVER;
184 185

  pDnode = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
186
  if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
187 188

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
189 190 191 192 193 194
  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)
K
kailixu 已提交
195
  if (sver > 1) {
K
kailixu 已提交
196 197 198 199 200
    int16_t keyLen = 0;
    SDB_GET_INT16(pRaw, dataPos, &keyLen, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pDnode->active, keyLen, _OVER)
    SDB_GET_INT16(pRaw, dataPos, &keyLen, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pDnode->connActive, keyLen, _OVER)
K
kailixu 已提交
201
  }
202 203

  terrno = 0;
204 205 206
  if (tmsgUpdateDnodeInfo(&pDnode->id, NULL, pDnode->fqdn, &pDnode->port)) {
    mInfo("dnode:%d, endpoint changed", pDnode->id);
  }
207

S
Shengliang Guan 已提交
208
_OVER:
209
  if (terrno != 0) {
210
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode == NULL ? 0 : pDnode->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
211
    taosMemoryFreeClear(pRow);
212 213
    return NULL;
  }
S
Shengliang Guan 已提交
214

215
  mTrace("dnode:%d, decode from raw:%p, row:%p ep:%s:%u", pDnode->id, pRaw, pDnode, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
216 217 218
  return pRow;
}

219
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
220
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
221
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
S
Shengliang Guan 已提交
222 223 224 225

  char ep[TSDB_EP_LEN] = {0};
  snprintf(ep, TSDB_EP_LEN - 1, "%s:%u", pDnode->fqdn, pDnode->port);
  tstrncpy(pDnode->ep, ep, TSDB_EP_LEN);
S
Shengliang Guan 已提交
226 227 228
  return 0;
}

S
Shengliang Guan 已提交
229
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
230
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
231 232
  return 0;
}
S
Shengliang Guan 已提交
233

234
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
S
Shengliang Guan 已提交
235
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
236
  pOld->updateTime = pNew->updateTime;
237 238 239 240 241 242 243 244
#ifdef TD_ENTERPRISE
  if (strncmp(pOld->active, pNew->active, TSDB_ACTIVE_KEY_LEN) != 0) {
    strncpy(pOld->active, pNew->active, TSDB_ACTIVE_KEY_LEN);
  }
  if (strncmp(pOld->connActive, pNew->connActive, TSDB_CONN_ACTIVE_KEY_LEN) != 0) {
    strncpy(pOld->connActive, pNew->connActive, TSDB_CONN_ACTIVE_KEY_LEN);
  }
#endif
S
Shengliang Guan 已提交
245
  return 0;
S
Shengliang Guan 已提交
246 247
}

S
Shengliang Guan 已提交
248
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
249 250 251
  SSdb      *pSdb = pMnode->pSdb;
  SDnodeObj *pDnode = sdbAcquire(pSdb, SDB_DNODE, &dnodeId);
  if (pDnode == NULL) {
252 253 254 255 256 257 258 259 260 261
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
      terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    } else if (terrno == TSDB_CODE_SDB_OBJ_CREATING) {
      terrno = TSDB_CODE_MND_DNODE_IN_CREATING;
    } else if (terrno == TSDB_CODE_SDB_OBJ_DROPPING) {
      terrno = TSDB_CODE_MND_DNODE_IN_DROPPING;
    } else {
      terrno = TSDB_CODE_APP_ERROR;
      mFatal("dnode:%d, failed to acquire db since %s", dnodeId, terrstr());
    }
S
Shengliang Guan 已提交
262
  }
263

S
Shengliang Guan 已提交
264
  return pDnode;
S
Shengliang Guan 已提交
265
}
S
Shengliang Guan 已提交
266

S
Shengliang Guan 已提交
267 268 269
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
270 271
}

S
Shengliang Guan 已提交
272
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
H
Haojun Liao 已提交
273 274
  SEpSet epSet = {0};
  addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
275 276 277
  return epSet;
}

S
Shengliang Guan 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
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 已提交
291 292

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
293 294
  }

S
Shengliang Guan 已提交
295
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
296 297 298
  return NULL;
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static SDnodeObj *mndAcquireDnodeAllStatusByEp(SMnode *pMnode, char *pEpStr) {
  SSdb *pSdb = pMnode->pSdb;

  void *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
    if (pIter == NULL) break;

    if (strncasecmp(pEpStr, pDnode->ep, TSDB_EP_LEN) == 0) {
      sdbCancelFetch(pSdb, pIter);
      return pDnode;
    }

    sdbRelease(pSdb, pDnode);
  }

  return NULL;
}

S
Shengliang Guan 已提交
320
int32_t mndGetDnodeSize(SMnode *pMnode) {
S
Shengliang Guan 已提交
321 322 323 324
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DNODE);
}

C
cadem 已提交
325 326 327 328 329
int32_t mndGetDbSize(SMnode *pMnode) {
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DB);
}

S
Shengliang Guan 已提交
330
bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs) {
dengyihao's avatar
dengyihao 已提交
331
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
S
Shengliang Guan 已提交
332
  if (interval > 5000 * (int64_t)tsStatusInterval) {
S
Shengliang Guan 已提交
333 334 335
    if (pDnode->rebootTime > 0) {
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
    }
S
Shengliang Guan 已提交
336 337 338 339 340
    return false;
  }
  return true;
}

K
kailixu 已提交
341
static void mndGetDnodeEps(SMnode *pMnode, SArray *pDnodeEps) {
S
Shengliang Guan 已提交
342 343
  SSdb *pSdb = pMnode->pSdb;

344
  int32_t numOfEps = 0;
S
Shengliang Guan 已提交
345 346 347
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
348 349
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
350 351
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
352 353 354
    SDnodeEp dnodeEp = {0};
    dnodeEp.id = pDnode->id;
    dnodeEp.ep.port = pDnode->port;
S
Shengliang Guan 已提交
355
    tstrncpy(dnodeEp.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
356
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
357

S
Shengliang Guan 已提交
358
    dnodeEp.isMnode = 0;
S
Shengliang Guan 已提交
359
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
360
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
361
    }
S
Shengliang Guan 已提交
362
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
363 364 365
  }
}

K
kailixu 已提交
366
void mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo) {
K
kailixu 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  SSdb *pSdb = pMnode->pSdb;

  int32_t numOfEps = 0;
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
    if (pIter == NULL) break;

    SDnodeInfo dInfo;
    dInfo.id = pDnode->id;
    dInfo.ep.port = pDnode->port;
    tstrncpy(dInfo.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
    tstrncpy(dInfo.active, pDnode->active, TSDB_ACTIVE_KEY_LEN);
    tstrncpy(dInfo.connActive, pDnode->connActive, TSDB_CONN_ACTIVE_KEY_LEN);
    sdbRelease(pSdb, pDnode);
K
kailixu 已提交
384 385 386 387 388
    if (mndIsMnode(pMnode, pDnode->id)) {
      dInfo.isMnode = 1;
    } else {
      dInfo.isMnode = 0;
    }
K
kailixu 已提交
389 390 391 392 393

    taosArrayPush(pDnodeInfo, &dInfo);
  }
}

S
Shengliang Guan 已提交
394
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
S
Shengliang Guan 已提交
395
  if (pCfg->statusInterval != tsStatusInterval) {
S
Shengliang Guan 已提交
396 397
    mError("dnode:%d, statusInterval:%d inconsistent with cluster:%d", pDnode->id, pCfg->statusInterval,
           tsStatusInterval);
S
Shengliang Guan 已提交
398 399 400
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
  }

wafwerar's avatar
wafwerar 已提交
401
  if ((0 != strcasecmp(pCfg->timezone, tsTimezoneStr)) && (pMnode->checkTime != pCfg->checkTime)) {
S
Shengliang Guan 已提交
402 403
    mError("dnode:%d, timezone:%s checkTime:%" PRId64 " inconsistent with cluster %s %" PRId64, pDnode->id,
           pCfg->timezone, pCfg->checkTime, tsTimezoneStr, pMnode->checkTime);
S
Shengliang Guan 已提交
404 405 406
    return DND_REASON_TIME_ZONE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
407
  if (0 != strcasecmp(pCfg->locale, tsLocale)) {
S
Shengliang Guan 已提交
408
    mError("dnode:%d, locale:%s inconsistent with cluster:%s", pDnode->id, pCfg->locale, tsLocale);
S
Shengliang Guan 已提交
409 410 411
    return DND_REASON_LOCALE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
412
  if (0 != strcasecmp(pCfg->charset, tsCharset)) {
S
Shengliang Guan 已提交
413
    mError("dnode:%d, charset:%s inconsistent with cluster:%s", pDnode->id, pCfg->charset, tsCharset);
S
Shengliang Guan 已提交
414 415 416 417 418 419
    return DND_REASON_CHARSET_NOT_MATCH;
  }

  return 0;
}

S
Shengliang Guan 已提交
420 421
static int32_t mndProcessStatusReq(SRpcMsg *pReq) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
422 423 424
  SStatusReq statusReq = {0};
  SDnodeObj *pDnode = NULL;
  int32_t    code = -1;
S
Shengliang Guan 已提交
425

S
Shengliang Guan 已提交
426
  if (tDeserializeSStatusReq(pReq->pCont, pReq->contLen, &statusReq) != 0) {
S
Shengliang Guan 已提交
427
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
428
    goto _OVER;
S
Shengliang Guan 已提交
429
  }
S
Shengliang Guan 已提交
430

C
cadem 已提交
431
  int64_t clusterid = mndGetClusterId(pMnode);
S
Shengliang Guan 已提交
432
  if (statusReq.clusterId != 0 && statusReq.clusterId != clusterid) {
C
cadem 已提交
433
    code = TSDB_CODE_MND_DNODE_DIFF_CLUSTER;
S
Shengliang Guan 已提交
434 435
    mWarn("dnode:%d, %s, its clusterid:%" PRId64 " differ from current cluster:%" PRId64 ", code:0x%x",
          statusReq.dnodeId, statusReq.dnodeEp, statusReq.clusterId, clusterid, code);
C
cadem 已提交
436 437 438
    goto _OVER;
  }

S
Shengliang Guan 已提交
439 440
  if (statusReq.dnodeId == 0) {
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
441
    if (pDnode == NULL) {
442
      mInfo("dnode:%s, not created yet", statusReq.dnodeEp);
S
Shengliang Guan 已提交
443
      goto _OVER;
S
Shengliang Guan 已提交
444 445
    }
  } else {
S
Shengliang Guan 已提交
446
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
S
Shengliang Guan 已提交
447
    if (pDnode == NULL) {
448
      int32_t err = terrno;
S
Shengliang Guan 已提交
449
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
450
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
451
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
452 453 454 455 456 457 458 459 460 461 462
        terrno = err;
        goto _OVER;
      }

      mError("dnode:%d, %s not exist, code:0x%x", statusReq.dnodeId, statusReq.dnodeEp, err);
      if (err == TSDB_CODE_MND_DNODE_NOT_EXIST) {
        terrno = err;
        goto _OVER;
      } else {
        pDnode = mndAcquireDnodeAllStatusByEp(pMnode, statusReq.dnodeEp);
        if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
463 464 465 466
      }
    }
  }

467 468 469 470 471 472 473 474 475
  int64_t dnodeVer = sdbGetTableVer(pMnode->pSdb, SDB_DNODE) + sdbGetTableVer(pMnode->pSdb, SDB_MNODE);
  int64_t curMs = taosGetTimestampMs();
  bool    online = mndIsDnodeOnline(pDnode, curMs);
  bool    dnodeChanged = (statusReq.dnodeVer == 0) || (statusReq.dnodeVer != dnodeVer);
  bool    reboot = (pDnode->rebootTime != statusReq.rebootTime);
  bool    needCheck = !online || dnodeChanged || reboot;

  const STraceId *trace = &pReq->info.traceId;
  mGTrace("dnode:%d, status received, accessTimes:%d check:%d online:%d reboot:%d changed:%d statusSeq:%d", pDnode->id,
476
          pDnode->accessTimes, needCheck, online, reboot, dnodeChanged, statusReq.statusSeq);
477

S
Shengliang Guan 已提交
478
  for (int32_t v = 0; v < taosArrayGetSize(statusReq.pVloads); ++v) {
S
Shengliang Guan 已提交
479 480 481 482
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);

    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
    if (pVgroup != NULL) {
S
Shengliang Guan 已提交
483
      if (pVload->syncState == TAOS_SYNC_STATE_LEADER) {
484
        pVgroup->cacheUsage = pVload->cacheUsage;
D
dapan1121 已提交
485
        pVgroup->numOfCachedTables = pVload->numOfCachedTables;
S
Shengliang Guan 已提交
486 487 488 489 490 491
        pVgroup->numOfTables = pVload->numOfTables;
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
        pVgroup->totalStorage = pVload->totalStorage;
        pVgroup->compStorage = pVload->compStorage;
        pVgroup->pointsWritten = pVload->pointsWritten;
      }
L
Liu Jicong 已提交
492
      bool roleChanged = false;
S
Shengliang Guan 已提交
493
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
494 495 496 497 498 499
        SVnodeGid *pGid = &pVgroup->vnodeGid[vg];
        if (pGid->dnodeId == statusReq.dnodeId) {
          if (pGid->syncState != pVload->syncState || pGid->syncRestore != pVload->syncRestore ||
              pGid->syncCanRead != pVload->syncCanRead) {
            mInfo(
                "vgId:%d, state changed by status msg, old state:%s restored:%d canRead:%d new state:%s restored:%d "
500
                "canRead:%d, dnode:%d",
501
                pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead,
502
                syncStr(pVload->syncState), pVload->syncRestore, pVload->syncCanRead, pDnode->id);
503 504 505
            pGid->syncState = pVload->syncState;
            pGid->syncRestore = pVload->syncRestore;
            pGid->syncCanRead = pVload->syncCanRead;
M
Minghao Li 已提交
506 507
            roleChanged = true;
          }
S
Shengliang Guan 已提交
508
          break;
L
Liu Jicong 已提交
509
        }
S
Shengliang Guan 已提交
510
      }
L
Liu Jicong 已提交
511
      if (roleChanged) {
512
        SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
513
        if (pDb != NULL && pDb->stateTs != curMs) {
dengyihao's avatar
dengyihao 已提交
514 515
          mInfo("db:%s, stateTs changed by status msg, old stateTs:%" PRId64 " new stateTs:%" PRId64, pDb->name,
                pDb->stateTs, curMs);
516 517 518
          pDb->stateTs = curMs;
        }
        mndReleaseDb(pMnode, pDb);
L
Liu Jicong 已提交
519
      }
S
Shengliang Guan 已提交
520 521 522 523 524
    }

    mndReleaseVgroup(pMnode, pVgroup);
  }

525 526
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
527 528 529 530 531
    if (pObj->syncState != statusReq.mload.syncState || pObj->syncRestore != statusReq.mload.syncRestore) {
      mInfo("dnode:%d, mnode syncState from %s to %s, restoreState from %d to %d", pObj->id, syncStr(pObj->syncState),
            syncStr(statusReq.mload.syncState), pObj->syncRestore, statusReq.mload.syncRestore);
      pObj->syncState = statusReq.mload.syncState;
      pObj->syncRestore = statusReq.mload.syncRestore;
532 533 534 535 536
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

D
dapan1121 已提交
537 538 539 540 541 542
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
  if (pQnode != NULL) {
    pQnode->load = statusReq.qload;
    mndReleaseQnode(pMnode, pQnode);
  }

543
  if (needCheck) {
S
Shengliang Guan 已提交
544
    if (statusReq.sver != tsVersion) {
S
Shengliang Guan 已提交
545
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
546
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
S
Shengliang Guan 已提交
547
      }
S
Shengliang Guan 已提交
548
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver, tsVersion);
549
      terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
S
Shengliang Guan 已提交
550
      goto _OVER;
S
Shengliang Guan 已提交
551 552
    }

S
Shengliang Guan 已提交
553
    if (statusReq.dnodeId == 0) {
S
Shengliang Guan 已提交
554
      mInfo("dnode:%d, %s first access, clusterId:%" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
S
Shengliang Guan 已提交
555
    } else {
S
Shengliang Guan 已提交
556
      if (statusReq.clusterId != pMnode->clusterId) {
S
Shengliang Guan 已提交
557 558 559
        if (pDnode != NULL) {
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
        }
S
Shengliang Guan 已提交
560
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
S
Shengliang Guan 已提交
561 562
               pMnode->clusterId);
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
S
Shengliang Guan 已提交
563
        goto _OVER;
S
Shengliang Guan 已提交
564
      }
S
Shengliang Guan 已提交
565 566 567
    }

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

575
    if (!online) {
576 577
      mInfo("dnode:%d, from offline to online, memory avail:%" PRId64 " total:%" PRId64 " cores:%.2f", pDnode->id,
            statusReq.memAvail, statusReq.memTotal, statusReq.numOfCores);
578
    } else {
579
      mInfo("dnode:%d, send dnode epset, online:%d dnodeVer:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
H
Hongze Cheng 已提交
580
            statusReq.dnodeVer, dnodeVer, reboot);
581
    }
S
Shengliang Guan 已提交
582

S
Shengliang Guan 已提交
583 584 585
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
586 587
    pDnode->memAvail = statusReq.memAvail;
    pDnode->memTotal = statusReq.memTotal;
S
Shengliang Guan 已提交
588

S
Shengliang Guan 已提交
589
    SStatusRsp statusRsp = {0};
590
    statusRsp.statusSeq++;
591
    statusRsp.dnodeVer = dnodeVer;
S
Shengliang Guan 已提交
592 593 594 595
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
    if (statusRsp.pDnodeEps == NULL) {
S
Shengliang Guan 已提交
596
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
597
      goto _OVER;
S
Shengliang Guan 已提交
598 599
    }

K
kailixu 已提交
600
    mndGetDnodeEps(pMnode, statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
601

S
Shengliang Guan 已提交
602
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
603
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
604
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
605
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
606

S
Shengliang Guan 已提交
607 608
    pReq->info.rspLen = contLen;
    pReq->info.rsp = pHead;
S
Shengliang Guan 已提交
609
  }
S
Shengliang Guan 已提交
610

611 612
  pDnode->accessTimes++;
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
613 614
  code = 0;

S
Shengliang Guan 已提交
615
_OVER:
S
Shengliang Guan 已提交
616
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
617
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
618
  return code;
S
Shengliang Guan 已提交
619 620
}

S
Shengliang Guan 已提交
621
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
622 623 624 625
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
626
  SDnodeObj dnodeObj = {0};
S
Shengliang Guan 已提交
627
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
S
Shengliang Guan 已提交
628 629
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
630
  dnodeObj.port = pCreate->port;
S
Shengliang Guan 已提交
631
  tstrncpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
632
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", pCreate->fqdn, pCreate->port);
S
Shengliang Guan 已提交
633

634
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_GLOBAL, pReq, "create-dnode");
S
Shengliang Guan 已提交
635
  if (pTrans == NULL) goto _OVER;
636
  mInfo("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
637
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
638

S
Shengliang Guan 已提交
639 640
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
641
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
642
  pRaw = NULL;
S
Shengliang Guan 已提交
643

S
Shengliang Guan 已提交
644 645
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
S
Shengliang Guan 已提交
646

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

K
kailixu 已提交
653
static int32_t mndConfigDnode(SMnode *pMnode, SRpcMsg *pReq, SMCfgDnodeReq *pCfgReq, int8_t action) {
K
kailixu 已提交
654 655 656 657
  SSdbRaw   *pRaw = NULL;
  STrans    *pTrans = NULL;
  SDnodeObj *pDnode = NULL;
  bool       cfgAll = pCfgReq->dnodeId == -1;
K
kailixu 已提交
658
  int32_t    iter = 0;
659 660 661 662

  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
K
kailixu 已提交
663 664 665
    if (cfgAll) {
      pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
      if (pIter == NULL) break;
K
kailixu 已提交
666 667
      ++iter;
    } else if (!(pDnode = mndAcquireDnode(pMnode, pCfgReq->dnodeId))) {
K
kailixu 已提交
668
      goto _OVER;
669 670
    }

671
    if (!pTrans) {
K
kailixu 已提交
672
      pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "config-dnode");
673 674 675
      if (!pTrans) goto _OVER;
      if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
    }
676 677

    SDnodeObj tmpDnode = *pDnode;
K
kailixu 已提交
678
    if (action == DND_ACTIVE_CODE) {
679
      strncpy(tmpDnode.active, pCfgReq->value, TSDB_ACTIVE_KEY_LEN);
K
kailixu 已提交
680
    } else if (action == DND_CONN_ACTIVE_CODE) {
681 682
      strncpy(tmpDnode.connActive, pCfgReq->value, TSDB_CONN_ACTIVE_KEY_LEN);
    } else {
K
kailixu 已提交
683 684
      terrno = TSDB_CODE_INVALID_CFG;
      goto _OVER;
685 686 687 688 689 690 691 692 693 694
    }

    pRaw = mndDnodeActionEncode(&tmpDnode);
    if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
    (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
    pRaw = NULL;

    mInfo("dnode:%d, config dnode, cfg:%d, app:%p config:%s value:%s", pDnode->id, pCfgReq->dnodeId, pReq->info.ahandle,
          pCfgReq->config, pCfgReq->value);

K
kailixu 已提交
695 696 697 698 699 700
    if (cfgAll) {
      sdbRelease(pSdb, pDnode);
      pDnode = NULL;
    } else {
      break;
    }
701 702
  }

703 704 705
  if (pTrans && mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  terrno = 0;
706 707

_OVER:
K
kailixu 已提交
708 709 710 711 712
  if (cfgAll) {
    sdbRelease(pSdb, pDnode);
  } else {
    mndReleaseDnode(pMnode, pDnode);
  }
K
kailixu 已提交
713

K
kailixu 已提交
714
  tsGrantHBInterval = MIN(MAX(3, iter / 2), 15);
K
kailixu 已提交
715

716 717
  mndTransDrop(pTrans);
  sdbFreeRaw(pRaw);
718
  return terrno;
719 720
}

D
dapan1121 已提交
721
static int32_t mndProcessDnodeListReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
722 723 724 725
  SMnode       *pMnode = pReq->info.node;
  SSdb         *pSdb = pMnode->pSdb;
  SDnodeObj    *pObj = NULL;
  void         *pIter = NULL;
D
dapan1121 已提交
726
  SDnodeListRsp rsp = {0};
S
Shengliang Guan 已提交
727 728
  int32_t       code = -1;

D
dapan1121 已提交
729 730 731 732 733 734
  rsp.dnodeList = taosArrayInit(5, sizeof(SEpSet));
  if (NULL == rsp.dnodeList) {
    mError("failed to alloc epSet while process dnode list req");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }
S
Shengliang Guan 已提交
735

D
dapan1121 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
  while (1) {
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SEpSet epSet = {0};
    epSet.numOfEps = 1;
    tstrncpy(epSet.eps[0].fqdn, pObj->fqdn, TSDB_FQDN_LEN);
    epSet.eps[0].port = pObj->port;

    (void)taosArrayPush(rsp.dnodeList, &epSet);

    sdbRelease(pSdb, pObj);
  }

  int32_t rspLen = tSerializeSDnodeListRsp(NULL, 0, &rsp);
  void   *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSDnodeListRsp(pRsp, rspLen, &rsp);

  pReq->info.rspLen = rspLen;
  pReq->info.rsp = pRsp;
  code = 0;

_OVER:

  if (code != 0) {
    mError("failed to get dnode list since %s", terrstr());
  }

  tFreeSDnodeListRsp(&rsp);

  return code;
}

D
dapan1121 已提交
774 775
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
  SShowVariablesRsp rsp = {0};
776 777 778 779 780
  int32_t           code = -1;

  if (mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIBALES) != 0) {
    goto _OVER;
  }
D
dapan1121 已提交
781 782 783 784 785 786 787 788 789 790 791 792

  rsp.variables = taosArrayInit(4, sizeof(SVariablesInfo));
  if (NULL == rsp.variables) {
    mError("failed to alloc SVariablesInfo array while process show variables req");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  SVariablesInfo info = {0};

  strcpy(info.name, "statusInterval");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
G
Ganlin Zhao 已提交
793
  strcpy(info.scope, "server");
D
dapan1121 已提交
794 795 796 797
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "timezone");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
G
Ganlin Zhao 已提交
798
  strcpy(info.scope, "both");
D
dapan1121 已提交
799
  taosArrayPush(rsp.variables, &info);
800

D
dapan1121 已提交
801 802
  strcpy(info.name, "locale");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
G
Ganlin Zhao 已提交
803
  strcpy(info.scope, "both");
D
dapan1121 已提交
804 805 806 807
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "charset");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
G
Ganlin Zhao 已提交
808
  strcpy(info.scope, "both");
D
dapan1121 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
  taosArrayPush(rsp.variables, &info);

  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
  void   *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp);

  pReq->info.rspLen = rspLen;
  pReq->info.rsp = pRsp;
  code = 0;

_OVER:

  if (code != 0) {
    mError("failed to get show variables info since %s", terrstr());
  }

  tFreeSShowVariablesRsp(&rsp);

  return code;
}

S
Shengliang Guan 已提交
835 836
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
837 838 839 840
  int32_t         code = -1;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

841
  if ((terrno = grantCheck(TSDB_GRANT_DNODE)) != 0 || (terrno = grantCheck(TSDB_GRANT_CPU_CORES)) != 0) {
C
Cary Xu 已提交
842 843 844
    code = terrno;
    goto _OVER;
  }
845

S
Shengliang Guan 已提交
846
  if (tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
847
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
848
    goto _OVER;
S
Shengliang Guan 已提交
849 850
  }

S
Shengliang Guan 已提交
851
  mInfo("dnode:%s:%d, start to create", createReq.fqdn, createReq.port);
852
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_DNODE) != 0) {
S
Shengliang Guan 已提交
853 854
    goto _OVER;
  }
S
Shengliang Guan 已提交
855

S
Shengliang Guan 已提交
856
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
857
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
858
    goto _OVER;
S
Shengliang Guan 已提交
859 860
  }

S
Shengliang Guan 已提交
861
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
862 863
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
864
  if (pDnode != NULL) {
865
    terrno = TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
Shengliang Guan 已提交
866
    goto _OVER;
S
Shengliang Guan 已提交
867
  }
S
Shengliang Guan 已提交
868

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

S
Shengliang Guan 已提交
872
_OVER:
S
Shengliang Guan 已提交
873
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
874
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
875 876
  }

S
Shengliang Guan 已提交
877 878
  mndReleaseDnode(pMnode, pDnode);
  return code;
S
Shengliang Guan 已提交
879 880
}

C
cadem 已提交
881 882 883 884 885 886 887 888
extern int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq);

int32_t mndProcessRestoreDnodeReq(SRpcMsg *pReq){
  return mndProcessRestoreDnodeReqImpl(pReq);
}

#ifndef TD_ENTERPRISE
int32_t mndProcessRestoreDnodeReqImpl(SRpcMsg *pReq){
D
dmchen 已提交
889
  return 0;
C
cadem 已提交
890
}
D
dmchen 已提交
891
#endif
C
cadem 已提交
892

893
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, SQnodeObj *pQObj,
D
dmchen 已提交
894
                            SSnodeObj *pSObj, int32_t numOfVnodes, bool force, bool unsafe) {
S
Shengliang Guan 已提交
895 896 897 898
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

899
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-dnode");
S
Shengliang Guan 已提交
900
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
901
  mndTransSetSerial(pTrans);
S
Shengliang Guan 已提交
902
  mInfo("trans:%d, used to drop dnode:%d, force:%d", pTrans->id, pDnode->id, force);
903
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
904

S
Shengliang Guan 已提交
905
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
906 907
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
908
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
S
Shengliang Guan 已提交
909
  pRaw = NULL;
S
Shengliang Guan 已提交
910

S
Shengliang Guan 已提交
911
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
912 913
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
914
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
915 916
  pRaw = NULL;

S
Shengliang Guan 已提交
917
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
918
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
919
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj, force) != 0) goto _OVER;
S
Shengliang Guan 已提交
920
  }
921 922 923

  if (pQObj != NULL) {
    mInfo("trans:%d, qnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
924
    if (mndSetDropQnodeInfoToTrans(pMnode, pTrans, pQObj, force) != 0) goto _OVER;
925 926 927 928
  }

  if (pSObj != NULL) {
    mInfo("trans:%d, snode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
929
    if (mndSetDropSnodeInfoToTrans(pMnode, pTrans, pSObj, force) != 0) goto _OVER;
930 931
  }

S
Shengliang Guan 已提交
932
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
933
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
D
dmchen 已提交
934
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id, force, unsafe) != 0) goto _OVER;
S
Shengliang Guan 已提交
935
  }
S
Shengliang Guan 已提交
936

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

S
Shengliang Guan 已提交
939 940 941
  code = 0;

_OVER:
S
Shengliang Guan 已提交
942
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
943 944
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
945 946
}

S
Shengliang Guan 已提交
947
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
948 949 950 951 952 953 954 955 956
  SMnode       *pMnode = pReq->info.node;
  int32_t       code = -1;
  SDnodeObj    *pDnode = NULL;
  SMnodeObj    *pMObj = NULL;
  SQnodeObj    *pQObj = NULL;
  SSnodeObj    *pSObj = NULL;
  SDropDnodeReq dropReq = {0};

  if (tDeserializeSDropDnodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
957
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
958
    goto _OVER;
S
Shengliang Guan 已提交
959
  }
S
Shengliang Guan 已提交
960

961
  mInfo("dnode:%d, start to drop, ep:%s:%d, force:%s, unsafe:%s",
D
dmchen 已提交
962
          dropReq.dnodeId, dropReq.fqdn, dropReq.port, dropReq.force?"true":"false", dropReq.unsafe?"true":"false");
963
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
964 965
    goto _OVER;
  }
S
Shengliang Guan 已提交
966

D
dmchen 已提交
967 968 969 970 971 972
  bool force = dropReq.force;
  if(dropReq.unsafe)
  {
    force = true;
  }

S
Shengliang Guan 已提交
973
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
974
  if (pDnode == NULL) {
975 976
    int32_t err = terrno;
    char    ep[TSDB_EP_LEN + 1] = {0};
977 978 979
    snprintf(ep, sizeof(ep), dropReq.fqdn, dropReq.port);
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
    if (pDnode == NULL) {
980
      terrno = err;
981 982
      goto _OVER;
    }
S
Shengliang Guan 已提交
983 984
  }

985 986
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
987 988
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
989 990 991 992 993
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
994 995 996 997 998 999
      terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
      goto _OVER;
    }
  }

  int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);
D
dmchen 已提交
1000
  bool isonline = mndIsDnodeOnline(pDnode, taosGetTimestampMs());
1001

D
dmchen 已提交
1002 1003 1004 1005 1006 1007 1008
  if (isonline && force) {
    terrno = TSDB_CODE_DNODE_ONLY_USE_WHEN_OFFLINE;
    mError("dnode:%d, failed to drop since %s, vnodes:%d mnode:%d qnode:%d snode:%d", pDnode->id, terrstr(),
            numOfVnodes, pMObj != NULL, pQObj != NULL, pSObj != NULL);
    goto _OVER;
  }

D
dmchen 已提交
1009 1010 1011 1012 1013
  if (!isonline && !force) {
    terrno = TSDB_CODE_DNODE_OFFLINE;
    mError("dnode:%d, failed to drop since %s, vnodes:%d mnode:%d qnode:%d snode:%d", pDnode->id, terrstr(),
            numOfVnodes, pMObj != NULL, pQObj != NULL, pSObj != NULL);
    goto _OVER;
1014 1015
  }

D
dmchen 已提交
1016
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, numOfVnodes, force, dropReq.unsafe);
S
Shengliang Guan 已提交
1017
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1018

S
Shengliang Guan 已提交
1019
_OVER:
S
Shengliang Guan 已提交
1020
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1021
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
1022 1023
  }

1024
  mndReleaseDnode(pMnode, pDnode);
1025
  mndReleaseMnode(pMnode, pMObj);
1026 1027
  mndReleaseQnode(pMnode, pQObj);
  mndReleaseSnode(pMnode, pSObj);
S
Shengliang Guan 已提交
1028
  return code;
S
Shengliang Guan 已提交
1029 1030
}

S
Shengliang Guan 已提交
1031
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
1032 1033
  SMnode     *pMnode = pReq->info.node;
  const char *options[] = {
1034 1035 1036
      "debugFlag",   "dDebugFlag",   "vDebugFlag",   "mDebugFlag",   "wDebugFlag",    "sDebugFlag",   "tsdbDebugFlag",
      "tqDebugFlag", "fsDebugFlag",  "udfDebugFlag", "smaDebugFlag", "idxDebugFlag",  "tdbDebugFlag", "tmrDebugFlag",
      "uDebugFlag",  "smaDebugFlag", "rpcDebugFlag", "qDebugFlag",   "metaDebugFlag",
1037 1038
  };
  int32_t optionSize = tListLen(options);
S
Shengliang Guan 已提交
1039 1040

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
1041
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
1042 1043 1044
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
1045

1046
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
K
kailixu 已提交
1047 1048
  if ((pReq->info.ahandle != (void *)0x818611) &&
      (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE) != 0)) {
S
Shengliang Guan 已提交
1049 1050 1051
    return -1;
  }

S
Shengliang Guan 已提交
1052
  SDCfgDnodeReq dcfgReq = {0};
1053 1054 1055
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
    strcpy(dcfgReq.config, "resetlog");
  } else if (strncasecmp(cfgReq.config, "monitor", 7) == 0) {
1056 1057 1058 1059 1060 1061
    if (' ' != cfgReq.config[7] && 0 != cfgReq.config[7]) {
      mError("dnode:%d, failed to config monitor since invalid conf:%s", cfgReq.dnodeId, cfgReq.config);
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

S
Shengliang Guan 已提交
1062 1063 1064
    const char *value = cfgReq.value;
    int32_t     flag = atoi(value);
    if (flag <= 0) {
1065
      flag = atoi(cfgReq.config + 8);
S
Shengliang Guan 已提交
1066
    }
1067 1068
    if (flag < 0 || flag > 2) {
      mError("dnode:%d, failed to config monitor since value:%d", cfgReq.dnodeId, flag);
S
Shengliang Guan 已提交
1069 1070 1071 1072
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

1073
    strcpy(dcfgReq.config, "monitor");
S
Shengliang Guan 已提交
1074
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
1075
  } else if (strncasecmp(cfgReq.config, "keeptimeoffset", 14) == 0) {
1076 1077 1078 1079
    int32_t optLen = strlen("keeptimeoffset");
    int32_t flag = -1;
    int32_t code = mndMCfgGetValInt32(&cfgReq, optLen, &flag);
    if (code < 0) return code;
1080

1081 1082
    if (flag < 0 || flag > 23) {
      mError("dnode:%d, failed to config keepTimeOffset since value:%d. Valid range: [0, 23]", cfgReq.dnodeId, flag);
1083 1084 1085 1086 1087
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

    strcpy(dcfgReq.config, "keeptimeoffset");
1088
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
1089
#ifdef TD_ENTERPRISE
K
kailixu 已提交
1090 1091 1092 1093
  } else if (strncasecmp(cfgReq.config, "activeCode", 10) == 0 || strncasecmp(cfgReq.config, "cActiveCode", 11) == 0) {
    int8_t opt = strncasecmp(cfgReq.config, "a", 1) == 0 ? DND_ACTIVE_CODE : DND_CONN_ACTIVE_CODE;
    int8_t index = opt == DND_ACTIVE_CODE ? 10 : 11;
    if (' ' != cfgReq.config[index] && 0 != cfgReq.config[index]) {
1094 1095 1096 1097
      mError("dnode:%d, failed to config activeCode since invalid conf:%s", cfgReq.dnodeId, cfgReq.config);
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }
1098
    int32_t vlen = strlen(cfgReq.value);
K
kailixu 已提交
1099 1100 1101
    if (vlen > 0 && ((opt == DND_ACTIVE_CODE && vlen != (TSDB_ACTIVE_KEY_LEN - 1)) ||
                     (opt == DND_CONN_ACTIVE_CODE &&
                      (vlen > (TSDB_CONN_ACTIVE_KEY_LEN - 1) || vlen < (TSDB_ACTIVE_KEY_LEN - 1))))) {
1102 1103
      mError("dnode:%d, failed to config activeCode since invalid vlen:%d. conf:%s, val:%s", cfgReq.dnodeId, vlen,
             cfgReq.config, cfgReq.value);
1104 1105 1106 1107
      terrno = TSDB_CODE_INVALID_OPTION;
      return -1;
    }

K
kailixu 已提交
1108
    strcpy(dcfgReq.config, opt == DND_ACTIVE_CODE ? "activeCode" : "cActiveCode");
K
kailixu 已提交
1109 1110
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%s", cfgReq.value);

K
kailixu 已提交
1111
    if (mndConfigDnode(pMnode, pReq, &cfgReq, opt) != 0) {
K
kailixu 已提交
1112 1113 1114
      mError("dnode:%d, failed to config activeCode since %s", cfgReq.dnodeId, terrstr());
      return -1;
    }
K
kailixu 已提交
1115
    return 0;
1116
#endif
S
Shengliang Guan 已提交
1117
  } else {
1118 1119 1120 1121 1122 1123
    bool findOpt = false;
    for (int32_t d = 0; d < optionSize; ++d) {
      const char *optName = options[d];
      int32_t     optLen = strlen(optName);
      if (strncasecmp(cfgReq.config, optName, optLen) != 0) continue;

1124 1125 1126 1127 1128 1129
      if (' ' != cfgReq.config[optLen] && 0 != cfgReq.config[optLen]) {
        mError("dnode:%d, failed to config since invalid conf:%s", cfgReq.dnodeId, cfgReq.config);
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
      }

1130
      const char *value = cfgReq.value;
M
Minglei Jin 已提交
1131
      int32_t     flag = atoi(value);
1132 1133 1134
      if (flag <= 0) {
        flag = atoi(cfgReq.config + optLen + 1);
      }
1135
      if (flag < 0 || flag > 255) {
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
        mError("dnode:%d, failed to config %s since value:%d", cfgReq.dnodeId, optName, flag);
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
      }

      tstrncpy(dcfgReq.config, optName, optLen + 1);
      snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
      findOpt = true;
    }

    if (!findOpt) {
      terrno = TSDB_CODE_INVALID_CFG;
      mError("dnode:%d, failed to config since %s", cfgReq.dnodeId, terrstr());
      return -1;
    }
S
Shengliang Guan 已提交
1151 1152
  }

S
Shengliang Guan 已提交
1153
  int32_t code = -1;
1154 1155
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1156 1157 1158 1159
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1160

S
Shengliang Guan 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169
    if (pDnode->id == cfgReq.dnodeId || cfgReq.dnodeId == -1 || cfgReq.dnodeId == 0) {
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, &dcfgReq);
      void   *pBuf = rpcMallocCont(bufLen);

      if (pBuf != NULL) {
        tSerializeSDCfgDnodeReq(pBuf, bufLen, &dcfgReq);
        mInfo("dnode:%d, send config req to dnode, app:%p config:%s value:%s", cfgReq.dnodeId, pReq->info.ahandle,
              dcfgReq.config, dcfgReq.value);
1170
        SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
S
Shengliang Guan 已提交
1171 1172 1173 1174
        tmsgSendReq(&epSet, &rpcMsg);
        code = 0;
      }
    }
S
Shengliang Guan 已提交
1175

S
Shengliang Guan 已提交
1176 1177
    sdbRelease(pSdb, pDnode);
  }
1178

S
Shengliang Guan 已提交
1179 1180 1181 1182
  if (code == -1) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
  }
  return code;
S
Shengliang Guan 已提交
1183 1184
}

S
Shengliang Guan 已提交
1185
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
1186
  mInfo("config rsp from dnode");
S
Shengliang Guan 已提交
1187
  return 0;
1188
}
S
Shengliang Guan 已提交
1189

S
Shengliang Guan 已提交
1190 1191
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1192
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
1193 1194
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
D
dapan1121 已提交
1195
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONFIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
1196
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
1197 1198
  int32_t cols = 0;

S
Shengliang Guan 已提交
1199
  cfgOpts[totalRows] = "statusInterval";
D
dapan1121 已提交
1200
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
S
Shengliang Guan 已提交
1201
  totalRows++;
S
Shengliang Guan 已提交
1202

S
Shengliang Guan 已提交
1203
  cfgOpts[totalRows] = "timezone";
D
dapan1121 已提交
1204
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
1205
  totalRows++;
S
Shengliang Guan 已提交
1206

S
Shengliang Guan 已提交
1207
  cfgOpts[totalRows] = "locale";
D
dapan1121 已提交
1208
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
S
Shengliang Guan 已提交
1209
  totalRows++;
S
Shengliang Guan 已提交
1210

S
Shengliang Guan 已提交
1211
  cfgOpts[totalRows] = "charset";
D
dapan1121 已提交
1212
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
S
Shengliang Guan 已提交
1213
  totalRows++;
S
Shengliang Guan 已提交
1214

1215
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
1216
  char bufVal[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
1217

S
Shengliang Guan 已提交
1218
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
1219 1220
    cols = 0;

1221
    STR_WITH_MAXSIZE_TO_VARSTR(buf, cfgOpts[i], TSDB_CONFIG_OPTION_LEN);
M
Minghao Li 已提交
1222
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1223
    colDataSetVal(pColInfo, numOfRows, (const char *)buf, false);
S
Shengliang Guan 已提交
1224

D
dapan1121 已提交
1225
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONFIG_VALUE_LEN);
1226
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1227
    colDataSetVal(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
1228 1229

    numOfRows++;
S
Shengliang Guan 已提交
1230 1231
  }

1232
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1233 1234 1235 1236 1237
  return numOfRows;
}

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

S
Shengliang Guan 已提交
1238 1239
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1240 1241 1242
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
1243
  ESdbStatus objStatus = 0;
S
Shengliang Guan 已提交
1244
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
1245
  int64_t    curMs = taosGetTimestampMs();
K
kailixu 已提交
1246
  char       buf[TSDB_CONN_ACTIVE_KEY_LEN + VARSTR_HEADER_SIZE];  // make sure TSDB_CONN_ACTIVE_KEY_LEN >= TSDB_EP_LEN
S
Shengliang Guan 已提交
1247 1248

  while (numOfRows < rows) {
1249
    pShow->pIter = sdbFetchAll(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
1250
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
1251
    bool online = mndIsDnodeOnline(pDnode, curMs);
S
Shengliang Guan 已提交
1252 1253 1254

    cols = 0;

M
Minghao Li 已提交
1255
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1256
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->id, false);
1257

1258
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
1259 1260

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1261
    colDataSetVal(pColInfo, numOfRows, buf, false);
1262 1263 1264

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    int16_t id = mndGetVnodesNum(pMnode, pDnode->id);
1265
    colDataSetVal(pColInfo, numOfRows, (const char *)&id, false);
S
Shengliang Guan 已提交
1266

1267
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1268
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->numOfSupportVnodes, false);
S
Shengliang Guan 已提交
1269

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
    const char *status = "ready";
    if (objStatus == SDB_STATUS_CREATING) status = "creating";
    if (objStatus == SDB_STATUS_DROPPING) status = "dropping";
    if (!online) {
      if (objStatus == SDB_STATUS_CREATING)
        status = "creating*";
      else if (objStatus == SDB_STATUS_DROPPING)
        status = "dropping*";
      else
        status = "offline";
    }

K
kailixu 已提交
1282
    STR_TO_VARSTR(buf, status);
1283
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
K
kailixu 已提交
1284
    colDataSetVal(pColInfo, numOfRows, buf, false);
S
Shengliang Guan 已提交
1285

1286
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1287
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->createdTime, false);
S
Shengliang Guan 已提交
1288

C
cadem 已提交
1289 1290 1291
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, numOfRows, (const char *)&pDnode->rebootTime, false);

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

1295
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1296
    colDataSetVal(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
1297
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
1298

K
kailixu 已提交
1299 1300
#ifdef TD_ENTERPRISE
    STR_TO_VARSTR(buf, pDnode->active);
K
kailixu 已提交
1301
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
K
kailixu 已提交
1302
    colDataSetVal(pColInfo, numOfRows, buf, false);
K
kailixu 已提交
1303

K
kailixu 已提交
1304
    STR_TO_VARSTR(buf, pDnode->connActive);
K
kailixu 已提交
1305
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
K
kailixu 已提交
1306 1307
    colDataSetVal(pColInfo, numOfRows, buf, false);
#endif
K
kailixu 已提交
1308

S
Shengliang Guan 已提交
1309 1310 1311 1312
    numOfRows++;
    sdbRelease(pSdb, pDnode);
  }

1313
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1314 1315 1316 1317 1318 1319
  return numOfRows;
}

static void mndCancelGetNextDnode(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
1320
}
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

// get int32_t value from 'SMCfgDnodeReq'
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t opLen, int32_t *pOutValue) {
  terrno = 0;
  if (' ' != pMCfgReq->config[opLen] && 0 != pMCfgReq->config[opLen]) {
    goto _err;
  }

  if (' ' == pMCfgReq->config[opLen]) {
    // 'key value'
    if (strlen(pMCfgReq->value) != 0) goto _err;
    *pOutValue = atoi(pMCfgReq->config + opLen + 1);
  } else {
    // 'key' 'value'
    if (strlen(pMCfgReq->value) == 0) goto _err;
    *pOutValue = atoi(pMCfgReq->value);
  }

  return 0;

_err:
  mError("dnode:%d, failed to config keeptimeoffset since invalid conf:%s", pMCfgReq->dnodeId, pMCfgReq->config);
  terrno = TSDB_CODE_INVALID_CFG;
  return -1;
}