mndDnode.c 36.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"
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"
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29
#define TSDB_DNODE_VER_NUMBER   1
30
#define TSDB_DNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
31

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

S
Shengliang Guan 已提交
55 56 57 58 59
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 已提交
60

S
Shengliang Guan 已提交
61
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
62
static void    mndCancelGetNextConfig(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
63
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
64
static void    mndCancelGetNextDnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
65 66

int32_t mndInitDnode(SMnode *pMnode) {
67 68 69 70 71 72 73 74 75 76
  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 已提交
77

S
Shengliang Guan 已提交
78 79 80
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_DNODE, mndProcessCreateDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_DNODE, mndProcessDropDnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
H
Hongze Cheng 已提交
81
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
S
Shengliang Guan 已提交
82
  mndSetMsgHandle(pMnode, TDMT_MND_STATUS, mndProcessStatusReq);
D
dapan1121 已提交
83
  mndSetMsgHandle(pMnode, TDMT_MND_DNODE_LIST, mndProcessDnodeListReq);
D
dapan1121 已提交
84
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
S
Shengliang Guan 已提交
85

86 87
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndRetrieveConfigs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONFIGS, mndCancelGetNextConfig);
S
Shengliang Guan 已提交
88 89 90
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndRetrieveDnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DNODE, mndCancelGetNextDnode);

S
Shengliang Guan 已提交
91 92 93 94 95 96
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupDnode(SMnode *pMnode) {}

static int32_t mndCreateDefaultDnode(SMnode *pMnode) {
S
Shengliang Guan 已提交
97 98 99 100
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
101 102 103 104
  SDnodeObj dnodeObj = {0};
  dnodeObj.id = 1;
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
105
  dnodeObj.port = tsServerPort;
S
Shengliang Guan 已提交
106 107
  tstrncpy(dnodeObj.fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
  dnodeObj.fqdn[TSDB_FQDN_LEN - 1] = 0;
S
Shengliang Guan 已提交
108
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", tsLocalFqdn, tsServerPort);
S
Shengliang Guan 已提交
109

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

S
Shengliang Guan 已提交
114 115
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
116
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
117
  pRaw = NULL;
118

S
Shengliang Guan 已提交
119 120
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
121

S
Shengliang Guan 已提交
122
_OVER:
123
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
124 125
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
126 127
}

S
Shengliang Guan 已提交
128
static SSdbRaw *mndDnodeActionEncode(SDnodeObj *pDnode) {
129 130
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
131
  SSdbRaw *pRaw = sdbAllocRaw(SDB_DNODE, TSDB_DNODE_VER_NUMBER, sizeof(SDnodeObj) + TSDB_DNODE_RESERVE_SIZE);
S
Shengliang Guan 已提交
132
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
133 134

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
135 136 137 138 139 140 141
  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);
142 143 144

  terrno = 0;

S
Shengliang Guan 已提交
145
_OVER:
146 147 148 149 150
  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 已提交
151

152
  mTrace("dnode:%d, encode to raw:%p, row:%p", pDnode->id, pRaw, pDnode);
S
Shengliang Guan 已提交
153 154 155 156
  return pRaw;
}

static SSdbRow *mndDnodeActionDecode(SSdbRaw *pRaw) {
157
  terrno = TSDB_CODE_OUT_OF_MEMORY;
158 159
  SSdbRow   *pRow = NULL;
  SDnodeObj *pDnode = NULL;
160

S
Shengliang Guan 已提交
161
  int8_t sver = 0;
S
Shengliang Guan 已提交
162
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
163
  if (sver != TSDB_DNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
164
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
165
    goto _OVER;
S
Shengliang Guan 已提交
166 167
  }

S
Shengliang Guan 已提交
168 169
  pRow = sdbAllocRow(sizeof(SDnodeObj));
  if (pRow == NULL) goto _OVER;
170 171

  pDnode = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
172
  if (pDnode == NULL) goto _OVER;
S
Shengliang Guan 已提交
173 174

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
175 176 177 178 179 180
  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)
181 182

  terrno = 0;
183 184 185
  if (tmsgUpdateDnodeInfo(&pDnode->id, NULL, pDnode->fqdn, &pDnode->port)) {
    mInfo("dnode:%d, endpoint changed", pDnode->id);
  }
186

