2022-11-15 10:34:42 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rangedloop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-05-09 12:13:19 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2022-11-15 10:34:42 +00:00
|
|
|
)
|
|
|
|
|
2023-05-09 12:13:19 +01:00
|
|
|
// Segment contains information about segment metadata which will be received by observers.
|
|
|
|
type Segment metabase.LoopSegmentEntry
|
|
|
|
|
|
|
|
// Inline returns true if segment is inline.
|
|
|
|
func (s Segment) Inline() bool {
|
|
|
|
return s.Redundancy.IsZero() && len(s.Pieces) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired checks if segment expired relative to now.
|
|
|
|
func (s *Segment) Expired(now time.Time) bool {
|
|
|
|
return s.ExpiresAt != nil && s.ExpiresAt.Before(now)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PieceSize returns calculated piece size for segment.
|
|
|
|
func (s Segment) PieceSize() int64 {
|
|
|
|
return s.Redundancy.PieceSize(int64(s.EncryptedSize))
|
|
|
|
}
|
|
|
|
|
2022-11-15 10:34:42 +00:00
|
|
|
// Observer subscribes to the parallel segment loop.
|
|
|
|
// It is intended that a naïve implementation is threadsafe.
|
|
|
|
type Observer interface {
|
2022-12-06 15:09:42 +00:00
|
|
|
// Start is called at the beginning of each segment loop.
|
|
|
|
Start(context.Context, time.Time) error
|
2022-11-15 10:34:42 +00:00
|
|
|
|
2022-12-06 15:09:42 +00:00
|
|
|
// Fork creates a Partial to process a chunk of all the segments. It is
|
|
|
|
// called after Start. It is not called concurrently.
|
2022-11-15 10:34:42 +00:00
|
|
|
Fork(context.Context) (Partial, error)
|
2022-12-06 15:09:42 +00:00
|
|
|
|
|
|
|
// Join is called for each partial returned by Fork. This gives the
|
|
|
|
// opportunity to merge the output like in a reduce step. It will be called
|
|
|
|
// before Finish. It is not called concurrently.
|
2022-11-15 10:34:42 +00:00
|
|
|
Join(context.Context, Partial) error
|
|
|
|
|
|
|
|
// Finish is called after all segments are processed by all observers.
|
|
|
|
Finish(context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Partial processes a part of the total range of segments.
|
|
|
|
type Partial interface {
|
|
|
|
// Process is called repeatedly with batches of segments.
|
|
|
|
// It is not called concurrently on the same instance.
|
2023-05-09 12:13:19 +01:00
|
|
|
Process(context.Context, []Segment) error
|
2022-11-15 10:34:42 +00:00
|
|
|
}
|