mndMnode.c 19.4 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(SRpcMsg *pReq);
static int32_t  mndProcessDropMnodeReq(SRpcMsg *pReq);
static int32_t  mndProcessCreateMnodeRsp(SRpcMsg *pRsp);
static int32_t  mndProcessAlterMnodeRsp(SRpcMsg *pRsp);
static int32_t  mndProcessDropMnodeRsp(SRpcMsg *pRsp);
static int32_t  mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
39
static void     mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
40 41

int32_t mndInitMnode(SMnode *pMnode) {
S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51
  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,
  };
S
Shengliang Guan 已提交
52

H
Hongze Cheng 已提交
53 54 55 56 57
  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 已提交
58 59 60

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndRetrieveMnodes);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndCancelGetNextMnode);
S
Shengliang Guan 已提交
61 62 63 64 65 66

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

void mndCleanupMnode(SMnode *pMnode) {}

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

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

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

    sdbRelease(pSdb, pObj);
  }
}

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

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

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

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

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

  terrno = 0;

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

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

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
142 143
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

152
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
153
  if (pRow == NULL) goto _OVER;
154

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

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

  terrno = 0;

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

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

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

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

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

  return 0;
}

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
280 281 282
    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
289
  createReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
290

S
Shengliang Guan 已提交
291 292 293 294 295 296 297
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    STransAction action = {0};

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

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

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

    sdbRelease(pSdb, pMObj);
  }

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

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

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

  return 0;
}

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

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

S
Shengliang Guan 已提交
350
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_MNODE, pReq);
351
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
352

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

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

360 361
  code = 0;

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

S
Shengliang Guan 已提交
367 368
static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
369 370 371 372 373 374
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateMnodeReq createReq = {0};

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

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

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

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

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

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

  code = mndCreateMnode(pMnode, pReq, pDnode, &createReq);
S
Shengliang Guan 已提交
407
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
408

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

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

  return code;
S
Shengliang Guan 已提交
419 420
}

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

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

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

S
Shengliang Guan 已提交
456 457 458
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
459
  alterReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
460

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

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

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

    sdbRelease(pSdb, pMObj);
  }

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

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

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

  return 0;
}

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

S
Shengliang Guan 已提交
517
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_MNODE, pReq);
518
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
519

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

522 523 524 525
  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 已提交
526

527 528
  code = 0;

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

S
Shengliang Guan 已提交
534 535
static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
536 537 538 539 540
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

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

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

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

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

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

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

S
Shengliang Guan 已提交
578
  code = mndDropMnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
579
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
580

581
_OVER:
S
Shengliang Guan 已提交
582
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
583 584 585 586 587 588 589
    mError("mnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
  }

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

  return code;
S
Shengliang Guan 已提交
590 591
}

S
Shengliang Guan 已提交
592
static int32_t mndProcessCreateMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
593
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
594 595 596
  return 0;
}

S
Shengliang Guan 已提交
597
static int32_t mndProcessAlterMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
598
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
599 600
  return 0;
}
S
Shengliang Guan 已提交
601

S
Shengliang Guan 已提交
602
static int32_t mndProcessDropMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
603
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
604 605
  return 0;
}
S
Shengliang Guan 已提交
606

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

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

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

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

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

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

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

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

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

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

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

  return numOfRows;
}

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