mndMnode.c 21.5 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

S
Shengliang Guan 已提交
24
#define TSDB_MNODE_VER_NUMBER   1
S
Shengliang Guan 已提交
25
#define TSDB_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 39
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  mndGetMnodeMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveMnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
40
static void     mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50 51

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

  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndGetMnodeMeta);
  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) {}

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

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

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

S
Shengliang Guan 已提交
94 95 96 97 98 99 100 101
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 已提交
102
    ESyncState lastRole = pObj->role;
S
Shengliang Guan 已提交
103 104 105 106 107
    if (pObj->id == 1) {
      pObj->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pObj->role = TAOS_SYNC_STATE_CANDIDATE;
    }
S
Shengliang Guan 已提交
108 109 110
    if (pObj->role != lastRole) {
      pObj->roleTime = taosGetTimestampMs();
    }
S
Shengliang Guan 已提交
111 112 113 114 115

    sdbRelease(pSdb, pObj);
  }
}

S
Shengliang Guan 已提交
116 117 118 119 120 121 122 123 124 125
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);

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

S
Shengliang Guan 已提交
130
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
131 132
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
133
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, TSDB_MNODE_VER_NUMBER, sizeof(SMnodeObj) + TSDB_MNODE_RESERVE_SIZE);
134
  if (pRaw == NULL) goto MNODE_ENCODE_OVER;
S
Shengliang Guan 已提交
135 136

  int32_t dataPos = 0;
137 138 139 140 141 142 143 144 145 146 147 148 149
  SDB_SET_INT32(pRaw, dataPos, pObj->id, MNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, MNODE_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, MNODE_ENCODE_OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_MNODE_RESERVE_SIZE, MNODE_ENCODE_OVER)

  terrno = 0;

MNODE_ENCODE_OVER:
  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 已提交
150

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

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
156 157
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
158 159 160
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

S
Shengliang Guan 已提交
161
  if (sver != TSDB_MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
162
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
163
    goto MNODE_DECODE_OVER;
S
Shengliang Guan 已提交
164 165
  }

166 167 168
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
  if (pRow == NULL) goto MNODE_DECODE_OVER;

S
Shengliang Guan 已提交
169
  SMnodeObj *pObj = sdbGetRowObj(pRow);
170
  if (pObj == NULL) goto MNODE_DECODE_OVER;
S
Shengliang Guan 已提交
171 172

  int32_t dataPos = 0;
173 174 175 176 177 178 179 180 181 182 183 184 185
  SDB_GET_INT32(pRaw, dataPos, &pObj->id, MNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, MNODE_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, MNODE_DECODE_OVER)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_MNODE_RESERVE_SIZE, MNODE_DECODE_OVER)

  terrno = 0;

MNODE_DECODE_OVER:
  if (terrno != 0) {
    mError("mnode:%d, failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
    tfree(pRow);
    return NULL;
  }
S
Shengliang Guan 已提交
186

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

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

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

S
Shengliang Guan 已提交
202
  mnodeResetMnode(pObj);
S
Shengliang Guan 已提交
203 204 205
  return 0;
}

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

  return 0;
}

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

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

S
Shengliang Guan 已提交
225 226
  SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
227 228 229
    return false;
  }

S
Shengliang Guan 已提交
230
  sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
231
  return true;
S
Shengliang Guan 已提交
232 233
}

S
Shengliang Guan 已提交
234 235 236 237 238 239 240
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
  SSdb *pSdb = pMnode->pSdb;

  pEpSet->numOfEps = 0;

  void *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
241 242
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
S
Shengliang Guan 已提交
243
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
244
    if (pObj->pDnode == NULL) break;
S
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246
    if (pObj->role == TAOS_SYNC_STATE_LEADER) {
S
Shengliang Guan 已提交
247 248 249
      pEpSet->inUse = pEpSet->numOfEps;
    }

S
Shengliang Guan 已提交
250
    addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
S
Shengliang Guan 已提交
251
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
252
  }
S
Shengliang Guan 已提交
253 254
}

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

S
Shengliang Guan 已提交
284
  SDCreateMnodeReq createReq = {0};
S
Shengliang Guan 已提交
285 286 287 288
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
289

S
Shengliang Guan 已提交
290
    SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
291 292
    pReplica->id = pMObj->id;
    pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
293 294
    memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
    numOfReplicas++;
295

S
Shengliang Guan 已提交
296 297 298
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
299
  SReplica *pReplica = &createReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
300 301
  pReplica->id = pDnode->id;
  pReplica->port = pDnode->port;
