mongo 1.3.0 → 1.12.5
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/{LICENSE.txt → LICENSE} +1 -1
- data/README.md +122 -271
- data/Rakefile +25 -209
- data/VERSION +1 -0
- data/bin/mongo_console +31 -9
- data/lib/mongo/bulk_write_collection_view.rb +387 -0
- data/lib/mongo/collection.rb +576 -269
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/connection/node.rb +249 -0
- data/lib/mongo/connection/pool.rb +340 -0
- data/lib/mongo/connection/pool_manager.rb +320 -0
- data/lib/mongo/connection/sharding_pool_manager.rb +67 -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 +87 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection.rb +7 -875
- data/lib/mongo/cursor.rb +403 -117
- data/lib/mongo/db.rb +444 -243
- data/lib/mongo/exception.rb +145 -0
- data/lib/mongo/functional/authentication.rb +455 -0
- data/lib/mongo/functional/logging.rb +85 -0
- data/lib/mongo/functional/read_preference.rb +183 -0
- data/lib/mongo/functional/scram.rb +556 -0
- data/lib/mongo/functional/uri_parser.rb +409 -0
- data/lib/mongo/functional/write_concern.rb +66 -0
- data/lib/mongo/functional.rb +20 -0
- data/lib/mongo/gridfs/grid.rb +30 -24
- data/lib/mongo/gridfs/grid_ext.rb +6 -10
- data/lib/mongo/gridfs/grid_file_system.rb +38 -20
- data/lib/mongo/gridfs/grid_io.rb +84 -75
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/legacy.rb +140 -0
- data/lib/mongo/mongo_client.rb +697 -0
- data/lib/mongo/mongo_replica_set_client.rb +535 -0
- data/lib/mongo/mongo_sharded_client.rb +159 -0
- data/lib/mongo/networking.rb +372 -0
- data/lib/mongo/{util → utils}/conversions.rb +29 -8
- data/lib/mongo/{util → utils}/core_ext.rb +28 -18
- data/lib/mongo/{util → utils}/server_version.rb +4 -6
- data/lib/mongo/{util → utils}/support.rb +29 -31
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo.rb +51 -50
- data/mongo.gemspec +29 -32
- data/test/functional/authentication_test.rb +39 -0
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1198 -0
- data/test/functional/client_test.rb +627 -0
- data/test/functional/collection_test.rb +2175 -0
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
- data/test/functional/cursor_fail_test.rb +57 -0
- data/test/functional/cursor_message_test.rb +56 -0
- data/test/functional/cursor_test.rb +683 -0
- data/test/functional/db_api_test.rb +835 -0
- data/test/functional/db_connection_test.rb +25 -0
- data/test/functional/db_test.rb +348 -0
- data/test/functional/grid_file_system_test.rb +285 -0
- data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
- data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
- data/test/functional/pool_test.rb +136 -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 +60 -0
- data/test/functional/uri_test.rb +446 -0
- data/test/functional/write_concern_test.rb +118 -0
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +476 -0
- data/test/replica_set/authentication_test.rb +37 -0
- data/test/replica_set/basic_test.rb +189 -0
- data/test/replica_set/client_test.rb +393 -0
- data/test/replica_set/connection_test.rb +138 -0
- data/test/replica_set/count_test.rb +66 -0
- data/test/replica_set/cursor_test.rb +220 -0
- data/test/replica_set/insert_test.rb +157 -0
- data/test/replica_set/max_values_test.rb +151 -0
- data/test/replica_set/pinning_test.rb +105 -0
- data/test/replica_set/query_test.rb +73 -0
- data/test/replica_set/read_preference_test.rb +219 -0
- data/test/replica_set/refresh_test.rb +211 -0
- data/test/replica_set/replication_ack_test.rb +95 -0
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +203 -0
- data/test/shared/authentication/basic_auth_shared.rb +260 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
- data/test/shared/authentication/gssapi_shared.rb +176 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/authentication/scram_shared.rb +92 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +53 -94
- data/test/threading/basic_test.rb +120 -0
- data/test/tools/mongo_config.rb +708 -0
- data/test/tools/mongo_config_test.rb +160 -0
- data/test/unit/client_test.rb +381 -0
- data/test/unit/collection_test.rb +89 -53
- data/test/unit/connection_test.rb +282 -32
- data/test/unit/cursor_test.rb +206 -8
- data/test/unit/db_test.rb +55 -13
- data/test/unit/grid_test.rb +43 -16
- 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 +111 -0
- data/test/unit/read_pref_test.rb +406 -0
- data/test/unit/read_test.rb +159 -0
- data/test/unit/safe_test.rb +69 -36
- data/test/unit/sharding_pool_manager_test.rb +84 -0
- data/test/unit/write_concern_test.rb +175 -0
- data.tar.gz.sig +3 -0
- metadata +227 -216
- metadata.gz.sig +0 -0
- data/docs/CREDITS.md +0 -123
- data/docs/FAQ.md +0 -116
- data/docs/GridFS.md +0 -158
- data/docs/HISTORY.md +0 -244
- data/docs/RELEASES.md +0 -33
- data/docs/REPLICA_SETS.md +0 -72
- data/docs/TUTORIAL.md +0 -247
- data/docs/WRITE_CONCERN.md +0 -28
- data/lib/mongo/exceptions.rb +0 -71
- data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
- data/lib/mongo/repl_set_connection.rb +0 -342
- data/lib/mongo/test.rb +0 -20
- data/lib/mongo/util/pool.rb +0 -177
- data/lib/mongo/util/uri_parser.rb +0 -185
- data/test/async/collection_test.rb +0 -224
- data/test/async/connection_test.rb +0 -24
- data/test/async/cursor_test.rb +0 -162
- data/test/async/worker_pool_test.rb +0 -99
- data/test/auxillary/1.4_features.rb +0 -166
- data/test/auxillary/authentication_test.rb +0 -68
- data/test/auxillary/autoreconnect_test.rb +0 -41
- data/test/auxillary/fork_test.rb +0 -30
- data/test/auxillary/repl_set_auth_test.rb +0 -58
- data/test/auxillary/slave_connection_test.rb +0 -36
- data/test/auxillary/threaded_authentication_test.rb +0 -101
- data/test/bson/binary_test.rb +0 -15
- data/test/bson/bson_test.rb +0 -649
- data/test/bson/byte_buffer_test.rb +0 -208
- data/test/bson/hash_with_indifferent_access_test.rb +0 -38
- data/test/bson/json_test.rb +0 -17
- data/test/bson/object_id_test.rb +0 -154
- data/test/bson/ordered_hash_test.rb +0 -204
- data/test/bson/timestamp_test.rb +0 -24
- data/test/collection_test.rb +0 -910
- data/test/connection_test.rb +0 -309
- data/test/cursor_fail_test.rb +0 -75
- data/test/cursor_message_test.rb +0 -43
- data/test/cursor_test.rb +0 -483
- data/test/db_api_test.rb +0 -726
- data/test/db_connection_test.rb +0 -15
- data/test/db_test.rb +0 -287
- data/test/grid_file_system_test.rb +0 -243
- data/test/load/resque/load.rb +0 -21
- data/test/load/resque/processor.rb +0 -26
- data/test/load/thin/load.rb +0 -24
- data/test/load/unicorn/load.rb +0 -23
- data/test/load/unicorn/unicorn.rb +0 -29
- data/test/replica_sets/connect_test.rb +0 -94
- data/test/replica_sets/connection_string_test.rb +0 -32
- data/test/replica_sets/count_test.rb +0 -35
- data/test/replica_sets/insert_test.rb +0 -53
- data/test/replica_sets/pooled_insert_test.rb +0 -55
- data/test/replica_sets/query_secondaries.rb +0 -96
- data/test/replica_sets/query_test.rb +0 -51
- data/test/replica_sets/replication_ack_test.rb +0 -66
- data/test/replica_sets/rs_test_helper.rb +0 -27
- data/test/safe_test.rb +0 -68
- data/test/support/hash_with_indifferent_access.rb +0 -186
- data/test/support/keys.rb +0 -45
- data/test/support_test.rb +0 -18
- data/test/threading/threading_with_large_pool_test.rb +0 -90
- data/test/threading_test.rb +0 -87
- data/test/tools/auth_repl_set_manager.rb +0 -14
- data/test/tools/load.rb +0 -58
- data/test/tools/repl_set_manager.rb +0 -266
- data/test/tools/sharding_manager.rb +0 -202
- data/test/tools/test.rb +0 -4
- data/test/unit/pool_test.rb +0 -9
- data/test/unit/repl_set_connection_test.rb +0 -59
- data/test/uri_test.rb +0 -91
@@ -0,0 +1,183 @@
|
|
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
|
+
module ReadPreference
|
17
|
+
READ_PREFERENCES = [
|
18
|
+
:primary,
|
19
|
+
:primary_preferred,
|
20
|
+
:secondary,
|
21
|
+
:secondary_preferred,
|
22
|
+
:nearest
|
23
|
+
]
|
24
|
+
|
25
|
+
MONGOS_MODES = {
|
26
|
+
:primary => 'primary',
|
27
|
+
:primary_preferred => 'primaryPreferred',
|
28
|
+
:secondary => 'secondary',
|
29
|
+
:secondary_preferred => 'secondaryPreferred',
|
30
|
+
:nearest => 'nearest'
|
31
|
+
}
|
32
|
+
|
33
|
+
# Commands that may be sent to replica-set secondaries, depending on
|
34
|
+
# read preference and tags. All other commands are always run on the primary.
|
35
|
+
SECONDARY_OK_COMMANDS = [
|
36
|
+
'group',
|
37
|
+
'aggregate',
|
38
|
+
'collstats',
|
39
|
+
'dbstats',
|
40
|
+
'count',
|
41
|
+
'distinct',
|
42
|
+
'geonear',
|
43
|
+
'geosearch',
|
44
|
+
'geowalk',
|
45
|
+
'mapreduce',
|
46
|
+
'replsetgetstatus',
|
47
|
+
'ismaster',
|
48
|
+
'parallelcollectionscan',
|
49
|
+
'text'
|
50
|
+
]
|
51
|
+
|
52
|
+
def self.mongos(mode, tag_sets)
|
53
|
+
if mode != :secondary_preferred || !tag_sets.empty?
|
54
|
+
mongos_read_preference = BSON::OrderedHash[:mode => MONGOS_MODES[mode]]
|
55
|
+
mongos_read_preference[:tags] = tag_sets if !tag_sets.empty?
|
56
|
+
end
|
57
|
+
mongos_read_preference
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.validate(value)
|
61
|
+
if READ_PREFERENCES.include?(value)
|
62
|
+
return true
|
63
|
+
else
|
64
|
+
raise MongoArgumentError, "#{value} is not a valid read preference. " +
|
65
|
+
"Please specify one of the following read preferences as a symbol: #{READ_PREFERENCES}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns true if it's ok to run the command on a secondary
|
70
|
+
def self.secondary_ok?(selector)
|
71
|
+
command = selector.keys.first.to_s.downcase
|
72
|
+
|
73
|
+
if command == 'mapreduce'
|
74
|
+
out = selector.select { |k, v| k.to_s.downcase == 'out' }.first.last
|
75
|
+
# the server only looks at the first key in the out object
|
76
|
+
return out.respond_to?(:keys) && out.keys.first.to_s.downcase == 'inline'
|
77
|
+
elsif command == 'aggregate'
|
78
|
+
pipeline = selector['pipeline'] || selector[:pipeline]
|
79
|
+
return pipeline.none? { |op| op.key?('$out') || op.key?(:$out) }
|
80
|
+
end
|
81
|
+
SECONDARY_OK_COMMANDS.member?(command)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns true if the command should be rerouted to the primary.
|
85
|
+
def self.reroute_cmd_primary?(read_pref, selector)
|
86
|
+
return false if read_pref == :primary
|
87
|
+
!secondary_ok?(selector)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Given a command and read preference, possibly reroute to primary.
|
91
|
+
def self.cmd_read_pref(read_pref, selector)
|
92
|
+
ReadPreference::validate(read_pref)
|
93
|
+
if reroute_cmd_primary?(read_pref, selector)
|
94
|
+
warn "Database command '#{selector.keys.first}' rerouted to primary node"
|
95
|
+
read_pref = :primary
|
96
|
+
end
|
97
|
+
read_pref
|
98
|
+
end
|
99
|
+
|
100
|
+
def read_preference
|
101
|
+
{
|
102
|
+
:mode => @read,
|
103
|
+
:tags => @tag_sets,
|
104
|
+
:latency => @acceptable_latency
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def read_pool(read_preference_override={})
|
109
|
+
return primary_pool if mongos?
|
110
|
+
|
111
|
+
read_pref = read_preference.merge(read_preference_override)
|
112
|
+
|
113
|
+
if pinned_pool && pinned_pool[:read_preference] == read_pref
|
114
|
+
pool = pinned_pool[:pool]
|
115
|
+
else
|
116
|
+
unpin_pool
|
117
|
+
pool = select_pool(read_pref)
|
118
|
+
end
|
119
|
+
|
120
|
+
unless pool
|
121
|
+
raise ConnectionFailure, "No replica set member available for query " +
|
122
|
+
"with read preference matching mode #{read_pref[:mode]} and tags " +
|
123
|
+
"matching #{read_pref[:tags]}."
|
124
|
+
end
|
125
|
+
|
126
|
+
pool
|
127
|
+
end
|
128
|
+
|
129
|
+
def select_pool(read_pref)
|
130
|
+
if read_pref[:mode] == :primary && !read_pref[:tags].empty?
|
131
|
+
raise MongoArgumentError, "Read preference :primary cannot be combined with tags"
|
132
|
+
end
|
133
|
+
|
134
|
+
case read_pref[:mode]
|
135
|
+
when :primary
|
136
|
+
primary_pool
|
137
|
+
when :primary_preferred
|
138
|
+
primary_pool || select_secondary_pool(secondary_pools, read_pref)
|
139
|
+
when :secondary
|
140
|
+
select_secondary_pool(secondary_pools, read_pref)
|
141
|
+
when :secondary_preferred
|
142
|
+
select_secondary_pool(secondary_pools, read_pref) || primary_pool
|
143
|
+
when :nearest
|
144
|
+
select_near_pool(pools, read_pref)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def select_secondary_pool(candidates, read_pref)
|
149
|
+
matching_pools = match_tag_sets(secondary_pools, read_pref[:tags])
|
150
|
+
select_near_pools(matching_pools, read_pref).first
|
151
|
+
end
|
152
|
+
|
153
|
+
def select_near_pool(candidates, read_pref)
|
154
|
+
matching_pools = match_tag_sets(candidates, read_pref[:tags])
|
155
|
+
select_near_pools(matching_pools, read_pref).first
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def select_near_pools(candidates, read_pref)
|
161
|
+
return candidates if candidates.empty?
|
162
|
+
latency = read_pref[:latency]
|
163
|
+
nearest_pool = candidates.min_by(&:ping_time)
|
164
|
+
max_latency = nearest_pool.ping_time + latency
|
165
|
+
near_pools = candidates.select { |candidate| candidate.ping_time <= max_latency }
|
166
|
+
near_pools.shuffle!
|
167
|
+
end
|
168
|
+
|
169
|
+
def match_tag_sets(candidates, tag_sets = [])
|
170
|
+
return candidates if tag_sets.empty?
|
171
|
+
matches = []
|
172
|
+
tag_sets.find do |tag_set|
|
173
|
+
matches = candidates.select do |candidate|
|
174
|
+
tag_set.none? do |k,v|
|
175
|
+
candidate.tags[k.to_s] != v
|
176
|
+
end
|
177
|
+
end
|
178
|
+
!matches.empty?
|
179
|
+
end
|
180
|
+
matches
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|