mndMnode.c 19.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
#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
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);
38
static int32_t  mndRetrieveMnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, 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
}

S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85
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 已提交
86
    ESyncState lastRole = pObj->role;
S
Shengliang Guan 已提交
87 88 89 90 91
    if (pObj->id == 1) {
      pObj->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pObj->role = TAOS_SYNC_STATE_CANDIDATE;
    }
S
Shengliang Guan 已提交
92 93 94
    if (pObj->role != lastRole) {
      pObj->roleTime = taosGetTimestampMs();
    }
S
Shengliang Guan 已提交
95 96 97 98 99

    sdbRelease(pSdb, pObj);
  }
}

S
Shengliang Guan 已提交
100 101 102 103 104 105 106 107 108 109
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);

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

S
Shengliang Guan 已提交
114
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
115 116
  terrno = TSDB_CODE_OUT_OF_MEMORY;

117 118
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, MNODE_VER_NUMBER, sizeof(SMnodeObj) + MNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
119 120

  int32_t dataPos = 0;
121 122 123 124
  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)
125 126 127

  terrno = 0;

128
_OVER:
129 130 131 132 133
  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 已提交
134

135
  mTrace("mnode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
136 137 138 139
  return pRaw;
}

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
140 141
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
142 143 144
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

145
  if (sver != MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
146
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
147
    goto _OVER;
S
Shengliang Guan 已提交
148 149
  }

150
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
151
  if (pRow == NULL) goto _OVER;
152

S
Shengliang Guan 已提交
153
  SMnodeObj *pObj = sdbGetRowObj(pRow);
154
  if (pObj == NULL) goto _OVER;
S
Shengliang Guan 已提交
155 156

  int32_t dataPos = 0;
157 158 159 160
  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)
161 162 163

  terrno = 0;

164
_OVER:
165 166
  if (terrno != 0) {
    mError("mnode:%d, failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
167
    taosMemoryFreeClear(pRow);
168 169
    return NULL;
  }
S
Shengliang Guan 已提交
170

171
  mTrace("mnode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
172 173 174
  return pRow;
}

S
Shengliang Guan 已提交
175
static int32_t mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj) {
176
  mTrace("mnode:%d, perform insert action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
177 178
  pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
  if (pObj->pDnode == NULL) {
S
Shengliang Guan 已提交
179
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
S
Shengliang Guan 已提交
180
    mError("mnode:%d, failed to perform insert action since %s", pObj->id, terrstr());
S
Shengliang Guan 已提交
181 182 183
    return -1;
  }

184
  pObj->role = TAOS_SYNC_STATE_FOLLOWER;
S
Shengliang Guan 已提交
185 186 187
  return 0;
}

S
Shengliang Guan 已提交
188
static int32_t mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj) {
189
  mTrace("mnode:%d, perform delete action, row:%p", pObj->id, pObj);
S
Shengliang Guan 已提交
190 191 192
  if (pObj->pDnode != NULL) {
    sdbRelease(pSdb, pObj->pDnode);
    pObj->pDnode = NULL;
S
Shengliang Guan 已提交
193 194 195 196 197
  }

  return 0;
}

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

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

S
Shengliang Guan 已提交
207 208
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
209 210 211
    return false;
  }

S
Shengliang Guan 已提交
212
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
213
  return true;
S
Shengliang Guan 已提交
214 215
}

S
Shengliang Guan 已提交
216 217 218 219 220 221
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
  SSdb *pSdb = pMnode->pSdb;
  pEpSet->numOfEps = 0;

  void *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
222 223
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
S
Shengliang Guan 已提交
224
    if (pIter == NULL) break;
225 226 227 228 229 230 231 232
    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 已提交
233 234
    }
  }
S
Shengliang Guan 已提交
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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 已提交
261 262 263 264
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
265

S
Shengliang Guan 已提交
266
  SDCreateMnodeReq createReq = {0};
S
Shengliang Guan 已提交
267 268 269 270
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
271

S
Shengliang Guan 已提交
272
    SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
273 274
    pReplica->id = pMObj->id;
    pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
275 276
    memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    numOfReplicas++;
277

S
Shengliang Guan 已提交
278 279 280
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
281
  SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
