From 39998d323d4a56baf03d00a2fc9ac0fade49bbcd Mon Sep 17 00:00:00 2001 From: William Arslett Date: Thu, 21 Jul 2022 09:15:48 +0100 Subject: [PATCH] [Cache] Ensured that redis adapter can use multiple redis sentinel hosts --- .github/workflows/integration-tests.yml | 2 +- .../Adapter/RedisAdapterSentinelTest.php | 6 ++-- .../Component/Cache/Traits/RedisTrait.php | 28 ++++++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index e9ea1900a2433..74e7cb078d38d 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -171,7 +171,7 @@ jobs: env: REDIS_HOST: 'localhost:16379' REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005' - REDIS_SENTINEL_HOSTS: 'localhost:26379' + REDIS_SENTINEL_HOSTS: 'localhost:26379 localhost:26379 localhost:26379' REDIS_SENTINEL_SERVICE: redis_sentinel MESSENGER_REDIS_DSN: redis://127.0.0.1:7006/messages MESSENGER_AMQP_DSN: amqp://localhost/%2f/messages diff --git a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php index 881630c062d6c..521c5dc4a5579 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php @@ -47,9 +47,9 @@ public function testInvalidDSNHasBothClusterAndSentinel() public function testExceptionMessageWhenFailingToRetrieveMasterInformation() { $hosts = getenv('REDIS_SENTINEL_HOSTS'); - $firstHost = explode(' ', $hosts)[0]; + $dsn = 'redis:?host['.str_replace(' ', ']&host[', $hosts).']'; $this->expectException(\Symfony\Component\Cache\Exception\InvalidArgumentException::class); - $this->expectExceptionMessage('Failed to retrieve master information from master name "invalid-masterset-name" and address "'.$firstHost.'".'); - AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => 'invalid-masterset-name']); + $this->expectExceptionMessage('Failed to retrieve master information from sentinel "invalid-masterset-name" and dsn "'.$dsn.'".'); + AbstractAdapter::createConnection($dsn, ['redis_sentinel' => 'invalid-masterset-name']); } } diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index dfb5898214f36..accee44bc8e2a 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -179,7 +179,7 @@ public static function createConnection(string $dsn, array $options = []) } if (null === $params['class'] && \extension_loaded('redis')) { - $class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) ? \RedisArray::class : \Redis::class); + $class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) && !isset($params['redis_sentinel']) ? \RedisArray::class : \Redis::class); } else { $class = $params['class'] ?? \Predis\Client::class; @@ -193,21 +193,29 @@ public static function createConnection(string $dsn, array $options = []) $redis = new $class(); $initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) { - $host = $hosts[0]['host'] ?? $hosts[0]['path']; - $port = $hosts[0]['port'] ?? 0; + $hostIndex = 0; + do { + $host = $hosts[$hostIndex]['host'] ?? $hosts[$hostIndex]['path']; + $port = $hosts[$hostIndex]['port'] ?? 0; + $address = false; + + if (isset($hosts[$hostIndex]['host']) && $tls) { + $host = 'tls://'.$host; + } - if (isset($hosts[0]['host']) && $tls) { - $host = 'tls://'.$host; - } + if (!isset($params['redis_sentinel'])) { + break; + } - if (isset($params['redis_sentinel'])) { $sentinel = new \RedisSentinel($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']); - if (!$address = $sentinel->getMasterAddrByName($params['redis_sentinel'])) { - throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $params['redis_sentinel'], $host, $port)); + if ($address = $sentinel->getMasterAddrByName($params['redis_sentinel'])) { + [$host, $port] = $address; } + } while (++$hostIndex < \count($hosts) && !$address); - [$host, $port] = $address; + if (isset($params['redis_sentinel']) && !$address) { + throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s" and dsn "%s".', $params['redis_sentinel'], $dsn)); } try { 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