S
Shengliang Guan 已提交
187
_OVER:
188
  if (terrno != 0) {
189
    mError("dnode:%d, failed to decode from raw:%p since %s", pDnode == NULL ? 0 : pDnode->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
190
    taosMemoryFreeClear(pRow);
191 192
    return NULL;
  }
S
Shengliang Guan 已提交
193

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

198
static int32_t mndDnodeActionInsert(SSdb *pSdb, SDnodeObj *pDnode) {
199
  mTrace("dnode:%d, perform insert action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
200
  pDnode->offlineReason = DND_REASON_STATUS_NOT_RECEIVED;
S
Shengliang Guan 已提交
201 202 203 204

  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 已提交
205 206 207
  return 0;
}

S
Shengliang Guan 已提交
208
static int32_t mndDnodeActionDelete(SSdb *pSdb, SDnodeObj *pDnode) {
209
  mTrace("dnode:%d, perform delete action, row:%p", pDnode->id, pDnode);
S
Shengliang Guan 已提交
210 211
  return 0;
}
S
Shengliang Guan 已提交
212

213
static int32_t mndDnodeActionUpdate(SSdb *pSdb, SDnodeObj *pOld, SDnodeObj *pNew) {
S
Shengliang Guan 已提交
214
  mTrace("dnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
215
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
216
  return 0;
S
Shengliang Guan 已提交
217 218
}

S
Shengliang Guan 已提交
219
SDnodeObj *mndAcquireDnode(SMnode *pMnode, int32_t dnodeId) {
S
Shengliang Guan 已提交
220 221 222
  SSdb      *pSdb = pMnode->pSdb;
  SDnodeObj *pDnode = sdbAcquire(pSdb, SDB_DNODE, &dnodeId);
  if (pDnode == NULL) {
223 224 225 226 227 228 229 230 231 232
    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 已提交
233
  }
234

S
Shengliang Guan 已提交
235
  return pDnode;
S
Shengliang Guan 已提交
236
}
S
Shengliang Guan 已提交
237

S
Shengliang Guan 已提交
238 239 240
void mndReleaseDnode(SMnode *pMnode, SDnodeObj *pDnode) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
241 242
}

S
Shengliang Guan 已提交
243
SEpSet mndGetDnodeEpset(SDnodeObj *pDnode) {
H
Haojun Liao 已提交
244 245
  SEpSet epSet = {0};
  addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port);
S
Shengliang Guan 已提交
246 247 248
  return epSet;
}

S
Shengliang Guan 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
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 已提交
262 263

    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
264 265
  }

S
Shengliang Guan 已提交
266
  terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
267 268 269
  return NULL;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
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 已提交
291
int32_t mndGetDnodeSize(SMnode *pMnode) {
S
Shengliang Guan 已提交
292 293 294 295
  SSdb *pSdb = pMnode->pSdb;
  return sdbGetSize(pSdb, SDB_DNODE);
}

S
Shengliang Guan 已提交
296
bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs) {
dengyihao's avatar
dengyihao 已提交
297
  int64_t interval = TABS(pDnode->lastAccessTime - curMs);
S
Shengliang Guan 已提交
298
  if (interval > 5000 * (int64_t)tsStatusInterval) {
S
Shengliang Guan 已提交
299 300 301
    if (pDnode->rebootTime > 0) {
      pDnode->offlineReason = DND_REASON_STATUS_MSG_TIMEOUT;
    }
S
Shengliang Guan 已提交
302 303 304 305 306
    return false;
  }
  return true;
}

