/
sdam_error_detection.rb
83 lines (74 loc) · 2.75 KB
/
sdam_error_detection.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
# frozen_string_literal: true
# encoding: utf-8
module Mongo
class Error
# @note Although not_master? and node_recovering? methods of this module
# are part of the public API, the fact that these methods are defined on
# this module and not on the classes which include this module is not
# part of the public API.
#
# @api semipublic
module SdamErrorDetection
# @api private
NOT_MASTER_CODES = [10107, 13435].freeze
# @api private
NODE_RECOVERING_CODES = [11600, 11602, 13436, 189, 91, 10058].freeze
# @api private
NODE_SHUTTING_DOWN_CODES = [11600, 91].freeze
# Whether the error is a "not master" error, or one of its variants.
#
# See https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering.
#
# @return [ true | false ] Whether the error is a not master.
#
# @since 2.8.0
def not_master?
# Require the error to be communicated at the top level of the response
# for it to influence SDAM state. See DRIVERS-1376 / RUBY-2516.
return false if document['ok'] == 1
if node_recovering?
false
elsif code
NOT_MASTER_CODES.include?(code)
elsif message
message.include?('not master')
else
false
end
end
# Whether the error is a "node is recovering" error, or one of its variants.
#
# See https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering.
#
# @return [ true | false ] Whether the error is a node is recovering.
#
# @since 2.8.0
def node_recovering?
# Require the error to be communicated at the top level of the response
# for it to influence SDAM state. See DRIVERS-1376 / RUBY-2516.
return false if document['ok'] == 1
if code
NODE_RECOVERING_CODES.include?(code)
elsif message
message.include?('node is recovering') || message.include?('not master or secondary')
else
false
end
end
# Whether the error is a "node is shutting down" type error.
#
# See https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering.
#
# @return [ true | false ] Whether the error is a node is shutting down.
#
# @since 2.9.0
def node_shutting_down?
if code && NODE_SHUTTING_DOWN_CODES.include?(code)
true
else
false
end
end
end
end
end