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-12-13 22:52:36 +00:00
|
|
|
retryCount := service.config.Check
|
|
|
|
if retryCount == 0 {
|
|
|
|
retryCount = len(segment.AliasPieces)
|
|
|
|
}
|
|
|
|
segment.Status.Retry = int32(retryCount)
|
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-12-13 22:52:36 +00:00
|
|
|
if service.config.Check <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-21 17:30:38 +01:00
|
|
|
|
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
|
|
|
|
2022-12-21 15:14:53 +00:00
|
|
|
info, err := service.GetNodeInfo(ctx, batch.Alias)
|
2022-09-16 16:35:19 +01:00
|
|
|
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-12-21 15:14:53 +00:00
|
|
|
verifiedCount, err := service.verifier.Verify(ctx, batch.Alias, info.NodeURL, info.Version, batch.Items, ignoreThrottle)
|
2022-09-09 09:16:22 +01:00
|
|
|
if err != nil {
|
|
|
|
if ErrNodeOffline.Has(err) {
|
|
|
|
mu.Lock()
|
2022-10-13 14:25:12 +01:00
|
|
|
if verifiedCount == 0 {
|
2023-01-02 19:37:21 +00:00
|
|
|
service.offlineNodes.Add(batch.Alias)
|
2022-10-13 14:25:12 +01:00
|
|
|
} else {
|
|
|
|
service.offlineCount[batch.Alias]++
|
2022-12-13 22:52:36 +00:00
|
|
|
if service.config.MaxOffline > 0 && service.offlineCount[batch.Alias] >= service.config.MaxOffline {
|
2023-01-02 19:37:21 +00:00
|
|
|
service.offlineNodes.Add(batch.Alias)
|
2022-10-13 14:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 09:16:22 +01:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
service.log.Error("verifying a batch failed", zap.Error(err))
|
2022-10-13 14:25:12 +01:00
|
|
|
} else {
|
|
|
|
mu.Lock()
|
|
|
|
if service.offlineCount[batch.Alias] > 0 {
|
|
|
|
service.offlineCount[batch.Alias]--
|
|
|
|
}
|
|
|
|
mu.Unlock()
|
2022-09-09 09:16:22 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:14:53 +00:00
|
|
|
// TODO: single responsibility?
|
|
|
|
service.nodesVersionMap[alias] = info.Version.Version
|
|
|
|
|
2022-09-16 16:35:19 +01:00
|
|
|
nodeURL = storj.NodeURL{
|
|
|
|
ID: info.Id,
|
|
|
|
Address: info.Address.Address,
|
|
|
|
}
|
|
|
|
|
|
|
|
service.aliasToNodeURL[alias] = nodeURL
|
|
|
|
}
|
|
|
|
return nodeURL, nil
|
|
|
|
}
|
2022-12-21 15:14:53 +00:00
|
|
|
|
|
|
|
// NodeInfo contains node information.
|
|
|
|
type NodeInfo struct {
|
|
|
|
Version string
|
|
|
|
NodeURL storj.NodeURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeInfo retrieves node information, using a cache if needed.
|
|
|
|
func (service *Service) GetNodeInfo(ctx context.Context, alias metabase.NodeAlias) (NodeInfo, error) {
|
|
|
|
nodeURL, err := service.convertAliasToNodeURL(ctx, alias)
|
|
|
|
if err != nil {
|
|
|
|
return NodeInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
version, ok := service.nodesVersionMap[alias]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
info, err := service.overlay.Get(ctx, nodeURL.ID)
|
|
|
|
if err != nil {
|
|
|
|
return NodeInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
service.nodesVersionMap[alias] = info.Version.Version
|
|
|
|
version = info.Version.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
return NodeInfo{
|
|
|
|
NodeURL: nodeURL,
|
|
|
|
Version: version,
|
|
|
|
}, nil
|
|
|
|
}
|