C
Cary Xu 已提交
307
void mndGetDnodeData(SMnode *pMnode, SArray *pDnodeEps) {
S
Shengliang Guan 已提交
308 309
  SSdb *pSdb = pMnode->pSdb;

310
  int32_t numOfEps = 0;
S
Shengliang Guan 已提交
311 312 313
  void   *pIter = NULL;
  while (1) {
    SDnodeObj *pDnode = NULL;
314 315
    ESdbStatus objStatus = 0;
    pIter = sdbFetchAll(pSdb, SDB_DNODE, pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
316 317
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
318 319 320
    SDnodeEp dnodeEp = {0};
    dnodeEp.id = pDnode->id;
    dnodeEp.ep.port = pDnode->port;
S
Shengliang Guan 已提交
321
    tstrncpy(dnodeEp.ep.fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
322
    sdbRelease(pSdb, pDnode);
S
Shengliang Guan 已提交
323

S
Shengliang Guan 已提交
324
    dnodeEp.isMnode = 0;
S
Shengliang Guan 已提交
325
    if (mndIsMnode(pMnode, pDnode->id)) {
S
Shengliang Guan 已提交
326
      dnodeEp.isMnode = 1;
S
Shengliang Guan 已提交
327
    }
S
Shengliang Guan 已提交
328
    taosArrayPush(pDnodeEps, &dnodeEp);
S
Shengliang Guan 已提交
329 330 331
  }
}

S
Shengliang Guan 已提交
332
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
S
Shengliang Guan 已提交
333
  if (pCfg->statusInterval != tsStatusInterval) {
S
Shengliang Guan 已提交
334 335
    mError("dnode:%d, statusInterval:%d inconsistent with cluster:%d", pDnode->id, pCfg->statusInterval,
           tsStatusInterval);
S
Shengliang Guan 已提交
336 337 338
    return DND_REASON_STATUS_INTERVAL_NOT_MATCH;
  }

wafwerar's avatar
wafwerar 已提交
339
  if ((0 != strcasecmp(pCfg->timezone, tsTimezoneStr)) && (pMnode->checkTime != pCfg->checkTime)) {
S
Shengliang Guan 已提交
340 341
    mError("dnode:%d, timezone:%s checkTime:%" PRId64 " inconsistent with cluster %s %" PRId64, pDnode->id,
           pCfg->timezone, pCfg->checkTime, tsTimezoneStr, pMnode->checkTime);
S
Shengliang Guan 已提交
342 343 344
    return DND_REASON_TIME_ZONE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
345
  if (0 != strcasecmp(pCfg->locale, tsLocale)) {
S
Shengliang Guan 已提交
346
    mError("dnode:%d, locale:%s inconsistent with cluster:%s", pDnode->id, pCfg->locale, tsLocale);
S
Shengliang Guan 已提交
347 348 349
    return DND_REASON_LOCALE_NOT_MATCH;
  }

S
os env  
Shengliang Guan 已提交
350
  if (0 != strcasecmp(pCfg->charset, tsCharset)) {
S
Shengliang Guan 已提交
351
    mError("dnode:%d, charset:%s inconsistent with cluster:%s", pDnode->id, pCfg->charset, tsCharset);
S
Shengliang Guan 已提交
352 353 354 355 356 357
    return DND_REASON_CHARSET_NOT_MATCH;
  }

  return 0;
}

S
Shengliang Guan 已提交
358 359
static int32_t mndProcessStatusReq(SRpcMsg *pReq) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
360 361 362
  SStatusReq statusReq = {0};
  SDnodeObj *pDnode = NULL;
  int32_t    code = -1;
S
Shengliang Guan 已提交
363

S
Shengliang Guan 已提交
364
  if (tDeserializeSStatusReq(pReq->pCont, pReq->contLen, &statusReq) != 0) {
S
Shengliang Guan 已提交
365
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
366
    goto _OVER;
S
Shengliang Guan 已提交
367
  }
S
Shengliang Guan 已提交
368

S
Shengliang Guan 已提交
369 370
  if (statusReq.dnodeId == 0) {
    pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
371
    if (pDnode == NULL) {
372
      mInfo("dnode:%s, not created yet", statusReq.dnodeEp);
S
Shengliang Guan 已提交
373
      goto _OVER;
S
Shengliang Guan 已提交
374 375
    }
  } else {
S
Shengliang Guan 已提交
376
    pDnode = mndAcquireDnode(pMnode, statusReq.dnodeId);
S
Shengliang Guan 已提交
377
    if (pDnode == NULL) {
378
      int32_t err = terrno;
S
Shengliang Guan 已提交
379
      pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp);
S
Shengliang Guan 已提交
380
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
381
        pDnode->offlineReason = DND_REASON_DNODE_ID_NOT_MATCH;
382 383 384 385 386 387 388 389 390 391 392
        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 已提交
393 394 395 396
      }
    }
  }

397 398 399 400 401 402 403 404 405
  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,
406
          pDnode->accessTimes, needCheck, online, reboot, dnodeChanged, statusReq.statusSeq);
407

S
Shengliang Guan 已提交
408
  for (int32_t v = 0; v < taosArrayGetSize(statusReq.pVloads); ++v) {
S
Shengliang Guan 已提交
409 410 411 412
    SVnodeLoad *pVload = taosArrayGet(statusReq.pVloads, v);

    SVgObj *pVgroup = mndAcquireVgroup(pMnode, pVload->vgId);
    if (pVgroup != NULL) {
S
Shengliang Guan 已提交
413
      if (pVload->syncState == TAOS_SYNC_STATE_LEADER) {
414
        pVgroup->cacheUsage = pVload->cacheUsage;
415
        pVgroup->numOfCachedTables = pVload->numOfCachedTables;
S
Shengliang Guan 已提交
416 417 418 419 420 421
        pVgroup->numOfTables = pVload->numOfTables;
        pVgroup->numOfTimeSeries = pVload->numOfTimeSeries;
        pVgroup->totalStorage = pVload->totalStorage;
        pVgroup->compStorage = pVload->compStorage;
        pVgroup->pointsWritten = pVload->pointsWritten;
      }
L
Liu Jicong 已提交
422
      bool roleChanged = false;
S
Shengliang Guan 已提交
423
      for (int32_t vg = 0; vg < pVgroup->replica; ++vg) {
424 425 426 427 428 429
        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 "
430
                "canRead:%d, dnode:%d",
431
                pVgroup->vgId, syncStr(pGid->syncState), pGid->syncRestore, pGid->syncCanRead,
432
                syncStr(pVload->syncState), pVload->syncRestore, pVload->syncCanRead, pDnode->id);
433 434 435
            pGid->syncState = pVload->syncState;
            pGid->syncRestore = pVload->syncRestore;
            pGid->syncCanRead = pVload->syncCanRead;
M
Minghao Li 已提交
436 437
            roleChanged = true;
          }
S
Shengliang Guan 已提交
438
          break;
L
Liu Jicong 已提交
439
        }
