Skip to content

Commit

Permalink
server/storage: simplify CreateConfigChangeEnts and GetEffectiveNodeI…
Browse files Browse the repository at this point in the history
…DsFromWALEntries

By using maps and slices functions.

Signed-off-by: Jes Cok <[email protected]>
  • Loading branch information
callthingsoff committed Feb 15, 2025
1 parent 14cf669 commit f785fb6
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions server/storage/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package storage
import (
"encoding/json"
"fmt"
"sort"
"maps"
"slices"

"go.uber.org/zap"

Expand Down Expand Up @@ -52,19 +53,12 @@ func AssertNoV2StoreContent(lg *zap.Logger, st v2store.Store, deprecationStage c
// If `self` is not inside the given ids, it creates a Raft entry to add a
// default member with the given `self`.
func CreateConfigChangeEnts(lg *zap.Logger, ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
found := false
for _, id := range ids {
if id == self {
found = true
}
}

var ents []raftpb.Entry
next := index + 1

// NB: always add self first, then remove other nodes. Raft will panic if the
// set of voters ever becomes empty.
if !found {
if !slices.Contains(ids, self) {
m := membership.Member{
ID: types.ID(self),
RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://localhost:2380"}},
Expand Down Expand Up @@ -124,7 +118,7 @@ func GetEffectiveNodeIdsFromWalEntries(lg *zap.Logger, snap *raftpb.Snapshot, en
// ID-related entry:
// - ConfChangeAddNode, in which case the contained ID will Be added into the set.
// - ConfChangeRemoveNode, in which case the contained ID will Be removed from the set.
// - ConfChangeAddLearnerNode, in which the contained ID will Be added into the set.
// - ConfChangeAddLearnerNode, in which case the contained ID will Be added into the set.
func GetEffectiveNodeIDsFromWALEntries(lg *zap.Logger, snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
ids := make(map[uint64]bool)
if snap != nil {
Expand All @@ -151,10 +145,8 @@ func GetEffectiveNodeIDsFromWALEntries(lg *zap.Logger, snap *raftpb.Snapshot, en
lg.Panic("unknown ConfChange Type", zap.String("type", cc.Type.String()))
}
}
sids := make(types.Uint64Slice, 0, len(ids))
for id := range ids {
sids = append(sids, id)
if len(ids) == 0 {
return []uint64{} // To pass the test; maybe it's OK to return nil.
}
sort.Sort(sids)
return sids
return slices.Sorted(maps.Keys(ids))
}

0 comments on commit f785fb6

Please sign in to comment.