HashRing.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Cluster\Distribution;
  11. use Predis\Cluster\Hash\HashGeneratorInterface;
  12. /**
  13. * This class implements an hashring-based distributor that uses the same
  14. * algorithm of memcache to distribute keys in a cluster using client-side
  15. * sharding.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. * @author Lorenzo Castelli <lcastelli@gmail.com>
  19. */
  20. class HashRing implements DistributionStrategyInterface, HashGeneratorInterface
  21. {
  22. const DEFAULT_REPLICAS = 128;
  23. const DEFAULT_WEIGHT = 100;
  24. private $ring;
  25. private $ringKeys;
  26. private $ringKeysCount;
  27. private $replicas;
  28. private $nodeHashCallback;
  29. private $nodes = array();
  30. /**
  31. * @param int $replicas Number of replicas in the ring.
  32. * @param mixed $nodeHashCallback Callback returning the string used to calculate the hash of a node.
  33. */
  34. public function __construct($replicas = self::DEFAULT_REPLICAS, $nodeHashCallback = null)
  35. {
  36. $this->replicas = $replicas;
  37. $this->nodeHashCallback = $nodeHashCallback;
  38. }
  39. /**
  40. * Adds a node to the ring with an optional weight.
  41. *
  42. * @param mixed $node Node object.
  43. * @param int $weight Weight for the node.
  44. */
  45. public function add($node, $weight = null)
  46. {
  47. // In case of collisions in the hashes of the nodes, the node added
  48. // last wins, thus the order in which nodes are added is significant.
  49. $this->nodes[] = array('object' => $node, 'weight' => (int) $weight ?: $this::DEFAULT_WEIGHT);
  50. $this->reset();
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function remove($node)
  56. {
  57. // A node is removed by resetting the ring so that it's recreated from
  58. // scratch, in order to reassign possible hashes with collisions to the
  59. // right node according to the order in which they were added in the
  60. // first place.
  61. for ($i = 0; $i < count($this->nodes); ++$i) {
  62. if ($this->nodes[$i]['object'] === $node) {
  63. array_splice($this->nodes, $i, 1);
  64. $this->reset();
  65. break;
  66. }
  67. }
  68. }
  69. /**
  70. * Resets the distributor.
  71. */
  72. private function reset()
  73. {
  74. unset(
  75. $this->ring,
  76. $this->ringKeys,
  77. $this->ringKeysCount
  78. );
  79. }
  80. /**
  81. * Returns the initialization status of the distributor.
  82. *
  83. * @return bool
  84. */
  85. private function isInitialized()
  86. {
  87. return isset($this->ringKeys);
  88. }
  89. /**
  90. * Calculates the total weight of all the nodes in the distributor.
  91. *
  92. * @return int
  93. */
  94. private function computeTotalWeight()
  95. {
  96. $totalWeight = 0;
  97. foreach ($this->nodes as $node) {
  98. $totalWeight += $node['weight'];
  99. }
  100. return $totalWeight;
  101. }
  102. /**
  103. * Initializes the distributor.
  104. */
  105. private function initialize()
  106. {
  107. if ($this->isInitialized()) {
  108. return;
  109. }
  110. if (!$this->nodes) {
  111. throw new EmptyRingException('Cannot initialize empty hashring');
  112. }
  113. $this->ring = array();
  114. $totalWeight = $this->computeTotalWeight();
  115. $nodesCount = count($this->nodes);
  116. foreach ($this->nodes as $node) {
  117. $weightRatio = $node['weight'] / $totalWeight;
  118. $this->addNodeToRing($this->ring, $node, $nodesCount, $this->replicas, $weightRatio);
  119. }
  120. ksort($this->ring, SORT_NUMERIC);
  121. $this->ringKeys = array_keys($this->ring);
  122. $this->ringKeysCount = count($this->ringKeys);
  123. }
  124. /**
  125. * Implements the logic needed to add a node to the hashring.
  126. *
  127. * @param array $ring Source hashring.
  128. * @param mixed $node Node object to be added.
  129. * @param int $totalNodes Total number of nodes.
  130. * @param int $replicas Number of replicas in the ring.
  131. * @param float $weightRatio Weight ratio for the node.
  132. */
  133. protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio)
  134. {
  135. $nodeObject = $node['object'];
  136. $nodeHash = $this->getNodeHash($nodeObject);
  137. $replicas = (int) round($weightRatio * $totalNodes * $replicas);
  138. for ($i = 0; $i < $replicas; $i++) {
  139. $key = crc32("$nodeHash:$i");
  140. $ring[$key] = $nodeObject;
  141. }
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. protected function getNodeHash($nodeObject)
  147. {
  148. if ($this->nodeHashCallback === null) {
  149. return (string) $nodeObject;
  150. }
  151. return call_user_func($this->nodeHashCallback, $nodeObject);
  152. }
  153. /**
  154. * Calculates the hash for the specified value.
  155. *
  156. * @param string $value Input value.
  157. * @return int
  158. */
  159. public function hash($value)
  160. {
  161. return crc32($value);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function get($key)
  167. {
  168. return $this->ring[$this->getNodeKey($key)];
  169. }
  170. /**
  171. * Calculates the corrisponding key of a node distributed in the hashring.
  172. *
  173. * @param int $key Computed hash of a key.
  174. * @return int
  175. */
  176. private function getNodeKey($key)
  177. {
  178. $this->initialize();
  179. $ringKeys = $this->ringKeys;
  180. $upper = $this->ringKeysCount - 1;
  181. $lower = 0;
  182. while ($lower <= $upper) {
  183. $index = ($lower + $upper) >> 1;
  184. $item = $ringKeys[$index];
  185. if ($item > $key) {
  186. $upper = $index - 1;
  187. } elseif ($item < $key) {
  188. $lower = $index + 1;
  189. } else {
  190. return $item;
  191. }
  192. }
  193. return $ringKeys[$this->wrapAroundStrategy($upper, $lower, $this->ringKeysCount)];
  194. }
  195. /**
  196. * Implements a strategy to deal with wrap-around errors during binary searches.
  197. *
  198. * @param int $upper
  199. * @param int $lower
  200. * @param int $ringKeysCount
  201. * @return int
  202. */
  203. protected function wrapAroundStrategy($upper, $lower, $ringKeysCount)
  204. {
  205. // Binary search for the last item in ringkeys with a value less or
  206. // equal to the key. If no such item exists, return the last item.
  207. return $upper >= 0 ? $upper : $ringKeysCount - 1;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getHashGenerator()
  213. {
  214. return $this;
  215. }
  216. }