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"
|
2020-01-30 19:30:34 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
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"
|
2023-05-05 22:26:53 +01:00
|
|
|
"storj.io/storj/cmd/storagenode/internalcmd"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-08-12 22:43:05 +01:00
|
|
|
"storj.io/storj/storagenode"
|
2023-04-05 18:03:06 +01:00
|
|
|
"storj.io/storj/storagenode/blobstore"
|
|
|
|
"storj.io/storj/storagenode/blobstore/filestore"
|
2019-08-12 22:43:05 +01:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
2023-05-05 22:26:53 +01:00
|
|
|
"storj.io/storj/storagenode/pieces/lazyfilewalker"
|
2019-08-12 22:43:05 +01:00
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDBInit(t *testing.T) {
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
|
|
|
|
// Expect that no total record exists since we haven't
|
|
|
|
// initialized yet
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err := spaceUsedDB.GetPieceTotals(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(0), piecesTotal)
|
|
|
|
require.Equal(t, int64(0), piecesContentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
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
|
2020-01-07 23:34:51 +00:00
|
|
|
err = spaceUsedDB.UpdatePieceTotals(ctx, int64(100), int64(101))
|
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
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err = spaceUsedDB.GetPieceTotals(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(100), piecesTotal)
|
|
|
|
require.Equal(t, int64(101), piecesContentSize)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
// Confirm the trash total record has been updated
|
2020-01-07 23:34:51 +00:00
|
|
|
trashTotal, err = spaceUsedDB.GetTrashTotal(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(150), trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
}
|
2020-01-30 22:25:04 +00:00
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
2020-04-29 16:41:44 +01:00
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-01-30 22:25:04 +00:00
|
|
|
sat1 := testrand.NodeID()
|
|
|
|
startSats := map[storj.NodeID]pieces.SatelliteUsage{
|
|
|
|
sat1: {
|
|
|
|
Total: -20,
|
|
|
|
ContentSize: -21,
|
|
|
|
},
|
|
|
|
}
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(zaptest.NewLogger(t), nil, -10, -11, -12, startSats)
|
2020-01-30 22:25:04 +00:00
|
|
|
|
|
|
|
// Sanity check that the values are negative to start
|
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(-10), piecesTotal)
|
|
|
|
require.Equal(t, int64(-11), piecesContentSize)
|
|
|
|
require.Equal(t, int64(-12), trashTotal)
|
|
|
|
|
|
|
|
cache.Update(ctx, sat1, -1, -2, -3)
|
|
|
|
|
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
trashTotal, err = cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(0), piecesTotal)
|
|
|
|
require.Equal(t, int64(0), piecesContentSize)
|
|
|
|
require.Equal(t, int64(0), trashTotal)
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
func TestCacheInit(t *testing.T) {
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
err := spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-30 19:30:34 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
2019-08-12 22:43:05 +01:00
|
|
|
// setup the cache with zero values
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(log, nil, 0, 0, 0, nil)
|
|
|
|
cacheService := pieces.NewService(log,
|
2019-08-12 22:43:05 +01:00
|
|
|
cache,
|
2023-02-28 13:54:01 +00:00
|
|
|
pieces.NewStore(log, pieces.NewFileWalker(log, cache, nil), nil, cache, nil, nil, spaceUsedDB, pieces.DefaultConfig),
|
2019-08-12 22:43:05 +01:00
|
|
|
1*time.Hour,
|
2022-08-07 23:40:59 +01:00
|
|
|
true,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(0), piecesContentSize)
|
|
|
|
satPiecesTotal, satPiecesContentSize, err := cache.SpaceUsedBySatellite(ctx, storj.NodeID{1})
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(0), satPiecesTotal)
|
|
|
|
require.Equal(t, int64(0), satPiecesContentSize)
|
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
|
2020-01-07 23:34:51 +00:00
|
|
|
expectedPiecesTotal := int64(150)
|
|
|
|
expectedPiecesContentSize := int64(151)
|
|
|
|
expectedTotalBySA := map[storj.NodeID]pieces.SatelliteUsage{
|
|
|
|
{1}: {
|
|
|
|
Total: 100,
|
|
|
|
ContentSize: 101,
|
|
|
|
},
|
|
|
|
{2}: {
|
|
|
|
Total: 50,
|
|
|
|
ContentSize: 51,
|
|
|
|
},
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedTrash := int64(127)
|
2020-01-30 19:30:34 +00:00
|
|
|
cache = pieces.NewBlobsUsageCacheTest(log, nil, expectedPiecesTotal, expectedPiecesContentSize, expectedTrash, expectedTotalBySA)
|
|
|
|
cacheService = pieces.NewService(log,
|
2019-08-12 22:43:05 +01:00
|
|
|
cache,
|
2023-02-28 13:54:01 +00:00
|
|
|
pieces.NewStore(log, pieces.NewFileWalker(log, cache, nil), nil, cache, nil, nil, spaceUsedDB, pieces.DefaultConfig),
|
2019-08-12 22:43:05 +01:00
|
|
|
1*time.Hour,
|
2022-08-07 23:40:59 +01:00
|
|
|
true,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
err = cacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-02 18:15:23 +00:00
|
|
|
// Now create an empty cache. Values will be read later by the db.
|
2020-01-30 19:30:34 +00:00
|
|
|
cache = pieces.NewBlobsUsageCacheTest(log, nil, 0, 0, 0, nil)
|
|
|
|
cacheService = pieces.NewService(log,
|
2020-01-02 18:15:23 +00:00
|
|
|
cache,
|
2023-02-28 13:54:01 +00:00
|
|
|
pieces.NewStore(log, pieces.NewFileWalker(log, cache, nil), nil, cache, nil, nil, spaceUsedDB, pieces.DefaultConfig),
|
2020-01-02 18:15:23 +00:00
|
|
|
1*time.Hour,
|
2022-08-07 23:40:59 +01:00
|
|
|
true,
|
2020-01-02 18:15:23 +00:00
|
|
|
)
|
2019-08-12 22:43:05 +01:00
|
|
|
// 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, expectedPiecesTotal, piecesTotal)
|
|
|
|
require.Equal(t, expectedPiecesContentSize, piecesContentSize)
|
|
|
|
sat1PiecesTotal, sat1PiecesContentSize, err := cache.SpaceUsedBySatellite(ctx, storj.NodeID{1})
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(100), sat1PiecesTotal)
|
|
|
|
require.Equal(t, int64(101), sat1PiecesContentSize)
|
|
|
|
sat2PiecesTotal, sat2PiecesContentSize, err := cache.SpaceUsedBySatellite(ctx, storj.NodeID{2})
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
require.Equal(t, int64(50), sat2PiecesTotal)
|
|
|
|
require.Equal(t, int64(51), sat2PiecesContentSize)
|
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
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:26:53 +01:00
|
|
|
func TestCacheServiceRun(t *testing.T) {
|
2020-02-04 23:06:08 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
|
2023-05-05 22:26:53 +01:00
|
|
|
store, err := filestore.NewAt(log, db.Config().Pieces, filestore.DefaultConfig)
|
2020-02-04 23:06:08 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-30 14:04:43 +00:00
|
|
|
defer ctx.Check(store.Close)
|
2020-02-04 23:06:08 +00:00
|
|
|
|
|
|
|
// Prior to initializing the cache service (which should walk the files),
|
|
|
|
// write a single file so something exists to be counted
|
|
|
|
expBlobSize := memory.KB
|
2023-04-05 18:03:06 +01:00
|
|
|
w, err := store.Create(ctx, blobstore.BlobRef{
|
2020-02-04 23:06:08 +00:00
|
|
|
Namespace: testrand.NodeID().Bytes(),
|
|
|
|
Key: testrand.PieceID().Bytes(),
|
|
|
|
}, -1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = w.Write(testrand.Bytes(expBlobSize))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Commit(ctx))
|
|
|
|
|
|
|
|
// Now write a piece that we are going to trash
|
|
|
|
expTrashSize := 2 * memory.KB
|
2023-04-05 18:03:06 +01:00
|
|
|
trashRef := blobstore.BlobRef{
|
2020-02-04 23:06:08 +00:00
|
|
|
Namespace: testrand.NodeID().Bytes(),
|
|
|
|
Key: testrand.PieceID().Bytes(),
|
|
|
|
}
|
2023-04-05 18:03:06 +01:00
|
|
|
w, err = store.Create(ctx, trashRef, -1)
|
2020-02-04 23:06:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = w.Write(testrand.Bytes(expTrashSize))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Commit(ctx))
|
2023-04-05 18:03:06 +01:00
|
|
|
require.NoError(t, store.Trash(ctx, trashRef)) // trash it
|
2020-02-04 23:06:08 +00:00
|
|
|
|
|
|
|
// Now instantiate the cache
|
2023-04-05 18:03:06 +01:00
|
|
|
cache := pieces.NewBlobsUsageCache(log, store)
|
2020-02-04 23:06:08 +00:00
|
|
|
cacheService := pieces.NewService(log,
|
|
|
|
cache,
|
2023-02-28 13:54:01 +00:00
|
|
|
pieces.NewStore(log, pieces.NewFileWalker(log, cache, nil), nil, cache, nil, nil, spaceUsedDB, pieces.DefaultConfig),
|
2020-02-04 23:06:08 +00:00
|
|
|
1*time.Hour,
|
2023-05-05 22:26:53 +01:00
|
|
|
true,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Init the cache service, to read the values from the db (should all be 0)
|
|
|
|
require.NoError(t, cacheService.Init(ctx))
|
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that all the values start as 0, since we have not walked the files
|
|
|
|
assert.Equal(t, int64(0), piecesTotal)
|
|
|
|
assert.Equal(t, int64(0), piecesContentSize)
|
|
|
|
assert.Equal(t, int64(0), trashTotal)
|
|
|
|
|
|
|
|
// Run the cache service, which will walk all the pieces
|
|
|
|
var eg errgroup.Group
|
|
|
|
eg.Go(func() error {
|
|
|
|
return cacheService.Run(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Wait for the cache service init to finish
|
|
|
|
cacheService.InitFence.Wait(ctx)
|
|
|
|
|
|
|
|
// Check and verify that the reported sizes match expected values
|
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
trashTotal, err = cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(expBlobSize), piecesTotal)
|
|
|
|
assert.Equal(t, int64(expBlobSize-pieces.V1PieceHeaderReservedArea), piecesContentSize)
|
|
|
|
assert.True(t, trashTotal >= int64(expTrashSize))
|
|
|
|
|
|
|
|
require.NoError(t, cacheService.Close())
|
|
|
|
require.NoError(t, eg.Wait())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheServiceRun_LazyFilewalker(t *testing.T) {
|
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
|
|
|
|
dbConfig := db.Config()
|
|
|
|
|
|
|
|
store, err := filestore.NewAt(log, dbConfig.Pieces, dbConfig.Filestore)
|
|
|
|
require.NoError(t, err)
|
2023-10-30 14:04:43 +00:00
|
|
|
defer ctx.Check(store.Close)
|
2023-05-05 22:26:53 +01:00
|
|
|
|
|
|
|
// Prior to initializing the cache service (which should walk the files),
|
|
|
|
// write a single file so something exists to be counted
|
|
|
|
expBlobSize := memory.KB
|
|
|
|
w, err := store.Create(ctx, blobstore.BlobRef{
|
|
|
|
Namespace: testrand.NodeID().Bytes(),
|
|
|
|
Key: testrand.PieceID().Bytes(),
|
|
|
|
}, -1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = w.Write(testrand.Bytes(expBlobSize))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Commit(ctx))
|
|
|
|
|
|
|
|
// Now write a piece that we are going to trash
|
|
|
|
expTrashSize := 2 * memory.KB
|
|
|
|
trashRef := blobstore.BlobRef{
|
|
|
|
Namespace: testrand.NodeID().Bytes(),
|
|
|
|
Key: testrand.PieceID().Bytes(),
|
|
|
|
}
|
|
|
|
w, err = store.Create(ctx, trashRef, -1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = w.Write(testrand.Bytes(expTrashSize))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Commit(ctx))
|
|
|
|
require.NoError(t, store.Trash(ctx, trashRef)) // trash it
|
|
|
|
|
|
|
|
// Set up the lazy filewalker
|
|
|
|
cfg := pieces.DefaultConfig
|
|
|
|
cfg.EnableLazyFilewalker = true
|
|
|
|
lazyFwCfg := dbConfig.LazyFilewalkerConfig()
|
|
|
|
lazyFwCfg.LowerIOPriority = false
|
|
|
|
lazyFw := lazyfilewalker.NewSupervisor(log, lazyFwCfg, "")
|
2023-05-24 22:26:33 +01:00
|
|
|
cmd := internalcmd.NewUsedSpaceFilewalkerCmd()
|
|
|
|
cmd.Logger = log.Named("used-space-filewalker")
|
|
|
|
cmd.Ctx = ctx
|
|
|
|
lazyFw.TestingSetUsedSpaceCmd(cmd)
|
2023-05-05 22:26:53 +01:00
|
|
|
|
|
|
|
// Now instantiate the cache
|
|
|
|
cache := pieces.NewBlobsUsageCache(log, store)
|
|
|
|
cacheService := pieces.NewService(log,
|
|
|
|
cache,
|
|
|
|
pieces.NewStore(log, nil, lazyFw, cache, nil, nil, spaceUsedDB, cfg),
|
|
|
|
1*time.Hour,
|
2022-08-07 23:40:59 +01:00
|
|
|
true,
|
2020-02-04 23:06:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init the cache service, to read the values from the db (should all be 0)
|
|
|
|
require.NoError(t, cacheService.Init(ctx))
|
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that all the values start as 0, since we have not walked the files
|
|
|
|
assert.Equal(t, int64(0), piecesTotal)
|
|
|
|
assert.Equal(t, int64(0), piecesContentSize)
|
|
|
|
assert.Equal(t, int64(0), trashTotal)
|
|
|
|
|
|
|
|
// Run the cache service, which will walk all the pieces
|
|
|
|
var eg errgroup.Group
|
|
|
|
eg.Go(func() error {
|
|
|
|
return cacheService.Run(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Wait for the cache service init to finish
|
|
|
|
cacheService.InitFence.Wait(ctx)
|
|
|
|
|
|
|
|
// Check and verify that the reported sizes match expected values
|
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedForPieces(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
trashTotal, err = cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(expBlobSize), piecesTotal)
|
|
|
|
assert.Equal(t, int64(expBlobSize-pieces.V1PieceHeaderReservedArea), piecesContentSize)
|
|
|
|
assert.True(t, trashTotal >= int64(expTrashSize))
|
|
|
|
|
|
|
|
require.NoError(t, cacheService.Close())
|
|
|
|
require.NoError(t, eg.Wait())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
func TestPersistCacheTotals(t *testing.T) {
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2020-01-30 19:30:34 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
// The database should start out with 0 for all totals
|
2020-01-07 23:34:51 +00:00
|
|
|
var expectedPiecesTotal int64
|
|
|
|
var expectedPiecesContentSize int64
|
2019-08-12 22:43:05 +01:00
|
|
|
spaceUsedDB := db.PieceSpaceUsedDB()
|
|
|
|
err := spaceUsedDB.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err := spaceUsedDB.GetPieceTotals(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedPiecesTotal, piecesTotal)
|
|
|
|
assert.Equal(t, expectedPiecesContentSize, piecesContentSize)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
var expectedTotalsBySA = map[storj.NodeID]pieces.SatelliteUsage{}
|
|
|
|
totalsBySA, err := spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedTotalsBySA, totalsBySA)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
// setup: update the cache then sync those cache values
|
|
|
|
// to the database
|
|
|
|
// setup the cache with zero values
|
2020-01-07 23:34:51 +00:00
|
|
|
expectedPiecesTotal = 150
|
|
|
|
expectedPiecesContentSize = 151
|
|
|
|
expectedTotalsBySA = map[storj.NodeID]pieces.SatelliteUsage{
|
|
|
|
{1}: {
|
|
|
|
Total: 100,
|
|
|
|
ContentSize: 101,
|
|
|
|
},
|
|
|
|
{2}: {
|
|
|
|
Total: 50,
|
|
|
|
ContentSize: 51,
|
|
|
|
},
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
expectedTrash = 127
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(log, nil, expectedPiecesTotal, expectedPiecesContentSize, expectedTrash, expectedTotalsBySA)
|
|
|
|
cacheService := pieces.NewService(log,
|
2019-08-12 22:43:05 +01:00
|
|
|
cache,
|
2023-02-28 13:54:01 +00:00
|
|
|
pieces.NewStore(log, pieces.NewFileWalker(log, cache, nil), nil, cache, nil, nil, spaceUsedDB, pieces.DefaultConfig),
|
2019-08-12 22:43:05 +01:00
|
|
|
1*time.Hour,
|
2022-08-07 23:40:59 +01:00
|
|
|
true,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
err = cacheService.PersistCacheTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm those cache values are now saved persistently in the database
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err = spaceUsedDB.GetPieceTotals(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedPiecesTotal, piecesTotal)
|
|
|
|
assert.Equal(t, expectedPiecesContentSize, piecesContentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
totalsBySA, err = spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedTotalsBySA, totalsBySA)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
trashTotal, err = spaceUsedDB.GetTrashTotal(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedTrash, trashTotal)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
// Change piece sizes
|
|
|
|
piecesTotalDelta := int64(35)
|
|
|
|
piecesContentSizeDelta := int64(30)
|
|
|
|
trashDelta := int64(35)
|
|
|
|
cache.Update(ctx, storj.NodeID{1}, -piecesTotalDelta, -piecesContentSizeDelta, 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
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err = spaceUsedDB.GetPieceTotals(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedPiecesTotal-piecesTotalDelta, piecesTotal)
|
|
|
|
assert.Equal(t, expectedPiecesContentSize-piecesContentSizeDelta, piecesContentSize)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
expectedTotalsBySA = map[storj.NodeID]pieces.SatelliteUsage{
|
|
|
|
{1}: {
|
|
|
|
Total: 65,
|
|
|
|
ContentSize: 71,
|
|
|
|
},
|
|
|
|
{2}: {
|
|
|
|
Total: 50,
|
|
|
|
ContentSize: 51,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
totalsBySA, err = spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expectedTotalsBySA, totalsBySA)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecalculateCache(t *testing.T) {
|
2020-01-07 23:34:51 +00:00
|
|
|
type testItem struct {
|
|
|
|
start, end, new, expected int64
|
|
|
|
}
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
piecesTotal testItem
|
|
|
|
piecesContentSize testItem
|
|
|
|
trash testItem
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
name: "1",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
new: 0,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
piecesContentSize: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
new: 0,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
trash: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
new: 0,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: 0,
|
|
|
|
expected: 50,
|
|
|
|
},
|
|
|
|
piecesContentSize: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: 0,
|
|
|
|
expected: 50,
|
|
|
|
},
|
|
|
|
trash: testItem{
|
|
|
|
start: 100,
|
|
|
|
end: 110,
|
|
|
|
new: 50,
|
|
|
|
expected: 55,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "3",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: 90,
|
|
|
|
expected: 140,
|
|
|
|
},
|
|
|
|
piecesContentSize: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 50,
|
|
|
|
new: 100,
|
|
|
|
expected: 125,
|
|
|
|
},
|
|
|
|
trash: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: 50,
|
|
|
|
expected: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "4",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: -25,
|
|
|
|
expected: 25,
|
|
|
|
},
|
|
|
|
piecesContentSize: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
new: -50,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
trash: testItem{
|
|
|
|
start: 0,
|
|
|
|
end: 10,
|
|
|
|
new: -3,
|
|
|
|
expected: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 100,
|
|
|
|
end: 0,
|
2020-01-30 22:25:04 +00:00
|
|
|
new: -90,
|
|
|
|
expected: 0,
|
2020-01-07 23:34:51 +00:00
|
|
|
},
|
|
|
|
piecesContentSize: testItem{
|
|
|
|
start: 100,
|
|
|
|
end: 0,
|
|
|
|
new: 50,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
trash: testItem{
|
|
|
|
start: 100,
|
|
|
|
end: 0,
|
|
|
|
new: -25,
|
|
|
|
expected: 0,
|
|
|
|
},
|
|
|
|
},
|
2020-01-30 22:25:04 +00:00
|
|
|
{
|
|
|
|
name: "6",
|
|
|
|
piecesTotal: testItem{
|
|
|
|
start: 50,
|
|
|
|
end: -50,
|
|
|
|
new: -50,
|
|
|
|
expected: 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()
|
2020-01-30 19:30:34 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
ID1 := storj.NodeID{1, 1}
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(log, nil,
|
2020-01-07 23:34:51 +00:00
|
|
|
tt.piecesTotal.end,
|
|
|
|
tt.piecesContentSize.end,
|
|
|
|
tt.trash.end,
|
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: tt.piecesTotal.end, ContentSize: tt.piecesContentSize.end}},
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
cache.Recalculate(
|
|
|
|
tt.piecesTotal.new,
|
|
|
|
tt.piecesTotal.start,
|
|
|
|
tt.piecesContentSize.new,
|
|
|
|
tt.piecesContentSize.start,
|
|
|
|
tt.trash.new,
|
|
|
|
tt.trash.start,
|
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: tt.piecesTotal.new, ContentSize: tt.piecesContentSize.new}},
|
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: tt.piecesTotal.start, ContentSize: tt.piecesContentSize.start}},
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test: confirm correct cache values
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, tt.piecesTotal.expected, piecesTotal)
|
|
|
|
assert.Equal(t, tt.piecesContentSize.expected, piecesContentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
sat1PiecesTotal, sat1PiecesContentSize, err := cache.SpaceUsedBySatellite(ctx, ID1)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, tt.piecesTotal.expected, sat1PiecesTotal)
|
|
|
|
assert.Equal(t, tt.piecesContentSize.expected, sat1PiecesContentSize)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, tt.trash.expected, trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecalculateCacheMissed(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2020-01-30 19:30:34 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
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
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCacheTest(log, nil,
|
2020-01-07 23:34:51 +00:00
|
|
|
150,
|
|
|
|
200,
|
|
|
|
100,
|
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: 100, ContentSize: 50}, ID2: {Total: 100, ContentSize: 50}},
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
cache.Recalculate(
|
|
|
|
100,
|
|
|
|
0,
|
|
|
|
50,
|
|
|
|
25,
|
2019-12-21 13:11:24 +00:00
|
|
|
200,
|
|
|
|
0,
|
2020-01-07 23:34:51 +00:00
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: 100, ContentSize: 50}},
|
|
|
|
map[storj.NodeID]pieces.SatelliteUsage{ID1: {Total: 0, ContentSize: 0}},
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test: confirm correct cache values
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, int64(175), piecesTotal)
|
|
|
|
assert.Equal(t, int64(137), piecesContentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedBySatellite(ctx, ID2)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, int64(50), piecesTotal)
|
|
|
|
assert.Equal(t, int64(25), piecesContentSize)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, int64(250), trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
func TestCacheCreateDeleteAndTrash(t *testing.T) {
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2020-01-30 19:30:34 +00:00
|
|
|
cache := pieces.NewBlobsUsageCache(zaptest.NewLogger(t), db.Pieces())
|
2019-12-21 13:11:24 +00:00
|
|
|
pieceContent := []byte("stuff")
|
|
|
|
satelliteID := testrand.NodeID()
|
2023-04-05 18:03:06 +01:00
|
|
|
refs := []blobstore.BlobRef{
|
2019-12-21 13:11:24 +00:00
|
|
|
{
|
|
|
|
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)
|
2022-08-01 10:30:33 +01:00
|
|
|
blobWriter, err := pieces.NewWriter(zaptest.NewLogger(t), blob, cache, satelliteID, pb.PieceHashAlgorithm_SHA256)
|
2019-12-21 13:11:24 +00:00
|
|
|
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
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues := func(msg string, satID storj.NodeID, expPiecesTotal, expPiecesContentSize, expTrash int) {
|
|
|
|
piecesTotal, piecesContentSize, err := cache.SpaceUsedForPieces(ctx)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err, msg)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expPiecesTotal, int(piecesTotal), msg)
|
|
|
|
assert.Equal(t, expPiecesContentSize, int(piecesContentSize), msg)
|
|
|
|
piecesTotal, piecesContentSize, err = cache.SpaceUsedBySatellite(ctx, satelliteID)
|
2019-12-21 13:11:24 +00:00
|
|
|
require.NoError(t, err, msg)
|
2020-01-07 23:34:51 +00:00
|
|
|
assert.Equal(t, expPiecesTotal, int(piecesTotal), msg)
|
|
|
|
assert.Equal(t, expPiecesContentSize, int(piecesContentSize), msg)
|
2019-12-21 13:11:24 +00:00
|
|
|
trashTotal, err := cache.SpaceUsedForTrash(ctx)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, expTrash, int(trashTotal), msg)
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
expPieceSize := len(pieceContent) + pieces.V1PieceHeaderReservedArea
|
|
|
|
|
|
|
|
assertValues("first write", satelliteID, expPieceSize*len(refs), len(pieceContent)*len(refs), 0)
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues("trashed refs[0]", satelliteID, expPieceSize, len(pieceContent), int(ref0Size))
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
// Restore one piece
|
|
|
|
_, err = cache.RestoreTrash(ctx, satelliteID.Bytes())
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues("restore trash for satellite", satelliteID, expPieceSize*len(refs), len(pieceContent)*len(refs), 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues("trashed again", satelliteID, expPieceSize, len(pieceContent), int(ref0Size))
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues("emptied trash", satelliteID, expPieceSize, 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)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
assertValues("delete item", satelliteID, 0, 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 {
|
2020-01-07 23:34:51 +00:00
|
|
|
piecesTotal, _, err := sn.Storage2.BlobsCache.SpaceUsedForPieces(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-07 23:34:51 +00:00
|
|
|
total += piecesTotal
|
|
|
|
totalBySA1, _, err := sn.Storage2.BlobsCache.SpaceUsedBySatellite(ctx, satellite1.Identity.ID)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
total1 += totalBySA1
|
2020-01-07 23:34:51 +00:00
|
|
|
totalBySA2, _, err := sn.Storage2.BlobsCache.SpaceUsedBySatellite(ctx, satellite2.Identity.ID)
|
2019-08-12 22:43:05 +01:00
|
|
|
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 {
|
2020-01-07 23:34:51 +00:00
|
|
|
node.Storage2.BlobsCache.Update(ctx, satellite.ID(), 2000, 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())
|
|
|
|
})
|
|
|
|
}
|