2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-02 20:46:29 +01:00
|
|
|
// 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"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-12-13 07:12:36 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-03-28 20:09:23 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
2018-11-27 15:57:51 +00:00
|
|
|
"storj.io/storj/storage"
|
2018-10-02 20:46:29 +01:00
|
|
|
)
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
// SegmentRepairer is a repairer for segments
|
|
|
|
type SegmentRepairer interface {
|
|
|
|
Repair(ctx context.Context, path storj.Path, lostPieces []int32) (err error)
|
2018-10-03 19:35:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Service contains the information needed to run the repair service
|
|
|
|
type Service struct {
|
2019-03-28 20:09:23 +00:00
|
|
|
queue queue.RepairQueue
|
|
|
|
config *Config
|
|
|
|
limiter *sync2.Limiter
|
|
|
|
ticker *time.Ticker
|
|
|
|
transport transport.Client
|
|
|
|
pointerdb *pointerdb.Service
|
|
|
|
orders *orders.Service
|
|
|
|
cache *overlay.Cache
|
|
|
|
repairer SegmentRepairer
|
2018-10-24 13:35:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// NewService creates repairing service
|
2019-03-28 20:09:23 +00:00
|
|
|
func NewService(queue queue.RepairQueue, config *Config, interval time.Duration, concurrency int, transport transport.Client, pointerdb *pointerdb.Service, orders *orders.Service, cache *overlay.Cache) *Service {
|
2019-01-18 13:54:08 +00:00
|
|
|
return &Service{
|
2019-03-28 20:09:23 +00:00
|
|
|
queue: queue,
|
|
|
|
config: config,
|
|
|
|
limiter: sync2.NewLimiter(concurrency),
|
|
|
|
ticker: time.NewTicker(interval),
|
|
|
|
transport: transport,
|
|
|
|
pointerdb: pointerdb,
|
|
|
|
orders: orders,
|
|
|
|
cache: cache,
|
2018-10-24 13:35:59 +01:00
|
|
|
}
|
2018-10-02 20:46:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Close closes resources
|
|
|
|
func (service *Service) Close() error { return nil }
|
|
|
|
|
2018-11-01 14:03:45 +00:00
|
|
|
// Run runs the repairer service
|
2019-01-18 13:54:08 +00:00
|
|
|
func (service *Service) Run(ctx context.Context) (err error) {
|
2018-10-25 19:59:36 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-02 20:46:29 +01:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
// TODO: close segment repairer, currently this leaks connections
|
2019-03-18 10:55:06 +00:00
|
|
|
service.repairer, err = service.config.GetSegmentRepairer(
|
|
|
|
ctx,
|
|
|
|
service.transport,
|
|
|
|
service.pointerdb,
|
2019-03-28 20:09:23 +00:00
|
|
|
service.orders,
|
2019-03-18 10:55:06 +00:00
|
|
|
service.cache,
|
|
|
|
service.transport.Identity(),
|
|
|
|
)
|
2019-02-01 17:28:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:35:59 +01:00
|
|
|
// wait for all repairs to complete
|
2018-12-13 07:12:36 +00:00
|
|
|
defer service.limiter.Wait()
|
2018-10-02 20:46:29 +01:00
|
|
|
|
|
|
|
for {
|
2018-12-13 07:12:36 +00:00
|
|
|
err := service.process(ctx)
|
2018-11-01 14:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("process", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:46:29 +01:00
|
|
|
select {
|
2018-12-13 07:12:36 +00:00
|
|
|
case <-service.ticker.C: // wait for the next interval to happen
|
|
|
|
case <-ctx.Done(): // or the repairer service is canceled via context
|
2018-10-25 19:59:36 +01:00
|
|
|
return ctx.Err()
|
2018-10-24 13:35:59 +01:00
|
|
|
}
|
2018-11-01 14:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
// process picks an item from repair queue and spawns a repair worker
|
2019-01-18 13:54:08 +00:00
|
|
|
func (service *Service) process(ctx context.Context) error {
|
2018-12-27 09:56:25 +00:00
|
|
|
seg, err := service.queue.Dequeue(ctx)
|
2018-11-01 14:03:45 +00:00
|
|
|
if err != nil {
|
2019-01-02 16:00:32 +00:00
|
|
|
if storage.ErrEmptyQueue.Has(err) {
|
2018-11-27 15:57:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-01 14:03:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-30 20:14:15 +00:00
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
service.limiter.Go(ctx, func() {
|
|
|
|
err := service.repairer.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
|
|
|
}
|