2018-10-02 20:46:29 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package checker
|
2018-10-03 19:35:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-30 19:16:40 +00:00
|
|
|
"time"
|
2018-10-04 22:40:34 +01:00
|
|
|
|
2018-10-09 17:09:33 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-10-04 22:40:34 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-10-09 17:09:33 +01:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/dht"
|
|
|
|
"storj.io/storj/pkg/node"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-10-09 17:09:33 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-03 19:35:56 +01:00
|
|
|
)
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
// Checker is the interface for data repair checker
|
2018-10-12 18:49:49 +01:00
|
|
|
type Checker interface {
|
2018-10-30 19:16:40 +00:00
|
|
|
Run(ctx context.Context) error
|
2018-10-03 19:35:56 +01:00
|
|
|
}
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
// Checker contains the information needed to do checks for missing pieces
|
2018-10-12 18:49:49 +01:00
|
|
|
type checker struct {
|
2018-10-09 17:09:33 +01:00
|
|
|
pointerdb *pointerdb.Server
|
|
|
|
repairQueue *queue.Queue
|
|
|
|
overlay pb.OverlayServer
|
|
|
|
limit int
|
|
|
|
logger *zap.Logger
|
2018-10-30 19:16:40 +00:00
|
|
|
ticker *time.Ticker
|
2018-10-09 17:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChecker creates a new instance of checker
|
2018-10-30 19:16:40 +00:00
|
|
|
func newChecker(pointerdb *pointerdb.Server, repairQueue *queue.Queue, overlay pb.OverlayServer, limit int, logger *zap.Logger, interval time.Duration) *checker {
|
2018-10-12 18:49:49 +01:00
|
|
|
return &checker{
|
2018-10-09 17:09:33 +01:00
|
|
|
pointerdb: pointerdb,
|
|
|
|
repairQueue: repairQueue,
|
|
|
|
overlay: overlay,
|
|
|
|
limit: limit,
|
|
|
|
logger: logger,
|
2018-10-30 19:16:40 +00:00
|
|
|
ticker: time.NewTicker(interval),
|
2018-10-09 17:09:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:03:45 +00:00
|
|
|
// Run the checker loop
|
|
|
|
func (c *checker) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for {
|
2018-11-08 16:18:28 +00:00
|
|
|
err = c.identifyInjuredSegments(ctx)
|
2018-11-01 14:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("Checker failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.ticker.C: // wait for the next interval to happen
|
|
|
|
case <-ctx.Done(): // or the checker is canceled via context
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
// identifyInjuredSegments checks for missing pieces off of the pointerdb and overlay cache
|
|
|
|
func (c *checker) identifyInjuredSegments(ctx context.Context) (err error) {
|
2018-10-09 17:09:33 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
c.logger.Debug("entering pointerdb iterate")
|
|
|
|
|
|
|
|
err = c.pointerdb.Iterate(ctx, &pb.IterateRequest{Recurse: true},
|
|
|
|
func(it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
2018-10-30 19:16:40 +00:00
|
|
|
lim := c.limit
|
|
|
|
if lim <= 0 || lim > storage.LookupLimit {
|
|
|
|
lim = storage.LookupLimit
|
2018-10-09 17:09:33 +01:00
|
|
|
}
|
2018-10-30 19:16:40 +00:00
|
|
|
for ; lim > 0 && it.Next(&item); lim-- {
|
2018-10-09 17:09:33 +01:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(item.Value, pointer)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("error unmarshalling pointer %s", err)
|
|
|
|
}
|
|
|
|
pieces := pointer.Remote.RemotePieces
|
|
|
|
var nodeIDs []dht.NodeID
|
|
|
|
for _, p := range pieces {
|
|
|
|
nodeIDs = append(nodeIDs, node.IDFromString(p.NodeId))
|
|
|
|
}
|
|
|
|
missingPieces, err := c.offlineNodes(ctx, nodeIDs)
|
|
|
|
if err != nil {
|
2018-11-08 16:18:28 +00:00
|
|
|
return Error.New("error getting offline nodes %s", err)
|
2018-10-09 17:09:33 +01:00
|
|
|
}
|
|
|
|
numHealthy := len(nodeIDs) - len(missingPieces)
|
|
|
|
if int32(numHealthy) < pointer.Remote.Redundancy.RepairThreshold {
|
|
|
|
err = c.repairQueue.Enqueue(&pb.InjuredSegment{
|
|
|
|
Path: string(item.Key),
|
|
|
|
LostPieces: missingPieces,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("error adding injured segment to queue %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
// returns the indices of offline nodes
|
2018-10-12 18:49:49 +01:00
|
|
|
func (c *checker) offlineNodes(ctx context.Context, nodeIDs []dht.NodeID) (offline []int32, err error) {
|
2018-11-08 16:18:28 +00:00
|
|
|
responses, err := c.overlay.BulkLookup(ctx, utils.NodeIDsToLookupRequests(nodeIDs))
|
2018-10-09 17:09:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return []int32{}, err
|
|
|
|
}
|
2018-11-08 16:18:28 +00:00
|
|
|
nodes := utils.LookupResponsesToNodes(responses)
|
2018-10-09 17:09:33 +01:00
|
|
|
for i, n := range nodes {
|
|
|
|
if n == nil {
|
|
|
|
offline = append(offline, int32(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offline, nil
|
|
|
|
}
|