S
Shengliang Guan 已提交
440
      }
L
Liu Jicong 已提交
441
      if (roleChanged) {
442
        SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName);
443
        if (pDb != NULL && pDb->stateTs != curMs) {
444 445
          mInfo("db:%s, stateTs changed by status msg, old stateTs:%" PRId64 " new stateTs:%" PRId64, pDb->name,
                pDb->stateTs, curMs);
446 447 448
          pDb->stateTs = curMs;
        }
        mndReleaseDb(pMnode, pDb);
L
Liu Jicong 已提交
449
      }
S
Shengliang Guan 已提交
450 451 452 453 454
    }

    mndReleaseVgroup(pMnode, pVgroup);
  }

455 456
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDnode->id);
  if (pObj != NULL) {
457 458 459 460 461
    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;
462 463 464 465 466
      pObj->stateStartTime = taosGetTimestampMs();
    }
    mndReleaseMnode(pMnode, pObj);
  }

D
dapan1121 已提交
467 468 469 470 471 472
  SQnodeObj *pQnode = mndAcquireQnode(pMnode, statusReq.qload.dnodeId);
  if (pQnode != NULL) {
    pQnode->load = statusReq.qload;
    mndReleaseQnode(pMnode, pQnode);
  }

473
  if (needCheck) {
S
Shengliang Guan 已提交
474
    if (statusReq.sver != tsVersion) {
S
Shengliang Guan 已提交
475
      if (pDnode != NULL) {
S
Shengliang Guan 已提交
476
        pDnode->offlineReason = DND_REASON_VERSION_NOT_MATCH;
S
Shengliang Guan 已提交
477
      }
S
Shengliang Guan 已提交
478
      mError("dnode:%d, status msg version:%d not match cluster:%d", statusReq.dnodeId, statusReq.sver, tsVersion);
479
      terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
S
Shengliang Guan 已提交
480
      goto _OVER;
S
Shengliang Guan 已提交
481 482
    }

S
Shengliang Guan 已提交
483
    if (statusReq.dnodeId == 0) {
S
Shengliang Guan 已提交
484
      mInfo("dnode:%d, %s first access, clusterId:%" PRId64, pDnode->id, pDnode->ep, pMnode->clusterId);
S
Shengliang Guan 已提交
485
    } else {
S
Shengliang Guan 已提交
486
      if (statusReq.clusterId != pMnode->clusterId) {
S
Shengliang Guan 已提交
487 488 489
        if (pDnode != NULL) {
          pDnode->offlineReason = DND_REASON_CLUSTER_ID_NOT_MATCH;
        }
S
Shengliang Guan 已提交
490
        mError("dnode:%d, clusterId %" PRId64 " not match exist %" PRId64, pDnode->id, statusReq.clusterId,
S
Shengliang Guan 已提交
491 492
               pMnode->clusterId);
        terrno = TSDB_CODE_MND_INVALID_CLUSTER_ID;
S
Shengliang Guan 已提交
493
        goto _OVER;
S
Shengliang Guan 已提交
494
      }
S
Shengliang Guan 已提交
495 496 497
    }

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

505
    if (!online) {
506 507
      mInfo("dnode:%d, from offline to online, memory avail:%" PRId64 " total:%" PRId64 " cores:%.2f", pDnode->id,
            statusReq.memAvail, statusReq.memTotal, statusReq.numOfCores);
508
    } else {
509
      mInfo("dnode:%d, send dnode epset, online:%d dnodeVer:%" PRId64 ":%" PRId64 " reboot:%d", pDnode->id, online,
H
Hongze Cheng 已提交
510
            statusReq.dnodeVer, dnodeVer, reboot);
511
    }
S
Shengliang Guan 已提交
512

S
Shengliang Guan 已提交
513 514 515
    pDnode->rebootTime = statusReq.rebootTime;
    pDnode->numOfCores = statusReq.numOfCores;
    pDnode->numOfSupportVnodes = statusReq.numOfSupportVnodes;
516 517
    pDnode->memAvail = statusReq.memAvail;
    pDnode->memTotal = statusReq.memTotal;
S
Shengliang Guan 已提交
518

S
Shengliang Guan 已提交
519
    SStatusRsp statusRsp = {0};
520
    statusRsp.statusSeq++;
521
    statusRsp.dnodeVer = dnodeVer;
S
Shengliang Guan 已提交
522 523 524 525
    statusRsp.dnodeCfg.dnodeId = pDnode->id;
    statusRsp.dnodeCfg.clusterId = pMnode->clusterId;
    statusRsp.pDnodeEps = taosArrayInit(mndGetDnodeSize(pMnode), sizeof(SDnodeEp));
    if (statusRsp.pDnodeEps == NULL) {
S
Shengliang Guan 已提交
526
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
527
      goto _OVER;
S
Shengliang Guan 已提交
528 529
    }

S
Shengliang Guan 已提交
530 531
    mndGetDnodeData(pMnode, statusRsp.pDnodeEps);

S
Shengliang Guan 已提交
532
    int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp);
