2022-09-09 09:16:22 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-16 16:35:19 +01:00
|
|
|
"io"
|
2022-09-12 20:22:01 +01:00
|
|
|
"time"
|
2022-09-09 09:16:22 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2022-09-12 20:22:01 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/rpc/rpcstatus"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
|
|
|
"storj.io/storj/satellite/orders"
|
|
|
|
"storj.io/uplink/private/piecestore"
|
|
|
|
)
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
// ErrNodeOffline is returned when it was not possible to contact a node or the node was not responding.
|
|
|
|
var ErrNodeOffline = errs.Class("node offline")
|
2022-09-09 09:16:22 +01:00
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
// VerifierConfig contains configurations for operation.
|
|
|
|
type VerifierConfig struct {
|
2022-09-19 13:40:22 +01:00
|
|
|
DialTimeout time.Duration `help:"how long to wait for a successful dial" default:"2s"`
|
2022-09-15 15:23:10 +01:00
|
|
|
PerPieceTimeout time.Duration `help:"duration to wait per piece download" default:"800ms"`
|
|
|
|
OrderRetryThrottle time.Duration `help:"how much to wait before retrying order creation" default:"50ms"`
|
2022-09-12 20:22:01 +01:00
|
|
|
|
2022-09-19 13:40:22 +01:00
|
|
|
RequestThrottle time.Duration `help:"minimum interval for sending out each request" default:"150ms"`
|
2022-09-15 15:23:10 +01:00
|
|
|
}
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
// NodeVerifier implements segment verification by dialing nodes.
|
|
|
|
type NodeVerifier struct {
|
2022-09-15 15:23:10 +01:00
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
config VerifierConfig
|
|
|
|
|
2022-09-12 20:22:01 +01:00
|
|
|
dialer rpc.Dialer
|
|
|
|
orders *orders.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Verifier = (*NodeVerifier)(nil)
|
|
|
|
|
|
|
|
// NewVerifier creates a new segment verifier using the specified dialer.
|
2022-09-15 15:23:10 +01:00
|
|
|
func NewVerifier(log *zap.Logger, dialer rpc.Dialer, orders *orders.Service, config VerifierConfig) *NodeVerifier {
|
2022-09-19 13:40:22 +01:00
|
|
|
configuredDialer := dialer
|
|
|
|
if config.DialTimeout > 0 {
|
|
|
|
configuredDialer.DialTimeout = config.DialTimeout
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:22:01 +01:00
|
|
|
return &NodeVerifier{
|
|
|
|
log: log,
|
2022-09-15 15:23:10 +01:00
|
|
|
config: config,
|
2022-09-19 13:40:22 +01:00
|
|
|
dialer: configuredDialer,
|
2022-09-12 20:22:01 +01:00
|
|
|
orders: orders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a collection of segments by attempting to download a byte from each segment from the target node.
|
2022-09-19 13:40:22 +01:00
|
|
|
func (service *NodeVerifier) Verify(ctx context.Context, target storj.NodeURL, segments []*Segment, ignoreThrottle bool) error {
|
2022-09-12 20:22:01 +01:00
|
|
|
client, err := piecestore.Dial(ctx, service.dialer, target, piecestore.DefaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
return ErrNodeOffline.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { _ = client.Close() }()
|
|
|
|
|
2022-09-21 15:55:46 +01:00
|
|
|
for i, segment := range segments {
|
2022-09-19 13:40:22 +01:00
|
|
|
downloadStart := time.Now()
|
2022-09-12 20:22:01 +01:00
|
|
|
err := service.verifySegment(ctx, client, target, segment)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2022-09-21 15:55:46 +01:00
|
|
|
throttle := service.config.RequestThrottle - time.Since(downloadStart)
|
|
|
|
if !ignoreThrottle && throttle > 0 && i < len(segments)-1 {
|
2022-09-19 13:40:22 +01:00
|
|
|
if !sync2.Sleep(ctx, throttle) {
|
|
|
|
return Error.Wrap(ctx.Err())
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 20:22:01 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifySegment tries to verify the segment by downloading a single byte from the specified segment.
|
|
|
|
func (service *NodeVerifier) verifySegment(ctx context.Context, client *piecestore.Client, target storj.NodeURL, segment *Segment) error {
|
|
|
|
limit, piecePrivateKey, _, err := service.orders.CreateAuditOrderLimit(ctx, target.ID, 0, segment.RootPieceID, 1)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("failed to create order limit",
|
2022-09-15 15:23:10 +01:00
|
|
|
zap.Stringer("retrying in", service.config.OrderRetryThrottle),
|
2022-09-12 20:22:01 +01:00
|
|
|
zap.String("stream-id", segment.StreamID.String()),
|
|
|
|
zap.Uint64("position", segment.Position.Encode()),
|
|
|
|
zap.Error(err))
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
if !sync2.Sleep(ctx, service.config.OrderRetryThrottle) {
|
2022-09-12 20:22:01 +01:00
|
|
|
return Error.Wrap(ctx.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
limit, piecePrivateKey, _, err = service.orders.CreateAuditOrderLimit(ctx, target.ID, 0, segment.RootPieceID, 1)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, service.config.PerPieceTimeout)
|
2022-09-12 20:22:01 +01:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
downloader, err := client.Download(timedCtx, limit.GetLimit(), piecePrivateKey, 0, 1)
|
|
|
|
if err != nil {
|
|
|
|
if errs2.IsRPC(err, rpcstatus.NotFound) {
|
|
|
|
service.log.Info("segment not found",
|
|
|
|
zap.String("stream-id", segment.StreamID.String()),
|
|
|
|
zap.Uint64("position", segment.Position.Encode()),
|
|
|
|
zap.Error(err))
|
|
|
|
segment.Status.MarkNotFound()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
service.log.Error("download failed",
|
|
|
|
zap.String("stream-id", segment.StreamID.String()),
|
|
|
|
zap.Uint64("position", segment.Position.Encode()),
|
|
|
|
zap.Error(err))
|
|
|
|
return ErrNodeOffline.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := [1]byte{}
|
2022-09-21 15:55:46 +01:00
|
|
|
_, errRead := io.ReadFull(downloader, buf[:])
|
|
|
|
errClose := downloader.Close()
|
|
|
|
|
|
|
|
err = errs.Combine(errClose, errRead)
|
2022-09-12 20:22:01 +01:00
|
|
|
if err != nil {
|
|
|
|
if errs2.IsRPC(err, rpcstatus.NotFound) {
|
|
|
|
service.log.Info("segment not found",
|
|
|
|
zap.String("stream-id", segment.StreamID.String()),
|
|
|
|
zap.Uint64("position", segment.Position.Encode()),
|
|
|
|
zap.Error(err))
|
|
|
|
segment.Status.MarkNotFound()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:55:46 +01:00
|
|
|
service.log.Error("read/close failed",
|
2022-09-12 20:22:01 +01:00
|
|
|
zap.String("stream-id", segment.StreamID.String()),
|
|
|
|
zap.Uint64("position", segment.Position.Encode()),
|
|
|
|
zap.Error(err))
|
|
|
|
return ErrNodeOffline.Wrap(err)
|
|
|
|
}
|
|
|
|
segment.Status.MarkFound()
|
|
|
|
|
|
|
|
return nil
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|