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-10-02 20:46:29 +01:00
|
|
|
q "storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
2018-10-03 19:35:56 +01:00
|
|
|
// Repairer is the interface for the data repair queue
|
|
|
|
type Repairer interface {
|
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-10-24 13:35:59 +01:00
|
|
|
queue q.RepairQueue
|
|
|
|
limiter *sync2.Limiter
|
|
|
|
ticker *time.Ticker
|
|
|
|
}
|
|
|
|
|
2018-10-25 19:59:36 +01:00
|
|
|
func newRepairer(queue q.RepairQueue, interval time.Duration, concurrency int) *repairer {
|
2018-10-24 13:35:59 +01:00
|
|
|
return &repairer{
|
|
|
|
queue: queue,
|
|
|
|
limiter: sync2.NewLimiter(concurrency),
|
|
|
|
ticker: time.NewTicker(interval),
|
|
|
|
}
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the repairer loop
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
seg, err := r.queue.Dequeue()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: only log when err != ErrQueueEmpty
|
|
|
|
zap.L().Error("dequeue", zap.Error(err))
|
|
|
|
continue
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
2018-10-24 13:35:59 +01:00
|
|
|
|
2018-10-25 19:59:36 +01:00
|
|
|
r.limiter.Go(ctx, func() {
|
|
|
|
err := r.Repair(ctx, &seg)
|
2018-10-24 13:35:59 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("Repair failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
})
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repair starts repair of the segment
|
2018-10-25 19:59:36 +01:00
|
|
|
func (r *repairer) Repair(ctx context.Context, seg *pb.InjuredSegment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-24 13:35:59 +01:00
|
|
|
// TODO:
|
|
|
|
zap.L().Debug("Repairing", zap.Any("segment", seg))
|
2018-10-02 20:46:29 +01:00
|
|
|
return err
|
|
|
|
}
|