What: this is a series of blog posts about building a small program that uses the etcd-io/raft
Go library.
Why: the README of etcd-io/raft
links to a "simple example application", raftexample, which I found too complex. For instance, it pulls dependencies from etcd-io/etcd
, which shouldn't be necessary for a minimal example. So, I'm attempting to build my own "simple example application", starting by following the "usage" section of the README.
How: The first post describes a minimal program src/01-single-node-cluster
, running a single-node raft cluster. Then, after a sequence of incremental changes, we end up with a CLI program src/11-persistence-shutdown
, similar to raftexample
: each instance runs one raft node, instances communicate over network, and together they maintain a replicated FSM.
etcd-io/raft
"memory storage" interface and implementation, plus some unit tests.02-single-node-proposals
.08-running-cluster
, with snapshots, log compaction, and so on. However, the "state machine handling loop" is implemented using the "asynchronous storage writes" mechanism.MemoryStorage
to disk. On startup, nodes check for persisted state on disk and recover from there, if available.The code for every chapter above is in a src/
sub-directory with the same name.
To run the code for a specific section without cloning the repository:
mkdir -p /tmp/01 && cd /tmp/01
go mod init tmp
go get -t github.com/zvold/using-etcd-io-raft/src/01-single-node-cluster@latest
go test -v github.com/zvold/using-etcd-io-raft/src/01-single-node-cluster
To run the code from a cloned repository, from the src
directory:
go test -v ./01-single-node-cluster
Final chapters provide a CLI program in addition to the test, here's an example for 11-persistence-shutdown:
# In one terminal, start node, listening on port 8866 and bootstrapping the raft cluster.
go run github.com/zvold/using-etcd-io-raft/src/11-persistence-shutdown@latest --bootstrap --port=8866
# In another terminal, start node 2, joining the cluster by talking to node 1 at localhost:8866.
go run github.com/zvold/using-etcd-io-raft/src/11-persistence-shutdown@latest --id=2 --join=1=localhost:8866
# Type 's' to print the FSM state, 'p' to propose a command, and 'q' to shutdown the node.