2022-09-12 20:22:01 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2022-09-15 15:23:10 +01:00
|
|
|
"time"
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-06 13:26:57 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
segmentverify "storj.io/storj/cmd/tools/segment-verify"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite/metabase"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifier(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2022-10-06 13:26:57 +01:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: testplanet.ReconfigureRS(4, 4, 4, 4),
|
|
|
|
},
|
2022-09-12 20:22:01 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
2022-10-06 13:26:57 +01:00
|
|
|
snoCount := int32(len(planet.StorageNodes))
|
|
|
|
|
2022-09-21 15:55:46 +01:00
|
|
|
config := segmentverify.VerifierConfig{
|
|
|
|
PerPieceTimeout: time.Second,
|
|
|
|
OrderRetryThrottle: 500 * time.Millisecond,
|
|
|
|
RequestThrottle: 500 * time.Millisecond,
|
|
|
|
}
|
2022-09-15 15:23:10 +01:00
|
|
|
service := segmentverify.NewVerifier(
|
|
|
|
planet.Log().Named("verifier"),
|
|
|
|
satellite.Dialer,
|
|
|
|
satellite.Orders.Service,
|
2022-09-21 15:55:46 +01:00
|
|
|
config)
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
// upload some data
|
|
|
|
data := testrand.Bytes(8 * memory.KiB)
|
|
|
|
for _, up := range planet.Uplinks {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
err := up.Upload(ctx, satellite, "bucket1", strconv.Itoa(i), data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := satellite.Metabase.DB.ListVerifySegments(ctx, metabase.ListVerifySegments{
|
|
|
|
CursorStreamID: uuid.UUID{},
|
|
|
|
CursorPosition: metabase.SegmentPosition{},
|
2022-09-21 15:55:46 +01:00
|
|
|
Limit: 10000,
|
2022-09-12 20:22:01 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
validSegments := []*segmentverify.Segment{}
|
|
|
|
for _, raw := range result.Segments {
|
|
|
|
validSegments = append(validSegments, &segmentverify.Segment{
|
|
|
|
VerifySegment: raw,
|
2022-10-06 13:26:57 +01:00
|
|
|
Status: segmentverify.Status{Retry: snoCount},
|
2022-09-12 20:22:01 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-06 13:26:57 +01:00
|
|
|
aliasMap, err := satellite.Metabase.DB.LatestNodesAliasMap(ctx)
|
2022-09-12 20:22:01 +01:00
|
|
|
require.NoError(t, err)
|
2022-10-06 13:26:57 +01:00
|
|
|
|
|
|
|
var g errgroup.Group
|
|
|
|
for _, node := range planet.StorageNodes {
|
|
|
|
node := node
|
|
|
|
alias, ok := aliasMap.Alias(node.ID())
|
|
|
|
require.True(t, ok)
|
|
|
|
g.Go(func() error {
|
|
|
|
return service.Verify(ctx, alias, node.NodeURL(), validSegments, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(t, g.Wait())
|
2022-09-12 20:22:01 +01:00
|
|
|
for _, seg := range validSegments {
|
2022-10-06 13:26:57 +01:00
|
|
|
require.Equal(t, segmentverify.Status{Found: snoCount, NotFound: 0, Retry: 0}, seg.Status)
|
2022-09-12 20:22:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// segment not found
|
2022-10-06 13:26:57 +01:00
|
|
|
alias0, ok := aliasMap.Alias(planet.StorageNodes[0].ID())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-09-21 15:55:46 +01:00
|
|
|
validSegment0 := &segmentverify.Segment{
|
|
|
|
VerifySegment: result.Segments[0],
|
|
|
|
Status: segmentverify.Status{Retry: 1},
|
|
|
|
}
|
2022-09-12 20:22:01 +01:00
|
|
|
missingSegment := &segmentverify.Segment{
|
|
|
|
VerifySegment: metabase.VerifySegment{
|
|
|
|
StreamID: testrand.UUID(),
|
|
|
|
Position: metabase.SegmentPosition{},
|
|
|
|
RootPieceID: testrand.PieceID(),
|
2022-10-06 13:26:57 +01:00
|
|
|
Redundancy: result.Segments[0].Redundancy,
|
|
|
|
AliasPieces: metabase.AliasPieces{{Number: 0, Alias: alias0}},
|
2022-09-12 20:22:01 +01:00
|
|
|
},
|
|
|
|
Status: segmentverify.Status{Retry: 1},
|
|
|
|
}
|
2022-09-21 15:55:46 +01:00
|
|
|
validSegment1 := &segmentverify.Segment{
|
|
|
|
VerifySegment: result.Segments[1],
|
|
|
|
Status: segmentverify.Status{Retry: 1},
|
|
|
|
}
|
|
|
|
|
2022-10-06 13:26:57 +01:00
|
|
|
err = service.Verify(ctx, alias0, planet.StorageNodes[0].NodeURL(),
|
2022-09-21 15:55:46 +01:00
|
|
|
[]*segmentverify.Segment{validSegment0, missingSegment, validSegment1}, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, segmentverify.Status{Found: 1}, validSegment0.Status)
|
|
|
|
require.Equal(t, segmentverify.Status{NotFound: 1}, missingSegment.Status)
|
|
|
|
require.Equal(t, segmentverify.Status{Found: 1}, validSegment1.Status)
|
2022-09-12 20:22:01 +01:00
|
|
|
|
2022-09-21 15:55:46 +01:00
|
|
|
// Test throttling
|
|
|
|
verifyStart := time.Now()
|
|
|
|
const throttleN = 5
|
2022-10-06 13:26:57 +01:00
|
|
|
err = service.Verify(ctx, alias0, planet.StorageNodes[0].NodeURL(), validSegments[:throttleN], false)
|
2022-09-12 20:22:01 +01:00
|
|
|
require.NoError(t, err)
|
2022-09-21 15:55:46 +01:00
|
|
|
verifyDuration := time.Since(verifyStart)
|
|
|
|
require.Greater(t, verifyDuration, config.RequestThrottle*(throttleN-1))
|
2022-09-12 20:22:01 +01:00
|
|
|
|
|
|
|
// TODO: test download timeout
|
|
|
|
|
|
|
|
// node offline
|
|
|
|
err = planet.StopNodeAndUpdate(ctx, planet.StorageNodes[0])
|
|
|
|
require.NoError(t, err)
|
2022-10-06 13:26:57 +01:00
|
|
|
err = service.Verify(ctx, alias0, planet.StorageNodes[0].NodeURL(), validSegments, true)
|
2022-09-12 20:22:01 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, segmentverify.ErrNodeOffline.Has(err))
|
|
|
|
})
|
|
|
|
}
|