storj/satellite/audit/collector.go

52 lines
1.5 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package audit
import (
"context"
"math/rand"
"storj.io/common/storj"
2019-09-10 14:24:16 +01:00
"storj.io/storj/satellite/metainfo"
)
var _ metainfo.Observer = (*Collector)(nil)
2019-09-10 14:24:16 +01:00
// Collector uses the metainfo 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,
}
}
// 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 *metainfo.Segment) (err error) {
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
}
// Object returns nil because the audit service does not interact with objects.
func (collector *Collector) Object(ctx context.Context, object *metainfo.Object) (err error) {
return nil
}
// InlineSegment returns nil because we're only auditing for storage nodes for now.
func (collector *Collector) InlineSegment(ctx context.Context, segment *metainfo.Segment) (err error) {
return nil
}