-
Notifications
You must be signed in to change notification settings - Fork 533
RUBY-2234 Fix error on large bulk writes with zlib #2026
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
8 commits
Select commit
Hold shift + click to select a range
acaf043
RUBY-2234 validate message size before compressing to ensure that bul…
7846be1
fix jruby buffer issues
d61527a
Use buffer.put_bytes and write in-depth comments about the situation.
6ffe815
Use get_bytes and put_bytes instead of get_string.
d909954
remove unnecessary conditional
d1771a1
simplify code
f6ef822
added todo comment
5c7dfc6
seraizlie the message inside a conditional
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 |
|---|---|---|
|
|
@@ -186,9 +186,6 @@ def deliver(message, client, options = {}) | |
| end | ||
|
|
||
| def serialize(message, client, buffer = BSON::ByteBuffer.new) | ||
| start_size = 0 | ||
| final_message = message.maybe_compress(compressor, options[:zlib_compression_level]) | ||
|
|
||
| # Driver specifications only mandate the fixed 16MiB limit for | ||
| # serialized BSON documents. However, the server returns its | ||
| # active serialized BSON document size limit in the ismaster response, | ||
|
|
@@ -213,12 +210,41 @@ def serialize(message, client, buffer = BSON::ByteBuffer.new) | |
| max_bson_size += MAX_BSON_COMMAND_OVERHEAD | ||
| end | ||
|
|
||
| final_message.serialize(buffer, max_bson_size) | ||
| if max_message_size && | ||
| (buffer.length - start_size) > max_message_size | ||
| then | ||
| raise Error::MaxMessageSize.new(max_message_size) | ||
| # RUBY-2234: It is necessary to check that the message size does not | ||
| # exceed the maximum bson object size before compressing and serializing | ||
| # the final message. | ||
| # | ||
| # This is to avoid the case where the user performs a bulk write | ||
| # larger than 16MiB which, when compressed, becomes smaller than 16MiB. | ||
| # If the driver does not split the bulk writes prior to compression, | ||
| # the entire operation will be sent to the server, which will raise an | ||
| # error because the uncompressed operation exceeds the maximum bson size. | ||
| # | ||
| # To address this problem, we serialize the message prior to compression | ||
| # and raise an exception if the serialized message exceeds the maximum | ||
| # bson size. | ||
| if max_message_size | ||
| # Create a separate buffer that contains the un-compressed message | ||
| # for the purpose of checking its size. Write any pre-existing contents | ||
| # from the original buffer into the temporary one. | ||
| temp_buffer = BSON::ByteBuffer.new | ||
|
|
||
| # TODO: address the fact that this line mutates the buffer. | ||
| temp_buffer.put_bytes(buffer.get_bytes(buffer.length)) | ||
|
|
||
| message.serialize(temp_buffer, max_bson_size) | ||
| if temp_buffer.length > max_message_size | ||
| raise Error::MaxMessageSize.new(max_message_size) | ||
| end | ||
| end | ||
|
Comment on lines
+226
to
239
Contributor
Author
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. @p-mongo is this what you had in mind?
Contributor
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. 👍 Yep looks good. |
||
|
|
||
| # RUBY-2335: When the un-compressed message is smaller than the maximum | ||
| # bson size limit, the message will be serialized twice. The operations | ||
| # layer should be refactored to allow compression on an already- | ||
| # serialized message. | ||
| final_message = message.maybe_compress(compressor, options[:zlib_compression_level]) | ||
| final_message.serialize(buffer, max_bson_size) | ||
|
|
||
| buffer | ||
| end | ||
| end | ||
|
|
||
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,19 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe 'Bulk writes' do | ||
| before do | ||
| authorized_collection.drop | ||
| end | ||
|
|
||
| context 'when bulk write is larger than 48MB' do | ||
| let(:operations) do | ||
| [ { insert_one: { text: 'a' * 1000 * 1000 } } ] * 48 | ||
| end | ||
|
|
||
| it 'succeeds' do | ||
| expect do | ||
| authorized_collection.bulk_write(operations) | ||
| end.not_to raise_error | ||
| end | ||
| end | ||
| end |
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
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.
Uh oh!
There was an error while loading. Please reload this page.