mongo 2.13.0.beta1 → 2.13.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -5
  4. data/Rakefile +15 -9
  5. data/lib/mongo.rb +4 -2
  6. data/lib/mongo/auth/aws/request.rb +4 -2
  7. data/lib/mongo/bulk_write.rb +1 -0
  8. data/lib/mongo/client.rb +143 -21
  9. data/lib/mongo/cluster.rb +53 -17
  10. data/lib/mongo/cluster/sdam_flow.rb +13 -10
  11. data/lib/mongo/cluster/topology/replica_set_no_primary.rb +3 -2
  12. data/lib/mongo/cluster/topology/sharded.rb +1 -1
  13. data/lib/mongo/cluster/topology/single.rb +1 -1
  14. data/lib/mongo/collection.rb +17 -13
  15. data/lib/mongo/collection/view/readable.rb +3 -1
  16. data/lib/mongo/collection/view/writable.rb +41 -5
  17. data/lib/mongo/database.rb +31 -4
  18. data/lib/mongo/database/view.rb +19 -4
  19. data/lib/mongo/distinguishing_semaphore.rb +55 -0
  20. data/lib/mongo/error.rb +1 -0
  21. data/lib/mongo/error/invalid_session.rb +2 -1
  22. data/lib/mongo/error/operation_failure.rb +6 -0
  23. data/lib/mongo/error/sessions_not_supported.rb +35 -0
  24. data/lib/mongo/event/base.rb +6 -0
  25. data/lib/mongo/grid/file.rb +5 -0
  26. data/lib/mongo/grid/file/chunk.rb +2 -0
  27. data/lib/mongo/grid/fs_bucket.rb +15 -13
  28. data/lib/mongo/grid/stream/write.rb +9 -3
  29. data/lib/mongo/monitoring.rb +38 -0
  30. data/lib/mongo/monitoring/command_log_subscriber.rb +10 -2
  31. data/lib/mongo/monitoring/event/command_failed.rb +11 -0
  32. data/lib/mongo/monitoring/event/command_started.rb +37 -2
  33. data/lib/mongo/monitoring/event/command_succeeded.rb +11 -0
  34. data/lib/mongo/monitoring/event/server_closed.rb +1 -1
  35. data/lib/mongo/monitoring/event/server_description_changed.rb +27 -4
  36. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +9 -2
  37. data/lib/mongo/monitoring/event/server_heartbeat_started.rb +9 -2
  38. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +9 -2
  39. data/lib/mongo/monitoring/event/server_opening.rb +1 -1
  40. data/lib/mongo/monitoring/event/topology_changed.rb +1 -1
  41. data/lib/mongo/monitoring/event/topology_closed.rb +1 -1
  42. data/lib/mongo/monitoring/event/topology_opening.rb +1 -1
  43. data/lib/mongo/monitoring/publishable.rb +6 -3
  44. data/lib/mongo/monitoring/server_description_changed_log_subscriber.rb +9 -1
  45. data/lib/mongo/monitoring/topology_changed_log_subscriber.rb +1 -1
  46. data/lib/mongo/protocol/message.rb +36 -8
  47. data/lib/mongo/protocol/msg.rb +14 -0
  48. data/lib/mongo/protocol/serializers.rb +5 -2
  49. data/lib/mongo/server.rb +10 -3
  50. data/lib/mongo/server/connection.rb +4 -4
  51. data/lib/mongo/server/connection_base.rb +3 -1
  52. data/lib/mongo/server/description.rb +5 -0
  53. data/lib/mongo/server/monitor.rb +76 -44
  54. data/lib/mongo/server/monitor/connection.rb +55 -7
  55. data/lib/mongo/server/pending_connection.rb +14 -4
  56. data/lib/mongo/server/push_monitor.rb +173 -0
  57. data/{spec/runners/transactions/context.rb → lib/mongo/server/push_monitor/connection.rb} +9 -14
  58. data/lib/mongo/server_selector.rb +0 -1
  59. data/lib/mongo/server_selector/base.rb +579 -1
  60. data/lib/mongo/server_selector/nearest.rb +1 -6
  61. data/lib/mongo/server_selector/primary.rb +1 -6
  62. data/lib/mongo/server_selector/primary_preferred.rb +7 -10
  63. data/lib/mongo/server_selector/secondary.rb +1 -6
  64. data/lib/mongo/server_selector/secondary_preferred.rb +1 -7
  65. data/lib/mongo/session.rb +2 -0
  66. data/lib/mongo/socket.rb +20 -8
  67. data/lib/mongo/socket/ssl.rb +1 -1
  68. data/lib/mongo/socket/tcp.rb +1 -1
  69. data/lib/mongo/topology_version.rb +9 -0
  70. data/lib/mongo/utils.rb +62 -0
  71. data/lib/mongo/version.rb +1 -1
  72. data/spec/README.aws-auth.md +2 -2
  73. data/spec/integration/awaited_ismaster_spec.rb +28 -0
  74. data/spec/integration/change_stream_examples_spec.rb +6 -2
  75. data/spec/integration/check_clean_slate_spec.rb +16 -0
  76. data/spec/integration/client_construction_spec.rb +1 -0
  77. data/spec/integration/connect_single_rs_name_spec.rb +5 -2
  78. data/spec/integration/connection_spec.rb +7 -4
  79. data/spec/integration/crud_spec.rb +4 -4
  80. data/spec/integration/docs_examples_spec.rb +6 -0
  81. data/spec/integration/grid_fs_bucket_spec.rb +48 -0
  82. data/spec/integration/heartbeat_events_spec.rb +4 -23
  83. data/spec/integration/read_concern_spec.rb +1 -1
  84. data/spec/integration/retryable_errors_spec.rb +1 -1
  85. data/spec/integration/retryable_writes/shared/performs_legacy_retries.rb +2 -2
  86. data/spec/integration/retryable_writes/shared/performs_modern_retries.rb +3 -3
  87. data/spec/integration/retryable_writes/shared/performs_no_retries.rb +2 -2
  88. data/spec/integration/sdam_error_handling_spec.rb +37 -15
  89. data/spec/integration/sdam_events_spec.rb +77 -6
  90. data/spec/integration/sdam_prose_spec.rb +64 -0
  91. data/spec/integration/server_monitor_spec.rb +25 -1
  92. data/spec/integration/size_limit_spec.rb +7 -3
  93. data/spec/integration/size_limit_spec.rb~12e1e9c4f... RUBY-2242 Fix zlib compression (#2021) +98 -0
  94. data/spec/integration/ssl_uri_options_spec.rb +2 -2
  95. data/spec/integration/zlib_compression_spec.rb +25 -0
  96. data/spec/lite_spec_helper.rb +12 -5
  97. data/spec/mongo/auth/aws/request_spec.rb +76 -0
  98. data/spec/mongo/auth/scram_spec.rb +1 -1
  99. data/spec/mongo/client_construction_spec.rb +207 -0
  100. data/spec/mongo/client_spec.rb +38 -3
  101. data/spec/mongo/cluster/topology/replica_set_spec.rb +52 -9
  102. data/spec/mongo/cluster/topology/single_spec.rb +4 -2
  103. data/spec/mongo/cluster_spec.rb +34 -35
  104. data/spec/mongo/collection/view/change_stream_resume_spec.rb +6 -6
  105. data/spec/mongo/collection_spec.rb +500 -0
  106. data/spec/mongo/database_spec.rb +245 -8
  107. data/spec/mongo/distinguishing_semaphore_spec.rb +63 -0
  108. data/spec/mongo/error/operation_failure_spec.rb +40 -0
  109. data/spec/mongo/index/view_spec.rb +2 -2
  110. data/spec/mongo/monitoring/event/server_description_changed_spec.rb +1 -4
  111. data/spec/mongo/protocol/msg_spec.rb +10 -0
  112. data/spec/mongo/semaphore_spec.rb +51 -0
  113. data/spec/mongo/server/connection_auth_spec.rb +2 -2
  114. data/spec/mongo/server_selector/nearest_spec.rb +23 -23
  115. data/spec/mongo/server_selector/primary_preferred_spec.rb +26 -26
  116. data/spec/mongo/server_selector/primary_spec.rb +9 -9
  117. data/spec/mongo/server_selector/secondary_preferred_spec.rb +22 -22
  118. data/spec/mongo/server_selector/secondary_spec.rb +18 -18
  119. data/spec/mongo/server_selector_spec.rb +4 -4
  120. data/spec/mongo/session_spec.rb +35 -0
  121. data/spec/runners/change_streams/test.rb +2 -2
  122. data/spec/runners/cmap.rb +1 -1
  123. data/spec/runners/command_monitoring.rb +3 -34
  124. data/spec/runners/crud/context.rb +9 -5
  125. data/spec/runners/crud/operation.rb +59 -27
  126. data/spec/runners/crud/spec.rb +0 -8
  127. data/spec/runners/crud/test.rb +1 -1
  128. data/spec/runners/sdam.rb +2 -2
  129. data/spec/runners/server_selection.rb +242 -28
  130. data/spec/runners/transactions.rb +12 -12
  131. data/spec/runners/transactions/operation.rb +151 -25
  132. data/spec/runners/transactions/test.rb +60 -16
  133. data/spec/spec_tests/command_monitoring_spec.rb +22 -12
  134. data/spec/spec_tests/crud_spec.rb +1 -1
  135. data/spec/spec_tests/data/change_streams/change-streams-errors.yml +4 -8
  136. data/spec/spec_tests/data/change_streams/change-streams-resume-whitelist.yml +66 -0
  137. data/spec/spec_tests/data/max_staleness/ReplicaSetNoPrimary/MaxStalenessTooSmall.yml +15 -0
  138. data/spec/spec_tests/data/max_staleness/ReplicaSetNoPrimary/NoKnownServers.yml +4 -3
  139. data/spec/spec_tests/data/max_staleness/Unknown/SmallMaxStaleness.yml +1 -0
  140. data/spec/spec_tests/data/sdam_integration/cancel-server-check.yml +96 -0
  141. data/spec/spec_tests/data/sdam_integration/connectTimeoutMS.yml +88 -0
  142. data/spec/spec_tests/data/sdam_integration/find-network-error.yml +83 -0
  143. data/spec/spec_tests/data/sdam_integration/find-shutdown-error.yml +116 -0
  144. data/spec/spec_tests/data/sdam_integration/insert-network-error.yml +86 -0
  145. data/spec/spec_tests/data/sdam_integration/insert-shutdown-error.yml +115 -0
  146. data/spec/spec_tests/data/sdam_integration/isMaster-command-error.yml +168 -0
  147. data/spec/spec_tests/data/sdam_integration/isMaster-network-error.yml +162 -0
  148. data/spec/spec_tests/data/sdam_integration/isMaster-timeout.yml +229 -0
  149. data/spec/spec_tests/data/sdam_integration/rediscover-quickly-after-step-down.yml +87 -0
  150. data/spec/spec_tests/max_staleness_spec.rb +4 -142
  151. data/spec/spec_tests/retryable_reads_spec.rb +2 -2
  152. data/spec/spec_tests/sdam_integration_spec.rb +13 -0
  153. data/spec/spec_tests/sdam_monitoring_spec.rb +1 -2
  154. data/spec/spec_tests/server_selection_spec.rb +4 -116
  155. data/spec/stress/cleanup_spec.rb +17 -2
  156. data/spec/stress/connection_pool_stress_spec.rb +10 -8
  157. data/spec/support/child_process_helper.rb +78 -0
  158. data/spec/support/client_registry.rb +1 -0
  159. data/spec/support/cluster_config.rb +4 -0
  160. data/spec/support/event_subscriber.rb +123 -33
  161. data/spec/support/keyword_struct.rb +26 -0
  162. data/spec/support/shared/server_selector.rb +13 -1
  163. data/spec/support/spec_config.rb +38 -13
  164. data/spec/support/spec_organizer.rb +129 -0
  165. data/spec/support/spec_setup.rb +1 -1
  166. data/spec/support/utils.rb +46 -0
  167. metadata +992 -942
  168. metadata.gz.sig +0 -0
  169. data/lib/mongo/server_selector/selectable.rb +0 -560
  170. data/spec/runners/sdam_monitoring.rb +0 -89
@@ -118,6 +118,8 @@ module Mongo
118
118
  options[:connect_timeout] || Server::CONNECT_TIMEOUT
119
119
  end
120
120
 
121
+ attr_reader :server_connection_id
122
+
121
123
  # Sends a message and returns the result.
122
124
  #
123
125
  # @param [ Protocol::Message ] The message to send.
@@ -131,15 +133,43 @@ module Mongo
131
133
  #
132
134
  # @param [ String ] The serialized message to send.
133
135
  #
136
+ # @option opts [ Numeric ] :read_socket_timeout The timeout to use for
137
+ # each read operation.
138
+ #
134
139
  # @return [ Protocol::Message ] The result.
135
- def dispatch_bytes(bytes)
140
+ def dispatch_bytes(bytes, **opts)
141
+ write_bytes(bytes)
142
+ read_response(
143
+ socket_timeout: opts[:read_socket_timeout],
144
+ )
145
+ end
146
+
147
+ def write_bytes(bytes)
136
148
  unless connected?
137
149
  raise ArgumentError, "Trying to dispatch on an unconnected connection #{self}"
138
150
  end
139
151
 
140
- add_server_diagnostics do
141
- socket.write(bytes)
142
- Protocol::Message.deserialize(socket)
152
+ add_server_connection_id do
153
+ add_server_diagnostics do
154
+ socket.write(bytes)
155
+ end
156
+ end
157
+ end
158
+
159
+ # @option opts [ Numeric ] :socket_timeout The timeout to use for
160
+ # each read operation.
161
+ def read_response(**opts)
162
+ unless connected?
163
+ raise ArgumentError, "Trying to read on an unconnected connection #{self}"
164
+ end
165
+
166
+ add_server_connection_id do
167
+ add_server_diagnostics do
168
+ Protocol::Message.deserialize(socket,
169
+ Protocol::Message::MAX_MESSAGE_SIZE,
170
+ nil,
171
+ **opts)
172
+ end
143
173
  end
144
174
  end
145
175
 
@@ -184,7 +214,7 @@ module Mongo
184
214
  # @since 2.0.0
185
215
  def disconnect!(options = nil)
186
216
  if socket
187
- socket.close
217
+ socket.close rescue nil
188
218
  @socket = nil
189
219
  end
190
220
  true
@@ -200,11 +230,29 @@ module Mongo
200
230
  message = dispatch_bytes(payload)
201
231
  reply = message.documents.first
202
232
  set_compressor!(reply)
233
+ @server_connection_id = reply['connectionId']
203
234
  reply
204
- rescue => e
205
- log_warn("Failed to handshake with #{address}: #{e.class}: #{e}:\n#{e.backtrace[0..5].join("\n")}")
235
+ rescue => exc
236
+ msg = "Failed to handshake with #{address}"
237
+ Utils.warn_monitor_exception(msg, exc,
238
+ logger: options[:logger],
239
+ log_prefix: options[:log_prefix],
240
+ bg_error_backtrace: options[:bg_error_backtrace],
241
+ )
206
242
  raise
207
243
  end
244
+
245
+ private
246
+
247
+ def add_server_connection_id
248
+ yield
249
+ rescue Mongo::Error => e
250
+ if server_connection_id
251
+ note = "sconn:#{server_connection_id}"
252
+ e.add_note(note)
253
+ end
254
+ raise e
255
+ end
208
256
  end
209
257
  end
210
258
  end
@@ -116,8 +116,13 @@ module Mongo
116
116
  Protocol::Message.deserialize(socket, Protocol::Message::MAX_MESSAGE_SIZE).documents.first
117
117
  end
118
118
  end
119
- rescue => e
120
- log_warn("Failed to handshake with #{address}: #{e.class}: #{e}:\n#{e.backtrace[0..5].join("\n")}")
119
+ rescue => exc
120
+ msg = "Failed to handshake with #{address}"
121
+ Utils.warn_monitor_exception(msg, exc,
122
+ logger: options[:logger],
123
+ log_prefix: options[:log_prefix],
124
+ bg_error_backtrace: options[:bg_error_backtrace],
125
+ )
121
126
  raise
122
127
  end
123
128
  end
@@ -151,8 +156,13 @@ module Mongo
151
156
  speculative_auth_result: speculative_auth_result,
152
157
  )
