2021-04-14 08:50:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2022-02-22 07:35:53 +00:00
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-04-14 08:50:21 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-10-08 13:26:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2021-04-14 08:50:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-02-22 07:35:53 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-22 07:35:53 +00:00
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/address/test"
|
2022-01-26 12:11:13 +00:00
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2021-04-14 08:50:21 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
2021-10-08 13:26:24 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2021-04-14 08:50:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-31 14:58:32 +00:00
|
|
|
"go.uber.org/atomic"
|
2021-04-14 08:50:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-02-22 07:35:53 +00:00
|
|
|
func BenchmarkExists(b *testing.B) {
|
|
|
|
b.Run("2 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 2)
|
|
|
|
})
|
|
|
|
b.Run("4 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 4)
|
|
|
|
})
|
|
|
|
b.Run("8 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 8)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkExists(b *testing.B, shardNum int) {
|
|
|
|
shards := make([]*shard.Shard, shardNum)
|
|
|
|
for i := 0; i < shardNum; i++ {
|
|
|
|
shards[i] = testNewShard(b, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := testNewEngineWithShards(shards...)
|
|
|
|
b.Cleanup(func() {
|
|
|
|
_ = e.Close()
|
|
|
|
_ = os.RemoveAll(b.Name())
|
|
|
|
})
|
|
|
|
|
|
|
|
addr := objecttest.Address()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
obj := generateRawObjectWithCID(b, cidtest.ID())
|
|
|
|
err := Put(e, obj.Object())
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ok, err := e.exists(addr)
|
|
|
|
if err != nil || ok {
|
|
|
|
b.Fatalf("%t %v", ok, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:50:21 +00:00
|
|
|
func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine {
|
|
|
|
engine := &StorageEngine{
|
|
|
|
cfg: &cfg{
|
|
|
|
log: zap.L(),
|
|
|
|
},
|
2021-10-08 13:26:24 +00:00
|
|
|
mtx: new(sync.RWMutex),
|
2022-01-31 14:58:32 +00:00
|
|
|
shards: make(map[string]shardWrapper, len(shards)),
|
2021-10-08 13:26:24 +00:00
|
|
|
shardPools: make(map[string]util.WorkerPool, len(shards)),
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range shards {
|
2021-10-08 13:26:24 +00:00
|
|
|
pool, err := ants.NewPool(10, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
engine.shards[s.ID().String()] = shardWrapper{
|
|
|
|
errorCount: atomic.NewUint32(0),
|
|
|
|
Shard: s,
|
|
|
|
}
|
2021-10-08 13:26:24 +00:00
|
|
|
engine.shardPools[s.ID().String()] = pool
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:35:53 +00:00
|
|
|
func testNewShard(t testing.TB, id int) *shard.Shard {
|
2021-04-14 08:50:21 +00:00
|
|
|
sid, err := generateShardID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := shard.New(
|
|
|
|
shard.WithID(sid),
|
|
|
|
shard.WithLogger(zap.L()),
|
|
|
|
shard.WithBlobStorOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
blobstor.WithRootPath(filepath.Join(t.Name(), fmt.Sprintf("%d.blobstor", id))),
|
2021-04-14 08:50:21 +00:00
|
|
|
blobstor.WithBlobovniczaShallowWidth(2),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(2),
|
|
|
|
blobstor.WithRootPerm(0700),
|
|
|
|
),
|
|
|
|
shard.WithMetaBaseOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.metabase", id))),
|
2021-04-14 08:50:21 +00:00
|
|
|
meta.WithPermissions(0700),
|
|
|
|
))
|
|
|
|
|
|
|
|
require.NoError(t, s.Open())
|
|
|
|
require.NoError(t, s.Init())
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func testOID() *oidSDK.ID {
|
2021-04-14 08:50:21 +00:00
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
_, _ = rand.Read(cs[:])
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
id := oidSDK.NewID()
|
2021-04-14 08:50:21 +00:00
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:35:53 +00:00
|
|
|
func generateRawObjectWithCID(t testing.TB, cid *cid.ID) *object.RawObject {
|
2021-11-10 07:08:33 +00:00
|
|
|
version := version.New()
|
2021-04-14 08:50:21 +00:00
|
|
|
version.SetMajor(2)
|
|
|
|
version.SetMinor(1)
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csum := new(checksum.Checksum)
|
2022-01-21 12:15:10 +00:00
|
|
|
csum.SetSHA256(sha256.Sum256(owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey)))
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csumTZ := new(checksum.Checksum)
|
2021-04-14 08:50:21 +00:00
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Sum()))
|
|
|
|
|
|
|
|
obj := object.NewRaw()
|
|
|
|
obj.SetID(testOID())
|
2021-12-01 13:56:48 +00:00
|
|
|
obj.SetOwnerID(ownertest.ID())
|
2021-04-14 08:50:21 +00:00
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetVersion(version)
|
|
|
|
obj.SetPayloadChecksum(csum)
|
|
|
|
obj.SetPayloadHomomorphicHash(csumTZ)
|
|
|
|
obj.SetPayload([]byte{1, 2, 3, 4, 5})
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAttribute(obj *object.RawObject, key, val string) {
|
|
|
|
attr := objectSDK.NewAttribute()
|
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
|
|
|
|
|
|
|
attrs := obj.Attributes()
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
}
|
2021-11-11 13:58:07 +00:00
|
|
|
|
|
|
|
func testNewEngineWithShardNum(t *testing.T, num int) *StorageEngine {
|
|
|
|
shards := make([]*shard.Shard, 0, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
shards = append(shards, testNewShard(t, i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return testNewEngineWithShards(shards...)
|
|
|
|
}
|