Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply primary readPreference to certain commands #187

Merged
merged 1 commit into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion lib/Mongo/MongoCommandCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ protected function ensureCursor()
}
}

$this->cursor = $this->db->command($convertedCommand, $this->getOptions());
$originalReadPreference = null;
if (!$this->supportsReadPreference()) {
$originalReadPreference = $this->readPreference;
$this->setReadPreference(\MongoClient::RP_PRIMARY);
}

try {
$this->cursor = $this->db->command($convertedCommand, $this->getOptions());
} finally {
if ($originalReadPreference) {
$this->readPreference = $originalReadPreference;
}
}
}

return $this->cursor;
Expand Down Expand Up @@ -112,4 +124,39 @@ public function __sleep()
{
return ['command'] + parent::__sleep();
}

/**
* @see https://github.com/mongodb/mongo-php-driver-legacy/blob/1.6.14/db.c#L51
* @return bool
*/
private function supportsReadPreference()
{
if ($this->command === []) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this merely guarding against the assumption below that there is at least one key in the command document?

Is the command document always a PHP array here, or might the user have supplied a stdClass? ext-mongo uses PHP's Hash API to access keys and would work with either.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about the object - I'll have to check that. I've only used arrays, but it's worth adding a test against objects as well 👍

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look and I typehinted array in the constructor of MongoCommandCursor, although I might have to loosen that up depending on what ext-mongo does. For now, the check against an empty array works.

}

$firstKey = array_keys($this->command)[0];
switch ($firstKey) {
case 'count':
case 'group':
case 'dbStats':
case 'geoNear':
case 'geoWalk':
case 'distinct':
case 'aggregate':
case 'collStats':
case 'geoSearch':
case 'parallelCollectionScan':
return true;

case 'mapreduce':
case 'mapReduce':
return (isset($this->command['out']) &&
is_array($this->command['out']) &&
array_key_exists('inline', $this->command['out']));

default:
return false;
}
}
}
137 changes: 137 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Alcaeus\MongoDbAdapter\Tests\Mongo;

use MongoDB\Database;
use MongoDB\Driver\ReadPreference;
use Alcaeus\MongoDbAdapter\Tests\TestCase;

Expand Down Expand Up @@ -65,4 +66,140 @@ public function testInfo()
$i++;
}
}

/**
* @dataProvider dataCommandAppliesCorrectReadPreference
*/
public function testCommandAppliesCorrectReadPreference($command, $expectedReadPreference)
{
$this->skipTestIf(extension_loaded('mongo'));

$checkReadPreference = function ($other) use ($expectedReadPreference) {
if (!is_array($other)) {
return false;
}

if (!array_key_exists('readPreference', $other)) {
return false;
}

if (!$other['readPreference'] instanceof ReadPreference) {
return false;
}

return $other['readPreference']->getMode() === $expectedReadPreference;
};

$databaseMock = $this->createMock(Database::class);
$databaseMock
->expects($this->once())
->method('command')
->with($this->anything(), $this->callback($checkReadPreference))
->will($this->returnValue(new \ArrayIterator()));

$cursor = new \MongoCommandCursor($this->getClient(), (string) $this->getDatabase(), $command);
$reflection = new \ReflectionProperty($cursor, 'db');
$reflection->setAccessible(true);
$reflection->setValue($cursor, $databaseMock);
$cursor->setReadPreference(\MongoClient::RP_SECONDARY);

iterator_to_array($cursor);

self::assertSame(\MongoClient::RP_SECONDARY, $cursor->getReadPreference()['type']);
}

public function dataCommandAppliesCorrectReadPreference()
{
return [
'findAndUpdate' => [
[
'findandmodify' => (string) $this->getCollection(),
'query' => [],
'update' => ['$inc' => ['field' => 1]],
],
ReadPreference::RP_PRIMARY,
],
'findAndRemove' => [
[
'findandremove' => (string) $this->getCollection(),
'query' => [],
],
ReadPreference::RP_PRIMARY,
],
'mapReduceWithOut' => [
[
'mapReduce' => (string) $this->getCollection(),
'out' => 'sample',
],
ReadPreference::RP_PRIMARY,
],
'mapReduceWithOutInline' => [
[
'mapReduce' => (string) $this->getCollection(),
'out' => ['inline' => true],
],
ReadPreference::RP_SECONDARY,
],
'count' => [
[
'count' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'group' => [
[
'group' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'dbStats' => [
[
'dbStats' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'geoNear' => [
[
'geoNear' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'geoWalk' => [
[
'geoWalk' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'distinct' => [
[
'distinct' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'aggregate' => [
[
'aggregate' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'collStats' => [
[
'collStats' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'geoSearch' => [
[
'geoSearch' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
'parallelCollectionScan' => [
[
'parallelCollectionScan' => (string) $this->getCollection(),
],
ReadPreference::RP_SECONDARY,
],
];
}
}