未验证 提交 67f91c6d 编写于 作者: L Li Minghao 提交者: GitHub

Merge pull request #10233 from taosdata/feature/3.0_mhli

TD-13476 add sync.h
/* /*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com> * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
* *
* This program is free software: you can use, redistribute, and/or modify * 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 * it under the terms of the GNU Affero General Public License, version 3
...@@ -23,7 +23,7 @@ extern "C" { ...@@ -23,7 +23,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include "taosdef.h" #include "taosdef.h"
typedef int32_t SyncNodeId; typedef uint64_t SyncNodeId;
typedef int32_t SyncGroupId; typedef int32_t SyncGroupId;
typedef int64_t SyncIndex; typedef int64_t SyncIndex;
typedef uint64_t SyncTerm; typedef uint64_t SyncTerm;
...@@ -46,109 +46,113 @@ typedef struct { ...@@ -46,109 +46,113 @@ typedef struct {
} SNodeInfo; } SNodeInfo;
typedef struct { typedef struct {
int32_t selfIndex; int32_t replicaNum;
int32_t replica;
SNodeInfo nodeInfo[TSDB_MAX_REPLICA]; SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
} SSyncCluster; } SSyncCfg;
typedef struct { typedef struct {
int32_t selfIndex; int32_t replicaNum;
int32_t replica; SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
SNodeInfo node[TSDB_MAX_REPLICA];
ESyncState role[TSDB_MAX_REPLICA]; ESyncState role[TSDB_MAX_REPLICA];
} SNodesRole; } SNodesRole;
typedef struct SSyncFSM { // abstract definition of snapshot
void* pData; typedef struct SSnapshot {
void* data;
// apply committed log, bufs will be free by sync module SyncIndex lastApplyIndex;
int32_t (*applyLog)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf, void* pData); } SSnapshot;
// cluster commit callback typedef struct SSyncFSM {
int32_t (*onClusterChanged)(struct SSyncFSM* fsm, const SSyncCluster* cluster, void* pData); void* data;
// fsm return snapshot in ppBuf, bufs will be free by sync module // when value in pBuf finish a raft flow, FpCommitCb is called, code indicates the result
// TODO: getSnapshot SHOULD be async? // user can do something according to the code and isWeak. for example, write data into tsdb
int32_t (*getSnapshot)(struct SSyncFSM* fsm, SSyncBuffer** ppBuf, int32_t* objId, bool* isLast); void (*FpCommitCb)(struct SSyncFSM* pFsm, const SSyncBuffer* pBuf, SyncIndex index, bool isWeak, int32_t code);
// fsm apply snapshot with pBuf data // when value in pBuf has been written into local log store, FpPreCommitCb is called, code indicates the result
int32_t (*applySnapshot)(struct SSyncFSM* fsm, SSyncBuffer* pBuf, int32_t objId, bool isLast); // 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);
// call when restore snapshot and log done // when log entry is updated by a new one, FpRollBackCb is called
int32_t (*onRestoreDone)(struct SSyncFSM* fsm); // 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 (*onRollback)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf); // user should implement this function, use "data" to take snapshot into "snapshot"
int32_t (*FpTakeSnapshot)(SSnapshot* snapshot);
void (*onRoleChanged)(struct SSyncFSM* fsm, const SNodesRole* pRole); // user should implement this function, restore "data" from "snapshot"
int32_t (*FpRestoreSnapshot)(const SSnapshot* snapshot);
} SSyncFSM; } SSyncFSM;
// abstract definition of log store in raft
// SWal implements it
typedef struct SSyncLogStore { typedef struct SSyncLogStore {
void* pData; void* data;
// append one log entry
int32_t (*appendEntry)(struct SSyncLogStore* pLogStore, SSyncBuffer* pBuf);
// get one log entry, user need to free pBuf->data
int32_t (*getEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncBuffer* pBuf);
// write log with given index // update log store commit index with "index"
int32_t (*logWrite)(struct SSyncLogStore* logStore, SyncIndex index, SSyncBuffer* pBuf); int32_t (*updateCommitIndex)(struct SSyncLogStore* pLogStore, SyncIndex index);
/** // truncate log with index, entries after the given index (>index) will be deleted
* read log from given index(included) with limit, return the actual num in nBuf, int32_t (*truncate)(struct SSyncLogStore* pLogStore, SyncIndex index);
* pBuf will be free in sync module
**/
int32_t (*logRead)(struct SSyncLogStore* logStore, SyncIndex index, int limit,
SSyncBuffer* pBuf, int* nBuf);
// mark log with given index has been commtted // return commit index of log
int32_t (*logCommit)(struct SSyncLogStore* logStore, SyncIndex index); SyncIndex (*getCommitIndex)(struct SSyncLogStore* pLogStore);
// prune log before given index(not included) // return index of last entry
int32_t (*logPrune)(struct SSyncLogStore* logStore, SyncIndex index); SyncIndex (*getLastIndex)(struct SSyncLogStore* pLogStore);
// rollback log after given index(included) // return term of last entry
int32_t (*logRollback)(struct SSyncLogStore* logStore, SyncIndex index); SyncTerm (*getLastTerm)(struct SSyncLogStore* pLogStore);
// return last index of log
SyncIndex (*logLastIndex)(struct SSyncLogStore* logStore);
} SSyncLogStore; } SSyncLogStore;
typedef struct SStateManager { // raft need to persist two variables in storage: currentTerm, voteFor
void* pData; typedef struct SStateMgr {
void* data;
// save serialized server state data, buffer will be free by Sync int32_t (*getCurrentTerm)(struct SStateMgr* pMgr, SyncTerm* pCurrentTerm);
int32_t (*saveServerState)(struct SStateManager* stateMng, const char* buffer, int n); int32_t (*persistCurrentTerm)(struct SStateMgr* pMgr, SyncTerm pCurrentTerm);
// read serialized server state data, buffer will be free by Sync int32_t (*getVoteFor)(struct SStateMgr* pMgr, SyncNodeId* pVoteFor);
int32_t (*readServerState)(struct SStateManager* stateMng, char** ppBuffer, int* n); int32_t (*persistVoteFor)(struct SStateMgr* pMgr, SyncNodeId voteFor);
// save serialized cluster state data, buffer will be free by Sync int32_t (*getSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg);
void (*saveClusterState)(struct SStateManager* stateMng, const char* buffer, int n); int32_t (*persistSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg);
// read serialized cluster state data, buffer will be free by Sync } SStateMgr;
int32_t (*readClusterState)(struct SStateManager* stateMng, char** ppBuffer, int* n);
} SStateManager;
typedef struct { typedef struct {
SyncGroupId vgId; SyncGroupId vgId;
SyncIndex appliedIndex; SSyncCfg syncCfg;
SSyncCluster syncCfg;
SSyncFSM fsm;
SSyncLogStore logStore; SSyncLogStore logStore;
SStateManager stateManager; SStateMgr stateManager;
SSyncFSM syncFsm;
} SSyncInfo; } SSyncInfo;
struct SSyncNode; // will be defined in syncInt.h, here just for complie
typedef struct SSyncNode SSyncNode; typedef struct SSyncNode {
} SSyncNode;
int32_t syncInit(); int32_t syncInit();
void syncCleanUp(); void syncCleanUp();
SSyncNode* syncStart(const SSyncInfo*); int64_t syncStart(const SSyncInfo*);
void syncReconfig(const SSyncNode*, const SSyncCluster*); void syncStop(int64_t rid);
void syncStop(const SSyncNode*); int32_t syncReconfig(int64_t rid, const SSyncCfg*);
int32_t syncPropose(SSyncNode* syncNode, const SSyncBuffer* pBuf, void* pData, bool isWeak);
int32_t syncAddNode(SSyncNode syncNode, const SNodeInfo *pNode); // int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pBuf, bool isWeak);
int32_t syncForwardToPeer(int64_t rid, const SSyncBuffer* pBuf, bool isWeak);
int32_t syncRemoveNode(SSyncNode syncNode, const SNodeInfo *pNode); ESyncState syncGetMyRole(int64_t rid);
void syncGetNodesRole(int64_t rid, SNodesRole*);
extern int32_t sDebugFlag; extern int32_t sDebugFlag;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册