2019-07-24 18:26:43 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gc_test
|
|
|
|
|
|
|
|
import (
|
2020-05-26 09:05:43 +01:00
|
|
|
"errors"
|
2019-07-24 18:26:43 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2019-07-24 18:26:43 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/encryption"
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/paths"
|
|
|
|
"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-07-24 18:26:43 +01:00
|
|
|
"storj.io/storj/satellite"
|
2019-08-08 02:47:30 +01:00
|
|
|
"storj.io/storj/storage"
|
2020-01-19 20:05:49 +00:00
|
|
|
"storj.io/storj/storagenode"
|
2019-07-24 18:26:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestGarbageCollection does the following:
|
|
|
|
// * Set up a network with one storagenode
|
|
|
|
// * Upload two objects
|
|
|
|
// * Delete one object from the metainfo service on the satellite
|
|
|
|
// * Wait for bloom filter generation
|
|
|
|
// * Check that pieces of the deleted object are deleted on the storagenode
|
2020-07-16 15:18:02 +01:00
|
|
|
// * Check that pieces of the kept object are not deleted on the storagenode.
|
2019-07-24 18:26:43 +01:00
|
|
|
func TestGarbageCollection(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.GarbageCollection.FalsePositiveRate = 0.000000001
|
|
|
|
config.GarbageCollection.Interval = 500 * time.Millisecond
|
|
|
|
},
|
2020-01-19 20:05:49 +00:00
|
|
|
StorageNode: func(index int, config *storagenode.Config) {
|
|
|
|
config.Retain.MaxTimeSkew = 0
|
|
|
|
},
|
2019-07-24 18:26:43 +01:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
upl := planet.Uplinks[0]
|
|
|
|
targetNode := planet.StorageNodes[0]
|
|
|
|
gcService := satellite.GarbageCollection.Service
|
2019-08-19 19:52:47 +01:00
|
|
|
gcService.Loop.Pause()
|
2019-07-24 18:26:43 +01:00
|
|
|
|
|
|
|
// Upload two objects
|
|
|
|
testData1 := testrand.Bytes(8 * memory.KiB)
|
|
|
|
testData2 := testrand.Bytes(8 * memory.KiB)
|
|
|
|
|
|
|
|
err := upl.Upload(ctx, satellite, "testbucket", "test/path/1", testData1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
deletedEncPath, pointerToDelete := getPointer(ctx, t, satellite, upl, "testbucket", "test/path/1")
|
|
|
|
var deletedPieceID storj.PieceID
|
|
|
|
for _, p := range pointerToDelete.GetRemote().GetRemotePieces() {
|
|
|
|
if p.NodeId == targetNode.ID() {
|
|
|
|
deletedPieceID = pointerToDelete.GetRemote().RootPieceId.Derive(p.NodeId, p.PieceNum)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotZero(t, deletedPieceID)
|
|
|
|
|
|
|
|
err = upl.Upload(ctx, satellite, "testbucket", "test/path/2", testData2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, pointerToKeep := getPointer(ctx, t, satellite, upl, "testbucket", "test/path/2")
|
|
|
|
var keptPieceID storj.PieceID
|
|
|
|
for _, p := range pointerToKeep.GetRemote().GetRemotePieces() {
|
|
|
|
if p.NodeId == targetNode.ID() {
|
|
|
|
keptPieceID = pointerToKeep.GetRemote().RootPieceId.Derive(p.NodeId, p.PieceNum)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotZero(t, keptPieceID)
|
|
|
|
|
|
|
|
// Delete one object from metainfo service on satellite
|
2019-11-06 17:02:14 +00:00
|
|
|
err = satellite.Metainfo.Service.UnsynchronizedDelete(ctx, deletedEncPath)
|
2019-07-24 18:26:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that piece of the deleted object is on the storagenode
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceAccess, err := targetNode.DB.Pieces().Stat(ctx, storage.BlobRef{
|
|
|
|
Namespace: satellite.ID().Bytes(),
|
|
|
|
Key: deletedPieceID.Bytes(),
|
|
|
|
})
|
2019-07-24 18:26:43 +01:00
|
|
|
require.NoError(t, err)
|
2019-08-08 02:47:30 +01:00
|
|
|
require.NotNil(t, pieceAccess)
|
2019-07-24 18:26:43 +01:00
|
|
|
|
|
|
|
// The pieceInfo.GetPieceIDs query converts piece creation and the filter creation timestamps
|
|
|
|
// to datetime in sql. This chops off all precision beyond seconds.
|
|
|
|
// In this test, the amount of time that elapses between piece uploads and the gc loop is
|
|
|
|
// less than a second, meaning datetime(piece_creation) < datetime(filter_creation) is false unless we sleep
|
|
|
|
// for a second.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
// Wait for next iteration of garbage collection to finish
|
2019-08-19 19:52:47 +01:00
|
|
|
gcService.Loop.Restart()
|
2019-07-24 18:26:43 +01:00
|
|
|
gcService.Loop.TriggerWait()
|
|
|
|
|
2019-08-19 19:52:47 +01:00
|
|
|
// Wait for the storagenode's RetainService queue to be empty
|
2019-08-28 21:35:25 +01:00
|
|
|
targetNode.Storage2.RetainService.TestWaitUntilEmpty()
|
2019-08-19 19:52:47 +01:00
|
|
|
|
2019-07-24 18:26:43 +01:00
|
|
|
// Check that piece of the deleted object is not on the storagenode
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceAccess, err = targetNode.DB.Pieces().Stat(ctx, storage.BlobRef{
|
|
|
|
Namespace: satellite.ID().Bytes(),
|
|
|
|
Key: deletedPieceID.Bytes(),
|
|
|
|
})
|
2019-07-24 18:26:43 +01:00
|
|
|
require.Error(t, err)
|
2019-08-08 02:47:30 +01:00
|
|
|
require.Nil(t, pieceAccess)
|
2019-07-24 18:26:43 +01:00
|
|
|
|
|
|
|
// Check that piece of the kept object is on the storagenode
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceAccess, err = targetNode.DB.Pieces().Stat(ctx, storage.BlobRef{
|
|
|
|
Namespace: satellite.ID().Bytes(),
|
|
|
|
Key: keptPieceID.Bytes(),
|
|
|
|
})
|
2019-07-24 18:26:43 +01:00
|
|
|
require.NoError(t, err)
|
2019-08-08 02:47:30 +01:00
|
|
|
require.NotNil(t, pieceAccess)
|
2019-07-24 18:26:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:46:40 +00:00
|
|
|
func getPointer(ctx *testcontext.Context, t *testing.T, satellite *testplanet.Satellite, upl *testplanet.Uplink, bucket, path string) (lastSegPath string, pointer *pb.Pointer) {
|
2019-07-24 18:26:43 +01:00
|
|
|
projects, err := satellite.DB.Console().Projects().GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, projects, 1)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
access := upl.Access[satellite.ID()]
|
|
|
|
|
|
|
|
serializedAccess, err := access.Serialize()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
store, err := encryptionAccess(serializedAccess)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
encryptedPath, err := encryption.EncryptPathWithStoreCipher(bucket, paths.NewUnencrypted(path), store)
|
2019-07-24 18:26:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lastSegPath = storj.JoinPaths(projects[0].ID.String(), "l", bucket, encryptedPath.Raw())
|
|
|
|
pointer, err = satellite.Metainfo.Service.Get(ctx, lastSegPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return lastSegPath, pointer
|
|
|
|
}
|
2020-05-26 09:05:43 +01:00
|
|
|
|
|
|
|
func encryptionAccess(access string) (*encryption.Store, error) {
|
|
|
|
data, version, err := base58.CheckDecode(access)
|
|
|
|
if err != nil || version != 0 {
|
|
|
|
return nil, errors.New("invalid access grant format")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(pb.Scope)
|
|
|
|
if err := pb.Unmarshal(data, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := storj.NewKey(p.EncryptionAccess.DefaultKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := encryption.NewStore()
|
|
|
|
store.SetDefaultKey(key)
|
|
|
|
store.SetDefaultPathCipher(storj.EncAESGCM)
|
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|