raft

package
v0.0.0-...-b115306 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FOLLOWER int = iota
	CANDIDATE
	LEADER
)

States that a Server can have

Variables

This section is empty.

Functions

This section is empty.

Types

type AddPeerArgs

type AddPeerArgs struct {
	Endpoint string
}

AddPeerArgs RPC structure

type AddPeerReply

type AddPeerReply struct {
	Success bool
}

AddPeerReply RPC structure

type AppendEntriesArgs

type AppendEntriesArgs struct {
	Term         int   // leader's term
	LeaderID     int   // so follower can redirect clients
	PrevLogIndex int   // index of log entry immediately preceding new ones
	PrevLogTerm  int   // term of prevLogIndex entry
	Entries      []Log // log entries to store (empty for heartbeat)
	LeaderCommit int   // leader's commitIndex
}

AppendEntriesArgs RPC structure

type AppendEntriesReply

type AppendEntriesReply struct {
	Term      int  // currentTerm, for leader to update itself
	Success   bool // true if follower contained entry matching prevLogIndex and prevLogTerm
	NextIndex int  // index to try next
}

AppendEntriesReply RPC structure

type ApplyMsg

type ApplyMsg struct {
	Index       int
	Command     interface{}
	UseSnapshot bool
	Snapshot    []byte
}

ApplyMsg structure As each Raft peer becomes aware that successive logs entries are committed, the peer should send an ApplyMsg to the service (or tester) on the same server, via the applyCh passed to Make().

type InstallSnapshotArgs

type InstallSnapshotArgs struct {
	Term             int    // leader's term
	LeaderID         int    // so follower can redirect clients
	LastIncludeIndex int    // snapshot replaces all entries up through and including this index
	LastIncludeTerm  int    // term of lastIncludedIndex
	Data             []byte // raw bytes of the snapshot chunk, starting at offset
}

InstallSnapshotArgs RPC structure

type InstallSnapshotReply

type InstallSnapshotReply struct {
	Term int // currentTerm, for leader to update itself
}

InstallSnapshotReply RPC structure

type Log

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

Log is the individual item in each server Logs

type Persister

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

Persister structure

func MakePersister

func MakePersister() *Persister

MakePersister create a Persister instance

func (*Persister) Copy

func (ps *Persister) Copy() *Persister

Copy a Persister

func (*Persister) RaftStateSize

func (ps *Persister) RaftStateSize() int

RaftStateSize return state size in int

func (*Persister) ReadRaftState

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

ReadRaftState return a list of byte

func (*Persister) ReadSnapshot

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

ReadSnapshot read data in list of byte

func (*Persister) SaveRaftState

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

SaveRaftState save data in a list of byte

func (*Persister) SaveSnapshot

func (ps *Persister) SaveSnapshot(snapshot []byte)

SaveSnapshot save a snapshot data in a list of byte

func (*Persister) SnapshotSize

func (ps *Persister) SnapshotSize() int

SnapshotSize return the value in int

type Raft

type Raft struct {
	Me int // this peer's index into peers[]
	// contains filtered or unexported fields
}

Peer is the Raft Peer

func Make

func Make(peers []*rpc.Client, me int,
	persister *Persister, applyCh chan ApplyMsg, e *echo.Echo) *Raft

Make a Raft instance 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) AddPeer

func (rf *Raft) AddPeer(args *AddPeerArgs, reply *AddPeerReply) error

AddPeer RPC handler

func (*Raft) AppendEntries

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

AppendEntries RPC handler

func (*Raft) GetPersistSize

func (rf *Raft) GetPersistSize() int

GetPersistSize returns RaftStateSize in integer

func (*Raft) GetState

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

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

func (*Raft) InstallSnapshot

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

InstallSnapshot RPC handler

func (*Raft) Kill

func (rf *Raft) Kill()

Kill is called by the tester calls 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) error

RequestVote RPC handler

func (*Raft) Start

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

Start the service using Raft (e.g. a k/v server) wants to start agreement on the next command to be appended to Raft's logs. 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 logs, since the leader may fail or lose an election.

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

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

StartSnapshot with a list of by and index

func (*Raft) TruncateLogs

func (rf *Raft) TruncateLogs(lastIndex int, lastTerm int)

TruncateLogs drops unnecessary logs

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 RPC structure

type RequestVoteReply

type RequestVoteReply struct {
	Term        int  // currentTerm, for candidate to update itself
	VoteGranted bool // true means candidate received vote
}

RequestVoteReply RPC structure

Jump to

Keyboard shortcuts

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