153
158
  auth.login
154
- rescue => e
155
- log_warn("Failed to authenticate to #{address}: #{e.class}: #{e}:\n#{e.backtrace[0..5].join("\n")}")
159
+ rescue => exc
160
+ msg = "Failed to authenticate to #{address}"
161
+ Utils.warn_monitor_exception(msg, exc,
162
+ logger: options[:logger],
163
+ log_prefix: options[:log_prefix],
164
+ bg_error_backtrace: options[:bg_error_backtrace],
165
+ )
156
166
  raise
157
167
  end
158
168
  end
@@ -0,0 +1,173 @@
1
+ # Copyright (C) 2020 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
+ class Server
17
+
18
+ # A monitor utilizing server-pushed ismaster requests.
19
+ #
20
+ # When a Monitor handshakes with a 4.4+ server, it creates an instance
21
+ # of PushMonitor. PushMonitor subsequently executes server-pushed ismaster
22
+ # (i.e. awaited & exhausted ismaster) to receive topology changes from the
23
+ # server as quickly as possible. The Monitor still monitors the server
24
+ # for round-trip time calculations and to perform immediate checks as
25
+ # requested by the application.
26
+ #
27
+ # @api private
28
+ class PushMonitor
29
+ extend Forwardable
30
+ include BackgroundThread
31
+
32
+ def initialize(monitor, topology_version, monitoring, **options)
33
+ if topology_version.nil?
34
+ raise ArgumentError, 'Topology version must be provided but it was nil'
35
+ end
36
+ @monitor = monitor
37
+ @topology_version = topology_version
38
+ @monitoring = monitoring
39
+ @options = options
40
+ @lock = Mutex.new
41
+ end
42
+
43
+ # @return [ Monitor ] The monitor to which this push monitor is attached.
44
+ attr_reader :monitor
45
+
46
+ # @return [ TopologyVersion ] Most recently received topology version.
47
+ attr_reader :topology_version
48
+
49
+ # @return [ Monitoring ] monitoring The monitoring.
50
+ attr_reader :monitoring
51
+
52
+ # @return [ Hash ] Push monitor options.
53
+ attr_reader :options
54
+
55
+ # @return [ Server ] The server that is being monitored.
56
+ def_delegator :monitor, :server
57
+
58
+ def start!
59
+ @lock.synchronize do
60
+ super
61
+ end
62
+ end
63
+
64
+ def stop!
65
+ @lock.synchronize do
66
+ @stop_requested = true
67
+ if @connection
68
+ # Interrupt any in-progress exhausted ismaster reads by
69
+ # disconnecting the connection.
70
+ @connection.send(:socket).close
71
+ end
72
+ end
73
+ super.tap do
74
+ @lock.synchronize do
75
+ if @connection
76
+ @connection.disconnect!
77
+ @connection = nil
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def do_work
84
+ @lock.synchronize do
85
+ return if @stop_requested
86
+ end
87
+
88
+ result = monitoring.publish_heartbeat(server, awaited: true) do
89
+ ismaster
90
+ end
91
+ new_description = monitor.run_sdam_flow(result, awaited: true)
92
+ # When ismaster fails due to a fail point, the response does not
93
+ # include topology version. In this case we need to keep our existing
94
+ # topology version so that we can resume monitoring.
95
+ # The spec does not appear to directly address this case but
96
+ # https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-monitoring.rst#streaming-ismaster
97
+ # says that topologyVersion should only be updated from successful
98
+ # ismaster responses.
99
+ if new_description.topology_version
100
+ @topology_version = new_description.topology_version
101
+ end
102
+ rescue Mongo::Error => exc
103
+ msg = "Error running awaited ismaster on #{server.address}"
104
+ Utils.warn_monitor_exception(msg, exc,
105
+ logger: options[:logger],
106
+ log_prefix: options[:log_prefix],
107
+ bg_error_backtrace: options[:bg_error_backtrace],
108
+ )
109
+ end
110
+
111
+ def ismaster
112
+ @lock.synchronize do
113
+ if @connection && @connection.pid != Process.pid
114
+ log_warn("Detected PID change - Mongo client should have been reconnected (old pid #{@connection.pid}, new pid #{Process.pid}")
115
+ @connection.disconnect!
116
+ @connection = nil
117
+ end
118
+ end
119
+
120
+ @lock.synchronize do
121
+ unless @connection
122
+ @server_pushing = false
123
+ connection = PushMonitor::Connection.new(server.address, options)
124
+ connection.connect!
125
+ @connection = connection
126
+ end
127
+ end
128
+
129
+ resp_msg = begin
130
+ unless @server_pushing
131
+ write_ismaster
132
+ end
133
+ read_response
134
+ rescue Mongo::Error
135
+ @lock.synchronize do
136
+ @connection.disconnect!
137
+ @connection = nil
138
+ end
139
+ raise
140
+ end
141
+ @server_pushing = resp_msg.flags.include?(:more_to_come)
142
+ result = resp_msg.documents.first
143
+ end
144
+
145
+ def write_ismaster
146
+ payload = Monitor::Connection::ISMASTER_OP_MSG.merge(
147
+ topologyVersion: topology_version.to_doc,
148
+ maxAwaitTimeMS: monitor.heartbeat_interval * 1000,
149
+ )
150
+
151
+ req_msg = Protocol::Msg.new([:exhaust_allowed], {}, payload)
152
+ @lock.synchronize { @connection }.write_bytes(req_msg.serialize.to_s)
153
+ end
154
+
155
+ def read_response
156
+ if timeout = options[:connect_timeout]
157
+ if timeout < 0
158
+ raise Mongo::SocketTimeoutError, "Requested to read with a negative timeout: #{}"
159
+ elsif timeout > 0
160
+ timeout += options[:heartbeat_frequency] || Monitor::DEFAULT_HEARTBEAT_INTERVAL
161
+ end
162
+ end
163
+ # We set the timeout twice: once passed into read_socket which applies
164
+ # to each individual read operation, and again around the entire read.
165
+ Timeout.timeout(timeout, Error::SocketTimeoutError, "Failed to read an awaited ismaster response in #{timeout} seconds") do
166
+ @lock.synchronize { @connection }.read_response(socket_timeout: timeout)
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ require 'mongo/server/push_monitor/connection'
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2019-2020 MongoDB Inc.
1
+ # Copyright (C) 2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,19 +13,14 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module Mongo
16
- module Transactions
17
- Context = Struct.new(
18
- :session0,
19
- :session1,
20
- :session,
21
- ) do
22
- def transform_arguments(arguments)
23
- arguments.dup.tap do |out|
24
- [:session].each do |key|
25
- if out[key]
26
- out[key] = send(key)
27
- end
28
- end
16
+ class Server
17
+ class PushMonitor
18
+
19
+ # @api private
20
+ class Connection < Server::Monitor::Connection
21
+
22
+ def socket_timeout
23
+ options[:socket_timeout]
29
24
  end
