From 8a051c001de04a279b326b603632796f71f160c0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 10 Nov 2022 17:05:13 +0800 Subject: [PATCH 1/3] refact: adjust sync debug codes --- source/libs/sync/inc/syncRequestVote.h | 1 - source/libs/sync/inc/syncRequestVoteReply.h | 1 - source/libs/sync/src/syncRaftStore.c | 1 + source/libs/sync/src/syncReplication.c | 5 +---- source/libs/sync/src/syncRequestVote.c | 2 +- source/libs/sync/src/syncRequestVoteReply.c | 4 +--- 6 files changed, 4 insertions(+), 10 deletions(-) diff --git a/source/libs/sync/inc/syncRequestVote.h b/source/libs/sync/inc/syncRequestVote.h index c51ea4eea6..ae8e3ff1cf 100644 --- a/source/libs/sync/inc/syncRequestVote.h +++ b/source/libs/sync/inc/syncRequestVote.h @@ -21,7 +21,6 @@ extern "C" { #endif #include "syncInt.h" -#include "syncMessage.h" // TLA+ Spec // HandleRequestVoteRequest(i, j, m) == diff --git a/source/libs/sync/inc/syncRequestVoteReply.h b/source/libs/sync/inc/syncRequestVoteReply.h index 1adab30aaa..ea43f0301d 100644 --- a/source/libs/sync/inc/syncRequestVoteReply.h +++ b/source/libs/sync/inc/syncRequestVoteReply.h @@ -21,7 +21,6 @@ extern "C" { #endif #include "syncInt.h" -#include "syncMessage.h" // TLA+ Spec // HandleRequestVoteResponse(i, j, m) == diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index dcc4e1f133..1c96e70858 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncRaftStore.h" #include "cJSON.h" #include "syncEnv.h" diff --git a/source/libs/sync/src/syncReplication.c b/source/libs/sync/src/syncReplication.c index 3f22a1f2cd..2a3705e139 100644 --- a/source/libs/sync/src/syncReplication.c +++ b/source/libs/sync/src/syncReplication.c @@ -13,14 +13,11 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncReplication.h" #include "syncIndexMgr.h" -#include "syncMessage.h" -#include "syncRaftCfg.h" #include "syncRaftEntry.h" -#include "syncRaftLog.h" #include "syncRaftStore.h" -#include "syncSnapshot.h" #include "syncUtil.h" // TLA+ Spec diff --git a/source/libs/sync/src/syncRequestVote.c b/source/libs/sync/src/syncRequestVote.c index 7e2587372b..f2b75def6b 100644 --- a/source/libs/sync/src/syncRequestVote.c +++ b/source/libs/sync/src/syncRequestVote.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncRequestVote.h" -#include "syncInt.h" #include "syncRaftCfg.h" #include "syncRaftStore.h" #include "syncUtil.h" diff --git a/source/libs/sync/src/syncRequestVoteReply.c b/source/libs/sync/src/syncRequestVoteReply.c index 44efaa068a..02b9bb40ac 100644 --- a/source/libs/sync/src/syncRequestVoteReply.c +++ b/source/libs/sync/src/syncRequestVoteReply.c @@ -13,11 +13,9 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncRequestVoteReply.h" -#include "syncInt.h" -#include "syncRaftCfg.h" #include "syncRaftStore.h" -#include "syncUtil.h" #include "syncVoteMgr.h" // TLA+ Spec From 4d0c5c9d573edb2cf0cef39c42b9dd9f1c176c04 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 11 Nov 2022 11:03:01 +0800 Subject: [PATCH 2/3] refact: adjust head file and sync log --- source/libs/sync/inc/syncEnv.h | 2 - source/libs/sync/inc/syncRaftLog.h | 1 - source/libs/sync/inc/syncRaftStore.h | 21 ++--- source/libs/sync/src/syncRaftLog.c | 17 ++-- source/libs/sync/src/syncRaftStore.c | 81 +----------------- .../sync/test/sync_test_lib/inc/syncTest.h | 10 +++ .../sync_test_lib/src/syncRaftStoreDebug.c | 84 +++++++++++++++++++ 7 files changed, 113 insertions(+), 103 deletions(-) create mode 100644 source/libs/sync/test/sync_test_lib/src/syncRaftStoreDebug.c diff --git a/source/libs/sync/inc/syncEnv.h b/source/libs/sync/inc/syncEnv.h index cf4e3309f1..628e8874b4 100644 --- a/source/libs/sync/inc/syncEnv.h +++ b/source/libs/sync/inc/syncEnv.h @@ -30,8 +30,6 @@ extern "C" { #define ELECT_TIMER_MS_RANGE (ELECT_TIMER_MS_MAX - ELECT_TIMER_MS_MIN) #define HEARTBEAT_TIMER_MS 1000 -#define EMPTY_RAFT_ID ((SRaftId){.addr = 0, .vgId = 0}) - typedef struct SSyncEnv { uint8_t isStart; diff --git a/source/libs/sync/inc/syncRaftLog.h b/source/libs/sync/inc/syncRaftLog.h index 17ea36c640..de8bd81b30 100644 --- a/source/libs/sync/inc/syncRaftLog.h +++ b/source/libs/sync/inc/syncRaftLog.h @@ -38,7 +38,6 @@ SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode); void logStoreDestory(SSyncLogStore* pLogStore); SyncIndex logStoreFirstIndex(SSyncLogStore* pLogStore); - SyncIndex logStoreWalCommitVer(SSyncLogStore* pLogStore); SyncIndex raftLogWriteIndex(struct SSyncLogStore* pLogStore); diff --git a/source/libs/sync/inc/syncRaftStore.h b/source/libs/sync/inc/syncRaftStore.h index 077e6a7658..bb6405f6b2 100644 --- a/source/libs/sync/inc/syncRaftStore.h +++ b/source/libs/sync/inc/syncRaftStore.h @@ -25,6 +25,8 @@ extern "C" { #define RAFT_STORE_BLOCK_SIZE 512 #define RAFT_STORE_PATH_LEN (TSDB_FILENAME_LEN * 2) +#define EMPTY_RAFT_ID ((SRaftId){.addr = 0, .vgId = 0}) + typedef struct SRaftStore { SyncTerm currentTerm; SRaftId voteFor; @@ -38,20 +40,11 @@ int32_t raftStorePersist(SRaftStore *pRaftStore); int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len); int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len); -bool raftStoreHasVoted(SRaftStore *pRaftStore); -void raftStoreVote(SRaftStore *pRaftStore, SRaftId *pRaftId); -void raftStoreClearVote(SRaftStore *pRaftStore); -void raftStoreNextTerm(SRaftStore *pRaftStore); -void raftStoreSetTerm(SRaftStore *pRaftStore, SyncTerm term); -int32_t raftStoreFromJson(SRaftStore *pRaftStore, cJSON *pJson); -cJSON *raftStore2Json(SRaftStore *pRaftStore); -char *raftStore2Str(SRaftStore *pRaftStore); - -// for debug ------------------- -void raftStorePrint(SRaftStore *pObj); -void raftStorePrint2(char *s, SRaftStore *pObj); -void raftStoreLog(SRaftStore *pObj); -void raftStoreLog2(char *s, SRaftStore *pObj); +bool raftStoreHasVoted(SRaftStore *pRaftStore); +void raftStoreVote(SRaftStore *pRaftStore, SRaftId *pRaftId); +void raftStoreClearVote(SRaftStore *pRaftStore); +void raftStoreNextTerm(SRaftStore *pRaftStore); +void raftStoreSetTerm(SRaftStore *pRaftStore, SyncTerm term); #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index d4a1d35c74..2cdb18a109 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -13,33 +13,33 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncRaftLog.h" #include "syncRaftCfg.h" #include "syncRaftStore.h" -//------------------------------- // log[m .. n] // public function static int32_t raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex); - static int32_t raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry); static int32_t raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIndex); static bool raftLogExist(struct SSyncLogStore* pLogStore, SyncIndex index); static int32_t raftLogUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index); static SyncIndex raftlogCommitIndex(SSyncLogStore* pLogStore); +static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry); -static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry); - -//------------------------------- SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) { - SSyncLogStore* pLogStore = taosMemoryMalloc(sizeof(SSyncLogStore)); - ASSERT(pLogStore != NULL); + SSyncLogStore* pLogStore = taosMemoryCalloc(1, sizeof(SSyncLogStore)); + if (pLogStore == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } pLogStore->pCache = taosLRUCacheInit(10 * 1024 * 1024, 1, .5); if (pLogStore->pCache == NULL) { - terrno = TSDB_CODE_WAL_OUT_OF_MEMORY; taosMemoryFree(pLogStore); + terrno = TSDB_CODE_WAL_OUT_OF_MEMORY; return NULL; } @@ -96,7 +96,6 @@ void logStoreDestory(SSyncLogStore* pLogStore) { } } -//------------------------------- // log[m .. n] static int32_t raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex) { ASSERT(snapshotIndex >= 0); diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index 1c96e70858..e328ed3d31 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -15,8 +15,6 @@ #define _DEFAULT_SOURCE #include "syncRaftStore.h" -#include "cJSON.h" -#include "syncEnv.h" #include "syncUtil.h" // private function @@ -27,22 +25,19 @@ static bool raftStoreFileExist(char *path); SRaftStore *raftStoreOpen(const char *path) { int32_t ret; - SRaftStore *pRaftStore = taosMemoryMalloc(sizeof(SRaftStore)); + SRaftStore *pRaftStore = taosMemoryCalloc(1, sizeof(SRaftStore)); if (pRaftStore == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } - memset(pRaftStore, 0, sizeof(*pRaftStore)); + snprintf(pRaftStore->path, sizeof(pRaftStore->path), "%s", path); - - char storeBuf[RAFT_STORE_BLOCK_SIZE] = {0}; - memset(storeBuf, 0, sizeof(storeBuf)); - if (!raftStoreFileExist(pRaftStore->path)) { ret = raftStoreInit(pRaftStore); ASSERT(ret == 0); } + char storeBuf[RAFT_STORE_BLOCK_SIZE] = {0}; pRaftStore->pFile = taosOpenFile(path, TD_FILE_READ | TD_FILE_WRITE); ASSERT(pRaftStore->pFile != NULL); @@ -73,9 +68,7 @@ static int32_t raftStoreInit(SRaftStore *pRaftStore) { } int32_t raftStoreClose(SRaftStore *pRaftStore) { - if (pRaftStore == NULL) { - return 0; - } + if (pRaftStore == NULL) return 0; taosCloseFile(&pRaftStore->pFile); taosMemoryFree(pRaftStore); @@ -183,69 +176,3 @@ void raftStoreSetTerm(SRaftStore *pRaftStore, SyncTerm term) { pRaftStore->currentTerm = term; raftStorePersist(pRaftStore); } - -int32_t raftStoreFromJson(SRaftStore *pRaftStore, cJSON *pJson) { return 0; } - -cJSON *raftStore2Json(SRaftStore *pRaftStore) { - char u64buf[128] = {0}; - cJSON *pRoot = cJSON_CreateObject(); - - if (pRaftStore != NULL) { - snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", pRaftStore->currentTerm); - cJSON_AddStringToObject(pRoot, "currentTerm", u64buf); - - cJSON *pVoteFor = cJSON_CreateObject(); - snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", pRaftStore->voteFor.addr); - cJSON_AddStringToObject(pVoteFor, "addr", u64buf); - { - uint64_t u64 = pRaftStore->voteFor.addr; - char host[128] = {0}; - uint16_t port; - syncUtilU642Addr(u64, host, sizeof(host), &port); - cJSON_AddStringToObject(pVoteFor, "addr_host", host); - cJSON_AddNumberToObject(pVoteFor, "addr_port", port); - } - cJSON_AddNumberToObject(pVoteFor, "vgId", pRaftStore->voteFor.vgId); - cJSON_AddItemToObject(pRoot, "voteFor", pVoteFor); - - int hasVoted = raftStoreHasVoted(pRaftStore); - cJSON_AddNumberToObject(pRoot, "hasVoted", hasVoted); - } - - cJSON *pJson = cJSON_CreateObject(); - cJSON_AddItemToObject(pJson, "SRaftStore", pRoot); - return pJson; -} - -char *raftStore2Str(SRaftStore *pRaftStore) { - cJSON *pJson = raftStore2Json(pRaftStore); - char *serialized = cJSON_Print(pJson); - cJSON_Delete(pJson); - return serialized; -} - -// for debug ------------------- -void raftStorePrint(SRaftStore *pObj) { - char *serialized = raftStore2Str(pObj); - printf("raftStorePrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized); - fflush(NULL); - taosMemoryFree(serialized); -} - -void raftStorePrint2(char *s, SRaftStore *pObj) { - char *serialized = raftStore2Str(pObj); - printf("raftStorePrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized); - fflush(NULL); - taosMemoryFree(serialized); -} -void raftStoreLog(SRaftStore *pObj) { - char *serialized = raftStore2Str(pObj); - sTrace("raftStoreLog | len:%d | %s", (int32_t)strlen(serialized), serialized); - taosMemoryFree(serialized); -} - -void raftStoreLog2(char *s, SRaftStore *pObj) { - char *serialized = raftStore2Str(pObj); - sTrace("raftStoreLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized); - taosMemoryFree(serialized); -} diff --git a/source/libs/sync/test/sync_test_lib/inc/syncTest.h b/source/libs/sync/test/sync_test_lib/inc/syncTest.h index a1881bf1b3..34ce8c3175 100644 --- a/source/libs/sync/test/sync_test_lib/inc/syncTest.h +++ b/source/libs/sync/test/sync_test_lib/inc/syncTest.h @@ -63,6 +63,14 @@ void raftEntryCachePrint2(char* s, SRaftEntryCache* pObj); void raftEntryCacheLog(SRaftEntryCache* pObj); void raftEntryCacheLog2(char* s, SRaftEntryCache* pObj); +int32_t raftStoreFromJson(SRaftStore* pRaftStore, cJSON* pJson); +cJSON* raftStore2Json(SRaftStore* pRaftStore); +char* raftStore2Str(SRaftStore* pRaftStore); +void raftStorePrint(SRaftStore* pObj); +void raftStorePrint2(char* s, SRaftStore* pObj); +void raftStoreLog(SRaftStore* pObj); +void raftStoreLog2(char* s, SRaftStore* pObj); + cJSON* syncAppendEntriesBatch2Json(const SyncAppendEntriesBatch* pMsg); char* syncAppendEntriesBatch2Str(const SyncAppendEntriesBatch* pMsg); void syncAppendEntriesBatchPrint(const SyncAppendEntriesBatch* pMsg); @@ -99,6 +107,8 @@ char* snapshotSender2Str(SSyncSnapshotSender* pSender); cJSON* snapshotReceiver2Json(SSyncSnapshotReceiver* pReceiver); char* snapshotReceiver2Str(SSyncSnapshotReceiver* pReceiver); + + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/test/sync_test_lib/src/syncRaftStoreDebug.c b/source/libs/sync/test/sync_test_lib/src/syncRaftStoreDebug.c new file mode 100644 index 0000000000..0c4dd95e48 --- /dev/null +++ b/source/libs/sync/test/sync_test_lib/src/syncRaftStoreDebug.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#define _DEFAULT_SOURCE +#include "syncTest.h" +#include "cJSON.h" + +int32_t raftStoreFromJson(SRaftStore *pRaftStore, cJSON *pJson) { return 0; } + +cJSON *raftStore2Json(SRaftStore *pRaftStore) { + char u64buf[128] = {0}; + cJSON *pRoot = cJSON_CreateObject(); + + if (pRaftStore != NULL) { + snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", pRaftStore->currentTerm); + cJSON_AddStringToObject(pRoot, "currentTerm", u64buf); + + cJSON *pVoteFor = cJSON_CreateObject(); + snprintf(u64buf, sizeof(u64buf), "%" PRIu64 "", pRaftStore->voteFor.addr); + cJSON_AddStringToObject(pVoteFor, "addr", u64buf); + { + uint64_t u64 = pRaftStore->voteFor.addr; + char host[128] = {0}; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pVoteFor, "addr_host", host); + cJSON_AddNumberToObject(pVoteFor, "addr_port", port); + } + cJSON_AddNumberToObject(pVoteFor, "vgId", pRaftStore->voteFor.vgId); + cJSON_AddItemToObject(pRoot, "voteFor", pVoteFor); + + int hasVoted = raftStoreHasVoted(pRaftStore); + cJSON_AddNumberToObject(pRoot, "hasVoted", hasVoted); + } + + cJSON *pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SRaftStore", pRoot); + return pJson; +} + +char *raftStore2Str(SRaftStore *pRaftStore) { + cJSON *pJson = raftStore2Json(pRaftStore); + char *serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ------------------- +void raftStorePrint(SRaftStore *pObj) { + char *serialized = raftStore2Str(pObj); + printf("raftStorePrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void raftStorePrint2(char *s, SRaftStore *pObj) { + char *serialized = raftStore2Str(pObj); + printf("raftStorePrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized); + fflush(NULL); + taosMemoryFree(serialized); +} +void raftStoreLog(SRaftStore *pObj) { + char *serialized = raftStore2Str(pObj); + sTrace("raftStoreLog | len:%d | %s", (int32_t)strlen(serialized), serialized); + taosMemoryFree(serialized); +} + +void raftStoreLog2(char *s, SRaftStore *pObj) { + char *serialized = raftStore2Str(pObj); + sTrace("raftStoreLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized); + taosMemoryFree(serialized); +} \ No newline at end of file From 5de865992a475ad76a31699e51b3f8758ec466e5 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 11 Nov 2022 12:09:31 +0800 Subject: [PATCH 3/3] refact: adjust head file and sync log --- source/libs/sync/inc/syncRaftCfg.h | 11 ++-------- source/libs/sync/src/syncRaftCfg.c | 22 +------------------ source/libs/sync/src/syncRaftEntry.c | 3 --- .../test/syncConfigChangeSnapshotTest.cpp | 3 ++- .../libs/sync/test/syncConfigChangeTest.cpp | 3 ++- source/libs/sync/test/syncTestTool.cpp | 4 ++-- .../sync/test/sync_test_lib/inc/syncTest.h | 3 +++ .../test/sync_test_lib/src/syncRaftCfgDebug.c | 16 ++++++++++++++ 8 files changed, 28 insertions(+), 37 deletions(-) diff --git a/source/libs/sync/inc/syncRaftCfg.h b/source/libs/sync/inc/syncRaftCfg.h index 6b6ca21399..823983e732 100644 --- a/source/libs/sync/inc/syncRaftCfg.h +++ b/source/libs/sync/inc/syncRaftCfg.h @@ -22,11 +22,9 @@ extern "C" { #include "syncInt.h" -#define CONFIG_FILE_LEN 2048 - +#define CONFIG_FILE_LEN 2048 #define MAX_CONFIG_INDEX_COUNT 256 -// SRaftCfgIndex ------------------------------------------ typedef struct SRaftCfgIndex { TdFilePtr pFile; char path[TSDB_FILENAME_LEN * 2]; @@ -44,11 +42,8 @@ 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; @@ -68,11 +63,9 @@ int32_t raftCfgClose(SRaftCfg *pRaftCfg); int32_t raftCfgPersist(SRaftCfg *pRaftCfg); int32_t raftCfgAddConfigIndex(SRaftCfg *pRaftCfg, SyncIndex configIndex); -cJSON *syncCfg2Json(SSyncCfg *pSyncCfg); -char *syncCfg2Str(SSyncCfg *pSyncCfg); void syncCfg2SimpleStr(const SSyncCfg *pCfg, char *str, int32_t bufLen); +cJSON *syncCfg2Json(SSyncCfg *pSyncCfg); int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg); -int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg); cJSON *raftCfg2Json(SRaftCfg *pRaftCfg); char *raftCfg2Str(SRaftCfg *pRaftCfg); diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c index 66c399d15a..b6b2616973 100644 --- a/source/libs/sync/src/syncRaftCfg.c +++ b/source/libs/sync/src/syncRaftCfg.c @@ -13,10 +13,8 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "syncRaftCfg.h" -#include "cJSON.h" -#include "syncEnv.h" -#include "syncUtil.h" // file must already exist! SRaftCfgIndex *raftCfgIndexOpen(const char *path) { @@ -242,13 +240,6 @@ cJSON *syncCfg2Json(SSyncCfg *pSyncCfg) { return pRoot; } -char *syncCfg2Str(SSyncCfg *pSyncCfg) { - cJSON *pJson = syncCfg2Json(pSyncCfg); - char *serialized = cJSON_Print(pJson); - cJSON_Delete(pJson); - return serialized; -} - int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg) { memset(pSyncCfg, 0, sizeof(SSyncCfg)); // cJSON *pJson = cJSON_GetObjectItem(pRoot, "SSyncCfg"); @@ -283,17 +274,6 @@ int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg) { return 0; } -int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg) { - cJSON *pRoot = cJSON_Parse(s); - ASSERT(pRoot != NULL); - - int32_t ret = syncCfgFromJson(pRoot, pSyncCfg); - ASSERT(ret == 0); - - cJSON_Delete(pRoot); - return 0; -} - cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) { cJSON *pRoot = cJSON_CreateObject(); cJSON_AddItemToObject(pRoot, "SSyncCfg", syncCfg2Json(&(pRaftCfg->cfg))); diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index 262a7b0879..540054e473 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -102,7 +102,6 @@ void syncEntry2OriginalRpc(const SSyncRaftEntry* pEntry, SRpcMsg* pRpcMsg) { memcpy(pRpcMsg->pCont, pEntry->data, pRpcMsg->contLen); } -//----------------------------------- SRaftEntryHashCache* raftCacheCreate(SSyncNode* pSyncNode, int32_t maxCount) { SRaftEntryHashCache* pCache = taosMemoryMalloc(sizeof(SRaftEntryHashCache)); if (pCache == NULL) { @@ -256,8 +255,6 @@ int32_t raftCacheClear(struct SRaftEntryHashCache* pCache) { return 0; } - -//----------------------------------- static char* keyFn(const void* pData) { SSyncRaftEntry* pEntry = (SSyncRaftEntry*)pData; return (char*)(&(pEntry->index)); diff --git a/source/libs/sync/test/syncConfigChangeSnapshotTest.cpp b/source/libs/sync/test/syncConfigChangeSnapshotTest.cpp index a6d33aa674..fb227dfad3 100644 --- a/source/libs/sync/test/syncConfigChangeSnapshotTest.cpp +++ b/source/libs/sync/test/syncConfigChangeSnapshotTest.cpp @@ -143,7 +143,8 @@ int32_t SnapshotDoWrite(struct SSyncFSM* pFsm, void* pWriter, void* pBuf, int32_ void RestoreFinishCb(struct SSyncFSM* pFsm) { sTrace("==callback== ==RestoreFinishCb=="); } void ReConfigCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta* cbMeta) { - sTrace("==callback== ==ReConfigCb== flag:0x%lX, index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 ", term:%" PRIu64, + sTrace("==callback== ==ReConfigCb== flag:0x%" PRIx64 ", index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 + ", term:%" PRIu64, cbMeta->flag, cbMeta->index, cbMeta->code, cbMeta->currentTerm, cbMeta->term); } diff --git a/source/libs/sync/test/syncConfigChangeTest.cpp b/source/libs/sync/test/syncConfigChangeTest.cpp index cf498933e1..23acadc0cc 100644 --- a/source/libs/sync/test/syncConfigChangeTest.cpp +++ b/source/libs/sync/test/syncConfigChangeTest.cpp @@ -73,7 +73,8 @@ int32_t GetSnapshotCb(const struct SSyncFSM* pFsm, SSnapshot* pSnapshot) { void RestoreFinishCb(struct SSyncFSM* pFsm) { sTrace("==callback== ==RestoreFinishCb=="); } void ReConfigCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta* cbMeta) { - sTrace("==callback== ==ReConfigCb== flag:0x%lX, index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 ", term:%" PRIu64, + sTrace("==callback== ==ReConfigCb== flag:%" PRIx64 ", index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 + ", term:%" PRIu64, cbMeta->flag, cbMeta->index, cbMeta->code, cbMeta->currentTerm, cbMeta->term); } diff --git a/source/libs/sync/test/syncTestTool.cpp b/source/libs/sync/test/syncTestTool.cpp index b0fe612b7f..e8d40d5054 100644 --- a/source/libs/sync/test/syncTestTool.cpp +++ b/source/libs/sync/test/syncTestTool.cpp @@ -148,8 +148,8 @@ void RestoreFinishCb(struct SSyncFSM* pFsm) { sTrace("==callback== ==RestoreFini void ReConfigCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta* cbMeta) { char* s = syncCfg2Str(&(cbMeta->newCfg)); - sTrace("==callback== ==ReConfigCb== flag:0x%lX, index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 ", term:%" PRIu64 - ", newCfg:%s", + sTrace("==callback== ==ReConfigCb== flag:%" PRIx64 ", index:%" PRId64 ", code:%d, currentTerm:%" PRIu64 + ", term:%" PRIu64 ", newCfg:%s", cbMeta->flag, cbMeta->index, cbMeta->code, cbMeta->currentTerm, cbMeta->term, s); taosMemoryFree(s); } diff --git a/source/libs/sync/test/sync_test_lib/inc/syncTest.h b/source/libs/sync/test/sync_test_lib/inc/syncTest.h index 34ce8c3175..50d9d12eee 100644 --- a/source/libs/sync/test/sync_test_lib/inc/syncTest.h +++ b/source/libs/sync/test/sync_test_lib/inc/syncTest.h @@ -49,6 +49,9 @@ void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj); void syncEntryLog(const SSyncRaftEntry* pObj); void syncEntryLog2(char* s, const SSyncRaftEntry* pObj); +char* syncCfg2Str(SSyncCfg* pSyncCfg); +int32_t syncCfgFromStr(const char* s, SSyncCfg* pSyncCfg); + cJSON* raftCache2Json(SRaftEntryHashCache* pObj); char* raftCache2Str(SRaftEntryHashCache* pObj); void raftCachePrint(SRaftEntryHashCache* pObj); diff --git a/source/libs/sync/test/sync_test_lib/src/syncRaftCfgDebug.c b/source/libs/sync/test/sync_test_lib/src/syncRaftCfgDebug.c index 88b697fbf1..50ecc5d478 100644 --- a/source/libs/sync/test/sync_test_lib/src/syncRaftCfgDebug.c +++ b/source/libs/sync/test/sync_test_lib/src/syncRaftCfgDebug.c @@ -16,4 +16,20 @@ #define _DEFAULT_SOURCE #include "syncTest.h" +char *syncCfg2Str(SSyncCfg *pSyncCfg) { + cJSON *pJson = syncCfg2Json(pSyncCfg); + char *serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} +int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg) { + cJSON *pRoot = cJSON_Parse(s); + ASSERT(pRoot != NULL); + + int32_t ret = syncCfgFromJson(pRoot, pSyncCfg); + ASSERT(ret == 0); + + cJSON_Delete(pRoot); + return 0; +}