2014년 12월 28일 일요일

PHP-MongoDB driver - persistent connections?

Hello experts,

I am sorry in advance if this question has already been addressed (if so, please point me to the correct forum post).

My lab environment:
Webserver: Apache 2.2
Application stack: PHP with Code Igniter.
PHP version: PHP 5.4.6-1ubuntu1.8 (cli) (built: Apr 4 2014 01:28:36)
Code Igniter version: 2.0.3
MongoDB PHP driver: 1.4.3

MongoDB version: 2.4.5 (Two-node replica set withArbiter)

I was testing the DB connection strings from thewebserver to MongoDB essentially to test failover. While I am fairly happy with the Failover testing, the concept of persistent connection caught my interest.

According to MongoDB documentation, by default the PHP MongoDB driver makes use of persistent connections.

As of MongoPHP driver version 1.2 - the "persist" option has been deprecated. The latest versions of the driver have no concept of pooling anymore and will maintain only one connection per process, for each connection type (ReplicaSet/standalone/mongos), for each credentials combination.

So my understanding is the connections are managed by PHP. How do I validate that persistent connections are being honored? For example, I took one of the AJAX calls from our web application and opened a new browser instance with this AJAX call and I was able to see a new DB connection (as expected), however, when I refreshed the page I could see the number of connections go up initially and subsequently the number of connections reduced. I am not certain if this is a valid test to begin with.

Here's the mongodb.php config file

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['default']['mongo_hostbase'] = 'x.x.x.x:27017,x.x.x.x:27017';
$config['default']['mongo_database'] = 'production';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';

$config['default']['mongo_persist']  = FALSE;

/*$config['default']['mongo_persist_key']  = 'ci_persist';*/
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = 'XX-1';
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag']   = FALSE;
?>

Any help is appreciated.



> As of MongoPHP driver version 1.2 - the "persist" option has been
> deprecated. The latest versions of the driver have no concept of pooling
> anymore and will maintain only one connection per process, for each
> connection type (ReplicaSet/standalone/mongos), for each credentials
> combination.
>
> So my understanding is the connections are managed by PHP. How do I
> validate that persistent connections are being honored? For example, I took
> one of the AJAX calls from our web application and opened a new browser
> instance with this AJAX call and I was able to see a new DB connection (as
> expected), however, when I refreshed the page I could see the number of
> connections go up initially and subsequently the number of connections
> reduced. I am not certain if this is a valid test to begin with.
>
> Here's the mongodb.php config file
>
> <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
>
> $config['default']['mongo_hostbase'] = 'x.x.x.x:27017,x.x.x.x:27017';
If you're using an IP, that is likely to cause issues. Especially if
your replicaset is configured with hostnames. Because the driver will
talk to the IP, find out that its canonical replicaset name is actually
a hostname, connects to this new hostname (with possibly the same IP),
and then drops the original connection to the IP address. It will that
twice too, because you specify two hosts.

To find out what happens, you should have a look at the MongoLog files.
At
https://github.com/derickr/mongo-php-driver/tree/fix-readme#full-debug-log
you can find how to make sure a full log, and it will tell you why it
connects and disconnects. You can simply set this as an auto-prepend
file, or just include it from the start of your PHP script.



Many thanks for the reply. I can confirm that the replicaset is configured with the hostname(s) and the same applies to the mongodb.php. I will get the full debug log and followup.



Update:

I enabled debugging for the MongoDB PHP driver and here are my findings:

  • I don't see a persistent connection being maintained, i.e.
    • I browse to our custom web application and I see the following in /tmp/MONGO-PHP-LOG
2014-12-17 23:13:13 | PARSE (INFO): Parsing mongodb://<MONGO-1>:27017,<MONGO-2>:27017
2014-12-17 23:13:13 | PARSE (INFO): - Found node: <MONGO-1>:27017
2014-12-17 23:13:13 | PARSE (INFO): - Found node: <MONGO-2>:27017
2014-12-17 23:13:13 | PARSE (INFO): - Connection type: MULTIPLE
2014-12-17 23:13:13 | PARSE (INFO): - Found option 'replicaSet': 'rs-1'
2014-12-17 23:13:13 | PARSE (INFO): - Switching connection type: REPLSET
2014-12-17 23:13:13 | CON (INFO): mongo_get_read_write_connection: finding a REPLSET connection (read)
2014-12-17 23:13:13 | CON (INFO): connection_create: creating new connection for <MONGO-1>:27017
2014-12-17 23:13:16 | CON (WARN): connection_create: error while creating connection for <MONGO-1>:27017: Connection refused

I have deliberately set iptables rule to REJECT all inbound and outbound traffic on Mongo-2(Secondary)
 
 
2014-12-17 23:13:16 | CON (WARN): Couldn't connect to '<MONGO-1>:27017': Connection refused
2014-12-17 23:13:16 | CON (INFO): connection_create: creating new connection for <MONGO-2>:27017
2014-12-17 23:13:16 | CON (INFO): stream_connect: Not establishing SSL for <MONGO-2>:27017
2014-12-17 23:13:16 | REPLSET (FINE): - connection: type: PRIMARY, socket: 42, ping: 0, hash: <MONGO-2>:27017;rs-1;.;16258 

As I browse through our custom web application pages, the logs tell me that new Mongo connections are being created (same browser session) unless I am interpreting the logs incorrectly here.

2014-12-17 23:18:16 | CON (INFO): connection_create: creating new connection for <MONGO-2>:27017
 
As I browse through more pages

2014-12-17 23:20:48 | CON (INFO): connection_create: creating new connection for <MONGO-1>:27017
2014-12-17 23:20:48 | CON (INFO): stream_connect: Not establishing SSL for <MONGO-1>:27017 
 
  • The second issue I noticed is if I configure the Secondary Mongo to DROP all incoming packets (via iptables), the PHP Mongo Driver  right away detects that a connection cannot be created to the Mongo, however browsing the app becomes very sluggish and slow, for example it took nearly two minutes to load the web page
2014-12-17 23:26:16 | PARSE (INFO): Parsing mongodb://<MONGO-1>:27017,<MONGO-2>:27017
2014-12-17 23:26:16 | PARSE (INFO): - Found node: <MONGO-1>:27017
2014-12-17 23:26:16 | PARSE (INFO): - Found node: <MONGO-2>:27017
2014-12-17 23:26:16 | PARSE (INFO): - Connection type: MULTIPLE
2014-12-17 23:26:16 | PARSE (INFO): - Found option 'replicaSet': 'rs-1'
2014-12-17 23:26:16 | PARSE (INFO): - Switching connection type: REPLSET
2014-12-17 23:26:16 | CON (INFO): mongo_get_read_write_connection: finding a REPLSET connection (read)
2014-12-17 23:26:16 | CON (INFO): connection_create: creating new connection for <MONGO-1>:27017
2014-12-17 23:27:17 | CON (WARN): connection_create: error while creating connection for <MONGO-1>:27017: Connection timed out
2014-12-17 23:27:17 | CON (WARN): Couldn't connect to '<MONGO-1>:27017': Connection timed out
2014-12-17 23:27:17 | CON (INFO): connection_create: creating new connection for <MONGO-2>:27017 
2014-12-17 23:27:17 | REPLSET (FINE): - connection: type: PRIMARY, socket: 42, ping: 1, hash: <MONGO-2>:27017;rs-1;.;16816 

is this a known issue or am I doing something terrible here?



Oh yes, MongoDB version is 2.4.5



Any suggestions?


댓글 없음:

댓글 쓰기