2019-08-21 16:49:27 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2020-12-14 12:54:22 +00:00
|
|
|
"time"
|
2019-08-21 16:49:27 +01:00
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
"storj.io/common/uuid"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-04-22 10:07:18 +01:00
|
|
|
"storj.io/storj/satellite/metabase/metaloop"
|
2019-08-21 16:49:27 +01:00
|
|
|
)
|
|
|
|
|
2019-09-05 16:40:52 +01:00
|
|
|
const maxReservoirSize = 3
|
2019-08-21 16:49:27 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Reservoir holds a certain number of segments to reflect a random sample.
|
2019-08-21 16:49:27 +01:00
|
|
|
type Reservoir struct {
|
2020-12-14 12:54:22 +00:00
|
|
|
Segments [maxReservoirSize]Segment
|
|
|
|
size int8
|
|
|
|
index int64
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewReservoir instantiates a Reservoir.
|
2019-08-21 16:49:27 +01:00
|
|
|
func NewReservoir(size int) *Reservoir {
|
|
|
|
if size < 1 {
|
|
|
|
size = 1
|
|
|
|
} else if size > maxReservoirSize {
|
|
|
|
size = maxReservoirSize
|
|
|
|
}
|
|
|
|
return &Reservoir{
|
|
|
|
size: int8(size),
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample makes sure that for every segment in metainfo from index i=size..n-1,
|
2020-07-16 15:18:02 +01:00
|
|
|
// pick a random number r = rand(0..i), and if r < size, replace reservoir.Segments[r] with segment.
|
2020-12-14 12:54:22 +00:00
|
|
|
func (reservoir *Reservoir) Sample(r *rand.Rand, segment Segment) {
|
2019-08-21 16:49:27 +01:00
|
|
|
if reservoir.index < int64(reservoir.size) {
|
2020-12-14 12:54:22 +00:00
|
|
|
reservoir.Segments[reservoir.index] = segment
|
2019-08-21 16:49:27 +01:00
|
|
|
} else {
|
2021-06-07 18:32:03 +01:00
|
|
|
random := r.Int63n(reservoir.index + 1)
|
2019-08-21 16:49:27 +01:00
|
|
|
if random < int64(reservoir.size) {
|
2020-12-14 12:54:22 +00:00
|
|
|
reservoir.Segments[random] = segment
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 18:32:03 +01:00
|
|
|
reservoir.index++
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
2020-12-14 12:54:22 +00:00
|
|
|
|
|
|
|
// Segment is a segment to audit.
|
|
|
|
type Segment struct {
|
|
|
|
metabase.SegmentLocation
|
|
|
|
StreamID uuid.UUID
|
|
|
|
ExpirationDate time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegment creates a new segment to audit from a metainfo loop segment.
|
2021-03-23 12:14:38 +00:00
|
|
|
func NewSegment(loopSegment *metaloop.Segment) Segment {
|
2020-12-14 12:54:22 +00:00
|
|
|
return Segment{
|
|
|
|
SegmentLocation: loopSegment.Location,
|
|
|
|
StreamID: loopSegment.StreamID,
|
|
|
|
ExpirationDate: loopSegment.ExpirationDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired checks if segment is expired relative to now.
|
|
|
|
func (segment *Segment) Expired(now time.Time) bool {
|
|
|
|
return !segment.ExpirationDate.IsZero() && segment.ExpirationDate.Before(now)
|
|
|
|
}
|