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/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-09-10 14:24:16 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2019-08-21 16:49:27 +01:00
|
|
|
)
|
|
|
|
|
2019-09-10 14:24:16 +01:00
|
|
|
var _ metainfo.Observer = (*PathCollector)(nil)
|
|
|
|
|
2019-08-21 16:49:27 +01:00
|
|
|
// PathCollector uses the metainfo loop to add paths to node reservoirs
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Observer
|
2019-08-21 16:49:27 +01:00
|
|
|
type PathCollector struct {
|
|
|
|
Reservoirs map[storj.NodeID]*Reservoir
|
|
|
|
slotCount int
|
|
|
|
rand *rand.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPathCollector instantiates a path collector
|
|
|
|
func NewPathCollector(reservoirSlots int, r *rand.Rand) *PathCollector {
|
|
|
|
return &PathCollector{
|
|
|
|
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
|
2019-09-12 11:38:49 +01:00
|
|
|
func (collector *PathCollector) RemoteSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-08-21 16:49:27 +01:00
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
|
|
|
if _, ok := collector.Reservoirs[piece.NodeId]; !ok {
|
|
|
|
collector.Reservoirs[piece.NodeId] = NewReservoir(collector.slotCount)
|
|
|
|
}
|
2019-09-12 11:38:49 +01:00
|
|
|
collector.Reservoirs[piece.NodeId].Sample(collector.rand, path.Raw)
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:51:41 +01:00
|
|
|
// Object returns nil because the audit service does not interact with objects
|
|
|
|
func (collector *PathCollector) Object(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-08-21 16:49:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InlineSegment returns nil because we're only auditing for storage nodes for now
|
2019-09-12 11:38:49 +01:00
|
|
|
func (collector *PathCollector) InlineSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-08-21 16:49:27 +01:00
|
|
|
return nil
|
|
|
|
}
|