HashRing.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Distribution;
  11. class HashRing implements IDistributionStrategy
  12. {
  13. const DEFAULT_REPLICAS = 128;
  14. const DEFAULT_WEIGHT = 100;
  15. private $_nodes;
  16. private $_ring;
  17. private $_ringKeys;
  18. private $_ringKeysCount;
  19. private $_replicas;
  20. public function __construct($replicas = self::DEFAULT_REPLICAS)
  21. {
  22. $this->_replicas = $replicas;
  23. $this->_nodes = array();
  24. }
  25. public function add($node, $weight = null)
  26. {
  27. // In case of collisions in the hashes of the nodes, the node added
  28. // last wins, thus the order in which nodes are added is significant.
  29. $this->_nodes[] = array('object' => $node, 'weight' => (int) $weight ?: $this::DEFAULT_WEIGHT);
  30. $this->reset();
  31. }
  32. public function remove($node)
  33. {
  34. // A node is removed by resetting the ring so that it's recreated from
  35. // scratch, in order to reassign possible hashes with collisions to the
  36. // right node according to the order in which they were added in the
  37. // first place.
  38. for ($i = 0; $i < count($this->_nodes); ++$i) {
  39. if ($this->_nodes[$i]['object'] === $node) {
  40. array_splice($this->_nodes, $i, 1);
  41. $this->reset();
  42. break;
  43. }
  44. }
  45. }
  46. private function reset()
  47. {
  48. unset(
  49. $this->_ring,
  50. $this->_ringKeys,
  51. $this->_ringKeysCount
  52. );
  53. }
  54. private function isInitialized()
  55. {
  56. return isset($this->_ringKeys);
  57. }
  58. private function computeTotalWeight()
  59. {
  60. $totalWeight = 0;
  61. foreach ($this->_nodes as $node) {
  62. $totalWeight += $node['weight'];
  63. }
  64. return $totalWeight;
  65. }
  66. private function initialize()
  67. {
  68. if ($this->isInitialized()) {
  69. return;
  70. }
  71. if (count($this->_nodes) === 0) {
  72. throw new EmptyRingException('Cannot initialize empty hashring');
  73. }
  74. $this->_ring = array();
  75. $totalWeight = $this->computeTotalWeight();
  76. $nodesCount = count($this->_nodes);
  77. foreach ($this->_nodes as $node) {
  78. $weightRatio = $node['weight'] / $totalWeight;
  79. $this->addNodeToRing($this->_ring, $node, $nodesCount, $this->_replicas, $weightRatio);
  80. }
  81. ksort($this->_ring, SORT_NUMERIC);
  82. $this->_ringKeys = array_keys($this->_ring);
  83. $this->_ringKeysCount = count($this->_ringKeys);
  84. }
  85. protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio)
  86. {
  87. $nodeObject = $node['object'];
  88. $nodeHash = $this->getNodeHash($nodeObject);
  89. $replicas = (int) round($weightRatio * $totalNodes * $replicas);
  90. for ($i = 0; $i < $replicas; $i++) {
  91. $key = crc32("$nodeHash:$i");
  92. $ring[$key] = $nodeObject;
  93. }
  94. }
  95. protected function getNodeHash($nodeObject)
  96. {
  97. return (string) $nodeObject;
  98. }
  99. public function generateKey($value)
  100. {
  101. return crc32($value);
  102. }
  103. public function get($key)
  104. {
  105. return $this->_ring[$this->getNodeKey($key)];
  106. }
  107. private function getNodeKey($key)
  108. {
  109. $this->initialize();
  110. $ringKeys = $this->_ringKeys;
  111. $upper = $this->_ringKeysCount - 1;
  112. $lower = 0;
  113. while ($lower <= $upper) {
  114. $index = ($lower + $upper) >> 1;
  115. $item = $ringKeys[$index];
  116. if ($item > $key) {
  117. $upper = $index - 1;
  118. }
  119. else if ($item < $key) {
  120. $lower = $index + 1;
  121. }
  122. else {
  123. return $item;
  124. }
  125. }
  126. return $ringKeys[$this->wrapAroundStrategy($upper, $lower, $this->_ringKeysCount)];
  127. }
  128. protected function wrapAroundStrategy($upper, $lower, $ringKeysCount)
  129. {
  130. // Binary search for the last item in _ringkeys with a value less or
  131. // equal to the key. If no such item exists, return the last item.
  132. return $upper >= 0 ? $upper : $ringKeysCount - 1;
  133. }
  134. }