mndMnode.c 33.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"
18
#include "mndCluster.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
H
Hongze Cheng 已提交
20
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
21
#include "mndShow.h"
22
#include "mndSync.h"
S
Shengliang Guan 已提交
23
#include "mndTrans.h"
H
Haojun Liao 已提交
24
#include "tmisce.h"
S
Shengliang Guan 已提交
25

C
cadem 已提交
26
#define MNODE_VER_NUMBER   2
27
#define MNODE_RESERVE_SIZE 64
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29
static int32_t  mndCreateDefaultMnode(SMnode *pMnode);
S
Shengliang Guan 已提交
30
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj);
S
Shengliang Guan 已提交
31
static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
32 33
static int32_t  mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj);
static int32_t  mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj);
S
Shengliang Guan 已提交
34
static int32_t  mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOld, SMnodeObj *pNew);
S
Shengliang Guan 已提交
35
static int32_t  mndProcessCreateMnodeReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
36
static int32_t  mndProcessAlterMnodeReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
37 38
static int32_t  mndProcessDropMnodeReq(SRpcMsg *pReq);
static int32_t  mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
39
static void     mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
40
static void     mndReloadSyncConfig(SMnode *pMnode);
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);
55
  mndSetMsgHandle(pMnode, TDMT_DND_CREATE_MNODE_RSP, mndTransProcessRsp);
C
cadem 已提交
56
  mndSetMsgHandle(pMnode, TDMT_DND_ALTER_MNODE_TYPE_RSP, mndTransProcessRsp);
S
Shengliang Guan 已提交
57
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_MNODE, mndProcessAlterMnodeReq);
58
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_MNODE_RSP, mndTransProcessRsp);
H
Hongze Cheng 已提交
59
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_MNODE, mndProcessDropMnodeReq);
60
  mndSetMsgHandle(pMnode, TDMT_DND_DROP_MNODE_RSP, mndTransProcessRsp);
S
Shengliang Guan 已提交
61 62 63

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

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

void mndCleanupMnode(SMnode *pMnode) {}

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

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

S
Shengliang Guan 已提交
83 84 85 86 87 88 89 90
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;
S
Shengliang Guan 已提交
91
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
92

93
  mInfo("mnode:%d, will be created when deploying, raw:%p", mnodeObj.id, pRaw);
94

95
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL, "create-mnode");
96
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
97
    sdbFreeRaw(pRaw);
98 99 100
    mError("mnode:%d, failed to create since %s", mnodeObj.id, terrstr());
    return -1;
  }
101
  mInfo("trans:%d, used to create mnode:%d", pTrans->id, mnodeObj.id);
102 103 104 105 106 107

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
108
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
109 110 111 112 113 114 115 116 117

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

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
118 119
}

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

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

  int32_t dataPos = 0;
127 128 129
  SDB_SET_INT32(pRaw, dataPos, pObj->id, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, _OVER)
C
cadem 已提交
130 131
  SDB_SET_INT32(pRaw, dataPos, pObj->role, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pObj->lastIndex, _OVER)
132
  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
  terrno = TSDB_CODE_OUT_OF_MEMORY;
149 150
  SSdbRow   *pRow = NULL;
  SMnodeObj *pObj = NULL;
151

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

C
cadem 已提交
155
  if (sver != 1 && sver != 2) {
S
Shengliang Guan 已提交
156
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
157
    goto _OVER;
S
Shengliang Guan 已提交
158 159
  }

160
  pRow = sdbAllocRow(sizeof(SMnodeObj));
161
  if (pRow == NULL) goto _OVER;
162

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

  int32_t dataPos = 0;
167 168 169
  SDB_GET_INT32(pRaw, dataPos, &pObj->id, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, _OVER)
C
cadem 已提交
170 171 172 173
  if(sver >=2){
    SDB_GET_INT32(pRaw, dataPos, &pObj->role, _OVER)
    SDB_GET_INT64(pRaw, dataPos, &pObj->lastIndex, _OVER)
  }
174
  SDB_GET_RESERVE(pRaw, dataPos, MNODE_RESERVE_SIZE, _OVER)
175 176 177

  terrno = 0;

178
_OVER:
179
  if (terrno != 0) {
180
    mError("mnode:%d, failed to decode from raw:%p since %s", pObj == NULL ? 0 : pObj->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
181
    taosMemoryFreeClear(pRow);
182 183
    return NULL;
  }
S
Shengliang Guan 已提交
184

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

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

S
Shengliang Guan 已提交
197
  pObj->syncState = TAOS_SYNC_STATE_OFFLINE;
198
  mndReloadSyncConfig(pSdb->pMnode);
S
Shengliang Guan 已提交
199 200 201
  return 0;
}

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

  return 0;
}

