Raft Hints
CS1380 S22
When in doubt, read the raft cheat sheet.
Play around with a Raft simulation here: https://raft.github.io/ (does not cover network partitions)
System Components
Node Anatomy
All of these variables are already initialized for you
currentTerm and votedFor are stored in StableStore. Use the provided helper functions GetCurrentTerm, SetCurrentTerm, etc. to access it.
commitIndex, nextIndex, matchIndex and lastApplied are kept inside the Node struct definition.
Remember to update these where appropriate in your implementation.
Node Anatomy
type Config struct {
ElectionTimeout time.Duration
HeartbeatTimeout time.Duration
ClusterSize int
NodeIDSize int
LogPath string
InMemory bool
}
(Nodes have access to this Config under the Config struct variable)
Node Anatomy
We’ve created four Go channels for you to read from:
n.appendEntries = make(chan AppendEntriesMsg) // AppendEntries RPC
n.requestVote = make(chan RequestVoteMsg) // RequestVote RPC
n.clientRequest = make(chan ClientRequestMsg) // client requests
n.gracefulExit = make(chan bool) // if true, shutdown right away
You are required to handle messages from any of these channels in any node state. For example, if you are in doFollower state, you should be able to handle clientRequest messages appropriately.
How State Transitions Work
Example of how to do a transition from Follower -> Candidate
// anywhere inside doFollower()
-> return doCandidate // return a function pointer to doCandidate function
Example of how to do a transition from Follower to shutdown state
// anywhere inside doFollower()
-> return nil // triggers a shutdown of the node
(you can generalize this transition logic to doLeader, doCandidate and doFollower respectively)
What You Are Expected to Do
Test Requirement
Coverage on the following files should all be >= 90% coverage.
Suggested testing scenarios: Leader election, log replication, log commitment, client interaction, and all of the above but during network partition
There is no coverage requirement for other files. Document your tests!
To check per-file coverage,
go test -coverprofile=coverage.out -coverpkg=raft/pkg # dumps cover profile�go tool cover -html=cover.out # opens a browser |
Test Helpers
Provided Helper Functions
Logging helpers
Applies a single log entry to the finite state machine
High Level APIs for StableStore (persistent state on all servers)
Client Registration
← What field of the newly created log can be used as a unique client id?
type RegisterClientMsg struct {
request *RegisterClientRequest
reply chan RegisterClientReply
}
Client Requests (Concepts)
4. If a duplicate request comes before processLogEntry is called, make sure when the reply is available, both old and new reply channels are replied to.
5. If a duplicate request comes after processLogEntry is called, call n.GetCachedReply.
type ClientRequestMsg struct {
request *ClientRequest
reply chan ClientReply
}
Client Requests (Implement)
← GetCachedReply caches replies that are already sent back to the user.
← requestsByCacheID caches requests that are in the log but not yet processed.
type ClientRequestMsg struct {
request *ClientRequest
reply chan ClientReply
}
Other Things
FAQ