2022-10-26 08:02:13 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rangedloop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-08 14:27:21 +00:00
|
|
|
"time"
|
2022-10-26 08:02:13 +01:00
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configurable values for the shared loop.
|
2022-12-08 14:27:21 +00:00
|
|
|
type Config struct {
|
|
|
|
Parallelism int `help:"how many chunks of segments to process in parallel" default:"2"`
|
|
|
|
BatchSize int `help:"how many items to query in a batch" default:"2500"`
|
|
|
|
AsOfSystemInterval time.Duration `help:"as of system interval" releaseDefault:"-5m" devDefault:"-1us" testDefault:"-1us"`
|
|
|
|
}
|
2022-10-26 08:02:13 +01:00
|
|
|
|
|
|
|
// Service iterates through all segments and calls the attached observers for every segment
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
2022-12-08 14:27:21 +00:00
|
|
|
log *zap.Logger
|
|
|
|
config Config
|
|
|
|
provider RangeSplitter
|
|
|
|
observers []Observer
|
2022-10-26 08:02:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new instance of the ranged loop service.
|
2022-12-08 14:27:21 +00:00
|
|
|
func NewService(log *zap.Logger, config Config, provider RangeSplitter, observers []Observer) *Service {
|
2022-10-26 08:02:13 +01:00
|
|
|
return &Service{
|
2022-12-08 14:27:21 +00:00
|
|
|
log: log,
|
|
|
|
config: config,
|
|
|
|
provider: provider,
|
|
|
|
observers: observers,
|
2022-10-26 08:02:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 14:27:21 +00:00
|
|
|
// observerState contains information to manage an observer during a loop iteration.
|
|
|
|
// Improvement: track duration.
|
|
|
|
type observerState struct {
|
|
|
|
observer Observer
|
2022-12-21 20:58:08 +00:00
|
|
|
rangeObservers []*rangeObserverState
|
|
|
|
}
|
|
|
|
|
|
|
|
type rangeObserverState struct {
|
|
|
|
rangeObserver Partial
|
|
|
|
duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObserverDuration reports back on how long it took the observer to process all the segments.
|
|
|
|
type ObserverDuration struct {
|
|
|
|
Observer Observer
|
|
|
|
Duration time.Duration
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 08:02:13 +01:00
|
|
|
// Run starts the looping service.
|
|
|
|
func (service *Service) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for {
|
2022-12-21 20:58:08 +00:00
|
|
|
_, err := service.RunOnce(ctx)
|
|
|
|
if err != nil {
|
2022-10-26 08:02:13 +01:00
|
|
|
service.log.Error("ranged loop failure", zap.Error(err))
|
|
|
|
|
|
|
|
if errs2.IsCanceled(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return errs.Combine(err, ctx.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
mon.Event("rangedloop_error") //mon:locked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunOnce goes through one time and sends information to observers.
|
2022-12-21 20:58:08 +00:00
|
|
|
func (service *Service) RunOnce(ctx context.Context) (observerDurations []ObserverDuration, err error) {
|
2022-10-26 08:02:13 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
observerStates, err := startObservers(ctx, service.observers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rangeProviders, err := service.provider.CreateRanges(service.config.Parallelism, service.config.BatchSize)
|
|
|
|
if err != nil {
|
2022-12-21 20:58:08 +00:00
|
|
|
return nil, err
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group := errs2.Group{}
|
2022-12-21 20:58:08 +00:00
|
|
|
for _, rangeProvider := range rangeProviders {
|
|
|
|
rangeObservers := []*rangeObserverState{}
|
2022-12-08 14:27:21 +00:00
|
|
|
for i, observerState := range observerStates {
|
|
|
|
rangeObserver, err := observerState.observer.Fork(ctx)
|
|
|
|
if err != nil {
|
2022-12-21 20:58:08 +00:00
|
|
|
return nil, err
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
2022-12-21 20:58:08 +00:00
|
|
|
rangeState := &rangeObserverState{
|
|
|
|
rangeObserver: rangeObserver,
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
2022-12-21 20:58:08 +00:00
|
|
|
rangeObservers = append(rangeObservers, rangeState)
|
|
|
|
observerStates[i].rangeObservers = append(observerStates[i].rangeObservers, rangeState)
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
// Create closure to capture loop variables.
|
|
|
|
group.Go(createGoroutineClosure(ctx, rangeProvider, rangeObservers))
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Improvement: stop all ranges when one has an error.
|
|
|
|
errList := group.Wait()
|
|
|
|
if errList != nil {
|
2022-12-21 20:58:08 +00:00
|
|
|
return nil, errs.Combine(errList...)
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
return finishObservers(ctx, observerStates)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createGoroutineClosure(ctx context.Context, rangeProvider SegmentProvider, states []*rangeObserverState) func() error {
|
|
|
|
return func() (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return rangeProvider.Iterate(ctx, func(segments []segmentloop.Segment) error {
|
|
|
|
for _, state := range states {
|
|
|
|
start := time.Now()
|
|
|
|
err := state.rangeObserver.Process(ctx, segments)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state.duration += time.Since(start)
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
2022-12-21 20:58:08 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startObservers(ctx context.Context, observers []Observer) (observerStates []observerState, err error) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
for _, obs := range observers {
|
|
|
|
state, err := startObserver(ctx, startTime, obs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
observerStates = append(observerStates, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
return observerStates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startObserver(ctx context.Context, startTime time.Time, observer Observer) (observerState, error) {
|
|
|
|
err := observer.Start(ctx, startTime)
|
|
|
|
|
|
|
|
return observerState{
|
|
|
|
observer: observer,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func finishObservers(ctx context.Context, observerStates []observerState) (observerDurations []ObserverDuration, err error) {
|
|
|
|
for _, state := range observerStates {
|
|
|
|
observerDuration, err := finishObserver(ctx, state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
observerDurations = append(observerDurations, observerDuration)
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:16:47 +00:00
|
|
|
sendObserverDurations(observerDurations)
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
return observerDurations, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the segments is done.
|
|
|
|
// This is the reduce step.
|
|
|
|
func finishObserver(ctx context.Context, state observerState) (ObserverDuration, error) {
|
|
|
|
var duration time.Duration
|
|
|
|
for _, rangeObserver := range state.rangeObservers {
|
|
|
|
err := state.observer.Join(ctx, rangeObserver.rangeObserver)
|
2022-12-08 14:27:21 +00:00
|
|
|
if err != nil {
|
2022-12-21 20:58:08 +00:00
|
|
|
return ObserverDuration{}, err
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
2022-12-21 20:58:08 +00:00
|
|
|
duration += rangeObserver.duration
|
2022-12-08 14:27:21 +00:00
|
|
|
}
|
2022-10-26 08:02:13 +01:00
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
return ObserverDuration{
|
|
|
|
Duration: duration,
|
|
|
|
Observer: state.observer,
|
|
|
|
}, state.observer.Finish(ctx)
|
2022-10-26 08:02:13 +01:00
|
|
|
}
|