Skip to content

Commit 912af82

Browse files
authored
Added support for CLUSTER container command (#1360)
* Added support for CLUSTER container command * Codestyle fixes
1 parent 2d74db1 commit 912af82

File tree

9 files changed

+212
-0
lines changed

9 files changed

+212
-0
lines changed

src/ClientContextInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Predis\Command\Argument\TimeSeries\RangeArguments;
4040
use Predis\Command\CommandInterface;
4141
use Predis\Command\Redis\Container\ACL;
42+
use Predis\Command\Redis\Container\CLUSTER;
4243
use Predis\Command\Redis\Container\FunctionContainer;
4344
use Predis\Command\Redis\Container\Json\JSONDEBUG;
4445
use Predis\Command\Redis\Container\Search\FTCONFIG;
@@ -340,6 +341,7 @@
340341
* @method $this geosearchstore(string $destination, string $source, FromInterface $from, ByInterface $by, ?string $sorting = null, int $count = -1, bool $any = false, bool $storeDist = false)
341342
*
342343
* Container commands
344+
* @property CLUSTER $cluster
343345
* @property FunctionContainer $function
344346
* @property FTCONFIG $ftconfig
345347
* @property FTCURSOR $ftcursor

src/ClientInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Predis\Command\CommandInterface;
4141
use Predis\Command\FactoryInterface;
4242
use Predis\Command\Redis\Container\ACL;
43+
use Predis\Command\Redis\Container\CLUSTER;
4344
use Predis\Command\Redis\Container\FunctionContainer;
4445
use Predis\Command\Redis\Container\Json\JSONDEBUG;
4546
use Predis\Command\Redis\Container\Search\FTCONFIG;
@@ -358,6 +359,7 @@
358359
* @method int geosearchstore(string $destination, string $source, FromInterface $from, ByInterface $by, ?string $sorting = null, int $count = -1, bool $any = false, bool $storeDist = false)
359360
*
360361
* Container commands
362+
* @property CLUSTER $cluster
361363
* @property FunctionContainer $function
362364
* @property FTCONFIG $ftconfig
363365
* @property FTCURSOR $ftcursor

src/Cluster/ClusterStrategy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ protected function getDefaultCommands()
174174
'GEODIST' => $getKeyFromFirstArgument,
175175
'GEORADIUS' => [$this, 'getKeyFromGeoradiusCommands'],
176176
'GEORADIUSBYMEMBER' => [$this, 'getKeyFromGeoradiusCommands'],
177+
178+
/* cluster */
179+
'CLUSTER' => [$this, 'getFakeKey'],
177180
];
178181
}
179182

