Skip to content

Commit

Permalink
[nspcc-dev#1770] shard: Move NewEpoch event routing on SE level
Browse files Browse the repository at this point in the history
It will allow dynamic shard management. Closing a shard does not allow
removing event handlers.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell authored and aprasolova committed Oct 19, 2022
1 parent 858907f commit 0c2ca78
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 77 deletions.
10 changes: 4 additions & 6 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ func initLocalStorage(c *cfg) {

ls := engine.New(engineOpts...)

addNewEpochAsyncNotificationHandler(c, func(ev event.Event) {
ls.HandleNewEpoch(ev.(netmap2.NewEpoch).EpochNumber())
})

// allocate memory for the service;
// service will be created later
c.cfgObject.getSvc = new(getsvc.Service)
Expand Down Expand Up @@ -494,11 +498,6 @@ func initShardOptions(c *cfg) {
metaPath := metabaseCfg.Path()
metaPerm := metabaseCfg.BoltDB().Perm()

gcEventChannel := make(chan shard.Event)
addNewEpochNotificationHandler(c, func(ev event.Event) {
gcEventChannel <- shard.EventNewEpoch(ev.(netmap2.NewEpoch).EpochNumber())
})

opts = append(opts, []shard.Option{
shard.WithLogger(c.log),
shard.WithRefillMetabase(sc.RefillMetabase()),
Expand Down Expand Up @@ -531,7 +530,6 @@ func initShardOptions(c *cfg) {

return pool
}),
shard.WithGCEventChannel(gcEventChannel),
})
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func testNewShard(t testing.TB, id int) *shard.Shard {
return s
}

func testEngineFromShardOpts(t *testing.T, num int, extraOpts func(int) []shard.Option) *StorageEngine {
func testEngineFromShardOpts(t *testing.T, num int, extraOpts []shard.Option) *StorageEngine {
engine := New()
for i := 0; i < num; i++ {
_, err := engine.AddShard(append([]shard.Option{
Expand All @@ -155,7 +155,7 @@ func testEngineFromShardOpts(t *testing.T, num int, extraOpts func(int) []shard.
),
shard.WithPiloramaOptions(
pilorama.WithPath(filepath.Join(t.Name(), fmt.Sprintf("pilorama%d", i)))),
}, extraOpts(i)...)...)
}, extraOpts...)...)
require.NoError(t, err)
}

Expand Down
82 changes: 26 additions & 56 deletions pkg/local_object_storage/engine/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,21 @@ func TestLockUserScenario(t *testing.T) {
// 4. saves tombstone for LOCK-object and receives error
// 5. waits for an epoch after the lock expiration one
// 6. tries to inhume the object and expects success
chEvents := make([]chan shard.Event, 2)

for i := range chEvents {
chEvents[i] = make(chan shard.Event, 1)
}

const lockerExpiresAfter = 13

cnr := cidtest.ID()
tombObj := generateObjectWithCID(t, cnr)
tombForLockID := oidtest.ID()
tombObj.SetID(tombForLockID)

e := testEngineFromShardOpts(t, 2, func(i int) []shard.Option {
return []shard.Option{
shard.WithGCEventChannel(chEvents[i]),
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
shard.WithTombstoneSource(tss{lockerExpiresAfter}),
}
e := testEngineFromShardOpts(t, 2, []shard.Option{
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
shard.WithTombstoneSource(tss{lockerExpiresAfter}),
})

t.Cleanup(func() {
Expand Down Expand Up @@ -137,9 +128,7 @@ func TestLockUserScenario(t *testing.T) {
require.ErrorIs(t, err, meta.ErrLockObjectRemoval)

// 5.
for i := range chEvents {
chEvents[i] <- shard.EventNewEpoch(lockerExpiresAfter + 1)
}
e.HandleNewEpoch(lockerExpiresAfter + 1)

// delay for GC
time.Sleep(time.Second)
Expand All @@ -156,22 +145,14 @@ func TestLockExpiration(t *testing.T) {
// 2. lock object for it is stored, and the object is locked
// 3. lock expiration epoch is coming
// 4. after some delay the object is not locked anymore
chEvents := make([]chan shard.Event, 2)

for i := range chEvents {
chEvents[i] = make(chan shard.Event, 1)
}

e := testEngineFromShardOpts(t, 2, func(i int) []shard.Option {
return []shard.Option{
shard.WithGCEventChannel(chEvents[i]),
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
}

e := testEngineFromShardOpts(t, 2, []shard.Option{
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
})

t.Cleanup(func() {
Expand Down Expand Up @@ -215,9 +196,7 @@ func TestLockExpiration(t *testing.T) {
require.ErrorAs(t, err, new(apistatus.ObjectLocked))

// 3.
for i := range chEvents {
chEvents[i] <- shard.EventNewEpoch(lockerExpiresAfter + 1)
}
e.HandleNewEpoch(lockerExpiresAfter + 1)

// delay for GC processing. It can't be estimated, but making it bigger
// will slow down test
Expand All @@ -237,25 +216,16 @@ func TestLockForceRemoval(t *testing.T) {
// 3. try to remove lock object and get error
// 4. force lock object removal
// 5. the object is not locked anymore
chEvents := make([]chan shard.Event, 2)

for i := range chEvents {
chEvents[i] = make(chan shard.Event, 1)
}

var e *StorageEngine

e = testEngineFromShardOpts(t, 2, func(i int) []shard.Option {
return []shard.Option{
shard.WithGCEventChannel(chEvents[i]),
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
shard.WithDeletedLockCallback(e.processDeletedLocks),
}
e = testEngineFromShardOpts(t, 2, []shard.Option{
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
pool, err := ants.NewPool(sz)
require.NoError(t, err)

return pool
}),
shard.WithDeletedLockCallback(e.processDeletedLocks),
})

t.Cleanup(func() {
Expand Down
12 changes: 12 additions & 0 deletions pkg/local_object_storage/engine/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func (e *StorageEngine) SetShardMode(id *shard.ID, m mode.Mode, resetErrorCounte
return errShardNotFound
}

// HandleNewEpoch notifies every shard about NewEpoch event.
func (e *StorageEngine) HandleNewEpoch(epoch uint64) {
ev := shard.EventNewEpoch(epoch)

e.mtx.RLock()
defer e.mtx.RUnlock()

for _, sh := range e.shards {
sh.NotificationChannel() <- ev
}
}

func (s hashedShard) Hash() uint64 {
return hrw.Hash(
[]byte(s.Shard.ID().String()),
Expand Down
1 change: 1 addition & 0 deletions pkg/local_object_storage/shard/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (s *Shard) Init() error {
gcCfg: s.gcCfg,
remover: s.removeGarbage,
stopChannel: make(chan struct{}),
eventChan: make(chan Event),
mEventHandler: map[eventType]*eventHandlers{
eventNewEpoch: {
cancelFunc: func() {},
Expand Down
15 changes: 9 additions & 6 deletions pkg/local_object_storage/shard/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ type gc struct {

remover func()

eventChan chan Event
mEventHandler map[eventType]*eventHandlers
}

type gcCfg struct {
eventChan <-chan Event

removerInterval time.Duration

log *logger.Logger
Expand All @@ -84,11 +83,7 @@ type gcCfg struct {
}

func defaultGCCfg() *gcCfg {
ch := make(chan Event)
close(ch)

return &gcCfg{
eventChan: ch,
removerInterval: 10 * time.Second,
log: zap.L(),
workerPoolInit: func(int) util.WorkerPool {
Expand Down Expand Up @@ -161,6 +156,9 @@ func (gc *gc) tickRemover() {
if gc.workerPool != nil {
gc.workerPool.Release()
}

close(gc.eventChan)

gc.log.Debug("GC is stopped")
return
case <-timer.C:
Expand Down Expand Up @@ -414,3 +412,8 @@ func (s *Shard) HandleDeletedLocks(lockers []oid.Address) {
return
}
}

// NotificationChannel returns channel for shard events.
func (s *Shard) NotificationChannel() chan<- Event {
return s.gc.eventChan
}
7 changes: 0 additions & 7 deletions pkg/local_object_storage/shard/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,6 @@ func WithGCWorkerPoolInitializer(wpInit func(int) util.WorkerPool) Option {
}
}

// WithGCEventChannel returns option to set a GC event channel.
func WithGCEventChannel(eventChan <-chan Event) Option {
return func(c *cfg) {
c.gcCfg.eventChan = eventChan
}
}

// WithGCRemoverSleepInterval returns option to specify sleep
// interval between object remover executions.
func WithGCRemoverSleepInterval(dur time.Duration) Option {
Expand Down

0 comments on commit 0c2ca78

Please sign in to comment.