2019-07-22 14:34:12 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-25 14:16:44 +00:00
|
|
|
"fmt"
|
2019-07-22 14:34:12 +01:00
|
|
|
"time"
|
|
|
|
|
2020-02-25 14:16:44 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-07-22 14:34:12 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-12-19 18:33:59 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-07-22 14:34:12 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// LoopError is a standard error class for this component.
|
|
|
|
LoopError = errs.Class("metainfo loop error")
|
2019-11-21 20:24:17 +00:00
|
|
|
// LoopClosedError is a loop closed error.
|
2019-07-22 14:34:12 +01:00
|
|
|
LoopClosedError = LoopError.New("loop closed")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Observer is an interface defining an observer that can subscribe to the metainfo loop.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Observer
|
2019-07-22 14:34:12 +01:00
|
|
|
type Observer interface {
|
2019-09-13 14:51:41 +01:00
|
|
|
Object(context.Context, ScopedPath, *pb.Pointer) error
|
2019-09-12 11:38:49 +01:00
|
|
|
RemoteSegment(context.Context, ScopedPath, *pb.Pointer) error
|
|
|
|
InlineSegment(context.Context, ScopedPath, *pb.Pointer) error
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:01:39 +00:00
|
|
|
// NullObserver is an observer that does nothing. This is useful for joining
|
|
|
|
// and ensuring the metainfo loop runs once before you use a real observer
|
|
|
|
type NullObserver struct{}
|
|
|
|
|
|
|
|
// Object implements the Observer interface
|
|
|
|
func (NullObserver) Object(context.Context, ScopedPath, *pb.Pointer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteSegment implements the Observer interface
|
|
|
|
func (NullObserver) RemoteSegment(context.Context, ScopedPath, *pb.Pointer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InlineSegment implements the Observer interface
|
|
|
|
func (NullObserver) InlineSegment(context.Context, ScopedPath, *pb.Pointer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:24:17 +00:00
|
|
|
// ScopedPath contains full expanded information about the path.
|
2019-09-12 11:38:49 +01:00
|
|
|
type ScopedPath struct {
|
2019-11-18 15:26:48 +00:00
|
|
|
ProjectID uuid.UUID
|
|
|
|
ProjectIDString string
|
|
|
|
Segment string
|
|
|
|
BucketName string
|
|
|
|
EncryptedObjectPath string
|
2019-09-12 11:38:49 +01:00
|
|
|
|
|
|
|
// TODO: should these be a []byte?
|
|
|
|
|
|
|
|
// Raw is the same path as pointerDB is using.
|
|
|
|
Raw storj.Path
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type observerContext struct {
|
2020-02-25 14:16:44 +00:00
|
|
|
observer Observer
|
|
|
|
|
2019-07-22 14:34:12 +01:00
|
|
|
ctx context.Context
|
|
|
|
done chan error
|
2020-02-25 14:16:44 +00:00
|
|
|
|
|
|
|
object *monkit.DurationDist
|
|
|
|
remote *monkit.DurationDist
|
|
|
|
inline *monkit.DurationDist
|
|
|
|
}
|
|
|
|
|
|
|
|
func newObserverContext(ctx context.Context, obs Observer) *observerContext {
|
|
|
|
name := fmt.Sprintf("%T", obs)
|
|
|
|
key := monkit.NewSeriesKey("observer").WithTag("name", name)
|
|
|
|
|
|
|
|
return &observerContext{
|
|
|
|
observer: obs,
|
|
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
done: make(chan error),
|
|
|
|
|
|
|
|
object: monkit.NewDurationDist(key.WithTag("pointer_type", "object")),
|
|
|
|
inline: monkit.NewDurationDist(key.WithTag("pointer_type", "inline")),
|
|
|
|
remote: monkit.NewDurationDist(key.WithTag("pointer_type", "remote")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) Object(ctx context.Context, path ScopedPath, pointer *pb.Pointer) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() { observer.object.Insert(time.Since(start)) }()
|
|
|
|
|
|
|
|
return observer.observer.Object(ctx, path, pointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) RemoteSegment(ctx context.Context, path ScopedPath, pointer *pb.Pointer) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() { observer.remote.Insert(time.Since(start)) }()
|
|
|
|
|
|
|
|
return observer.observer.RemoteSegment(ctx, path, pointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) InlineSegment(ctx context.Context, path ScopedPath, pointer *pb.Pointer) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() { observer.inline.Insert(time.Since(start)) }()
|
|
|
|
|
|
|
|
return observer.observer.InlineSegment(ctx, path, pointer)
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) HandleError(err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
observer.done <- err
|
|
|
|
observer.Finish()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) Finish() {
|
|
|
|
close(observer.done)
|
2020-02-25 14:16:44 +00:00
|
|
|
|
|
|
|
name := fmt.Sprintf("%T", observer.observer)
|
|
|
|
stats := allObserverStatsCollectors.GetStats(name)
|
|
|
|
stats.Observe(observer)
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (observer *observerContext) Wait() error {
|
|
|
|
return <-observer.done
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoopConfig contains configurable values for the metainfo loop.
|
|
|
|
type LoopConfig struct {
|
|
|
|
CoalesceDuration time.Duration `help:"how long to wait for new observers before starting iteration" releaseDefault:"5s" devDefault:"5s"`
|
2020-05-05 07:51:24 +01:00
|
|
|
RateLimit float64 `help:"rate limit (default is 0 which is unlimited segments per second)" default:"0"`
|
|
|
|
ListLimit int `help:"how many items to query in a batch" default:"10000"`
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop is a metainfo loop service.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-07-22 14:34:12 +01:00
|
|
|
type Loop struct {
|
2019-10-08 15:39:23 +01:00
|
|
|
config LoopConfig
|
|
|
|
db PointerDB
|
2020-04-16 08:30:20 +01:00
|
|
|
join chan []*observerContext
|
2019-10-08 15:39:23 +01:00
|
|
|
done chan struct{}
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoop creates a new metainfo loop service.
|
2019-10-08 15:39:23 +01:00
|
|
|
func NewLoop(config LoopConfig, db PointerDB) *Loop {
|
2019-07-22 14:34:12 +01:00
|
|
|
return &Loop{
|
2019-10-08 15:39:23 +01:00
|
|
|
db: db,
|
|
|
|
config: config,
|
2020-04-16 08:30:20 +01:00
|
|
|
join: make(chan []*observerContext),
|
2019-10-08 15:39:23 +01:00
|
|
|
done: make(chan struct{}),
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join will join the looper for one full cycle until completion and then returns.
|
|
|
|
// On ctx cancel the observer will return without completely finishing.
|
|
|
|
// Only on full complete iteration it will return nil.
|
|
|
|
// Safe to be called concurrently.
|
2020-04-16 08:30:20 +01:00
|
|
|
func (loop *Loop) Join(ctx context.Context, observers ...Observer) (err error) {
|
2019-07-22 14:34:12 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-04-16 08:30:20 +01:00
|
|
|
obsContexts := make([]*observerContext, len(observers))
|
|
|
|
for i, obs := range observers {
|
|
|
|
obsContexts[i] = newObserverContext(ctx, obs)
|
|
|
|
}
|
2019-07-22 14:34:12 +01:00
|
|
|
|
|
|
|
select {
|
2020-04-16 08:30:20 +01:00
|
|
|
case loop.join <- obsContexts:
|
2019-07-22 14:34:12 +01:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-loop.done:
|
|
|
|
return LoopClosedError
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:30:20 +01:00
|
|
|
var errList errs.Group
|
|
|
|
for _, ctx := range obsContexts {
|
|
|
|
errList.Add(ctx.Wait())
|
|
|
|
}
|
|
|
|
|
|
|
|
return errList.Err()
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the looping service.
|
|
|
|
// It can only be called once, otherwise a panic will occur.
|
|
|
|
func (loop *Loop) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := loop.runOnce(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:14:39 +01:00
|
|
|
// Close closes the looping services.
|
|
|
|
func (loop *Loop) Close() (err error) {
|
|
|
|
close(loop.done)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 14:34:12 +01:00
|
|
|
// runOnce goes through metainfo one time and sends information to observers.
|
|
|
|
func (loop *Loop) runOnce(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var observers []*observerContext
|
|
|
|
|
|
|
|
// wait for the first observer, or exit because context is canceled
|
|
|
|
select {
|
2020-04-16 08:30:20 +01:00
|
|
|
case list := <-loop.join:
|
|
|
|
observers = append(observers, list...)
|
2019-07-22 14:34:12 +01:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// after the first observer is found, set timer for CoalesceDuration and add any observers that try to join before the timer is up
|
|
|
|
timer := time.NewTimer(loop.config.CoalesceDuration)
|
|
|
|
waitformore:
|
|
|
|
for {
|
|
|
|
select {
|
2020-04-16 08:30:20 +01:00
|
|
|
case list := <-loop.join:
|
|
|
|
observers = append(observers, list...)
|
2019-07-22 14:34:12 +01:00
|
|
|
case <-timer.C:
|
|
|
|
break waitformore
|
|
|
|
case <-ctx.Done():
|
2019-11-18 15:26:48 +00:00
|
|
|
finishObservers(observers)
|
2019-07-22 14:34:12 +01:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 07:51:24 +01:00
|
|
|
return iterateDatabase(ctx, loop.db, observers, loop.config.ListLimit, rate.NewLimiter(rate.Limit(loop.config.RateLimit), 1))
|
2019-11-18 15:26:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:24:17 +00:00
|
|
|
// IterateDatabase iterates over PointerDB and notifies specified observers about results.
|
2020-05-05 07:51:24 +01:00
|
|
|
//
|
|
|
|
// It uses 10000 as the lookup limit for iterating.
|
2019-12-19 18:33:59 +00:00
|
|
|
func IterateDatabase(ctx context.Context, rateLimit float64, db PointerDB, observers ...Observer) error {
|
2019-11-18 15:26:48 +00:00
|
|
|
obsContexts := make([]*observerContext, len(observers))
|
|
|
|
for i, observer := range observers {
|
2020-02-25 14:16:44 +00:00
|
|
|
obsContexts[i] = newObserverContext(ctx, observer)
|
2019-11-18 15:26:48 +00:00
|
|
|
}
|
2020-05-05 07:51:24 +01:00
|
|
|
return iterateDatabase(ctx, db, obsContexts, 10000, rate.NewLimiter(rate.Limit(rateLimit), 1))
|
2019-11-18 15:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlePointer deals with a pointer for a single observer
|
2019-11-21 20:24:17 +00:00
|
|
|
// if there is some error on the observer, handles the error and returns false. Otherwise, returns true.
|
2019-11-18 15:26:48 +00:00
|
|
|
func handlePointer(ctx context.Context, observer *observerContext, path ScopedPath, isLastSegment bool, pointer *pb.Pointer) bool {
|
|
|
|
switch pointer.GetType() {
|
|
|
|
case pb.Pointer_REMOTE:
|
|
|
|
if observer.HandleError(observer.RemoteSegment(ctx, path, pointer)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case pb.Pointer_INLINE:
|
|
|
|
if observer.HandleError(observer.InlineSegment(ctx, path, pointer)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if isLastSegment {
|
|
|
|
if observer.HandleError(observer.Object(ctx, path, pointer)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-observer.ctx.Done():
|
|
|
|
observer.HandleError(observer.ctx.Err())
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for run to be finished.
|
|
|
|
// Safe to be called concurrently.
|
|
|
|
func (loop *Loop) Wait() {
|
|
|
|
<-loop.done
|
|
|
|
}
|
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
func iterateDatabase(ctx context.Context, db PointerDB, observers []*observerContext, limit int, rateLimiter *rate.Limiter) (err error) {
|
2019-11-18 15:26:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, observer := range observers {
|
|
|
|
observer.HandleError(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finishObservers(observers)
|
|
|
|
}()
|
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
err = db.IterateWithoutLookupLimit(ctx, storage.IterateOptions{
|
|
|
|
Recurse: true,
|
|
|
|
Limit: limit,
|
|
|
|
}, func(ctx context.Context, it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
|
|
|
|
|
|
|
// iterate over every segment in metainfo
|
|
|
|
nextSegment:
|
|
|
|
for it.Next(ctx, &item) {
|
|
|
|
if err := rateLimiter.Wait(ctx); err != nil {
|
|
|
|
// We don't really execute concurrent batches so we should never
|
|
|
|
// exceed the burst size of 1 and this should never happen.
|
|
|
|
// We can also enter here if the context is cancelled.
|
|
|
|
return LoopError.Wrap(err)
|
|
|
|
}
|
2019-12-19 18:33:59 +00:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
rawPath := item.Key.String()
|
|
|
|
pointer := &pb.Pointer{}
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
err := pb.Unmarshal(item.Value, pointer)
|
|
|
|
if err != nil {
|
|
|
|
return LoopError.New("unexpected error unmarshalling pointer %s", err)
|
|
|
|
}
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
pathElements := storj.SplitPath(rawPath)
|
2019-11-18 15:26:48 +00:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
if len(pathElements) < 4 {
|
|
|
|
// We skip this path because it belongs to bucket metadata, not to an
|
|
|
|
// actual object
|
|
|
|
continue nextSegment
|
|
|
|
}
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
isLastSegment := pathElements[1] == "l"
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
path := ScopedPath{
|
|
|
|
Raw: rawPath,
|
|
|
|
ProjectIDString: pathElements[0],
|
|
|
|
Segment: pathElements[1],
|
|
|
|
BucketName: pathElements[2],
|
|
|
|
EncryptedObjectPath: storj.JoinPaths(pathElements[3:]...),
|
|
|
|
}
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
path.ProjectID, err = uuid.FromString(path.ProjectIDString)
|
|
|
|
if err != nil {
|
|
|
|
return LoopError.Wrap(err)
|
|
|
|
}
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
nextObservers := observers[:0]
|
|
|
|
for _, observer := range observers {
|
|
|
|
keepObserver := handlePointer(ctx, observer, path, isLastSegment, pointer)
|
|
|
|
if keepObserver {
|
|
|
|
nextObservers = append(nextObservers, observer)
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
2020-05-05 07:51:24 +01:00
|
|
|
}
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
observers = nextObservers
|
|
|
|
if len(observers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2020-05-05 07:51:24 +01:00
|
|
|
// if context has been canceled exit. Otherwise, continue
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
2020-05-05 07:51:24 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-07-22 14:34:12 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:26:48 +00:00
|
|
|
func finishObservers(observers []*observerContext) {
|
|
|
|
for _, observer := range observers {
|
|
|
|
observer.Finish()
|
2019-07-22 14:34:12 +01:00
|
|
|
}
|
|
|
|
}
|