Skip to content

Commit

Permalink
bug #37140 [Lock] Fixed reading locks from replica set secondary node…
Browse files Browse the repository at this point in the history
…s (kralos)

This PR was squashed before being merged into the 5.1 branch.

Discussion
----------

[Lock] Fixed reading locks from replica set secondary nodes

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37139
| License       | MIT
| Doc PR        | symfony/symfony-docs#13775

Force lock existence query to use `readPreference=primary` in case the given mongo connection is using any of the following `readPreference`s:

* primaryPreferred
* secondary
* secondaryPreferred
* nearest

Any of the above would fail if a secondary node is queried during a lock release.

Commits
-------

ebf7eaf [Lock] Fixed reading locks from replica set secondary nodes
  • Loading branch information
fabpot committed Jun 10, 2020
2 parents 5cefcc2 + ebf7eaf commit 292be5f
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 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,8 +54,6 @@ class MongoDbStore implements BlockingStoreInterface
private $options;
private $initialTtl;

private $databaseVersion;

use ExpiringStoreTrait;

/**
Expand Down Expand Up @@ -87,8 +85,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 +260,8 @@ public function exists(Key $key): bool
'expires_at' => [
'$gt' => $this->createMongoDateTime(microtime(true)),
],
], [
'readPreference' => new ReadPreference(\defined(ReadPreference::PRIMARY) ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY),
]);
}

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

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

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

return $this->databaseVersion;
}

private function getCollection(): Collection
{
if (null !== $this->collection) {
Expand Down

0 comments on commit 292be5f

Please sign in to comment.