mndMnode.c 21.8 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
static int32_t  mndProcessCreateMnodeReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
34
static int32_t  mndProcessAlterMnodeReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
35 36 37 38 39
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 已提交
40
static void     mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
41 42

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

H
Hongze Cheng 已提交
54
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_MNODE, mndProcessCreateMnodeReq);
S
Shengliang Guan 已提交
55
  mndSetMsgHandle(pMnode, TDMT_DND_ALTER_MNODE, mndProcessAlterMnodeReq);
H
Hongze Cheng 已提交
56 57 58 59
  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 已提交
60 61 62

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

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

void mndCleanupMnode(SMnode *pMnode) {}

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

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

S
Shengliang Guan 已提交
82 83 84 85 86 87 88 89 90 91
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);

92
  mDebug("mnode:%d, will be created while deploy sdb, raw:%p", mnodeObj.id, pRaw);
93 94

#if 0
S
Shengliang Guan 已提交
95
  return sdbWrite(pMnode->pSdb, pRaw);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#else
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_DNODE, NULL);
  if (pTrans == NULL) {
    mError("mnode:%d, failed to create since %s", mnodeObj.id, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, mnodeObj.id);

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
#endif
S
Shengliang Guan 已提交
120 121
}

S
Shengliang Guan 已提交
122
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
123 124
  terrno = TSDB_CODE_OUT_OF_MEMORY;

125 126
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, MNODE_VER_NUMBER, sizeof(SMnodeObj) + MNODE_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
127 128

  int32_t dataPos = 0;
129 130 131 132
  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)
133 134 135

  terrno = 0;

136
_OVER:
137 138 139 140 141
  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 已提交
142

143
  mTrace("mnode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
144 145 146 147
  return pRaw;
}

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
148 149
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
150 151 152
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

153
  if (sver != MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
154
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
155
    goto _OVER;
S
Shengliang Guan 已提交
156 157
  }

158
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
159
  if (pRow == NULL) goto _OVER;
160

S
Shengliang Guan 已提交
161
  SMnodeObj *pObj = sdbGetRowObj(pRow);
162
  if (pObj == NULL) goto _OVER;
S
Shengliang Guan 已提交
163 164

  int32_t dataPos = 0;
165 166 167 168
  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)
169 170 171

  terrno = 0;

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

179
  mTrace("mnode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
S
Shengliang Guan 已提交
180 181 182
  return pRow;
}

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

192
  pObj->state = TAOS_SYNC_STATE_ERROR;
S
Shengliang Guan 已提交
193 194 195
  return 0;
}

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

  return 0;
}

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

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

S
Shengliang Guan 已提交
215 216
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
217 218 219
    return false;
  }

S
Shengliang Guan 已提交
220
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
221
  return true;
S
Shengliang Guan 已提交
222 223
}

S
Shengliang Guan 已提交
224 225 226 227 228 229
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
  SSdb *pSdb = pMnode->pSdb;
  pEpSet->numOfEps = 0;

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

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
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 已提交
269
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
S
Shengliang Guan 已提交
270 271 272 273
  SSdb            *pSdb = pMnode->pSdb;
  void            *pIter = NULL;
  int32_t          numOfReplicas = 0;
  SDAlterMnodeReq  alterReq = {0};
S
Shengliang Guan 已提交
274
  SDCreateMnodeReq createReq = {0};
S
Shengliang Guan 已提交
275 276 277
  SEpSet           alterEpset = {0};
  SEpSet           createEpset = {0};

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

