Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed PHP-511: Setting slaveOkay on MongoDB doesn't get passed proper…

…ly to MongoCollection.
  • Loading branch information...
commit da737dae8a033f3c15a7d9cb3e5a2ad7b80f9073 1 parent 73adbe4
Derick Rethans authored October 09, 2012
9  collection.c
@@ -568,10 +568,10 @@ PHP_METHOD(MongoCollection, batchInsert) {
568 568
 PHP_METHOD(MongoCollection, find)
569 569
 {
570 570
   zval *query = 0, *fields = 0;
571  
-	mongo_read_preference rp;
572 571
   mongo_collection *c;
573 572
   mongo_link *link;
574 573
   zval temp;
  574
+	mongo_cursor *cursor;
575 575
 
576 576
   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &query, &fields) == FAILURE) {
577 577
     return;
@@ -585,8 +585,6 @@ PHP_METHOD(MongoCollection, find)
585 585
 
586 586
   object_init_ex(return_value, mongo_ce_Cursor);
587 587
 
588  
-	/* save & replace slave_okay */
589  
-	mongo_read_preference_copy(&link->servers->read_pref, &rp);
590 588
 	mongo_read_preference_replace(&c->read_pref, &link->servers->read_pref);
591 589
 
592 590
 	/* TODO: Don't call an internal function like this, but add a new C-level
@@ -601,8 +599,9 @@ PHP_METHOD(MongoCollection, find)
601 599
     MONGO_METHOD4(MongoCursor, __construct, &temp, return_value, c->link, c->ns, query, fields);
602 600
   }
603 601
 
604  
-	mongo_read_preference_replace(&rp, &link->servers->read_pref);
605  
-	mongo_read_preference_dtor(&rp);
  602
+	/* add read preferences to cursor */
  603
+	cursor = (mongo_cursor*)zend_object_store_get_object(return_value TSRMLS_CC);
  604
+	mongo_read_preference_replace(&c->read_pref, &cursor->read_pref);
606 605
 }
607 606
 
