53 const RING_SIZE = 4294967296.0;
55 const HASHES_PER_LOCATION = 40;
57 const SECTORS_PER_HASH = 4;
84 protected function init( array $map,
$algo, array $ejections ) {
85 if ( !in_array(
$algo, hash_algos(),
true ) ) {
86 throw new RuntimeException( __METHOD__ .
": unsupported '$algo' hash algorithm." );
91 throw new UnexpectedValueException(
"No locations with non-zero weight." );
92 } elseif ( min( $map ) < 0 ) {
93 throw new InvalidArgumentException(
"Location weight cannot be negative." );
98 $this->ejectExpiryByLocation = $ejections;
123 public function getLocations( $item, $limit, $from = self::RING_ALL ) {
124 if ( $from === self::RING_ALL ) {
126 } elseif ( $from === self::RING_LIVE ) {
129 throw new InvalidArgumentException(
"Invalid ring source specified." );
134 if ( count( $this->weightByLocation ) == 1 ) {
142 $currentIndex =
null;
143 while ( count( $locations ) < $limit ) {
144 if ( $currentIndex ===
null ) {
145 $currentIndex = $itemIndex;
148 if ( $currentIndex === $itemIndex ) {
153 if ( !in_array( $nodeLocation, $locations,
true ) ) {
155 $locations[] = $nodeLocation;
168 $count = count( $ring );
169 if ( $count === 0 ) {
177 $midPos = (int)( ( $lowPos + $highPos ) / 2 );
178 if ( $midPos === $count ) {
184 $midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][
self::KEY_POS];
185 if ( $position <= $midVal && $position > $midMinusOneVal ) {
190 if ( $midVal < $position ) {
191 $lowPos = $midPos + 1;
193 $highPos = $midPos - 1;
196 if ( $lowPos > $highPos ) {
223 if ( !isset( $this->weightByLocation[$location] ) ) {
224 throw new UnexpectedValueException(
"No location '$location' in the ring." );
228 $this->ejectExpiryByLocation[$location] = $expiry;
230 $this->liveRing =
null;
232 return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) );
243 return $this->
getLocations( $item, 1, self::RING_LIVE )[0];
255 return $this->
getLocations( $item, $limit, self::RING_LIVE );
267 return array_diff_key(
268 $this->weightByLocation,
270 $this->ejectExpiryByLocation,
271 function ( $expiry ) use ( $now ) {
272 return ( $expiry > $now );
290 $ratio = $weight / $totalWeight;
293 $nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount );
294 for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) {
299 foreach ( $positions as $gi => $position ) {
300 $node = ( $qi * self::SECTORS_PER_HASH + $gi ) .
"@$location";
301 $posKey = (string)$position;
302 if ( isset( $claimed[$posKey] ) ) {
304 if ( $claimed[$posKey][
'node'] > $node ) {
307 unset( $ring[$claimed[$posKey][
'index']] );
311 self::KEY_POS => $position,
312 self::KEY_LOCATION => $location
314 $claimed[$posKey] = [
'node' => $node,
'index' => count( $ring ) - 1 ];
319 usort( $ring,
function ( $a, $b ) {
320 if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) {
321 throw new UnexpectedValueException(
'Duplicate node positions.' );
324 return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 );
337 $octets = substr( hash( $this->algo, (
string)$item,
true ), 0, 4 );
338 if ( strlen( $octets ) != 4 ) {
339 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 32 bits." );
342 $pos = unpack(
'V', $octets )[1];
346 $pos = sprintf(
'%u', $pos );
357 $octets = substr( hash( $this->algo, (
string)$nodeGroupName,
true ), 0, 16 );
358 if ( strlen( $octets ) != 16 ) {
359 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 128 bits." );
363 foreach ( unpack(
'V4', $octets ) as $signed ) {
364 $positions[] = (float)sprintf(
'%u', $signed );
376 if ( !isset( $ring[$i] ) ) {
377 throw new UnexpectedValueException( __METHOD__ .
": reference index is invalid." );
382 return ( $next < count( $ring ) ) ? $next : 0;
392 if ( !$this->ejectExpiryByLocation ) {
398 if ( $this->liveRing ===
null || min( $this->ejectExpiryByLocation ) <= $now ) {
400 $this->ejectExpiryByLocation = array_filter(
401 $this->ejectExpiryByLocation,
402 function ( $expiry ) use ( $now ) {
403 return ( $expiry > $now );
407 if ( count( $this->ejectExpiryByLocation ) ) {
410 foreach ( $this->baseRing as $i => $nodeInfo ) {
412 if ( !isset( $this->ejectExpiryByLocation[$location] ) ) {
423 if ( !$this->liveRing ) {
424 throw new UnexpectedValueException(
"The live ring is currently empty." );
439 'algorithm' => $this->algo,
440 'locations' => $this->weightByLocation,
441 'ejections' => $this->ejectExpiryByLocation
447 if ( is_array( $data ) ) {
448 $this->
init( $data[
'locations'], $data[
'algorithm'], $data[
'ejections'] );
450 throw new UnexpectedValueException( __METHOD__ .
": unable to decode JSON." );
Convenience class for weighted consistent hash rings.
getLiveLocations( $item, $limit)
Get the location of an item on the "live" ring, as well as the next locations.
int[] $ejectExpiryByLocation
Map of (location => UNIX timestamp)
getLiveLocationWeights()
Get the map of "live" locations to weight (does not include zero weight items)
array[] $baseRing
Non-empty position-ordered list of (position, location name)
init(array $map, $algo, array $ejections)
getLiveLocation( $item)
Get the location of an item on the "live" ring.
int[] $weightByLocation
Non-empty (location => integer weight)
unserialize( $serialized)
getLocation( $item)
Get the location of an item on the ring.
findNodeIndexForPosition( $position, $ring)
array[] $liveRing
Non-empty position-ordered list of (position, location name)
getNodePositionQuartet( $nodeGroupName)
getNextClockwiseNodeIndex( $i, $ring)
buildLocationRing(array $weightByLocation)
getLocationWeights()
Get the map of locations to weight (does not include zero weight items)
getLocations( $item, $limit, $from=self::RING_ALL)
Get the location of an item on the ring followed by the next ring locations.
getLiveRing()
Get the "live" hash ring (which does not include ejected locations)
string $algo
Hashing algorithm for hash()
__construct(array $map, $algo='sha1', array $ejections=[])
Make a consistent hash ring given a set of locations and their weight values.
ejectFromLiveRing( $location, $ttl)
Remove a location from the "live" hash ring.
foreach( $res as $row) $serialized