S
Shengliang Guan 已提交
302 303 304
  memcpy(pReplica->fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  numOfReplicas++;

S
Shengliang Guan 已提交
305
  createReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
306

S
Shengliang Guan 已提交
307 308 309 310 311 312 313
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    STransAction action = {0};

S
Shengliang Guan 已提交
314 315 316 317
    createReq.dnodeId = pMObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
    void   *pReq = malloc(contLen);
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
318 319

    action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
320
    action.pCont = pReq;
S
Shengliang Guan 已提交
321
    action.contLen = contLen;
H
Hongze Cheng 已提交
322
    action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
323
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
324 325

    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
326
      free(pReq);
S
Shengliang Guan 已提交
327 328 329 330 331 332 333 334 335 336 337 338
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
339 340 341 342
    createReq.dnodeId = pObj->id;
    int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq);
    void   *pReq = malloc(contLen);
    tSerializeSDCreateMnodeReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
343 344

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
345
    action.pCont = pReq;
S
Shengliang Guan 已提交
346
    action.contLen = contLen;
H
Hongze Cheng 已提交
347
    action.msgType = TDMT_DND_CREATE_MNODE;
S
shm  
Shengliang Guan 已提交
348
    action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
349
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
350
      free(pReq);
S
Shengliang Guan 已提交
351 352
      return -1;
    }
353 354 355 356 357
  }

  return 0;
}

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

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

S
Shengliang Guan 已提交
366
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_MNODE, &pReq->rpcMsg);
S
Shengliang Guan 已提交
367
  if (pTrans == NULL) goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
368

S
Shengliang Guan 已提交
369 370 371 372
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
  if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) goto CREATE_MNODE_OVER;
  if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeObj) != 0) goto CREATE_MNODE_OVER;
  if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
373

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

376 377 378
  code = 0;

CREATE_MNODE_OVER:
S
Shengliang Guan 已提交
379
  mndTransDrop(pTrans);
380
  return code;
S
Shengliang Guan 已提交
381 382
}

S
Shengliang Guan 已提交
383 384
static int32_t mndProcessCreateMnodeReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
385 386 387 388 389 390 391 392 393 394
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SUserObj        *pUser = NULL;
  SMCreateMnodeReq createReq = {0};

  if (tDeserializeSMCreateDropMnodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_MNODE_OVER;
  }
S
Shengliang Guan 已提交
395

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

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

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

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

S
Shengliang Guan 已提交
418
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
419 420 421 422 423 424 425 426 427
    goto CREATE_MNODE_OVER;
  }

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

CREATE_MNODE_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
428 429
  }

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

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

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

S
Shengliang Guan 已提交
458
  SDAlterMnodeReq alterReq = {0};
S
Shengliang Guan 已提交
459 460 461 462
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
463

S
Shengliang Guan 已提交
464
    if (pMObj->id != pObj->id) {
S
Shengliang Guan 已提交
465
      SReplica *pReplica = &alterReq.replicas[numOfReplicas];
S
Shengliang Guan 已提交
466 467
      pReplica->id = pMObj->id;
      pReplica->port = pMObj->pDnode->port;
S
Shengliang Guan 已提交
468 469 470
      memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfReplicas++;
    }
471

S
Shengliang Guan 已提交
472 473 474
    sdbRelease(pSdb, pMObj);
  }

S
Shengliang Guan 已提交
475
  alterReq.replica = numOfReplicas;
S
Shengliang Guan 已提交
476

S
Shengliang Guan 已提交
477 478 479 480 481 482 483
  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 已提交
484 485 486 487
      alterReq.dnodeId = pMObj->id;
      int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq);
      void   *pReq = malloc(contLen);
      tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
488 489

      action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
490
      action.pCont = pReq;
S
Shengliang Guan 已提交
491
      action.contLen = contLen;
H
Hongze Cheng 已提交
492
      action.msgType = TDMT_DND_ALTER_MNODE;
S
shm  
Shengliang Guan 已提交
493
      action.acceptableCode = TSDB_CODE_NODE_ALREADY_DEPLOYED;
S
Shengliang Guan 已提交
494 495

      if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
496
        free(pReq);
S
Shengliang Guan 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
    }

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
510 511 512 513 514
    SDDropMnodeReq dropReq = {0};
    dropReq.dnodeId = pObj->id;
    int32_t contLen = tSerializeSMCreateDropMnodeReq(NULL, 0, &dropReq);
    void   *pReq = malloc(contLen);
    tSerializeSMCreateDropMnodeReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
515 516

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
517
    action.pCont = pReq;
S
Shengliang Guan 已提交
518
    action.contLen = contLen;
H
Hongze Cheng 已提交
519
    action.msgType = TDMT_DND_DROP_MNODE;
S
shm  
Shengliang Guan 已提交
520
    action.acceptableCode = TSDB_CODE_NODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
521
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
522
      free(pReq);
