raft

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FOLLOWER    = 0
	CANDIDATE   = 1
	LEADER      = 2
	CopyEntries = 3
	HeartBeat   = 4
)
View Source
const Debug = 0

Debugging

Variables

This section is empty.

Functions

func DPrintf

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

Types

type AppendEntriesArgs

type AppendEntriesArgs struct {
	Term         int        // 领导人的任期号
	LeaderId     int        // 领导人的 Id,以便于跟随者重定向请求
	PrevLogIndex int        // 新的日志条目紧随之前的索引值
	PrevLogTerm  int        // PrevLogIndex 条目的任期号
	Entries      []LogEntry // 准备存储的日志条目(表示心跳时为空;一次性发送多个是为了提高效率)
	LeaderCommit int        // 领导人已经提交的日志的索引值
}

type AppendEntriesReply

type AppendEntriesReply struct {
	Term         int  //当前的任期号,用于领导人去更新自己
	Success      bool // 跟随者包含了匹配上 PrevLogIndex 和 PrevLogTerm 的日志时为真
	PrevLogIndex int
}

type ApplyMsg

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

type CommandState

type CommandState struct {
	Term  int
	Index int
}

type LogEntry

type LogEntry struct {
	Command interface{}
	Term    int
	Index   int
}

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 []*rpcutil.ClientEnd, me int,
	persister *Persister, applyCh chan ApplyMsg) *Raft

func (*Raft) AppendEntries

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

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

func (*Raft) RequestVote

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

func (*Raft) Start

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

type RequestVoteArgs

type RequestVoteArgs struct {
	Term         int // 候选人的任期号
	CandidateId  int // 请求选票的候选人的 Id
	LastLogIndex int // 候选人的最后日志条目的索引值
	LastLogTerm  int // 候选人最后日志条目的任期号
}

type RequestVoteReply

type RequestVoteReply struct {
	Term        int  // 当前任期号,以便于候选人去更新自己的任期号
	VoteGranted bool // 候选人赢得了此张选票时为真
}

Jump to

Keyboard shortcuts

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