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"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
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 {
|
2019-08-21 16:49:27 +01:00
|
|
|
Reservoirs map[storj.NodeID]*Reservoir
|
|
|
|
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{
|
2019-08-21 16:49:27 +01:00
|
|
|
Reservoirs: make(map[storj.NodeID]*Reservoir),
|
|
|
|
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.
|
2021-06-14 16:40:46 +01:00
|
|
|
func (collector *Collector) RemoteSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
|
2021-06-15 00:21:20 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-10-27 06:59:14 +00:00
|
|
|
for _, piece := range segment.Pieces {
|
|
|
|
if _, ok := collector.Reservoirs[piece.StorageNode]; !ok {
|
|
|
|
collector.Reservoirs[piece.StorageNode] = NewReservoir(collector.slotCount)
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
2020-12-14 12:54:22 +00:00
|
|
|
collector.Reservoirs[piece.StorageNode].Sample(collector.rand, NewSegment(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
|
|
|
|
}
|