Skip to content

Commit 6922894

Browse files
p-mongop
authored andcommitted
RUBY-1904 Consolidate response handling code in operations layer (#1506)
1 parent 1479906 commit 6922894

File tree

9 files changed

+95
-112
lines changed

9 files changed

+95
-112
lines changed

lib/mongo/bulk_write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
module Mongo
2424
class BulkWrite
2525
extend Forwardable
26-
include Operation::Unpinnable
26+
include Operation::ResponseHandling
2727

2828
# @return [ Mongo::Collection ] collection The collection.
2929
attr_reader :collection

lib/mongo/operation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'mongo/operation/result'
22

3+
require 'mongo/operation/shared/response_handling'
34
require 'mongo/operation/shared/executable'
45
require 'mongo/operation/shared/executable_no_validate'
56
require 'mongo/operation/shared/executable_transaction_label'
@@ -13,7 +14,6 @@
1314
require 'mongo/operation/shared/causal_consistency_supported'
1415
require 'mongo/operation/shared/write'
1516
require 'mongo/operation/shared/idable'
16-
require 'mongo/operation/shared/unpinnable'
1717
require 'mongo/operation/shared/specifiable'
1818
require 'mongo/operation/shared/object_id_generator'
1919
require 'mongo/operation/shared/op_msg_or_command'

lib/mongo/operation/insert/command.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,18 @@ class Insert
2424
class Command
2525
include Specifiable
2626
include Executable
27+
include ExecutableNoValidate
2728
include Idable
2829
include Limited
2930
include WriteConcernSupported
3031
include BypassDocumentValidation
3132

32-
# Execute the operation.
33-
#
34-
# @example
35-
# operation.execute(server)
36-
#
37-
# @param [ Mongo::Server ] server The server to send the operation to.
38-
#
39-
# @return [ Mongo::Operation::Insert::Result ] The operation result.
40-
#
41-
# @since 2.5.2
42-
def execute(server)
43-
result = Result.new(dispatch_message(server), @ids)
44-
process_result(result, server)
45-
end
46-
4733
private
4834

35+
def get_result(server)
36+
Result.new(dispatch_message(server), @ids)
37+
end
38+
4939
def selector(server)
5040
{ insert: coll_name,
5141
documents: send(IDENTIFIER),

lib/mongo/operation/insert/legacy.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,12 @@ class Legacy
2828
include Executable
2929
include Idable
3030

31-
# Execute the operation.
32-
#
33-
# @example
34-
# operation.execute(server)
35-
#
36-
# @param [ Mongo::Server ] server The server to send the operation to.
37-
#
38-
# @return [ Mongo::Operation::Insert::Result ] The operation result.
39-
#
40-
# @since 2.5.2
41-
def execute(server)
42-
result = Result.new(dispatch_message(server), @ids)
43-
process_result(result, server)
44-
result.validate!
45-
end
46-
4731
private
4832

33+
def get_result(server)
34+
Result.new(dispatch_message(server), @ids)
35+
end
36+
4937
def selector
5038
send(IDENTIFIER).first
5139
end

lib/mongo/operation/shared/executable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module Operation
2020
# @since 2.5.2
2121
module Executable
2222

23+
include ResponseHandling
24+
2325
def do_execute(server)
2426
unpin_maybe(session) do
2527
add_error_labels do
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (C) 2019 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Mongo
16+
module Operation
17+
18+
# Shared behavior of response handling for operations.
19+
#
20+
# @api private
21+
module ResponseHandling
22+
23+
private
24+
25+
def validate_result(result)
26+
unpin_maybe(session) do
27+
add_error_labels do
28+
result.validate!
29+
end
30+
end
31+
end
32+
33+
# Adds error labels to exceptions raised in the yielded to block,
34+
# which should perform MongoDB operations and raise Mongo::Errors on
35+
# failure. This method handles network errors (Error::SocketError)
36+
# and server-side errors (Error::OperationFailure); it does not
37+
# handle server selection errors (Error::NoServerAvailable), for which
38+
# labels are added in the server selection code.
39+
def add_error_labels
40+
begin
41+
yield
42+
rescue Mongo::Error::SocketError => e
43+
if session && session.in_transaction? && !session.committing_transaction?
44+
e.add_label('TransientTransactionError')
45+
end
46+
if session && session.committing_transaction?
47+
e.add_label('UnknownTransactionCommitResult')
48+
end
49+
raise e
50+
rescue Mongo::Error::OperationFailure => e
51+
if session && session.committing_transaction?
52+
if e.write_retryable? || e.wtimeout? || (e.write_concern_error? &&
53+
!Session::UNLABELED_WRITE_CONCERN_CODES.include?(e.write_concern_error_code)
54+
) || e.max_time_ms_expired?
55+
e.add_label('UnknownTransactionCommitResult')
56+
end
57+
end
58+
raise e
59+
end
60+
end
61+
62+
# Unpins the session if the session is pinned and the yielded to block
63+
# raises errors that are required to unpin the session.
64+
#
65+
# @note This method takes the session as an argument because this module
66+
# is included in BulkWrite which does not store the session in the
67+
# receiver (despite Specifiable doing so).
68+
#
69+
# @param [ Session | nil ] Session to consider.
70+
def unpin_maybe(session)
71+
yield
72+
rescue Mongo::Error => e
73+
if session
74+
session.unpin_maybe(e)
75+
end
76+
raise
77+
end
78+
end
79+
end
80+
end

lib/mongo/operation/shared/specifiable.rb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Operation
2020
#
2121
# @since 2.0.0
2222
module Specifiable
23-
include Unpinnable
2423

2524
# The field for database name.
2625
#
@@ -565,45 +564,6 @@ def array_filters
565564
def acknowledged_write?
566565
write_concern.nil? || write_concern.acknowledged?
567566
end
568-
569-
private
570-
571-
def validate_result(result)
572-
unpin_maybe(session) do
573-
add_error_labels do
574-
result.validate!
575-
end
576-
end
577-
end
578-
579-
# Adds error labels to exceptions raised in the yielded to block,
580-
# which should perform MongoDB operations and raise Mongo::Errors on
581-
# failure. This method handles network errors (Error::SocketError)
582-
# and server-side errors (Error::OperationFailure); it does not
583-
# handle server selection errors (Error::NoServerAvailable), for which
584-
# labels are added in the server selection code.
585-
def add_error_labels
586-
begin
587-
yield
588-
rescue Mongo::Error::SocketError => e
589-
if session && session.in_transaction? && !session.committing_transaction?
590-
e.add_label('TransientTransactionError')
591-
end
592-
if session && session.committing_transaction?
593-
e.add_label('UnknownTransactionCommitResult')
594-
end
595-
raise e
596-
rescue Mongo::Error::OperationFailure => e
597-
if session && session.committing_transaction?
598-
if e.write_retryable? || e.wtimeout? || (e.write_concern_error? &&
599-
!Session::UNLABELED_WRITE_CONCERN_CODES.include?(e.write_concern_error_code)
600-
) || e.max_time_ms_expired?
601-
e.add_label('UnknownTransactionCommitResult')
602-
end
603-
end
604-
raise e
605-
end
606-
end
607567
end
608568
end
609569
end

lib/mongo/operation/shared/unpinnable.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/mongo/operation/shared/write.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module Operation
2020
# @since 2.5.2
2121
module Write
2222

23+
include ResponseHandling
24+
2325
# Execute the operation.
2426
#
2527
# @example

0 commit comments

Comments
 (0)