mndMnode.c 20.6 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 18 19
#include "mndMnode.h"
#include "mndDnode.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
20
#include "mndTrans.h"
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22 23
#define TSDB_MNODE_VER_NUMBER 1
#define TSDB_MNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
static int32_t  mndCreateDefaultMnode(SMnode *pMnode);
S
Shengliang Guan 已提交
26
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj);
S
Shengliang Guan 已提交
27
static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
28 29
static int32_t  mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj);
static int32_t  mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj);
S
Shengliang Guan 已提交
30
static int32_t  mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOldMnode, SMnodeObj *pNewMnode);
S
Shengliang Guan 已提交
31 32
static int32_t  mndProcessCreateMnodeReq(SMnodeMsg *pMsg);
static int32_t  mndProcessDropMnodeReq(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
33
static int32_t  mndProcessCreateMnodeRsp(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
34
static int32_t  mndProcessAlterMnodeRsp(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
35 36 37 38
static int32_t  mndProcessDropMnodeRsp(SMnodeMsg *pMsg);
static int32_t  mndGetMnodeMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t  mndRetrieveMnodes(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void     mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
39 40 41 42 43 44 45 46 47 48 49

int32_t mndInitMnode(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_MNODE,
                     .keyType = SDB_KEY_INT32,
                     .deployFp = (SdbDeployFp)mndCreateDefaultMnode,
                     .encodeFp = (SdbEncodeFp)mndMnodeActionEncode,
                     .decodeFp = (SdbDecodeFp)mndMnodeActionDecode,
                     .insertFp = (SdbInsertFp)mndMnodeActionInsert,
                     .updateFp = (SdbUpdateFp)mndMnodeActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndMnodeActionDelete};

H
Hongze Cheng 已提交
50 51 52 53 54
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_MNODE, mndProcessCreateMnodeReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_MNODE, mndProcessDropMnodeReq);
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_MNODE_RSP, mndProcessCreateMnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_ALTER_MNODE_RSP, mndProcessAlterMnodeRsp);
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_MNODE_RSP, mndProcessDropMnodeRsp);
S
Shengliang Guan 已提交
55 56 57 58

  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndGetMnodeMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndRetrieveMnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndCancelGetNextMnode);
S
Shengliang Guan 已提交
59 60 61 62 63 64

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupMnode(SMnode *pMnode) {}

S
Shengliang Guan 已提交
65
static SMnodeObj *mndAcquireMnode(SMnode *pMnode, int32_t mnodeId) {
S
Shengliang Guan 已提交
66 67 68 69 70 71
  SSdb      *pSdb = pMnode->pSdb;
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &mnodeId);
  if (pObj == NULL) {
    terrno = TSDB_CODE_MND_MNODE_NOT_EXIST;
  }
  return pObj;
S
Shengliang Guan 已提交
72 73
}

S
Shengliang Guan 已提交
74
static void mndReleaseMnode(SMnode *pMnode, SMnodeObj *pObj) {
S
Shengliang Guan 已提交
75
  SSdb *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
76
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}

char *mndGetRoleStr(int32_t showType) {
  switch (showType) {
    case TAOS_SYNC_STATE_FOLLOWER:
      return "unsynced";
    case TAOS_SYNC_STATE_CANDIDATE:
      return "slave";
    case TAOS_SYNC_STATE_LEADER:
      return "master";
    default:
      return "undefined";
  }
}

S
Shengliang Guan 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
void mndUpdateMnodeRole(SMnode *pMnode) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    if (pObj->id == 1) {
      pObj->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pObj->role = TAOS_SYNC_STATE_CANDIDATE;
    }

    sdbRelease(pSdb, pObj);
  }
}

S
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
static int32_t mndCreateDefaultMnode(SMnode *pMnode) {
  SMnodeObj mnodeObj = {0};
  mnodeObj.id = 1;
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;

  SSdbRaw *pRaw = mndMnodeActionEncode(&mnodeObj);
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

  mDebug("mnode:%d, will be created while deploy sdb", mnodeObj.id);
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
124
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
S
Shengliang Guan 已提交
125
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, TSDB_MNODE_VER_NUMBER, sizeof(SMnodeObj) + TSDB_MNODE_RESERVE_SIZE);
S
Shengliang Guan 已提交
126 127 128
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
129 130 131
  SDB_SET_INT32(pRaw, dataPos, pObj->id);
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime)
S
Shengliang Guan 已提交
132
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_MNODE_RESERVE_SIZE)
S
Shengliang Guan 已提交
133 134 135 136 137 138 139 140

  return pRaw;
}

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

