raft

package
v0.0.0-...-6a77d36 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

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 {
	//2A
	Term     int //领导人任期
	LeaderId int //领导人的Id

	//2B
	PrevLogIndex int
	PrevLogTerm  int
	Entries      []Log
	LeaderCommit int
}

type AppendEntriesReply

type AppendEntriesReply struct {
	Term          int  //当前的任期号,用于领导人更新自己的任期号
	Success       bool //是否接受
	ConflictTerm  int
	ConflictIndex int
}

type ApplyMsg

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

type Log

type Log struct {
	Term    int
	Command interface{}
}

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

测试服务会创建peers切片并填充其中的通信方式,然后调用多次该函数产生raft服务器(由协程持续运行), peers存储了所有raft服务器的通信方式?

func (*Raft) AppendEntries

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

func (*Raft) Candidate

func (rf *Raft) Candidate()

func (*Raft) Follower

func (rf *Raft) Follower()

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

func (rf *Raft) Leader()

func (*Raft) RequestVote

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

func (*Raft) Start

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

type RequestVoteArgs

type RequestVoteArgs struct {

	//2A
	Term        int //候选人的任期号
	CandidateId int //请求投票的候选人id

	//2B
	LastLogIndex int
	LastLogTerm  int
}

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 //如果候选人收到选票(对方认可为leader),为true
}

Jump to

Keyboard shortcuts

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