From 02f0f85aabb8f19a2d8d595aa991b0a12872c5e1 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Mon, 28 Feb 2022 14:10:34 +0800 Subject: [PATCH 01/19] sync modify timer --- include/libs/sync/sync.h | 4 +- source/libs/sync/inc/syncEnv.h | 10 ++-- source/libs/sync/inc/syncIO.h | 6 --- source/libs/sync/inc/syncInt.h | 38 ++++++++-------- source/libs/sync/inc/syncRaft.h | 4 ++ source/libs/sync/inc/syncRaftStore.h | 4 +- source/libs/sync/src/syncEnv.c | 47 ++++++++++++++++++- source/libs/sync/src/syncIO.c | 63 +++++++++++++++++--------- source/libs/sync/src/syncMain.c | 47 ++++++++++++++----- source/libs/sync/src/syncRaft.c | 4 ++ source/libs/sync/src/syncRaftStore.c | 16 +++---- source/libs/sync/test/syncEnvTest.cpp | 9 ++-- source/libs/sync/test/syncPingTest.cpp | 15 ++++-- source/libs/sync/test/syncTest.cpp | 4 +- 14 files changed, 187 insertions(+), 84 deletions(-) diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index a619e66622..5b387852fa 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -34,7 +34,9 @@ typedef enum { TAOS_SYNC_STATE_FOLLOWER = 0, TAOS_SYNC_STATE_CANDIDATE = 1, TAOS_SYNC_STATE_LEADER = 2, -} ESyncState; +} ESyncRole; + +typedef ESyncRole ESyncState; typedef struct SSyncBuffer { void* data; diff --git a/source/libs/sync/inc/syncEnv.h b/source/libs/sync/inc/syncEnv.h index f1c4327b69..960afe37b0 100644 --- a/source/libs/sync/inc/syncEnv.h +++ b/source/libs/sync/inc/syncEnv.h @@ -26,19 +26,21 @@ extern "C" { #include "syncInt.h" #include "taosdef.h" #include "trpc.h" +#include "ttimer.h" typedef struct SSyncEnv { - void *pTimer; - void *pTimerManager; + tmr_h pEnvTickTimer; + tmr_h pTimerManager; + char name[128]; } SSyncEnv; int32_t syncEnvStart(); int32_t syncEnvStop(); -static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv); +tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void *param); -static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv); +void syncEnvStopTimer(tmr_h *pTimer); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index 4b788efd79..49edeb638b 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -61,12 +61,6 @@ int32_t syncIOStop(); int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg); SSyncIO *syncIOCreate(); -static int32_t doSyncIOStart(SSyncIO *io); -static int32_t doSyncIOStop(SSyncIO *io); -static int32_t doSyncIOPing(SSyncIO *io); -static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static int32_t doSyncIODestroy(SSyncIO *io); - #ifdef __cplusplus } #endif diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index ad8484662a..a8451b3dc4 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -26,6 +26,7 @@ extern "C" { #include "sync.h" #include "taosdef.h" #include "tlog.h" +#include "ttimer.h" extern int32_t sDebugFlag; @@ -87,15 +88,28 @@ typedef struct SyncAppendEntries SyncAppendEntries; struct SyncAppendEntriesReply; typedef struct SyncAppendEntriesReply SyncAppendEntriesReply; +typedef struct SRaftId { + SyncNodeId addr; + SyncGroupId vgId; +} SRaftId; + typedef struct SSyncNode { - int8_t replica; - int8_t quorum; + int8_t replica; + int8_t quorum; + int32_t refCount; + int64_t rid; SyncGroupId vgId; SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; - SRaft* pRaft; + ESyncRole role; + SRaftId raftId; + SSyncFSM* pFsm; + + tmr_h pPingTimer; + tmr_h pElectionTimer; + tmr_h pHeartbeatTimer; int32_t (*FpPing)(struct SSyncNode* ths, const SyncPing* pMsg); @@ -123,23 +137,11 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo); void syncNodeClose(SSyncNode* pSyncNode); -static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg); +void syncNodePingAll(SSyncNode* pSyncNode); -static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg); +void syncNodePingPeers(SSyncNode* pSyncNode); -static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg); - -static int32_t doSyncNodeRequestVote(struct SSyncNode* ths, const SyncRequestVote* pMsg); - -static int32_t onSyncNodeRequestVote(struct SSyncNode* ths, SyncRequestVote* pMsg); - -static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVoteReply* pMsg); - -static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg); - -static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg); - -static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); +void syncNodePingSelf(SSyncNode* pSyncNode); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncRaft.h b/source/libs/sync/inc/syncRaft.h index a247a29fc4..bc5cf26a4c 100644 --- a/source/libs/sync/inc/syncRaft.h +++ b/source/libs/sync/inc/syncRaft.h @@ -27,6 +27,8 @@ extern "C" { #include "syncMessage.h" #include "taosdef.h" +#if 0 + typedef struct SRaftId { SyncNodeId addr; SyncGroupId vgId; @@ -82,6 +84,8 @@ int32_t raftPropose(SRaft* pRaft, const SSyncBuffer* pBuf, bool isWeak); static int raftSendMsg(SRaftId destRaftId, const void* pMsg, const SRaft* pRaft); +#endif + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/inc/syncRaftStore.h b/source/libs/sync/inc/syncRaftStore.h index 0fdbd7a150..c54f1a1c83 100644 --- a/source/libs/sync/inc/syncRaftStore.h +++ b/source/libs/sync/inc/syncRaftStore.h @@ -34,8 +34,8 @@ extern "C" { typedef struct SRaftStore { SyncTerm currentTerm; SRaftId voteFor; - //FileFd fd; - char path[RAFT_STORE_PATH_LEN]; + // FileFd fd; + char path[RAFT_STORE_PATH_LEN]; } SRaftStore; SRaftStore *raftStoreOpen(const char *path); diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index e71cf55cb1..0447fc246a 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -18,6 +18,14 @@ SSyncEnv *gSyncEnv = NULL; +// local function ----------------- +static void syncEnvTick(void *param, void *tmrId); +static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv); +static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv); +static tmr_h doSyncEnvStartTimer(SSyncEnv *pSyncEnv, TAOS_TMR_CALLBACK fp, int mseconds, void *param); +static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer); +// -------------------------------- + int32_t syncEnvStart() { int32_t ret; gSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv)); @@ -31,6 +39,41 @@ int32_t syncEnvStop() { return ret; } -static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv) { return 0; } +tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void *param) { + return doSyncEnvStartTimer(gSyncEnv, fp, mseconds, param); +} -static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) { return 0; } +void syncEnvStopTimer(tmr_h *pTimer) { doSyncEnvStopTimer(gSyncEnv, pTimer); } + +// local function ----------------- +static void syncEnvTick(void *param, void *tmrId) { + SSyncEnv *pSyncEnv = (SSyncEnv *)param; + sTrace("syncEnvTick ... name:%s ", pSyncEnv->name); + + pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager); +} + +static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv) { + snprintf(pSyncEnv->name, sizeof(pSyncEnv->name), "SyncEnv_%p", pSyncEnv); + + // start tmr thread + pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV"); + + pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager); + + sTrace("SyncEnv start ok, name:%s", pSyncEnv->name); + + return 0; +} + +static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) { + taosTmrCleanUp(pSyncEnv->pTimerManager); + return 0; +} + +static tmr_h doSyncEnvStartTimer(SSyncEnv *pSyncEnv, TAOS_TMR_CALLBACK fp, int mseconds, void *param) { + return taosTmrStart(fp, mseconds, pSyncEnv, pSyncEnv->pTimerManager); +} + +static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer) {} +// -------------------------------- \ No newline at end of file diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index bb20d11e37..34eaf5fe1e 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -22,12 +22,49 @@ SSyncIO *gSyncIO = NULL; +// local function ------------ +static void *syncConsumer(void *param); +static int retrieveAuthInfo(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); +static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static void processRequestMsg(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static void syncTick(void *param, void *tmrId); + +static int32_t doSyncIOStart(SSyncIO *io); +static int32_t doSyncIOStop(SSyncIO *io); +static int32_t doSyncIOPing(SSyncIO *io); +static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static int32_t doSyncIODestroy(SSyncIO *io); +// ---------------------------- + int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { return 0; } -int32_t syncIOStart() { return 0; } +int32_t syncIOStart() { + gSyncIO = syncIOCreate(); + assert(gSyncIO != NULL); + + return 0; +} int32_t syncIOStop() { return 0; } +SSyncIO *syncIOCreate() { + SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); + memset(io, 0, sizeof(*io)); + + io->pMsgQ = taosOpenQueue(); + io->pQset = taosOpenQset(); + taosAddIntoQset(io->pQset, io->pMsgQ, NULL); + + io->start = doSyncIOStart; + io->stop = doSyncIOStop; + io->ping = doSyncIOPing; + io->onMsg = doSyncIOOnMsg; + io->destroy = doSyncIODestroy; + + return io; +} + +// local function ------------ static void syncTick(void *param, void *tmrId) { SSyncIO *io = (SSyncIO *)param; sDebug("syncTick ... "); @@ -46,14 +83,15 @@ static void syncTick(void *param, void *tmrId) { taosWriteQitem(io->pMsgQ, pTemp); - io->syncTimer = taosTmrStart(syncTick, 1000, io, io->syncTimerManager); + bool b = taosTmrReset(syncTick, 1000, io, io->syncTimerManager, io->syncTimer); + assert(b); } -void *syncConsumer(void *param) { +static void *syncConsumer(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg * pRpcMsg, rpcMsg; + SRpcMsg *pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); @@ -114,23 +152,6 @@ static void processRequestMsg(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { taosWriteQitem(io->pMsgQ, pTemp); } -SSyncIO *syncIOCreate() { - SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); - memset(io, 0, sizeof(*io)); - - io->pMsgQ = taosOpenQueue(); - io->pQset = taosOpenQset(); - taosAddIntoQset(io->pQset, io->pMsgQ, NULL); - - io->start = doSyncIOStart; - io->stop = doSyncIOStop; - io->ping = doSyncIOPing; - io->onMsg = doSyncIOOnMsg; - io->destroy = doSyncIODestroy; - - return io; -} - static int32_t doSyncIOStart(SSyncIO *io) { taosBlockSIGPIPE(); diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index bd2952505e..75b40dc625 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -18,9 +18,26 @@ #include "syncInt.h" #include "syncRaft.h" -int32_t syncInit() { return 0; } +static int32_t tsNodeRefId = -1; -void syncCleanUp() {} +// ------ local funciton --------- +static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg); +static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg); +static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg); +static int32_t doSyncNodeRequestVote(struct SSyncNode* ths, const SyncRequestVote* pMsg); +static int32_t onSyncNodeRequestVote(struct SSyncNode* ths, SyncRequestVote* pMsg); +static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVoteReply* pMsg); +static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg); +static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg); +static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); +// --------------------------------- + +int32_t syncInit() { + sTrace("syncInit ok"); + return 0; +} + +void syncCleanUp() { sTrace("syncCleanUp ok"); } int64_t syncStart(const SSyncInfo* pSyncInfo) { SSyncNode* pSyncNode = syncNodeOpen(pSyncInfo); @@ -59,51 +76,57 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { void syncNodeClose(SSyncNode* pSyncNode) { assert(pSyncNode != NULL); - raftClose(pSyncNode->pRaft); free(pSyncNode); } +void syncNodePingAll(SSyncNode* pSyncNode) { sTrace("syncNodePingAll %p ", pSyncNode); } + +void syncNodePingPeers(SSyncNode* pSyncNode) {} + +void syncNodePingSelf(SSyncNode* pSyncNode) {} + +// ------ local funciton --------- static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg) { - int32_t ret = ths->pRaft->FpPing(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg) { - int32_t ret = ths->pRaft->FpOnPing(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg) { - int32_t ret = ths->pRaft->FpOnPingReply(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t doSyncNodeRequestVote(struct SSyncNode* ths, const SyncRequestVote* pMsg) { - int32_t ret = ths->pRaft->FpRequestVote(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodeRequestVote(struct SSyncNode* ths, SyncRequestVote* pMsg) { - int32_t ret = ths->pRaft->FpOnRequestVote(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVoteReply* pMsg) { - int32_t ret = ths->pRaft->FpOnRequestVoteReply(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg) { - int32_t ret = ths->pRaft->FpAppendEntries(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg) { - int32_t ret = ths->pRaft->FpOnAppendEntries(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg) { - int32_t ret = ths->pRaft->FpOnAppendEntriesReply(ths->pRaft, pMsg); + int32_t ret = 0; return ret; } \ No newline at end of file diff --git a/source/libs/sync/src/syncRaft.c b/source/libs/sync/src/syncRaft.c index 9f139730d1..b07c6ea797 100644 --- a/source/libs/sync/src/syncRaft.c +++ b/source/libs/sync/src/syncRaft.c @@ -16,6 +16,8 @@ #include "syncRaft.h" #include "sync.h" +#if 0 + SRaft* raftOpen(SRaftId raftId, SSyncFSM* pFsm) { SRaft* pRaft = (SRaft*)malloc(sizeof(SRaft)); assert(pRaft != NULL); @@ -64,3 +66,5 @@ static int32_t onRaftAppendEntriesReply(struct SRaft* ths, RaftAppendEntriesRepl int32_t raftPropose(SRaft* pRaft, const SSyncBuffer* pBuf, bool isWeak) { return 0; } static int raftSendMsg(SRaftId destRaftId, const void* pMsg, const SRaft* pRaft) { return 0; } + +#endif \ No newline at end of file diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index 964cc78490..1c88be72e7 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -18,24 +18,22 @@ // to complie success: FileIO interface is modified -SRaftStore *raftStoreOpen(const char *path) { return NULL;} +SRaftStore *raftStoreOpen(const char *path) { return NULL; } -static int32_t raftStoreInit(SRaftStore *pRaftStore) { return 0;} +static int32_t raftStoreInit(SRaftStore *pRaftStore) { return 0; } -int32_t raftStoreClose(SRaftStore *pRaftStore) { return 0;} +int32_t raftStoreClose(SRaftStore *pRaftStore) { return 0; } -int32_t raftStorePersist(SRaftStore *pRaftStore) { return 0;} +int32_t raftStorePersist(SRaftStore *pRaftStore) { return 0; } -static bool raftStoreFileExist(char *path) { return 0;} +static bool raftStoreFileExist(char *path) { return 0; } -int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0;} +int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0; } -int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0;} +int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0; } void raftStorePrint(SRaftStore *pRaftStore) {} - - #if 0 SRaftStore *raftStoreOpen(const char *path) { diff --git a/source/libs/sync/test/syncEnvTest.cpp b/source/libs/sync/test/syncEnvTest.cpp index 1d050e7094..1ac4357c5a 100644 --- a/source/libs/sync/test/syncEnvTest.cpp +++ b/source/libs/sync/test/syncEnvTest.cpp @@ -34,19 +34,20 @@ void doSync() { } int main() { - //taosInitLog((char*)"syncEnvTest.log", 100000, 10); + // taosInitLog((char*)"syncEnvTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; + int32_t ret; logTest(); - int32_t ret = syncIOStart(); - assert(ret == 0); + // ret = syncIOStart(); + // assert(ret == 0); ret = syncEnvStart(); assert(ret == 0); - doSync(); + // doSync(); while (1) { taosMsleep(1000); diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index e62d051946..110c8c472f 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -13,7 +13,7 @@ void logTest() { sFatal("--- sync log test: fatal"); } -void doSync() { +SSyncNode* doSync() { SSyncFSM* pFsm; SSyncInfo syncInfo; @@ -40,10 +40,17 @@ void doSync() { gSyncIO->FpOnPing = pSyncNode->FpOnPing; gSyncIO->pSyncNode = pSyncNode; + + return pSyncNode; +} + +void timerPingAll(void *param, void *tmrId) { + SSyncNode *pSyncNode = (SSyncNode*)param; + syncNodePingAll(pSyncNode); } int main() { - //taosInitLog((char*)"syncPingTest.log", 100000, 10); + // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; @@ -55,7 +62,9 @@ int main() { ret = syncEnvStart(); assert(ret == 0); - doSync(); + SSyncNode* pSyncNode = doSync(); + + pSyncNode->pPingTimer = syncEnvStartTimer(timerPingAll, 1000, pSyncNode); while (1) { taosMsleep(1000); diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index 0f72fd822f..0843a48bb8 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -1,8 +1,8 @@ #include +#include "gtest/gtest.h" #include "syncIO.h" #include "syncInt.h" #include "syncRaftStore.h" -#include "gtest/gtest.h" void *pingFunc(void *param) { SSyncIO *io = (SSyncIO *)param; @@ -15,7 +15,7 @@ void *pingFunc(void *param) { } int main() { - //taosInitLog((char *)"syncTest.log", 100000, 10); + // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; From c9efe3dea71e1be4eb9de260ce0210295edb6f73 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Mon, 28 Feb 2022 16:36:57 +0800 Subject: [PATCH 02/19] sync modify timer --- source/libs/sync/inc/syncEnv.h | 4 ++ source/libs/sync/inc/syncIO.h | 13 +++-- source/libs/sync/inc/syncInt.h | 37 ++++++++++--- source/libs/sync/src/syncEnv.c | 2 +- source/libs/sync/src/syncIO.c | 75 +++++++++++++------------- source/libs/sync/src/syncMain.c | 48 +++++++++++++++-- source/libs/sync/test/syncPingTest.cpp | 12 +++-- 7 files changed, 131 insertions(+), 60 deletions(-) diff --git a/source/libs/sync/inc/syncEnv.h b/source/libs/sync/inc/syncEnv.h index 960afe37b0..c4735d4481 100644 --- a/source/libs/sync/inc/syncEnv.h +++ b/source/libs/sync/inc/syncEnv.h @@ -28,12 +28,16 @@ extern "C" { #include "trpc.h" #include "ttimer.h" +#define TIMER_MAX_MS 0x7FFFFFFF + typedef struct SSyncEnv { tmr_h pEnvTickTimer; tmr_h pTimerManager; char name[128]; } SSyncEnv; +extern SSyncEnv *gSyncEnv; + int32_t syncEnvStart(); int32_t syncEnvStop(); diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index 49edeb638b..ec887aa855 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -30,10 +30,10 @@ extern "C" { #include "trpc.h" typedef struct SSyncIO { - void * serverRpc; - void * clientRpc; + void *serverRpc; + void *clientRpc; STaosQueue *pMsgQ; - STaosQset * pQset; + STaosQset *pQset; pthread_t tid; int8_t isStart; @@ -56,10 +56,9 @@ typedef struct SSyncIO { extern SSyncIO *gSyncIO; -int32_t syncIOStart(); -int32_t syncIOStop(); -int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg); -SSyncIO *syncIOCreate(); +int32_t syncIOStart(); +int32_t syncIOStop(); +int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index a8451b3dc4..e02e2d7374 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -88,6 +88,9 @@ typedef struct SyncAppendEntries SyncAppendEntries; struct SyncAppendEntriesReply; typedef struct SyncAppendEntriesReply SyncAppendEntriesReply; +struct SSyncEnv; +typedef struct SSyncEnv SSyncEnv; + typedef struct SRaftId { SyncNodeId addr; SyncGroupId vgId; @@ -103,32 +106,46 @@ typedef struct SSyncNode { SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; + SNodeInfo me; + SNodeInfo peers[TSDB_MAX_REPLICA]; + int32_t peersNum; + ESyncRole role; SRaftId raftId; SSyncFSM* pFsm; - tmr_h pPingTimer; - tmr_h pElectionTimer; - tmr_h pHeartbeatTimer; + tmr_h pPingTimer; + int32_t pingTimerMS; + uint8_t pingTimerStart; + TAOS_TMR_CALLBACK FpPingTimer; // Timer Fp + uint64_t pingTimerCounter; - int32_t (*FpPing)(struct SSyncNode* ths, const SyncPing* pMsg); + tmr_h pElectTimer; + int32_t electTimerMS; + uint8_t electTimerStart; + TAOS_TMR_CALLBACK FpElectTimer; // Timer Fp + uint64_t electTimerCounter; + tmr_h pHeartbeatTimer; + int32_t heartbeatTimerMS; + uint8_t heartbeatTimerStart; + TAOS_TMR_CALLBACK FpHeartbeatTimer; // Timer Fp + uint64_t heartbeatTimerCounter; + + // callback int32_t (*FpOnPing)(struct SSyncNode* ths, SyncPing* pMsg); int32_t (*FpOnPingReply)(struct SSyncNode* ths, SyncPingReply* pMsg); - int32_t (*FpRequestVote)(struct SSyncNode* ths, const SyncRequestVote* pMsg); - int32_t (*FpOnRequestVote)(struct SSyncNode* ths, SyncRequestVote* pMsg); int32_t (*FpOnRequestVoteReply)(struct SSyncNode* ths, SyncRequestVoteReply* pMsg); - int32_t (*FpAppendEntries)(struct SSyncNode* ths, const SyncAppendEntries* pMsg); - int32_t (*FpOnAppendEntries)(struct SSyncNode* ths, SyncAppendEntries* pMsg); int32_t (*FpOnAppendEntriesReply)(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); + // passed from outside int32_t (*FpSendMsg)(void* handle, const SEpSet* pEpSet, SRpcMsg* pMsg); } SSyncNode; @@ -143,6 +160,10 @@ void syncNodePingPeers(SSyncNode* pSyncNode); void syncNodePingSelf(SSyncNode* pSyncNode); +int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode); + +int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 0447fc246a..bfdf2a9c5e 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -59,7 +59,7 @@ static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv) { // start tmr thread pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV"); - pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager); + // pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager); sTrace("SyncEnv start ok, name:%s", pSyncEnv->name); diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 34eaf5fe1e..5c27f9ec3a 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -23,17 +23,18 @@ SSyncIO *gSyncIO = NULL; // local function ------------ -static void *syncConsumer(void *param); -static int retrieveAuthInfo(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); -static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static void processRequestMsg(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static void syncTick(void *param, void *tmrId); - static int32_t doSyncIOStart(SSyncIO *io); static int32_t doSyncIOStop(SSyncIO *io); static int32_t doSyncIOPing(SSyncIO *io); static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static int32_t doSyncIODestroy(SSyncIO *io); + +static SSyncIO *syncIOCreate(); +static void * syncIOConsumer(void *param); +static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); +static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static void syncIOTick(void *param, void *tmrId); // ---------------------------- int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { return 0; } @@ -47,27 +48,10 @@ int32_t syncIOStart() { int32_t syncIOStop() { return 0; } -SSyncIO *syncIOCreate() { - SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); - memset(io, 0, sizeof(*io)); - - io->pMsgQ = taosOpenQueue(); - io->pQset = taosOpenQset(); - taosAddIntoQset(io->pQset, io->pMsgQ, NULL); - - io->start = doSyncIOStart; - io->stop = doSyncIOStop; - io->ping = doSyncIOPing; - io->onMsg = doSyncIOOnMsg; - io->destroy = doSyncIODestroy; - - return io; -} - // local function ------------ -static void syncTick(void *param, void *tmrId) { +static void syncIOTick(void *param, void *tmrId) { SSyncIO *io = (SSyncIO *)param; - sDebug("syncTick ... "); + sDebug("syncIOTick ... "); SRpcMsg rpcMsg; rpcMsg.pCont = rpcMallocCont(10); @@ -83,15 +67,15 @@ static void syncTick(void *param, void *tmrId) { taosWriteQitem(io->pMsgQ, pTemp); - bool b = taosTmrReset(syncTick, 1000, io, io->syncTimerManager, io->syncTimer); + bool b = taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, io->syncTimer); assert(b); } -static void *syncConsumer(void *param) { +static void *syncIOConsumer(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg *pRpcMsg, rpcMsg; + SRpcMsg * pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); @@ -129,19 +113,19 @@ static void *syncConsumer(void *param) { return NULL; } -static int retrieveAuthInfo(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { +static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { // app shall retrieve the auth info based on meterID from DB or a data file // demo code here only for simple demo int ret = 0; return ret; } -static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - sDebug("processResponse ... "); +static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { + sDebug("syncIODoReply ... "); rpcFreeCont(pMsg->pCont); } -static void processRequestMsg(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { +static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { SSyncIO *io = pParent; SRpcMsg *pTemp; @@ -152,6 +136,23 @@ static void processRequestMsg(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { taosWriteQitem(io->pMsgQ, pTemp); } +static SSyncIO *syncIOCreate() { + SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); + memset(io, 0, sizeof(*io)); + + io->pMsgQ = taosOpenQueue(); + io->pQset = taosOpenQset(); + taosAddIntoQset(io->pQset, io->pMsgQ, NULL); + + io->start = doSyncIOStart; + io->stop = doSyncIOStop; + io->ping = doSyncIOPing; + io->onMsg = doSyncIOOnMsg; + io->destroy = doSyncIODestroy; + + return io; +} + static int32_t doSyncIOStart(SSyncIO *io) { taosBlockSIGPIPE(); @@ -164,7 +165,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { rpcInit.localPort = 0; rpcInit.label = "SYNC-IO-CLIENT"; rpcInit.numOfThreads = 1; - rpcInit.cfp = processResponse; + rpcInit.cfp = syncIODoReply; rpcInit.sessions = 100; rpcInit.idleTime = 100; rpcInit.user = "sync-io"; @@ -187,10 +188,10 @@ static int32_t doSyncIOStart(SSyncIO *io) { rpcInit.localPort = 38000; rpcInit.label = "SYNC-IO-SERVER"; rpcInit.numOfThreads = 1; - rpcInit.cfp = processRequestMsg; + rpcInit.cfp = syncIODoRequest; rpcInit.sessions = 1000; rpcInit.idleTime = 2 * 1500; - rpcInit.afp = retrieveAuthInfo; + rpcInit.afp = syncIOAuth; rpcInit.parent = io; rpcInit.connType = TAOS_CONN_SERVER; @@ -206,7 +207,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { // start consumer thread { - if (pthread_create(&io->tid, NULL, syncConsumer, io) != 0) { + if (pthread_create(&io->tid, NULL, syncIOConsumer, io) != 0) { sError("failed to create sync consumer thread since %s", strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; @@ -215,7 +216,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { // start tmr thread io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); - io->syncTimer = taosTmrStart(syncTick, 1000, io, io->syncTimerManager); + io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); return 0; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 75b40dc625..7cfc470f60 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -15,6 +15,7 @@ #include #include "sync.h" +#include "syncEnv.h" #include "syncInt.h" #include "syncRaft.h" @@ -30,6 +31,7 @@ static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVote static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg); static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg); static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); +static void syncNodePingTimerCb(void* param, void* tmrId); // --------------------------------- int32_t syncInit() { @@ -58,16 +60,19 @@ void syncGetNodesRole(int64_t rid, SNodesRole* pNodeRole) {} SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { SSyncNode* pSyncNode = (SSyncNode*)malloc(sizeof(SSyncNode)); assert(pSyncNode != NULL); + memset(pSyncNode, 0, sizeof(SSyncNode)); + + pSyncNode->pPingTimer = NULL; + pSyncNode->pingTimerMS = 1000; + atomic_store_8(&pSyncNode->pingTimerStart, 0); + pSyncNode->FpPingTimer = syncNodePingTimerCb; + pSyncNode->pingTimerCounter = 0; pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; - - pSyncNode->FpPing = doSyncNodePing; pSyncNode->FpOnPing = onSyncNodePing; pSyncNode->FpOnPingReply = onSyncNodePingReply; - pSyncNode->FpRequestVote = doSyncNodeRequestVote; pSyncNode->FpOnRequestVote = onSyncNodeRequestVote; pSyncNode->FpOnRequestVoteReply = onSyncNodeRequestVoteReply; - pSyncNode->FpAppendEntries = doSyncNodeAppendEntries; pSyncNode->FpOnAppendEntries = onSyncNodeAppendEntries; pSyncNode->FpOnAppendEntriesReply = onSyncNodeAppendEntriesReply; @@ -85,6 +90,25 @@ void syncNodePingPeers(SSyncNode* pSyncNode) {} void syncNodePingSelf(SSyncNode* pSyncNode) {} +int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode) { + if (pSyncNode->pPingTimer == NULL) { + pSyncNode->pPingTimer = + taosTmrStart(pSyncNode->FpPingTimer, pSyncNode->pingTimerCounter, pSyncNode, gSyncEnv->pTimerManager); + } else { + taosTmrReset(pSyncNode->FpPingTimer, pSyncNode->pingTimerCounter, pSyncNode, gSyncEnv->pTimerManager, + &pSyncNode->pPingTimer); + } + + atomic_store_8(&pSyncNode->pingTimerStart, 1); + return 0; +} + +int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { + atomic_store_8(&pSyncNode->pingTimerStart, 0); + pSyncNode->pingTimerCounter = TIMER_MAX_MS; + return 0; +} + // ------ local funciton --------- static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg) { int32_t ret = 0; @@ -129,4 +153,20 @@ static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg) { int32_t ret = 0; return ret; +} + +static void syncNodePingTimerCb(void* param, void* tmrId) { + SSyncNode* pSyncNode = (SSyncNode*)param; + if (atomic_load_8(&pSyncNode->pingTimerStart)) { + ++(pSyncNode->pingTimerCounter); + // pSyncNode->pingTimerMS += 100; + + sTrace("pSyncNode->pingTimerCounter:%lu, pSyncNode->pingTimerMS:%d, pSyncNode->pPingTimer:%p, tmrId:%p ", + pSyncNode->pingTimerCounter, pSyncNode->pingTimerMS, pSyncNode->pPingTimer, tmrId); + + taosTmrReset(syncNodePingTimerCb, pSyncNode->pingTimerMS, pSyncNode, &gSyncEnv->pTimerManager, + &pSyncNode->pPingTimer); + + syncNodePingSelf(pSyncNode); + } } \ No newline at end of file diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 110c8c472f..bf0d342ca4 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -44,8 +44,8 @@ SSyncNode* doSync() { return pSyncNode; } -void timerPingAll(void *param, void *tmrId) { - SSyncNode *pSyncNode = (SSyncNode*)param; +void timerPingAll(void* param, void* tmrId) { + SSyncNode* pSyncNode = (SSyncNode*)param; syncNodePingAll(pSyncNode); } @@ -64,7 +64,13 @@ int main() { SSyncNode* pSyncNode = doSync(); - pSyncNode->pPingTimer = syncEnvStartTimer(timerPingAll, 1000, pSyncNode); + ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); + + taosMsleep(5000); + + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); while (1) { taosMsleep(1000); From eaf6142c793e0ccdfcf116c866d7856df062d6fd Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Mon, 28 Feb 2022 17:47:47 +0800 Subject: [PATCH 03/19] sync modify timer --- include/libs/sync/sync.h | 4 ++- source/libs/sync/inc/syncEnv.h | 7 +++-- source/libs/sync/inc/syncIO.h | 6 ++-- source/libs/sync/inc/syncInt.h | 3 +- source/libs/sync/inc/syncUtil.h | 40 ++++++++++++++++++++++++++ source/libs/sync/src/syncMain.c | 38 ++++++++++++++++++++++-- source/libs/sync/src/syncUtil.c | 22 ++++++++++++++ source/libs/sync/test/syncPingTest.cpp | 1 + 8 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 source/libs/sync/inc/syncUtil.h create mode 100644 source/libs/sync/src/syncUtil.c diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index 5b387852fa..c6f4a4f5c0 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -137,7 +137,9 @@ typedef struct SSyncInfo { SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; SSyncFSM* pFsm; - int32_t (*FpSendMsg)(void* handle, const SEpSet* pEpSet, SRpcMsg* pMsg); + + void* rpcClient; + int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg); } SSyncInfo; diff --git a/source/libs/sync/inc/syncEnv.h b/source/libs/sync/inc/syncEnv.h index c4735d4481..7e60822a28 100644 --- a/source/libs/sync/inc/syncEnv.h +++ b/source/libs/sync/inc/syncEnv.h @@ -34,17 +34,18 @@ typedef struct SSyncEnv { tmr_h pEnvTickTimer; tmr_h pTimerManager; char name[128]; + } SSyncEnv; -extern SSyncEnv *gSyncEnv; +extern SSyncEnv* gSyncEnv; int32_t syncEnvStart(); int32_t syncEnvStop(); -tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void *param); +tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void* param); -void syncEnvStopTimer(tmr_h *pTimer); +void syncEnvStopTimer(tmr_h* pTimer); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index ec887aa855..6723fa66c5 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -30,10 +30,10 @@ extern "C" { #include "trpc.h" typedef struct SSyncIO { - void *serverRpc; - void *clientRpc; + void * serverRpc; + void * clientRpc; STaosQueue *pMsgQ; - STaosQset *pQset; + STaosQset * pQset; pthread_t tid; int8_t isStart; diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index e02e2d7374..b2922a8676 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -146,7 +146,8 @@ typedef struct SSyncNode { int32_t (*FpOnAppendEntriesReply)(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); // passed from outside - int32_t (*FpSendMsg)(void* handle, const SEpSet* pEpSet, SRpcMsg* pMsg); + void* rpcClient; + int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg); } SSyncNode; diff --git a/source/libs/sync/inc/syncUtil.h b/source/libs/sync/inc/syncUtil.h new file mode 100644 index 0000000000..f3d4c2ed07 --- /dev/null +++ b/source/libs/sync/inc/syncUtil.h @@ -0,0 +1,40 @@ +/* + * 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 . + */ + +#ifndef _TD_LIBS_SYNC_UTIL_H +#define _TD_LIBS_SYNC_UTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "syncInt.h" +#include "syncMessage.h" +#include "taosdef.h" + +void nodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet); + +void raftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet); + +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_LIBS_SYNC_UTIL_H*/ diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 7cfc470f60..9b7d068ada 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -18,10 +18,14 @@ #include "syncEnv.h" #include "syncInt.h" #include "syncRaft.h" +#include "syncUtil.h" static int32_t tsNodeRefId = -1; // ------ local funciton --------- +static int32_t doSyncNodeSendMsgById(SRaftId* destRaftId, struct SSyncNode* pSyncNode, SRpcMsg* pMsg); +static int32_t doSyncNodeSendMsgByInfo(SNodeInfo* nodeInfo, struct SSyncNode* pSyncNode, SRpcMsg* pMsg); + static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg); static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg); static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg); @@ -68,7 +72,9 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { pSyncNode->FpPingTimer = syncNodePingTimerCb; pSyncNode->pingTimerCounter = 0; + pSyncNode->rpcClient = pSyncInfo->rpcClient; pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; + pSyncNode->FpOnPing = onSyncNodePing; pSyncNode->FpOnPingReply = onSyncNodePingReply; pSyncNode->FpOnRequestVote = onSyncNodeRequestVote; @@ -84,7 +90,11 @@ void syncNodeClose(SSyncNode* pSyncNode) { free(pSyncNode); } -void syncNodePingAll(SSyncNode* pSyncNode) { sTrace("syncNodePingAll %p ", pSyncNode); } +void syncNodePingAll(SSyncNode* pSyncNode) { + sTrace("syncNodePingAll %p ", pSyncNode); + SyncPing msg; + doSyncNodePing(pSyncNode, &msg); +} void syncNodePingPeers(SSyncNode* pSyncNode) {} @@ -110,8 +120,30 @@ int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { } // ------ local funciton --------- + +static int32_t doSyncNodeSendMsgById(SRaftId* destRaftId, struct SSyncNode* pSyncNode, SRpcMsg* pMsg) { + SEpSet epSet; + raftId2EpSet(destRaftId, &epSet); + pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + return 0; +} + +static int32_t doSyncNodeSendMsgByInfo(SNodeInfo* nodeInfo, struct SSyncNode* pSyncNode, SRpcMsg* pMsg) { + SEpSet epSet; + nodeInfo2EpSet(nodeInfo, &epSet); + + pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + return 0; +} + static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg) { - int32_t ret = 0; + int32_t ret; + for (int i = 0; i < ths->syncCfg.replicaNum; ++i) { + SRpcMsg* rpcMsg; + syncPing2RpcMsg(pMsg, rpcMsg); + doSyncNodeSendMsgByInfo(&ths->syncCfg.nodeInfo[i], ths, rpcMsg); + } + return ret; } @@ -167,6 +199,6 @@ static void syncNodePingTimerCb(void* param, void* tmrId) { taosTmrReset(syncNodePingTimerCb, pSyncNode->pingTimerMS, pSyncNode, &gSyncEnv->pTimerManager, &pSyncNode->pPingTimer); - syncNodePingSelf(pSyncNode); + syncNodePingAll(pSyncNode); } } \ No newline at end of file diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c new file mode 100644 index 0000000000..d68b1def4d --- /dev/null +++ b/source/libs/sync/src/syncUtil.c @@ -0,0 +1,22 @@ +/* + * 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 . + */ + +#include "syncUtil.h" + +void nodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) {} + +void raftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) {} + +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) {} \ No newline at end of file diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index bf0d342ca4..43196bdd1f 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -18,6 +18,7 @@ SSyncNode* doSync() { SSyncInfo syncInfo; syncInfo.vgId = 1; + syncInfo.rpcClient = gSyncIO->clientRpc; syncInfo.FpSendMsg = syncIOSendMsg; syncInfo.pFsm = pFsm; snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./test_sync_ping"); From d57320031fd92607f7401ca59fccdf049e8ea1d1 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 15:16:44 +0800 Subject: [PATCH 04/19] sync ping --- include/libs/sync/sync.h | 14 ++-- source/libs/sync/inc/syncInt.h | 16 ++-- source/libs/sync/inc/syncMessage.h | 72 +++++++++++----- source/libs/sync/inc/syncUtil.h | 23 ++++- source/libs/sync/src/syncMain.c | 130 +++++++++++++++++------------ source/libs/sync/src/syncMessage.c | 130 ++++++++++++++++++++++++++++- source/libs/sync/src/syncUtil.c | 61 +++++++++++++- 7 files changed, 348 insertions(+), 98 deletions(-) diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index c6f4a4f5c0..cd04783dbc 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -71,15 +71,15 @@ typedef struct SSyncFSM { // when value in pBuf finish a raft flow, FpCommitCb is called, code indicates the result // user can do something according to the code and isWeak. for example, write data into tsdb - void (*FpCommitCb)(struct SSyncFSM* pFsm, const SSyncBuffer* pBuf, SyncIndex index, bool isWeak, int32_t code); + void (*FpCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pBuf, SyncIndex index, bool isWeak, int32_t code); // when value in pBuf has been written into local log store, FpPreCommitCb is called, code indicates the result // user can do something according to the code and isWeak. for example, write data into tsdb - void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SSyncBuffer* pBuf, SyncIndex index, bool isWeak, int32_t code); + void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pBuf, SyncIndex index, bool isWeak, int32_t code); // when log entry is updated by a new one, FpRollBackCb is called // user can do something to roll back. for example, delete data from tsdb, or just ignore it - void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SSyncBuffer* pBuf, SyncIndex index, bool isWeak, int32_t code); + void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SRpcMsg* pBuf, SyncIndex index, bool isWeak, int32_t code); // user should implement this function, use "data" to take snapshot into "snapshot" int32_t (*FpTakeSnapshot)(SSnapshot* snapshot); @@ -95,10 +95,10 @@ typedef struct SSyncLogStore { void* data; // append one log entry - int32_t (*appendEntry)(struct SSyncLogStore* pLogStore, SSyncBuffer* pBuf); + int32_t (*appendEntry)(struct SSyncLogStore* pLogStore, SRpcMsg* pBuf); // get one log entry, user need to free pBuf->data - int32_t (*getEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncBuffer* pBuf); + int32_t (*getEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SRpcMsg* pBuf); // update log store commit index with "index" int32_t (*updateCommitIndex)(struct SSyncLogStore* pLogStore, SyncIndex index); @@ -153,8 +153,8 @@ int64_t syncStart(const SSyncInfo* pSyncInfo); void syncStop(int64_t rid); int32_t syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg); -// int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pBuf, bool isWeak); -int32_t syncForwardToPeer(int64_t rid, const SSyncBuffer* pBuf, bool isWeak); +int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pBuf, bool isWeak); +// int32_t syncForwardToPeer(int64_t rid, const SSyncBuffer* pBuf, bool isWeak); ESyncState syncGetMyRole(int64_t rid); void syncGetNodesRole(int64_t rid, SNodesRole* pNodeRole); diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index b2922a8676..82108acf7b 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -92,8 +92,8 @@ struct SSyncEnv; typedef struct SSyncEnv SSyncEnv; typedef struct SRaftId { - SyncNodeId addr; - SyncGroupId vgId; + SyncNodeId addr; // typedef uint64_t SyncNodeId; + SyncGroupId vgId; // typedef int32_t SyncGroupId; } SRaftId; typedef struct SSyncNode { @@ -133,17 +133,17 @@ typedef struct SSyncNode { uint64_t heartbeatTimerCounter; // callback - int32_t (*FpOnPing)(struct SSyncNode* ths, SyncPing* pMsg); + int32_t (*FpOnPing)(SSyncNode* ths, SyncPing* pMsg); - int32_t (*FpOnPingReply)(struct SSyncNode* ths, SyncPingReply* pMsg); + int32_t (*FpOnPingReply)(SSyncNode* ths, SyncPingReply* pMsg); - int32_t (*FpOnRequestVote)(struct SSyncNode* ths, SyncRequestVote* pMsg); + int32_t (*FpOnRequestVote)(SSyncNode* ths, SyncRequestVote* pMsg); - int32_t (*FpOnRequestVoteReply)(struct SSyncNode* ths, SyncRequestVoteReply* pMsg); + int32_t (*FpOnRequestVoteReply)(SSyncNode* ths, SyncRequestVoteReply* pMsg); - int32_t (*FpOnAppendEntries)(struct SSyncNode* ths, SyncAppendEntries* pMsg); + int32_t (*FpOnAppendEntries)(SSyncNode* ths, SyncAppendEntries* pMsg); - int32_t (*FpOnAppendEntriesReply)(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); + int32_t (*FpOnAppendEntriesReply)(SSyncNode* ths, SyncAppendEntriesReply* pMsg); // passed from outside void* rpcClient; diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 2ee5e0109c..0de8e8cf4b 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -27,6 +27,7 @@ extern "C" { #include "syncRaftEntry.h" #include "taosdef.h" +// encode as uint64 typedef enum ESyncMessageType { SYNC_PING = 0, SYNC_PING_REPLY, @@ -38,29 +39,47 @@ typedef enum ESyncMessageType { SYNC_APPEND_ENTRIES_REPLY, } ESyncMessageType; +/* +typedef struct SRaftId { + SyncNodeId addr; // typedef uint64_t SyncNodeId; + SyncGroupId vgId; // typedef int32_t SyncGroupId; +} SRaftId; +*/ + typedef struct SyncPing { - ESyncMessageType msgType; - const SSyncBuffer *pData; -} SyncPing, RaftPing; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + uint32_t dataLen; + char* data; +} SyncPing; + +#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) typedef struct SyncPingReply { - ESyncMessageType msgType; - const SSyncBuffer *pData; -} SyncPingReply, RaftPingReply; + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + uint32_t dataLen; + char* data; +} SyncPingReply; typedef struct SyncClientRequest { - ESyncMessageType msgType; - const SSyncBuffer *pData; - int64_t seqNum; - bool isWeak; -} SyncClientRequest, RaftClientRequest; + ESyncMessageType msgType; + char* data; + uint32_t dataLen; + int64_t seqNum; + bool isWeak; +} SyncClientRequest; typedef struct SyncClientRequestReply { - ESyncMessageType msgType; - int32_t errCode; - const SSyncBuffer *pErrMsg; - const SSyncBuffer *pLeaderHint; -} SyncClientRequestReply, RaftClientRequestReply; + ESyncMessageType msgType; + int32_t errCode; + SSyncBuffer* pErrMsg; + SSyncBuffer* pLeaderHint; +} SyncClientRequestReply; typedef struct SyncRequestVote { ESyncMessageType msgType; @@ -69,7 +88,7 @@ typedef struct SyncRequestVote { SyncGroupId vgId; SyncIndex lastLogIndex; SyncTerm lastLogTerm; -} SyncRequestVote, RaftRequestVote; +} SyncRequestVote; typedef struct SyncRequestVoteReply { ESyncMessageType msgType; @@ -77,7 +96,7 @@ typedef struct SyncRequestVoteReply { SyncNodeId nodeId; SyncGroupId vgId; bool voteGranted; -} SyncRequestVoteReply, RaftRequestVoteReply; +} SyncRequestVoteReply; typedef struct SyncAppendEntries { ESyncMessageType msgType; @@ -86,9 +105,9 @@ typedef struct SyncAppendEntries { SyncIndex prevLogIndex; SyncTerm prevLogTerm; int32_t entryCount; - SSyncRaftEntry * logEntries; + SSyncRaftEntry* logEntries; SyncIndex commitIndex; -} SyncAppendEntries, RaftAppendEntries; +} SyncAppendEntries; typedef struct SyncAppendEntriesReply { ESyncMessageType msgType; @@ -96,7 +115,18 @@ typedef struct SyncAppendEntriesReply { SyncNodeId nodeId; bool success; SyncIndex matchIndex; -} SyncAppendEntriesReply, RaftAppendEntriesReply; +} SyncAppendEntriesReply; + +// ---- message build ---- +SyncPing* syncPingBuild(uint32_t dataLen); + +void syncPingDestroy(SyncPing* pSyncPing); + +void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen); + +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); + +void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncUtil.h b/source/libs/sync/inc/syncUtil.h index f3d4c2ed07..93d2c12525 100644 --- a/source/libs/sync/inc/syncUtil.h +++ b/source/libs/sync/inc/syncUtil.h @@ -27,11 +27,28 @@ extern "C" { #include "syncMessage.h" #include "taosdef.h" -void nodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet); +// ---- encode / decode -void raftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet); +uint64_t syncUtilAddr2U64(const char* host, uint16_t port); -void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); +void syncUtilU642Addr(uint64_t u64, char* host, size_t len, uint16_t* port); + +void syncUtilnodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet); + +void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet); + +void syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId); + +// ---- SSyncBuffer ---- +#if 0 +void syncUtilbufBuild(SSyncBuffer* syncBuf, size_t len); + +void syncUtilbufDestroy(SSyncBuffer* syncBuf); + +void syncUtilbufCopy(const SSyncBuffer* src, SSyncBuffer* dest); + +void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest); +#endif #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 9b7d068ada..4d9e5887cd 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -23,19 +23,20 @@ static int32_t tsNodeRefId = -1; // ------ local funciton --------- -static int32_t doSyncNodeSendMsgById(SRaftId* destRaftId, struct SSyncNode* pSyncNode, SRpcMsg* pMsg); -static int32_t doSyncNodeSendMsgByInfo(SNodeInfo* nodeInfo, struct SSyncNode* pSyncNode, SRpcMsg* pMsg); - -static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg); -static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg); -static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg); -static int32_t doSyncNodeRequestVote(struct SSyncNode* ths, const SyncRequestVote* pMsg); -static int32_t onSyncNodeRequestVote(struct SSyncNode* ths, SyncRequestVote* pMsg); -static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVoteReply* pMsg); -static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg); -static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg); -static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg); +static int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg); +static int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg); static void syncNodePingTimerCb(void* param, void* tmrId); + +static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg); +static int32_t syncNodeRequestVote(SSyncNode* ths, const SyncRequestVote* pMsg); +static int32_t syncNodeAppendEntries(SSyncNode* ths, const SyncAppendEntries* pMsg); + +static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); +static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); +static int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg); +static int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg); +static int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg); +static int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* pMsg); // --------------------------------- int32_t syncInit() { @@ -55,7 +56,9 @@ void syncStop(int64_t rid) {} int32_t syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg) { return 0; } -int32_t syncForwardToPeer(int64_t rid, const SSyncBuffer* pBuf, bool isWeak) { return 0; } +// int32_t syncForwardToPeer(int64_t rid, const SSyncBuffer* pBuf, bool isWeak) { return 0; } + +int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pBuf, bool isWeak) { return 0; } ESyncState syncGetMyRole(int64_t rid) { return TAOS_SYNC_STATE_LEADER; } @@ -75,12 +78,12 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { pSyncNode->rpcClient = pSyncInfo->rpcClient; pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; - pSyncNode->FpOnPing = onSyncNodePing; - pSyncNode->FpOnPingReply = onSyncNodePingReply; - pSyncNode->FpOnRequestVote = onSyncNodeRequestVote; - pSyncNode->FpOnRequestVoteReply = onSyncNodeRequestVoteReply; - pSyncNode->FpOnAppendEntries = onSyncNodeAppendEntries; - pSyncNode->FpOnAppendEntriesReply = onSyncNodeAppendEntriesReply; + pSyncNode->FpOnPing = syncNodeOnPingCb; + pSyncNode->FpOnPingReply = syncNodeOnPingReplyCb; + pSyncNode->FpOnRequestVote = syncNodeOnRequestVoteCb; + pSyncNode->FpOnRequestVoteReply = syncNodeOnRequestVoteReplyCb; + pSyncNode->FpOnAppendEntries = syncNodeOnAppendEntriesCb; + pSyncNode->FpOnAppendEntriesReply = syncNodeOnAppendEntriesReplyCb; return pSyncNode; } @@ -92,13 +95,35 @@ void syncNodeClose(SSyncNode* pSyncNode) { void syncNodePingAll(SSyncNode* pSyncNode) { sTrace("syncNodePingAll %p ", pSyncNode); - SyncPing msg; - doSyncNodePing(pSyncNode, &msg); + int32_t ret = 0; + for (int i = 0; i < pSyncNode->syncCfg.replicaNum; ++i) { + SyncPing* pSyncPing; + SRaftId raftId; + syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &raftId); + ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + assert(ret == 0); + } } -void syncNodePingPeers(SSyncNode* pSyncNode) {} +void syncNodePingPeers(SSyncNode* pSyncNode) { + int32_t ret = 0; + for (int i = 0; i < pSyncNode->peersNum; ++i) { + SyncPing* pSyncPing; + SRaftId raftId; + syncUtilnodeInfo2raftId(&pSyncNode->peers[i], pSyncNode->vgId, &raftId); + ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + assert(ret == 0); + } +} -void syncNodePingSelf(SSyncNode* pSyncNode) {} +void syncNodePingSelf(SSyncNode* pSyncNode) { + int32_t ret = 0; + SyncPing* pSyncPing; + SRaftId raftId; + syncUtilnodeInfo2raftId(&pSyncNode->me, pSyncNode->vgId, &raftId); + ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + assert(ret == 0); +} int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode) { if (pSyncNode->pPingTimer == NULL) { @@ -120,69 +145,64 @@ int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { } // ------ local funciton --------- +static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg) { + int32_t ret = 0; + SRpcMsg* rpcMsg; + syncPing2RpcMsg(pMsg, rpcMsg); + syncNodeSendMsgById(destRaftId, pSyncNode, rpcMsg); + return ret; +} -static int32_t doSyncNodeSendMsgById(SRaftId* destRaftId, struct SSyncNode* pSyncNode, SRpcMsg* pMsg) { +static int32_t syncNodeRequestVote(SSyncNode* ths, const SyncRequestVote* pMsg) { + int32_t ret = 0; + return ret; +} + +static int32_t syncNodeAppendEntries(SSyncNode* ths, const SyncAppendEntries* pMsg) { + int32_t ret = 0; + return ret; +} + +static int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg) { SEpSet epSet; - raftId2EpSet(destRaftId, &epSet); + syncUtilraftId2EpSet(destRaftId, &epSet); pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); return 0; } -static int32_t doSyncNodeSendMsgByInfo(SNodeInfo* nodeInfo, struct SSyncNode* pSyncNode, SRpcMsg* pMsg) { +static int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg) { SEpSet epSet; - nodeInfo2EpSet(nodeInfo, &epSet); - + syncUtilnodeInfo2EpSet(nodeInfo, &epSet); pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); return 0; } -static int32_t doSyncNodePing(struct SSyncNode* ths, const SyncPing* pMsg) { - int32_t ret; - for (int i = 0; i < ths->syncCfg.replicaNum; ++i) { - SRpcMsg* rpcMsg; - syncPing2RpcMsg(pMsg, rpcMsg); - doSyncNodeSendMsgByInfo(&ths->syncCfg.nodeInfo[i], ths, rpcMsg); - } - - return ret; -} - -static int32_t onSyncNodePing(struct SSyncNode* ths, SyncPing* pMsg) { +static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { int32_t ret = 0; return ret; } -static int32_t onSyncNodePingReply(struct SSyncNode* ths, SyncPingReply* pMsg) { +static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { int32_t ret = 0; return ret; } -static int32_t doSyncNodeRequestVote(struct SSyncNode* ths, const SyncRequestVote* pMsg) { +static int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg) { int32_t ret = 0; return ret; } -static int32_t onSyncNodeRequestVote(struct SSyncNode* ths, SyncRequestVote* pMsg) { +static int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) { int32_t ret = 0; return ret; } -static int32_t onSyncNodeRequestVoteReply(struct SSyncNode* ths, SyncRequestVoteReply* pMsg) { +static int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { int32_t ret = 0; return ret; } -static int32_t doSyncNodeAppendEntries(struct SSyncNode* ths, const SyncAppendEntries* pMsg) { - int32_t ret = 0; - return ret; -} - -static int32_t onSyncNodeAppendEntries(struct SSyncNode* ths, SyncAppendEntries* pMsg) { - int32_t ret = 0; - return ret; -} - -static int32_t onSyncNodeAppendEntriesReply(struct SSyncNode* ths, SyncAppendEntriesReply* pMsg) { +static int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* pMsg) { int32_t ret = 0; return ret; } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 8937303725..baff4ed50b 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -15,5 +15,133 @@ #include "syncMessage.h" #include "syncRaft.h" +#include "tcoding.h" -void onMessage(SRaft *pRaft, void *pMsg) {} \ No newline at end of file +void onMessage(SRaft* pRaft, void* pMsg) {} + +// ---- message build ---- +SyncPing* syncPingBuild(uint32_t dataLen) { + uint32_t bytes = SYNC_PING_FIX_LEN + dataLen; + SyncPing* pSyncPing = malloc(bytes); + memset(pSyncPing, 0, bytes); + pSyncPing->bytes = bytes; + pSyncPing->msgType = SYNC_PING; + pSyncPing->dataLen = dataLen; +} + +void syncPingDestroy(SyncPing* pSyncPing) { + if (pSyncPing != NULL) { + free(pSyncPing); + } +} + +void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) { + assert(pSyncPing->bytes <= bufLen); + memcpy(buf, pSyncPing, pSyncPing->bytes); +} + +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { + uint32_t* pU32 = (uint32_t*)buf; + uint32_t bytes = *pU32; + pSyncPing = (SyncPing*)malloc(bytes); + memcpy(pSyncPing, buf, len); + assert(len == pSyncPing->bytes); + assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen); +} + +void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { + pRpcMsg->msgType = pSyncPing->msgType; + uint32_t bufLen = pSyncPing->bytes; + char* buf = malloc(bufLen); + syncPingSerialize(pSyncPing, buf, bufLen); + pRpcMsg->contLen = bufLen; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + memcpy(pRpcMsg->pCont, buf, pRpcMsg->contLen); + free(buf); +} + +/* +typedef struct SRaftId { + SyncNodeId addr; // typedef uint64_t SyncNodeId; + SyncGroupId vgId; // typedef int32_t SyncGroupId; +} SRaftId; + +typedef struct SyncPing { + uint32_t bytes; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + uint32_t dataLen; + char* data; +} SyncPing; +*/ + +/* +void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) { + *bufLen = sizeof(SyncPing) + pSyncPing->dataLen; + *ppBuf = (char*)malloc(*bufLen); + void* pStart = *ppBuf; + uint32_t allBytes = *bufLen; + + int len = 0; + len = taosEncodeFixedU32(&pStart, pSyncPing->msgType); + allBytes -= len; + assert(len > 0); + pStart += len; + + len = taosEncodeFixedU64(&pStart, pSyncPing->srcId.addr); + allBytes -= len; + assert(len > 0); + pStart += len; + + len = taosEncodeFixedI32(&pStart, pSyncPing->srcId.vgId); + allBytes -= len; + assert(len > 0); + pStart += len; + + len = taosEncodeFixedU64(&pStart, pSyncPing->destId.addr); + allBytes -= len; + assert(len > 0); + pStart += len; + + len = taosEncodeFixedI32(&pStart, pSyncPing->destId.vgId); + allBytes -= len; + assert(len > 0); + pStart += len; + + len = taosEncodeFixedU32(&pStart, pSyncPing->dataLen); + allBytes -= len; + assert(len > 0); + pStart += len; + + memcpy(pStart, pSyncPing->data, pSyncPing->dataLen); + allBytes -= pSyncPing->dataLen; + assert(allBytes == 0); +} + + +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { + void* pStart = (void*)buf; + uint64_t u64; + int32_t i32; + uint32_t u32; + + pStart = taosDecodeFixedU64(pStart, &u64); + pSyncPing->msgType = u64; + + pStart = taosDecodeFixedU64(pStart, &u64); + pSyncPing->srcId.addr = u64; + + pStart = taosDecodeFixedI32(pStart, &i32); + pSyncPing->srcId.vgId = i32; + + pStart = taosDecodeFixedU64(pStart, &u64); + pSyncPing->destId.addr = u64; + + pStart = taosDecodeFixedI32(pStart, &i32); + pSyncPing->destId.vgId = i32; + + pStart = taosDecodeFixedU32(pStart, &u32); + pSyncPing->dataLen = u32; +} +*/ \ No newline at end of file diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index d68b1def4d..080840bbf6 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -14,9 +14,64 @@ */ #include "syncUtil.h" +#include +#include +#include -void nodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) {} +// ---- encode / decode +uint64_t syncUtilAddr2U64(const char* host, uint16_t port) { + uint64_t u64; + uint32_t hostU32 = (uint32_t)inet_addr(host); + assert(hostU32 != (uint32_t)-1); + u64 = (((uint64_t)hostU32) << 32) | (((uint32_t)port) << 16); + return u64; +} -void raftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) {} +void syncUtilU642Addr(uint64_t u64, char* host, size_t len, uint16_t* port) { + uint32_t hostU32 = (uint32_t)((u64 >> 32) & 0x00000000FFFFFFFF); -void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) {} \ No newline at end of file + struct in_addr addr; + addr.s_addr = hostU32; + snprintf(host, len, "%s", inet_ntoa(addr)); + *port = (uint16_t)((u64 & 0x00000000FFFF0000) >> 16); +} + +void syncUtilnodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) { + pEpSet->inUse = 0; + addEpIntoEpSet(pEpSet, pNodeInfo->nodeFqdn, pNodeInfo->nodePort); +} + +void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) { + char host[TSDB_FQDN_LEN]; + uint16_t port; + + syncUtilU642Addr(raftId->addr, host, sizeof(host), &port); + pEpSet->inUse = 0; + addEpIntoEpSet(pEpSet, host, port); +} + +void syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId) { + raftId->addr = syncUtilAddr2U64(pNodeInfo->nodeFqdn, pNodeInfo->nodePort); + raftId->vgId = vgId; +} + +// ---- SSyncBuffer ----- +#if 0 +void syncUtilbufBuild(SSyncBuffer* syncBuf, size_t len) { + syncBuf->len = len; + syncBuf->data = malloc(syncBuf->len); +} + +void syncUtilbufDestroy(SSyncBuffer* syncBuf) { free(syncBuf->data); } + +void syncUtilbufCopy(const SSyncBuffer* src, SSyncBuffer* dest) { + dest->len = src->len; + dest->data = src->data; +} + +void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest) { + dest->len = src->len; + dest->data = malloc(dest->len); + memcpy(dest->data, src->data, dest->len); +} +#endif \ No newline at end of file From 48bed2020c6f24492852e26150d0f846cf3e6f0c Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 16:19:42 +0800 Subject: [PATCH 05/19] sync encode test --- source/libs/sync/inc/syncMessage.h | 7 ++- source/libs/sync/src/syncMessage.c | 39 ++++++++------- source/libs/sync/test/CMakeLists.txt | 14 ++++++ source/libs/sync/test/syncEncodeTest.cpp | 60 ++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 source/libs/sync/test/syncEncodeTest.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 0de8e8cf4b..5f73601576 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -23,6 +23,7 @@ extern "C" { #include #include #include +#include "cJSON.h" #include "sync.h" #include "syncRaftEntry.h" #include "taosdef.h" @@ -52,7 +53,7 @@ typedef struct SyncPing { SRaftId srcId; SRaftId destId; uint32_t dataLen; - char* data; + char data[]; } SyncPing; #define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) @@ -63,7 +64,7 @@ typedef struct SyncPingReply { SRaftId srcId; SRaftId destId; uint32_t dataLen; - char* data; + char data[]; } SyncPingReply; typedef struct SyncClientRequest { @@ -128,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); +cJSON* syncPing2Json(const SyncPing* pSyncPing); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index baff4ed50b..dc584c4e7b 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -41,9 +41,11 @@ void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) { } void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { + /* uint32_t* pU32 = (uint32_t*)buf; uint32_t bytes = *pU32; pSyncPing = (SyncPing*)malloc(bytes); + */ memcpy(pSyncPing, buf, len); assert(len == pSyncPing->bytes); assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen); @@ -60,23 +62,28 @@ void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { free(buf); } -/* -typedef struct SRaftId { - SyncNodeId addr; // typedef uint64_t SyncNodeId; - SyncGroupId vgId; // typedef int32_t SyncGroupId; -} SRaftId; +cJSON* syncPing2Json(const SyncPing* pSyncPing) { + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pSyncPing->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pSyncPing->msgType); -typedef struct SyncPing { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - uint32_t dataLen; - char* data; -} SyncPing; -*/ + cJSON* pSrcId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pSrcId, "addr", pSyncPing->srcId.addr); + cJSON_AddNumberToObject(pSrcId, "vgId", pSyncPing->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); -/* + cJSON* pDestId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pDestId, "addr", pSyncPing->destId.addr); + cJSON_AddNumberToObject(pDestId, "vgId", pSyncPing->destId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pDestId); + + cJSON_AddNumberToObject(pRoot, "dataLen", pSyncPing->dataLen); + cJSON_AddStringToObject(pRoot, "data", pSyncPing->data); + + return pRoot; +} + +#if 0 void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) { *bufLen = sizeof(SyncPing) + pSyncPing->dataLen; *ppBuf = (char*)malloc(*bufLen); @@ -144,4 +151,4 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { pStart = taosDecodeFixedU32(pStart, &u32); pSyncPing->dataLen = u32; } -*/ \ No newline at end of file +#endif \ No newline at end of file diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index e655ac01be..8e1c99d179 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(syncTest "") add_executable(syncEnvTest "") add_executable(syncPingTest "") +add_executable(syncEncodeTest "") target_sources(syncTest @@ -15,6 +16,10 @@ target_sources(syncPingTest PRIVATE "syncPingTest.cpp" ) +target_sources(syncEncodeTest + PRIVATE + "syncEncodeTest.cpp" +) target_include_directories(syncTest @@ -32,6 +37,11 @@ target_include_directories(syncPingTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncEncodeTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -46,6 +56,10 @@ target_link_libraries(syncPingTest sync gtest_main ) +target_link_libraries(syncEncodeTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp new file mode 100644 index 0000000000..1e8007e422 --- /dev/null +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -0,0 +1,60 @@ +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +#define PING_MSG_LEN 20 + +int main() { + // taosInitLog((char*)"syncPingTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "test ping"); + SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); + pSyncPing->srcId.addr = 1; + pSyncPing->srcId.vgId = 2; + pSyncPing->destId.addr = 3; + pSyncPing->destId.vgId = 4; + memcpy(pSyncPing->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPing2Json(pSyncPing); + char* serialized = cJSON_Print(pJson); + printf("SyncPing: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pSyncPing->bytes; + char* buf = (char*)malloc(bufLen); + syncPingSerialize(pSyncPing, buf, bufLen); + + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); + syncPingDeserialize(buf, bufLen, pSyncPing2); + + { + cJSON* pJson = syncPing2Json(pSyncPing2); + char* serialized = cJSON_Print(pJson); + printf("SyncPing2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingDestroy(pSyncPing); + syncPingDestroy(pSyncPing2); + free(buf); + + return 0; +} From 12050c9a2a0734290f83821e33ca2f283f3fb8ae Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 16:42:37 +0800 Subject: [PATCH 06/19] sync encode test --- source/libs/sync/inc/syncMessage.h | 2 + source/libs/sync/src/syncMessage.c | 12 +++--- source/libs/sync/test/syncEncodeTest.cpp | 55 +++++++++++++++++++++--- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 5f73601576..121314c589 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -129,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing); + cJSON* syncPing2Json(const SyncPing* pSyncPing); #ifdef __cplusplus diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index dc584c4e7b..80dd184018 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -53,13 +53,13 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { pRpcMsg->msgType = pSyncPing->msgType; - uint32_t bufLen = pSyncPing->bytes; - char* buf = malloc(bufLen); - syncPingSerialize(pSyncPing, buf, bufLen); - pRpcMsg->contLen = bufLen; + pRpcMsg->contLen = pSyncPing->bytes; pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); - memcpy(pRpcMsg->pCont, buf, pRpcMsg->contLen); - free(buf); + syncPingSerialize(pSyncPing, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing) { + syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pSyncPing); } cJSON* syncPing2Json(const SyncPing* pSyncPing) { diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index 1e8007e422..029e2bd496 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -15,10 +15,8 @@ void logTest() { #define PING_MSG_LEN 20 -int main() { - // taosInitLog((char*)"syncPingTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; +void test1() { + sTrace("test1: ----"); char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "test ping"); @@ -40,7 +38,7 @@ int main() { uint32_t bufLen = pSyncPing->bytes; char* buf = (char*)malloc(bufLen); syncPingSerialize(pSyncPing, buf, bufLen); - + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); syncPingDeserialize(buf, bufLen, pSyncPing2); @@ -55,6 +53,53 @@ int main() { syncPingDestroy(pSyncPing); syncPingDestroy(pSyncPing2); free(buf); +} + +void test2() { + sTrace("test2: ----"); + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "hello raft"); + SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); + pSyncPing->srcId.addr = 100; + pSyncPing->srcId.vgId = 200; + pSyncPing->destId.addr = 300; + pSyncPing->destId.vgId = 400; + memcpy(pSyncPing->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPing2Json(pSyncPing); + char* serialized = cJSON_Print(pJson); + printf("SyncPing: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncPing2RpcMsg(pSyncPing, &rpcMsg); + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); + syncPingFromRpcMsg(&rpcMsg, pSyncPing2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncPing2Json(pSyncPing2); + char* serialized = cJSON_Print(pJson); + printf("SyncPing2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingDestroy(pSyncPing); + syncPingDestroy(pSyncPing2); +} + +int main() { + // taosInitLog((char*)"syncPingTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + test1(); + test2(); return 0; } From 12c202aa316ad0f7288060b25a49047f676f76ee Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 18:35:07 +0800 Subject: [PATCH 07/19] sync encode test --- source/libs/sync/inc/syncMessage.h | 46 +++++-- source/libs/sync/src/syncMessage.c | 166 +++++++++++++++-------- source/libs/sync/test/syncEncodeTest.cpp | 137 +++++++++++++++---- 3 files changed, 251 insertions(+), 98 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 121314c589..a7de7b9019 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -58,6 +58,20 @@ typedef struct SyncPing { #define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) +SyncPing* syncPingBuild(uint32_t dataLen); + +void syncPingDestroy(SyncPing* pMsg); + +void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); + +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); + +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); + +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); + +cJSON* syncPing2Json(const SyncPing* pMsg); + typedef struct SyncPingReply { uint32_t bytes; uint32_t msgType; @@ -67,6 +81,23 @@ typedef struct SyncPingReply { char data[]; } SyncPingReply; +#define SYNC_PING_REPLY_FIX_LEN \ + (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) + +SyncPingReply* syncPingReplyBuild(uint32_t dataLen); + +void syncPingReplyDestroy(SyncPingReply* pMsg); + +void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); + +void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); + +void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); + +void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); + +cJSON* syncPingReply2Json(const SyncPingReply* pMsg); + typedef struct SyncClientRequest { ESyncMessageType msgType; char* data; @@ -118,21 +149,6 @@ typedef struct SyncAppendEntriesReply { SyncIndex matchIndex; } SyncAppendEntriesReply; -// ---- message build ---- -SyncPing* syncPingBuild(uint32_t dataLen); - -void syncPingDestroy(SyncPing* pSyncPing); - -void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen); - -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); - -void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); - -void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing); - -cJSON* syncPing2Json(const SyncPing* pSyncPing); - #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 80dd184018..3122aa66bb 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -19,136 +19,194 @@ void onMessage(SRaft* pRaft, void* pMsg) {} -// ---- message build ---- +// ---- message process SyncPing---- SyncPing* syncPingBuild(uint32_t dataLen) { uint32_t bytes = SYNC_PING_FIX_LEN + dataLen; - SyncPing* pSyncPing = malloc(bytes); - memset(pSyncPing, 0, bytes); - pSyncPing->bytes = bytes; - pSyncPing->msgType = SYNC_PING; - pSyncPing->dataLen = dataLen; + SyncPing* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_PING; + pMsg->dataLen = dataLen; } -void syncPingDestroy(SyncPing* pSyncPing) { - if (pSyncPing != NULL) { - free(pSyncPing); +void syncPingDestroy(SyncPing* pMsg) { + if (pMsg != NULL) { + free(pMsg); } } -void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) { - assert(pSyncPing->bytes <= bufLen); - memcpy(buf, pSyncPing, pSyncPing->bytes); +void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); } -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { - /* - uint32_t* pU32 = (uint32_t*)buf; - uint32_t bytes = *pU32; - pSyncPing = (SyncPing*)malloc(bytes); - */ - memcpy(pSyncPing, buf, len); - assert(len == pSyncPing->bytes); - assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen); +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); + assert(pMsg->bytes == SYNC_PING_FIX_LEN + pMsg->dataLen); } -void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { - pRpcMsg->msgType = pSyncPing->msgType; - pRpcMsg->contLen = pSyncPing->bytes; +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) { + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); - syncPingSerialize(pSyncPing, pRpcMsg->pCont, pRpcMsg->contLen); + syncPingSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); } -void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing) { - syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pSyncPing); +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) { + syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } -cJSON* syncPing2Json(const SyncPing* pSyncPing) { +cJSON* syncPing2Json(const SyncPing* pMsg) { cJSON* pRoot = cJSON_CreateObject(); - cJSON_AddNumberToObject(pRoot, "bytes", pSyncPing->bytes); - cJSON_AddNumberToObject(pRoot, "msgType", pSyncPing->msgType); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); cJSON* pSrcId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pSrcId, "addr", pSyncPing->srcId.addr); - cJSON_AddNumberToObject(pSrcId, "vgId", pSyncPing->srcId.vgId); + cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); cJSON_AddItemToObject(pRoot, "srcId", pSrcId); cJSON* pDestId = cJSON_CreateObject(); - cJSON_AddNumberToObject(pDestId, "addr", pSyncPing->destId.addr); - cJSON_AddNumberToObject(pDestId, "vgId", pSyncPing->destId.vgId); + cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); cJSON_AddItemToObject(pRoot, "srcId", pDestId); - cJSON_AddNumberToObject(pRoot, "dataLen", pSyncPing->dataLen); - cJSON_AddStringToObject(pRoot, "data", pSyncPing->data); + cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", pMsg->data); - return pRoot; + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncPing", pRoot); + return pJson; +} + +// ---- message process SyncPingReply---- +SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { + uint32_t bytes = SYNC_PING_REPLY_FIX_LEN + dataLen; + SyncPingReply* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_PING; + pMsg->dataLen = dataLen; +} + +void syncPingReplyDestroy(SyncPingReply* pMsg) { + if (pMsg != NULL) { + free(pMsg); + } +} + +void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); + assert(pMsg->bytes == SYNC_PING_FIX_LEN + pMsg->dataLen); +} + +void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) { + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncPingReplySerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg) { + syncPingReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); + + cJSON* pSrcId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pDestId); + + cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", pMsg->data); + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncPingReply", pRoot); + return pJson; } #if 0 -void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) { - *bufLen = sizeof(SyncPing) + pSyncPing->dataLen; +void syncPingSerialize(const SyncPing* pMsg, char** ppBuf, uint32_t* bufLen) { + *bufLen = sizeof(SyncPing) + pMsg->dataLen; *ppBuf = (char*)malloc(*bufLen); void* pStart = *ppBuf; uint32_t allBytes = *bufLen; int len = 0; - len = taosEncodeFixedU32(&pStart, pSyncPing->msgType); + len = taosEncodeFixedU32(&pStart, pMsg->msgType); allBytes -= len; assert(len > 0); pStart += len; - len = taosEncodeFixedU64(&pStart, pSyncPing->srcId.addr); + len = taosEncodeFixedU64(&pStart, pMsg->srcId.addr); allBytes -= len; assert(len > 0); pStart += len; - len = taosEncodeFixedI32(&pStart, pSyncPing->srcId.vgId); + len = taosEncodeFixedI32(&pStart, pMsg->srcId.vgId); allBytes -= len; assert(len > 0); pStart += len; - len = taosEncodeFixedU64(&pStart, pSyncPing->destId.addr); + len = taosEncodeFixedU64(&pStart, pMsg->destId.addr); allBytes -= len; assert(len > 0); pStart += len; - len = taosEncodeFixedI32(&pStart, pSyncPing->destId.vgId); + len = taosEncodeFixedI32(&pStart, pMsg->destId.vgId); allBytes -= len; assert(len > 0); pStart += len; - len = taosEncodeFixedU32(&pStart, pSyncPing->dataLen); + len = taosEncodeFixedU32(&pStart, pMsg->dataLen); allBytes -= len; assert(len > 0); pStart += len; - memcpy(pStart, pSyncPing->data, pSyncPing->dataLen); - allBytes -= pSyncPing->dataLen; + memcpy(pStart, pMsg->data, pMsg->dataLen); + allBytes -= pMsg->dataLen; assert(allBytes == 0); } -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { void* pStart = (void*)buf; uint64_t u64; int32_t i32; uint32_t u32; pStart = taosDecodeFixedU64(pStart, &u64); - pSyncPing->msgType = u64; + pMsg->msgType = u64; pStart = taosDecodeFixedU64(pStart, &u64); - pSyncPing->srcId.addr = u64; + pMsg->srcId.addr = u64; pStart = taosDecodeFixedI32(pStart, &i32); - pSyncPing->srcId.vgId = i32; + pMsg->srcId.vgId = i32; pStart = taosDecodeFixedU64(pStart, &u64); - pSyncPing->destId.addr = u64; + pMsg->destId.addr = u64; pStart = taosDecodeFixedI32(pStart, &i32); - pSyncPing->destId.vgId = i32; + pMsg->destId.vgId = i32; pStart = taosDecodeFixedU32(pStart, &u32); - pSyncPing->dataLen = u32; + pMsg->dataLen = u32; } #endif \ No newline at end of file diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index 029e2bd496..4deceb603e 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -16,59 +16,59 @@ void logTest() { #define PING_MSG_LEN 20 void test1() { - sTrace("test1: ----"); + sTrace("test1: ---- syncPingSerialize, syncPingDeserialize"); char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "test ping"); - SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); - pSyncPing->srcId.addr = 1; - pSyncPing->srcId.vgId = 2; - pSyncPing->destId.addr = 3; - pSyncPing->destId.vgId = 4; - memcpy(pSyncPing->data, msg, PING_MSG_LEN); + SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); + pMsg->srcId.addr = 1; + pMsg->srcId.vgId = 2; + pMsg->destId.addr = 3; + pMsg->destId.vgId = 4; + memcpy(pMsg->data, msg, PING_MSG_LEN); { - cJSON* pJson = syncPing2Json(pSyncPing); + cJSON* pJson = syncPing2Json(pMsg); char* serialized = cJSON_Print(pJson); printf("SyncPing: \n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } - uint32_t bufLen = pSyncPing->bytes; + uint32_t bufLen = pMsg->bytes; char* buf = (char*)malloc(bufLen); - syncPingSerialize(pSyncPing, buf, bufLen); + syncPingSerialize(pMsg, buf, bufLen); - SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); - syncPingDeserialize(buf, bufLen, pSyncPing2); + SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes); + syncPingDeserialize(buf, bufLen, pMsg2); { - cJSON* pJson = syncPing2Json(pSyncPing2); + cJSON* pJson = syncPing2Json(pMsg2); char* serialized = cJSON_Print(pJson); printf("SyncPing2: \n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } - syncPingDestroy(pSyncPing); - syncPingDestroy(pSyncPing2); + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); free(buf); } void test2() { - sTrace("test2: ----"); + sTrace("test2: ---- syncPing2RpcMsg, syncPingFromRpcMsg"); char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "hello raft"); - SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); - pSyncPing->srcId.addr = 100; - pSyncPing->srcId.vgId = 200; - pSyncPing->destId.addr = 300; - pSyncPing->destId.vgId = 400; - memcpy(pSyncPing->data, msg, PING_MSG_LEN); + SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); + pMsg->srcId.addr = 100; + pMsg->srcId.vgId = 200; + pMsg->destId.addr = 300; + pMsg->destId.vgId = 400; + memcpy(pMsg->data, msg, PING_MSG_LEN); { - cJSON* pJson = syncPing2Json(pSyncPing); + cJSON* pJson = syncPing2Json(pMsg); char* serialized = cJSON_Print(pJson); printf("SyncPing: \n%s\n\n", serialized); free(serialized); @@ -76,23 +76,100 @@ void test2() { } SRpcMsg rpcMsg; - syncPing2RpcMsg(pSyncPing, &rpcMsg); - SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); - syncPingFromRpcMsg(&rpcMsg, pSyncPing2); + syncPing2RpcMsg(pMsg, &rpcMsg); + SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes); + syncPingFromRpcMsg(&rpcMsg, pMsg2); rpcFreeCont(rpcMsg.pCont); { - cJSON* pJson = syncPing2Json(pSyncPing2); + cJSON* pJson = syncPing2Json(pMsg2); char* serialized = cJSON_Print(pJson); printf("SyncPing2: \n%s\n\n", serialized); free(serialized); cJSON_Delete(pJson); } - syncPingDestroy(pSyncPing); - syncPingDestroy(pSyncPing2); + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); } +void test3() { + sTrace("test3: ---- syncPingReplySerialize, syncPingReplyDeserialize"); + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "test ping"); + SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); + pMsg->srcId.addr = 19; + pMsg->srcId.vgId = 29; + pMsg->destId.addr = 39; + pMsg->destId.vgId = 49; + memcpy(pMsg->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPingReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("SyncPingReply: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pMsg->bytes; + char* buf = (char*)malloc(bufLen); + syncPingReplySerialize(pMsg, buf, bufLen); + + SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes); + syncPingReplyDeserialize(buf, bufLen, pMsg2); + + { + cJSON* pJson = syncPingReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("SyncPingReply2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); + free(buf); +} + +void test4() { + sTrace("test4: ---- syncPingReply2RpcMsg, syncPingReplyFromRpcMsg"); + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "hello raft"); + SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); + pMsg->srcId.addr = 66; + pMsg->srcId.vgId = 77; + pMsg->destId.addr = 88; + pMsg->destId.vgId = 99; + memcpy(pMsg->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPingReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + printf("SyncPingReply: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncPingReply2RpcMsg(pMsg, &rpcMsg); + SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes); + syncPingReplyFromRpcMsg(&rpcMsg, pMsg2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncPingReply2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + printf("SyncPingReply2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} int main() { // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; @@ -100,6 +177,8 @@ int main() { test1(); test2(); + test3(); + test4(); return 0; } From aaf5e20fdc6b84538532e306e68793f08b117fe6 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 20:29:49 +0800 Subject: [PATCH 08/19] sync encode test --- source/libs/sync/inc/syncInt.h | 18 ++++++------ source/libs/sync/src/syncIO.c | 20 ++++++++----- source/libs/sync/src/syncMain.c | 51 +++++++++++++++++++++++---------- source/libs/sync/src/syncUtil.c | 2 +- 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index 82108acf7b..d75c5424ea 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -97,14 +97,17 @@ typedef struct SRaftId { } SRaftId; typedef struct SSyncNode { - int8_t replica; - int8_t quorum; - int32_t refCount; - int64_t rid; - SyncGroupId vgId; SSyncCfg syncCfg; char path[TSDB_FILENAME_LEN]; + SSyncFSM* pFsm; + + // passed from outside + void* rpcClient; + int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg); + + int32_t refCount; + int64_t rid; SNodeInfo me; SNodeInfo peers[TSDB_MAX_REPLICA]; @@ -112,7 +115,6 @@ typedef struct SSyncNode { ESyncRole role; SRaftId raftId; - SSyncFSM* pFsm; tmr_h pPingTimer; int32_t pingTimerMS; @@ -145,10 +147,6 @@ typedef struct SSyncNode { int32_t (*FpOnAppendEntriesReply)(SSyncNode* ths, SyncAppendEntriesReply* pMsg); - // passed from outside - void* rpcClient; - int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg); - } SSyncNode; SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo); diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 5c27f9ec3a..c035ad5d6b 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -30,19 +30,26 @@ static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, S static int32_t doSyncIODestroy(SSyncIO *io); static SSyncIO *syncIOCreate(); -static void * syncIOConsumer(void *param); +static void *syncIOConsumer(void *param); static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static void syncIOTick(void *param, void *tmrId); // ---------------------------- -int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { return 0; } +int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { + pMsg->handle = NULL; + rpcSendRequest(handle, pEpSet, pMsg, NULL); + return 0; +} int32_t syncIOStart() { gSyncIO = syncIOCreate(); assert(gSyncIO != NULL); + int32_t ret = doSyncIOStart(gSyncIO); + assert(ret == 0); + return 0; } @@ -67,15 +74,14 @@ static void syncIOTick(void *param, void *tmrId) { taosWriteQitem(io->pMsgQ, pTemp); - bool b = taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, io->syncTimer); - assert(b); + taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, io->syncTimer); } static void *syncIOConsumer(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg * pRpcMsg, rpcMsg; + SRpcMsg *pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); @@ -215,8 +221,8 @@ static int32_t doSyncIOStart(SSyncIO *io) { } // start tmr thread - io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); - io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); + // io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); + // io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); return 0; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 4d9e5887cd..0aa3e56062 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -69,15 +69,34 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { assert(pSyncNode != NULL); memset(pSyncNode, 0, sizeof(SSyncNode)); + pSyncNode->vgId = pSyncInfo->vgId; + pSyncNode->syncCfg = pSyncInfo->syncCfg; + memcpy(pSyncNode->path, pSyncInfo->path, sizeof(pSyncNode->path)); + pSyncNode->pFsm = pSyncInfo->pFsm; + + pSyncNode->rpcClient = pSyncInfo->rpcClient; + pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; + + pSyncNode->me = pSyncInfo->syncCfg.nodeInfo[pSyncInfo->syncCfg.myIndex]; + pSyncNode->peersNum = pSyncInfo->syncCfg.replicaNum - 1; + + int j = 0; + for (int i = 0; i < pSyncInfo->syncCfg.replicaNum; ++i) { + if (i != pSyncInfo->syncCfg.myIndex) { + pSyncNode->peers[j] = pSyncInfo->syncCfg.nodeInfo[i]; + j++; + } + } + + pSyncNode->role = TAOS_SYNC_STATE_FOLLOWER; + syncUtilnodeInfo2raftId(&pSyncNode->me, pSyncNode->vgId, &pSyncNode->raftId); + pSyncNode->pPingTimer = NULL; pSyncNode->pingTimerMS = 1000; atomic_store_8(&pSyncNode->pingTimerStart, 0); pSyncNode->FpPingTimer = syncNodePingTimerCb; pSyncNode->pingTimerCounter = 0; - pSyncNode->rpcClient = pSyncInfo->rpcClient; - pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; - pSyncNode->FpOnPing = syncNodeOnPingCb; pSyncNode->FpOnPingReply = syncNodeOnPingReplyCb; pSyncNode->FpOnRequestVote = syncNodeOnRequestVoteCb; @@ -97,10 +116,11 @@ void syncNodePingAll(SSyncNode* pSyncNode) { sTrace("syncNodePingAll %p ", pSyncNode); int32_t ret = 0; for (int i = 0; i < pSyncNode->syncCfg.replicaNum; ++i) { - SyncPing* pSyncPing; - SRaftId raftId; - syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &raftId); - ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + SyncPing* pMsg = syncPingBuild(strlen("ping") + 1); + memcpy(pMsg->data, "ping", strlen("ping") + 1); + syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &pMsg->destId); + pMsg->srcId = pSyncNode->raftId; + ret = syncNodePing(pSyncNode, &pMsg->destId, pMsg); assert(ret == 0); } } @@ -118,10 +138,11 @@ void syncNodePingPeers(SSyncNode* pSyncNode) { void syncNodePingSelf(SSyncNode* pSyncNode) { int32_t ret = 0; - SyncPing* pSyncPing; - SRaftId raftId; - syncUtilnodeInfo2raftId(&pSyncNode->me, pSyncNode->vgId, &raftId); - ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + SyncPing* pMsg = syncPingBuild(strlen("ping") + 1); + memcpy(pMsg->data, "ping", strlen("ping") + 1); + pMsg->destId = pSyncNode->raftId; + pMsg->srcId = pSyncNode->raftId; + ret = syncNodePing(pSyncNode, &pMsg->destId, pMsg); assert(ret == 0); } @@ -146,10 +167,10 @@ int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { // ------ local funciton --------- static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg) { - int32_t ret = 0; - SRpcMsg* rpcMsg; - syncPing2RpcMsg(pMsg, rpcMsg); - syncNodeSendMsgById(destRaftId, pSyncNode, rpcMsg); + int32_t ret = 0; + SRpcMsg rpcMsg; + syncPing2RpcMsg(pMsg, &rpcMsg); + syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg); return ret; } diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 080840bbf6..0066ab783e 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -22,7 +22,7 @@ uint64_t syncUtilAddr2U64(const char* host, uint16_t port) { uint64_t u64; uint32_t hostU32 = (uint32_t)inet_addr(host); - assert(hostU32 != (uint32_t)-1); + // assert(hostU32 != (uint32_t)-1); u64 = (((uint64_t)hostU32) << 32) | (((uint32_t)port) << 16); return u64; } From ee43a70c4dde610857508312228306d1c5260928 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 10:43:59 +0800 Subject: [PATCH 09/19] sync modify timer --- source/libs/sync/inc/syncInt.h | 30 ++++++++-------- source/libs/sync/inc/syncMessage.h | 4 +++ source/libs/sync/src/syncIO.c | 11 +++--- source/libs/sync/src/syncMain.c | 49 ++++++++++++++++++-------- source/libs/sync/src/syncMessage.c | 14 ++++++++ source/libs/sync/test/syncPingTest.cpp | 4 ++- 6 files changed, 76 insertions(+), 36 deletions(-) diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index d75c5424ea..0901330488 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -48,23 +48,23 @@ extern int32_t sDebugFlag; taosPrintLog("SYN WARN ", sDebugFlag, __VA_ARGS__); \ } \ } -#define sInfo(...) \ - { \ - if (sDebugFlag & DEBUG_INFO) { \ - taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); \ - } \ +#define sInfo(...) \ + { \ + if (sDebugFlag & DEBUG_INFO) { \ + taosPrintLog("SYN INFO ", sDebugFlag, __VA_ARGS__); \ + } \ } -#define sDebug(...) \ - { \ - if (sDebugFlag & DEBUG_DEBUG) { \ - taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); \ - } \ +#define sDebug(...) \ + { \ + if (sDebugFlag & DEBUG_DEBUG) { \ + taosPrintLog("SYN DEBUG ", sDebugFlag, __VA_ARGS__); \ + } \ } -#define sTrace(...) \ - { \ - if (sDebugFlag & DEBUG_TRACE) { \ - taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); \ - } \ +#define sTrace(...) \ + { \ + if (sDebugFlag & DEBUG_TRACE) { \ + taosPrintLog("SYN TRACE ", sDebugFlag, __VA_ARGS__); \ + } \ } struct SRaft; diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index a7de7b9019..603ef00517 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -72,6 +72,10 @@ void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); cJSON* syncPing2Json(const SyncPing* pMsg); +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); + +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId); + typedef struct SyncPingReply { uint32_t bytes; uint32_t msgType; diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index c035ad5d6b..27434a4029 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -38,6 +38,7 @@ static void syncIOTick(void *param, void *tmrId); // ---------------------------- int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { + sTrace("syncIOSendMsg ... "); pMsg->handle = NULL; rpcSendRequest(handle, pEpSet, pMsg, NULL); return 0; @@ -74,7 +75,7 @@ static void syncIOTick(void *param, void *tmrId) { taosWriteQitem(io->pMsgQ, pTemp); - taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, io->syncTimer); + taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, &io->syncTimer); } static void *syncIOConsumer(void *param) { @@ -191,7 +192,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { { SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 38000; + rpcInit.localPort = 7010; rpcInit.label = "SYNC-IO-SERVER"; rpcInit.numOfThreads = 1; rpcInit.cfp = syncIODoRequest; @@ -209,7 +210,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { } io->epSet.inUse = 0; - addEpIntoEpSet(&io->epSet, "127.0.0.1", 38000); + addEpIntoEpSet(&io->epSet, "127.0.0.1", 7010); // start consumer thread { @@ -221,8 +222,8 @@ static int32_t doSyncIOStart(SSyncIO *io) { } // start tmr thread - // io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); - // io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); + io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); + io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); return 0; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 0aa3e56062..9e335c5e55 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -113,37 +113,36 @@ void syncNodeClose(SSyncNode* pSyncNode) { } void syncNodePingAll(SSyncNode* pSyncNode) { - sTrace("syncNodePingAll %p ", pSyncNode); + sTrace("syncNodePingAll pSyncNode:%p ", pSyncNode); int32_t ret = 0; for (int i = 0; i < pSyncNode->syncCfg.replicaNum; ++i) { - SyncPing* pMsg = syncPingBuild(strlen("ping") + 1); - memcpy(pMsg->data, "ping", strlen("ping") + 1); - syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &pMsg->destId); - pMsg->srcId = pSyncNode->raftId; - ret = syncNodePing(pSyncNode, &pMsg->destId, pMsg); + SRaftId destId; + syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &destId); + SyncPing* pMsg = syncPingBuild3(&pSyncNode->raftId, &destId); + ret = syncNodePing(pSyncNode, &destId, pMsg); assert(ret == 0); + syncPingDestroy(pMsg); } } void syncNodePingPeers(SSyncNode* pSyncNode) { int32_t ret = 0; for (int i = 0; i < pSyncNode->peersNum; ++i) { - SyncPing* pSyncPing; - SRaftId raftId; - syncUtilnodeInfo2raftId(&pSyncNode->peers[i], pSyncNode->vgId, &raftId); - ret = syncNodePing(pSyncNode, &raftId, pSyncPing); + SRaftId destId; + syncUtilnodeInfo2raftId(&pSyncNode->peers[i], pSyncNode->vgId, &destId); + SyncPing* pMsg = syncPingBuild3(&pSyncNode->raftId, &destId); + ret = syncNodePing(pSyncNode, &destId, pMsg); assert(ret == 0); + syncPingDestroy(pMsg); } } void syncNodePingSelf(SSyncNode* pSyncNode) { - int32_t ret = 0; - SyncPing* pMsg = syncPingBuild(strlen("ping") + 1); - memcpy(pMsg->data, "ping", strlen("ping") + 1); - pMsg->destId = pSyncNode->raftId; - pMsg->srcId = pSyncNode->raftId; + int32_t ret; + SyncPing* pMsg = syncPingBuild3(&pSyncNode->raftId, &pSyncNode->raftId); ret = syncNodePing(pSyncNode, &pMsg->destId, pMsg); assert(ret == 0); + syncPingDestroy(pMsg); } int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode) { @@ -167,10 +166,29 @@ int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { // ------ local funciton --------- static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg) { + sTrace("syncNodePing pSyncNode:%p ", pSyncNode); int32_t ret = 0; SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg); + + { + cJSON* pJson = syncPing2Json(pMsg); + char* serialized = cJSON_Print(pJson); + sTrace("syncNodePing pMsg:%s ", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + { + SyncPing* pMsg2 = rpcMsg.pCont; + cJSON* pJson = syncPing2Json(pMsg2); + char* serialized = cJSON_Print(pJson); + sTrace("syncNodePing pMsg2:%s ", serialized); + free(serialized); + cJSON_Delete(pJson); + } + return ret; } @@ -185,6 +203,7 @@ static int32_t syncNodeAppendEntries(SSyncNode* ths, const SyncAppendEntries* pM } static int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg) { + sTrace("syncNodeSendMsgById pSyncNode:%p ", pSyncNode); SEpSet epSet; syncUtilraftId2EpSet(destRaftId, &epSet); pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 3122aa66bb..e0975fb3c1 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -80,6 +80,20 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { return pJson; } +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { + uint32_t dataLen = strlen(str) + 1; + SyncPing* pMsg = syncPingBuild(dataLen); + pMsg->srcId = *srcId; + pMsg->destId = *destId; + snprintf(pMsg->data, pMsg->dataLen, "%s", str); + return pMsg; +} + +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId) { + SyncPing* pMsg = syncPingBuild2(srcId, destId, "ping"); + return pMsg; +} + // ---- message process SyncPingReply---- SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { uint32_t bytes = SYNC_PING_REPLY_FIX_LEN + dataLen; diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 43196bdd1f..b57f6c5259 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -25,7 +25,7 @@ SSyncNode* doSync() { SSyncCfg* pCfg = &syncInfo.syncCfg; pCfg->myIndex = 0; - pCfg->replicaNum = 3; + pCfg->replicaNum = 1; pCfg->nodeInfo[0].nodePort = 7010; taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); @@ -63,6 +63,7 @@ int main() { ret = syncEnvStart(); assert(ret == 0); +/* SSyncNode* pSyncNode = doSync(); ret = syncNodeStartPingTimer(pSyncNode); @@ -72,6 +73,7 @@ int main() { ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); +*/ while (1) { taosMsleep(1000); From 5a4fbcf2621f54aec1fd4fd9db2429c3c32228bb Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 15:11:54 +0800 Subject: [PATCH 10/19] sync io --- source/libs/sync/inc/syncIO.h | 38 +-- source/libs/sync/src/syncEnv.c | 1 - source/libs/sync/src/syncIO.c | 327 +++++++++++++---------- source/libs/sync/test/CMakeLists.txt | 28 ++ source/libs/sync/test/syncIOTickPing.cpp | 35 +++ source/libs/sync/test/syncIOTickQ.cpp | 35 +++ source/libs/sync/test/syncPingTest.cpp | 20 +- source/libs/sync/test/syncTest.cpp | 2 +- 8 files changed, 310 insertions(+), 176 deletions(-) create mode 100644 source/libs/sync/test/syncIOTickPing.cpp create mode 100644 source/libs/sync/test/syncIOTickQ.cpp diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index 6723fa66c5..e5c55baff4 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -30,35 +30,37 @@ extern "C" { #include "trpc.h" typedef struct SSyncIO { - void * serverRpc; - void * clientRpc; STaosQueue *pMsgQ; - STaosQset * pQset; - pthread_t tid; - int8_t isStart; + STaosQset *pQset; + pthread_t consumerTid; - SEpSet epSet; + void *serverRpc; + void *clientRpc; + SEpSet myAddr; - void *syncTimer; - void *syncTimerManager; - - int32_t (*start)(struct SSyncIO *ths); - int32_t (*stop)(struct SSyncIO *ths); - int32_t (*ping)(struct SSyncIO *ths); - - int32_t (*onMsg)(struct SSyncIO *ths, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); - int32_t (*destroy)(struct SSyncIO *ths); + void *ioTimerTickQ; + void *ioTimerTickPing; + void *ioTimerManager; void *pSyncNode; - int32_t (*FpOnPing)(struct SSyncNode *ths, SyncPing *pMsg); + int32_t (*FpOnSyncPing)(SSyncNode *pSyncNode, SyncPing *pMsg); + int32_t (*FpOnSyncPingReply)(SSyncNode *pSyncNode, SyncPingReply *pMsg); + int32_t (*FpOnSyncRequestVote)(SSyncNode *pSyncNode, SyncRequestVote *pMsg); + int32_t (*FpOnSyncRequestVoteReply)(SSyncNode *pSyncNode, SyncRequestVoteReply *pMsg); + int32_t (*FpOnSyncAppendEntries)(SSyncNode *pSyncNode, SyncAppendEntries *pMsg); + int32_t (*FpOnSyncAppendEntriesReply)(SSyncNode *pSyncNode, SyncAppendEntriesReply *pMsg); + + int8_t isStart; } SSyncIO; extern SSyncIO *gSyncIO; -int32_t syncIOStart(); +int32_t syncIOStart(char *host, uint16_t port); int32_t syncIOStop(); -int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg); +int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg); +int32_t syncIOTickQ(); +int32_t syncIOTickPing(); #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index bfdf2a9c5e..a9cf035650 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -76,4 +76,3 @@ static tmr_h doSyncEnvStartTimer(SSyncEnv *pSyncEnv, TAOS_TMR_CALLBACK fp, int m } static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer) {} -// -------------------------------- \ No newline at end of file diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 27434a4029..932c27d64a 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -23,146 +23,66 @@ SSyncIO *gSyncIO = NULL; // local function ------------ -static int32_t doSyncIOStart(SSyncIO *io); -static int32_t doSyncIOStop(SSyncIO *io); -static int32_t doSyncIOPing(SSyncIO *io); -static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static int32_t doSyncIODestroy(SSyncIO *io); +static int32_t syncIOStartInternal(SSyncIO *io); +static int32_t syncIOStopInternal(SSyncIO *io); +static SSyncIO *syncIOCreate(char *host, uint16_t port); +static int32_t syncIODestroy(SSyncIO *io); -static SSyncIO *syncIOCreate(); -static void *syncIOConsumer(void *param); -static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); -static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); -static void syncIOTick(void *param, void *tmrId); +static void *syncIOConsumerFunc(void *param); +static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); +static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); +static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); + +static int32_t syncIOTickQInternal(SSyncIO *io); +static void syncIOTickQFunc(void *param, void *tmrId); +static int32_t syncIOTickPingInternal(SSyncIO *io); +static void syncIOTickPingFunc(void *param, void *tmrId); // ---------------------------- -int32_t syncIOSendMsg(void *handle, const SEpSet *pEpSet, SRpcMsg *pMsg) { +// public function ------------ +int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { sTrace("syncIOSendMsg ... "); pMsg->handle = NULL; - rpcSendRequest(handle, pEpSet, pMsg, NULL); + rpcSendRequest(clientRpc, pEpSet, pMsg, NULL); return 0; } -int32_t syncIOStart() { - gSyncIO = syncIOCreate(); +int32_t syncIOStart(char *host, uint16_t port) { + gSyncIO = syncIOCreate(host, port); assert(gSyncIO != NULL); - int32_t ret = doSyncIOStart(gSyncIO); + int32_t ret = syncIOStartInternal(gSyncIO); assert(ret == 0); return 0; } -int32_t syncIOStop() { return 0; } +int32_t syncIOStop() { + int32_t ret = syncIOStopInternal(gSyncIO); + assert(ret == 0); -// local function ------------ -static void syncIOTick(void *param, void *tmrId) { - SSyncIO *io = (SSyncIO *)param; - sDebug("syncIOTick ... "); - - SRpcMsg rpcMsg; - rpcMsg.pCont = rpcMallocCont(10); - snprintf(rpcMsg.pCont, 10, "TICK"); - rpcMsg.contLen = 10; - rpcMsg.handle = NULL; - rpcMsg.msgType = 2; - - SRpcMsg *pTemp; - - pTemp = taosAllocateQitem(sizeof(SRpcMsg)); - memcpy(pTemp, &rpcMsg, sizeof(SRpcMsg)); - - taosWriteQitem(io->pMsgQ, pTemp); - - taosTmrReset(syncIOTick, 1000, io, io->syncTimerManager, &io->syncTimer); -} - -static void *syncIOConsumer(void *param) { - SSyncIO *io = param; - - STaosQall *qall; - SRpcMsg *pRpcMsg, rpcMsg; - int type; - - qall = taosAllocateQall(); - - while (1) { - int numOfMsgs = taosReadAllQitemsFromQset(io->pQset, qall, NULL, NULL); - sDebug("%d sync-io msgs are received", numOfMsgs); - if (numOfMsgs <= 0) break; - - for (int i = 0; i < numOfMsgs; ++i) { - taosGetQitem(qall, (void **)&pRpcMsg); - sDebug("sync-io recv type:%d msg:%s", pRpcMsg->msgType, (char *)(pRpcMsg->pCont)); - } - - taosResetQitems(qall); - for (int i = 0; i < numOfMsgs; ++i) { - taosGetQitem(qall, (void **)&pRpcMsg); - rpcFreeCont(pRpcMsg->pCont); - - if (pRpcMsg->handle != NULL) { - int msgSize = 128; - memset(&rpcMsg, 0, sizeof(rpcMsg)); - rpcMsg.pCont = rpcMallocCont(msgSize); - rpcMsg.contLen = msgSize; - rpcMsg.handle = pRpcMsg->handle; - rpcMsg.code = 0; - rpcSendResponse(&rpcMsg); - } - - taosFreeQitem(pRpcMsg); - } - } - - taosFreeQall(qall); - return NULL; -} - -static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { - // app shall retrieve the auth info based on meterID from DB or a data file - // demo code here only for simple demo - int ret = 0; + ret = syncIODestroy(gSyncIO); + assert(ret == 0); return ret; } -static void syncIODoReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - sDebug("syncIODoReply ... "); - rpcFreeCont(pMsg->pCont); +int32_t syncIOTickQ() { + int32_t ret = syncIOTickQInternal(gSyncIO); + assert(ret == 0); + return ret; } -static void syncIODoRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - SSyncIO *io = pParent; - SRpcMsg *pTemp; - - pTemp = taosAllocateQitem(sizeof(SRpcMsg)); - memcpy(pTemp, pMsg, sizeof(SRpcMsg)); - - sDebug("request is received, type:%d, contLen:%d, item:%p", pMsg->msgType, pMsg->contLen, pTemp); - taosWriteQitem(io->pMsgQ, pTemp); +int32_t syncIOTickPing() { + int32_t ret = syncIOTickPingInternal(gSyncIO); + assert(ret == 0); + return ret; } -static SSyncIO *syncIOCreate() { - SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); - memset(io, 0, sizeof(*io)); - - io->pMsgQ = taosOpenQueue(); - io->pQset = taosOpenQset(); - taosAddIntoQset(io->pQset, io->pMsgQ, NULL); - - io->start = doSyncIOStart; - io->stop = doSyncIOStop; - io->ping = doSyncIOPing; - io->onMsg = doSyncIOOnMsg; - io->destroy = doSyncIODestroy; - - return io; -} - -static int32_t doSyncIOStart(SSyncIO *io) { +// local function ------------ +static int32_t syncIOStartInternal(SSyncIO *io) { taosBlockSIGPIPE(); + rpcInit(); tsRpcForceTcp = 1; // cient rpc init @@ -172,7 +92,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { rpcInit.localPort = 0; rpcInit.label = "SYNC-IO-CLIENT"; rpcInit.numOfThreads = 1; - rpcInit.cfp = syncIODoReply; + rpcInit.cfp = syncIOProcessReply; rpcInit.sessions = 100; rpcInit.idleTime = 100; rpcInit.user = "sync-io"; @@ -195,7 +115,7 @@ static int32_t doSyncIOStart(SSyncIO *io) { rpcInit.localPort = 7010; rpcInit.label = "SYNC-IO-SERVER"; rpcInit.numOfThreads = 1; - rpcInit.cfp = syncIODoRequest; + rpcInit.cfp = syncIOProcessRequest; rpcInit.sessions = 1000; rpcInit.idleTime = 2 * 1500; rpcInit.afp = syncIOAuth; @@ -209,12 +129,9 @@ static int32_t doSyncIOStart(SSyncIO *io) { } } - io->epSet.inUse = 0; - addEpIntoEpSet(&io->epSet, "127.0.0.1", 7010); - // start consumer thread { - if (pthread_create(&io->tid, NULL, syncIOConsumer, io) != 0) { + if (pthread_create(&io->consumerTid, NULL, syncIOConsumerFunc, io) != 0) { sError("failed to create sync consumer thread since %s", strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; @@ -222,35 +139,32 @@ static int32_t doSyncIOStart(SSyncIO *io) { } // start tmr thread - io->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); - io->syncTimer = taosTmrStart(syncIOTick, 1000, io, io->syncTimerManager); + io->ioTimerManager = taosTmrInit(1000, 50, 10000, "SYNC"); return 0; } -static int32_t doSyncIOStop(SSyncIO *io) { +static int32_t syncIOStopInternal(SSyncIO *io) { atomic_store_8(&io->isStart, 0); - pthread_join(io->tid, NULL); + pthread_join(io->consumerTid, NULL); return 0; } -static int32_t doSyncIOPing(SSyncIO *io) { - SRpcMsg rpcMsg, rspMsg; +static SSyncIO *syncIOCreate(char *host, uint16_t port) { + SSyncIO *io = (SSyncIO *)malloc(sizeof(SSyncIO)); + memset(io, 0, sizeof(*io)); - rpcMsg.pCont = rpcMallocCont(10); - snprintf(rpcMsg.pCont, 10, "ping"); - rpcMsg.contLen = 10; - rpcMsg.handle = NULL; - rpcMsg.msgType = 1; + io->pMsgQ = taosOpenQueue(); + io->pQset = taosOpenQset(); + taosAddIntoQset(io->pQset, io->pMsgQ, NULL); - rpcSendRequest(io->clientRpc, &io->epSet, &rpcMsg, NULL); + io->myAddr.inUse = 0; + addEpIntoEpSet(&io->myAddr, host, port); - return 0; + return io; } -static int32_t doSyncIOOnMsg(struct SSyncIO *io, void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { return 0; } - -static int32_t doSyncIODestroy(SSyncIO *io) { +static int32_t syncIODestroy(SSyncIO *io) { int8_t start = atomic_load_8(&io->isStart); assert(start == 0); @@ -264,15 +178,136 @@ static int32_t doSyncIODestroy(SSyncIO *io) { io->clientRpc = NULL; } - if (io->pMsgQ != NULL) { - free(io->pMsgQ); - io->pMsgQ = NULL; - } - - if (io->pQset != NULL) { - free(io->pQset); - io->pQset = NULL; - } + taosCloseQueue(io->pMsgQ); + taosCloseQset(io->pQset); return 0; } + +static void *syncIOConsumerFunc(void *param) { + SSyncIO *io = param; + + STaosQall *qall; + SRpcMsg *pRpcMsg, rpcMsg; + int type; + + qall = taosAllocateQall(); + + while (1) { + int numOfMsgs = taosReadAllQitemsFromQset(io->pQset, qall, NULL, NULL); + sTrace("syncIOConsumerFunc %d msgs are received", numOfMsgs); + if (numOfMsgs <= 0) break; + + for (int i = 0; i < numOfMsgs; ++i) { + taosGetQitem(qall, (void **)&pRpcMsg); + sTrace("syncIOConsumerFunc get item from queue: msgType:%d contLen:%d msg:%s", pRpcMsg->msgType, pRpcMsg->contLen, + (char *)(pRpcMsg->pCont)); + + if (pRpcMsg->msgType == SYNC_PING) { + if (io->FpOnSyncPing != NULL) { + SyncPing *pSyncMsg = syncPingBuild(pRpcMsg->contLen); + syncPingFromRpcMsg(pRpcMsg, pSyncMsg); + io->FpOnSyncPing(io->pSyncNode, pSyncMsg); + } + } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { + SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen); + syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg); + io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg); + } else { + ; + } + } + + taosResetQitems(qall); + for (int i = 0; i < numOfMsgs; ++i) { + taosGetQitem(qall, (void **)&pRpcMsg); + rpcFreeCont(pRpcMsg->pCont); + + if (pRpcMsg->handle != NULL) { + int msgSize = 128; + memset(&rpcMsg, 0, sizeof(rpcMsg)); + rpcMsg.pCont = rpcMallocCont(msgSize); + rpcMsg.contLen = msgSize; + snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "give a reply"); + rpcMsg.handle = pRpcMsg->handle; + rpcMsg.code = 0; + + sTrace("syncIOConsumerFunc rpcSendResponse ... msgType:%d contLen:%d", pRpcMsg->msgType, rpcMsg.contLen); + rpcSendResponse(&rpcMsg); + } + + taosFreeQitem(pRpcMsg); + } + } + + taosFreeQall(qall); + return NULL; +} + +static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { + // app shall retrieve the auth info based on meterID from DB or a data file + // demo code here only for simple demo + int ret = 0; + return ret; +} + +static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { + sTrace("syncIOProcessRequest: type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen, (char *)pMsg->pCont); + + SSyncIO *io = pParent; + SRpcMsg *pTemp; + + pTemp = taosAllocateQitem(sizeof(SRpcMsg)); + memcpy(pTemp, pMsg, sizeof(SRpcMsg)); + + taosWriteQitem(io->pMsgQ, pTemp); +} + +static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { + sTrace("syncIOProcessReply: type:%d, contLen:%d msg:%s", pMsg->msgType, pMsg->contLen, (char *)pMsg->pCont); + rpcFreeCont(pMsg->pCont); +} + +static int32_t syncIOTickQInternal(SSyncIO *io) { + io->ioTimerTickQ = taosTmrStart(syncIOTickQFunc, 1000, io, io->ioTimerManager); + return 0; +} + +static void syncIOTickQFunc(void *param, void *tmrId) { + SSyncIO *io = (SSyncIO *)param; + sTrace("<-- syncIOTickQFunc -->"); + + SRpcMsg rpcMsg; + rpcMsg.contLen = 64; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOTickQ"); + rpcMsg.handle = NULL; + rpcMsg.msgType = 55; + + SRpcMsg *pTemp; + pTemp = taosAllocateQitem(sizeof(SRpcMsg)); + memcpy(pTemp, &rpcMsg, sizeof(SRpcMsg)); + + taosWriteQitem(io->pMsgQ, pTemp); + taosTmrReset(syncIOTickQFunc, 1000, io, io->ioTimerManager, &io->ioTimerTickQ); +} + +static int32_t syncIOTickPingInternal(SSyncIO *io) { + io->ioTimerTickPing = taosTmrStart(syncIOTickPingFunc, 1000, io, io->ioTimerManager); + return 0; +} + +static void syncIOTickPingFunc(void *param, void *tmrId) { + SSyncIO *io = (SSyncIO *)param; + sTrace("<-- syncIOTickPingFunc -->"); + + SRpcMsg rpcMsg; + rpcMsg.contLen = 64; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOTickPing"); + rpcMsg.handle = NULL; + rpcMsg.msgType = 77; + + rpcSendRequest(io->clientRpc, &io->myAddr, &rpcMsg, NULL); + taosTmrReset(syncIOTickPingFunc, 1000, io, io->ioTimerManager, &io->ioTimerTickPing); +} diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 8e1c99d179..9abe3f232e 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable(syncTest "") add_executable(syncEnvTest "") add_executable(syncPingTest "") add_executable(syncEncodeTest "") +add_executable(syncIOTickQ "") +add_executable(syncIOTickPing "") target_sources(syncTest @@ -20,6 +22,14 @@ target_sources(syncEncodeTest PRIVATE "syncEncodeTest.cpp" ) +target_sources(syncIOTickQ + PRIVATE + "syncIOTickQ.cpp" +) +target_sources(syncIOTickPing + PRIVATE + "syncIOTickPing.cpp" +) target_include_directories(syncTest @@ -42,6 +52,16 @@ target_include_directories(syncEncodeTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncIOTickQ + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncIOTickPing + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -60,6 +80,14 @@ target_link_libraries(syncEncodeTest sync gtest_main ) +target_link_libraries(syncIOTickQ + sync + gtest_main +) +target_link_libraries(syncIOTickPing + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncIOTickPing.cpp b/source/libs/sync/test/syncIOTickPing.cpp new file mode 100644 index 0000000000..1559b57585 --- /dev/null +++ b/source/libs/sync/test/syncIOTickPing.cpp @@ -0,0 +1,35 @@ +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + logTest(); + + int32_t ret; + + ret = syncIOStart((char*)"127.0.0.1", 7010); + assert(ret == 0); + + ret = syncIOTickPing(); + assert(ret == 0); + + while (1) { + sleep(1); + } + return 0; +} diff --git a/source/libs/sync/test/syncIOTickQ.cpp b/source/libs/sync/test/syncIOTickQ.cpp new file mode 100644 index 0000000000..90304079e3 --- /dev/null +++ b/source/libs/sync/test/syncIOTickQ.cpp @@ -0,0 +1,35 @@ +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + logTest(); + + int32_t ret; + + ret = syncIOStart((char*)"127.0.0.1", 7010); + assert(ret == 0); + + ret = syncIOTickQ(); + assert(ret == 0); + + while (1) { + sleep(1); + } + return 0; +} diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index b57f6c5259..bca634765e 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -39,7 +39,7 @@ SSyncNode* doSync() { SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); assert(pSyncNode != NULL); - gSyncIO->FpOnPing = pSyncNode->FpOnPing; + gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; gSyncIO->pSyncNode = pSyncNode; return pSyncNode; @@ -57,23 +57,23 @@ int main() { logTest(); - int32_t ret = syncIOStart(); + int32_t ret = syncIOStart((char*)"127.0.0.1", 7010); assert(ret == 0); ret = syncEnvStart(); assert(ret == 0); -/* - SSyncNode* pSyncNode = doSync(); + /* + SSyncNode* pSyncNode = doSync(); - ret = syncNodeStartPingTimer(pSyncNode); - assert(ret == 0); + ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); - taosMsleep(5000); + taosMsleep(5000); - ret = syncNodeStopPingTimer(pSyncNode); - assert(ret == 0); -*/ + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); + */ while (1) { taosMsleep(1000); diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index 0843a48bb8..0b397ef921 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -8,7 +8,7 @@ void *pingFunc(void *param) { SSyncIO *io = (SSyncIO *)param; while (1) { sDebug("io->ping"); - io->ping(io); + // io->ping(io); sleep(1); } return NULL; From 3e591a022ce0775565642a855f7fcaf8874b1d53 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 15:20:49 +0800 Subject: [PATCH 11/19] sync io --- source/libs/sync/inc/syncIO.h | 6 +++--- source/libs/sync/src/syncIO.c | 2 +- source/libs/sync/test/CMakeLists.txt | 20 +++++++++---------- ...cIOTickPing.cpp => syncIOTickPingTest.cpp} | 0 .../{syncIOTickQ.cpp => syncIOTickQTest.cpp} | 0 source/libs/sync/test/syncPingTest.cpp | 14 ++++++------- 6 files changed, 20 insertions(+), 22 deletions(-) rename source/libs/sync/test/{syncIOTickPing.cpp => syncIOTickPingTest.cpp} (100%) rename source/libs/sync/test/{syncIOTickQ.cpp => syncIOTickQTest.cpp} (100%) diff --git a/source/libs/sync/inc/syncIO.h b/source/libs/sync/inc/syncIO.h index e5c55baff4..238948b403 100644 --- a/source/libs/sync/inc/syncIO.h +++ b/source/libs/sync/inc/syncIO.h @@ -31,11 +31,11 @@ extern "C" { typedef struct SSyncIO { STaosQueue *pMsgQ; - STaosQset *pQset; + STaosQset * pQset; pthread_t consumerTid; - void *serverRpc; - void *clientRpc; + void * serverRpc; + void * clientRpc; SEpSet myAddr; void *ioTimerTickQ; diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 932c27d64a..bb71b1e73d 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -188,7 +188,7 @@ static void *syncIOConsumerFunc(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg *pRpcMsg, rpcMsg; + SRpcMsg * pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 9abe3f232e..c089987c19 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -2,8 +2,8 @@ add_executable(syncTest "") add_executable(syncEnvTest "") add_executable(syncPingTest "") add_executable(syncEncodeTest "") -add_executable(syncIOTickQ "") -add_executable(syncIOTickPing "") +add_executable(syncIOTickQTest "") +add_executable(syncIOTickPingTest "") target_sources(syncTest @@ -22,13 +22,13 @@ target_sources(syncEncodeTest PRIVATE "syncEncodeTest.cpp" ) -target_sources(syncIOTickQ +target_sources(syncIOTickQTest PRIVATE - "syncIOTickQ.cpp" + "syncIOTickQTest.cpp" ) -target_sources(syncIOTickPing +target_sources(syncIOTickPingTest PRIVATE - "syncIOTickPing.cpp" + "syncIOTickPingTest.cpp" ) @@ -52,12 +52,12 @@ target_include_directories(syncEncodeTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncIOTickQ +target_include_directories(syncIOTickQTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncIOTickPing +target_include_directories(syncIOTickPingTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" @@ -80,11 +80,11 @@ target_link_libraries(syncEncodeTest sync gtest_main ) -target_link_libraries(syncIOTickQ +target_link_libraries(syncIOTickQTest sync gtest_main ) -target_link_libraries(syncIOTickPing +target_link_libraries(syncIOTickPingTest sync gtest_main ) diff --git a/source/libs/sync/test/syncIOTickPing.cpp b/source/libs/sync/test/syncIOTickPingTest.cpp similarity index 100% rename from source/libs/sync/test/syncIOTickPing.cpp rename to source/libs/sync/test/syncIOTickPingTest.cpp diff --git a/source/libs/sync/test/syncIOTickQ.cpp b/source/libs/sync/test/syncIOTickQTest.cpp similarity index 100% rename from source/libs/sync/test/syncIOTickQ.cpp rename to source/libs/sync/test/syncIOTickQTest.cpp diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index bca634765e..b80b0f2e20 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -63,17 +63,15 @@ int main() { ret = syncEnvStart(); assert(ret == 0); - /* - SSyncNode* pSyncNode = doSync(); + SSyncNode* pSyncNode = doSync(); - ret = syncNodeStartPingTimer(pSyncNode); - assert(ret == 0); + ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); - taosMsleep(5000); + taosMsleep(5000); - ret = syncNodeStopPingTimer(pSyncNode); - assert(ret == 0); - */ + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); while (1) { taosMsleep(1000); From a2d9cd4403c484b234ddea0ea3fcdd5d09ddbf19 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 16:19:15 +0800 Subject: [PATCH 12/19] sync io --- source/libs/sync/src/syncIO.c | 4 ++-- source/libs/sync/src/syncUtil.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index bb71b1e73d..96bb305e76 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -188,7 +188,7 @@ static void *syncIOConsumerFunc(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg * pRpcMsg, rpcMsg; + SRpcMsg *pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); @@ -310,4 +310,4 @@ static void syncIOTickPingFunc(void *param, void *tmrId) { rpcSendRequest(io->clientRpc, &io->myAddr, &rpcMsg, NULL); taosTmrReset(syncIOTickPingFunc, 1000, io, io->ioTimerManager, &io->ioTimerTickPing); -} +} \ No newline at end of file diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 0066ab783e..9c134e2d08 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -46,12 +46,25 @@ void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) { uint16_t port; syncUtilU642Addr(raftId->addr, host, sizeof(host), &port); + + /* + pEpSet->numOfEps = 1; + pEpSet->inUse = 0; + pEpSet->eps[0].port = port; + snprintf(epSet.eps[0].fqdn, sizeof(epSet.eps[0].fqdn), host); + */ + pEpSet->inUse = 0; + pEpSet->numOfEps = 1; addEpIntoEpSet(pEpSet, host, port); } void syncUtilnodeInfo2raftId(const SNodeInfo* pNodeInfo, SyncGroupId vgId, SRaftId* raftId) { - raftId->addr = syncUtilAddr2U64(pNodeInfo->nodeFqdn, pNodeInfo->nodePort); + uint32_t ipv4 = taosGetIpv4FromFqdn(pNodeInfo->nodeFqdn); + assert(ipv4 != 0xFFFFFFFF); + char ipbuf[128]; + tinet_ntoa(ipbuf, ipv4); + raftId->addr = syncUtilAddr2U64(ipbuf, pNodeInfo->nodePort); raftId->vgId = vgId; } From 398d5ec3a7219feed9557d3efcc1108eb35af1bd Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 16:37:04 +0800 Subject: [PATCH 13/19] sync io --- source/libs/sync/src/syncIO.c | 2 +- source/libs/sync/test/CMakeLists.txt | 42 ++++++++++++++++ .../sync/test/syncIOSendMsgClientTest.cpp | 50 +++++++++++++++++++ .../sync/test/syncIOSendMsgServerTest.cpp | 33 ++++++++++++ source/libs/sync/test/syncIOSendMsgTest.cpp | 50 +++++++++++++++++++ source/libs/sync/test/syncPingTest.cpp | 5 +- 6 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 source/libs/sync/test/syncIOSendMsgClientTest.cpp create mode 100644 source/libs/sync/test/syncIOSendMsgServerTest.cpp create mode 100644 source/libs/sync/test/syncIOSendMsgTest.cpp diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 96bb305e76..88fba6429d 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -112,7 +112,7 @@ static int32_t syncIOStartInternal(SSyncIO *io) { { SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 7010; + rpcInit.localPort = io->myAddr.eps[0].port; rpcInit.label = "SYNC-IO-SERVER"; rpcInit.numOfThreads = 1; rpcInit.cfp = syncIOProcessRequest; diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index c089987c19..f28bbf46cc 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -4,6 +4,9 @@ add_executable(syncPingTest "") add_executable(syncEncodeTest "") add_executable(syncIOTickQTest "") add_executable(syncIOTickPingTest "") +add_executable(syncIOSendMsgTest "") +add_executable(syncIOSendMsgClientTest "") +add_executable(syncIOSendMsgServerTest "") target_sources(syncTest @@ -30,6 +33,18 @@ target_sources(syncIOTickPingTest PRIVATE "syncIOTickPingTest.cpp" ) +target_sources(syncIOSendMsgTest + PRIVATE + "syncIOSendMsgTest.cpp" +) +target_sources(syncIOSendMsgClientTest + PRIVATE + "syncIOSendMsgClientTest.cpp" +) +target_sources(syncIOSendMsgServerTest + PRIVATE + "syncIOSendMsgServerTest.cpp" +) target_include_directories(syncTest @@ -62,6 +77,21 @@ target_include_directories(syncIOTickPingTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncIOSendMsgTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncIOSendMsgClientTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncIOSendMsgServerTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -88,6 +118,18 @@ target_link_libraries(syncIOTickPingTest sync gtest_main ) +target_link_libraries(syncIOSendMsgTest + sync + gtest_main +) +target_link_libraries(syncIOSendMsgClientTest + sync + gtest_main +) +target_link_libraries(syncIOSendMsgServerTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncIOSendMsgClientTest.cpp b/source/libs/sync/test/syncIOSendMsgClientTest.cpp new file mode 100644 index 0000000000..09dc9bbb42 --- /dev/null +++ b/source/libs/sync/test/syncIOSendMsgClientTest.cpp @@ -0,0 +1,50 @@ +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + logTest(); + + int32_t ret; + + ret = syncIOStart((char *)"127.0.0.1", 7010); + assert(ret == 0); + + for (int i = 0; i < 10; ++i) { + SEpSet epSet; + epSet.inUse = 0; + epSet.numOfEps = 1; + addEpIntoEpSet(&epSet, "127.0.0.1", 7030); + + SRpcMsg rpcMsg; + rpcMsg.contLen = 64; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); + rpcMsg.handle = NULL; + rpcMsg.msgType = 77; + + syncIOSendMsg(gSyncIO->clientRpc, &epSet, &rpcMsg); + sleep(1); + } + + while (1) { + sleep(1); + } + + return 0; +} diff --git a/source/libs/sync/test/syncIOSendMsgServerTest.cpp b/source/libs/sync/test/syncIOSendMsgServerTest.cpp new file mode 100644 index 0000000000..8af9344ed6 --- /dev/null +++ b/source/libs/sync/test/syncIOSendMsgServerTest.cpp @@ -0,0 +1,33 @@ +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + logTest(); + + int32_t ret; + + ret = syncIOStart((char *)"127.0.0.1", 7030); + assert(ret == 0); + + while (1) { + sleep(1); + } + + return 0; +} diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp new file mode 100644 index 0000000000..70fbe55b67 --- /dev/null +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -0,0 +1,50 @@ +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + logTest(); + + int32_t ret; + + ret = syncIOStart((char *)"127.0.0.1", 7010); + assert(ret == 0); + + for (int i = 0; i < 10; ++i) { + SEpSet epSet; + epSet.inUse = 0; + epSet.numOfEps = 1; + addEpIntoEpSet(&epSet, "127.0.0.1", 7010); + + SRpcMsg rpcMsg; + rpcMsg.contLen = 64; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); + rpcMsg.handle = NULL; + rpcMsg.msgType = 77; + + syncIOSendMsg(gSyncIO->clientRpc, &epSet, &rpcMsg); + sleep(1); + } + + while (1) { + sleep(1); + } + + return 0; +} diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index b80b0f2e20..01e5731dde 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -68,10 +68,11 @@ int main() { ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); - taosMsleep(5000); - + /* + taosMsleep(10000); ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); + */ while (1) { taosMsleep(1000); From 8057e44d1702af275db0cf30d085692c766b7fa5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 17:40:22 +0800 Subject: [PATCH 14/19] sync modify timer --- source/libs/sync/src/syncIO.c | 4 +++- source/libs/sync/src/syncMain.c | 14 +++++++++----- source/libs/sync/src/syncMessage.c | 4 ++-- source/libs/sync/src/syncUtil.c | 5 ++--- source/libs/sync/test/syncIOSendMsgClientTest.cpp | 2 +- source/libs/sync/test/syncIOSendMsgTest.cpp | 2 +- source/libs/sync/test/syncPingTest.cpp | 9 ++++++--- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 88fba6429d..74f848338f 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -41,7 +41,8 @@ static void syncIOTickPingFunc(void *param, void *tmrId); // public function ------------ int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { - sTrace("syncIOSendMsg ... "); + sTrace("<--- syncIOSendMsg ---> clientRpc:%p, numOfEps:%d, inUse:%d, destAddr:%s-%u", clientRpc, pEpSet->numOfEps, + pEpSet->inUse, pEpSet->eps[0].fqdn, pEpSet->eps[0].port); pMsg->handle = NULL; rpcSendRequest(clientRpc, pEpSet, pMsg, NULL); return 0; @@ -54,6 +55,7 @@ int32_t syncIOStart(char *host, uint16_t port) { int32_t ret = syncIOStartInternal(gSyncIO); assert(ret == 0); + sTrace("syncIOStart ok, gSyncIO:%p gSyncIO->clientRpc:%p", gSyncIO, gSyncIO->clientRpc); return 0; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 9e335c5e55..d19c51900f 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -184,7 +184,7 @@ static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, Syn SyncPing* pMsg2 = rpcMsg.pCont; cJSON* pJson = syncPing2Json(pMsg2); char* serialized = cJSON_Print(pJson); - sTrace("syncNodePing pMsg2:%s ", serialized); + sTrace("syncNodePing rpcMsg.pCont:%s ", serialized); free(serialized); cJSON_Delete(pJson); } @@ -253,12 +253,16 @@ static void syncNodePingTimerCb(void* param, void* tmrId) { ++(pSyncNode->pingTimerCounter); // pSyncNode->pingTimerMS += 100; - sTrace("pSyncNode->pingTimerCounter:%lu, pSyncNode->pingTimerMS:%d, pSyncNode->pPingTimer:%p, tmrId:%p ", - pSyncNode->pingTimerCounter, pSyncNode->pingTimerMS, pSyncNode->pPingTimer, tmrId); + sTrace( + "syncNodePingTimerCb: pSyncNode->pingTimerCounter:%lu, pSyncNode->pingTimerMS:%d, pSyncNode->pPingTimer:%p, " + "tmrId:%p ", + pSyncNode->pingTimerCounter, pSyncNode->pingTimerMS, pSyncNode->pPingTimer, tmrId); + + syncNodePingAll(pSyncNode); taosTmrReset(syncNodePingTimerCb, pSyncNode->pingTimerMS, pSyncNode, &gSyncEnv->pTimerManager, &pSyncNode->pPingTimer); - - syncNodePingAll(pSyncNode); + } else { + sTrace("syncNodePingTimerCb: pingTimerStart:%u ", pSyncNode->pingTimerStart); } } \ No newline at end of file diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index e0975fb3c1..8e7dc8ba61 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -70,7 +70,7 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { cJSON* pDestId = cJSON_CreateObject(); cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); - cJSON_AddItemToObject(pRoot, "srcId", pDestId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); cJSON_AddStringToObject(pRoot, "data", pMsg->data); @@ -145,7 +145,7 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { cJSON* pDestId = cJSON_CreateObject(); cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); - cJSON_AddItemToObject(pRoot, "srcId", pDestId); + cJSON_AddItemToObject(pRoot, "destId", pDestId); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); cJSON_AddStringToObject(pRoot, "data", pMsg->data); diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 9c134e2d08..a3fbfaf905 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -51,11 +51,10 @@ void syncUtilraftId2EpSet(const SRaftId* raftId, SEpSet* pEpSet) { pEpSet->numOfEps = 1; pEpSet->inUse = 0; pEpSet->eps[0].port = port; - snprintf(epSet.eps[0].fqdn, sizeof(epSet.eps[0].fqdn), host); + snprintf(pEpSet->eps[0].fqdn, sizeof(pEpSet->eps[0].fqdn), "%s", host); */ - pEpSet->inUse = 0; - pEpSet->numOfEps = 1; + pEpSet->numOfEps = 0; addEpIntoEpSet(pEpSet, host, port); } diff --git a/source/libs/sync/test/syncIOSendMsgClientTest.cpp b/source/libs/sync/test/syncIOSendMsgClientTest.cpp index 09dc9bbb42..8adaff1a2f 100644 --- a/source/libs/sync/test/syncIOSendMsgClientTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgClientTest.cpp @@ -34,7 +34,7 @@ int main() { SRpcMsg rpcMsg; rpcMsg.contLen = 64; rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); - snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); + snprintf((char *)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); rpcMsg.handle = NULL; rpcMsg.msgType = 77; diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp index 70fbe55b67..33af51f23d 100644 --- a/source/libs/sync/test/syncIOSendMsgTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -34,7 +34,7 @@ int main() { SRpcMsg rpcMsg; rpcMsg.contLen = 64; rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); - snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); + snprintf((char *)rpcMsg.pCont, rpcMsg.contLen, "%s", "syncIOSendMsgTest"); rpcMsg.handle = NULL; rpcMsg.msgType = 77; diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 01e5731dde..902513f7cb 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -28,13 +28,16 @@ SSyncNode* doSync() { pCfg->replicaNum = 1; pCfg->nodeInfo[0].nodePort = 7010; - taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); + snprintf(pCfg->nodeInfo[0].nodeFqdn, sizeof(pCfg->nodeInfo[0].nodeFqdn), "%s", "127.0.0.1"); + // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); pCfg->nodeInfo[1].nodePort = 7110; - taosGetFqdn(pCfg->nodeInfo[1].nodeFqdn); + snprintf(pCfg->nodeInfo[1].nodeFqdn, sizeof(pCfg->nodeInfo[1].nodeFqdn), "%s", "127.0.0.1"); + // taosGetFqdn(pCfg->nodeInfo[1].nodeFqdn); pCfg->nodeInfo[2].nodePort = 7210; - taosGetFqdn(pCfg->nodeInfo[2].nodeFqdn); + snprintf(pCfg->nodeInfo[2].nodeFqdn, sizeof(pCfg->nodeInfo[2].nodeFqdn), "%s", "127.0.0.1"); + // taosGetFqdn(pCfg->nodeInfo[2].nodeFqdn); SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); assert(pSyncNode != NULL); From 6d6e7bdf3d854bb6d423ac4cf8d9fa75bc420dbb Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 18:30:21 +0800 Subject: [PATCH 15/19] sync modify timer --- source/libs/sync/inc/syncMessage.h | 2 +- source/libs/sync/src/syncIO.c | 7 +++++-- source/libs/sync/src/syncMain.c | 12 ++++++++++++ source/libs/sync/src/syncMessage.c | 1 + source/libs/sync/test/syncIOSendMsgClientTest.cpp | 2 +- source/libs/sync/test/syncIOSendMsgTest.cpp | 2 +- source/libs/sync/test/syncPingTest.cpp | 1 + 7 files changed, 22 insertions(+), 5 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 603ef00517..a4ae910608 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -30,7 +30,7 @@ extern "C" { // encode as uint64 typedef enum ESyncMessageType { - SYNC_PING = 0, + SYNC_PING = 100, SYNC_PING_REPLY, SYNC_CLIENT_REQUEST, SYNC_CLIENT_REQUEST_REPLY, diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 74f848338f..bd49b04375 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -41,8 +41,11 @@ static void syncIOTickPingFunc(void *param, void *tmrId); // public function ------------ int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { - sTrace("<--- syncIOSendMsg ---> clientRpc:%p, numOfEps:%d, inUse:%d, destAddr:%s-%u", clientRpc, pEpSet->numOfEps, - pEpSet->inUse, pEpSet->eps[0].fqdn, pEpSet->eps[0].port); + sTrace( + "<--- syncIOSendMsg ---> clientRpc:%p, numOfEps:%d, inUse:%d, destAddr:%s-%u, pMsg->ahandle:%p, pMsg->handle:%p, " + "pMsg->msgType:%d, pMsg->contLen:%d", + clientRpc, pEpSet->numOfEps, pEpSet->inUse, pEpSet->eps[0].fqdn, pEpSet->eps[0].port, pMsg->ahandle, pMsg->handle, + pMsg->msgType, pMsg->contLen); pMsg->handle = NULL; rpcSendRequest(clientRpc, pEpSet, pMsg, NULL); return 0; diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index d19c51900f..180a2b13a1 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -168,8 +168,19 @@ int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode) { static int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg) { sTrace("syncNodePing pSyncNode:%p ", pSyncNode); int32_t ret = 0; + SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); + + /* + SRpcMsg rpcMsg; + rpcMsg.contLen = 64; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + snprintf((char*)rpcMsg.pCont, rpcMsg.contLen, "%s", "xxxxxxxxxxxxxx"); + rpcMsg.handle = NULL; + rpcMsg.msgType = 1; + */ + syncNodeSendMsgById(destRaftId, pSyncNode, &rpcMsg); { @@ -219,6 +230,7 @@ static int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSync static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { int32_t ret = 0; + sTrace("syncNodeOnPingCb ---- ========="); return ret; } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 8e7dc8ba61..1d1b2e6965 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -47,6 +47,7 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { } void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; pRpcMsg->contLen = pMsg->bytes; pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); diff --git a/source/libs/sync/test/syncIOSendMsgClientTest.cpp b/source/libs/sync/test/syncIOSendMsgClientTest.cpp index 8adaff1a2f..9e8c7c8b65 100644 --- a/source/libs/sync/test/syncIOSendMsgClientTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgClientTest.cpp @@ -28,7 +28,7 @@ int main() { for (int i = 0; i < 10; ++i) { SEpSet epSet; epSet.inUse = 0; - epSet.numOfEps = 1; + epSet.numOfEps = 0; addEpIntoEpSet(&epSet, "127.0.0.1", 7030); SRpcMsg rpcMsg; diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp index 33af51f23d..a297981ee5 100644 --- a/source/libs/sync/test/syncIOSendMsgTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -28,7 +28,7 @@ int main() { for (int i = 0; i < 10; ++i) { SEpSet epSet; epSet.inUse = 0; - epSet.numOfEps = 1; + epSet.numOfEps = 0; addEpIntoEpSet(&epSet, "127.0.0.1", 7010); SRpcMsg rpcMsg; diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 902513f7cb..002838b010 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -67,6 +67,7 @@ int main() { assert(ret == 0); SSyncNode* pSyncNode = doSync(); + gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); From 99bc46e7de6bbcd938c5d413c033d0e560b03b74 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 19:56:15 +0800 Subject: [PATCH 16/19] sync modify timer --- source/libs/sync/src/syncIO.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index bd49b04375..85a9459483 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -210,10 +210,28 @@ static void *syncIOConsumerFunc(void *param) { if (pRpcMsg->msgType == SYNC_PING) { if (io->FpOnSyncPing != NULL) { - SyncPing *pSyncMsg = syncPingBuild(pRpcMsg->contLen); - syncPingFromRpcMsg(pRpcMsg, pSyncMsg); + SyncPing *pSyncMsg; + + SRpcMsg tmpRpcMsg; + memcpy(&tmpRpcMsg, pRpcMsg, sizeof(SRpcMsg)); + pSyncMsg = syncPingBuild(tmpRpcMsg.contLen); + + // syncPingFromRpcMsg(pRpcMsg, pSyncMsg); + + memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen); + +#if 0 + { + cJSON *pJson = syncPing2Json(pSyncMsg); + char *serialized = cJSON_Print(pJson); + sTrace("syncIOConsumerFunc syncNodePing pMsg:%s ", serialized); + free(serialized); + cJSON_Delete(pJson); + } +#endif io->FpOnSyncPing(io->pSyncNode, pSyncMsg); } + } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen); syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg); @@ -257,7 +275,8 @@ static int syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, cha } static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - sTrace("syncIOProcessRequest: type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen, (char *)pMsg->pCont); + sTrace("<-- syncIOProcessRequest --> type:%d, contLen:%d, cont:%s", pMsg->msgType, pMsg->contLen, + (char *)pMsg->pCont); SSyncIO *io = pParent; SRpcMsg *pTemp; From 08df4b42de6823bbdf9231aa68d0e974f86be371 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 20:08:46 +0800 Subject: [PATCH 17/19] sync modify timer --- source/libs/sync/inc/syncMessage.h | 4 ++-- source/libs/sync/src/syncIO.c | 13 ++----------- source/libs/sync/src/syncMain.c | 11 ++++++++++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index a4ae910608..bb9ec65d3b 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -30,8 +30,8 @@ extern "C" { // encode as uint64 typedef enum ESyncMessageType { - SYNC_PING = 100, - SYNC_PING_REPLY, + SYNC_PING = 101, + SYNC_PING_REPLY = 103, SYNC_CLIENT_REQUEST, SYNC_CLIENT_REQUEST_REPLY, SYNC_REQUEST_VOTE, diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 85a9459483..590466d1cc 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -216,19 +216,10 @@ static void *syncIOConsumerFunc(void *param) { memcpy(&tmpRpcMsg, pRpcMsg, sizeof(SRpcMsg)); pSyncMsg = syncPingBuild(tmpRpcMsg.contLen); - // syncPingFromRpcMsg(pRpcMsg, pSyncMsg); + syncPingFromRpcMsg(pRpcMsg, pSyncMsg); - memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen); + // memcpy(pSyncMsg, tmpRpcMsg.pCont, tmpRpcMsg.contLen); -#if 0 - { - cJSON *pJson = syncPing2Json(pSyncMsg); - char *serialized = cJSON_Print(pJson); - sTrace("syncIOConsumerFunc syncNodePing pMsg:%s ", serialized); - free(serialized); - cJSON_Delete(pJson); - } -#endif io->FpOnSyncPing(io->pSyncNode, pSyncMsg); } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 180a2b13a1..ce3dba4c0e 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -230,7 +230,16 @@ static int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSync static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { int32_t ret = 0; - sTrace("syncNodeOnPingCb ---- ========="); + sTrace("<-- syncNodeOnPingCb -->"); + + { + cJSON* pJson = syncPing2Json(pMsg); + char* serialized = cJSON_Print(pJson); + sTrace("syncNodeOnPingCb syncNodePing pMsg:%s ", serialized); + free(serialized); + cJSON_Delete(pJson); + } + return ret; } From fe1f280ce4440c1d71fdda5897eb5ae1c2f161d5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 2 Mar 2022 20:43:03 +0800 Subject: [PATCH 18/19] sync modify timer --- source/libs/sync/inc/syncMessage.h | 4 ++ source/libs/sync/src/syncIO.c | 5 ++- source/libs/sync/src/syncMain.c | 15 +++++++ source/libs/sync/src/syncMessage.c | 54 +++++++++++++++++++++++++- source/libs/sync/src/syncUtil.c | 1 + source/libs/sync/test/syncPingTest.cpp | 3 +- 6 files changed, 79 insertions(+), 3 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index bb9ec65d3b..be53559e8a 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -102,6 +102,10 @@ void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); cJSON* syncPingReply2Json(const SyncPingReply* pMsg); +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); + +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId); + typedef struct SyncClientRequest { ESyncMessageType msgType; char* data; diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 590466d1cc..abb52b42d0 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -226,7 +226,10 @@ static void *syncIOConsumerFunc(void *param) { } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { SyncPingReply *pSyncMsg = syncPingReplyBuild(pRpcMsg->contLen); syncPingReplyFromRpcMsg(pRpcMsg, pSyncMsg); - io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg); + + if (io->FpOnSyncPingReply != NULL) { + io->FpOnSyncPingReply(io->pSyncNode, pSyncMsg); + } } else { ; } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index ce3dba4c0e..49fac038da 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -240,11 +240,26 @@ static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { cJSON_Delete(pJson); } + SyncPingReply* pMsgReply = syncPingReplyBuild3(&ths->raftId, &pMsg->srcId); + SRpcMsg rpcMsg; + syncPingReply2RpcMsg(pMsgReply, &rpcMsg); + syncNodeSendMsgById(&pMsgReply->destId, ths, &rpcMsg); + return ret; } static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { int32_t ret = 0; + sTrace("<-- syncNodeOnPingReplyCb -->"); + + { + cJSON* pJson = syncPingReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + sTrace("syncNodeOnPingReplyCb syncNodePing pMsg:%s ", serialized); + free(serialized); + cJSON_Delete(pJson); + } + return ret; } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 1d1b2e6965..a26c8401b2 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -15,6 +15,7 @@ #include "syncMessage.h" #include "syncRaft.h" +#include "syncUtil.h" #include "tcoding.h" void onMessage(SRaft* pRaft, void* pMsg) {} @@ -65,11 +66,29 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { cJSON* pSrcId = cJSON_CreateObject(); cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); cJSON_AddItemToObject(pRoot, "srcId", pSrcId); cJSON* pDestId = cJSON_CreateObject(); cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); cJSON_AddItemToObject(pRoot, "destId", pDestId); @@ -101,7 +120,7 @@ SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { SyncPingReply* pMsg = malloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_PING; + pMsg->msgType = SYNC_PING_REPLY; pMsg->dataLen = dataLen; } @@ -123,6 +142,7 @@ void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg } void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; pRpcMsg->contLen = pMsg->bytes; pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); @@ -140,11 +160,29 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { cJSON* pSrcId = cJSON_CreateObject(); cJSON_AddNumberToObject(pSrcId, "addr", pMsg->srcId.addr); + { + uint64_t u64 = pMsg->srcId.addr; + cJSON* pTmp = pSrcId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } cJSON_AddNumberToObject(pSrcId, "vgId", pMsg->srcId.vgId); cJSON_AddItemToObject(pRoot, "srcId", pSrcId); cJSON* pDestId = cJSON_CreateObject(); cJSON_AddNumberToObject(pDestId, "addr", pMsg->destId.addr); + { + uint64_t u64 = pMsg->destId.addr; + cJSON* pTmp = pDestId; + char host[128]; + uint16_t port; + syncUtilU642Addr(u64, host, sizeof(host), &port); + cJSON_AddStringToObject(pTmp, "addr_host", host); + cJSON_AddNumberToObject(pTmp, "addr_port", port); + } cJSON_AddNumberToObject(pDestId, "vgId", pMsg->destId.vgId); cJSON_AddItemToObject(pRoot, "destId", pDestId); @@ -156,6 +194,20 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { return pJson; } +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { + uint32_t dataLen = strlen(str) + 1; + SyncPingReply* pMsg = syncPingReplyBuild(dataLen); + pMsg->srcId = *srcId; + pMsg->destId = *destId; + snprintf(pMsg->data, pMsg->dataLen, "%s", str); + return pMsg; +} + +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId) { + SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, "pang"); + return pMsg; +} + #if 0 void syncPingSerialize(const SyncPing* pMsg, char** ppBuf, uint32_t* bufLen) { *bufLen = sizeof(SyncPing) + pMsg->dataLen; diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index a3fbfaf905..b4959a810b 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -38,6 +38,7 @@ void syncUtilU642Addr(uint64_t u64, char* host, size_t len, uint16_t* port) { void syncUtilnodeInfo2EpSet(const SNodeInfo* pNodeInfo, SEpSet* pEpSet) { pEpSet->inUse = 0; + pEpSet->numOfEps = 0; addEpIntoEpSet(pEpSet, pNodeInfo->nodeFqdn, pNodeInfo->nodePort); } diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 002838b010..24d9ead5e3 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -25,7 +25,7 @@ SSyncNode* doSync() { SSyncCfg* pCfg = &syncInfo.syncCfg; pCfg->myIndex = 0; - pCfg->replicaNum = 1; + pCfg->replicaNum = 2; pCfg->nodeInfo[0].nodePort = 7010; snprintf(pCfg->nodeInfo[0].nodeFqdn, sizeof(pCfg->nodeInfo[0].nodeFqdn), "%s", "127.0.0.1"); @@ -68,6 +68,7 @@ int main() { SSyncNode* pSyncNode = doSync(); gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; + gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); From a78d7028ef51edd829922713df6c60e3fb714ecb Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 3 Mar 2022 10:46:48 +0800 Subject: [PATCH 19/19] new fileIO --- source/libs/sync/inc/syncRaftStore.h | 3 +- source/libs/sync/src/syncIO.c | 2 +- source/libs/sync/src/syncRaftStore.c | 120 ++++++++++++++++++-- source/libs/sync/test/CMakeLists.txt | 14 +++ source/libs/sync/test/syncRaftStoreTest.cpp | 42 +++++++ 5 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 source/libs/sync/test/syncRaftStoreTest.cpp diff --git a/source/libs/sync/inc/syncRaftStore.h b/source/libs/sync/inc/syncRaftStore.h index c54f1a1c83..c480486ff0 100644 --- a/source/libs/sync/inc/syncRaftStore.h +++ b/source/libs/sync/inc/syncRaftStore.h @@ -35,7 +35,8 @@ typedef struct SRaftStore { SyncTerm currentTerm; SRaftId voteFor; // FileFd fd; - char path[RAFT_STORE_PATH_LEN]; + TdFilePtr pFile; + char path[RAFT_STORE_PATH_LEN]; } SRaftStore; SRaftStore *raftStoreOpen(const char *path); diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index abb52b42d0..8d04b5bb26 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -193,7 +193,7 @@ static void *syncIOConsumerFunc(void *param) { SSyncIO *io = param; STaosQall *qall; - SRpcMsg *pRpcMsg, rpcMsg; + SRpcMsg * pRpcMsg, rpcMsg; int type; qall = taosAllocateQall(); diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index 1c88be72e7..59c85c38de 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -18,21 +18,125 @@ // to complie success: FileIO interface is modified -SRaftStore *raftStoreOpen(const char *path) { return NULL; } +SRaftStore *raftStoreOpen(const char *path) { + int32_t ret; -static int32_t raftStoreInit(SRaftStore *pRaftStore) { return 0; } + SRaftStore *pRaftStore = malloc(sizeof(SRaftStore)); + if (pRaftStore == NULL) { + sError("raftStoreOpen malloc error"); + return NULL; + } + memset(pRaftStore, 0, sizeof(*pRaftStore)); + snprintf(pRaftStore->path, sizeof(pRaftStore->path), "%s", path); -int32_t raftStoreClose(SRaftStore *pRaftStore) { return 0; } + char storeBuf[RAFT_STORE_BLOCK_SIZE]; + memset(storeBuf, 0, sizeof(storeBuf)); -int32_t raftStorePersist(SRaftStore *pRaftStore) { return 0; } + if (!raftStoreFileExist(pRaftStore->path)) { + ret = raftStoreInit(pRaftStore); + assert(ret == 0); + } -static bool raftStoreFileExist(char *path) { return 0; } + pRaftStore->pFile = taosOpenFile(path, TD_FILE_READ | TD_FILE_WRITE); + assert(pRaftStore->pFile != NULL); -int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0; } + int len = taosReadFile(pRaftStore->pFile, storeBuf, RAFT_STORE_BLOCK_SIZE); + assert(len == RAFT_STORE_BLOCK_SIZE); -int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0; } + ret = raftStoreDeserialize(pRaftStore, storeBuf, len); + assert(ret == 0); -void raftStorePrint(SRaftStore *pRaftStore) {} + return pRaftStore; +} + +static int32_t raftStoreInit(SRaftStore *pRaftStore) { + assert(pRaftStore != NULL); + + pRaftStore->pFile = taosOpenFile(pRaftStore->path, TD_FILE_CTEATE | TD_FILE_WRITE); + assert(pRaftStore->pFile != NULL); + + pRaftStore->currentTerm = 0; + pRaftStore->voteFor.addr = 0; + pRaftStore->voteFor.vgId = 0; + + int32_t ret = raftStorePersist(pRaftStore); + assert(ret == 0); + + taosCloseFile(&pRaftStore->pFile); + return 0; +} + +int32_t raftStoreClose(SRaftStore *pRaftStore) { + assert(pRaftStore != NULL); + + taosCloseFile(&pRaftStore->pFile); + free(pRaftStore); + pRaftStore = NULL; + return 0; +} + +int32_t raftStorePersist(SRaftStore *pRaftStore) { + assert(pRaftStore != NULL); + + int32_t ret; + char storeBuf[RAFT_STORE_BLOCK_SIZE]; + ret = raftStoreSerialize(pRaftStore, storeBuf, sizeof(storeBuf)); + assert(ret == 0); + + taosLSeekFile(pRaftStore->pFile, 0, SEEK_SET); + + ret = taosWriteFile(pRaftStore->pFile, storeBuf, sizeof(storeBuf)); + assert(ret == RAFT_STORE_BLOCK_SIZE); + + taosFsyncFile(pRaftStore->pFile); + return 0; +} + +static bool raftStoreFileExist(char *path) { return taosStatFile(path, NULL, NULL) >= 0; } + +int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len) { + assert(pRaftStore != NULL); + + cJSON *pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "current_term", pRaftStore->currentTerm); + cJSON_AddNumberToObject(pRoot, "vote_for_addr", pRaftStore->voteFor.addr); + cJSON_AddNumberToObject(pRoot, "vote_for_vgid", pRaftStore->voteFor.vgId); + + char *serialized = cJSON_Print(pRoot); + int len2 = strlen(serialized); + assert(len2 < len); + memset(buf, 0, len); + snprintf(buf, len, "%s", serialized); + free(serialized); + + cJSON_Delete(pRoot); + return 0; +} + +int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len) { + assert(pRaftStore != NULL); + + assert(len > 0 && len <= RAFT_STORE_BLOCK_SIZE); + cJSON *pRoot = cJSON_Parse(buf); + + cJSON *pCurrentTerm = cJSON_GetObjectItem(pRoot, "current_term"); + pRaftStore->currentTerm = pCurrentTerm->valueint; + + cJSON *pVoteForAddr = cJSON_GetObjectItem(pRoot, "vote_for_addr"); + pRaftStore->voteFor.addr = pVoteForAddr->valueint; + + cJSON *pVoteForVgid = cJSON_GetObjectItem(pRoot, "vote_for_vgid"); + pRaftStore->voteFor.vgId = pVoteForVgid->valueint; + + cJSON_Delete(pRoot); + return 0; +} + +void raftStorePrint(SRaftStore *pRaftStore) { + char storeBuf[RAFT_STORE_BLOCK_SIZE]; + raftStoreSerialize(pRaftStore, storeBuf, sizeof(storeBuf)); + printf("%s\n", storeBuf); +} #if 0 diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index f28bbf46cc..2913d230b2 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(syncIOTickPingTest "") add_executable(syncIOSendMsgTest "") add_executable(syncIOSendMsgClientTest "") add_executable(syncIOSendMsgServerTest "") +add_executable(syncRaftStoreTest "") target_sources(syncTest @@ -45,6 +46,10 @@ target_sources(syncIOSendMsgServerTest PRIVATE "syncIOSendMsgServerTest.cpp" ) +target_sources(syncRaftStoreTest + PRIVATE + "syncRaftStoreTest.cpp" +) target_include_directories(syncTest @@ -92,6 +97,11 @@ target_include_directories(syncIOSendMsgServerTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncRaftStoreTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -130,6 +140,10 @@ target_link_libraries(syncIOSendMsgServerTest sync gtest_main ) +target_link_libraries(syncRaftStoreTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncRaftStoreTest.cpp b/source/libs/sync/test/syncRaftStoreTest.cpp new file mode 100644 index 0000000000..e533b89a92 --- /dev/null +++ b/source/libs/sync/test/syncRaftStoreTest.cpp @@ -0,0 +1,42 @@ +#include "syncRaftStore.h" +#include +#include "gtest/gtest.h" +#include "syncIO.h" +#include "syncInt.h" + +void *pingFunc(void *param) { + SSyncIO *io = (SSyncIO *)param; + while (1) { + sDebug("io->ping"); + // io->ping(io); + sleep(1); + } + return NULL; +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + sTrace("sync log test: trace"); + sDebug("sync log test: debug"); + sInfo("sync log test: info"); + sWarn("sync log test: warn"); + sError("sync log test: error"); + sFatal("sync log test: fatal"); + + SRaftStore *pRaftStore = raftStoreOpen("./raft_store.json"); + assert(pRaftStore != NULL); + + raftStorePrint(pRaftStore); + + pRaftStore->currentTerm = 100; + pRaftStore->voteFor.addr = 200; + pRaftStore->voteFor.vgId = 300; + + raftStorePrint(pRaftStore); + raftStorePersist(pRaftStore); + + return 0; +}