New command: SENTINEL (Redis 2.6).

This commit is contained in:
Daniele Alessandri
2013-11-17 09:52:09 +01:00
parent c6f51e82bf
commit 1b5e2d0d0d
7 changed files with 269 additions and 8 deletions
+64
View File
@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
/**
* @link http://redis.io/topics/sentinel
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ServerSentinel extends AbstractCommand
{
/**
* {@inheritdoc}
*/
public function getId()
{
return 'SENTINEL';
}
/**
* {@inheritdoc}
*/
public function parseResponse($data)
{
switch (strtolower($this->getArgument(0))) {
case 'masters':
case 'slaves':
return self::processMastersOrSlaves($data);
default:
return $data;
}
}
/**
* Returns a processed response to SENTINEL MASTERS or SENTINEL SLAVES.
*
* @param array List of Redis servers.
* @return array
*/
protected static function processMastersOrSlaves(array $servers)
{
foreach ($servers as $idx => $node) {
$processed = array();
$count = count($node);
for ($i = 0; $i < $count; $i++) {
$processed[$node[$i]] = $node[++$i];
}
$servers[$idx] = $processed;
}
return $servers;
}
}
+1
View File
@@ -233,6 +233,7 @@ class ServerVersion26 extends ServerProfile
/* remote server control commands */
'info' => 'Predis\Command\ServerInfoV26x',
'time' => 'Predis\Command\ServerTime',
'sentinel' => 'Predis\Command\ServerSentinel',
);
}
}
+1
View File
@@ -233,6 +233,7 @@ class ServerVersion28 extends ServerProfile
/* remote server control commands */
'info' => 'Predis\Command\ServerInfoV26x',
'time' => 'Predis\Command\ServerTime',
'sentinel' => 'Predis\Command\ServerSentinel',
/* ---------------- Redis 2.8 ---------------- */
+192
View File
@@ -0,0 +1,192 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command;
use PHPUnit_Framework_TestCase as StandardTestCase;
/**
* @group commands
* @group realm-server
*/
class ServerSentinelTest extends CommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand()
{
return 'Predis\Command\ServerSentinel';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId()
{
return 'SENTINEL';
}
/**
* @group disconnected
*/
public function testFilterArguments()
{
$arguments = array('get-master-addr-by-name', 'predis:master');
$expected = array('get-master-addr-by-name', 'predis:master');
$command = $this->getCommandWithArgumentsArray($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse()
{
$expected = array('127.0.0.1', '6379');
$command = $this->getCommand();
$this->assertSame($expected, $command->parseResponse($expected));
}
/**
* @group disconnected
*/
public function testSentinelMastersResponse()
{
$response = array(
array(
"name", "predis:master",
"ip", "127.0.0.1",
"port", "6379",
"runid", "89f6128a7e5780aa6ef7d4d7022cfafbf799b3ab",
"flags", "master",
"pending-commands", "0",
"last-ok-ping-reply", "386",
"last-ping-reply", "386",
"info-refresh", "9926",
"num-slaves", "1",
"num-other-sentinels", "0",
"quorum", "2",
),
);
$expected = array(
array(
"name" => "predis:master",
"ip" => "127.0.0.1",
"port" => "6379",
"runid" => "89f6128a7e5780aa6ef7d4d7022cfafbf799b3ab",
"flags" => "master",
"pending-commands" => "0",
"last-ok-ping-reply" => "386",
"last-ping-reply" => "386",
"info-refresh" => "9926",
"num-slaves" => "1",
"num-other-sentinels" => "0",
"quorum" => "2",
),
);
$command = $this->getCommandWithArguments('masters');
$this->assertSame($expected, $command->parseResponse($response));
}
/**
* @group disconnected
*/
public function testSentinelSlavesResponse()
{
$response = array(
array(
"name", "127.0.0.1:6380",
"ip", "127.0.0.1",
"port", "6380",
"runid", "92aea60e4fead2507cccd6574e4c7139d401d0ae",
"flags", "slave",
"pending-commands", "0",
"last-ok-ping-reply", "1011",
"last-ping-reply", "1011",
"info-refresh", "4366",
"master-link-down-time", "0",
"master-link-status", "ok",
"master-host", "127.0.0.1",
"master-port", "6379",
"slave-priority", "100",
)
);
$expected = array(
array(
"name" => "127.0.0.1:6380",
"ip" => "127.0.0.1",
"port" => "6380",
"runid" => "92aea60e4fead2507cccd6574e4c7139d401d0ae",
"flags" => "slave",
"pending-commands" => "0",
"last-ok-ping-reply" => "1011",
"last-ping-reply" => "1011",
"info-refresh" => "4366",
"master-link-down-time" => "0",
"master-link-status" => "ok",
"master-host" => "127.0.0.1",
"master-port" => "6379",
"slave-priority" => "100",
)
);
$command = $this->getCommandWithArguments('slaves', 'predis:master');
$this->assertSame($expected, $command->parseResponse($response));
}
/**
* @group disconnected
*/
public function testSentinelIsMasterDownByAddr()
{
$response = array("0", "7388832d5fdee6a2e301d6bbc5052bd1526d741c");
$expected = array("0", "7388832d5fdee6a2e301d6bbc5052bd1526d741c");
$command = $this->getCommandWithArguments('is-master-down-by-addr', '127.0.0.1', '6379');
$this->assertSame($expected, $command->parseResponse($response));
}
/**
* @group disconnected
*/
public function testSentinelGetMasterAddrByName()
{
$response = array("127.0.0.1", "6379");
$expected = array("127.0.0.1", "6379");
$command = $this->getCommandWithArguments('get-master-addr-by-name', 'predis:master');
$this->assertSame($expected, $command->parseResponse($response));
}
/**
* @group disconnected
*/
public function testSentinelReset()
{
$response = 1;
$expected = 1;
$command = $this->getCommandWithArguments('reset', 'predis:*');
$this->assertSame($expected, $command->parseResponse($response));
}
}
@@ -173,6 +173,7 @@ class ServerVersion26Test extends ServerVersionTestCase
132 => 'evalsha',
133 => 'script',
134 => 'time',
135 => 'sentinel',
);
}
}
+5 -4
View File
@@ -173,10 +173,11 @@ class ServerVersion28Test extends ServerVersionTestCase
132 => 'evalsha',
133 => 'script',
134 => 'time',
135 => 'scan',
136 => 'sscan',
137 => 'zscan',
138 => 'hscan',
135 => 'sentinel',
136 => 'scan',
137 => 'sscan',
138 => 'zscan',
139 => 'hscan',
);
}
}
@@ -173,10 +173,11 @@ class ServerVersionNextTest extends ServerVersionTestCase
132 => 'evalsha',
133 => 'script',
134 => 'time',
135 => 'scan',
136 => 'sscan',
137 => 'zscan',
138 => 'hscan',
135 => 'sentinel',
136 => 'scan',
137 => 'sscan',
138 => 'zscan',
139 => 'hscan',
);
}
}