Skip to content

Commit

Permalink
go.mod: Update hrw library to v2 version
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Nov 13, 2023
1 parent 1e86be9 commit 3b24af0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 115 deletions.
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ require (
github.com/google/uuid v1.3.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/hrw v1.0.9
github.com/nspcc-dev/hrw/v2 v2.0.0-20231110133715-66edfeac6cfb
github.com/nspcc-dev/neo-go v0.102.0
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0
github.com/nspcc-dev/tzhash v1.7.1
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.24.1
github.com/twmb/murmur3 v1.1.8
go.uber.org/zap v1.26.0
)

Expand Down Expand Up @@ -52,18 +53,17 @@ require (
github.com/shirou/gopsutil/v3 v3.23.7 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.11.1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
71 changes: 19 additions & 52 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions netmap/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package netmap
import (
"errors"

"github.com/nspcc-dev/hrw"
"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)

Expand All @@ -27,7 +27,7 @@ type context struct {
hrwSeed []byte

// hrw.Hash of hrwSeed
hrwSeedHash uint64
hrwSeedHash hrw.Hashable

// weightFunc is a weighting function for determining node priority
// which combines low price and high performance
Expand Down Expand Up @@ -64,7 +64,7 @@ func newContext(nm NetMap) *context {
func (c *context) setPivot(pivot []byte) {
if len(pivot) != 0 {
c.hrwSeed = pivot
c.hrwSeedHash = hrw.Hash(pivot)
c.hrwSeedHash = hrw.WrapBytes(pivot)
}
}

Expand Down
8 changes: 4 additions & 4 deletions netmap/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/sha256"
"fmt"

"github.com/nspcc-dev/hrw"
"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
Expand Down Expand Up @@ -104,7 +104,7 @@ func (m NetMap) Epoch() uint64 {
type nodes []NodeInfo

// assert nodes type provides hrw.Hasher required for HRW sorting.
var _ hrw.Hasher = nodes{}
var _ hrw.Hashable = nodes{}

// Hash is a function from hrw.Hasher interface. It is implemented
// to support weighted hrw sorting of buckets. Each bucket is already sorted by hrw,
Expand Down Expand Up @@ -152,14 +152,14 @@ func (m NetMap) PlacementVectors(vectors [][]NodeInfo, objectID oid.ID) ([][]Nod
pivot := make([]byte, sha256.Size)
objectID.Encode(pivot)

h := hrw.Hash(pivot)
h := hrw.WrapBytes(pivot)
wf := defaultWeightFunc(m.nodes)
result := make([][]NodeInfo, len(vectors))

for i := range vectors {
result[i] = make([]NodeInfo, len(vectors[i]))
copy(result[i], vectors[i])
hrw.SortSliceByWeightValue(result[i], nodes(result[i]).weights(wf), h)
hrw.SortWeighted(result[i], nodes(result[i]).weights(wf), h)
}

return result, nil
Expand Down
7 changes: 4 additions & 3 deletions netmap/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"strconv"
"strings"

"github.com/nspcc-dev/hrw"
"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/twmb/murmur3"
)

// NodeInfo groups information about NeoFS storage node which is reflected
Expand Down Expand Up @@ -217,14 +218,14 @@ func IterateNetworkEndpoints(node NodeInfo, f func(string)) {
}

// assert NodeInfo type provides hrw.Hasher required for HRW sorting.
var _ hrw.Hasher = NodeInfo{}
var _ hrw.Hashable = NodeInfo{}

// Hash implements hrw.Hasher interface.
//
// Hash is needed to support weighted HRW therefore sort function sorts nodes
// based on their public key. Hash isn't expected to be used directly.
func (x NodeInfo) Hash() uint64 {
return hrw.Hash(x.m.GetPublicKey())
return murmur3.Sum64(x.m.GetPublicKey())
}

// less declares "less than" comparison between two NodeInfo instances:
Expand Down
6 changes: 3 additions & 3 deletions netmap/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"sort"

"github.com/nspcc-dev/hrw"
"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)

Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *context) getSelection(_ PlacementPolicy, s netmap.Selector) ([]nodes, e
weights[i] = calcBucketWeight(res[i], newMeanIQRAgg(), c.weightFunc)
}

hrw.SortSliceByWeightValue(res, weights, c.hrwSeedHash)
hrw.SortWeighted(res, weights, c.hrwSeedHash)
}

if s.GetAttribute() == "" {
Expand Down Expand Up @@ -159,7 +159,7 @@ func (c *context) getSelectionBase(s netmap.Selector) []nodeAttrPair {

if len(c.hrwSeed) != 0 {
for i := range result {
hrw.SortSliceByWeightValue(result[i].nodes, result[i].nodes.weights(c.weightFunc), c.hrwSeedHash)
hrw.SortWeighted(result[i].nodes, result[i].nodes.weights(c.weightFunc), c.hrwSeedHash)
}
}

Expand Down
51 changes: 10 additions & 41 deletions netmap/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package netmap
import (
"fmt"
"math/rand"
"sort"
"strconv"
"testing"

"github.com/nspcc-dev/hrw"
"github.com/nspcc-dev/hrw/v2"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/stretchr/testify/require"
)

type hashableUint uint64

func (h hashableUint) Hash() uint64 {
return uint64(h)
}

func BenchmarkHRWSort(b *testing.B) {
const netmapSize = 1000

Expand All @@ -31,18 +36,7 @@ func BenchmarkHRWSort(b *testing.B) {
weights[i] = float64(rand.Uint32()%10) / 10.0
}

pivot := rand.Uint64()
b.Run("sort by index, no weight", func(b *testing.B) {
realNodes := make([]nodes, netmapSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
copy(realNodes, vectors)
b.StartTimer()

hrw.SortSliceByIndex(realNodes, pivot)
}
})
pivot := hashableUint(rand.Uint64())
b.Run("sort by value, no weight", func(b *testing.B) {
realNodes := make([]nodes, netmapSize)
b.ResetTimer()
Expand All @@ -51,18 +45,7 @@ func BenchmarkHRWSort(b *testing.B) {
copy(realNodes, vectors)
b.StartTimer()

hrw.SortSliceByValue(realNodes, pivot)
}
})
b.Run("only sort by index", func(b *testing.B) {
realNodes := make([]nodes, netmapSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
copy(realNodes, vectors)
b.StartTimer()

hrw.SortSliceByWeightIndex(realNodes, weights, pivot)
hrw.Sort(realNodes, pivot)
}
})
b.Run("sort by value", func(b *testing.B) {
Expand All @@ -73,21 +56,7 @@ func BenchmarkHRWSort(b *testing.B) {
copy(realNodes, vectors)
b.StartTimer()

hrw.SortSliceByWeightValue(realNodes, weights, pivot)
}
})
b.Run("sort by ID, then by index (deterministic)", func(b *testing.B) {
realNodes := make([]nodes, netmapSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
copy(realNodes, vectors)
b.StartTimer()

sort.Slice(vectors, func(i, j int) bool {
return less(vectors[i][0], vectors[j][0])
})
hrw.SortSliceByWeightIndex(realNodes, weights, pivot)
hrw.SortWeighted(realNodes, weights, pivot)
}
})
}
Expand Down

0 comments on commit 3b24af0

Please sign in to comment.