2020-04-15 20:20:16 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package expireddeletion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/storj/satellite/metainfo"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ metainfo.Observer = (*expiredDeleter)(nil)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// expiredDeleter implements the metainfo loop observer interface for expired segment cleanup.
|
2020-04-15 20:20:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Observer
|
|
|
|
type expiredDeleter struct {
|
|
|
|
log *zap.Logger
|
|
|
|
metainfo *metainfo.Service
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RemoteSegment deletes the segment if it is expired.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (ed *expiredDeleter) RemoteSegment(ctx context.Context, segment *metainfo.Segment) (err error) {
|
2020-09-02 08:16:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-04-15 20:20:16 +01:00
|
|
|
|
2020-10-27 06:59:14 +00:00
|
|
|
return ed.deleteSegmentIfExpired(ctx, segment)
|
2020-04-15 20:20:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// InlineSegment deletes the segment if it is expired.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (ed *expiredDeleter) InlineSegment(ctx context.Context, segment *metainfo.Segment) (err error) {
|
2020-09-02 08:16:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-04-15 20:20:16 +01:00
|
|
|
|
2020-10-27 06:59:14 +00:00
|
|
|
return ed.deleteSegmentIfExpired(ctx, segment)
|
2020-04-15 20:20:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Object returns nil because the expired deleter only cares about segments.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (ed *expiredDeleter) Object(ctx context.Context, object *metainfo.Object) (err error) {
|
2020-04-15 20:20:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:59:14 +00:00
|
|
|
func (ed *expiredDeleter) deleteSegmentIfExpired(ctx context.Context, segment *metainfo.Segment) error {
|
|
|
|
if segment.Expired(time.Now()) {
|
|
|
|
pointerBytes, err := pb.Marshal(segment.Pointer)
|
2020-04-15 20:20:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 06:59:14 +00:00
|
|
|
err = ed.metainfo.Delete(ctx, segment.Location.Encode(), pointerBytes)
|
2020-04-15 20:20:16 +01:00
|
|
|
if storj.ErrObjectNotFound.Has(err) {
|
|
|
|
// segment already deleted
|
|
|
|
return nil
|
|
|
|
} else if storage.ErrValueChanged.Has(err) {
|
|
|
|
// segment was replaced
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|