diff --git a/include/common/tmsgcb.h b/include/common/tmsgcb.h index 32d00bb422..a1ebd855cd 100644 --- a/include/common/tmsgcb.h +++ b/include/common/tmsgcb.h @@ -39,6 +39,7 @@ typedef enum { QUEUE_MAX, } EQueueType; +typedef int32_t (*UpdateDnodeInfoFp)(void* pData, int32_t* dnodeId, int64_t* clusterId, char* fqdn, uint16_t* port); typedef int32_t (*PutToQueueFp)(void* pMgmt, EQueueType qtype, SRpcMsg* pMsg); typedef int32_t (*GetQueueSizeFp)(void* pMgmt, int32_t vgId, EQueueType qtype); typedef int32_t (*SendReqFp)(const SEpSet* pEpSet, SRpcMsg* pMsg); @@ -48,6 +49,7 @@ typedef void (*ReleaseHandleFp)(SRpcHandleInfo* pHandle, int8_t type); typedef void (*ReportStartup)(const char* name, const char* desc); typedef struct { + void* data; void* mgmt; void* clientRpc; PutToQueueFp putToQueueFp; @@ -57,6 +59,7 @@ typedef struct { RegisterBrokenLinkArgFp registerBrokenLinkArgFp; ReleaseHandleFp releaseHandleFp; ReportStartup reportStartupFp; + UpdateDnodeInfoFp updateDnodeInfoFp; } SMsgCb; void tmsgSetDefault(const SMsgCb* msgcb); @@ -67,6 +70,7 @@ void tmsgSendRsp(SRpcMsg* pMsg); void tmsgRegisterBrokenLinkArg(SRpcMsg* pMsg); void tmsgReleaseHandle(SRpcHandleInfo* pHandle, int8_t type); void tmsgReportStartup(const char* name, const char* desc); +int32_t tmsgUpdateDnodeInfo(int32_t* dnodeId, int64_t* clusterId, char* fqdn, uint16_t* port); #ifdef __cplusplus } diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index 559dc1009d..d37f8f76c2 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -78,6 +78,8 @@ typedef enum { } ESyncState; typedef struct SNodeInfo { + int64_t clusterId; + int32_t nodeId; uint16_t nodePort; char nodeFqdn[TSDB_FQDN_LEN]; } SNodeInfo; diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c index c5ddb9f021..f736ffd0c8 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c @@ -180,6 +180,6 @@ int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) { return -1; } - dDebug("successed to write %s, deployed:%d", realfile, pOption->deploy); + dDebug("succeed to write %s, deployed:%d", realfile, pOption->deploy); return 0; } diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c index a49e855e39..188e854cb2 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c @@ -213,6 +213,6 @@ _OVER: if (code != 0) return -1; - dDebug("successed to write %s, numOfVnodes:%d", realfile, numOfVnodes); + dDebug("succeed to write %s, numOfVnodes:%d", realfile, numOfVnodes); return taosRenameFile(file, realfile); } \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index bc46772858..61ed1d83b7 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -134,8 +134,10 @@ static void vmGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) { memset(&pCfg->syncCfg.nodeInfo, 0, sizeof(pCfg->syncCfg.nodeInfo)); for (int i = 0; i < pCreate->replica; ++i) { SNodeInfo *pNode = &pCfg->syncCfg.nodeInfo[i]; + pNode->nodeId = pCreate->replicas[i].id; pNode->nodePort = pCreate->replicas[i].port; - tstrncpy(pNode->nodeFqdn, pCreate->replicas[i].fqdn, sizeof(pNode->nodeFqdn)); + tstrncpy(pNode->nodeFqdn, pCreate->replicas[i].fqdn, TSDB_FQDN_LEN); + (void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort); } } @@ -188,8 +190,8 @@ int32_t vmProcessCreateVnodeReq(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { req.walRollPeriod, req.walSegmentSize, req.hashMethod, req.hashBegin, req.hashEnd, req.hashPrefix, req.hashSuffix, req.replica, req.selfIndex, req.strict); for (int32_t i = 0; i < req.replica; ++i) { - dInfo("vgId:%d, replica:%d id:%d fqdn:%s port:%u", req.vgId, i, req.replicas[i].id, req.replicas[i].fqdn, - req.replicas[i].port); + dInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", req.vgId, i, req.replicas[i].fqdn, req.replicas[i].port, + req.replicas[i].id); } SReplica *pReplica = &req.replicas[req.selfIndex]; diff --git a/source/dnode/mgmt/node_mgmt/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c index 0ff41d429e..dcb63f6524 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -345,6 +345,8 @@ SMsgCb dmGetMsgcb(SDnode *pDnode) { .registerBrokenLinkArgFp = dmRegisterBrokenLinkArg, .releaseHandleFp = dmReleaseHandle, .reportStartupFp = dmReportStartup, + .updateDnodeInfoFp = dmUpdateDnodeInfo, + .data = &pDnode->data, }; return msgCb; } diff --git a/source/dnode/mgmt/node_util/inc/dmUtil.h b/source/dnode/mgmt/node_util/inc/dmUtil.h index 2124b387ec..92b66230e3 100644 --- a/source/dnode/mgmt/node_util/inc/dmUtil.h +++ b/source/dnode/mgmt/node_util/inc/dmUtil.h @@ -167,6 +167,7 @@ void dmUpdateEps(SDnodeData *pData, SArray *pDnodeEps); void dmGetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet); void dmGetMnodeEpSetForRedirect(SDnodeData *pData, SRpcMsg *pMsg, SEpSet *pEpSet); void dmSetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet); +int32_t dmUpdateDnodeInfo(void *pData, int32_t *dnodeId, int64_t *clusterId, char *fqdn, uint16_t *port); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/node_util/src/dmEps.c b/source/dnode/mgmt/node_util/src/dmEps.c index 2ced9a350d..84689ead33 100644 --- a/source/dnode/mgmt/node_util/src/dmEps.c +++ b/source/dnode/mgmt/node_util/src/dmEps.c @@ -182,22 +182,25 @@ _OVER: } int32_t dmWriteEps(SDnodeData *pData) { + int32_t code = -1; + char *content = NULL; + TdFilePtr pFile = NULL; + char file[PATH_MAX] = {0}; char realfile[PATH_MAX] = {0}; - snprintf(file, sizeof(file), "%s%sdnode%sdnode.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP); snprintf(realfile, sizeof(realfile), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP); - TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); + pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { - dError("failed to write %s since %s", file, strerror(errno)); + dError("failed to open %s since %s", file, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + goto _OVER; } int32_t len = 0; int32_t maxLen = 256 * 1024; - char *content = taosMemoryCalloc(1, maxLen + 1); + content = taosMemoryCalloc(1, maxLen + 1); len += snprintf(content + len, maxLen - len, "{\n"); len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", pData->dnodeId); @@ -221,20 +224,39 @@ int32_t dmWriteEps(SDnodeData *pData) { } len += snprintf(content + len, maxLen - len, "}\n"); - taosWriteFile(pFile, content, len); - taosFsyncFile(pFile); + if (taosWriteFile(pFile, content, len) != len) { + dError("failed to write %s since %s", file, strerror(errno)); + terrno = TAOS_SYSTEM_ERROR(errno); + goto _OVER; + } + + if (taosFsyncFile(pFile) < 0) { + dError("failed to fsync %s since %s", file, strerror(errno)); + terrno = TAOS_SYSTEM_ERROR(errno); + goto _OVER; + } + taosCloseFile(&pFile); - taosMemoryFree(content); + taosMemoryFreeClear(content); if (taosRenameFile(file, realfile) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to rename %s since %s", file, terrstr()); - return -1; + goto _OVER; } + code = 0; pData->updateTime = taosGetTimestampMs(); - dDebug("successed to write %s, dnodeVer:%" PRId64, realfile, pData->dnodeVer); - return 0; + dInfo("succeed to write %s, dnodeVer:%" PRId64, realfile, pData->dnodeVer); + +_OVER: + if (content != NULL) taosMemoryFreeClear(content); + if (pFile != NULL) taosCloseFile(&pFile); + if (code != 0) { + dError("failed to write file %s since %s", realfile, terrstr()); + } + + return code; } void dmUpdateEps(SDnodeData *pData, SArray *eps) { @@ -332,3 +354,41 @@ void dmSetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) { dInfo("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port); } } + +int32_t dmUpdateDnodeInfo(void *data, int32_t *dnodeId, int64_t *clusterId, char *fqdn, uint16_t *port) { + SDnodeData *pData = data; + int32_t ret = -1; + taosThreadRwlockRdlock(&pData->lock); + if (*dnodeId <= 0) { + for (int32_t i = 0; i < (int32_t)taosArrayGetSize(pData->dnodeEps); ++i) { + SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i); + if (strcmp(pDnodeEp->ep.fqdn, fqdn) == 0 && pDnodeEp->ep.port == *port) { + dInfo("dnode:%s:%u, update dnodeId from %d to %d", fqdn, port, *dnodeId, pDnodeEp->id); + *dnodeId = pDnodeEp->id; + *clusterId = pData->clusterId; + ret = 0; + } + } + if (ret != 0) { + dInfo("dnode:%s:%u, failed to update dnodeId:%d", fqdn, port, *dnodeId); + } + } else { + SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, dnodeId, sizeof(int32_t)); + if (pDnodeEp) { + if (strcmp(pDnodeEp->ep.fqdn, fqdn) != 0) { + dInfo("dnode:%d, update port from %s to %s", *dnodeId, fqdn, pDnodeEp->ep.fqdn); + tstrncpy(fqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN); + } + if (pDnodeEp->ep.port != *port) { + dInfo("dnode:%d, update port from %u to %u", *dnodeId, *port, pDnodeEp->ep.port); + *port = pDnodeEp->ep.port; + } + *clusterId = pData->clusterId; + ret = 0; + } else { + dInfo("dnode:%d, failed to update dnode info", *dnodeId); + } + } + taosThreadRwlockUnlock(&pData->lock); + return ret; +} \ No newline at end of file diff --git a/source/dnode/mgmt/node_util/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c index d387fe4a3f..2eb1462efc 100644 --- a/source/dnode/mgmt/node_util/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -105,7 +105,7 @@ int32_t dmWriteFile(const char *path, const char *name, bool deployed) { return -1; } - dInfo("successed to write %s, deployed:%d", realfile, deployed); + dInfo("succeed to write %s, deployed:%d", realfile, deployed); code = 0; _OVER: diff --git a/source/dnode/mnode/impl/src/mndMnode.c b/source/dnode/mnode/impl/src/mndMnode.c index b9448797ea..37fc9f79e1 100644 --- a/source/dnode/mnode/impl/src/mndMnode.c +++ b/source/dnode/mnode/impl/src/mndMnode.c @@ -21,6 +21,7 @@ #include "mndSync.h" #include "mndTrans.h" #include "tmisce.h" +#include "mndCluster.h" #define MNODE_VER_NUMBER 1 #define MNODE_RESERVE_SIZE 64 @@ -743,8 +744,12 @@ static void mndReloadSyncConfig(SMnode *pMnode) { if (objStatus == SDB_STATUS_READY || objStatus == SDB_STATUS_CREATING) { SNodeInfo *pNode = &cfg.nodeInfo[cfg.replicaNum]; - tstrncpy(pNode->nodeFqdn, pObj->pDnode->fqdn, sizeof(pNode->nodeFqdn)); + 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; } @@ -775,7 +780,8 @@ static void mndReloadSyncConfig(SMnode *pMnode) { 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, fqdn:%s port:%d", i, pNode->nodeFqdn, pNode->nodePort); + 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); diff --git a/source/dnode/mnode/impl/src/mndSync.c b/source/dnode/mnode/impl/src/mndSync.c index 54d8aa7f60..5d66b0d0a2 100644 --- a/source/dnode/mnode/impl/src/mndSync.c +++ b/source/dnode/mnode/impl/src/mndSync.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "mndSync.h" +#include "mndCluster.h" #include "mndTrans.h" static int32_t mndSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) { @@ -297,9 +298,12 @@ int32_t mndInitSync(SMnode *pMnode) { pCfg->myIndex = pMgmt->selfIndex; for (int32_t i = 0; i < pMgmt->numOfReplicas; ++i) { SNodeInfo *pNode = &pCfg->nodeInfo[i]; - tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn)); + pNode->nodeId = pMgmt->replicas[i].id; pNode->nodePort = pMgmt->replicas[i].port; - mInfo("vgId:1, index:%d ep:%s:%u", i, pNode->nodeFqdn, pNode->nodePort); + tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn)); + (void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort); + mInfo("vgId:1, index:%d ep:%s:%u dnode:%d cluster:" PRId64, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId, + pNode->clusterId); } tsem_init(&pMgmt->syncSem, 0, 0); diff --git a/source/dnode/vnode/src/vnd/vnodeCfg.c b/source/dnode/vnode/src/vnd/vnodeCfg.c index 782cc69d30..125db8dd92 100644 --- a/source/dnode/vnode/src/vnd/vnodeCfg.c +++ b/source/dnode/vnode/src/vnd/vnodeCfg.c @@ -125,13 +125,17 @@ int vnodeEncodeConfig(const void *pObj, SJson *pJson) { if (tjsonAddIntegerToObject(pJson, "vndStats.timeseries", pCfg->vndStats.numOfTimeSeries) < 0) return -1; if (tjsonAddIntegerToObject(pJson, "vndStats.ntimeseries", pCfg->vndStats.numOfNTimeSeries) < 0) return -1; - SJson *pNodeInfoArr = tjsonCreateArray(); - tjsonAddItemToObject(pJson, "syncCfg.nodeInfo", pNodeInfoArr); + SJson *nodeInfo = tjsonCreateArray(); + if (nodeInfo == NULL) return -1; + if (tjsonAddItemToObject(pJson, "syncCfg.nodeInfo", nodeInfo) < 0) return -1; for (int i = 0; i < pCfg->syncCfg.replicaNum; ++i) { - SJson *pNodeInfo = tjsonCreateObject(); - tjsonAddIntegerToObject(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort); - tjsonAddStringToObject(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); - tjsonAddItemToArray(pNodeInfoArr, pNodeInfo); + SJson *info = tjsonCreateObject(); + if (info == NULL) return -1; + if (tjsonAddIntegerToObject(info, "nodePort", pCfg->syncCfg.nodeInfo[i].nodePort) < 0) return -1; + if (tjsonAddStringToObject(info, "nodeFqdn", pCfg->syncCfg.nodeInfo[i].nodeFqdn) < 0) return -1; + if (tjsonAddIntegerToObject(info, "nodeId", pCfg->syncCfg.nodeInfo[i].nodeId) < 0) return -1; + if (tjsonAddIntegerToObject(info, "clusterId", pCfg->syncCfg.nodeInfo[i].clusterId) < 0) return -1; + if (tjsonAddItemToArray(nodeInfo, info) < 0) return -1; } return 0; @@ -240,15 +244,19 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) { tjsonGetNumberValue(pJson, "vndStats.ntimeseries", pCfg->vndStats.numOfNTimeSeries, code); if (code < 0) return -1; - SJson *pNodeInfoArr = tjsonGetObjectItem(pJson, "syncCfg.nodeInfo"); - int arraySize = tjsonGetArraySize(pNodeInfoArr); - assert(arraySize == pCfg->syncCfg.replicaNum); + SJson *nodeInfo = tjsonGetObjectItem(pJson, "syncCfg.nodeInfo"); + int arraySize = tjsonGetArraySize(nodeInfo); + if (arraySize != pCfg->syncCfg.replicaNum) return -1; for (int i = 0; i < arraySize; ++i) { - SJson *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i); - assert(pNodeInfo != NULL); - tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort, code); - tjsonGetStringValue(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); + SJson *info = tjsonGetArrayItem(nodeInfo, i); + if (info == NULL) return -1; + tjsonGetNumberValue(info, "nodePort", pCfg->syncCfg.nodeInfo[i].nodePort, code); + if (code < 0) return -1; + tjsonGetStringValue(info, "nodeFqdn", pCfg->syncCfg.nodeInfo[i].nodeFqdn); + if (code < 0) return -1; + tjsonGetNumberValue(info, "nodeId", pCfg->syncCfg.nodeInfo[i].nodeId, code); + tjsonGetNumberValue(info, "clusterId", pCfg->syncCfg.nodeInfo[i].clusterId, code); } tjsonGetNumberValue(pJson, "tsdbPageSize", pCfg->tsdbPageSize, code); diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index e09fafb756..9cfcbda890 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -82,8 +82,10 @@ int32_t vnodeAlter(const char *path, SAlterVnodeReplicaReq *pReq, STfs *pTfs) { vInfo("vgId:%d, save config, replicas:%d selfIndex:%d", pReq->vgId, pCfg->replicaNum, pCfg->myIndex); for (int i = 0; i < pReq->replica; ++i) { SNodeInfo *pNode = &pCfg->nodeInfo[i]; + pNode->nodeId = pReq->replicas[i].id; pNode->nodePort = pReq->replicas[i].port; tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn)); + (void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort); vInfo("vgId:%d, save config, replica:%d ep:%s:%u", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort); } diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index 5697487743..749c81224c 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -578,7 +578,8 @@ int32_t vnodeSyncOpen(SVnode *pVnode, char *path) { vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex); for (int32_t i = 0; i < pCfg->replicaNum; ++i) { SNodeInfo *pNode = &pCfg->nodeInfo[i]; - vInfo("vgId:%d, index:%d ep:%s:%u", pVnode->config.vgId, i, pNode->nodeFqdn, pNode->nodePort); + vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn, pNode->nodePort, + pNode->nodeId, pNode->clusterId); } pVnode->sync = syncOpen(&syncInfo); diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index b5227152df..3bf4a8d1cd 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -53,6 +53,18 @@ typedef struct SyncPreSnapshot SyncPreSnapshot; typedef struct SSyncLogBuffer SSyncLogBuffer; typedef struct SSyncLogReplMgr SSyncLogReplMgr; +#define MAX_CONFIG_INDEX_COUNT 256 + +typedef struct SRaftCfg { + SSyncCfg cfg; + int32_t batchSize; + int8_t isStandBy; + int8_t snapshotStrategy; + SyncIndex lastConfigIndex; + int32_t configIndexCount; + SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT]; +} SRaftCfg; + typedef struct SRaftId { SyncNodeId addr; SyncGroupId vgId; @@ -93,7 +105,7 @@ typedef struct SPeerState { typedef struct SSyncNode { // init by SSyncInfo SyncGroupId vgId; - SRaftCfg* pRaftCfg; + SRaftCfg raftCfg; char path[TSDB_FILENAME_LEN]; char raftStorePath[TSDB_FILENAME_LEN * 2]; char configPath[TSDB_FILENAME_LEN * 2]; @@ -112,6 +124,7 @@ typedef struct SSyncNode { int32_t peersNum; SNodeInfo peersNodeInfo[TSDB_MAX_REPLICA]; + SEpSet peersEpset[TSDB_MAX_REPLICA]; SRaftId peersId[TSDB_MAX_REPLICA]; int32_t replicaNum; @@ -245,7 +258,6 @@ int32_t syncNodeRestartHeartbeatTimer(SSyncNode* pSyncNode); // utils -------------- int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg); -int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg); SyncIndex syncMinMatchIndex(SSyncNode* pSyncNode); int32_t syncCacheEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry, LRUHandle** h); bool syncNodeHeartbeatReplyTimeout(SSyncNode* pSyncNode); diff --git a/source/libs/sync/inc/syncRaftCfg.h b/source/libs/sync/inc/syncRaftCfg.h index 823983e732..4f03a60fbc 100644 --- a/source/libs/sync/inc/syncRaftCfg.h +++ b/source/libs/sync/inc/syncRaftCfg.h @@ -22,64 +22,9 @@ extern "C" { #include "syncInt.h" -#define CONFIG_FILE_LEN 2048 -#define MAX_CONFIG_INDEX_COUNT 256 - -typedef struct SRaftCfgIndex { - TdFilePtr pFile; - char path[TSDB_FILENAME_LEN * 2]; - - SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT]; - int32_t configIndexCount; -} SRaftCfgIndex; - -SRaftCfgIndex *raftCfgIndexOpen(const char *path); -int32_t raftCfgIndexClose(SRaftCfgIndex *pRaftCfgIndex); -int32_t raftCfgIndexPersist(SRaftCfgIndex *pRaftCfgIndex); -int32_t raftCfgIndexAddConfigIndex(SRaftCfgIndex *pRaftCfgIndex, SyncIndex configIndex); - -cJSON *raftCfgIndex2Json(SRaftCfgIndex *pRaftCfgIndex); -char *raftCfgIndex2Str(SRaftCfgIndex *pRaftCfgIndex); -int32_t raftCfgIndexFromJson(const cJSON *pRoot, SRaftCfgIndex *pRaftCfgIndex); -int32_t raftCfgIndexFromStr(const char *s, SRaftCfgIndex *pRaftCfgIndex); -int32_t raftCfgIndexCreateFile(const char *path); - -typedef struct SRaftCfg { - SSyncCfg cfg; - TdFilePtr pFile; - char path[TSDB_FILENAME_LEN * 2]; - int8_t isStandBy; - int32_t batchSize; - int8_t snapshotStrategy; - SyncIndex lastConfigIndex; - - SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT]; - int32_t configIndexCount; - -} SRaftCfg; - -SRaftCfg *raftCfgOpen(const char *path); -int32_t raftCfgClose(SRaftCfg *pRaftCfg); -int32_t raftCfgPersist(SRaftCfg *pRaftCfg); -int32_t raftCfgAddConfigIndex(SRaftCfg *pRaftCfg, SyncIndex configIndex); - -void syncCfg2SimpleStr(const SSyncCfg *pCfg, char *str, int32_t bufLen); -cJSON *syncCfg2Json(SSyncCfg *pSyncCfg); -int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg); - -cJSON *raftCfg2Json(SRaftCfg *pRaftCfg); -char *raftCfg2Str(SRaftCfg *pRaftCfg); -int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg); -int32_t raftCfgFromStr(const char *s, SRaftCfg *pRaftCfg); - -typedef struct SRaftCfgMeta { - int8_t isStandBy; - int32_t batchSize; - int8_t snapshotStrategy; - SyncIndex lastConfigIndex; -} SRaftCfgMeta; - -int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path); +int32_t syncWriteCfgFile(SSyncNode *pNode); +int32_t syncReadCfgFile(SSyncNode *pNode); +int32_t syncAddCfgIndex(SSyncNode *pNode, SyncIndex cfgIndex); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncUtil.h b/source/libs/sync/inc/syncUtil.h index 7d08585656..39c679a2ad 100644 --- a/source/libs/sync/inc/syncUtil.h +++ b/source/libs/sync/inc/syncUtil.h @@ -62,22 +62,19 @@ extern "C" { // clang-format on -uint64_t syncUtilAddr2U64(const char* host, uint16_t port); -void syncUtilU642Addr(uint64_t u64, char* host, int64_t len, uint16_t* port); -void syncUtilNodeInfo2EpSet(const SNodeInfo* pInfo, SEpSet* pEpSet); -void syncUtilRaftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet); -bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* raftId); -bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2); -bool syncUtilEmptyId(const SRaftId* pId); +#define CID(pRaftId) (int32_t)(((pRaftId)->addr) >> 32) +#define DID(pRaftId) (int32_t)((pRaftId)->addr) +#define SYNC_ADDR(pInfo) (int64_t)(((pInfo)->clusterId << 32) | (pInfo)->nodeId) + +void syncUtilNodeInfo2EpSet(const SNodeInfo* pInfo, SEpSet* pEpSet); +bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* raftId); +bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2); +bool syncUtilEmptyId(const SRaftId* pId); int32_t syncUtilElectRandomMS(int32_t min, int32_t max); int32_t syncUtilQuorum(int32_t replicaNum); -cJSON* syncUtilRaftId2Json(const SRaftId* p); const char* syncStr(ESyncState state); -char* syncUtilPrintBin(char* ptr, uint32_t len); -char* syncUtilPrintBin2(char* ptr, uint32_t len); void syncUtilMsgHtoN(void* msg); -void syncUtilMsgNtoH(void* msg); bool syncUtilUserPreCommit(tmsg_t msgType); bool syncUtilUserRollback(tmsg_t msgType); diff --git a/source/libs/sync/src/syncElection.c b/source/libs/sync/src/syncElection.c index 8d548114fb..bcc95c5f10 100644 --- a/source/libs/sync/src/syncElection.c +++ b/source/libs/sync/src/syncElection.c @@ -94,7 +94,7 @@ int32_t syncNodeElect(SSyncNode* pSyncNode) { voteGrantedUpdate(pSyncNode->pVotesGranted, pSyncNode); votesRespondUpdate(pSyncNode->pVotesRespond, pSyncNode); - pSyncNode->quorum = syncUtilQuorum(pSyncNode->pRaftCfg->cfg.replicaNum); + pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum); syncNodeCandidate2Leader(pSyncNode); pSyncNode->pVotesGranted->toLeader = true; diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 28ae8711a8..0d6d0f93f1 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -115,7 +115,7 @@ void syncHbTimerDataRemove(int64_t rid) { taosRemoveRef(gHbDataRefId, rid); } SSyncHbTimerData *syncHbTimerDataAcquire(int64_t rid) { SSyncHbTimerData *pData = taosAcquireRef(gHbDataRefId, rid); if (pData == NULL) { - sError("failed to acquire hb-timer-data from refId:%" PRId64, rid); + sInfo("failed to acquire hb-timer-data from refId:%" PRId64, rid); terrno = TSDB_CODE_SYN_INTERNAL_ERROR; } diff --git a/source/libs/sync/src/syncIndexMgr.c b/source/libs/sync/src/syncIndexMgr.c index 0950ab7372..7ecb9d7782 100644 --- a/source/libs/sync/src/syncIndexMgr.c +++ b/source/libs/sync/src/syncIndexMgr.c @@ -64,10 +64,8 @@ void syncIndexMgrSetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, Sync } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr set index:%" PRId64 " for %s:%d failed", pIndexMgr->pNode->vgId, index, host, port); + sError("vgId:%d, indexmgr set index:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, index, + DID(pRaftId), CID(pRaftId)); } SSyncLogReplMgr *syncNodeGetLogReplMgr(SSyncNode *pNode, SRaftId *pRaftId) { @@ -77,10 +75,7 @@ SSyncLogReplMgr *syncNodeGetLogReplMgr(SSyncNode *pNode, SRaftId *pRaftId) { } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr get replmgr from %s:%d failed", pNode->vgId, host, port); + sError("vgId:%d, indexmgr get replmgr from dnode:%d cluster:%d failed", pNode->vgId, DID(pRaftId), CID(pRaftId)); return NULL; } @@ -92,10 +87,8 @@ SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr get index from %s:%d failed", pIndexMgr->pNode->vgId, host, port); + sError("vgId:%d, indexmgr get index from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId), + CID(pRaftId)); return SYNC_INDEX_INVALID; } @@ -107,11 +100,8 @@ void syncIndexMgrSetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr set start-time:%" PRId64 " for %s:%d failed", pIndexMgr->pNode->vgId, startTime, host, - port); + sError("vgId:%d, indexmgr set start-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, + startTime, DID(pRaftId), CID(pRaftId)); } int64_t syncIndexMgrGetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) { @@ -122,10 +112,8 @@ int64_t syncIndexMgrGetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftI } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr get start-time from %s:%d failed", pIndexMgr->pNode->vgId, host, port); + sError("vgId:%d, indexmgr get start-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId), + CID(pRaftId)); return -1; } @@ -137,10 +125,8 @@ void syncIndexMgrSetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, i } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr set recv-time:%" PRId64 " for %s:%d failed", pIndexMgr->pNode->vgId, recvTime, host, port); + sError("vgId:%d, indexmgr set recv-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, recvTime, + DID(pRaftId), CID(pRaftId)); } int64_t syncIndexMgrGetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) { @@ -151,10 +137,8 @@ int64_t syncIndexMgrGetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr get recv-time from %s:%d failed", pIndexMgr->pNode->vgId, host, port); + sError("vgId:%d, indexmgr get recv-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId), + CID(pRaftId)); return -1; } @@ -166,10 +150,8 @@ void syncIndexMgrSetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncT } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr set term:%" PRId64 " for %s:%d failed", pIndexMgr->pNode->vgId, term, host, port); + sError("vgId:%d, indexmgr set term:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, term, + DID(pRaftId), CID(pRaftId)); } SyncTerm syncIndexMgrGetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) { @@ -180,9 +162,7 @@ SyncTerm syncIndexMgrGetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) { } } - char host[128]; - uint16_t port; - syncUtilU642Addr(pRaftId->addr, host, sizeof(host), &port); - sError("vgId:%d, indexmgr get term from %s:%d failed", pIndexMgr->pNode->vgId, host, port); + sError("vgId:%d, indexmgr get term from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId), + CID(pRaftId)); return -1; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 1a481a7e14..8f64d9b717 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -306,15 +306,10 @@ int32_t syncBeginSnapshot(int64_t rid, int64_t lastApplyIndex) { for (int32_t i = 0; i < pSyncNode->peersNum; ++i) { int64_t matchIndex = syncIndexMgrGetIndex(pSyncNode->pMatchIndex, &(pSyncNode->peersId[i])); if (lastApplyIndex > matchIndex) { - do { - char host[64]; - uint16_t port; - syncUtilU642Addr(pSyncNode->peersId[i].addr, host, sizeof(host), &port); - sNTrace(pSyncNode, - "new-snapshot-index:%" PRId64 " is greater than match-index:%" PRId64 - " of %s:%d, do not delete wal", - lastApplyIndex, matchIndex, host, port); - } while (0); + sNTrace(pSyncNode, + "new-snapshot-index:%" PRId64 " is greater than match-index:%" PRId64 + " of dnode:%d, do not delete wal", + lastApplyIndex, matchIndex, DID(&pSyncNode->peersId[i])); syncNodeRelease(pSyncNode); return 0; @@ -556,7 +551,7 @@ int32_t syncNodeLeaderTransferTo(SSyncNode* pSyncNode, SNodeInfo newLeader) { (void)syncBuildLeaderTransfer(&rpcMsg, pSyncNode->vgId); SyncLeaderTransfer* pMsg = rpcMsg.pCont; - pMsg->newLeaderId.addr = syncUtilAddr2U64(newLeader.nodeFqdn, newLeader.nodePort); + pMsg->newLeaderId.addr = SYNC_ADDR(&newLeader); pMsg->newLeaderId.vgId = pSyncNode->vgId; pMsg->newNodeInfo = newLeader; @@ -622,9 +617,9 @@ int32_t syncGetSnapshotMeta(int64_t rid, struct SSnapshotMeta* sMeta) { return -1; } ASSERT(rid == pSyncNode->rid); - sMeta->lastConfigIndex = pSyncNode->pRaftCfg->lastConfigIndex; + sMeta->lastConfigIndex = pSyncNode->raftCfg.lastConfigIndex; - sTrace("vgId:%d, get snapshot meta, lastConfigIndex:%" PRId64, pSyncNode->vgId, pSyncNode->pRaftCfg->lastConfigIndex); + sTrace("vgId:%d, get snapshot meta, lastConfigIndex:%" PRId64, pSyncNode->vgId, pSyncNode->raftCfg.lastConfigIndex); syncNodeRelease(pSyncNode); return 0; @@ -637,13 +632,13 @@ int32_t syncGetSnapshotMetaByIndex(int64_t rid, SyncIndex snapshotIndex, struct } ASSERT(rid == pSyncNode->rid); - ASSERT(pSyncNode->pRaftCfg->configIndexCount >= 1); - SyncIndex lastIndex = (pSyncNode->pRaftCfg->configIndexArr)[0]; + ASSERT(pSyncNode->raftCfg.configIndexCount >= 1); + SyncIndex lastIndex = (pSyncNode->raftCfg.configIndexArr)[0]; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->configIndexCount; ++i) { - if ((pSyncNode->pRaftCfg->configIndexArr)[i] > lastIndex && - (pSyncNode->pRaftCfg->configIndexArr)[i] <= snapshotIndex) { - lastIndex = (pSyncNode->pRaftCfg->configIndexArr)[i]; + for (int32_t i = 0; i < pSyncNode->raftCfg.configIndexCount; ++i) { + if ((pSyncNode->raftCfg.configIndexArr)[i] > lastIndex && + (pSyncNode->raftCfg.configIndexArr)[i] <= snapshotIndex) { + lastIndex = (pSyncNode->raftCfg.configIndexArr)[i]; } } sMeta->lastConfigIndex = lastIndex; @@ -656,13 +651,13 @@ int32_t syncGetSnapshotMetaByIndex(int64_t rid, SyncIndex snapshotIndex, struct #endif SyncIndex syncNodeGetSnapshotConfigIndex(SSyncNode* pSyncNode, SyncIndex snapshotLastApplyIndex) { - ASSERT(pSyncNode->pRaftCfg->configIndexCount >= 1); - SyncIndex lastIndex = (pSyncNode->pRaftCfg->configIndexArr)[0]; + ASSERT(pSyncNode->raftCfg.configIndexCount >= 1); + SyncIndex lastIndex = (pSyncNode->raftCfg.configIndexArr)[0]; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->configIndexCount; ++i) { - if ((pSyncNode->pRaftCfg->configIndexArr)[i] > lastIndex && - (pSyncNode->pRaftCfg->configIndexArr)[i] <= snapshotLastApplyIndex) { - lastIndex = (pSyncNode->pRaftCfg->configIndexArr)[i]; + for (int32_t i = 0; i < pSyncNode->raftCfg.configIndexCount; ++i) { + if ((pSyncNode->raftCfg.configIndexArr)[i] > lastIndex && + (pSyncNode->raftCfg.configIndexArr)[i] <= snapshotLastApplyIndex) { + lastIndex = (pSyncNode->raftCfg.configIndexArr)[i]; } } sTrace("vgId:%d, sync get last config index, index:%" PRId64 " lcindex:%" PRId64, pSyncNode->vgId, @@ -677,15 +672,15 @@ void syncGetRetryEpSet(int64_t rid, SEpSet* pEpSet) { SSyncNode* pSyncNode = syncNodeAcquire(rid); if (pSyncNode == NULL) return; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { SEp* pEp = &pEpSet->eps[i]; - tstrncpy(pEp->fqdn, pSyncNode->pRaftCfg->cfg.nodeInfo[i].nodeFqdn, TSDB_FQDN_LEN); - pEp->port = (pSyncNode->pRaftCfg->cfg.nodeInfo)[i].nodePort; + tstrncpy(pEp->fqdn, pSyncNode->raftCfg.cfg.nodeInfo[i].nodeFqdn, TSDB_FQDN_LEN); + pEp->port = (pSyncNode->raftCfg.cfg.nodeInfo)[i].nodePort; pEpSet->numOfEps++; sDebug("vgId:%d, sync get retry epset, index:%d %s:%d", pSyncNode->vgId, i, pEp->fqdn, pEp->port); } if (pEpSet->numOfEps > 0) { - pEpSet->inUse = (pSyncNode->pRaftCfg->cfg.myIndex + 1) % pEpSet->numOfEps; + pEpSet->inUse = (pSyncNode->raftCfg.cfg.myIndex + 1) % pEpSet->numOfEps; } sInfo("vgId:%d, sync get retry epset numOfEps:%d inUse:%d", pSyncNode->vgId, pEpSet->numOfEps, pEpSet->inUse); @@ -849,44 +844,43 @@ SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo) { } } + memcpy(pSyncNode->path, pSyncInfo->path, sizeof(pSyncNode->path)); + snprintf(pSyncNode->raftStorePath, sizeof(pSyncNode->raftStorePath), "%s%sraft_store.json", pSyncInfo->path, + TD_DIRSEP); snprintf(pSyncNode->configPath, sizeof(pSyncNode->configPath), "%s%sraft_config.json", pSyncInfo->path, TD_DIRSEP); + if (!taosCheckExistFile(pSyncNode->configPath)) { // create a new raft config file - SRaftCfgMeta meta = {0}; - meta.isStandBy = pSyncInfo->isStandBy; - meta.snapshotStrategy = pSyncInfo->snapshotStrategy; - meta.lastConfigIndex = SYNC_INDEX_INVALID; - meta.batchSize = pSyncInfo->batchSize; - if (raftCfgCreateFile(&pSyncInfo->syncCfg, meta, pSyncNode->configPath) != 0) { - sError("vgId:%d, failed to create raft cfg file at %s", pSyncNode->vgId, pSyncNode->configPath); + pSyncNode->raftCfg.isStandBy = pSyncInfo->isStandBy; + pSyncNode->raftCfg.snapshotStrategy = pSyncInfo->snapshotStrategy; + pSyncNode->raftCfg.lastConfigIndex = SYNC_INDEX_INVALID; + pSyncNode->raftCfg.batchSize = pSyncInfo->batchSize; + pSyncNode->raftCfg.cfg = pSyncInfo->syncCfg; + pSyncNode->raftCfg.configIndexCount = 1; + pSyncNode->raftCfg.configIndexArr[0] = -1; + + if (syncWriteCfgFile(pSyncNode) != 0) { + sError("vgId:%d, failed to create sync cfg file", pSyncNode->vgId); goto _error; } - if (pSyncInfo->syncCfg.replicaNum == 0) { - sInfo("vgId:%d, sync config not input", pSyncNode->vgId); - pSyncInfo->syncCfg = pSyncNode->pRaftCfg->cfg; - } } else { // update syncCfg by raft_config.json - pSyncNode->pRaftCfg = raftCfgOpen(pSyncNode->configPath); - if (pSyncNode->pRaftCfg == NULL) { - sError("vgId:%d, failed to open raft cfg file at %s", pSyncNode->vgId, pSyncNode->configPath); + if (syncReadCfgFile(pSyncNode) != 0) { + sError("vgId:%d, failed to read sync cfg file", pSyncNode->vgId); goto _error; } - if (pSyncInfo->syncCfg.replicaNum > 0 && syncIsConfigChanged(&pSyncNode->pRaftCfg->cfg, &pSyncInfo->syncCfg)) { + if (pSyncInfo->syncCfg.replicaNum > 0 && syncIsConfigChanged(&pSyncNode->raftCfg.cfg, &pSyncInfo->syncCfg)) { sInfo("vgId:%d, use sync config from input options and write to cfg file", pSyncNode->vgId); - pSyncNode->pRaftCfg->cfg = pSyncInfo->syncCfg; - if (raftCfgPersist(pSyncNode->pRaftCfg) != 0) { - sError("vgId:%d, failed to persist raft cfg file at %s", pSyncNode->vgId, pSyncNode->configPath); + pSyncNode->raftCfg.cfg = pSyncInfo->syncCfg; + if (syncWriteCfgFile(pSyncNode) != 0) { + sError("vgId:%d, failed to write sync cfg file", pSyncNode->vgId); goto _error; } } else { - sInfo("vgId:%d, use sync config from raft cfg file", pSyncNode->vgId); - pSyncInfo->syncCfg = pSyncNode->pRaftCfg->cfg; + sInfo("vgId:%d, use sync config from sync cfg file", pSyncNode->vgId); + pSyncInfo->syncCfg = pSyncNode->raftCfg.cfg; } - - raftCfgClose(pSyncNode->pRaftCfg); - pSyncNode->pRaftCfg = NULL; } // init by SSyncInfo @@ -895,13 +889,10 @@ SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo) { sInfo("vgId:%d, start to open sync node, replica:%d selfIndex:%d", pSyncNode->vgId, pCfg->replicaNum, pCfg->myIndex); for (int32_t i = 0; i < pCfg->replicaNum; ++i) { SNodeInfo* pNode = &pCfg->nodeInfo[i]; - sInfo("vgId:%d, index:%d ep:%s:%u", pSyncNode->vgId, i, pNode->nodeFqdn, pNode->nodePort); + sInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pSyncNode->vgId, i, pNode->nodeFqdn, pNode->nodePort, + pNode->nodeId, pNode->clusterId); } - memcpy(pSyncNode->path, pSyncInfo->path, sizeof(pSyncNode->path)); - snprintf(pSyncNode->raftStorePath, sizeof(pSyncNode->raftStorePath), "%s%sraft_store.json", pSyncInfo->path, - TD_DIRSEP); - snprintf(pSyncNode->configPath, sizeof(pSyncNode->configPath), "%s%sraft_config.json", pSyncInfo->path, TD_DIRSEP); pSyncNode->pWal = pSyncInfo->pWal; pSyncNode->msgcb = pSyncInfo->msgcb; @@ -916,26 +907,20 @@ SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo) { goto _error; } - // init raft config - pSyncNode->pRaftCfg = raftCfgOpen(pSyncNode->configPath); - if (pSyncNode->pRaftCfg == NULL) { - sError("vgId:%d, failed to open raft cfg file at %s", pSyncNode->vgId, pSyncNode->configPath); - goto _error; - } - // init internal - pSyncNode->myNodeInfo = pSyncNode->pRaftCfg->cfg.nodeInfo[pSyncNode->pRaftCfg->cfg.myIndex]; + pSyncNode->myNodeInfo = pSyncNode->raftCfg.cfg.nodeInfo[pSyncNode->raftCfg.cfg.myIndex]; if (!syncUtilNodeInfo2RaftId(&pSyncNode->myNodeInfo, pSyncNode->vgId, &pSyncNode->myRaftId)) { sError("vgId:%d, failed to determine my raft member id", pSyncNode->vgId); goto _error; } // init peersNum, peers, peersId - pSyncNode->peersNum = pSyncNode->pRaftCfg->cfg.replicaNum - 1; + pSyncNode->peersNum = pSyncNode->raftCfg.cfg.replicaNum - 1; int32_t j = 0; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { - if (i != pSyncNode->pRaftCfg->cfg.myIndex) { - pSyncNode->peersNodeInfo[j] = pSyncNode->pRaftCfg->cfg.nodeInfo[i]; + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { + if (i != pSyncNode->raftCfg.cfg.myIndex) { + pSyncNode->peersNodeInfo[j] = pSyncNode->raftCfg.cfg.nodeInfo[i]; + syncUtilNodeInfo2EpSet(&pSyncNode->peersNodeInfo[j], &pSyncNode->peersEpset[j]); j++; } } @@ -947,9 +932,9 @@ SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo) { } // init replicaNum, replicasId - pSyncNode->replicaNum = pSyncNode->pRaftCfg->cfg.replicaNum; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { - if (!syncUtilNodeInfo2RaftId(&pSyncNode->pRaftCfg->cfg.nodeInfo[i], pSyncNode->vgId, &pSyncNode->replicasId[i])) { + pSyncNode->replicaNum = pSyncNode->raftCfg.cfg.replicaNum; + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { + if (!syncUtilNodeInfo2RaftId(&pSyncNode->raftCfg.cfg.nodeInfo[i], pSyncNode->vgId, &pSyncNode->replicasId[i])) { sError("vgId:%d, failed to determine raft member id, replica:%d", pSyncNode->vgId, i); goto _error; } @@ -958,7 +943,7 @@ SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo) { // init raft algorithm pSyncNode->pFsm = pSyncInfo->pFsm; pSyncInfo->pFsm = NULL; - pSyncNode->quorum = syncUtilQuorum(pSyncNode->pRaftCfg->cfg.replicaNum); + pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum); pSyncNode->leaderCache = EMPTY_RAFT_ID; // init life cycle outside @@ -1293,8 +1278,6 @@ void syncNodeClose(SSyncNode* pSyncNode) { pSyncNode->pLogStore = NULL; syncLogBufferDestroy(pSyncNode->pLogBuf); pSyncNode->pLogBuf = NULL; - raftCfgClose(pSyncNode->pRaftCfg); - pSyncNode->pRaftCfg = NULL; syncNodeStopPingTimer(pSyncNode); syncNodeStopElectTimer(pSyncNode); @@ -1330,7 +1313,7 @@ void syncNodeClose(SSyncNode* pSyncNode) { taosMemoryFree(pSyncNode); } -ESyncStrategy syncNodeStrategy(SSyncNode* pSyncNode) { return pSyncNode->pRaftCfg->snapshotStrategy; } +ESyncStrategy syncNodeStrategy(SSyncNode* pSyncNode) { return pSyncNode->raftCfg.snapshotStrategy; } // timer control -------------- int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode) { @@ -1392,7 +1375,7 @@ int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms) { void syncNodeResetElectTimer(SSyncNode* pSyncNode) { int32_t electMS; - if (pSyncNode->pRaftCfg->isStandBy) { + if (pSyncNode->raftCfg.isStandBy) { electMS = TIMER_MAX_MS; } else { electMS = syncUtilElectRandomMS(pSyncNode->electBaseLine, 2 * pSyncNode->electBaseLine); @@ -1461,55 +1444,46 @@ int32_t syncNodeRestartHeartbeatTimer(SSyncNode* pSyncNode) { return 0; } -int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg) { - SEpSet epSet; - syncUtilRaftId2EpSet(destRaftId, &epSet); +int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pNode, SRpcMsg* pMsg) { + SEpSet* epSet = NULL; + for (int32_t i = 0; i < pNode->peersNum; ++i) { + if (destRaftId->addr == pNode->peersId[i].addr) { + epSet = &pNode->peersEpset[i]; + break; + } + } - if (pSyncNode->syncSendMSg != NULL) { + if (pNode->syncSendMSg != NULL && epSet != NULL) { syncUtilMsgHtoN(pMsg->pCont); pMsg->info.noResp = 1; - return pSyncNode->syncSendMSg(&epSet, pMsg); + return pNode->syncSendMSg(epSet, pMsg); } else { - sError("vgId:%d, sync send msg by id error, fp-send-msg is null", pSyncNode->vgId); + sError("vgId:%d, sync send msg by id error, fp:%p epset:%p", pNode->vgId, pNode->syncSendMSg, epSet); rpcFreeCont(pMsg->pCont); terrno = TSDB_CODE_SYN_INTERNAL_ERROR; return -1; } } -int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg) { - SEpSet epSet; - syncUtilNodeInfo2EpSet(nodeInfo, &epSet); - if (pSyncNode->syncSendMSg != NULL) { - // htonl - syncUtilMsgHtoN(pMsg->pCont); - - pMsg->info.noResp = 1; - pSyncNode->syncSendMSg(&epSet, pMsg); - } else { - sError("vgId:%d, sync send msg by info error, fp-send-msg is null", pSyncNode->vgId); - } - return 0; -} - -inline bool syncNodeInConfig(SSyncNode* pSyncNode, const SSyncCfg* config) { +inline bool syncNodeInConfig(SSyncNode* pNode, const SSyncCfg* pCfg) { bool b1 = false; bool b2 = false; - for (int32_t i = 0; i < config->replicaNum; ++i) { - if (strcmp((config->nodeInfo)[i].nodeFqdn, pSyncNode->myNodeInfo.nodeFqdn) == 0 && - (config->nodeInfo)[i].nodePort == pSyncNode->myNodeInfo.nodePort) { + for (int32_t i = 0; i < pCfg->replicaNum; ++i) { + if (strcmp(pCfg->nodeInfo[i].nodeFqdn, pNode->myNodeInfo.nodeFqdn) == 0 && + pCfg->nodeInfo[i].nodePort == pNode->myNodeInfo.nodePort) { b1 = true; break; } } - for (int32_t i = 0; i < config->replicaNum; ++i) { - SRaftId raftId; - raftId.addr = syncUtilAddr2U64((config->nodeInfo)[i].nodeFqdn, (config->nodeInfo)[i].nodePort); - raftId.vgId = pSyncNode->vgId; + for (int32_t i = 0; i < pCfg->replicaNum; ++i) { + SRaftId raftId = { + .addr = SYNC_ADDR(&pCfg->nodeInfo[i]), + .vgId = pNode->vgId, + }; - if (syncUtilSameId(&raftId, &(pSyncNode->myRaftId))) { + if (syncUtilSameId(&raftId, &pNode->myRaftId)) { b2 = true; break; } @@ -1533,14 +1507,14 @@ static bool syncIsConfigChanged(const SSyncCfg* pOldCfg, const SSyncCfg* pNewCfg } void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncIndex lastConfigChangeIndex) { - SSyncCfg oldConfig = pSyncNode->pRaftCfg->cfg; + SSyncCfg oldConfig = pSyncNode->raftCfg.cfg; if (!syncIsConfigChanged(&oldConfig, pNewConfig)) { sInfo("vgId:1, sync not reconfig since not changed"); return; } - pSyncNode->pRaftCfg->cfg = *pNewConfig; - pSyncNode->pRaftCfg->lastConfigIndex = lastConfigChangeIndex; + pSyncNode->raftCfg.cfg = *pNewConfig; + pSyncNode->raftCfg.lastConfigIndex = lastConfigChangeIndex; pSyncNode->configChangeNum++; @@ -1563,21 +1537,18 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde } // log begin config change - char oldCfgStr[1024] = {0}; - char newCfgStr[1024] = {0}; - syncCfg2SimpleStr(&oldConfig, oldCfgStr, sizeof(oldCfgStr)); - syncCfg2SimpleStr(pNewConfig, oldCfgStr, sizeof(oldCfgStr)); - sNInfo(pSyncNode, "begin do config change, from %s to %s", oldCfgStr, oldCfgStr); + sNInfo(pSyncNode, "begin do config change, from %d to %d", pSyncNode->vgId, oldConfig.replicaNum, + pNewConfig->replicaNum); if (IamInNew) { - pSyncNode->pRaftCfg->isStandBy = 0; // change isStandBy to normal + pSyncNode->raftCfg.isStandBy = 0; // change isStandBy to normal } if (isDrop) { - pSyncNode->pRaftCfg->isStandBy = 1; // set standby + pSyncNode->raftCfg.isStandBy = 1; // set standby } // add last config index - raftCfgAddConfigIndex(pSyncNode->pRaftCfg, lastConfigChangeIndex); + syncAddCfgIndex(pSyncNode, lastConfigChangeIndex); if (IamInNew) { //----------------------------------------- @@ -1594,15 +1565,16 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde } // init internal - pSyncNode->myNodeInfo = pSyncNode->pRaftCfg->cfg.nodeInfo[pSyncNode->pRaftCfg->cfg.myIndex]; + pSyncNode->myNodeInfo = pSyncNode->raftCfg.cfg.nodeInfo[pSyncNode->raftCfg.cfg.myIndex]; syncUtilNodeInfo2RaftId(&pSyncNode->myNodeInfo, pSyncNode->vgId, &pSyncNode->myRaftId); // init peersNum, peers, peersId - pSyncNode->peersNum = pSyncNode->pRaftCfg->cfg.replicaNum - 1; + pSyncNode->peersNum = pSyncNode->raftCfg.cfg.replicaNum - 1; int32_t j = 0; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { - if (i != pSyncNode->pRaftCfg->cfg.myIndex) { - pSyncNode->peersNodeInfo[j] = pSyncNode->pRaftCfg->cfg.nodeInfo[i]; + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { + if (i != pSyncNode->raftCfg.cfg.myIndex) { + pSyncNode->peersNodeInfo[j] = pSyncNode->raftCfg.cfg.nodeInfo[i]; + syncUtilNodeInfo2EpSet(&pSyncNode->peersNodeInfo[j], &pSyncNode->peersEpset[j]); j++; } } @@ -1611,13 +1583,13 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde } // init replicaNum, replicasId - pSyncNode->replicaNum = pSyncNode->pRaftCfg->cfg.replicaNum; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { - syncUtilNodeInfo2RaftId(&pSyncNode->pRaftCfg->cfg.nodeInfo[i], pSyncNode->vgId, &pSyncNode->replicasId[i]); + pSyncNode->replicaNum = pSyncNode->raftCfg.cfg.replicaNum; + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { + syncUtilNodeInfo2RaftId(&pSyncNode->raftCfg.cfg.nodeInfo[i], pSyncNode->vgId, &pSyncNode->replicasId[i]); } // update quorum first - pSyncNode->quorum = syncUtilQuorum(pSyncNode->pRaftCfg->cfg.replicaNum); + pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum); syncIndexMgrUpdate(pSyncNode->pNextIndex, pSyncNode); syncIndexMgrUpdate(pSyncNode->pMatchIndex, pSyncNode); @@ -1637,11 +1609,8 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde bool reset = false; for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) { if (syncUtilSameId(&(pSyncNode->replicasId)[i], &oldReplicasId[j]) && oldSenders[j] != NULL) { - char host[128]; - uint16_t port; - syncUtilU642Addr((pSyncNode->replicasId)[i].addr, host, sizeof(host), &port); - sNTrace(pSyncNode, "snapshot sender reset for: %" PRId64 ", newIndex:%d, %s:%d, %p", - (pSyncNode->replicasId)[i].addr, i, host, port, oldSenders[j]); + sNTrace(pSyncNode, "snapshot sender reset for:%" PRId64 ", newIndex:%d, dnode:%d, %p", + (pSyncNode->replicasId)[i].addr, i, DID(&pSyncNode->replicasId[i]), oldSenders[j]); pSyncNode->senders[i] = oldSenders[j]; oldSenders[j] = NULL; @@ -1651,8 +1620,8 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde int32_t oldreplicaIndex = pSyncNode->senders[i]->replicaIndex; pSyncNode->senders[i]->replicaIndex = i; - sNTrace(pSyncNode, "snapshot sender udpate replicaIndex from %d to %d, %s:%d, %p, reset:%d", oldreplicaIndex, - i, host, port, pSyncNode->senders[i], reset); + sNTrace(pSyncNode, "snapshot sender udpate replicaIndex from %d to %d, dnode:%d, %p, reset:%d", + oldreplicaIndex, i, DID(&pSyncNode->replicasId[i]), pSyncNode->senders[i], reset); break; } @@ -1684,33 +1653,30 @@ void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* pNewConfig, SyncInde } // persist cfg - raftCfgPersist(pSyncNode->pRaftCfg); + syncWriteCfgFile(pSyncNode); - char tmpbuf[1024] = {0}; - snprintf(tmpbuf, sizeof(tmpbuf), "config change from %d to %d, index:%" PRId64 ", %s --> %s", - oldConfig.replicaNum, pNewConfig->replicaNum, lastConfigChangeIndex, oldCfgStr, newCfgStr); // change isStandBy to normal (election timeout) if (pSyncNode->state == TAOS_SYNC_STATE_LEADER) { - syncNodeBecomeLeader(pSyncNode, tmpbuf); + syncNodeBecomeLeader(pSyncNode, ""); // Raft 3.6.2 Committing entries from previous terms syncNodeAppendNoop(pSyncNode); // syncMaybeAdvanceCommitIndex(pSyncNode); } else { - syncNodeBecomeFollower(pSyncNode, tmpbuf); + syncNodeBecomeFollower(pSyncNode, ""); } } else { // persist cfg - raftCfgPersist(pSyncNode->pRaftCfg); - sNInfo(pSyncNode, "do not config change from %d to %d, index:%" PRId64 ", %s --> %s", oldConfig.replicaNum, - pNewConfig->replicaNum, lastConfigChangeIndex, oldCfgStr, newCfgStr); + syncWriteCfgFile(pSyncNode); + sNInfo(pSyncNode, "do not config change from %d to %d", oldConfig.replicaNum, pNewConfig->replicaNum); } _END: // log end config change - sNInfo(pSyncNode, "end do config change, from %s to %s", oldCfgStr, newCfgStr); + sNInfo(pSyncNode, "end do config change, from %d to %d", pSyncNode->vgId, oldConfig.replicaNum, + pNewConfig->replicaNum); } // raft state change -------------- @@ -2889,9 +2855,10 @@ int32_t syncDoLeaderTransfer(SSyncNode* ths, SRpcMsg* pRpcMsg, SSyncRaftEntry* p int32_t syncNodeUpdateNewConfigIndex(SSyncNode* ths, SSyncCfg* pNewCfg) { for (int32_t i = 0; i < pNewCfg->replicaNum; ++i) { - SRaftId raftId; - raftId.addr = syncUtilAddr2U64((pNewCfg->nodeInfo)[i].nodeFqdn, (pNewCfg->nodeInfo)[i].nodePort); - raftId.vgId = ths->vgId; + SRaftId raftId = { + .addr = SYNC_ADDR(&pNewCfg->nodeInfo[i]), + .vgId = ths->vgId, + }; if (syncUtilSameId(&(ths->myRaftId), &raftId)) { pNewCfg->myIndex = i; diff --git a/source/libs/sync/src/syncPipeline.c b/source/libs/sync/src/syncPipeline.c index ee649c268c..1837ac98e0 100644 --- a/source/libs/sync/src/syncPipeline.c +++ b/source/libs/sync/src/syncPipeline.c @@ -652,18 +652,15 @@ int32_t syncLogReplMgrProcessReplyInRecoveryMode(SSyncLogReplMgr* pMgr, SSyncNod SSyncLogBuffer* pBuf = pNode->pLogBuf; SRaftId destId = pMsg->srcId; ASSERT(pMgr->restored == false); - char host[64]; - uint16_t port; - syncUtilU642Addr(destId.addr, host, sizeof(host), &port); if (pMgr->endIndex == 0) { ASSERT(pMgr->startIndex == 0); ASSERT(pMgr->matchIndex == 0); if (pMsg->matchIndex < 0) { pMgr->restored = true; - sInfo("vgId:%d, sync log repl mgr restored. peer: %s:%d (%" PRIx64 "), mgr: rs(%d) [%" PRId64 " %" PRId64 + sInfo("vgId:%d, sync log repl mgr restored. peer: dnode:%d (%" PRIx64 "), mgr: rs(%d) [%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", - pNode->vgId, host, port, destId.addr, pMgr->restored, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, + pNode->vgId, DID(&destId), destId.addr, pMgr->restored, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex); return 0; } @@ -678,21 +675,21 @@ int32_t syncLogReplMgrProcessReplyInRecoveryMode(SSyncLogReplMgr* pMgr, SSyncNod if (pMsg->success && pMsg->matchIndex == pMsg->lastSendIndex) { pMgr->matchIndex = pMsg->matchIndex; pMgr->restored = true; - sInfo("vgId:%d, sync log repl mgr restored. peer: %s:%d (%" PRIx64 "), mgr: rs(%d) [%" PRId64 " %" PRId64 + sInfo("vgId:%d, sync log repl mgr restored. peer: dnode:%d (%" PRIx64 "), mgr: rs(%d) [%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", - pNode->vgId, host, port, destId.addr, pMgr->restored, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, + pNode->vgId, DID(&destId), destId.addr, pMgr->restored, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex); return 0; } if (pMsg->success == false && pMsg->matchIndex >= pMsg->lastSendIndex) { - sWarn("vgId:%d, failed to rollback match index. peer: %s:%d, match index: %" PRId64 ", last sent: %" PRId64, - pNode->vgId, host, port, pMsg->matchIndex, pMsg->lastSendIndex); + sWarn("vgId:%d, failed to rollback match index. peer: dnode:%d, match index: %" PRId64 ", last sent: %" PRId64, + pNode->vgId, DID(&destId), pMsg->matchIndex, pMsg->lastSendIndex); if (syncNodeStartSnapshot(pNode, &destId) < 0) { - sError("vgId:%d, failed to start snapshot for peer %s:%d", pNode->vgId, host, port); + sError("vgId:%d, failed to start snapshot for peer dnode:%d", pNode->vgId, DID(&destId)); return -1; } - sInfo("vgId:%d, snapshot replication to peer %s:%d", pNode->vgId, host, port); + sInfo("vgId:%d, snapshot replication to peer dnode:%d", pNode->vgId, DID(&destId)); return 0; } } @@ -707,10 +704,10 @@ int32_t syncLogReplMgrProcessReplyInRecoveryMode(SSyncLogReplMgr* pMgr, SSyncNod if (term < 0 || (term != pMsg->lastMatchTerm && (index + 1 == firstVer || index == firstVer))) { ASSERT(term >= 0 || terrno == TSDB_CODE_WAL_LOG_NOT_EXIST); if (syncNodeStartSnapshot(pNode, &destId) < 0) { - sError("vgId:%d, failed to start snapshot for peer %s:%d", pNode->vgId, host, port); + sError("vgId:%d, failed to start snapshot for peer %s:%d", pNode->vgId, DID(&destId)); return -1; } - sInfo("vgId:%d, snapshot replication to peer %s:%d", pNode->vgId, host, port); + sInfo("vgId:%d, snapshot replication to peer %s:%d", pNode->vgId, DID(&destId)); return 0; } diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c index c609bfba93..60f27c18b3 100644 --- a/source/libs/sync/src/syncRaftCfg.c +++ b/source/libs/sync/src/syncRaftCfg.c @@ -16,382 +16,226 @@ #define _DEFAULT_SOURCE #include "syncRaftCfg.h" #include "syncUtil.h" +#include "tjson.h" -// file must already exist! -SRaftCfgIndex *raftCfgIndexOpen(const char *path) { - SRaftCfgIndex *pRaftCfgIndex = taosMemoryMalloc(sizeof(SRaftCfg)); - snprintf(pRaftCfgIndex->path, sizeof(pRaftCfgIndex->path), "%s", path); +static int32_t syncEncodeSyncCfg(const void *pObj, SJson *pJson) { + SSyncCfg *pCfg = (SSyncCfg *)pObj; + if (tjsonAddIntegerToObject(pJson, "replicaNum", pCfg->replicaNum) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "myIndex", pCfg->myIndex) < 0) return -1; - pRaftCfgIndex->pFile = taosOpenFile(pRaftCfgIndex->path, TD_FILE_READ | TD_FILE_WRITE); - ASSERT(pRaftCfgIndex->pFile != NULL); - - taosLSeekFile(pRaftCfgIndex->pFile, 0, SEEK_SET); - - int32_t bufLen = MAX_CONFIG_INDEX_COUNT * 16; - char *pBuf = taosMemoryMalloc(bufLen); - memset(pBuf, 0, bufLen); - int64_t len = taosReadFile(pRaftCfgIndex->pFile, pBuf, bufLen); - ASSERT(len > 0); - - int32_t ret = raftCfgIndexFromStr(pBuf, pRaftCfgIndex); - ASSERT(ret == 0); - - taosMemoryFree(pBuf); - - return pRaftCfgIndex; -} - -int32_t raftCfgIndexClose(SRaftCfgIndex *pRaftCfgIndex) { - if (pRaftCfgIndex != NULL) { - int64_t ret = taosCloseFile(&(pRaftCfgIndex->pFile)); - ASSERT(ret == 0); - taosMemoryFree(pRaftCfgIndex); - } - return 0; -} - -int32_t raftCfgIndexPersist(SRaftCfgIndex *pRaftCfgIndex) { - ASSERT(pRaftCfgIndex != NULL); - - char *s = raftCfgIndex2Str(pRaftCfgIndex); - taosLSeekFile(pRaftCfgIndex->pFile, 0, SEEK_SET); - - int64_t ret = taosWriteFile(pRaftCfgIndex->pFile, s, strlen(s) + 1); - ASSERT(ret == strlen(s) + 1); - - taosMemoryFree(s); - taosFsyncFile(pRaftCfgIndex->pFile); - return 0; -} - -int32_t raftCfgIndexAddConfigIndex(SRaftCfgIndex *pRaftCfgIndex, SyncIndex configIndex) { - ASSERT(pRaftCfgIndex->configIndexCount <= MAX_CONFIG_INDEX_COUNT); - (pRaftCfgIndex->configIndexArr)[pRaftCfgIndex->configIndexCount] = configIndex; - ++(pRaftCfgIndex->configIndexCount); - return 0; -} - -cJSON *raftCfgIndex2Json(SRaftCfgIndex *pRaftCfgIndex) { - cJSON *pRoot = cJSON_CreateObject(); - - cJSON_AddNumberToObject(pRoot, "configIndexCount", pRaftCfgIndex->configIndexCount); - cJSON *pIndexArr = cJSON_CreateArray(); - cJSON_AddItemToObject(pRoot, "configIndexArr", pIndexArr); - for (int i = 0; i < pRaftCfgIndex->configIndexCount; ++i) { - char buf64[128]; - snprintf(buf64, sizeof(buf64), "%" PRId64, (pRaftCfgIndex->configIndexArr)[i]); - cJSON *pIndexObj = cJSON_CreateObject(); - cJSON_AddStringToObject(pIndexObj, "index", buf64); - cJSON_AddItemToArray(pIndexArr, pIndexObj); - } - - cJSON *pJson = cJSON_CreateObject(); - cJSON_AddItemToObject(pJson, "SRaftCfgIndex", pRoot); - return pJson; -} - -char *raftCfgIndex2Str(SRaftCfgIndex *pRaftCfgIndex) { - cJSON *pJson = raftCfgIndex2Json(pRaftCfgIndex); - char *serialized = cJSON_Print(pJson); - cJSON_Delete(pJson); - return serialized; -} - -int32_t raftCfgIndexFromJson(const cJSON *pRoot, SRaftCfgIndex *pRaftCfgIndex) { - cJSON *pJson = cJSON_GetObjectItem(pRoot, "SRaftCfgIndex"); - - cJSON *pJsonConfigIndexCount = cJSON_GetObjectItem(pJson, "configIndexCount"); - pRaftCfgIndex->configIndexCount = cJSON_GetNumberValue(pJsonConfigIndexCount); - - cJSON *pIndexArr = cJSON_GetObjectItem(pJson, "configIndexArr"); - int arraySize = cJSON_GetArraySize(pIndexArr); - ASSERT(arraySize == pRaftCfgIndex->configIndexCount); - - memset(pRaftCfgIndex->configIndexArr, 0, sizeof(pRaftCfgIndex->configIndexArr)); - for (int i = 0; i < arraySize; ++i) { - cJSON *pIndexObj = cJSON_GetArrayItem(pIndexArr, i); - ASSERT(pIndexObj != NULL); - - cJSON *pIndex = cJSON_GetObjectItem(pIndexObj, "index"); - ASSERT(cJSON_IsString(pIndex)); - (pRaftCfgIndex->configIndexArr)[i] = atoll(pIndex->valuestring); + SJson *nodeInfo = tjsonCreateArray(); + if (nodeInfo == NULL) return -1; + if (tjsonAddItemToObject(pJson, "nodeInfo", nodeInfo) < 0) return -1; + for (int32_t i = 0; i < pCfg->replicaNum; ++i) { + SJson *info = tjsonCreateObject(); + if (info == NULL) return -1; + if (tjsonAddIntegerToObject(info, "nodePort", pCfg->nodeInfo[i].nodePort) < 0) return -1; + if (tjsonAddStringToObject(info, "nodeFqdn", pCfg->nodeInfo[i].nodeFqdn) < 0) return -1; + if (tjsonAddIntegerToObject(info, "nodeId", pCfg->nodeInfo[i].nodeId) < 0) return -1; + if (tjsonAddIntegerToObject(info, "clusterId", pCfg->nodeInfo[i].clusterId) < 0) return -1; + if (tjsonAddItemToArray(nodeInfo, info) < 0) return -1; } return 0; } -int32_t raftCfgIndexFromStr(const char *s, SRaftCfgIndex *pRaftCfgIndex) { - cJSON *pRoot = cJSON_Parse(s); - ASSERT(pRoot != NULL); +static int32_t syncEncodeRaftCfg(const void *pObj, SJson *pJson) { + SRaftCfg *pCfg = (SRaftCfg *)pObj; + if (tjsonAddObject(pJson, "SSyncCfg", syncEncodeSyncCfg, (void *)&pCfg->cfg) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "isStandBy", pCfg->isStandBy) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "snapshotStrategy", pCfg->snapshotStrategy) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "batchSize", pCfg->batchSize) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "lastConfigIndex", pCfg->lastConfigIndex) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "configIndexCount", pCfg->configIndexCount) < 0) return -1; - int32_t ret = raftCfgIndexFromJson(pRoot, pRaftCfgIndex); - ASSERT(ret == 0); + SJson *configIndexArr = tjsonCreateArray(); + if (configIndexArr == NULL) return -1; + if (tjsonAddItemToObject(pJson, "configIndexArr", configIndexArr) < 0) return -1; + for (int32_t i = 0; i < pCfg->configIndexCount; ++i) { + SJson *configIndex = tjsonCreateObject(); + if (configIndex == NULL) return -1; + if (tjsonAddIntegerToObject(configIndex, "index", pCfg->configIndexArr[i]) < 0) return -1; + if (tjsonAddItemToArray(configIndexArr, configIndex) < 0) return -1; + } - cJSON_Delete(pRoot); return 0; } -int32_t raftCfgIndexCreateFile(const char *path) { - TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE); +int32_t syncWriteCfgFile(SSyncNode *pNode) { + int32_t code = -1; + char *buffer = NULL; + SJson *pJson = NULL; + TdFilePtr pFile = NULL; + const char *realfile = pNode->configPath; + SRaftCfg *pCfg = &pNode->raftCfg; + char file[PATH_MAX] = {0}; + snprintf(file, sizeof(file), "%s.bak", realfile); + + pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { - int32_t err = terrno; - const char *errStr = tstrerror(err); - int32_t sysErr = errno; - const char *sysErrStr = strerror(errno); - sError("create raft cfg index file error, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", err, err, errStr, sysErr, - sysErrStr); - ASSERT(0); - - return -1; + terrno = TAOS_SYSTEM_ERROR(errno); + sError("failed to open sync cfg file:%s since %s", pNode->vgId, realfile, terrstr()); + goto _OVER; } - SRaftCfgIndex raftCfgIndex; - memset(raftCfgIndex.configIndexArr, 0, sizeof(raftCfgIndex.configIndexArr)); - raftCfgIndex.configIndexCount = 1; - raftCfgIndex.configIndexArr[0] = -1; + terrno = TSDB_CODE_OUT_OF_MEMORY; + pJson = tjsonCreateObject(); + if (pJson == NULL) goto _OVER; + if (tjsonAddObject(pJson, "RaftCfg", syncEncodeRaftCfg, pCfg) < 0) goto _OVER; - char *s = raftCfgIndex2Str(&raftCfgIndex); - int64_t ret = taosWriteFile(pFile, s, strlen(s) + 1); - ASSERT(ret == strlen(s) + 1); + buffer = tjsonToString(pJson); + if (buffer == NULL) goto _OVER; - taosMemoryFree(s); + int32_t len = strlen(buffer); + if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER; + if (taosFsyncFile(pFile) < 0) goto _OVER; taosCloseFile(&pFile); - return 0; -} -// --------------------------------------- -// file must already exist! -SRaftCfg *raftCfgOpen(const char *path) { - SRaftCfg *pCfg = taosMemoryMalloc(sizeof(SRaftCfg)); - snprintf(pCfg->path, sizeof(pCfg->path), "%s", path); - - pCfg->pFile = taosOpenFile(pCfg->path, TD_FILE_READ | TD_FILE_WRITE); - ASSERT(pCfg->pFile != NULL); - - taosLSeekFile(pCfg->pFile, 0, SEEK_SET); - - char buf[CONFIG_FILE_LEN] = {0}; - int len = taosReadFile(pCfg->pFile, buf, sizeof(buf)); - ASSERT(len > 0); - - int32_t ret = raftCfgFromStr(buf, pCfg); - ASSERT(ret == 0); - - return pCfg; -} - -int32_t raftCfgClose(SRaftCfg *pRaftCfg) { - int64_t ret = taosCloseFile(&(pRaftCfg->pFile)); - ASSERT(ret == 0); - taosMemoryFree(pRaftCfg); - return 0; -} - -int32_t raftCfgPersist(SRaftCfg *pRaftCfg) { - ASSERT(pRaftCfg != NULL); - - char *s = raftCfg2Str(pRaftCfg); - taosLSeekFile(pRaftCfg->pFile, 0, SEEK_SET); - - char buf[CONFIG_FILE_LEN] = {0}; - memset(buf, 0, sizeof(buf)); - - if (strlen(s) + 1 > CONFIG_FILE_LEN) { - sError("too long config str:%s", s); - ASSERT(0); + if (taosRenameFile(file, realfile) != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + sError("vgId:%d, failed to rename sync cfg file:%s to %s since %s", pNode->vgId, file, realfile, terrstr()); + goto _OVER; } - snprintf(buf, sizeof(buf), "%s", s); - int64_t ret = taosWriteFile(pRaftCfg->pFile, buf, sizeof(buf)); - ASSERT(ret == sizeof(buf)); + code = 0; + sInfo("vgId:%d, succeed to write sync cfg file:%s, len:%d", pNode->vgId, realfile, len); - // int64_t ret = taosWriteFile(pRaftCfg->pFile, s, strlen(s) + 1); - // ASSERT(ret == strlen(s) + 1); +_OVER: + if (pJson != NULL) tjsonDelete(pJson); + if (buffer != NULL) taosMemoryFree(buffer); + if (pFile != NULL) taosCloseFile(&pFile); - taosMemoryFree(s); - taosFsyncFile(pRaftCfg->pFile); - return 0; -} - -int32_t raftCfgAddConfigIndex(SRaftCfg *pRaftCfg, SyncIndex configIndex) { - ASSERT(pRaftCfg->configIndexCount <= MAX_CONFIG_INDEX_COUNT); - (pRaftCfg->configIndexArr)[pRaftCfg->configIndexCount] = configIndex; - ++(pRaftCfg->configIndexCount); - return 0; -} - -cJSON *syncCfg2Json(SSyncCfg *pSyncCfg) { - char u64buf[128] = {0}; - cJSON *pRoot = cJSON_CreateObject(); - - if (pSyncCfg != NULL) { - cJSON_AddNumberToObject(pRoot, "replicaNum", pSyncCfg->replicaNum); - cJSON_AddNumberToObject(pRoot, "myIndex", pSyncCfg->myIndex); - - cJSON *pNodeInfoArr = cJSON_CreateArray(); - cJSON_AddItemToObject(pRoot, "nodeInfo", pNodeInfoArr); - for (int i = 0; i < pSyncCfg->replicaNum; ++i) { - cJSON *pNodeInfo = cJSON_CreateObject(); - cJSON_AddNumberToObject(pNodeInfo, "nodePort", ((pSyncCfg->nodeInfo)[i]).nodePort); - cJSON_AddStringToObject(pNodeInfo, "nodeFqdn", ((pSyncCfg->nodeInfo)[i]).nodeFqdn); - cJSON_AddItemToArray(pNodeInfoArr, pNodeInfo); - } + if (code != 0) { + sError("failed to write sync cfg file:%s since %s", pNode->vgId, realfile, terrstr()); } - - return pRoot; -} - -int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg) { - memset(pSyncCfg, 0, sizeof(SSyncCfg)); - // cJSON *pJson = cJSON_GetObjectItem(pRoot, "SSyncCfg"); - const cJSON *pJson = pRoot; - - cJSON *pReplicaNum = cJSON_GetObjectItem(pJson, "replicaNum"); - ASSERT(cJSON_IsNumber(pReplicaNum)); - pSyncCfg->replicaNum = cJSON_GetNumberValue(pReplicaNum); - - cJSON *pMyIndex = cJSON_GetObjectItem(pJson, "myIndex"); - ASSERT(cJSON_IsNumber(pMyIndex)); - pSyncCfg->myIndex = cJSON_GetNumberValue(pMyIndex); - - cJSON *pNodeInfoArr = cJSON_GetObjectItem(pJson, "nodeInfo"); - int arraySize = cJSON_GetArraySize(pNodeInfoArr); - ASSERT(arraySize == pSyncCfg->replicaNum); - - for (int i = 0; i < arraySize; ++i) { - cJSON *pNodeInfo = cJSON_GetArrayItem(pNodeInfoArr, i); - ASSERT(pNodeInfo != NULL); - - cJSON *pNodePort = cJSON_GetObjectItem(pNodeInfo, "nodePort"); - ASSERT(cJSON_IsNumber(pNodePort)); - ((pSyncCfg->nodeInfo)[i]).nodePort = cJSON_GetNumberValue(pNodePort); - - cJSON *pNodeFqdn = cJSON_GetObjectItem(pNodeInfo, "nodeFqdn"); - ASSERT(cJSON_IsString(pNodeFqdn)); - snprintf(((pSyncCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pSyncCfg->nodeInfo)[i]).nodeFqdn), "%s", - pNodeFqdn->valuestring); - } - - return 0; -} - -cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) { - cJSON *pRoot = cJSON_CreateObject(); - cJSON_AddItemToObject(pRoot, "SSyncCfg", syncCfg2Json(&(pRaftCfg->cfg))); - cJSON_AddNumberToObject(pRoot, "isStandBy", pRaftCfg->isStandBy); - cJSON_AddNumberToObject(pRoot, "snapshotStrategy", pRaftCfg->snapshotStrategy); - cJSON_AddNumberToObject(pRoot, "batchSize", pRaftCfg->batchSize); - - char buf64[128]; - snprintf(buf64, sizeof(buf64), "%" PRId64, pRaftCfg->lastConfigIndex); - cJSON_AddStringToObject(pRoot, "lastConfigIndex", buf64); - - cJSON_AddNumberToObject(pRoot, "configIndexCount", pRaftCfg->configIndexCount); - cJSON *pIndexArr = cJSON_CreateArray(); - cJSON_AddItemToObject(pRoot, "configIndexArr", pIndexArr); - for (int i = 0; i < pRaftCfg->configIndexCount; ++i) { - snprintf(buf64, sizeof(buf64), "%" PRId64, (pRaftCfg->configIndexArr)[i]); - cJSON *pIndexObj = cJSON_CreateObject(); - cJSON_AddStringToObject(pIndexObj, "index", buf64); - cJSON_AddItemToArray(pIndexArr, pIndexObj); - } - - cJSON *pJson = cJSON_CreateObject(); - cJSON_AddItemToObject(pJson, "RaftCfg", pRoot); - return pJson; -} - -char *raftCfg2Str(SRaftCfg *pRaftCfg) { - cJSON *pJson = raftCfg2Json(pRaftCfg); - char *serialized = cJSON_Print(pJson); - cJSON_Delete(pJson); - return serialized; -} - -int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path) { - TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE); - if (pFile == NULL) { - int32_t err = terrno; - const char *errStr = tstrerror(err); - int32_t sysErr = errno; - const char *sysErrStr = strerror(errno); - sError("create raft cfg file error, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", err, err, errStr, sysErr, sysErrStr); - return -1; - } - - SRaftCfg raftCfg; - raftCfg.cfg = *pCfg; - raftCfg.isStandBy = meta.isStandBy; - raftCfg.batchSize = meta.batchSize; - raftCfg.snapshotStrategy = meta.snapshotStrategy; - raftCfg.lastConfigIndex = meta.lastConfigIndex; - raftCfg.configIndexCount = 1; - memset(raftCfg.configIndexArr, 0, sizeof(raftCfg.configIndexArr)); - raftCfg.configIndexArr[0] = -1; - char *s = raftCfg2Str(&raftCfg); - - char buf[CONFIG_FILE_LEN] = {0}; - memset(buf, 0, sizeof(buf)); - ASSERT(strlen(s) + 1 <= CONFIG_FILE_LEN); - snprintf(buf, sizeof(buf), "%s", s); - int64_t ret = taosWriteFile(pFile, buf, sizeof(buf)); - ASSERT(ret == sizeof(buf)); - - // int64_t ret = taosWriteFile(pFile, s, strlen(s) + 1); - // ASSERT(ret == strlen(s) + 1); - - taosMemoryFree(s); - taosCloseFile(&pFile); - return 0; -} - -int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg) { - // memset(pRaftCfg, 0, sizeof(SRaftCfg)); - cJSON *pJson = cJSON_GetObjectItem(pRoot, "RaftCfg"); - - cJSON *pJsonIsStandBy = cJSON_GetObjectItem(pJson, "isStandBy"); - pRaftCfg->isStandBy = cJSON_GetNumberValue(pJsonIsStandBy); - - cJSON *pJsonBatchSize = cJSON_GetObjectItem(pJson, "batchSize"); - pRaftCfg->batchSize = cJSON_GetNumberValue(pJsonBatchSize); - - cJSON *pJsonSnapshotStrategy = cJSON_GetObjectItem(pJson, "snapshotStrategy"); - pRaftCfg->snapshotStrategy = cJSON_GetNumberValue(pJsonSnapshotStrategy); - - cJSON *pJsonLastConfigIndex = cJSON_GetObjectItem(pJson, "lastConfigIndex"); - pRaftCfg->lastConfigIndex = atoll(cJSON_GetStringValue(pJsonLastConfigIndex)); - - cJSON *pJsonConfigIndexCount = cJSON_GetObjectItem(pJson, "configIndexCount"); - pRaftCfg->configIndexCount = cJSON_GetNumberValue(pJsonConfigIndexCount); - - cJSON *pIndexArr = cJSON_GetObjectItem(pJson, "configIndexArr"); - int arraySize = cJSON_GetArraySize(pIndexArr); - ASSERT(arraySize == pRaftCfg->configIndexCount); - - memset(pRaftCfg->configIndexArr, 0, sizeof(pRaftCfg->configIndexArr)); - for (int i = 0; i < arraySize; ++i) { - cJSON *pIndexObj = cJSON_GetArrayItem(pIndexArr, i); - ASSERT(pIndexObj != NULL); - - cJSON *pIndex = cJSON_GetObjectItem(pIndexObj, "index"); - ASSERT(cJSON_IsString(pIndex)); - (pRaftCfg->configIndexArr)[i] = atoll(pIndex->valuestring); - } - - cJSON *pJsonSyncCfg = cJSON_GetObjectItem(pJson, "SSyncCfg"); - int32_t code = syncCfgFromJson(pJsonSyncCfg, &(pRaftCfg->cfg)); - ASSERT(code == 0); - return code; } -int32_t raftCfgFromStr(const char *s, SRaftCfg *pRaftCfg) { - cJSON *pRoot = cJSON_Parse(s); - ASSERT(pRoot != NULL); +static int32_t syncDecodeSyncCfg(const SJson *pJson, void *pObj) { + SSyncCfg *pCfg = (SSyncCfg *)pObj; + int32_t code = 0; - int32_t ret = raftCfgFromJson(pRoot, pRaftCfg); - ASSERT(ret == 0); + tjsonGetNumberValue(pJson, "replicaNum", pCfg->replicaNum, code); + if (code < 0) return -1; + tjsonGetNumberValue(pJson, "myIndex", pCfg->myIndex, code); + if (code < 0) return -1; + + SJson *nodeInfo = tjsonGetObjectItem(pJson, "nodeInfo"); + if (nodeInfo == NULL) return -1; + pCfg->replicaNum = tjsonGetArraySize(nodeInfo); + + for (int32_t i = 0; i < pCfg->replicaNum; ++i) { + SJson *info = tjsonGetArrayItem(nodeInfo, i); + if (info == NULL) return -1; + tjsonGetNumberValue(info, "nodePort", pCfg->nodeInfo[i].nodePort, code); + if (code < 0) return -1; + tjsonGetStringValue(info, "nodeFqdn", pCfg->nodeInfo[i].nodeFqdn); + if (code < 0) return -1; + tjsonGetNumberValue(info, "nodeId", pCfg->nodeInfo[i].nodeId, code); + tjsonGetNumberValue(info, "clusterId", pCfg->nodeInfo[i].clusterId, code); + } - cJSON_Delete(pRoot); return 0; } + +static int32_t syncDecodeRaftCfg(const SJson *pJson, void *pObj) { + SRaftCfg *pCfg = (SRaftCfg *)pObj; + int32_t code = 0; + + if (tjsonToObject(pJson, "SSyncCfg", syncDecodeSyncCfg, (void *)&pCfg->cfg) < 0) return -1; + + tjsonGetNumberValue(pJson, "isStandBy", pCfg->isStandBy, code); + if (code < 0) return -1; + tjsonGetNumberValue(pJson, "snapshotStrategy", pCfg->snapshotStrategy, code); + if (code < 0) return -1; + tjsonGetNumberValue(pJson, "batchSize", pCfg->batchSize, code); + if (code < 0) return -1; + tjsonGetNumberValue(pJson, "lastConfigIndex", pCfg->lastConfigIndex, code); + if (code < 0) return -1; + tjsonGetNumberValue(pJson, "configIndexCount", pCfg->configIndexCount, code); + + SJson *configIndexArr = tjsonGetObjectItem(pJson, "configIndexArr"); + if (configIndexArr == NULL) return -1; + + pCfg->configIndexCount = tjsonGetArraySize(configIndexArr); + for (int32_t i = 0; i < pCfg->configIndexCount; ++i) { + SJson *configIndex = tjsonGetArrayItem(configIndexArr, i); + if (configIndex == NULL) return -1; + tjsonGetNumberValue(configIndex, "index", pCfg->configIndexArr[i], code); + if (code < 0) return -1; + } + + return 0; +} + +int32_t syncReadCfgFile(SSyncNode *pNode) { + int32_t code = -1; + TdFilePtr pFile = NULL; + char *pData = NULL; + SJson *pJson = NULL; + const char *file = pNode->configPath; + SRaftCfg *pCfg = &pNode->raftCfg; + + pFile = taosOpenFile(file, TD_FILE_READ); + if (pFile == NULL) { + terrno = TAOS_SYSTEM_ERROR(errno); + sError("vgId:%d, failed to open sync cfg file:%s since %s", pNode->vgId, file, terrstr()); + goto _OVER; + } + + int64_t size = 0; + if (taosFStatFile(pFile, &size, NULL) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + sError("vgId:%d, failed to fstat sync cfg file:%s since %s", pNode->vgId, file, terrstr()); + goto _OVER; + } + + pData = taosMemoryMalloc(size + 1); + if (pData == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } + + if (taosReadFile(pFile, pData, size) != size) { + terrno = TAOS_SYSTEM_ERROR(errno); + sError("vgId:%d, failed to read sync cfg file:%s since %s", pNode->vgId, file, terrstr()); + goto _OVER; + } + + pData[size] = '\0'; + + pJson = tjsonParse(pData); + if (pJson == NULL) { + terrno = TSDB_CODE_INVALID_JSON_FORMAT; + goto _OVER; + } + + if (tjsonToObject(pJson, "RaftCfg", syncDecodeRaftCfg, (void *)pCfg) < 0) { + terrno = TSDB_CODE_INVALID_JSON_FORMAT; + goto _OVER; + } + + code = 0; + sInfo("vgId:%d, succceed to read sync cfg file %s", pNode->vgId, file); + +_OVER: + if (pData != NULL) taosMemoryFree(pData); + if (pJson != NULL) cJSON_Delete(pJson); + if (pFile != NULL) taosCloseFile(&pFile); + + if (code != 0) { + sError("vgId:%d, failed to read sync cfg file:%s since %s", pNode->vgId, file, terrstr()); + } + return code; +} + +int32_t syncAddCfgIndex(SSyncNode *pNode, SyncIndex cfgIndex) { + SRaftCfg *pCfg = &pNode->raftCfg; + if (pCfg->configIndexCount <= MAX_CONFIG_INDEX_COUNT) { + return -1; + } + + pCfg->configIndexArr[pCfg->configIndexCount] = cfgIndex; + pCfg->configIndexCount++; + return 0; +} \ No newline at end of file diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index e328ed3d31..b19cda2a44 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -112,13 +112,6 @@ int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len) { cJSON_AddNumberToObject(pRoot, "vote_for_vgid", pRaftStore->voteFor.vgId); - uint64_t u64 = pRaftStore->voteFor.addr; - char host[128] = {0}; - uint16_t port; - syncUtilU642Addr(u64, host, sizeof(host), &port); - cJSON_AddStringToObject(pRoot, "addr_host", host); - cJSON_AddNumberToObject(pRoot, "addr_port", port); - char *serialized = cJSON_Print(pRoot); int len2 = strlen(serialized); ASSERT(len2 < len); diff --git a/source/libs/sync/src/syncReplication.c b/source/libs/sync/src/syncReplication.c index 0f56921ec7..a03e5aa80f 100644 --- a/source/libs/sync/src/syncReplication.c +++ b/source/libs/sync/src/syncReplication.c @@ -107,10 +107,7 @@ int32_t syncNodeReplicateOne(SSyncNode* pSyncNode, SRaftId* pDestId, bool snapsh pMsg = rpcMsg.pCont; } else { - char host[64]; - uint16_t port; - syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port); - sNError(pSyncNode, "replicate to %s:%d error, next-index:%" PRId64, host, port, nextIndex); + sNError(pSyncNode, "replicate to dnode:%d error, next-index:%" PRId64, DID(pDestId), nextIndex); return -1; } } @@ -171,10 +168,7 @@ int32_t syncNodeReplicateOld(SSyncNode* pSyncNode) { SRaftId* pDestId = &(pSyncNode->peersId[i]); ret = syncNodeReplicateOne(pSyncNode, pDestId, true); if (ret != 0) { - char host[64]; - int16_t port; - syncUtilU642Addr(pDestId->addr, host, sizeof(host), &port); - sError("vgId:%d, do append entries error for %s:%d", pSyncNode->vgId, host, port); + sError("vgId:%d, do append entries error for %s:%d", pSyncNode->vgId, DID(pDestId)); } } @@ -183,7 +177,6 @@ int32_t syncNodeReplicateOld(SSyncNode* pSyncNode) { int32_t syncNodeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, SRpcMsg* pRpcMsg) { SyncAppendEntries* pMsg = pRpcMsg->pCont; - int32_t ret = 0; pMsg->destId = *destRaftId; syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg); return 0; @@ -229,11 +222,7 @@ int32_t syncNodeMaybeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* dest if (syncNodeNeedSendAppendEntries(pSyncNode, destRaftId, pMsg)) { ret = syncNodeSendAppendEntries(pSyncNode, destRaftId, pRpcMsg); } else { - char logBuf[128]; - char host[64]; - int16_t port; - syncUtilU642Addr(destRaftId->addr, host, sizeof(host), &port); - sNTrace(pSyncNode, "do not repcate to %s:%d for index:%" PRId64, host, port, pMsg->prevLogIndex + 1); + sNTrace(pSyncNode, "do not repcate to dnode:%d for index:%" PRId64, DID(destRaftId), pMsg->prevLogIndex + 1); rpcFreeCont(pRpcMsg->pCont); } diff --git a/source/libs/sync/src/syncTimeout.c b/source/libs/sync/src/syncTimeout.c index 16e593d0e4..859183db95 100644 --- a/source/libs/sync/src/syncTimeout.c +++ b/source/libs/sync/src/syncTimeout.c @@ -24,33 +24,35 @@ #include "syncUtil.h" static void syncNodeCleanConfigIndex(SSyncNode* ths) { +#if 0 int32_t newArrIndex = 0; SyncIndex newConfigIndexArr[MAX_CONFIG_INDEX_COUNT] = {0}; SSnapshot snapshot = {0}; ths->pFsm->FpGetSnapshotInfo(ths->pFsm, &snapshot); if (snapshot.lastApplyIndex != SYNC_INDEX_INVALID) { - for (int32_t i = 0; i < ths->pRaftCfg->configIndexCount; ++i) { - if (ths->pRaftCfg->configIndexArr[i] < snapshot.lastConfigIndex) { + for (int32_t i = 0; i < ths->raftCfg.configIndexCount; ++i) { + if (ths->raftCfg.configIndexArr[i] < snapshot.lastConfigIndex) { // pass } else { // save - newConfigIndexArr[newArrIndex] = ths->pRaftCfg->configIndexArr[i]; + newConfigIndexArr[newArrIndex] = ths->raftCfg.configIndexArr[i]; ++newArrIndex; } } - int32_t oldCnt = ths->pRaftCfg->configIndexCount; - ths->pRaftCfg->configIndexCount = newArrIndex; - memcpy(ths->pRaftCfg->configIndexArr, newConfigIndexArr, sizeof(newConfigIndexArr)); + int32_t oldCnt = ths->raftCfg.configIndexCount; + ths->raftCfg.configIndexCount = newArrIndex; + memcpy(ths->raftCfg.configIndexArr, newConfigIndexArr, sizeof(newConfigIndexArr)); - int32_t code = raftCfgPersist(ths->pRaftCfg); + int32_t code = syncWriteCfgFile(ths); if (code != 0) { sNFatal(ths, "failed to persist cfg"); } else { - sNTrace(ths, "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt, ths->pRaftCfg->configIndexCount); + sNTrace(ths, "clean config index arr, old-cnt:%d, new-cnt:%d", oldCnt, ths->raftCfg.configIndexCount); } } +#endif } static int32_t syncNodeTimerRoutine(SSyncNode* ths) { diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 49a24bebde..0ec24d5326 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -21,42 +21,23 @@ #include "syncRaftStore.h" #include "syncSnapshot.h" -extern void addEpIntoEpSet(SEpSet* pEpSet, const char* fqdn, uint16_t port); +void syncCfg2SimpleStr(const SSyncCfg* pCfg, char* buf, int32_t bufLen) { + int32_t len = snprintf(buf, bufLen, "{r-num:%d, my:%d, ", pCfg->replicaNum, pCfg->myIndex); -uint64_t syncUtilAddr2U64(const char* host, uint16_t port) { - uint32_t hostU32 = taosGetIpv4FromFqdn(host); - if (hostU32 == (uint32_t)-1) { - sError("failed to resolve ipv4 addr, host:%s", host); - terrno = TSDB_CODE_TSC_INVALID_FQDN; - return -1; + for (int32_t i = 0; i < pCfg->replicaNum; ++i) { + if (i < pCfg->replicaNum - 1) { + len += snprintf(buf + len, bufLen - len, "%s:%d, ", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort); + } else { + len += snprintf(buf + len, bufLen - len, "%s:%d}", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort); + } } - - uint64_t u64 = (((uint64_t)hostU32) << 32) | (((uint32_t)port) << 16); - return u64; -} - -void syncUtilU642Addr(uint64_t u64, char* host, int64_t len, uint16_t* port) { - uint32_t hostU32 = (uint32_t)((u64 >> 32) & 0x00000000FFFFFFFF); - - struct in_addr addr = {.s_addr = hostU32}; - taosInetNtoa(addr, host, len); - *port = (uint16_t)((u64 & 0x00000000FFFF0000) >> 16); } void syncUtilNodeInfo2EpSet(const SNodeInfo* pInfo, SEpSet* pEpSet) { pEpSet->inUse = 0; - pEpSet->numOfEps = 0; - addEpIntoEpSet(pEpSet, pInfo->nodeFqdn, pInfo->nodePort); -} - -void syncUtilRaftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) { - char host[TSDB_FQDN_LEN] = {0}; - uint16_t port = 0; - - syncUtilU642Addr(raftId->addr, host, sizeof(host), &port); - pEpSet->inUse = 0; - pEpSet->numOfEps = 0; - addEpIntoEpSet(pEpSet, host, port); + pEpSet->numOfEps = 1; + pEpSet->eps[0].port = pInfo->nodePort; + tstrncpy(pEpSet->eps[0].fqdn, pInfo->nodeFqdn, TSDB_FQDN_LEN); } bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* raftId) { @@ -69,13 +50,18 @@ bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* char ipbuf[128] = {0}; tinet_ntoa(ipbuf, ipv4); - raftId->addr = syncUtilAddr2U64(ipbuf, pInfo->nodePort); + raftId->addr = SYNC_ADDR(pInfo); raftId->vgId = vgId; + + sInfo("vgId:%d, sync addr:%" PRIu64 ", dnode:%d cluster:%" PRId64 " fqdn:%s ip:%s port:%u ipv4:%u", vgId, + raftId->addr, pInfo->nodeId, pInfo->clusterId, pInfo->nodeFqdn, ipbuf, pInfo->nodePort, ipv4); return true; } bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) { - return pId1->addr == pId2->addr && pId1->vgId == pId2->vgId; + if (pId1->addr == pId2->addr && pId1->vgId == pId2->vgId) return true; + if ((CID(pId1) == 0 || CID(pId2) == 0) && (DID(pId1) == DID(pId2)) && pId1->vgId == pId2->vgId) return true; + return false; } bool syncUtilEmptyId(const SRaftId* pId) { return (pId->addr == 0 && pId->vgId == 0); } @@ -91,89 +77,16 @@ int32_t syncUtilElectRandomMS(int32_t min, int32_t max) { int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; } -cJSON* syncUtilRaftId2Json(const SRaftId* p) { - char u64buf[128] = {0}; - cJSON* pRoot = cJSON_CreateObject(); - - snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", p->addr); - cJSON_AddStringToObject(pRoot, "addr", u64buf); - char host[128] = {0}; - uint16_t port; - syncUtilU642Addr(p->addr, host, sizeof(host), &port); - cJSON_AddStringToObject(pRoot, "host", host); - cJSON_AddNumberToObject(pRoot, "port", port); - cJSON_AddNumberToObject(pRoot, "vgId", p->vgId); - - cJSON* pJson = cJSON_CreateObject(); - cJSON_AddItemToObject(pJson, "SRaftId", pRoot); - return pJson; -} - -static inline bool syncUtilCanPrint(char c) { - if (c >= 32 && c <= 126) { - return true; - } else { - return false; - } -} - -char* syncUtilPrintBin(char* ptr, uint32_t len) { - int64_t memLen = (int64_t)(len + 1); - char* s = taosMemoryMalloc(memLen); - ASSERT(s != NULL); - memset(s, 0, len + 1); - memcpy(s, ptr, len); - - for (int32_t i = 0; i < len; ++i) { - if (!syncUtilCanPrint(s[i])) { - s[i] = '.'; - } - } - return s; -} - -char* syncUtilPrintBin2(char* ptr, uint32_t len) { - uint32_t len2 = len * 4 + 1; - char* s = taosMemoryMalloc(len2); - ASSERT(s != NULL); - memset(s, 0, len2); - - char* p = s; - for (int32_t i = 0; i < len; ++i) { - int32_t n = sprintf(p, "%d,", ptr[i]); - p += n; - } - return s; -} - void syncUtilMsgHtoN(void* msg) { SMsgHead* pHead = msg; pHead->contLen = htonl(pHead->contLen); pHead->vgId = htonl(pHead->vgId); } -void syncUtilMsgNtoH(void* msg) { - SMsgHead* pHead = msg; - pHead->contLen = ntohl(pHead->contLen); - pHead->vgId = ntohl(pHead->vgId); -} - bool syncUtilUserPreCommit(tmsg_t msgType) { return msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_LEADER_TRANSFER; } bool syncUtilUserRollback(tmsg_t msgType) { return msgType != TDMT_SYNC_NOOP && msgType != TDMT_SYNC_LEADER_TRANSFER; } -void syncCfg2SimpleStr(const SSyncCfg* pCfg, char* buf, int32_t bufLen) { - int32_t len = snprintf(buf, bufLen, "{r-num:%d, my:%d, ", pCfg->replicaNum, pCfg->myIndex); - - for (int32_t i = 0; i < pCfg->replicaNum; ++i) { - if (i < pCfg->replicaNum - 1) { - len += snprintf(buf + len, bufLen - len, "%s:%d, ", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort); - } else { - len += snprintf(buf + len, bufLen - len, "%s:%d}", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort); - } - } -} - // for leader static void syncHearbeatReplyTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) { int32_t len = 5; @@ -222,7 +135,7 @@ static void syncPeerState2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) { } void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNode* pNode, const char* format, ...) { - if (pNode == NULL || pNode->pRaftCfg == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; + if (pNode == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; int64_t currentTerm = pNode->pRaftStore->currentTerm; // save error code, otherwise it will be overwritten @@ -244,11 +157,7 @@ void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNo int32_t cacheMiss = pNode->pLogStore->cacheMiss; char cfgStr[1024]; - if (pNode->pRaftCfg != NULL) { - syncCfg2SimpleStr(&(pNode->pRaftCfg->cfg), cfgStr, sizeof(cfgStr)); - } else { - return; - } + syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr)); char peerStr[1024] = "{"; syncPeerState2Str(pNode, peerStr, sizeof(peerStr)); @@ -259,8 +168,6 @@ void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNo char hbTimeStr[256] = "hb:{"; syncHearbeatTime2Str(pNode, hbTimeStr, sizeof(hbTimeStr)); - int32_t quorum = syncNodeDynamicQuorum(pNode); - char eventLog[512]; // {0}; va_list argpointer; va_start(argpointer, format); @@ -275,7 +182,7 @@ void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNo // restore error code terrno = errCode; - if (pNode != NULL && pNode->pRaftCfg != NULL) { + if (pNode != NULL) { taosPrintLog(flags, level, dflag, "vgId:%d, %s, sync:%s, term:%" PRIu64 ", commit-index:%" PRId64 ", first-ver:%" PRId64 ", last-ver:%" PRId64 ", min:%" PRId64 ", snap:%" PRId64 ", snap-term:%" PRIu64 @@ -286,7 +193,7 @@ void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNo logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm, pNode->electNum, pNode->becomeLeaderNum, pNode->configChangeNum, cacheHit, cacheMiss, pNode->hbSlowNum, pNode->hbrSlowNum, aqItems, pNode->snapshottingIndex, pNode->replicaNum, - pNode->pRaftCfg->lastConfigIndex, pNode->changing, pNode->restoreFinish, quorum, + pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, peerStr, cfgStr, hbTimeStr, hbrTimeStr); } @@ -295,7 +202,7 @@ void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, SSyncNo void syncPrintSnapshotSenderLog(const char* flags, ELogLevel level, int32_t dflag, SSyncSnapshotSender* pSender, const char* format, ...) { SSyncNode* pNode = pSender->pSyncNode; - if (pNode == NULL || pNode->pRaftCfg == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; + if (pNode == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0}; if (pNode->pFsm != NULL && pNode->pFsm->FpGetSnapshotInfo != NULL) { @@ -310,17 +217,11 @@ void syncPrintSnapshotSenderLog(const char* flags, ELogLevel level, int32_t dfla } char cfgStr[1024]; - syncCfg2SimpleStr(&(pNode->pRaftCfg->cfg), cfgStr, sizeof(cfgStr)); + syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr)); char peerStr[1024] = "{"; syncPeerState2Str(pNode, peerStr, sizeof(peerStr)); - int32_t quorum = syncNodeDynamicQuorum(pNode); - SRaftId destId = pNode->replicasId[pSender->replicaIndex]; - char host[64]; - uint16_t port; - syncUtilU642Addr(destId.addr, host, sizeof(host), &port); - char eventLog[512]; // {0}; va_list argpointer; va_start(argpointer, format); @@ -330,24 +231,24 @@ void syncPrintSnapshotSenderLog(const char* flags, ELogLevel level, int32_t dfla taosPrintLog(flags, level, dflag, "vgId:%d, %s, sync:%s, {%p s-param:%" PRId64 " e-param:%" PRId64 " laindex:%" PRId64 " laterm:%" PRIu64 " lcindex:%" PRId64 - " seq:%d ack:%d finish:%d replica-index:%d %s:%d}" + " seq:%d ack:%d finish:%d replica-index:%d dnode:%d}" ", tm:%" PRIu64 ", cmt:%" PRId64 ", fst:%" PRId64 ", lst:%" PRId64 ", min:%" PRId64 ", snap:%" PRId64 ", snap-tm:%" PRIu64 ", sby:%d, stgy:%d, bch:%d, r-num:%d, lcfg:%" PRId64 ", chging:%d, rsto:%d, dquorum:%d, elt:%" PRId64 ", hb:%" PRId64 ", %s, %s", pNode->vgId, eventLog, syncStr(pNode->state), pSender, pSender->snapshotParam.start, pSender->snapshotParam.end, pSender->snapshot.lastApplyIndex, pSender->snapshot.lastApplyTerm, pSender->snapshot.lastConfigIndex, pSender->seq, pSender->ack, pSender->finish, pSender->replicaIndex, - host, port, pNode->pRaftStore->currentTerm, pNode->commitIndex, logBeginIndex, logLastIndex, - pNode->minMatchIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm, pNode->pRaftCfg->isStandBy, - pNode->pRaftCfg->snapshotStrategy, pNode->pRaftCfg->batchSize, pNode->replicaNum, - pNode->pRaftCfg->lastConfigIndex, pNode->changing, pNode->restoreFinish, quorum, + DID(&pNode->replicasId[pSender->replicaIndex]), pNode->pRaftStore->currentTerm, pNode->commitIndex, + logBeginIndex, logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm, + pNode->raftCfg.isStandBy, pNode->raftCfg.snapshotStrategy, pNode->raftCfg.batchSize, pNode->replicaNum, + pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, peerStr, cfgStr); } void syncPrintSnapshotReceiverLog(const char* flags, ELogLevel level, int32_t dflag, SSyncSnapshotReceiver* pReceiver, const char* format, ...) { SSyncNode* pNode = pReceiver->pSyncNode; - if (pNode == NULL || pNode->pRaftCfg == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; + if (pNode == NULL || pNode->pRaftStore == NULL || pNode->pLogStore == NULL) return; SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0}; if (pNode->pFsm != NULL && pNode->pFsm->FpGetSnapshotInfo != NULL) { @@ -362,17 +263,11 @@ void syncPrintSnapshotReceiverLog(const char* flags, ELogLevel level, int32_t df } char cfgStr[1024]; - syncCfg2SimpleStr(&(pNode->pRaftCfg->cfg), cfgStr, sizeof(cfgStr)); + syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr)); char peerStr[1024] = "{"; syncPeerState2Str(pNode, peerStr, sizeof(peerStr)); - int32_t quorum = syncNodeDynamicQuorum(pNode); - SRaftId fromId = pReceiver->fromId; - char host[128]; - uint16_t port; - syncUtilU642Addr(fromId.addr, host, sizeof(host), &port); - char eventLog[512]; // {0}; va_list argpointer; va_start(argpointer, format); @@ -381,19 +276,19 @@ void syncPrintSnapshotReceiverLog(const char* flags, ELogLevel level, int32_t df taosPrintLog(flags, level, dflag, "vgId:%d, %s, sync:%s," - " {%p start:%d ack:%d term:%" PRIu64 " start-time:%" PRId64 " from:%s:%d s-param:%" PRId64 + " {%p start:%d ack:%d term:%" PRIu64 " start-time:%" PRId64 " from dnode:%d s-param:%" PRId64 " e-param:%" PRId64 " laindex:%" PRId64 " laterm:%" PRIu64 " lcindex:%" PRId64 "}" ", tm:%" PRIu64 ", cmt:%" PRId64 ", fst:%" PRId64 ", lst:%" PRId64 ", min:%" PRId64 ", snap:%" PRId64 ", snap-tm:%" PRIu64 ", sby:%d, stgy:%d, bch:%d, r-num:%d, lcfg:%" PRId64 ", chging:%d, rsto:%d, dquorum:%d, elt:%" PRId64 ", hb:%" PRId64 ", %s, %s", pNode->vgId, eventLog, syncStr(pNode->state), pReceiver, pReceiver->start, pReceiver->ack, - pReceiver->term, pReceiver->startTime, host, port, pReceiver->snapshotParam.start, + pReceiver->term, pReceiver->startTime, DID(&pReceiver->fromId), pReceiver->snapshotParam.start, pReceiver->snapshotParam.end, pReceiver->snapshot.lastApplyIndex, pReceiver->snapshot.lastApplyTerm, pReceiver->snapshot.lastConfigIndex, pNode->pRaftStore->currentTerm, pNode->commitIndex, logBeginIndex, logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex, snapshot.lastApplyTerm, - pNode->pRaftCfg->isStandBy, pNode->pRaftCfg->snapshotStrategy, pNode->pRaftCfg->batchSize, - pNode->replicaNum, pNode->pRaftCfg->lastConfigIndex, pNode->changing, pNode->restoreFinish, quorum, + pNode->raftCfg.isStandBy, pNode->raftCfg.snapshotStrategy, pNode->raftCfg.batchSize, pNode->replicaNum, + pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, peerStr, cfgStr); } @@ -408,56 +303,37 @@ void syncLogRecvTimer(SSyncNode* pSyncNode, const SyncTimeout* pMsg, const char* } void syncLogRecvLocalCmd(SSyncNode* pSyncNode, const SyncLocalCmd* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - sNTrace(pSyncNode, "recv sync-local-cmd {cmd:%d-%s, sd-new-term:%" PRId64 ", fc-index:%" PRId64 "}, %s", pMsg->cmd, syncLocalCmdGetStr(pMsg->cmd), pMsg->sdNewTerm, pMsg->fcIndex, s); } void syncLogSendAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - sNTrace(pSyncNode, - "send sync-append-entries-reply to %s:%d, {term:%" PRId64 ", pterm:%" PRId64 + "send sync-append-entries-reply to dnode:%d, {term:%" PRId64 ", pterm:%" PRId64 ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s", - host, port, pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s); + DID(&pMsg->destId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s); } void syncLogRecvAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - sNTrace(pSyncNode, - "recv sync-append-entries-reply from %s:%d {term:%" PRId64 ", pterm:%" PRId64 + "recv sync-append-entries-reply from dnode:%d {term:%" PRId64 ", pterm:%" PRId64 ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s", - host, port, pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s); + DID(&pMsg->srcId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s); } void syncLogSendHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, bool printX, int64_t timerElapsed, int64_t execTime) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - if (printX) { sNTrace(pSyncNode, - "send sync-heartbeat to %s:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 + "send sync-heartbeat to dnode:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 "}, x", - host, port, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp); + DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp); } else { sNTrace(pSyncNode, - "send sync-heartbeat to %s:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 + "send sync-heartbeat to dnode:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 "}, timer-elapsed:%" PRId64 ", next-exec:%" PRId64, - host, port, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, timerElapsed, execTime); + DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, timerElapsed, + execTime); } } @@ -465,183 +341,109 @@ void syncLogRecvHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, int64 if (timeDiff > SYNC_HEARTBEAT_SLOW_MS) { pSyncNode->hbSlowNum++; - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); sNInfo(pSyncNode, - "recv sync-heartbeat from %s:%d slow {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 - "}, %s, net elapsed:%" PRId64, - host, port, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, s, timeDiff); + "recv sync-heartbeat from dnode:%d slow {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 + ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, + DID(&pMsg->srcId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, s, timeDiff); } - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); sNTrace(pSyncNode, - "recv sync-heartbeat from %s:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 + "recv sync-heartbeat from dnode:%d {term:%" PRId64 ", cmt:%" PRId64 ", min-match:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, - host, port, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, s, timeDiff); + DID(&pMsg->srcId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pMsg->timeStamp, s, timeDiff); } void syncLogSendHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - - sNTrace(pSyncNode, "send sync-heartbeat-reply from %s:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s", host, port, - pMsg->term, pMsg->timeStamp, s); + sNTrace(pSyncNode, "send sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s", + DID(&pMsg->destId), pMsg->term, pMsg->timeStamp, s); } void syncLogRecvHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, int64_t timeDiff, const char* s) { if (timeDiff > SYNC_HEARTBEAT_REPLY_SLOW_MS) { pSyncNode->hbrSlowNum++; - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); sNTrace(pSyncNode, - "recv sync-heartbeat-reply from %s:%d slow {term:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, - host, port, pMsg->term, pMsg->timeStamp, s, timeDiff); + "recv sync-heartbeat-reply from dnode:%d slow {term:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, + DID(&pMsg->srcId), pMsg->term, pMsg->timeStamp, s, timeDiff); } - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); sNTrace(pSyncNode, - "recv sync-heartbeat-reply from %s:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, host, - port, pMsg->term, pMsg->timeStamp, s, timeDiff); + "recv sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64, + DID(&pMsg->srcId), pMsg->term, pMsg->timeStamp, s, timeDiff); } void syncLogSendSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_DEBUG)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - sNDebug(pSyncNode, - "send sync-snapshot-send to %s:%u, %s, seq:%d, term:%" PRId64 ", begin:%" PRId64 ", end:%" PRId64 + "send sync-snapshot-send to dnode:%d, %s, seq:%d, term:%" PRId64 ", begin:%" PRId64 ", end:%" PRId64 ", lterm:%" PRId64 ", stime:%" PRId64, - host, port, s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm, pMsg->startTime); + DID(&pMsg->destId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm, + pMsg->startTime); } void syncLogRecvSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_DEBUG)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - sNDebug(pSyncNode, - "recv sync-snapshot-send from %s:%u, %s, seq:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 + "recv sync-snapshot-send from dnode:%d, %s, seq:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 ", lterm:%" PRId64 ", stime:%" PRId64 ", len:%u", - host, port, s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm, pMsg->startTime, - pMsg->dataLen); + DID(&pMsg->srcId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm, + pMsg->startTime, pMsg->dataLen); } void syncLogSendSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_DEBUG)) return; - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - sNDebug(pSyncNode, - "send sync-snapshot-rsp to %s:%u, %s, ack:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 + "send sync-snapshot-rsp to dnode:%d, %s, ack:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 ", lterm:%" PRId64 ", stime:%" PRId64, - host, port, s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm, pMsg->startTime); + DID(&pMsg->destId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm, + pMsg->startTime); } void syncLogRecvSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_DEBUG)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - sNDebug(pSyncNode, - "recv sync-snapshot-rsp from %s:%u, %s, ack:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 + "recv sync-snapshot-rsp from dnode:%d, %s, ack:%d, term:%" PRId64 ", begin:%" PRId64 ", lst:%" PRId64 ", lterm:%" PRId64 ", stime:%" PRId64, - host, port, s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm, pMsg->startTime); + DID(&pMsg->srcId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm, + pMsg->startTime); } void syncLogRecvAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - sNTrace(pSyncNode, - "recv sync-append-entries from %s:%d {term:%" PRId64 ", pre-index:%" PRId64 ", pre-term:%" PRId64 + "recv sync-append-entries from dnode:%d {term:%" PRId64 ", pre-index:%" PRId64 ", pre-term:%" PRId64 ", cmt:%" PRId64 ", pterm:%" PRId64 ", datalen:%d}, %s", - host, port, pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, pMsg->commitIndex, pMsg->privateTerm, + DID(&pMsg->srcId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, pMsg->commitIndex, pMsg->privateTerm, pMsg->dataLen, s); } void syncLogSendAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s) { - if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); sNTrace(pSyncNode, - "send sync-append-entries to %s:%d, {term:%" PRId64 ", pre-index:%" PRId64 ", pre-term:%" PRId64 + "send sync-append-entries to dnode:%d, {term:%" PRId64 ", pre-index:%" PRId64 ", pre-term:%" PRId64 ", lsend-index:%" PRId64 ", cmt:%" PRId64 ", datalen:%d}, %s", - host, port, pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, (pMsg->prevLogIndex + 1), pMsg->commitIndex, - pMsg->dataLen, s); + DID(&pMsg->destId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, (pMsg->prevLogIndex + 1), + pMsg->commitIndex, pMsg->dataLen, s); } void syncLogRecvRequestVote(SSyncNode* pSyncNode, const SyncRequestVote* pMsg, int32_t voteGranted, const char* s) { - // if (!(sDebugFlag & DEBUG_TRACE)) return; - - char logBuf[256]; - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - if (voteGranted == -1) { sNInfo(pSyncNode, - "recv sync-request-vote from %s:%d, {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 "}, %s", host, - port, pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s); + "recv sync-request-vote from dnode:%d, {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 "}, %s", + DID(&pMsg->srcId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s); } else { sNInfo(pSyncNode, - "recv sync-request-vote from %s:%d, {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 "}, granted:%d", - host, port, pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, voteGranted); + "recv sync-request-vote from dnode:%d, {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 + "}, granted:%d", + DID(&pMsg->srcId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, voteGranted); } } void syncLogSendRequestVote(SSyncNode* pNode, const SyncRequestVote* pMsg, const char* s) { - // if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - sNInfo(pNode, "send sync-request-vote to %s:%d {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 "}, %s", host, - port, pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s); + sNInfo(pNode, "send sync-request-vote to dnode:%d {term:%" PRId64 ", lindex:%" PRId64 ", lterm:%" PRId64 "}, %s", + DID(&pMsg->destId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s); } void syncLogRecvRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s) { - // if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->srcId.addr, host, sizeof(host), &port); - sNInfo(pSyncNode, "recv sync-request-vote-reply from %s:%d {term:%" PRId64 ", grant:%d}, %s", host, port, pMsg->term, - pMsg->voteGranted, s); + sNInfo(pSyncNode, "recv sync-request-vote-reply from dnode:%d {term:%" PRId64 ", grant:%d}, %s", DID(&pMsg->srcId), + pMsg->term, pMsg->voteGranted, s); } void syncLogSendRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s) { - // if (!(sDebugFlag & DEBUG_TRACE)) return; - - char host[64]; - uint16_t port; - syncUtilU642Addr(pMsg->destId.addr, host, sizeof(host), &port); - sNInfo(pSyncNode, "send sync-request-vote-reply to %s:%d {term:%" PRId64 ", grant:%d}, %s", host, port, pMsg->term, - pMsg->voteGranted, s); + sNInfo(pSyncNode, "send sync-request-vote-reply to dnode:%d {term:%" PRId64 ", grant:%d}, %s", DID(&pMsg->destId), + pMsg->term, pMsg->voteGranted, s); } diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index 7b636085f2..a20d9b4bb1 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -70,3 +70,47 @@ int main(int argc, char** argv) { taosCloseLog(); return 0; } + + +static inline bool syncUtilCanPrint(char c) { + if (c >= 32 && c <= 126) { + return true; + } else { + return false; + } +} + +char* syncUtilPrintBin(char* ptr, uint32_t len) { + int64_t memLen = (int64_t)(len + 1); + char* s = taosMemoryMalloc(memLen); + ASSERT(s != NULL); + memset(s, 0, len + 1); + memcpy(s, ptr, len); + + for (int32_t i = 0; i < len; ++i) { + if (!syncUtilCanPrint(s[i])) { + s[i] = '.'; + } + } + return s; +} + +char* syncUtilPrintBin2(char* ptr, uint32_t len) { + uint32_t len2 = len * 4 + 1; + char* s = taosMemoryMalloc(len2); + ASSERT(s != NULL); + memset(s, 0, len2); + + char* p = s; + for (int32_t i = 0; i < len; ++i) { + int32_t n = sprintf(p, "%d,", ptr[i]); + p += n; + } + return s; +} + +void syncUtilMsgNtoH(void* msg) { + SMsgHead* pHead = msg; + pHead->contLen = ntohl(pHead->contLen); + pHead->vgId = ntohl(pHead->vgId); +} diff --git a/source/libs/sync/test/sync_test_lib/src/syncMainDebug.c b/source/libs/sync/test/sync_test_lib/src/syncMainDebug.c index 6b461da0e5..31dcadeb88 100644 --- a/source/libs/sync/test/sync_test_lib/src/syncMainDebug.c +++ b/source/libs/sync/test/sync_test_lib/src/syncMainDebug.c @@ -200,8 +200,8 @@ inline char* syncNode2SimpleStr(const SSyncNode* pSyncNode) { "r-num:%d, " "lcfg:%" PRId64 ", chging:%d, rsto:%d", pSyncNode->vgId, syncStr(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, pSyncNode->commitIndex, - logBeginIndex, logLastIndex, snapshot.lastApplyIndex, pSyncNode->pRaftCfg->isStandBy, pSyncNode->replicaNum, - pSyncNode->pRaftCfg->lastConfigIndex, pSyncNode->changing, pSyncNode->restoreFinish); + logBeginIndex, logLastIndex, snapshot.lastApplyIndex, pSyncNode->raftCfg.isStandBy, pSyncNode->replicaNum, + pSyncNode->raftCfg.lastConfigIndex, pSyncNode->changing, pSyncNode->restoreFinish); return s; } @@ -243,7 +243,7 @@ int32_t syncNodePingPeers(SSyncNode* pSyncNode) { int32_t syncNodePingAll(SSyncNode* pSyncNode) { int32_t ret = 0; - for (int32_t i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { + for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.replicaNum; ++i) { SRaftId* destId = &(pSyncNode->replicasId[i]); SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, destId, pSyncNode->vgId); ret = syncNodePing(pSyncNode, destId, pMsg); diff --git a/source/libs/transport/src/tmsgcb.c b/source/libs/transport/src/tmsgcb.c index 95bc532994..4131619ed9 100644 --- a/source/libs/transport/src/tmsgcb.c +++ b/source/libs/transport/src/tmsgcb.c @@ -24,7 +24,6 @@ static SMsgCb defaultMsgCb; void tmsgSetDefault(const SMsgCb* msgcb) { defaultMsgCb = *msgcb; } int32_t tmsgPutToQueue(const SMsgCb* msgcb, EQueueType qtype, SRpcMsg* pMsg) { - ASSERT(msgcb != NULL); int32_t code = (*msgcb->putToQueueFp)(msgcb->mgmt, qtype, pMsg); if (code != 0) { rpcFreeCont(pMsg->pCont); @@ -59,3 +58,7 @@ void tmsgRegisterBrokenLinkArg(SRpcMsg* pMsg) { (*defaultMsgCb.registerBrokenLin void tmsgReleaseHandle(SRpcHandleInfo* pHandle, int8_t type) { (*defaultMsgCb.releaseHandleFp)(pHandle, type); } void tmsgReportStartup(const char* name, const char* desc) { (*defaultMsgCb.reportStartupFp)(name, desc); } + +int32_t tmsgUpdateDnodeInfo(int32_t* dnodeId, int64_t* clusterId, char* fqdn, uint16_t* port) { + return (*defaultMsgCb.updateDnodeInfoFp)(defaultMsgCb.data, dnodeId, clusterId, fqdn, port); +} diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index d8cccc83ed..f5ca52ec9f 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -640,7 +640,7 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { int32_t taosFsyncFile(TdFilePtr pFile) { if (pFile == NULL) { - return 0; + return -1; } // this implementation is WRONG diff --git a/utils/tsim/inc/simInt.h b/utils/tsim/inc/simInt.h index f512b119b4..8ff3f3b183 100644 --- a/utils/tsim/inc/simInt.h +++ b/utils/tsim/inc/simInt.h @@ -181,7 +181,7 @@ typedef struct _script_t { extern SScript *simScriptList[MAX_MAIN_SCRIPT_NUM]; extern SCommand simCmdList[]; extern int32_t simScriptPos; -extern int32_t simScriptSucced; +extern int32_t simScriptSucceed; extern int32_t simDebugFlag; extern char simScriptDir[]; extern bool abortExecution; diff --git a/utils/tsim/src/simSystem.c b/utils/tsim/src/simSystem.c index f2fefb903d..98f9217fd6 100644 --- a/utils/tsim/src/simSystem.c +++ b/utils/tsim/src/simSystem.c @@ -20,7 +20,7 @@ SScript *simScriptList[MAX_MAIN_SCRIPT_NUM]; SCommand simCmdList[SIM_CMD_END]; int32_t simScriptPos = -1; -int32_t simScriptSucced = 0; +int32_t simScriptSucceed = 0; int32_t simDebugFlag = 143; void simCloseTaosdConnect(SScript *script); char simScriptDir[PATH_MAX] = {0}; @@ -88,13 +88,13 @@ SScript *simProcessCallOver(SScript *script) { } simCloseTaosdConnect(script); - simScriptSucced++; + simScriptSucceed++; simScriptPos--; simFreeScript(script); if (simScriptPos == -1 && simExecSuccess) { simInfo("----------------------------------------------------------------------"); - simInfo("Simulation Test Done, " SUCCESS_PREFIX "%d" SUCCESS_POSTFIX " Passed:\n", simScriptSucced); + simInfo("Simulation Test Done, " SUCCESS_PREFIX "%d" SUCCESS_POSTFIX " Passed:\n", simScriptSucceed); return NULL; }