Fetch updated slots map from node indicated by -MOVED response.

This optimization makes it possible to fetch the slots map directly
from the server indicated by the -MOVED response eliminating the need
to use a random node in the pool, which in turn could require Predis
to open a new and useless connection.
This commit is contained in:
Daniele Alessandri
2014-07-21 12:59:22 +02:00
parent 217bd69b7f
commit 4ab2bcea64
2 changed files with 13 additions and 13 deletions
+7 -5
View File
@@ -175,14 +175,16 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable
}
/**
* Generates the current slots map by fetching the cluster configuration to
* one of the nodes by leveraging the CLUSTER NODES command.
* Generates an updated slots map fetching the cluster configuration using
* the CLUSTER NODES command against the specified node or a random one from
* the pool.
*
* @param NodeConnectionInterface $connection Optional connection instance.
* @return array
*/
public function askSlotsMap()
public function askSlotsMap(NodeConnectionInterface $connection = null)
{
if (!$connection = $this->getRandomConnection()) {
if (!$connection && !$connection = $this->getRandomConnection()) {
return array();
}
@@ -408,7 +410,7 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable
}
if ($this->askSlotsMap) {
$this->askSlotsMap();
$this->askSlotsMap($connection);
}
$this->move($connection, $slot);
@@ -680,25 +680,23 @@ class RedisClusterTest extends PredisTestCase
{
$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))
$connection1->expects($this->once())
->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())
$connection2->expects($this->at(0))
->method('executeCommand')
->with($this->isRedisCommand('CLUSTER', array('SLOTS')))
->will($this->returnValue($rspSlotsArray));
$connection2->expects($this->at(2))
->method('executeCommand')
->with($cmdGET)
->will($this->returnValue('foobar'));