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-05-08 12:11:59 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-06-06 20:06:31 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-19 21:13:11 +01:00
|
|
|
"storj.io/storj/uplink"
|
2019-05-08 12:11:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCollector(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 3, UplinkCount: 1,
|
|
|
|
}, 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
|
|
|
|
storageNode.Storage2.Sender.Loop.Pause()
|
|
|
|
}
|
|
|
|
|
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
|
2019-06-26 11:38:51 +01:00
|
|
|
err := planet.Uplinks[0].UploadWithExpirationAndConfig(ctx,
|
2019-06-19 21:13:11 +01:00
|
|
|
planet.Satellites[0],
|
|
|
|
&uplink.RSConfig{
|
|
|
|
MinThreshold: 1,
|
|
|
|
RepairThreshold: 1,
|
|
|
|
SuccessThreshold: 2,
|
|
|
|
MaxThreshold: 2,
|
|
|
|
},
|
|
|
|
"testbucket", "test/path",
|
2019-05-08 12:11:59 +01:00
|
|
|
expectedData, time.Now().Add(8*24*time.Hour))
|
|
|
|
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 {
|
|
|
|
pieceinfos := storageNode.DB.PieceInfo()
|
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
|
|
|
|
used, err := pieceinfos.SpaceUsed(ctx)
|
|
|
|
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 {
|
|
|
|
pieceinfos := storageNode.DB.PieceInfo()
|
|
|
|
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
|
|
|
|
used, err := pieceinfos.SpaceUsed(ctx)
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|