Skip to content

Commit

Permalink
quorum: minor code simplifications
Browse files Browse the repository at this point in the history
Epic: none
Release note: none
  • Loading branch information
pav-kv committed Sep 28, 2024
1 parent 5cbd417 commit 4bc4f00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
17 changes: 4 additions & 13 deletions pkg/raft/quorum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package quorum

import (
"fmt"
"maps"
"strings"

pb "github.com/cockroachdb/cockroach/pkg/raft/raftpb"
Expand Down Expand Up @@ -101,20 +102,10 @@ func (c Config) String() string {

// Clone returns a copy of the Config that shares no memory with the original.
func (c *Config) Clone() Config {
clone := func(m map[pb.PeerID]struct{}) map[pb.PeerID]struct{} {
if m == nil {
return nil
}
mm := make(map[pb.PeerID]struct{}, len(m))
for k := range m {
mm[k] = struct{}{}
}
return mm
}
return Config{
Voters: JointConfig{clone(c.Voters[0]), clone(c.Voters[1])},
Learners: clone(c.Learners),
LearnersNext: clone(c.LearnersNext),
Voters: JointConfig{maps.Clone(c.Voters[0]), maps.Clone(c.Voters[1])},
Learners: maps.Clone(c.Learners),
LearnersNext: maps.Clone(c.LearnersNext),
}
}

Expand Down
9 changes: 2 additions & 7 deletions pkg/raft/quorum/joint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c JointConfig) String() string {
// IDs returns a newly initialized map representing the set of voters present
// in the joint configuration.
func (c JointConfig) IDs() map[pb.PeerID]struct{} {
m := map[pb.PeerID]struct{}{}
m := make(map[pb.PeerID]struct{}, len(c[0])+len(c[1]))
for _, cc := range c {
for id := range cc {
m[id] = struct{}{}
Expand All @@ -55,12 +55,7 @@ func (c JointConfig) Describe(l AckedIndexer) string {
// quorum. An index is jointly committed if it is committed in both constituent
// majorities.
func (c JointConfig) CommittedIndex(l AckedIndexer) Index {
idx0 := c[0].CommittedIndex(l)
idx1 := c[1].CommittedIndex(l)
if idx0 < idx1 {
return idx0
}
return idx1
return min(c[0].CommittedIndex(l), c[1].CommittedIndex(l))
}

// VoteResult takes a mapping of voters to yes/no (true/false) votes and returns
Expand Down

0 comments on commit 4bc4f00

Please sign in to comment.