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

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndMnode.h"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
S
Shengliang Guan 已提交
19 20
#include "mndDnode.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
21
#include "mndTrans.h"
S
Shengliang Guan 已提交
22
#include "mndUser.h"
S
Shengliang Guan 已提交
23

24 25
#define MNODE_VER_NUMBER   1
#define MNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26

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

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 已提交
51 52 53 54 55
  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 已提交
56 57 58

  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) {}

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

73
void mndReleaseMnode(SMnode *pMnode, SMnodeObj *pObj) {
S
Shengliang Guan 已提交
74
  SSdb *pSdb = pMnode->pSdb;
75
  sdbRelease(pMnode->pSdb, pObj);
S
Shengliang Guan 已提交
76 77
}

78
const char *mndGetRoleStr(int32_t showType) {
S
Shengliang Guan 已提交
79 80 81 82 83 84 85 86 87 88 89 90
  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 已提交
91 92 93 94 95 96 97 98
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;

S
Shengliang Guan 已提交
99
    ESyncState lastRole = pObj->role;
S
Shengliang Guan 已提交
100 101 102 103 104
    if (pObj->id == 1) {
      pObj->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pObj->role = TAOS_SYNC_STATE_CANDIDATE;
    }
S
Shengliang Guan 已提交
105 106 107
    if (pObj->role != lastRole) {
      pObj->roleTime = taosGetTimestampMs();
    }
S
Shengliang Guan 已提交
108 109 110 111 112

    sdbRelease(pSdb, pObj);
  }
}

S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120 121 122
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);

123
  mDebug("mnode:%d, will be created while deploy sdb, raw:%p", mnodeObj.id, pRaw);
S
Shengliang Guan 已提交
124 125 126
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
127
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
128 129
  terrno = TSDB_CODE_OUT_OF_MEMORY;

130 131
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, MNODE_VER_NUMBER, sizeof(SMnodeObj) + MNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
132 133

  int32_t dataPos = 0;
134 135 136 137
  SDB_SET_INT32(pRaw, dataPos, pObj->id, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, _OVER)
  SDB_SET_RESERVE(pRaw, dataPos, MNODE_RESERVE_SIZE, _OVER)
138 139 140

  terrno = 0;

141
_OVER:
142 143 144 145 146
  if (terrno != 0) {
    mError("mnode:%d, failed to encode to raw:%p since %s", pObj->id, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
147

148
  mTrace("mnode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
149 150 151 152
  return pRaw;
}

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
153 154
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
155 156 157
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

158
  if (sver != MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
159
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
160
    goto _OVER;
S
Shengliang Guan 已提交
161 162
  }

163
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
164
  if (pRow == NULL) goto _OVER;
165

S
Shengliang Guan 已提交
166
  SMnodeObj *pObj = sdbGetRowObj(pRow);
167
  if (pObj == NULL) goto _OVER;
S
Shengliang Guan 已提交
168 169

  int32_t dataPos = 0;
170 171 172 173
  SDB_GET_INT32(pRaw, dataPos, &pObj->id, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, _OVER)
  SDB_GET_RESERVE(pRaw, dataPos, MNODE_RESERVE_SIZE, _OVER)
174 175 176

  terrno = 0;

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

184
  mTrace("mnode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
185 186 187
  return pRow;
}

S
Shengliang Guan 已提交
188
static int32_t mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj) {
189
  mTrace("mnode:%d, perform insert action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
190 191
  pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
  if (pObj->pDnode == NULL) {
S
Shengliang Guan 已提交
192
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
193
    mError("mnode:%d, failed to perform insert action since %s", pObj->id, terrstr());
S
Shengliang Guan 已提交
194 195 196
    return -1;
  }

197
  pObj->role = TAOS_SYNC_STATE_FOLLOWER;
S
Shengliang Guan 已提交
198 199 200
  return 0;
}

S
Shengliang Guan 已提交
201
static int32_t mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj) {
202
  mTrace("mnode:%d, perform delete action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
203 204 205
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
S
Shengliang Guan 已提交
206 207 208 209 210
  }

  return 0;
}

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

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

S
Shengliang Guan 已提交
220 221
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
222 223 224
    return false;
  }

S
Shengliang Guan 已提交
225
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
226
  return true;
S
Shengliang Guan 已提交
227 228
}

S
Shengliang Guan 已提交
229 230 231 232 233 234
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
  SSdb *pSdb = pMnode->pSdb;
  pEpSet->numOfEps = 0;

  void *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
235 236
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
S
Shengliang Guan 已提交
237
    if (pIter == NULL) break;
238 239 240 241 242 243 244 245
    if (pObj->pDnode == NULL) {
      mError("mnode:%d, no corresponding dnode exists", pObj->id);
    } else {
      if (pObj->role == TAOS_SYNC_STATE_LEADER) {
        pEpSet->inUse = pEpSet->numOfEps;
      }
      addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
      sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
246 247
    }
  }
