Mongoid 4 / MongoDB 2.4+ "Freezing" Issue

585 Views Asked by At

I've had a strange issue since with maintaining a connection to mongodb using Mongoid. I thought this was originally due to upgrading to Rails 4.0+ (which required Mongoid/Moped to be updated), but I've also had this issue with other Rack-based apps (Sinatra and Grape to be specific).

We updated our mongodb instance from 2.4 to 2.6 after the stable was released, but this issue was present on 2.4 as well.

I wrote this up in an issue on Github (https://github.com/mongoid/moped/issues/274) but I'll copy it here as well as I'm not getting any traction there.

The TL;DR version is that my apps will randomly hang when trying to execute a query using Mongoid. This happens whether i'm using the Rails console or if the app is running (or deployed). If I'm in the console, hitting CTRL+C then retrying the request always works, so my suspicion is that this is a connectivity issue of some kind.

My mongoid config is similar to the following:

mongoid.yml

default: &default
  sessions:
    default:      
      uri: "mongodb://localhost:27017/database"
      options:
        timeout: 15
        retry_interval: 1
        max_retries: 5
        pool_size: 10

development:
  <<: *default

When I change the connection details to reference a "remote" mongodb instance, I start to get periodic connection issues.

mongoid.yml

default: &default
  sessions:
    default:      
      uri: "mongodb://remote-mongodb-url:27017/database"
      options:
        timeout: 15
        retry_interval: 1
        max_retries: 5
        pool_size: 10

development:
  <<: *default

The issue manifests in the following way:

1) The web application becomes completely unresponsive (the request just hangs) 2) The Rails log doesn't indicate anything out of the ordinary (even with Moped and Mongoid log levels set to DEBUG)

ex:

Started GET "/accounts" for 172.16.3.180 at 2014-04-23 09:11:34 -0400
Processing by AccountsController#index as HTML

I have tried using multiple web servers (Webrick, Puma, Unicorn) and they all experience the same issue.

3) If I force exit the application server (CTRL+C), I see the following:

Started GET "/accounts" for 172.16.3.180 at 2014-04-23 09:11:34 -0400
Processing by AccountsController#index as HTML
^C[2014-04-23 09:12:52] INFO  going to shutdown ...
^CExiting
D, [2014-04-23T09:12:53.516499 #4673] DEBUG -- :   MOPED: 168.62.164.129:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 78545.7536ms
Completed 401 Unauthorized in 78638ms
/home/alex/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:25:in `shutdown': undefined method `shutdown' for nil:NilClass (NoMethodError)

The weird thing here is if I deploy the application to production (which is on the same server as mongodb and has a mongoid.yml connection setup for localhost:27017), I have no issues.

0

There are 0 best solutions below