S
Shengliang Guan 已提交
283 284 285
    alterReq.replicas[numOfReplicas].id = pMObj->id;
    alterReq.replicas[numOfReplicas].port = pMObj->pDnode->port;
    memcpy(alterReq.replicas[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
286

S
Shengliang Guan 已提交
287 288 289 290 291 292 293
    alterEpset.eps[numOfReplicas].port = pMObj->pDnode->port;
    memcpy(alterEpset.eps[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    if (pMObj->state == TAOS_SYNC_STATE_LEADER) {
      alterEpset.inUse = numOfReplicas;
    }

    numOfReplicas++;
S
Shengliang Guan 已提交
294 295 296
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
297 298 299 300
  alterReq.replica = numOfReplicas + 1;
  alterReq.replicas[numOfReplicas].id = pDnode->id;
  alterReq.replicas[numOfReplicas].port = pDnode->port;
  memcpy(alterReq.replicas[numOfReplicas].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
301

S
Shengliang Guan 已提交
302 303 304
  alterEpset.numOfEps = numOfReplicas + 1;
  alterEpset.eps[numOfReplicas].port = pDnode->port;
  memcpy(alterEpset.eps[numOfReplicas].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
305

S
Shengliang Guan 已提交
306 307 308 309
  createReq.replica = 1;
  createReq.replicas[0].id = pDnode->id;
  createReq.replicas[0].port = pDnode->port;
  memcpy(createReq.replicas[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
310

S
Shengliang Guan 已提交
311 312 313
  createEpset.numOfEps = 1;
  createEpset.eps[0].port = pDnode->port;
  memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
314

S
Shengliang Guan 已提交
315
  {
316
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
wafwerar's avatar
wafwerar 已提交
317
    void   *pReq = taosMemoryMalloc(contLen);
318
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
319

S
Shengliang Guan 已提交
320
    STransAction action = {
321
        .epSet = createEpset,
S
Shengliang Guan 已提交
322 323
        .pCont = pReq,
        .contLen = contLen,
324 325
        .msgType = TDMT_DND_CREATE_MNODE,
        .acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED,
S
Shengliang Guan 已提交
326
    };
S
Shengliang Guan 已提交
327 328

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
329
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
330 331 332 333 334
      return -1;
    }
  }

  {
335
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq);
wafwerar's avatar
wafwerar 已提交
336
    void   *pReq = taosMemoryMalloc(contLen);
337
    tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
338

S
Shengliang Guan 已提交
339
    STransAction action = {
340
        .epSet = alterEpset,
S
Shengliang Guan 已提交
341 342
        .pCont = pReq,
        .contLen = contLen,
343 344
        .msgType = TDMT_DND_ALTER_MNODE,
        .acceptableCode = 0,
S
Shengliang Guan 已提交
345 346
    };

S
Shengliang Guan 已提交
347
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
348
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
349 350
      return -1;
    }
351 352 353 354 355
  }

  return 0;
}

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

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

S
Shengliang Guan 已提交
364
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_MNODE, pReq);
365
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
366

S
Shengliang Guan 已提交
367
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
368 369 370
  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;
371
  mndTransSetExecOneByOne(pTrans);
S
Shengliang Guan 已提交
372

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

375 376
  code = 0;

377
_OVER:
S
Shengliang Guan 已提交
378
  mndTransDrop(pTrans);
379
  return code;
S
Shengliang Guan 已提交
380 381
}

S
Shengliang Guan 已提交
382 383
static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
384 385 386 387 388 389
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateMnodeReq createReq = {0};

S
Shengliang Guan 已提交
390
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
391
    terrno = TSDB_CODE_INVALID_MSG;
392
    goto _OVER;
S
Shengliang Guan 已提交
393
  }
S
Shengliang Guan 已提交
394

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

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

S
Shengliang Guan 已提交
405
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
406 407
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
408
    goto _OVER;
S
Shengliang Guan 已提交
409 410
  }

S
Shengliang Guan 已提交
411
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
412 413
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
414
    goto _OVER;
S
Shengliang Guan 已提交
415
  }
S
Shengliang Guan 已提交
416

S
Shengliang Guan 已提交
417
  if (mndCheckNodeAuth(pUser)) {
418
    goto _OVER;
S
Shengliang Guan 已提交
419 420 421
  }

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

424
_OVER:
S
Shengliang Guan 已提交
425
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
426
    mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
427 428
  }

S
Shengliang Guan 已提交
429 430 431 432 433
  mndReleaseMnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
