raft

package
v0.0.0-...-e532b39 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	NoTimeOut = iota
	ElectionTimedOut
	HeartBeatTimeOut
)

Variables

This section is empty.

Functions

func Start

func Start()

Types

type CandidateState

type CandidateState struct {
	Majority         int
	ElectionDeadline int
	ElectionTimeout  int
	VotesReceived    map[ID]bool
}

func (*CandidateState) CountVote

func (c *CandidateState) CountVote() int

func (*CandidateState) GotVoteFrom

func (s *CandidateState) GotVoteFrom(id ID)

func (*CandidateState) HasElectionTimeout

func (c *CandidateState) HasElectionTimeout(currentTick int) bool

func (*CandidateState) HasMajority

func (s *CandidateState) HasMajority() bool

func (*CandidateState) ResetTime

func (c *CandidateState) ResetTime(currentTick int)

func (*CandidateState) ResetVotes

func (c *CandidateState) ResetVotes()

type ChannelComms

type ChannelComms struct {
	SelfId ID
	// contains filtered or unexported fields
}

func NewChannelComms

func NewChannelComms(selfId ID, conns map[ID]chan rpc.Message) *ChannelComms

func (*ChannelComms) BroadcastRpc

func (comms *ChannelComms) BroadcastRpc(ctx context.Context, msg rpc.Message)

func (*ChannelComms) Close

func (comms *ChannelComms) Close()

func (*ChannelComms) Reply

func (comms *ChannelComms) Reply() <-chan rpc.Message

func (*ChannelComms) Rpc

func (comms *ChannelComms) Rpc(ctx context.Context, id ID, msg rpc.Message)

func (*ChannelComms) Start

func (comms *ChannelComms) Start()

type Cluster

type Cluster struct {
	AllMembers map[ID]bool
	SelfID     ID
}

func NewCluster

func NewCluster(self ID, allIds []ID) *Cluster

func (*Cluster) AddNode

func (c *Cluster) AddNode(id ID)

func (*Cluster) RemoveNode

func (c *Cluster) RemoveNode(id ID)

type CommonState

type CommonState struct {
	CurrentTerm int
	VotedFor    ID

	CommitIndex      int
	LastAppliedIndex int
}

func (*CommonState) NextTerm

func (s *CommonState) NextTerm()

type Comms

type Comms interface {
	BroadcastRpc(ctx context.Context, msg rpc.Message)
	Rpc(ctx context.Context, id ID, msg rpc.Message)
	Reply() <-chan rpc.Message
}

func ConnectToCluster

func ConnectToCluster(cluster *Cluster, virtualLan *LAN) (Comms, error)

type Entry

type Entry struct {
	Index int
	Term  int
	Data  interface{}
}

type FollowerState

type FollowerState struct {
	HeartBeatDeadline int
	HeartBeatTimeout  int
}

func (*FollowerState) HasHeartBeatTimeout

func (s *FollowerState) HasHeartBeatTimeout(currentTick int) bool

func (*FollowerState) ResetHeartBeatTimeout

func (s *FollowerState) ResetHeartBeatTimeout(currentTick int)

type ID

type ID int

type InMemoryLog

type InMemoryLog struct {
	Entries       []Entry
	LastApplied   uint64
	LastCommitted uint64
	// contains filtered or unexported fields
}

func (*InMemoryLog) Append

func (l *InMemoryLog) Append(entry Entry) error

func (*InMemoryLog) Commit

func (l *InMemoryLog) Commit(index int) error

func (*InMemoryLog) GetLastApplied

func (l *InMemoryLog) GetLastApplied() int

func (*InMemoryLog) GetLastCommitted

func (l *InMemoryLog) GetLastCommitted() int

func (*InMemoryLog) GetLastLogTermIndex

func (l *InMemoryLog) GetLastLogTermIndex() (term, index int)

func (*InMemoryLog) Read

func (l *InMemoryLog) Read(index int) (entry *Entry, ok bool)

func (*InMemoryLog) TruncateTo

func (l *InMemoryLog) TruncateTo(index int) error

type LAN

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

func CreateFullyConnected

func CreateFullyConnected(peerIds []ID, bufferSize int) *LAN

func (*LAN) Close

func (lan *LAN) Close()

func (*LAN) GetConnection

func (lan *LAN) GetConnection(n1, n2 ID) (conn chan rpc.Message, ok bool)

func (*LAN) GetMulticastConns

func (lan *LAN) GetMulticastConns(node ID) (conns map[ID]chan rpc.Message, ok bool)

type LeaderState

