Skip to content

Commit 022906d

Browse files
committed
Slight refactorig of prior commit. Main goal was to preserve ordering of hosts on the
token ring; Set.to_a returns values in an "uncertain" order so we attempt to avoid it all together. Beneficial side effect of this change is that the impl moves to methods on Enumerable.
1 parent 42c435d commit 022906d

File tree

2 files changed

+12
-12
lines changed
  • lib/cassandra/cluster/schema/replication_strategies
  • spec/cassandra/cluster/schema/replication_strategies

2 files changed

+12
-12
lines changed

lib/cassandra/cluster/schema/replication_strategies/simple.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ def replication_map(token_hosts, token_ring, replication_options)
2929
factor = size if size < factor
3030
replication_map = ::Hash.new
3131

32+
hosts_ring = token_ring.map { |t| token_hosts[t] }
3233
token_ring.each_with_index do |token, i|
33-
replicas = ::Set.new
34-
size.times do |j|
35-
replicas << token_hosts[token_ring[(i + j) % size]]
36-
break if replicas.size == factor
37-
end
38-
replication_map[token] = replicas.to_a.freeze
34+
candidates = hosts_ring.cycle(2 * size).drop(i).take(size).uniq
35+
replication_map[token] = candidates.take(factor).freeze
3936
end
4037

4138
replication_map

spec/cassandra/cluster/schema/replication_strategies/simple_spec.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ module ReplicationStrategies
6363

6464
it do
6565
replication_map = subject.replication_map(token_hosts, token_ring, replication_options)
66-
expect(replication_map[token_ring[0]].sort_by(&:ip).map do |host|
67-
[host.ip]
68-
end).to eq([
69-
['127.0.0.1'],
70-
['127.0.0.2'],
71-
['127.0.0.3'],
66+
expect(replication_map[token_ring[0]].map(&:ip)).to eq([
67+
'127.0.0.1',
68+
'127.0.0.2',
69+
'127.0.0.3',
70+
])
71+
expect(replication_map[token_ring[2]].map(&:ip)).to eq([
72+
'127.0.0.2',
73+
'127.0.0.3',
74+
'127.0.0.1',
7275
])
7376
end
7477
end

0 commit comments

Comments
 (0)