434 435
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
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 已提交
452
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
S
Shengliang Guan 已提交
453 454 455
  SSdb           *pSdb = pMnode->pSdb;
  void           *pIter = NULL;
  int32_t         numOfReplicas = 0;
S
Shengliang Guan 已提交
456
  SDAlterMnodeReq alterReq = {0};
S
Shengliang Guan 已提交
457 458 459 460
  SDDropMnodeReq  dropReq = {0};
  SEpSet          alterEpset = {0};
  SEpSet          dropEpSet = {0};

S
Shengliang Guan 已提交
461 462 463 464
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
465 466 467 468 469 470 471 472
    if (pMObj->id == pObj->id) {
      sdbRelease(pSdb, pMObj);
      continue;
    }

    alterReq.replicas[numOfReplicas].id = pMObj->id;
    alterReq.replicas[numOfReplicas].port = pMObj->pDnode->port;
    memcpy(alterReq.replicas[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
473

S
Shengliang Guan 已提交
474 475 476 477
    alterEpset.eps[numOfReplicas].port = pMObj->pDnode->port;
    memcpy(alterEpset.eps[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    if (pMObj->state == TAOS_SYNC_STATE_LEADER) {
      alterEpset.inUse = numOfReplicas;
S
Shengliang Guan 已提交
478
    }
479

S
Shengliang Guan 已提交
480
    numOfReplicas++;
S
Shengliang Guan 已提交
481 482 483
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
484
  alterReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
485
  alterEpset.numOfEps = numOfReplicas;
S
Shengliang Guan 已提交
486

S
Shengliang Guan 已提交
487 488 489 490
  dropReq.dnodeId = pDnode->id;
  dropEpSet.numOfEps = 1;
  dropEpSet.eps[0].port = pDnode->port;
  memcpy(dropEpSet.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
491

S
Shengliang Guan 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  {
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq);
    void   *pReq = taosMemoryMalloc(contLen);
    tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq);

    STransAction action = {
        .epSet = alterEpset,
        .pCont = pReq,
        .contLen = contLen,
        .msgType = TDMT_DND_ALTER_MNODE,
        .acceptableCode = 0,
    };

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      taosMemoryFree(pReq);
      return -1;
    }
S
Shengliang Guan 已提交
509 510 511
  }

  {
512
    int32_t contLen = tSerializeSCreateDropMQSBNodeReq(NULL, 0, &dropReq);
wafwerar's avatar
wafwerar 已提交
513
    void   *pReq = taosMemoryMalloc(contLen);
514
    tSerializeSCreateDropMQSBNodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
515

S
Shengliang Guan 已提交
516 517 518 519 520 521 522 523
    STransAction action = {
        .epSet = dropEpSet,
        .pCont = pReq,
        .contLen = contLen,
        .msgType = TDMT_DND_DROP_MNODE,
        .acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED,
    };

S
Shengliang Guan 已提交
524
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
525
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
526 527
      return -1;
    }
528 529 530 531 532
  }

  return 0;
}

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

S
Shengliang Guan 已提交
536
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_MNODE, pReq);
537
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
538

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

541 542 543 544
  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;
545
  mndTransSetExecOneByOne(pTrans);
S
Shengliang Guan 已提交
546

547 548
  code = 0;

549
_OVER:
S
Shengliang Guan 已提交
550
  mndTransDrop(pTrans);
551
  return code;
S
Shengliang Guan 已提交
552 553
}

S
Shengliang Guan 已提交
554 555
static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
556 557 558 559 560
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

S
Shengliang Guan 已提交
561
  if (tDeserializeSCreateDropMQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
562
    terrno = TSDB_CODE_INVALID_MSG;
563
    goto _OVER;
S
Shengliang Guan 已提交
564
  }
S
Shengliang Guan 已提交
565

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

S
Shengliang Guan 已提交
568
  if (dropReq.dnodeId <= 0) {
569 570
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
S
Shengliang Guan 已提交
571 572
  }

S
Shengliang Guan 已提交
573
  pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
574
  if (pObj == NULL) {
575 576 577
    goto _OVER;
  }

