Part A
PartA的任务主要是实现领导人选举和心跳机制,因为不要求实现日志复制和校验功能,所以实现起来比较简单。这里必须要小心的是计时器的实现,课程推荐不要用go里面的timer,我就自己实现了一个,计时器的性能其实对后面的实验很关键,一开始的计时器实现的比较糟糕,后面实现就一直出错.... 后来想到的办法是利用go的channel配合循环来实现的,每轮循环先抢锁来检验server自己的状态,来判断其行为。还有一个就是投票计数的功能,我用的是一个局部变量votes来计数,之前尝试过用go的条件变量来实现该计数方法,不过后面那种方法的实现看起来比较复杂,就采用了原子计数法来实现。完整代码如下:
package raft
//
// this is an outline of the API that raft must expose to
// the service (or tester). see comments below for
// each of these functions for more details.
//
// rf = Make(...)
// create a new Raft server.
// rf.Start(command interface{}) (index, term, isleader)
// start agreement on a new log entry
// rf.GetState() (term, isLeader)
// ask a Raft for its current term, and whether it thinks it is leader
// ApplyMsg
// each time a new entry is committed to the log, each Raft peer
// should send an ApplyMsg to the service (or tester)
// in the same server.
//
import (
"math/rand"
"sync"
"time"
)
import "sync/atomic"
import "../labrpc"
// import "bytes"
// import "../labgob"
const (
Follower = iota
Candidate
Leader
ResetTimer
FlushState
)
//The tester requires that the leader send heartbeat RPCs no more than ten times per second.
const HeartBeatTimeout = time.Duration(100) * time.Millisecond
//
// 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 ApplyMsg struct {
CommandValid bool
Command interface{}
CommandIndex int
}
type Entry struct {
Term int
Command interface{}
}
//
// A Go object implementing a single Raft peer.
//
type Raft struct {
mu sync.Mutex // Lock to protect shared access to this peer's state
peers []*labrpc.ClientEnd // RPC end points of all peers
persister *Persister // Object to hold this peer's persisted state
me int // this peer's index into peers[]
dead int32 // set by Kill()
// Your data here (2A, 2B, 2C).
// Look at the paper's Figure 2 for a description of what
// state a Raft server must maintain.
majority int32
state int
flushCh chan int
// Persistent state on all servers
votedFor int
currentTerm int
log []Entry
// Volatile state on all servers
lastApplied int
commitIndex int
// Volatile state on leader
nextIndex []int
matchIndex []int
}
// return currentTerm and whether this server
// believes it is the leader.
func (rf *Raft) GetState() (int, bool) {
var term int
var isleader bool
// Your code here (2A).
rf.mu.Lock()
defer rf.mu.Unlock()
term = rf.currentTerm
isleader = rf.state == Leader
return term, isleader
}
//
// save Raft's persistent state to stable storage,
// where it can later be retrieved after a crash and restart.
// see paper's Figure 2 for a description of what should be persistent.
//
func (rf *Raft) persist() {
// Your code here (2C).
// Example:
// w := new(bytes.Buffer)
// e := labgob.NewEncoder(w)
// e.Encode(rf.xxx)
// e.Encode(rf.yyy)
// data := w.Bytes()
// rf.persister.SaveRaftState(data)
}
//
// restore previously persisted state.
//
func (rf *Raft) readPersist(data []byte) {
if data == nil || len(data) < 1 { // bootstrap without any state?
return
}
// Your code here (2C).
// Example:
// r := bytes.NewBuffer(data)
// d := labgob.NewDecoder(r)
// var xxx
// var yyy
// if d.Decode(&xxx) != nil ||
// d.Decode(&yyy) != nil {
// error...
// } else {
// rf.xxx = xxx
// rf.yyy = yyy
// }
}
type RequestVoteArgs struct {
// Your data here (2A, 2B).
Term int
CandidateId int
//LastLogIndex int
//LastLogTerm int
}
type RequestVoteReply struct {
// Your data here (2A).
Term int
VoteGranted bool
}
func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
// Your code here (2A, 2B).
rf.mu.Lock()
defer rf.mu.Unlock()
reply.Term = rf.currentTerm
if rf.currentTerm > args.Term {
reply.VoteGranted = false
//DPrintf("%d refuse RV from %d",rf.me,args.CandidateId)
return
}
if rf.currentTerm < args.Term {
rf.beFollower(args.Term)
}
// If votedFor is null or candidateId, and candidate’s log is at
// least as up-to-date as receiver’s log, grant vote
if rf.votedFor == -1 || rf.votedFor == args.CandidateId {
rf.votedFor = args.CandidateId
reply.VoteGranted = true
rf.state = Follower
//vote granted reset timer
rf.flush(ResetTimer)
}
}
//
// example code to send a RequestVote RPC to a server.
// server is the index of the target server in rf.peers[].
// expects RPC arguments in args.
// fills in *reply with RPC reply, so caller should
// pass &reply.
// the types of the args and reply passed to Call() must be
// the same as the types of the arguments declared in the
// handler function (including whether they are pointers).
//
// The labrpc package simulates a lossy network, in which servers
// may be unreachable, and in which requests and replies may be lost.
// Call() sends a request and waits for a reply. If a reply arrives
// within a timeout interval, Call() returns true; otherwise
// Call() returns false. Thus Call() may not return for a while.
// A false return can be caused by a dead server, a live server that
// can't be reached, a lost request, or a lost reply.
//
// Call() is guaranteed to return (perhaps after a delay) *except* if the
// handler function on the server side does not return. Thus there
// is no need to implement your own timeouts around Call().
//
// look at the comments in ../labrpc/labrpc.go for more details.
//
// if you're having trouble getting RPC to work, check that you've
// capitalized all field names in structs passed over RPC, and
// that the caller passes the address of the reply struct with &, not
// the struct itself.
//
func (rf *Raft) sendRequestVote(server int, args *RequestVoteArgs, reply *RequestVoteReply) bool {
ok := rf.peers[server].Call("Raft.RequestVote", args, reply)
return ok
}
type AppendEntriesArgs struct {
Term int
LeaderId int
}
type AppendEntriesReply struct {
Term int
Success bool
}
func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) {
rf.mu.Lock()
defer rf.mu.Unlock()
reply.Term = rf.currentTerm
if rf.currentTerm > args.Term {
reply.Success = false
//DPrintf("%d refuse AE from %d",rf.me,args.LeaderId)
return
}
if rf.currentTerm < args.Term {
rf.beFollower(args.Term)
}
reply.Success = true
//DPrintf("%d accept AE from %d",rf.me,args.LeaderId)
rf.flush(ResetTimer)
}
func (rf *Raft) sendAppendEntries(server int, args *AppendEntriesArgs, reply *AppendEntriesReply) bool {
ok := rf.peers[server].Call("Raft.AppendEntries", args, reply)
//DPrintf("%d send AE to %d",rf.me,server)
return ok
}
//
// 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.
//
func (rf *Raft) Start(command interface{}) (int, int, bool) {
index := -1
term := -1
isLeader := true
// Your code here (2B).
return index, term, isLeader
}
//
// the tester doesn't halt goroutines created by Raft after each test,
// but it does call the Kill() method. your code can use killed() to
// check whether Kill() has been called. the use of atomic avoids the
// need for a lock.
//
// the issue is that long-running goroutines use memory and may chew
// up CPU time, perhaps causing later tests to fail and generating
// confusing debug output. any goroutine with a long-running loop
// should call killed() to check whether it should stop.
//
func (rf *Raft) Kill() {
atomic.StoreInt32(&rf.dead, 1)
// Your code here, if desired.
}
func (rf *Raft) killed() bool {
z := atomic.LoadInt32(&rf.dead)
return z == 1
}
//Mass RPC to AppendEntries
func (rf *Raft) groupAppendLog() {
rf.mu.Lock()
args := &AppendEntriesArgs{
rf.currentTerm,
rf.me,
}
rf.mu.Unlock()
for pid := range rf.peers {
if pid != rf.me {
go func(idx int) {
reply := &AppendEntriesReply{}
ok := rf.sendAppendEntries(idx, args, reply)
if ok {
rf.mu.Lock()
if reply.Term > rf.currentTerm {
rf.beFollower(reply.Term)
}
rf.mu.Unlock()
//TODO log handle
}
}(pid)
}
}
}
//Mass RPC to RequestVote
func (rf *Raft) kickoffElection(args *RequestVoteArgs) {
//vote counter,start from 1
var votes int32 = 1
for pid := range rf.peers {
if pid != rf.me {
go func(idx int) {
reply := &RequestVoteReply{}
ret := rf.sendRequestVote(idx, args, reply)
if ret {
rf.mu.Lock()
defer rf.mu.Unlock()
//If RPC request or response contains term T > currentTerm
//set currentTerm = T, convert to follower
if reply.Term > rf.currentTerm {
rf.beFollower(reply.Term)
return
}
if rf.state != Candidate || rf.currentTerm != args.Term {
return
}
if reply.VoteGranted {
atomic.AddInt32(&votes, 1)
if atomic.LoadInt32(&votes) > rf.majority {
rf.beLeader()
//DPrintf("%d step into leader" ,rf.me)
//be leader,flush state and start heartbeat
rf.flush(FlushState)
}
}
}
}(pid)
}
}
}
func (rf *Raft) beFollower(term int) {
rf.state = Follower
rf.votedFor = -1
rf.currentTerm = term
//DPrintf("%d convert to follower",rf.me)
}
func (rf *Raft) beCandidate() {
rf.state = Candidate
rf.currentTerm++
rf.votedFor = rf.me
args := RequestVoteArgs {
Term: rf.currentTerm,
CandidateId: rf.me,
}
go rf.kickoffElection(&args)
}
func (rf *Raft) beLeader() {
//weather it still candidate
if rf.state != Candidate {
return
}
rf.state = Leader
}
//case 1:receive AppendEntries RPC from current leader or granting vote to candidate,reset timer
//case 2:step into leader and start heartbeat,jump out of select section in ticker() to flush leader state
func (rf *Raft) flush(behaviour int) {
//select {
//case <- rf.flushCh:
//default:
//}
rf.flushCh <- behaviour
//DPrintf("%d reset",rf.me)
}
func (rf *Raft) ticker() {
for !rf.killed() {
electionTimeout := rand.Intn(150) + 350 //election timeout between 350-500 ms
rf.mu.Lock()
state := rf.state
rf.mu.Unlock()
switch state {
case Follower, Candidate:
select {
case <-time.After(time.Duration(electionTimeout) * time.Millisecond):
rf.mu.Lock()
//out of time,kick off election
rf.beCandidate()
//DPrintf("%d kick off election",rf.me)
rf.mu.Unlock()
case <-rf.flushCh:
//case 1:receive heartBeat or replicated log or vote for candidate,reset timer
//case 2:be leader and start heartbeat,jump out of select to flush state
}
case Leader:
//DPrintf("%d is LEADER",rf.me)
rf.groupAppendLog()
time.Sleep(HeartBeatTimeout)
}
}
}
//
// 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 Make(peers []*labrpc.ClientEnd, me int,
persister *Persister, applyCh chan ApplyMsg) *Raft {
rf := &Raft{}
rf.peers = peers
rf.persister = persister
rf.me = me
// Your initialization code here (2A, 2B, 2C).
rf.majority = int32(len(rf.peers) / 2)
rf.flushCh = make(chan int, 1)
rf.votedFor = -1
rf.currentTerm = 0
rf.state = Follower
rf.log = make([]Entry, 1)
// initialize from state persisted before a crash
rf.readPersist(persister.ReadRaftState())
// timer start
go rf.ticker()
return rf
}