S
Shengliang Guan 已提交
212
static int32_t mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOld, SMnodeObj *pNew) {
S
Shengliang Guan 已提交
213
  mTrace("mnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
C
cadem 已提交
214
  pOld->role = pNew->role;
S
Shengliang Guan 已提交
215
  pOld->updateTime = pNew->updateTime;
C
cadem 已提交
216
  pOld->lastIndex = pNew->lastIndex;
217 218
  mndReloadSyncConfig(pSdb->pMnode);

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
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
235 236
  SSdb   *pSdb = pMnode->pSdb;
  int32_t totalMnodes = sdbGetSize(pSdb, SDB_MNODE);
S
Shengliang Guan 已提交
237 238 239 240
  if (totalMnodes == 0) {
    syncGetRetryEpSet(pMnode->syncMgmt.sync, pEpSet);
    return;
  }
S
Shengliang Guan 已提交
241

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

    if (pObj->id == pMnode->selfDnodeId) {
249
      if (mndIsLeader(pMnode)) {
250
        pEpSet->inUse = pEpSet->numOfEps;
251 252
      } else {
        pEpSet->inUse = (pEpSet->numOfEps + 1) % totalMnodes;
253
      }
S
Shengliang Guan 已提交
254
    }
S
Shengliang Guan 已提交
255 256 257
    if (pObj->pDnode != NULL) {
      addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
    }
258
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
259
  }
260 261 262 263

  if (pEpSet->numOfEps == 0) {
    syncGetRetryEpSet(pMnode->syncMgmt.sync, pEpSet);
  }
264 265 266 267

  if (pEpSet->inUse >= pEpSet->numOfEps) {
    pEpSet->inUse = 0;
  }
S
Shengliang Guan 已提交
268 269
}

270 271 272 273 274 275 276 277
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;
}

C
cadem 已提交
278 279 280 281 282 283 284 285
int32_t mndSetRestoreCreateMnodeRedoLogs(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_READY) != 0) return -1;
  return 0;
}

286 287 288 289 290 291 292 293
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;
}

C
cadem 已提交
294
int32_t mndSetCreateMnodeCommitLogs(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj) {
295 296 297 298 299 300 301
  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;
}

302 303 304 305 306 307 308 309 310 311
static int32_t mndBuildCreateMnodeRedoAction(STrans *pTrans, SDCreateMnodeReq *pCreateReq, SEpSet *pCreateEpSet) {
  int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, pCreateReq);
  void   *pReq = taosMemoryMalloc(contLen);
  tSerializeSDCreateMnodeReq(pReq, contLen, pCreateReq);

  STransAction action = {
      .epSet = *pCreateEpSet,
      .pCont = pReq,
      .contLen = contLen,
      .msgType = TDMT_DND_CREATE_MNODE,
312
      .acceptableCode = TSDB_CODE_MNODE_ALREADY_DEPLOYED,
313 314 315 316 317 318 319 320 321
  };

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }
  return 0;
}

S
Shungang Li 已提交
322
static int32_t mndBuildAlterMnodeTypeRedoAction(STrans *pTrans,
C
cadem 已提交
323 324 325 326 327 328 329 330 331 332
                    SDAlterMnodeTypeReq *pAlterMnodeTypeReq, SEpSet *pAlterMnodeTypeEpSet) {
  int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, pAlterMnodeTypeReq);
  void   *pReq = taosMemoryMalloc(contLen);
  tSerializeSDCreateMnodeReq(pReq, contLen, pAlterMnodeTypeReq);

  STransAction action = {
      .epSet = *pAlterMnodeTypeEpSet,
      .pCont = pReq,
      .contLen = contLen,
      .msgType = TDMT_DND_ALTER_MNODE_TYPE,
C
cadem 已提交
333 334
      .retryCode = TSDB_CODE_MNODE_NOT_CATCH_UP,
      .acceptableCode = TSDB_CODE_MNODE_ALREADY_IS_VOTER,
C
cadem 已提交
335 336 337 338 339 340 341 342 343
  };

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }
  return 0;
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static int32_t mndBuildAlterMnodeRedoAction(STrans *pTrans, SDCreateMnodeReq *pAlterReq, SEpSet *pAlterEpSet) {
  int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, pAlterReq);
  void   *pReq = taosMemoryMalloc(contLen);
  tSerializeSDCreateMnodeReq(pReq, contLen, pAlterReq);

  STransAction action = {
      .epSet = *pAlterEpSet,
      .pCont = pReq,
      .contLen = contLen,
      .msgType = TDMT_MND_ALTER_MNODE,
      .acceptableCode = 0,
  };

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }

  return 0;
}