S
Shengliang Guan 已提交
533
    void   *pHead = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
534
    tSerializeSStatusRsp(pHead, contLen, &statusRsp);
S
Shengliang Guan 已提交
535
    taosArrayDestroy(statusRsp.pDnodeEps);
S
Shengliang Guan 已提交
536

S
Shengliang Guan 已提交
537 538
    pReq->info.rspLen = contLen;
    pReq->info.rsp = pHead;
S
Shengliang Guan 已提交
539
  }
S
Shengliang Guan 已提交
540

541 542
  pDnode->accessTimes++;
  pDnode->lastAccessTime = curMs;
S
Shengliang Guan 已提交
543 544
  code = 0;

S
Shengliang Guan 已提交
545
_OVER:
S
Shengliang Guan 已提交
546
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
547
  taosArrayDestroy(statusReq.pVloads);
S
Shengliang Guan 已提交
548
  return code;
S
Shengliang Guan 已提交
549 550
}

S
Shengliang Guan 已提交
551
static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pCreate) {
S
Shengliang Guan 已提交
552 553 554 555
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

S
Shengliang Guan 已提交
556
  SDnodeObj dnodeObj = {0};
S
Shengliang Guan 已提交
557
  dnodeObj.id = sdbGetMaxId(pMnode->pSdb, SDB_DNODE);
S
Shengliang Guan 已提交
558 559
  dnodeObj.createdTime = taosGetTimestampMs();
  dnodeObj.updateTime = dnodeObj.createdTime;
S
Shengliang Guan 已提交
560
  dnodeObj.port = pCreate->port;
S
Shengliang Guan 已提交
561
  tstrncpy(dnodeObj.fqdn, pCreate->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
562
  snprintf(dnodeObj.ep, TSDB_EP_LEN - 1, "%s:%u", pCreate->fqdn, pCreate->port);
S
Shengliang Guan 已提交
563

564
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_GLOBAL, pReq, "create-dnode");
S
Shengliang Guan 已提交
565
  if (pTrans == NULL) goto _OVER;
566
  mInfo("trans:%d, used to create dnode:%s", pTrans->id, dnodeObj.ep);
567
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
568

S
Shengliang Guan 已提交
569 570
  pRaw = mndDnodeActionEncode(&dnodeObj);
  if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
571
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
572
  pRaw = NULL;
S
Shengliang Guan 已提交
573

S
Shengliang Guan 已提交
574 575
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  code = 0;
S
Shengliang Guan 已提交
576

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

D
dapan1121 已提交
583
static int32_t mndProcessDnodeListReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
584 585 586 587
  SMnode       *pMnode = pReq->info.node;
  SSdb         *pSdb = pMnode->pSdb;
  SDnodeObj    *pObj = NULL;
  void         *pIter = NULL;
D
dapan1121 已提交
588
  SDnodeListRsp rsp = {0};
S
Shengliang Guan 已提交
589 590
  int32_t       code = -1;

D
dapan1121 已提交
591 592 593 594 595 596
  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 已提交
597

D
dapan1121 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  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 已提交
636 637
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
  SShowVariablesRsp rsp = {0};
638 639 640 641 642
  int32_t           code = -1;

  if (mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIBALES) != 0) {
    goto _OVER;
  }
D
dapan1121 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

  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);
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "timezone");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
  taosArrayPush(rsp.variables, &info);
660