282 283
  pReplica->id = pDnode->id;
  pReplica->port = pDnode->port;
S
Shengliang Guan 已提交
284 285 286
  memcpy(pReplica->fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  numOfReplicas++;

S
Shengliang Guan 已提交
287
  createReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
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};

S
Shengliang Guan 已提交
296 297
    createReq.dnodeId = pMObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
298
    void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
299
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
300 301

    action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
302
    action.pCont = pReq;
S
Shengliang Guan 已提交
303
    action.contLen = contLen;
H
Hongze Cheng 已提交
304
    action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
305
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
306 307

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
308
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
309 310 311 312 313 314 315 316 317 318 319 320
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
321 322
    createReq.dnodeId = pObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
323
    void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
324
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
325 326

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
327
    action.pCont = pReq;
S
Shengliang Guan 已提交
328
    action.contLen = contLen;
H
Hongze Cheng 已提交
329
    action.msgType = TDMT_DND_CREATE_MNODE;
S
shm  
Shengliang Guan 已提交
330
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
331
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
332
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
333 334
      return -1;
    }
335 336 337 338 339
  }

  return 0;
}

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

S
Shengliang Guan 已提交
343
  SMnodeObj mnodeObj = {0};
S
Shengliang Guan 已提交
344
  mnodeObj.id = pDnode->id;
S
Shengliang Guan 已提交
345 346 347
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;

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

S
Shengliang Guan 已提交
351
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
352 353 354
  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 已提交
355

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

358 359
  code = 0;

360
_OVER:
S
Shengliang Guan 已提交
361
  mndTransDrop(pTrans);
362
  return code;
S
Shengliang Guan 已提交
363 364
}

S
Shengliang Guan 已提交
365 366
static int32_t mndProcessCreateMnodeReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
367 368 369 370 371 372
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateMnodeReq createReq = {0};

373
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
374
    terrno = TSDB_CODE_INVALID_MSG;
375
    goto _OVER;
S
Shengliang Guan 已提交
376
  }
S
Shengliang Guan 已提交
377

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

S
Shengliang Guan 已提交
380
  pObj = mndAcquireMnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
381
  if (pObj != NULL) {
S
Shengliang Guan 已提交
382
    terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
383
    goto _OVER;
S
Shengliang Guan 已提交
384
  } else if (terrno != TSDB_CODE_MND_MNODE_NOT_EXIST) {
385
    goto _OVER;
S
Shengliang Guan 已提交
386 387
  }

S
Shengliang Guan 已提交
388
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
389 390
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
391
    goto _OVER;
S
Shengliang Guan 已提交
392 393
  }

S
Shengliang Guan 已提交
394 395 396
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
397
    goto _OVER;
S
Shengliang Guan 已提交
398
  }
S
Shengliang Guan 已提交
399

S
Shengliang Guan 已提交
400
  if (mndCheckNodeAuth(pUser)) {
401
    goto _OVER;
S
Shengliang Guan 已提交
402 403 404 405 406
  }

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

407
_OVER:
S
Shengliang Guan 已提交
408 409
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
410 411
  }

S
Shengliang Guan 已提交
412 413 414 415 416
  mndReleaseMnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
417 418
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
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 已提交
435 436 437 438
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int32_t numOfReplicas = 0;
439

S
Shengliang Guan 已提交
440
  SDAlterMnodeReq alterReq = {0};
S
Shengliang Guan 已提交
441 442 443 444
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
445

S
Shengliang Guan 已提交
446
    if (pMObj->id != pObj->id) {
S
Shengliang Guan 已提交
447
      SReplica *pReplica = &alterReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
448 449
      pReplica->id = pMObj->id;
      pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
450 451 452
      memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfReplicas++;
    }
453

S
Shengliang Guan 已提交
454 455 456
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
457
  alterReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
458

S
Shengliang Guan 已提交
459 460 461 462 463 464 465
  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 已提交
466 467
      alterReq.dnodeId = pMObj->id;
      int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq);
wafwerar's avatar
wafwerar 已提交
468
      void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
469
      tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
470 471

      action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
472
      action.pCont = pReq;
S
Shengliang Guan 已提交
473
      action.contLen = contLen;
H
Hongze Cheng 已提交
474
      action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
