From bd00b8cfe2fed9ff0985b20afc7b1f7d81d1fd08 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Mon, 21 Jul 2014 12:42:26 +0200 Subject: [PATCH] Fetch cluster slots from node upon first -MOVED response. This is a more sane default as it allows users to indicate only a few servers of the whole cluster composition, while it previously required a more complex configuration of the client using client options. This feature can be disabled using the "enableAutoSlotsMap()" method. --- src/Connection/Aggregate/RedisCluster.php | 2 +- .../Connection/Aggregate/RedisClusterTest.php | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Connection/Aggregate/RedisCluster.php b/src/Connection/Aggregate/RedisCluster.php index 03150b1c..9b5f0b53 100644 --- a/src/Connection/Aggregate/RedisCluster.php +++ b/src/Connection/Aggregate/RedisCluster.php @@ -49,7 +49,7 @@ use Predis\Response\ErrorInterface as ErrorResponseInterface; */ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable { - private $askSlotsMap = false; + private $askSlotsMap = true; private $defaultParameters = array(); private $pool = array(); private $slots = array(); diff --git a/tests/Predis/Connection/Aggregate/RedisClusterTest.php b/tests/Predis/Connection/Aggregate/RedisClusterTest.php index 28d7fd95..0017b406 100644 --- a/tests/Predis/Connection/Aggregate/RedisClusterTest.php +++ b/tests/Predis/Connection/Aggregate/RedisClusterTest.php @@ -422,6 +422,7 @@ class RedisClusterTest extends PredisTestCase $connection2->expects($this->never())->method('writeRequest'); $cluster = new RedisCluster(); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -442,6 +443,7 @@ class RedisClusterTest extends PredisTestCase $connection2->expects($this->once())->method('readResponse')->with($command); $cluster = new RedisCluster(); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -501,6 +503,7 @@ class RedisClusterTest extends PredisTestCase $factory->expects($this->never())->method('create'); $cluster = new RedisCluster($factory); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -544,6 +547,7 @@ class RedisClusterTest extends PredisTestCase ->will($this->returnValue($connection3)); $cluster = new RedisCluster($factory); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -577,6 +581,7 @@ class RedisClusterTest extends PredisTestCase $factory->expects($this->never())->method('create'); $cluster = new RedisCluster($factory); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -617,6 +622,7 @@ class RedisClusterTest extends PredisTestCase ->will($this->returnValue($connection3)); $cluster = new RedisCluster($factory); + $cluster->enableAutoSlotsMap(false); $cluster->add($connection1); $cluster->add($connection2); @@ -667,6 +673,49 @@ class RedisClusterTest extends PredisTestCase $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1); } + /** + * @group disconnected + */ + public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault() + { + $cmdGET = Command\RawCommand::create('GET', 'node:1001'); + $rspMOVED = new Response\Error('MOVED 1970 127.0.0.1:6380'); + + $cmdCLUSTER = Command\RawCommand::create('CLUSTER', 'SLOTS'); + $rspSlotsArray = array( + array(0 , 8191, array('127.0.0.1', 6379)), + array(8192, 16383, array('127.0.0.1', 6380)), + ); + + $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379'); + $connection1->expects($this->at(2)) + ->method('executeCommand') + ->with($cmdGET) + ->will($this->returnValue($rspMOVED)); + $connection1->expects($this->at(3)) + ->method('executeCommand') + ->with($cmdCLUSTER) + ->will($this->returnValue($rspSlotsArray)); + + $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380'); + $connection2->expects($this->once()) + ->method('executeCommand') + ->with($cmdGET) + ->will($this->returnValue('foobar')); + + $factory = $this->getMock('Predis\Connection\Factory'); + $factory->expects($this->once()) + ->method('create') + ->with(array('host' => '127.0.0.1', 'port' => '6380')) + ->will($this->returnValue($connection2)); + + $cluster = new RedisCluster($factory); + $cluster->add($connection1); + + $this->assertSame('foobar', $cluster->executeCommand($cmdGET)); + $this->assertSame(2, count($cluster)); + } + /** * @group disconnected * @expectedException Predis\NotSupportedException