static int32_t mndBuildDropMnodeRedoAction(STrans *pTrans, SDDropMnodeReq *pDropReq, SEpSet *pDroprEpSet) {
S
Shengliang Guan 已提交
366
  int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, pDropReq);
367
  void   *pReq = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
368
  tSerializeSCreateDropMQSNodeReq(pReq, contLen, pDropReq);
369 370 371 372 373 374

  STransAction action = {
      .epSet = *pDroprEpSet,
      .pCont = pReq,
      .contLen = contLen,
      .msgType = TDMT_DND_DROP_MNODE,
375
      .acceptableCode = TSDB_CODE_MNODE_NOT_DEPLOYED,
376 377 378 379 380 381 382 383 384
  };

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }
  return 0;
}

S
Shengliang Guan 已提交
385
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
S
Shengliang Guan 已提交
386 387 388
  SSdb            *pSdb = pMnode->pSdb;
  void            *pIter = NULL;
  int32_t          numOfReplicas = 0;
C
cadem 已提交
389
  int32_t          numOfLearnerReplicas = 0;
S
Shengliang Guan 已提交
390
  SDCreateMnodeReq createReq = {0};
S
Shengliang Guan 已提交
391 392
  SEpSet           createEpset = {0};

S
Shengliang Guan 已提交
393 394 395 396
  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;
397

