-
Notifications
You must be signed in to change notification settings - Fork 45
fstree: make a new object storage structure #3451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
End-rey
wants to merge
2
commits into
master
Choose a base branch
from
new-object-structure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package fstree_test | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/nspcc-dev/neofs-node/pkg/core/object" | ||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/compression" | ||
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree" | ||
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" | ||
oid "github.com/nspcc-dev/neofs-sdk-go/object/id" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkFSTree_Head(b *testing.B) { | ||
for _, size := range payloadSizes { | ||
b.Run(generateSizeLabel(size), func(b *testing.B) { | ||
fsTree := fstree.New(fstree.WithPath(b.TempDir())) | ||
|
||
require.NoError(b, fsTree.Open(false)) | ||
require.NoError(b, fsTree.Init()) | ||
|
||
testReadOp(b, fsTree, fsTree.Head, "Head", size) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFSTree_Get(b *testing.B) { | ||
for _, size := range payloadSizes { | ||
b.Run(generateSizeLabel(size), func(b *testing.B) { | ||
fsTree := fstree.New(fstree.WithPath(b.TempDir())) | ||
|
||
require.NoError(b, fsTree.Open(false)) | ||
require.NoError(b, fsTree.Init()) | ||
|
||
testReadOp(b, fsTree, fsTree.Get, "Get", size) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFSTree_GetStream(b *testing.B) { | ||
for _, size := range payloadSizes { | ||
b.Run(generateSizeLabel(size), func(b *testing.B) { | ||
fsTree := fstree.New(fstree.WithPath(b.TempDir())) | ||
|
||
require.NoError(b, fsTree.Open(false)) | ||
require.NoError(b, fsTree.Init()) | ||
|
||
testGetStreamOp(b, fsTree, size) | ||
}) | ||
} | ||
} | ||
|
||
func testReadOp(b *testing.B, fsTree *fstree.FSTree, read func(address oid.Address) (*objectSDK.Object, error), | ||
name string, payloadSize int) { | ||
b.Run(name+"_regular", func(b *testing.B) { | ||
obj := generateTestObject(payloadSize) | ||
addr := object.AddressOf(obj) | ||
|
||
require.NoError(b, fsTree.Put(addr, obj.Marshal())) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for range b.N { | ||
_, err := read(addr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run(name+"_combined", func(b *testing.B) { | ||
const numObjects = 10 | ||
|
||
objMap := make(map[oid.Address][]byte, numObjects) | ||
addrs := make([]oid.Address, numObjects) | ||
for i := range numObjects { | ||
o := generateTestObject(payloadSize) | ||
objMap[object.AddressOf(o)] = o.Marshal() | ||
addrs[i] = object.AddressOf(o) | ||
} | ||
require.NoError(b, fsTree.PutBatch(objMap)) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for k := range b.N { | ||
_, err := read(addrs[k%numObjects]) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run(name+"_compressed", func(b *testing.B) { | ||
obj := generateTestObject(payloadSize) | ||
addr := object.AddressOf(obj) | ||
|
||
compressConfig := &compression.Config{ | ||
Enabled: true, | ||
} | ||
require.NoError(b, compressConfig.Init()) | ||
fsTree.SetCompressor(compressConfig) | ||
require.NoError(b, fsTree.Put(addr, obj.Marshal())) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for range b.N { | ||
_, err := read(addr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func testGetStreamOp(b *testing.B, fsTree *fstree.FSTree, payloadSize int) { | ||
b.Run("GetStream_regular", func(b *testing.B) { | ||
obj := generateTestObject(payloadSize) | ||
addr := object.AddressOf(obj) | ||
|
||
require.NoError(b, fsTree.Put(addr, obj.Marshal())) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for range b.N { | ||
header, reader, err := fsTree.GetStream(addr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if header == nil { | ||
b.Fatal("header is nil") | ||
} | ||
if reader != nil { | ||
reader.Close() | ||
} | ||
} | ||
}) | ||
|
||
b.Run("GetStream_combined", func(b *testing.B) { | ||
const numObjects = 10 | ||
|
||
objMap := make(map[oid.Address][]byte, numObjects) | ||
addrs := make([]oid.Address, numObjects) | ||
for i := range numObjects { | ||
o := generateTestObject(payloadSize) | ||
objMap[object.AddressOf(o)] = o.Marshal() | ||
addrs[i] = object.AddressOf(o) | ||
} | ||
require.NoError(b, fsTree.PutBatch(objMap)) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for k := range b.N { | ||
header, reader, err := fsTree.GetStream(addrs[k%numObjects]) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if header == nil { | ||
b.Fatal("header is nil") | ||
} | ||
if reader != nil { | ||
reader.Close() | ||
} | ||
} | ||
}) | ||
|
||
b.Run("GetStream_compressed", func(b *testing.B) { | ||
obj := generateTestObject(payloadSize) | ||
addr := object.AddressOf(obj) | ||
|
||
compressConfig := &compression.Config{ | ||
Enabled: true, | ||
} | ||
require.NoError(b, compressConfig.Init()) | ||
fsTree.SetCompressor(compressConfig) | ||
require.NoError(b, fsTree.Put(addr, obj.Marshal())) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for range b.N { | ||
header, reader, err := fsTree.GetStream(addr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if header == nil { | ||
b.Fatal("header is nil") | ||
} | ||
if reader != nil { | ||
reader.Close() | ||
} | ||
} | ||
}) | ||
|
||
b.Run("GetStream_with_payload_read", func(b *testing.B) { | ||
obj := generateTestObject(payloadSize) | ||
addr := object.AddressOf(obj) | ||
|
||
require.NoError(b, fsTree.Put(addr, obj.Marshal())) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for range b.N { | ||
header, reader, err := fsTree.GetStream(addr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if header == nil { | ||
b.Fatal("header is nil") | ||
} | ||
if reader != nil { | ||
// Read all payload to simulate real usage | ||
_, err := io.ReadAll(reader) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
reader.Close() | ||
} | ||
} | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" | ||
oid "github.com/nspcc-dev/neofs-sdk-go/object/id" | ||
"go.uber.org/zap" | ||
"google.golang.org/protobuf/encoding/protowire" | ||
) | ||
|
||
// FSTree represents an object storage as a filesystem tree. | ||
|
@@ -90,6 +91,26 @@ const ( | |
// combinedDataOff is the offset from the start of the combined prefix to object data. | ||
// It's also the length of the prefix in total. | ||
combinedDataOff = combinedLengthOff + combinedLenSize | ||
|
||
// streamPrefix is the prefix for streamed objects. It is used to distinguish | ||
// streamed objects from regular ones. | ||
streamPrefix = 0x7e | ||
|
||
// streamLenHeaderOff is the offset from the start of the stream prefix to | ||
// the length of the header data. | ||
streamLenHeaderOff = 2 | ||
|
||
// streamLenSize is sizeof(uint32), length of a serialized 32-bit BE integer | ||
// that represents the length of the header or payload data. | ||
streamLenSize = 4 | ||
|
||
// streamLenDataOff is the offset from the start of the stream prefix to | ||
// the length of the data. | ||
streamLenDataOff = streamLenHeaderOff + streamLenSize | ||
|
||
// streamDataOff is the offset from the start of the stream prefix to the | ||
// start of the data. It is used to read the data after the header. | ||
streamDataOff = streamLenDataOff + streamLenSize | ||
) | ||
|
||
var _ common.Storage = (*FSTree)(nil) | ||
|
@@ -339,7 +360,7 @@ func (t *FSTree) Put(addr oid.Address, data []byte) error { | |
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil { | ||
return fmt.Errorf("mkdirall for %q: %w", p, err) | ||
} | ||
data = t.Compress(data) | ||
data = t.processHeaderAndPayload(data) | ||
|
||
err := t.writer.writeData(addr.Object(), p, data) | ||
if err != nil { | ||
|
@@ -363,7 +384,7 @@ func (t *FSTree) PutBatch(objs map[oid.Address][]byte) error { | |
writeDataUnits = append(writeDataUnits, writeDataUnit{ | ||
id: addr.Object(), | ||
path: p, | ||
data: t.Compress(data), | ||
data: t.processHeaderAndPayload(data), | ||
}) | ||
} | ||
|
||
|
@@ -375,6 +396,32 @@ func (t *FSTree) PutBatch(objs map[oid.Address][]byte) error { | |
return nil | ||
} | ||
|
||
// processHeaderAndPayload processes the header and payload of the object data. | ||
func (t *FSTree) processHeaderAndPayload(data []byte) []byte { | ||
headerEnd, payloadStart, err := extractHeaderAndPayload(data, nil) | ||
if err != nil || headerEnd == 0 { | ||
return data | ||
} | ||
|
||
header := data[:headerEnd] | ||
payload := data[payloadStart:] | ||
|
||
hLen := len(header) | ||
payload = t.Compress(payload) | ||
pLen := len(payload) | ||
|
||
res := make([]byte, hLen+pLen+streamDataOff) | ||
res[0] = streamPrefix | ||
res[1] = 0 // version 0 | ||
binary.BigEndian.PutUint32(res[streamLenHeaderOff:], uint32(hLen)) | ||
binary.BigEndian.PutUint32(res[streamLenDataOff:], uint32(pLen)) | ||
|
||
copy(res[streamDataOff:], header) | ||
copy(res[streamDataOff+hLen:], payload) | ||
|
||
return res | ||
} | ||
|
||
// Get returns an object from the storage by address. | ||
func (t *FSTree) Get(addr oid.Address) (*objectSDK.Object, error) { | ||
data, err := t.getObjBytes(addr) | ||
|
@@ -433,6 +480,16 @@ func parseCombinedPrefix(p []byte) ([]byte, uint32) { | |
binary.BigEndian.Uint32(p[combinedLengthOff:combinedDataOff]) | ||
} | ||
|
||
// parseStreamPrefix checks the given byte slice for stream prefix and returns | ||
// the length of the header and data if so (0, 0 otherwise). | ||
func parseStreamPrefix(p []byte) (uint32, uint32) { | ||
if p[0] != streamPrefix || p[1] != 0 { // Only version 0 is supported now. | ||
return 0, 0 | ||
} | ||
return binary.BigEndian.Uint32(p[streamLenHeaderOff:streamLenDataOff]), | ||
binary.BigEndian.Uint32(p[streamLenDataOff:]) | ||
} | ||
|
||
func (t *FSTree) extractCombinedObject(id oid.ID, f *os.File) ([]byte, error) { | ||
var ( | ||
comBuf [combinedDataOff]byte | ||
|
@@ -485,6 +542,23 @@ func (t *FSTree) readFullObject(f io.Reader, initial []byte, size int64) ([]byte | |
return nil, fmt.Errorf("read: %w", err) | ||
} | ||
data = data[:len(initial)+n] | ||
hLen, _ := parseStreamPrefix(data) | ||
if hLen > 0 { | ||
data = data[streamDataOff:] | ||
payload, err := t.Decompress(data[hLen:]) | ||
if err != nil { | ||
return nil, fmt.Errorf("decompress payload: %w", err) | ||
} | ||
pLen := len(payload) | ||
payloadNum := protowire.Number(4) | ||
n := protowire.SizeTag(payloadNum) + protowire.SizeVarint(uint64(pLen)) | ||
buf := make([]byte, int(hLen)+pLen+n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This explains There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to commit message |
||
copy(buf[:hLen], data) | ||
off := binary.PutUvarint(buf[hLen:], protowire.EncodeTag(payloadNum, protowire.BytesType)) + int(hLen) | ||
off += binary.PutUvarint(buf[off:], uint64(pLen)) | ||
copy(buf[off:], payload) | ||
data = buf | ||
} | ||
|
||
return t.Decompress(data) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really needed, there is versioning in
combined
format.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
combinedPrefix
is only for small objects, butstreamPrefix
is for everyone, isn't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't matter much to me, the point is that it's not a simple serialized protobuf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't really understand.
combinedPrefix
is written only when the file is combined. Should I make it always written for any objects?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to use some prefix anyway, version one of
0x7f
is no worse than version zero of0x7e
. It's a bit better to me in fact.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to implement with version 1, but it turned out to be a very complicated implementation due to backward compatibility. A lot of places are becoming too complicated, due to the support of the prefix for all objects, not just combined ones, and due to the increase in prefix length. I still think that a separate prefix would be better, at least because of the separation of combined objects and the new storage format.