[#1175] engine: Fix `AddShard` implementation

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
remotes/fyrchik/fix-lock
Leonard Lyubich 2022-03-11 10:13:33 +03:00 committed by LeL
parent 9dff07200c
commit 9feb5f9405
2 changed files with 12 additions and 19 deletions

View File

@ -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)

View File

@ -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) {