30
25
  end
31
26
  end
@@ -13,7 +13,6 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require 'mongo/server_selector/base'
16
- require 'mongo/server_selector/selectable'
17
16
  require 'mongo/server_selector/nearest'
18
17
  require 'mongo/server_selector/primary'
19
18
  require 'mongo/server_selector/primary_preferred'
@@ -16,8 +16,352 @@ module Mongo
16
16
 
17
17
  module ServerSelector
18
18
 
19
- # @api private
20
19
  class Base
20
+
21
+ # Initialize the server selector.
22
+ #
23
+ # @example Initialize the selector.
24
+ # Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}])
25
+ #
26
+ # @example Initialize the preference with no options.
27
+ # Mongo::ServerSelector::Secondary.new
28
+ #
29
+ # @param [ Hash ] options The server preference options.
30
+ #
31
+ # @option options [ Integer ] :local_threshold The local threshold boundary for
32
+ # nearest selection in seconds.
33
+ # @option options [ Integer ] max_staleness The maximum replication lag,
34
+ # in seconds, that a secondary can suffer and still be eligible for a read.
35
+ # A value of -1 is treated identically to nil, which is to not
36
+ # have a maximum staleness.
37
+ # @option options [ Hash | nil ] hedge A Hash specifying whether to enable hedged
38
+ # reads on the server. Hedged reads are not enabled by default. When
39
+ # specifying this option, it must be in the format: { enabled: true },
40
+ # where the value of the :enabled key is a boolean value.
41
+ #
42
+ # @raise [ Error::InvalidServerPreference ] If tag sets are specified
43
+ # but not allowed.
44
+ #
45
+ # @api private
46
+ def initialize(options = nil)
47
+ options = options ? options.dup : {}
48
+ if options[:max_staleness] == -1
49
+ options.delete(:max_staleness)
50
+ end
51
+ @options = options
52
+ @tag_sets = options[:tag_sets] || []
53
+ @max_staleness = options[:max_staleness]
54
+ @hedge = options[:hedge]
55
+
56
+ validate!
57
+ end
58
+
59
+ # @return [ Hash ] options The options.
60
+ attr_reader :options
61
+
62
+ # @return [ Array ] tag_sets The tag sets used to select servers.
63
+ attr_reader :tag_sets
64
+
65
+ # @return [ Integer ] max_staleness The maximum replication lag, in
66
+ # seconds, that a secondary can suffer and still be eligible for a read.
67
+ #
68
+ # @since 2.4.0
69
+ attr_reader :max_staleness
70
+
71
+ # @return [ Hash | nil ] hedge The document specifying whether to enable
72
+ # hedged reads.
73
+ attr_reader :hedge
74
+
75
+ # Get the timeout for server selection.
76
+ #
77
+ # @example Get the server selection timeout, in seconds.
78
+ # selector.server_selection_timeout
79
+ #
80
+ # @return [ Float ] The timeout.
81
+ #
82
+ # @since 2.0.0
83
+ #
84
+ # @deprecated This setting is now taken from the cluster options when
85
+ # a server is selected. Will be removed in version 3.0.
86
+ def server_selection_timeout
87
+ @server_selection_timeout ||=
88
+ (options[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT)
89
+ end
90
+
91
+ # Get the local threshold boundary for nearest selection in seconds.
92
+ #
93
+ # @example Get the local threshold.
94
+ # selector.local_threshold
95
+ #
96
+ # @return [ Float ] The local threshold.
97
+ #
98
+ # @since 2.0.0
99
+ #
100
+ # @deprecated This setting is now taken from the cluster options when
101
+ # a server is selected. Will be removed in version 3.0.
102
+ def local_threshold
103
+ @local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD)
104
+ end
105
+
106
+ # @api private
107
+ def local_threshold_with_cluster(cluster)
108
+ options[:local_threshold] || cluster.options[:local_threshold] || LOCAL_THRESHOLD
109
+ end
110
+
111
+ # Inspect the server selector.
112
+ #
113
+ # @example Inspect the server selector.
114
+ # selector.inspect
115
+ #
116
+ # @return [ String ] The inspection.
117
+ #
118
+ # @since 2.2.0
119
+ def inspect
120
+ "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} max_staleness=#{max_staleness.inspect} hedge=#{hedge}>"
121
+ end
122
+
123
+ # Check equality of two server selectors.
124
+ #
125
+ # @example Check server selector equality.
126
+ # preference == other
127
+ #
128
+ # @param [ Object ] other The other preference.
129
+ #
130
+ # @return [ true, false ] Whether the objects are equal.
131
+ #
132
+ # @since 2.0.0
133
+ def ==(other)
134
+ name == other.name && hedge == other.hedge &&
135
+ max_staleness == other.max_staleness && tag_sets == other.tag_sets
136
+ end
137
+
138
+ # Select a server from the specified cluster, taking into account
139
+ # mongos pinning for the specified session.
140
+ #
141
+ # If the session is given and has a pinned server, this server is the
142
+ # only server considered for selection. If the server is of type mongos,
143
+ # it is returned immediately; otherwise monitoring checks on this
144
+ # server are initiated to update its status, and if the server becomes
145
+ # a mongos within the server selection timeout, it is returned.
146
+ #
147
+ # If no session is given or the session does not have a pinned server,
148
+ # normal server selection process is performed among all servers in the
149
+ # specified cluster matching the preference of this server selector
150
+ # object. Monitoring checks are initiated on servers in the cluster until
151
+ # a suitable server is found, up to the server selection timeout.
152
+ #
153
+ # If a suitable server is not found within the server selection timeout,
154
+ # this method raises Error::NoServerAvailable.
155
+ #
156
+ # @param [ Mongo::Cluster ] cluster The cluster from which to select
157
+ # an eligible server.
158
+ # @param [ true, false ] ping Whether to ping the server before selection.
159
+ # Deprecated and ignored.
160
+ # @param [ Session | nil ] session Optional session to take into account
161
+ # for mongos pinning. Added in version 2.10.0.
162
+ #
163
+ # @return [ Mongo::Server ] A server matching the server preference.
164
+ #
165
+ # @raise [ Error::NoServerAvailable ] No server was found matching the
166
+ # specified preference / pinning requirement in the server selection
167
+ # timeout.
168
+ # @raise [ Error::LintError ] An unexpected condition was detected, and
169
+ # lint mode is enabled.
170
+ #
171
+ # @since 2.0.0
172
+ def select_server(cluster, ping = nil, session = nil)
173
+ server_selection_timeout = cluster.options[:server_selection_timeout] || SERVER_SELECTION_TIMEOUT
174
+
175
+ # Special handling for zero timeout: if we have to select a server,
176
+ # and the timeout is zero, fail immediately (since server selection
177
+ # will take some non-zero amount of time in any case).
178
+ if server_selection_timeout == 0
179
+ msg = "Failing server selection due to zero timeout. " +
180
+ " Requested #{name} in cluster: #{cluster.summary}"
181
+ raise Error::NoServerAvailable.new(self, cluster, msg)
182
+ end
183
+
184
+ deadline = Time.now + server_selection_timeout
185
+
186
+ if session && session.pinned_server
187
+ if Mongo::Lint.enabled?
188
+ unless cluster.sharded?
189
+ raise Error::LintError, "Session has a pinned server in a non-sharded topology: #{topology}"
190
+ end
191
+ end
192
+
193
+ if !session.in_transaction?
194
+ session.unpin
195
+ end
196
+
197
+ if server = session.pinned_server
198
+ # Here we assume that a mongos stays in the topology indefinitely.
199
+ # This will no longer be the case once SRV polling is implemented.
200
+
201
+ unless server.mongos?
202
+ while (time_remaining = deadline - Time.now) > 0
203
+ wait_for_server_selection(cluster, time_remaining)
204
+ end
205
+
206
+ unless server.mongos?
207
+ msg = "The session being used is pinned to the server which is not a mongos: #{server.summary} " +
208
+ "(after #{server_selection_timeout} seconds)"
209
+ raise Error::NoServerAvailable.new(self, cluster, msg)
210
+ end
211
+ end
212
+
213
+ return server
214
+ end
215
+ end
216
+
217
+ if cluster.replica_set?
218
+ validate_max_staleness_value_early!
219
+ end
220
+
221
+ if cluster.addresses.empty?
222
+ if Lint.enabled?
223
+ unless cluster.servers.empty?
224
+ raise Error::LintError, "Cluster has no addresses but has servers: #{cluster.servers.map(&:inspect).join(', ')}"
225
+ end
226
+ end
227
+ msg = "Cluster has no addresses, and therefore will never have a server"
228
+ raise Error::NoServerAvailable.new(self, cluster, msg)
229
+ end
230
+
231
+ =begin Add this check in version 3.0.0
232
+ unless cluster.connected?
233
+ msg = 'Cluster is disconnected'
234
+ raise Error::NoServerAvailable.new(self, cluster, msg)
235
+ end
236
+ =end
237
+
238
+ loop do
239
+ server = try_select_server(cluster)
240
+
241
+ if server
242
+ unless cluster.topology.compatible?
243
+ raise Error::UnsupportedFeatures, cluster.topology.compatibility_error.to_s
244
+ end
245
+
246
+ if session && session.starting_transaction? && cluster.sharded?
247
+ session.pin(server)
248
+ end
249
+
250
+ return server
251
+ end
252
+
253
+ cluster.scan!(false)
254
+
255
+ time_remaining = deadline - Time.now
256
+ if time_remaining > 0
257
+ wait_for_server_selection(cluster, time_remaining)
258
+
259
+ # If we wait for server selection, perform another round of
260
+ # attempting to locate a suitable server. Otherwise server selection
261
+ # can raise NoServerAvailable message when the diagnostics
262
+ # reports an available server of the requested type.
263
+ else
264
+ break
265
+ end
266
+ end
267
+
268
+ msg = "No #{name} server is available in cluster: #{cluster.summary} " +
269
+ "with timeout=#{server_selection_timeout}, " +
270
+ "LT=#{local_threshold_with_cluster(cluster)}"
271
+ msg += server_selection_diagnostic_message(cluster)
272
+ raise Error::NoServerAvailable.new(self, cluster, msg)
273
+ rescue Error::NoServerAvailable => e
274
+ if session && session.in_transaction? && !session.committing_transaction?
275
+ e.add_label('TransientTransactionError')
276
+ end
277
+ if session && session.committing_transaction?
278
+ e.add_label('UnknownTransactionCommitResult')
279
+ end
280
+ raise e
281
+ end
282
+
283
+ # Tries to find a suitable server, returns the server if one is available
284
+ # or nil if there isn't a suitable server.
285
+ #
286
+ # @return [ Server | nil ] A suitable server, if one exists.
287
+ #
288
+ # @api private
289
+ def try_select_server(cluster)
290
+ servers = suitable_servers(cluster)
291
+
292
+ # This list of servers may be ordered in a specific way
293
+ # by the selector (e.g. for secondary preferred, the first
294
+ # server may be a secondary and the second server may be primary)
295
+ # and we should take the first server here respecting the order
296
+ server = servers.first
297
+
298
+ if server
299
+ if Lint.enabled?
300
+ # It is possible for a server to have a nil average RTT here
301
+ # because the ARTT comes from description which may be updated
302
+ # by a background thread while server selection is running.
303
+ # Currently lint mode is not a public feature, if/when this
304
+ # changes (https://jira.mongodb.org/browse/RUBY-1576) the
305
+ # requirement for ARTT to be not nil would need to be removed.
306
+ if server.average_round_trip_time.nil?
307
+ raise Error::LintError, "Server #{server.address} has nil average rtt"
308
+ end
309
+ end
310
+ end
311
+
312
+ server
313
+ end
314
+
315
+ # Returns servers of acceptable types from the cluster.
316
+ #
317
+ # Does not perform staleness validation, staleness filtering or
318
+ # latency filtering.
319
+ #
320
+ # @param [ Cluster ] cluster The cluster.
321
+ #
322
+ # @return [ Array<Server> ] The candidate servers.
323
+ #
324
+ # @api private
325
+ def candidates(cluster)
326
+ servers = cluster.servers
327
+ servers.each do |server|
328
+ validate_max_staleness_support!(server)
329
+ end
330
+ if cluster.single?
331
+ servers
332
+ elsif cluster.sharded?
333
+ servers
334
+ elsif cluster.replica_set?
335
+ select_in_replica_set(servers)
336
+ else
337
+ # Unknown cluster - no servers
338
+ []
339
+ end
340
+ end
341
+
342
+ # Returns servers satisfying the server selector from the cluster.
343
+ #
344
+ # @param [ Cluster ] cluster The cluster.
345
+ #
346
+ # @return [ Array<Server> ] The suitable servers.
347
+ #
348
+ # @api private
349
+ def suitable_servers(cluster)
350
+ if cluster.single?
351
+ candidates(cluster)
352
+ elsif cluster.sharded?
353
+ local_threshold = local_threshold_with_cluster(cluster)
354
+ servers = candidates(cluster)
355
+ near_servers(servers, local_threshold)
356
+ elsif cluster.replica_set?
357
+ validate_max_staleness_value!(cluster)
358
+ candidates(cluster)
359
+ else
360
+ # Unknown cluster - no servers
361
+ []
362
+ end
363
+ end
364
+
21
365
  private