C
cadem 已提交
398 399 400 401 402 403 404 405 406 407 408 409
    if(pMObj->role == TAOS_SYNC_ROLE_VOTER){
      createReq.replicas[numOfReplicas].id = pMObj->id;
      createReq.replicas[numOfReplicas].port = pMObj->pDnode->port;
      memcpy(createReq.replicas[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfReplicas++;
    }
    else{
      createReq.learnerReplicas[numOfLearnerReplicas].id = pMObj->id;
      createReq.learnerReplicas[numOfLearnerReplicas].port = pMObj->pDnode->port;
      memcpy(createReq.learnerReplicas[numOfLearnerReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      numOfLearnerReplicas++;
    }
S
Shengliang Guan 已提交
410

S
Shengliang Guan 已提交
411 412 413
    sdbRelease(pSdb, pMObj);
  }

C
cadem 已提交
414 415 416 417 418 419 420
  createReq.replica = numOfReplicas;
  createReq.learnerReplica = numOfLearnerReplicas + 1;
  createReq.learnerReplicas[numOfLearnerReplicas].id = pDnode->id;
  createReq.learnerReplicas[numOfLearnerReplicas].port = pDnode->port;
  memcpy(createReq.learnerReplicas[numOfLearnerReplicas].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);

  createReq.lastIndex = pObj->lastIndex;
S
Shengliang Guan 已提交
421

422
  createEpset.inUse = 0;
S
Shengliang Guan 已提交
423 424 425
  createEpset.numOfEps = 1;
  createEpset.eps[0].port = pDnode->port;
  memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
426

427
  if (mndBuildCreateMnodeRedoAction(pTrans, &createReq, &createEpset) != 0) return -1;
428 429 430 431

  return 0;
}

C
cadem 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
int32_t mndSetRestoreCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb            *pSdb = pMnode->pSdb;
  void            *pIter = NULL;
  SDCreateMnodeReq createReq = {0};
  SEpSet           createEpset = {0};

  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    if(pMObj->id == pDnode->id) {
      sdbRelease(pSdb, pMObj);
      continue;
    }

    if(pMObj->role == TAOS_SYNC_ROLE_VOTER){
      createReq.replicas[createReq.replica].id = pMObj->id;
      createReq.replicas[createReq.replica].port = pMObj->pDnode->port;
      memcpy(createReq.replicas[createReq.replica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      createReq.replica++;
    }
    else{
      createReq.learnerReplicas[createReq.learnerReplica].id = pMObj->id;
      createReq.learnerReplicas[createReq.learnerReplica].port = pMObj->pDnode->port;
      memcpy(createReq.learnerReplicas[createReq.learnerReplica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      createReq.learnerReplica++;
    }

    sdbRelease(pSdb, pMObj);
  }

  createReq.learnerReplicas[createReq.learnerReplica].id = pDnode->id;
  createReq.learnerReplicas[createReq.learnerReplica].port = pDnode->port;
  memcpy(createReq.learnerReplicas[createReq.learnerReplica].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  createReq.learnerReplica++;

  createReq.lastIndex = pObj->lastIndex;

  createEpset.inUse = 0;
  createEpset.numOfEps = 1;
  createEpset.eps[0].port = pDnode->port;
  memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);

  if (mndBuildCreateMnodeRedoAction(pTrans, &createReq, &createEpset) != 0) return -1;

  return 0;
}

C
cadem 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static int32_t mndSetAlterMnodeTypeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb            *pSdb = pMnode->pSdb;
  void            *pIter = NULL;
  SDAlterMnodeTypeReq  alterReq = {0};
  SEpSet           createEpset = {0};

  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    if(pMObj->role == TAOS_SYNC_ROLE_VOTER){
      alterReq.replicas[alterReq.replica].id = pMObj->id;
      alterReq.replicas[alterReq.replica].port = pMObj->pDnode->port;
      memcpy(alterReq.replicas[alterReq.replica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      alterReq.replica++;
    }
    else{
      alterReq.learnerReplicas[alterReq.learnerReplica].id = pMObj->id;
      alterReq.learnerReplicas[alterReq.learnerReplica].port = pMObj->pDnode->port;
      memcpy(alterReq.learnerReplicas[alterReq.learnerReplica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      alterReq.learnerReplica++;
    }

    sdbRelease(pSdb, pMObj);
  }

  alterReq.replicas[alterReq.replica].id = pDnode->id;
C
cadem 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  alterReq.replicas[alterReq.replica].port = pDnode->port;
  memcpy(alterReq.replicas[alterReq.replica].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  alterReq.replica++;

  alterReq.lastIndex = pObj->lastIndex;

  createEpset.inUse = 0;
  createEpset.numOfEps = 1;
  createEpset.eps[0].port = pDnode->port;
  memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);

  if (mndBuildAlterMnodeTypeRedoAction(pTrans, &alterReq, &createEpset) != 0) return -1;

  return 0;
}

int32_t mndSetRestoreAlterMnodeTypeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
  SSdb            *pSdb = pMnode->pSdb;
  void            *pIter = NULL;
  SDAlterMnodeTypeReq  alterReq = {0};
  SEpSet           createEpset = {0};

  while (1) {
    SMnodeObj *pMObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
    if (pIter == NULL) break;

    if(pMObj->id == pDnode->id) {
      sdbRelease(pSdb, pMObj);
      continue;
    }

    if(pMObj->role == TAOS_SYNC_ROLE_VOTER){
      alterReq.replicas[alterReq.replica].id = pMObj->id;
      alterReq.replicas[alterReq.replica].port = pMObj->pDnode->port;
      memcpy(alterReq.replicas[alterReq.replica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      alterReq.replica++;
    }
    else{
      alterReq.learnerReplicas[alterReq.learnerReplica].id = pMObj->id;
      alterReq.learnerReplicas[alterReq.learnerReplica].port = pMObj->pDnode->port;
      memcpy(alterReq.learnerReplicas[alterReq.learnerReplica].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
      alterReq.learnerReplica++;
    }

    sdbRelease(pSdb, pMObj);
  }

  alterReq.replicas[alterReq.replica].id = pDnode->id;
C
cadem 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  alterReq.replicas[alterReq.replica].port = pDnode->port;
  memcpy(alterReq.replicas[alterReq.replica].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
  alterReq.replica++;

  alterReq.lastIndex = pObj->lastIndex;

  createEpset.inUse = 0;
  createEpset.numOfEps = 1;
  createEpset.eps[0].port = pDnode->port;
  memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);

  if (mndBuildAlterMnodeTypeRedoAction(pTrans, &alterReq, &createEpset) != 0) return -1;

  return 0;
}

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

577
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "create-mnode");
578
  if (pTrans == NULL) goto _OVER;
579
  mndTransSetSerial(pTrans);
580
  mInfo("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
581
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
582

C
cadem 已提交
583 584 585 586 587 588 589
  SMnodeObj mnodeObj = {0};
  mnodeObj.id = pDnode->id;
  mnodeObj.createdTime = taosGetTimestampMs();
  mnodeObj.updateTime = mnodeObj.createdTime;
  mnodeObj.role = TAOS_SYNC_ROLE_LEARNER;
  mnodeObj.lastIndex = pMnode->applied;

590
  if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) goto _OVER;
591
  if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) goto _OVER;
C
cadem 已提交
592 593 594 595 596 597 598 599 600 601

  SMnodeObj mnodeLeaderObj = {0};
  mnodeLeaderObj.id = pDnode->id;
  mnodeLeaderObj.createdTime = taosGetTimestampMs();
  mnodeLeaderObj.updateTime = mnodeLeaderObj.createdTime;
  mnodeLeaderObj.role = TAOS_SYNC_ROLE_VOTER;
  mnodeLeaderObj.lastIndex = pMnode->applied + 1;

  if (mndSetAlterMnodeTypeRedoActions(pMnode, pTrans, pDnode, &mnodeLeaderObj) != 0) goto _OVER;
  if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeLeaderObj) != 0) goto _OVER;
602
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
603

604 605
  code = 0;

606
_OVER:
S
Shengliang Guan 已提交
607
  mndTransDrop(pTrans);
608
  return code;
S
Shengliang Guan 已提交
609 610
}

S
Shengliang Guan 已提交
611 612
static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
613 614 615 616 617
  int32_t          code = -1;
  SMnodeObj       *pObj = NULL;
  SDnodeObj       *pDnode = NULL;
  SMCreateMnodeReq createReq = {0};

S
Shengliang Guan 已提交
618
  if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
619
    terrno = TSDB_CODE_INVALID_MSG;
620
    goto _OVER;
S
Shengliang Guan 已提交
621
  }
S
Shengliang Guan 已提交
622

623
  mInfo("mnode:%d, start to create", createReq.dnodeId);
624
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_MNODE) != 0) {
S
Shengliang Guan 已提交
625 626
    goto _OVER;
  }
