From 0ef3d5ab03f696f3e97b6e940947362a6d43b6d9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 18 Jan 2022 17:27:07 +0300 Subject: [PATCH] [#1085] writecache: persist in-memory objects before shutdown Signed-off-by: Evgenii Stratonikov --- .../shard/evacuate_test.go | 2 +- pkg/local_object_storage/shard/shard_test.go | 5 +- .../shard/shutdown_test.go | 52 +++++++++++++++++++ .../writecache/writecache.go | 3 ++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 pkg/local_object_storage/shard/shutdown_test.go diff --git a/pkg/local_object_storage/shard/evacuate_test.go b/pkg/local_object_storage/shard/evacuate_test.go index 4cef3ceb8..3f5f74a21 100644 --- a/pkg/local_object_storage/shard/evacuate_test.go +++ b/pkg/local_object_storage/shard/evacuate_test.go @@ -39,7 +39,7 @@ func testEvacuate(t *testing.T, objCount int, hasWriteCache bool) { if !hasWriteCache { sh = newShard(t, false) } else { - sh = newCustomShard(t, true, + sh = newCustomShard(t, t.TempDir(), true, writecache.WithSmallObjectSize(wcSmallObjectSize), writecache.WithMaxObjectSize(wcBigObjectSize)) } diff --git a/pkg/local_object_storage/shard/shard_test.go b/pkg/local_object_storage/shard/shard_test.go index e4e4ae3b3..08905fbd6 100644 --- a/pkg/local_object_storage/shard/shard_test.go +++ b/pkg/local_object_storage/shard/shard_test.go @@ -27,11 +27,10 @@ import ( ) func newShard(t testing.TB, enableWriteCache bool) *shard.Shard { - return newCustomShard(t, enableWriteCache, writecache.WithMaxMemSize(0)) + return newCustomShard(t, t.TempDir(), enableWriteCache, writecache.WithMaxMemSize(0)) } -func newCustomShard(t testing.TB, enableWriteCache bool, wcOpts ...writecache.Option) *shard.Shard { - rootPath := t.Name() +func newCustomShard(t testing.TB, rootPath string, enableWriteCache bool, wcOpts ...writecache.Option) *shard.Shard { if enableWriteCache { rootPath = path.Join(rootPath, "wc") } else { diff --git a/pkg/local_object_storage/shard/shutdown_test.go b/pkg/local_object_storage/shard/shutdown_test.go new file mode 100644 index 000000000..36d13030a --- /dev/null +++ b/pkg/local_object_storage/shard/shutdown_test.go @@ -0,0 +1,52 @@ +package shard_test + +import ( + "math/rand" + "testing" + + "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" + cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" + "github.com/stretchr/testify/require" +) + +func TestWriteCacheObjectLoss(t *testing.T) { + const ( + smallSize = 1024 + objCount = 100 + ) + + objects := make([]*object.Object, objCount) + for i := range objects { + size := smallSize + //if i%2 == 0 { + size = smallSize / 2 + //} + data := make([]byte, size) + rand.Read(data) + + objects[i] = generateRawObjectWithPayload(cidtest.ID(), data).Object() + } + + dir := t.TempDir() + wcOpts := []writecache.Option{ + writecache.WithSmallObjectSize(smallSize), + writecache.WithMaxObjectSize(smallSize * 2)} + + sh := newCustomShard(t, dir, true, wcOpts...) + + for i := range objects { + _, err := sh.Put(new(shard.PutPrm).WithObject(objects[i])) + require.NoError(t, err) + } + require.NoError(t, sh.Close()) + + sh = newCustomShard(t, dir, true, wcOpts...) + defer releaseShard(sh, t) + + for i := range objects { + _, err := sh.Get(new(shard.GetPrm).WithAddress(objects[i].Address())) + require.NoError(t, err, i) + } +} diff --git a/pkg/local_object_storage/writecache/writecache.go b/pkg/local_object_storage/writecache/writecache.go index f3f5c0852..5bdad7e3b 100644 --- a/pkg/local_object_storage/writecache/writecache.go +++ b/pkg/local_object_storage/writecache/writecache.go @@ -140,6 +140,9 @@ func (c *cache) Init() error { // Close closes db connection and stops services. Executes ObjectCounters.FlushAndClose op. func (c *cache) Close() error { + // Finish all in-progress operations. + c.SetMode(ModeReadOnly) + close(c.closeCh) c.objCounters.FlushAndClose() return c.db.Close()