-
Notifications
You must be signed in to change notification settings - Fork 50
Implement EC logic in PUT #3457
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
02106af
sn/object: Refactor broadcast on PUT
cthulhu-rider f376418
sn/object: Separate code for ready object saving
cthulhu-rider 22e3dfc
sn/object: Replace stateless method with standalone function
cthulhu-rider cd3e5d3
sn/object: Initial support for erasure coding policies by PUT server
cthulhu-rider 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
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
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
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,80 @@ | ||
| package ec | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "slices" | ||
| "strconv" | ||
|
|
||
| "github.com/klauspost/reedsolomon" | ||
| ) | ||
|
|
||
| // Erasure coding attributes. | ||
| const ( | ||
| AttributePrefix = "__NEOFS__EC_" | ||
| AttributeRuleIdx = AttributePrefix + "RULE_IDX" | ||
| AttributePartIdx = AttributePrefix + "PART_IDX" | ||
| ) | ||
|
|
||
| // Rule represents erasure coding rule for object payload's encoding and placement. | ||
| type Rule struct { | ||
| DataPartNum uint8 | ||
| ParityPartNum uint8 | ||
| } | ||
|
|
||
| // String implements [fmt.Stringer]. | ||
| func (x Rule) String() string { | ||
| return strconv.FormatUint(uint64(x.DataPartNum), 10) + "/" + strconv.FormatUint(uint64(x.ParityPartNum), 10) | ||
| } | ||
|
|
||
| // Encode encodes given data according to specified EC rule and returns coded | ||
| // parts. First [Rule.DataPartNum] elements are data parts, other | ||
| // [Rule.ParityPartNum] ones are parity blocks. | ||
| // | ||
| // All parts are the same length. If data len is not divisible by | ||
| // [Rule.DataPartNum], last data part is aligned with zeros. | ||
| // | ||
| // If data is empty, all parts are nil. | ||
| func Encode(rule Rule, data []byte) ([][]byte, error) { | ||
| if len(data) == 0 { | ||
| return make([][]byte, rule.DataPartNum+rule.ParityPartNum), nil | ||
| } | ||
|
|
||
| // TODO: Explore reedsolomon.Option for performance improvement. https://github.com/nspcc-dev/neofs-node/issues/3501 | ||
| enc, err := reedsolomon.New(int(rule.DataPartNum), int(rule.ParityPartNum)) | ||
| if err != nil { // should never happen with correct rule | ||
| return nil, fmt.Errorf("init Reed-Solomon encoder: %w", err) | ||
| } | ||
|
|
||
| parts, err := enc.Split(data) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("split data: %w", err) | ||
| } | ||
|
|
||
| if err := enc.Encode(parts); err != nil { | ||
| return nil, fmt.Errorf("calculate Reed-Solomon parity: %w", err) | ||
| } | ||
|
|
||
| return parts, nil | ||
| } | ||
|
|
||
| // Decode decodes source data of known len from EC parts obtained by applying | ||
| // specified rule. | ||
| func Decode(rule Rule, dataLen uint64, parts [][]byte) ([]byte, error) { | ||
| // TODO: Explore reedsolomon.Option for performance improvement. https://github.com/nspcc-dev/neofs-node/issues/3501 | ||
| dec, err := reedsolomon.New(int(rule.DataPartNum), int(rule.ParityPartNum)) | ||
| if err != nil { // should never happen with correct rule | ||
| return nil, fmt.Errorf("init Reed-Solomon decoder: %w", err) | ||
| } | ||
|
|
||
| required := make([]bool, rule.DataPartNum+rule.ParityPartNum) | ||
| for i := range rule.DataPartNum { | ||
| required[i] = true | ||
| } | ||
|
|
||
| if err := dec.ReconstructSome(parts, required); err != nil { | ||
| return nil, fmt.Errorf("restore Reed-Solomon: %w", err) | ||
| } | ||
|
|
||
| // TODO: last part may be shorter, do not overallocate buffer. | ||
| return slices.Concat(parts[:rule.DataPartNum]...)[:dataLen], nil | ||
| } | ||
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,76 @@ | ||
| package ec_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/klauspost/reedsolomon" | ||
| iec "github.com/nspcc-dev/neofs-node/internal/ec" | ||
| islices "github.com/nspcc-dev/neofs-node/internal/slices" | ||
| "github.com/nspcc-dev/neofs-node/internal/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRule_String(t *testing.T) { | ||
| r := iec.Rule{ | ||
| DataPartNum: 12, | ||
| ParityPartNum: 23, | ||
| } | ||
| require.Equal(t, "12/23", r.String()) | ||
| } | ||
|
|
||
| func testEncode(t *testing.T, rule iec.Rule, data []byte) { | ||
| ln := uint64(len(data)) | ||
|
|
||
| parts, err := iec.Encode(rule, data) | ||
| require.NoError(t, err) | ||
|
|
||
| res, err := iec.Decode(rule, ln, parts) | ||
| require.NoError(t, err) | ||
| require.Equal(t, data, res) | ||
|
|
||
| for lostCount := 1; lostCount <= int(rule.ParityPartNum); lostCount++ { | ||
| for _, lostIdxs := range islices.IndexCombos(len(parts), lostCount) { | ||
| res, err := iec.Decode(rule, ln, islices.NilTwoDimSliceElements(parts, lostIdxs)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, data, res) | ||
| } | ||
| } | ||
|
|
||
| for _, lostIdxs := range islices.IndexCombos(len(parts), int(rule.ParityPartNum)+1) { | ||
| _, err := iec.Decode(rule, ln, islices.NilTwoDimSliceElements(parts, lostIdxs)) | ||
| require.ErrorContains(t, err, "restore Reed-Solomon") | ||
| require.ErrorIs(t, err, reedsolomon.ErrTooFewShards) | ||
| } | ||
| } | ||
|
|
||
| func TestEncode(t *testing.T) { | ||
| rules := []iec.Rule{ | ||
| {DataPartNum: 3, ParityPartNum: 1}, | ||
| {DataPartNum: 12, ParityPartNum: 4}, | ||
| } | ||
|
|
||
| data := testutil.RandByteSlice(4 << 10) | ||
|
|
||
| t.Run("empty", func(t *testing.T) { | ||
| for _, rule := range rules { | ||
| t.Run(rule.String(), func(t *testing.T) { | ||
| test := func(t *testing.T, data []byte) { | ||
| res, err := iec.Encode(rule, []byte{}) | ||
| require.NoError(t, err) | ||
|
|
||
| total := int(rule.DataPartNum + rule.ParityPartNum) | ||
| require.Len(t, res, total) | ||
| require.EqualValues(t, total, islices.CountNilsInTwoDimSlice(res)) | ||
| } | ||
| test(t, nil) | ||
| test(t, []byte{}) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| for _, rule := range rules { | ||
| t.Run(rule.String(), func(t *testing.T) { | ||
| testEncode(t, rule, data) | ||
| }) | ||
| } | ||
| } |
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.
I doubt it's a problem practically, just a tiny rounding error.
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.
thinkin about parts of several MB len with the tiny last one (1B in particular)
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.
But aren't EC part all ~same size? The difference I expect here is on the order of NUM_OF_PARTS bytes.
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.
they all exactly the same size. So the last part may require alignment. And it may be quite large