@@ -21,7 +21,6 @@ def rebuild
21
21
replicate ( @clients )
22
22
save_config ( @clients )
23
23
wait_cluster_building ( @clients )
24
- sleep 3
25
24
end
26
25
27
26
def down
@@ -30,8 +29,9 @@ def down
30
29
end
31
30
32
31
def failover
33
- take_slaves ( @clients ) . last . cluster ( :failover , :takeover )
34
- sleep 3
32
+ master , slave = take_replication_pairs ( @clients )
33
+ slave . cluster ( :failover , :takeover )
34
+ wait_failover ( to_node_key ( master ) , to_node_key ( slave ) , @clients )
35
35
end
36
36
37
37
def start_resharding ( slot , src_node_key , dest_node_key )
@@ -157,20 +157,44 @@ def save_config(clients)
157
157
clients . each { |c | c . cluster ( :saveconfig ) }
158
158
end
159
159
160
- def wait_cluster_building ( clients )
161
- first_cliient = clients . first
160
+ def wait_cluster_building ( clients , max_attempts : 200 )
161
+ attempt_count = 0
162
162
163
- loop do
164
- info = hashify_cluster_info ( first_cliient )
165
- break if info [ 'cluster_state' ] == 'ok'
166
- sleep 0.1
163
+ clients . each do |client |
164
+ loop do
165
+ info = hashify_cluster_info ( client )
166
+ attempt_count += 1
167
+ break if info [ 'cluster_state' ] == 'ok' || attempt_count > max_attempts
168
+ sleep 0.1
169
+ end
170
+ end
171
+ end
172
+
173
+ def wait_failover ( master_key , slave_key , clients , max_attempts : 200 )
174
+ attempt_count = 0
175
+
176
+ clients . each do |client |
177
+ loop do
178
+ flags = hashify_cluster_node_flags ( client )
179
+ attempt_count += 1
180
+ break if ( flags [ master_key ] == 'slave' && flags [ slave_key ] == 'master' ) || attempt_count > max_attempts
181
+ sleep 0.1
182
+ end
167
183
end
168
184
end
169
185
170
186
def hashify_cluster_info ( client )
171
187
client . cluster ( :info ) . split ( "\r \n " ) . map { |str | str . split ( ':' ) } . to_h
172
188
end
173
189
190
+ def hashify_cluster_node_flags ( client )
191
+ client . cluster ( :nodes )
192
+ . split ( "\n " )
193
+ . map { |str | str . split ( ' ' ) }
194
+ . map { |arr | [ arr [ 1 ] . split ( '@' ) . first , ( arr [ 2 ] . split ( ',' ) & %w[ master slave ] ) . first ] }
195
+ . to_h
196
+ end
197
+
174
198
def hashify_node_map ( client )
175
199
client . cluster ( :nodes )
176
200
. split ( "\n " )
@@ -191,10 +215,16 @@ def take_slaves(clients)
191
215
clients [ size ..size * 2 ]
192
216
end
193
217
218
+ def take_replication_pairs ( clients )
219
+ [ take_masters ( clients ) . last , take_slaves ( clients ) . last ]
220
+ end
221
+
194
222
def find_client ( clients , node_key )
195
- clients . find do |cli |
196
- con = cli . connection
197
- node_key == "#{ con . fetch ( :host ) } :#{ con . fetch ( :port ) } "
198
- end
223
+ clients . find { |cli | node_key == to_node_key ( cli ) }
224
+ end
225
+
226
+ def to_node_key ( client )
227
+ con = client . connection
228
+ "#{ con . fetch ( :host ) } :#{ con . fetch ( :port ) } "
199
229
end
200
230
end
0 commit comments