[#378] shard: Collect expired non-tombstone objects in GC every epoch

Add new epoch event handler to GC that finds all expired non-tombstone
objects and marks them to be removed.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-16 20:34:45 +03:00 committed by Alex Vanin
parent 89a22450e5
commit dab45050b9
2 changed files with 60 additions and 3 deletions

View file

@ -40,9 +40,16 @@ func (s *Shard) Init() error {
}
gc := &gc{
gcCfg: s.gcCfg,
remover: s.removeGarbage,
mEventHandler: map[eventType]*eventHandlers{},
gcCfg: s.gcCfg,
remover: s.removeGarbage,
mEventHandler: map[eventType]*eventHandlers{
eventNewEpoch: {
cancelFunc: func() {},
handlers: []eventHandler{
s.collectExpiredObjects,
},
},
},
}
gc.init()

View file

@ -186,3 +186,53 @@ func (s *Shard) removeGarbage() {
return
}
}
func (s *Shard) collectExpiredObjects(ctx context.Context, e Event) {
epoch := e.(newEpoch).epoch
var expired []*object.Address
// collect expired non-tombstone object
err := s.metaBase.IterateExpired(epoch, func(expiredObject *meta.ExpiredObject) error {
select {
case <-ctx.Done():
return meta.ErrInterruptIterator
default:
}
if expiredObject.Type() != object.TypeTombstone {
expired = append(expired, expiredObject.Address())
}
return nil
})
if err != nil {
s.log.Warn("iterator over expired objects failed",
zap.String("error", err.Error()),
)
return
} else if len(expired) == 0 {
return
}
// check if context canceled
select {
case <-ctx.Done():
return
default:
}
// inhume the collected objects
_, err = s.metaBase.Inhume(new(meta.InhumePrm).
WithAddresses(expired...).
WithGCMark(),
)
if err != nil {
s.log.Warn("could not inhume the objects",
zap.String("error", err.Error()),
)
return
}
}