578
  if (pMnode->selfDnodeId == dropReq.dnodeId) {
579 580 581 582 583 584 585
    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 已提交
586 587
  }

S
Shengliang Guan 已提交
588
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
589 590
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
591
    goto _OVER;
S
Shengliang Guan 已提交
592 593
  }

S
Shengliang Guan 已提交
594
  if (mndCheckNodeAuth(pUser)) {
595
    goto _OVER;
S
Shengliang Guan 已提交
596
  }
S
Shengliang Guan 已提交
597

S
Shengliang Guan 已提交
598
  code = mndDropMnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
599
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
600

601
_OVER:
S
Shengliang Guan 已提交
602
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
603 604 605 606 607 608 609
    mError("mnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
  }

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

  return code;
S
Shengliang Guan 已提交
610 611
}

S
Shengliang Guan 已提交
612
static int32_t mndProcessCreateMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
613
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
614 615 616
  return 0;
}

S
Shengliang Guan 已提交
617
static int32_t mndProcessAlterMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
618
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
619 620
  return 0;
}
S
Shengliang Guan 已提交
621

S
Shengliang Guan 已提交
622
static int32_t mndProcessDropMnodeRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
623
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
624 625
  return 0;
}
S
Shengliang Guan 已提交
626

S
Shengliang Guan 已提交
627 628
static int32_t mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
629 630 631
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
632
  SMnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
633 634 635
  char      *pWrite;

  while (numOfRows < rows) {
S
Shengliang Guan 已提交
636
    pShow->pIter = sdbFetch(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj);
S
Shengliang Guan 已提交
637 638 639
    if (pShow->pIter == NULL) break;

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

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

646 647
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
648

649 650
    const char *roles = NULL;
    if (pObj->id == pMnode->selfDnodeId) {
651
      roles = syncStr(TAOS_SYNC_STATE_LEADER);
652 653
    } else {
      roles = syncStr(pObj->state);
654 655
    }
    char *b2 = taosMemoryCalloc(1, 12 + VARSTR_HEADER_SIZE);
656
    STR_WITH_MAXSIZE_TO_VARSTR(b2, roles, pShow->pMeta->pSchemas[cols].bytes);
657 658

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

661 662
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
663 664

    numOfRows++;
S
Shengliang Guan 已提交
665
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
666 667
  }

668
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
669 670 671 672 673 674 675 676

  return numOfRows;
}

static void mndCancelGetNextMnode(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}
S
Shengliang Guan 已提交
677 678

static int32_t mndProcessAlterMnodeReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
679
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  SDAlterMnodeReq alterReq = {0};

  if (tDeserializeSDCreateMnodeReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  SSyncCfg cfg = {.replicaNum = alterReq.replica, .myIndex = -1};
  for (int32_t i = 0; i < alterReq.replica; ++i) {
    SNodeInfo *pNode = &cfg.nodeInfo[i];
    tstrncpy(pNode->nodeFqdn, alterReq.replicas[i].fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = alterReq.replicas[i].port;
    if (alterReq.replicas[i].id == pMnode->selfDnodeId) cfg.myIndex = i;
  }

  if (cfg.myIndex == -1) {
    mError("failed to alter mnode since myindex is -1");
    return -1;
  } else {
    mInfo("start to alter mnode sync, replica:%d myindex:%d", cfg.replicaNum, cfg.myIndex);
    for (int32_t i = 0; i < alterReq.replica; ++i) {
      SNodeInfo *pNode = &cfg.nodeInfo[i];
      mInfo("index:%d, fqdn:%s port:%d", i, pNode->nodeFqdn, pNode->nodePort);
    }
  }

  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  pMgmt->standby = 0;
  int32_t code = syncReconfig(pMgmt->sync, &cfg);
  if (code != 0) {
    mError("failed to alter mnode sync since %s", terrstr());
    return code;
  } else {
    pMgmt->errCode = 0;
    tsem_wait(&pMgmt->syncSem);
    mInfo("alter mnode sync result:%s", tstrerror(pMgmt->errCode));
    terrno = pMgmt->errCode;
    return pMgmt->errCode;
  }
}