475
      action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
476 477

      if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
478
        taosMemoryFree(pReq);
S
Shengliang Guan 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
492 493
    SDDropMnodeReq dropReq = {0};
    dropReq.dnodeId = pObj->id;
494
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
495
    void   *pReq = taosMemoryMalloc(contLen);
496
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
497 498

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
499
    action.pCont = pReq;
S
Shengliang Guan 已提交
500
    action.contLen = contLen;
H
Hongze Cheng 已提交
501
    action.msgType = TDMT_DND_DROP_MNODE;
S
shm  
Shengliang Guan 已提交
502
    action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
503
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
504
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
505 506
      return -1;
    }
507 508 509 510 511
  }

  return 0;
}

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

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

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

520 521 522 523
  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 已提交
524

525 526
  code = 0;

527
_OVER:
S
Shengliang Guan 已提交
528
  mndTransDrop(pTrans);
529
  return code;
S
Shengliang Guan 已提交
530 531
}

S
Shengliang Guan 已提交
532 533
static int32_t mndProcessDropMnodeReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
534 535 536 537 538
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

539
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
540
    terrno = TSDB_CODE_INVALID_MSG;
541
    goto _OVER;
S
Shengliang Guan 已提交
542
  }
S
Shengliang Guan 已提交
543

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

S
Shengliang Guan 已提交
546
  if (dropReq.dnodeId <= 0) {
547 548
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
S
Shengliang Guan 已提交
549 550
  }

S
Shengliang Guan 已提交
551
  pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
552
  if (pObj == NULL) {
553 554 555 556 557 558 559 560 561 562 563
    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 已提交
564 565
  }

S
Shengliang Guan 已提交
566 567 568
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
569
    goto _OVER;
S
Shengliang Guan 已提交
570 571
  }

S
Shengliang Guan 已提交
572
  if (mndCheckNodeAuth(pUser)) {
573
    goto _OVER;
S
Shengliang Guan 已提交
574
  }
S
Shengliang Guan 已提交
575

S
Shengliang Guan 已提交
576 577 578
  code = mndDropMnode(pMnode, pReq, pObj);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

579
_OVER:
S
Shengliang Guan 已提交
580 581 582 583 584 585 586 587
  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 已提交
588 589
}

S
Shengliang Guan 已提交
590
static int32_t mndProcessCreateMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
591
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
592 593 594
  return 0;
}

S
Shengliang Guan 已提交
595
static int32_t mndProcessAlterMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
596
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
597 598
  return 0;
}
S
Shengliang Guan 已提交
599

S
Shengliang Guan 已提交
600
static int32_t mndProcessDropMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
601
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
602 603
  return 0;
}
S
Shengliang Guan 已提交
604

S
Shengliang Guan 已提交
605
static int32_t mndRetrieveMnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
606
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
607 608 609
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
610
  SMnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
611 612 613
  char      *pWrite;

  while (numOfRows < rows) {
S
Shengliang Guan 已提交
614
    pShow->pIter = sdbFetch(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj);
S
Shengliang Guan 已提交
615 616 617
    if (pShow->pIter == NULL) break;

    cols = 0;
S
Shengliang Guan 已提交
618 619
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->id, false);
S
Shengliang Guan 已提交
620

621
    char b1[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
622
    STR_WITH_MAXSIZE_TO_VARSTR(b1, pObj->pDnode->ep, pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
623

624 625
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
626

S
Shengliang Guan 已提交
627
    const char *roles = syncStr(pObj->role);
628
    char       *b2 = taosMemoryCalloc(1, strlen(roles) + 1 + VARSTR_HEADER_SIZE);
629
    STR_WITH_MAXSIZE_TO_VARSTR(b2, roles, pShow->pMeta->pSchemas[cols].bytes);
630 631

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
S
Shengliang Guan 已提交
632
    colDataAppend(pColInfo, numOfRows, (const char *)b2, false);
S
Shengliang Guan 已提交
633

634 635
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->roleTime, false);
S
Shengliang Guan 已提交
636

637 638
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
639 640

    numOfRows++;
S
Shengliang Guan 已提交
641
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
642 643
  }

644
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
645 646 647 648 649 650 651 652

  return numOfRows;
}

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