Skip to content

Commit

Permalink
raft/tracker: pull Voters and Learners into Config struct
Browse files Browse the repository at this point in the history
This is helpful to quickly print the configuration log messages without
having to specify Voters and Learners separately.

It will also come in handy for joint quorums because it allows holding
on to voters and learners as a unit, which is useful for unit testing.
  • Loading branch information
tbg committed Jun 28, 2019
1 parent 3c65a08 commit c143dd7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
7 changes: 7 additions & 0 deletions raft/quorum/joint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ package quorum
// majority configurations. Decisions require the support of both majorities.
type JointConfig [2]MajorityConfig

func (c JointConfig) String() string {
if len(c[1]) > 0 {
return c[0].String() + "&&" + c[1].String()
}
return c[0].String()
}

// IDs returns a newly initialized map representing the set of voters present
// in the joint configuration.
func (c JointConfig) IDs() map[uint64]struct{} {
Expand Down
18 changes: 18 additions & 0 deletions raft/quorum/majority.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ import (
// MajorityConfig is a set of IDs that uses majority quorums to make decisions.
type MajorityConfig map[uint64]struct{}

func (c MajorityConfig) String() string {
sl := make([]uint64, 0, len(c))
for id := range c {
sl = append(sl, id)
}
sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] })
var buf strings.Builder
buf.WriteByte('(')
for i := range sl {
if i > 0 {
buf.WriteByte(' ')
}
buf.WriteString(fmt.Sprintf("%d", sl[i]))
}
buf.WriteByte(')')
return buf.String()
}

// Describe returns a (multi-line) representation of the commit indexes for the
// given lookuper.
func (c MajorityConfig) Describe(l AckedIndexer) string {
Expand Down
1 change: 1 addition & 0 deletions raft/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
)

// Index is a Raft log position.
type Index uint64

