mndMnode.c 21.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 18 19
#include "mndMnode.h"
#include "mndDnode.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
20
#include "mndTrans.h"
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22 23
#define TSDB_MNODE_VER_NUMBER 1
#define TSDB_MNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
24

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

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

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

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

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

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 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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;

    if (pObj->id == 1) {
      pObj->role = TAOS_SYNC_STATE_LEADER;
    } else {
      pObj->role = TAOS_SYNC_STATE_CANDIDATE;
    }

    sdbRelease(pSdb, pObj);
  }
}

S
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118 119
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);

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

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

S
Shengliang Guan 已提交
127
  SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, TSDB_MNODE_VER_NUMBER, sizeof(SMnodeObj) + TSDB_MNODE_RESERVE_SIZE);
128
  if (pRaw == NULL) goto MNODE_ENCODE_OVER;
S
Shengliang Guan 已提交
129 130

  int32_t dataPos = 0;
131 132 133 134 135 136 137 138 139 140 141 142 143
  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 已提交
144

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

static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
150 151
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

S
Shengliang Guan 已提交
155
  if (sver != TSDB_MNODE_VER_NUMBER) {
S
Shengliang Guan 已提交
156
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
157
    goto MNODE_DECODE_OVER;
S
Shengliang Guan 已提交
158 159
  }

160 161 162
  SSdbRow *pRow = sdbAllocRow(sizeof(SMnodeObj));
  if (pRow == NULL) goto MNODE_DECODE_OVER;

S
Shengliang Guan 已提交
163
  SMnodeObj *pObj = sdbGetRowObj(pRow);
164
  if (pObj == NULL) goto MNODE_DECODE_OVER;
S
Shengliang Guan 已提交
165 166

  int32_t dataPos = 0;
167 168 169 170 171 172 173 174 175 176 177 178 179
  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 已提交
180

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

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

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

S
Shengliang Guan 已提交
196
  mnodeResetMnode(pObj);
S
Shengliang Guan 已提交
197 198 199
  return 0;
}

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

  return 0;
}

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

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

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

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

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

  pEpSet->numOfEps = 0;

  void *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
235 236
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
S
Shengliang Guan 已提交
237
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
238
    if (pObj->pDnode == NULL) break;
S
Shengliang Guan 已提交
239

S
Shengliang Guan 已提交
240 241 242
    pEpSet->port[pEpSet->numOfEps] = htons(pObj->pDnode->port);
    memcpy(pEpSet->fqdn[pEpSet->numOfEps], pObj->pDnode->fqdn, TSDB_FQDN_LEN);
    if (pObj->role == TAOS_SYNC_STATE_LEADER) {
S
Shengliang Guan 已提交
243 244 245 246
      pEpSet->inUse = pEpSet->numOfEps;
    }

    pEpSet->numOfEps++;
S
Shengliang Guan 已提交
247
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
248
  }
S
Shengliang Guan 已提交
249 250
}

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

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

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

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

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

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

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

    STransAction action = {0};

S
Shengliang Guan 已提交
310 311
    SDAlterMnodeReq *pReq = malloc(sizeof(SDAlterMnodeReq));
    if (pReq == NULL) {
S
Shengliang Guan 已提交
312 313 314 315
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pMObj);
      return -1;
    }
S
Shengliang Guan 已提交
316
    memcpy(pReq, &createReq, sizeof(SDAlterMnodeReq));
S
Shengliang Guan 已提交
317

S
Shengliang Guan 已提交
318
    pReq->dnodeId = htonl(pMObj->id);
S
Shengliang Guan 已提交
319
    action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
320
    action.pCont = pReq;
S
Shengliang Guan 已提交
321
    action.contLen = sizeof(SDAlterMnodeReq);
H
Hongze Cheng 已提交
322
    action.msgType = TDMT_DND_ALTER_MNODE;
S
Shengliang Guan 已提交
323 324

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

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
338 339 340 341
    SDCreateMnodeReq *pReq = malloc(sizeof(SDCreateMnodeReq));
    if (pReq == NULL) return -1;
    memcpy(pReq, &createReq, sizeof(SDAlterMnodeReq));
    pReq->dnodeId = htonl(pObj->id);
