2019-10-07 21:38:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gracefulexit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-10-24 17:24:42 +01:00
|
|
|
"go.uber.org/zap"
|
2019-10-07 21:38:05 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-10-07 21:38:05 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/gracefulexit"
|
2020-09-03 14:54:56 +01:00
|
|
|
"storj.io/storj/satellite/metainfo/metabase"
|
2019-10-07 21:38:05 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChore(t *testing.T) {
|
2019-10-24 17:24:42 +01:00
|
|
|
var maximumInactiveTimeFrame = time.Second * 1
|
2019-10-07 21:38:05 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 8,
|
|
|
|
UplinkCount: 1,
|
2019-10-24 17:24:42 +01:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2020-11-10 11:56:30 +00:00
|
|
|
Satellite: testplanet.Combine(
|
|
|
|
func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.GracefulExit.MaxInactiveTimeFrame = maximumInactiveTimeFrame
|
|
|
|
},
|
|
|
|
testplanet.ReconfigureRS(4, 6, 8, 8),
|
|
|
|
),
|
2019-10-24 17:24:42 +01:00
|
|
|
},
|
2019-10-07 21:38:05 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
uplinkPeer := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
exitingNode := planet.StorageNodes[1]
|
|
|
|
|
|
|
|
satellite.GracefulExit.Chore.Loop.Pause()
|
|
|
|
|
2020-01-21 10:38:41 +00:00
|
|
|
err := uplinkPeer.Upload(ctx, satellite, "testbucket", "test/path1", testrand.Bytes(5*memory.KiB))
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-21 10:38:41 +00:00
|
|
|
err = uplinkPeer.Upload(ctx, satellite, "testbucket", "test/path2", testrand.Bytes(5*memory.KiB))
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-24 17:24:42 +01:00
|
|
|
exitStatusRequest := overlay.ExitStatusRequest{
|
2019-10-07 21:38:05 +01:00
|
|
|
NodeID: exitingNode.ID(),
|
2020-03-10 22:05:01 +00:00
|
|
|
ExitInitiatedAt: time.Now(),
|
2019-10-07 21:38:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:24:42 +01:00
|
|
|
_, err = satellite.Overlay.DB.UpdateExitStatus(ctx, &exitStatusRequest)
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-24 17:24:42 +01:00
|
|
|
exitingNodes, err := satellite.Overlay.DB.GetExitingNodes(ctx)
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-24 17:24:42 +01:00
|
|
|
nodeIDs := make(storj.NodeIDList, 0, len(exitingNodes))
|
|
|
|
for _, exitingNode := range exitingNodes {
|
|
|
|
if exitingNode.ExitLoopCompletedAt == nil {
|
|
|
|
nodeIDs = append(nodeIDs, exitingNode.NodeID)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 21:38:05 +01:00
|
|
|
require.Len(t, nodeIDs, 1)
|
|
|
|
|
|
|
|
satellite.GracefulExit.Chore.Loop.TriggerWait()
|
|
|
|
|
|
|
|
incompleteTransfers, err := satellite.DB.GracefulExit().GetIncomplete(ctx, exitingNode.ID(), 20, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, incompleteTransfers, 2)
|
|
|
|
for _, incomplete := range incompleteTransfers {
|
|
|
|
require.True(t, incomplete.DurabilityRatio > 0)
|
2019-11-07 16:13:05 +00:00
|
|
|
require.NotNil(t, incomplete.RootPieceID)
|
2019-10-07 21:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// test the other nodes don't have anything to transfer
|
2020-05-07 09:23:40 +01:00
|
|
|
for _, node := range planet.StorageNodes {
|
|
|
|
if node.ID() == exitingNode.ID() {
|
2019-10-07 21:38:05 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-05-07 09:23:40 +01:00
|
|
|
incompleteTransfers, err := satellite.DB.GracefulExit().GetIncomplete(ctx, node.ID(), 20, 0)
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, incompleteTransfers, 0)
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:24:42 +01:00
|
|
|
exitingNodes, err = satellite.Overlay.DB.GetExitingNodes(ctx)
|
2019-10-07 21:38:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-24 17:24:42 +01:00
|
|
|
nodeIDs = make(storj.NodeIDList, 0, len(exitingNodes))
|
|
|
|
for _, exitingNode := range exitingNodes {
|
|
|
|
if exitingNode.ExitLoopCompletedAt == nil {
|
|
|
|
nodeIDs = append(nodeIDs, exitingNode.NodeID)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 21:38:05 +01:00
|
|
|
require.Len(t, nodeIDs, 0)
|
2019-10-24 17:24:42 +01:00
|
|
|
|
|
|
|
satellite.GracefulExit.Chore.Loop.Pause()
|
|
|
|
err = satellite.DB.GracefulExit().IncrementProgress(ctx, exitingNode.ID(), 0, 0, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
incompleteTransfers, err = satellite.DB.GracefulExit().GetIncomplete(ctx, exitingNode.ID(), 20, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, incompleteTransfers, 2)
|
|
|
|
|
|
|
|
// node should fail graceful exit if it has been inactive for maximum inactive time frame since last activity
|
|
|
|
time.Sleep(maximumInactiveTimeFrame + time.Second*1)
|
|
|
|
satellite.GracefulExit.Chore.Loop.TriggerWait()
|
|
|
|
|
|
|
|
exitStatus, err := satellite.Overlay.DB.GetExitStatus(ctx, exitingNode.ID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, exitStatus.ExitSuccess)
|
|
|
|
require.NotNil(t, exitStatus.ExitFinishedAt)
|
|
|
|
|
|
|
|
incompleteTransfers, err = satellite.DB.GracefulExit().GetIncomplete(ctx, exitingNode.ID(), 20, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, incompleteTransfers, 0)
|
|
|
|
|
2019-10-07 21:38:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:04:48 +00:00
|
|
|
func TestDurabilityRatio(t *testing.T) {
|
2020-01-21 10:38:41 +00:00
|
|
|
const (
|
|
|
|
maximumInactiveTimeFrame = time.Second * 1
|
|
|
|
successThreshold = 4
|
|
|
|
)
|
2019-11-26 17:04:48 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 4,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2020-11-10 11:56:30 +00:00
|
|
|
Satellite: testplanet.Combine(
|
|
|
|
func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.GracefulExit.MaxInactiveTimeFrame = maximumInactiveTimeFrame
|
|
|
|
},
|
|
|
|
testplanet.ReconfigureRS(2, 3, successThreshold, 4),
|
|
|
|
),
|
2019-11-26 17:04:48 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
uplinkPeer := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
nodeToRemove := planet.StorageNodes[0]
|
|
|
|
exitingNode := planet.StorageNodes[1]
|
|
|
|
|
|
|
|
satellite.GracefulExit.Chore.Loop.Pause()
|
|
|
|
|
2020-01-21 10:38:41 +00:00
|
|
|
err := uplinkPeer.Upload(ctx, satellite, "testbucket", "test/path1", testrand.Bytes(5*memory.KiB))
|
2019-11-26 17:04:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
exitStatusRequest := overlay.ExitStatusRequest{
|
|
|
|
NodeID: exitingNode.ID(),
|
2020-03-10 22:05:01 +00:00
|
|
|
ExitInitiatedAt: time.Now(),
|
2019-11-26 17:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = satellite.Overlay.DB.UpdateExitStatus(ctx, &exitStatusRequest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
exitingNodes, err := satellite.Overlay.DB.GetExitingNodes(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
nodeIDs := make(storj.NodeIDList, 0, len(exitingNodes))
|
|
|
|
for _, exitingNode := range exitingNodes {
|
|
|
|
if exitingNode.ExitLoopCompletedAt == nil {
|
|
|
|
nodeIDs = append(nodeIDs, exitingNode.NodeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Len(t, nodeIDs, 1)
|
|
|
|
|
|
|
|
// retrieve remote segment
|
2020-12-16 16:47:31 +00:00
|
|
|
segments, err := satellite.Metainfo.Metabase.TestingAllSegments(ctx)
|
2019-11-26 17:04:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-16 16:47:31 +00:00
|
|
|
require.Len(t, segments, 1)
|
2019-11-26 17:04:48 +00:00
|
|
|
|
2020-12-16 16:47:31 +00:00
|
|
|
remotePieces := segments[0].Pieces
|
|
|
|
var newPieces metabase.Pieces = make(metabase.Pieces, len(remotePieces)-1)
|
2019-11-26 17:04:48 +00:00
|
|
|
idx := 0
|
|
|
|
for _, p := range remotePieces {
|
2020-12-16 16:47:31 +00:00
|
|
|
if p.StorageNode != nodeToRemove.ID() {
|
2019-11-26 17:04:48 +00:00
|
|
|
newPieces[idx] = p
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 16:47:31 +00:00
|
|
|
|
|
|
|
err = satellite.Metainfo.Metabase.UpdateSegmentPieces(ctx, metabase.UpdateSegmentPieces{
|
|
|
|
StreamID: segments[0].StreamID,
|
|
|
|
Position: segments[0].Position,
|
|
|
|
|
|
|
|
OldPieces: segments[0].Pieces,
|
|
|
|
NewPieces: newPieces,
|
|
|
|
})
|
2019-11-26 17:04:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
satellite.GracefulExit.Chore.Loop.TriggerWait()
|
|
|
|
|
|
|
|
incompleteTransfers, err := satellite.DB.GracefulExit().GetIncomplete(ctx, exitingNode.ID(), 20, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, incompleteTransfers, 1)
|
|
|
|
for _, incomplete := range incompleteTransfers {
|
2020-01-21 10:38:41 +00:00
|
|
|
require.Equal(t, float64(successThreshold-1)/float64(successThreshold), incomplete.DurabilityRatio)
|
2019-11-26 17:04:48 +00:00
|
|
|
require.NotNil(t, incomplete.RootPieceID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-07 21:38:05 +01:00
|
|
|
func BenchmarkChore(b *testing.B) {
|
|
|
|
satellitedbtest.Bench(b, func(b *testing.B, db satellite.DB) {
|
|
|
|
gracefulexitdb := db.GracefulExit()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
b.Run("BatchUpdateStats-100", func(b *testing.B) {
|
|
|
|
batch(ctx, b, gracefulexitdb, 100)
|
|
|
|
})
|
2020-07-10 07:10:01 +01:00
|
|
|
if !testing.Short() {
|
|
|
|
b.Run("BatchUpdateStats-250", func(b *testing.B) {
|
|
|
|
batch(ctx, b, gracefulexitdb, 250)
|
|
|
|
})
|
|
|
|
b.Run("BatchUpdateStats-500", func(b *testing.B) {
|
|
|
|
batch(ctx, b, gracefulexitdb, 500)
|
|
|
|
})
|
|
|
|
b.Run("BatchUpdateStats-1000", func(b *testing.B) {
|
|
|
|
batch(ctx, b, gracefulexitdb, 1000)
|
|
|
|
})
|
|
|
|
b.Run("BatchUpdateStats-5000", func(b *testing.B) {
|
|
|
|
batch(ctx, b, gracefulexitdb, 5000)
|
|
|
|
})
|
|
|
|
}
|
2019-10-07 21:38:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
func batch(ctx context.Context, b *testing.B, db gracefulexit.DB, size int) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
var transferQueueItems []gracefulexit.TransferQueueItem
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
item := gracefulexit.TransferQueueItem{
|
|
|
|
NodeID: testrand.NodeID(),
|
2020-09-03 10:38:54 +01:00
|
|
|
Key: testrand.Bytes(memory.B * 256),
|
2019-10-07 21:38:05 +01:00
|
|
|
PieceNum: 0,
|
|
|
|
DurabilityRatio: 1.0,
|
|
|
|
}
|
|
|
|
transferQueueItems = append(transferQueueItems, item)
|
|
|
|
}
|
|
|
|
err := db.Enqueue(ctx, transferQueueItems)
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|