raft

package
v0.0.0-...-a70543b Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FOLLOWER  = "FOLLOWER"
	CANDIDATE = "CANDIDATE"
	LEADER    = "LEADER"
)
View Source
const Debug = 1

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     // Leader's Term
	LeaderId     int     // Needed so follower can redirect clients
	Entries      []Entry // Log entries to store, empty if heartbeat
	PrevLogIndex int     // Index of the log Entry immediately preceding new ones
	PrevLogTerm  int     // Term of the prevLogIndex Entry
	LeaderCommit int     // Leader's CommitIndex
}

type AppendEntriesReply

type AppendEntriesReply struct {
	Success       bool // True if follower contained entry matching log index and term
	Term          int  // The current term for the leader to update itself
	ConflictIndex int  // The conflicting entry's index in the peer's log to send back to the leader. If no conflicting index is present, send back the len(log)
	ConflictTerm  int  // The term of the conflicting entry
}

type ApplyMsg

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

type Entry

type Entry struct {
	Term    int         // The leader's term that requested this Entry to be replicated
	Command interface{} // The client command to apply to the peer's state machine
}

type InstallSnapshotArgs

type InstallSnapshotArgs struct {
	Term              int     // Leader's term
	LeaderId          int     // So the follower can redirect clients
	LastIncludedIndex int     // The snapshot replaces all entries up through and including this index
	LastIncludedTerm  int     // Term of LastIncludedIndex
	Offset            int64   // Byte offset where the chunk is positioned in the snapshot file
	Data              []int64 // Raw bytes of the snapshot chunk, starting at offset
	Done              bool    // True if this is the last chunk
}

Invoked by the leader to send chunks of a snapshot to a follower. Leaders always send chunks in order.

type InstallSnapshotReply

type InstallSnapshotReply struct {
	Term int // Current term for the leader to update itself
}

type PersistentState

type PersistentState struct {
	CurrentTerm int
	VotedFor    int
	Log         []Entry
}

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
}

func Make

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

func (*Raft) AppendEntries

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

func (*Raft) GetState

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

func (*Raft) InstallSnapshot

func (rf *Raft) InstallSnapshot(args *InstallSnapshotArgs, reply *InstallSnapshotReply)

func (*Raft) Kill

func (rf *Raft) Kill()

func (*Raft) RequestVote

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

*

  • RequestVote is a candidate's attempt to request votes from peers
  • For a peer to give a candidate their vote
  • A candidate's term must be >= this peer's term and the peer cannot have voted on someone else already *
  • @param args
  • @param reply

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.

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 entry
}

type RequestVoteReply

type RequestVoteReply struct {
	Term        int  // CurrentTerm, for Candidate to update itself
	VoteGranted bool // True means Candidate received vote
}

Jump to

Keyboard shortcuts

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