608 607
 PHP_METHOD(MongoCollection, findOne) {
16  cursor.c
@@ -253,6 +253,11 @@ PHP_METHOD(MongoCursor, __construct) {
253 253
   cursor->resource = zlink;
254 254
   zval_add_ref(&zlink);
255 255
 
  256
+	/* Initialize read_pref to empty */
  257
+	cursor->read_pref.type = MONGO_RP_PRIMARY;
  258
+	cursor->read_pref.tagset_count = 0;
  259
+	cursor->read_pref.tagsets = NULL;
  260
+
256 261
   // change ['x', 'y', 'z'] into {'x' : 1, 'y' : 1, 'z' : 1}
257 262
   if (Z_TYPE_P(zfields) == IS_ARRAY) {
258 263
     HashPosition pointer;
@@ -824,6 +829,7 @@ int mongo_cursor__do_query(zval *this_ptr, zval *return_value TSRMLS_DC) {
824 829
   zval *errmsg;
825 830
 	char *error_message;
826 831
 	mongo_link *link;
  832
+	mongo_read_preference rp;
827 833
 
828 834
   cursor = (mongo_cursor*)zend_object_store_get_object(getThis() TSRMLS_CC);
829 835
   if (!cursor) {
@@ -840,6 +846,10 @@ int mongo_cursor__do_query(zval *this_ptr, zval *return_value TSRMLS_DC) {
840 846
 		return FAILURE;
841 847
 	}
842 848
 
  849
+	/* store the link's read preference to backup, and overwrite with the collection's read preferences */
  850
+	mongo_read_preference_copy(&link->servers->read_pref, &rp);
  851
+	mongo_read_preference_replace(&cursor->read_pref, &link->servers->read_pref);
  852
+
843 853
 	/* Sets the wire protocol flag to allow reading from a secondary. The read
844 854
 	 * preference spec states: "slaveOk remains as a bit in the wire protocol
845 855
 	 * and drivers will set this bit to 1 for all reads except with PRIMARY
@@ -860,6 +870,10 @@ int mongo_cursor__do_query(zval *this_ptr, zval *return_value TSRMLS_DC) {
860 870
 	 * append_getlasterror, where this has to be done too. */
861 871
 	cursor->connection = mongo_get_read_write_connection(link->manager, link->servers, MONGO_CON_FLAG_READ, (char**) &error_message);
862 872
 
  873
+	/* restore read preferences from backup */
  874
+	mongo_read_preference_replace(&rp, &link->servers->read_pref);
  875
+	mongo_read_preference_dtor(&rp);
  876
+
863 877
 	if (!cursor->connection && error_message) {
864 878
 		efree(buf.start);
865 879
 		zend_throw_exception(mongo_ce_ConnectionException, error_message, 71 TSRMLS_CC);
@@ -1649,6 +1663,8 @@ void php_mongo_cursor_free(void *object TSRMLS_DC) {
1649 1663
 
1650 1664
     if (cursor->resource) zval_ptr_dtor(&cursor->resource);
1651 1665
 
  1666
+		mongo_read_preference_dtor(&cursor->read_pref);
  1667
+
1652 1668
     zend_object_std_dtor(&cursor->std TSRMLS_CC);
1653 1669
 
1654 1670
     efree(cursor);
2  php_mongo.h
@@ -404,6 +404,8 @@ typedef struct {
404 404
   zval *current;
405 405
   int retry;
406 406
 
  407
+	mongo_read_preference read_pref;
  408
+
407 409
 	int dead;
408 410
 } mongo_cursor;
409 411
 
51  tests/replicaset/bug00511.phpt
... ...
@@ -0,0 +1,51 @@
  1
+--TEST--
  2
+Test for PHP-511: Setting slaveOkay on MongoDB doesn't get passed properly to MongoCollection
  3
+--SKIPIF--
  4
+<?php require_once dirname(__FILE__) ."/skipif.inc"; ?>
  5
+--FILE--
  6
+<?php
  7
+$mentions = array(); 
  8
+require_once dirname(__FILE__) . "/../utils.inc";
  9
+
  10
+$m = new Mongo("mongodb://localhost:13000", array("replicaSet" => true));
  11
+$db = $m->test;
  12
+
  13
+MongoLog::setModule( MongoLog::RS );
  14
+MongoLog::setLevel( MongoLog::FINE );
  15
+MongoLog::setCallback( function($a, $b, $message) { if (preg_match('/connection: type: ([A-Z]+),/', $message, $m )) { @$GLOBALS['mentions'][$m[1]]++; }; } );
  16
+$db->setSlaveOkay(true);
  17
+
  18
+$mentions = array();
  19
+
  20
+// Normal find
  21
+$ret = $db->safe->find(array("doc" => 1));
  22
+iterator_to_array($ret);
  23
+var_dump($mentions); $mentions = array();
  24
+ 
  25
+// Force primary for command
  26
+$db->setProfilingLevel(42);
  27
+var_dump($mentions); $mentions = array();
  28
+ 
  29
+// Normal find
  30
+$ret = $db->safe->find(array("doc" => 1));
  31
+iterator_to_array($ret);
  32
+var_dump($mentions); $mentions = array();
  33
+?>
  34
+--EXPECTF--
  35
+Deprecated: Function MongoDB::setSlaveOkay() is deprecated in %sbug00511.php on line %d
  36
+array(2) {
  37
+  ["PRIMARY"]=>
  38
+  int(3)
  39
+  ["SECONDARY"]=>
  40
+  int(3)
  41
+}
  42
+array(1) {
  43
+  ["PRIMARY"]=>
  44
+  int(6)
  45
+}
  46
+array(2) {
  47
+  ["PRIMARY"]=>
  48
+  int(3)
  49
+  ["SECONDARY"]=>
  50
+  int(3)
  51
+}

0 notes on commit da737da

Please sign in to comment.
Something went wrong with that request. Please try again.