PredisCluster.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Cluster\PredisClusterHashStrategy;
  14. use Predis\Cluster\Distribution\DistributionStrategyInterface;
  15. use Predis\Cluster\Distribution\HashRing;
  16. use Predis\Command\CommandInterface;
  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 $strategy;
  28. private $distributor;
  29. /**
  30. * @param DistributionStrategyInterface $distributor Distribution strategy used by the cluster.
  31. */
  32. public function __construct(DistributionStrategyInterface $distributor = null)
  33. {
  34. $distributor = $distributor ?: new HashRing();
  35. $this->pool = array();
  36. $this->strategy = new PredisClusterHashStrategy($distributor->getHashGenerator());
  37. $this->distributor = $distributor;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function isConnected()
  43. {
  44. foreach ($this->pool as $connection) {
  45. if ($connection->isConnected()) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function connect()
  55. {
  56. foreach ($this->pool as $connection) {
  57. $connection->connect();
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function disconnect()
  64. {
  65. foreach ($this->pool as $connection) {
  66. $connection->disconnect();
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function add(SingleConnectionInterface $connection)
  73. {
  74. $parameters = $connection->getParameters();
  75. if (isset($parameters->alias)) {
  76. $this->pool[$parameters->alias] = $connection;
  77. } else {
  78. $this->pool[] = $connection;
  79. }
  80. $weight = isset($parameters->weight) ? $parameters->weight : null;
  81. $this->distributor->add($connection, $weight);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function remove(SingleConnectionInterface $connection)
  87. {
  88. if (($id = array_search($connection, $this->pool, true)) !== false) {
  89. unset($this->pool[$id]);
  90. $this->distributor->remove($connection);
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Removes a connection instance using its alias or index.
  97. *
  98. * @param string $connectionId Alias or index of a connection.
  99. * @return Boolean Returns true if the connection was in the pool.
  100. */
  101. public function removeById($connectionId)
  102. {
  103. if ($connection = $this->getConnectionById($connectionId)) {
  104. return $this->remove($connection);
  105. }
  106. return false;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getConnection(CommandInterface $command)
  112. {
  113. $hash = $this->strategy->getHash($command);
  114. if (!isset($hash)) {
  115. throw new NotSupportedException("Cannot use {$command->getId()} with a cluster of connections");
  116. }
  117. $node = $this->distributor->get($hash);
  118. return $node;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getConnectionById($connectionId)
  124. {
  125. return isset($this->pool[$connectionId]) ? $this->pool[$connectionId] : null;
  126. }
  127. /**
  128. * Retrieves a connection instance from the cluster using a key.
  129. *
  130. * @param string $key Key of a Redis value.
  131. * @return SingleConnectionInterface
  132. */
  133. public function getConnectionByKey($key)
  134. {
  135. $hash = $this->strategy->getKeyHash($key);
  136. $node = $this->distributor->get($hash);
  137. return $node;
  138. }
  139. /**
  140. * Returns the underlying command hash strategy used to hash
  141. * commands by their keys.
  142. *
  143. * @return Predis\Cluster\CommandHashStrategyInterface
  144. */
  145. public function getCommandHashStrategy()
  146. {
  147. return $this->strategy;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function count()
  153. {
  154. return count($this->pool);
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function getIterator()
  160. {
  161. return new \ArrayIterator($this->pool);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function writeCommand(CommandInterface $command)
  167. {
  168. $this->getConnection($command)->writeCommand($command);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function readResponse(CommandInterface $command)
  174. {
  175. return $this->getConnection($command)->readResponse($command);
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function executeCommand(CommandInterface $command)
  181. {
  182. return $this->getConnection($command)->executeCommand($command);
  183. }
  184. /**
  185. * Executes the specified Redis command on all the nodes of a cluster.
  186. *
  187. * @param CommandInterface $command A Redis command.
  188. * @return array
  189. */
  190. public function executeCommandOnNodes(CommandInterface $command)
  191. {
  192. $replies = array();
  193. foreach ($this->pool as $connection) {
  194. $replies[] = $connection->executeCommand($command);
  195. }
  196. return $replies;
  197. }
  198. }