2019-08-12 22:43:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pieces_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
2019-09-12 17:42:39 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"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-08-12 22:43:05 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDBInit(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
2019-12-21 13:11:24 +00:00
|
|
|
total, err := spaceUsedDB.GetPieceTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, total, int64(0))
|
|
|
|
|
|
|
|
// Expect that no total record exists since we haven't
|
|
|
|
// initialized yet
|
2019-12-21 13:11:24 +00:00
|
|
|
total, err = spaceUsedDB.GetPieceTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, total, int64(0))
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// Expect no record for trash total
|
|
|
|
trashTotal, err := spaceUsedDB.GetTrashTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), trashTotal)
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
// Now initialize the db to create the total record
|
|
|
|
err = spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Now that a total record exists, we can update it
|
2019-12-21 13:11:24 +00:00
|
|
|
err = spaceUsedDB.UpdatePieceTotal(ctx, int64(100))
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
err = spaceUsedDB.UpdateTrashTotal(ctx, int64(150))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm the total record has been updated
|
|
|
|
total, err = spaceUsedDB.GetPieceTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, total, int64(100))
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
// Confirm the trash total record has been updated
|
|
|
|
total, err = spaceUsedDB.GetTrashTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, total, int64(150))
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
func TestCacheInit(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
err := spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// setup the cache with zero values
|
2019-12-21 13:11:24 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(nil, 0, 0, nil)
|
2019-08-12 22:43:05 +01:00
|
|
|
cacheService := pieces.NewService(zap.L(),
|
|
|
|
cache,
|
|
|
|
pieces.NewStore(zap.L(), cache, nil, nil, spaceUsedDB),
|
|
|
|
1*time.Hour,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Confirm that when we call init before the cache has been persisted.
|
|
|
|
// that the cache gets initialized with zero values
|
|
|
|
err = cacheService.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
piecesTotal, err := cache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.Equal(t, int64(0), piecesTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
actualTotalBySA, err := cache.SpaceUsedBySatellite(ctx, storj.NodeID{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), actualTotalBySA)
|
2019-12-21 13:11:24 +00:00
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
// setup: update the cache then sync those cache values
|
|
|
|
// to the database
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedPieces := int64(150)
|
2019-08-12 22:43:05 +01:00
|
|
|
expectedTotalBySA := map[storj.NodeID]int64{{1}: 100, {2}: 50}
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedTrash := int64(127)
|
|
|
|
cache = pieces.NewBlobsUsageCacheTest(nil, expectedPieces, expectedTrash, expectedTotalBySA)
|
2019-08-12 22:43:05 +01:00
|
|
|
cacheService = pieces.NewService(zap.L(),
|
|
|
|
cache,
|
|
|
|
pieces.NewStore(zap.L(), cache, nil, nil, spaceUsedDB),
|
|
|
|
1*time.Hour,
|
|
|
|
)
|
|
|
|
err = cacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm that when we call Init after the cache has been persisted
|
|
|
|
// that the cache gets initialized with the values from the database
|
|
|
|
err = cacheService.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
piecesTotal, err = cache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.Equal(t, expectedPieces, piecesTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
actualTotalBySA, err = cache.SpaceUsedBySatellite(ctx, storj.NodeID{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(100), actualTotalBySA)
|
|
|
|
actualTotalBySA, err = cache.SpaceUsedBySatellite(ctx, storj.NodeID{2})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(50), actualTotalBySA)
|
2019-12-21 13:11:24 +00:00
|
|
|
actualTrash, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(127), actualTrash)
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersistCacheTotals(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// The database should start out with 0 for all totals
|
2019-12-21 13:11:24 +00:00
|
|
|
var expectedPieces int64
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
err := spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
piecesTotal, err := spaceUsedDB.GetPieceTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedPieces, piecesTotal)
|
|
|
|
|
|
|
|
var expectedTrash int64
|
|
|
|
err = spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
trashTotal, err := spaceUsedDB.GetTrashTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assert.Equal(t, expectedTrash, trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
var expectedTotalBySA = map[storj.NodeID]int64{}
|
2019-12-21 13:11:24 +00:00
|
|
|
actualTotalBySA, err := spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedTotalBySA, actualTotalBySA)
|
|
|
|
|
|
|
|
// setup: update the cache then sync those cache values
|
|
|
|
// to the database
|
|
|
|
// setup the cache with zero values
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedPieces = 150
|
2019-08-12 22:43:05 +01:00
|
|
|
expectedTotalBySA = map[storj.NodeID]int64{{1}: 100, {2}: 50}
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedTrash = 127
|
|
|
|
cache := pieces.NewBlobsUsageCacheTest(nil, expectedPieces, expectedTrash, expectedTotalBySA)
|
2019-08-12 22:43:05 +01:00
|
|
|
cacheService := pieces.NewService(zap.L(),
|
|
|
|
cache,
|
|
|
|
pieces.NewStore(zap.L(), cache, nil, nil, spaceUsedDB),
|
|
|
|
1*time.Hour,
|
|
|
|
)
|
|
|
|
err = cacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm those cache values are now saved persistently in the database
|
2019-12-21 13:11:24 +00:00
|
|
|
piecesTotal, err = spaceUsedDB.GetPieceTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assert.Equal(t, expectedPieces, piecesTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
actualTotalBySA, err = spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedTotalBySA, actualTotalBySA)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
trashTotal, err = spaceUsedDB.GetTrashTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedTrash, trashTotal)
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
// Delete some piece content
|
|
|
|
pieceContentSize := -int64(100)
|
2019-12-21 13:11:24 +00:00
|
|
|
trashDelta := int64(104)
|
|
|
|
cache.Update(ctx, storj.NodeID{1}, pieceContentSize, trashDelta)
|
2019-08-12 22:43:05 +01:00
|
|
|
err = cacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm that the deleted stuff is not in the database anymore
|
2019-12-21 13:11:24 +00:00
|
|
|
piecesTotal, err = spaceUsedDB.GetPieceTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedPieces+pieceContentSize, piecesTotal)
|
|
|
|
|
|
|
|
trashTotal, err = spaceUsedDB.GetTrashTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assert.Equal(t, expectedTrash+trashDelta, trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
expectedTotalBySA = map[storj.NodeID]int64{{2}: 50}
|
2019-12-21 13:11:24 +00:00
|
|
|
actualTotalBySA, err = spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedTotalBySA, actualTotalBySA)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecalculateCache(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
start int64
|
|
|
|
end int64
|
|
|
|
new int64
|
|
|
|
expected int64
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
startTrash int64
|
|
|
|
endTrash int64
|
|
|
|
newTrash int64
|
|
|
|
expectedTrash int64
|
2019-08-12 22:43:05 +01:00
|
|
|
}{
|
2019-12-21 13:11:24 +00:00
|
|
|
{"1", 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{"2", 0, 100, 0, 50, 100, 110, 50, 55},
|
|
|
|
{"3", 0, 100, 90, 140, 0, 100, 50, 100},
|
|
|
|
{"4", 0, 100, 110, 160, 0, 100, -10, 40},
|
|
|
|
{"5", 0, 100, -10, 40, 0, 0, 0, 0},
|
|
|
|
{"6", 0, 100, -200, 0, 0, 0, 0, 0},
|
|
|
|
{"7", 100, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{"8", 100, 0, 90, 40, 0, 0, 0, 0},
|
|
|
|
{"9", 100, 0, 30, 0, 0, 0, 0, 0},
|
|
|
|
{"10", 100, 0, 110, 60, 0, 0, 0, 0},
|
|
|
|
{"11", 100, 0, -10, 0, 0, 0, 0, 0},
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
ID1 := storj.NodeID{1, 1}
|
|
|
|
cache := pieces.NewBlobsUsageCacheTest(nil,
|
|
|
|
tt.end,
|
2019-12-21 13:11:24 +00:00
|
|
|
tt.endTrash,
|
2019-08-12 22:43:05 +01:00
|
|
|
map[storj.NodeID]int64{ID1: tt.end},
|
|
|
|
)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
cache.Recalculate(ctx,
|
2019-08-12 22:43:05 +01:00
|
|
|
tt.new,
|
|
|
|
tt.start,
|
|
|
|
map[storj.NodeID]int64{ID1: tt.new},
|
|
|
|
map[storj.NodeID]int64{ID1: tt.start},
|
2019-12-21 13:11:24 +00:00
|
|
|
tt.newTrash,
|
|
|
|
tt.startTrash,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test: confirm correct cache values
|
|
|
|
actualTotalSpaceUsed, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.expected, actualTotalSpaceUsed)
|
|
|
|
|
|
|
|
actualTotalSpaceUsedBySA, err := cache.SpaceUsedBySatellite(ctx, ID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.expected, actualTotalSpaceUsedBySA)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
actualTrash, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.expectedTrash, actualTrash)
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecalculateCacheMissed(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
ID1 := storj.NodeID{1}
|
|
|
|
ID2 := storj.NodeID{2}
|
|
|
|
|
|
|
|
// setup: once we are done recalculating the pieces on disk,
|
|
|
|
// there are items in the cache that are not in the
|
|
|
|
// new recalculated values
|
|
|
|
cache := pieces.NewBlobsUsageCacheTest(nil,
|
|
|
|
int64(150),
|
2019-12-21 13:11:24 +00:00
|
|
|
int64(100),
|
2019-08-12 22:43:05 +01:00
|
|
|
map[storj.NodeID]int64{ID1: int64(100), ID2: int64(50)},
|
|
|
|
)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
cache.Recalculate(ctx,
|
2019-08-12 22:43:05 +01:00
|
|
|
int64(100),
|
|
|
|
int64(0),
|
|
|
|
map[storj.NodeID]int64{ID1: int64(100)},
|
|
|
|
map[storj.NodeID]int64{ID1: int64(0)},
|
2019-12-21 13:11:24 +00:00
|
|
|
200,
|
|
|
|
0,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test: confirm correct cache values
|
|
|
|
actualTotalSpaceUsed, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(175), actualTotalSpaceUsed)
|
|
|
|
|
|
|
|
actualTotalSpaceUsedBySA, err := cache.SpaceUsedBySatellite(ctx, ID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(25), actualTotalSpaceUsedBySA)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
actualTrash, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(250), actualTrash)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
func TestCacheCreateDeleteAndTrash(t *testing.T) {
|
2019-08-12 22:43:05 +01:00
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
cache := pieces.NewBlobsUsageCache(db.Pieces())
|
2019-12-21 13:11:24 +00:00
|
|
|
pieceContent := []byte("stuff")
|
|
|
|
satelliteID := testrand.NodeID()
|
|
|
|
refs := []storage.BlobRef{
|
|
|
|
{
|
|
|
|
Namespace: satelliteID.Bytes(),
|
|
|
|
Key: testrand.Bytes(32),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: satelliteID.Bytes(),
|
|
|
|
Key: testrand.Bytes(32),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, ref := range refs {
|
|
|
|
blob, err := cache.Create(ctx, ref, int64(4096))
|
|
|
|
require.NoError(t, err)
|
|
|
|
blobWriter, err := pieces.NewWriter(blob, cache, satelliteID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = blobWriter.Write(pieceContent)
|
|
|
|
require.NoError(t, err)
|
|
|
|
header := pb.PieceHeader{}
|
|
|
|
err = blobWriter.Commit(ctx, &header)
|
|
|
|
require.NoError(t, err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
assertValues := func(msg string, satID storj.NodeID, expPiece, expTrash int) {
|
|
|
|
piecesTotal, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, expPiece, int(piecesTotal), msg)
|
|
|
|
actualTotalBySA, err := cache.SpaceUsedBySatellite(ctx, satelliteID)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, expPiece, int(actualTotalBySA), msg)
|
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, expTrash, int(trashTotal), msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertValues("first write", satelliteID, len(pieceContent)*2, 0)
|
|
|
|
|
|
|
|
// Trash one piece
|
|
|
|
blobInfo, err := cache.Stat(ctx, refs[0])
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
fileInfo, err := blobInfo.Stat(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
ref0Size := fileInfo.Size()
|
|
|
|
err = cache.Trash(ctx, refs[0])
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assertValues("trashed refs[0]", satelliteID, len(pieceContent), int(ref0Size))
|
|
|
|
|
|
|
|
// Restore one piece
|
|
|
|
_, err = cache.RestoreTrash(ctx, satelliteID.Bytes())
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assertValues("restore trash for satellite", satelliteID, len(pieceContent)*2, 0)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// Trash piece again
|
|
|
|
err = cache.Trash(ctx, refs[0])
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assertValues("trashed refs[0]", satelliteID, len(pieceContent), int(ref0Size))
|
|
|
|
|
|
|
|
// Empty trash
|
|
|
|
_, _, err = cache.EmptyTrash(ctx, satelliteID.Bytes(), time.Now().Add(24*time.Hour))
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-21 13:11:24 +00:00
|
|
|
assertValues("trashed refs[0]", satelliteID, len(pieceContent), 0)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
// Delete that piece and confirm the cache is updated
|
2019-12-21 13:11:24 +00:00
|
|
|
err = cache.Delete(ctx, refs[1])
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
assertValues("delete refs[0]", satelliteID, 0, 0)
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheCreateMultipleSatellites(t *testing.T) {
|
2019-08-13 16:06:28 +01:00
|
|
|
t.Skip("flaky: V3-2416")
|
2019-08-12 22:43:05 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 2, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite1 := planet.Satellites[0]
|
|
|
|
satellite2 := planet.Satellites[1]
|
|
|
|
uplink := planet.Uplinks[0]
|
|
|
|
// Setup: create data for the uplink to upload
|
|
|
|
expectedData := testrand.Bytes(5 * memory.KiB)
|
|
|
|
err := uplink.Upload(ctx, satellite1, "testbucket", "test/path", expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = uplink.Upload(ctx, satellite2, "testbucket", "test/path", expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var total, total1, total2 int64
|
|
|
|
for _, sn := range planet.StorageNodes {
|
|
|
|
totalP, err := sn.Storage2.BlobsCache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
total += totalP
|
|
|
|
totalBySA1, err := sn.Storage2.BlobsCache.SpaceUsedBySatellite(ctx, satellite1.Identity.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
total1 += totalBySA1
|
|
|
|
totalBySA2, err := sn.Storage2.BlobsCache.SpaceUsedBySatellite(ctx, satellite2.Identity.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
total2 += totalBySA2
|
|
|
|
}
|
|
|
|
require.Equal(t, int64(47104), total)
|
|
|
|
require.Equal(t, int64(23552), total1)
|
|
|
|
require.Equal(t, int64(23552), total2)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2019-09-12 17:42:39 +01:00
|
|
|
|
|
|
|
func TestConcurrency(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
node := planet.StorageNodes[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
2019-12-21 13:11:24 +00:00
|
|
|
node.Storage2.BlobsCache.Update(ctx, satellite.ID(), 1000, 0)
|
2019-09-12 17:42:39 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
err := node.Storage2.CacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, group.Wait())
|
|
|
|
})
|
|
|
|
}
|