Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 292be5f

Browse files
committed
bug #37140 [Lock] Fixed reading locks from replica set secondary nodes (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
2 parents 5cefcc2 + ebf7eaf commit 292be5f

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use MongoDB\BSON\UTCDateTime;
1515
use MongoDB\Client;
1616
use MongoDB\Collection;
17-
use MongoDB\Driver\Command;
1817
use MongoDB\Driver\Exception\WriteException;
18+
use MongoDB\Driver\ReadPreference;
1919
use MongoDB\Exception\DriverRuntimeException;
2020
use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException;
2121
use MongoDB\Exception\UnsupportedException;
@@ -54,8 +54,6 @@ class MongoDbStore implements BlockingStoreInterface
5454
private $options;
5555
private $initialTtl;
5656

57-
private $databaseVersion;
58-
5957
use ExpiringStoreTrait;
6058

6159
/**
@@ -87,8 +85,8 @@ class MongoDbStore implements BlockingStoreInterface
8785
* to 0.0 and optionally leverage
8886
* self::createTtlIndex(int $expireAfterSeconds = 0).
8987
*
90-
* writeConcern, readConcern and readPreference are not specified by
91-
* MongoDbStore meaning the collection's settings will take effect.
88+
* writeConcern and readConcern are not specified by MongoDbStore meaning the connection's settings will take effect.
89+
* readPreference is primary for all queries.
9290
* @see https://docs.mongodb.com/manual/applications/replication/
9391
*/
9492
public function __construct($mongo, array $options = [], float $initialTtl = 300.0)
@@ -262,6 +260,8 @@ public function exists(Key $key): bool
262260
'expires_at' => [
263261
'$gt' => $this->createMongoDateTime(microtime(true)),
264262
],
263+
], [
264+
'readPreference' => new ReadPreference(\defined(ReadPreference::PRIMARY) ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY),
265265
]);
266266
}
267267

@@ -315,25 +315,6 @@ private function isDuplicateKeyException(WriteException $e): bool
315315
return 11000 === $code;
316316
}
317317

318-
private function getDatabaseVersion(): string
319-
{
320-
if (null !== $this->databaseVersion) {
321-
return $this->databaseVersion;
322-
}
323-
324-
$command = new Command([
325-
'buildinfo' => 1,
326-
]);
327-
$cursor = $this->getCollection()->getManager()->executeReadCommand(
328-
$this->getCollection()->getDatabaseName(),
329-
$command
330-
);
331-
$buildInfo = $cursor->toArray()[0];
332-
$this->databaseVersion = $buildInfo->version;
333-
334-
return $this->databaseVersion;
335-
}
336-
337318
private function getCollection(): Collection
338319
{
339320
if (null !== $this->collection) {

0 commit comments

Comments
 (0)