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"
|
2019-09-05 16:40:52 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-08-20 14:29:02 +01:00
|
|
|
log *zap.Logger
|
|
|
|
rand *rand.Rand
|
|
|
|
queues *Queues
|
|
|
|
Loop *sync2.Cycle
|
2019-09-05 16:40:52 +01:00
|
|
|
|
|
|
|
metainfoLoop *metainfo.Loop
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore instantiates Chore.
|
2020-08-20 14:29:02 +01:00
|
|
|
func NewChore(log *zap.Logger, queues *Queues, metaLoop *metainfo.Loop, config Config) *Chore {
|
2019-09-05 16:40:52 +01:00
|
|
|
return &Chore{
|
2020-08-20 14:29:02 +01:00
|
|
|
log: log,
|
|
|
|
rand: rand.New(rand.NewSource(time.Now().Unix())),
|
|
|
|
queues: queues,
|
|
|
|
Loop: sync2.NewCycle(config.ChoreInterval),
|
2019-09-05 16:40:52 +01:00
|
|
|
|
|
|
|
metainfoLoop: metaLoop,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-20 14:29:02 +01:00
|
|
|
// If the previously pushed queue is still waiting to be swapped in, wait.
|
|
|
|
err = chore.queues.WaitForSwap(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
collector := NewCollector(chore.config.Slots, chore.rand)
|
|
|
|
err = chore.metainfoLoop.Join(ctx, collector)
|
2019-09-05 16:40:52 +01:00
|
|
|
if err != nil {
|
|
|
|
chore.log.Error("error joining metainfoloop", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
var newQueue []Segment
|
|
|
|
queueSegments := make(map[Segment]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]
|
|
|
|
if segment == (Segment{}) {
|
2019-09-05 16:40:52 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-12-14 12:54:22 +00:00
|
|
|
if _, ok := queueSegments[segment]; !ok {
|
|
|
|
newQueue = append(newQueue, segment)
|
|
|
|
queueSegments[segment] = 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.
|
|
|
|
return chore.queues.Push(newQueue)
|
2019-09-05 16:40:52 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes chore.
|
|
|
|
func (chore *Chore) Close() error {
|
|
|
|
chore.Loop.Close()
|
|
|
|
return nil
|
|
|
|
}
|