PredisCluster.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection;
  11. use Predis\ClientException;
  12. use Predis\NotSupportedException;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Command\Hash\CommandHashStrategy;
  15. use Predis\Distribution\HashRing;
  16. use Predis\Distribution\DistributionStrategyInterface;
  17. /**
  18. * Abstraction for a cluster of aggregated connections to various Redis servers
  19. * implementing client-side sharding based on pluggable distribution strategies.
  20. *
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. * @todo Add the ability to remove connections from pool.
  23. */
  24. class PredisCluster implements ClusterConnectionInterface, \IteratorAggregate, \Countable
  25. {
  26. private $pool;
  27. private $distributor;
  28. private $cmdHasher;
  29. /**
  30. * @param DistributionStrategyInterface $distributor Distribution strategy used by the cluster.
  31. */
  32. public function __construct(DistributionStrategyInterface $distributor = null)
  33. {
  34. $this->pool = array();
  35. $this->cmdHasher = new CommandHashStrategy();
  36. $this->distributor = $distributor ?: new HashRing();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function isConnected()
  42. {
  43. foreach ($this->pool as $connection) {
  44. if ($connection->isConnected()) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function connect()
  54. {
  55. foreach ($this->pool as $connection) {
  56. $connection->connect();
  57. }
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function disconnect()
  63. {
  64. foreach ($this->pool as $connection) {
  65. $connection->disconnect();
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function add(SingleConnectionInterface $connection)
  72. {
  73. $parameters = $connection->getParameters();
  74. if (isset($parameters->alias)) {
  75. $this->pool[$parameters->alias] = $connection;
  76. } else {
  77. $this->pool[] = $connection;
  78. }
  79. $weight = isset($parameters->weight) ? $parameters->weight : null;
  80. $this->distributor->add($connection, $weight);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function remove(SingleConnectionInterface $connection)
  86. {
  87. if (($id = array_search($connection, $this->pool, true)) !== false) {
  88. unset($this->pool[$id]);
  89. $this->distributor->remove($connection);
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * Removes a connection instance using its alias or index.
  96. *
  97. * @param string $connectionId Alias or index of a connection.
  98. * @return Boolean Returns true if the connection was in the pool.
  99. */
  100. public function removeById($connectionId)
  101. {
  102. if ($connection = $this->getConnectionById($connectionId)) {
  103. return $this->remove($connection);
  104. }
  105. return false;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getConnection(CommandInterface $command)
  111. {
  112. $hash = $command->getHash();
  113. if (isset($hash)) {
  114. return $this->distributor->get($hash);
  115. }
  116. if ($hash = $this->cmdHasher->getHash($this->distributor, $command)) {
  117. $command->setHash($hash);
  118. return $this->distributor->get($hash);
  119. }
  120. throw new NotSupportedException("Cannot send {$command->getId()} to a cluster of connections");
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getConnectionById($id = null)
  126. {
  127. $alias = $id ?: 0;
  128. return isset($this->pool[$alias]) ? $this->pool[$alias] : null;
  129. }
  130. /**
  131. * Retrieves a connection instance from the cluster using a key.
  132. *
  133. * @param string $key Key of a Redis value.
  134. * @return SingleConnectionInterface
  135. */
  136. public function getConnectionByKey($key)
  137. {
  138. $hash = $this->cmdHasher->getKeyHash($this->distributor, $key);
  139. $node = $this->distributor->get($hash);
  140. return $node;
  141. }
  142. /**
  143. * Returns the underlying command hash strategy used to hash
  144. * commands by their keys.
  145. *
  146. * @return CommandHashStrategy
  147. */
  148. public function getCommandHashStrategy()
  149. {
  150. return $this->cmdHasher;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function count()
  156. {
  157. return count($this->pool);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getIterator()
  163. {
  164. return new \ArrayIterator($this->pool);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function writeCommand(CommandInterface $command)
  170. {
  171. $this->getConnection($command)->writeCommand($command);
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function readResponse(CommandInterface $command)
  177. {
  178. return $this->getConnection($command)->readResponse($command);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function executeCommand(CommandInterface $command)
  184. {
  185. return $this->getConnection($command)->executeCommand($command);
  186. }
  187. /**
  188. * Executes the specified Redis command on all the nodes of a cluster.
  189. *
  190. * @param CommandInterface $command A Redis command.
  191. * @return array
  192. */
  193. public function executeCommandOnNodes(CommandInterface $command)
  194. {
  195. $replies = array();
  196. foreach ($this->pool as $connection) {
  197. $replies[] = $connection->executeCommand($command);
  198. }
  199. return $replies;
  200. }
  201. }