Skip to content

RUBY-900 Don't add discovered servers to the cluster with Single topology #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongo/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Address
::Socket::AF_INET => IPv4
}

# @return [ String ] address The seed address.
# @return [ String ] seed The seed address.
attr_reader :seed

# @return [ String ] host The original host name.
Expand Down
8 changes: 7 additions & 1 deletion lib/mongo/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def ==(other)
# @since 2.0.0
def add(host)
address = Address.new(host)
unless addresses.include?(address)
if !addresses.include?(address) || direct_connection?(address)
log_debug([ "Adding #{address.to_s} to the cluster." ])
addresses.push(address)
server = Server.new(address, event_listeners,
Expand Down Expand Up @@ -201,5 +201,11 @@ def self.create(client)
cluster = Cluster.new(client.cluster.addresses.map(&:to_s), client.options)
client.instance_variable_set(:@cluster, cluster)
end

private

def direct_connection?(address)
@topology.single? && address.seed == @topology.seed
end
end
end
6 changes: 3 additions & 3 deletions lib/mongo/cluster/topology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ module Topology
# @since 2.0.0
def initial(seeds, options)
if options.has_key?(:connect)
return OPTIONS.fetch(options[:connect]).new(options)
return OPTIONS.fetch(options[:connect]).new(options, seeds)
end
if options.has_key?(:replica_set)
ReplicaSet.new(options)
ReplicaSet.new(options, seeds)
else
seeds.size > 1 ? Unknown.new(options) : Single.new(options)
seeds.size > 1 ? Unknown.new(options, seeds) : Single.new(options, seeds)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/cluster/topology/replica_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def elect_primary(description, servers)
# @param [ Hash ] options The options.
#
# @since 2.0.0
def initialize(options)
def initialize(options, seeds = [])
@options = options
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/cluster/topology/sharded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def elect_primary(description, servers); self; end
# @param [ Hash ] options The options.
#
# @since 2.0.0
def initialize(options)
def initialize(options, seeds = [])
@options = options
end

Expand Down
6 changes: 5 additions & 1 deletion lib/mongo/cluster/topology/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ module Topology
# @since 2.0.0
class Single

# @return [ String ] seed The seed address.
attr_reader :seed

# The display name for the topology.
#
# @since 2.0.0
Expand Down Expand Up @@ -59,8 +62,9 @@ def elect_primary(description, servers); self; end
# @param [ Hash ] options The options.
#
# @since 2.0.0
def initialize(options)
def initialize(options, seeds = [])
@options = options
@seed = seeds.first
end

# A single topology is not a replica set.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/cluster/topology/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def elect_primary(description, servers)
# @param [ Hash ] options The options.
#
# @since 2.0.0
def initialize(options)
def initialize(options, seeds = [])
@options = options
end

Expand Down
4 changes: 4 additions & 0 deletions spec/mongo/cluster/topology_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
it 'returns a single topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
end

it 'sets the seed on the topology' do
expect(topology.seed).to eq('a')
end
end

context 'when provided a sharded option' do
Expand Down
48 changes: 25 additions & 23 deletions spec/mongo/cluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe Mongo::Cluster do

describe '#==' do
let(:cluster) do
described_class.new(ADDRESSES)
end

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ])
end
describe '#==' do

context 'when the other is a cluster' do

Expand Down Expand Up @@ -61,10 +61,6 @@
Mongo::ServerSelector.get
end

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ])
end

it 'displays the cluster seeds and topology' do
expect(cluster.inspect).to include('topology')
expect(cluster.inspect).to include('servers')
Expand All @@ -78,13 +74,13 @@
end

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ], :replica_set => 'testing')
described_class.new(ADDRESSES, :replica_set => 'testing')
end

context 'when the option is provided' do

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ], :replica_set => 'testing')
described_class.new(ADDRESSES, :replica_set => 'testing')
end

it 'returns the name' do
Expand All @@ -95,7 +91,7 @@
context 'when the option is not provided' do

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ])
described_class.new(ADDRESSES)
end

it 'returns nil' do
Expand All @@ -110,10 +106,6 @@
Mongo::ServerSelector.get
end

let(:cluster) do
described_class.new([ '127.0.0.1:27017' ])
end

let(:known_servers) do
cluster.instance_variable_get(:@servers)
end
Expand All @@ -131,10 +123,6 @@

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

let(:cluster) do
described_class.new(ADDRESSES)
end

context 'when the server is a mongos', if: single_mongos? do

it 'returns the mongos' do
Expand All @@ -152,10 +140,6 @@

context 'when the cluster has no servers' do

let(:cluster) do
described_class.new(ADDRESSES)
end

let(:servers) do
[nil]
end
Expand Down Expand Up @@ -210,4 +194,22 @@
end
end
end

describe '#add' do

context 'when topology is Single' do

let(:topology) do
Mongo::Cluster::Topology::Single.new({})
end

before do
cluster.add('a')
end

it 'does not add discovered servers to the cluster' do
expect(cluster.servers[0].address.seed).to_not eq('a')
end
end
end
end