mongodb / mongo-php-library

The Official MongoDB PHP library
https://mongodb.com/docs/php-library/current/
Apache License 2.0
1.6k stars 261 forks source link

Close Connection with Latest version #600

Closed sajumani closed 4 years ago

sajumani commented 5 years ago

Hi,

In the new version , How do i close MongoDb Connection ? Will it auto destroy ?

derickr commented 5 years ago

The MongoDB driver persists connections between scripts for performance reasons. If a PHP process ends, then the driver will close the connection automatically. There is currently no way to close connections manually, but we also believe that this is not necessary.

yunyulian77 commented 5 years ago

But pm.max_requests default Value: 0, the child process will exist forever. Through I set this value=500, mongo server connections are always full.

jmikola commented 5 years ago

@yunyulian77: Per my previous explanation in https://github.com/mongodb/mongo-php-driver/issues/662#issuecomment-338003969, the number of server connections we expect to be created by the PHP driver is numAppServers × numRequestWorkersPerAppServer. This assumes that you are using the same connection string/options, which will leverage the persistence behavior discussed in Connection Handling within the driver documentation.

It'd be helpful if you shared a few details about your situation:

Based on previous issues in both this repository and mongo-php-driver, the root problem is often due to the aforementioned formula. That assumes PHP workers aren't connecting to the same MongoDB servers with subtly different connection strings, which impairs the driver's ability to share sockets between MongoDB\Driver\Manager instances.

So long as the potential number of active workers (based on pm options) can exceed the database server's connection limits, an API to manually close sockets within the driver is not really a solution.

yunyulian77 commented 5 years ago

@jmikola as follows:

pid = run/php-fpm.pid
pm = static
pm.max_children = 100
pm.start_servers = 200
pm.min_spare_servers = 50
pm.max_spare_servers = 200
pm.max_requests = 500
"connections" : {
    "current" : 788,
    "available" : 212,
    "internal_current" : 10,
    "internal_available" : 990,
    "totalCreated" : NumberLong(82412096)
},
rs.status();
{
    "set" : "mgset-4721041",
    "date" : ISODate("2019-01-05T08:05:44.267Z"),
    "myState" : 1,
    "term" : NumberLong(3),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
            "_id" : 0,
            "name" : "primaryNode",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 36374361,
            "optime" : {
                "ts" : Timestamp(1546675543, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2019-01-05T08:05:43Z"),
            "electionTime" : Timestamp(1523676364, 1),
            "electionDate" : ISODate("2018-04-14T03:26:04Z"),
            "configVersion" : 2,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "secondaryNode",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 22999186,
            "optime" : {
                "ts" : Timestamp(1546675543, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2019-01-05T08:05:43Z"),
            "lastHeartbeat" : ISODate("2019-01-05T08:05:43.906Z"),
            "lastHeartbeatRecv" : ISODate("2019-01-05T08:05:43.616Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "11.218.63.157:3010",
            "configVersion" : 2
        },
        {
            "_id" : 2,
            "name" : "hiddenNode",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 22999186,
            "optime" : {
                "ts" : Timestamp(1546675543, 1),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2019-01-05T08:05:43Z"),
            "lastHeartbeat" : ISODate("2019-01-05T08:05:43.375Z"),
            "lastHeartbeatRecv" : ISODate("2019-01-05T08:05:43.179Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "11.218.63.157:3010",
            "configVersion" : 2
        }
    ],
    "ok" : 1
}
jmikola commented 5 years ago

@yunyulian77: Based on the FPM configuration you shared, the only relevant options are:

pm = static
pm.max_children = 100
pm.max_requests = 500

The start_servers and spare_servers options are only relevant when pm = dynamic, so we can ignore those. Given the above and 4 application servers, I'd expect a peak of 400 connections originating from the PHP driver on each non-hidden mongod. It's unclear to my why 788 is being reported. The next thing to check would be the number of outgoing connections to the database servers from each PHP application server.

You should be able to use netstat to calculate that, as described in this SO thread. Based on the rs.status() output, it looks like the primary's host is 11.218.63.157:3010. Assuming the application servers route to the same host when connecting to the primary, you might run netstat -nputw | grep "11.218.63.157:3010" to report all outgoing connections to that host. Note that you may need to adjust the filtering if your cloud provider is using separate hosts for communication between mongod processes and application servers.

Finally, you should count the number of reported connections across all application servers and compare that to the value you saw reported in the server status (i.e. 788 above). It may also be helpful to aggregate the number of connections on each app server by state (e.g. "ESTABLISHED", "CLOSE_WAIT").

alcaeus commented 4 years ago

Closing due to inactivity. Feel free to follow up if this is still outstanding.