2019-05-08 12:11:59 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package collector_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-13 13:46:08 +01:00
|
|
|
|
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-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-05-08 12:11:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCollector(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 3, UplinkCount: 1,
|
2020-01-21 10:38:41 +00:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: testplanet.ReconfigureRS(1, 1, 2, 2),
|
|
|
|
},
|
2019-05-08 12:11:59 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
// stop collector, so we can run it manually
|
|
|
|
storageNode.Collector.Loop.Pause()
|
|
|
|
// stop order sender because we will stop satellite later
|
2019-08-22 15:33:14 +01:00
|
|
|
storageNode.Storage2.Orders.Sender.Pause()
|
2019-05-08 12:11:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
expectedData := testrand.Bytes(100 * memory.KiB)
|
2019-05-08 12:11:59 +01:00
|
|
|
|
2019-06-19 21:13:11 +01:00
|
|
|
// upload some data to exactly 2 nodes that expires in 8 days
|
2020-01-21 10:38:41 +00:00
|
|
|
err := planet.Uplinks[0].UploadWithExpiration(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData, time.Now().Add(8*24*time.Hour))
|
2019-05-08 12:11:59 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-19 21:13:11 +01:00
|
|
|
// stop satellite to prevent audits
|
2019-05-08 12:11:59 +01:00
|
|
|
require.NoError(t, planet.StopPeer(planet.Satellites[0]))
|
|
|
|
|
|
|
|
collections := 0
|
2019-06-06 20:06:31 +01:00
|
|
|
serialsPresent := 0
|
2019-05-08 12:11:59 +01:00
|
|
|
|
2019-07-09 22:16:30 +01:00
|
|
|
// imagine we are 30 minutes in the future
|
2019-05-08 12:11:59 +01:00
|
|
|
for _, storageNode := range planet.StorageNodes {
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceStore := storageNode.DB.Pieces()
|
2019-06-06 20:06:31 +01:00
|
|
|
usedSerials := storageNode.DB.UsedSerials()
|
2019-05-08 12:11:59 +01:00
|
|
|
|
|
|
|
// verify that we actually have some data on storage nodes
|
2019-12-21 13:11:24 +00:00
|
|
|
used, err := pieceStore.SpaceUsedForBlobs(ctx)
|
2019-05-08 12:11:59 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
if used == 0 {
|
|
|
|
// this storage node didn't get picked for storing data
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all the data
|
2019-07-09 22:16:30 +01:00
|
|
|
err = storageNode.Collector.Collect(ctx, time.Now().Add(30*time.Minute))
|
2019-05-08 12:11:59 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-06 20:06:31 +01:00
|
|
|
// ensure we haven't deleted used serials
|
|
|
|
err = usedSerials.IterateAll(ctx, func(_ storj.NodeID, _ storj.SerialNumber, _ time.Time) {
|
|
|
|
serialsPresent++
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
collections++
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NotZero(t, collections)
|
2019-06-19 21:13:11 +01:00
|
|
|
require.Equal(t, 2, serialsPresent)
|
2019-06-06 20:06:31 +01:00
|
|
|
|
|
|
|
serialsPresent = 0
|
|
|
|
|
2019-07-09 22:16:30 +01:00
|
|
|
// imagine we are 2 hours in the future
|
2019-06-06 20:06:31 +01:00
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
usedSerials := storageNode.DB.UsedSerials()
|
|
|
|
|
|
|
|
// collect all the data
|
2019-07-09 22:16:30 +01:00
|
|
|
err = storageNode.Collector.Collect(ctx, time.Now().Add(2*time.Hour))
|
2019-06-06 20:06:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ensure we have deleted used serials
|
|
|
|
err = usedSerials.IterateAll(ctx, func(id storj.NodeID, serial storj.SerialNumber, expiration time.Time) {
|
|
|
|
serialsPresent++
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
collections++
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, 0, serialsPresent)
|
2019-07-09 22:16:30 +01:00
|
|
|
|
|
|
|
// imagine we are 10 days in the future
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceStore := storageNode.DB.Pieces()
|
2019-07-09 22:16:30 +01:00
|
|
|
usedSerials := storageNode.DB.UsedSerials()
|
|
|
|
|
|
|
|
// collect all the data
|
|
|
|
err = storageNode.Collector.Collect(ctx, time.Now().Add(10*24*time.Hour))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that we deleted everything
|
2019-12-21 13:11:24 +00:00
|
|
|
used, err := pieceStore.SpaceUsedForBlobs(ctx)
|
2019-07-09 22:16:30 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), used)
|
|
|
|
|
|
|
|
// ensure we have deleted used serials
|
|
|
|
err = usedSerials.IterateAll(ctx, func(id storj.NodeID, serial storj.SerialNumber, expiration time.Time) {
|
|
|
|
serialsPresent++
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
collections++
|
|
|
|
}
|
2019-07-10 15:48:00 +01:00
|
|
|
|
|
|
|
require.Equal(t, 0, serialsPresent)
|
2019-05-08 12:11:59 +01:00
|
|
|
})
|
|
|
|
}
|