raft

package
v0.0.0-...-dce1da6 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MetaStore

type MetaStore struct {
	// contains filtered or unexported fields
}

func OpenMetaStore

func OpenMetaStore(dir string) *MetaStore

func (*MetaStore) Load

func (m *MetaStore) Load() (*Metadata, error)

func (*MetaStore) Save

func (m *MetaStore) Save(meta *Metadata) error

type Metadata

type Metadata struct {
	CurrentTerm uint64 `json:"currentTerm"`
	VotedFor    string `json:"votedFor"`
	CommitIndex uint64 `json:"commitIndex"`
	LastApplied uint64 `json:"lastApplied"`
}

type Node

type Node struct {
	// contains filtered or unexported fields
}

func NewNode

func NewNode(id, address string, peers []string, storageDir string) *Node

func (*Node) Address

func (n *Node) Address() string

func (*Node) AppendCommand

func (n *Node) AppendCommand(cmd *pb.Command) uint64

func (*Node) AppendEntriesToLog

func (n *Node) AppendEntriesToLog(prevLogIndex uint64, entries []*pb.LogEntry) bool

func (*Node) BecomeFollower

func (n *Node) BecomeFollower(term uint64)

func (*Node) BecomeLeader

func (n *Node) BecomeLeader()

func (*Node) CanGrantVote

func (n *Node) CanGrantVote(candidateID string, candidateTerm, candidateLastLogIndex, candidateLastLogTerm uint64) (voteGranted bool, currTerm uint64)

func (*Node) ClusterSize

func (n *Node) ClusterSize() int

func (*Node) CommitIndex

func (n *Node) CommitIndex() uint64

func (*Node) CurrentTerm

func (n *Node) CurrentTerm() uint64

func (*Node) ElectionTimerLoop

func (n *Node) ElectionTimerLoop()

func (*Node) GetLogEntries

func (n *Node) GetLogEntries(startIndex uint64) []*pb.LogEntry

func (*Node) GetLogEntry

func (n *Node) GetLogEntry(index uint64) *pb.LogEntry

func (*Node) GetMatchIndex

func (n *Node) GetMatchIndex(peerAddr string) uint64

func (*Node) GetNextIndex

func (n *Node) GetNextIndex(peerAddr string) uint64

func (*Node) GetPrevLogInfo

func (n *Node) GetPrevLogInfo(nextIndex uint64) (prevLogIndex, prevLogTerm uint64)

func (*Node) GetValue

func (n *Node) GetValue(key string) (string, bool)

func (*Node) HandleElectionTimeout

func (n *Node) HandleElectionTimeout()

func (*Node) HandleInstallSnapshot

func (n *Node) HandleInstallSnapshot(
	term uint64,
	leaderID string,
	lastIncludedIndex uint64,
	lastIncludedTerm uint64,
	data map[string]string,
) uint64

func (*Node) ID

func (n *Node) ID() string

func (*Node) IsLogUpToDate

func (n *Node) IsLogUpToDate(candidateLastLogIndex, candidateLastLogTerm uint64) bool

func (*Node) LastIncludedIndex

func (n *Node) LastIncludedIndex() uint64

func (*Node) LastIncludedTerm

func (n *Node) LastIncludedTerm() uint64

func (*Node) LastLogIndex

func (n *Node) LastLogIndex() uint64

func (*Node) LastLogTerm

func (n *Node) LastLogTerm() uint64

func (*Node) LeaderAddr

func (n *Node) LeaderAddr() string

func (*Node) LoadSnapshot

func (n *Node) LoadSnapshot() (*Snapshot, error)

func (*Node) LogLength

func (n *Node) LogLength() uint64

func (*Node) MajoritySize

func (n *Node) MajoritySize() int

func (*Node) MatchesPrevLog

func (n *Node) MatchesPrevLog(prevLogIndex, prevLogTerm uint64) bool

func (*Node) Peers

func (n *Node) Peers() []string