S
Shengliang Guan 已提交
248 249
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
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 已提交
274 275 276 277
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
278

S
Shengliang Guan 已提交
279
  SDCreateMnodeReq createReq = {0};
S
Shengliang Guan 已提交
280 281 282 283
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
284

S
Shengliang Guan 已提交
285
    SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
286 287
    pReplica->id = pMObj->id;
    pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
288 289
    memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    numOfReplicas++;
290

S
Shengliang Guan 已提交
291 292 293
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
294
  SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
295 296
  pReplica->id = pDnode->id;
  pReplica->port = pDnode->port;
S
Shengliang Guan 已提交
297 298 299
  memcpy(pReplica->fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  numOfReplicas++;

S
Shengliang Guan 已提交
300
  createReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
301

S
Shengliang Guan 已提交
302 303 304 305 306 307 308
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    STransAction action = {0};

S
Shengliang Guan 已提交
309 310
    createReq.dnodeId = pMObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
311
    void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
312
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
313 314

    action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
315
    action.pCont = pReq;
S
Shengliang Guan 已提交
316
    action.contLen = contLen;
H
Hongze Cheng 已提交
317
    action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
318
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
319 320

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
321
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
322 323 324 325 326 327 328 329 330 331 332 333
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
334 335
    createReq.dnodeId = pObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
336
    void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
337
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
338 339

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
340
    action.pCont = pReq;
S
Shengliang Guan 已提交
341
    action.contLen = contLen;
H
Hongze Cheng 已提交
342
    action.msgType = TDMT_DND_CREATE_MNODE;
S
shm  
Shengliang Guan 已提交
343
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
344
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
345
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
346 347
      return -1;
    }
348 349 350 351 352
  }

  return 0;
}

S
Shengliang Guan 已提交
353
static int32_t mndCreateMnode(SMnode *pMnode, SNodeMsg *pReq, SDnodeObj *pDnode, SMCreateMnodeReq *pCreate) {
S
Shengliang Guan 已提交
354 355
  int32_t code = -1;

S
Shengliang Guan 已提交
356
  SMnodeObj mnodeObj = {0};
S
Shengliang Guan 已提交
357
  mnodeObj.id = pDnode->id;
S
Shengliang Guan 已提交
358 359 360
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;

S
Shengliang Guan 已提交
361
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_MNODE, &pReq->rpcMsg);
362
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
363

S
Shengliang Guan 已提交
364
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
365 366 367
  if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) goto _OVER;
  if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeObj) != 0) goto _OVER;
  if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) goto _OVER;
S
Shengliang Guan 已提交
368

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

371 372
  code = 0;

373
_OVER:
S
Shengliang Guan 已提交
374
  mndTransDrop(pTrans);
375
  return code;
S
Shengliang Guan 已提交
376 377
}

S
Shengliang Guan 已提交
378 379
static int32_t mndProcessCreateMnodeReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
380 381 382 383 384 385
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateMnodeReq createReq = {0};

386
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
387
    terrno = TSDB_CODE_INVALID_MSG;
388
    goto _OVER;
S
Shengliang Guan 已提交
389
  }
S
Shengliang Guan 已提交
390

S
Shengliang Guan 已提交
391
  mDebug("mnode:%d, start to create", createReq.dnodeId);
S
Shengliang Guan 已提交
392

S
Shengliang Guan 已提交
393
  pObj = mndAcquireMnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
394
  if (pObj != NULL) {
S
Shengliang Guan 已提交
395
    terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
396
    goto _OVER;
S
Shengliang Guan 已提交
397
  } else if (terrno != TSDB_CODE_MND_MNODE_NOT_EXIST) {
398
    goto _OVER;
S
Shengliang Guan 已提交
399 400
  }

S
Shengliang Guan 已提交
401
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
402 403
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
404
    goto _OVER;
S
Shengliang Guan 已提交
405 406
  }

S
Shengliang Guan 已提交
407 408 409
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
410
    goto _OVER;
S
Shengliang Guan 已提交
411
  }
S
Shengliang Guan 已提交
412

S
Shengliang Guan 已提交
413
  if (mndCheckNodeAuth(pUser)) {
414
    goto _OVER;
S
Shengliang Guan 已提交
415 416 417 418 419
  }

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

420
_OVER:
S
Shengliang Guan 已提交
421 422
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
423 424
  }

S
Shengliang Guan 已提交
425 426 427 428 429
  mndReleaseMnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
430 431
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
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 已提交
448 449 450 451
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
452

S
Shengliang Guan 已提交
453
  SDAlterMnodeReq alterReq = {0};
S
Shengliang Guan 已提交
454 455 456 457
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
458

S
Shengliang Guan 已提交
459
    if (pMObj->id != pObj->id) {
S
Shengliang Guan 已提交
460
      SReplica *pReplica = &alterReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
461 462
      pReplica->id = pMObj->id;
      pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
463 464 465
      memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfReplicas++;
    }
