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) LeaderID() string
- func (n *Node) NodeID() string
- func (n *Node) RemovePeer(nodeID string) error
- func (n *Node) ResignLeader(ctx context.Context) 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
ShutdownTimeout time.Duration
HeartbeatTimeout time.Duration
LeaderLeaseTimeout time.Duration
ElectionTimeout time.Duration
SnapshotThreshold uint64
TrailingLogs uint64
}
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) LeaderID ¶ added in v1.1.1
LeaderID returns the server ID of the current cluster leader. Returns an empty string if the receiver is nil, raft is uninitialized, or no leader has been elected yet. The value may be momentarily stale between raft leadership changes; callers that need a strong guarantee should cross-check with HasQuorum.
func (*Node) RemovePeer ¶
RemovePeer removes a peer from the raft cluster
func (*Node) ResignLeader ¶ added in v1.1.1
ResignLeader synchronously transfers leadership to the most up-to-date follower. It is a no-op when the node is nil or not currently the leader. Call this before cancelling the node context on graceful shutdown to minimise the window where a dying leader could still serve blocks. The transfer is abandoned and ctx.Err() is returned if ctx expires first.
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
Term uint64 // raft term in which this entry was committed
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.