S
Shengliang Guan 已提交
141
  if (sver != TSDB_MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
142 143 144 145 146 147
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    mError("failed to decode mnode since %s", terrstr());
    return NULL;
  }

  SSdbRow   *pRow = sdbAllocRow(sizeof(SMnodeObj));
S
Shengliang Guan 已提交
148 149
  SMnodeObj *pObj = sdbGetRowObj(pRow);
  if (pObj == NULL) return NULL;
S
Shengliang Guan 已提交
150 151

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
152 153 154
  SDB_GET_INT32(pRaw, pRow, dataPos, &pObj->id)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pObj->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pObj->updateTime)
S
Shengliang Guan 已提交
155
  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_MNODE_RESERVE_SIZE)
S
Shengliang Guan 已提交
156 157 158 159

  return pRow;
}

S
Shengliang Guan 已提交
160
static void mnodeResetMnode(SMnodeObj *pObj) { pObj->role = TAOS_SYNC_STATE_FOLLOWER; }
S
Shengliang Guan 已提交
161

S
Shengliang Guan 已提交
162 163 164 165
static int32_t mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj) {
  mTrace("mnode:%d, perform insert action", pObj->id);
  pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
  if (pObj->pDnode == NULL) {
S
Shengliang Guan 已提交
166
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
167
    mError("mnode:%d, failed to perform insert action since %s", pObj->id, terrstr());
S
Shengliang Guan 已提交
168 169 170
    return -1;
  }

S
Shengliang Guan 已提交
171
  mnodeResetMnode(pObj);
S
Shengliang Guan 已提交
172 173 174
  return 0;
}

S
Shengliang Guan 已提交
175 176 177 178 179
static int32_t mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj) {
  mTrace("mnode:%d, perform delete action", pObj->id);
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
S
Shengliang Guan 已提交
180 181 182 183 184
  }

  return 0;
}

S
Shengliang Guan 已提交
185 186 187
static int32_t mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOldMnode, SMnodeObj *pNewMnode) {
  mTrace("mnode:%d, perform update action", pOldMnode->id);
  pOldMnode->updateTime = pNewMnode->updateTime;
S
Shengliang Guan 已提交
188
  return 0;
S
Shengliang Guan 已提交
189 190 191 192 193
}

bool mndIsMnode(SMnode *pMnode, int32_t dnodeId) {
  SSdb *pSdb = pMnode->pSdb;

S
Shengliang Guan 已提交
194 195
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
196 197 198
    return false;
  }

S
Shengliang Guan 已提交
199
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
200
  return true;
S
Shengliang Guan 已提交
201 202
}

S
Shengliang Guan 已提交
203 204 205 206 207 208 209
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
  SSdb *pSdb = pMnode->pSdb;

  pEpSet->numOfEps = 0;

  void *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
210 211
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
S
Shengliang Guan 已提交
212
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
213
    if (pObj->pDnode == NULL) break;
S
Shengliang Guan 已提交
214

S
Shengliang Guan 已提交
215 216 217
    pEpSet->port[pEpSet->numOfEps] = htons(pObj->pDnode->port);
    memcpy(pEpSet->fqdn[pEpSet->numOfEps], pObj->pDnode->fqdn, TSDB_FQDN_LEN);
    if (pObj->role == TAOS_SYNC_STATE_LEADER) {
S
Shengliang Guan 已提交
218 219 220 221 222
      pEpSet->inUse = pEpSet->numOfEps;
    }

    pEpSet->numOfEps++;
  }
S
Shengliang Guan 已提交
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static int32_t mndSetCreateMnodeRedoLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndMnodeActionEncode(pObj);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;
  return 0;
}

static int32_t mndSetCreateMnodeUndoLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
  SSdbRaw *pUndoRaw = mndMnodeActionEncode(pObj);
  if (pUndoRaw == NULL) return -1;
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) return -1;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

static int32_t mndSetCreateMnodeCommitLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndMnodeActionEncode(pObj);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
249 250 251 252
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
253

