Skip to content

RUBY-899 Cluster returns empty list if there are no servers #607

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 22, 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/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def scan!
#
# @since 2.0.0
def servers
topology.servers(@servers)
topology.servers(@servers.compact).compact
end

# Create a cluster for the provided client, for use when we don't want the
Expand Down
1 change: 1 addition & 0 deletions lib/mongo/cluster/topology/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def replica_set_name; nil; end
#
# @since 2.0.0
def servers(servers)
[]
end

# An unknown topology is not sharded.
Expand Down
2 changes: 2 additions & 0 deletions lib/mongo/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Error < StandardError
require 'mongo/error/invalid_file'
require 'mongo/error/invalid_nonce'
require 'mongo/error/invalid_replacement_document'
require 'mongo/error/invalid_server_preference'
require 'mongo/error/invalid_signature'
require 'mongo/error/invalid_update_document'
require 'mongo/error/invalid_uri'
Expand All @@ -79,6 +80,7 @@ class Error < StandardError
require 'mongo/error/max_message_size'
require 'mongo/error/multi_index_drop'
require 'mongo/error/need_primary_server'
require 'mongo/error/no_server_available'
require 'mongo/error/socket_error'
require 'mongo/error/socket_timeout_error'
require 'mongo/error/unsupported_features'
36 changes: 36 additions & 0 deletions lib/mongo/error/invalid_server_preference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2014-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Error

# Raised when an invalid server preference is provided.
#
# @since 2.0.0
class InvalidServerPreference < Error

# Instantiate the new exception.
#
# @example Instantiate the exception.
# Mongo::ServerSelector::InvalidServerPreference.new
#
# @param [ String ] name The preference name.
#
# @since 2.0.0
def initialize(name)
super("This server preference #{name} cannot be combined with tags.")
end
end
end
end
37 changes: 37 additions & 0 deletions lib/mongo/error/no_server_available.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (C) 2014-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Error

# Raised if there are no servers available matching the preference.
#
# @since 2.0.0
class NoServerAvailable < Error

# Instantiate the new exception.
#
# @example Instantiate the exception.
# Mongo::Error::NoServerAvailable.new(server_selector)
#
# @param [ Hash ] server_selector The server preference that could not be
# satisfied.
#
# @since 2.0.0
def initialize(server_selector)
super("No server is available matching preference: #{server_selector.inspect}")
end
end
end
end
19 changes: 0 additions & 19 deletions lib/mongo/server_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,5 @@ def get(preference = {}, options = {})
options
)
end

# Exception raised if there are no servers available matching the preference.
#
# @since 2.0.0
class NoServerAvailable < Error

# Instantiate the new exception.
#
# @example Instantiate the exception.
# Mongo::ServerSelector::NoServerAvailable.new(server_selector)
#
# @param [ Hash ] server_selector The server preference that could not be
# satisfied.
#
# @since 2.0.0
def initialize(server_selector)
super("No server is available matching preference: #{server_selector.inspect}")
end
end
end
end
22 changes: 2 additions & 20 deletions lib/mongo/server_selector/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def ==(other)
# @since 2.0.0
def initialize(tag_sets = [], options = {})
if !tag_sets.all? { |set| set.empty? } && !tags_allowed?
raise ServerSelector::InvalidServerPreference.new(name)
raise Error::InvalidServerPreference.new(name)
end
@tag_sets = tag_sets
@options = options
Expand Down Expand Up @@ -96,7 +96,7 @@ def select_server(cluster)
return servers.first if servers && !servers.compact.empty?
cluster.scan!
end
raise NoServerAvailable.new(self)
raise Error::NoServerAvailable.new(self)
end

# Get the timeout for server selection.
Expand Down Expand Up @@ -187,23 +187,5 @@ def match_tag_sets(candidates)
matches || []
end
end

# Raised when an invalid server preference is provided.
#
# @since 2.0.0
class InvalidServerPreference < Error

