raft

package
v0.0.0-...-d610aed Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Follower  ServerState = 0
	Candidate             = 1
	Leader                = 2
)
View Source
const DEBUG_MODE = 0

0-不打印日志; 1-打印日志

View Source
const Debug = 0

Debugging

Variables

This section is empty.

Functions

func DPrintf

func DPrintf(format string, a ...interface{}) (n int, err error)

func RandInt

func RandInt(min, max int) int

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      []LogEntry // log entries to store (empty for heartbeat; may send more than one for efficiency)
	LeaderCommit int        // leader’s commitIndex

	DebugSerial int32 // serial for debug
}

AppendEntries

type AppendEntriesReply

type AppendEntriesReply struct {
	OriginTerm int
	Term       int  // currentTerm, for leader to update itself
	Success    bool // true if follower contained entry matching prevLogIndex and prevLogTerm

	ConflictIndex int
	ConflictTerm  int
}

func AppendEntriesRoutine

func AppendEntriesRoutine(rf *Raft, i int) (bool, *AppendEntriesReply)

进入前,务必Lock; 退出后, 务必Unlock!

func (*AppendEntriesReply) ToString

func (aer *AppendEntriesReply) ToString() string

type ApplyMsg

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

type LogEntry

type LogEntry struct {
	TermId  int // term that is received by leader
	Command interface{}
}

logEntry

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 {
	// contains filtered or unexported fields
}

A Go object implementing a single Raft peer.

func Make

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

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) AppendEntries

func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply)

func (*Raft) DebugInfo

func (rf *Raft) DebugInfo()

func (*Raft) GetState

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

return currentTerm and whether this server believes it is the leader.

func (*Raft) Kill

func (rf *Raft) Kill()

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 (*Raft) RequestVote

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

example RequestVote RPC handler.

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.

func (*Raft) SwitchState

func (rf *Raft) SwitchState(s ServerState)

* rf.mu.Lock() before use

type RequestVoteArgs

type RequestVoteArgs struct {
	// Your data here (2A, 2B).
	Term         int
	CandidateId  int
	LastLogIndex int
	LastLogTerm  int
}
RequestVotes

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

type RequestVoteReply

type RequestVoteReply struct {
	// Your data here (2A).
	Term        int
	VoteGranted bool
}

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

type ServerState

type ServerState int64

Server State

func (*ServerState) ToString

func (ss *ServerState) ToString() string

Jump to

Keyboard shortcuts

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