S
Shengliang Guan 已提交
342 343

    action.epSet = mndGetDnodeEpset(pDnode);
S
Shengliang Guan 已提交
344
    action.pCont = pReq;
S
Shengliang Guan 已提交
345
    action.contLen = sizeof(SDCreateMnodeReq);
H
Hongze Cheng 已提交
346
    action.msgType = TDMT_DND_CREATE_MNODE;
S
Shengliang Guan 已提交
347
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
348
      free(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, SMnodeMsg *pReq, SDnodeObj *pDnode, SMCreateMnodeReq *pCreate) {
S
Shengliang Guan 已提交
357
  SMnodeObj mnodeObj = {0};
S
Shengliang Guan 已提交
358
  mnodeObj.id = pDnode->id;
S
Shengliang Guan 已提交
359 360 361
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;

362
  int32_t code = -1;
S
Shengliang Guan 已提交
363
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pReq->rpcMsg);
S
Shengliang Guan 已提交
364
  if (pTrans == NULL) {
365 366
    mError("mnode:%d, failed to create since %s", pCreate->dnodeId, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
367
  }
368
  mDebug("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
S
Shengliang Guan 已提交
369

370 371 372
  if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) {
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
373 374
  }

375 376 377
  if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeObj) != 0) {
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
378 379
  }

S
Shengliang Guan 已提交
380
  if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) {
381 382
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
383 384
  }

S
Shengliang Guan 已提交
385
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
386
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
387
    goto CREATE_MNODE_OVER;
S
Shengliang Guan 已提交
388 389
  }

390 391 392
  code = 0;

CREATE_MNODE_OVER:
S
Shengliang Guan 已提交
393
  mndTransDrop(pTrans);
394
  return code;
S
Shengliang Guan 已提交
395 396
}

S
Shengliang Guan 已提交
397 398 399
static int32_t mndProcessCreateMnodeReq(SMnodeMsg *pReq) {
  SMnode           *pMnode = pReq->pMnode;
  SMCreateMnodeReq *pCreate = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
400 401 402 403 404

  pCreate->dnodeId = htonl(pCreate->dnodeId);

  mDebug("mnode:%d, start to create", pCreate->dnodeId);

S
Shengliang Guan 已提交
405 406
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pCreate->dnodeId);
  if (pObj != NULL) {
S
Shengliang Guan 已提交
407
    mndReleaseMnode(pMnode, pObj);
S
Shengliang Guan 已提交
408
    mError("mnode:%d, mnode already exist", pObj->id);
S
Shengliang Guan 已提交
409 410 411 412
    terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
413 414
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, pCreate->dnodeId);
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
415
    mError("mnode:%d, dnode not exist", pCreate->dnodeId);
S
Shengliang Guan 已提交
416 417 418 419
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
420
  int32_t code = mndCreateMnode(pMnode, pReq, pDnode, pCreate);
S
Shengliang Guan 已提交
421
  mndReleaseDnode(pMnode, pDnode);
S
Shengliang Guan 已提交
422 423 424 425 426 427 428 429 430

  if (code != 0) {
    mError("mnode:%d, failed to create since %s", pCreate->dnodeId, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

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

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

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

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

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

S
Shengliang Guan 已提交
471 472 473 474 475 476 477
  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 已提交
478 479
      SDAlterMnodeReq *pReq = malloc(sizeof(SDAlterMnodeReq));
      if (pReq == NULL) {
S
Shengliang Guan 已提交
480 481 482 483
        sdbCancelFetch(pSdb, pIter);
        sdbRelease(pSdb, pMObj);
        return -1;
      }
S
Shengliang Guan 已提交
484
      memcpy(pReq, &alterReq, sizeof(SDAlterMnodeReq));
S
Shengliang Guan 已提交
485

S
Shengliang Guan 已提交
486
      pReq->dnodeId = htonl(pMObj->id);
S
Shengliang Guan 已提交
487
      action.epSet = mndGetDnodeEpset(pMObj->pDnode);
S
Shengliang Guan 已提交
488
      action.pCont = pReq;
S
Shengliang Guan 已提交
489
      action.contLen = sizeof(SDAlterMnodeReq);
H
Hongze Cheng 已提交
490
      action.msgType = TDMT_DND_ALTER_MNODE;
S
Shengliang Guan 已提交
491 492

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

    sdbRelease(pSdb, pMObj);
  }

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

S
Shengliang Guan 已提交
507 508
    SDDropMnodeReq *pReq = malloc(sizeof(SDDropMnodeReq));
    if (pReq == NULL) {
S
Shengliang Guan 已提交
509 510 511
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
512
    pReq->dnodeId = htonl(pObj->id);
S
Shengliang Guan 已提交
513 514

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

  return 0;
}

S
Shengliang Guan 已提交
527
static int32_t mndDropMnode(SMnode *pMnode, SMnodeMsg *pReq, SMnodeObj *pObj) {
528
  int32_t code = -1;
S
Shengliang Guan 已提交
529
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pReq->rpcMsg);
S
Shengliang Guan 已提交
530
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
531
    mError("mnode:%d, failed to drop since %s", pObj->id, terrstr());
532
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
533 534
  }

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

