loopback-connector-mongodb icon indicating copy to clipboard operation
loopback-connector-mongodb copied to clipboard

ping() never returns if database is down

Open dkrantsberg opened this issue 6 years ago • 9 comments

Whenever mongo database is down ping() method never returns. This prevents implementing health checks on APIs with mongo datasource. There was a bug reported for this issue back in 2014 and was marked as fixed. However I'm seeing that this issue is still there.

Steps to reproduce

  1. Create a Loopback 4 application with mongodb as datasource
  2. Implement live/ready healthcheck endpoint as described here which calls ds.ping()
  3. Take mongo database down and hit /live endpoint. The request never completes

Related Issues

https://github.com/strongloop/loopback-connector-mongodb/issues/47

dkrantsberg avatar Dec 09 '19 18:12 dkrantsberg

@emonddr, could you please take a look? Thanks.

dhmlau avatar Dec 10 '19 13:12 dhmlau

I just tried the steps above.

When the mongo db is down:

  • the very 1st request : GET /live takes 2 minutes but does complete with an empty result
  • the 2nd request completes immediately
$ curl -X GET "http://[::1]:3000/live" -H "accept: application/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:02:00 --:--:--     0
curl: (52) Empty reply from server

$ curl -X GET "http://[::1]:3000/live" -H "accept: application/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   199  100   199    0     0  24875      0 --:--:-- --:--:-- --:--:-- 24875
{
  "status": "DOWN",
  "checks": [
    {
      "name": "health.MyLiveCheck",
      "state": "UP",
      "data": {
        "reason": ""
      }
    },
    {
      "name": "health.MyDBCheck",
      "state": "DOWN",
      "data": {
        "reason": "pool is draining, new operations prohibited"
      }
    }
  ]
}

emonddr avatar Dec 10 '19 19:12 emonddr

Even if I restart the docker container which has the mongo db,

any new requests still show the DB as down

$ curl -X GET "http://[::1]:8050/live" -H "accept: application/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   170  100   170    0     0  21250      0 --:--:-- --:--:-- --:--:-- 21250
{
  "status": "DOWN",
  "checks": [
    {
      "name": "health.MyLiveCheck",
      "state": "UP",
      "data": {
        "reason": ""
      }
    },
    {
      "name": "health.MyDBCheck",
      "state": "DOWN",
      "data": {
        "reason": "pool destroyed"
      }
    }
  ]
}

emonddr avatar Dec 10 '19 20:12 emonddr

I have to stop the LB4 app, and restart it.

Then I perform another test, and it finally shows the DB is up

$ curl -X GET "http://[::1]:8050/live" -H "accept: application/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   152  100   152    0     0   2576      0 --:--:-- --:--:-- --:--:--  2576
{
  "status": "UP",
  "checks": [
    {
      "name": "health.MyLiveCheck",
      "state": "UP",
      "data": {
        "reason": ""
      }
    },
    {
      "name": "health.MyDBCheck",
      "state": "UP",
      "data": {
        "reason": ""
      }
    }
  ]
}

emonddr avatar Dec 10 '19 20:12 emonddr

I actually had to define my provider like this:

// Define a provider to check the liveness of a datasource
  class DBLiveCheckProvider implements Provider<LiveCheck> {
    constructor(@inject('datasources.db') private ds: DataSource) {}

    value() {
      return async () => {

        await this.ds.ping();

   
      }

    }

  }

instead of

// Define a provider to check the liveness of a datasource
class DBLiveCheckProvider implements Provider<LiveCheck> {
  constructor(@inject('datasources.db') private ds: DataSource) {}

  value() {
    return () => this.ds.ping();
  }
}

Otherwise my VSCode editor would complain about this value() method:

image

emonddr avatar Dec 10 '19 20:12 emonddr

@dkrantsberg, did the comments from @emonddr resolve your issue? If yes, please close this issue. Thanks.

dhmlau avatar Jan 14 '20 01:01 dhmlau

@dhmlau No, wrapping the call to the ping() function in an async function doesn't make any difference. The behavior is still the same:

  • After mongo container stopped the first call to db.ping() never returns (neither resolves nor rejects).
  • After some however (maybe ~30 seconds) subsequent calls to db.ping() get rejected fast with 'pool destroyed' error
  • Even after mongo is brought back up db.ping() rejects with the same 'pool destroyed' error
  • Only restarting the LB4 app makes db.ping() succeed again

dkrantsberg avatar Jan 15 '20 22:01 dkrantsberg

@dkrantsberg Have you tried to tune the datasource setting for reconnect per https://mongodb.github.io/node-mongodb-native/3.5/reference/connecting/connection-settings/?

raymondfeng avatar Jan 15 '20 23:01 raymondfeng

Closing due to inactivity. Please create a new issue if you are still running into problems.

dhmlau avatar Feb 10 '20 18:02 dhmlau