Documentation
¶
Index ¶
- Constants
- Variables
- func DPrintf(format string, a ...interface{}) (n int, err error)
- type ApplyMsg
- type HardState
- type Number
- type PersistentState
- type Persister
- func (ps *Persister) Copy() *Persister
- func (ps *Persister) RaftStateSize() int64
- func (ps *Persister) ReadRaftState() []byte
- func (ps *Persister) ReadSnapshot() []byte
- func (ps *Persister) SaveRaftState(state []byte)
- func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot []byte)
- func (ps *Persister) SnapshotSize() int
- type Raft
- func (rf *Raft) AppendEntries(ctx context.Context, args *pb.AppendEntriesArgs) (reply *pb.AppendEntriesReply, err error)
- func (rf *Raft) CondInstallSnapshot(lastIncludedTerm uint64, lastIncludedIndex uint64, snapshot []byte) bool
- func (rf *Raft) GetLeaderId() uint64
- func (rf *Raft) GetState() (uint64, bool)
- func (rf *Raft) HasCurrentTermLog() bool
- func (rf *Raft) InstallSnapshot(ctx context.Context, args *pb.InstallSnapshotArgs) (reply *pb.InstallSnapshotReply, err error)
- func (rf *Raft) Kill()
- func (rf *Raft) LogSize() int64
- func (rf *Raft) RequestVote(ctx context.Context, args *pb.RequestVoteArgs) (reply *pb.RequestVoteReply, err error)
- func (rf *Raft) Snapshot(index uint64, snapshot []byte)
- func (rf *Raft) Start(command []byte) (index uint64, term uint64, isLeader bool)
- type RequestVoteArgs
- type ServerState
Constants ¶
const Debug = false
Debugging
const EmptyEntryValue = "Empty"
const HeartBeatInterval = 100 * time.Millisecond
const None uint64 = 0
Variables ¶
var ( ErrTimeout = errTimeout() ErrNoReply = errNoReply() )
Functions ¶
Types ¶
type ApplyMsg ¶
type ApplyMsg struct {
CommandValid bool
Command []byte
CommandIndex uint64
// For 2D:
SnapshotValid bool
Snapshot []byte
SnapshotTerm uint64
SnapshotIndex uint64
}
ApplyMsg as each Raft peer becomes aware that successive log entries are committed, the peer should send an ApplyMsg to the service (or tester) on the same server, via the applyCh passed to Make(). set CommandValid to true to indicate that the ApplyMsg contains a newly committed log entry.
in part 2D you'll want to send other kinds of messages (e.g., snapshots) on the applyCh, but set CommandValid to false for these other uses.
type PersistentState ¶
type Persister ¶
type Persister struct {
// contains filtered or unexported fields
}
func MakePersister ¶
func MakePersister() *Persister
func (*Persister) RaftStateSize ¶
func (*Persister) ReadRaftState ¶
func (*Persister) ReadSnapshot ¶
func (*Persister) SaveRaftState ¶
func (*Persister) SaveStateAndSnapshot ¶
Save both Raft state and K/V snapshot as a single atomic action, to help avoid them getting out of sync.
func (*Persister) SnapshotSize ¶
type Raft ¶
type Raft struct {
// Persistent state on all servers:
pb.RaftState
// contains filtered or unexported fields
}
A Go object implementing a single Raft peer.
func Make ¶
func Make(peersAddr []string, me uint64, persister *Persister, applyCh chan ApplyMsg) (*Raft, error)
Make 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) AppendEntries ¶
func (rf *Raft) AppendEntries(ctx context.Context, args *pb.AppendEntriesArgs) (reply *pb.AppendEntriesReply, err error)
func (*Raft) CondInstallSnapshot ¶
func (rf *Raft) CondInstallSnapshot(lastIncludedTerm uint64, lastIncludedIndex uint64, snapshot []byte) bool
CondInstallSnapshot A service wants to switch to snapshot. Only do so if Raft hasn't have more recent info since it communicate the snapshot on applyCh.
func (*Raft) GetLeaderId ¶
func (*Raft) GetState ¶
GetState return currentTerm and whether this server believes it is the leader.
func (*Raft) HasCurrentTermLog ¶
func (*Raft) InstallSnapshot ¶
func (rf *Raft) InstallSnapshot(ctx context.Context, args *pb.InstallSnapshotArgs) (reply *pb.InstallSnapshotReply, err error)
func (*Raft) Kill ¶
func (rf *Raft) Kill()
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) RequestVote ¶
func (rf *Raft) RequestVote(ctx context.Context, args *pb.RequestVoteArgs) (reply *pb.RequestVoteReply, err error)
func (*Raft) Snapshot ¶
Snapshot the service says it has created a snapshot that has all info up to and including index. this means the service no longer needs the log through (and including) that index. Raft should now trim its log as much as possible.
func (*Raft) Start ¶
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 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 {
// Your Entries here (2A, 2B).
Term int
CandidateId int
LastLogIndex int
LastLogTerm int
}
RequestVoteArgs example RequestVote RPC arguments structure. field names must start with capital letters!
type ServerState ¶
type ServerState int32
const ( ServerStateLeader ServerState = 1 ServerStateFollower ServerState = 2 ServerStateCandidate ServerState = 3 )