Skip to content

Commit 42c435d

Browse files
authored
Merge pull request #267 from abicky/fix-replication-strategies-simple
Fix ReplicationStrategies::Simple not to include duplicate hosts
1 parent 765f9c9 commit 42c435d

File tree

2 files changed

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

2 files changed

+85
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ def replication_map(token_hosts, token_ring, replication_options)
3030
replication_map = ::Hash.new
3131

3232
token_ring.each_with_index do |token, i|
33-
replication_map[token] = factor.times.map do |j|
34-
token_hosts[token_ring[(i + j) % size]]
35-
end.freeze
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
3639
end
3740

3841
replication_map
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# encoding: utf-8
2+
3+
#--
4+
# Copyright DataStax, Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#++
18+
19+
require 'spec_helper'
20+
21+
module Cassandra
22+
class Cluster
23+
class Schema
24+
module ReplicationStrategies
25+
describe(Simple) do
26+
subject { Simple.new }
27+
28+
describe('#replication_map') do
29+
let(:hosts) {
30+
[
31+
Host.new('127.0.0.1', 111, 'rack1', 'dc1'),
32+
Host.new('127.0.0.2', 112, 'rack1', 'dc1'),
33+
Host.new('127.0.0.3', 113, 'rack1', 'dc1'),
34+
]
35+
}
36+
37+
let(:token_hosts) {
38+
{
39+
-9000000000000000000 => hosts[0],
40+
-8000000000000000000 => hosts[0],
41+
-7000000000000000000 => hosts[1],
42+
-6000000000000000000 => hosts[1],
43+
-5000000000000000000 => hosts[2],
44+
-4000000000000000000 => hosts[2],
45+
-3000000000000000000 => hosts[0],
46+
-2000000000000000000 => hosts[0],
47+
-1000000000000000000 => hosts[1],
48+
+0000000000000000000 => hosts[1],
49+
+1000000000000000000 => hosts[2],
50+
+2000000000000000000 => hosts[2],
51+
}
52+
}
53+
54+
let(:token_ring) {
55+
token_hosts.keys
56+
}
57+
58+
let(:replication_options) {
59+
{
60+
'replication_factor' => 3,
61+
}
62+
}
63+
64+
it do
65+
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'],
72+
])
73+
end
74+
end
75+
end
76+
end
77+
end
78+
end
79+
end

0 commit comments

Comments
 (0)