TDengine/include/libs/sync/sync.h

323 lines
10 KiB
C
Raw Normal View History

2022-02-09 06:51:23 +00:00
/*
2022-02-12 13:11:21 +00:00
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
2022-02-09 06:51:23 +00:00
*
* 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/>.
*/
#ifndef _TD_LIBS_SYNC_H
#define _TD_LIBS_SYNC_H
#ifdef __cplusplus
extern "C" {
#endif
2022-04-18 13:50:56 +00:00
#include "cJSON.h"
2022-04-22 09:11:56 +00:00
#include "tdef.h"
2022-09-20 11:57:02 +00:00
#include "tlrucache.h"
2022-05-19 11:44:01 +00:00
#include "tmsgcb.h"
2022-02-09 06:51:23 +00:00
#define SYNC_RESP_TTL_MS 30000
#define SYNC_SPEED_UP_HB_TIMER 400
#define SYNC_SPEED_UP_AFTER_MS (1000 * 20)
#define SYNC_SLOW_DOWN_RANGE 100
#define SYNC_MAX_READ_RANGE 2
#define SYNC_MAX_PROGRESS_WAIT_MS 4000
#define SYNC_MAX_START_TIME_RANGE_MS (1000 * 20)
#define SYNC_MAX_RECV_TIME_RANGE_MS 1200
#define SYNC_ADD_QUORUM_COUNT 3
#define SYNC_VNODE_LOG_RETENTION (TSDB_SYNC_LOG_BUFFER_RETENTION + 1)
#define SNAPSHOT_WAIT_MS 1000 * 5
2022-07-12 08:57:19 +00:00
#define SYNC_WAL_LOG_RETENTION_SIZE (8LL * 1024 * 1024 * 1024)
2022-11-11 14:55:21 +00:00
#define SYNC_MAX_RETRY_BACKOFF 5
#define SYNC_LOG_REPL_RETRY_WAIT_MS 100
2022-10-16 04:07:02 +00:00
#define SYNC_APPEND_ENTRIES_TIMEOUT_MS 10000
#define SYNC_HEART_TIMEOUT_MS 1000 * 15
2022-07-12 08:57:19 +00:00
#define SYNC_HEARTBEAT_SLOW_MS 1500
#define SYNC_HEARTBEAT_REPLY_SLOW_MS 1500
#define SYNC_SNAP_RESEND_MS 1000 * 60
2024-09-29 07:01:03 +00:00
#define SYNC_SNAP_TIMEOUT_MS 1000 * 180
2023-03-24 01:05:06 +00:00
#define SYNC_VND_COMMIT_MIN_MS 3000
2022-12-23 12:16:23 +00:00
2022-08-01 11:45:39 +00:00
#define SYNC_MAX_BATCH_SIZE 1
#define SYNC_INDEX_BEGIN 0
#define SYNC_INDEX_INVALID -1
2022-12-23 12:16:23 +00:00
#define SYNC_TERM_INVALID -1
2022-05-20 02:23:48 +00:00
2025-06-30 02:33:34 +00:00
#define SYNC_LEARNER_CATCHUP 10
#define APPLY_QUEUE_ERROR_THRESHOLD 50
2023-04-18 11:03:45 +00:00
2022-07-04 06:55:26 +00:00
typedef enum {
SYNC_STRATEGY_NO_SNAPSHOT = 0,
SYNC_STRATEGY_STANDARD_SNAPSHOT = 1,
SYNC_STRATEGY_WAL_FIRST = 2,
} ESyncStrategy;
2022-02-12 13:11:21 +00:00
typedef uint64_t SyncNodeId;
2022-02-09 06:51:23 +00:00
typedef int32_t SyncGroupId;
typedef int64_t SyncIndex;
2022-10-31 04:59:42 +00:00
typedef int64_t SyncTerm;
2022-02-09 06:51:23 +00:00
2022-05-20 02:23:48 +00:00
typedef struct SSyncNode SSyncNode;
typedef struct SSyncRaftEntry SSyncRaftEntry;
2022-02-09 06:51:23 +00:00
typedef enum {
2022-11-30 04:02:54 +00:00
TAOS_SYNC_STATE_OFFLINE = 0,
2022-03-08 05:43:54 +00:00
TAOS_SYNC_STATE_FOLLOWER = 100,
TAOS_SYNC_STATE_CANDIDATE = 101,
TAOS_SYNC_STATE_LEADER = 102,
2022-03-22 08:17:17 +00:00
TAOS_SYNC_STATE_ERROR = 103,
2023-04-18 11:03:45 +00:00
TAOS_SYNC_STATE_LEARNER = 104,
TAOS_SYNC_STATE_ASSIGNED_LEADER = 105,
2022-03-03 09:28:00 +00:00
} ESyncState;
2022-02-09 06:51:23 +00:00
2023-04-18 11:03:45 +00:00
typedef enum {
TAOS_SYNC_ROLE_VOTER = 0,
TAOS_SYNC_ROLE_LEARNER = 1,
TAOS_SYNC_ROLE_ERROR = 2,
} ESyncRole;
typedef enum {
SYNC_FSM_STATE_COMPLETE = 0,
SYNC_FSM_STATE_INCOMPLETE,
} ESyncFsmState;
2022-02-25 09:55:20 +00:00
typedef struct SNodeInfo {
2023-04-18 11:03:45 +00:00
int64_t clusterId;
int32_t nodeId;
uint16_t nodePort;
char nodeFqdn[TSDB_FQDN_LEN];
ESyncRole nodeRole;
2022-02-09 06:51:23 +00:00
} SNodeInfo;
typedef struct SSyncTLV {
int32_t typ;
int32_t len;
char val[];
} SSyncTLV;
2022-02-25 09:55:20 +00:00
typedef struct SSyncCfg {
2023-04-18 11:03:45 +00:00
int32_t totalReplicaNum;
2022-02-12 13:11:21 +00:00
int32_t replicaNum;
2022-02-25 09:55:20 +00:00
int32_t myIndex;
2023-04-18 11:03:45 +00:00
SNodeInfo nodeInfo[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
SyncIndex lastIndex;
2023-07-18 08:09:38 +00:00
int32_t changeVersion;
2022-02-12 13:11:21 +00:00
} SSyncCfg;
2022-02-09 06:51:23 +00:00
2022-04-18 13:50:56 +00:00
typedef struct SFsmCbMeta {
2022-06-20 09:48:56 +00:00
int32_t code;
2022-04-18 13:50:56 +00:00
SyncIndex index;
2022-06-20 09:48:56 +00:00
SyncTerm term;
uint64_t seqNum;
SyncIndex lastConfigIndex;
2022-04-18 13:50:56 +00:00
ESyncState state;
SyncTerm currentTerm;
2022-06-20 09:48:56 +00:00
bool isWeak;
uint64_t flag;
2022-04-18 13:50:56 +00:00
} SFsmCbMeta;
typedef struct SReConfigCbMeta {
2022-06-20 09:48:56 +00:00
int32_t code;
SyncIndex index;
SyncTerm term;
uint64_t seqNum;
SyncIndex lastConfigIndex;
ESyncState state;
SyncTerm currentTerm;
bool isWeak;
uint64_t flag;
// config info
2022-05-26 06:21:24 +00:00
SSyncCfg oldCfg;
SSyncCfg newCfg;
2022-06-20 09:48:56 +00:00
SyncIndex newCfgIndex;
SyncTerm newCfgTerm;
uint64_t newCfgSeqNum;
} SReConfigCbMeta;
typedef struct SSnapshotParam {
SyncIndex start;
SyncIndex end;
SSyncTLV* data;
} SSnapshotParam;
typedef struct SSnapshot {
int32_t type;
2024-08-20 01:54:26 +00:00
SSyncTLV* data;
ESyncFsmState state;
2024-08-20 01:54:26 +00:00
SyncIndex lastApplyIndex;
SyncTerm lastApplyTerm;
SyncIndex lastConfigIndex;
} SSnapshot;
typedef struct SSnapshotMeta {
SyncIndex lastConfigIndex;
} SSnapshotMeta;
2022-02-12 13:11:21 +00:00
typedef struct SSyncFSM {
void* data;
int32_t (*FpCommitCb)(const struct SSyncFSM* pFsm, SRpcMsg* pMsg, SFsmCbMeta* pMeta);
2023-03-22 08:08:16 +00:00
SyncIndex (*FpAppliedIndexCb)(const struct SSyncFSM* pFsm);
int32_t (*FpPreCommitCb)(const struct SSyncFSM* pFsm, SRpcMsg* pMsg, SFsmCbMeta* pMeta);
void (*FpRollBackCb)(const struct SSyncFSM* pFsm, SRpcMsg* pMsg, SFsmCbMeta* pMeta);
2023-03-22 08:08:16 +00:00
void (*FpRestoreFinishCb)(const struct SSyncFSM* pFsm, const SyncIndex commitIdx);
void (*FpAfterRestoredCb)(const struct SSyncFSM* pFsm, const SyncIndex commitIdx);
void (*FpReConfigCb)(const struct SSyncFSM* pFsm, SRpcMsg* pMsg, SReConfigCbMeta* pMeta);
void (*FpLeaderTransferCb)(const struct SSyncFSM* pFsm, SRpcMsg* pMsg, SFsmCbMeta* pMeta);
bool (*FpApplyQueueEmptyCb)(const struct SSyncFSM* pFsm);
int32_t (*FpApplyQueueItems)(const struct SSyncFSM* pFsm);
2022-05-25 06:43:45 +00:00
2022-11-01 07:15:58 +00:00
void (*FpBecomeLeaderCb)(const struct SSyncFSM* pFsm);
void (*FpBecomeFollowerCb)(const struct SSyncFSM* pFsm);
2023-04-18 11:03:45 +00:00
void (*FpBecomeLearnerCb)(const struct SSyncFSM* pFsm);
void (*FpBecomeAssignedLeaderCb)(const struct SSyncFSM* pFsm);
2022-11-01 07:15:58 +00:00
int32_t (*FpGetSnapshot)(const struct SSyncFSM* pFsm, SSnapshot* pSnapshot, void* pReaderParam, void** ppReader);
int32_t (*FpGetSnapshotInfo)(const struct SSyncFSM* pFsm, SSnapshot* pSnapshot);
2022-11-01 07:15:58 +00:00
int32_t (*FpSnapshotStartRead)(const struct SSyncFSM* pFsm, void* pReaderParam, void** ppReader);
void (*FpSnapshotStopRead)(const struct SSyncFSM* pFsm, void* pReader);
2022-11-01 07:15:58 +00:00
int32_t (*FpSnapshotDoRead)(const struct SSyncFSM* pFsm, void* pReader, void** ppBuf, int32_t* len);
2022-11-01 07:15:58 +00:00
int32_t (*FpSnapshotStartWrite)(const struct SSyncFSM* pFsm, void* pWriterParam, void** ppWriter);
int32_t (*FpSnapshotStopWrite)(const struct SSyncFSM* pFsm, void* pWriter, bool isApply, SSnapshot* pSnapshot);
int32_t (*FpSnapshotDoWrite)(const struct SSyncFSM* pFsm, void* pWriter, void* pBuf, int32_t len);
2022-02-09 06:51:23 +00:00
} SSyncFSM;
2022-02-12 13:11:21 +00:00
// abstract definition of log store in raft
// SWal implements it
2022-02-09 06:51:23 +00:00
typedef struct SSyncLogStore {
2022-09-20 11:57:02 +00:00
SLRUCache* pCache;
int32_t cacheHit;
int32_t cacheMiss;
void* data;
2022-02-12 13:11:21 +00:00
2022-10-18 05:53:03 +00:00
int32_t (*syncLogUpdateCommitIndex)(struct SSyncLogStore* pLogStore, SyncIndex index);
SyncIndex (*syncLogCommitIndex)(struct SSyncLogStore* pLogStore);
2022-03-09 07:07:43 +00:00
2022-06-05 11:47:54 +00:00
SyncIndex (*syncLogBeginIndex)(struct SSyncLogStore* pLogStore);
SyncIndex (*syncLogEndIndex)(struct SSyncLogStore* pLogStore);
2022-10-18 05:53:03 +00:00
2022-06-05 11:47:54 +00:00
int32_t (*syncLogEntryCount)(struct SSyncLogStore* pLogStore);
int32_t (*syncLogRestoreFromSnapshot)(struct SSyncLogStore* pLogStore, SyncIndex index);
2022-10-18 05:53:03 +00:00
bool (*syncLogIsEmpty)(struct SSyncLogStore* pLogStore);
bool (*syncLogExist)(struct SSyncLogStore* pLogStore, SyncIndex index);
2022-06-05 11:47:54 +00:00
2022-06-05 13:48:50 +00:00
SyncIndex (*syncLogWriteIndex)(struct SSyncLogStore* pLogStore);
2022-06-05 11:47:54 +00:00
SyncIndex (*syncLogLastIndex)(struct SSyncLogStore* pLogStore);
SyncIndex (*syncLogIndexRetention)(struct SSyncLogStore* pLogStore, int64_t bytes);
2022-06-05 11:47:54 +00:00
SyncTerm (*syncLogLastTerm)(struct SSyncLogStore* pLogStore);
int32_t (*syncLogAppendEntry)(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry, bool forcSync);
2022-06-05 11:47:54 +00:00
int32_t (*syncLogGetEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry);
int32_t (*syncLogTruncate)(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
2022-02-09 06:51:23 +00:00
} SSyncLogStore;
2022-02-25 09:55:20 +00:00
typedef struct SSyncInfo {
2022-07-04 06:55:26 +00:00
bool isStandBy;
ESyncStrategy snapshotStrategy;
SyncGroupId vgId;
2025-07-12 07:39:29 +00:00
SyncGroupId mountVgId;
2022-07-04 06:55:26 +00:00
int32_t batchSize;
SSyncCfg syncCfg;
char path[TSDB_FILENAME_LEN];
struct SWal* pWal;
2022-07-04 06:55:26 +00:00
SSyncFSM* pFsm;
SMsgCb* msgcb;
2022-11-01 07:40:23 +00:00
int32_t pingMs;
int32_t electMs;
int32_t heartbeatMs;
2022-11-01 07:40:23 +00:00
int32_t (*syncSendMSg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
int32_t (*syncEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
int32_t (*syncEqCtrlMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
2022-02-09 06:51:23 +00:00
} SSyncInfo;
2022-06-11 04:44:58 +00:00
// if state == leader
// if restored, display "leader"
// if !restored && canRead, display "leader*"
// if !restored && !canRead, display "leader**"
2022-11-02 02:24:55 +00:00
typedef struct SSyncState {
ESyncState state;
bool restored;
bool canRead;
2023-07-18 08:09:38 +00:00
int32_t progress;
SyncTerm term;
int64_t roleTimeMs;
2023-08-15 02:10:06 +00:00
int64_t startTimeMs;
2022-11-02 02:24:55 +00:00
} SSyncState;
2025-06-30 02:33:34 +00:00
typedef struct SSyncMetrics {
int64_t wal_write_bytes;
int64_t wal_write_time;
} SSyncMetrics;
2024-08-20 01:54:26 +00:00
int32_t syncInit();
void syncCleanUp();
int64_t syncOpen(SSyncInfo* pSyncInfo, int32_t vnodeVersion);
int32_t syncStart(int64_t rid);
void syncStop(int64_t rid);
void syncPreStop(int64_t rid);
void syncPostStop(int64_t rid);
int32_t syncPropose(int64_t rid, SRpcMsg* pMsg, bool isWeak, int64_t* seq);
int32_t syncCheckMember(int64_t rid);
int32_t syncIsCatchUp(int64_t rid);
2023-04-24 02:23:43 +00:00
ESyncRole syncGetRole(int64_t rid);
int64_t syncGetTerm(int64_t rid);
int32_t syncProcessMsg(int64_t rid, SRpcMsg* pMsg);
int32_t syncReconfig(int64_t rid, SSyncCfg* pCfg);
int32_t syncBeginSnapshot(int64_t rid, int64_t lastApplyIndex);
int32_t syncEndSnapshot(int64_t rid);
int32_t syncLeaderTransfer(int64_t rid);
int32_t syncStepDown(int64_t rid, SyncTerm newTerm);
2025-06-30 02:33:34 +00:00
void syncResetMetrics(int64_t rid, const SSyncMetrics* pOldMetrics);
bool syncIsReadyForRead(int64_t rid);
bool syncSnapshotSending(int64_t rid);
bool syncSnapshotRecving(int64_t rid);
int32_t syncSendTimeoutRsp(int64_t rid, int64_t seq);
int32_t syncForceBecomeFollower(SSyncNode* ths, const SRpcMsg* pRpcMsg);
int32_t syncBecomeAssignedLeader(SSyncNode* ths, SRpcMsg* pRpcMsg);
int32_t syncUpdateArbTerm(int64_t rid, SyncTerm arbTerm);
2022-10-31 06:17:26 +00:00
2022-11-02 02:24:55 +00:00
SSyncState syncGetState(int64_t rid);
int32_t syncResetTimer(int64_t rid, int32_t electInterval, int32_t heartbeatInterval);
2025-06-30 02:33:34 +00:00
SSyncMetrics syncGetMetrics(int64_t rid);
void syncGetCommitIndex(int64_t rid, int64_t* syncCommitIndex);
int32_t syncSetElectBaseline(int64_t rid, int32_t ms);
int32_t syncGetArbToken(int64_t rid, char* outToken);
2024-12-24 12:14:47 +00:00
int32_t syncCheckSynced(int64_t rid);
2022-11-02 02:24:55 +00:00
void syncGetRetryEpSet(int64_t rid, SEpSet* pEpSet);
const char* syncStr(ESyncState state);
2024-08-20 01:54:26 +00:00
int32_t syncNodeGetConfig(int64_t rid, SSyncCfg* cfg);
2023-08-16 02:20:48 +00:00
// util
int32_t syncSnapInfoDataRealloc(SSnapshot* pSnap, int32_t size);
2022-02-09 06:51:23 +00:00
#ifdef __cplusplus
}
#endif
#endif /*_TD_LIBS_SYNC_H*/