2019-08-21 16:49:27 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2022-12-09 22:22:39 +00:00
|
|
|
"math"
|
2019-08-21 16:49:27 +01:00
|
|
|
"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-06-14 16:40:46 +01:00
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
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 {
|
2022-09-23 15:00:56 +01:00
|
|
|
Segments [maxReservoirSize]segmentloop.Segment
|
2022-12-09 22:22:39 +00:00
|
|
|
Keys [maxReservoirSize]float64
|
2020-12-14 12:54:22 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 22:22:39 +00:00
|
|
|
// Sample tries to ensure that each segment passed in has a chance (proportional
|
|
|
|
// to its size) to be in the reservoir when sampling is complete.
|
|
|
|
//
|
|
|
|
// The tricky part is that we do not know ahead of time how many segments will
|
|
|
|
// be passed in. The way this is accomplished is known as _Reservoir Sampling_.
|
|
|
|
// The specific algorithm we are using here is called A-Res on the Wikipedia
|
|
|
|
// article: https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_A-Res
|
2022-09-23 15:00:56 +01:00
|
|
|
func (reservoir *Reservoir) Sample(r *rand.Rand, segment *segmentloop.Segment) {
|
2022-12-09 22:22:39 +00:00
|
|
|
k := -math.Log(r.Float64()) / float64(segment.EncryptedSize)
|
2019-08-21 16:49:27 +01:00
|
|
|
if reservoir.index < int64(reservoir.size) {
|
2022-09-23 15:00:56 +01:00
|
|
|
reservoir.Segments[reservoir.index] = *segment
|
2022-12-09 22:22:39 +00:00
|
|
|
reservoir.Keys[reservoir.index] = k
|
2019-08-21 16:49:27 +01:00
|
|
|
} else {
|
2022-12-09 22:22:39 +00:00
|
|
|
max := 0
|
|
|
|
for i := 1; i < int(reservoir.size); i++ {
|
|
|
|
if reservoir.Keys[i] > reservoir.Keys[max] {
|
|
|
|
max = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if k < reservoir.Keys[max] {
|
|
|
|
reservoir.Segments[max] = *segment
|
|
|
|
reservoir.Keys[max] = k
|
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 {
|
2021-11-29 17:29:02 +00:00
|
|
|
StreamID uuid.UUID
|
|
|
|
Position metabase.SegmentPosition
|
|
|
|
ExpiresAt *time.Time
|
|
|
|
EncryptedSize int32 // size of the whole segment (not a piece)
|
2020-12-14 12:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegment creates a new segment to audit from a metainfo loop segment.
|
2022-09-23 15:00:56 +01:00
|
|
|
func NewSegment(loopSegment segmentloop.Segment) Segment {
|
2020-12-14 12:54:22 +00:00
|
|
|
return Segment{
|
2021-11-29 17:29:02 +00:00
|
|
|
StreamID: loopSegment.StreamID,
|
|
|
|
Position: loopSegment.Position,
|
|
|
|
ExpiresAt: loopSegment.ExpiresAt,
|
|
|
|
EncryptedSize: loopSegment.EncryptedSize,
|
2020-12-14 12:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired checks if segment is expired relative to now.
|
|
|
|
func (segment *Segment) Expired(now time.Time) bool {
|
2021-06-14 16:40:46 +01:00
|
|
|
return segment.ExpiresAt != nil && segment.ExpiresAt.Before(now)
|
2020-12-14 12:54:22 +00:00
|
|
|
}
|