2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-09 22:10:37 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
2019-03-19 17:37:26 +00:00
|
|
|
"time"
|
2018-10-09 22:10:37 +01:00
|
|
|
|
|
|
|
"github.com/vivint/infectious"
|
2019-01-29 20:42:27 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-03-18 10:55:06 +00:00
|
|
|
"go.uber.org/zap"
|
2018-12-14 20:17:30 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-10-09 22:10:37 +01:00
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/auth/signing"
|
2019-01-30 20:47:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-10-09 22:10:37 +01:00
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-05-23 21:07:19 +01:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-09 22:10:37 +01:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-03-28 20:09:23 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/uplink/piecestore"
|
2018-10-09 22:10:37 +01:00
|
|
|
)
|
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
2019-05-27 12:13:47 +01:00
|
|
|
|
|
|
|
// ErrNotEnoughShares is the errs class for when not enough shares are available to do an audit
|
|
|
|
ErrNotEnoughShares = errs.Class("not enough shares for successful audit")
|
2019-03-19 17:37:26 +00:00
|
|
|
)
|
2018-10-09 22:10:37 +01:00
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// Share represents required information about an audited share
|
|
|
|
type Share struct {
|
2019-03-18 10:55:06 +00:00
|
|
|
Error error
|
|
|
|
PieceNum int
|
|
|
|
Data []byte
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// Verifier helps verify the correctness of a given stripe
|
|
|
|
type Verifier struct {
|
2019-06-03 10:17:09 +01:00
|
|
|
log *zap.Logger
|
|
|
|
orders *orders.Service
|
|
|
|
auditor *identity.PeerIdentity
|
|
|
|
transport transport.Client
|
|
|
|
overlay *overlay.Cache
|
|
|
|
containment Containment
|
|
|
|
minBytesPerSecond memory.Size
|
|
|
|
minDownloadTimeout time.Duration
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// NewVerifier creates a Verifier
|
2019-06-03 10:17:09 +01:00
|
|
|
func NewVerifier(log *zap.Logger, transport transport.Client, overlay *overlay.Cache, containment Containment, orders *orders.Service, id *identity.FullIdentity, minBytesPerSecond memory.Size, minDownloadTimeout time.Duration) *Verifier {
|
2019-05-27 12:13:47 +01:00
|
|
|
return &Verifier{
|
2019-06-03 10:17:09 +01:00
|
|
|
log: log,
|
|
|
|
orders: orders,
|
|
|
|
auditor: id.PeerIdentity(),
|
|
|
|
transport: transport,
|
|
|
|
overlay: overlay,
|
|
|
|
containment: containment,
|
|
|
|
minBytesPerSecond: minBytesPerSecond,
|
|
|
|
minDownloadTimeout: minDownloadTimeout,
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 10:54:37 +00:00
|
|
|
// Verify downloads shares then verifies the data correctness at the given stripe
|
2019-05-27 12:13:47 +01:00
|
|
|
func (verifier *Verifier) Verify(ctx context.Context, stripe *Stripe, skip map[storj.NodeID]bool) (report *Report, err error) {
|
2019-03-20 10:54:37 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
pointer := stripe.Segment
|
|
|
|
shareSize := pointer.GetRemote().GetRedundancy().GetErasureShareSize()
|
2019-03-28 20:09:23 +00:00
|
|
|
bucketID := createBucketID(stripe.SegmentPath)
|
|
|
|
|
2019-05-27 12:13:47 +01:00
|
|
|
// TODO(kaloyan): CreateAuditOrderLimits checks overlay cache if nodes are online and won't return
|
|
|
|
// order limits for offline node. So we miss to record them as offline during the audit
|
|
|
|
orderLimits, err := verifier.orders.CreateAuditOrderLimits(ctx, verifier.auditor, bucketID, pointer, skip)
|
2019-03-28 20:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-20 10:54:37 +00:00
|
|
|
|
2019-05-17 19:48:32 +01:00
|
|
|
shares, nodes, err := verifier.DownloadShares(ctx, orderLimits, stripe.Index, shareSize)
|
2019-03-20 10:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var offlineNodes storj.NodeIDList
|
|
|
|
var failedNodes storj.NodeIDList
|
2019-05-23 21:07:19 +01:00
|
|
|
containedNodes := make(map[int]storj.NodeID)
|
2019-03-20 10:54:37 +00:00
|
|
|
sharesToAudit := make(map[int]Share)
|
|
|
|
|
|
|
|
for pieceNum, share := range shares {
|
2019-05-24 17:57:07 +01:00
|
|
|
if share.Error != nil {
|
2019-05-23 21:07:19 +01:00
|
|
|
// TODO(kaloyan): we need to check the logic here if we correctly identify offline nodes from those that didn't respond.
|
2019-05-27 12:13:47 +01:00
|
|
|
if share.Error == context.DeadlineExceeded || !transport.Error.Has(share.Error) || ContainError.Has(share.Error) {
|
2019-05-23 21:07:19 +01:00
|
|
|
containedNodes[pieceNum] = nodes[pieceNum]
|
2019-03-20 10:54:37 +00:00
|
|
|
} else {
|
|
|
|
offlineNodes = append(offlineNodes, nodes[pieceNum])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sharesToAudit[pieceNum] = share
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
required := int(pointer.Remote.Redundancy.GetMinReq())
|
|
|
|
total := int(pointer.Remote.Redundancy.GetTotal())
|
|
|
|
|
|
|
|
if len(sharesToAudit) < required {
|
2019-05-23 23:32:19 +01:00
|
|
|
return &Report{
|
|
|
|
Offlines: offlineNodes,
|
2019-05-27 12:13:47 +01:00
|
|
|
}, ErrNotEnoughShares.New("got %d, required %d", len(sharesToAudit), required)
|
2019-03-20 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:07:19 +01:00
|
|
|
pieceNums, correctedShares, err := auditShares(ctx, required, total, sharesToAudit)
|
2019-03-20 10:54:37 +00:00
|
|
|
if err != nil {
|
2019-05-23 23:32:19 +01:00
|
|
|
return &Report{
|
|
|
|
Offlines: offlineNodes,
|
2019-05-23 21:07:19 +01:00
|
|
|
}, err
|
2019-03-20 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pieceNum := range pieceNums {
|
|
|
|
failedNodes = append(failedNodes, nodes[pieceNum])
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:07:19 +01:00
|
|
|
successNodes := getSuccessNodes(ctx, nodes, failedNodes, offlineNodes, containedNodes)
|
|
|
|
|
2019-05-31 21:46:25 +01:00
|
|
|
totalInPointer := len(stripe.Segment.GetRemote().GetRemotePieces())
|
|
|
|
totalAudited := len(successNodes) + len(failedNodes) + len(offlineNodes)
|
|
|
|
numOffline := len(offlineNodes)
|
|
|
|
numSuccessful := len(successNodes)
|
|
|
|
numFailed := len(failedNodes)
|
|
|
|
auditedPercentage := float64(totalAudited) / float64(totalInPointer)
|
|
|
|
offlinePercentage := float64(0)
|
|
|
|
successfulPercentage := float64(0)
|
|
|
|
failedPercentage := float64(0)
|
|
|
|
if totalAudited > 0 {
|
|
|
|
offlinePercentage = float64(numOffline) / float64(totalAudited)
|
|
|
|
successfulPercentage = float64(numSuccessful) / float64(totalAudited)
|
|
|
|
failedPercentage = float64(numFailed) / float64(totalAudited)
|
|
|
|
}
|
|
|
|
|
|
|
|
mon.Meter("audit_success_nodes_global").Mark(numSuccessful)
|
|
|
|
mon.Meter("audit_fail_nodes_global").Mark(numFailed)
|
|
|
|
mon.Meter("audit_offline_nodes_global").Mark(numOffline)
|
|
|
|
mon.Meter("audit_total_nodes_global").Mark(totalAudited)
|
|
|
|
mon.Meter("audit_total_pointer_nodes_global").Mark(totalInPointer)
|
|
|
|
|
|
|
|
mon.IntVal("audit_success_nodes").Observe(int64(len(successNodes)))
|
|
|
|
mon.IntVal("audit_fail_nodes").Observe(int64(len(failedNodes)))
|
|
|
|
mon.IntVal("audit_offline_nodes").Observe(int64(len(offlineNodes)))
|
|
|
|
mon.IntVal("audit_total_nodes").Observe(int64(totalAudited))
|
|
|
|
mon.IntVal("audit_total_pointer_nodes").Observe(int64(totalInPointer))
|
|
|
|
mon.FloatVal("audited_percentage").Observe(auditedPercentage)
|
|
|
|
mon.FloatVal("audit_offline_percentage").Observe(offlinePercentage)
|
|
|
|
mon.FloatVal("audit_successful_percentage").Observe(successfulPercentage)
|
|
|
|
mon.FloatVal("audit_failed_percentage").Observe(failedPercentage)
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
pendingAudits, err := createPendingAudits(ctx, containedNodes, correctedShares, stripe)
|
2019-05-23 21:07:19 +01:00
|
|
|
if err != nil {
|
2019-05-23 23:32:19 +01:00
|
|
|
return &Report{
|
|
|
|
Successes: successNodes,
|
|
|
|
Fails: failedNodes,
|
|
|
|
Offlines: offlineNodes,
|
2019-05-23 21:07:19 +01:00
|
|
|
}, err
|
|
|
|
}
|
2019-03-20 10:54:37 +00:00
|
|
|
|
2019-05-23 23:32:19 +01:00
|
|
|
return &Report{
|
|
|
|
Successes: successNodes,
|
|
|
|
Fails: failedNodes,
|
|
|
|
Offlines: offlineNodes,
|
|
|
|
PendingAudits: pendingAudits,
|
2019-03-20 10:54:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 19:48:32 +01:00
|
|
|
// DownloadShares downloads shares from the nodes where remote pieces are located
|
|
|
|
func (verifier *Verifier) DownloadShares(ctx context.Context, limits []*pb.AddressedOrderLimit, stripeIndex int64, shareSize int32) (shares map[int]Share, nodes map[int]storj.NodeID, err error) {
|
2019-03-20 10:54:37 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
shares = make(map[int]Share, len(limits))
|
|
|
|
nodes = make(map[int]storj.NodeID, len(limits))
|
|
|
|
|
|
|
|
for i, limit := range limits {
|
|
|
|
if limit == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:13:47 +01:00
|
|
|
// TODO(kaloyan): execute in goroutine for better performance
|
|
|
|
share, err := verifier.GetShare(ctx, limit, stripeIndex, shareSize, i)
|
2019-03-20 10:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
share = Share{
|
|
|
|
Error: err,
|
|
|
|
PieceNum: i,
|
|
|
|
Data: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shares[share.PieceNum] = share
|
|
|
|
nodes[share.PieceNum] = limit.GetLimit().StorageNodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
return shares, nodes, nil
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:13:47 +01:00
|
|
|
// Reverify reverifies the contained nodes in the stripe
|
|
|
|
func (verifier *Verifier) Reverify(ctx context.Context, stripe *Stripe) (report *Report, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// result status enum
|
|
|
|
const (
|
|
|
|
skipped = iota
|
|
|
|
success
|
|
|
|
offline
|
|
|
|
failed
|
|
|
|
contained
|
|
|
|
erred
|
|
|
|
)
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
nodeID storj.NodeID
|
|
|
|
status int
|
|
|
|
pendingAudit *PendingAudit
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
pieces := stripe.Segment.GetRemote().GetRemotePieces()
|
|
|
|
ch := make(chan result, len(pieces))
|
|
|
|
|
|
|
|
for _, piece := range pieces {
|
|
|
|
pending, err := verifier.containment.Get(ctx, piece.NodeId)
|
|
|
|
if err != nil {
|
|
|
|
if ErrContainedNotFound.Has(err) {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: skipped}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: erred, err: err}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(pending *PendingAudit, piece *pb.RemotePiece) {
|
|
|
|
limit, err := verifier.orders.CreateAuditOrderLimit(ctx, verifier.auditor, createBucketID(stripe.SegmentPath), pending.NodeID, pending.PieceID, pending.ShareSize)
|
|
|
|
if err != nil {
|
|
|
|
if overlay.ErrNodeOffline.Has(err) {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: offline}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: erred, err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
share, err := verifier.GetShare(ctx, limit, pending.StripeIndex, pending.ShareSize, int(piece.PieceNum))
|
|
|
|
if err != nil {
|
|
|
|
// TODO(kaloyan): we need to check the logic here if we correctly identify offline nodes from those that didn't respond.
|
|
|
|
if err == context.DeadlineExceeded || !transport.Error.Has(err) {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: contained}
|
|
|
|
} else {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: offline}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadedHash := pkcrypto.SHA256Hash(share.Data)
|
|
|
|
if bytes.Equal(downloadedHash, pending.ExpectedShareHash) {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: success}
|
|
|
|
} else {
|
|
|
|
ch <- result{nodeID: piece.NodeId, status: failed}
|
|
|
|
}
|
|
|
|
}(pending, piece)
|
|
|
|
}
|
|
|
|
|
|
|
|
report = &Report{}
|
|
|
|
for range pieces {
|
|
|
|
result := <-ch
|
|
|
|
switch result.status {
|
|
|
|
case success:
|
|
|
|
report.Successes = append(report.Successes, result.nodeID)
|
|
|
|
case offline:
|
|
|
|
report.Offlines = append(report.Offlines, result.nodeID)
|
|
|
|
case failed:
|
|
|
|
report.Fails = append(report.Fails, result.nodeID)
|
|
|
|
case contained:
|
|
|
|
report.PendingAudits = append(report.PendingAudits, result.pendingAudit)
|
|
|
|
case erred:
|
|
|
|
err = errs.Combine(err, result.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return report, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetShare use piece store client to download shares from nodes
|
|
|
|
func (verifier *Verifier) GetShare(ctx context.Context, limit *pb.AddressedOrderLimit, stripeIndex int64, shareSize int32, pieceNum int) (share Share, err error) {
|
2018-10-09 22:10:37 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-10 20:13:40 +00:00
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
bandwidthMsgSize := shareSize
|
|
|
|
|
|
|
|
// determines number of seconds allotted for receiving data from a storage node
|
2019-03-22 13:14:17 +00:00
|
|
|
timedCtx := ctx
|
2019-05-17 19:48:32 +01:00
|
|
|
if verifier.minBytesPerSecond > 0 {
|
|
|
|
maxTransferTime := time.Duration(int64(time.Second) * int64(bandwidthMsgSize) / verifier.minBytesPerSecond.Int64())
|
2019-06-03 10:17:09 +01:00
|
|
|
if maxTransferTime < verifier.minDownloadTimeout {
|
|
|
|
maxTransferTime = verifier.minDownloadTimeout
|
2019-04-03 18:17:29 +01:00
|
|
|
}
|
2019-03-22 13:14:17 +00:00
|
|
|
var cancel func()
|
|
|
|
timedCtx, cancel = context.WithTimeout(ctx, maxTransferTime)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2019-03-19 17:37:26 +00:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
storageNodeID := limit.GetLimit().StorageNodeId
|
2018-10-09 22:10:37 +01:00
|
|
|
|
2019-05-17 19:48:32 +01:00
|
|
|
conn, err := verifier.transport.DialNode(timedCtx, &pb.Node{
|
2019-03-18 10:55:06 +00:00
|
|
|
Id: storageNodeID,
|
|
|
|
Address: limit.GetStorageNodeAddress(),
|
|
|
|
})
|
2018-10-09 22:10:37 +01:00
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return Share{}, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
ps := piecestore.NewClient(
|
2019-05-17 19:48:32 +01:00
|
|
|
verifier.log.Named(storageNodeID.String()),
|
|
|
|
signing.SignerFromFullIdentity(verifier.transport.Identity()),
|
2019-03-18 10:55:06 +00:00
|
|
|
conn,
|
|
|
|
piecestore.DefaultConfig,
|
|
|
|
)
|
2019-04-03 14:42:24 +01:00
|
|
|
defer func() {
|
|
|
|
err := ps.Close()
|
|
|
|
if err != nil {
|
2019-05-17 19:48:32 +01:00
|
|
|
verifier.log.Error("audit verifier failed to close conn to node: %+v", zap.Error(err))
|
2019-04-03 14:42:24 +01:00
|
|
|
}
|
|
|
|
}()
|
2018-10-09 22:10:37 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
offset := int64(shareSize) * stripeIndex
|
2018-10-09 22:10:37 +01:00
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
downloader, err := ps.Download(timedCtx, limit.GetLimit(), offset, int64(shareSize))
|
2018-10-09 22:10:37 +01:00
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return Share{}, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
defer func() { err = errs.Combine(err, downloader.Close()) }()
|
2018-10-09 22:10:37 +01:00
|
|
|
|
|
|
|
buf := make([]byte, shareSize)
|
2019-03-18 10:55:06 +00:00
|
|
|
_, err = io.ReadFull(downloader, buf)
|
2018-10-09 22:10:37 +01:00
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return Share{}, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return Share{
|
|
|
|
Error: nil,
|
|
|
|
PieceNum: pieceNum,
|
|
|
|
Data: buf,
|
|
|
|
}, nil
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// auditShares takes the downloaded shares and uses infectious's Correct function to check that they
|
2019-05-23 21:07:19 +01:00
|
|
|
// haven't been altered. auditShares returns a slice containing the piece numbers of altered shares,
|
|
|
|
// and a slice of the corrected shares.
|
|
|
|
func auditShares(ctx context.Context, required, total int, originals map[int]Share) (pieceNums []int, corrected []infectious.Share, err error) {
|
2018-10-09 22:10:37 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
f, err := infectious.NewFEC(required, total)
|
|
|
|
if err != nil {
|
2019-05-23 21:07:19 +01:00
|
|
|
return nil, nil, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
2018-11-07 01:16:43 +00:00
|
|
|
|
2018-10-09 22:10:37 +01:00
|
|
|
copies, err := makeCopies(ctx, originals)
|
|
|
|
if err != nil {
|
2019-05-23 21:07:19 +01:00
|
|
|
return nil, nil, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Correct(copies)
|
|
|
|
if err != nil {
|
2019-05-23 21:07:19 +01:00
|
|
|
return nil, nil, err
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
2019-03-19 17:37:26 +00:00
|
|
|
|
2018-11-28 07:33:17 +00:00
|
|
|
for _, share := range copies {
|
|
|
|
if !bytes.Equal(originals[share.Number].Data, share.Data) {
|
2018-10-09 22:10:37 +01:00
|
|
|
pieceNums = append(pieceNums, share.Number)
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 21:07:19 +01:00
|
|
|
return pieceNums, copies, nil
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 10:54:37 +00:00
|
|
|
// makeCopies takes in a map of audit Shares and deep copies their data to a slice of infectious Shares
|
|
|
|
func makeCopies(ctx context.Context, originals map[int]Share) (copies []infectious.Share, err error) {
|
2018-10-09 22:10:37 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-20 10:54:37 +00:00
|
|
|
copies = make([]infectious.Share, 0, len(originals))
|
|
|
|
for _, original := range originals {
|
|
|
|
copies = append(copies, infectious.Share{
|
|
|
|
Data: append([]byte{}, original.Data...),
|
|
|
|
Number: original.PieceNum})
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
2019-03-20 10:54:37 +00:00
|
|
|
return copies, nil
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 21:07:19 +01:00
|
|
|
// getSuccessNodes uses the failed nodes, offline nodes and contained nodes arrays to determine which nodes passed the audit
|
|
|
|
func getSuccessNodes(ctx context.Context, nodes map[int]storj.NodeID, failedNodes, offlineNodes storj.NodeIDList, containedNodes map[int]storj.NodeID) (successNodes storj.NodeIDList) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2018-11-29 18:39:27 +00:00
|
|
|
fails := make(map[storj.NodeID]bool)
|
2018-10-16 18:40:34 +01:00
|
|
|
for _, fail := range failedNodes {
|
|
|
|
fails[fail] = true
|
2018-10-09 22:10:37 +01:00
|
|
|
}
|
2018-10-16 18:40:34 +01:00
|
|
|
for _, offline := range offlineNodes {
|
|
|
|
fails[offline] = true
|
|
|
|
}
|
2019-05-23 21:07:19 +01:00
|
|
|
for _, contained := range containedNodes {
|
|
|
|
fails[contained] = true
|
|
|
|
}
|
2018-10-16 18:40:34 +01:00
|
|
|
|
|
|
|
for _, node := range nodes {
|
2019-02-01 14:48:57 +00:00
|
|
|
if !fails[node] {
|
|
|
|
successNodes = append(successNodes, node)
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 14:48:57 +00:00
|
|
|
|
2018-10-16 18:40:34 +01:00
|
|
|
return successNodes
|
|
|
|
}
|
2019-03-28 20:09:23 +00:00
|
|
|
|
|
|
|
func createBucketID(path storj.Path) []byte {
|
|
|
|
comps := storj.SplitPath(path)
|
2019-04-01 21:14:58 +01:00
|
|
|
if len(comps) < 3 {
|
2019-03-28 20:09:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-01 21:14:58 +01:00
|
|
|
// project_id/bucket_name
|
|
|
|
return []byte(storj.JoinPaths(comps[0], comps[2]))
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
2019-05-23 21:07:19 +01:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
func createPendingAudits(ctx context.Context, containedNodes map[int]storj.NodeID, correctedShares []infectious.Share, stripe *Stripe) (_ []*PendingAudit, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-05-23 21:07:19 +01:00
|
|
|
if len(containedNodes) > 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
redundancy := stripe.Segment.GetRemote().GetRedundancy()
|
|
|
|
required := int(redundancy.GetMinReq())
|
|
|
|
total := int(redundancy.GetTotal())
|
|
|
|
shareSize := redundancy.GetErasureShareSize()
|
|
|
|
|
|
|
|
fec, err := infectious.NewFEC(required, total)
|
|
|
|
if err != nil {
|
2019-05-27 12:13:47 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-05-23 21:07:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
stripeData, err := rebuildStripe(ctx, fec, correctedShares, int(shareSize))
|
2019-05-23 21:07:19 +01:00
|
|
|
if err != nil {
|
2019-05-27 12:13:47 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-05-23 21:07:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var pendingAudits []*PendingAudit
|
|
|
|
for pieceNum, nodeID := range containedNodes {
|
|
|
|
share := make([]byte, shareSize)
|
|
|
|
err = fec.EncodeSingle(stripeData, share, pieceNum)
|
|
|
|
if err != nil {
|
2019-05-27 12:13:47 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-05-23 21:07:19 +01:00
|
|
|
}
|
|
|
|
pendingAudits = append(pendingAudits, &PendingAudit{
|
|
|
|
NodeID: nodeID,
|
|
|
|
PieceID: stripe.Segment.GetRemote().RootPieceId,
|
|
|
|
StripeIndex: stripe.Index,
|
|
|
|
ShareSize: shareSize,
|
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(share),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return pendingAudits, nil
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
func rebuildStripe(ctx context.Context, fec *infectious.FEC, corrected []infectious.Share, shareSize int) (_ []byte, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-05-23 21:07:19 +01:00
|
|
|
stripe := make([]byte, fec.Required()*shareSize)
|
2019-06-04 12:36:27 +01:00
|
|
|
err = fec.Rebuild(corrected, func(share infectious.Share) {
|
2019-05-23 21:07:19 +01:00
|
|
|
copy(stripe[share.Number*shareSize:], share.Data)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stripe, nil
|
|
|
|
}
|