Skip to content

[Cache] Ensured that redis adapter can use multiple redis sentinel hosts #47003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
28 changes: 18 additions & 10 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down