2018-10-02 20:46:29 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package repairer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-05 16:58:07 +01:00
|
|
|
"time"
|
2018-10-02 20:46:29 +01:00
|
|
|
|
2018-10-12 19:04:16 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-10-24 13:35:59 +01:00
|
|
|
"storj.io/storj/internal/sync2"
|
2018-11-01 14:03:45 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
2018-10-02 20:46:29 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 14:52:11 +00:00
|
|
|
segment "storj.io/storj/pkg/storage/segments"
|
2018-10-02 20:46:29 +01:00
|
|
|
)
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
// Repairer is the interface for the data repairer
|
2018-10-03 19:35:56 +01:00
|
|
|
type Repairer interface {
|
2018-11-20 15:54:22 +00:00
|
|
|
//do we need this method? It doesn't look implemented
|
2018-10-25 19:59:36 +01:00
|
|
|
Repair(ctx context.Context, seg *pb.InjuredSegment) error
|
|
|
|
Run(ctx context.Context) error
|
2018-10-03 19:35:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// repairer holds important values for data repair
|
|
|
|
type repairer struct {
|
2018-11-01 14:03:45 +00:00
|
|
|
queue queue.RepairQueue
|
2018-11-06 14:52:11 +00:00
|
|
|
store segment.Store
|
2018-10-24 13:35:59 +01:00
|
|
|
limiter *sync2.Limiter
|
|
|
|
ticker *time.Ticker
|
|
|
|
}
|
|
|
|
|
2018-11-06 14:52:11 +00:00
|
|
|
func newRepairer(queue queue.RepairQueue, ss segment.Store, interval time.Duration, concurrency int) *repairer {
|
2018-10-24 13:35:59 +01:00
|
|
|
return &repairer{
|
|
|
|
queue: queue,
|
2018-11-06 14:52:11 +00:00
|
|
|
store: ss,
|
2018-10-24 13:35:59 +01:00
|
|
|
limiter: sync2.NewLimiter(concurrency),
|
|
|
|
ticker: time.NewTicker(interval),
|
|
|
|
}
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:03:45 +00:00
|
|
|
// Run runs the repairer service
|
2018-10-25 19:59:36 +01:00
|
|
|
func (r *repairer) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-02 20:46:29 +01:00
|
|
|
|
2018-10-24 13:35:59 +01:00
|
|
|
// wait for all repairs to complete
|
|
|
|
defer r.limiter.Wait()
|
2018-10-02 20:46:29 +01:00
|
|
|
|
|
|
|
for {
|
2018-11-01 14:03:45 +00:00
|
|
|
err := r.process(ctx)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("process", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:46:29 +01:00
|
|
|
select {
|
2018-10-24 13:35:59 +01:00
|
|
|
case <-r.ticker.C: // wait for the next interval to happen
|
2018-10-25 19:59:36 +01:00
|
|
|
case <-ctx.Done(): // or the repairer is canceled via context
|
|
|
|
return ctx.Err()
|
2018-10-24 13:35:59 +01:00
|
|
|
}
|
2018-11-01 14:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process picks an item from repair queue and spawns a repairer
|
|
|
|
func (r *repairer) process(ctx context.Context) error {
|
|
|
|
seg, err := r.queue.Dequeue()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: only log when err != ErrQueueEmpty
|
|
|
|
return err
|
|
|
|
}
|
2018-10-30 20:14:15 +00:00
|
|
|
|
2018-11-01 14:03:45 +00:00
|
|
|
r.limiter.Go(ctx, func() {
|
2018-11-06 14:52:11 +00:00
|
|
|
err := r.store.Repair(ctx, seg.GetPath(), seg.GetLostPieces())
|
2018-10-30 20:14:15 +00:00
|
|
|
if err != nil {
|
2018-11-01 14:03:45 +00:00
|
|
|
zap.L().Error("Repair failed", zap.Error(err))
|
2018-10-30 20:14:15 +00:00
|
|
|
}
|
2018-11-01 14:03:45 +00:00
|
|
|
})
|
2018-10-30 20:14:15 +00:00
|
|
|
|
2018-11-01 14:03:45 +00:00
|
|
|
return nil
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|