TDengine/include/libs/sync/sync.h

160 lines
4.7 KiB
C
Raw Normal View History

2021-10-23 14:29:00 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
2021-10-25 09:58:33 +00:00
#ifndef _TD_LIBS_SYNC_H
#define _TD_LIBS_SYNC_H
2021-10-23 14:29:00 +00:00
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "taosdef.h"
2021-10-28 07:50:53 +00:00
typedef int32_t SyncNodeId;
typedef int32_t SyncGroupId;
typedef int64_t SyncIndex;
typedef uint64_t SyncTerm;
2021-10-23 14:29:00 +00:00
typedef enum {
TAOS_SYNC_STATE_FOLLOWER = 0,
TAOS_SYNC_STATE_CANDIDATE = 1,
TAOS_SYNC_STATE_LEADER = 2,
} ESyncState;
2021-10-23 14:29:00 +00:00
typedef struct {
2021-10-25 07:07:41 +00:00
void* data;
2021-10-23 14:29:00 +00:00
size_t len;
} SSyncBuffer;
typedef struct {
SyncNodeId nodeId;
2021-10-28 07:50:53 +00:00
uint16_t nodePort; // node sync Port
char nodeFqdn[TSDB_FQDN_LEN]; // node FQDN
2021-10-23 14:29:00 +00:00
} SNodeInfo;
typedef struct {
2021-10-28 08:57:02 +00:00
int32_t selfIndex;
int32_t replica;
2021-10-28 07:50:53 +00:00
SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
2021-10-23 14:29:00 +00:00
} SSyncCluster;
typedef struct {
2021-10-28 07:50:53 +00:00
int32_t selfIndex;
2021-10-28 08:57:02 +00:00
int32_t replica;
2021-10-28 07:50:53 +00:00
SNodeInfo node[TSDB_MAX_REPLICA];
ESyncState role[TSDB_MAX_REPLICA];
2021-10-23 14:29:00 +00:00
} SNodesRole;
typedef struct SSyncFSM {
void* pData;
// apply committed log, bufs will be free by sync module
2021-10-28 08:57:02 +00:00
int32_t (*applyLog)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf, void* pData);
2021-10-23 14:29:00 +00:00
2021-10-25 07:07:41 +00:00
// cluster commit callback
2021-10-28 08:57:02 +00:00
int32_t (*onClusterChanged)(struct SSyncFSM* fsm, const SSyncCluster* cluster, void* pData);
2021-10-23 14:29:00 +00:00
// fsm return snapshot in ppBuf, bufs will be free by sync module
2021-10-23 14:29:00 +00:00
// TODO: getSnapshot SHOULD be async?
2021-10-28 08:57:02 +00:00
int32_t (*getSnapshot)(struct SSyncFSM* fsm, SSyncBuffer** ppBuf, int32_t* objId, bool* isLast);
2021-10-23 14:29:00 +00:00
// fsm apply snapshot with pBuf data
2021-10-28 08:57:02 +00:00
int32_t (*applySnapshot)(struct SSyncFSM* fsm, SSyncBuffer* pBuf, int32_t objId, bool isLast);
2021-10-23 14:29:00 +00:00
// call when restore snapshot and log done
2021-10-28 08:57:02 +00:00
int32_t (*onRestoreDone)(struct SSyncFSM* fsm);
2021-10-23 14:29:00 +00:00
2021-10-25 07:07:41 +00:00
void (*onRollback)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf);
2021-10-23 14:29:00 +00:00
2021-10-25 07:07:41 +00:00
void (*onRoleChanged)(struct SSyncFSM* fsm, const SNodesRole* pRole);
2021-10-23 14:29:00 +00:00
} SSyncFSM;
typedef struct SSyncLogStore {
void* pData;
// write log with given index
int32_t (*logWrite)(struct SSyncLogStore* logStore, SyncIndex index, SSyncBuffer* pBuf);
/**
* read log from given index(included) with limit, return the actual num in nBuf,
* 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
int32_t (*logCommit)(struct SSyncLogStore* logStore, SyncIndex index);
// prune log before given index(not included)
int32_t (*logPrune)(struct SSyncLogStore* logStore, SyncIndex index);
// rollback log after given index(included)
int32_t (*logRollback)(struct SSyncLogStore* logStore, SyncIndex index);
// return last index of log
SyncIndex (*logLastIndex)(struct SSyncLogStore* logStore);
} SSyncLogStore;
2021-10-23 14:29:00 +00:00
typedef struct SStateManager {
void* pData;
// save serialized server state data, buffer will be free by Sync
int32_t (*saveServerState)(struct SStateManager* stateMng, const char* buffer, int n);
2021-10-23 14:29:00 +00:00
// read serialized server state data, buffer will be free by Sync
int32_t (*readServerState)(struct SStateManager* stateMng, char** ppBuffer, int* n);
2021-10-23 14:29:00 +00:00
// save serialized cluster state data, buffer will be free by Sync
void (*saveClusterState)(struct SStateManager* stateMng, const char* buffer, int n);
2021-10-23 14:29:00 +00:00
// read serialized cluster state data, buffer will be free by Sync
int32_t (*readClusterState)(struct SStateManager* stateMng, char** ppBuffer, int* n);
2021-10-23 14:29:00 +00:00
} SStateManager;
typedef struct {
2021-10-28 07:50:53 +00:00
SyncGroupId vgId;
SyncIndex appliedIndex;
2021-10-28 07:50:53 +00:00
SSyncCluster syncCfg;
SSyncFSM fsm;
SSyncLogStore logStore;
2021-10-23 14:29:00 +00:00
SStateManager stateManager;
} SSyncInfo;
struct SSyncNode;
typedef struct SSyncNode SSyncNode;
2021-10-23 14:29:00 +00:00
int32_t syncInit();
void syncCleanUp();
2021-10-28 07:50:53 +00:00
SSyncNode* syncStart(const SSyncInfo*);
void syncReconfig(const SSyncNode*, const SSyncCluster*);
void syncStop(const SSyncNode*);
2021-10-23 14:29:00 +00:00
int32_t syncPropose(SSyncNode* syncNode, const SSyncBuffer* pBuf, void* pData, bool isWeak);
2021-10-23 14:29:00 +00:00
int32_t syncAddNode(SSyncNode syncNode, const SNodeInfo *pNode);
int32_t syncRemoveNode(SSyncNode syncNode, const SNodeInfo *pNode);
2021-10-29 06:31:21 +00:00
extern int32_t sDebugFlag;
2021-10-23 14:29:00 +00:00
#ifdef __cplusplus
}
#endif
2021-10-28 07:50:53 +00:00
#endif /*_TD_LIBS_SYNC_H*/