Skip to content

Commit

Permalink
Merge pull request #65 from tioncico/master
Browse files Browse the repository at this point in the history
修复redis 集群一个节点同时存在多个槽位时无法识别的bug
  • Loading branch information
tioncico authored Dec 24, 2020
2 parents aff6e52 + 48f6ed5 commit 6737732
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
7 changes: 1 addition & 6 deletions phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

defined("REDIS_CLUSTER_SERVER_LIST") ?: define('REDIS_CLUSTER_SERVER_LIST',
[
['127.0.0.1', 6380],
['127.0.0.1', 6381],
['127.0.0.1', 6382],
['127.0.0.1', 6383],
['127.0.0.1', 6384],
['127.0.0.1', 6385],
['127.0.0.1', 9001],
]);
defined("REDIS_CLUSTER_AUTH") ?: define('REDIS_CLUSTER_AUTH', '');
13 changes: 12 additions & 1 deletion src/CommandHandel/ClusterCommand/ClusterNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ public function handelRecv(Response $recv)
'port' => $port,
'flags' => $data[2],
'connected' => $data[7],
'slot' => isset($data[8]) ? explode('-', $data[8]) : [],
'slotList' => $this->getSlotList($data),
];
$nodeList[$node['name']] = $node;
}
return $nodeList;
}

protected function getSlotList($data)
{
$slotList = [];
//获取8后面的数据
$slotOriginalData = array_slice($data, 8);
foreach ($slotOriginalData as $slotString) {
$slotList[] = explode('-', $slotString);
}
return $slotList;
}
}
9 changes: 5 additions & 4 deletions src/RedisCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,17 @@ public function getMoveNodeId(Response $response)
public function getSlotNodeId($slotId)
{
foreach ($this->nodeList as $key => $node) {
if (empty($node['slot'])) {
if (empty($node['slotList'])) {
continue;
}

if (strpos($node['flags'], 'master') === false) {
continue;
}

if ($node['slot'][0] <= $slotId && $node['slot'][1] >= $slotId) {
return $key;
foreach ($node['slotList'] as $slot){
if ($slot[0] <= $slotId && $slot[1] >= $slotId) {
return $key;
}
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/RedisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RedisClusterTest extends TestCase
*/
protected $redisPHPSerialize;

protected function setUp()
protected function setUp():void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
Expand Down
2 changes: 1 addition & 1 deletion tests/UnixSocketRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UnixSocketRedisTest extends TestCase
*/
protected $redis;

protected function setUp()
protected function setUp():void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->redis = new Redis(new RedisConfig([
Expand Down

0 comments on commit 6737732

Please sign in to comment.