consensus

package module
v0.0.0-...-40baf90 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func GenerateRandomTerm(min, max int) int

GenerateRandomTerm generates a random term within a reasonable range for election timeout to avoid candidates timing out simultaneously.

func GetMajorityCount

func GetMajorityCount(total int) int

GetMajorityCount returns the minimum number of votes required for a majority.

func IsMajority

func IsMajority(count, total int) bool

IsMajority checks if the given count is a majority in the cluster.

func PeerCount

func PeerCount(peers []string) int

PeerCount returns the number of peers in the cluster.

func SendHeartbeat

func SendHeartbeat(peers []string, leaderID string)

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

func ValidateTerm(nodeTerm, replyTerm int) bool

ValidateTerm checks if a given term is valid (greater than or equal to the node's term).

Types

type AppendEntriesArgs

type AppendEntriesArgs struct {
	Term         int
	LeaderID     string
	PrevLogIndex int
	PrevLogTerm  int
	Entries      []LogEntry
	LeaderCommit int
}

type AppendEntriesReply

type AppendEntriesReply struct {
	Term    int
	Success bool
}

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

func CreateRaftNode(id string, timeout time.Duration, heartbeatTimeout time.Duration) *RaftNode

CreateRaftNode is for creating a new Raft node.

func (*RaftNode) AddLogEntry

func (node *RaftNode) AddLogEntry(command string)

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 (node *RaftNode) AppendEntriesResponse(success bool, followerID string, matchIndex int)

func (*RaftNode) ApplyLog

func (node *RaftNode) ApplyLog(entry LogEntry)

func (*RaftNode) BecomeCandidate

func (node *RaftNode) BecomeCandidate()

BecomeCandidate transitions the node to a candidate state.

func (*RaftNode) BecomeFollower

func (node *RaftNode) BecomeFollower(term int)

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) Commit

func (node *RaftNode) Commit()

func (*RaftNode) CommitTransaction

func (node *RaftNode) CommitTransaction(command string, args []interface{}) error

func (*RaftNode) HandleAppendEntries

func (node *RaftNode) HandleAppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply)

HandleAppendEntries handles an AppendEntries RPC.

func (*RaftNode) HandleAppendEntriesResponse

func (node *RaftNode) HandleAppendEntriesResponse(success bool, term int)

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

func (node *RaftNode) HandleHeartbeatResponse(success bool, term int)

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) LoadState

func (node *RaftNode) LoadState() error

LoadState loads the state of the Raft node from disk.

func (*RaftNode) LogStatus

func (node *RaftNode) LogStatus()

LogStatus logs the current status of the Raft node.

func (*RaftNode) Metrics

func (node *RaftNode) Metrics() map[string]interface{}

func (*RaftNode) ProcessElectionResult

func (node *RaftNode) ProcessElectionResult(votes int)

ProcessElectionResult processes the result of an election.

func (*RaftNode) ProposeTransaction

func (rn *RaftNode) ProposeTransaction(transaction map[string]interface{}) error

func (*RaftNode) RequestVote

func (node *RaftNode) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error

func (*RaftNode) RequestVoteResponse

func (node *RaftNode) RequestVoteResponse(voteGranted bool)

func (*RaftNode) SaveState

func (node *RaftNode) SaveState() error

SaveState saves the current state of the Raft node to disk.

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) Start

func (node *RaftNode) Start()

func (*RaftNode) StartElection

func (node *RaftNode) StartElection()

func (*RaftNode) StopNode

func (node *RaftNode) StopNode()

StopNode stops the Raft node.

func (*RaftNode) SubmitTransaction

func (node *RaftNode) SubmitTransaction(command string, args []interface{})

func (*RaftNode) UpdateMetrics

func (node *RaftNode) UpdateMetrics()

UpdateMetrics updates the metrics of the Raft node.

func (*RaftNode) UpdatePeerList

func (node *RaftNode) UpdatePeerList(peers []string)

UpdatePeerList updates the list of peers for the Raft node.

func (*RaftNode) UpdateTimeouts

func (node *RaftNode) UpdateTimeouts(electionTimeout, heartbeatTimeout time.Duration)

UpdateTimeouts updates the election and heartbeat timeouts for the Raft node.

type RaftState

type RaftState struct {
	Term        int
	VotedFor    string
	Log         []LogEntry
	CommitIndex int
	LastApplied int
}

RaftState represents the persistent state of a Raft node.

type RequestVoteArgs

type RequestVoteArgs struct {
	Term         int
	CandidateID  string
	LastLogIndex int
	LastLogTerm  int
}

type RequestVoteReply

type RequestVoteReply struct {
	Term        int
	VoteGranted bool
}

func SendVoteRequest

func SendVoteRequest(peerID string, currentTerm int) (*RequestVoteReply, error)

SendVoteRequest sends a vote request to another node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL