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;
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." );
94 throw new UnexpectedValueException(
"No locations with non-zero weight." );
95 } elseif ( min( $map ) < 0 ) {
96 throw new InvalidArgumentException(
"Location weight cannot be negative." );
101 $this->ejectExpiryByLocation = $ejections;
102 $this->baseRing = $this->buildLocationRing( $this->weightByLocation );
126 public function getLocations( $item, $limit, $from = self::RING_ALL ) {
127 if ( $from === self::RING_ALL ) {
129 } elseif ( $from === self::RING_LIVE ) {
132 throw new InvalidArgumentException(
"Invalid ring source specified." );
137 if ( count( $this->weightByLocation ) == 1 ) {
142 $itemIndex = $this->findNodeIndexForPosition( $this->getItemPosition( $item ), $ring );
145 $currentIndex =
null;
146 while ( count( $locations ) < $limit ) {
147 if ( $currentIndex ===
null ) {
148 $currentIndex = $itemIndex;
150 $currentIndex = $this->getNextClockwiseNodeIndex( $currentIndex, $ring );
151 if ( $currentIndex === $itemIndex ) {
157 if ( !in_array( $nodeLocation, $locations,
true ) ) {
159 $locations[] = $nodeLocation;
171 private function findNodeIndexForPosition( $position, $ring ) {
172 $count = count( $ring );
173 if ( $count === 0 ) {
181 $midPos = (int)( ( $lowPos + $highPos ) / 2 );
182 if ( $midPos === $count ) {
188 $midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][self::
KEY_POS];
189 if ( $position <= $midVal && $position > $midMinusOneVal ) {
194 if ( $midVal < $position ) {
195 $lowPos = $midPos + 1;
197 $highPos = $midPos - 1;
200 if ( $lowPos > $highPos ) {
227 if ( !isset( $this->weightByLocation[$location] ) ) {
228 throw new UnexpectedValueException(
"No location '$location' in the ring." );
232 $this->ejectExpiryByLocation[$location] = $expiry;
234 $this->liveRing =
null;
236 return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) );
247 return $this->
getLocations( $item, 1, self::RING_LIVE )[0];
259 return $this->
getLocations( $item, $limit, self::RING_LIVE );
271 return array_diff_key(
272 $this->weightByLocation,
274 $this->ejectExpiryByLocation,
275 static function ( $expiry ) use ( $now ) {
276 return ( $expiry > $now );
294 $ratio = $weight / $totalWeight;
297 $nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount );
298 for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) {
302 $positions = $this->getNodePositionQuartet(
"{$location}-{$qi}" );
303 foreach ( $positions as $gi => $position ) {
304 $node = ( $qi * self::SECTORS_PER_HASH + $gi ) .
"@$location";
305 $posKey = (string)$position;
306 if ( isset( $claimed[$posKey] ) ) {
308 if ( $claimed[$posKey][
'node'] > $node ) {
311 unset( $ring[$claimed[$posKey][
'index']] );
315 self::KEY_POS => $position,
316 self::KEY_LOCATION => $location
318 $claimed[$posKey] = [
'node' => $node,
'index' => count( $ring ) - 1 ];
323 usort( $ring,
static function ( $a, $b ) {
324 if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) {
325 throw new UnexpectedValueException(
'Duplicate node positions.' );
328 return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 );
338 private function getItemPosition( $item ) {
341 $octets = substr( hash( $this->algo, (
string)$item,
true ), 0, 4 );
342 if ( strlen( $octets ) != 4 ) {
343 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 32 bits." );
346 $pos = unpack(
'V', $octets )[1];
350 $pos = sprintf(
'%u', $pos );
360 private function getNodePositionQuartet( $nodeGroupName ) {
361 $octets = substr( hash( $this->algo, (
string)$nodeGroupName,
true ), 0, 16 );
362 if ( strlen( $octets ) != 16 ) {
363 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 128 bits." );
367 foreach ( unpack(
'V4', $octets ) as $signed ) {
368 $positions[] = (float)sprintf(
'%u', $signed );
379 private function getNextClockwiseNodeIndex( $i, $ring ) {
380 if ( !isset( $ring[$i] ) ) {
381 throw new UnexpectedValueException( __METHOD__ .
": reference index is invalid." );
386 return ( $next < count( $ring ) ) ? $next : 0;
396 if ( !$this->ejectExpiryByLocation ) {
402 if ( $this->liveRing ===
null || min( $this->ejectExpiryByLocation ) <= $now ) {
404 $this->ejectExpiryByLocation = array_filter(
405 $this->ejectExpiryByLocation,
406 static function ( $expiry ) use ( $now ) {
407 return ( $expiry > $now );
411 if ( count( $this->ejectExpiryByLocation ) ) {
414 foreach ( $this->baseRing as $nodeInfo ) {
416 if ( !isset( $this->ejectExpiryByLocation[$location] ) ) {
427 if ( !$this->liveRing ) {
428 throw new UnexpectedValueException(
"The live ring is currently empty." );
450 if ( is_array( $data ) ) {
451 $this->
init( $data[
'locations'] ?? [], $data[
'algorithm'] ??
'sha1', $data[
'ejections'] ?? [] );
453 throw new UnexpectedValueException( __METHOD__ .
": unable to decode JSON." );