466

S
Shengliang Guan 已提交
467 468 469
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
470
  alterReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
471

S
Shengliang Guan 已提交
472 473 474 475 476 477 478
  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};

S
Shengliang Guan 已提交
479 480
      alterReq.dnodeId = pMObj->id;
      int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq);
wafwerar's avatar
wafwerar 已提交
481
      void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
482
      tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
483 484

      action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
485
      action.pCont = pReq;
S
Shengliang Guan 已提交
486
      action.contLen = contLen;
H
Hongze Cheng 已提交
487
      action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
488
      action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
489 490

      if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
491
        taosMemoryFree(pReq);
S
Shengliang Guan 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
505 506
    SDDropMnodeReq dropReq = {0};
    dropReq.dnodeId = pObj->id;
507
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
508
    void   *pReq = taosMemoryMalloc(contLen);
509
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
510 511

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
512
    action.pCont = pReq;
S
Shengliang Guan 已提交
513
    action.contLen = contLen;
H
Hongze Cheng 已提交
514
    action.msgType = TDMT_DND_DROP_MNODE;
S
shm  
Shengliang Guan 已提交
515
    action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
516
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
517
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
518 519
      return -1;
    }
520 521 522 523 524
  }

  return 0;
}

S
Shengliang Guan 已提交
525
static int32_t mndDropMnode(SMnode *pMnode, SNodeMsg *pReq, SMnodeObj *pObj) {
526
  int32_t code = -1;
S
Shengliang Guan 已提交
527

S
Shengliang Guan 已提交
528
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_MNODE, &pReq->rpcMsg);
529
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
530

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

533 534 535 536
  if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) goto _OVER;
  if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
537

538 539
  code = 0;

540
_OVER:
S
Shengliang Guan 已提交
541
  mndTransDrop(pTrans);
542
  return code;
S
Shengliang Guan 已提交
543 544
}

S
Shengliang Guan 已提交
545 546
static int32_t mndProcessDropMnodeReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
547 548 549 550 551
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

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

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

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

S
Shengliang Guan 已提交
564
  pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
565
  if (pObj == NULL) {
566 567 568 569 570 571 572 573 574 575 576
    goto _OVER;
  }

  if (pMnode->selfId == dropReq.dnodeId) {
    terrno = TSDB_CODE_MND_CANT_DROP_MASTER;
    goto _OVER;
  }

  if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
    terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
    goto _OVER;
S
Shengliang Guan 已提交
577 578
  }

S
Shengliang Guan 已提交
579 580 581
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
582
    goto _OVER;
S
Shengliang Guan 已提交
583 584
  }

S
Shengliang Guan 已提交
585
  if (mndCheckNodeAuth(pUser)) {
586
    goto _OVER;
S
Shengliang Guan 已提交
587
  }
S
Shengliang Guan 已提交
588

S
Shengliang Guan 已提交
589 590 591
  code = mndDropMnode(pMnode, pReq, pObj);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

592
_OVER:
S
Shengliang Guan 已提交
593 594 595 596 597 598 599 600
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("mnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
  }

  mndReleaseMnode(pMnode, pObj);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
601 602
}

S
Shengliang Guan 已提交
603
static int32_t mndProcessCreateMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
604
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
605 606 607
  return 0;
}

S
Shengliang Guan 已提交
608
static int32_t mndProcessAlterMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
609
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
610 611
  return 0;
}
S
Shengliang Guan 已提交
612

S
Shengliang Guan 已提交
613
static int32_t mndProcessDropMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
614
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
615 616
  return 0;
}
S
Shengliang Guan 已提交
617

S
Shengliang Guan 已提交
618 619
static int32_t mndRetrieveMnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
620 621 622
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
623
  SMnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
624 625 626
  char      *pWrite;

  while (numOfRows < rows) {
S
Shengliang Guan 已提交
627
    pShow->pIter = sdbFetch(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj);
S
Shengliang Guan 已提交
628 629 630 631 632
    if (pShow->pIter == NULL) break;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
633
    *(int16_t *)pWrite = pObj->id;
S
Shengliang Guan 已提交
634 635 636
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
637
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pObj->pDnode->ep, pShow->bytes[cols]);
S
Shengliang Guan 已提交
638 639 640 641

    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
642
    const char *roles = mndGetRoleStr(pObj->role);
S
Shengliang Guan 已提交
643 644 645 646
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, roles, pShow->bytes[cols]);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
647
    *(int64_t *)pWrite = pObj->roleTime;
S
Shengliang Guan 已提交
648 649 650
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
651
    *(int64_t *)pWrite = pObj->createdTime;
S
Shengliang Guan 已提交
652 653 654
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
655
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
656 657
  }

S
Shengliang Guan 已提交
658
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
659 660 661 662 663 664 665 666 667
  pShow->numOfReads += numOfRows;

  return numOfRows;
}

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