Skip to content

Commit e2209d5

Browse files
committed
PHPC-689: Use mongoc_cursor_set_hint() for query/command execution
Test changes were necessary because mongoc_cursor_set_hint() forces the slaveOk flag (CDRIVER-903).
1 parent f15f20a commit e2209d5

6 files changed

+82
-55
lines changed

php_phongo.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,9 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
644644
return false;
645645
}
646646

647-
if (server_id > 0) {
648-
cursor->server_id = server_id;
647+
if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
648+
phongo_throw_exception(PHONGO_ERROR_MONGOC_FAILED TSRMLS_CC, "%s", "Could not set cursor server_id");
649+
return false;
649650
}
650651

651652
if (!phongo_advance_cursor_and_check_for_error(cursor TSRMLS_CC)) {
@@ -668,8 +669,10 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
668669

669670

670671
cursor = mongoc_client_command(client, db, MONGOC_QUERY_NONE, 0, 1, 0, command, NULL, read_preference);
671-
if (server_id > 0) {
672-
cursor->server_id = server_id;
672+
673+
if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
674+
phongo_throw_exception(PHONGO_ERROR_MONGOC_FAILED TSRMLS_CC, "%s", "Could not set cursor server_id");
675+
return false;
673676
}
674677

675678
if (!phongo_advance_cursor_and_check_for_error(cursor TSRMLS_CC)) {

tests/server/server-executeCommand-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
5959
}
6060
}
6161
["flags"]=>
62-
int(0)
62+
int(4)
6363
["skip"]=>
6464
int(0)
6565
["limit"]=>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeCommand() with conflicting read preference for secondary
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$secondaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
12+
$secondary = $manager->selectServer($secondaryRp);
13+
14+
/* Note: this is testing that the read preference (even a conflicting one) has
15+
* no effect when directly querying a server, since the slaveOk flag is always
16+
* set for hinted commands. */
17+
$primaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
18+
$cursor = $secondary->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(array('ping' => 1)), $primaryRp);
19+
20+
var_dump($cursor->toArray());
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECTF--
26+
array(1) {
27+
[0]=>
28+
object(stdClass)#%d (%d) {
29+
["ok"]=>
30+
float(1)
31+
}
32+
}
33+
===DONE===

tests/server/server-executeCommand_error-001.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeQuery() with conflicting read preference for secondary
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); CLEANUP(REPLICASET); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$primaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
12+
$primary = $manager->selectServer($primaryRp);
13+
14+
$bulk = new \MongoDB\Driver\BulkWrite;
15+
$bulk->insert(['_id' => 1, 'x' => 1]);
16+
$primary->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
17+
18+
$secondaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
19+
$secondary = $manager->selectServer($secondaryRp);
20+
21+
/* Note: this is testing that the read preference (even a conflicting one) has
22+
* no effect when directly querying a server, since the slaveOk flag is always
23+
* set for hinted queries. */
24+
$cursor = $secondary->executeQuery(NS, new MongoDB\Driver\Query(['x' => 1]), $primaryRp);
25+
26+
var_dump($cursor->toArray());
27+
28+
?>
29+
===DONE===
30+
<?php exit(0); ?>(
31+
--EXPECTF--
32+
array(1) {
33+
[0]=>
34+
object(stdClass)#%d (%d) {
35+
["_id"]=>
36+
int(1)
37+
["x"]=>
38+
int(1)
39+
}
40+
}
41+
===DONE===

tests/server/server-executeQuery_error-001.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)