mongo 1.10.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE +190 -0
- data/README.md +149 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/bin/mongo_console +43 -0
- data/ext/jsasl/target/jsasl.jar +0 -0
- data/lib/mongo.rb +90 -0
- data/lib/mongo/bulk_write_collection_view.rb +380 -0
- data/lib/mongo/collection.rb +1164 -0
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/connection/node.rb +239 -0
- data/lib/mongo/connection/pool.rb +347 -0
- data/lib/mongo/connection/pool_manager.rb +325 -0
- data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/cursor.rb +719 -0
- data/lib/mongo/db.rb +735 -0
- data/lib/mongo/exception.rb +88 -0
- data/lib/mongo/functional.rb +21 -0
- data/lib/mongo/functional/authentication.rb +318 -0
- data/lib/mongo/functional/logging.rb +85 -0
- data/lib/mongo/functional/read_preference.rb +174 -0
- data/lib/mongo/functional/sasl_java.rb +48 -0
- data/lib/mongo/functional/uri_parser.rb +374 -0
- data/lib/mongo/functional/write_concern.rb +66 -0
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/gridfs/grid.rb +112 -0
- data/lib/mongo/gridfs/grid_ext.rb +53 -0
- data/lib/mongo/gridfs/grid_file_system.rb +163 -0
- data/lib/mongo/gridfs/grid_io.rb +484 -0
- data/lib/mongo/legacy.rb +140 -0
- data/lib/mongo/mongo_client.rb +702 -0
- data/lib/mongo/mongo_replica_set_client.rb +523 -0
- data/lib/mongo/mongo_sharded_client.rb +159 -0
- data/lib/mongo/networking.rb +370 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo/utils/conversions.rb +110 -0
- data/lib/mongo/utils/core_ext.rb +70 -0
- data/lib/mongo/utils/server_version.rb +69 -0
- data/lib/mongo/utils/support.rb +80 -0
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/mongo.gemspec +36 -0
- data/test/functional/authentication_test.rb +35 -0
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1129 -0
- data/test/functional/client_test.rb +565 -0
- data/test/functional/collection_test.rb +2073 -0
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +163 -0
- data/test/functional/cursor_fail_test.rb +63 -0
- data/test/functional/cursor_message_test.rb +57 -0
- data/test/functional/cursor_test.rb +625 -0
- data/test/functional/db_api_test.rb +819 -0
- data/test/functional/db_connection_test.rb +27 -0
- data/test/functional/db_test.rb +344 -0
- data/test/functional/grid_file_system_test.rb +285 -0
- data/test/functional/grid_io_test.rb +252 -0
- data/test/functional/grid_test.rb +273 -0
- data/test/functional/pool_test.rb +62 -0
- data/test/functional/safe_test.rb +98 -0
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +62 -0
- data/test/functional/timeout_test.rb +58 -0
- data/test/functional/uri_test.rb +330 -0
- data/test/functional/write_concern_test.rb +118 -0
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +317 -0
- data/test/replica_set/authentication_test.rb +35 -0
- data/test/replica_set/basic_test.rb +174 -0
- data/test/replica_set/client_test.rb +341 -0
- data/test/replica_set/complex_connect_test.rb +77 -0
- data/test/replica_set/connection_test.rb +138 -0
- data/test/replica_set/count_test.rb +64 -0
- data/test/replica_set/cursor_test.rb +212 -0
- data/test/replica_set/insert_test.rb +140 -0
- data/test/replica_set/max_values_test.rb +145 -0
- data/test/replica_set/pinning_test.rb +55 -0
- data/test/replica_set/query_test.rb +73 -0
- data/test/replica_set/read_preference_test.rb +214 -0
- data/test/replica_set/refresh_test.rb +175 -0
- data/test/replica_set/replication_ack_test.rb +94 -0
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +197 -0
- data/test/shared/authentication/basic_auth_shared.rb +286 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
- data/test/shared/authentication/gssapi_shared.rb +164 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +56 -0
- data/test/threading/basic_test.rb +120 -0
- data/test/tools/mongo_config.rb +608 -0
- data/test/tools/mongo_config_test.rb +160 -0
- data/test/unit/client_test.rb +347 -0
- data/test/unit/collection_test.rb +166 -0
- data/test/unit/connection_test.rb +325 -0
- data/test/unit/cursor_test.rb +299 -0
- data/test/unit/db_test.rb +136 -0
- data/test/unit/grid_test.rb +76 -0
- data/test/unit/mongo_sharded_client_test.rb +48 -0
- data/test/unit/node_test.rb +93 -0
- data/test/unit/pool_manager_test.rb +142 -0
- data/test/unit/read_pref_test.rb +115 -0
- data/test/unit/read_test.rb +159 -0
- data/test/unit/safe_test.rb +158 -0
- data/test/unit/sharding_pool_manager_test.rb +84 -0
- data/test/unit/write_concern_test.rb +175 -0
- metadata +260 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,523 @@
|
|
1
|
+
# Copyright (C) 2009-2013 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
|
+
|
17
|
+
# Instantiates and manages connections to a MongoDB replica set.
|
18
|
+
class MongoReplicaSetClient < MongoClient
|
19
|
+
include ReadPreference
|
20
|
+
include ThreadLocalVariableManager
|
21
|
+
|
22
|
+
REPL_SET_OPTS = [
|
23
|
+
:refresh_mode,
|
24
|
+
:refresh_interval,
|
25
|
+
:read_secondary,
|
26
|
+
:rs_name,
|
27
|
+
:name
|
28
|
+
]
|
29
|
+
|
30
|
+
attr_reader :replica_set_name,
|
31
|
+
:seeds,
|
32
|
+
:refresh_interval,
|
33
|
+
:refresh_mode,
|
34
|
+
:refresh_version,
|
35
|
+
:manager
|
36
|
+
|
37
|
+
# Create a connection to a MongoDB replica set.
|
38
|
+
#
|
39
|
+
# If no args are provided, it will check <code>ENV["MONGODB_URI"]</code>.
|
40
|
+
#
|
41
|
+
# Once connected to a replica set, you can find out which nodes are primary, secondary, and
|
42
|
+
# arbiters with the corresponding accessors: MongoClient#primary, MongoClient#secondaries, and
|
43
|
+
# MongoClient#arbiters. This is useful if your application needs to connect manually to nodes other
|
44
|
+
# than the primary.
|
45
|
+
#
|
46
|
+
# @overload initialize(seeds=ENV["MONGODB_URI"], opts={})
|
47
|
+
# @param [Array<String>, Array<Array(String, Integer)>] seeds
|
48
|
+
#
|
49
|
+
# @option opts [String, Integer, Symbol] :w (1) Set default number of nodes to which a write
|
50
|
+
# should be acknowledged.
|
51
|
+
# @option opts [Integer] :wtimeout (nil) Set replica set acknowledgement timeout.
|
52
|
+
# @option opts [Boolean] :j (false) If true, block until write operations have been committed
|
53
|
+
# to the journal. Cannot be used in combination with 'fsync'. Prior to MongoDB 2.6 this option was
|
54
|
+
# ignored if the server was running without journaling. Starting with MongoDB 2.6, write operations will
|
55
|
+
# fail with an exception if this option is used when the server is running without journaling.
|
56
|
+
# @option opts [Boolean] :fsync (false) If true, and the server is running without journaling, blocks until
|
57
|
+
# the server has synced all data files to disk. If the server is running with journaling, this acts the same as
|
58
|
+
# the 'j' option, blocking until write operations have been committed to the journal.
|
59
|
+
# Cannot be used in combination with 'j'.
|
60
|
+
#
|
61
|
+
# Notes about write concern options:
|
62
|
+
# Write concern options are propagated to objects instantiated from this MongoReplicaSetClient.
|
63
|
+
# These defaults can be overridden upon instantiation of any object by explicitly setting an options hash
|
64
|
+
# on initialization.
|
65
|
+
# @option opts [:primary, :primary_preferred, :secondary, :secondary_preferred, :nearest] :read (:primary)
|
66
|
+
# A "read preference" determines the candidate replica set members to which a query or command can be sent.
|
67
|
+
# [:primary]
|
68
|
+
# * Read from primary only.
|
69
|
+
# * Cannot be combined with tags.
|
70
|
+
# [:primary_preferred]
|
71
|
+
# * Read from primary if available, otherwise read from a secondary.
|
72
|
+
# [:secondary]
|
73
|
+
# * Read from secondary if available.
|
74
|
+
# [:secondary_preferred]
|
75
|
+
# * Read from a secondary if available, otherwise read from the primary.
|
76
|
+
# [:nearest]
|
77
|
+
# * Read from any member.
|
78
|
+
# @option opts [Array<Hash{ String, Symbol => Tag Value }>] :tag_sets ([])
|
79
|
+
# Read from replica-set members with these tags.
|
80
|
+
# @option opts [Integer] :secondary_acceptable_latency_ms (15) The acceptable
|
81
|
+
# nearest available member for a member to be considered "near".
|
82
|
+
# @option opts [Logger] :logger (nil) Logger instance to receive driver operation log.
|
83
|
+
# @option opts [Integer] :pool_size (1) The maximum number of socket connections allowed per
|
84
|
+
# connection pool. Note: this setting is relevant only for multi-threaded applications.
|
85
|
+
# @option opts [Float] :pool_timeout (5.0) When all of the connections a pool are checked out,
|
86
|
+
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
|
87
|
+
# Note: this setting is relevant only for multi-threaded applications.
|
88
|
+
# @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
|
89
|
+
# @option opts [Float] :connect_timeout (30) The number of seconds to wait before timing out a
|
90
|
+
# connection attempt.
|
91
|
+
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
|
92
|
+
# @option opts [String] :ssl_cert (nil) The certificate file used to identify the local connection against MongoDB.
|
93
|
+
# @option opts [String] :ssl_key (nil) The private keyfile used to identify the local connection against MongoDB.
|
94
|
+
# Note that even if the key is stored in the same file as the certificate, both need to be explicitly specified.
|
95
|
+
# @option opts [String] :ssl_key_pass_phrase (nil) A passphrase for the private key.
|
96
|
+
# @option opts [Boolean] :ssl_verify (nil) Specifies whether or not peer certification validation should occur.
|
97
|
+
# @option opts [String] :ssl_ca_cert (nil) The ca_certs file contains a set of concatenated "certification authority"
|
98
|
+
# certificates, which are used to validate certificates passed from the other end of the connection.
|
99
|
+
# Required for :ssl_verify.
|
100
|
+
# @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the
|
101
|
+
# state of the connection every :refresh_interval seconds. Replica set connection failures
|
102
|
+
# will always trigger a complete refresh. This option is useful when you want to add new nodes
|
103
|
+
# or remove replica set nodes not currently in use by the driver.
|
104
|
+
# @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds
|
105
|
+
# between calls to check the replica set's state.
|
106
|
+
# @note the number of seed nodes does not have to be equal to the number of replica set members.
|
107
|
+
# The purpose of seed nodes is to permit the driver to find at least one replica set member even if a member is down.
|
108
|
+
#
|
109
|
+
# @example Connect to a replica set and provide two seed nodes.
|
110
|
+
# MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'])
|
111
|
+
#
|
112
|
+
# @example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named 'prod':
|
113
|
+
# MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'], :name => 'prod')
|
114
|
+
#
|
115
|
+
# @example Connect to a replica set providing two seed nodes and allowing reads from a secondary node:
|
116
|
+
# MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'], :read => :secondary)
|
117
|
+
#
|
118
|
+
# @see http://api.mongodb.org/ruby/current/file.REPLICA_SETS.html Replica sets in Ruby
|
119
|
+
#
|
120
|
+
# @raise [MongoArgumentError] This is raised for usage errors.
|
121
|
+
#
|
122
|
+
# @raise [ConnectionFailure] This is raised for the various connection failures.
|
123
|
+
def initialize(*args)
|
124
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
125
|
+
nodes = args.shift || []
|
126
|
+
|
127
|
+
raise MongoArgumentError, "Too many arguments" unless args.empty?
|
128
|
+
|
129
|
+
# This is temporary until support for the old format is dropped
|
130
|
+
@seeds = nodes.collect do |node|
|
131
|
+
if node.is_a?(Array)
|
132
|
+
warn "Initiating a MongoReplicaSetClient with seeds passed as individual [host, port] array arguments is deprecated."
|
133
|
+
warn "Please specify hosts as an array of 'host:port' strings; the old format will be removed in v2.0"
|
134
|
+
node
|
135
|
+
elsif node.is_a?(String)
|
136
|
+
Support.normalize_seeds(node)
|
137
|
+
else
|
138
|
+
raise MongoArgumentError "Bad seed format!"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
if @seeds.empty? && ENV.has_key?('MONGODB_URI')
|
143
|
+
parser = URIParser.new ENV['MONGODB_URI']
|
144
|
+
if parser.direct?
|
145
|
+
raise MongoArgumentError,
|
146
|
+
"ENV['MONGODB_URI'] implies a direct connection."
|
147
|
+
end
|
148
|
+
opts = parser.connection_options.merge! opts
|
149
|
+
@seeds = parser.nodes
|
150
|
+
end
|
151
|
+
|
152
|
+
if @seeds.length.zero?
|
153
|
+
raise MongoArgumentError, "A MongoReplicaSetClient requires at least one seed node."
|
154
|
+
end
|
155
|
+
|
156
|
+
@seeds.freeze
|
157
|
+
|
158
|
+
# Refresh
|
159
|
+
@last_refresh = Time.now
|
160
|
+
@refresh_version = 0
|
161
|
+
|
162
|
+
# No connection manager by default.
|
163
|
+
@manager = nil
|
164
|
+
|
165
|
+
# Lock for request ids.
|
166
|
+
@id_lock = Mutex.new
|
167
|
+
|
168
|
+
@connected = false
|
169
|
+
|
170
|
+
@connect_mutex = Mutex.new
|
171
|
+
|
172
|
+
@mongos = false
|
173
|
+
|
174
|
+
check_opts(opts)
|
175
|
+
setup(opts.dup)
|
176
|
+
end
|
177
|
+
|
178
|
+
def valid_opts
|
179
|
+
super + REPL_SET_OPTS - CLIENT_ONLY_OPTS
|
180
|
+
end
|
181
|
+
|
182
|
+
def inspect
|
183
|
+
"<Mongo::MongoReplicaSetClient:0x#{self.object_id.to_s(16)} @seeds=#{@seeds.inspect} " +
|
184
|
+
"@connected=#{@connected}>"
|
185
|
+
end
|
186
|
+
|
187
|
+
# Initiate a connection to the replica set.
|
188
|
+
def connect(force = !connected?)
|
189
|
+
return unless force
|
190
|
+
log(:info, "Connecting...")
|
191
|
+
|
192
|
+
# Prevent recursive connection attempts from the same thread.
|
193
|
+
# This is done rather than using a Monitor to prevent potentially recursing
|
194
|
+
# infinitely while attempting to connect and continually failing. Instead, fail fast.
|
195
|
+
raise ConnectionFailure, "Failed to get node data." if thread_local[:locks][:connecting] == true
|
196
|
+
|
197
|
+
current_version = @refresh_version
|
198
|
+
@connect_mutex.synchronize do
|
199
|
+
# don't try to connect if another thread has done so while we were waiting for the lock
|
200
|
+
return unless current_version == @refresh_version
|
201
|
+
begin
|
202
|
+
thread_local[:locks][:connecting] = true
|
203
|
+
if @manager
|
204
|
+
ensure_manager
|
205
|
+
@manager.refresh!(@seeds)
|
206
|
+
else
|
207
|
+
@manager = PoolManager.new(self, @seeds)
|
208
|
+
ensure_manager
|
209
|
+
@manager.connect
|
210
|
+
end
|
211
|
+
ensure
|
212
|
+
thread_local[:locks][:connecting] = false
|
213
|
+
end
|
214
|
+
@refresh_version += 1
|
215
|
+
|
216
|
+
if @manager.pools.empty?
|
217
|
+
close
|
218
|
+
raise ConnectionFailure, "Failed to connect to any node."
|
219
|
+
end
|
220
|
+
check_wire_version_in_range
|
221
|
+
@connected = true
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# Determine whether a replica set refresh is
|
226
|
+
# required. If so, run a hard refresh. You can
|
227
|
+
# force a hard refresh by running
|
228
|
+
# MongoReplicaSetClient#hard_refresh!
|
229
|
+
#
|
230
|
+
# @return [Boolean] +true+ unless a hard refresh
|
231
|
+
# is run and the refresh lock can't be acquired.
|
232
|
+
def refresh(opts={})
|
233
|
+
if !connected?
|
234
|
+
log(:info, "Trying to check replica set health but not " +
|
235
|
+
"connected...")
|
236
|
+
return hard_refresh!
|
237
|
+
end
|
238
|
+
|
239
|
+
log(:debug, "Checking replica set connection health...")
|
240
|
+
ensure_manager
|
241
|
+
@manager.check_connection_health
|
242
|
+
|
243
|
+
if @manager.refresh_required?
|
244
|
+
return hard_refresh!
|
245
|
+
end
|
246
|
+
|
247
|
+
return true
|
248
|
+
end
|
249
|
+
|
250
|
+
# Force a hard refresh of this connection's view
|
251
|
+
# of the replica set.
|
252
|
+
#
|
253
|
+
# @return [Boolean] +true+ if hard refresh
|
254
|
+
# occurred. +false+ is returned when unable
|
255
|
+
# to get the refresh lock.
|
256
|
+
def hard_refresh!
|
257
|
+
log(:info, "Initiating hard refresh...")
|
258
|
+
connect(true)
|
259
|
+
return true
|
260
|
+
end
|
261
|
+
|
262
|
+
def connected?
|
263
|
+
@connected && !@manager.pools.empty?
|
264
|
+
end
|
265
|
+
|
266
|
+
# @deprecated
|
267
|
+
def connecting?
|
268
|
+
warn "MongoReplicaSetClient#connecting? is deprecated and will be removed in v2.0."
|
269
|
+
false
|
270
|
+
end
|
271
|
+
|
272
|
+
# The replica set primary's host name.
|
273
|
+
#
|
274
|
+
# @return [String]
|
275
|
+
def host
|
276
|
+
@manager.primary_pool.host
|
277
|
+
end
|
278
|
+
|
279
|
+
# The replica set primary's port.
|
280
|
+
#
|
281
|
+
# @return [Integer]
|
282
|
+
def port
|
283
|
+
@manager.primary_pool.port
|
284
|
+
end
|
285
|
+
|
286
|
+
def nodes
|
287
|
+
warn "MongoReplicaSetClient#nodes is DEPRECATED and will be removed in v2.0. " +
|
288
|
+
"Please use MongoReplicaSetClient#seeds instead."
|
289
|
+
@seeds
|
290
|
+
end
|
291
|
+
|
292
|
+
# Determine whether we're reading from a primary node. If false,
|
293
|
+
# this connection connects to a secondary node and @read_secondaries is true.
|
294
|
+
#
|
295
|
+
# @return [Boolean]
|
296
|
+
def read_primary?
|
297
|
+
read_pool == primary_pool
|
298
|
+
end
|
299
|
+
alias :primary? :read_primary?
|
300
|
+
|
301
|
+
# Close the connection to the database.
|
302
|
+
def close(opts={})
|
303
|
+
if opts[:soft]
|
304
|
+
@manager.close(:soft => true) if @manager
|
305
|
+
else
|
306
|
+
@manager.close if @manager
|
307
|
+
end
|
308
|
+
|
309
|
+
# Clear the reference to this object.
|
310
|
+
thread_local[:managers].delete(self)
|
311
|
+
unpin_pool
|
312
|
+
|
313
|
+
@connected = false
|
314
|
+
end
|
315
|
+
|
316
|
+
# If a ConnectionFailure is raised, this method will be called
|
317
|
+
# to close the connection and reset connection values.
|
318
|
+
# @deprecated
|
319
|
+
def reset_connection
|
320
|
+
close
|
321
|
+
warn "MongoReplicaSetClient#reset_connection is now deprecated and will be removed in v2.0. " +
|
322
|
+
"Use MongoReplicaSetClient#close instead."
|
323
|
+
end
|
324
|
+
|
325
|
+
# Returns +true+ if it's okay to read from a secondary node.
|
326
|
+
#
|
327
|
+
# This method exist primarily so that Cursor objects will
|
328
|
+
# generate query messages with a slaveOkay value of +true+.
|
329
|
+
#
|
330
|
+
# @return [Boolean] +true+
|
331
|
+
def slave_ok?
|
332
|
+
@read != :primary
|
333
|
+
end
|
334
|
+
|
335
|
+
# Generic socket checkout
|
336
|
+
# Takes a block that returns a socket from pool
|
337
|
+
def checkout
|
338
|
+
ensure_manager
|
339
|
+
|
340
|
+
connected? ? sync_refresh : connect
|
341
|
+
|
342
|
+
begin
|
343
|
+
socket = yield
|
344
|
+
rescue => ex
|
345
|
+
checkin(socket) if socket
|
346
|
+
raise ex
|
347
|
+
end
|
348
|
+
|
349
|
+
if socket
|
350
|
+
return socket
|
351
|
+
else
|
352
|
+
@connected = false
|
353
|
+
raise ConnectionFailure.new("Could not checkout a socket.")
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
def checkout_reader(read_pref={})
|
358
|
+
checkout do
|
359
|
+
pool = read_pool(read_pref)
|
360
|
+
get_socket_from_pool(pool)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
# Checkout a socket for writing (i.e., a primary node).
|
365
|
+
def checkout_writer
|
366
|
+
checkout do
|
367
|
+
get_socket_from_pool(primary_pool)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
# Checkin a socket used for reading.
|
372
|
+
def checkin(socket)
|
373
|
+
if socket && socket.pool
|
374
|
+
socket.checkin
|
375
|
+
end
|
376
|
+
sync_refresh
|
377
|
+
end
|
378
|
+
|
379
|
+
def ensure_manager
|
380
|
+
thread_local[:managers][self] = @manager
|
381
|
+
end
|
382
|
+
|
383
|
+
def pinned_pool
|
384
|
+
thread_local[:pinned_pools][@manager.object_id] if @manager
|
385
|
+
end
|
386
|
+
|
387
|
+
def pin_pool(pool, read_preference)
|
388
|
+
if @manager
|
389
|
+
thread_local[:pinned_pools][@manager.object_id] = {
|
390
|
+
:pool => pool,
|
391
|
+
:read_preference => read_preference
|
392
|
+
}
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
def unpin_pool
|
397
|
+
thread_local[:pinned_pools].delete @manager.object_id if @manager
|
398
|
+
end
|
399
|
+
|
400
|
+
def get_socket_from_pool(pool)
|
401
|
+
begin
|
402
|
+
pool.checkout if pool
|
403
|
+
rescue ConnectionFailure
|
404
|
+
nil
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
def local_manager
|
409
|
+
thread_local[:managers][self]
|
410
|
+
end
|
411
|
+
|
412
|
+
def arbiters
|
413
|
+
local_manager.arbiters.nil? ? [] : local_manager.arbiters
|
414
|
+
end
|
415
|
+
|
416
|
+
def primary
|
417
|
+
local_manager ? local_manager.primary : nil
|
418
|
+
end
|
419
|
+
|
420
|
+
# Note: might want to freeze these after connecting.
|
421
|
+
def secondaries
|
422
|
+
local_manager ? local_manager.secondaries : []
|
423
|
+
end
|
424
|
+
|
425
|
+
def hosts
|
426
|
+
local_manager ? local_manager.hosts : []
|
427
|
+
end
|
428
|
+
|
429
|
+
def primary_pool
|
430
|
+
local_manager ? local_manager.primary_pool : nil
|
431
|
+
end
|
432
|
+
|
433
|
+
def secondary_pool
|
434
|
+
local_manager ? local_manager.secondary_pool : nil
|
435
|
+
end
|
436
|
+
|
437
|
+
def secondary_pools
|
438
|
+
local_manager ? local_manager.secondary_pools : []
|
439
|
+
end
|
440
|
+
|
441
|
+
def pools
|
442
|
+
local_manager ? local_manager.pools : []
|
443
|
+
end
|
444
|
+
|
445
|
+
def tag_map
|
446
|
+
local_manager ? local_manager.tag_map : {}
|
447
|
+
end
|
448
|
+
|
449
|
+
def max_bson_size
|
450
|
+
return local_manager.max_bson_size if local_manager
|
451
|
+
DEFAULT_MAX_BSON_SIZE
|
452
|
+
end
|
453
|
+
|
454
|
+
def max_message_size
|
455
|
+
return local_manager.max_message_size if local_manager
|
456
|
+
max_bson_size * MESSAGE_SIZE_FACTOR
|
457
|
+
end
|
458
|
+
|
459
|
+
def max_wire_version
|
460
|
+
return local_manager.max_wire_version if local_manager
|
461
|
+
0
|
462
|
+
end
|
463
|
+
|
464
|
+
def min_wire_version
|
465
|
+
return local_manager.min_wire_version if local_manager
|
466
|
+
0
|
467
|
+
end
|
468
|
+
|
469
|
+
def primary_wire_version_feature?(feature)
|
470
|
+
local_manager && local_manager.primary_pool && local_manager.primary_pool.node.wire_version_feature?(feature)
|
471
|
+
end
|
472
|
+
|
473
|
+
def max_write_batch_size
|
474
|
+
local_manager && local_manager.primary_pool && local_manager.primary_pool.node.max_write_batch_size
|
475
|
+
end
|
476
|
+
|
477
|
+
private
|
478
|
+
|
479
|
+
# Parse option hash
|
480
|
+
def setup(opts)
|
481
|
+
# Refresh
|
482
|
+
@refresh_mode = opts.delete(:refresh_mode) || false
|
483
|
+
@refresh_interval = opts.delete(:refresh_interval) || 90
|
484
|
+
|
485
|
+
if @refresh_mode && @refresh_interval < 60
|
486
|
+
@refresh_interval = 60 unless ENV['TEST_MODE'] = 'TRUE'
|
487
|
+
end
|
488
|
+
|
489
|
+
if @refresh_mode == :async
|
490
|
+
warn ":async refresh mode has been deprecated. Refresh
|
491
|
+
mode will be disabled."
|
492
|
+
elsif ![:sync, false].include?(@refresh_mode)
|
493
|
+
raise MongoArgumentError,
|
494
|
+
"Refresh mode must be either :sync or false."
|
495
|
+
end
|
496
|
+
|
497
|
+
if opts[:read_secondary]
|
498
|
+
warn ":read_secondary options has now been deprecated and will " +
|
499
|
+
"be removed in driver v2.0. Use the :read option instead."
|
500
|
+
@read_secondary = opts.delete(:read_secondary) || false
|
501
|
+
end
|
502
|
+
|
503
|
+
# Replica set name
|
504
|
+
if opts[:rs_name]
|
505
|
+
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
506
|
+
"Please use :name instead."
|
507
|
+
@replica_set_name = opts.delete(:rs_name)
|
508
|
+
else
|
509
|
+
@replica_set_name = opts.delete(:name)
|
510
|
+
end
|
511
|
+
|
512
|
+
super opts
|
513
|
+
end
|
514
|
+
|
515
|
+
def sync_refresh
|
516
|
+
if @refresh_mode == :sync &&
|
517
|
+
((Time.now - @last_refresh) > @refresh_interval)
|
518
|
+
@last_refresh = Time.now
|
519
|
+
refresh
|
520
|
+
end
|
521
|
+
end
|
522
|
+
end
|
523
|
+
end
|