D
dapan1121 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  strcpy(info.name, "locale");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
  taosArrayPush(rsp.variables, &info);

  strcpy(info.name, "charset");
  snprintf(info.value, TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
  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 已提交
693 694
static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
695 696 697 698
  int32_t         code = -1;
  SDnodeObj      *pDnode = NULL;
  SCreateDnodeReq createReq = {0};

C
Cary Xu 已提交
699 700 701 702
  if ((terrno = grantCheck(TSDB_GRANT_DNODE)) != 0) {
    code = terrno;
    goto _OVER;
  }
703

S
Shengliang Guan 已提交
704
  if (tDeserializeSCreateDnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
705
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
706
    goto _OVER;
S
Shengliang Guan 已提交
707 708
  }

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

S
Shengliang Guan 已提交
714
  if (createReq.fqdn[0] == 0 || createReq.port <= 0 || createReq.port > UINT16_MAX) {
715
    terrno = TSDB_CODE_MND_INVALID_DNODE_EP;
S
Shengliang Guan 已提交
716
    goto _OVER;
S
Shengliang Guan 已提交
717 718
  }

S
Shengliang Guan 已提交
719
  char ep[TSDB_EP_LEN];
S
Shengliang Guan 已提交
720 721
  snprintf(ep, TSDB_EP_LEN, "%s:%d", createReq.fqdn, createReq.port);
  pDnode = mndAcquireDnodeByEp(pMnode, ep);
S
Shengliang Guan 已提交
722
  if (pDnode != NULL) {
723
    terrno = TSDB_CODE_MND_DNODE_ALREADY_EXIST;
S
Shengliang Guan 已提交
724
    goto _OVER;
S
Shengliang Guan 已提交
725
  }
S
Shengliang Guan 已提交
726

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

S
Shengliang Guan 已提交
730
_OVER:
S
Shengliang Guan 已提交
731
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
732
    mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
S
Shengliang Guan 已提交
733 734
  }

S
Shengliang Guan 已提交
735 736
  mndReleaseDnode(pMnode, pDnode);
  return code;
S
Shengliang Guan 已提交
737 738
}

739
static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMnodeObj *pMObj, SQnodeObj *pQObj,
S
Shengliang Guan 已提交
740
                            SSnodeObj *pSObj, int32_t numOfVnodes, bool force) {
S
Shengliang Guan 已提交
741 742 743 744
  int32_t  code = -1;
  SSdbRaw *pRaw = NULL;
  STrans  *pTrans = NULL;

745
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-dnode");
S
Shengliang Guan 已提交
746
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
747
  mndTransSetSerial(pTrans);
S
Shengliang Guan 已提交
748
  mInfo("trans:%d, used to drop dnode:%d, force:%d", pTrans->id, pDnode->id, force);
749
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
750

S
Shengliang Guan 已提交
751
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
752 753
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendRedolog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
754
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPING);
S
Shengliang Guan 已提交
755
  pRaw = NULL;
S
Shengliang Guan 已提交
756

S
Shengliang Guan 已提交
757
  pRaw = mndDnodeActionEncode(pDnode);
S
Shengliang Guan 已提交
758 759
  if (pRaw == NULL) goto _OVER;
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
760
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
761 762
  pRaw = NULL;

S
Shengliang Guan 已提交
763
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
764
    mInfo("trans:%d, mnode on dnode:%d will be dropped", pTrans->id, pDnode->id);
S
Shengliang Guan 已提交
765
    if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pMObj, force) != 0) goto _OVER;
S
Shengliang Guan 已提交
766
  }
767 768 769

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

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

S
Shengliang Guan 已提交
778
  if (numOfVnodes > 0) {
S
Shengliang Guan 已提交
779
    mInfo("trans:%d, %d vnodes on dnode:%d will be dropped", pTrans->id, numOfVnodes, pDnode->id);
S
Shengliang Guan 已提交
780
    if (mndSetMoveVgroupsInfoToTrans(pMnode, pTrans, pDnode->id, force) != 0) goto _OVER;
S
Shengliang Guan 已提交
781
  }
S
Shengliang Guan 已提交
782

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

S
Shengliang Guan 已提交
785 786 787
  code = 0;

_OVER:
S
Shengliang Guan 已提交
788
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
789 790
  sdbFreeRaw(pRaw);
  return code;
S
Shengliang Guan 已提交
791 792
}

S
Shengliang Guan 已提交
793
static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) {
794 795 796 797 798 799 800 801 802
  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 已提交
803
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
804
    goto _OVER;
S
Shengliang Guan 已提交
805
  }
S
Shengliang Guan 已提交
806

807
  mInfo("dnode:%d, start to drop, ep:%s:%d", dropReq.dnodeId, dropReq.fqdn, dropReq.port);
808
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
809 810
    goto _OVER;
  }
S
Shengliang Guan 已提交
811

S
Shengliang Guan 已提交
812
  pDnode = mndAcquireDnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
