Skip to content

RUBY-949 Don't set slave_ok flag if server is a mongos #639

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 1 commit into from
Jun 23, 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
6 changes: 5 additions & 1 deletion lib/mongo/operation/read_preferrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ def update_selector(context)
end
end

def slave_ok?(context)
!context.mongos? && (context.cluster.single? || read.slave_ok?)
end

def update_options(context)
if context.cluster.single? || (!context.mongos? && read.slave_ok?)
if slave_ok?(context)
options.dup.tap do |opts|
(opts[:flags] ||= []) << SLAVE_OK
end
Expand Down
119 changes: 82 additions & 37 deletions spec/mongo/operation/read_preferrable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
{}
end

let(:mongos) do
false
let(:cluster_double) do
double('cluster')
end

let(:slave_ok) do
let(:single?) do
true
end

let(:mongos?) do
false
end

let(:read_pref) do
Mongo::ServerSelector.get
end

let(:read_preferrable) do
Class.new do
include Mongo::Operation::ReadPreferrable
Expand All @@ -28,15 +36,11 @@
end
end

let(:cluster_double) do
double('cluster')
end

let(:context) do
double('context').tap do |c|
allow(c).to receive(:cluster).and_return(cluster_double)
allow(cluster_double).to receive(:single?).and_return(slave_ok)
allow(c).to receive(:mongos?).and_return(mongos)
allow(cluster_double).to receive(:single?).and_return(single?)
allow(c).to receive(:mongos?).and_return(mongos?)
end
end

Expand Down Expand Up @@ -65,7 +69,7 @@
selector.merge(:$readPreference => read_pref.to_mongos)
end

it 'returns a special selector' do
it 'returns an unaltered special selector' do
expect(read_preferrable.send(:update_selector, context)).to eq(expected)
end
end
Expand All @@ -77,14 +81,14 @@
Mongo::ServerSelector.get(:mode => mode)
end

it 'returns a special selector' do
it 'returns a selector' do
expect(read_preferrable.send(:update_selector, context)).to eq(selector)
end
end

context 'when the server is a mongos' do

let(:mongos) do
let(:mongos?) do
true
end

Expand Down Expand Up @@ -136,38 +140,27 @@

context 'when the server is not a mongos' do

let(:mongos?) do
false
end

let(:mode) do
:secondary_preferred
end

it_behaves_like 'not a selector updater'
end

context 'when the server context requires the slaveOk bit to be set' do

let(:read_pref) do
Mongo::ServerSelector.get(:mode => :secondary)
end

let(:expected) do
{ :flags => [ :slave_ok ] }
end

let(:slave_ok) do
true
end
context 'when the server is not a mongos' do

it 'sets the slave_ok flag' do
expect(read_preferrable.send(:update_options, context)).to eq(expected)
let(:mongos?) do
false
end
end

context 'when the server is not a mongos' do

context 'when the read preference requires the slaveOk bit to be set' do
context 'when the topology is Single' do

let(:read_pref) do
Mongo::ServerSelector.get(:mode => :secondary)
let(:single?) do
true
end

let(:expected) do
Expand All @@ -179,19 +172,71 @@
end
end

context 'when the read preference does not require the slaveOk bit to be set' do
context 'when the topology is not Single' do

let(:read_pref) do
Mongo::ServerSelector.get(:mode => :primary)
let(:single?) do
false
end

context 'when the read preference requires the slave_ok flag' do

let(:read_pref) do
Mongo::ServerSelector.get(:mode => :secondary)
end

let(:expected) do
{ :flags => [ :slave_ok ] }
end

it 'sets the slave_ok flag' do
expect(read_preferrable.send(:update_options, context)).to eq(expected)
end
end

context 'when the read preference does not require the slave_ok flag' do

let(:read_pref) do
Mongo::ServerSelector.get(:mode => :primary)
end

let(:expected) do
{ }
end

it 'does not set the slave_ok flag' do
expect(read_preferrable.send(:update_options, context)).to eq(expected)
end
end
end

context 'when the topology is Single' do

let(:single?) do
true
end

let(:expected) do
{ }
{ :flags => [ :slave_ok ] }
end

it 'sets the slave_ok flag' do
expect(read_preferrable.send(:update_options, context)).to eq(expected)
end
end
end

context 'when the server is a mongos' do

let(:mongos?) do
true
end

let(:expected) do
{ }
end

it 'does not set the slave_ok flag' do
expect(read_preferrable.send(:update_options, context)).to eq(expected)
end
end
end