Skip to content

Commit 7fcefea

Browse files
axos88djanowski
authored andcommitted
Added collision detection and test for adding two nodes with colliding CRCs (or the same node twice)
See #354
1 parent 3d475b2 commit 7fcefea

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/redis/hash_ring.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def add_node(node)
2525
@nodes << node
2626
@replicas.times do |i|
2727
key = Zlib.crc32("#{node.id}:#{i}")
28+
raise "Node ID collision" if @ring.has_key?(key)
2829
@ring[key] = node
2930
@sorted_keys << key
3031
end

test/distributed_internals_test.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class TestDistributedInternals < Test::Unit::TestCase
77
include Helper::Distributed
88

99
def test_provides_a_meaningful_inspect
10-
nodes = ["redis://127.0.0.1:#{PORT}/15", *NODES]
10+
nodes = ["redis://localhost:#{PORT}/15", *NODES]
1111
redis = Redis::Distributed.new nodes
1212

1313
assert_equal "#<Redis client v#{Redis::VERSION} for #{redis.nodes.map(&:id).join(', ')}>", redis.inspect
1414
end
1515

1616
def test_default_as_urls
17-
nodes = ["redis://127.0.0.1:#{PORT}/15", *NODES]
17+
nodes = ["redis://localhost:#{PORT}/15", *NODES]
1818
redis = Redis::Distributed.new nodes
19-
assert_equal ["redis://127.0.0.1:#{PORT}/15", *NODES], redis.nodes.map { |node| node.client.id}
19+
assert_equal ["redis://localhost:#{PORT}/15", *NODES], redis.nodes.map { |node| node.client.id}
2020
end
2121

2222
def test_default_as_config_hashes
@@ -67,4 +67,13 @@ def test_keeps_options_after_dup
6767

6868
assert_equal [], r2.sinter("baz:foo", "baz:bar")
6969
end
70+
71+
def test_colliding_node_ids
72+
nodes = ["redis://localhost:#{PORT}/15", "redis://localhost:#{PORT}/15", *NODES]
73+
74+
assert_raise(RuntimeError) do
75+
redis = Redis::Distributed.new nodes
76+
end
77+
end
78+
7079
end

0 commit comments

Comments
 (0)