Skip to content

Commit 01cf34d

Browse files
committed
RUBY-949 Don't set slave_ok flag if server is a mongos
1 parent 721ed56 commit 01cf34d

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed

lib/mongo/operation/read_preferrable.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ def update_selector(context)
3636
end
3737
end
3838

39+
def slave_ok?(context)
40+
!context.mongos? && (context.cluster.single? || read.slave_ok?)
41+
end
42+
3943
def update_options(context)
40-
if context.cluster.single? || (!context.mongos? && read.slave_ok?)
44+
if slave_ok?(context)
4145
options.dup.tap do |opts|
4246
(opts[:flags] ||= []) << SLAVE_OK
4347
end

spec/mongo/operation/read_preferrable_spec.rb

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@
1010
{}
1111
end
1212

13-
let(:mongos) do
14-
false
13+
let(:cluster_double) do
14+
double('cluster')
1515
end
1616

17-
let(:slave_ok) do
17+
let(:single?) do
18+
true
19+
end
20+
21+
let(:mongos?) do
1822
false
1923
end
2024

25+
let(:read_pref) do
26+
Mongo::ServerSelector.get
27+
end
28+
2129
let(:read_preferrable) do
2230
Class.new do
2331
include Mongo::Operation::ReadPreferrable
@@ -28,15 +36,11 @@
2836
end
2937
end
3038

31-
let(:cluster_double) do
32-
double('cluster')
33-
end
34-
3539
let(:context) do
3640
double('context').tap do |c|
3741
allow(c).to receive(:cluster).and_return(cluster_double)
38-
allow(cluster_double).to receive(:single?).and_return(slave_ok)
39-
allow(c).to receive(:mongos?).and_return(mongos)
42+
allow(cluster_double).to receive(:single?).and_return(single?)
43+
allow(c).to receive(:mongos?).and_return(mongos?)
4044
end
4145
end
4246

@@ -65,7 +69,7 @@
6569
selector.merge(:$readPreference => read_pref.to_mongos)
6670
end
6771

68-
it 'returns a special selector' do
72+
it 'returns an unaltered special selector' do
6973
expect(read_preferrable.send(:update_selector, context)).to eq(expected)
7074
end
7175
end
@@ -77,14 +81,14 @@
7781
Mongo::ServerSelector.get(:mode => mode)
7882
end
7983

80-
it 'returns a special selector' do
84+
it 'returns a selector' do
8185
expect(read_preferrable.send(:update_selector, context)).to eq(selector)
8286
end
8387
end
8488

8589
context 'when the server is a mongos' do
8690

87-
let(:mongos) do
91+
let(:mongos?) do
8892
true
8993
end
9094

@@ -136,38 +140,27 @@
136140

137141
context 'when the server is not a mongos' do
138142

143+
let(:mongos?) do
144+
false
145+
end
146+
139147
let(:mode) do
140148
:secondary_preferred
141149
end
142150

143151
it_behaves_like 'not a selector updater'
144152
end
145153

146-
context 'when the server context requires the slaveOk bit to be set' do
147-
148-
let(:read_pref) do
149-
Mongo::ServerSelector.get(:mode => :secondary)
150-
end
151-
152-
let(:expected) do
153-
{ :flags => [ :slave_ok ] }
154-
end
155-
156-
let(:slave_ok) do
157-
true
158-
end
154+
context 'when the server is not a mongos' do
159155

160-
it 'sets the slave_ok flag' do
161-
expect(read_preferrable.send(:update_options, context)).to eq(expected)
156+
let(:mongos?) do
157+
false
162158
end
163-
end
164-
165-
context 'when the server is not a mongos' do
166159

167-
context 'when the read preference requires the slaveOk bit to be set' do
160+
context 'when the topology is Single' do
168161

169-
let(:read_pref) do
170-
Mongo::ServerSelector.get(:mode => :secondary)
162+
let(:single?) do
163+
true
171164
end
172165

173166
let(:expected) do
@@ -179,19 +172,71 @@
179172
end
180173
end
181174

182-
context 'when the read preference does not require the slaveOk bit to be set' do
175+
context 'when the topology is not Single' do
183176

184-
let(:read_pref) do
185-
Mongo::ServerSelector.get(:mode => :primary)
177+
let(:single?) do
178+
false
179+
end
180+
181+
context 'when the read preference requires the slave_ok flag' do
182+
183+
let(:read_pref) do
184+
Mongo::ServerSelector.get(:mode => :secondary)
185+
end
186+
187+
let(:expected) do
188+
{ :flags => [ :slave_ok ] }
189+
end
190+
191+
it 'sets the slave_ok flag' do
192+
expect(read_preferrable.send(:update_options, context)).to eq(expected)
193+
end
194+
end
195+
196+
context 'when the read preference does not require the slave_ok flag' do
197+
198+
let(:read_pref) do
199+
Mongo::ServerSelector.get(:mode => :primary)
200+
end
201+
202+
let(:expected) do
203+
{ }
204+
end
205+
206+
it 'does not set the slave_ok flag' do
207+
expect(read_preferrable.send(:update_options, context)).to eq(expected)
208+
end
209+
end
210+
end
211+
212+
context 'when the topology is Single' do
213+
214+
let(:single?) do
215+
true
186216
end
187217

188218
let(:expected) do
189-
{ }
219+
{ :flags => [ :slave_ok ] }
190220
end
191221

192222
it 'sets the slave_ok flag' do
193223
expect(read_preferrable.send(:update_options, context)).to eq(expected)
194224
end
195225
end
196226
end
227+
228+
context 'when the server is a mongos' do
229+
230+
let(:mongos?) do
231+
true
232+
end
233+
234+
let(:expected) do
235+
{ }
236+
end
237+
238+
it 'does not set the slave_ok flag' do
239+
expect(read_preferrable.send(:update_options, context)).to eq(expected)
240+
end
241+
end
197242
end

0 commit comments

Comments
 (0)