Skip to content

Commit

Permalink
#37139 Fixed reading locks from a replica set where connection reads …
Browse files Browse the repository at this point in the history
…from secondaries
  • Loading branch information
Joe Bennett committed Jun 8, 2020
1 parent 5de548b commit a0e4f1f
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/Symfony/Component/Lock/Store/MongoDbStore.php
Expand Up @@ -14,8 +14,8 @@
use MongoDB\BSON\UTCDateTime;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\WriteException;
use MongoDB\Driver\ReadPreference;
use MongoDB\Exception\DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
Expand Down Expand Up @@ -54,7 +54,7 @@ class MongoDbStore implements BlockingStoreInterface
private $options;
private $initialTtl;

private $databaseVersion;
private $readPreference;

use ExpiringStoreTrait;

Expand Down Expand Up @@ -87,8 +87,8 @@ class MongoDbStore implements BlockingStoreInterface
* to 0.0 and optionally leverage
* self::createTtlIndex(int $expireAfterSeconds = 0).
*
* writeConcern, readConcern and readPreference are not specified by
* MongoDbStore meaning the collection's settings will take effect.
* writeConcern and readConcern are not specified by MongoDbStore meaning the connection's settings will take effect.
* readPreference is primary for all queries.
* @see https://docs.mongodb.com/manual/applications/replication/
*/
public function __construct($mongo, array $options = [], float $initialTtl = 300.0)
Expand Down Expand Up @@ -262,6 +262,8 @@ public function exists(Key $key): bool
'expires_at' => [
'$gt' => $this->createMongoDateTime(microtime(true)),
],
], [
'readPreference' => $this->getReadPreference(),
]);
}

Expand Down Expand Up @@ -315,23 +317,15 @@ private function isDuplicateKeyException(WriteException $e): bool
return 11000 === $code;
}

private function getDatabaseVersion(): string
private function getReadPreference(): ReadPreference
{
if (null !== $this->databaseVersion) {
return $this->databaseVersion;
if (null !== $this->readPreference) {
return $this->readPreference;
}

$command = new Command([
'buildinfo' => 1,
]);
$cursor = $this->getCollection()->getManager()->executeReadCommand(
$this->getCollection()->getDatabaseName(),
$command
);
$buildInfo = $cursor->toArray()[0];
$this->databaseVersion = $buildInfo->version;
$this->readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);

return $this->databaseVersion;
return $this->readPreference;
}

private function getCollection(): Collection
Expand Down

0 comments on commit a0e4f1f

Please sign in to comment.