HashRing.php 3.8 KB

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