22
366
 
23
367
  # Convert this server preference definition into a format appropriate
@@ -35,6 +379,240 @@ module Mongo
35
379
  preference
36
380
  end
37
381
  end
382
+
383
+ # Select the primary from a list of provided candidates.
384
+ #
385
+ # @param [ Array ] candidates List of candidate servers to select the
386
+ # primary from.
387
+ #
388
+ # @return [ Array ] The primary.
389
+ #
390
+ # @since 2.0.0
391
+ def primary(candidates)
392
+ candidates.select do |server|
393
+ server.primary?
394
+ end
395
+ end
396
+
397
+ # Select the secondaries from a list of provided candidates.
398
+ #
399
+ # @param [ Array ] candidates List of candidate servers to select the
400
+ # secondaries from.
401
+ #
402
+ # @return [ Array ] The secondary servers.
403
+ #
404
+ # @since 2.0.0
405
+ def secondaries(candidates)
406
+ matching_servers = candidates.select(&:secondary?)
407
+ matching_servers = filter_stale_servers(matching_servers, primary(candidates).first)
408
+ matching_servers = match_tag_sets(matching_servers) unless tag_sets.empty?
409
+ # Per server selection spec the server selected MUST be a random
410
+ # one matching staleness and latency requirements.
411
+ # Selectors always pass the output of #secondaries to #nearest
412
+ # which shuffles the server list, fulfilling this requirement.
413
+ matching_servers
414
+ end
415
+
416
+ # Select the near servers from a list of provided candidates, taking the
417
+ # local threshold into account.
418
+ #
419
+ # @param [ Array ] candidates List of candidate servers to select the
420
+ # near servers from.
421
+ # @param [ Integer ] local_threshold Local threshold. This parameter
422
+ # will be required in driver version 3.0.
423
+ #
424
+ # @return [ Array ] The near servers.
425
+ #
426
+ # @since 2.0.0
427
+ def near_servers(candidates = [], local_threshold = nil)
428
+ return candidates if candidates.empty?
429
+
430
+ # Average RTT on any server may change at any time by the server
431
+ # monitor's background thread. ARTT may also become nil if the
432
+ # server is marked unknown. Take a snapshot of ARTTs for the duration
433
+ # of this method.
434
+
435
+ candidates = candidates.map do |server|
436
+ {server: server, artt: server.average_round_trip_time}
437
+ end.reject do |candidate|
438
+ candidate[:artt].nil?
439
+ end
440
+
441
+ return candidates if candidates.empty?
442
+
443
+ nearest_candidate = candidates.min_by do |candidate|
444
+ candidate[:artt]
445
+ end
446
+
447
+ # Default for legacy signarure
448
+ local_threshold ||= self.local_threshold
449
+
450
+ threshold = nearest_candidate[:artt] + local_threshold
451
+
452
+ candidates.select do |candidate|
453
+ candidate[:artt] <= threshold
454
+ end.map do |candidate|
455
+ candidate[:server]
456
+ end.shuffle!
457
+ end
458
+
459
+ # Select the servers matching the defined tag sets.
460
+ #
461
+ # @param [ Array ] candidates List of candidate servers from which those
462
+ # matching the defined tag sets should be selected.
463
+ #
464
+ # @return [ Array ] The servers matching the defined tag sets.
465
+ #
466
+ # @since 2.0.0
467
+ def match_tag_sets(candidates)
468
+ matches = []
469
+ tag_sets.find do |tag_set|
470
+ matches = candidates.select { |server| server.matches_tag_set?(tag_set) }
471
+ !matches.empty?
472
+ end
473
+ matches || []
474
+ end
475
+
476
+ def filter_stale_servers(candidates, primary = nil)
477
+ return candidates unless @max_staleness
478
+
479
+ # last_scan is filled out by the Monitor, and can be nil if a server
480
+ # had its description manually set rather than being normally updated
481
+ # via the SDAM flow. We don't handle the possibility of a nil
482
+ # last_scan here.
483
+ if primary
484
+ candidates.select do |server|
485
+ validate_max_staleness_support!(server)
486
+ staleness = (server.last_scan - server.last_write_date) -
487
+ (primary.last_scan - primary.last_write_date) +
488
+ server.cluster.heartbeat_interval
489
+ staleness <= @max_staleness
490
+ end
491
+ else
492
+ max_write_date = candidates.collect(&:last_write_date).max
493
+ candidates.select do |server|
494
+ validate_max_staleness_support!(server)
495
+ staleness = max_write_date - server.last_write_date + server.cluster.heartbeat_interval
496
+ staleness <= @max_staleness
497
+ end
498
+ end
499
+ end
500
+
501
+ def validate!
502
+ if !@tag_sets.all? { |set| set.empty? } && !tags_allowed?
503
+ raise Error::InvalidServerPreference.new(Error::InvalidServerPreference::NO_TAG_SUPPORT)
504
+ elsif @max_staleness && !max_staleness_allowed?
505
+ raise Error::InvalidServerPreference.new(Error::InvalidServerPreference::NO_MAX_STALENESS_SUPPORT)
506
+ end
507
+
508
+ if @hedge
509
+ unless hedge_allowed?
510
+ raise Error::InvalidServerPreference.new(Error::InvalidServerPreference::NO_HEDGE_SUPPORT)
511
+ end
512
+
513
+ unless @hedge.is_a?(Hash) && @hedge.key?(:enabled) &&
514
+ [true, false].include?(@hedge[:enabled])
515
+ raise Error::InvalidServerPreference.new(
516
+ "`hedge` value (#{hedge}) is invalid - hedge must be a Hash in the " \
517
+ "format { enabled: true }"
518
+ )
519
+ end
520
+ end
521
+ end
522
+
523
+ def validate_max_staleness_support!(server)
524
+ if @max_staleness && !server.features.max_staleness_enabled?
525
+ raise Error::InvalidServerPreference.new(Error::InvalidServerPreference::NO_MAX_STALENESS_WITH_LEGACY_SERVER)
526
+ end
527
+ end
528
+
529
+ def validate_max_staleness_value_early!
530
+ if @max_staleness
531
+ unless @max_staleness >= SMALLEST_MAX_STALENESS_SECONDS
532
+ msg = "`max_staleness` value (#{@max_staleness}) is too small - it must be at least " +
533
+ "`Mongo::ServerSelector::SMALLEST_MAX_STALENESS_SECONDS` (#{ServerSelector::SMALLEST_MAX_STALENESS_SECONDS})"
534
+ raise Error::InvalidServerPreference.new(msg)
535
+ end
536
+ end
537
+ end
538
+
539
+ def validate_max_staleness_value!(cluster)
540
+ if @max_staleness
541
+ heartbeat_interval = cluster.heartbeat_interval
542
+ unless @max_staleness >= [
543
+ SMALLEST_MAX_STALENESS_SECONDS,
544
+ min_cluster_staleness = heartbeat_interval + Cluster::IDLE_WRITE_PERIOD_SECONDS,
545
+ ].max
546
+ msg = "`max_staleness` value (#{@max_staleness}) is too small - it must be at least " +
547
+ "`Mongo::ServerSelector::SMALLEST_MAX_STALENESS_SECONDS` (#{ServerSelector::SMALLEST_MAX_STALENESS_SECONDS}) and (the cluster's heartbeat_frequency " +
548
+ "setting + `Mongo::Cluster::IDLE_WRITE_PERIOD_SECONDS`) (#{min_cluster_staleness})"
549
+ raise Error::InvalidServerPreference.new(msg)
550
+ end
551
+ end
552
+ end
553
+
554
+ # Waits for server state changes in the specified cluster.
555
+ #
556
+ # If the cluster has a server selection semaphore, waits on that
557
+ # semaphore up to the specified remaining time. Any change in server
558
+ # state resulting from SDAM will immediately wake up this method and
559
+ # cause it to return.
560
+ #
561
+ # If the cluster des not have a server selection semaphore, waits
562
+ # the smaller of 0.25 seconds and the specified remaining time.
563
+ # This functionality is provided for backwards compatibilty only for
564
+ # applications directly invoking the server selection process.
565
+ # If lint mode is enabled and the cluster does not have a server
566
+ # selection semaphore, Error::LintError will be raised.
567
+ #
568
+ # @param [ Cluster ] cluster The cluster to wait for.
569
+ # @param [ Numeric ] time_remaining Maximum time to wait, in seconds.
570
+ def wait_for_server_selection(cluster, time_remaining)
571
+ if cluster.server_selection_semaphore
572
+ # Since the semaphore may have been signaled between us checking
573
+ # the servers list earlier and the wait call below, we should not
574
+ # wait for the full remaining time - wait for up to 1 second, then
575
+ # recheck the state.
576
+ cluster.server_selection_semaphore.wait([time_remaining, 1].min)
577
+ else
578
+ if Lint.enabled?
579
+ raise Error::LintError, 'Waiting for server selection without having a server selection semaphore'
580
+ end
581
+ sleep [time_remaining, 0.25].min
582
+ end
583
+ end
584
+
585
+ # Creates a diagnostic message when server selection fails.
586
+ #
587
+ # The diagnostic message includes the following information, as applicable:
588
+ #
589
+ # - Servers having dead monitor threads
590
+ # - Cluster is disconnected
591
+ #
592
+ # If none of the conditions for diagnostic messages apply, an empty string
593
+ # is returned.
594
+ #
595
+ # @param [ Cluster ] cluster The cluster on which server selection was
596
+ # performed.
597
+ #
598
+ # @return [ String ] The diagnostic message.
599
+ def server_selection_diagnostic_message(cluster)
600
+ msg = ''
601
+ dead_monitors = []
602
+ cluster.servers_list.each do |server|
603
+ thread = server.monitor.instance_variable_get('@thread')
604
+ if thread.nil? || !thread.alive?
605
+ dead_monitors << server
606
+ end
607
+ end
608
+ if dead_monitors.any?
609
+ msg += ". The following servers have dead monitor threads: #{dead_monitors.map(&:summary).join(', ')}"
610
+ end
611
+ unless cluster.connected?
612
+ msg += ". The cluster is disconnected (client may have been closed)"
613
+ end
614
+ msg
615
+ end
38
616
  end
39
617
  end
40
618
  end