|
20 | 20 | use stdClass;
|
21 | 21 | use UnexpectedValueException;
|
22 | 22 |
|
23 |
| -use function array_merge; |
24 | 23 | use function call_user_func;
|
25 | 24 | use function count;
|
26 | 25 | use function current;
|
@@ -90,63 +89,16 @@ public static function createTestManager(?string $uri = null, array $options = [
|
90 | 89 |
|
91 | 90 | public static function getUri($allowMultipleMongoses = false): string
|
92 | 91 | {
|
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. */ |
95 | 94 | 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(); |
107 | 96 | }
|
108 | 97 |
|
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(); |
150 | 102 | }
|
151 | 103 |
|
152 | 104 | protected function assertCollectionCount($namespace, $count): void
|
@@ -629,6 +581,70 @@ private function disableFailPoints(): void
|
629 | 581 | }
|
630 | 582 | }
|
631 | 583 |
|
| 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 | + |
632 | 648 | /**
|
633 | 649 | * Checks if the failCommand command is supported on this server version
|
634 | 650 | */
|
|
0 commit comments