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
@@ -1,266 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby
|
2
|
-
|
3
|
-
require 'thread'
|
4
|
-
|
5
|
-
STDOUT.sync = true
|
6
|
-
|
7
|
-
unless defined? Mongo
|
8
|
-
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')
|
9
|
-
end
|
10
|
-
|
11
|
-
class ReplSetManager
|
12
|
-
|
13
|
-
attr_accessor :host, :start_port, :ports, :name, :mongods
|
14
|
-
|
15
|
-
def initialize(opts={})
|
16
|
-
@start_port = opts[:start_port] || 30000
|
17
|
-
@ports = []
|
18
|
-
@name = opts[:name] || 'replica-set-foo'
|
19
|
-
@host = opts[:host] || 'localhost'
|
20
|
-
@retries = opts[:retries] || 60
|
21
|
-
@config = {"_id" => @name, "members" => []}
|
22
|
-
@durable = opts.fetch(:durable, false)
|
23
|
-
@path = File.join(File.expand_path(File.dirname(__FILE__)), "data")
|
24
|
-
|
25
|
-
@arbiter_count = opts[:arbiter_count] || 2
|
26
|
-
@secondary_count = opts[:secondary_count] || 1
|
27
|
-
@passive_count = opts[:passive_count] || 1
|
28
|
-
@primary_count = 1
|
29
|
-
|
30
|
-
@count = @primary_count + @passive_count + @arbiter_count + @secondary_count
|
31
|
-
if @count > 7
|
32
|
-
raise StandardError, "Cannot create a replica set with #{node_count} nodes. 7 is the max."
|
33
|
-
end
|
34
|
-
|
35
|
-
@mongods = {}
|
36
|
-
end
|
37
|
-
|
38
|
-
def start_set
|
39
|
-
puts "** Starting a replica set with #{@count} nodes"
|
40
|
-
|
41
|
-
system("killall mongod")
|
42
|
-
|
43
|
-
n = 0
|
44
|
-
(@primary_count + @secondary_count).times do |n|
|
45
|
-
init_node(n)
|
46
|
-
n += 1
|
47
|
-
end
|
48
|
-
|
49
|
-
@passive_count.times do
|
50
|
-
init_node(n) do |attrs|
|
51
|
-
attrs['priority'] = 0
|
52
|
-
end
|
53
|
-
n += 1
|
54
|
-
end
|
55
|
-
|
56
|
-
@arbiter_count.times do
|
57
|
-
init_node(n) do |attrs|
|
58
|
-
attrs['arbiterOnly'] = true
|
59
|
-
end
|
60
|
-
n += 1
|
61
|
-
end
|
62
|
-
|
63
|
-
initiate
|
64
|
-
ensure_up
|
65
|
-
end
|
66
|
-
|
67
|
-
def cleanup_set
|
68
|
-
system("killall mongod")
|
69
|
-
@count.times do |n|
|
70
|
-
system("rm -rf #{@mongods[n]['db_path']}")
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def init_node(n)
|
75
|
-
@mongods[n] ||= {}
|
76
|
-
port = @start_port + n
|
77
|
-
@ports << port
|
78
|
-
@mongods[n]['port'] = port
|
79
|
-
@mongods[n]['db_path'] = get_path("rs-#{port}")
|
80
|
-
@mongods[n]['log_path'] = get_path("log-#{port}")
|
81
|
-
system("rm -rf #{@mongods[n]['db_path']}")
|
82
|
-
system("mkdir -p #{@mongods[n]['db_path']}")
|
83
|
-
|
84
|
-
@mongods[n]['start'] = start_cmd(n)
|
85
|
-
start(n)
|
86
|
-
|
87
|
-
member = {'_id' => n, 'host' => "#{@host}:#{@mongods[n]['port']}"}
|
88
|
-
|
89
|
-
if block_given?
|
90
|
-
custom_attrs = {}
|
91
|
-
yield custom_attrs
|
92
|
-
member.merge!(custom_attrs)
|
93
|
-
@mongods[n].merge!(custom_attrs)
|
94
|
-
end
|
95
|
-
|
96
|
-
@config['members'] << member
|
97
|
-
end
|
98
|
-
|
99
|
-
def start_cmd(n)
|
100
|
-
@mongods[n]['start'] = "mongod --replSet #{@name} --logpath '#{@mongods[n]['log_path']}' " +
|
101
|
-
" --dbpath #{@mongods[n]['db_path']} --port #{@mongods[n]['port']} --fork"
|
102
|
-
@mongods[n]['start'] += " --dur" if @durable
|
103
|
-
@mongods[n]['start']
|
104
|
-
end
|
105
|
-
|
106
|
-
def kill(node, signal=2)
|
107
|
-
pid = @mongods[node]['pid']
|
108
|
-
puts "** Killing node with pid #{pid} at port #{@mongods[node]['port']}"
|
109
|
-
system("kill -#{signal} #{@mongods[node]['pid']}")
|
110
|
-
@mongods[node]['up'] = false
|
111
|
-
sleep(1)
|
112
|
-
end
|
113
|
-
|
114
|
-
def kill_primary(signal=2)
|
115
|
-
node = get_node_with_state(1)
|
116
|
-
kill(node, signal)
|
117
|
-
return node
|
118
|
-
end
|
119
|
-
|
120
|
-
# Note that we have to rescue a connection failure
|
121
|
-
# when we run the StepDown command because that
|
122
|
-
# command will close the connection.
|
123
|
-
def step_down_primary
|
124
|
-
primary = get_node_with_state(1)
|
125
|
-
con = get_connection(primary)
|
126
|
-
begin
|
127
|
-
con['admin'].command({'replSetStepDown' => 90})
|
128
|
-
rescue Mongo::ConnectionFailure
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def kill_secondary
|
133
|
-
node = get_node_with_state(2)
|
134
|
-
kill(node)
|
135
|
-
return node
|
136
|
-
end
|
137
|
-
|
138
|
-
def restart_killed_nodes
|
139
|
-
nodes = @mongods.keys.select do |key|
|
140
|
-
@mongods[key]['up'] == false
|
141
|
-
end
|
142
|
-
|
143
|
-
nodes.each do |node|
|
144
|
-
start(node)
|
145
|
-
end
|
146
|
-
|
147
|
-
ensure_up
|
148
|
-
end
|
149
|
-
|
150
|
-
def get_node_from_port(port)
|
151
|
-
@mongods.keys.detect { |key| @mongods[key]['port'] == port }
|
152
|
-
end
|
153
|
-
|
154
|
-
def start(node)
|
155
|
-
system(@mongods[node]['start'])
|
156
|
-
@mongods[node]['up'] = true
|
157
|
-
sleep(0.5)
|
158
|
-
@mongods[node]['pid'] = File.open(File.join(@mongods[node]['db_path'], 'mongod.lock')).read.strip
|
159
|
-
end
|
160
|
-
alias :restart :start
|
161
|
-
|
162
|
-
def ensure_up
|
163
|
-
print "** Ensuring members are up..."
|
164
|
-
|
165
|
-
attempt do
|
166
|
-
con = get_connection
|
167
|
-
status = con['admin'].command({'replSetGetStatus' => 1})
|
168
|
-
print "."
|
169
|
-
if status['members'].all? { |m| m['health'] == 1 && [1, 2, 7].include?(m['state']) } &&
|
170
|
-
status['members'].any? { |m| m['state'] == 1 }
|
171
|
-
print "all members up!\n\n"
|
172
|
-
return status
|
173
|
-
else
|
174
|
-
raise Mongo::OperationFailure
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def primary
|
180
|
-
nodes = get_all_host_pairs_with_state(1)
|
181
|
-
nodes.empty? ? nil : nodes[0]
|
182
|
-
end
|
183
|
-
|
184
|
-
def secondaries
|
185
|
-
get_all_host_pairs_with_state(2)
|
186
|
-
end
|
187
|
-
|
188
|
-
def arbiters
|
189
|
-
get_all_host_pairs_with_state(7)
|
190
|
-
end
|
191
|
-
|
192
|
-
# String used for adding a shard via mongos
|
193
|
-
# using the addshard command.
|
194
|
-
def shard_string
|
195
|
-
str = "#{@name}/"
|
196
|
-
str << @mongods.map do |k, mongod|
|
197
|
-
"#{@host}:#{mongod['port']}"
|
198
|
-
end.join(',')
|
199
|
-
str
|
200
|
-
end
|
201
|
-
|
202
|
-
private
|
203
|
-
|
204
|
-
def initiate
|
205
|
-
con = get_connection
|
206
|
-
|
207
|
-
attempt do
|
208
|
-
con['admin'].command({'replSetInitiate' => @config})
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def get_node_with_state(state)
|
213
|
-
status = ensure_up
|
214
|
-
node = status['members'].detect {|m| m['state'] == state}
|
215
|
-
if node
|
216
|
-
host_port = node['name'].split(':')
|
217
|
-
port = host_port[1] ? host_port[1].to_i : 27017
|
218
|
-
key = @mongods.keys.detect {|key| @mongods[key]['port'] == port}
|
219
|
-
return key
|
220
|
-
else
|
221
|
-
return false
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
def get_all_host_pairs_with_state(state)
|
226
|
-
status = ensure_up
|
227
|
-
nodes = status['members'].select {|m| m['state'] == state}
|
228
|
-
nodes.map do |node|
|
229
|
-
host_port = node['name'].split(':')
|
230
|
-
port = host_port[1] ? host_port[1].to_i : 27017
|
231
|
-
[host, port]
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
def get_connection(node=nil)
|
236
|
-
con = attempt do
|
237
|
-
if !node
|
238
|
-
node = @mongods.keys.detect {|key| !@mongods[key]['arbiterOnly'] && @mongods[key]['up'] }
|
239
|
-
end
|
240
|
-
con = Mongo::Connection.new(@host, @mongods[node]['port'], :slave_ok => true)
|
241
|
-
end
|
242
|
-
|
243
|
-
return con
|
244
|
-
end
|
245
|
-
|
246
|
-
def get_path(name)
|
247
|
-
File.join(@path, name)
|
248
|
-
end
|
249
|
-
|
250
|
-
def attempt
|
251
|
-
raise "No block given!" unless block_given?
|
252
|
-
count = 0
|
253
|
-
|
254
|
-
while count < @retries do
|
255
|
-
begin
|
256
|
-
return yield
|
257
|
-
rescue Mongo::OperationFailure, Mongo::ConnectionFailure => ex
|
258
|
-
sleep(1)
|
259
|
-
count += 1
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
raise ex
|
264
|
-
end
|
265
|
-
|
266
|
-
end
|
@@ -1,202 +0,0 @@
|
|
1
|
-
require 'repl_set_manager'
|
2
|
-
require 'thread'
|
3
|
-
|
4
|
-
class ShardingManager
|
5
|
-
|
6
|
-
attr_accessor :shards
|
7
|
-
|
8
|
-
def initialize(opts={})
|
9
|
-
@durable = opts.fetch(:durable, true)
|
10
|
-
@host = "localhost"
|
11
|
-
|
12
|
-
@mongos_port = opts[:mongos_port] || 50000
|
13
|
-
@config_port = opts[:config_port] || 40000
|
14
|
-
@shard_start_port = opts[:start_shard_port] || 30000
|
15
|
-
@path = File.join(File.expand_path(File.dirname(__FILE__)), "data")
|
16
|
-
system("rm -rf #{@path}")
|
17
|
-
|
18
|
-
@shard_count = 2
|
19
|
-
@mongos_count = 1
|
20
|
-
@config_count = opts.fetch(:config_count, 1)
|
21
|
-
if ![1, 3].include?(@config_count)
|
22
|
-
raise ArgumentError, "Must specify 1 or 3 config servers."
|
23
|
-
end
|
24
|
-
|
25
|
-
@config_servers = {}
|
26
|
-
@mongos_servers = {}
|
27
|
-
@shards = []
|
28
|
-
@ports = []
|
29
|
-
end
|
30
|
-
|
31
|
-
def kill_random
|
32
|
-
shard_to_kill = rand(@shard_count)
|
33
|
-
@shards[shard_to_kill].kill_primary
|
34
|
-
end
|
35
|
-
|
36
|
-
def restart_killed
|
37
|
-
threads = []
|
38
|
-
@shards.each do |k, shard|
|
39
|
-
threads << Thread.new do
|
40
|
-
shard.restart_killed_nodes
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def start_cluster
|
46
|
-
start_sharding_components
|
47
|
-
start_mongos_servers
|
48
|
-
configure_cluster
|
49
|
-
end
|
50
|
-
|
51
|
-
def configure_cluster
|
52
|
-
add_shards
|
53
|
-
enable_sharding
|
54
|
-
shard_collection
|
55
|
-
end
|
56
|
-
|
57
|
-
def enable_sharding
|
58
|
-
mongos['admin'].command({:enablesharding => "app"})
|
59
|
-
end
|
60
|
-
|
61
|
-
def shard_collection
|
62
|
-
cmd = BSON::OrderedHash.new
|
63
|
-
cmd[:shardcollection] = "app.photos"
|
64
|
-
cmd[:key] = {:tid => 1}
|
65
|
-
p mongos['admin'].command(cmd)
|
66
|
-
end
|
67
|
-
|
68
|
-
def add_shards
|
69
|
-
@shards.each do |shard|
|
70
|
-
cmd = {:addshard => shard.shard_string}
|
71
|
-
p cmd
|
72
|
-
p mongos['admin'].command(cmd)
|
73
|
-
end
|
74
|
-
p mongos['admin'].command({:listshards => 1})
|
75
|
-
end
|
76
|
-
|
77
|
-
def mongos
|
78
|
-
attempt do
|
79
|
-
@mongos ||= Mongo::Connection.new(@host, @mongos_servers[0]['port'])
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
private
|
84
|
-
|
85
|
-
def start_sharding_components
|
86
|
-
system("killall mongos")
|
87
|
-
|
88
|
-
threads = []
|
89
|
-
threads << Thread.new do
|
90
|
-
start_shards
|
91
|
-
end
|
92
|
-
|
93
|
-
threads << Thread.new do
|
94
|
-
start_config_servers
|
95
|
-
end
|
96
|
-
threads.each {|t| t.join}
|
97
|
-
puts "\nShards and config servers up!"
|
98
|
-
end
|
99
|
-
|
100
|
-
def start_shards
|
101
|
-
threads = []
|
102
|
-
@shard_count.times do |n|
|
103
|
-
threads << Thread.new do
|
104
|
-
port = @shard_start_port + n * 100
|
105
|
-
shard = ReplSetManager.new(:arbiter_count => 0, :secondary_count => 2,
|
106
|
-
:passive_count => 0, :start_port => port, :durable => @durable,
|
107
|
-
:name => "shard-#{n}")
|
108
|
-
shard.start_set
|
109
|
-
shard.ensure_up
|
110
|
-
@shards << shard
|
111
|
-
end
|
112
|
-
end
|
113
|
-
threads.each {|t| t.join}
|
114
|
-
end
|
115
|
-
|
116
|
-
def start_config_servers
|
117
|
-
@config_count.times do |n|
|
118
|
-
@config_servers[n] ||= {}
|
119
|
-
port = @config_port + n
|
120
|
-
@ports << port
|
121
|
-
@config_servers[n]['port'] = port
|
122
|
-
@config_servers[n]['db_path'] = get_path("config-#{port}")
|
123
|
-
@config_servers[n]['log_path'] = get_path("log-config-#{port}")
|
124
|
-
system("rm -rf #{@config_servers[n]['db_path']}")
|
125
|
-
system("mkdir -p #{@config_servers[n]['db_path']}")
|
126
|
-
|
127
|
-
@config_servers[n]['start'] = start_config_cmd(n)
|
128
|
-
|
129
|
-
start(@config_servers, n)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def start_mongos_servers
|
134
|
-
@mongos_count.times do |n|
|
135
|
-
@mongos_servers[n] ||= {}
|
136
|
-
port = @mongos_port + n
|
137
|
-
@ports << port
|
138
|
-
@mongos_servers[n]['port'] = port
|
139
|
-
@mongos_servers[n]['db_path'] = get_path("mongos-#{port}")
|
140
|
-
@mongos_servers[n]['pidfile_path'] = File.join(@mongos_servers[n]['db_path'], "mongod.lock")
|
141
|
-
@mongos_servers[n]['log_path'] = get_path("log-mongos-#{port}")
|
142
|
-
system("rm -rf #{@mongos_servers[n]['db_path']}")
|
143
|
-
system("mkdir -p #{@mongos_servers[n]['db_path']}")
|
144
|
-
|
145
|
-
@mongos_servers[n]['start'] = start_mongos_cmd(n)
|
146
|
-
|
147
|
-
start(@mongos_servers, n)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def start_config_cmd(n)
|
152
|
-
cmd = "mongod --configsvr --logpath '#{@config_servers[n]['log_path']}' " +
|
153
|
-
" --dbpath #{@config_servers[n]['db_path']} --port #{@config_servers[n]['port']} --fork"
|
154
|
-
cmd += " --dur" if @durable
|
155
|
-
cmd
|
156
|
-
end
|
157
|
-
|
158
|
-
def start_mongos_cmd(n)
|
159
|
-
"mongos --configdb #{config_db_string} --logpath '#{@mongos_servers[n]['log_path']}' " +
|
160
|
-
"--pidfilepath #{@mongos_servers[n]['pidfile_path']} --port #{@mongos_servers[n]['port']} --fork"
|
161
|
-
end
|
162
|
-
|
163
|
-
def config_db_string
|
164
|
-
@config_servers.map do |k, v|
|
165
|
-
"#{@host}:#{v['port']}"
|
166
|
-
end.join(',')
|
167
|
-
end
|
168
|
-
|
169
|
-
def start(set, node)
|
170
|
-
system(set[node]['start'])
|
171
|
-
set[node]['up'] = true
|
172
|
-
sleep(0.5)
|
173
|
-
set[node]['pid'] = File.open(File.join(set[node]['db_path'], 'mongod.lock')).read.strip
|
174
|
-
end
|
175
|
-
alias :restart :start
|
176
|
-
|
177
|
-
private
|
178
|
-
|
179
|
-
def cleanup_config
|
180
|
-
end
|
181
|
-
|
182
|
-
def get_path(name)
|
183
|
-
File.join(@path, name)
|
184
|
-
end
|
185
|
-
|
186
|
-
# TODO: put this into a shared module
|
187
|
-
def attempt
|
188
|
-
raise "No block given!" unless block_given?
|
189
|
-
count = 0
|
190
|
-
|
191
|
-
while count < 50 do
|
192
|
-
begin
|
193
|
-
return yield
|
194
|
-
rescue Mongo::OperationFailure, Mongo::ConnectionFailure
|
195
|
-
sleep(1)
|
196
|
-
count += 1
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
raise exception
|
201
|
-
end
|
202
|
-
end
|
data/test/tools/test.rb
DELETED
data/test/unit/pool_test.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require './test/test_helper'
|
2
|
-
include Mongo
|
3
|
-
|
4
|
-
class ReplSetConnectionTest < Test::Unit::TestCase
|
5
|
-
context "Initialization: " do
|
6
|
-
context "connecting to a replica set" do
|
7
|
-
setup do
|
8
|
-
TCPSocket.stubs(:new).returns(new_mock_socket('localhost', 27017))
|
9
|
-
@conn = ReplSetConnection.new(['localhost', 27017], :connect => false, :read_secondary => true)
|
10
|
-
|
11
|
-
admin_db = new_mock_db
|
12
|
-
@hosts = ['localhost:27018', 'localhost:27019', 'localhost:27020']
|
13
|
-
|
14
|
-
admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts}).
|
15
|
-
then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
|
16
|
-
then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
|
17
|
-
then.returns({'ok' => 1, 'ismaster' => 0, 'arbiterOnly' => 1})
|
18
|
-
|
19
|
-
@conn.stubs(:[]).with('admin').returns(admin_db)
|
20
|
-
@conn.connect
|
21
|
-
end
|
22
|
-
|
23
|
-
should "store the hosts returned from the ismaster command" do
|
24
|
-
assert_equal 'localhost', @conn.primary_pool.host
|
25
|
-
assert_equal 27017, @conn.primary_pool.port
|
26
|
-
|
27
|
-
assert_equal 'localhost', @conn.secondary_pools[0].host
|
28
|
-
assert_equal 27018, @conn.secondary_pools[0].port
|
29
|
-
|
30
|
-
assert_equal 'localhost', @conn.secondary_pools[1].host
|
31
|
-
assert_equal 27019, @conn.secondary_pools[1].port
|
32
|
-
|
33
|
-
assert_equal 2, @conn.secondary_pools.length
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "connecting to a replica set and providing seed nodes" do
|
38
|
-
setup do
|
39
|
-
TCPSocket.stubs(:new).returns(new_mock_socket)
|
40
|
-
@conn = ReplSetConnection.new(['localhost', 27017], ['localhost', 27019], :connect => false)
|
41
|
-
|
42
|
-
admin_db = new_mock_db
|
43
|
-
@hosts = ['localhost:27017', 'localhost:27018', 'localhost:27019']
|
44
|
-
admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts})
|
45
|
-
@conn.stubs(:[]).with('admin').returns(admin_db)
|
46
|
-
@conn.connect
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context "initializing with a mongodb uri" do
|
51
|
-
|
52
|
-
should "parse a uri specifying multiple nodes" do
|
53
|
-
@conn = Connection.from_uri("mongodb://localhost:27017,mydb.com:27018", :connect => false)
|
54
|
-
assert_equal ['localhost', 27017], @conn.nodes[0]
|
55
|
-
assert_equal ['mydb.com', 27018], @conn.nodes[1]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
data/test/uri_test.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require './test/test_helper'
|
2
|
-
|
3
|
-
class TestThreading < Test::Unit::TestCase
|
4
|
-
include Mongo
|
5
|
-
|
6
|
-
def test_uri_without_port
|
7
|
-
parser = Mongo::URIParser.new('mongodb://localhost')
|
8
|
-
assert_equal 1, parser.nodes.length
|
9
|
-
assert_equal 'localhost', parser.nodes[0][0]
|
10
|
-
assert_equal 27017, parser.nodes[0][1]
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_basic_uri
|
14
|
-
parser = Mongo::URIParser.new('mongodb://localhost:27018')
|
15
|
-
assert_equal 1, parser.nodes.length
|
16
|
-
assert_equal 'localhost', parser.nodes[0][0]
|
17
|
-
assert_equal 27018, parser.nodes[0][1]
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_multiple_uris
|
21
|
-
parser = Mongo::URIParser.new('mongodb://a.example.com:27018,b.example.com')
|
22
|
-
assert_equal 2, parser.nodes.length
|
23
|
-
assert_equal 'a.example.com', parser.nodes[0][0]
|
24
|
-
assert_equal 27018, parser.nodes[0][1]
|
25
|
-
assert_equal 'b.example.com', parser.nodes[1][0]
|
26
|
-
assert_equal 27017, parser.nodes[1][1]
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_complex_passwords
|
30
|
-
parser = Mongo::URIParser.new('mongodb://bob:secret.word@a.example.com:27018/test')
|
31
|
-
assert_equal "bob", parser.auths[0]["username"]
|
32
|
-
assert_equal "secret.word", parser.auths[0]["password"]
|
33
|
-
|
34
|
-
parser = Mongo::URIParser.new('mongodb://bob:s-_3#%R.t@a.example.com:27018/test')
|
35
|
-
assert_equal "bob", parser.auths[0]["username"]
|
36
|
-
assert_equal "s-_3#%R.t", parser.auths[0]["password"]
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_passwords_contain_no_commas
|
40
|
-
assert_raise MongoArgumentError do
|
41
|
-
Mongo::URIParser.new('mongodb://bob:a,b@a.example.com:27018/test')
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_multiple_uris_with_auths
|
46
|
-
parser = Mongo::URIParser.new('mongodb://bob:secret@a.example.com:27018/test,joe:secret2@b.example.com/test2')
|
47
|
-
assert_equal 2, parser.nodes.length
|
48
|
-
assert_equal 'a.example.com', parser.nodes[0][0]
|
49
|
-
assert_equal 27018, parser.nodes[0][1]
|
50
|
-
assert_equal 'b.example.com', parser.nodes[1][0]
|
51
|
-
assert_equal 27017, parser.nodes[1][1]
|
52
|
-
assert_equal 2, parser.auths.length
|
53
|
-
assert_equal "bob", parser.auths[0]["username"]
|
54
|
-
assert_equal "secret", parser.auths[0]["password"]
|
55
|
-
assert_equal "test", parser.auths[0]["db_name"]
|
56
|
-
assert_equal "joe", parser.auths[1]["username"]
|
57
|
-
assert_equal "secret2", parser.auths[1]["password"]
|
58
|
-
assert_equal "test2", parser.auths[1]["db_name"]
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_opts_basic
|
62
|
-
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct;slaveok=true;safe=true')
|
63
|
-
assert_equal 'direct', parser.connect
|
64
|
-
assert parser.slaveok
|
65
|
-
assert parser.safe
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_opts_with_amp_separator
|
69
|
-
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct&slaveok=true&safe=true')
|
70
|
-
assert_equal 'direct', parser.connect
|
71
|
-
assert parser.slaveok
|
72
|
-
assert parser.safe
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_opts_safe
|
76
|
-
parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;wtimeout=200;fsync=true')
|
77
|
-
assert parser.safe
|
78
|
-
assert_equal 2, parser.w
|
79
|
-
assert_equal 200, parser.wtimeout
|
80
|
-
assert parser.fsync
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_opts_replica_set
|
84
|
-
assert_raise_error MongoArgumentError, "specify that connect=replicaset" do
|
85
|
-
Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo')
|
86
|
-
end
|
87
|
-
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=replicaset;replicaset=foo')
|
88
|
-
assert_equal 'foo', parser.replicaset
|
89
|
-
assert_equal 'replicaset', parser.connect
|
90
|
-
end
|
91
|
-
end
|