Documentation ¶
Index ¶
- Constants
- type AddPeerArgs
- type AddPeerReply
- type AppendEntriesArgs
- type AppendEntriesReply
- type ApplyMsg
- type InstallSnapshotArgs
- type InstallSnapshotReply
- type Log
- 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(data []byte)
- func (ps *Persister) SaveSnapshot(snapshot []byte)
- func (ps *Persister) SnapshotSize() int
- type Raft
- func (rf *Raft) AddPeer(args *AddPeerArgs, reply *AddPeerReply) error
- func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
- func (rf *Raft) GetPersistSize() int
- func (rf *Raft) GetState() (int, bool)
- func (rf *Raft) InstallSnapshot(args InstallSnapshotArgs, reply *InstallSnapshotReply) error
- func (rf *Raft) Kill()
- func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error
- func (rf *Raft) Start(command interface{}) (int, int, bool)
- func (rf *Raft) StartSnapshot(snapshot []byte, index int) error
- func (rf *Raft) TruncateLogs(lastIndex int, lastTerm int)
- type RequestVoteArgs
- type RequestVoteReply
Constants ¶
const ( FOLLOWER int = iota CANDIDATE LEADER )
States that a Server can have
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AppendEntriesArgs ¶
type AppendEntriesArgs struct { Term int // leader's term LeaderID int // so follower can redirect clients PrevLogIndex int // index of log entry immediately preceding new ones PrevLogTerm int // term of prevLogIndex entry Entries []Log // log entries to store (empty for heartbeat) LeaderCommit int // leader's commitIndex }
AppendEntriesArgs RPC structure
type AppendEntriesReply ¶
type AppendEntriesReply struct { Term int // currentTerm, for leader to update itself Success bool // true if follower contained entry matching prevLogIndex and prevLogTerm NextIndex int // index to try next }
AppendEntriesReply RPC structure
type ApplyMsg ¶
ApplyMsg structure As each Raft peer becomes aware that successive logs 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 InstallSnapshotArgs ¶
type InstallSnapshotArgs struct { Term int // leader's term LeaderID int // so follower can redirect clients LastIncludeIndex int // snapshot replaces all entries up through and including this index LastIncludeTerm int // term of lastIncludedIndex Data []byte // raw bytes of the snapshot chunk, starting at offset }
InstallSnapshotArgs RPC structure
type InstallSnapshotReply ¶
type InstallSnapshotReply struct {
Term int // currentTerm, for leader to update itself
}
InstallSnapshotReply RPC structure
type Persister ¶
type Persister struct {
// contains filtered or unexported fields
}
Persister structure
func (*Persister) RaftStateSize ¶
RaftStateSize return state size in int
func (*Persister) ReadRaftState ¶
ReadRaftState return a list of byte
func (*Persister) ReadSnapshot ¶
ReadSnapshot read data in list of byte
func (*Persister) SaveRaftState ¶
SaveRaftState save data in a list of byte
func (*Persister) SaveSnapshot ¶
SaveSnapshot save a snapshot data in a list of byte
func (*Persister) SnapshotSize ¶
SnapshotSize return the value in int
type Raft ¶
type Raft struct { Me int // this peer's index into peers[] // contains filtered or unexported fields }
Peer is the Raft Peer
func Make ¶
func Make(peers []*rpc.Client, me int, persister *Persister, applyCh chan ApplyMsg, e *echo.Echo) *Raft
Make a Raft instance 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) AddPeer ¶
func (rf *Raft) AddPeer(args *AddPeerArgs, reply *AddPeerReply) error
AddPeer RPC handler
func (*Raft) AppendEntries ¶
func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
AppendEntries RPC handler
func (*Raft) GetPersistSize ¶
GetPersistSize returns RaftStateSize in integer
func (*Raft) GetState ¶
GetState returns currentTerm and whether this server believes it is the leader.
func (*Raft) InstallSnapshot ¶
func (rf *Raft) InstallSnapshot(args InstallSnapshotArgs, reply *InstallSnapshotReply) error
InstallSnapshot RPC handler
func (*Raft) Kill ¶
func (rf *Raft) Kill()
Kill is called by the tester calls 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
RequestVote RPC handler
func (*Raft) Start ¶
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 logs. 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 logs, 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.
func (*Raft) StartSnapshot ¶
StartSnapshot with a list of by and index
func (*Raft) TruncateLogs ¶
TruncateLogs drops unnecessary logs
type RequestVoteArgs ¶
type RequestVoteArgs struct { Term int // candidate's term CandidateID int // candidate requesting vote LastLogIndex int // index of candidate's last log entry LastLogTerm int // term of candidate's last log entry }
RequestVoteArgs RPC structure
type RequestVoteReply ¶
type RequestVoteReply struct { Term int // currentTerm, for candidate to update itself VoteGranted bool // true means candidate received vote }
RequestVoteReply RPC structure