Documentation
¶
Index ¶
- Variables
- type Config
- type DynamicLeaderElection
- type FSM
- type Node
- func (n *Node) AddPeer(nodeID, addr string) error
- func (n *Node) Broadcast(ctx context.Context, state *RaftBlockState) error
- func (n *Node) Config() Config
- func (n *Node) GetState() *RaftBlockState
- func (n *Node) HasQuorum() bool
- func (n *Node) IsLeader() bool
- func (n *Node) NodeID() string
- func (n *Node) RemovePeer(nodeID string) error
- func (n *Node) SetApplyCallback(ch chan<- RaftApplyMsg)
- func (n *Node) Shutdown() error
- func (n *Node) Start(_ context.Context) error
- func (n *Node) Stop() error
- type RaftApplyMsg
- type RaftBlockState
- type Runnable
Constants ¶
This section is empty.
Variables ¶
var ErrLeadershipLost = fmt.Errorf("leader lock lost")
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
NodeID string
RaftAddr string
RaftDir string
Bootstrap bool
Peers []string
SnapCount uint64
SendTimeout time.Duration
HeartbeatTimeout time.Duration
LeaderLeaseTimeout time.Duration
}
Config holds raft node configuration
type DynamicLeaderElection ¶
type DynamicLeaderElection struct {
// contains filtered or unexported fields
}
func NewDynamicLeaderElection ¶
func NewDynamicLeaderElection( logger zerolog.Logger, leaderFactory func() (Runnable, error), followerFactory func() (Runnable, error), node *Node, ) *DynamicLeaderElection
NewDynamicLeaderElection constructor
func (*DynamicLeaderElection) IsRunning ¶
func (d *DynamicLeaderElection) IsRunning() bool
type FSM ¶
type FSM struct {
// contains filtered or unexported fields
}
FSM implements raft.FSM for block state
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a raft consensus node
func (*Node) Broadcast ¶
func (n *Node) Broadcast(ctx context.Context, state *RaftBlockState) error
Broadcast proposes a block state to be replicated via raft
func (*Node) GetState ¶
func (n *Node) GetState() *RaftBlockState
GetState returns the current replicated state
func (*Node) HasQuorum ¶
HasQuorum checks if the leader can still contact a quorum of peers. This should be called before producing a block to ensure consensus is possible. Returns false if the leader cannot verify its leadership with the cluster.
func (*Node) RemovePeer ¶
RemovePeer removes a peer from the raft cluster
func (*Node) SetApplyCallback ¶
func (n *Node) SetApplyCallback(ch chan<- RaftApplyMsg)
SetApplyCallback sets a callback channel to receive notifications when a new block state is replicated. The channel must have sufficient buffer space since updates are published only once without blocking. If the channel is full, state updates will be skipped to prevent blocking the raft cluster.
type RaftApplyMsg ¶
type RaftApplyMsg struct {
Index uint64
State *RaftBlockState
}
RaftApplyMsg is sent when raft applies a log entry
type RaftBlockState ¶
type RaftBlockState = pb.RaftBlockState
RaftBlockState represents a replicated block state
type Runnable ¶
type Runnable interface {
// Run runs the main logic of the component using the provided context and returns an error if it fails.
Run(ctx context.Context) error
// IsSynced checks whether the component is synced with the given RaftBlockState.
// -1 means raft is ahead by 1, 0 equal and a positive number the blocks that the local state is ahead of the raft state.
IsSynced(*RaftBlockState) (int, error)
Recover(ctx context.Context, state *RaftBlockState) error
}
Runnable represents a component that can be started and performs specific operations while running.