sync refactor
This commit is contained in:
parent
94efe10119
commit
be532bd5eb
|
@ -29,6 +29,7 @@ extern "C" {
|
||||||
#include "ttimer.h"
|
#include "ttimer.h"
|
||||||
|
|
||||||
#define TIMER_MAX_MS 0x7FFFFFFF
|
#define TIMER_MAX_MS 0x7FFFFFFF
|
||||||
|
#define ENV_TICK_TIMER_MS 1000
|
||||||
#define PING_TIMER_MS 1000
|
#define PING_TIMER_MS 1000
|
||||||
#define ELECT_TIMER_MS_MIN 150
|
#define ELECT_TIMER_MS_MIN 150
|
||||||
#define ELECT_TIMER_MS_MAX 300
|
#define ELECT_TIMER_MS_MAX 300
|
||||||
|
@ -38,17 +39,28 @@ extern "C" {
|
||||||
#define EMPTY_RAFT_ID ((SRaftId){.addr = 0, .vgId = 0})
|
#define EMPTY_RAFT_ID ((SRaftId){.addr = 0, .vgId = 0})
|
||||||
|
|
||||||
typedef struct SSyncEnv {
|
typedef struct SSyncEnv {
|
||||||
tmr_h pEnvTickTimer;
|
// tick timer
|
||||||
|
tmr_h pEnvTickTimer;
|
||||||
|
int32_t envTickTimerMS;
|
||||||
|
uint64_t envTickTimerLogicClock;
|
||||||
|
uint64_t envTickTimerLogicClockUser;
|
||||||
|
TAOS_TMR_CALLBACK FpEnvTickTimer; // Timer Fp
|
||||||
|
uint64_t envTickTimerCounter;
|
||||||
|
|
||||||
|
// timer manager
|
||||||
tmr_h pTimerManager;
|
tmr_h pTimerManager;
|
||||||
char name[128];
|
|
||||||
|
// other resources shared by SyncNodes
|
||||||
|
// ...
|
||||||
|
|
||||||
} SSyncEnv;
|
} SSyncEnv;
|
||||||
|
|
||||||
extern SSyncEnv* gSyncEnv;
|
extern SSyncEnv* gSyncEnv;
|
||||||
|
|
||||||
int32_t syncEnvStart();
|
int32_t syncEnvStart();
|
||||||
int32_t syncEnvStop();
|
int32_t syncEnvStop();
|
||||||
tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void* param);
|
int32_t syncEnvStartTimer();
|
||||||
void syncEnvStopTimer(tmr_h* pTimer);
|
int32_t syncEnvStopTimer();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,19 +19,18 @@
|
||||||
SSyncEnv *gSyncEnv = NULL;
|
SSyncEnv *gSyncEnv = NULL;
|
||||||
|
|
||||||
// local function -----------------
|
// local function -----------------
|
||||||
static void syncEnvTick(void *param, void *tmrId);
|
static SSyncEnv *doSyncEnvStart();
|
||||||
static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv);
|
static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv);
|
||||||
static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv);
|
static int32_t doSyncEnvStartTimer(SSyncEnv *pSyncEnv);
|
||||||
static tmr_h doSyncEnvStartTimer(SSyncEnv *pSyncEnv, TAOS_TMR_CALLBACK fp, int mseconds, void *param);
|
static int32_t doSyncEnvStopTimer(SSyncEnv *pSyncEnv);
|
||||||
static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer);
|
static void syncEnvTick(void *param, void *tmrId);
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
|
|
||||||
int32_t syncEnvStart() {
|
int32_t syncEnvStart() {
|
||||||
int32_t ret;
|
int32_t ret = 0;
|
||||||
taosSeedRand(taosGetTimestampSec());
|
taosSeedRand(taosGetTimestampSec());
|
||||||
gSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv));
|
gSyncEnv = doSyncEnvStart(gSyncEnv);
|
||||||
assert(gSyncEnv != NULL);
|
assert(gSyncEnv != NULL);
|
||||||
ret = doSyncEnvStart(gSyncEnv);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,31 +39,46 @@ int32_t syncEnvStop() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmr_h syncEnvStartTimer(TAOS_TMR_CALLBACK fp, int mseconds, void *param) {
|
int32_t syncEnvStartTimer() {
|
||||||
return doSyncEnvStartTimer(gSyncEnv, fp, mseconds, param);
|
int32_t ret = doSyncEnvStartTimer(gSyncEnv);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void syncEnvStopTimer(tmr_h *pTimer) { doSyncEnvStopTimer(gSyncEnv, pTimer); }
|
int32_t syncEnvStopTimer() {
|
||||||
|
int32_t ret = doSyncEnvStopTimer(gSyncEnv);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// local function -----------------
|
// local function -----------------
|
||||||
static void syncEnvTick(void *param, void *tmrId) {
|
static void syncEnvTick(void *param, void *tmrId) {
|
||||||
SSyncEnv *pSyncEnv = (SSyncEnv *)param;
|
SSyncEnv *pSyncEnv = (SSyncEnv *)param;
|
||||||
sTrace("syncEnvTick ... name:%s ", pSyncEnv->name);
|
if (atomic_load_64(&pSyncEnv->envTickTimerLogicClockUser) <= atomic_load_64(&pSyncEnv->envTickTimerLogicClock)) {
|
||||||
|
++(pSyncEnv->envTickTimerCounter);
|
||||||
|
sTrace(
|
||||||
|
"syncEnvTick ... envTickTimerLogicClockUser:%lu, envTickTimerLogicClock:%lu, envTickTimerCounter:%lu, "
|
||||||
|
"envTickTimerMS:%d",
|
||||||
|
pSyncEnv->envTickTimerLogicClockUser, pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerCounter,
|
||||||
|
pSyncEnv->envTickTimerMS);
|
||||||
|
|
||||||
pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager);
|
// do something, tick ...
|
||||||
|
taosTmrReset(syncEnvTick, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager, &pSyncEnv->pEnvTickTimer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t doSyncEnvStart(SSyncEnv *pSyncEnv) {
|
static SSyncEnv *doSyncEnvStart() {
|
||||||
snprintf(pSyncEnv->name, sizeof(pSyncEnv->name), "SyncEnv_%p", pSyncEnv);
|
SSyncEnv *pSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv));
|
||||||
|
assert(pSyncEnv != NULL);
|
||||||
|
memset(pSyncEnv, 0, sizeof(pSyncEnv));
|
||||||
|
|
||||||
|
pSyncEnv->envTickTimerCounter = 0;
|
||||||
|
pSyncEnv->envTickTimerMS = ENV_TICK_TIMER_MS;
|
||||||
|
pSyncEnv->FpEnvTickTimer = syncEnvTick;
|
||||||
|
atomic_store_64(&pSyncEnv->envTickTimerLogicClock, 0);
|
||||||
|
atomic_store_64(&pSyncEnv->envTickTimerLogicClockUser, 0);
|
||||||
|
|
||||||
// start tmr thread
|
// start tmr thread
|
||||||
pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV");
|
pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV");
|
||||||
|
return pSyncEnv;
|
||||||
// pSyncEnv->pEnvTickTimer = taosTmrStart(syncEnvTick, 1000, pSyncEnv, pSyncEnv->pTimerManager);
|
|
||||||
|
|
||||||
sTrace("SyncEnv start ok, name:%s", pSyncEnv->name);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) {
|
static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) {
|
||||||
|
@ -72,8 +86,17 @@ static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static tmr_h doSyncEnvStartTimer(SSyncEnv *pSyncEnv, TAOS_TMR_CALLBACK fp, int mseconds, void *param) {
|
static int32_t doSyncEnvStartTimer(SSyncEnv *pSyncEnv) {
|
||||||
return taosTmrStart(fp, mseconds, pSyncEnv, pSyncEnv->pTimerManager);
|
int32_t ret = 0;
|
||||||
|
pSyncEnv->pEnvTickTimer =
|
||||||
|
taosTmrStart(pSyncEnv->FpEnvTickTimer, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager);
|
||||||
|
atomic_store_64(&pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerLogicClockUser);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer) {}
|
static int32_t doSyncEnvStopTimer(SSyncEnv *pSyncEnv) {
|
||||||
|
atomic_add_fetch_64(&pSyncEnv->envTickTimerLogicClockUser, 1);
|
||||||
|
taosTmrStop(pSyncEnv->pEnvTickTimer);
|
||||||
|
pSyncEnv->pEnvTickTimer = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -14,15 +14,6 @@ void logTest() {
|
||||||
sFatal("--- sync log test: fatal");
|
sFatal("--- sync log test: fatal");
|
||||||
}
|
}
|
||||||
|
|
||||||
void *pTimer = NULL;
|
|
||||||
void *pTimerMgr = NULL;
|
|
||||||
int g = 300;
|
|
||||||
|
|
||||||
static void timerFp(void *param, void *tmrId) {
|
|
||||||
printf("param:%p, tmrId:%p, pTimer:%p, pTimerMgr:%p \n", param, tmrId, pTimer, pTimerMgr);
|
|
||||||
taosTmrReset(timerFp, 1000, param, pTimerMgr, &pTimer);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// taosInitLog((char*)"syncEnvTest.log", 100000, 10);
|
// taosInitLog((char*)"syncEnvTest.log", 100000, 10);
|
||||||
tsAsyncLog = 0;
|
tsAsyncLog = 0;
|
||||||
|
@ -34,13 +25,20 @@ int main() {
|
||||||
ret = syncEnvStart();
|
ret = syncEnvStart();
|
||||||
assert(ret == 0);
|
assert(ret == 0);
|
||||||
|
|
||||||
// timer
|
for (int i = 0; i < 5; ++i) {
|
||||||
pTimerMgr = taosTmrInit(1000, 50, 10000, "SYNC-ENV-TEST");
|
ret = syncEnvStartTimer();
|
||||||
taosTmrStart(timerFp, 1000, &g, pTimerMgr);
|
assert(ret == 0);
|
||||||
|
|
||||||
while (1) {
|
taosMsleep(5000);
|
||||||
taosMsleep(1000);
|
|
||||||
|
ret = syncEnvStopTimer();
|
||||||
|
assert(ret == 0);
|
||||||
|
|
||||||
|
taosMsleep(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = syncEnvStop();
|
||||||
|
assert(ret == 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ SyncAppendEntriesReply *createSyncAppendEntriesReply() {
|
||||||
|
|
||||||
void test1() {
|
void test1() {
|
||||||
SyncTimeout *pMsg = createSyncTimeout();
|
SyncTimeout *pMsg = createSyncTimeout();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncTimeout2RpcMsg(pMsg, &rpcMsg);
|
syncTimeout2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test1", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test1", &rpcMsg);
|
||||||
syncTimeoutDestroy(pMsg);
|
syncTimeoutDestroy(pMsg);
|
||||||
|
@ -108,7 +108,7 @@ void test1() {
|
||||||
|
|
||||||
void test2() {
|
void test2() {
|
||||||
SyncPing *pMsg = createSyncPing();
|
SyncPing *pMsg = createSyncPing();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncPing2RpcMsg(pMsg, &rpcMsg);
|
syncPing2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test2", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test2", &rpcMsg);
|
||||||
syncPingDestroy(pMsg);
|
syncPingDestroy(pMsg);
|
||||||
|
@ -116,7 +116,7 @@ void test2() {
|
||||||
|
|
||||||
void test3() {
|
void test3() {
|
||||||
SyncPingReply *pMsg = createSyncPingReply();
|
SyncPingReply *pMsg = createSyncPingReply();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncPingReply2RpcMsg(pMsg, &rpcMsg);
|
syncPingReply2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test3", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test3", &rpcMsg);
|
||||||
syncPingReplyDestroy(pMsg);
|
syncPingReplyDestroy(pMsg);
|
||||||
|
@ -132,7 +132,7 @@ void test4() {
|
||||||
|
|
||||||
void test5() {
|
void test5() {
|
||||||
SyncRequestVoteReply *pMsg = createSyncRequestVoteReply();
|
SyncRequestVoteReply *pMsg = createSyncRequestVoteReply();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
|
syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test5", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test5", &rpcMsg);
|
||||||
syncRequestVoteReplyDestroy(pMsg);
|
syncRequestVoteReplyDestroy(pMsg);
|
||||||
|
@ -140,7 +140,7 @@ void test5() {
|
||||||
|
|
||||||
void test6() {
|
void test6() {
|
||||||
SyncAppendEntries *pMsg = createSyncAppendEntries();
|
SyncAppendEntries *pMsg = createSyncAppendEntries();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
|
syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test6", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test6", &rpcMsg);
|
||||||
syncAppendEntriesDestroy(pMsg);
|
syncAppendEntriesDestroy(pMsg);
|
||||||
|
@ -148,7 +148,7 @@ void test6() {
|
||||||
|
|
||||||
void test7() {
|
void test7() {
|
||||||
SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply();
|
SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
|
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test7", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test7", &rpcMsg);
|
||||||
syncAppendEntriesReplyDestroy(pMsg);
|
syncAppendEntriesReplyDestroy(pMsg);
|
||||||
|
@ -156,7 +156,7 @@ void test7() {
|
||||||
|
|
||||||
void test8() {
|
void test8() {
|
||||||
SyncClientRequest *pMsg = createSyncClientRequest();
|
SyncClientRequest *pMsg = createSyncClientRequest();
|
||||||
SRpcMsg rpcMsg;
|
SRpcMsg rpcMsg;
|
||||||
syncClientRequest2RpcMsg(pMsg, &rpcMsg);
|
syncClientRequest2RpcMsg(pMsg, &rpcMsg);
|
||||||
syncRpcMsgPrint2((char *)"test8", &rpcMsg);
|
syncRpcMsgPrint2((char *)"test8", &rpcMsg);
|
||||||
syncClientRequestDestroy(pMsg);
|
syncClientRequestDestroy(pMsg);
|
||||||
|
|
Loading…
Reference in New Issue