2019-09-25 18:12:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gracefulexit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
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-09-25 18:12:44 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/gracefulexit"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-09-25 18:12:44 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProgress(t *testing.T) {
|
|
|
|
// test basic graceful exit progress crud
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-09-25 18:12:44 +01:00
|
|
|
geDB := db.GracefulExit()
|
|
|
|
|
|
|
|
testData := []struct {
|
|
|
|
nodeID storj.NodeID
|
|
|
|
bytes int64
|
|
|
|
transferred int64
|
|
|
|
failed int64
|
|
|
|
}{
|
|
|
|
{testrand.NodeID(), 10, 2, 1},
|
|
|
|
{testrand.NodeID(), 1, 4, 0},
|
|
|
|
}
|
|
|
|
for _, data := range testData {
|
|
|
|
err := geDB.IncrementProgress(ctx, data.nodeID, data.bytes, data.transferred, data.failed)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
progress, err := geDB.GetProgress(ctx, data.nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, data.bytes, progress.BytesTransferred)
|
|
|
|
require.Equal(t, data.transferred, progress.PiecesTransferred)
|
|
|
|
require.Equal(t, data.failed, progress.PiecesFailed)
|
|
|
|
|
|
|
|
err = geDB.IncrementProgress(ctx, data.nodeID, 1, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
progress, err = geDB.GetProgress(ctx, data.nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, data.bytes+1, progress.BytesTransferred)
|
|
|
|
require.Equal(t, data.transferred+1, progress.PiecesTransferred)
|
|
|
|
require.Equal(t, data.failed+1, progress.PiecesFailed)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferQueueItem(t *testing.T) {
|
|
|
|
// test basic graceful exit transfer queue crud
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-09-25 18:12:44 +01:00
|
|
|
geDB := db.GracefulExit()
|
|
|
|
|
|
|
|
nodeID1 := testrand.NodeID()
|
|
|
|
nodeID2 := testrand.NodeID()
|
2020-09-03 10:38:54 +01:00
|
|
|
key1 := metabase.SegmentKey(testrand.Bytes(memory.B * 32))
|
|
|
|
key2 := metabase.SegmentKey(testrand.Bytes(memory.B * 32))
|
2019-11-07 16:13:05 +00:00
|
|
|
// root piece IDs for path 1 and 2
|
|
|
|
rootPieceID1 := testrand.PieceID()
|
|
|
|
rootPieceID2 := testrand.PieceID()
|
2019-09-25 18:12:44 +01:00
|
|
|
items := []gracefulexit.TransferQueueItem{
|
|
|
|
{
|
|
|
|
NodeID: nodeID1,
|
2020-09-03 10:38:54 +01:00
|
|
|
Key: key1,
|
2019-09-25 18:12:44 +01:00
|
|
|
PieceNum: 1,
|
2019-11-07 16:13:05 +00:00
|
|
|
RootPieceID: rootPieceID1,
|
2019-09-25 18:12:44 +01:00
|
|
|
DurabilityRatio: 0.9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: nodeID1,
|
2020-09-03 10:38:54 +01:00
|
|
|
Key: key2,
|
2019-09-25 18:12:44 +01:00
|
|
|
PieceNum: 2,
|
2019-11-07 16:13:05 +00:00
|
|
|
RootPieceID: rootPieceID2,
|
2019-09-25 18:12:44 +01:00
|
|
|
DurabilityRatio: 1.1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: nodeID2,
|
2020-09-03 10:38:54 +01:00
|
|
|
Key: key1,
|
2019-09-25 18:12:44 +01:00
|
|
|
PieceNum: 2,
|
2019-11-07 16:13:05 +00:00
|
|
|
RootPieceID: rootPieceID1,
|
2019-09-25 18:12:44 +01:00
|
|
|
DurabilityRatio: 0.9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: nodeID2,
|
2020-09-03 10:38:54 +01:00
|
|
|
Key: key2,
|
2019-09-25 18:12:44 +01:00
|
|
|
PieceNum: 1,
|
2019-11-07 16:13:05 +00:00
|
|
|
RootPieceID: rootPieceID2,
|
2019-09-25 18:12:44 +01:00
|
|
|
DurabilityRatio: 1.1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// test basic create, update, get delete
|
|
|
|
{
|
2021-02-10 18:09:49 +00:00
|
|
|
batchSize := 1000
|
|
|
|
err := geDB.Enqueue(ctx, items, batchSize)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tqi := range items {
|
2020-09-03 10:38:54 +01:00
|
|
|
item, err := geDB.GetTransferQueueItem(ctx, tqi.NodeID, tqi.Key, tqi.PieceNum)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.NoError(t, err)
|
2019-11-07 16:13:05 +00:00
|
|
|
require.Equal(t, tqi.RootPieceID, item.RootPieceID)
|
|
|
|
require.Equal(t, tqi.DurabilityRatio, item.DurabilityRatio)
|
2019-09-25 18:12:44 +01:00
|
|
|
|
2020-03-10 22:05:01 +00:00
|
|
|
now := time.Now()
|
2019-09-25 18:12:44 +01:00
|
|
|
item.DurabilityRatio = 1.2
|
2019-10-11 22:18:05 +01:00
|
|
|
item.RequestedAt = &now
|
2019-09-25 18:12:44 +01:00
|
|
|
|
|
|
|
err = geDB.UpdateTransferQueueItem(ctx, *item)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-03 10:38:54 +01:00
|
|
|
latestItem, err := geDB.GetTransferQueueItem(ctx, tqi.NodeID, tqi.Key, tqi.PieceNum)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.NoError(t, err)
|
2019-11-07 16:13:05 +00:00
|
|
|
|
|
|
|
require.Equal(t, item.RootPieceID, latestItem.RootPieceID)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.Equal(t, item.DurabilityRatio, latestItem.DurabilityRatio)
|
2020-01-21 17:43:53 +00:00
|
|
|
require.WithinDuration(t, now, *latestItem.RequestedAt, time.Second)
|
2019-09-25 18:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
queueItems, err := geDB.GetIncomplete(ctx, nodeID1, 10, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, queueItems, 2)
|
|
|
|
}
|
2020-01-19 17:12:43 +00:00
|
|
|
|
2019-09-25 18:12:44 +01:00
|
|
|
// mark the first item finished and test that only 1 item gets returned from the GetIncomplete
|
|
|
|
{
|
2020-09-03 10:38:54 +01:00
|
|
|
item, err := geDB.GetTransferQueueItem(ctx, nodeID1, key1, 1)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-10 22:05:01 +00:00
|
|
|
now := time.Now()
|
2019-10-11 22:18:05 +01:00
|
|
|
item.FinishedAt = &now
|
|
|
|
|
2019-09-25 18:12:44 +01:00
|
|
|
err = geDB.UpdateTransferQueueItem(ctx, *item)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
queueItems, err := geDB.GetIncomplete(ctx, nodeID1, 10, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, queueItems, 1)
|
|
|
|
for _, queueItem := range queueItems {
|
|
|
|
require.Equal(t, nodeID1, queueItem.NodeID)
|
2020-09-03 10:38:54 +01:00
|
|
|
require.Equal(t, key2, queueItem.Key)
|
2019-09-25 18:12:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:38:54 +01:00
|
|
|
// test delete finished queue items. Only key1 should be removed
|
2019-09-25 18:12:44 +01:00
|
|
|
{
|
|
|
|
err := geDB.DeleteFinishedTransferQueueItems(ctx, nodeID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-03 10:38:54 +01:00
|
|
|
// key1 should no longer exist for nodeID1
|
|
|
|
_, err = geDB.GetTransferQueueItem(ctx, nodeID1, key1, 1)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2020-09-03 10:38:54 +01:00
|
|
|
// key2 should still exist for nodeID1
|
|
|
|
_, err = geDB.GetTransferQueueItem(ctx, nodeID1, key2, 2)
|
2019-09-25 18:12:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test delete all for a node
|
|
|
|
{
|
|
|
|
queueItems, err := geDB.GetIncomplete(ctx, nodeID2, 10, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, queueItems, 2)
|
|
|
|
|
|
|
|
err = geDB.DeleteTransferQueueItems(ctx, nodeID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
queueItems, err = geDB.GetIncomplete(ctx, nodeID2, 10, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, queueItems, 0)
|
|
|
|
}
|
2019-11-13 14:54:50 +00:00
|
|
|
|
|
|
|
// test increment order limit send count
|
2020-09-03 10:38:54 +01:00
|
|
|
err := geDB.IncrementOrderLimitSendCount(ctx, nodeID1, key2, 2)
|
2019-11-13 14:54:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-03 10:38:54 +01:00
|
|
|
// get queue item for key2 since that still exists
|
|
|
|
item, err := geDB.GetTransferQueueItem(ctx, nodeID1, key2, 2)
|
2019-11-13 14:54:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, item.OrderLimitSendCount)
|
2019-09-25 18:12:44 +01:00
|
|
|
})
|
|
|
|
}
|