[#1175] engine: Fix AddShard implementation

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
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

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