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.
  • Loading branch information
callthingsoff committed Feb 14, 2025
1 parent 8c52b41 commit 75a9275
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 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,12 +118,12 @@ 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)
ids := make(map[uint64]struct{})
if snap != nil {
for _, id := range snap.Metadata.ConfState.Voters {
ids[id] = true
ids[id] = struct{}{}
}
}
for _, e := range ents {
Expand All @@ -139,10 +133,8 @@ func GetEffectiveNodeIDsFromWALEntries(lg *zap.Logger, snap *raftpb.Snapshot, en
var cc raftpb.ConfChange
pbutil.MustUnmarshal(&cc, e.Data)
switch cc.Type {
case raftpb.ConfChangeAddLearnerNode:
ids[cc.NodeID] = true
case raftpb.ConfChangeAddNode:
ids[cc.NodeID] = true
case raftpb.ConfChangeAddLearnerNode, raftpb.ConfChangeAddNode:
ids[cc.NodeID] = struct{}{}
case raftpb.ConfChangeRemoveNode:
delete(ids, cc.NodeID)
case raftpb.ConfChangeUpdateNode:
Expand All @@ -151,10 +143,9 @@ 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 {
// To pass the test; maybe it's ok to return nil
return []uint64{}
}
sort.Sort(sids)
return sids
return slices.Sorted(maps.Keys(ids))
}

0 comments on commit 75a9275

Please sign in to comment.