8000 feature #48930 [Cache] Add Redis Relay support (ostrolucky) · symfony/symfony@c223437 · GitHub
[go: up one dir, main page]

Skip to content

Commit c223437

Browse files
feature #48930 [Cache] Add Redis Relay support (ostrolucky)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Cache] Add Redis Relay support | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? |no | Tickets | | License | MIT | Doc PR | This PR adds support for [Relay](https://relay.so/), a next-gen Redis client written in C by the makers of PhpRedis and Predis. It’s built as a drop-in replacement for PhpRedis with a backwards compatible interface for easy adoption. Relay is significantly faster than existing clients by leveraging Redis 6's client-side-caching. While Relay is still on 0.x (pending the addition of cluster support in a few weeks for a 1.0 tag), it’s interface is stable and it’s heavily used in production deployments. Similarly, I've also added Relay support to most popular symfony redis bundle, [snc/redis-bundle](snc/SncRedisBundle#688). But to be able to support these new Redis instances in Symfony components as a Cache, Lock and so on, support had to be added here as well. Since method and constant declarations are compatible with PhpRedis, I've opted into reusing most of the 8000 code instead of creating completely new adapters, similarly as was done in case of Predis+PhpRedis. At the same time, I made it a goal not having to have PhpRedis installed in case somebody wishes to use Relay, which explains things like conditional fetch of value constants. Commits ------- c1e5035 [VarDumper] Add Relay support 327969e [redis-messenger] Add Relay support 1d7b409 [Semaphore] Accept Relay connection 0366a32 [HttpFoundation] Accept Relay connection a4c2ca8 [Lock] Accept Relay connection 4173c38 [Cache] Add Relay support
2 parents 9572cae + c1e5035 commit c223437

35 files changed

+1715
-96
lines changed

.github/patch-types.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php'):
5555
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionUnionTypeWithIntersectionFixture.php'):
5656
case false !== strpos($file, '/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/ReadOnlyClass.php'):
57+
case false !== strpos($file, '/src/Symfony/Component/Cache/Traits/RelayProxy.php'):
5758
continue 2;
5859
}
5960

.github/workflows/integration-tests.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,19 @@ jobs:
134134
uses: shivammathur/setup-php@v2
135135
with:
136136
coverage: "none"
137-
extensions: "json,couchbase-3.2.2,memcached,mongodb-1.12.0,redis,rdkafka,xsl,ldap"
137+
extensions: "json,couchbase-3.2.2,memcached,mongodb-1.12.0,redis,rdkafka,xsl,ldap,msgpack,igbinary"
138138
ini-values: date.timezone=UTC,memory_limit=-1,default_socket_timeout=10,session.gc_probability=0,apc.enable_cli=1,zend.assertions=1
139139
php-version: "${{ matrix.php }}"
140140
tools: pecl
141141

142+
- name: Install Relay
143+
run: |
144+
curl -L "https://builds.r2.relay.so/dev/relay-dev-php${{ matrix.php }}-debian-x86-64.tar.gz" | tar xz
145+
cd relay-dev-php${{ matrix.php }}-debian-x86-64
146+
sudo cp relay.ini $(php-config --ini-dir)
147+
sudo cp relay-pkg.so $(php-config --extension-dir)/relay.so
148+
sudo sed -i "s/00000000-0000-0000-0000-000000000000/$(cat /proc/sys/kernel/random/uuid)/" $(php-config --extension-dir)/relay.so
149+
142150
- name: Display versions
143151
run: |
144152
php -r 'foreach (get_loaded_extensions() as $extension) echo $extension . " " . phpversion($extension) . PHP_EOL;'

.github/workflows/psalm.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ jobs:
2828
ini-values: "memory_limit=-1"
2929
coverage: none
3030

31+
- name: Install Relay
32+
run: |
33+
curl -L "https://builds.r2.relay.so/dev/relay-dev-php8.1-debian-x86-64.tar.gz" | tar xz
34+
cd relay-dev-php8.1-debian-x86-64
35+
sudo cp relay.ini $(php-config --ini-dir)
36+
sudo cp relay-pkg.so $(php-config --extension-dir)/relay.so
37+
sudo sed -i "s/00000000-0000-0000-0000-000000000000/$(cat /proc/sys/kernel/random/uuid)/" $(php-config --extension-dir)/relay.so
38+
3139
- name: Checkout target branch
3240
uses: actions/checkout@v3
3341
with:

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
// stop removing spaces on the end of the line in strings
6868
->notPath('Symfony/Component/Messenger/Tests/Command/FailedMessagesShowCommandTest.php')
6969
// auto-generated proxies
70+
->notPath('Symfony/Component/Cache/Traits/RelayProxy.php')
7071
->notPath('Symfony/Component/Cache/Traits/Redis5Proxy.php')
7172
->notPath('Symfony/Component/Cache/Traits/Redis6Proxy.php')
7273
->notPath('Symfony/Component/Cache/Traits/RedisCluster5Proxy.php')

