storj/satellite/audit/collector.go
Michał Niewrzał 70e6cdfd06 satellite/audit: move to segmentloop
Change-Id: I10e63a1e4b6b62f5cd3098f5922ad3de1ec5af51
2021-06-28 11:32:00 +00:00

54 lines
1.5 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package audit
import (
"context"
"math/rand"
"storj.io/common/storj"
"storj.io/storj/satellite/metabase/segmentloop"
)
var _ segmentloop.Observer = (*Collector)(nil)
// Collector uses the segment loop to add segments to node reservoirs.
type Collector struct {
Reservoirs map[storj.NodeID]*Reservoir
slotCount int
rand *rand.Rand
}
// NewCollector instantiates a segment collector.
func NewCollector(reservoirSlots int, r *rand.Rand) *Collector {
return &Collector{
Reservoirs: make(map[storj.NodeID]*Reservoir),
slotCount: reservoirSlots,
rand: r,
}
}
// LoopStarted is called at each start of a loop.
func (collector *Collector) LoopStarted(context.Context, segmentloop.LoopInfo) (err error) {
return nil
}
// RemoteSegment takes a remote segment found in metainfo and creates a reservoir for it if it doesn't exist already.
func (collector *Collector) RemoteSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
defer mon.Task()(&ctx)(&err)
for _, piece := range segment.Pieces {
if _, ok := collector.Reservoirs[piece.StorageNode]; !ok {
collector.Reservoirs[piece.StorageNode] = NewReservoir(collector.slotCount)
}
collector.Reservoirs[piece.StorageNode].Sample(collector.rand, NewSegment(segment))
}
return nil
}
// InlineSegment returns nil because we're only auditing for storage nodes for now.
func (collector *Collector) InlineSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
return nil
}