raft

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 13 Imported by: 0

README

Raft

This is an instructional implementation of the Raft consensus algorithm.

It is not intended for production use.

A grpc server is used for inter-node communication. We use a health-server with an observer pattern to monitor the health of the nodes and advertise the leader. By using a serviceConfig, we can use the health server to find the leader and send requests to it. We are hence complying with the Raft specification that clients should only send requests to the leader (linearizable reads and writes).

We provide a client api that can be used to send requests to the leader. See the raft-test.go file for an example of how to use the client. We provide our own grpc resolver to find the leader in suite_test.go.

Usage

Run the tests:

$ make tests

Update protobuf definitions:

$ make proto-raft

Update protobuf raft client definitions:

$ make proto-raft-client

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Observer

type Observer interface {
	// Notify is called when the fsm observes a change in leadership.
	Notify(state bool)
}

Observer is an interface that is notified when the RaftNode observes a change in leadership. It is used to notify the leader health service.

type Options

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

Options holds the configurable options for a RaftNode.

type OptionsFn

type OptionsFn func(opt Options)

OptionsFn is a function that sets an option.

func WithBufferSize

func WithBufferSize(len int) OptionsFn

WithBufferSize sets the size of the commit channel.

func WithElectionTimeout

func WithElectionTimeout(min, max int) OptionsFn

WithElectionTimeout sets the election timeout for the RaftNode.

func WithHeartbeatTimeout

func WithHeartbeatTimeout(timeout int) OptionsFn

WithHeartbeatTimeout sets the heartbeat timeout for the RaftNode.

func WithTimeout

func WithTimeout(timeout int) OptionsFn

WithTimeout sets the timeout for the RaftNode.

type RaftNode

type RaftNode struct {

	// RPCServer is the server used to communicate with other peers.
	RPCServer server.Server

	// commitChan is a channel that receives committed entries from the RaftNode to be applied to the state machine.
	// It is buffered to allow the RaftNode to continue committing entries while the state machine is busy.
	CommitChan chan log.LogEntry

	Options
	// contains filtered or unexported fields
}

RaftNode is a member of the Raft cluster

func New

func New(peers map[uint]string, id int32, port uint16, storage storage.Store, logger *slog.Logger, opts ...OptionsFn) (*RaftNode, error)

New creates a new RaftNode.

func (*RaftNode) CopyPeers

func (r *RaftNode) CopyPeers() map[uint]string

func (*RaftNode) GetCurrentTerm

func (r *RaftNode) GetCurrentTerm() uint64

func (*RaftNode) GetID

func (r *RaftNode) GetID() int32

func (*RaftNode) GetLastApplied

func (r *RaftNode) GetLastApplied() uint64

func (*RaftNode) GetLeaderID

func (r *RaftNode) GetLeaderID() int32

func (*RaftNode) GetLog

func (r *RaftNode) GetLog() []log.LogEntry

func (*RaftNode) GetLogByIndex

func (r *RaftNode) GetLogByIndex(index uint64) log.LogEntry

func (*RaftNode) GetPeers

func (r *RaftNode) GetPeers() map[uint]string

func (*RaftNode) GetState

func (r *RaftNode) GetState() (uint64, bool)

GetState returns the currentTerm and whether this server believes it is the leader.

func (*RaftNode) GetVotedFor

func (r *RaftNode) GetVotedFor() int32

func (*RaftNode) IsLeader

func (r *RaftNode) IsLeader() bool

func (*RaftNode) IsStopped

func (r *RaftNode) IsStopped() bool

IsStopped returns true if the RaftNode has been killed.

func (*RaftNode) Run

func (r *RaftNode) Run(ctx context.Context, secure bool) error

func (*RaftNode) SetCurrentTerm

func (r *RaftNode) SetCurrentTerm(term uint64)

func (*RaftNode) SetLastApplied

func (r *RaftNode) SetLastApplied(index uint64)

func (*RaftNode) SetLog

func (r *RaftNode) SetLog(log []log.LogEntry)

func (*RaftNode) SetVotedFor

func (r *RaftNode) SetVotedFor(id int32)

func (*RaftNode) Stop

func (r *RaftNode) Stop() error

Stop tells the RaftNode to shut itself down.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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