[TD-10645][raft]<feature>add sync node timer
This commit is contained in:
parent
c319d1cb12
commit
5b7261d63f
|
@ -29,5 +29,6 @@ typedef struct SSyncRaft {
|
||||||
|
|
||||||
int32_t syncRaftStart(SSyncRaft* pRaft, const SSyncInfo* pInfo);
|
int32_t syncRaftStart(SSyncRaft* pRaft, const SSyncInfo* pInfo);
|
||||||
int32_t syncRaftStep(SSyncRaft* pRaft, const RaftMessage* pMsg);
|
int32_t syncRaftStep(SSyncRaft* pRaft, const RaftMessage* pMsg);
|
||||||
|
int32_t syncRaftTick(SSyncRaft* pRaft);
|
||||||
|
|
||||||
#endif /* _TD_LIBS_SYNC_RAFT_H */
|
#endif /* _TD_LIBS_SYNC_RAFT_H */
|
|
@ -73,4 +73,6 @@ static FORCE_INLINE bool syncIsInternalMsg(const RaftMessage* pMsg) {
|
||||||
return pMsg->msgType == RAFT_MSG_INTERNAL_PROP;
|
return pMsg->msgType == RAFT_MSG_INTERNAL_PROP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void syncFreeMessage(const RaftMessage* pMsg);
|
||||||
|
|
||||||
#endif /* _TD_LIBS_SYNC_RAFT_MESSAGE_H */
|
#endif /* _TD_LIBS_SYNC_RAFT_MESSAGE_H */
|
|
@ -30,8 +30,10 @@ typedef struct SSyncWorker {
|
||||||
|
|
||||||
struct SSyncNode {
|
struct SSyncNode {
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
int32_t refCount;
|
||||||
SyncGroupId vgId;
|
SyncGroupId vgId;
|
||||||
SSyncRaft raft;
|
SSyncRaft raft;
|
||||||
|
void* syncTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct SSyncManager {
|
typedef struct SSyncManager {
|
||||||
|
@ -46,6 +48,9 @@ typedef struct SSyncManager {
|
||||||
// vgroup hash table
|
// vgroup hash table
|
||||||
SHashObj* vgroupTable;
|
SHashObj* vgroupTable;
|
||||||
|
|
||||||
|
// timer manager
|
||||||
|
void* syncTimerManager;
|
||||||
|
|
||||||
} SSyncManager;
|
} SSyncManager;
|
||||||
|
|
||||||
extern SSyncManager* gSyncManager;
|
extern SSyncManager* gSyncManager;
|
||||||
|
|
|
@ -67,8 +67,10 @@ int32_t syncRaftStart(SSyncRaft* pRaft, const SSyncInfo* pInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t syncRaftStep(SSyncRaft* pRaft, const RaftMessage* pMsg) {
|
int32_t syncRaftStep(SSyncRaft* pRaft, const RaftMessage* pMsg) {
|
||||||
if (!syncIsInternalMsg(pMsg)) {
|
syncFreeMessage(pMsg);
|
||||||
free(pMsg);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t syncRaftTick(SSyncRaft* pRaft) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -15,3 +15,8 @@
|
||||||
|
|
||||||
#include "raft_message.h"
|
#include "raft_message.h"
|
||||||
|
|
||||||
|
void syncFreeMessage(const RaftMessage* pMsg) {
|
||||||
|
if (!syncIsInternalMsg(pMsg)) {
|
||||||
|
free((RaftMessage*)pMsg);
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,12 +14,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "syncInt.h"
|
#include "syncInt.h"
|
||||||
|
#include "ttimer.h"
|
||||||
|
|
||||||
SSyncManager* gSyncManager = NULL;
|
SSyncManager* gSyncManager = NULL;
|
||||||
|
|
||||||
|
#define SYNC_TICK_TIMER 50
|
||||||
|
|
||||||
static int syncOpenWorkerPool(SSyncManager* syncManager);
|
static int syncOpenWorkerPool(SSyncManager* syncManager);
|
||||||
static int syncCloseWorkerPool(SSyncManager* syncManager);
|
static int syncCloseWorkerPool(SSyncManager* syncManager);
|
||||||
static void *syncWorkerMain(void *argv);
|
static void *syncWorkerMain(void *argv);
|
||||||
|
static void syncNodeTick(void *param, void *tmrId);
|
||||||
|
|
||||||
int32_t syncInit() {
|
int32_t syncInit() {
|
||||||
if (gSyncManager != NULL) {
|
if (gSyncManager != NULL) {
|
||||||
|
@ -33,6 +37,14 @@ int32_t syncInit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_init(&gSyncManager->mutex, NULL);
|
pthread_mutex_init(&gSyncManager->mutex, NULL);
|
||||||
|
|
||||||
|
// init sync timer manager
|
||||||
|
gSyncManager->syncTimerManager = taosTmrInit(1000, 50, 10000, "SYNC");
|
||||||
|
if (gSyncManager->syncTimerManager == NULL) {
|
||||||
|
syncCleanUp();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// init worker pool
|
// init worker pool
|
||||||
if (syncOpenWorkerPool(gSyncManager) != 0) {
|
if (syncOpenWorkerPool(gSyncManager) != 0) {
|
||||||
syncCleanUp();
|
syncCleanUp();
|
||||||
|
@ -56,6 +68,7 @@ void syncCleanUp() {
|
||||||
if (gSyncManager->vgroupTable) {
|
if (gSyncManager->vgroupTable) {
|
||||||
taosHashCleanup(gSyncManager->vgroupTable);
|
taosHashCleanup(gSyncManager->vgroupTable);
|
||||||
}
|
}
|
||||||
|
taosTmrCleanUp(gSyncManager->syncTimerManager);
|
||||||
syncCloseWorkerPool(gSyncManager);
|
syncCloseWorkerPool(gSyncManager);
|
||||||
pthread_mutex_unlock(&gSyncManager->mutex);
|
pthread_mutex_unlock(&gSyncManager->mutex);
|
||||||
pthread_mutex_destroy(&gSyncManager->mutex);
|
pthread_mutex_destroy(&gSyncManager->mutex);
|
||||||
|
@ -80,6 +93,8 @@ SSyncNode* syncStart(const SSyncInfo* pInfo) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pNode->syncTimer = taosTmrStart(syncNodeTick, SYNC_TICK_TIMER, (void*)pInfo->vgId, gSyncManager->syncTimerManager);
|
||||||
|
|
||||||
// start raft
|
// start raft
|
||||||
pNode->raft.pNode = pNode;
|
pNode->raft.pNode = pNode;
|
||||||
if (syncRaftStart(&pNode->raft, pInfo) != 0) {
|
if (syncRaftStart(&pNode->raft, pInfo) != 0) {
|
||||||
|
@ -106,7 +121,8 @@ void syncStop(const SSyncNode* pNode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assert(*ppNode == pNode);
|
assert(*ppNode == pNode);
|
||||||
|
taosTmrStop(pNode->syncTimer);
|
||||||
|
|
||||||
taosHashRemove(gSyncManager->vgroupTable, &pNode->vgId, sizeof(SyncGroupId));
|
taosHashRemove(gSyncManager->vgroupTable, &pNode->vgId, sizeof(SyncGroupId));
|
||||||
pthread_mutex_unlock(&gSyncManager->mutex);
|
pthread_mutex_unlock(&gSyncManager->mutex);
|
||||||
|
|
||||||
|
@ -158,4 +174,19 @@ static void *syncWorkerMain(void *argv) {
|
||||||
setThreadName("syncWorker");
|
setThreadName("syncWorker");
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void syncNodeTick(void *param, void *tmrId) {
|
||||||
|
SyncGroupId vgId = (SyncGroupId)param;
|
||||||
|
SSyncNode **ppNode = taosHashGet(gSyncManager->vgroupTable, &vgId, sizeof(SyncGroupId));
|
||||||
|
if (ppNode == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SSyncNode *pNode = *ppNode;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&pNode->mutex);
|
||||||
|
syncRaftTick(&pNode->raft);
|
||||||
|
pthread_mutex_unlock(&pNode->mutex);
|
||||||
|
|
||||||
|
pNode->syncTimer = taosTmrStart(syncNodeTick, SYNC_TICK_TIMER, (void*)pNode->vgId, gSyncManager->syncTimerManager);
|
||||||
}
|
}
|
Loading…
Reference in New Issue