2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-22 09:58:20 +00:00
|
|
|
"os"
|
2023-06-22 11:55:30 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2023-12-22 09:58:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger/test"
|
2023-06-22 11:55:30 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-12-22 09:58:20 +00:00
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2023-06-22 11:55:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-22 09:58:20 +00:00
|
|
|
"go.uber.org/zap"
|
2023-06-22 11:55:30 +00:00
|
|
|
)
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
func TestFlush(t *testing.T) {
|
2024-01-09 08:37:41 +00:00
|
|
|
testlogger := test.NewLogger(t)
|
2023-12-22 09:58:20 +00:00
|
|
|
|
2024-09-10 10:12:17 +00:00
|
|
|
createCacheFn := func(t *testing.T, mb *meta.DB, bs MainStorage, opts ...Option) Cache {
|
2023-12-22 09:58:20 +00:00
|
|
|
return New(
|
|
|
|
append([]Option{
|
|
|
|
WithLogger(testlogger),
|
|
|
|
WithPath(filepath.Join(t.TempDir(), "writecache")),
|
|
|
|
WithMetabase(mb),
|
|
|
|
WithBlobstor(bs),
|
|
|
|
WithDisableBackgroundFlush(),
|
|
|
|
}, opts...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
errCountOpt := func() (Option, *atomic.Uint32) {
|
|
|
|
cnt := &atomic.Uint32{}
|
2024-10-21 13:27:28 +00:00
|
|
|
return WithReportErrorFunc(func(ctx context.Context, msg string, err error) {
|
2023-12-22 09:58:20 +00:00
|
|
|
cnt.Add(1)
|
2024-10-21 13:27:28 +00:00
|
|
|
testlogger.Warn(ctx, msg, zap.Uint32("error_count", cnt.Load()), zap.Error(err))
|
2023-12-22 09:58:20 +00:00
|
|
|
}), cnt
|
|
|
|
}
|
|
|
|
|
|
|
|
failures := []TestFailureInjector[Option]{
|
|
|
|
{
|
|
|
|
Desc: "fs, read error",
|
|
|
|
InjectFn: func(t *testing.T, wc Cache) {
|
|
|
|
c := wc.(*cache)
|
|
|
|
obj := testutil.GenerateObject()
|
|
|
|
data, err := obj.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Address = objectCore.AddressOf(obj)
|
|
|
|
prm.RawData = data
|
|
|
|
|
|
|
|
_, err = c.fsTree.Put(context.Background(), prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p := prm.Address.Object().EncodeToString() + "." + prm.Address.Container().EncodeToString()
|
|
|
|
p = filepath.Join(c.fsTree.RootPath, p[:1], p[1:])
|
|
|
|
|
|
|
|
_, err = os.Stat(p) // sanity check
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, os.Truncate(p, 0)) // corrupt the file contents, so that it can't be unmarshalled
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Desc: "fs, invalid object",
|
|
|
|
InjectFn: func(t *testing.T, wc Cache) {
|
|
|
|
c := wc.(*cache)
|
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Address = oidtest.Address()
|
|
|
|
prm.RawData = []byte{1, 2, 3}
|
|
|
|
_, err := c.fsTree.Put(context.Background(), prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runFlushTest(t, createCacheFn, errCountOpt, failures...)
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
const (
|
|
|
|
objCount = 4
|
|
|
|
smallSize = 256
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateCacheFunc[Option any] func(
|
|
|
|
t *testing.T,
|
|
|
|
meta *meta.DB,
|
2023-12-22 09:58:20 +00:00
|
|
|
bs MainStorage,
|
2023-06-22 11:55:30 +00:00
|
|
|
opts ...Option,
|
2023-12-22 09:58:20 +00:00
|
|
|
) Cache
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
type TestFailureInjector[Option any] struct {
|
|
|
|
Desc string
|
2023-12-22 09:58:20 +00:00
|
|
|
InjectFn func(*testing.T, Cache)
|
2023-06-22 11:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type objectPair struct {
|
|
|
|
addr oid.Address
|
|
|
|
obj *objectSDK.Object
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
func runFlushTest[Option any](
|
2023-06-22 11:55:30 +00:00
|
|
|
t *testing.T,
|
|
|
|
createCacheFn CreateCacheFunc[Option],
|
|
|
|
errCountOption func() (Option, *atomic.Uint32),
|
|
|
|
failures ...TestFailureInjector[Option],
|
|
|
|
) {
|
|
|
|
t.Run("no errors", func(t *testing.T) {
|
2024-09-10 10:12:17 +00:00
|
|
|
wc, bs, mb := newCache(t, createCacheFn)
|
2024-10-21 13:27:28 +00:00
|
|
|
defer func() { require.NoError(t, wc.Close(context.Background())) }()
|
2023-06-22 11:55:30 +00:00
|
|
|
objects := putObjects(t, wc)
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
require.NoError(t, bs.SetMode(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, mb.SetMode(context.Background(), mode.ReadWrite))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
require.NoError(t, wc.Flush(context.Background(), false, false))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
check(t, mb, bs, objects)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("flush on moving to degraded mode", func(t *testing.T) {
|
2024-09-10 10:12:17 +00:00
|
|
|
wc, bs, mb := newCache(t, createCacheFn)
|
2024-10-21 13:27:28 +00:00
|
|
|
defer func() { require.NoError(t, wc.Close(context.Background())) }()
|
2023-06-22 11:55:30 +00:00
|
|
|
objects := putObjects(t, wc)
|
|
|
|
|
|
|
|
// Blobstor is read-only, so we expect en error from `flush` here.
|
2024-10-21 08:56:38 +00:00
|
|
|
require.Error(t, wc.SetMode(context.Background(), mode.Degraded))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
require.NoError(t, bs.SetMode(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, mb.SetMode(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, wc.SetMode(context.Background(), mode.Degraded))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
check(t, mb, bs, objects)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ignore errors", func(t *testing.T) {
|
|
|
|
for _, f := range failures {
|
|
|
|
t.Run(f.Desc, func(t *testing.T) {
|
|
|
|
errCountOpt, errCount := errCountOption()
|
2024-09-10 10:12:17 +00:00
|
|
|
wc, bs, mb := newCache(t, createCacheFn, errCountOpt)
|
2024-10-21 13:27:28 +00:00
|
|
|
defer func() { require.NoError(t, wc.Close(context.Background())) }()
|
2023-06-22 11:55:30 +00:00
|
|
|
objects := putObjects(t, wc)
|
|
|
|
f.InjectFn(t, wc)
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
require.NoError(t, bs.SetMode(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, mb.SetMode(context.Background(), mode.ReadWrite))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
require.Equal(t, uint32(0), errCount.Load())
|
2023-12-27 05:20:15 +00:00
|
|
|
require.Error(t, wc.Flush(context.Background(), false, false))
|
2023-12-11 10:09:57 +00:00
|
|
|
require.Greater(t, errCount.Load(), uint32(0))
|
2023-12-27 05:20:15 +00:00
|
|
|
require.NoError(t, wc.Flush(context.Background(), true, false))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
check(t, mb, bs, objects)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCache[Option any](
|
|
|
|
t *testing.T,
|
|
|
|
createCacheFn CreateCacheFunc[Option],
|
|
|
|
opts ...Option,
|
2023-12-22 09:58:20 +00:00
|
|
|
) (Cache, *blobstor.BlobStor, *meta.DB) {
|
2023-06-22 11:55:30 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
mb := meta.New(
|
|
|
|
meta.WithPath(filepath.Join(dir, "meta")),
|
|
|
|
meta.WithEpochState(dummyEpoch{}))
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, mb.Open(context.Background(), mode.ReadWrite))
|
2024-10-21 13:27:28 +00:00
|
|
|
require.NoError(t, mb.Init(context.Background()))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
bs := blobstor.New(blobstor.WithStorages([]blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(filepath.Join(dir, "blob")),
|
|
|
|
fstree.WithDepth(0),
|
|
|
|
fstree.WithDirNameLen(1)),
|
|
|
|
},
|
|
|
|
}))
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, bs.Open(context.Background(), mode.ReadWrite))
|
2024-10-21 13:27:28 +00:00
|
|
|
require.NoError(t, bs.Init(context.Background()))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
2024-09-10 10:12:17 +00:00
|
|
|
wc := createCacheFn(t, mb, bs, opts...)
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, wc.Open(context.Background(), mode.ReadWrite))
|
2024-10-21 13:27:28 +00:00
|
|
|
require.NoError(t, wc.Init(context.Background()))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
// First set mode for metabase and blobstor to prevent background flushes.
|
2024-10-21 08:56:38 +00:00
|
|
|
require.NoError(t, mb.SetMode(context.Background(), mode.ReadOnly))
|
|
|
|
require.NoError(t, bs.SetMode(context.Background(), mode.ReadOnly))
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
return wc, bs, mb
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
func putObject(t *testing.T, c Cache, size int) objectPair {
|
2023-06-22 11:55:30 +00:00
|
|
|
obj := testutil.GenerateObjectWithSize(size)
|
|
|
|
data, err := obj.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Address = objectCore.AddressOf(obj)
|
|
|
|
prm.Object = obj
|
|
|
|
prm.RawData = data
|
|
|
|
|
|
|
|
_, err = c.Put(context.Background(), prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return objectPair{prm.Address, prm.Object}
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
func putObjects(t *testing.T, c Cache) []objectPair {
|
2023-06-22 11:55:30 +00:00
|
|
|
objects := make([]objectPair, objCount)
|
|
|
|
for i := range objects {
|
|
|
|
objects[i] = putObject(t, c, 1+(i%2)*smallSize)
|
|
|
|
}
|
|
|
|
return objects
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(t *testing.T, mb *meta.DB, bs *blobstor.BlobStor, objects []objectPair) {
|
|
|
|
for i := range objects {
|
|
|
|
var mPrm meta.StorageIDPrm
|
|
|
|
mPrm.SetAddress(objects[i].addr)
|
|
|
|
|
|
|
|
mRes, err := mb.StorageID(context.Background(), mPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var prm common.GetPrm
|
|
|
|
prm.Address = objects[i].addr
|
|
|
|
prm.StorageID = mRes.StorageID()
|
|
|
|
|
|
|
|
res, err := bs.Get(context.Background(), prm)
|
2024-09-09 15:37:06 +00:00
|
|
|
require.NoError(t, err, objects[i].addr)
|
2023-06-22 11:55:30 +00:00
|
|
|
require.Equal(t, objects[i].obj, res.Object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyEpoch struct{}
|
|
|
|
|
|
|
|
func (dummyEpoch) CurrentEpoch() uint64 {
|
|
|
|
return 0
|
|
|
|
}
|