فهرست منبع

Aggregated connection for redis-cluster now uses 16384 hash slots.

This commit reflects the recent change from the redis unstable branch
in which the number of hash slots was increased from 4096 to 16384.

See https://github.com/antirez/redis/commit/ebd666d for reference.
Daniele Alessandri 12 سال پیش
والد
کامیت
c354d02105
3فایلهای تغییر یافته به همراه34 افزوده شده و 30 حذف شده
  1. 4 0
      CHANGELOG.md
  2. 4 4
      lib/Predis/Connection/RedisCluster.php
  3. 26 26
      tests/Predis/Connection/RedisClusterTest.php

+ 4 - 0
CHANGELOG.md

@@ -7,6 +7,10 @@ v0.8.3 (2013-xx-xx)
   `phpiredis` extension just like `Predis\Connection\PhpiredisStreamConnection`
   but does not require the `socket` extension since it relies on PHP's stream.
 
+- Updated the aggregated connection class for redis-cluster to work with 16384
+  hash slots instead of 4096 to reflect this recent change from redis unstable
+  ([see this commit](https://github.com/antirez/redis/commit/ebd666d)).
+
 
 v0.8.2 (2013-02-03)
 ===============================================================================

+ 4 - 4
lib/Predis/Connection/RedisCluster.php

@@ -135,7 +135,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
     public function buildSlotsMap()
     {
         $this->slotsMap = array();
-        $this->slotsPerNode = (int) (4096 / count($this->pool));
+        $this->slotsPerNode = (int) (16384 / count($this->pool));
 
         foreach ($this->pool as $connectionID => $connection) {
             $parameters = $connection->getParameters();
@@ -177,7 +177,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
      */
     public function setSlots($first, $last, $connection)
     {
-        if ($first < 0 || $first > 4095 || $last < 0 || $last > 4095 || $last < $first) {
+        if ($first < 0x0000 || $first > 0x3FFF || $last < 0x0000 || $last > 0x3FFF || $last < $first) {
             throw new \OutOfBoundsException("Invalid slot values for $connection: [$first-$last]");
         }
 
@@ -195,7 +195,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
             throw new NotSupportedException("Cannot use {$command->getId()} with redis-cluster");
         }
 
-        $slot = $hash & 4095; // 0x0FFF
+        $slot = $hash & 0x3FFF;
 
         if (isset($this->slots[$slot])) {
             return $this->slots[$slot];
@@ -214,7 +214,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
      */
     public function getConnectionBySlot($slot)
     {
-        if ($slot < 0 || $slot > 4095) {
+        if ($slot < 0x0000 || $slot > 0x3FFF) {
             throw new \OutOfBoundsException("Invalid slot value [$slot]");
         }
 

+ 26 - 26
tests/Predis/Connection/RedisClusterTest.php

@@ -279,9 +279,9 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testCanAssignConnectionsToCustomSlotsFromParameters()
     {
-        $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
-        $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
-        $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
+        $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460');
+        $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-10921');
+        $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-16383');
 
         $cluster = new RedisCluster();
         $cluster->add($connection1);
@@ -289,9 +289,9 @@ class RedisClusterTest extends StandardTestCase
         $cluster->add($connection3);
 
         $expectedMap = array_merge(
-            array_fill(0, 1365, '127.0.0.1:6379'),
-            array_fill(1364, 1365, '127.0.0.1:6380'),
-            array_fill(2729, 1366, '127.0.0.1:6381')
+            array_fill(0, 5461, '127.0.0.1:6379'),
+            array_fill(5460, 5461, '127.0.0.1:6380'),
+            array_fill(10921, 5462, '127.0.0.1:6381')
         );
 
         $cluster->buildSlotsMap();
@@ -314,11 +314,11 @@ class RedisClusterTest extends StandardTestCase
         $cluster->add($connection3);
 
         $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
-        $this->assertSame($connection2, $cluster->getConnectionBySlot(1365));
-        $this->assertSame($connection3, $cluster->getConnectionBySlot(2730));
+        $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
+        $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
 
-        $cluster->setSlots(1365, 3000, '127.0.0.1:6380');
-        $this->assertSame($connection2, $cluster->getConnectionBySlot(2730));
+        $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
+        $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
     }
 
     /**
@@ -337,8 +337,8 @@ class RedisClusterTest extends StandardTestCase
         $cluster->add($connection2);
         $cluster->add($connection3);
 
-        $set = $profile->createCommand('set', array('node:1024', 'foobar'));
-        $get = $profile->createCommand('get', array('node:1024'));
+        $set = $profile->createCommand('set', array('node:1001', 'foobar'));
+        $get = $profile->createCommand('get', array('node:1001'));
         $this->assertSame($connection1, $cluster->getConnection($set));
         $this->assertSame($connection1, $cluster->getConnection($get));
 
@@ -358,7 +358,7 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testWritesCommandToCorrectConnection()
     {
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->once())->method('writeCommand')->with($command);
@@ -378,7 +378,7 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testReadsCommandFromCorrectConnection()
     {
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1048'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1050'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->never())->method('readResponse');
@@ -407,13 +407,13 @@ class RedisClusterTest extends StandardTestCase
         $cluster->add($connection1);
         $cluster->add($connection2);
 
-        $set = $profile->createCommand('set', array('{node:1024}:foo', 'foobar'));
-        $get = $profile->createCommand('get', array('{node:1024}:foo'));
+        $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
+        $get = $profile->createCommand('get', array('{node:1001}:foo'));
         $this->assertSame($connection1, $cluster->getConnection($set));
         $this->assertSame($connection1, $cluster->getConnection($get));
 
-        $set = $profile->createCommand('set', array('{node:1024}:bar', 'foobar'));
-        $get = $profile->createCommand('get', array('{node:1024}:bar'));
+        $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
+        $get = $profile->createCommand('get', array('{node:1001}:bar'));
         $this->assertSame($connection2, $cluster->getConnection($set));
         $this->assertSame($connection2, $cluster->getConnection($get));
     }
@@ -423,9 +423,9 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testAskResponseWithConnectionInPool()
     {
-        $askResponse = new ResponseError('ASK 373 127.0.0.1:6380');
+        $askResponse = new ResponseError('ASK 1970 127.0.0.1:6380');
 
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->exactly(2))
@@ -456,9 +456,9 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testAskResponseWithConnectionNotInPool()
     {
-        $askResponse = new ResponseError('ASK 373 127.0.0.1:6381');
+        $askResponse = new ResponseError('ASK 1970 127.0.0.1:6381');
 
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->exactly(2))
@@ -496,9 +496,9 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testMovedResponseWithConnectionInPool()
     {
-        $movedResponse = new ResponseError('MOVED 373 127.0.0.1:6380');
+        $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6380');
 
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->exactly(1))
@@ -530,9 +530,9 @@ class RedisClusterTest extends StandardTestCase
      */
     public function testMovedResponseWithConnectionNotInPool()
     {
-        $movedResponse = new ResponseError('MOVED 373 127.0.0.1:6381');
+        $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6381');
 
-        $command = ServerProfile::getDefault()->createCommand('get', array('node:1024'));
+        $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
 
         $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
         $connection1->expects($this->once())