S
Shengliang Guan 已提交
254 255 256 257 258
  SCreateMnodeInMsg createMsg = {0};
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
259

S
Shengliang Guan 已提交
260 261 262 263 264
    SReplica *pReplica = &createMsg.replicas[numOfReplicas];
    pReplica->id = htonl(pMObj->id);
    pReplica->port = htons(pMObj->pDnode->port);
    memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    numOfReplicas++;
265

S
Shengliang Guan 已提交
266 267 268 269 270 271 272 273 274
    sdbRelease(pSdb, pMObj);
  }

  SReplica *pReplica = &createMsg.replicas[numOfReplicas];
  pReplica->id = htonl(pDnode->id);
  pReplica->port = htons(pDnode->port);
  memcpy(pReplica->fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  numOfReplicas++;

S
Shengliang Guan 已提交
275 276
  createMsg.replica = numOfReplicas;

S
Shengliang Guan 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    STransAction action = {0};

    SAlterMnodeInMsg *pMsg = malloc(sizeof(SAlterMnodeInMsg));
    if (pMsg == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }
    memcpy(pMsg, &createMsg, sizeof(SAlterMnodeInMsg));

    pMsg->dnodeId = htonl(pMObj->id);
    action.epSet = mndGetDnodeEpset(pMObj->pDnode);
    action.pCont = pMsg;
    action.contLen = sizeof(SAlterMnodeInMsg);
H
Hongze Cheng 已提交
296
    action.msgType = TDMT_DND_ALTER_MNODE;
S
Shengliang Guan 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pMsg);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }

    sdbRelease(pSdb, pMObj);
  }

  {
    STransAction action = {0};
    action.epSet = mndGetDnodeEpset(pDnode);

    SCreateMnodeInMsg *pMsg = malloc(sizeof(SCreateMnodeInMsg));
    if (pMsg == NULL) return -1;
    memcpy(pMsg, &createMsg, sizeof(SAlterMnodeInMsg));
    pMsg->dnodeId = htonl(pObj->id);

    action.epSet = mndGetDnodeEpset(pDnode);
    action.pCont = pMsg;
    action.contLen = sizeof(SCreateMnodeInMsg);
H
Hongze Cheng 已提交
320
    action.msgType = TDMT_DND_CREATE_MNODE;
S
Shengliang Guan 已提交
321 322 323 324
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pMsg);
      return -1;
    }
325 326 327 328 329
  }

  return 0;
}

S
Shengliang Guan 已提交
330
static int32_t mndCreateMnode(SMnode *pMnode, SMnodeMsg *pMsg, SDnodeObj *pDnode, SCreateMnodeMsg *pCreate) {
S
Shengliang Guan 已提交
331
  SMnodeObj mnodeObj = {0};
S
Shengliang Guan 已提交
332
  mnodeObj.id = pDnode->id;
S
Shengliang Guan 已提交
333 334 335
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;

336
  int32_t code = -1;
S
Shengliang Guan 已提交
337
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pMsg->rpcMsg);
S
Shengliang Guan 已提交
338
  if (pTrans == NULL) {
339 340
    mError("mnode:%d, failed to create since %s", pCreate->dnodeId, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
341
  }
342
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
S
Shengliang Guan 已提交
343

344 345 346
  if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) {
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
347 348
  }

349 350 351
  if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeObj) != 0) {
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
352 353
  }

S
Shengliang Guan 已提交
354
  if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) {
355 356
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
357 358
  }

S
Shengliang Guan 已提交
359
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
360
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
361
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
362 363
  }

364 365 366
  code = 0;

CREATE_MNODE_OVER:
S
Shengliang Guan 已提交
367
  mndTransDrop(pTrans);
368
  return code;
S
Shengliang Guan 已提交
369 370
}

S
Shengliang Guan 已提交
371
static int32_t mndProcessCreateMnodeReq(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
372 373 374 375 376 377 378
  SMnode          *pMnode = pMsg->pMnode;
  SCreateMnodeMsg *pCreate = pMsg->rpcMsg.pCont;

  pCreate->dnodeId = htonl(pCreate->dnodeId);

  mDebug("mnode:%d, start to create", pCreate->dnodeId);

S
Shengliang Guan 已提交
379 380
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pCreate->dnodeId);
  if (pObj != NULL) {
S
Shengliang Guan 已提交
381
    mndReleaseMnode(pMnode, pObj);
S
Shengliang Guan 已提交
382
    mError("mnode:%d, mnode already exist", pObj->id);
S
Shengliang Guan 已提交
383 384 385 386
    terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
387 388
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, pCreate->dnodeId);
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
389
    mError("mnode:%d, dnode not exist", pCreate->dnodeId);
