TDengine/source/dnode/mnode/impl/src/mndMnode.c

794 lines
25 KiB
C
Raw Normal View History

2021-09-22 08:15:20 +00:00
/*
* 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/>.
*/
2021-10-16 07:16:05 +00:00
#define _DEFAULT_SOURCE
2021-12-08 11:34:54 +00:00
#include "mndMnode.h"
#include "mndDnode.h"
2022-10-13 03:56:16 +00:00
#include "mndPrivilege.h"
2021-12-08 11:34:54 +00:00
#include "mndShow.h"
#include "mndSync.h"
2021-11-30 11:42:51 +00:00
#include "mndTrans.h"
2022-11-23 14:08:35 +00:00
#include "tmisce.h"
#include "mndCluster.h"
2021-10-17 03:42:05 +00:00
2022-04-09 11:00:52 +00:00
#define MNODE_VER_NUMBER 1
#define MNODE_RESERVE_SIZE 64
2021-10-20 01:06:08 +00:00
2021-12-08 06:30:28 +00:00
static int32_t mndCreateDefaultMnode(SMnode *pMnode);
2021-12-23 02:30:51 +00:00
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj);
2021-12-08 06:30:28 +00:00
static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw);
2021-12-23 02:30:51 +00:00
static int32_t mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj);
static int32_t mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj);
2022-01-07 11:23:22 +00:00
static int32_t mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOld, SMnodeObj *pNew);
2022-05-16 06:55:31 +00:00
static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq);
2022-05-25 09:44:43 +00:00
static int32_t mndProcessAlterMnodeReq(SRpcMsg *pReq);
2022-05-16 06:55:31 +00:00
static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq);
static int32_t mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
2021-12-08 11:34:54 +00:00
static void mndCancelGetNextMnode(SMnode *pMnode, void *pIter);
static void mndReloadSyncConfig(SMnode *pMnode);
2021-12-08 06:30:28 +00:00
int32_t mndInitMnode(SMnode *pMnode) {
2022-05-24 07:05:01 +00:00
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,
};
2021-12-08 06:30:28 +00:00
2021-12-24 07:00:51 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_CREATE_MNODE, mndProcessCreateMnodeReq);
mndSetMsgHandle(pMnode, TDMT_DND_CREATE_MNODE_RSP, mndTransProcessRsp);
2022-06-02 11:00:14 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_ALTER_MNODE, mndProcessAlterMnodeReq);
mndSetMsgHandle(pMnode, TDMT_MND_ALTER_MNODE_RSP, mndTransProcessRsp);
2021-12-24 07:00:51 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_DROP_MNODE, mndProcessDropMnodeReq);
mndSetMsgHandle(pMnode, TDMT_DND_DROP_MNODE_RSP, mndTransProcessRsp);
2021-12-08 11:34:54 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndRetrieveMnodes);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_MNODE, mndCancelGetNextMnode);
2021-12-08 06:30:28 +00:00
return sdbSetTable(pMnode->pSdb, table);
}
void mndCleanupMnode(SMnode *pMnode) {}
2022-04-09 11:00:52 +00:00
SMnodeObj *mndAcquireMnode(SMnode *pMnode, int32_t mnodeId) {
SMnodeObj *pObj = sdbAcquire(pMnode->pSdb, SDB_MNODE, &mnodeId);
2022-01-07 11:23:22 +00:00
if (pObj == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
2021-12-20 12:30:55 +00:00
terrno = TSDB_CODE_MND_MNODE_NOT_EXIST;
}
return pObj;
2021-12-08 11:34:54 +00:00
}
2022-04-09 11:00:52 +00:00
void mndReleaseMnode(SMnode *pMnode, SMnodeObj *pObj) {
2021-12-08 11:34:54 +00:00
SSdb *pSdb = pMnode->pSdb;
2022-04-09 11:00:52 +00:00
sdbRelease(pMnode->pSdb, pObj);
2021-12-08 11:34:54 +00:00
}
2021-12-08 06:30:28 +00:00
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;
2022-10-11 01:23:47 +00:00
(void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
2021-12-08 06:30:28 +00:00
2022-08-25 14:33:27 +00:00
mInfo("mnode:%d, will be created when deploying, raw:%p", mnodeObj.id, pRaw);
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, NULL, "create-mnode");
if (pTrans == NULL) {
2022-09-30 03:26:26 +00:00
sdbFreeRaw(pRaw);
mError("mnode:%d, failed to create since %s", mnodeObj.id, terrstr());
return -1;
}
2022-08-25 14:33:27 +00:00
mInfo("trans:%d, used to create mnode:%d", pTrans->id, mnodeObj.id);
if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
2022-10-10 02:51:16 +00:00
(void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
if (mndTransPrepare(pMnode, pTrans) != 0) {
mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
mndTransDrop(pTrans);
return 0;
2021-12-08 06:30:28 +00:00
}
2021-12-23 02:30:51 +00:00
static SSdbRaw *mndMnodeActionEncode(SMnodeObj *pObj) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-04-09 11:00:52 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_MNODE, MNODE_VER_NUMBER, sizeof(SMnodeObj) + MNODE_RESERVE_SIZE);
if (pRaw == NULL) goto _OVER;
2021-11-30 11:42:51 +00:00
int32_t dataPos = 0;
2022-04-09 11:00:52 +00:00
SDB_SET_INT32(pRaw, dataPos, pObj->id, _OVER)
SDB_SET_INT64(pRaw, dataPos, pObj->createdTime, _OVER)
SDB_SET_INT64(pRaw, dataPos, pObj->updateTime, _OVER)
SDB_SET_RESERVE(pRaw, dataPos, MNODE_RESERVE_SIZE, _OVER)
2021-12-31 06:22:50 +00:00
terrno = 0;
2022-04-09 11:00:52 +00:00
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
mError("mnode:%d, failed to encode to raw:%p since %s", pObj->id, pRaw, terrstr());
sdbFreeRaw(pRaw);
return NULL;
}
2021-11-30 11:42:51 +00:00
2021-12-31 06:22:50 +00:00
mTrace("mnode:%d, encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
2021-11-30 11:42:51 +00:00
return pRaw;
}
static SSdbRow *mndMnodeActionDecode(SSdbRaw *pRaw) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-12-01 08:04:39 +00:00
SSdbRow *pRow = NULL;
SMnodeObj *pObj = NULL;
2021-12-31 06:22:50 +00:00
2021-11-30 11:42:51 +00:00
int8_t sver = 0;
if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
2022-04-09 11:00:52 +00:00
if (sver != MNODE_VER_NUMBER) {
2021-11-30 11:42:51 +00:00
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
2022-04-09 11:00:52 +00:00
goto _OVER;
2021-11-30 11:42:51 +00:00
}
2022-12-01 08:04:39 +00:00
pRow = sdbAllocRow(sizeof(SMnodeObj));
2022-04-09 11:00:52 +00:00
if (pRow == NULL) goto _OVER;
2021-12-31 06:22:50 +00:00
2022-12-01 08:04:39 +00:00
pObj = sdbGetRowObj(pRow);
2022-04-09 11:00:52 +00:00
if (pObj == NULL) goto _OVER;
2021-11-30 11:42:51 +00:00
int32_t dataPos = 0;
2022-04-09 11:00:52 +00:00
SDB_GET_INT32(pRaw, dataPos, &pObj->id, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pObj->createdTime, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pObj->updateTime, _OVER)
SDB_GET_RESERVE(pRaw, dataPos, MNODE_RESERVE_SIZE, _OVER)
2021-12-31 06:22:50 +00:00
terrno = 0;
2022-04-09 11:00:52 +00:00
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
2022-12-01 08:04:39 +00:00
mError("mnode:%d, failed to decode from raw:%p since %s", pObj == NULL ? 0 : pObj->id, pRaw, terrstr());
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(pRow);
2021-12-31 06:22:50 +00:00
return NULL;
}
2021-11-30 11:42:51 +00:00
2021-12-31 06:22:50 +00:00
mTrace("mnode:%d, decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
2021-11-30 11:42:51 +00:00
return pRow;
}
2021-12-23 02:30:51 +00:00
static int32_t mndMnodeActionInsert(SSdb *pSdb, SMnodeObj *pObj) {
2021-12-31 06:22:50 +00:00
mTrace("mnode:%d, perform insert action, row:%p", pObj->id, pObj);
2021-12-23 02:30:51 +00:00
pObj->pDnode = sdbAcquire(pSdb, SDB_DNODE, &pObj->id);
if (pObj->pDnode == NULL) {
2021-11-30 11:42:51 +00:00
terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
2021-12-23 02:30:51 +00:00
mError("mnode:%d, failed to perform insert action since %s", pObj->id, terrstr());
2021-11-30 11:42:51 +00:00
return -1;
}
2022-11-30 04:02:54 +00:00
pObj->syncState = TAOS_SYNC_STATE_OFFLINE;
mndReloadSyncConfig(pSdb->pMnode);
2021-11-30 11:42:51 +00:00
return 0;
}
2021-12-23 02:30:51 +00:00
static int32_t mndMnodeActionDelete(SSdb *pSdb, SMnodeObj *pObj) {
2021-12-31 06:22:50 +00:00
mTrace("mnode:%d, perform delete action, row:%p", pObj->id, pObj);
2021-12-23 02:30:51 +00:00
if (pObj->pDnode != NULL) {
sdbRelease(pSdb, pObj->pDnode);
pObj->pDnode = NULL;
2021-11-30 11:42:51 +00:00
}
return 0;
}
2022-01-07 11:23:22 +00:00
static int32_t mndMnodeActionUpdate(SSdb *pSdb, SMnodeObj *pOld, SMnodeObj *pNew) {
2022-01-10 08:24:23 +00:00
mTrace("mnode:%d, perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
2022-01-07 11:23:22 +00:00
pOld->updateTime = pNew->updateTime;
mndReloadSyncConfig(pSdb->pMnode);
2021-12-01 09:27:31 +00:00
return 0;
2021-11-30 11:42:51 +00:00
}
bool mndIsMnode(SMnode *pMnode, int32_t dnodeId) {
SSdb *pSdb = pMnode->pSdb;
2021-12-23 02:30:51 +00:00
SMnodeObj *pObj = sdbAcquire(pSdb, SDB_MNODE, &dnodeId);
if (pObj == NULL) {
2021-11-30 11:42:51 +00:00
return false;
}
2021-12-23 02:30:51 +00:00
sdbRelease(pSdb, pObj);
2021-11-30 11:42:51 +00:00
return true;
2021-12-03 06:36:41 +00:00
}
2021-12-05 10:37:54 +00:00
void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {
SSdb *pSdb = pMnode->pSdb;
int32_t totalMnodes = sdbGetSize(pSdb, SDB_MNODE);
2022-09-30 03:26:26 +00:00
if (totalMnodes == 0) {
syncGetRetryEpSet(pMnode->syncMgmt.sync, pEpSet);
return;
}
2021-12-05 10:37:54 +00:00
2022-09-30 03:26:26 +00:00
void *pIter = NULL;
2021-12-05 10:37:54 +00:00
while (1) {
2021-12-23 02:30:51 +00:00
SMnodeObj *pObj = NULL;
pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
2021-12-05 10:37:54 +00:00
if (pIter == NULL) break;
if (pObj->id == pMnode->selfDnodeId) {
if (mndIsLeader(pMnode)) {
2022-04-09 11:00:52 +00:00
pEpSet->inUse = pEpSet->numOfEps;
} else {
pEpSet->inUse = (pEpSet->numOfEps + 1) % totalMnodes;
2022-04-09 11:00:52 +00:00
}
2021-12-05 10:37:54 +00:00
}
2022-10-13 02:49:56 +00:00
if (pObj->pDnode != NULL) {
addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
}
sdbRelease(pSdb, pObj);
2021-12-05 10:37:54 +00:00
}
if (pEpSet->numOfEps == 0) {
syncGetRetryEpSet(pMnode->syncMgmt.sync, pEpSet);
}
if (pEpSet->inUse >= pEpSet->numOfEps) {
pEpSet->inUse = 0;
}
2021-12-08 06:30:28 +00:00
}
2021-12-23 04:09:37 +00:00
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;
}
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,
.acceptableCode = TSDB_CODE_MNODE_ALREADY_DEPLOYED,
};
if (mndTransAppendRedoAction(pTrans, &action) != 0) {
taosMemoryFree(pReq);
return -1;
}
return 0;
}
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) {
2022-10-18 09:34:58 +00:00
int32_t contLen = tSerializeSCreateDropMQSNodeReq(NULL, 0, pDropReq);
void *pReq = taosMemoryMalloc(contLen);
2022-10-18 09:34:58 +00:00
tSerializeSCreateDropMQSNodeReq(pReq, contLen, pDropReq);
STransAction action = {
.epSet = *pDroprEpSet,
.pCont = pReq,
.contLen = contLen,
.msgType = TDMT_DND_DROP_MNODE,
.acceptableCode = TSDB_CODE_MNODE_NOT_DEPLOYED,
};
if (mndTransAppendRedoAction(pTrans, &action) != 0) {
taosMemoryFree(pReq);
return -1;
}
return 0;
}
2021-12-23 06:09:05 +00:00
static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj) {
2022-05-25 15:07:06 +00:00
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
int32_t numOfReplicas = 0;
2022-01-07 11:23:22 +00:00
SDCreateMnodeReq createReq = {0};
2022-05-25 15:07:06 +00:00
SEpSet createEpset = {0};
2021-12-23 06:09:05 +00:00
while (1) {
SMnodeObj *pMObj = NULL;
pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pMObj);
if (pIter == NULL) break;
2021-12-23 04:09:37 +00:00
createReq.replicas[numOfReplicas].id = pMObj->id;
createReq.replicas[numOfReplicas].port = pMObj->pDnode->port;
memcpy(createReq.replicas[numOfReplicas].fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN);
2022-05-25 15:07:06 +00:00
numOfReplicas++;
2021-12-23 06:09:05 +00:00
sdbRelease(pSdb, pMObj);
}
createReq.replica = numOfReplicas + 1;
createReq.replicas[numOfReplicas].id = pDnode->id;
createReq.replicas[numOfReplicas].port = pDnode->port;
memcpy(createReq.replicas[numOfReplicas].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
2021-12-23 06:09:05 +00:00
createEpset.inUse = 0;
2022-05-25 15:07:06 +00:00
createEpset.numOfEps = 1;
createEpset.eps[0].port = pDnode->port;
memcpy(createEpset.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
2021-12-23 06:09:05 +00:00
if (mndBuildCreateMnodeRedoAction(pTrans, &createReq, &createEpset) != 0) return -1;
2021-12-23 04:09:37 +00:00
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndCreateMnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SMCreateMnodeReq *pCreate) {
2022-01-07 11:52:59 +00:00
int32_t code = -1;
2021-12-08 11:34:54 +00:00
SMnodeObj mnodeObj = {0};
2021-12-29 12:05:10 +00:00
mnodeObj.id = pDnode->id;
2021-12-08 11:34:54 +00:00
mnodeObj.createdTime = taosGetTimestampMs();
mnodeObj.updateTime = mnodeObj.createdTime;
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "create-mnode");
2022-04-09 11:00:52 +00:00
if (pTrans == NULL) goto _OVER;
mndTransSetSerial(pTrans);
2022-08-25 14:33:27 +00:00
mInfo("trans:%d, used to create mnode:%d", pTrans->id, pCreate->dnodeId);
if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
2022-06-06 08:21:56 +00:00
if (mndSetCreateMnodeRedoActions(pMnode, pTrans, pDnode, &mnodeObj) != 0) goto _OVER;
2022-04-09 11:00:52 +00:00
if (mndSetCreateMnodeRedoLogs(pMnode, pTrans, &mnodeObj) != 0) goto _OVER;
if (mndSetCreateMnodeCommitLogs(pMnode, pTrans, &mnodeObj) != 0) goto _OVER;
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
2021-12-08 11:34:54 +00:00
2021-12-23 04:09:37 +00:00
code = 0;
2022-04-09 11:00:52 +00:00
_OVER:
2021-12-08 11:34:54 +00:00
mndTransDrop(pTrans);
2021-12-23 04:09:37 +00:00
return code;
2021-12-08 11:34:54 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-12 06:21:26 +00:00
int32_t code = -1;
SMnodeObj *pObj = NULL;
SDnodeObj *pDnode = NULL;
SMCreateMnodeReq createReq = {0};
2022-10-18 09:34:58 +00:00
if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
2022-02-12 06:21:26 +00:00
terrno = TSDB_CODE_INVALID_MSG;
2022-04-09 11:00:52 +00:00
goto _OVER;
2022-02-12 06:21:26 +00:00
}
2021-12-08 11:34:54 +00:00
2022-08-25 14:33:27 +00:00
mInfo("mnode:%d, start to create", createReq.dnodeId);
2022-06-25 01:09:33 +00:00
if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_MNODE) != 0) {
2022-06-25 00:56:47 +00:00
goto _OVER;
}
2021-12-08 11:34:54 +00:00
2022-02-12 06:21:26 +00:00
pObj = mndAcquireMnode(pMnode, createReq.dnodeId);
2021-12-23 02:30:51 +00:00
if (pObj != NULL) {
2021-12-08 11:34:54 +00:00
terrno = TSDB_CODE_MND_MNODE_ALREADY_EXIST;
2022-04-09 11:00:52 +00:00
goto _OVER;
2022-01-07 11:52:59 +00:00
} else if (terrno != TSDB_CODE_MND_MNODE_NOT_EXIST) {
2022-04-09 11:00:52 +00:00
goto _OVER;
2021-12-08 11:34:54 +00:00
}
2022-02-12 06:21:26 +00:00
pDnode = mndAcquireDnode(pMnode, createReq.dnodeId);
2021-12-23 06:09:05 +00:00
if (pDnode == NULL) {
terrno = TSDB_CODE_MND_DNODE_NOT_EXIST;
2022-04-09 11:00:52 +00:00
goto _OVER;
2021-12-23 06:09:05 +00:00
}
2022-06-11 07:22:26 +00:00
if (sdbGetSize(pMnode->pSdb, SDB_MNODE) >= 3) {
terrno = TSDB_CODE_MND_TOO_MANY_MNODES;
goto _OVER;
}
2022-06-06 03:49:30 +00:00
if (!mndIsDnodeOnline(pDnode, taosGetTimestampMs())) {
terrno = TSDB_CODE_DNODE_OFFLINE;
goto _OVER;
}
2022-02-12 06:21:26 +00:00
code = mndCreateMnode(pMnode, pReq, pDnode, &createReq);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2022-02-12 06:21:26 +00:00
2022-04-09 11:00:52 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-12 06:21:26 +00:00
mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
2021-12-08 11:34:54 +00:00
}
2022-02-12 06:21:26 +00:00
mndReleaseMnode(pMnode, pObj);
mndReleaseDnode(pMnode, pDnode);
return code;
2021-12-08 11:34:54 +00:00
}
2021-12-23 04:09:37 +00:00
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;
}
2022-10-31 13:33:06 +00:00
static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnodeObj *pDnode, SMnodeObj *pObj,
bool force) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
SDDropMnodeReq dropReq = {0};
SEpSet dropEpSet = {0};
2021-12-23 11:59:42 +00:00
2022-05-25 15:07:06 +00:00
dropReq.dnodeId = pDnode->id;
dropEpSet.numOfEps = 1;
dropEpSet.eps[0].port = pDnode->port;
memcpy(dropEpSet.eps[0].fqdn, pDnode->fqdn, TSDB_FQDN_LEN);
2021-12-23 06:09:05 +00:00
int32_t totalMnodes = sdbGetSize(pSdb, SDB_MNODE);
if (totalMnodes == 2) {
2022-10-31 07:54:47 +00:00
if (force) {
mError("cant't force drop dnode, since a mnode on it and replica is 2");
terrno = TSDB_CODE_DNODE_OFFLINE;
2022-10-31 07:54:47 +00:00
return -1;
}
mInfo("vgId:1, has %d mnodes, exec redo log first", totalMnodes);
if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) return -1;
2022-10-31 07:54:47 +00:00
if (!force) {
if (mndBuildDropMnodeRedoAction(pTrans, &dropReq, &dropEpSet) != 0) return -1;
}
} else if (totalMnodes == 3) {
mInfo("vgId:1, has %d mnodes, exec redo action first", totalMnodes);
2022-10-31 07:54:47 +00:00
if (!force) {
if (mndBuildDropMnodeRedoAction(pTrans, &dropReq, &dropEpSet) != 0) return -1;
}
if (mndSetDropMnodeRedoLogs(pMnode, pTrans, pObj) != 0) return -1;
} else {
return -1;
2021-12-23 04:09:37 +00:00
}
return 0;
}
2022-10-31 07:54:47 +00:00
int32_t mndSetDropMnodeInfoToTrans(SMnode *pMnode, STrans *pTrans, SMnodeObj *pObj, bool force) {
2022-06-06 10:03:23 +00:00
if (pObj == NULL) return 0;
2022-10-31 07:54:47 +00:00
if (mndSetDropMnodeRedoActions(pMnode, pTrans, pObj->pDnode, pObj, force) != 0) return -1;
if (mndSetDropMnodeCommitLogs(pMnode, pTrans, pObj) != 0) return -1;
2022-06-06 03:49:30 +00:00
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndDropMnode(SMnode *pMnode, SRpcMsg *pReq, SMnodeObj *pObj) {
2021-12-23 04:09:37 +00:00
int32_t code = -1;
2022-06-06 03:49:30 +00:00
STrans *pTrans = NULL;
2022-01-07 11:52:59 +00:00
2022-09-22 08:18:51 +00:00
pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "drop-mnode");
2022-04-09 11:00:52 +00:00
if (pTrans == NULL) goto _OVER;
2022-06-06 08:21:56 +00:00
mndTransSetSerial(pTrans);
2022-08-25 14:33:27 +00:00
mInfo("trans:%d, used to drop mnode:%d", pTrans->id, pObj->id);
if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
2022-06-06 08:21:56 +00:00
2022-10-31 07:54:47 +00:00
if (mndSetDropMnodeInfoToTrans(pMnode, pTrans, pObj, false) != 0) goto _OVER;
2022-04-09 11:00:52 +00:00
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
2021-12-08 11:34:54 +00:00
2021-12-23 04:09:37 +00:00
code = 0;
2022-04-09 11:00:52 +00:00
_OVER:
2021-12-08 11:34:54 +00:00
mndTransDrop(pTrans);
2021-12-23 04:09:37 +00:00
return code;
2021-12-08 11:34:54 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-12 06:21:26 +00:00
int32_t code = -1;
SMnodeObj *pObj = NULL;
SMDropMnodeReq dropReq = {0};
2022-10-18 09:34:58 +00:00
if (tDeserializeSCreateDropMQSNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
2022-02-12 06:21:26 +00:00
terrno = TSDB_CODE_INVALID_MSG;
2022-04-09 11:00:52 +00:00
goto _OVER;
2022-02-12 06:21:26 +00:00
}
2021-12-08 11:34:54 +00:00
2022-08-25 14:33:27 +00:00
mInfo("mnode:%d, start to drop", dropReq.dnodeId);
2022-06-25 01:09:33 +00:00
if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_MNODE) != 0) {
2022-06-25 00:56:47 +00:00
goto _OVER;
}
2021-12-08 11:34:54 +00:00
2022-02-12 06:21:26 +00:00
if (dropReq.dnodeId <= 0) {
2022-04-09 11:00:52 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2021-12-08 11:34:54 +00:00
}
2022-02-12 06:21:26 +00:00
pObj = mndAcquireMnode(pMnode, dropReq.dnodeId);
2021-12-23 02:30:51 +00:00
if (pObj == NULL) {
2022-04-09 11:00:52 +00:00
goto _OVER;
}
2022-05-25 08:41:38 +00:00
if (pMnode->selfDnodeId == dropReq.dnodeId) {
2022-06-07 06:31:07 +00:00
terrno = TSDB_CODE_MND_CANT_DROP_LEADER;
2022-04-09 11:00:52 +00:00
goto _OVER;
}
if (sdbGetSize(pMnode->pSdb, SDB_MNODE) <= 1) {
terrno = TSDB_CODE_MND_TOO_FEW_MNODES;
goto _OVER;
2021-12-08 11:34:54 +00:00
}
2022-06-11 07:22:26 +00:00
if (!mndIsDnodeOnline(pObj->pDnode, taosGetTimestampMs())) {
terrno = TSDB_CODE_DNODE_OFFLINE;
2022-06-11 07:22:26 +00:00
goto _OVER;
}
2022-02-12 06:21:26 +00:00
code = mndDropMnode(pMnode, pReq, pObj);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2022-02-12 06:21:26 +00:00
2022-04-09 11:00:52 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-12 06:21:26 +00:00
mError("mnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
}
mndReleaseMnode(pMnode, pObj);
return code;
2021-12-08 11:34:54 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveMnodes(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SMnode *pMnode = pReq->info.node;
2021-12-08 11:34:54 +00:00
SSdb *pSdb = pMnode->pSdb;
int32_t numOfRows = 0;
int32_t cols = 0;
2021-12-23 02:30:51 +00:00
SMnodeObj *pObj = NULL;
ESdbStatus objStatus = 0;
2021-12-08 11:34:54 +00:00
char *pWrite;
int64_t curMs = taosGetTimestampMs();
2021-12-08 11:34:54 +00:00
while (numOfRows < rows) {
pShow->pIter = sdbFetchAll(pSdb, SDB_MNODE, pShow->pIter, (void **)&pObj, &objStatus, true);
2021-12-08 11:34:54 +00:00
if (pShow->pIter == NULL) break;
cols = 0;
2022-04-18 08:05:06 +00:00
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pObj->id, false);
2021-12-08 11:34:54 +00:00
char b1[TSDB_EP_LEN + VARSTR_HEADER_SIZE] = {0};
2022-09-30 03:26:26 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(b1, pObj->pDnode->ep, TSDB_EP_LEN + VARSTR_HEADER_SIZE);
2021-12-08 11:34:54 +00:00
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, b1, false);
2021-12-08 11:34:54 +00:00
char role[20] = "offline";
2022-05-25 08:41:38 +00:00
if (pObj->id == pMnode->selfDnodeId) {
snprintf(role, sizeof(role), "%s%s", syncStr(TAOS_SYNC_STATE_LEADER), pMnode->restored ? "" : "*");
}
2022-09-30 03:26:26 +00:00
if (mndIsDnodeOnline(pObj->pDnode, curMs)) {
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));
2022-06-16 04:01:13 +00:00
mError("mnode:%d, is leader too", pObj->id);
}
}
char b2[12 + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(b2, role, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2022-04-18 08:05:06 +00:00
colDataAppend(pColInfo, numOfRows, (const char *)b2, false);
2021-12-08 11:34:54 +00:00
2022-06-02 06:35:59 +00:00
const char *status = "ready";
if (objStatus == SDB_STATUS_CREATING) status = "creating";
if (objStatus == SDB_STATUS_DROPPING) status = "dropping";
if (!mndIsDnodeOnline(pObj->pDnode, curMs)) status = "offline";
char b3[9 + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(b3, status, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)b3, false);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pObj->createdTime, false);
2021-12-08 11:34:54 +00:00
numOfRows++;
2021-12-23 02:30:51 +00:00
sdbRelease(pSdb, pObj);
2021-12-08 11:34:54 +00:00
}
pShow->numOfRows += numOfRows;
2021-12-08 11:34:54 +00:00
return numOfRows;
}
static void mndCancelGetNextMnode(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetch(pSdb, pIter);
}
2022-05-25 09:44:43 +00:00
static int32_t mndProcessAlterMnodeReq(SRpcMsg *pReq) {
#if 1
return 0;
#else
2022-05-25 15:07:06 +00:00
SMnode *pMnode = pReq->info.node;
2022-05-25 09:44:43 +00:00
SDAlterMnodeReq alterReq = {0};
if (tDeserializeSDCreateMnodeReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
return -1;
}
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;
}
2022-05-25 09:44:43 +00:00
SSyncCfg cfg = {.replicaNum = alterReq.replica, .myIndex = -1};
for (int32_t i = 0; i < alterReq.replica; ++i) {
SNodeInfo *pNode = &cfg.nodeInfo[i];
tstrncpy(pNode->nodeFqdn, alterReq.replicas[i].fqdn, sizeof(pNode->nodeFqdn));
pNode->nodePort = alterReq.replicas[i].port;
if (alterReq.replicas[i].id == pMnode->selfDnodeId) {
cfg.myIndex = i;
}
2022-05-25 09:44:43 +00:00
}
if (cfg.myIndex == -1) {
mError("failed to alter mnode since myindex is -1");
return -1;
} else {
mInfo("start to alter mnode sync, replica:%d myIndex:%d", cfg.replicaNum, cfg.myIndex);
2022-05-25 09:44:43 +00:00
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);
}
}
int32_t code = syncReconfig(pMnode->syncMgmt.sync, &cfg);
2022-05-25 09:44:43 +00:00
if (code != 0) {
mError("failed to sync reconfig since %s", terrstr());
2022-05-25 09:44:43 +00:00
} else {
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;
int32_t updatingMnodes = 0;
int32_t readyMnodes = 0;
SSyncCfg cfg = {.myIndex = -1};
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));
updatingMnodes++;
}
if (objStatus == SDB_STATUS_READY) {
mInfo("vgId:1, has ready mnode:%d, status:%s", pObj->id, sdbStatusName(objStatus));
readyMnodes++;
}
if (objStatus == SDB_STATUS_READY || objStatus == SDB_STATUS_CREATING) {
SNodeInfo *pNode = &cfg.nodeInfo[cfg.replicaNum];
pNode->nodeId = pObj->pDnode->id;
pNode->clusterId = mndGetClusterId(pMnode);
pNode->nodePort = pObj->pDnode->port;
tstrncpy(pNode->nodeFqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN);
(void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
mInfo("vgId:1, ep:%s:%u dnode:%d", pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
if (pObj->pDnode->id == pMnode->selfDnodeId) {
cfg.myIndex = cfg.replicaNum;
}
cfg.replicaNum++;
}
sdbReleaseLock(pSdb, pObj, false);
}
if (readyMnodes <= 0 || updatingMnodes <= 0) {
mInfo("vgId:1, mnode sync not reconfig since readyMnodes:%d updatingMnodes:%d", readyMnodes, updatingMnodes);
return;
}
if (cfg.myIndex == -1) {
2022-10-18 04:31:15 +00:00
#if 1
mInfo("vgId:1, mnode sync not reconfig since selfIndex is -1");
2022-10-18 04:31:15 +00:00
#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
return;
}
if (updatingMnodes > 0) {
mInfo("vgId:1, mnode sync reconfig, replica:%d myIndex:%d", cfg.replicaNum, cfg.myIndex);
for (int32_t i = 0; i < cfg.replicaNum; ++i) {
SNodeInfo *pNode = &cfg.nodeInfo[i];
mInfo("vgId:1, index:%d, ep:%s:%u dnode:%d cluster:%" PRId64, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId,
pNode->clusterId);
}
int32_t code = syncReconfig(pMnode->syncMgmt.sync, &cfg);
if (code != 0) {
2023-01-04 03:48:30 +00:00
mError("vgId:1, mnode sync reconfig failed since %s", terrstr());
} else {
2023-01-04 03:48:30 +00:00
mInfo("vgId:1, mnode sync reconfig success");
}
2022-05-25 09:44:43 +00:00
}
}