Documentation ¶
Index ¶
- Constants
- func DPrintf(format string, a ...interface{}) (n int, err error)
- type AppendEntryArgs
- type AppendEntryReply
- type ApplyMsg
- type LogEntry
- type Persister
- func (ps *Persister) Copy() *Persister
- func (ps *Persister) RaftStateSize() int
- func (ps *Persister) ReadRaftState() []byte
- func (ps *Persister) ReadSnapshot() []byte
- func (ps *Persister) SaveRaftState(state []byte)
- func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot []byte)
- func (ps *Persister) SnapshotSize() int
- type Raft
- type RequestVoteArgs
- type RequestVoteReply
Constants ¶
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 )
const Debug = 0
Debugging
Variables ¶
This section is empty.
Functions ¶
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 ¶
AppendEntryReply is ...
type ApplyMsg ¶
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 Persister ¶
type Persister struct {
// contains filtered or unexported fields
}
func MakePersister ¶
func MakePersister() *Persister
func (*Persister) RaftStateSize ¶
func (*Persister) ReadRaftState ¶
func (*Persister) ReadSnapshot ¶
func (*Persister) SaveRaftState ¶
func (*Persister) SaveStateAndSnapshot ¶
Save both Raft state and K/V snapshot as a single atomic action, to help avoid them getting out of sync.
func (*Persister) SnapshotSize ¶
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 (*Raft) AppendEntries ¶
func (rf *Raft) AppendEntries(args *AppendEntryArgs, reply *AppendEntryReply)
AppendEntries ...
func (*Raft) GetState ¶
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) Start ¶
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!