# Instantiate the new exception.
#
# @example Instantiate the exception.
# Mongo::ServerSelector::InvalidServerPreference.new
#
# @param [ String ] name The preference name.
#
# @since 2.0.0
def initialize(name)
super("This server preference #{name} cannot be combined with tags.")
end
end
end
end
60 changes: 60 additions & 0 deletions spec/mongo/cluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,65 @@
end
end
end

context 'when the cluster has no servers' do

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

let(:servers) do
[nil]
end

before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end

context 'when topology is Single' do

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

it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end

context 'when topology is ReplicaSet' do

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

it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end

context 'when topology is Sharded' do

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

it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end

context 'when topology is Unknown' do

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

it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/mongo/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
it 'uses that read preference', unless: sharded? do
expect do
database.command({ ping: 1 }, { read: read })
end.to raise_error(Mongo::ServerSelector::NoServerAvailable)
end.to raise_error(Mongo::Error::NoServerAvailable)
end
end
end
Expand Down
15 changes: 3 additions & 12 deletions spec/mongo/server_selection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
allow(cluster).to receive(:scan!).and_return(true)
end

context 'Valid read preference and matching server available', unless: spec.raises_exception? do
context 'Valid read preference and matching server available', if: spec.server_available? do

it 'Finds all suitable servers in the latency window', if: spec.replica_set? do
expect(server_selector.send(:select, cluster.servers)).to eq(in_latency_window)
Expand All @@ -72,21 +72,12 @@
end
end

context 'Invalid read preference', if: spec.invalid_server_preference? do
context 'No matching server available', if: !spec.server_available? do

it 'Raises exception' do
expect do
server_selector.select_server(cluster)
end.to raise_exception(Mongo::ServerSelector::InvalidServerPreference)
end
end

context 'No matching server available', unless: spec.server_available? do

it 'Raises exception' do
expect do
server_selector.select_server(cluster)
end.to raise_exception(Mongo::ServerSelector::NoServerAvailable)
end.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
end
Expand Down
53 changes: 52 additions & 1 deletion spec/mongo/server_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,59 @@
it 'raises a NoServerAvailable error' do
expect do
read_pref.select_server(cluster)
end.to raise_exception(Mongo::ServerSelector::NoServerAvailable)
end.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
end

shared_context 'a ServerSelector' do

context 'when cluster#servers is empty' do

let(:servers) { [] }

let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:servers).and_return(servers)
allow(c).to receive(:single?).and_return(single)
allow(c).to receive(:sharded?).and_return(sharded)
allow(c).to receive(:scan!).and_return(true)
end
end

let(:read_pref) do
described_class.get({ mode: :primary }, server_selection_timeout: 1)
end

it 'raises a NoServerAvailable error' do
expect do
read_pref.select_server(cluster)
end.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
end

context 'when the cluster has a Single topology' do

let(:single) { true }
let(:sharded) { false }

it_behaves_like 'a ServerSelector'
end

context 'when the cluster has a ReplicaSet topology' do

let(:single) { false }
let(:sharded) { false }

it_behaves_like 'a ServerSelector'
end

context 'when the cluster has a Sharded topology' do

let(:single) { false }
let(:sharded) { true }

it_behaves_like 'a ServerSelector'
end
end
24 changes: 0 additions & 24 deletions spec/support/server_selection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ def replica_set?
type == Mongo::Cluster::Topology::ReplicaSet
end

# Does this spec raise an exception.
#
# @example Determine if the spec raises an exception.
# spec.raises_exception?
#
# @return [true, false] If the spec raises an exception.
#
# @since 2.0.0
def raises_exception?
!server_available? || invalid_server_preference?
end

# Does this spec expect a server to be found.
#
# @example Will a server be found with this spec.
Expand All @@ -116,18 +104,6 @@ def server_available?
!in_latency_window.empty?
end

# Is the read preference defined in the spec invalid.
#
# @example Determine if the spec's read preference is invalid.
# spec.invalid_server_preference?
#
# @return [true, false] If the spec's read preference is invalid.
#
# @since 2.0.0
def invalid_server_preference?
read_preference['mode'] == 'Primary' && read_preference['tag_sets']
end

# The subset of suitable servers that falls within the allowable latency
# window.
# We have to correct for our server selection algorithm that adds the primary
Expand Down