Documentation ¶
Index ¶
- Constants
- Variables
- func Color(colorString string) func(...interface{}) string
- func Dlog(format string, a ...interface{}) (n int, err error)
- func MicroSecondNow() int64
- type AppendEntryArgs
- type AppendEntryReply
- type ApplyMsg
- type Entry
- type ErrNotFound
- 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) SaveSnapShot(data []byte)
- func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot []byte)
- func (ps *Persister) SnapshotSize() int
- type Raft
- func (rf *Raft) AppendEntry(args *AppendEntryArgs, reply *AppendEntryReply)
- func (rf *Raft) DropBeforeIndex(index int, justCheck bool, snapshot []byte) int
- func (rf *Raft) GetState() (int, bool)
- func (rf *Raft) GetState2() (int, int, bool)
- func (rf *Raft) InstallSnapshot(args *AppendEntryArgs, reply *AppendEntryReply)
- func (rf *Raft) Kill()
- func (rf *Raft) Killed() bool
- func (rf *Raft) MaySaveSnapShot(snapShot []byte, index, term int) int
- func (rf *Raft) ReadSnapShot() []byte
- func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply)
- func (rf *Raft) SateSize() int
- func (rf *Raft) SaveSnapShot(data []byte)
- func (rf *Raft) Start(command interface{}) (int, int, bool)
- type RequestVoteArgs
- type RequestVoteReply
Constants ¶
const ( STATE_LEADER = iota STATE_CANDIDATER = iota STATE_FOLLOWER = iota )
const ( VOTED_FOR_NULL = -1 INIT_APPMSG_TERM = -2 INIT_APPMSG_INDEX = -2 INVALID_INDEX_HINT = -2 NO_LEADER = -1 INVALID_TERM = -2 )
const ( NOT_FOUND_LEFT = -1 DROP_RAFT_STATE_FAILED = -1 NO_NEED_TO_DROP_RAFT_STATE = -2 )
const Debug = 0
Variables ¶
var ( NANO_TO_MILLI = int64(1000000) Black = Color("\033[1;30m%s\033[0m") Red = Color("\033[1;31m%s\033[0m") Blue = Color("\033[0;31m%s\033[0m") Green = Color("\033[1;32m%s\033[0m") Yellow = Color("\033[1;33m%s\033[0m") Purple = Color("\033[1;34m%s\033[0m") Magenta = Color("\033[1;35m%s\033[0m") Teal = Color("\033[1;36m%s\033[0m") White = Color("\033[1;37m%s\033[0m") )
Functions ¶
func MicroSecondNow ¶
func MicroSecondNow() int64
Types ¶
type AppendEntryArgs ¶
type AppendEntryArgs struct { Term int LeaderId int PrevLogIndex int PrevLogTerm int Entries []Entry CommitIndex int SnapShot []byte WillInstallSnapShot bool }
appendEntry RPC arguments structure.
type AppendEntryReply ¶
appendEntry RPC reply structure.
type ErrNotFound ¶
type ErrNotFound struct {
Msg string
}
func (*ErrNotFound) Error ¶
func (e *ErrNotFound) Error() string
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) SaveSnapShot ¶
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 { LeaderId int Term int State int // Leader, Candidator, Follower VotedFor int MinElectTime int MaxElectTime int HeartBeatInterval time.Duration // contains filtered or unexported fields }
A Go object implementing a single Raft peer.
func Make ¶
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) AppendEntry ¶
func (rf *Raft) AppendEntry(args *AppendEntryArgs, reply *AppendEntryReply)
func (*Raft) DropBeforeIndex ¶
raft.log 至少保存一条记录!
func (*Raft) InstallSnapshot ¶
func (rf *Raft) InstallSnapshot(args *AppendEntryArgs, reply *AppendEntryReply)
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) MaySaveSnapShot ¶
func (*Raft) ReadSnapShot ¶
func (*Raft) RequestVote ¶
func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply)
example RequestVote RPC handler.
func (*Raft) SaveSnapShot ¶
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.
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 RequestVoteReply ¶
example RequestVote RPC reply structure. field names must start with capital letters!