Class: Mink::ReplSetManager
- Inherits:
-
Object
- Object
- Mink::ReplSetManager
show all
- Includes:
- ManagerHelper
- Defined in:
- lib/mink/managers/repl_set_manager.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#attempt, #get_path, #kill_existing_mongods, #kill_existing_mongos, #kill_pidlist
Constructor Details
Returns a new instance of ReplSetManager.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/mink/managers/repl_set_manager.rb', line 7
def initialize(opts={})
@durable = opts.fetch(:durable, false)
@start_port = opts.fetch(:start_port, 30000)
@name = opts.fetch(:name, 'replica-set-foo')
@host = opts.fetch(:host, 'localhost')
@working_dir = opts.fetch(:working_dir, nil)
@mongod_path = opts.fetch(:mongod_path, "mongod")
@write_conf = opts.fetch(:write_conf, false)
@write_pids = opts.fetch(:write_pids, false)
@replica_count = opts[:replica_count] || 2
@arbiter_count = opts[:arbiter_count] || 2
@passive_count = opts[:passive_count] || 1
check_member_count
if !@working_dir
raise ArgumentError, "A working directory must be specified"
end
@data_path = opts.fetch(:path, File.join(@working_dir, "data"))
@pidlistfile = File.join(@working_dir, "mink.pidlist")
@ports = []
@mongods = []
@pids = []
@config = {"_id" => @name, "members" => []}
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def host
@host
end
|
#mongods ⇒ Object
Returns the value of attribute mongods.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def mongods
@mongods
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def name
@name
end
|
#pids ⇒ Object
Returns the value of attribute pids.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def pids
@pids
end
|
#ports ⇒ Object
Returns the value of attribute ports.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def ports
@ports
end
|
#start_port ⇒ Object
Returns the value of attribute start_port.
5
6
7
|
# File 'lib/mink/managers/repl_set_manager.rb', line 5
def start_port
@start_port
end
|
Instance Method Details
#arbiters ⇒ Object
179
180
181
|
# File 'lib/mink/managers/repl_set_manager.rb', line 179
def arbiters
get_all_host_pairs_with_state(7)
end
|
#cleanup_set ⇒ Object
64
65
66
67
68
69
|
# File 'lib/mink/managers/repl_set_manager.rb', line 64
def cleanup_set
system("killall mongod")
@mongods.each do |mongod|
system("rm -rf #{mongod['db_path']}")
end
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/mink/managers/repl_set_manager.rb', line 71
def configure_node(n)
@mongods[n] ||= {}
port = @start_port + n
@ports << port
@mongods[n]['port'] = port
@mongods[n]['db_path'] = get_path("#{port}.data")
@mongods[n]['log_path'] = get_path("#{port}.log")
@mongods[n]['start'] = start_cmd(n)
member = {'_id' => n, 'host' => "#{@host}:#{@mongods[n]['port']}"}
if block_given?
custom_attrs = {}
yield custom_attrs
member.merge!(custom_attrs)
@mongods[n].merge!(custom_attrs)
end
@config['members'] << member
end
|
#ensure_up ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/mink/managers/repl_set_manager.rb', line 153
def ensure_up
print "[RS #{@name}] Ensuring members are up...\n"
attempt do
con = get_connection
status = con['admin'].command({'replSetGetStatus' => 1})
print "."
if status['members'].all? { |m| m['health'] == 1 && [1, 2, 7].include?(m['state']) } &&
status['members'].any? { |m| m['state'] == 1 }
print "all members up!\n\n"
return status
else
raise Mongo::OperationFailure
end
end
end
|
#get_manual_conf ⇒ Object
195
196
|
# File 'lib/mink/managers/repl_set_manager.rb', line 195
def get_manual_conf
end
|
#get_node_from_port(port) ⇒ Object
141
142
143
|
# File 'lib/mink/managers/repl_set_manager.rb', line 141
def get_node_from_port(port)
@mongods.detect { |mongod| mongod['port'] == port }
end
|
#kill(node, signal = 2) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/mink/managers/repl_set_manager.rb', line 100
def kill(node, signal=2)
pid = @mongods[node]['pid']
puts "** Killing node with pid #{pid} at port #{@mongods[node]['port']}"
system("kill -#{signal} #{@mongods[node]['pid']}")
@mongods[node]['up'] = false
sleep(1)
end
|
#kill_primary(signal = 2) ⇒ Object
108
109
110
111
112
|
# File 'lib/mink/managers/repl_set_manager.rb', line 108
def kill_primary(signal=2)
node = get_node_with_state(1)
kill(node, signal)
return node
end
|
#kill_secondary ⇒ Object
123
124
125
126
127
|
# File 'lib/mink/managers/repl_set_manager.rb', line 123
def kill_secondary
node = get_node_with_state(2)
kill(node)
return node
end
|
#primary ⇒ Object
170
171
172
173
|
# File 'lib/mink/managers/repl_set_manager.rb', line 170
def primary
nodes = get_all_host_pairs_with_state(1)
nodes.empty? ? nil : nodes[0]
end
|
#restart_killed_nodes ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/mink/managers/repl_set_manager.rb', line 129
def restart_killed_nodes
nodes = @mongods.select do |mongods|
@mongods['up'] == false
end
nodes.each do |node|
start(node)
end
ensure_up
end
|
#secondaries ⇒ Object
175
176
177
|
# File 'lib/mink/managers/repl_set_manager.rb', line 175
def secondaries
get_all_host_pairs_with_state(2)
end
|
#shard_string ⇒ Object
String used for adding a shard via mongos using the addshard command.
185
186
187
188
189
190
191
192
193
|
# File 'lib/mink/managers/repl_set_manager.rb', line 185
def shard_string
str = "#{@name}/"
str << @mongods.select do |mongod|
!mongod['arbiterOnly'] && mongod['priority'] != 0
end.map do |mongod|
"#{@host}:#{mongod['port']}"
end.join(',')
str
end
|
#start(node) ⇒ Object
Also known as:
restart
145
146
147
148
149
150
|
# File 'lib/mink/managers/repl_set_manager.rb', line 145
def start(node)
system(@mongods[node]['start'])
@mongods[node]['up'] = true
sleep(0.5)
@mongods[node]['pid'] = File.open(File.join(@mongods[node]['db_path'], 'mongod.lock')).read.strip
end
|
#start_cmd(n) ⇒ Object
93
94
95
96
97
98
|
# File 'lib/mink/managers/repl_set_manager.rb', line 93
def start_cmd(n)
@mongods[n]['start'] = "#{@mongod_path} --replSet #{@name} --logpath '#{@mongods[n]['log_path']}' " +
" --dbpath #{@mongods[n]['db_path']} --port #{@mongods[n]['port']} --fork"
@mongods[n]['start'] += " --dur" if @durable
@mongods[n]['start']
end
|
#start_set ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/mink/managers/repl_set_manager.rb', line 34
def start_set
puts "** Starting a replica set with #{@count} nodes"
kill_existing_mongods
n = 0
@replica_count.times do |n|
configure_node(n)
n += 1
end
@passive_count.times do
configure_node(n) do |attrs|
attrs['priority'] = 0
end
n += 1
end
@arbiter_count.times do
configure_node(n) do |attrs|
attrs['arbiterOnly'] = true
end
n += 1
end
write_conf if @write_conf
startup_mongods
initiate_repl_set
ensure_up
end
|
#step_down_primary ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/mink/managers/repl_set_manager.rb', line 114
def step_down_primary
primary = get_node_with_state(1)
con = get_connection(primary)
begin
con['admin'].command({'replSetStepDown' => 90})
rescue Mongo::ConnectionFailure
end
end
|
#write_conf(filename = nil) ⇒ Object
198
199
|
# File 'lib/mink/managers/repl_set_manager.rb', line 198
def write_conf(filename=nil)
end
|