[#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

@ -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 { func testEngineFromShardOpts(t *testing.T, num int, extraOpts func(int) []shard.Option) *StorageEngine {
engine := New() engine := New()
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
sid, err := generateShardID() _, err := engine.AddShard(append([]shard.Option{
require.NoError(t, err)
err = engine.addShard(sid, append([]shard.Option{
shard.WithBlobStorOptions( 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.WithBlobovniczaShallowWidth(1),
blobstor.WithBlobovniczaShallowDepth(1), blobstor.WithBlobovniczaShallowDepth(1),
blobstor.WithRootPerm(0700), blobstor.WithRootPerm(0700),
), ),
shard.WithMetaBaseOptions( 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), meta.WithPermissions(0700),
)}, extraOpts(i)...)...) )}, extraOpts(i)...)...)
require.NoError(t, err) require.NoError(t, err)

View file

@ -21,21 +21,17 @@ type hashedShard shardWrapper
// Returns any error encountered that did not allow adding a shard. // Returns any error encountered that did not allow adding a shard.
// Otherwise returns the ID of the added shard. // Otherwise returns the ID of the added shard.
func (e *StorageEngine) AddShard(opts ...shard.Option) (*shard.ID, error) { 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() e.mtx.Lock()
defer e.mtx.Unlock() defer e.mtx.Unlock()
pool, err := ants.NewPool(int(e.shardPoolSize), ants.WithNonblocking(true)) pool, err := ants.NewPool(int(e.shardPoolSize), ants.WithNonblocking(true))
if err != nil { 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, 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 { 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() strID := sh.ID().String()
if _, ok := e.shards[strID]; ok { 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{ e.shards[strID] = shardWrapper{
@ -60,7 +56,7 @@ func (e *StorageEngine) addShard(id *shard.ID, opts ...shard.Option) error {
e.shardPools[strID] = pool e.shardPools[strID] = pool
return nil return sh.ID(), nil
} }
func generateShardID() (*shard.ID, error) { func generateShardID() (*shard.ID, error) {