123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace Predis\Connection;
- use Predis\ClientException;
- use Predis\NotSupportedException;
- use Predis\ResponseErrorInterface;
- use Predis\Command\CommandInterface;
- use Predis\Distribution\CRC16HashGenerator;
- class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \Countable
- {
- private $pool;
- private $slots;
- private $connections;
- private $hashgenerator;
-
- public function __construct(FactoryInterface $connections = null)
- {
- $this->pool = array();
- $this->slots = array();
- $this->connections = $connections;
- $this->hashgenerator = new CRC16HashGenerator();
- }
-
- public function isConnected()
- {
- foreach ($this->pool as $connection) {
- if ($connection->isConnected()) {
- return true;
- }
- }
- return false;
- }
-
- public function connect()
- {
- foreach ($this->pool as $connection) {
- $connection->connect();
- }
- }
-
- public function disconnect()
- {
- foreach ($this->pool as $connection) {
- $connection->disconnect();
- }
- }
-
- public function add(SingleConnectionInterface $connection)
- {
- $parameters = $connection->getParameters();
- $this->pool["{$parameters->host}:{$parameters->port}"] = $connection;
- }
-
- public function remove(SingleConnectionInterface $connection)
- {
- if (($id = array_search($connection, $this->pool, true)) !== false) {
- unset($this->pool[$id]);
- return true;
- }
- return false;
- }
-
- public function removeById($connectionId)
- {
- if (isset($this->pool[$connectionId])) {
- unset($this->pool[$connectionId]);
- return true;
- }
- return false;
- }
-
- public function getConnection(CommandInterface $command)
- {
- if ($hash = $command->getHash() === null) {
- $hash = $this->hashgenerator->hash($command->getArgument(0));
- if (!isset($hash)) {
- throw new NotSupportedException("Cannot send {$command->getId()} commands to redis-cluster");
- }
- }
- $slot = $hash & 0x0FFF;
- if (isset($this->slots[$slot])) {
- return $this->slots[$slot];
- }
- $connection = $this->pool[array_rand($this->pool)];
- $this->slots[$slot] = $connection;
- return $connection;
- }
-
- public function getConnectionById($id = null)
- {
- if (!isset($id)) {
- throw new \InvalidArgumentException("A valid connection ID must be specified");
- }
- return isset($this->pool[$id]) ? $this->pool[$id] : null;
- }
-
- protected function onMoveRequest(CommandInterface $command, $request, $details)
- {
- list($slot, $host) = explode(' ', $details, 2);
- $connection = $this->getConnectionById($host);
- if (!isset($connection)) {
- $parameters = array('host' => null, 'port' => null);
- list($parameters['host'], $parameters['port']) = explode(':', $host, 2);
- $connection = $this->connections->create($parameters);
- }
- switch ($request) {
- case 'MOVED':
- $this->add($connection);
- $this->slots[$slot] = $connection;
- return $this->executeCommand($command);
- case 'ASK':
- return $connection->executeCommand($command);
- default:
- throw new ClientException("Unexpected request type for a move request: $request");
- }
- }
-
- public function count()
- {
- return count($this->pool);
- }
-
- public function getIterator()
- {
- return new \ArrayIterator($this->pool);
- }
-
- protected function handleServerError(CommandInterface $command, ResponseErrorInterface $error)
- {
- list($type, $details) = explode(' ', $error->getMessage(), 2);
- switch ($type) {
- case 'MOVED':
- case 'ASK':
- return $this->onMoveRequest($command, $type, $details);
- default:
- return $error;
- }
- }
-
- public function writeCommand(CommandInterface $command)
- {
- $this->getConnection($command)->writeCommand($command);
- }
-
- public function readResponse(CommandInterface $command)
- {
- return $this->getConnection($command)->readResponse($command);
- }
-
- public function executeCommand(CommandInterface $command)
- {
- $connection = $this->getConnection($command);
- $reply = $connection->executeCommand($command);
- if ($reply instanceof ResponseErrorInterface) {
- return $this->handleServerError($command, $reply);
- }
- return $reply;
- }
- }
|