S
Shengliang Guan 已提交
390 391 392 393 394 395
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    return -1;
  }

  int32_t code = mndCreateMnode(pMnode, pMsg, pDnode, pCreate);
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
396 397 398 399 400 401 402 403 404

  if (code != 0) {
    mError("mnode:%d, failed to create since %s", pCreate->dnodeId, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static int32_t mndSetDropMnodeRedoLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
  SSdbRaw *pRedoRaw = mndMnodeActionEncode(pObj);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;
  return 0;
}

static int32_t mndSetDropMnodeCommitLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
  SSdbRaw *pCommitRaw = mndMnodeActionEncode(pObj);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;
  return 0;
}

S
Shengliang Guan 已提交
421 422 423 424
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
425

S
Shengliang Guan 已提交
426 427 428 429 430
  SAlterMnodeInMsg alterMsg = {0};
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
431

S
Shengliang Guan 已提交
432 433 434 435 436 437 438
    if (pMObj->id != pObj->id) {
      SReplica *pReplica = &alterMsg.replicas[numOfReplicas];
      pReplica->id = htonl(pMObj->id);
      pReplica->port = htons(pMObj->pDnode->port);
      memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfReplicas++;
    }
439

S
Shengliang Guan 已提交
440 441 442
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
443 444
  alterMsg.replica = numOfReplicas;

S
Shengliang Guan 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
    if (pMObj->id != pObj->id) {
      STransAction action = {0};

      SAlterMnodeInMsg *pMsg = malloc(sizeof(SAlterMnodeInMsg));
      if (pMsg == NULL) {
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
      memcpy(pMsg, &alterMsg, sizeof(SAlterMnodeInMsg));

      pMsg->dnodeId = htonl(pMObj->id);
      action.epSet = mndGetDnodeEpset(pMObj->pDnode);
      action.pCont = pMsg;
      action.contLen = sizeof(SAlterMnodeInMsg);
H
Hongze Cheng 已提交
464
      action.msgType = TDMT_DND_ALTER_MNODE;
S
Shengliang Guan 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

      if (mndTransAppendRedoAction(pTrans, &action) != 0) {
        free(pMsg);
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
    }

    sdbRelease(pSdb, pMObj);
  }

  {
    STransAction action = {0};
    action.epSet = mndGetDnodeEpset(pDnode);

    SDropMnodeInMsg *pMsg = malloc(sizeof(SDropMnodeInMsg));
    if (pMsg == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    pMsg->dnodeId = htonl(pObj->id);

    action.epSet = mndGetDnodeEpset(pDnode);
    action.pCont = pMsg;
    action.contLen = sizeof(SDropMnodeInMsg);
H
Hongze Cheng 已提交
491
    action.msgType = TDMT_DND_DROP_MNODE;
S
Shengliang Guan 已提交
492 493 494 495
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pMsg);
      return -1;
    }
496 497 498 499 500
  }

  return 0;
}

S
Shengliang Guan 已提交
501
static int32_t mndDropMnode(SMnode *pMnode, SMnodeMsg *pMsg, SMnodeObj *pObj) {
502
  int32_t code = -1;
S
Shengliang Guan 已提交
503
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pMsg->rpcMsg);
S
Shengliang Guan 已提交
504
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
505
    mError("mnode:%d, failed to drop since %s", pObj->id, terrstr());
506
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
507 508
  }

509 510
  mDebug("trans:%d, used to drop mnode:%d", pTrans->id, pObj->id);

S
Shengliang Guan 已提交
511
  if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) {
512 513
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
514 515
  }

S
Shengliang Guan 已提交
516
  if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) {
517 518
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
519 520
  }

S
Shengliang Guan 已提交
521
  if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj) != 0) {
522 523
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
524 525
  }

S
Shengliang Guan 已提交
526
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
527
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
528
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
529 530
  }

531 532 533
  code = 0;

