From 9feb5f9405ce028a6230c4c48ac0178c5c589425 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 11 Mar 2022 10:13:33 +0300 Subject: [PATCH] [#1175] engine: Fix `AddShard` implementation Signed-off-by: Leonard Lyubich --- .../engine/engine_test.go | 9 +++----- pkg/local_object_storage/engine/shards.go | 22 ++++++++----------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index 561b83f63c..4f045ac878 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -117,18 +117,15 @@ func testNewShard(t testing.TB, id int) *shard.Shard { func testEngineFromShardOpts(t *testing.T, num int, extraOpts func(int) []shard.Option) *StorageEngine { engine := New() for i := 0; i < num; i++ { - sid, err := generateShardID() - require.NoError(t, err) - - err = engine.addShard(sid, append([]shard.Option{ + _, err := engine.AddShard(append([]shard.Option{ shard.WithBlobStorOptions( - blobstor.WithRootPath(filepath.Join(t.Name(), fmt.Sprintf("%d.blobstor", sid))), + blobstor.WithRootPath(filepath.Join(t.Name(), fmt.Sprintf("blobstor%d", i))), blobstor.WithBlobovniczaShallowWidth(1), blobstor.WithBlobovniczaShallowDepth(1), blobstor.WithRootPerm(0700), ), shard.WithMetaBaseOptions( - meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.metabase", sid))), + meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("metabase%d", i))), meta.WithPermissions(0700), )}, extraOpts(i)...)...) require.NoError(t, err) diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index 5494d23cc6..b8f64897a0 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -21,21 +21,17 @@ type hashedShard shardWrapper // Returns any error encountered that did not allow adding a shard. // Otherwise returns the ID of the added shard. func (e *StorageEngine) AddShard(opts ...shard.Option) (*shard.ID, error) { - id, err := generateShardID() - if err != nil { - return nil, fmt.Errorf("could not generate shard ID: %w", err) - } - - return id, e.addShard(id, opts...) -} - -func (e *StorageEngine) addShard(id *shard.ID, opts ...shard.Option) error { e.mtx.Lock() defer e.mtx.Unlock() pool, err := ants.NewPool(int(e.shardPoolSize), ants.WithNonblocking(true)) if err != nil { - return err + return nil, err + } + + id, err := generateShardID() + if err != nil { + return nil, fmt.Errorf("could not generate shard ID: %w", err) } sh := shard.New(append(opts, @@ -45,12 +41,12 @@ func (e *StorageEngine) addShard(id *shard.ID, opts ...shard.Option) error { )...) if err := sh.UpdateID(); err != nil { - return fmt.Errorf("could not open shard: %w", err) + return nil, fmt.Errorf("could not open shard: %w", err) } strID := sh.ID().String() if _, ok := e.shards[strID]; ok { - return fmt.Errorf("shard with id %s was already added", strID) + return nil, fmt.Errorf("shard with id %s was already added", strID) } e.shards[strID] = shardWrapper{ @@ -60,7 +56,7 @@ func (e *StorageEngine) addShard(id *shard.ID, opts ...shard.Option) error { e.shardPools[strID] = pool - return nil + return sh.ID(), nil } func generateShardID() (*shard.ID, error) {