Documentation ¶
Index ¶
- Variables
- func GenerateRandomTerm(min, max int) int
- func GetMajorityCount(total int) int
- func IsMajority(count, total int) bool
- func PeerCount(peers []string) int
- func SendHeartbeat(peers []string, leaderID string)
- func ValidateConfig(config RaftConfig) error
- func ValidateTerm(nodeTerm, replyTerm int) bool
- type AppendEntriesArgs
- type AppendEntriesReply
- type LogEntry
- type RaftConfig
- type RaftNode
- func (node *RaftNode) AddLogEntry(command string)
- func (node *RaftNode) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
- func (node *RaftNode) AppendEntriesResponse(success bool, followerID string, matchIndex int)
- func (node *RaftNode) ApplyLog(entry LogEntry)
- func (node *RaftNode) BecomeCandidate()
- func (node *RaftNode) BecomeFollower(term int)
- func (node *RaftNode) BecomeLeader()
- func (node *RaftNode) Commit()
- func (node *RaftNode) CommitTransaction(command string, args []interface{}) error
- func (node *RaftNode) HandleAppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply)
- func (node *RaftNode) HandleAppendEntriesResponse(success bool, term int)
- func (node *RaftNode) HandleElectionWin()
- func (node *RaftNode) HandleHeartbeatResponse(success bool, term int)
- func (node *RaftNode) HandleRequestVote(args *RequestVoteArgs, reply *RequestVoteReply)
- func (node *RaftNode) HandleTimeout()
- func (node *RaftNode) HandleVoteResponse(reply *RequestVoteReply)
- func (node *RaftNode) InitializeNode()
- func (node *RaftNode) LoadState() error
- func (node *RaftNode) LogStatus()
- func (node *RaftNode) Metrics() map[string]interface{}
- func (node *RaftNode) ProcessElectionResult(votes int)
- func (rn *RaftNode) ProposeTransaction(transaction map[string]interface{}) error
- func (node *RaftNode) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error
- func (node *RaftNode) RequestVoteResponse(voteGranted bool)
- func (node *RaftNode) SaveState() error
- func (node *RaftNode) SendAppendEntries(target string, args *AppendEntriesArgs, reply *AppendEntriesReply) error
- func (node *RaftNode) SendAppendEntriesWithRetry(target string, args *AppendEntriesArgs, reply *AppendEntriesReply, retries int) error
- func (node *RaftNode) SendRequestVote(target string, args *RequestVoteArgs, reply *RequestVoteReply) error
- func (node *RaftNode) SendRequestVoteWithRetry(target string, args *RequestVoteArgs, reply *RequestVoteReply, retries int) error
- func (node *RaftNode) Start()
- func (node *RaftNode) StartElection()
- func (node *RaftNode) StopNode()
- func (node *RaftNode) SubmitTransaction(command string, args []interface{})
- func (node *RaftNode) UpdateMetrics()
- func (node *RaftNode) UpdatePeerList(peers []string)
- func (node *RaftNode) UpdateTimeouts(electionTimeout, heartbeatTimeout time.Duration)
- type RaftState
- type RequestVoteArgs
- type RequestVoteReply
Constants ¶
This section is empty.
Variables ¶
var DefaultRaftConfig = RaftConfig{ ElectionTimeoutMin: 150 * time.Millisecond, ElectionTimeoutMax: 300 * time.Millisecond, HeartbeatInterval: 100 * time.Millisecond, PeerCount: 5, MaxRetries: 3, }
Default configuration values for Raft protocol
Functions ¶
func GenerateRandomTerm ¶
GenerateRandomTerm generates a random term within a reasonable range for election timeout to avoid candidates timing out simultaneously.
func GetMajorityCount ¶
GetMajorityCount returns the minimum number of votes required for a majority.
func IsMajority ¶
IsMajority checks if the given count is a majority in the cluster.
func SendHeartbeat ¶
SendHeartbeat simulates sending a heartbeat message to all follower peers.
func ValidateConfig ¶
func ValidateConfig(config RaftConfig) error
ValidateConfig checks that the provided RaftConfig is valid.
func ValidateTerm ¶
ValidateTerm checks if a given term is valid (greater than or equal to the node's term).
Types ¶
type AppendEntriesArgs ¶
type AppendEntriesReply ¶
type LogEntry ¶
type LogEntry struct { Args []interface{} // contains filtered or unexported fields }
type RaftConfig ¶
type RaftConfig struct { ElectionTimeoutMin time.Duration // Minimum timeout for elections ElectionTimeoutMax time.Duration // Maximum timeout for elections HeartbeatInterval time.Duration // Interval for heartbeats PeerCount int // Total number of peers in the cluster MaxRetries int // Maximum number of retries for vote requests }
RaftConfig holds the configuration parameters for the Raft consensus system.
func NewConfig ¶
func NewConfig(minTimeout, maxTimeout, heartbeatInterval time.Duration, peerCount, maxRetries int) RaftConfig
NewConfig creates a new configuration for the Raft system with the specified parameters. You can use this function if you want to modify the defaults for a specific Raft node setup.
type RaftNode ¶
type RaftNode struct { ElectionTimeoutMin time.Duration // Minimum timeout for elections ElectionTimeoutMax time.Duration // Maximum timeout for elections HeartbeatInterval time.Duration // Interval for heartbeats PeerCount int // Total number of peers in the cluster MaxRetries int // contains filtered or unexported fields }
func CreateRaftNode ¶
CreateRaftNode is for creating a new Raft node.
func (*RaftNode) AddLogEntry ¶
AddLogEntry is for adding a new log entry to the Raft node.
func (*RaftNode) AppendEntries ¶
func (node *RaftNode) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
func (*RaftNode) AppendEntriesResponse ¶
func (*RaftNode) BecomeCandidate ¶
func (node *RaftNode) BecomeCandidate()
BecomeCandidate transitions the node to a candidate state.
func (*RaftNode) BecomeFollower ¶
BecomeFollower transitions the node to a follower state.
func (*RaftNode) BecomeLeader ¶
func (node *RaftNode) BecomeLeader()
BecomeLeader transitions the node to the leader state.
func (*RaftNode) CommitTransaction ¶
func (*RaftNode) HandleAppendEntries ¶
func (node *RaftNode) HandleAppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply)
HandleAppendEntries handles an AppendEntries RPC.
func (*RaftNode) HandleAppendEntriesResponse ¶
HandleAppendEntriesResponse handles the response to an AppendEntries RPC.
func (*RaftNode) HandleElectionWin ¶
func (node *RaftNode) HandleElectionWin()
HandleElectionWin handles the event when a node wins the election.
func (*RaftNode) HandleHeartbeatResponse ¶
HandleHeartbeatResponse handles the response to a heartbeat.
func (*RaftNode) HandleRequestVote ¶
func (node *RaftNode) HandleRequestVote(args *RequestVoteArgs, reply *RequestVoteReply)
HandleRequestVote handles a RequestVote RPC.
func (*RaftNode) HandleTimeout ¶
func (node *RaftNode) HandleTimeout()
HandleTimeout handles the election timeout event.
func (*RaftNode) HandleVoteResponse ¶
func (node *RaftNode) HandleVoteResponse(reply *RequestVoteReply)
HandleVoteResponse handles the response to a RequestVote RPC.
func (*RaftNode) InitializeNode ¶
func (node *RaftNode) InitializeNode()
InitializeNode initializes the Raft node.
func (*RaftNode) LogStatus ¶
func (node *RaftNode) LogStatus()
LogStatus logs the current status of the Raft node.
func (*RaftNode) ProcessElectionResult ¶
ProcessElectionResult processes the result of an election.
func (*RaftNode) ProposeTransaction ¶
func (*RaftNode) RequestVote ¶
func (node *RaftNode) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error
func (*RaftNode) RequestVoteResponse ¶
func (*RaftNode) SendAppendEntries ¶
func (node *RaftNode) SendAppendEntries(target string, args *AppendEntriesArgs, reply *AppendEntriesReply) error
SendAppendEntries is for sending an AppendEntries RPC to another Raft node.
func (*RaftNode) SendAppendEntriesWithRetry ¶
func (node *RaftNode) SendAppendEntriesWithRetry(target string, args *AppendEntriesArgs, reply *AppendEntriesReply, retries int) error
SendAppendEntriesWithRetry sends an AppendEntries RPC with retry logic.
func (*RaftNode) SendRequestVote ¶
func (node *RaftNode) SendRequestVote(target string, args *RequestVoteArgs, reply *RequestVoteReply) error
SendRequestVote is for sending a RequestVote RPC to another Raft node.
func (*RaftNode) SendRequestVoteWithRetry ¶
func (node *RaftNode) SendRequestVoteWithRetry(target string, args *RequestVoteArgs, reply *RequestVoteReply, retries int) error
SendRequestVoteWithRetry sends a RequestVote RPC with retry logic.
func (*RaftNode) StartElection ¶
func (node *RaftNode) StartElection()
func (*RaftNode) SubmitTransaction ¶
func (*RaftNode) UpdateMetrics ¶
func (node *RaftNode) UpdateMetrics()
UpdateMetrics updates the metrics of the Raft node.
func (*RaftNode) UpdatePeerList ¶
UpdatePeerList updates the list of peers for the Raft node.
func (*RaftNode) UpdateTimeouts ¶
UpdateTimeouts updates the election and heartbeat timeouts for the Raft node.
type RequestVoteArgs ¶
type RequestVoteReply ¶
func SendVoteRequest ¶
func SendVoteRequest(peerID string, currentTerm int) (*RequestVoteReply, error)
SendVoteRequest sends a vote request to another node.