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 430c4cf commit 745431f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 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
1 change: 1 addition & 0 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,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
31 changes: 25 additions & 6 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 Down

0 comments on commit 745431f

Please sign in to comment.