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-03-23 12:14:38 +00:00
|
|
|
"storj.io/storj/satellite/metainfo/metaloop"
|
2019-08-21 16:49:27 +01:00
|
|
|
)
|
|
|
|
|
2021-03-23 12:14:38 +00:00
|
|
|
var _ metaloop.Observer = (*Collector)(nil)
|
2019-09-10 14:24:16 +01:00
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
// Collector uses the metainfo loop to add segments to node reservoirs.
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-23 12:14:38 +00:00
|
|
|
func (collector *Collector) RemoteSegment(ctx context.Context, segment *metaloop.Segment) (err error) {
|
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
|
|
|
// Object returns nil because the audit service does not interact with objects.
|
2021-03-23 12:14:38 +00:00
|
|
|
func (collector *Collector) Object(ctx context.Context, object *metaloop.Object) (err error) {
|
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-03-23 12:14:38 +00:00
|
|
|
func (collector *Collector) InlineSegment(ctx context.Context, segment *metaloop.Segment) (err error) {
|
2019-08-21 16:49:27 +01:00
|
|
|
return nil
|
|
|
|
}
|