PredisCluster.php 5.7 KB

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