rafty

package module
v0.0.0-...-ef7c0cb Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 34 Imported by: 0

README

rafty

Rafty is yet another golang library that manage to replicate log state machine. Details about Raft protocol can be found here

Reading this documentation is recommanded before going any further.

Check out these websites will also be useful:

Why another library?

There are many libraries out there implementing the search of an understandable consensus algorithm. Unfortunately, I mostly found them difficult to understand as there is not so much clear documentation about how to use them. As examples, we have production ready repositories like:

So let's try to redo the wheel with more explanations.

Supported features

Here is a list of the supported features of rafty:

  • PreVote election
  • Leader election
  • [] Logs
    • Log replication
    • Submit write commands
    • [] Submit read commands
    • [] Log compaction
  • [] Membership changes
    • [] Add member
    • [] remove member
    • [] promote member

References

Documentation

Index

Constants

View Source
const (
	// GRPCAddress defines the default address to run the grpc server
	GRPCAddress string = "127.0.0.1"

	// GRPCPort define the default port to run the grpc server
	GRPCPort uint16 = 50051
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Peer

type Peer struct {
	// Address is the address of a peer node, must be just the ip or ip:port
	Address string
	// contains filtered or unexported fields
}

type Rafty

type Rafty struct {

	// LeaderLastContactDate is the last date we heard from the leader
	LeaderLastContactDate *time.Time

	// Logger expose zerolog so it can be override
	Logger *zerolog.Logger

	Status

	// PreCandidatePeers hold the list of the peers
	// that will be use to elect a new leader
	// if no leader has been detected
	PreCandidatePeers []Peer

	// Peers hold the list of the peers
	Peers []Peer

	// TimeMultiplier is a scaling factor that will be used during election timeout
	// by electionTimeoutMin/electionTimeoutMax/leaderHeartBeatTimeout in order to avoid cluster instability
	// The default value is 1 and the maximum is 10
	TimeMultiplier uint

	// MinimumClusterSize is the size minimum to have before starting prevote or election campain
	// default is 3
	// all members of the cluster will be contacted before any other tasks
	MinimumClusterSize uint64

	// MaxAppendEntries will hold how much append entries the leader will send to the follower at once
	MaxAppendEntries uint64

	// DataDir is the default data directory that will be used to store all data on the disk
	// Defaults to os.TempDir()/rafty ex: /tmp/rafty
	DataDir string

	// PersistDataOnDisk is a boolean that allow us to persist data on disk
	PersistDataOnDisk bool
	// contains filtered or unexported fields
}

Rafty is a struct representing the raft requirements

func NewRafty

func NewRafty() *Rafty

func NewServer

func NewServer(address net.TCPAddr) *Rafty

NewServer instantiate default configuration of the gRPC server and rafty to later start or stop it

func (*Rafty) SendGetLeaderRequest

func (r *Rafty) SendGetLeaderRequest()

SendGetLeaderRequest allow the current node ask to other nodes who is the actual leader it also permit to get id of other nodes

func (*Rafty) Start

func (r *Rafty) Start() error

Start permits to start the gRPC server with the provided configuration ready parameter is the channel that will be used by server to accept client requests

func (*Rafty) Stop

func (r *Rafty) Stop()

Stop permits to stop the gRPC server and Rafty with the provided configuration

func (*Rafty) SubmitCommand

func (r *Rafty) SubmitCommand(command command) ([]byte, error)

type State

type State uint32

State represent the current status of the raft server. The state can only be Leader, Candidate, Follower, ReadOnly, Down

const (
	// Down state is a node that has been unreachable for a long period of time
	Down State = iota

	// ReadOnly state is a node that does not pariticipate into the voting campain
	// It's a passive node that issue no requests on his own but simply respond from the leader
	// This node can never become a follower
	ReadOnly

	// Follower state is a node that participate into the voting campain
	// It's a passive node that issue no requests on his own but simply respond from the leader
	// This node can become a Precandidate if all requirements are available
	Follower

	// Candidate state is a node that participate into the voting campain.
	// It can become a Leader
	Candidate

	// Leader state is a node that was previously a Candidate
	// It received the majority of the votes including itself and get elected as the Leader.
	// It will then handle all client requests
	// Writes requests can only be done on the leader
	Leader
)

func (State) String

func (s State) String() string

String return a human readable state of the raft server

type Status

type Status struct {
	// ID of the current raft server
	ID string

	// Address is the current address of the raft server
	Address net.TCPAddr

	// State of the current raft server
	// Can only be Leader, Candidate, Follower, ReadOnly, Down
	State

	// CurrentTerm is latest term seen during the voting campain
	CurrentTerm uint64

	// CurrentCommitIndex is the index of the highest log entry know to be commited
	CurrentCommitIndex uint64

	// LastApplied is the index of the highest log entry applied to the current raft server
	LastApplied uint64
}

Status of the raft server

Jump to

Keyboard shortcuts

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