Skip to content

Commit 591f619

Browse files
committed
Fix cluster abnormal test cases that occasionally fail on CI. Use sure waiting logic instead of unstable workaround sleep.
1 parent 1453d1b commit 591f619

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

test/support/cluster/orchestrator.rb

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def rebuild
2121
replicate(@clients)
2222
save_config(@clients)
2323
wait_cluster_building(@clients)
24-
sleep 3
2524
end
2625

2726
def down
@@ -30,8 +29,9 @@ def down
3029
end
3130

3231
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)
3535
end
3636

3737
def start_resharding(slot, src_node_key, dest_node_key)
@@ -157,20 +157,44 @@ def save_config(clients)
157157
clients.each { |c| c.cluster(:saveconfig) }
158158
end
159159

160-
def wait_cluster_building(clients)
161-
first_cliient = clients.first
160+
def wait_cluster_building(clients, max_attempts: 200)
161+
attempt_count = 0
162162

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
167183
end
168184
end
169185

170186
def hashify_cluster_info(client)
171187
client.cluster(:info).split("\r\n").map { |str| str.split(':') }.to_h
172188
end
173189

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+
174198
def hashify_node_map(client)
175199
client.cluster(:nodes)
176200
.split("\n")
@@ -191,10 +215,16 @@ def take_slaves(clients)
191215
clients[size..size * 2]
192216
end
193217

218+
def take_replication_pairs(clients)
219+
[take_masters(clients).last, take_slaves(clients).last]
220+
end
221+
194222
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)}"
199229
end
200230
end

0 commit comments

Comments
 (0)