2019-08-21 16:49:27 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
|
2023-02-17 09:51:57 +00:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-06-14 16:40:46 +01:00
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
2019-08-21 16:49:27 +01:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
var _ segmentloop.Observer = (*Collector)(nil)
|
2019-09-10 14:24:16 +01:00
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
// Collector uses the segment loop to add segments to node reservoirs.
|
2020-12-14 12:54:22 +00:00
|
|
|
type Collector struct {
|
2023-02-17 09:51:57 +00:00
|
|
|
Reservoirs map[metabase.NodeAlias]*Reservoir
|
2019-08-21 16:49:27 +01:00
|
|
|
slotCount int
|
|
|
|
rand *rand.Rand
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
// NewCollector instantiates a segment collector.
|
|
|
|
func NewCollector(reservoirSlots int, r *rand.Rand) *Collector {
|
|
|
|
return &Collector{
|
2023-02-17 09:51:57 +00:00
|
|
|
Reservoirs: make(map[metabase.NodeAlias]*Reservoir),
|
2019-08-21 16:49:27 +01:00
|
|
|
slotCount: reservoirSlots,
|
|
|
|
rand: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 11:56:39 +01:00
|
|
|
// LoopStarted is called at each start of a loop.
|
2021-06-14 16:40:46 +01:00
|
|
|
func (collector *Collector) LoopStarted(context.Context, segmentloop.LoopInfo) (err error) {
|
2021-04-01 11:56:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RemoteSegment takes a remote segment found in metainfo and creates a reservoir for it if it doesn't exist already.
|
2022-05-04 17:31:53 +01:00
|
|
|
func (collector *Collector) RemoteSegment(ctx context.Context, segment *segmentloop.Segment) error {
|
2022-09-23 15:00:56 +01:00
|
|
|
// we are expliticy not adding monitoring here as we are tracking loop observers separately
|
2021-06-15 00:21:20 +01:00
|
|
|
|
2023-02-17 09:51:57 +00:00
|
|
|
for _, piece := range segment.AliasPieces {
|
|
|
|
res, ok := collector.Reservoirs[piece.Alias]
|
2022-05-04 17:31:53 +01:00
|
|
|
if !ok {
|
|
|
|
res = NewReservoir(collector.slotCount)
|
2023-02-17 09:51:57 +00:00
|
|
|
collector.Reservoirs[piece.Alias] = res
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
2022-09-23 15:00:56 +01:00
|
|
|
res.Sample(collector.rand, segment)
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// InlineSegment returns nil because we're only auditing for storage nodes for now.
|
2021-06-14 16:40:46 +01:00
|
|
|
func (collector *Collector) InlineSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
|
2019-08-21 16:49:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-15 00:37:37 +00:00
|
|
|
|
|
|
|
// Process performs per-node reservoir sampling on remote segments for addition into the audit queue.
|
|
|
|
func (collector *Collector) Process(ctx context.Context, segments []segmentloop.Segment) (err error) {
|
|
|
|
for _, segment := range segments {
|
|
|
|
// The reservoir ends up deferencing and copying the segment internally
|
|
|
|
// but that's not obvious, so alias the loop variable.
|
|
|
|
segment := segment
|
|
|
|
if segment.Inline() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := collector.RemoteSegment(ctx, &segment); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|