DROP_MNODE_OVER:
S
Shengliang Guan 已提交
534
  mndTransDrop(pTrans);
535
  return code;
S
Shengliang Guan 已提交
536 537
}

S
Shengliang Guan 已提交
538
static int32_t mndProcessDropMnodeReq(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
539 540 541 542 543 544 545 546 547 548 549 550
  SMnode        *pMnode = pMsg->pMnode;
  SDropMnodeMsg *pDrop = pMsg->rpcMsg.pCont;
  pDrop->dnodeId = htonl(pDrop->dnodeId);

  mDebug("mnode:%d, start to drop", pDrop->dnodeId);

  if (pDrop->dnodeId <= 0) {
    terrno = TSDB_CODE_SDB_APP_ERROR;
    mError("mnode:%d, failed to drop since %s", pDrop->dnodeId, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
551 552
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDrop->dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
553 554 555 556 557
    mError("mnode:%d, not exist", pDrop->dnodeId);
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
558
  int32_t code = mndDropMnode(pMnode, pMsg, pObj);
S
Shengliang Guan 已提交
559 560 561 562 563

  if (code != 0) {
    mError("mnode:%d, failed to drop since %s", pMnode->dnodeId, terrstr());
    return -1;
  }
S
Shengliang Guan 已提交
564

S
Shengliang Guan 已提交
565
  sdbRelease(pMnode->pSdb, pObj);
S
Shengliang Guan 已提交
566 567 568
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
569
static int32_t mndProcessCreateMnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
570
  mndTransProcessRsp(pMsg);
S
Shengliang Guan 已提交
571 572 573 574
  return 0;
}

static int32_t mndProcessAlterMnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
575
  mndTransProcessRsp(pMsg);
S
Shengliang Guan 已提交
576 577
  return 0;
}
S
Shengliang Guan 已提交
578

S
Shengliang Guan 已提交
579
static int32_t mndProcessDropMnodeRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
580
  mndTransProcessRsp(pMsg);
S
Shengliang Guan 已提交
581 582
  return 0;
}
S
Shengliang Guan 已提交
583 584 585 586 587 588

static int32_t mndGetMnodeMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
589
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
590 591 592 593

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
H
Haojun Liao 已提交
594
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
595 596 597 598
  cols++;

  pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
599 600
  strcpy(pSchema[cols].name, "endpoint");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
601 602 603 604 605
  cols++;

  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "role");
H
Haojun Liao 已提交
606
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
607 608 609 610
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
611 612
  strcpy(pSchema[cols].name, "role_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
613 614 615 616
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
617 618
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
619 620
  cols++;

H
Haojun Liao 已提交
621
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
622 623 624 625 626 627 628 629 630
  pShow->numOfColumns = cols;

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

  pShow->numOfRows = sdbGetSize(pSdb, SDB_MNODE);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
631
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
632

S
Shengliang Guan 已提交
633
  mndUpdateMnodeRole(pMnode);
S
Shengliang Guan 已提交
634 635 636 637 638 639 640 641
  return 0;
}

static int32_t mndRetrieveMnodes(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pMsg->pMnode;
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
642
  SMnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
643 644 645
  char      *pWrite;

  while (numOfRows < rows) {
S
Shengliang Guan 已提交
646
    pShow->pIter = sdbFetch(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj);
S
Shengliang Guan 已提交
647 648 649 650 651
    if (pShow->pIter == NULL) break;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
652
    *(int16_t *)pWrite = pObj->id;
S
Shengliang Guan 已提交
653 654 655
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
656
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pObj->pDnode->ep, pShow->bytes[cols]);
S
Shengliang Guan 已提交
657 658 659 660

    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
661
    char *roles = mndGetRoleStr(pObj->role);
S
Shengliang Guan 已提交
662 663 664 665
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, roles, pShow->bytes[cols]);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
666
    *(int64_t *)pWrite = pObj->roleTime;
S
Shengliang Guan 已提交
667 668 669
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
670
    *(int64_t *)pWrite = pObj->createdTime;
S
Shengliang Guan 已提交
671 672 673
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
674
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
675 676
  }

S
Shengliang Guan 已提交
677
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
678 679 680 681 682 683 684 685 686
  pShow->numOfReads += numOfRows;

  return numOfRows;
}

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