2021-02-16 11:34:48 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-16 12:17:37 +00:00
|
|
|
"sync"
|
2021-02-16 11:34:48 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-02-16 11:34:48 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Event represents class of external events.
|
|
|
|
type Event interface {
|
|
|
|
typ() eventType
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ eventType = iota
|
2021-02-16 17:32:46 +00:00
|
|
|
eventNewEpoch
|
2021-02-16 11:34:48 +00:00
|
|
|
)
|
|
|
|
|
2021-02-16 17:32:46 +00:00
|
|
|
type newEpoch struct {
|
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e newEpoch) typ() eventType {
|
|
|
|
return eventNewEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventNewEpoch returns new epoch event.
|
|
|
|
func EventNewEpoch(e uint64) Event {
|
|
|
|
return newEpoch{
|
|
|
|
epoch: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
type eventHandler func(context.Context, Event)
|
|
|
|
|
|
|
|
type eventHandlers struct {
|
2021-02-16 12:17:37 +00:00
|
|
|
prevGroup sync.WaitGroup
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
|
|
|
|
handlers []eventHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
type gc struct {
|
|
|
|
*gcCfg
|
|
|
|
|
2021-11-11 13:54:36 +00:00
|
|
|
onceStop sync.Once
|
2021-08-04 12:04:37 +00:00
|
|
|
stopChannel chan struct{}
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
workerPool util.WorkerPool
|
|
|
|
|
|
|
|
remover func()
|
|
|
|
|
|
|
|
mEventHandler map[eventType]*eventHandlers
|
|
|
|
}
|
|
|
|
|
|
|
|
type gcCfg struct {
|
|
|
|
eventChanInit func() <-chan Event
|
|
|
|
|
|
|
|
removerInterval time.Duration
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
workerPoolInit func(int) util.WorkerPool
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultGCCfg() *gcCfg {
|
|
|
|
ch := make(chan Event)
|
|
|
|
close(ch)
|
|
|
|
|
|
|
|
return &gcCfg{
|
|
|
|
eventChanInit: func() <-chan Event {
|
|
|
|
return ch
|
|
|
|
},
|
|
|
|
removerInterval: 10 * time.Second,
|
|
|
|
log: zap.L(),
|
|
|
|
workerPoolInit: func(int) util.WorkerPool {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *gc) init() {
|
|
|
|
sz := 0
|
|
|
|
|
|
|
|
for _, v := range gc.mEventHandler {
|
|
|
|
sz += len(v.handlers)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sz > 0 {
|
|
|
|
gc.workerPool = gc.workerPoolInit(sz)
|
|
|
|
}
|
|
|
|
|
|
|
|
go gc.tickRemover()
|
|
|
|
go gc.listenEvents()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *gc) listenEvents() {
|
|
|
|
eventChan := gc.eventChanInit()
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, ok := <-eventChan
|
|
|
|
if !ok {
|
|
|
|
gc.log.Warn("stop event listener by closed channel")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := gc.mEventHandler[event.typ()]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v.cancelFunc()
|
2021-02-16 12:17:37 +00:00
|
|
|
v.prevGroup.Wait()
|
2021-02-16 11:34:48 +00:00
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
ctx, v.cancelFunc = context.WithCancel(context.Background())
|
|
|
|
|
2021-02-16 12:17:37 +00:00
|
|
|
v.prevGroup.Add(len(v.handlers))
|
|
|
|
|
2021-02-19 08:14:51 +00:00
|
|
|
for i := range v.handlers {
|
|
|
|
h := v.handlers[i]
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
err := gc.workerPool.Submit(func() {
|
|
|
|
h(ctx, event)
|
2021-02-16 12:17:37 +00:00
|
|
|
v.prevGroup.Done()
|
2021-02-16 11:34:48 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
gc.log.Warn("could not submit GC job to worker pool",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-02-16 12:17:37 +00:00
|
|
|
|
|
|
|
v.prevGroup.Done()
|
2021-02-16 11:34:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *gc) tickRemover() {
|
|
|
|
timer := time.NewTimer(gc.removerInterval)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for {
|
2021-08-04 12:04:37 +00:00
|
|
|
select {
|
|
|
|
case <-gc.stopChannel:
|
2021-10-19 13:34:34 +00:00
|
|
|
if gc.workerPool != nil {
|
|
|
|
gc.workerPool.Release()
|
|
|
|
}
|
2021-08-04 12:04:37 +00:00
|
|
|
gc.log.Debug("GC is stopped")
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
gc.remover()
|
|
|
|
timer.Reset(gc.removerInterval)
|
|
|
|
}
|
2021-02-16 11:34:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
func (gc *gc) stop() {
|
2021-11-11 13:54:36 +00:00
|
|
|
gc.onceStop.Do(func() {
|
|
|
|
gc.stopChannel <- struct{}{}
|
|
|
|
})
|
2021-08-04 12:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
// iterates over metabase graveyard and deletes objects
|
|
|
|
// with GC-marked graves.
|
2021-12-27 11:04:07 +00:00
|
|
|
// Does nothing if shard is in "read-only" mode.
|
2021-02-16 11:34:48 +00:00
|
|
|
func (s *Shard) removeGarbage() {
|
2021-12-27 18:07:02 +00:00
|
|
|
if s.GetMode() == ModeReadOnly {
|
2021-12-27 11:04:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
buf := make([]*addressSDK.Address, 0, s.rmBatchSize)
|
2021-02-16 11:34:48 +00:00
|
|
|
|
|
|
|
// iterate over metabase graveyard and accumulate
|
2021-02-19 08:24:10 +00:00
|
|
|
// objects with GC mark (no more the s.rmBatchSize objects)
|
2021-02-16 11:34:48 +00:00
|
|
|
err := s.metaBase.IterateOverGraveyard(func(g *meta.Grave) error {
|
|
|
|
if g.WithGCMark() {
|
|
|
|
buf = append(buf, g.Address())
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:24:10 +00:00
|
|
|
if len(buf) == s.rmBatchSize {
|
|
|
|
return meta.ErrInterruptIterator
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("iterator over metabase graveyard failed",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete accumulated objects
|
|
|
|
_, err = s.Delete(new(DeletePrm).
|
|
|
|
WithAddresses(buf...),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("could not delete the objects",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 17:34:45 +00:00
|
|
|
|
|
|
|
func (s *Shard) collectExpiredObjects(ctx context.Context, e Event) {
|
2022-01-19 12:12:34 +00:00
|
|
|
expired, err := s.getExpiredObjects(ctx, e.(newEpoch).epoch, false)
|
|
|
|
if err != nil || len(expired) == 0 {
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("iterator over expired objects failed", zap.String("error", err.Error()))
|
2021-02-16 17:34:45 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 12:27:40 +00:00
|
|
|
|
|
|
|
func (s *Shard) collectExpiredTombstones(ctx context.Context, e Event) {
|
2022-01-19 12:12:34 +00:00
|
|
|
expired, err := s.getExpiredObjects(ctx, e.(newEpoch).epoch, true)
|
|
|
|
if err != nil || len(expired) == 0 {
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("iterator over expired tombstones failes", zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.expiredTombstonesCallback(ctx, expired)
|
|
|
|
}
|
2021-02-17 12:27:40 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (s *Shard) getExpiredObjects(ctx context.Context, epoch uint64, collectTombstones bool) ([]*addressSDK.Address, error) {
|
|
|
|
var expired []*addressSDK.Address
|
2021-02-17 12:27:40 +00:00
|
|
|
|
|
|
|
err := s.metaBase.IterateExpired(epoch, func(expiredObject *meta.ExpiredObject) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return meta.ErrInterruptIterator
|
|
|
|
default:
|
2022-01-19 12:12:34 +00:00
|
|
|
if (expiredObject.Type() == object.TypeTombstone) == collectTombstones {
|
|
|
|
expired = append(expired, expiredObject.Address())
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-17 12:27:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-01-19 12:12:34 +00:00
|
|
|
return nil, err
|
2021-02-17 12:27:40 +00:00
|
|
|
}
|
2022-01-19 12:12:34 +00:00
|
|
|
return expired, ctx.Err()
|
2021-02-17 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 13:23:21 +00:00
|
|
|
// HandleExpiredTombstones marks to be removed all objects that are
|
|
|
|
// protected by tombstones with string addresses from tss.
|
|
|
|
// If successful, marks tombstones themselves as garbage.
|
2021-02-17 12:27:40 +00:00
|
|
|
//
|
|
|
|
// Does not modify tss.
|
2022-02-07 15:08:00 +00:00
|
|
|
func (s *Shard) HandleExpiredTombstones(tss map[string]*addressSDK.Address) {
|
2022-01-26 12:11:13 +00:00
|
|
|
inhume := make([]*addressSDK.Address, 0, len(tss))
|
2021-02-17 12:27:40 +00:00
|
|
|
|
2021-10-14 13:23:21 +00:00
|
|
|
// Collect all objects covered by the tombstones.
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
err := s.metaBase.IterateCoveredByTombstones(tss, func(addr *addressSDK.Address) error {
|
2021-02-17 12:27:40 +00:00
|
|
|
inhume = append(inhume, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("iterator over expired objects failed",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-14 13:23:21 +00:00
|
|
|
// Mark collected objects as garbage.
|
|
|
|
|
|
|
|
var pInhume meta.InhumePrm
|
|
|
|
|
|
|
|
pInhume.WithGCMark()
|
|
|
|
|
|
|
|
if len(inhume) > 0 {
|
|
|
|
// inhume objects
|
|
|
|
pInhume.WithAddresses(inhume...)
|
|
|
|
|
|
|
|
_, err = s.metaBase.Inhume(&pInhume)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("could not inhume objects under the expired tombstone",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the tombstones as garbage.
|
|
|
|
|
|
|
|
inhume = inhume[:0]
|
|
|
|
|
2022-02-07 15:08:00 +00:00
|
|
|
for _, addr := range tss {
|
2021-10-14 13:23:21 +00:00
|
|
|
inhume = append(inhume, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
pInhume.WithAddresses(inhume...) // GC mark is already set above
|
|
|
|
|
|
|
|
// inhume tombstones
|
|
|
|
_, err = s.metaBase.Inhume(&pInhume)
|
2021-02-17 12:27:40 +00:00
|
|
|
if err != nil {
|
2021-10-14 13:23:21 +00:00
|
|
|
s.log.Warn("could not mark tombstones as garbage",
|
2021-02-17 12:27:40 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|