-
Notifications
You must be signed in to change notification settings - Fork 530
RUBY-3602 Include server address in OperationFailure and BulkWriteError messages #3027
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
12 commits
Select commit
Hold shift + click to select a range
fa03598
RUBY-3602 add include_server_address_in_errors config flag
comandeo-mongo 5f282dd
RUBY-3602 accept server_address option in OperationFailure
comandeo-mongo 241c3b1
RUBY-3602 cover Address and Description inputs for OperationFailure#s…
comandeo-mongo 0898aea
RUBY-3602 render server address suffix in OperationFailure message
comandeo-mongo 5af60b7
RUBY-3602 pin include_server_address_in_errors flag in flag-off context
comandeo-mongo 409e500
RUBY-3602 pass server_address from Operation::Result to OperationFailure
comandeo-mongo c39ff04
RUBY-3602 accept server_addresses kwarg in BulkWriteError
comandeo-mongo 07c2b63
RUBY-3602 render server address suffix in BulkWriteError message
comandeo-mongo 9650b51
RUBY-3602 thread server_addresses through BulkWrite::Result and Resul…
comandeo-mongo dd8029b
RUBY-3602 integration coverage for BulkWriteError server address suffix
comandeo-mongo 3c1e89b
RUBY-3602 audit fixups
comandeo-mongo 6535216
RUBY-3602 dedupe server_addresses at assignment
comandeo-mongo 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
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 |
|---|---|---|
|
|
@@ -34,17 +34,26 @@ class BulkWriteError < Error | |
| # @return [ BSON::Document ] result The error result. | ||
| attr_reader :result | ||
|
|
||
| # @return [ Array<String> ] Deduplicated list of "host:port" addresses of | ||
| # the servers that produced this bulk write error. Empty when no | ||
| # addresses were supplied. | ||
| attr_reader :server_addresses | ||
|
|
||
| # Instantiate the new exception. | ||
| # | ||
| # @example Instantiate the exception. | ||
| # Mongo::Error::BulkWriteError.new(response) | ||
| # | ||
| # @param [ Hash ] result A processed response from the server | ||
| # reporting results of the operation. | ||
| # @param [ Array<String | Mongo::Address | Mongo::Server::Description> ] | ||
| # server_addresses Addresses of the servers that produced this error. | ||
| # Entries are normalized to "host:port" strings. | ||
| # | ||
| # @since 2.0.0 | ||
| def initialize(result) | ||
| def initialize(result, server_addresses: nil) | ||
| @result = result | ||
| @server_addresses = normalize_server_addresses(server_addresses) | ||
|
|
||
|
Comment on lines
+54
to
57
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. Good catch. Fixed in 6535216 — |
||
| # Exception constructor behaves differently for a nil argument and | ||
| # for no argument. Avoid passing nil explicitly. | ||
|
|
@@ -93,8 +102,27 @@ def build_message | |
|
|
||
| fragment = "Multiple errors: #{fragment}" if errors.length > 1 | ||
|
|
||
| if Mongo.include_server_address_in_errors && @server_addresses.any? | ||
| fragment = "#{fragment} (on #{@server_addresses.join(', ')})" | ||
| end | ||
|
|
||
| fragment | ||
| end | ||
|
|
||
| def normalize_server_addresses(value) | ||
| return [] if value.nil? | ||
|
|
||
| Array(value).filter_map do |entry| | ||
| case entry | ||
| when String then entry | ||
| when Mongo::Address then entry.seed | ||
| when Mongo::Server::Description then entry.address&.seed | ||
| else | ||
| raise ArgumentError, | ||
| "server_addresses entries must be String, Mongo::Address, or Mongo::Server::Description; got #{entry.class}" | ||
| end | ||
| end.uniq | ||
| end | ||
| 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
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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| describe Mongo::BulkWrite::ResultCombiner do | ||
| describe 'server_addresses accumulation' do | ||
| let(:combiner) { described_class.new } | ||
|
|
||
| def stub_op_result(seed) | ||
| description = instance_double( | ||
| Mongo::Server::Description, | ||
| address: Mongo::Address.new(seed) | ||
| ) | ||
| result = double('op_result') | ||
| allow(result).to receive(:write_concern_error?).and_return(false) | ||
| allow(result).to receive(:acknowledged?).and_return(true) | ||
| allow(result).to receive(:aggregate_write_errors).and_return(nil) | ||
| allow(result).to receive(:aggregate_write_concern_errors).and_return(nil) | ||
| allow(result).to receive(:validate!).and_return(true) | ||
| allow(result).to receive(:respond_to?).and_return(false) | ||
| allow(result).to receive(:connection_description).and_return(description) | ||
| result | ||
| end | ||
|
|
||
| it 'collects unique seeds from combined results' do | ||
| combiner.combine!(stub_op_result('h1:27017'), 1) | ||
| combiner.combine!(stub_op_result('h2:27017'), 1) | ||
| combiner.combine!(stub_op_result('h1:27017'), 1) | ||
|
|
||
| final = combiner.result | ||
| expect(final.server_addresses).to contain_exactly('h1:27017', 'h2:27017') | ||
| end | ||
|
|
||
| it 'defaults to empty when no results combined' do | ||
| expect(combiner.server_addresses).to eq([]) | ||
| 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
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.
Leaving this as-is. The complexity here is O(n²) in the number of distinct server addresses, not the number of batches — dedup keeps n tiny. A bulk write targets a primary in a replset or a handful of shards (typically n ≤ 10), so even thousands of combined batches do only a few thousand comparisons total. Switching to a
Setwould add a second data structure without a measurable win; happy to revisit if profiling ever turns this up.