diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index a587ad6ef22fb80538147a61980ae4cdadd8ec03..1f71c242ae44a0be62dcd2e642376a2b350033ac 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -88,7 +88,7 @@ typedef struct SReConfigCbMeta { } SReConfigCbMeta; typedef struct SSnapshot { - void *data; + void* data; SyncIndex lastApplyIndex; SyncTerm lastApplyTerm; } SSnapshot; @@ -145,6 +145,7 @@ typedef struct SSyncLogStore { typedef struct SSyncInfo { bool isStandBy; + bool snapshotEnable; SyncGroupId vgId; SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; diff --git a/source/dnode/mnode/impl/src/mndSync.c b/source/dnode/mnode/impl/src/mndSync.c index ecf7cabc134fa4c607af876182d8334220ffe2b7..8e181963140a0e9bdbee6990df2e7f670b7d1acc 100644 --- a/source/dnode/mnode/impl/src/mndSync.c +++ b/source/dnode/mnode/impl/src/mndSync.c @@ -146,6 +146,7 @@ int32_t mndInitSync(SMnode *pMnode) { syncInfo.pWal = pMgmt->pWal; syncInfo.pFsm = mndSyncMakeFsm(pMnode); syncInfo.isStandBy = pMgmt->standby; + syncInfo.snapshotEnable = true; SSyncCfg *pCfg = &syncInfo.syncCfg; pCfg->replicaNum = pMnode->replica; diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index 240b478ac0797d8e03a8311b637bc28f81d1c670..ed10a0a8a45466262d9c7bf2cc726dd784b53e53 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -26,6 +26,8 @@ static int32_t vnodeSyncGetSnapshot(SSyncFSM *pFsm, SSnapshot *pSnapshot); int32_t vnodeSyncOpen(SVnode *pVnode, char *path) { SSyncInfo syncInfo = { + .isStandBy = false, + .snapshotEnable = false, .vgId = pVnode->config.vgId, .syncCfg = pVnode->config.syncCfg, .pWal = pVnode->pWal, diff --git a/source/libs/sync/inc/syncElection.h b/source/libs/sync/inc/syncElection.h index 85a82dcfb70ffd7431e41c14d81263344ec68ddb..7a3d848d465c1e4f9e0f0eff120cc506034c1073 100644 --- a/source/libs/sync/inc/syncElection.h +++ b/source/libs/sync/inc/syncElection.h @@ -39,6 +39,7 @@ extern "C" { // /\ UNCHANGED <> // int32_t syncNodeRequestVotePeers(SSyncNode* pSyncNode); +int32_t syncNodeRequestVotePeersSnapshot(SSyncNode* pSyncNode); int32_t syncNodeElect(SSyncNode* pSyncNode); int32_t syncNodeRequestVote(SSyncNode* pSyncNode, const SRaftId* destRaftId, const SyncRequestVote* pMsg); diff --git a/source/libs/sync/inc/syncRaftCfg.h b/source/libs/sync/inc/syncRaftCfg.h index 1061e8bdc4b248511eb3a580b76056cbc830f02b..86c5fab87c0febd915818442cadd310b17619676 100644 --- a/source/libs/sync/inc/syncRaftCfg.h +++ b/source/libs/sync/inc/syncRaftCfg.h @@ -34,6 +34,7 @@ typedef struct SRaftCfg { TdFilePtr pFile; char path[TSDB_FILENAME_LEN * 2]; int8_t isStandBy; + int8_t snapshotEnable; } SRaftCfg; SRaftCfg *raftCfgOpen(const char *path); @@ -50,7 +51,12 @@ char * raftCfg2Str(SRaftCfg *pRaftCfg); int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg); int32_t raftCfgFromStr(const char *s, SRaftCfg *pRaftCfg); -int32_t raftCfgCreateFile(SSyncCfg *pCfg, int8_t isStandBy, const char *path); +typedef struct SRaftCfgMeta { + int8_t isStandBy; + int8_t snapshotEnable; +} SRaftCfgMeta; + +int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path); // for debug ---------------------- void syncCfgPrint(SSyncCfg *pCfg); diff --git a/source/libs/sync/src/syncElection.c b/source/libs/sync/src/syncElection.c index 5101344b84a7d1e508d540a15f9a2ecaa0f14075..6f2088e967537ff5972fef8546f5fa27cf7d9439 100644 --- a/source/libs/sync/src/syncElection.c +++ b/source/libs/sync/src/syncElection.c @@ -49,6 +49,8 @@ int32_t syncNodeRequestVotePeers(SSyncNode* pSyncNode) { return ret; } +int32_t syncNodeRequestVotePeersSnapshot(SSyncNode* pSyncNode) { return 0; } + int32_t syncNodeElect(SSyncNode* pSyncNode) { int32_t ret = 0; if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) { diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 5ad8df11a922e14b470b5e09a916c8cb4c70a239..deec71c4a432f365d39dd8312751293597fe2e61 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -411,8 +411,11 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) { snprintf(pSyncNode->configPath, sizeof(pSyncNode->configPath), "%s/raft_config.json", pSyncInfo->path); if (!taosCheckExistFile(pSyncNode->configPath)) { - // create raft config file - ret = raftCfgCreateFile((SSyncCfg*)&(pSyncInfo->syncCfg), pSyncInfo->isStandBy, pSyncNode->configPath); + // create a new raft config file + SRaftCfgMeta meta; + meta.isStandBy = pSyncInfo->isStandBy; + meta.snapshotEnable = pSyncInfo->snapshotEnable; + ret = raftCfgCreateFile((SSyncCfg*)&(pSyncInfo->syncCfg), meta, pSyncNode->configPath); assert(ret == 0); } else { diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c index 86a07ca4f8a9846801510ad5763f687a4db290b3..cb7562f2ca82432216ea5661f864cd4309c68a1f 100644 --- a/source/libs/sync/src/syncRaftCfg.c +++ b/source/libs/sync/src/syncRaftCfg.c @@ -148,6 +148,7 @@ cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) { cJSON *pRoot = cJSON_CreateObject(); cJSON_AddItemToObject(pRoot, "SSyncCfg", syncCfg2Json(&(pRaftCfg->cfg))); cJSON_AddNumberToObject(pRoot, "isStandBy", pRaftCfg->isStandBy); + cJSON_AddNumberToObject(pRoot, "snapshotEnable", pRaftCfg->snapshotEnable); cJSON *pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "RaftCfg", pRoot); @@ -161,7 +162,7 @@ char *raftCfg2Str(SRaftCfg *pRaftCfg) { return serialized; } -int32_t raftCfgCreateFile(SSyncCfg *pCfg, int8_t isStandBy, const char *path) { +int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path) { assert(pCfg != NULL); TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE); @@ -169,7 +170,8 @@ int32_t raftCfgCreateFile(SSyncCfg *pCfg, int8_t isStandBy, const char *path) { SRaftCfg raftCfg; raftCfg.cfg = *pCfg; - raftCfg.isStandBy = isStandBy; + raftCfg.isStandBy = meta.isStandBy; + raftCfg.snapshotEnable = meta.snapshotEnable; char *s = raftCfg2Str(&raftCfg); char buf[CONFIG_FILE_LEN]; @@ -194,6 +196,9 @@ int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg) { cJSON *pJsonIsStandBy = cJSON_GetObjectItem(pJson, "isStandBy"); pRaftCfg->isStandBy = cJSON_GetNumberValue(pJsonIsStandBy); + cJSON *pJsonSnapshotEnable = cJSON_GetObjectItem(pJson, "snapshotEnable"); + pRaftCfg->snapshotEnable = cJSON_GetNumberValue(pJsonSnapshotEnable); + cJSON * pJsonSyncCfg = cJSON_GetObjectItem(pJson, "SSyncCfg"); int32_t code = syncCfgFromJson(pJsonSyncCfg, &(pRaftCfg->cfg)); ASSERT(code == 0); diff --git a/source/libs/sync/test/syncConfigChangeTest.cpp b/source/libs/sync/test/syncConfigChangeTest.cpp index 2880d8951484e3ee51eb10d05cda159a90129e8f..143d722b261ba4d01a7a6f86b065875785f31d21 100644 --- a/source/libs/sync/test/syncConfigChangeTest.cpp +++ b/source/libs/sync/test/syncConfigChangeTest.cpp @@ -93,7 +93,6 @@ SSyncFSM* createFsm() { pFsm->FpGetSnapshot = GetSnapshotCb; pFsm->FpRestoreFinishCb = RestoreFinishCb; - pFsm->FpReConfigCb = ReConfigCb; return pFsm; diff --git a/source/libs/sync/test/syncRaftCfgTest.cpp b/source/libs/sync/test/syncRaftCfgTest.cpp index f5b24db651f9ed94a290aa2e1ea9611a11f74a04..564cbdb69abca3674eaaa36e2e4eca70accc06d4 100644 --- a/source/libs/sync/test/syncRaftCfgTest.cpp +++ b/source/libs/sync/test/syncRaftCfgTest.cpp @@ -71,7 +71,10 @@ void test3() { if (taosCheckExistFile(s)) { printf("%s file: %s already exist! \n", (char*)__FUNCTION__, s); } else { - raftCfgCreateFile(pCfg, 7, s); + SRaftCfgMeta meta; + meta.isStandBy = 7; + meta.snapshotEnable = 9; + raftCfgCreateFile(pCfg, meta, s); printf("%s create json file: %s \n", (char*)__FUNCTION__, s); } @@ -94,6 +97,7 @@ void test5() { pCfg->cfg.myIndex = taosGetTimestampSec(); pCfg->isStandBy += 2; + pCfg->snapshotEnable += 3; raftCfgPersist(pCfg); printf("%s update json file: %s myIndex->%d \n", (char*)__FUNCTION__, "./test3_raft_cfg.json", pCfg->cfg.myIndex);