2019-07-24 18:26:43 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gc_test
|
|
|
|
|
|
|
|
import (
|
2020-12-21 14:59:11 +00:00
|
|
|
"context"
|
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"
|
2020-12-21 14:59:11 +00:00
|
|
|
"storj.io/storj/satellite/gc"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-08-08 02:47:30 +01:00
|
|
|
"storj.io/storj/storage"
|
2020-01-19 20:05:49 +00:00
|
|
|
"storj.io/storj/storagenode"
|
2020-12-21 14:59:11 +00:00
|
|
|
"storj.io/uplink/private/testuplink"
|
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)
|
2020-12-14 11:36:58 +00:00
|
|
|
|
|
|
|
objectLocationToDelete, segmentToDelete := getSegment(ctx, t, satellite, upl, "testbucket", "test/path/1")
|
|
|
|
|
2019-07-24 18:26:43 +01:00
|
|
|
var deletedPieceID storj.PieceID
|
2020-12-14 11:36:58 +00:00
|
|
|
for _, p := range segmentToDelete.Pieces {
|
|
|
|
if p.StorageNode == targetNode.ID() {
|
|
|
|
deletedPieceID = segmentToDelete.RootPieceID.Derive(p.StorageNode, int32(p.Number))
|
2019-07-24 18:26:43 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotZero(t, deletedPieceID)
|
|
|
|
|
|
|
|
err = upl.Upload(ctx, satellite, "testbucket", "test/path/2", testData2)
|
|
|
|
require.NoError(t, err)
|
2020-12-14 11:36:58 +00:00
|
|
|
_, segmentToKeep := getSegment(ctx, t, satellite, upl, "testbucket", "test/path/2")
|
2019-07-24 18:26:43 +01:00
|
|
|
var keptPieceID storj.PieceID
|
2020-12-14 11:36:58 +00:00
|
|
|
for _, p := range segmentToKeep.Pieces {
|
|
|
|
if p.StorageNode == targetNode.ID() {
|
|
|
|
keptPieceID = segmentToKeep.RootPieceID.Derive(p.StorageNode, int32(p.Number))
|
2019-07-24 18:26:43 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotZero(t, keptPieceID)
|
|
|
|
|
|
|
|
// Delete one object from metainfo service on satellite
|
2021-09-07 09:15:47 +01:00
|
|
|
_, err = satellite.Metabase.DB.DeleteObjectsAllVersions(ctx, metabase.DeleteObjectsAllVersions{
|
2020-12-14 11:36:58 +00:00
|
|
|
Locations: []metabase.ObjectLocation{objectLocationToDelete},
|
|
|
|
})
|
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-12-14 11:36:58 +00:00
|
|
|
func getSegment(ctx *testcontext.Context, t *testing.T, satellite *testplanet.Satellite, upl *testplanet.Uplink, bucket, path string) (_ metabase.ObjectLocation, _ metabase.Segment) {
|
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)
|
|
|
|
|
2020-12-14 11:36:58 +00:00
|
|
|
objectLocation :=
|
|
|
|
metabase.ObjectLocation{
|
|
|
|
ProjectID: upl.Projects[0].ID,
|
|
|
|
BucketName: "testbucket",
|
|
|
|
ObjectKey: metabase.ObjectKey(encryptedPath.Raw()),
|
|
|
|
}
|
2020-08-28 16:55:59 +01:00
|
|
|
|
2021-09-07 09:15:47 +01:00
|
|
|
lastSegment, err := satellite.Metabase.DB.GetLatestObjectLastSegment(ctx, metabase.GetLatestObjectLastSegment{
|
2020-12-14 11:36:58 +00:00
|
|
|
ObjectLocation: objectLocation,
|
|
|
|
})
|
2019-07-24 18:26:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-14 11:36:58 +00:00
|
|
|
return objectLocation, lastSegment
|
2019-07-24 18:26:43 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-12-21 14:59:11 +00:00
|
|
|
|
|
|
|
func TestGarbageCollection_PendingObject(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: testplanet.Combine(
|
|
|
|
func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.GarbageCollection.FalsePositiveRate = 0.000000001
|
|
|
|
config.GarbageCollection.Interval = 500 * time.Millisecond
|
|
|
|
},
|
|
|
|
testplanet.MaxSegmentSize(20*memory.KiB),
|
|
|
|
),
|
|
|
|
StorageNode: func(index int, config *storagenode.Config) {
|
|
|
|
config.Retain.MaxTimeSkew = 0
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
upl := planet.Uplinks[0]
|
|
|
|
|
|
|
|
testData := testrand.Bytes(15 * memory.KiB)
|
|
|
|
pendingStreamID := startMultipartUpload(ctx, t, upl, satellite, "testbucket", "multi", testData)
|
|
|
|
|
2021-09-07 09:15:47 +01:00
|
|
|
segments, err := satellite.Metabase.DB.TestingAllSegments(ctx)
|
2020-12-21 14:59:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, segments, 1)
|
|
|
|
require.Len(t, segments[0].Pieces, 1)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
lastPieceCounts := map[storj.NodeID]int{}
|
|
|
|
pieceTracker := gc.NewPieceTracker(satellite.Log.Named("gc observer"), gc.Config{
|
|
|
|
FalsePositiveRate: 0.000000001,
|
|
|
|
InitialPieces: 10,
|
|
|
|
}, lastPieceCounts)
|
|
|
|
|
2021-09-07 09:15:47 +01:00
|
|
|
err = satellite.Metabase.SegmentLoop.Join(ctx, pieceTracker)
|
2020-12-21 14:59:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEmpty(t, pieceTracker.RetainInfos)
|
|
|
|
info := pieceTracker.RetainInfos[planet.StorageNodes[0].ID()]
|
|
|
|
require.NotNil(t, info)
|
|
|
|
require.Equal(t, 1, info.Count)
|
|
|
|
|
|
|
|
completeMultipartUpload(ctx, t, upl, satellite, "testbucket", "multi", pendingStreamID)
|
|
|
|
gotData, err := upl.Download(ctx, satellite, "testbucket", "multi")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, testData, gotData)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func startMultipartUpload(ctx context.Context, t *testing.T, uplink *testplanet.Uplink, satellite *testplanet.Satellite, bucketName string, path storj.Path, data []byte) string {
|
|
|
|
_, found := testuplink.GetMaxSegmentSize(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = testuplink.WithMaxSegmentSize(ctx, satellite.Config.Metainfo.MaxSegmentSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := uplink.GetProject(ctx, satellite)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, project.Close()) }()
|
|
|
|
|
|
|
|
_, err = project.EnsureBucket(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-20 09:06:56 +01:00
|
|
|
info, err := project.BeginUpload(ctx, bucketName, path, nil)
|
2020-12-21 14:59:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-20 09:06:56 +01:00
|
|
|
upload, err := project.UploadPart(ctx, bucketName, path, info.UploadID, 1)
|
2020-12-21 14:59:11 +00:00
|
|
|
require.NoError(t, err)
|
2021-04-20 09:06:56 +01:00
|
|
|
_, err = upload.Write(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, upload.Commit())
|
2020-12-21 14:59:11 +00:00
|
|
|
|
2021-04-20 09:06:56 +01:00
|
|
|
return info.UploadID
|
2020-12-21 14:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func completeMultipartUpload(ctx context.Context, t *testing.T, uplink *testplanet.Uplink, satellite *testplanet.Satellite, bucketName string, path storj.Path, streamID string) {
|
|
|
|
_, found := testuplink.GetMaxSegmentSize(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = testuplink.WithMaxSegmentSize(ctx, satellite.Config.Metainfo.MaxSegmentSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := uplink.GetProject(ctx, satellite)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, project.Close()) }()
|
|
|
|
|
2021-04-20 09:06:56 +01:00
|
|
|
_, err = project.CommitUpload(ctx, bucketName, path, streamID, nil)
|
2020-12-21 14:59:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|