1
1
#!/usr/bin/ruby
2
2
3
+ require 'thread'
4
+
3
5
STDOUT . sync = true
4
6
5
7
unless defined? Mongo
@@ -17,6 +19,7 @@ def initialize(opts={})
17
19
@host = opts [ :host ] || 'localhost'
18
20
@retries = opts [ :retries ] || 60
19
21
@config = { "_id" => @name , "members" => [ ] }
22
+ @durable = opts . fetch ( :durable , false )
20
23
@path = File . join ( File . expand_path ( File . dirname ( __FILE__ ) ) , "data" )
21
24
22
25
@arbiter_count = opts [ :arbiter_count ] || 2
@@ -96,19 +99,21 @@ def init_node(n)
96
99
def start_cmd ( n )
97
100
@mongods [ n ] [ 'start' ] = "mongod --replSet #{ @name } --logpath '#{ @mongods [ n ] [ 'log_path' ] } ' " +
98
101
" --dbpath #{ @mongods [ n ] [ 'db_path' ] } --port #{ @mongods [ n ] [ 'port' ] } --fork"
102
+ @mongods [ n ] [ 'start' ] += " --dur" if @durable
103
+ @mongods [ n ] [ 'start' ]
99
104
end
100
105
101
- def kill ( node )
106
+ def kill ( node , signal = 2 )
102
107
pid = @mongods [ node ] [ 'pid' ]
103
108
puts "** Killing node with pid #{ pid } at port #{ @mongods [ node ] [ 'port' ] } "
104
- system ( "kill -2 #{ @mongods [ node ] [ 'pid' ] } " )
109
+ system ( "kill -#{ signal } #{ @mongods [ node ] [ 'pid' ] } " )
105
110
@mongods [ node ] [ 'up' ] = false
106
111
sleep ( 1 )
107
112
end
108
113
109
- def kill_primary
114
+ def kill_primary ( signal = 2 )
110
115
node = get_node_with_state ( 1 )
111
- kill ( node )
116
+ kill ( node , signal )
112
117
return node
113
118
end
114
119
@@ -184,6 +189,16 @@ def arbiters
184
189
get_all_host_pairs_with_state ( 7 )
185
190
end
186
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
+
187
202
private
188
203
189
204
def initiate
@@ -239,13 +254,13 @@ def attempt
239
254
while count < @retries do
240
255
begin
241
256
return yield
242
- rescue Mongo ::OperationFailure , Mongo ::ConnectionFailure
257
+ rescue Mongo ::OperationFailure , Mongo ::ConnectionFailure => ex
243
258
sleep ( 1 )
244
259
count += 1
245
260
end
246
261
end
247
262
248
- raise exception
263
+ raise ex
249
264
end
250
265
251
266
end
0 commit comments