Open anshulpundir opened 7 years ago
The wasLeader
stuff is about detecting leadership transitions. The etcd/raft
state machine tells us if we are currently the leader or not, but we need to keep track ourselves to see whether this has changed. Based on that, we can do certain things that need to happen at the transition.
if snapshotMeta.Index > n.snapshotMeta.Index {
A snapshot finished saving to disk, and it's newer than the previously most current snapshot. This triggers garbage collection of old snapshots.
func (n *Node) restoreFromSnapshot(ctx context.Context, data []byte) error {
Restores the in-memory state from a snapshot that either came over the wire from another node, or from the disk (for example at startup).
When code isn't clear, you might fight it helpful to look at the commit that added it. Commit messages tend to be pretty informative, and the PRs associated with them often contained a lot of discussion.
@cyli is also familiar with the raft code.
Few places which could use more comments:
https://github.com/docker/swarmkit/blob/master/manager/state/raft/raft.go#L545 https://github.com/docker/swarmkit/blob/master/manager/state/raft/raft.go#L566 https://github.com/docker/swarmkit/blob/master/manager/state/raft/raft.go#L632 https://github.com/docker/swarmkit/blob/master/manager/state/raft/raft.go#L681 https://github.com/docker/swarmkit/blob/master/manager/state/raft/raft.go#L714
There is code in store which would also be more readable with more comments.