3 * Convenience class for weighted consistent hash rings.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Aaron Schulz
25 * Convenience class for weighted consistent hash rings
30 /** @var Array (location => weight) */
31 protected $sourceMap = array();
32 /** @var Array (location => (start, end)) */
33 protected $ring = array();
35 /** @var Array (location => (start, end)) */
37 /** @var Array (location => UNIX timestamp) */
38 protected $ejectionExpiries = array();
39 /** @var integer UNIX timestamp */
40 protected $ejectionNextExpiry = INF
;
42 const RING_SIZE
= 268435456; // 2^28
45 * @param array $map (location => weight)
47 public function __construct( array $map ) {
48 $map = array_filter( $map, function ( $w ) {
51 if ( !count( $map ) ) {
52 throw new UnexpectedValueException( "Ring is empty or all weights are zero." );
54 $this->sourceMap
= $map;
55 // Sort the locations based on the hash of their names
57 foreach ( $map as $location => $weight ) {
58 $hashes[$location] = sha1( $location );
60 uksort( $map, function ( $a, $b ) use ( $hashes ) {
61 return strcmp( $hashes[$a], $hashes[$b] );
63 // Fit the map to weight-proportionate one with a space of size RING_SIZE
64 $sum = array_sum( $map );
65 $standardMap = array();
66 foreach ( $map as $location => $weight ) {
67 $standardMap[$location] = (int)floor( $weight / $sum * self
::RING_SIZE
);
69 // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
71 foreach ( $standardMap as $location => $weight ) {
72 // Location covers half-closed interval [$index,$index + $weight)
73 $this->ring
[$location] = array( $index, $index +
$weight );
76 // Make sure the last location covers what is left
78 $this->ring
[key( $this->ring
)][1] = self
::RING_SIZE
;
82 * Get the location of an item on the ring
85 * @return string Location
87 public function getLocation( $item ) {
88 $locations = $this->getLocations( $item, 1 );
94 * Get the location of an item on the ring, as well as the next locations
97 * @param integer $limit Maximum number of locations to return
98 * @return array List of locations
100 public function getLocations( $item, $limit ) {
101 $locations = array();
102 $primaryLocation = null;
103 $spot = hexdec( substr( sha1( $item ), 0, 7 ) ); // first 28 bits
104 foreach ( $this->ring
as $location => $range ) {
105 if ( count( $locations ) >= $limit ) {
108 // The $primaryLocation is the location the item spot is in.
109 // After that is reached, keep appending the next locations.
110 if ( ( $range[0] <= $spot && $spot < $range[1] ) ||
$primaryLocation !== null ) {
111 if ( $primaryLocation === null ) {
112 $primaryLocation = $location;
114 $locations[] = $location;
117 // If more locations are requested, wrap-around and keep adding them
118 reset( $this->ring
);
119 while ( count( $locations ) < $limit ) {
120 list( $location, ) = each( $this->ring
);
121 if ( $location === $primaryLocation ) {
122 break; // don't go in circles
124 $locations[] = $location;
131 * Get the map of locations to weight (ignores 0-weight items)
135 public function getLocationWeights() {
136 return $this->sourceMap
;
140 * Get a new hash ring with a location removed from the ring
142 * @param string $location
143 * @return HashRing|bool Returns false if no non-zero weighted spots are left
145 public function newWithoutLocation( $location ) {
146 $map = $this->sourceMap
;
147 unset( $map[$location] );
149 return count( $map ) ?
new self( $map ) : false;
153 * Remove a location from the "live" hash ring
155 * @param string $location
156 * @param integer $ttl Seconds
157 * @return bool Whether some non-ejected locations are left
159 public function ejectFromLiveRing( $location, $ttl ) {
160 if ( !isset( $this->sourceMap
[$location] ) ) {
161 throw new UnexpectedValueException( "No location '$location' in the ring." );
163 $expiry = time() +
$ttl;
164 $this->liveRing
= null; // stale
165 $this->ejectionExpiries
[$location] = $expiry;
166 $this->ejectionNextExpiry
= min( $expiry, $this->ejectionNextExpiry
);
168 return ( count( $this->ejectionExpiries
) < count( $this->sourceMap
) );
172 * Get the "live" hash ring (which does not include ejected locations)
175 * @throws UnexpectedValueException
177 public function getLiveRing() {
179 if ( $this->liveRing
=== null ||
$this->ejectionNextExpiry
<= $now ) {
180 $this->ejectionExpiries
= array_filter(
181 $this->ejectionExpiries
,
182 function( $expiry ) use ( $now ) {
183 return ( $expiry > $now );
186 if ( count( $this->ejectionExpiries
) ) {
187 $map = array_diff_key( $this->sourceMap
, $this->ejectionExpiries
);
188 $this->liveRing
= count( $map ) ?
new self( $map ) : false;
190 $this->ejectionNextExpiry
= min( $this->ejectionExpiries
);
191 } else { // common case; avoid recalculating ring
192 $this->liveRing
= clone $this;
193 $this->liveRing
->ejectionExpiries
= array();
194 $this->liveRing
->ejectionNextExpiry
= INF
;
195 $this->liveRing
->liveRing
= null;
197 $this->ejectionNextExpiry
= INF
;
200 if ( !$this->liveRing
) {
201 throw new UnexpectedValueException( "The live ring is currently empty." );
204 return $this->liveRing
;
208 * Get the location of an item on the "live" ring
210 * @param string $item
211 * @return string Location
212 * @throws UnexpectedValueException
214 public function getLiveLocation( $item ) {
215 return $this->getLiveRing()->getLocation( $item );
219 * Get the location of an item on the "live" ring, as well as the next locations
221 * @param string $item
222 * @param integer $limit Maximum number of locations to return
223 * @return array List of locations
224 * @throws UnexpectedValueException
226 public function getLiveLocations( $item ) {
227 return $this->getLiveRing()->getLocations( $item );
231 * Get the map of "live" locations to weight (ignores 0-weight items)
234 * @throws UnexpectedValueException
236 public function getLiveLocationWeights() {
237 return $this->getLiveRing()->getLocationWeights();