src/Symfony/Component/Cache/Adapter/RedisAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RedisAdapter extends AbstractAdapter
1818
{
1919
use RedisTrait;
2020

21-
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
21+
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|\Relay\Relay $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
2222
{
2323
$this->init($redis, $namespace, $defaultLifetime, $marshaller);
2424
}

src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Predis\Connection\Aggregate\ReplicationInterface;
1717
use Predis\Response\ErrorInterface;
1818
use Predis\Response\Status;
19+
use Relay\Relay;
1920
use Symfony\Component\Cache\CacheItem;
2021
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2122
use Symfony\Component\Cache\Exception\LogicException;
@@ -59,18 +60,19 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
5960
private string $redisEvictionPolicy;
6061
private string $namespace;
6162

62-
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
63+
public function __construct(\Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
6364
{
6465
if ($redis instanceof \Predis\ClientInterface && $redis->getConnection() instanceof ClusterInterface && !$redis->getConnection() instanceof PredisCluster) {
6566
throw new InvalidArgumentException(sprintf('Unsupported Predis cluster connection: only "%s" is, "%s" given.', PredisCluster::class, get_debug_type($redis->getConnection())));
6667
}
6768

68-
if (\defined('Redis::OPT_COMPRESSION') && \in_array($redis::class, [\Redis::class, \RedisArray::class, \RedisCluster::class], true)) {
69-
$compression = $redis->getOption(\Redis::OPT_COMPRESSION);
69+
$isRelay = $redis instanceof Relay;
70+
if ($isRelay || \defined('Redis::OPT_COMPRESSION') && \in_array($redis::class, [\Redis::class, \RedisArray::class, \RedisCluster::class], true)) {
71+
$compression = $redis->getOption($isRelay ? Relay::OPT_COMPRESSION : \Redis::OPT_COMPRESSION);
7072

7173
foreach (\is_array($compression) ? $compression : [$compression] as $c) {
72-
if (\Redis::COMPRESSION_NONE !== $c) {
73-
throw new InvalidArgumentException(sprintf('phpredis compression must be disabled when using "%s", use "%s" instead.', static::class, DeflateMarshaller::class));
74+
if ($isRelay ? Relay::COMPRESSION_NONE : \Redis::COMPRESSION_NONE !== $c) {
75+
throw new InvalidArgumentException(sprintf('redis compression must be disabled when using "%s", use "%s" instead.', static::class, DeflateMarshaller::class));
7476
}
7577
}
7678
}
@@ -154,7 +156,7 @@ protected function doDeleteYieldTags(array $ids): iterable
154156
});
155157

156158
foreach ($results as $id => $result) {
157-
if ($result instanceof \RedisException || $result instanceof ErrorInterface) {
159+
if ($result instanceof \RedisException || $result instanceof \Relay\Exception || $result instanceof ErrorInterface) {
158160
CacheItem::log($this->logger, 'Failed to delete key "{key}": '.$result->getMessage(), ['key' => substr($id, \strlen($this->namespace)), 'exception' => $result]);
159161

160162
continue;
@@ -221,7 +223,7 @@ protected function doInvalidate(array $tagIds): bool
221223
$results = $this->pipeline(function () use ($tagIds, $lua) {
222224
if ($this->redis instanceof \Predis\ClientInterface) {
223225
$prefix = $this->redis->getOptions()->prefix ? $this->redis->getOptions()->prefix->getPrefix() : '';
224-
} elseif (\is_array($prefix = $this->redis->getOption(\Redis::OPT_PREFIX) ?? '')) {
226+
} elseif (\is_array($prefix = $this->redis->getOption($this->redis instanceof Relay ? Relay::OPT_PREFIX : \Redis::OPT_PREFIX) ?? '')) {
225227
$prefix = current($prefix);
226228
}
227229

@@ -242,7 +244,7 @@ protected function doInvalidate(array $tagIds): bool
242244

243245
$success = true;
244246
foreach ($results as $id => $values) {
245-
if ($values instanceof \RedisException || $values instanceof ErrorInterface) {
247+
if ($values instanceof \RedisException || $values instanceof \Relay\Exception || $values instanceof ErrorInterface) {
246248
CacheItem::log($this->logger, 'Failed to invalidate key "{key}": '.$values->getMessage(), ['key' => substr($id, \strlen($this->namespace)), 'exception' => $values]);
247249
$success = false;
248250

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add support for Relay PHP extension for Redis
8+
49
6.1
510
---
611

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use PHPUnit\Framework\SkippedTestSuiteError;
15+
use Relay\Relay;
16+
use Relay\Sentinel;
17+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
18+
19+
/**
20+
* @group integration
21+
*/
22+
class RelayAdapterSentinelTest extends AbstractRedisAdapterTest
23+
{
24+
public static function setUpBeforeClass(): void
25+
{
26+
if (!class_exists(Sentinel::class)) {
27+
throw new SkippedTestSuiteError('The Relay\Sentinel class is required.');
28+
}
29+
if (!$hosts = getenv('REDIS_SENTINEL_HOSTS')) {
30+
throw new SkippedTestSuiteError('REDIS_SENTINEL_HOSTS env var is not defined.');
31+
}
32+
if (!$service = getenv('REDIS_SENTINEL_SERVICE')) {
33+
throw new SkippedTestSuiteError('REDIS_SENTINEL_SERVICE env var is not defined.');
34+
}
35+
36+
self::$redis = AbstractAdapter::createConnection(
37+
'redis:?host['.str_replace(' ', ']&host[', $hosts).']',
38+
['redis_sentinel' => $service, 'prefix' => 'prefix_', 'class' => Relay::class],
39+
);
40+
self::assertInstanceOf(Relay::class, self::$redis);
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use PHPUnit\Framework\SkippedTestSuiteError;
15+
use Relay\Relay;
16+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
17+
use Symfony\Component\Cache\Adapter\RedisAdapter;
18+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
19+
use Symfony\Component\Cache\Traits\RelayProxy;
20+
21+
/**
22+
* @requires extension relay
23+
*
24+
* @group integration
25+
*/
26+
class RelayAdapterTest extends AbstractRedisAdapterTest
27+
{
28+
public static function setUpBeforeClass(): void
29+
{
30+
try {
31+
new Relay(...explode(':', getenv('REDIS_HOST')));
32+
} catch (\Relay\Exception $e) {
33+
throw new SkippedTestSuiteError(getenv('REDIS_HOST').': '.$e->getMessage());
34+
}
35+
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), ['lazy' => true, 'class' => Relay::class]);
36+
self::assertInstanceOf(RelayProxy::class, self::$redis);
37+
}
38+
39+
public function testCreateHostConnection()
40+
{
41+
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST').'?class=Relay\Relay');
42+
$this->assertInstanceOf(Relay::class, $redis);
43+
$this->assertTrue($redis->isConnected());
44+
$this->assertSame(0, $redis->getDbNum());
45+
}
46+
47+
public function testLazyConnection()
48+
{
49+
$redis = RedisAdapter::createConnection('redis://nonexistenthost?class=Relay\Relay&lazy=1');
50+
$this->assertInstanceOf(RelayProxy::class, $redis);
51+
// no exception until now
52+
$this->expectException(InvalidArgumentException::class);
53+
$this->expectExceptionMessage('Failed to resolve host address');
54+
$redis->getHost(); // yep, only here exception is thrown
55+
}
56+
}

src/Symfony/Component/Cache/Tests/Traits/RedisProxiesTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
namespace Symfony\Component\Cache\Tests\Traits;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Relay\Relay;
1516
use Symfony\Component\VarExporter\LazyProxyTrait;
1617
use Symfony\Component\VarExporter\ProxyHelper;
1718

18-
/**
19-
* @requires extension redis
20-
*/
2119
class RedisProxiesTest extends TestCase
2220
{
2321
/**
22+
* @requires extension redis
23+
*
2424
* @testWith ["Redis"]
2525
* ["RedisCluster"]
2626
48DA */
@@ -50,6 +50,36 @@ public function testRedis5Proxy($class)
5050
}
5151

5252
/**
53+
* @requires extension relay
54+
*/
55+
public function testRelayProxy()
56+
{
57+
$proxy = file_get_contents(\dirname(__DIR__, 2).'/Traits/RelayProxy.php');
58+
$proxy = substr($proxy, 0, 8 + strpos($proxy, "\n ];"));
59+
$methods = [];
60+
61+
foreach ((new \ReflectionClass(Relay::class))->getMethods() as $method) {
62+
if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name) || $method->isStatic()) {
63+
continue;
64+
}
65+
$return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
66+
$methods[] = "\n ".ProxyHelper::exportSignature($method, false)."\n".<<<EOPHP
67+
{
68+
{$return}\$this->lazyObjectReal->{$method->name}(...\\func_get_args());
69+
}
70+
71+
EOPHP;
72+
}
73+
74+
uksort($methods, 'strnatcmp');
75+
$proxy .= implode('', $methods)."}\n";
76+
77+
$this->assertStringEqualsFile(\dirname(__DIR__, 2).'/Traits/RelayProxy.php', $proxy);
78+
}
79+
80+
/**
81+
* @requires extension redis
82+
*
5383
* @testWith ["Redis", "redis"]
5484
* ["RedisCluster", "redis_cluster"]
5585
*/

0 commit comments

Comments
 (0)
0