raft

package
v0.0.0-...-2ce89f0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 4 Imported by: 0

README

This is an implementation of the Raft consensus algorithm

Implementation structure

interface.go

  • Customizable parts of the Raft consensus implementation. Most of the configurable parts of the algorithm are described here

rpc.go

  • Raw RPC arguments and replies DTOs. Placed in a separate file only to improve readability of the main implementation

raft.go

  • Distilled implementation of the Raft consensus. Depends only on the types in interface.go and rpc.go

raft_test.go

  • Unit-tests for the consensus algorithm

Notes

  • The persistent storage is not implemented to keep the implementation simple

Package 'test' contains testing utils

Documentation

Index

Constants

View Source
const (
	// State of Raft instance
	Leader    string = "Leader"
	Follower  string = "Follower"
	Candidate string = "Candidate"

	// Special value for no vote
	Nobody string = ""

	// Special value for uninitialized indices
	Initial int = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AppendEntriesArgs

type AppendEntriesArgs struct {
	Term         int
	LeaderId     string
	PrevLogIndex int
	PrevLogTerm  int
	Entries      []Entry
	LeaderCommit int
}

AppendEntries RPC args

type AppendEntriesReply

type AppendEntriesReply struct {
	Term    int
	Success bool
}

AppendEntries RPC reply

type Entry

type Entry struct {
	Command any
	Term    int
}

AppendEntries RPC Log entry

type Raft

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

Local Raft instance state

func NewRaft

func NewRaft(node RaftNode, settings RaftSettings) *Raft

Construct new Raft object

func (*Raft) AppendEntries

func (raft *Raft) AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) error

AppendEntries RPC handler

func (*Raft) Apply

func (raft *Raft) Apply(command any) error

Apply command to the local instance

Error is returned either if the instance is not Leader at the time or in case of any other error

func (*Raft) Down

func (raft *Raft) Down() error

Shutdown local Raft instance

Idempotent. Returns nil if already stopped. Successful call indicates that the instance is gracefully shut down and can be started again

func (*Raft) GetInfo

func (raft *Raft) GetInfo() RaftInfo

func (*Raft) RequestVote

func (raft *Raft) RequestVote(args RequestVoteArgs, reply *RequestVoteReply) error

RequestVote RPC handler

func (*Raft) Up

func (raft *Raft) Up() error

Start up local Raft instance. Non-blocking

Idempotent. Returns nil if already started. Each call to this function must be followed by a corresponding shutdown

type RaftInfo

type RaftInfo struct {
	State       string
	Term        int
	Log         []Entry
	CommitIndex int
}

Get actual state and current term of the instance

type RaftNode

type RaftNode interface {
	Id() string
	Peers() []RaftPeer
}

Network topology info provider for local Raft instance

Implementation is expected to be thread-safe

type RaftPeer

type RaftPeer interface {
	Id() string
	RequestVote(args RequestVoteArgs, reply *RequestVoteReply) error
	AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) error
}

Remote Raft instance

Implementation is expected to be thread-safe

type RaftSettings

type RaftSettings interface {
	HeartbeatTimeout() time.Duration
	ElectionTimeout() time.Duration
	Majority(peers int) int
}

Static-ish settings and configuration for local Raft instance

Implementation is expected to be thread-safe

type RequestVoteArgs

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

RequestVote RPC args

type RequestVoteReply

type RequestVoteReply struct {
	Term        int
	VoteGranted bool
}

RequestVote RPC reply

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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