This section addresses some leftovers from the previous one:
MemoryStorage to a file, and remove themselves from the cluster, by sending a ConfChange proposal.As explained in 03-detour-memory-storage, MemoryStorage is fully described by just 3 fields. For serialization, we store them in a blob struct (another option could be using the Message protobuf):
type blob struct {
HardState raftpb.HardState // Storage::InitialState
Snapshot raftpb.Snapshot // Storage::Snapshot
Entries []raftpb.Entry // Storage::Entries
}
The file blob.go implements the necessary functions for writing / reading blobs and for conversion between blobs and MemoryStorage. The test cases from 03-detour-memory-storage/memorystorage_test.go come handy here, and are reused for testing that different MemoryStorage instances can be correctly persisted to disk and recovered.
Note:
The nodes should really persist data to disk on any modifications to
MemoryStorage, so that they'll be able to recover in case the process crashes. For simplicity, writing to disk happens only on node shutdown — this is enough for the testing purposes.
The test in 11-persistence-shutdown/the_test.go now checks that a node can be shut down and restarted from a persisted state:
.memory-3 and removes itself from the cluster..memory-3 file and re-joins the cluster.See these instructions on how to run both the test and the CLI program.