type LeaderState struct {
	NextIndices       map[ID]int
	MatchIndices      map[ID]int
	NextHeartBeatTime int
	HeartBeatTimeout  int
}

func (*LeaderState) Reset

func (s *LeaderState) Reset()

func (*LeaderState) ResetNextHeartBeatTime

func (s *LeaderState) ResetNextHeartBeatTime(currentTick int)

type RaftDriver

type RaftDriver interface {
	Init(fsm RaftFSM, tickInterval time.Duration)
	Shutdown() <-chan struct{}
	Run(ctx context.Context)
}

func NewTestDriver

func NewTestDriver() RaftDriver

type RaftFSM

type RaftFSM interface {
	Id() ID
	Role() Role
	GetCurrentTerm() int
	CommitIndex() int
	LastAppliedIndex() int
	VotedFor() ID
	Log() RaftLog
	GetComms() Comms
	Tick() int
	ReceiveMsg(msg rpc.Message)
	ReplicateToLog(data interface{}) error
	// contains filtered or unexported methods
}

func NewRaftFSM

func NewRaftFSM(id ID, peers []ID, electionTimeout int, heartBeatTimeout int, comms Comms) RaftFSM

type RaftLog

type RaftLog interface {
	GetLastApplied() int
	GetLastCommitted() int
	GetLastLogTermIndex() (term, index int)
	RaftLogAppender
	RaftLogTruncator
	RaftLogReader
	RaftLogCommitter
}

func NewInMemoryLog

func NewInMemoryLog() RaftLog

type RaftLogAppender

type RaftLogAppender interface {
	Append(entry Entry) error
}

type RaftLogCommitter

type RaftLogCommitter interface {
	Commit(index int) error
}

type RaftLogReader

type RaftLogReader interface {
	Read(index int) (entry *Entry, ok bool)
}

type RaftLogTruncator

type RaftLogTruncator interface {
	TruncateTo(index int) error
}

type RaftNode

type RaftNode struct {
	Id     int
	FSM    RaftFSM
	Comms  Comms
	Driver RaftDriver
}

func NewNode

func NewNode() *RaftNode

type RealRaftFSM

type RealRaftFSM struct {
	CommonState
	LeaderState
	FollowerState
	CandidateState
	// contains filtered or unexported fields
}

func (*RealRaftFSM) BecomeCandidate

func (r *RealRaftFSM) BecomeCandidate()

func (*RealRaftFSM) BecomeFollower

func (r *RealRaftFSM) BecomeFollower()

func (*RealRaftFSM) BecomeLeader

func (r *RealRaftFSM) BecomeLeader()

func (*RealRaftFSM) CommitIndex

func (r *RealRaftFSM) CommitIndex() int

func (*RealRaftFSM) GetComms

func (r *RealRaftFSM) GetComms() Comms

func (*RealRaftFSM) GetCurrentTerm

func (r *RealRaftFSM) GetCurrentTerm() int

func (*RealRaftFSM) Id

func (r *RealRaftFSM) Id() ID

func (*RealRaftFSM) LastAppliedIndex

func (r *RealRaftFSM) LastAppliedIndex() int

func (*RealRaftFSM) Log

func (r *RealRaftFSM) Log() RaftLog

func (*RealRaftFSM) ReceiveAppendEntriesReply

func (r *RealRaftFSM) ReceiveAppendEntriesReply(msg *rpc.AppendEntriesReply)

func (*RealRaftFSM) ReceiveAppendEntriesRpc

func (r *RealRaftFSM) ReceiveAppendEntriesRpc(msg *rpc.AppendEntriesReq)

func (*RealRaftFSM) ReceiveMsg

func (r *RealRaftFSM) ReceiveMsg(msg rpc.Message)

func (*RealRaftFSM) ReceiveVoteRequestRpc

func (r *RealRaftFSM) ReceiveVoteRequestRpc(msg *rpc.RequestVote)

func (*RealRaftFSM) ReceiveVotedFor

func (r *RealRaftFSM) ReceiveVotedFor(msg *rpc.VotedFor)

func (*RealRaftFSM) ReplicateToLog

func (r *RealRaftFSM) ReplicateToLog(data interface{}) error

func (*RealRaftFSM) Role

func (r *RealRaftFSM) Role() Role

func (*RealRaftFSM) Tick

func (r *RealRaftFSM) Tick() int

func (*RealRaftFSM) VotedFor

func (r *RealRaftFSM) VotedFor() ID

type Role

type Role int
const (
	Follower Role = iota
	NoneVoteFollower
	Candidate
	Leader
)

type Status

type Status int

type TCPNetworkComms

type TCPNetworkComms struct {
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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