Skip to content

Commit cac8e3d

Browse files
jmikolaalcaeus
authored andcommitted
PHPLIB-794: Use single/multi-mongos LB URIs when available
This ensures MONGODB_SINGLE_MONGOS_LB_URI and MONGODB_MULTI_MONGOS_LB_URI are now set for load balancer tasks in Evergreen and used within the test suite. Also fixes some bugs in the original getUri() code for reassembling a URL with a single mongos host. Previously, auth credentials were not properly appended (d5b1156) and a query string could be appended without a slash between the question mark and last host in the seedlist (49d32aa).
1 parent dea346e commit cac8e3d

File tree

3 files changed

+76
-60
lines changed

3 files changed

+76
-60
lines changed

.evergreen/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ functions:
297297
export KMS_TLS_CA_FILE="${client_side_encryption_kms_tls_ca_file}"
298298
export KMS_TLS_CERTIFICATE_KEY_FILE="${client_side_encryption_kms_tls_certificate_key_file}"
299299
export PATH="${PHP_PATH}/bin:$PATH"
300+
export MONGODB_SINGLE_MONGOS_LB_URI="${SINGLE_MONGOS_LB_URI}"
301+
export MONGODB_MULTI_MONGOS_LB_URI="${MULTI_MONGOS_LB_URI}"
300302
301303
API_VERSION=${API_VERSION} \
302304
CRYPT_SHARED_LIB_PATH=${CRYPT_SHARED_LIB_PATH} \

tests/FunctionalTestCase.php

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use stdClass;
2121
use UnexpectedValueException;
2222

23-
use function array_merge;
2423
use function call_user_func;
2524
use function count;
2625
use function current;
@@ -90,63 +89,16 @@ public static function createTestManager(?string $uri = null, array $options = [
9089

9190
public static function getUri($allowMultipleMongoses = false): string
9291
{
93-
$uri = parent::getUri();
94-
92+
/* If multiple mongoses are allowed, the multi-mongos load balanced URI
93+
* can be used if available; otherwise, fall back MONGODB_URI. */
9594
if ($allowMultipleMongoses) {
96-
return $uri;
97-
}
98-
99-
$urlParts = parse_url($uri);
100-
if ($urlParts === false) {
101-
return $uri;
102-
}
103-
104-
// Only modify URIs using the mongodb scheme
105-
if ($urlParts['scheme'] !== 'mongodb') {
106-
return $uri;
95+
return getenv('MONGODB_MULTI_MONGOS_LB_URI') ?: parent::getUri();
10796
}
10897

109-
$hosts = explode(',', $urlParts['host']);
110-
$numHosts = count($hosts);
111-
if ($numHosts === 1) {
112-
return $uri;
113-
}
114-
115-
$manager = static::createTestManager($uri);
116-
if ($manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY))->getType() !== Server::TYPE_MONGOS) {
117-
return $uri;
118-
}
119-
120-
// Re-append port to last host
121-
if (isset($urlParts['port'])) {
122-
$hosts[$numHosts - 1] .= ':' . $urlParts['port'];
123-
}
124-
125-
$parts = ['mongodb://'];
126-
127-
if (isset($urlParts['user'], $urlParts['pass'])) {
128-
$parts += [
129-
$urlParts['user'],
130-
':',
131-
$urlParts['pass'],
132-
'@',
133-
];
134-
}
135-
136-
$parts[] = $hosts[0];
137-
138-
if (isset($urlParts['path'])) {
139-
$parts[] = $urlParts['path'];
140-
}
141-
142-
if (isset($urlParts['query'])) {
143-
$parts = array_merge($parts, [
144-
'?',
145-
$urlParts['query'],
146-
]);
147-
}
148-
149-
return implode('', $parts);
98+
/* If multiple mongoses are prohibited, the single-mongos load balanced
99+
* URI can be used if available; otherwise, we need to conditionally
100+
* process MONGODB_URI. */
101+
return getenv('MONGODB_SINGLE_MONGOS_LB_URI') ?: static::getUriWithoutMultipleMongoses();
150102
}
151103

152104
protected function assertCollectionCount($namespace, $count): void
@@ -629,6 +581,70 @@ private function disableFailPoints(): void
629581
}
630582
}
631583

584+
private static function getUriWithoutMultipleMongoses(): string
585+
{
586+
/* Cache the result. We can safely assume the topology type will remain
587+
* constant for the duration of the test suite. */
588+
static $uri;
589+
590+
if (isset($uri)) {
591+
return $uri;
592+
}
593+
594+
$uri = parent::getUri();
595+
$parsed = parse_url($uri);
596+
597+
if (! isset($parsed['scheme'], $parsed['host'])) {
598+
throw new UnexpectedValueException('Failed to parse scheme and host components from URI: ' . $uri);
599+
}
600+
601+
// Only modify URIs using the mongodb scheme
602+
if ($parsed['scheme'] !== 'mongodb') {
603+
return $uri;
604+
}
605+
606+
$hosts = explode(',', $parsed['host']);
607+
$numHosts = count($hosts);
608+
609+
if ($numHosts === 1) {
610+
return $uri;
611+
}
612+
613+
$manager = static::createTestManager($uri);
614+
if ($manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY))->getType() !== Server::TYPE_MONGOS) {
615+
return $uri;
616+
}
617+
618+
// Re-append port to last host
619+
if (isset($parsed['port'])) {
620+
$hosts[$numHosts - 1] .= ':' . $parsed['port'];
621+
}
622+
623+
$parts = ['mongodb://'];
624+
625+
if (isset($parsed['user'], $parsed['pass'])) {
626+
$parts[] = $parsed['user'] . ':' . $parsed['pass'] . '@';
627+
}
628+
629+
$parts[] = $hosts[0];
630+
631+
if (isset($parsed['path'])) {
632+
$parts[] = $parsed['path'];
633+
} elseif (isset($parsed['query'])) {
634+
/* URIs containing connection options but no auth database component
635+
* still require a slash before the question mark */
636+
$parts[] = '/';
637+
}
638+
639+
if (isset($parsed['query'])) {
640+
$parts[] = '?' . $parsed['query'];
641+
}
642+
643+
$uri = implode('', $parts);
644+
645+
return $uri;
646+
}
647+
632648
/**
633649
* Checks if the failCommand command is supported on this server version
634650
*/

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,12 @@ private function createContext(): Context
543543
$context->setUrisForUseMultipleMongoses($singleMongosUri, $multiMongosUri);
544544
}
545545

546-
/* TODO: Enable this logic once PHPLIB-794 is implemented. For now, load
547-
* balancer tests will continue to use MONGODB_URI. */
548-
if (false && $this->getPrimaryServer()->getType() === Server::TYPE_LOAD_BALANCER && ! $this->isServerless()) {
546+
if ($this->getPrimaryServer()->getType() === Server::TYPE_LOAD_BALANCER && ! $this->isServerless()) {
549547
$singleMongosUri = getenv('MONGODB_SINGLE_MONGOS_LB_URI');
550548
$multiMongosUri = getenv('MONGODB_MULTI_MONGOS_LB_URI');
551549

552-
assertNotFalse($singleMongosUri);
553-
assertNotFalse($multiMongosUri);
550+
assertNotEmpty($singleMongosUri);
551+
assertNotEmpty($multiMongosUri);
554552

555553
$context->setUrisForUseMultipleMongoses($singleMongosUri, $multiMongosUri);
556554
}

0 commit comments

Comments
 (0)