Skip to content

Commit c9508c2

Browse files
p-mongop
andcommitted
Fix RUBY-2932 map_reduce changes broke read preference passing from Mongoid (#2433)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent cac4c60 commit c9508c2

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(options.slice(: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
@@ -250,7 +250,20 @@ def new(options)
250250
end
251251

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

256269
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
@@ -878,4 +878,20 @@
878878
end
879879
end
880880
end
881+
882+
describe '#map_reduce_spec' do
883+
context 'when read preference is given' do
884+
let(:view_options) do
885+
{ read: {mode: :secondary} }
886+
end
887+
888+
context 'selector' do
889+
# For compatibility with released versions of Mongoid, this method
890+
# must return read preference under the :read key.
891+
it 'contains read preference' do
892+
map_reduce_spec[:selector][:read].should == {'mode' => :secondary}
893+
end
894+
end
895+
end
896+
end
881897
end

0 commit comments

Comments
 (0)