Skip to content

[Cache] Tests for Redis Replication with cache #59751

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 1 commit into from
Feb 11, 2025
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
12 changes: 12 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ jobs:
REDIS_MASTER_HOST: redis
REDIS_MASTER_SET: redis_sentinel
REDIS_SENTINEL_QUORUM: 1
redis-primary:
image: redis:latest
hostname: redis-primary
ports:
- 16381:6379

redis-replica:
image: redis:latest
ports:
- 16382:6379
command: redis-server --slaveof redis-primary 6379
memcached:
image: memcached:1.6.5
ports:
Expand Down Expand Up @@ -239,6 +250,7 @@ jobs:
REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005'
REDIS_SENTINEL_HOSTS: 'unreachable-host:26379 localhost:26379 localhost:26379'
REDIS_SENTINEL_SERVICE: redis_sentinel
REDIS_REPLICATION_HOSTS: 'localhost:16381 localhost:16382'
MESSENGER_REDIS_DSN: redis://127.0.0.1:7006/messages
MESSENGER_AMQP_DSN: amqp://localhost/%2f/messages
MESSENGER_SQS_DSN: "sqs://localhost:4566/messages?sslmode=disable&poll_timeout=0.01"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Symfony\Component\Cache\Adapter\RedisAdapter;

/**
* @group integration
*/
class PredisRedisReplicationAdapterTest extends AbstractRedisAdapterTestCase
{
public static function setUpBeforeClass(): void
{
if (!$hosts = getenv('REDIS_REPLICATION_HOSTS')) {
self::markTestSkipped('REDIS_REPLICATION_HOSTS env var is not defined.');
}

self::$redis = RedisAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).'][alias]=master', ['class' => \Predis\Client::class, 'prefix' => 'prefix_']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

/**
* @group integration
*/
class PredisReplicationAdapterTest extends AbstractRedisAdapterTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), ['prefix' => 'prefix_']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;

/**
* @group integration
*/
class PredisTagAwareReplicationAdapterTest extends PredisReplicationAdapterTest
{
use TagAwareTestTrait;

protected function setUp(): void
{
parent::setUp();
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

return $adapter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Traits\RedisClusterProxy;

/**
* @group integration
*/
class RedisReplicationAdapterTest extends AbstractRedisAdapterTestCase
{
public static function setUpBeforeClass(): void
{
if (!$hosts = getenv('REDIS_REPLICATION_HOSTS')) {
self::markTestSkipped('REDIS_REPLICATION_HOSTS env var is not defined.');
}

self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).'][alias]=master', ['lazy' => true]);
self::$redis->setOption(\Redis::OPT_PREFIX, 'prefix_');
}

public function createCachePool(int $defaultLifetime = 0, ?string $testMethod = null): CacheItemPoolInterface
{
if ('testClearWithPrefix' === $testMethod && \defined('Redis::SCAN_PREFIX')) {
self::$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_PREFIX);
}

$this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
$adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);

return $adapter;
}

/**
* @dataProvider provideFailedCreateConnection
*/
public function testFailedCreateConnection(string $dsn)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Redis connection ');
RedisAdapter::createConnection($dsn);
}

public static function provideFailedCreateConnection(): array
{
return [
['redis://localhost:1234'],
['redis://foo@localhost?role=master'],
['redis://localhost/123?role=master'],
];
}
}
14 changes: 11 additions & 3 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Predis\Connection\Aggregate\ReplicationInterface;
use Predis\Connection\Cluster\ClusterInterface as Predis2ClusterInterface;
use Predis\Connection\Cluster\RedisCluster as Predis2RedisCluster;
use Predis\Connection\Replication\ReplicationInterface as Predis2ReplicationInterface;
use Predis\Response\ErrorInterface;
use Predis\Response\Status;
use Relay\Relay;
Expand Down Expand Up @@ -473,9 +474,16 @@ protected function doClear(string $namespace): bool
$cleared = true;
$hosts = $this->getHosts();
$host = reset($hosts);
if ($host instanceof \Predis\Client && $host->getConnection() instanceof ReplicationInterface) {
// Predis supports info command only on the master in replication environments
$hosts = [$host->getClientFor('master')];
if ($host instanceof \Predis\Client) {
$connection = $host->getConnection();

if ($connection instanceof ReplicationInterface) {
$hosts = [$host->getClientFor('master')];
} elseif ($connection instanceof Predis2ReplicationInterface) {
$connection->switchToMaster();

$hosts = [$host];
}
}

foreach ($hosts as $host) {
Expand Down
Loading