2019-07-22 14:34:12 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-09-12 11:38:49 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-07-22 14:34:12 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Observer
|
|
|
|
ctx context.Context
|
|
|
|
done chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
join chan *observerContext
|
|
|
|
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,
|
|
|
|
join: make(chan *observerContext),
|
|
|
|
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.
|
|
|
|
func (loop *Loop) Join(ctx context.Context, observer Observer) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
obsContext := &observerContext{
|
|
|
|
Observer: observer,
|
|
|
|
ctx: ctx,
|
|
|
|
done: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case loop.join <- obsContext:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-loop.done:
|
|
|
|
return LoopClosedError
|
|
|
|
}
|
|
|
|
|
|
|
|
return obsContext.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
case observer := <-loop.join:
|
|
|
|
observers = append(observers, observer)
|
|
|
|
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 {
|
|
|
|
case observer := <-loop.join:
|
|
|
|
observers = append(observers, observer)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:26:48 +00:00
|
|
|
return iterateDatabase(ctx, loop.db, observers)
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:24:17 +00:00
|
|
|
// IterateDatabase iterates over PointerDB and notifies specified observers about results.
|
2019-11-18 15:26:48 +00:00
|
|
|
func IterateDatabase(ctx context.Context, db PointerDB, observers ...Observer) error {
|
|
|
|
obsContexts := make([]*observerContext, len(observers))
|
|
|
|
for i, observer := range observers {
|
|
|
|
obsContexts[i] = &observerContext{
|
|
|
|
Observer: observer,
|
|
|
|
ctx: ctx,
|
|
|
|
done: make(chan error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iterateDatabase(ctx, db, obsContexts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func iterateDatabase(ctx context.Context, db PointerDB, observers []*observerContext) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, observer := range observers {
|
|
|
|
observer.HandleError(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finishObservers(observers)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = db.Iterate(ctx, storage.IterateOptions{Recurse: true},
|
2019-07-22 14:34:12 +01:00
|
|
|
func(ctx context.Context, it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
|
|
|
|
|
|
|
// iterate over every segment in metainfo
|
2019-11-20 16:37:48 +00:00
|
|
|
nextSegment:
|
2019-07-22 14:34:12 +01:00
|
|
|
for it.Next(ctx, &item) {
|
2019-09-12 11:38:49 +01:00
|
|
|
rawPath := item.Key.String()
|
2019-07-22 14:34:12 +01:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
|
2019-11-18 15:26:48 +00:00
|
|
|
err := proto.Unmarshal(item.Value, pointer)
|
2019-07-22 14:34:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return LoopError.New("unexpected error unmarshalling pointer %s", err)
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:38:49 +01:00
|
|
|
pathElements := storj.SplitPath(rawPath)
|
2019-11-18 15:26:48 +00:00
|
|
|
|
|
|
|
if len(pathElements) < 4 {
|
2019-11-20 16:29:52 +00:00
|
|
|
// We skip this path because it belongs to bucket metadata no an
|
|
|
|
// actual object
|
2019-11-20 16:37:48 +00:00
|
|
|
continue nextSegment
|
2019-09-12 11:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isLastSegment := pathElements[1] == "l"
|
2019-07-22 14:34:12 +01:00
|
|
|
|
2019-09-12 11:38:49 +01:00
|
|
|
path := ScopedPath{
|
2019-11-18 15:26:48 +00:00
|
|
|
Raw: rawPath,
|
|
|
|
ProjectIDString: pathElements[0],
|
|
|
|
Segment: pathElements[1],
|
|
|
|
BucketName: pathElements[2],
|
|
|
|
EncryptedObjectPath: storj.JoinPaths(pathElements[3:]...),
|
2019-09-12 11:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.Parse(path.ProjectIDString)
|
|
|
|
if err != nil {
|
|
|
|
return LoopError.Wrap(err)
|
|
|
|
}
|
|
|
|
path.ProjectID = *projectID
|
|
|
|
|
|
|
|
nextObservers := observers[:0]
|
2019-07-22 14:34:12 +01:00
|
|
|
for _, observer := range observers {
|
2019-09-12 11:38:49 +01:00
|
|
|
keepObserver := handlePointer(ctx, observer, path, isLastSegment, pointer)
|
2019-07-22 14:34:12 +01:00
|
|
|
if keepObserver {
|
|
|
|
nextObservers = append(nextObservers, observer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
observers = nextObservers
|
|
|
|
if len(observers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if context has been canceled exit. Otherwise, continue
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|