Skip to content

Commit fe7f5a0

Browse files
p-mongop
andcommitted
Fix RUBY-2932 map_reduce changes broke read preference passing from Mongoid (#2433)
Co-authored-by: Oleg Pudeyev <code@olegp.name>
1 parent c1ef472 commit fe7f5a0

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

lib/mongo/collection/view/builder/map_reduce.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ def map_reduce_command
115115
end
116116
command.update(view_options)
117117
command.update(Utils.slice_hash(options, :collation))
118+
118119
# Read preference isn't simply passed in the command payload
119-
# (it may need to be converted to wire protocol flags)
120-
# so remove it here and hopefully it's handled elsewhere.
121-
# If not, RUBY-2706.
122-
command.delete(:read)
120+
# (it may need to be converted to wire protocol flags).
121+
# Ideally it should be removed here, however due to Mongoid 7
122+
# using this method and requiring :read to be returned from it,
123+
# we cannot do this just yet - see RUBY-2932.
124+
#command.delete(:read)
125+
123126
command.merge!(Options::Mapper.transform_documents(options, MAPPINGS))
124127
command
125128
end

lib/mongo/collection/view/map_reduce.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,20 @@ def new(options)
248248
end
249249

250250
def initial_query_op(session)
251-
Operation::MapReduce.new(map_reduce_spec(session))
251+
spec = map_reduce_spec(session)
252+
# Read preference isn't simply passed in the command payload
253+
# (it may need to be converted to wire protocol flags).
254+
# Passing it in command payload produces errors on at least
255+
# 5.0 mongoses.
256+
# In the future map_reduce_command should remove :read
257+
# from its return value, however we cannot do this right now
258+
# due to Mongoid 7 relying on :read being returned as part of
259+
# the command - see RUBY-2932.
260+
# Delete :read here for now because it cannot be sent to mongos this way.
261+
spec = spec.dup
262+
spec[:selector] = spec[:selector].dup
263+
spec[:selector].delete(:read)
264+
Operation::MapReduce.new(spec)
252265
end
253266

254267
def valid_server?(server)

spec/mongo/collection/view/map_reduce_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,20 @@
865865
end
866866
end
867867
end
868+
869+
describe '#map_reduce_spec' do
870+
context 'when read preference is given' do
871+
let(:view_options) do
872+
{ read: {mode: :secondary} }
873+
end
874+
875+
context 'selector' do
876+
# For compatibility with released versions of Mongoid, this method
877+
# must return read preference under the :read key.
878+
it 'contains read preference' do
879+
map_reduce_spec[:selector][:read].should == {'mode' => :secondary}
880+
end
881+
end
882+
end
883+
end
868884
end

0 commit comments

Comments
 (0)