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-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
|
2020-12-14 12:54:22 +00:00
|
|
|
size int8
|
|
|
|
index int64
|
2021-11-29 17:29:02 +00:00
|
|
|
wSum 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,
|
2021-11-29 17:29:02 +00:00
|
|
|
// compute the relative weight based on segment size, and pick a random floating
|
|
|
|
// point number r = rand(0..1), and if r < the relative weight of the segment,
|
|
|
|
// select uniformly a random segment reservoir.Segments[rand(0..i)] to replace with
|
|
|
|
// segment. See https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_A-Chao
|
|
|
|
// for the algorithm used.
|
2022-09-23 15:00:56 +01:00
|
|
|
func (reservoir *Reservoir) Sample(r *rand.Rand, segment *segmentloop.Segment) {
|
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
|
2021-11-29 17:29:02 +00:00
|
|
|
reservoir.wSum += int64(segment.EncryptedSize)
|
2019-08-21 16:49:27 +01:00
|
|
|
} else {
|
2021-11-29 17:29:02 +00:00
|
|
|
reservoir.wSum += int64(segment.EncryptedSize)
|
|
|
|
p := float64(segment.EncryptedSize) / float64(reservoir.wSum)
|
|
|
|
random := r.Float64()
|
|
|
|
if random < p {
|
|
|
|
index := r.Int31n(int32(reservoir.size))
|
2022-09-23 15:00:56 +01:00
|
|
|
reservoir.Segments[index] = *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 {
|
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
|
|
|
}
|