2019-09-05 16:40:52 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/sync2"
|
2022-09-23 15:00:56 +01:00
|
|
|
"storj.io/common/uuid"
|
2021-06-14 16:40:46 +01:00
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
2019-09-05 16:40:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Chore populates reservoirs and the audit queue.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2019-09-05 16:40:52 +01:00
|
|
|
type Chore struct {
|
2022-11-11 23:11:40 +00:00
|
|
|
log *zap.Logger
|
|
|
|
rand *rand.Rand
|
|
|
|
queue VerifyQueue
|
|
|
|
Loop *sync2.Cycle
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
segmentLoop *segmentloop.Service
|
|
|
|
config Config
|
2019-09-05 16:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore instantiates Chore.
|
2022-11-11 23:11:40 +00:00
|
|
|
func NewChore(log *zap.Logger, queue VerifyQueue, loop *segmentloop.Service, config Config) *Chore {
|
2019-09-05 16:40:52 +01:00
|
|
|
return &Chore{
|
2022-11-11 23:11:40 +00:00
|
|
|
log: log,
|
|
|
|
rand: rand.New(rand.NewSource(time.Now().Unix())),
|
|
|
|
queue: queue,
|
|
|
|
Loop: sync2.NewCycle(config.ChoreInterval),
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
segmentLoop: loop,
|
|
|
|
config: config,
|
2019-09-05 16:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the chore.
|
|
|
|
func (chore *Chore) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return chore.Loop.Run(ctx, func(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
collector := NewCollector(chore.config.Slots, chore.rand)
|
2021-06-14 16:40:46 +01:00
|
|
|
err = chore.segmentLoop.Join(ctx, collector)
|
2019-09-05 16:40:52 +01:00
|
|
|
if err != nil {
|
2021-06-14 16:40:46 +01:00
|
|
|
chore.log.Error("error joining segmentloop", zap.Error(err))
|
2019-09-05 16:40:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:00:56 +01:00
|
|
|
type SegmentKey struct {
|
|
|
|
StreamID uuid.UUID
|
|
|
|
Position uint64
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
var newQueue []Segment
|
2022-09-23 15:00:56 +01:00
|
|
|
queueSegments := make(map[SegmentKey]struct{})
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
// Add reservoir segments to queue in pseudorandom order.
|
2019-09-05 16:40:52 +01:00
|
|
|
for i := 0; i < chore.config.Slots; i++ {
|
2020-12-14 12:54:22 +00:00
|
|
|
for _, res := range collector.Reservoirs {
|
|
|
|
// Skip reservoir if no segment at this index.
|
|
|
|
if len(res.Segments) <= i {
|
2019-09-05 16:40:52 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-12-14 12:54:22 +00:00
|
|
|
segment := res.Segments[i]
|
2022-09-23 15:00:56 +01:00
|
|
|
segmentKey := SegmentKey{
|
|
|
|
StreamID: segment.StreamID,
|
|
|
|
Position: segment.Position.Encode(),
|
|
|
|
}
|
|
|
|
if segmentKey == (SegmentKey{}) {
|
2019-09-05 16:40:52 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-09-23 15:00:56 +01:00
|
|
|
|
|
|
|
if _, ok := queueSegments[segmentKey]; !ok {
|
|
|
|
newQueue = append(newQueue, NewSegment(segment))
|
|
|
|
queueSegments[segmentKey] = struct{}{}
|
2019-09-05 16:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 23:24:15 +01:00
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
// Push new queue to queues struct so it can be fetched by worker.
|
2022-11-11 23:11:40 +00:00
|
|
|
return chore.queue.Push(ctx, newQueue)
|
2019-09-05 16:40:52 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes chore.
|
|
|
|
func (chore *Chore) Close() error {
|
|
|
|
chore.Loop.Close()
|
|
|
|
return nil
|
|
|
|
}
|