813
  if (pDnode == NULL) {
814 815
    int32_t err = terrno;
    char    ep[TSDB_EP_LEN + 1] = {0};
816 817 818
    snprintf(ep, sizeof(ep), dropReq.fqdn, dropReq.port);
    pDnode = mndAcquireDnodeByEp(pMnode, ep);
    if (pDnode == NULL) {
819
      terrno = err;
820 821
      goto _OVER;
    }
S
Shengliang Guan 已提交
822 823
  }

824 825
  pQObj = mndAcquireQnode(pMnode, dropReq.dnodeId);
  pSObj = mndAcquireSnode(pMnode, dropReq.dnodeId);
826 827
  pMObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
  if (pMObj != NULL) {
S
Shengliang Guan 已提交
828 829 830 831 832
    if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
      terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
      goto _OVER;
    }
    if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
833 834 835 836 837 838
      terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
      goto _OVER;
    }
  }

  int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id);
S
Shengliang Guan 已提交
839
  if ((numOfVnodes > 0 || pMObj != NULL || pSObj != NULL || pQObj != NULL) && !dropReq.force) {
S
Shengliang Guan 已提交
840
    if (!mndIsDnodeOnline(pDnode, taosGetTimestampMs())) {
841
      terrno = TSDB_CODE_DNODE_OFFLINE;
S
Shengliang Guan 已提交
842 843
      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);
S
Shengliang Guan 已提交
844 845
      goto _OVER;
    }
846 847
  }

S
Shengliang Guan 已提交
848
  code = mndDropDnode(pMnode, pReq, pDnode, pMObj, pQObj, pSObj, numOfVnodes, dropReq.force);
S
Shengliang Guan 已提交
849
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
850

S
Shengliang Guan 已提交
851
_OVER:
S
Shengliang Guan 已提交
852
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
853
    mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
854 855
  }

856
  mndReleaseDnode(pMnode, pDnode);
857
  mndReleaseMnode(pMnode, pMObj);
858 859
  mndReleaseQnode(pMnode, pQObj);
  mndReleaseSnode(pMnode, pSObj);
S
Shengliang Guan 已提交
860
  return code;
S
Shengliang Guan 已提交
861 862
}

S
Shengliang Guan 已提交
863
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
864 865
  SMnode     *pMnode = pReq->info.node;
  const char *options[] = {
866 867 868
      "debugFlag",   "dDebugFlag",   "vDebugFlag",   "mDebugFlag",   "wDebugFlag",    "sDebugFlag",   "tsdbDebugFlag",
      "tqDebugFlag", "fsDebugFlag",  "udfDebugFlag", "smaDebugFlag", "idxDebugFlag",  "tdbDebugFlag", "tmrDebugFlag",
      "uDebugFlag",  "smaDebugFlag", "rpcDebugFlag", "qDebugFlag",   "metaDebugFlag",
869 870
  };
  int32_t optionSize = tListLen(options);
S
Shengliang Guan 已提交
871 872

  SMCfgDnodeReq cfgReq = {0};
S
Shengliang Guan 已提交
873
  if (tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
S
Shengliang Guan 已提交
874 875 876
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
877

878
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
879
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE) != 0) {
S
Shengliang Guan 已提交
880 881 882
    return -1;
  }

S
Shengliang Guan 已提交
883
  SDCfgDnodeReq dcfgReq = {0};
884 885 886
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
    strcpy(dcfgReq.config, "resetlog");
  } else if (strncasecmp(cfgReq.config, "monitor", 7) == 0) {
887 888 889 890 891 892
    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 已提交
893 894 895
    const char *value = cfgReq.value;
    int32_t     flag = atoi(value);
    if (flag <= 0) {
896
      flag = atoi(cfgReq.config + 8);
S
Shengliang Guan 已提交
897
    }
898 899
    if (flag < 0 || flag > 2) {
      mError("dnode:%d, failed to config monitor since value:%d", cfgReq.dnodeId, flag);
S
Shengliang Guan 已提交
900 901 902 903
      terrno = TSDB_CODE_INVALID_CFG;
      return -1;
    }

904
    strcpy(dcfgReq.config, "monitor");
S
Shengliang Guan 已提交
905 906
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
  } else {
907 908 909 910 911 912
    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;

913 914 915 916 917 918
      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;
      }

919
      const char *value = cfgReq.value;
M
Minglei Jin 已提交
920
      int32_t     flag = atoi(value);
921 922 923
      if (flag <= 0) {
        flag = atoi(cfgReq.config + optLen + 1);
      }
924
      if (flag < 0 || flag > 255) {
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
        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 已提交
940 941
  }