S
Shengliang Guan 已提交
627

S
Shengliang Guan 已提交
628
  pObj = mndAcquireMnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
629
  if (pObj != NULL) {
S
Shengliang Guan 已提交
630
    terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
631
    goto _OVER;
S
Shengliang Guan 已提交
632
  } else if (terrno != TSDB_CODE_MND_MNODE_NOT_EXIST) {
633
    goto _OVER;
S
Shengliang Guan 已提交
634 635
  }

S
Shengliang Guan 已提交
636
  pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
S
Shengliang Guan 已提交
637 638
  if (pDnode == NULL) {
    terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
639
    goto _OVER;
S
Shengliang Guan 已提交
640 641
  }

642 643 644 645 646
  if (sdbGetSize(pMnode->pSdb, SDB_MNODE) >= 3) {
    terrno = TSDB_CODE_MND_TOO_MANY_MNODES;
    goto _OVER;
  }

S
Shengliang Guan 已提交
647
  if (!mndIsDnodeOnline(pDnode, taosGetTimestampMs())) {
648
    terrno = TSDB_CODE_DNODE_OFFLINE;
649 650 651
    goto _OVER;
  }

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

655
_OVER:
S
Shengliang Guan 已提交
656
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
657
    mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
S
Shengliang Guan 已提交
658 659
  }

S
Shengliang Guan 已提交
660 661 662 663
  mndReleaseMnode(pMnode, pObj);
  mndReleaseDnode(pMnode, pDnode);

  return code;
S
Shengliang Guan 已提交
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
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 已提交
682 683
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj,
                                          bool force) {
684 685 686 687
  SSdb          *pSdb = pMnode->pSdb;
  void          *pIter = NULL;
  SDDropMnodeReq dropReq = {0};
  SEpSet         dropEpSet = {0};
S
Shengliang Guan 已提交
688

S
Shengliang Guan 已提交
689 690 691 692
  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 已提交
693

694 695
  int32_t totalMnodes = sdbGetSize(pSdb, SDB_MNODE);
  if (totalMnodes == 2) {
S
Shengliang Guan 已提交
696 697
    if (force) {
      mError("cant't force drop dnode, since a mnode on it and replica is 2");
D
dmchen 已提交
698
      terrno = TSDB_CODE_MNODE_ONLY_TWO_MNODE;
S
Shengliang Guan 已提交
699 700
      return -1;
    }
701 702
    mInfo("vgId:1, has %d mnodes, exec redo log first", totalMnodes);
    if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) return -1;
S
Shengliang Guan 已提交
703 704 705
    if (!force) {
      if (mndBuildDropMnodeRedoAction(pTrans, &dropReq, &dropEpSet) != 0) return -1;
    }
706 707
  } else if (totalMnodes == 3) {
    mInfo("vgId:1, has %d mnodes, exec redo action first", totalMnodes);
S
Shengliang Guan 已提交
708 709 710
    if (!force) {
      if (mndBuildDropMnodeRedoAction(pTrans, &dropReq, &dropEpSet) != 0) return -1;
    }
711 712 713
    if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) return -1;
  } else {
    return -1;
714 715 716 717 718
  }

  return 0;
}

S
Shengliang Guan 已提交
719
int32_t mndSetDropMnodeInfoToTrans(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj, bool force) {
720
  if (pObj == NULL) return 0;
C
cadem 已提交
721
  pObj->lastIndex = pMnode->applied;
S
Shengliang Guan 已提交
722
  if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj, force) != 0) return -1;
723
  if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) return -1;
S
Shengliang Guan 已提交
724 725 726
  return 0;
}

