raft

package
v0.0.0-...-28e4c44 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// STATE_FOLLOWER .. Follwer state of raft server
	STATE_FOLLOWER = 0
	// STATE_CANDIDATE .. Follwer state of raft server
	STATE_CANDIDATE = 1
	// STATE_LEADER .. Follwer state of raft server
	STATE_LEADER = 2

	MAX_TIMEOUT = 500
	MIN_TIMEOUT = 200
)
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 AppendEntryArgs

type AppendEntryArgs struct {
	Term              int
	LeaderID          int
	PrevLogIndex      int
	PrevLogTerm       int
	Entries           []LogEntry
	LeaderCommitIndex int
}

AppendEntry is ... Arguments: term leader’s term leaderId so follower can redirect clients prevLogIndex index of log entry immediately preceding new ones prevLogTerm term of prevLogIndex entry entries[] log entries to store (empty for heartbeat; may send more than one for efficiency) leaderCommit leader’s commitIndex Results: term currentTerm, for leader to update itself success true if follower contained entry matching prevLogIndex and prevLogTerm

type AppendEntryReply

type AppendEntryReply struct {
	Term         int
	Success      bool
	NextTryIndex int
}

AppendEntryReply is ...

type ApplyMsg

type ApplyMsg struct {
	CommandValid bool
	Command      interface{}
	CommandIndex int
}

ApplyMsg ... 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(). set CommandValid to true to indicate that the ApplyMsg contains a newly committed log entry.

in Lab 3 you'll want to send other kinds of messages (e.g., snapshots) on the applyCh; at that point you can add fields to ApplyMsg, but set CommandValid to false for these other uses.

type LogEntry

type LogEntry struct {
	Index   int
	Term    int
	Command interface{}
}

LogEntry ... A Go object implementing a single Raft peer.

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(state []byte)

func (*Persister) SaveStateAndSnapshot

func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot []byte)

Save both Raft state and K/V snapshot as a single atomic action, to help avoid them getting out of sync.

func (*Persister) SnapshotSize

func (ps *Persister) SnapshotSize() int

type Raft

type Raft struct {

	// These are the states added from the Fig2 of raft paper
	CurrentTerm int
	VotedFor    int

	LastApplied  int
	NextIndex    []int
	MatchIndex   []int
	CurrentState int
	Mtx          sync.Mutex
	VoteCount    map[int]int
	Log          []LogEntry

	GrantVoteChannel chan bool
	WinElectChannel  chan bool
	HeartbeatChannel chan bool
	// contains filtered or unexported fields
}

Raft ...

func Make

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

Make Function is ...

func (*Raft) AppendEntries

func (rf *Raft) AppendEntries(args *AppendEntryArgs, reply *AppendEntryReply)

AppendEntries ...

func (*Raft) GetState

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

GetState ... 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. for your convenience, we supply code to set rf.dead (without needing a lock), and a killed() method to test rf.dead in long-running loops. you can also add your own code to Kill(). you're not required to do anything about this, but it may be convenient (for example) to suppress debug output from a Kill()ed instance.

func (*Raft) RequestVote

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

RequestVote ... example RequestVote RPC handler.

func (*Raft) Run

func (rf *Raft) Run()

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. even if the Raft instance has been killed, this function should return gracefully.

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 {

	// Arguments:
	// term candidate’s term
	// candidateId candidate requesting vote
	// lastLogIndex index of candidate’s last log entry (§5.4)
	// lastLogTerm term of candidate’s last log entry (§5.4)
	Term         int
	CandidateID  int
	LastLogIndex int
	LastLogTerm  int
}

RequestVoteArgs ... Fill in the RequestVoteArgs and RequestVoteReply structs. Modify Make() to create a background goroutine that will kick off leader election periodically by sending out RequestVote RPCs when it hasn't heard from another peer for a while. This way a peer will learn who is the leader, if there is already a leader, or become the leader itself. Implement the RequestVote() RPC handler so that servers will vote for one another.

type RequestVoteReply

type RequestVoteReply struct {

	// Results:
	// term currentTerm, for candidate to update itself
	// voteGranted true means candidate received vote
	Term        int
	VoteGranted bool
}

RequestVoteReply ... example RequestVote RPC reply structure. field names must start with capital letters!

Jump to

Keyboard shortcuts

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