S
Shengliang Guan 已提交
942
  int32_t code = -1;
943 944
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
945 946 947 948
  while (1) {
    SDnodeObj *pDnode = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
949

S
Shengliang Guan 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963
    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);
        SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
        tmsgSendReq(&epSet, &rpcMsg);
        code = 0;
      }
    }
S
Shengliang Guan 已提交
964

S
Shengliang Guan 已提交
965 966
    sdbRelease(pSdb, pDnode);
  }
967

S
Shengliang Guan 已提交
968 969 970 971
  if (code == -1) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
  }
  return code;
S
Shengliang Guan 已提交
972 973
}

S
Shengliang Guan 已提交
974
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
975
  mInfo("config rsp from dnode");
S
Shengliang Guan 已提交
976
  return 0;
977
}
S
Shengliang Guan 已提交
978

S
Shengliang Guan 已提交
979 980
static int32_t mndRetrieveConfigs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
981
  int32_t totalRows = 0;
S
Shengliang Guan 已提交
982 983
  int32_t numOfRows = 0;
  char   *cfgOpts[TSDB_CONFIG_NUMBER] = {0};
D
dapan1121 已提交
984
  char    cfgVals[TSDB_CONFIG_NUMBER][TSDB_CONFIG_VALUE_LEN + 1] = {0};
S
Shengliang Guan 已提交
985
  char   *pWrite = NULL;
S
Shengliang Guan 已提交
986 987
  int32_t cols = 0;

S
Shengliang Guan 已提交
988
  cfgOpts[totalRows] = "statusInterval";
D
dapan1121 已提交
989
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%d", tsStatusInterval);
S
Shengliang Guan 已提交
990
  totalRows++;
S
Shengliang Guan 已提交
991

S
Shengliang Guan 已提交
992
  cfgOpts[totalRows] = "timezone";
D
dapan1121 已提交
993
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsTimezoneStr);
S
Shengliang Guan 已提交
994
  totalRows++;
S
Shengliang Guan 已提交
995

S
Shengliang Guan 已提交
996
  cfgOpts[totalRows] = "locale";
D
dapan1121 已提交
997
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsLocale);
S
Shengliang Guan 已提交
998
  totalRows++;
S
Shengliang Guan 已提交
999

S
Shengliang Guan 已提交
1000
  cfgOpts[totalRows] = "charset";
D
dapan1121 已提交
1001
  snprintf(cfgVals[totalRows], TSDB_CONFIG_VALUE_LEN, "%s", tsCharset);
S
Shengliang Guan 已提交
1002
  totalRows++;
S
Shengliang Guan 已提交
1003

1004
  char buf[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
D
dapan1121 已提交
1005
  char bufVal[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
1006

S
Shengliang Guan 已提交
1007
  for (int32_t i = 0; i < totalRows; i++) {
S
Shengliang Guan 已提交
1008 1009
    cols = 0;

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

D
dapan1121 已提交
1014
    STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONFIG_VALUE_LEN);
1015
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1016
    colDataSetVal(pColInfo, numOfRows, (const char *)bufVal, false);
S
Shengliang Guan 已提交
1017 1018

    numOfRows++;
S
Shengliang Guan 已提交
1019 1020
  }

1021
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1022 1023 1024 1025 1026
  return numOfRows;
}

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

S
Shengliang Guan 已提交
1027 1028
static int32_t mndRetrieveDnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1029 1030 1031
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
1032
  ESdbStatus objStatus = 0;
S
Shengliang Guan 已提交
1033
  SDnodeObj *pDnode = NULL;
S
Shengliang Guan 已提交
1034
  int64_t    curMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
1035 1036

  while (numOfRows < rows) {
1037
    pShow->pIter = sdbFetchAll(pSdb, SDB_DNODE, pShow->pIter, (void **)&pDnode, &objStatus, true);
S
Shengliang Guan 已提交
1038
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
1039
    bool online = mndIsDnodeOnline(pDnode, curMs);
S
Shengliang Guan 已提交
1040 1041 1042

    cols = 0;

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

    char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0};
1047
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
1048 1049

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1050
    colDataSetVal(pColInfo, numOfRows, buf, false);
1051 1052 1053

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

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

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    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";
    }

1071
    char b1[16] = {0};
1072
    STR_TO_VARSTR(b1, status);
1073
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1074
    colDataSetVal(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
1075

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

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

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

1085
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1086
    colDataSetVal(pColInfo, numOfRows, b, false);
wafwerar's avatar
wafwerar 已提交
1087
    taosMemoryFreeClear(b);
S
Shengliang Guan 已提交
1088 1089 1090 1091 1092

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

1093
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1094 1095 1096 1097 1098 1099
  return numOfRows;
}

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