2022-09-09 09:16:22 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-09-12 20:22:01 +01:00
|
|
|
"storj.io/common/storj"
|
2022-09-09 09:16:22 +01:00
|
|
|
"storj.io/common/sync2"
|
2022-09-12 20:22:01 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2022-09-09 09:16:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Verify verifies a collection of segments.
|
2022-09-14 15:15:58 +01:00
|
|
|
func (service *Service) Verify(ctx context.Context, segments []*Segment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-09-09 09:16:22 +01:00
|
|
|
for _, segment := range segments {
|
2022-09-15 15:23:10 +01:00
|
|
|
segment.Status.Retry = int32(service.config.Check)
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:15:58 +01:00
|
|
|
batches, err := service.CreateBatches(ctx, segments)
|
2022-09-09 09:16:22 +01:00
|
|
|
if err != nil {
|
2022-09-14 15:15:58 +01:00
|
|
|
return Error.Wrap(err)
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:22:01 +01:00
|
|
|
err = service.VerifyBatches(ctx, batches)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2022-09-09 09:16:22 +01:00
|
|
|
|
|
|
|
retrySegments := []*Segment{}
|
|
|
|
for _, segment := range segments {
|
|
|
|
if segment.Status.Retry > 0 {
|
|
|
|
retrySegments = append(retrySegments, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:30:38 +01:00
|
|
|
if len(retrySegments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 09:16:22 +01:00
|
|
|
// Reverse the pieces slice to ensure we pick different nodes this time.
|
|
|
|
for _, segment := range retrySegments {
|
2022-09-13 14:28:26 +01:00
|
|
|
xs := segment.AliasPieces
|
2022-09-09 09:16:22 +01:00
|
|
|
for i, j := 0, len(xs)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
xs[i], xs[j] = xs[j], xs[i]
|
|
|
|
}
|
|
|
|
// Also remove priority nodes, because we have already checked them.
|
|
|
|
service.removePriorityPieces(segment)
|
|
|
|
}
|
|
|
|
|
2022-09-14 15:15:58 +01:00
|
|
|
retryBatches, err := service.CreateBatches(ctx, retrySegments)
|
2022-09-09 09:16:22 +01:00
|
|
|
if err != nil {
|
2022-09-14 15:15:58 +01:00
|
|
|
return Error.Wrap(err)
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:22:01 +01:00
|
|
|
err = service.VerifyBatches(ctx, retryBatches)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2022-09-09 09:16:22 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyBatches verifies batches.
|
2022-09-12 20:22:01 +01:00
|
|
|
func (service *Service) VerifyBatches(ctx context.Context, batches []*Batch) error {
|
2022-09-14 15:15:58 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
|
2022-09-09 09:16:22 +01:00
|
|
|
var mu sync.Mutex
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
limiter := sync2.NewLimiter(service.config.Concurrency)
|
2022-09-16 16:35:19 +01:00
|
|
|
for _, batch := range batches {
|
2022-09-09 09:16:22 +01:00
|
|
|
batch := batch
|
2022-09-16 16:35:19 +01:00
|
|
|
|
|
|
|
nodeURL, err := service.convertAliasToNodeURL(ctx, batch.Alias)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2022-09-19 13:40:22 +01:00
|
|
|
ignoreThrottle := service.priorityNodes.Contains(batch.Alias)
|
|
|
|
|
2022-09-09 09:16:22 +01:00
|
|
|
limiter.Go(ctx, func() {
|
2022-10-06 13:26:57 +01:00
|
|
|
err := service.verifier.Verify(ctx, batch.Alias, nodeURL, batch.Items, ignoreThrottle)
|
2022-09-09 09:16:22 +01:00
|
|
|
if err != nil {
|
|
|
|
if ErrNodeOffline.Has(err) {
|
|
|
|
mu.Lock()
|
2022-09-16 16:35:19 +01:00
|
|
|
service.onlineNodes.Remove(batch.Alias)
|
2022-09-09 09:16:22 +01:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
service.log.Error("verifying a batch failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
limiter.Wait()
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
return nil
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|
2022-09-16 16:35:19 +01:00
|
|
|
|
|
|
|
// convertAliasToNodeURL converts a node alias to node url, using a cache if needed.
|
|
|
|
func (service *Service) convertAliasToNodeURL(ctx context.Context, alias metabase.NodeAlias) (_ storj.NodeURL, err error) {
|
|
|
|
nodeURL, ok := service.aliasToNodeURL[alias]
|
|
|
|
if !ok {
|
2022-10-05 15:12:51 +01:00
|
|
|
nodeID, ok := service.aliasMap.Node(alias)
|
|
|
|
if !ok {
|
|
|
|
latest, err := service.metabase.LatestNodesAliasMap(ctx)
|
|
|
|
if !ok {
|
|
|
|
return storj.NodeURL{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
service.aliasMap = latest
|
|
|
|
|
|
|
|
nodeID, ok = service.aliasMap.Node(alias)
|
|
|
|
if !ok {
|
|
|
|
return storj.NodeURL{}, Error.Wrap(err)
|
|
|
|
}
|
2022-09-16 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:12:51 +01:00
|
|
|
info, err := service.overlay.Get(ctx, nodeID)
|
2022-09-16 16:35:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return storj.NodeURL{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeURL = storj.NodeURL{
|
|
|
|
ID: info.Id,
|
|
|
|
Address: info.Address.Address,
|
|
|
|
}
|
|
|
|
|
|
|
|
service.aliasToNodeURL[alias] = nodeURL
|
|
|
|
}
|
|
|
|
return nodeURL, nil
|
|
|
|
}
|