S
Shengliang Guan 已提交
537
  if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) {
538 539
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
540 541
  }

S
Shengliang Guan 已提交
542
  if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) {
543 544
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
545 546
  }

S
Shengliang Guan 已提交
547
  if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj) != 0) {
548 549
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
550 551
  }

S
Shengliang Guan 已提交
552
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
553
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
554
    goto DROP_MNODE_OVER;
S
Shengliang Guan 已提交
555 556
  }

557 558 559
  code = 0;

DROP_MNODE_OVER:
S
Shengliang Guan 已提交
560
  mndTransDrop(pTrans);
561
  return code;
S
Shengliang Guan 已提交
562 563
}

S
Shengliang Guan 已提交
564 565 566
static int32_t mndProcessDropMnodeReq(SMnodeMsg *pReq) {
  SMnode         *pMnode = pReq->pMnode;
  SMDropMnodeReq *pDrop = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
567 568 569 570 571 572 573 574 575 576
  pDrop->dnodeId = htonl(pDrop->dnodeId);

  mDebug("mnode:%d, start to drop", pDrop->dnodeId);

  if (pDrop->dnodeId <= 0) {
    terrno = TSDB_CODE_SDB_APP_ERROR;
    mError("mnode:%d, failed to drop since %s", pDrop->dnodeId, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
577 578
  SMnodeObj *pObj = mndAcquireMnode(pMnode, pDrop->dnodeId);
  if (pObj == NULL) {
S
Shengliang Guan 已提交
579 580 581 582 583
    mError("mnode:%d, not exist", pDrop->dnodeId);
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
584
  int32_t code = mndDropMnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
585 586 587 588 589

  if (code != 0) {
    mError("mnode:%d, failed to drop since %s", pMnode->dnodeId, terrstr());
    return -1;
  }
S
Shengliang Guan 已提交
590

S
Shengliang Guan 已提交
591
  sdbRelease(pMnode->pSdb, pObj);
S
Shengliang Guan 已提交
592 593 594
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

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

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

S
Shengliang Guan 已提交
605 606
static int32_t mndProcessDropMnodeRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
S
Shengliang Guan 已提交
607 608
  return 0;
}
S
Shengliang Guan 已提交
609

S
Shengliang Guan 已提交
610 611
static int32_t mndGetMnodeMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
612 613 614
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
615
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
616 617 618 619

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "id");
H
Haojun Liao 已提交
620
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
621 622 623 624
  cols++;

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

  pShow->bytes[cols] = 12 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "role");
H
Haojun Liao 已提交
632
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
633 634 635 636
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
637 638
  strcpy(pSchema[cols].name, "role_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
639 640 641 642
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
643 644
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
645 646
  cols++;

H
Haojun Liao 已提交
647
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
648 649 650 651 652 653 654 655 656
  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];
657
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
658

S
Shengliang Guan 已提交
659
  mndUpdateMnodeRole(pMnode);
S
Shengliang Guan 已提交
660 661 662
  return 0;
}

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

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

    cols = 0;

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

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

    cols++;

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

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
692
    *(int64_t *)pWrite = pObj->roleTime;
S
Shengliang Guan 已提交
693 694 695
    cols++;

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

    numOfRows++;
S
Shengliang Guan 已提交
700
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
701 702
  }

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

  return numOfRows;
}

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