src/Command/Redis/CLUSTER.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2023 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis;
14+
15+
use Predis\Command\Command as RedisCommand;
16+
17+
/**
18+
* @see https://redis.io/commands/?name=cluster
19+
*/
20+
class CLUSTER extends RedisCommand
21+
{
22+
public function getId()
23+
{
24+
return 'CLUSTER';
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2023 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis\Container;
14+
15+
use Predis\Response\Status;
16+
17+
/**
18+
* @method Status addSlotsRange(int ...$startEndSlots)
19+
* @method Status delSlotsRange(int ...$startEndSlots)
20+
* @method array links()
21+
* @method array shards()
22+
*/
23+
class CLUSTER extends AbstractContainer
24+
{
25+
public function getContainerCommandId(): string
26+
{
27+
return 'CLUSTER';
28+
}
29+
}

tests/Predis/Cluster/PredisStrategyTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ protected function getExpectedCommands(string $type = null): array
463463
'GEODIST' => 'keys-first',
464464
'GEORADIUS' => 'keys-georadius',
465465
'GEORADIUSBYMEMBER' => 'keys-georadius',
466+
467+
/* cluster */
468+
'CLUSTER' => 'keys-fake',
466469
];
467470

468471
if (isset($type)) {

tests/Predis/Cluster/RedisStrategyTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ protected function getExpectedCommands(string $type = null): array
486486
'GEODIST' => 'keys-first',
487487
'GEORADIUS' => 'keys-georadius',
488488
'GEORADIUSBYMEMBER' => 'keys-georadius',
489+
490+
/* cluster */
491+
'CLUSTER' => 'keys-fake',
489492
];
490493

491494
if (isset($type)) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2023 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis;
14+
15+
class CLUSTER_Test extends PredisCommandTestCase
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
protected function getExpectedCommand(): string
21+
{
22+
return CLUSTER::class;
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
protected function getExpectedId(): string
29+
{
30+
return 'CLUSTER';
31+
}
32+
33+
/**
34+
* @group disconnected
35+
*/
36+
public function testFilterArgumentsOfAddSlotsRange(): void
37+
{
38+
$arguments = ['ADDSLOTSRANGE', 1, 1000];
39+
$expected = ['ADDSLOTSRANGE', 1, 1000];
40+
41+
$command = $this->getCommand();
42+
$command->setArguments($arguments);
43+
44+
$this->assertSame($expected, $command->getArguments());
45+
}
46+
47+
/**
48+
* @group disconnected
49+
*/
50+
public function testFilterArgumentsOfDelSlotsRange(): void
51+
{
52+
$arguments = ['DELSLOTSRANGE', 1, 1000];
53+
$expected = ['DELSLOTSRANGE', 1, 1000];
54+
55+
$command = $this->getCommand();
56+
$command->setArguments($arguments);
57+
58+
$this->assertSame($expected, $command->getArguments());
59+
}
60+
61+
/**
62+
* @group disconnected
63+
*/
64+
public function testFilterArgumentsOfLinks(): void
65+
{
66+
$arguments = ['LINKS'];
67+
$expected = ['LINKS'];
68+
69+
$command = $this->getCommand();
70+
$command->setArguments($arguments);
71+
72+
$this->assertSame($expected, $command->getArguments());
73+
}
74+
75+
/**
76+
* @group disconnected
77+
*/
78+
public function testFilterArgumentsOfShards(): void
79+
{
80+
$arguments = ['SHARDS'];
81+
$expected = ['SHARDS'];
82+
83+
$command = $this->getCommand();
84+
$command->setArguments($arguments);
85+
86+
$this->assertSame($expected, $command->getArguments());
87+
}
88+
89+
/**
90+
* @group connected
91+
* @group cluster
92+
* @return void
93+
* @requiresRedisVersion >= 7.0.0
94+
*/
95+
public function testAddSlotsRangeToGivenNode(): void
96+
{
97+
$redis = $this->getClient();
98+
99+
[$startSlot, $endSlot] = $redis->cluster->shards()[0][1];
100+
101+
$this->assertEquals('OK', $redis->cluster->delSlotsRange($startSlot, $endSlot));
102+
$this->assertEquals('OK', $redis->cluster->addSlotsRange($startSlot, $endSlot));
103+
}
104+
105+
/**
106+
* @group connected
107+
* @group cluster
108+
* @return void
109+
* @requiresRedisVersion >= 7.0.0
110+
*/
111+
public function testLinksReturnsClusterPeerLinks(): void
112+
{
113+
$redis = $this->getClient();
114+
115+
$this->assertNotEmpty($redis->cluster->links());
116+
}
117+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Predis package.
5+
*
6+
* (c) 2009-2020 Daniele Alessandri
7+
* (c) 2021-2023 Till Krüss
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Predis\Command\Redis\Container;
14+
15+
use Predis\ClientInterface;
16+
use PredisTestCase;
17+
18+
class CLUSTER_Test extends PredisTestCase
19+
{
20+
public function testGetContainerCommandId(): void
21+
{
22+
$mockClient = $this->getMockBuilder(ClientInterface::class)->getMock();
23+
$command = new CLUSTER($mockClient);
24+
25+
$this->assertSame('CLUSTER', $command->getContainerCommandId());
26+
}
27+
}

0 commit comments

Comments
 (0)