Exemple #1
0
// New instance set up for a fresh leader
func NewLeaderVolatileState(
	clusterInfo *config.ClusterInfo,
	indexOfLastEntry LogIndex,
) (*LeaderVolatileState, error) {
	lvs := &LeaderVolatileState{
		make(map[ServerId]LogIndex),
		make(map[ServerId]LogIndex),
	}

	err := clusterInfo.ForEachPeer(
		func(peerId ServerId) error {
			// #5.3-p8s4: When a leader first comes to power, it initializes
			// all nextIndex values to the index just after the last one in
			// its log (11 in Figure 7).
			lvs.NextIndex[peerId] = indexOfLastEntry + 1
			//
			lvs.MatchIndex[peerId] = 0

			return nil
		},
	)
	if err != nil {
		return nil, err
	}

	return lvs, nil
}
Exemple #2
0
// New instance set up for a fresh election
func NewCandidateVolatileState(
	clusterInfo *config.ClusterInfo,
) (*CandidateVolatileState, error) {
	cvs := &CandidateVolatileState{
		1, // assumes we always vote for ourself
		clusterInfo.QuorumSizeForCluster(),
		make(map[ServerId]bool),
	}

	err := clusterInfo.ForEachPeer(
		func(peerId ServerId) error {
			cvs.VotedPeers[peerId] = false
			return nil
		},
	)
	if err != nil {
		return nil, err
	}

	return cvs, nil
}