forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_failure.rb
214 lines (194 loc) · 6.74 KB
/
operation_failure.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright (C) 2015-2019 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module Mongo
class Error
# Raised when an operation fails for some reason.
#
# @since 2.0.0
class OperationFailure < Error
extend Forwardable
include SdamErrorDetection
# Error codes and code names that should result in a failing write
# being retried.
#
# @since 2.6.0
# @api private
WRITE_RETRY_ERRORS = [
{:code_name => 'InterruptedAtShutdown', :code => 11600},
{:code_name => 'InterruptedDueToStepDown', :code => 11602},
{:code_name => 'NotMaster', :code => 10107},
{:code_name => 'NotMasterNoSlaveOk', :code => 13435},
{:code_name => 'NotMasterOrSecondary', :code => 13436},
{:code_name => 'PrimarySteppedDown', :code => 189},
{:code_name => 'ShutdownInProgress', :code => 91},
{:code_name => 'HostNotFound', :code => 7},
{:code_name => 'HostUnreachable', :code => 6},
{:code_name => 'NetworkTimeout', :code => 89},
{:code_name => 'SocketException', :code => 9001},
].freeze
# These are magic error messages that could indicate a master change.
#
# @since 2.4.2
# @api private
WRITE_RETRY_MESSAGES = [
'not master',
'node is recovering',
].freeze
# These are magic error messages that could indicate a cluster
# reconfiguration behind a mongos.
#
# @since 2.1.1
# @api private
RETRY_MESSAGES = WRITE_RETRY_MESSAGES + [
'transport error',
'socket exception',
"can't connect",
'connect failed',
'error querying',
'could not get last error',
'connection attempt failed',
'interrupted at shutdown',
'unknown replica set',
'dbclient error communicating with server'
].freeze
def_delegators :@result, :operation_time
# @return [ Integer ] code The error code parsed from the document.
# @since 2.6.0
attr_reader :code
# @return [ String ] code_name The error code name parsed from the document.
# @since 2.6.0
attr_reader :code_name
# Whether the error is a retryable error according to the legacy read retry
# logic.
#
# @return [ true, false ]
#
# @since 2.1.1
# @deprecated
def retryable?
RETRY_MESSAGES.any?{ |m| message.include?(m) }
end
# Whether the error is a retryable error according to the modern retryable
# reads and retryable writes specifications.
#
# This method is also used by the legacy retryable write logic to determine
# whether an error is a retryable one.
#
# @return [ true, false ]
#
# @since 2.4.2
def write_retryable?
WRITE_RETRY_MESSAGES.any? { |m| message.include?(m) } ||
write_retryable_code?
end
def write_retryable_code?
if code
WRITE_RETRY_ERRORS.any? { |e| e[:code] == code }
else
# return false rather than nil
false
end
end
private :write_retryable_code?
# Error codes and code names that should result in a failing getMore
# command on a change stream NOT being resumed.
#
# @since 2.6.0
# @api private
CHANGE_STREAM_NOT_RESUME_ERRORS = [
{:code_name => 'CappedPositionLost', :code => 136},
{:code_name => 'CursorKilled', :code => 237},
{:code_name => 'Interrupted', :code => 11601},
].freeze
# Change stream can be resumed when these error messages are encountered.
#
# @since 2.6.0
# @api private
CHANGE_STREAM_RESUME_MESSAGES = WRITE_RETRY_MESSAGES
# Can the change stream on which this error occurred be resumed,
# provided the operation that triggered this error was a getMore?
#
# @example Is the error resumable for the change stream?
# error.change_stream_resumable?
#
# @return [ true, false ] Whether the error is resumable.
#
# @since 2.6.0
def change_stream_resumable?
if @result && @result.is_a?(Mongo::Operation::GetMore::Result)
!change_stream_not_resumable_label? &&
(change_stream_resumable_message? ||
change_stream_resumable_code?)
else
false
end
end
def change_stream_resumable_message?
CHANGE_STREAM_RESUME_MESSAGES.any? { |m| message.include?(m) }
end
private :change_stream_resumable_message?
def change_stream_resumable_code?
if code
!CHANGE_STREAM_NOT_RESUME_ERRORS.any? { |e| e[:code] == code }
else
true
end
end
private :change_stream_resumable_code?
def change_stream_not_resumable_label?
if labels
labels.include? 'NonResumableChangeStreamError'
else
false
end
end
private :change_stream_not_resumable_label?
# Create the operation failure.
#
# @example Create the error object
# OperationFailure.new(message, result)
#
# @example Create the error object with a code and a code name
# OperationFailure.new(message, result, :code => code, :code_name => code_name)
#
# @param [ String ] message The error message.
# @param [ Operation::Result ] result The result object.
# @param [ Hash ] options Additional parameters
#
# @option options [ Integer ] :code Error code
# @option options [ String ] :code_name Error code name
# @option options [ Array<String> ] :labels The set of labels associated
# with the error
# @option options [ true | false ] :wtimeout Whether the error is a wtimeout
#
# @since 2.5.0, options added in 2.6.0
def initialize(message = nil, result = nil, options = {})
@result = result
@code = options[:code]
@code_name = options[:code_name]
@labels = options[:labels]
@wtimeout = !!options[:wtimeout]
super(message)
end
# Whether the error is a write concern timeout.
#
# @return [ true | false ] Whether the error is a write concern timeout.
#
# @since 2.7.1
def wtimeout?
@wtimeout
end
end
end
end