raft

package
v0.0.0-...-2d5887b Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const CandidateDuration = HeartbeatDuration * 2

竞选周期

View Source
const Debug = 0

Debugging

View Source
const Fallower, Leader, Candidate int = 1, 2, 3

import "os" 节点状态

View Source
const HeartbeatDuration = time.Duration(time.Millisecond * 600)

心跳周期

Variables

This section is empty.

Functions

func DPrintf

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

Types

type AppendEntries

type AppendEntries struct {
	Me           int
	Term         int
	PrevLogTerm  int
	PrevLogIndex int
	Entries      []LogEntry
	LeaderCommit int
	Snapshot     LogSnapshot //快照
}

日志复制请求

type ApplyMsg

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

状态机apply

type LogEntry

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

日志

type LogSnapshot

type LogSnapshot struct {
	Term  int
	Index int
	Datas []byte
}

日志快照

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 {
	EnableDebugLog bool //打印调试日志开关
	LastGetLock    string
	// contains filtered or unexported fields
}

func Make

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

func (*Raft) ElectionLoop

func (rf *Raft) ElectionLoop()

选举定时器loop

func (*Raft) GetState

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

获取状态和任期

func (*Raft) Kill

func (rf *Raft) Kill()

func (*Raft) ReplicateLogLoop

func (rf *Raft) ReplicateLogLoop(peer int)

心跳周期复制日志loop

func (*Raft) RequestAppendEntries

func (rf *Raft) RequestAppendEntries(req *AppendEntries, resp *RespEntries)

func (*Raft) RequestVote

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

收到投票请求

func (*Raft) SaveSnapshot

func (rf *Raft) SaveSnapshot(index int, snapshot []byte)

保存快照

func (*Raft) Start

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

func (*Raft) Vote

func (rf *Raft) Vote()

type RequestVoteArgs

type RequestVoteArgs struct {
	Me           int
	ElectionTerm int
	LogIndex     int
	LogTerm      int
}

投票请求

type RequestVoteReply

type RequestVoteReply struct {
	IsAgree     bool
	CurrentTerm int
}

投票rpc返回

type RespEntries

type RespEntries struct {
	Term        int
	Successed   bool
	LastApplied int
}

回复日志更新请求

Jump to

Keyboard shortcuts

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