Skip to content

Commit 39998d3

Browse files
warslettnicolas-grekas
authored andcommitted
[Cache] Ensured that redis adapter can use multiple redis sentinel hosts
1 parent 9d83034 commit 39998d3

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ jobs:
171171
env:
172172
REDIS_HOST: 'localhost:16379'
173173
REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005'
174-
REDIS_SENTINEL_HOSTS: 'localhost:26379'
174+
REDIS_SENTINEL_HOSTS: 'localhost:26379 localhost:26379 localhost:26379'
175175
REDIS_SENTINEL_SERVICE: redis_sentinel
176176
MESSENGER_REDIS_DSN: redis://127.0.0.1:7006/messages
177177
MESSENGER_AMQP_DSN: amqp://localhost/%2f/messages

src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public function testInvalidDSNHasBothClusterAndSentinel()
4747
public function testExceptionMessageWhenFailingToRetrieveMasterInformation()
4848
{
4949
$hosts = getenv('REDIS_SENTINEL_HOSTS');
50-
$firstHost = explode(' ', $hosts)[0];
50+
$dsn = 'redis:?host['.str_replace(' ', ']&host[', $hosts).']';
5151
$this->expectException(\Symfony\Component\Cache\Exception\InvalidArgumentException::class);
52-
$this->expectExceptionMessage('Failed to retrieve master information from master name "invalid-masterset-name" and address "'.$firstHost.'".');
53-
AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => 'invalid-masterset-name']);
52+
$this->expectExceptionMessage('Failed to retrieve master information from sentinel "invalid-masterset-name" and dsn "'.$dsn.'".');
53+
AbstractAdapter::createConnection($dsn, ['redis_sentinel' => 'invalid-masterset-name']);
5454
}
5555
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static function createConnection(string $dsn, array $options = [])
179179
}
180180

181181
if (null === $params['class'] && \extension_loaded('redis')) {
182-
$class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) ? \RedisArray::class : \Redis::class);
182+
$class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) && !isset($params['redis_sentinel']) ? \RedisArray::class : \Redis::class);
183183
} else {
184184
$class = $params['class'] ?? \Predis\Client::class;
185185

@@ -193,21 +193,29 @@ public static function createConnection(string $dsn, array $options = [])
193193
$redis = new $class();
194194

195195
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) {
196-
$host = $hosts[0]['host'] ?? $hosts[0]['path'];
197-
$port = $hosts[0]['port'] ?? 0;
196+
$hostIndex = 0;
197+
do {
198+
$host = $hosts[$hostIndex]['host'] ?? $hosts[$hostIndex]['path'];
199+
$port = $hosts[$hostIndex]['port'] ?? 0;
200+
$address = false;
201+
202+
if (isset($hosts[$hostIndex]['host']) && $tls) {
203+
$host = 'tls://'.$host;
204+
}
198205

199-
if (isset($hosts[0]['host']) && $tls) {
200-
$host = 'tls://'.$host;
201-
}
206+
if (!isset($params['redis_sentinel'])) {
207+
break;
208+
}
202209

203-
if (isset($params['redis_sentinel'])) {
204210
$sentinel = new \RedisSentinel($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
205211

206-
if (!$address = $sentinel->getMasterAddrByName($params['redis_sentinel'])) {
207-
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $params['redis_sentinel'], $host, $port));
212+
if ($address = $sentinel->getMasterAddrByName($params['redis_sentinel'])) {
213+
[$host, $port] = $address;
208214
}
215+
} while (++$hostIndex < \count($hosts) && !$address);
209216

210-
[$host, $port] = $address;
217+
if (isset($params['redis_sentinel']) && !$address) {
218+
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s" and dsn "%s".', $params['redis_sentinel'], $dsn));
211219
}
212220

213221
try {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy