Permalink
Browse files

Fix and optimize node guessing algorithm.

  • Loading branch information...
1 parent f4f9d02 commit b9eddae96f423b88f560aa296a996b82dbc20a34 @nrk committed Jul 30, 2012
Showing with 18 additions and 6 deletions.
  1. +18 −6 lib/Predis/Connection/RedisCluster.php
@@ -28,6 +28,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
private $pool;
private $slots;
private $slotsMap;
+ private $slotsPerNode;
private $connections;
private $distributor;
private $cmdHasher;
@@ -84,7 +85,10 @@ public function disconnect()
public function add(SingleConnectionInterface $connection)
{
$this->pool[(string) $connection] = $connection;
- unset($this->slotsMap);
+ unset(
+ $this->slotsMap,
+ $this->slotsPerNode
+ );
}
/**
@@ -93,8 +97,11 @@ public function add(SingleConnectionInterface $connection)
public function remove(SingleConnectionInterface $connection)
{
if (($id = array_search($connection, $this->pool, true)) !== false) {
- unset($this->pool[$id]);
- unset($this->slotsMap);
+ unset(
+ $this->pool[$id],
+ $this->slotsMap,
+ $this->slotsPerNode
+ );
return true;
}
@@ -111,7 +118,11 @@ public function remove(SingleConnectionInterface $connection)
public function removeById($connectionId)
{
if (isset($this->pool[$connectionId])) {
- unset($this->pool[$connectionId]);
+ unset(
+ $this->pool[$connectionId],
+ $this->slotsMap,
+ $this->slotsPerNode
+ );
return true;
}
@@ -125,6 +136,7 @@ public function removeById($connectionId)
public function buildSlotsMap()
{
$this->slotsMap = array();
+ $this->slotsPerNode = (int) (4096 / count($this->pool));
foreach ($this->pool as $connectionID => $connection) {
$parameters = $connection->getParameters();
@@ -203,10 +215,10 @@ protected function guessNode($slot)
return $this->slotsMap[$slot];
}
- $index = $slot / (int) (4096 / count($this->pool));
+ $index = min((int) ($slot / $this->slotsPerNode), count($this->pool) - 1);
$nodes = array_keys($this->pool);
- return $nodes[min($index, 4095)];
+ return $nodes[$index];
}
/**

0 comments on commit b9eddae

Please sign in to comment.