2021-09-13 11:51:17 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2022-01-20 16:32:49 +00:00
|
|
|
"bytes"
|
2021-09-13 11:51:17 +00:00
|
|
|
"encoding/binary"
|
2022-01-20 16:32:49 +00:00
|
|
|
"errors"
|
2021-09-13 11:51:17 +00:00
|
|
|
"os"
|
2022-01-20 16:32:49 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2021-09-13 11:51:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-01-20 16:32:49 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
2021-09-13 11:51:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2021-09-13 11:51:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIterateObjects(t *testing.T) {
|
|
|
|
p := t.Name()
|
|
|
|
|
|
|
|
const smalSz = 50
|
|
|
|
|
|
|
|
// create BlobStor instance
|
|
|
|
blobStor := New(
|
2021-10-07 14:19:55 +00:00
|
|
|
WithCompressObjects(true),
|
2021-09-13 11:51:17 +00:00
|
|
|
WithRootPath(p),
|
|
|
|
WithSmallSizeLimit(smalSz),
|
|
|
|
WithBlobovniczaShallowWidth(1),
|
|
|
|
WithBlobovniczaShallowDepth(1),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer os.RemoveAll(p)
|
|
|
|
|
|
|
|
// open Blobstor
|
|
|
|
require.NoError(t, blobStor.Open())
|
|
|
|
|
|
|
|
// initialize Blobstor
|
|
|
|
require.NoError(t, blobStor.Init())
|
|
|
|
|
|
|
|
defer blobStor.Close()
|
|
|
|
|
|
|
|
const objNum = 5
|
|
|
|
|
|
|
|
type addrData struct {
|
|
|
|
big bool
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-09-13 11:51:17 +00:00
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
mObjs := make(map[string]addrData)
|
|
|
|
|
|
|
|
for i := uint64(0); i < objNum; i++ {
|
|
|
|
sz := smalSz
|
|
|
|
|
|
|
|
big := i < objNum/2
|
|
|
|
if big {
|
|
|
|
sz++
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, sz)
|
|
|
|
binary.BigEndian.PutUint64(data, i)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr := oidtest.Address()
|
2021-09-13 11:51:17 +00:00
|
|
|
|
|
|
|
mObjs[string(data)] = addrData{
|
|
|
|
big: big,
|
|
|
|
addr: addr,
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range mObjs {
|
2022-01-10 12:46:01 +00:00
|
|
|
_, err := blobStor.PutRaw(v.addr, v.data, true)
|
2021-09-13 11:51:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-21 07:52:03 +00:00
|
|
|
err := IterateBinaryObjects(blobStor, func(_ oid.Address, data []byte, blzID *blobovnicza.ID) error {
|
2021-09-13 11:51:17 +00:00
|
|
|
v, ok := mObjs[string(data)]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, v.data, data)
|
|
|
|
|
|
|
|
if v.big {
|
|
|
|
require.Nil(t, blzID)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, blzID)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(mObjs, string(data))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, mObjs)
|
|
|
|
}
|
2022-01-20 16:32:49 +00:00
|
|
|
|
|
|
|
func TestIterate_IgnoreErrors(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
const (
|
|
|
|
smallSize = 512
|
|
|
|
objCount = 5
|
|
|
|
)
|
|
|
|
bsOpts := []Option{
|
|
|
|
WithCompressObjects(true),
|
|
|
|
WithRootPath(dir),
|
|
|
|
WithSmallSizeLimit(smallSize * 2), // + header
|
|
|
|
WithBlobovniczaOpenedCacheSize(1),
|
|
|
|
WithBlobovniczaShallowWidth(1),
|
|
|
|
WithBlobovniczaShallowDepth(1)}
|
|
|
|
bs := New(bsOpts...)
|
|
|
|
require.NoError(t, bs.Open())
|
|
|
|
require.NoError(t, bs.Init())
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addrs := make([]oid.Address, objCount)
|
2022-01-20 16:32:49 +00:00
|
|
|
for i := range addrs {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrs[i] = oidtest.Address()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2022-05-31 17:00:41 +00:00
|
|
|
obj.SetContainerID(addrs[i].Container())
|
|
|
|
obj.SetID(addrs[i].Object())
|
2022-01-20 16:32:49 +00:00
|
|
|
obj.SetPayload(make([]byte, smallSize<<(i%2)))
|
|
|
|
|
|
|
|
objData, err := obj.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = bs.PutRaw(addrs[i], objData, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct corrupted compressed object.
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
badObject := make([]byte, smallSize/2+1)
|
|
|
|
enc, err := zstd.NewWriter(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rawData := enc.EncodeAll(badObject, nil)
|
|
|
|
for i := 4; /* magic size */ i < len(rawData); i += 2 {
|
|
|
|
rawData[i] ^= 0xFF
|
|
|
|
}
|
|
|
|
// Will be put uncompressed but fetched as compressed because of magic.
|
2022-05-31 17:00:41 +00:00
|
|
|
_, err = bs.PutRaw(oidtest.Address(), rawData, false)
|
2022-01-20 16:32:49 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-31 17:00:41 +00:00
|
|
|
require.NoError(t, bs.fsTree.Put(oidtest.Address(), rawData))
|
2022-01-20 16:32:49 +00:00
|
|
|
|
|
|
|
require.NoError(t, bs.Close())
|
|
|
|
|
|
|
|
// Increase width to have blobovnicza which is definitely empty.
|
|
|
|
b := New(append(bsOpts, WithBlobovniczaShallowWidth(2))...)
|
|
|
|
require.NoError(t, b.Open())
|
|
|
|
require.NoError(t, b.Init())
|
|
|
|
|
|
|
|
var p string
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
bp := filepath.Join(bs.blzRootPath, "1", strconv.FormatUint(uint64(i), 10))
|
|
|
|
if _, ok := bs.blobovniczas.opened.Get(bp); !ok {
|
|
|
|
p = bp
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotEqual(t, "", p, "expected to not have at least 1 blobovnicza in cache")
|
|
|
|
require.NoError(t, os.Chmod(p, 0))
|
|
|
|
|
2022-03-25 07:59:32 +00:00
|
|
|
require.NoError(t, b.Close())
|
|
|
|
require.NoError(t, bs.Open())
|
|
|
|
require.NoError(t, bs.Init())
|
|
|
|
|
2022-01-20 16:32:49 +00:00
|
|
|
var prm IteratePrm
|
|
|
|
prm.SetIterationHandler(func(e IterationElement) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_, err = bs.Iterate(prm)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
prm.IgnoreErrors()
|
|
|
|
|
|
|
|
t.Run("skip invalid objects", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
actual := make([]oid.Address, 0, len(addrs))
|
2022-01-20 16:32:49 +00:00
|
|
|
prm.SetIterationHandler(func(e IterationElement) error {
|
|
|
|
obj := object.New()
|
|
|
|
err := obj.Unmarshal(e.data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := obj.ContainerID()
|
2022-05-31 17:00:41 +00:00
|
|
|
addr.SetContainer(cnr)
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := obj.ID()
|
2022-05-31 17:00:41 +00:00
|
|
|
addr.SetObject(id)
|
2022-01-20 16:32:49 +00:00
|
|
|
actual = append(actual, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := bs.Iterate(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, addrs, actual)
|
|
|
|
})
|
|
|
|
t.Run("return errors from handler", func(t *testing.T) {
|
|
|
|
n := 0
|
|
|
|
expectedErr := errors.New("expected error")
|
|
|
|
prm.SetIterationHandler(func(e IterationElement) error {
|
|
|
|
if n++; n == objCount/2 {
|
|
|
|
return expectedErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_, err := bs.Iterate(prm)
|
2022-04-29 10:22:29 +00:00
|
|
|
require.ErrorIs(t, err, expectedErr)
|
2022-01-20 16:32:49 +00:00
|
|
|
})
|
|
|
|
}
|