2018-10-02 20:46:29 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package repairer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
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-02 20:46:29 +01:00
|
|
|
q "storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-11 19:42:32 +01:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-10-02 20:46:29 +01:00
|
|
|
)
|
|
|
|
|
2018-10-03 19:35:56 +01:00
|
|
|
// Repairer is the interface for the data repair queue
|
|
|
|
type Repairer interface {
|
|
|
|
Repair(seg *pb.InjuredSegment) error
|
|
|
|
Run() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// repairer holds important values for data repair
|
|
|
|
type repairer struct {
|
2018-10-02 20:46:29 +01:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
queue q.RepairQueue
|
|
|
|
errs []error
|
|
|
|
mu sync.Mutex
|
|
|
|
cond sync.Cond
|
|
|
|
maxRepair int
|
|
|
|
inProgress int
|
2018-10-05 16:58:07 +01:00
|
|
|
interval time.Duration
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the repairer loop
|
2018-10-03 19:35:56 +01:00
|
|
|
func (r *repairer) Run() (err error) {
|
2018-10-12 19:04:16 +01:00
|
|
|
zap.S().Info("Repairer is starting up")
|
|
|
|
|
2018-10-02 20:46:29 +01:00
|
|
|
c := make(chan *pb.InjuredSegment)
|
2018-10-05 16:58:07 +01:00
|
|
|
|
|
|
|
ticker := time.NewTicker(r.interval)
|
|
|
|
defer ticker.Stop()
|
2018-10-02 20:46:29 +01:00
|
|
|
go func() {
|
2018-10-05 16:58:07 +01:00
|
|
|
for range ticker.C {
|
2018-10-02 20:46:29 +01:00
|
|
|
for r.inProgress >= r.maxRepair {
|
|
|
|
r.cond.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNext should lock until there is an actual next item in the queue
|
|
|
|
seg, err := r.queue.Dequeue()
|
|
|
|
if err != nil {
|
|
|
|
r.errs = append(r.errs, err)
|
|
|
|
r.cancel()
|
|
|
|
}
|
|
|
|
c <- &seg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.ctx.Done():
|
2018-10-11 19:42:32 +01:00
|
|
|
return utils.CombineErrors(r.errs...)
|
2018-10-02 20:46:29 +01:00
|
|
|
case seg := <-c:
|
|
|
|
go func() {
|
|
|
|
err := r.Repair(seg)
|
|
|
|
if err != nil {
|
|
|
|
r.errs = append(r.errs, err)
|
|
|
|
r.cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repair starts repair of the segment
|
2018-10-03 19:35:56 +01:00
|
|
|
func (r *repairer) Repair(seg *pb.InjuredSegment) (err error) {
|
2018-10-02 20:46:29 +01:00
|
|
|
defer mon.Task()(&r.ctx)(&err)
|
|
|
|
r.inProgress++
|
|
|
|
fmt.Println(seg)
|
|
|
|
|
|
|
|
r.inProgress--
|
|
|
|
r.cond.Signal()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the repairer loop
|
2018-10-03 19:35:56 +01:00
|
|
|
func (r *repairer) Stop() (err error) {
|
2018-10-02 20:46:29 +01:00
|
|
|
r.cancel()
|
|
|
|
return nil
|
|
|
|
}
|