50 private const HASHES_PER_LOCATION = 40;
52 private const SECTORS_PER_HASH = 4;
58 public const RING_ALL = 0;
60 public const RING_LIVE = 1;
79 protected function init( array $map,
$algo, array $ejections ) {
80 if ( !in_array(
$algo, hash_algos(),
true ) ) {
81 throw new RuntimeException( __METHOD__ .
": unsupported '$algo' hash algorithm." );
86 throw new UnexpectedValueException(
"No locations with non-zero weight." );
87 } elseif ( min( $map ) < 0 ) {
88 throw new InvalidArgumentException(
"Location weight cannot be negative." );
93 $this->ejectExpiryByLocation = $ejections;
94 $this->baseRing = $this->buildLocationRing( $this->weightByLocation );
117 public function getLocations( $item, $limit, $from = self::RING_ALL ) {
118 if ( $from === self::RING_ALL ) {
120 } elseif ( $from === self::RING_LIVE ) {
123 throw new InvalidArgumentException(
"Invalid ring source specified." );
128 if ( count( $this->weightByLocation ) == 1 ) {
133 $itemIndex = $this->findNodeIndexForPosition( $this->getItemPosition( $item ), $ring );
136 $currentIndex =
null;
137 while ( count( $locations ) < $limit ) {
138 if ( $currentIndex ===
null ) {
139 $currentIndex = $itemIndex;
141 $currentIndex = $this->getNextClockwiseNodeIndex( $currentIndex, $ring );
142 if ( $currentIndex === $itemIndex ) {
148 if ( !in_array( $nodeLocation, $locations,
true ) ) {
150 $locations[] = $nodeLocation;
162 private function findNodeIndexForPosition( $position, $ring ) {
163 $count = count( $ring );
164 if ( $count === 0 ) {
172 $midPos = (int)( ( $lowPos + $highPos ) / 2 );
173 if ( $midPos === $count ) {
179 $midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][self::
KEY_POS];
180 if ( $position <= $midVal && $position > $midMinusOneVal ) {
185 if ( $midVal < $position ) {
186 $lowPos = $midPos + 1;
188 $highPos = $midPos - 1;
191 if ( $lowPos > $highPos ) {
218 if ( !isset( $this->weightByLocation[$location] ) ) {
219 throw new UnexpectedValueException(
"No location '$location' in the ring." );
223 $this->ejectExpiryByLocation[$location] = $expiry;
225 $this->liveRing =
null;
227 return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) );
238 return $this->
getLocations( $item, 1, self::RING_LIVE )[0];
250 return $this->
getLocations( $item, $limit, self::RING_LIVE );
262 return array_diff_key(
263 $this->weightByLocation,
265 $this->ejectExpiryByLocation,
266 static function ( $expiry ) use ( $now ) {
267 return ( $expiry > $now );
285 $ratio = $weight / $totalWeight;
288 $nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount );
289 for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) {
293 $positions = $this->getNodePositionQuartet(
"{$location}-{$qi}" );
294 foreach ( $positions as $gi => $position ) {
295 $node = ( $qi * self::SECTORS_PER_HASH + $gi ) .
"@$location";
296 $posKey = (string)$position;
297 if ( isset( $claimed[$posKey] ) ) {
299 if ( $claimed[$posKey][
'node'] > $node ) {
302 unset( $ring[$claimed[$posKey][
'index']] );
306 self::KEY_POS => $position,
307 self::KEY_LOCATION => $location
309 $claimed[$posKey] = [
'node' => $node,
'index' => count( $ring ) - 1 ];
314 usort( $ring,
static function ( $a, $b ) {
315 if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) {
316 throw new UnexpectedValueException(
'Duplicate node positions.' );
319 return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 );
329 private function getItemPosition( $item ) {
332 $octets = substr( hash( $this->algo, (
string)$item,
true ), 0, 4 );
333 if ( strlen( $octets ) != 4 ) {
334 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 32 bits." );
337 $pos = unpack(
'V', $octets )[1];
341 $pos = sprintf(
'%u', $pos );
351 private function getNodePositionQuartet( $nodeGroupName ) {
352 $octets = substr( hash( $this->algo, (
string)$nodeGroupName,
true ), 0, 16 );
353 if ( strlen( $octets ) != 16 ) {
354 throw new UnexpectedValueException( __METHOD__ .
": {$this->algo} is < 128 bits." );
358 foreach ( unpack(
'V4', $octets ) as $signed ) {
359 $positions[] = (float)sprintf(
'%u', $signed );
370 private function getNextClockwiseNodeIndex( $i, $ring ) {
371 if ( !isset( $ring[$i] ) ) {
372 throw new UnexpectedValueException( __METHOD__ .
": reference index is invalid." );
377 return ( $next < count( $ring ) ) ? $next : 0;
387 if ( !$this->ejectExpiryByLocation ) {
393 if ( $this->liveRing ===
null || min( $this->ejectExpiryByLocation ) <= $now ) {
395 $this->ejectExpiryByLocation = array_filter(
396 $this->ejectExpiryByLocation,
397 static function ( $expiry ) use ( $now ) {
398 return ( $expiry > $now );
402 if ( count( $this->ejectExpiryByLocation ) ) {
405 foreach ( $this->baseRing as $nodeInfo ) {
407 if ( !isset( $this->ejectExpiryByLocation[$location] ) ) {
418 if ( !$this->liveRing ) {
419 throw new UnexpectedValueException(
"The live ring is currently empty." );
441 if ( is_array( $data ) ) {
442 $this->
init( $data[
'locations'] ?? [], $data[
'algorithm'] ??
'sha1', $data[
'ejections'] ?? [] );
444 throw new UnexpectedValueException( __METHOD__ .
": unable to decode JSON." );