Skip to content

Commit 1c9a2e5

Browse files
committed
PHPLIB-1202: Assume sharded clusters use replica sets
1 parent 5aff53f commit 1c9a2e5

9 files changed

+17
-88
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ public function testSnapshotQueries(): void
15501550
{
15511551
$this->skipIfServerVersion('<', '5.0.0', 'Snapshot queries outside of transactions are not supported');
15521552

1553-
if (! ($this->isReplicaSet() || $this->isShardedClusterUsingReplicasets())) {
1553+
if ($this->isStandalone()) {
15541554
$this->markTestSkipped('Snapshot read concern is only supported with replicasets');
15551555
}
15561556

@@ -1803,7 +1803,7 @@ public function testWithTransactionExample(): void
18031803
*/
18041804
public function testQueryableEncryption(): void
18051805
{
1806-
if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) {
1806+
if ($this->isStandalone()) {
18071807
$this->markTestSkipped('Queryable encryption requires replica sets');
18081808
}
18091809

tests/FunctionalTestCase.php

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use MongoDB\Driver\Command;
1010
use MongoDB\Driver\Exception\CommandException;
1111
use MongoDB\Driver\Manager;
12-
use MongoDB\Driver\Query;
1312
use MongoDB\Driver\ReadPreference;
1413
use MongoDB\Driver\Server;
1514
use MongoDB\Driver\ServerApi;
@@ -444,33 +443,6 @@ protected function isShardedCluster()
444443
return false;
445444
}
446445

447-
protected function isShardedClusterUsingReplicasets()
448-
{
449-
// Assume serverless is a sharded cluster using replica sets
450-
if (static::isServerless()) {
451-
return true;
452-
}
453-
454-
$cursor = $this->getPrimaryServer()->executeQuery(
455-
'config.shards',
456-
new Query([], ['limit' => 1]),
457-
);
458-
459-
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
460-
$document = current($cursor->toArray());
461-
462-
if (! $document) {
463-
return false;
464-
}
465-
466-
/**
467-
* Use regular expression to distinguish between standalone or replicaset:
468-
* Without a replicaset: "host" : "localhost:4100"
469-
* With a replicaset: "host" : "dec6d8a7-9bc1-4c0e-960c-615f860b956f/localhost:4400,localhost:4401"
470-
*/
471-
return preg_match('@^.*/.*:\d+@', $document['host']);
472-
}
473-
474446
protected function skipIfServerVersion(string $operator, string $version, ?string $message = null): void
475447
{
476448
if (version_compare($this->getServerVersion(), $version, $operator)) {
@@ -480,43 +452,25 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin
480452

481453
protected function skipIfChangeStreamIsNotSupported(): void
482454
{
483-
switch ($this->getPrimaryServer()->getType()) {
484-
case Server::TYPE_MONGOS:
485-
case Server::TYPE_LOAD_BALANCER:
486-
if (! $this->isShardedClusterUsingReplicasets()) {
487-
$this->markTestSkipped('$changeStream is only supported with replicasets');
488-
}
489-
490-
break;
491-
492-
case Server::TYPE_RS_PRIMARY:
493-
break;
494-
495-
default:
496-
$this->markTestSkipped('$changeStream is not supported');
455+
if ($this->isStandalone()) {
456+
$this->markTestSkipped('$changeStream requires replica sets');
497457
}
498458
}
499459

500460
protected function skipIfCausalConsistencyIsNotSupported(): void
501461
{
502462
switch ($this->getPrimaryServer()->getType()) {
503-
case Server::TYPE_MONGOS:
504-
case Server::TYPE_LOAD_BALANCER:
505-
if (! $this->isShardedClusterUsingReplicasets()) {
506-
$this->markTestSkipped('Causal Consistency is only supported with replicasets');
507-
}
508-
463+
case Server::TYPE_STANDALONE:
464+
$this->markTestSkipped('Causal consistency requires replica sets');
509465
break;
510466

511467
case Server::TYPE_RS_PRIMARY:
468+
// Note: mongos does not report storage engine information
512469
if ($this->getServerStorageEngine() !== 'wiredTiger') {
513-
$this->markTestSkipped('Causal Consistency requires WiredTiger storage engine');
470+
$this->markTestSkipped('Causal consistency requires WiredTiger storage engine');
514471
}
515472

516473
break;
517-
518-
default:
519-
$this->markTestSkipped('Causal Consistency is not supported');
520474
}
521475
}
522476

@@ -549,10 +503,6 @@ protected function skipIfTransactionsAreNotSupported(): void
549503
}
550504

551505
if ($this->isShardedCluster()) {
552-
if (! $this->isShardedClusterUsingReplicasets()) {
553-
$this->markTestSkipped('Transactions are not supported on sharded clusters without replica sets');
554-
}
555-
556506
if (version_compare($this->getFeatureCompatibilityVersion(), '4.2', '<')) {
557507
$this->markTestSkipped('Transactions are only supported on FCV 4.2 or higher');
558508
}

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function setUp(): void
3030
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
3131
}
3232

33-
if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) {
33+
if ($this->isStandalone()) {
3434
$this->markTestSkipped('Queryable Encryption requires replica sets');
3535
}
3636

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function testHintOptionAndUnacknowledgedWriteConcernUnsupportedClientSide
159159

160160
public function testFindAndModifyReportedWriteConcernError(): void
161161
{
162-
if (($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets()) || ! $this->isReplicaSet()) {
162+
if ($this->isStandalone()) {
163163
$this->markTestSkipped('Test only applies to replica sets');
164164
}
165165

tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setUp(): void
3131
{
3232
parent::setUp();
3333

34-
if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) {
34+
if ($this->isStandalone()) {
3535
$this->markTestSkipped('Automatic data encryption key tests require replica sets');
3636
}
3737

tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function setUp(): void
3939
{
4040
parent::setUp();
4141

42-
if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) {
42+
if ($this->isStandalone()) {
4343
$this->markTestSkipped('Range explicit encryption tests require replica sets');
4444
}
4545

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ static function (self $test, ClientEncryption $clientEncryptionNoClientCert, Cli
13231323
*/
13241324
public function testExplicitEncryption(Closure $test): void
13251325
{
1326-
if ($this->isStandalone() || ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets())) {
1326+
if ($this->isStandalone()) {
13271327
$this->markTestSkipped('Explicit encryption tests require replica sets');
13281328
}
13291329

tests/SpecTests/RetryableWritesSpecTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class RetryableWritesSpecTest extends FunctionalTestCase
3434
*/
3535
public function testRetryableWrites(stdClass $test, ?array $runOn, array $data): void
3636
{
37-
if ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets()) {
38-
$this->markTestSkipped('Transaction numbers are only allowed on a replica set member or mongos (PHPC-1415)');
39-
}
40-
4137
$useMultipleMongoses = isset($test->useMultipleMongoses) && $test->useMultipleMongoses && $this->isMongos();
4238

4339
if (isset($runOn)) {

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use function PHPUnit\Framework\assertIsString;
3434
use function PHPUnit\Framework\assertNotEmpty;
3535
use function PHPUnit\Framework\assertNotFalse;
36-
use function preg_match;
3736
use function preg_replace;
3837
use function sprintf;
3938
use function strlen;
@@ -299,9 +298,10 @@ private function getTopology(): string
299298
return RunOnRequirement::TOPOLOGY_REPLICASET;
300299

301300
case Server::TYPE_MONGOS:
302-
return $this->isShardedClusterUsingReplicasets()
303-
? RunOnRequirement::TOPOLOGY_SHARDED_REPLICASET
304-
: RunOnRequirement::TOPOLOGY_SHARDED;
301+
/* Since MongoDB 3.6, all sharded clusters use replica sets. The
302+
* unified test format deprecated use of "sharded-replicaset" in
303+
* tests but we should still identify as such. */
304+
return RunOnRequirement::TOPOLOGY_SHARDED_REPLICASET;
305305

306306
case Server::TYPE_LOAD_BALANCER:
307307
return RunOnRequirement::TOPOLOGY_LOAD_BALANCED;
@@ -368,23 +368,6 @@ private function isSchemaVersionSupported(string $schemaVersion): bool
368368
return version_compare($schemaVersion, self::MIN_SCHEMA_VERSION, '>=') && version_compare($schemaVersion, self::MAX_SCHEMA_VERSION, '<=');
369369
}
370370

371-
private function isShardedClusterUsingReplicasets(): bool
372-
{
373-
$collection = $this->internalClient->selectCollection('config', 'shards');
374-
$config = $collection->findOne();
375-
376-
if ($config === null) {
377-
return false;
378-
}
379-
380-
/**
381-
* Use regular expression to distinguish between standalone or replicaset:
382-
* Without a replicaset: "host" : "localhost:4100"
383-
* With a replicaset: "host" : "dec6d8a7-9bc1-4c0e-960c-615f860b956f/localhost:4400,localhost:4401"
384-
*/
385-
return preg_match('@^.*/.*:\d+@', $config['host']);
386-
}
387-
388371
/**
389372
* Kill all sessions on the cluster.
390373
*

0 commit comments

Comments
 (0)