S
Shengliang Guan 已提交
727
static int32_t mndDropMnode(SMnode *pMnode, SRpcMsg *pReq, SMnodeObj *pObj) {
728
  int32_t code = -1;
S
Shengliang Guan 已提交
729
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
730

731
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-mnode");
732
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
733
  mndTransSetSerial(pTrans);
734
  mInfo("trans:%d, used to drop mnode:%d", pTrans->id, pObj->id);
735
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
736

S
Shengliang Guan 已提交
737
  if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pObj, false) != 0) goto _OVER;
738
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
739

740 741
  code = 0;

742
_OVER:
S
Shengliang Guan 已提交
743
  mndTransDrop(pTrans);
744
  return code;
S
Shengliang Guan 已提交
745 746
}

S
Shengliang Guan 已提交
747 748
static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
749 750 751 752
  int32_t        code = -1;
  SMnodeObj     *pObj = NULL;
  SMDropMnodeReq dropReq = {0};

S
Shengliang Guan 已提交
753
  if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
754
    terrno = TSDB_CODE_INVALID_MSG;
755
    goto _OVER;
S
Shengliang Guan 已提交
756
  }
S
Shengliang Guan 已提交
757

758
  mInfo("mnode:%d, start to drop", dropReq.dnodeId);
759
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
S
Shengliang Guan 已提交
760 761
    goto _OVER;
  }
S
Shengliang Guan 已提交
762

S
Shengliang Guan 已提交
763
  if (dropReq.dnodeId <= 0) {
764 765
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
S
Shengliang Guan 已提交
766 767
  }

S
Shengliang Guan 已提交
768
  pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
S
Shengliang Guan 已提交
769
  if (pObj == NULL) {
770 771 772
    goto _OVER;
  }

773
  if (pMnode->selfDnodeId == dropReq.dnodeId) {
S
Shengliang Guan 已提交
774
    terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
775 776 777 778 779 780
    goto _OVER;
  }

  if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
    terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
    goto _OVER;
S
Shengliang Guan 已提交
781 782
  }

783
  if (!mndIsDnodeOnline(pObj->pDnode, taosGetTimestampMs())) {
784
    terrno = TSDB_CODE_DNODE_OFFLINE;
785 786 787
    goto _OVER;
  }

S
Shengliang Guan 已提交
788
  code = mndDropMnode(pMnode, pReq, pObj);
S
Shengliang Guan 已提交
789
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
790

791
_OVER:
S
Shengliang Guan 已提交
792
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
793 794 795 796 797
    mError("mnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
  }

  mndReleaseMnode(pMnode, pObj);
  return code;
S
Shengliang Guan 已提交
798 799
}

S
Shengliang Guan 已提交
800 801
static int32_t mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode    *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
802 803 804
  SSdb      *pSdb = pMnode->pSdb;
  int32_t    numOfRows = 0;
  int32_t    cols = 0;
S
Shengliang Guan 已提交
805
  SMnodeObj *pObj = NULL;
S
Shungang Li 已提交
806
  SMnodeObj *pSelfObj = NULL;
807
  ESdbStatus objStatus = 0;
S
Shengliang Guan 已提交
808
  char      *pWrite;
809
  int64_t    curMs = taosGetTimestampMs();
S
Shungang Li 已提交
810 811 812 813 814 815 816
  int64_t    dummyTimeMs = 0;

  pSelfObj = sdbAcquire(pSdb, SDB_MNODE, &pMnode->selfDnodeId);
  if (pSelfObj == NULL) {
    mError("mnode:%d, failed to acquire self %s", pMnode->selfDnodeId, terrstr());
    goto _out;
  }
