ff6d640fca
Minimal implementation of the ranged (=threaded) segment loop service, to improve performance over the existing loop. Has tests with a an inmemory segment database and example observer. Does not have yet: database link, observer duration tracking, suspicious processed ratio guard, rate limiting, minimum execution interval per observer, etc. Part of https://github.com/storj/storj/issues/5223 Change-Id: I08ffb392c3539e380f4e7b4f1afd56c4c394668d
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
// Copyright (C) 2022 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package rangedlooptest
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"storj.io/storj/satellite/metabase/rangedloop"
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
|
)
|
|
|
|
var _ rangedloop.Observer = (*CountObserver)(nil)
|
|
var _ rangedloop.Partial = (*CountObserver)(nil)
|
|
|
|
// CountObserver is a subscriber to the ranged segment loop which counts the number of segments.
|
|
type CountObserver struct {
|
|
NumSegments int
|
|
}
|
|
|
|
// Start is the callback for segment loop start.
|
|
func (c *CountObserver) Start(ctx context.Context, time time.Time) error {
|
|
c.NumSegments = 0
|
|
return nil
|
|
}
|
|
|
|
// Fork splits the observer to count ranges of segments.
|
|
func (c *CountObserver) Fork(ctx context.Context) (rangedloop.Partial, error) {
|
|
// return new instance for threadsafety
|
|
return &CountObserver{}, nil
|
|
}
|
|
|
|
// Join adds the count of all the ranges together.
|
|
func (c *CountObserver) Join(ctx context.Context, partial rangedloop.Partial) error {
|
|
countPartial := partial.(*CountObserver)
|
|
c.NumSegments += countPartial.NumSegments
|
|
// Range done
|
|
return nil
|
|
}
|
|
|
|
// Finish is the callback for ranged segment loop end.
|
|
func (c *CountObserver) Finish(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// Process counts the size of a batch of segments.
|
|
func (c *CountObserver) Process(ctx context.Context, segments []segmentloop.Segment) error {
|
|
c.NumSegments += len(segments)
|
|
return nil
|
|
}
|