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"
|
|
|
|
"storj.io/storj/storage"
|
2018-10-03 19:35:56 +01:00
|
|
|
)
|
|
|
|
|
2018-10-12 18:49:49 +01:00
|
|
|
// Checker is the interface for the data repair queue
|
|
|
|
type Checker interface {
|
|
|
|
IdentifyInjuredSegments(ctx context.Context) (err error)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentifyInjuredSegments checks for missing pieces off of the pointerdb and overlay cache
|
2018-10-12 18:49:49 +01:00
|
|
|
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 {
|
|
|
|
return Error.New("error getting missing offline nodes %s", err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the indices of offline and online nodes
|
2018-10-12 18:49:49 +01:00
|
|
|
func (c *checker) offlineNodes(ctx context.Context, nodeIDs []dht.NodeID) (offline []int32, err error) {
|
2018-10-09 17:09:33 +01:00
|
|
|
responses, err := c.overlay.BulkLookup(ctx, nodeIDsToLookupRequests(nodeIDs))
|
|
|
|
if err != nil {
|
|
|
|
return []int32{}, err
|
|
|
|
}
|
|
|
|
nodes := lookupResponsesToNodes(responses)
|
|
|
|
for i, n := range nodes {
|
|
|
|
if n == nil {
|
|
|
|
offline = append(offline, int32(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offline, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeIDsToLookupRequests(nodeIDs []dht.NodeID) *pb.LookupRequests {
|
|
|
|
var rq []*pb.LookupRequest
|
|
|
|
for _, v := range nodeIDs {
|
|
|
|
r := &pb.LookupRequest{NodeID: v.String()}
|
|
|
|
rq = append(rq, r)
|
|
|
|
}
|
|
|
|
return &pb.LookupRequests{Lookuprequest: rq}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupResponsesToNodes(responses *pb.LookupResponses) []*pb.Node {
|
|
|
|
var nodes []*pb.Node
|
|
|
|
for _, v := range responses.Lookupresponse {
|
|
|
|
n := v.Node
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
2018-10-12 18:49:49 +01:00
|
|
|
|
2018-10-30 19:16:40 +00:00
|
|
|
// Run the checker loop
|
|
|
|
func (c *checker) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-12 18:49:49 +01:00
|
|
|
|
2018-10-30 19:16:40 +00:00
|
|
|
for {
|
|
|
|
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-10-30 20:14:15 +00:00
|
|
|
|
|
|
|
err = c.IdentifyInjuredSegments(ctx)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("Checker failed", zap.Error(err))
|
|
|
|
}
|
2018-10-30 19:16:40 +00:00
|
|
|
}
|
2018-10-12 18:49:49 +01:00
|
|
|
}
|