raft

package
v0.0.0-...-8c27e12 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LEADER state = iota
	CANDIDATE
	FOLLOWER
)

规定了 server 所需的 3 种状态

View Source
const Debugging = 0

Debugging is

View Source
const (
	// NOBODY used for Raft.votedFor, means vote for none
	NOBODY = -1
)

Variables

This section is empty.

Functions

func DPrintf

func DPrintf(format string, a ...interface{})

DPrintf is

Types

type AppendEntriesArgs

type AppendEntriesArgs struct {
	Term         int        // leader.currentTerm
	LeaderID     int        // leader.me
	PrevLogIndex int        // index of log entry immediately preceding new ones
	PrevLogTerm  int        // term of prevLogIndex entry
	LeaderCommit int        // leader.commitIndex
	Entries      []LogEntry // 需要添加的 log 单元
}

AppendEntriesArgs 是添加 log 的参数

func (AppendEntriesArgs) String

func (a AppendEntriesArgs) String() string

type AppendEntriesReply

type AppendEntriesReply struct {
	Term      int  // 回复者的 term
	Success   bool // 返回 true,如果被调用的 rf.logs 真的 append 了 entries
	NextIndex int  // 下一次发送的 AppendEntriesArgs.Entries[0] 在 Leader.logs 中的索引号
}

AppendEntriesReply 是 flower 回复 leader 的内容

func (AppendEntriesReply) String

func (r AppendEntriesReply) String() string

type ApplyMsg

type ApplyMsg struct {
	CommandValid bool // CommandValid = true 表示, 此消息是用于应用 Command
	CommandIndex int  // Command 所在的 logEntry.logIndex 值
	Command      interface{}
}

ApplyMsg is 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.

func (ApplyMsg) String

func (m ApplyMsg) String() string

type LogEntry

type LogEntry struct {
	LogIndex int         // raft.logs 会被压缩裁剪,需要保存此 log 在原本的索引号
	LogTerm  int         // LEADER 在生成此 log 时的 LEADER.currentTerm
	Command  interface{} // 具体的命令内容
}

LogEntry is log entry

type Persister

type Persister struct {
	// contains filtered or unexported fields
}

Persister is

func MakePersister

func MakePersister() *Persister

MakePersister is

func (*Persister) Copy

func (ps *Persister) Copy() *Persister

Copy is

func (*Persister) RaftStateSize

func (ps *Persister) RaftStateSize() int

RaftStateSize is

func (*Persister) ReadRaftState

func (ps *Persister) ReadRaftState() []byte

ReadRaftState is

func (*Persister) ReadSnapshot

func (ps *Persister) ReadSnapshot() []byte

ReadSnapshot is

func (*Persister) SaveRaftState

func (ps *Persister) SaveRaftState(state []byte)

SaveRaftState is

func (*Persister) SaveStateAndSnapshot

func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot []byte)

SaveStateAndSnapshot is 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

SnapshotSize is

type Raft

type Raft struct {
	// contains filtered or unexported fields
}

Raft is A Go object implementing a single Raft peer.

func Make

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

Make is 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)

AppendEntries 会处理收到 AppendEntries RPC

func (*Raft) GetState

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

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

func (*Raft) Kill

func (rf *Raft) Kill()

Kill is 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) RequestVote

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

RequestVote is example RequestVote RPC handler.

func (*Raft) Start

func (rf *Raft) Start(command interface{}) (int, int, bool)

Start is 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, without waiting for the log appends to complete. ** 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) String

func (rf *Raft) String() string

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 获取投票参数 example RequestVote RPC arguments structure. field names must start with capital letters!

func (RequestVoteArgs) String

func (a RequestVoteArgs) String() string

type RequestVoteReply

type RequestVoteReply struct {
	Term        int
	VoteGranted bool
}

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

func (RequestVoteReply) String

func (reply RequestVoteReply) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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