S
Shengliang Guan 已提交
817 818

  while (numOfRows < rows) {
819
    pShow->pIter = sdbFetchAll(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj, &objStatus, true);
S
Shengliang Guan 已提交
820 821 822
    if (pShow->pIter == NULL) break;

    cols = 0;
S
Shengliang Guan 已提交
823
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
824
    colDataSetVal(pColInfo, numOfRows, (const char *)&pObj->id, false);
S
Shengliang Guan 已提交
825

826
    char b1[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
827
    STR_WITH_MAXSIZE_TO_VARSTR(b1, pObj->pDnode->ep, TSDB_EP_LEN + VARSTR_HEADER_SIZE);
S
Shengliang Guan 已提交
828

829
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
830
    colDataSetVal(pColInfo, numOfRows, b1, false);
S
Shengliang Guan 已提交
831

832
    char role[20] = "offline";
833
    if (pObj->id == pMnode->selfDnodeId) {
834
      snprintf(role, sizeof(role), "%s%s", syncStr(TAOS_SYNC_STATE_LEADER), pMnode->restored ? "" : "*");
835
    }
S
Shungang Li 已提交
836 837
    bool isDnodeOnline = mndIsDnodeOnline(pObj->pDnode, curMs);
    if (isDnodeOnline) {
838 839 840
      tstrncpy(role, syncStr(pObj->syncState), sizeof(role));
      if (pObj->syncState == TAOS_SYNC_STATE_LEADER && pObj->id != pMnode->selfDnodeId) {
        tstrncpy(role, syncStr(TAOS_SYNC_STATE_ERROR), sizeof(role));
S
Shengliang Guan 已提交
841 842
        mError("mnode:%d, is leader too", pObj->id);
      }
843 844
    }
    char b2[12 + VARSTR_HEADER_SIZE] = {0};
845
    STR_WITH_MAXSIZE_TO_VARSTR(b2, role, pShow->pMeta->pSchemas[cols].bytes);
846
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
847
    colDataSetVal(pColInfo, numOfRows, (const char *)b2, false);
S
Shengliang Guan 已提交
848

849 850 851
    const char *status = "ready";
    if (objStatus == SDB_STATUS_CREATING) status = "creating";
    if (objStatus == SDB_STATUS_DROPPING) status = "dropping";
S
Shungang Li 已提交
852
    if (!isDnodeOnline) status = "offline";
853 854 855
    char b3[9 + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(b3, status, pShow->pMeta->pSchemas[cols].bytes);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
856
    colDataSetVal(pColInfo, numOfRows, (const char *)b3, false);
857

858
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
859
    colDataSetVal(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
S
Shengliang Guan 已提交
860

C
cadem 已提交
861
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
S
Shungang Li 已提交
862 863 864 865 866 867 868 869 870
    if (pObj->syncTerm != pSelfObj->syncTerm || !isDnodeOnline) {
      // state of old term / no status report => use dummyTimeMs
      if (pObj->syncTerm > pSelfObj->syncTerm) {
        mError("mnode:%d has a newer term:%" PRId64 " than me:%" PRId64, pObj->id, pObj->syncTerm, pSelfObj->syncTerm);
      }
      colDataSetVal(pColInfo, numOfRows, (const char *)&dummyTimeMs, false);
    } else {
      colDataSetVal(pColInfo, numOfRows, (const char *)&pObj->roleTimeMs, false);
    }
C
cadem 已提交
871

S
Shengliang Guan 已提交
872
    numOfRows++;
S
Shengliang Guan 已提交
873
    sdbRelease(pSdb, pObj);
S
Shengliang Guan 已提交
874 875
  }

876
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
877

S
Shungang Li 已提交
878 879
_out:
  sdbRelease(pSdb, pSelfObj);
S
Shengliang Guan 已提交
880 881 882 883 884 885 886
  return numOfRows;
}

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

static int32_t mndProcessAlterMnodeReq(SRpcMsg *pReq) {
889 890 891
#if 1
  return 0;
#else
S
Shengliang Guan 已提交
892
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
893 894 895 896 897 898 899
  SDAlterMnodeReq alterReq = {0};

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

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
  SMnodeOpt option = {.deploy = true, .numOfReplicas = alterReq.replica, .selfIndex = -1};
  memcpy(option.replicas, alterReq.replicas, sizeof(alterReq.replicas));
  for (int32_t i = 0; i < option.numOfReplicas; ++i) {
    if (alterReq.replicas[i].id == pMnode->selfDnodeId) {
      option.selfIndex = i;
    }
  }

  if (option.selfIndex == -1) {
    mInfo("alter mnode not processed since selfIndex is -1", terrstr());
    return 0;
  }

  if (mndWriteFile(pMnode->path, &option) != 0) {
    mError("failed to write mnode file since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
918 919 920 921 922
  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;
923 924 925
    if (alterReq.replicas[i].id == pMnode->selfDnodeId) {
      cfg.myIndex = i;
    }
S
Shengliang Guan 已提交
926 927 928 929 930 931
  }

  if (cfg.myIndex == -1) {
    mError("failed to alter mnode since myindex is -1");
    return -1;
  } else {
932
    mInfo("start to alter mnode sync, replica:%d myIndex:%d", cfg.replicaNum, cfg.myIndex);
S
Shengliang Guan 已提交
933 934 935 936 937 938
    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);
    }
  }

939
  int32_t code = syncReconfig(pMnode->syncMgmt.sync, &cfg);
S
Shengliang Guan 已提交
940
  if (code != 0) {
941
    mError("failed to sync reconfig since %s", terrstr());
S
Shengliang Guan 已提交
942
  } else {
943 944 945 946 947 948 949 950 951 952 953 954
    mInfo("alter mnode sync success");
  }

  return code;
#endif
}

static void mndReloadSyncConfig(SMnode *pMnode) {
  SSdb      *pSdb = pMnode->pSdb;
  SMnodeObj *pObj = NULL;
  ESdbStatus objStatus = 0;
  void      *pIter = NULL;
955 956
  int32_t    updatingMnodes = 0;
  int32_t    readyMnodes = 0;
C
cadem 已提交
957 958
  SSyncCfg   cfg = {.myIndex = -1, .lastIndex = 0,};
  SyncIndex  maxIndex = 0;
959 960 961 962 963 964

  while (1) {
    pIter = sdbFetchAll(pSdb, SDB_MNODE, pIter, (void **)&pObj, &objStatus, false);
    if (pIter == NULL) break;
    if (objStatus == SDB_STATUS_CREATING || objStatus == SDB_STATUS_DROPPING) {
      mInfo("vgId:1, has updating mnode:%d, status:%s", pObj->id, sdbStatusName(objStatus));
965 966 967 968 969
      updatingMnodes++;
    }
    if (objStatus == SDB_STATUS_READY) {
      mInfo("vgId:1, has ready mnode:%d, status:%s", pObj->id, sdbStatusName(objStatus));
      readyMnodes++;
970 971 972
    }

    if (objStatus == SDB_STATUS_READY || objStatus == SDB_STATUS_CREATING) {
C
cadem 已提交
973
      SNodeInfo *pNode = &cfg.nodeInfo[cfg.totalReplicaNum];
974 975
      pNode->nodeId = pObj->pDnode->id;
      pNode->clusterId = mndGetClusterId(pMnode);
976
      pNode->nodePort = pObj->pDnode->port;
C
cadem 已提交
977
      pNode->nodeRole = pObj->role;
978
      tstrncpy(pNode->nodeFqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN);
979
      (void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
980
      mInfo("vgId:1, ep:%s:%u dnode:%d", pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
981
      if (pObj->pDnode->id == pMnode->selfDnodeId) {
C
cadem 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995
        cfg.myIndex = cfg.totalReplicaNum;
      }
      if(pNode->nodeRole == TAOS_SYNC_ROLE_VOTER){
        cfg.replicaNum++;
      }
      cfg.totalReplicaNum++;
      if(pObj->lastIndex > cfg.lastIndex){
        cfg.lastIndex = pObj->lastIndex;
      }
    }

    if (objStatus == SDB_STATUS_DROPPING) {
      if(pObj->lastIndex > cfg.lastIndex){
        cfg.lastIndex = pObj->lastIndex;
996 997 998
      }
    }

C
cadem 已提交
999 1000
    mInfo("vgId:1, mnode:%d, role:%d, lastIndex:%" PRId64, pObj->id, pObj->role, pObj->lastIndex);

1001 1002 1003
    sdbReleaseLock(pSdb, pObj, false);
  }

C
cadem 已提交
1004 1005 1006 1007
  //if (readyMnodes <= 0 || updatingMnodes <= 0) {
  //  mInfo("vgId:1, mnode sync not reconfig since readyMnodes:%d updatingMnodes:%d", readyMnodes, updatingMnodes);
  //  return;
  //}
1008

1009
  if (cfg.myIndex == -1) {
1010
#if 1
1011
    mInfo("vgId:1, mnode sync not reconfig since selfIndex is -1");
1012 1013 1014 1015 1016
#else
    // cannot reconfig because the leader may fail to elect after reboot
    mInfo("vgId:1, mnode sync not reconfig since selfIndex is -1, do sync stop oper");
    syncStop(pMnode->syncMgmt.sync);
#endif
1017 1018 1019
    return;
  }

C
cadem 已提交
1020
  if (pMnode->syncMgmt.sync > 0) {
S
Shungang Li 已提交
1021
    mInfo("vgId:1, mnode sync reconfig, totalReplica:%d replica:%d myIndex:%d",
C
cadem 已提交
1022 1023 1024
                                        cfg.totalReplicaNum, cfg.replicaNum, cfg.myIndex);

    for (int32_t i = 0; i < cfg.totalReplicaNum; ++i) {
1025
      SNodeInfo *pNode = &cfg.nodeInfo[i];
S
Shungang Li 已提交
1026
      mInfo("vgId:1, index:%d, ep:%s:%u dnode:%d cluster:%" PRId64 " role:%d", i, pNode->nodeFqdn, pNode->nodePort,
C
cadem 已提交
1027
            pNode->nodeId, pNode->clusterId, pNode->nodeRole);
1028 1029 1030 1031
    }

    int32_t code = syncReconfig(pMnode->syncMgmt.sync, &cfg);
    if (code != 0) {
S
Shengliang Guan 已提交
1032
      mError("vgId:1, mnode sync reconfig failed since %s", terrstr());
1033
    } else {
S
Shengliang Guan 已提交
1034
      mInfo("vgId:1, mnode sync reconfig success");
1035
    }
S
Shengliang Guan 已提交
1036 1037
  }
}