Skip to content

Commit 2f38db3

Browse files
committed
Merge pull request #609 from estolfo/RUBY-900-single-top
RUBY-900 Don't add discovered servers to the cluster with Single topology
2 parents 802b208 + b4c4a89 commit 2f38db3

File tree

9 files changed

+48
-32
lines changed

9 files changed

+48
-32
lines changed

lib/mongo/address.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Address
3333
::Socket::AF_INET => IPv4
3434
}
3535

36-
# @return [ String ] address The seed address.
36+
# @return [ String ] seed The seed address.
3737
attr_reader :seed
3838

3939
# @return [ String ] host The original host name.

lib/mongo/cluster.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def ==(other)
6666
# @since 2.0.0
6767
def add(host)
6868
address = Address.new(host)
69-
unless addresses.include?(address)
69+
if !addresses.include?(address) || direct_connection?(address)
7070
log_debug([ "Adding #{address.to_s} to the cluster." ])
7171
addresses.push(address)
7272
server = Server.new(address, event_listeners,
@@ -201,5 +201,11 @@ def self.create(client)
201201
cluster = Cluster.new(client.cluster.addresses.map(&:to_s), client.options)
202202
client.instance_variable_set(:@cluster, cluster)
203203
end
204+
205+
private
206+
207+
def direct_connection?(address)
208+
@topology.single? && address.seed == @topology.seed
209+
end
204210
end
205211
end

lib/mongo/cluster/topology.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ module Topology
4848
# @since 2.0.0
4949
def initial(seeds, options)
5050
if options.has_key?(:connect)
51-
return OPTIONS.fetch(options[:connect]).new(options)
51+
return OPTIONS.fetch(options[:connect]).new(options, seeds)
5252
end
5353
if options.has_key?(:replica_set)
54-
ReplicaSet.new(options)
54+
ReplicaSet.new(options, seeds)
5555
else
56-
seeds.size > 1 ? Unknown.new(options) : Single.new(options)
56+
seeds.size > 1 ? Unknown.new(options, seeds) : Single.new(options, seeds)
5757
end
5858
end
5959
end

lib/mongo/cluster/topology/replica_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def elect_primary(description, servers)
8282
# @param [ Hash ] options The options.
8383
#
8484
# @since 2.0.0
85-
def initialize(options)
85+
def initialize(options, seeds = [])
8686
@options = options
8787
end
8888

lib/mongo/cluster/topology/sharded.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def elect_primary(description, servers); self; end
5959
# @param [ Hash ] options The options.
6060
#
6161
# @since 2.0.0
62-
def initialize(options)
62+
def initialize(options, seeds = [])
6363
@options = options
6464
end
6565

lib/mongo/cluster/topology/single.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module Topology
2121
# @since 2.0.0
2222
class Single
2323

24+
# @return [ String ] seed The seed address.
25+
attr_reader :seed
26+
2427
# The display name for the topology.
2528
#
2629
# @since 2.0.0
@@ -59,8 +62,9 @@ def elect_primary(description, servers); self; end
5962
# @param [ Hash ] options The options.
6063
#
6164
# @since 2.0.0
62-
def initialize(options)
65+
def initialize(options, seeds = [])
6366
@options = options
67+
@seed = seeds.first
6468
end
6569

6670
# A single topology is not a replica set.

lib/mongo/cluster/topology/unknown.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def elect_primary(description, servers)
7070
# @param [ Hash ] options The options.
7171
#
7272
# @since 2.0.0
73-
def initialize(options)
73+
def initialize(options, seeds = [])
7474
@options = options
7575
end
7676

spec/mongo/cluster/topology_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
it 'returns a single topology' do
2525
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
2626
end
27+
28+
it 'sets the seed on the topology' do
29+
expect(topology.seed).to eq('a')
30+
end
2731
end
2832

2933
context 'when provided a sharded option' do

spec/mongo/cluster_spec.rb

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
describe Mongo::Cluster do
44

5-
describe '#==' do
5+
let(:cluster) do
6+
described_class.new(ADDRESSES)
7+
end
68

7-
let(:cluster) do
8-
described_class.new([ '127.0.0.1:27017' ])
9-
end
9+
describe '#==' do
1010

1111
context 'when the other is a cluster' do
1212

@@ -61,10 +61,6 @@
6161
Mongo::ServerSelector.get
6262
end
6363

64-
let(:cluster) do
65-
described_class.new([ '127.0.0.1:27017' ])
66-
end
67-
6864
it 'displays the cluster seeds and topology' do
6965
expect(cluster.inspect).to include('topology')
7066
expect(cluster.inspect).to include('servers')
@@ -78,13 +74,13 @@
7874
end
7975

8076
let(:cluster) do
81-
described_class.new([ '127.0.0.1:27017' ], :replica_set => 'testing')
77+
described_class.new(ADDRESSES, :replica_set => 'testing')
8278
end
8379

8480
context 'when the option is provided' do
8581

8682
let(:cluster) do
87-
described_class.new([ '127.0.0.1:27017' ], :replica_set => 'testing')
83+
described_class.new(ADDRESSES, :replica_set => 'testing')
8884
end
8985

9086
it 'returns the name' do
@@ -95,7 +91,7 @@
9591
context 'when the option is not provided' do
9692

9793
let(:cluster) do
98-
described_class.new([ '127.0.0.1:27017' ])
94+
described_class.new(ADDRESSES)
9995
end
10096

10197
it 'returns nil' do
@@ -110,10 +106,6 @@
110106
Mongo::ServerSelector.get
111107
end
112108

113-
let(:cluster) do
114-
described_class.new([ '127.0.0.1:27017' ])
115-
end
116-
117109
let(:known_servers) do
118110
cluster.instance_variable_get(:@servers)
119111
end
@@ -131,10 +123,6 @@
131123

132124
context 'when topology is single', if: single_seed? do
133125

134-
let(:cluster) do
135-
described_class.new(ADDRESSES)
136-
end
137-
138126
context 'when the server is a mongos', if: single_mongos? do
139127

140128
it 'returns the mongos' do
@@ -152,10 +140,6 @@
152140

153141
context 'when the cluster has no servers' do
154142

155-
let(:cluster) do
156-
described_class.new(ADDRESSES)
157-
end
158-
159143
let(:servers) do
160144
[nil]
161145
end
@@ -210,4 +194,22 @@
210194
end
211195
end
212196
end
197+
198+
describe '#add' do
199+
200+
context 'when topology is Single' do
201+
202+
let(:topology) do
203+
Mongo::Cluster::Topology::Single.new({})
204+
end
205+
206+
before do
207+
cluster.add('a')
208+
end
209+
210+
it 'does not add discovered servers to the cluster' do
211+
expect(cluster.servers[0].address.seed).to_not eq('a')
212+
end
213+
end
214+
end
213215
end

0 commit comments

Comments
 (0)