flyerhzm / bullet

help to kill N+1 queries and unused eager loading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Breaks with mongoid 8

Taywee opened this issue · comments

With the latest mongoid, I've had this issue:

     TypeError:
       no implicit conversion of Integer into Hash
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/builder/command.rb:90:in `<'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/builder/command.rb:90:in `convert_limit_and_batch_size!'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/builder/command.rb:76:in `block in selector'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/builder/command.rb:63:in `tap'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/builder/command.rb:63:in `selector'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/find/op_msg.rb:42:in `selector'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/sessions_supported.rb:135:in `command'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/op_msg_base.rb:29:in `message'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:107:in `build_message'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/sessions_supported.rb:256:in `build_message'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:97:in `dispatch_message'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:92:in `get_result'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:34:in `block (3 levels) in do_execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/response_handling.rb:108:in `add_server_diagnostics'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:33:in `block (2 levels) in do_execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/response_handling.rb:54:in `add_error_labels'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:32:in `block in do_execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/response_handling.rb:94:in `unpin_maybe'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:31:in `do_execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/executable.rb:80:in `execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/op_msg_executable.rb:49:in `execute_with_connection'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/op_msg_executable.rb:36:in `block in execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/server/connection_pool.rb:705:in `with_connection'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/server.rb:494:in `with_connection'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/operation/shared/op_msg_executable.rb:35:in `execute'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/collection/view/iterable.rb:199:in `send_initial_query'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/collection/view/iterable.rb:127:in `block in select_cursor'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/retryable/read_worker.rb:68:in `block in read_with_retry_cursor'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/retryable/read_worker.rb:193:in `modern_read_with_retry'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/retryable/read_worker.rb:117:in `read_with_retry'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/retryable/read_worker.rb:67:in `read_with_retry_cursor'
     # /usr/lib/ruby/2.7.0/forwardable.rb:235:in `read_with_retry_cursor'
     # /usr/lib/ruby/2.7.0/forwardable.rb:235:in `read_with_retry_cursor'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/collection/view/iterable.rb:126:in `select_cursor'
     # /mnt/bundle/ruby/2.7.0/gems/mongo-2.19.2/lib/mongo/collection/view/iterable.rb:60:in `each'
     # /mnt/bundle/ruby/2.7.0/gems/mongoid-8.1.3/lib/mongoid/contextual/mongo.rb:1085:in `to_a'
     # /mnt/bundle/ruby/2.7.0/gems/mongoid-8.1.3/lib/mongoid/contextual/mongo.rb:1085:in `retrieve_nth_with_limit'
     # /mnt/bundle/ruby/2.7.0/gems/mongoid-8.1.3/lib/mongoid/contextual/mongo.rb:585:in `first'
     # /mnt/bundle/ruby/2.7.0/gems/bullet-7.1.2/lib/bullet/mongoid8x.rb:12:in `first'
     # /usr/lib/ruby/2.7.0/forwardable.rb:235:in `first'
     # /mnt/bundle/ruby/2.7.0/gems/mongoid-8.1.3/lib/mongoid/criteria/modifiable.rb:200:in `find_or'
     # /mnt/bundle/ruby/2.7.0/gems/mongoid-8.1.3/lib/mongoid/criteria/modifiable.rb:112:in `find_or_initialize_by'
     # /usr/lib/ruby/2.7.0/forwardable.rb:235:in `find_or_initialize_by'

As far as I can tell, it seems to be caused by the first method in mongoid being first(limit): https://github.com/mongodb/mongoid/blob/v8.1.3/lib/mongoid/contextual/mongo.rb#L581
But bullet's wrapper is first(opts = {}):

def first(opts = {})

This causes the limit to be set to an empty hash when called without arguments. I've tried to work around this, but given that this method is called for all find methods in mongoid, I couldn't get it working without removing bullet in my case.

It looks like this would have worked with mongoid 7.x, which has the interface as find(limit_or_opts = nil): https://github.com/mongodb/mongoid/blob/384c03a8bd3f618be4a16e328a552e256515832f/lib/mongoid/contextual/mongo.rb#L262-L263

But I'm not sure if this wrapper would have worked with any version of mongoid 8.x