Skip to content
This repository

Update to handle upcoming releases (and features) of MongoDB driver #669

Closed
wants to merge 1 commit into from

3 participants

Paul Reinheimer Nate Abele Raymond Julin
Paul Reinheimer

Upcoming releases of the MongoDB driver will provide the ability to configure ReadPreference (http://www.php.net/manual/en/mongo.setreadpreference.php) which is handy for people dealing with replica sets. It will also depricate the connect->true option on the constructor.

  • Switch from mongodb driver depricated connect->true option on the constructor to the manual ->connect() call.
  • Provide configuration ability for readPrefernce options
Paul Reinheimer - Switch from using depricated connect = true option to manually inv…
…oking connect()

 - Provide configuration ability for readPrefernce options
7b41348
Paul Reinheimer

Note: You can't actually specify the Read Preference in the latest stable release, you need to be on the beta for 1.3.0.

Well, actually, if you want to get down to it. That beta isn't new enough either. There's an issue in the 1.3.0 beta that effectively prevents you from executing queries quite often if you specify a read preference, or simply tell it that it's okay to talk to a secondary server.

What you actually need is a release build with this fix: mongodb/mongo-php-driver@da737da

Which doesn't exist yet.

But PREPARE FOR THE FUTURE!

Raymond Julin nervetattoo commented on the diff
data/source/MongoDb.php
@@ -249,7 +248,12 @@ public function connect() {
249 248
 				$options['persist'] = $persist === true ? 'default' : $persist;
250 249
 			}
251 250
 			$this->server = new Mongo($connection, $options);
252  
-
  251
+			$this->server->connect();
  252
+			
  253
+			if (isset($cfg['readPreference'])) {
  254
+				$this->server->setReadPreference($cfg['readPreference']);
3

This should support the second argument of setReadPreference as well where you can specify preferences on server tags:

$m->setReadPreference(Mongo::RP_NEAREST, array(
    array('dc' => 'east'),
));

Hmm, good point. Let me play around a bit.

I'm not sure how notifications work on github, but please check out my comment here: #669 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Paul Reinheimer

How about:

        $this->server->connect();

        //Allow developer to configure MongoDB to support Read Preference setting, and possibly tags
        //If no tags are passed use an empty array.
        //See: http://www.php.net/manual/en/mongo.setreadpreference.php
        if (isset($cfg['readPreference'])) {
            $tags = isset($cfg['readPreferenceTags']) ? $cfg['readPreferenceTags'] : array();
            $this->server->setReadPreference($cfg['readPreference'], $tags);
        }
Nate Abele
Owner

I think this would be better:

diff --git a/data/source/MongoDb.php b/data/source/MongoDb.php
index 092147f..a43a931 100644
--- a/data/source/MongoDb.php
+++ b/data/source/MongoDb.php
@@ -122,7 +122,8 @@ class MongoDb extends \lithium\data\Source {
     *
     * @see lithium\data\Connections::add()
     * @see lithium\data\source\MongoDb::$_schema
-    * @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
+    * @link http://php.net/manual/en/mongo.construct.php PHP Manual: `Mongo::__construct()`
+    * @link http://php.net/manual/en/mongo.readpreferences.php PHP Manual: Mongo Read Preferences
     * @param array $config All information required to connect to the database, including:
     *        - `'database'` _string_: The name of the database to connect to. Defaults to `null`.
     *        - `'host'` _string_: The IP or machine name where Mongo is running, followed by a
@@ -139,6 +140,11 @@ class MongoDb extends \lithium\data\Source {
     *          collections. Defaults to `'fs'`.
     *        - `'replicaSet'` _boolean_: See the documentation for `Mongo::__construct()`. Defaults
     *          to `false`.
+    *        - `'readPreference'` _mixed_: Specifies the read preferences for this database
+    *          connection. See the read preferences docs for more information. This value may be
+    *          set to a preference, or an array containing a preference and tags, i.e.
+    *          `'readPreference' => Mongo::RP_NEAREST` or
+    *          `'readPreference' => array(Mongo::RP_NEAREST, array(array('dc' => 'east')))`.
     *
     * Typically, these parameters are set in `Connections::add()`, when adding the adapter to the
     * list of active connections.
@@ -157,7 +163,8 @@ class MongoDb extends \lithium\data\Source {
            'timeout'    => 100,
            'replicaSet' => false,
            'schema'     => null,
-           'gridPrefix' => 'fs'
+           'gridPrefix' => 'fs',
+           'readPreference' => null
        );
        parent::__construct($config + $defaults);
    }
@@ -238,11 +245,7 @@ class MongoDb extends \lithium\data\Source {
        $login = $cfg['login'] ? "{$cfg['login']}:{$cfg['password']}@" : '';
        $connection = "mongodb://{$login}{$host}" . ($login ? "/{$cfg['database']}" : '');

-       $options = array(
-           'connect' => true,
-           'timeout' => $cfg['timeout'],
-           'replicaSet' => $cfg['replicaSet']
-       );
+       $options = array('timeout' => $cfg['timeout'], 'replicaSet' => $cfg['replicaSet']);

        try {
            if ($persist = $cfg['persistent']) {
@@ -250,8 +253,13 @@ class MongoDb extends \lithium\data\Source {
            }
            $this->server = new Mongo($connection, $options);

-           if ($this->connection = $this->server->{$cfg['database']}) {
+           if ($this->server->connect() && $this->connection = $this->server->{$cfg['database']}) {
                $this->_isConnected = true;
+
+               if ($prefs = $cfg['readPreference']) {
+                   $prefs = !is_array($prefs) ? array($prefs, array()) : $prefs;
+                   $this->server->setReadPreference($prefs[0], $prefs[1]);
+               }
            }
        } catch (Exception $e) {
            throw new NetworkException("Could not connect to the database.", 503, $e);

Just needs a unit test. Also, be sure to re-submit to the dev branch.

Nate Abele
Owner

Actually, since GitHub doesn't let you switch branches, I'm just gonna go ahead and close this. Once you've resubmitted against the dev branch, and we get a unit test worked up, I'll merge it in ASAP. Thanks, man!

Nate Abele nateabele closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 28, 2012
Paul Reinheimer - Switch from using depricated connect = true option to manually inv…
…oking connect()

 - Provide configuration ability for readPrefernce options
7b41348
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 6 additions and 2 deletions. Show diff stats Hide diff stats

  1. 8  data/source/MongoDb.php
8  data/source/MongoDb.php
@@ -239,7 +239,6 @@ public function connect() {
239 239
 		$connection = "mongodb://{$login}{$host}" . ($login ? "/{$cfg['database']}" : '');
240 240
 
241 241
 		$options = array(
242  
-			'connect' => true,
243 242
 			'timeout' => $cfg['timeout'],
244 243
 			'replicaSet' => $cfg['replicaSet']
245 244
 		);
@@ -249,7 +248,12 @@ public function connect() {
249 248
 				$options['persist'] = $persist === true ? 'default' : $persist;
250 249
 			}
251 250
 			$this->server = new Mongo($connection, $options);
252  
-
  251
+			$this->server->connect();
  252
+			
  253
+			if (isset($cfg['readPreference'])) {
  254
+				$this->server->setReadPreference($cfg['readPreference']);
  255
+			}
  256
+			
253 257
 			if ($this->connection = $this->server->{$cfg['database']}) {
254 258
 				$this->_isConnected = true;
255 259
 			}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.