From 8b1bed42aa11b4a55a33787b866adee3fdcd8adf Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Mon, 27 Feb 2023 16:16:37 +0300 Subject: [PATCH 1/2] [TrueCloudLab/hrw#2] node: Optimize shard hash Compute shard hash only once Signed-off-by: Dmitrii Stepanov --- pkg/local_object_storage/engine/engine.go | 4 ++-- .../engine/engine_test.go | 10 +++++++--- pkg/local_object_storage/engine/shards.go | 20 +++++++++++-------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/local_object_storage/engine/engine.go b/pkg/local_object_storage/engine/engine.go index f34d5c0bd..cacb23b8e 100644 --- a/pkg/local_object_storage/engine/engine.go +++ b/pkg/local_object_storage/engine/engine.go @@ -19,7 +19,7 @@ type StorageEngine struct { mtx *sync.RWMutex - shards map[string]shardWrapper + shards map[string]hashedShard shardPools map[string]util.WorkerPool @@ -223,7 +223,7 @@ func New(opts ...Option) *StorageEngine { return &StorageEngine{ cfg: c, mtx: new(sync.RWMutex), - shards: make(map[string]shardWrapper), + shards: make(map[string]hashedShard), shardPools: make(map[string]util.WorkerPool), closeCh: make(chan struct{}), setModeCh: make(chan setModeRequest), diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index d554d3773..69514b532 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -21,6 +21,7 @@ import ( oidtest "github.com/TrueCloudLab/frostfs-sdk-go/object/id/test" usertest "github.com/TrueCloudLab/frostfs-sdk-go/user/test" "github.com/TrueCloudLab/frostfs-sdk-go/version" + "github.com/TrueCloudLab/hrw" "github.com/TrueCloudLab/tzhash/tz" "github.com/panjf2000/ants/v2" "github.com/stretchr/testify/require" @@ -86,9 +87,12 @@ func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine { panic(err) } - engine.shards[s.ID().String()] = shardWrapper{ - errorCount: atomic.NewUint32(0), - Shard: s, + engine.shards[s.ID().String()] = hashedShard{ + shardWrapper: shardWrapper{ + errorCount: atomic.NewUint32(0), + Shard: s, + }, + hash: hrw.Hash([]byte(s.ID().String())), } engine.shardPools[s.ID().String()] = pool } diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index a6ab92e55..9341be9f9 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -16,7 +16,10 @@ import ( var errShardNotFound = logicerr.New("shard not found") -type hashedShard shardWrapper +type hashedShard struct { + shardWrapper + hash uint64 +} type metricsWithID struct { id string @@ -127,9 +130,12 @@ func (e *StorageEngine) addShard(sh *shard.Shard) error { return fmt.Errorf("shard with id %s was already added", strID) } - e.shards[strID] = shardWrapper{ - errorCount: atomic.NewUint32(0), - Shard: sh, + e.shards[strID] = hashedShard{ + shardWrapper: shardWrapper{ + errorCount: atomic.NewUint32(0), + Shard: sh, + }, + hash: hrw.Hash([]byte(strID)), } e.shardPools[strID] = pool @@ -144,7 +150,7 @@ func (e *StorageEngine) removeShards(ids ...string) { return } - ss := make([]shardWrapper, 0, len(ids)) + ss := make([]hashedShard, 0, len(ids)) e.mtx.Lock() for _, id := range ids { @@ -276,7 +282,5 @@ func (e *StorageEngine) HandleNewEpoch(epoch uint64) { } func (s hashedShard) Hash() uint64 { - return hrw.Hash( - []byte(s.Shard.ID().String()), - ) + return s.hash } -- 2.45.2 From 6726c9a8d512b880ed4e6a776bc8635d97de2d57 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Mon, 27 Feb 2023 16:39:15 +0300 Subject: [PATCH 2/2] [TrueCloudLab/hrw#2] node: Use typed HRW methods Update HRW lib and use typed HRW methods to sort shards and nodes Signed-off-by: Dmitrii Stepanov --- CHANGELOG.md | 1 + go.mod | 2 +- go.sum | Bin 90481 -> 90714 bytes pkg/local_object_storage/engine/evacuate.go | 2 +- pkg/local_object_storage/engine/shards.go | 2 +- pkg/services/reputation/common/managers.go | 2 +- 6 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9228226df..9bf7ee9b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Changelog for FrostFS Node - `golang.org/x/term` to `v0.3.0` - `google.golang.org/grpc` to `v1.51.0` - `github.com/nats-io/nats.go` to `v1.22.1` +- `github.com/TrueCloudLab/hrw` to `v.1.1.1` - Minimum go version to v1.18 ### Updating from v0.35.0 diff --git a/go.mod b/go.mod index b7758b927..cf21b9025 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/TrueCloudLab/frostfs-api-go/v2 v2.0.0-20221212144048-1351b6656d68 github.com/TrueCloudLab/frostfs-contract v0.0.0-20221213081248-6c805c1b4e42 github.com/TrueCloudLab/frostfs-sdk-go v0.0.0-20221214065929-4c779423f556 - github.com/TrueCloudLab/hrw v1.1.0 + github.com/TrueCloudLab/hrw v1.1.1-0.20230227111858-79b208bebf52 github.com/TrueCloudLab/tzhash v1.7.0 github.com/cheggaaa/pb v1.0.29 github.com/chzyer/readline v1.5.1 diff --git a/go.sum b/go.sum index 1d85e41cbf126c1f0b85a5126b57f2a15f0f856c..b81b444f26f49df5b22dd1a54dfa17b3cb81b356 100644 GIT binary patch delta 208 zcmex(i1pSH)(tKqVHri`3T1|RK&We=XJlYxY+z($ZfIy|VQQgkZkc3cV3CxXlxAwA zkYQ+*=9KDg;F|4Y9*}6{Vi4?^8C7Cj85rqa5TNf_RuqwLQsEll7-iy>RXMp)NIH&K zqxIAC^>Xu5KsGqJoB36hIG6e*8-%(QnfZo$nfUq|7Ki3o=2TRe1vmwj`{agHq!gtZ T8BA^z(P$15*&ZaqDDMjZ8UsDU delta 18 Zcmcb0g!SVg)(tKq&AOu7bwwF