58 private const HASHES_PER_LOCATION = 40;
60 private const SECTORS_PER_HASH = 4;
66 public const RING_ALL = 0;
68 public const RING_LIVE = 1;
78 public function __construct( array $map, $algo =
'sha1', array $ejections = [] ) {
79 $this->
init( $map, $algo, $ejections );
87 protected function init( array $map, $algo, array $ejections ) {
88 if ( !in_array( $algo, hash_algos(),
true ) ) {
89 throw new RuntimeException( __METHOD__ .
": unsupported '$algo' hash algorithm." );
92 $weightByLocation = array_filter( $map );
93 if ( $weightByLocation === [] ) {
94 throw new UnexpectedValueException(
"No locations with non-zero weight." );
95 } elseif ( min( $map ) < 0 ) {
96 throw new InvalidArgumentException(
"Location weight cannot be negative." );
100 $this->weightByLocation = $weightByLocation;
101 $this->ejectExpiryByLocation = $ejections;
102 $this->baseRing = $this->buildLocationRing( $this->weightByLocation );
125 public function getLocations( $item, $limit, $from = self::RING_ALL ) {
126 if ( $from === self::RING_ALL ) {
127 $ring = $this->baseRing;
128 } elseif ( $from === self::RING_LIVE ) {
131 throw new InvalidArgumentException(
"Invalid ring source specified." );
136 if ( count( $this->weightByLocation ) == 1 ) {
137 return ( $limit > 0 ) ? [ $ring[0][self::KEY_LOCATION] ] : [];
141 $itemIndex = $this->findNodeIndexForPosition( $this->getItemPosition( $item ), $ring );
144 $currentIndex =
null;
145 while ( count( $locations ) < $limit ) {
146 if ( $currentIndex ===
null ) {
147 $currentIndex = $itemIndex;
149 $currentIndex = $this->getNextClockwiseNodeIndex( $currentIndex, $ring );
150 if ( $currentIndex === $itemIndex ) {
155 $nodeLocation = $ring[$currentIndex][self::KEY_LOCATION];
156 if ( !in_array( $nodeLocation, $locations,
true ) ) {
158 $locations[] = $nodeLocation;
170 private function findNodeIndexForPosition( $position, $ring ) {
171 $count = count( $ring );
172 if ( $count === 0 ) {
180 $midPos = (int)( ( $lowPos + $highPos ) / 2 );
181 if ( $midPos === $count ) {
186 $midVal = $ring[$midPos][self::KEY_POS];
187 $midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][self::
KEY_POS];
188 if ( $position <= $midVal && $position > $midMinusOneVal ) {
193 if ( $midVal < $position ) {
194 $lowPos = $midPos + 1;
196 $highPos = $midPos - 1;
199 if ( $lowPos > $highPos ) {
214 return $this->weightByLocation;
226 if ( !isset( $this->weightByLocation[$location] ) ) {
227 throw new UnexpectedValueException(
"No location '$location' in the ring." );
231 $this->ejectExpiryByLocation[$location] = $expiry;
233 $this->liveRing =
null;
235 return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) );
246 return $this->
getLocations( $item, 1, self::RING_LIVE )[0];
258 return $this->
getLocations( $item, $limit, self::RING_LIVE );
270 return array_diff_key(
271 $this->weightByLocation,
273 $this->ejectExpiryByLocation,
274 static function ( $expiry ) use ( $now ) {
275 return ( $expiry > $now );
285 private function buildLocationRing( array $weightByLocation ) {
286 $locationCount = count( $weightByLocation );
287 $totalWeight = array_sum( $weightByLocation );
292 foreach ( $weightByLocation as $location => $weight ) {
293 $ratio = $weight / $totalWeight;
296 $nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount );
297 for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) {
301 $positions = $this->getNodePositionQuartet(
"{$location}-{$qi}" );
302 foreach ( $positions as $gi => $position ) {
303 $node = ( $qi * self::SECTORS_PER_HASH + $gi ) .
"@$location";
304 $posKey = (string)$position;
305 if ( isset( $claimed[$posKey] ) ) {
307 if ( $claimed[$posKey][
'node'] > $node ) {
310 unset( $ring[$claimed[$posKey][
'index']] );
314 self::KEY_POS => $position,
315 self::KEY_LOCATION => $location
317 $claimed[$posKey] = [
'node' => $node,
'index' => count( $ring ) - 1 ];
322 usort( $ring,
static function ( $a, $b ) {
323 if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) {
324 throw new UnexpectedValueException(
'Duplicate node positions.' );
327 return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 );
337 private function getItemPosition( $item ) {
340 $octets = substr( hash( $this->algo, (
string)$item,
true ), 0, 4 );
341 if ( strlen( $octets ) != 4 ) {
342 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 32 bits." );
345 $pos = unpack(
'V', $octets )[1];
349 $pos = sprintf(
'%u', $pos );
359 private function getNodePositionQuartet( $nodeGroupName ) {
360 $octets = substr( hash( $this->algo, (
string)$nodeGroupName,
true ), 0, 16 );
361 if ( strlen( $octets ) != 16 ) {
362 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 128 bits." );
366 foreach ( unpack(
'V4', $octets ) as $signed ) {
367 $positions[] = (float)sprintf(
'%u', $signed );
378 private function getNextClockwiseNodeIndex( $i, $ring ) {
379 if ( !isset( $ring[$i] ) ) {
380 throw new UnexpectedValueException( __METHOD__ .
": reference index is invalid." );
385 return ( $next < count( $ring ) ) ? $next : 0;
395 if ( !$this->ejectExpiryByLocation ) {
396 return $this->baseRing;
401 if ( $this->liveRing ===
null || min( $this->ejectExpiryByLocation ) <= $now ) {
403 $this->ejectExpiryByLocation = array_filter(
404 $this->ejectExpiryByLocation,
405 static function ( $expiry ) use ( $now ) {
406 return ( $expiry > $now );
410 if ( count( $this->ejectExpiryByLocation ) ) {
413 foreach ( $this->baseRing as $nodeInfo ) {
414 $location = $nodeInfo[self::KEY_LOCATION];
415 if ( !isset( $this->ejectExpiryByLocation[$location] ) ) {
426 if ( !$this->liveRing ) {
427 throw new UnexpectedValueException(
"The live ring is currently empty." );
430 return $this->liveRing;
442 'algorithm' => $this->algo,
443 'locations' => $this->weightByLocation,
444 'ejections' => $this->ejectExpiryByLocation
449 if ( is_array( $data ) ) {
450 $this->
init( $data[
'locations'] ?? [], $data[
'algorithm'] ??
'sha1', $data[
'ejections'] ?? [] );
452 throw new UnexpectedValueException( __METHOD__ .
": unable to decode JSON." );