S
Shengliang Guan 已提交
523 524
      return -1;
    }
525 526 527 528 529
  }

  return 0;
}

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

S
Shengliang Guan 已提交
533
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_DROP_MNODE, &pReq->rpcMsg);
S
Shengliang Guan 已提交
534
  if (pTrans == NULL) goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
535

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

S
Shengliang Guan 已提交
538 539 540 541
  if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) goto DROP_MNODE_OVER;
  if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) goto DROP_MNODE_OVER;
  if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj) != 0) goto DROP_MNODE_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
542

543 544 545
  code = 0;

DROP_MNODE_OVER:
S
Shengliang Guan 已提交
546
  mndTransDrop(pTrans);
547
  return code;
S
Shengliang Guan 已提交
548 549
}

S
Shengliang Guan 已提交
550 551
static int32_t mndProcessDropMnodeReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
552 553 554 555 556 557 558 559 560
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

  if (tDeserializeSMCreateDropMnodeReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_MNODE_OVER;
  }
S
Shengliang Guan 已提交
561

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

S
Shengliang Guan 已提交
564
  if (dropReq.dnodeId <= 0) {
S
Shengliang Guan 已提交
565
    terrno = TSDB_CODE_SDB_APP_ERROR;
S
Shengliang Guan 已提交
566
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
567 568
  }

S
Shengliang Guan 已提交
569
  pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
570
  if (pObj == NULL) {
S
Shengliang Guan 已提交
571
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
572 573
  }

S
Shengliang Guan 已提交
574 575 576 577 578 579
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto DROP_MNODE_OVER;
  }

S
Shengliang Guan 已提交
580
  if (mndCheckNodeAuth(pUser)) {
S
Shengliang Guan 已提交
581
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
582
  }
S
Shengliang Guan 已提交
583

S
Shengliang Guan 已提交
584 585 586 587 588 589 590 591 592 593 594 595
  code = mndDropMnode(pMnode, pReq, pObj);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

DROP_MNODE_OVER:
  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 已提交
596 597
}

S
Shengliang Guan 已提交
598
static int32_t mndProcessCreateMnodeRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
599
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
600 601 602
  return 0;
}

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

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

S
Shengliang Guan 已提交
613 614
static int32_t mndGetMnodeMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
615 616 617
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
618
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
619 620 621 622

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
S
Shengliang Guan 已提交
623
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
624 625 626 627
  cols++;

  pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
628
  strcpy(pSchema[cols].name, "endpoint");
S
Shengliang Guan 已提交
629
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
630 631 632 633 634
  cols++;

  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "role");
S
Shengliang Guan 已提交
635
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
636 637 638 639
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
640
  strcpy(pSchema[cols].name, "create_time");
S
Shengliang Guan 已提交
641
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
642 643 644 645
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
646
  strcpy(pSchema[cols].name, "role_time");
S
Shengliang Guan 已提交
647
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
648 649
  cols++;

S
Shengliang Guan 已提交
650
  pMeta->numOfColumns = cols;
S
Shengliang Guan 已提交
651 652 653 654 655 656 657 658 659
  pShow->numOfColumns = cols;

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

  pShow->numOfRows = sdbGetSize(pSdb, SDB_MNODE);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
660
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
661

S
Shengliang Guan 已提交
662
  mndUpdateMnodeRole(pMnode);
S
Shengliang Guan 已提交
663 664 665
  return 0;
}

S
Shengliang Guan 已提交
666 667
static int32_t mndRetrieveMnodes(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode    *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
668 669 670
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
671
  SMnodeObj *pObj = NULL;
S
Shengliang Guan 已提交
672 673 674
  char      *pWrite;

  while (numOfRows < rows) {
S
Shengliang Guan 已提交
675
    pShow->pIter = sdbFetch(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj);
S
Shengliang Guan 已提交
676 677 678 679 680
    if (pShow->pIter == NULL) break;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
681
    *(int16_t *)pWrite = pObj->id;
S
Shengliang Guan 已提交
682 683 684
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
685
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pObj->pDnode->ep, pShow->bytes[cols]);
S
Shengliang Guan 已提交
686 687 688 689

    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
690
    char *roles = mndGetRoleStr(pObj->role);
S
Shengliang Guan 已提交
691 692 693 694
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, roles, pShow->bytes[cols]);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
695
    *(int64_t *)pWrite = pObj->createdTime;
S
Shengliang Guan 已提交
696 697 698
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
699
    *(int64_t *)pWrite = pObj->roleTime;
S
Shengliang Guan 已提交
700 701 702
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
703
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
704 705
  }

S
Shengliang Guan 已提交
706
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
707 708 709 710 711 712 713 714 715
  pShow->numOfReads += numOfRows;

  return numOfRows;
}

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