raft

package
v0.0.0-...-8f2cee6 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ElectionTimeoutMin = 150
	ElectionTimeoutMax = 300
	HeartbeatTimeout   = 50
)
View Source
const Debug = 0

Debugging

Variables

This section is empty.

Functions

func DPrintf

func DPrintf(format string, a ...interface{}) (n int, err error)

Types

type AppendEntriesArgs

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

type AppendEntriesMessage

type AppendEntriesMessage struct {
	Args  AppendEntriesArgs
	Reply AppendEntriesReply
}

type AppendEntriesReply

type AppendEntriesReply struct {
	Term          int
	Success       bool
	Conflict      bool
	ConflictIndex int
	ConflictTerm  int
}

type ApplyMsg

type ApplyMsg struct {
	Index       int
	Command     interface{}
	UseSnapshot bool   // ignore for lab2; only used in lab3
	Snapshot    []byte // ignore for lab2; only used in lab3
}

as each Raft peer becomes aware that successive log entries are committed, the peer should send an ApplyMsg to the service (or tester) on the same server, via the applyCh passed to Make().

type Candidate

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

func (*Candidate) AppendEntries

func (c *Candidate) AppendEntries(rf *Raft, args AppendEntriesArgs, reply *AppendEntriesReply)

func (*Candidate) HandleRequestVote

func (c *Candidate) HandleRequestVote(rf *Raft, server int, args RequestVoteArgs)

func (*Candidate) Kill

func (c *Candidate) Kill(rf *Raft)

func (*Candidate) RequestVote

func (c *Candidate) RequestVote(rf *Raft, args RequestVoteArgs, reply *RequestVoteReply)

func (*Candidate) Start

func (c *Candidate) Start(rf *Raft, command interface{}) (int, int, bool)

func (*Candidate) Wait

func (c *Candidate) Wait(rf *Raft)

Wait for either the election to be won or the timeout to go off.

type Entry

type Entry struct {
	Term    int
	Command interface{}
}

Entry in a Raft server.

type Follower

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

func (*Follower) AppendEntries

func (f *Follower) AppendEntries(rf *Raft, args AppendEntriesArgs, reply *AppendEntriesReply)

func (*Follower) Kill

func (f *Follower) Kill(rf *Raft)

func (*Follower) RequestVote

func (f *Follower) RequestVote(rf *Raft, args RequestVoteArgs, reply *RequestVoteReply)

func (*Follower) Start

func (f *Follower) Start(rf *Raft, command interface{}) (int, int, bool)

func (*Follower) Wait

func (f *Follower) Wait(rf *Raft)

type Leader

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

func (*Leader) AppendEntries

func (l *Leader) AppendEntries(rf *Raft, args AppendEntriesArgs, reply *AppendEntriesReply)

func (*Leader) HandleAppendEntries

func (l *Leader) HandleAppendEntries(rf *Raft, server int)

func (*Leader) HandleOneAppendEntries

func (l *Leader) HandleOneAppendEntries(rf *Raft, server int, args AppendEntriesArgs)

func (*Leader) Kill

func (l *Leader) Kill(rf *Raft)

func (*Leader) RequestVote

func (l *Leader) RequestVote(rf *Raft, args RequestVoteArgs, reply *RequestVoteReply)

func (*Leader) Start

func (l *Leader) Start(rf *Raft, command interface{}) (int, int, bool)

type Log

type Log struct {
	Entries []Entry
}

Log in a Raft server.

func NewLog

func NewLog() *Log

Creates a new log. Raft logs are 1-indexed, so a sentinel entry acts as the 0-th entry.

func (*Log) Compare

func (l *Log) Compare(lastLogIndex int, lastLogTerm int) int

Returns * 1 if this log > given log * 0 if this log = given log * -1 if this log = given log

func (*Log) Contains

func (l *Log) Contains(index int, term int) bool

Returns true if there is a log entry with the given index and term.

func (*Log) GetLastLogIndex

func (l *Log) GetLastLogIndex() int

Returns the index of the last log.

func (*Log) GetLastLogTerm

func (l *Log) GetLastLogTerm() int

Returns the term of the last log.

type Persister

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

func MakePersister

func MakePersister() *Persister

func (*Persister) Copy

func (ps *Persister) Copy() *Persister

func (*Persister) RaftStateSize

func (ps *Persister) RaftStateSize() int

func (*Persister) ReadRaftState

func (ps *Persister) ReadRaftState() []byte

func (*Persister) ReadSnapshot

func (ps *Persister) ReadSnapshot() []byte

func (*Persister) SaveRaftState

func (ps *Persister) SaveRaftState(data []byte)

func (*Persister) SaveSnapshot

func (ps *Persister) SaveSnapshot(snapshot []byte)

type Raft

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

A Go object implementing a single Raft peer.

func Make

func Make(peers []labrpc.Client, me int,
	persister *Persister, applyCh chan ApplyMsg) *Raft

the service or tester wants to create a Raft server. the ports of all the Raft servers (including this one) are in peers[]. this server's port is peers[me]. all the servers' peers[] arrays have the same order. persister is a place for this server to save its persistent state, and also initially holds the most recent saved state, if any. applyCh is a channel on which the tester or service expects Raft to send ApplyMsg messages. Make() must return quickly, so it should start goroutines for any long-running work.

func (*Raft) AppendEntries

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

func (*Raft) Commit

func (rf *Raft) Commit()

func (*Raft) GetState

func (rf *Raft) GetState() (int, bool)

return currentTerm and whether this server believes it is the leader.

func (*Raft) Kill

func (rf *Raft) Kill()

the tester calls Kill() when a Raft instance won't be needed again. you are not required to do anything in Kill(), but it might be convenient to (for example) turn off debug output from this instance.

func (*Raft) RequestVote

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

example RequestVote RPC handler.

func (*Raft) SetState

func (rf *Raft) SetState(state State)

func (*Raft) Start

func (rf *Raft) Start(command interface{}) (int, int, bool)

the service using Raft (e.g. a k/v server) wants to start agreement on the next command to be appended to Raft's log. if this server isn't the leader, returns false. otherwise start the agreement and return immediately. there is no guarantee that this command will ever be committed to the Raft log, since the leader may fail or lose an election.

the first return value is the index that the command will appear at if it's ever committed. the second return value is the current term. the third return value is true if this server believes it is the leader.

type RequestVoteArgs

type RequestVoteArgs struct {
	Term         int
	CandidateId  int // index of candidate in `peers`
	LastLogIndex int
	LastLogTerm  int
}

type RequestVoteReply

type RequestVoteReply struct {
	Term        int
	VoteGranted bool
}

type State

type State interface {
	Start(*Raft, interface{}) (int, int, bool)
	Kill(*Raft)

	AppendEntries(*Raft, AppendEntriesArgs, *AppendEntriesReply)
	RequestVote(*Raft, RequestVoteArgs, *RequestVoteReply)
}

func NewCandidate

func NewCandidate(rf *Raft) State

func NewFollower

func NewFollower(rf *Raft) State

func NewLeader

func NewLeader(rf *Raft) State

Jump to

Keyboard shortcuts

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