8000 [Messenger] Add Redis Sentinel support by norbertschultheisz · Pull Request #43163 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add Redis Sentinel support #43163

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
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/Bridge/Redis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add support for Redis Sentinel

6.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,26 @@ public function provideIdPatterns(): \Generator
$redis->expects($this->atLeastOnce())->method('rawCommand')->willReturn('1');
yield '100ms delay' => ['/^\w+\.\d+$/', $redis, 100];
}

public function testInvalidSentinelMasterName()
{
try {
Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['delete_after_ack' => true], null);
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}

if (!getenv('MESSENGER_REDIS_SENTINEL_MASTER')) {
self::markTestSkipped('Redis sentinel is not configured');
}

$master = getenv('MESSENGER_REDIS_DSN');
$uid = uniqid('sentinel_');

$exp = explode('://', $master, 2)[1];
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Failed to retrieve master information from master name "%s" and address "%s".', $uid, $exp));

Connection::fromDsn(sprintf('%s/messenger-clearlasterror', $master), ['delete_after_ack' => true, 'sentinel_master' => $uid], null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void

try {
$this->redis = new \Redis();
$this->connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), [], $this->redis);
$this->connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER')], $this->redis);
$this->connection->cleanup();
$this->connection->setup();
} catch (\Exception $e) {
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testConnectionSendDelayedMessagesWithSameContent()
public function testConnectionBelowRedeliverTimeout()
{
// lower redeliver timeout and claim interval
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), [], $this->redis);
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER')], $this->redis);

$connection->cleanup();
$connection->setup();
Expand Down Expand Up @@ -170,7 +170,8 @@ public function testConnectionClaimAndRedeliver()
// lower redeliver timeout and claim interval
$connection = Connection::fromDsn(
getenv('MESSENGER_REDIS_DSN'),
['redeliver_timeout' => 0, 'claim_interval' => 500],
['redeliver_timeout' => 0, 'claim_interval' => 500, 'sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER')],

$this->redis
);

Expand Down Expand Up @@ -214,6 +215,25 @@ public function testConnectionClaimAndRedeliver()
$connection->ack($message['id']);
}

public function testLazySentinel()
{
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'),
['lazy' => true,
'delete_after_ack' => true,
'sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER'), ], $this->redis);

$connection->add('1', []);
$this->assertNotEmpty($message = $connection->get());
$this->assertSame([
'message' => json_encode([
'body' => '1',
'headers' => [],
]),
], $message['data']);
$connection->reject($message['id']);
$connection->cleanup();
}

public function testLazyCluster()
{
$this->skipIfRedisClusterUnavailable();
Expand Down Expand Up @@ -273,7 +293,7 @@ public function testFromDsnWithMultipleHosts()
}, $hosts);
$dsn = implode(',', $dsn);

$this->assertInstanceOf(Connection::class, Connection::fromDsn($dsn));
$this->assertInstanceOf(Connection::class, Connection::fromDsn($dsn, ['sentinel_master' => getenv('MESSENGER_REDIS_SENTINEL_MASTER')]));
}

public function testJsonError()
Expand All @@ -292,7 +312,7 @@ public function testGetNonBlocking()
{
$redis = new \Redis();

$connection = Connection::fromDsn('redis://localhost/messenger-getnonblocking', [], $redis);
$connection = Connection::fromDsn('redis://localhost/messenger-getnonblocking', ['sentinel_master' => null], $redis);

$this->assertNull($connection->get()); // no message, should return null immediately
$connection->add('1', []);
Expand All @@ -304,15 +324,16 @@ public function testGetNonBlocking()
public function testGetAfterReject()
{
$redis = new \Redis();
$connection = Connection::fromDsn('redis://localhost/messenger-rejectthenget', [], $redis);
$connection = Connection::fromDsn('redis://localhost/messenger-rejectthenget', ['sentinel_master' => null], $redis);

$connection->add('1', []);
$connection->add('2', []);

$failing = $connection->get();
$connection->reject($failing['id']);

$connection = Connection::fromDsn('redis://localhost/messenger-rejectthenget', []);
$connection = Connection::fromDsn('redis://localhost/messenger-rejectthenget', ['sentinel_master' => null]);

$this->assertNotNull($connection->get());

$redis->del('messenger-rejectthenget');
Expand Down
Original file line number< 8000 /th> Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Connection
'lazy' => false,
'auth' => null,
'serializer' => \Redis::SERIALIZER_PHP,
'sentinel_master' => null, // String, master to look for (optional, default is NULL meaning Sentinel support is disabled)
'timeout' => 0, // Float, value in seconds (optional, default is 0 meaning unlimited)
'read_timeout' => 0, // Float, value in seconds (optional, default is 0 meaning unlimited)
'retry_interval' => 0, // Int, value in milliseconds (optional, default is 0)
'persistent_id' => null, // String, persistent connection id (optional, default is NULL meaning not persistent)
];

private \Redis|\RedisCluster|RedisProxy|RedisClusterProxy $connection;
Expand Down Expand Up @@ -72,6 +77,21 @@ public function __construct(array $configuration, array $connectionCredentials =
$auth = null;
}

$sm = $redisOptions['sentinel_master'] ?? null;
$sentinelMaster = (null == $sm || (\is_string($sm) && '' === $sm)) ? null : $sm;
$sentinelTimeout = $redisOptions['timeout'] ?? self::DEFAULT_OPTIONS['timeout'];
$sentinelReadTimeout = $redisOptions['read_timeout'] ?? self::DEFAULT_OPTIONS['read_timeout'];
$sentinelRetryInterval = $redisOptions['retry_interval'] ?? self::DEFAULT_OPTIONS['retry_interval'];
$sentinelPersistentId = $redisOptions['persistent_id'] ?? self::DEFAULT_OPTIONS['persistent_id'];

if (null !== $sentinelMaster && !class_exists(\Predis\Client::class) && !class_exists(\RedisSentinel::class)) {
throw new InvalidArgumentException('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher.');
}

if (null !== $sentinelMaster && $redis instanceof \RedisCluster) {
throw new InvalidArgumentException('Cannot configure Redis Sentinel and Redis Cluster instance at the same time.');
}

$lazy = $configuration['lazy'] ?? self::DEFAULT_OPTIONS['lazy'];
if (\is_array($host) || $redis instanceof \RedisCluster) {
$hosts = \is_string($host) ? [$host.':'.$port] : $host; // Always ensure we have an array
Expand All @@ -80,6 +100,16 @@ public function __construct(array $configuration, array $connectionCredentials =
};
$redis = $lazy ? new RedisClusterProxy($redis, $initializer) : $initializer($redis);
} else {
if (null !== $sentinelMaster) {
$sentinelClient = new \RedisSentinel($host, $port, $sentinelTimeout, $sentinelPersistentId, $sentinelRetryInterval, $sentinelReadTimeout);

if (!$address = $sentinelClient->getMas 5648 terAddrByName($sentinelMaster)) {
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $sentinelMaster, $host, $port));
}

[$host, $port] = $address;
}

$redis = $redis ?? new \Redis();
$initializer = static function ($redis) use ($host, $port, $auth, $serializer, $dbIndex) {
return self::initializeRedis($redis, $host, $port, $auth, $serializer, $dbIndex);
Expand Down
0