Skip to content

Commit 1e1387b

Browse files
authored
fix: increase attempt count to measure latency (#103)
* fix: increase attempt count to measure latency * test: prevent freaky failure for CI that occurring replication delay
1 parent 777b33e commit 1e1387b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/redis_client/cluster/node/latency_replica.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LatencyReplica
1111
attr_reader :replica_clients
1212

1313
DUMMY_LATENCY_SEC = 100.0
14+
MEASURE_ATTEMPT_COUNT = 10
1415

1516
def initialize(replications, options, pool, **kwargs)
1617
super
@@ -37,17 +38,21 @@ def any_replica_node_key(seed: nil)
3738

3839
private
3940

40-
def measure_latencies(clients) # rubocop:disable Metrics/MethodLength
41+
def measure_latencies(clients) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
4142
latencies = {}
4243

4344
clients.each_slice(::RedisClient::Cluster::Node::MAX_THREADS) do |chuncked_clients|
4445
threads = chuncked_clients.map do |k, v|
4546
Thread.new(k, v) do |node_key, client|
4647
Thread.pass
47-
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
48-
client.send(:call_once, 'PING')
49-
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50-
latencies[node_key] = ending - starting
48+
min = DUMMY_LATENCY_SEC + 1.0
49+
MEASURE_ATTEMPT_COUNT.times do
50+
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
51+
client.send(:call_once, 'PING')
52+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting
53+
min = duration if duration < min
54+
end
55+
latencies[node_key] = min
5156
rescue StandardError
5257
latencies[node_key] = DUMMY_LATENCY_SEC
5358
end

test/redis_client/test_cluster.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def test_dedicated_commands
208208

209209
def test_compatibility_with_redis_gem
210210
assert_equal('OK', @client.set('foo', 100))
211+
wait_for_replication
211212
assert_equal('100', @client.get('foo'))
212213
assert_raises(NoMethodError) { @client.densaugeo('1m') }
213214
end

0 commit comments

Comments
 (0)