func (i Index) String() string {
Expand Down
5 changes: 3 additions & 2 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ func (r *raft) campaign(t CampaignType) {
}
return
}
for id := range r.prs.Voters.IDs() {
for id := range r.prs.Config.Voters.IDs() {
if id == r.id {
continue
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ func stepLeader(r *raft, m pb.Message) error {
return nil
}

if r.prs.Voters.VoteResult(r.readOnly.recvAck(m.From, m.Context)) != quorum.VoteWon {
if r.prs.Config.Voters.VoteResult(r.readOnly.recvAck(m.From, m.Context)) != quorum.VoteWon {
return nil
}

Expand Down Expand Up @@ -1455,6 +1455,7 @@ func (r *raft) applyConfChange(cc pb.ConfChange) pb.ConfState {
}
}

r.logger.Infof("%x switched to configuration %s", r.id, r.prs.Config)
// Now that the configuration is updated, handle any side effects.

cs := pb.ConfState{Nodes: r.prs.VoterNodes(), Learners: r.prs.LearnerNodes()}
Expand Down
8 changes: 4 additions & 4 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4118,8 +4118,8 @@ func newNetworkWithConfig(configFunc func(*Config), peers ...stateMachine) *netw
sm := newRaft(cfg)
npeers[id] = sm
case *raft:
learners := make(map[uint64]bool, len(v.prs.Learners))
for i := range v.prs.Learners {
learners := make(map[uint64]bool, len(v.prs.Config.Learners))
for i := range v.prs.Config.Learners {
learners[i] = true
}
v.id = id
Expand All @@ -4128,9 +4128,9 @@ func newNetworkWithConfig(configFunc func(*Config), peers ...stateMachine) *netw
pr := &tracker.Progress{}
if _, ok := learners[peerAddrs[i]]; ok {
pr.IsLearner = true
v.prs.Learners[peerAddrs[i]] = struct{}{}
v.prs.Config.Learners[peerAddrs[i]] = struct{}{}
} else {
v.prs.Voters[0][peerAddrs[i]] = struct{}{}
v.prs.Config.Voters[0][peerAddrs[i]] = struct{}{}
}
v.prs.Progress[peerAddrs[i]] = pr
}
Expand Down
61 changes: 40 additions & 21 deletions raft/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ import (
"go.etcd.io/etcd/raft/quorum"
)

// A Config reflects the configuration tracked in a ProgressTracker.
type Config struct {
Voters quorum.JointConfig
Learners map[uint64]struct{}
}

func (c *Config) String() string {
if len(c.Learners) == 0 {
return fmt.Sprintf("voters=%s", c.Voters)
}
return fmt.Sprintf(
"voters=%s learners=%s",
c.Voters, quorum.MajorityConfig(c.Learners).String(),
)
}

// ProgressTracker tracks the currently active configuration and the information
// known about the nodes and learners in it. In particular, it tracks the match
// index for each peer which in turn allows reasoning about the committed index.
type ProgressTracker struct {
Voters quorum.JointConfig
Learners map[uint64]struct{}
Config

Progress map[uint64]*Progress

Expand All @@ -39,11 +54,15 @@ type ProgressTracker struct {
func MakeProgressTracker(maxInflight int) ProgressTracker {
p := ProgressTracker{
MaxInflight: maxInflight,
Voters: quorum.JointConfig{
quorum.MajorityConfig{},
quorum.MajorityConfig{},
Config: Config{
Voters: quorum.JointConfig{
quorum.MajorityConfig{},
// TODO(tbg): this will be mostly empty, so make it a nil pointer
// in the common case.
quorum.MajorityConfig{},
},
Learners: map[uint64]struct{}{},
},
Learners: map[uint64]struct{}{},
Votes: map[uint64]bool{},
Progress: map[uint64]*Progress{},
}
Expand All @@ -53,7 +72,7 @@ func MakeProgressTracker(maxInflight int) ProgressTracker {
// IsSingleton returns true if (and only if) there is only one voting member
// (i.e. the leader) in the current configuration.
func (p *ProgressTracker) IsSingleton() bool {
return len(p.Voters[0]) == 1 && len(p.Voters[1]) == 0
return len(p.Config.Voters[0]) == 1 && len(p.Config.Voters[1]) == 0
}

type matchAckIndexer map[uint64]*Progress
Expand All @@ -72,16 +91,16 @@ func (l matchAckIndexer) AckedIndex(id uint64) (quorum.Index, bool) {
// Committed returns the largest log index known to be committed based on what
// the voting members of the group have acknowledged.
func (p *ProgressTracker) Committed() uint64 {
return uint64(p.Voters.CommittedIndex(matchAckIndexer(p.Progress)))
return uint64(p.Config.Voters.CommittedIndex(matchAckIndexer(p.Progress)))
}

// RemoveAny removes this peer, which *must* be tracked as a voter or learner,
// from the tracker.
func (p *ProgressTracker) RemoveAny(id uint64) {
_, okPR := p.Progress[id]
_, okV1 := p.Voters[0][id]
_, okV2 := p.Voters[1][id]
_, okL := p.Learners[id]
_, okV1 := p.Config.Voters[0][id]
_, okV2 := p.Config.Voters[1][id]
_, okL := p.Config.Learners[id]

okV := okV1 || okV2

Expand All @@ -93,9 +112,9 @@ func (p *ProgressTracker) RemoveAny(id uint64) {
panic(fmt.Sprintf("peer %x is both voter and learner", id))
}

delete(p.Voters[0], id)
delete(p.Voters[1], id)
delete(p.Learners, id)
delete(p.Config.Voters[0], id)
delete(p.Config.Voters[1], id)
delete(p.Config.Learners, id)
delete(p.Progress, id)
}

Expand All @@ -106,9 +125,9 @@ func (p *ProgressTracker) InitProgress(id, match, next uint64, isLearner bool) {
panic(fmt.Sprintf("peer %x already tracked as node %v", id, pr))
}
if !isLearner {
p.Voters[0][id] = struct{}{}
p.Config.Voters[0][id] = struct{}{}
} else {
p.Learners[id] = struct{}{}
p.Config.Learners[id] = struct{}{}
}
p.Progress[id] = &Progress{Next: next, Match: match, Inflights: NewInflights(p.MaxInflight), IsLearner: isLearner}
}
Expand All @@ -131,12 +150,12 @@ func (p *ProgressTracker) QuorumActive() bool {
votes[id] = pr.RecentActive
})

return p.Voters.VoteResult(votes) == quorum.VoteWon
return p.Config.Voters.VoteResult(votes) == quorum.VoteWon
}

// VoterNodes returns a sorted slice of voters.
func (p *ProgressTracker) VoterNodes() []uint64 {
m := p.Voters.IDs()
m := p.Config.Voters.IDs()
nodes := make([]uint64, 0, len(m))
for id := range m {
nodes = append(nodes, id)
Expand All @@ -147,8 +166,8 @@ func (p *ProgressTracker) VoterNodes() []uint64 {

// LearnerNodes returns a sorted slice of learners.
func (p *ProgressTracker) LearnerNodes() []uint64 {
nodes := make([]uint64, 0, len(p.Learners))
for id := range p.Learners {
nodes := make([]uint64, 0, len(p.Config.Learners))
for id := range p.Config.Learners {
nodes = append(nodes, id)
}
sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })
Expand Down Expand Up @@ -190,6 +209,6 @@ func (p *ProgressTracker) TallyVotes() (granted int, rejected int, _ quorum.Vote
rejected++
}
}
result := p.Voters.VoteResult(p.Votes)
result := p.Config.Voters.VoteResult(p.Votes)
return granted, rejected, result
}

0 comments on commit c143dd7

Please sign in to comment.