func (*Node) ResetElectionTimer

func (n *Node) ResetElectionTimer()

func (*Node) SetCallbacks

func (n *Node) SetCallbacks(onBecomeCandidate func(), onSendHeartbeat func())

func (*Node) SetCommitIndex

func (n *Node) SetCommitIndex(index uint64)

func (*Node) SetLeaderAddr

func (n *Node) SetLeaderAddr(addr string)

func (*Node) SetMatchIndex

func (n *Node) SetMatchIndex(peerAddr string, index uint64)

func (*Node) SetNextIndex

func (n *Node) SetNextIndex(peerAddr string, index uint64)

func (*Node) SetState

func (n *Node) SetState(state NodeState)

func (*Node) SetVotedFor

func (n *Node) SetVotedFor(candidateID string)

func (*Node) StartElectionTimer

func (n *Node) StartElectionTimer()

func (*Node) StartHeartbeatLoop

func (n *Node) StartHeartbeatLoop()

func (*Node) State

func (n *Node) State() NodeState

func (*Node) StopTimers

func (n *Node) StopTimers()

func (*Node) UpdateCommitIndex

func (n *Node) UpdateCommitIndex()

func (*Node) VotedFor

func (n *Node) VotedFor() string

type NodeState

type NodeState int // role of node in raft
const (
	Follower  NodeState = 0
	Candidate NodeState = 1
	Leader    NodeState = 2
)

func (NodeState) String

func (state NodeState) String() string

func (NodeState) ToProto

func (state NodeState) ToProto() pb.NodeState

type Server

type Server struct {
	pb.UnimplementedRaftServiceServer
	// contains filtered or unexported fields
}

func NewServer

func NewServer(node *Node, httpAddr string) *Server

func (*Server) AppendEntries

func (s *Server) AppendEntries(ctx context.Context, req *pb.AppendEntriesRequest) (*pb.AppendEntriesResponse, error)

func (*Server) ConnectToPeers

func (s *Server) ConnectToPeers()

func (*Server) GetLeaderHttpAddr

func (s *Server) GetLeaderHttpAddr() string

func (*Server) InstallSnapshot

func (s *Server) InstallSnapshot(ctx context.Context, req *pb.InstallSnapshotRequest) (*pb.InstallSnapshotResponse, error)

func (*Server) Node

func (s *Server) Node() *Node

func (*Server) Ping

func (s *Server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error)

func (*Server) RequestVote

func (s *Server) RequestVote(ctx context.Context, req *pb.RequestVoteRequest) (*pb.RequestVoteResponse, error)

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop()

type Snapshot

type Snapshot struct {
	LastIncludedIndex uint64            `json:"lastIncludedIndex"`
	LastIncludedTerm  uint64            `json:"lastIncludedTerm"`
	Data              map[string]string `json:"data"`
}

type SnapshotStore

type SnapshotStore struct {
	// contains filtered or unexported fields
}

func OpenSnapshotStore

func OpenSnapshotStore(dir string) *SnapshotStore

func (*SnapshotStore) LoadSnapshot

func (s *SnapshotStore) LoadSnapshot() (*Snapshot, error)

func (*SnapshotStore) SaveSnapshot

func (s *SnapshotStore) SaveSnapshot(snap *Snapshot) error

type WAL

type WAL struct {
	// contains filtered or unexported fields
}

func OpenWAL

func OpenWAL(dir string) (*WAL, error)

func (*WAL) Append

func (w *WAL) Append(entry *pb.LogEntry) error

func (*WAL) Close

func (w *WAL) Close() error

func (*WAL) Compact

func (w *WAL) Compact(keepEntries []*pb.LogEntry) error

func (*WAL) ReadAll

func (w *WAL) ReadAll() ([]*pb.LogEntry, error)

func (*WAL) Sync

func (w *WAL) Sync() error

func (*WAL) Truncate

func (w *WAL) Truncate(index uint64) error

Jump to

Keyboard shortcuts

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