| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class HashRing { |
| 45 |
|
| 46 |
protected $algo; |
| 47 |
|
| 48 |
protected $weightByLocation; |
| 49 |
|
| 50 |
protected $ejectExpiryByLocation; |
| 51 |
|
| 52 |
|
| 53 |
protected $baseRing; |
| 54 |
|
| 55 |
protected $liveRing; |
| 56 |
|
| 57 |
|
| 58 |
private const HASHES_PER_LOCATION = 40; |
| 59 |
|
| 60 |
private const SECTORS_PER_HASH = 4; |
| 61 |
|
| 62 |
public const KEY_POS = 0; |
| 63 |
public const KEY_LOCATION = 1; |
| 64 |
|
| 65 |
|
| 66 |
public const RING_ALL = 0; |
| 67 |
|
| 68 |
public const RING_LIVE = 1; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct( array $map, $algo = 'sha1', array $ejections = [] ) { |
| 79 |
$this->init( $map, $algo, $ejections ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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." ); |
| 90 |
} |
| 91 |
|
| 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." ); |
| 97 |
} |
| 98 |
|
| 99 |
$this->algo = $algo; |
| 100 |
$this->weightByLocation = $weightByLocation; |
| 101 |
$this->ejectExpiryByLocation = $ejections; |
| 102 |
$this->baseRing = $this->buildLocationRing( $this->weightByLocation ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
final public function getLocation( $item ) { |
| 113 |
return $this->getLocations( $item, 1 )[0]; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 ) { |
| 129 |
$ring = $this->getLiveRing(); |
| 130 |
} else { |
| 131 |
throw new InvalidArgumentException( "Invalid ring source specified." ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if ( count( $this->weightByLocation ) == 1 ) { |
| 137 |
return ( $limit > 0 ) ? [ $ring[0][self::KEY_LOCATION] ] : []; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$itemIndex = $this->findNodeIndexForPosition( $this->getItemPosition( $item ), $ring ); |
| 142 |
|
| 143 |
$locations = []; |
| 144 |
$currentIndex = null; |
| 145 |
while ( count( $locations ) < $limit ) { |
| 146 |
if ( $currentIndex === null ) { |
| 147 |
$currentIndex = $itemIndex; |
| 148 |
} else { |
| 149 |
$currentIndex = $this->getNextClockwiseNodeIndex( $currentIndex, $ring ); |
| 150 |
if ( $currentIndex === $itemIndex ) { |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$nodeLocation = $ring[$currentIndex][self::KEY_LOCATION]; |
| 156 |
if ( !in_array( $nodeLocation, $locations, true ) ) { |
| 157 |
|
| 158 |
$locations[] = $nodeLocation; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $locations; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private function findNodeIndexForPosition( $position, $ring ) { |
| 171 |
$count = count( $ring ); |
| 172 |
if ( $count === 0 ) { |
| 173 |
return null; |
| 174 |
} |
| 175 |
|
| 176 |
$index = null; |
| 177 |
$lowPos = 0; |
| 178 |
$highPos = $count; |
| 179 |
while ( true ) { |
| 180 |
$midPos = (int)( ( $lowPos + $highPos ) / 2 ); |
| 181 |
if ( $midPos === $count ) { |
| 182 |
$index = 0; |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
$midVal = $ring[$midPos][self::KEY_POS]; |
| 187 |
$midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][self::KEY_POS]; |
| 188 |
if ( $position <= $midVal && $position > $midMinusOneVal ) { |
| 189 |
$index = $midPos; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
if ( $midVal < $position ) { |
| 194 |
$lowPos = $midPos + 1; |
| 195 |
} else { |
| 196 |
$highPos = $midPos - 1; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $lowPos > $highPos ) { |
| 200 |
$index = 0; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return $index; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function getLocationWeights() { |
| 214 |
return $this->weightByLocation; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function ejectFromLiveRing( $location, $ttl ) { |
| 226 |
if ( !isset( $this->weightByLocation[$location] ) ) { |
| 227 |
throw new UnexpectedValueException( "No location '$location' in the ring." ); |
| 228 |
} |
| 229 |
|
| 230 |
$expiry = $this->getCurrentTime() + $ttl; |
| 231 |
$this->ejectExpiryByLocation[$location] = $expiry; |
| 232 |
|
| 233 |
$this->liveRing = null; |
| 234 |
|
| 235 |
return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
final public function getLiveLocation( $item ) { |
| 246 |
return $this->getLocations( $item, 1, self::RING_LIVE )[0]; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
final public function getLiveLocations( $item, $limit ) { |
| 258 |
return $this->getLocations( $item, $limit, self::RING_LIVE ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function getLiveLocationWeights() { |
| 268 |
$now = $this->getCurrentTime(); |
| 269 |
|
| 270 |
return array_diff_key( |
| 271 |
$this->weightByLocation, |
| 272 |
array_filter( |
| 273 |
$this->ejectExpiryByLocation, |
| 274 |
static function ( $expiry ) use ( $now ) { |
| 275 |
return ( $expiry > $now ); |
| 276 |
} |
| 277 |
) |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private function buildLocationRing( array $weightByLocation ) { |
| 286 |
$locationCount = count( $weightByLocation ); |
| 287 |
$totalWeight = array_sum( $weightByLocation ); |
| 288 |
|
| 289 |
$ring = []; |
| 290 |
|
| 291 |
$claimed = []; |
| 292 |
foreach ( $weightByLocation as $location => $weight ) { |
| 293 |
$ratio = $weight / $totalWeight; |
| 294 |
|
| 295 |
|
| 296 |
$nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount ); |
| 297 |
for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 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] ) ) { |
| 306 |
|
| 307 |
if ( $claimed[$posKey]['node'] > $node ) { |
| 308 |
continue; |
| 309 |
} else { |
| 310 |
unset( $ring[$claimed[$posKey]['index']] ); |
| 311 |
} |
| 312 |
} |
| 313 |
$ring[] = [ |
| 314 |
self::KEY_POS => $position, |
| 315 |
self::KEY_LOCATION => $location |
| 316 |
]; |
| 317 |
$claimed[$posKey] = [ 'node' => $node, 'index' => count( $ring ) - 1 ]; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
usort( $ring, static function ( $a, $b ) { |
| 323 |
if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) { |
| 324 |
throw new UnexpectedValueException( 'Duplicate node positions.' ); |
| 325 |
} |
| 326 |
|
| 327 |
return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 ); |
| 328 |
} ); |
| 329 |
|
| 330 |
return $ring; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private function getItemPosition( $item ) { |
| 338 |
|
| 339 |
|
| 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." ); |
| 343 |
} |
| 344 |
|
| 345 |
$pos = unpack( 'V', $octets )[1]; |
| 346 |
if ( $pos < 0 ) { |
| 347 |
|
| 348 |
|
| 349 |
$pos = sprintf( '%u', $pos ); |
| 350 |
} |
| 351 |
|
| 352 |
return (float)$pos; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 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." ); |
| 363 |
} |
| 364 |
|
| 365 |
$positions = []; |
| 366 |
foreach ( unpack( 'V4', $octets ) as $signed ) { |
| 367 |
$positions[] = (float)sprintf( '%u', $signed ); |
| 368 |
} |
| 369 |
|
| 370 |
return $positions; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
private function getNextClockwiseNodeIndex( $i, $ring ) { |
| 379 |
if ( !isset( $ring[$i] ) ) { |
| 380 |
throw new UnexpectedValueException( __METHOD__ . ": reference index is invalid." ); |
| 381 |
} |
| 382 |
|
| 383 |
$next = $i + 1; |
| 384 |
|
| 385 |
return ( $next < count( $ring ) ) ? $next : 0; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
protected function getLiveRing() { |
| 395 |
if ( !$this->ejectExpiryByLocation ) { |
| 396 |
return $this->baseRing; |
| 397 |
} |
| 398 |
|
| 399 |
$now = $this->getCurrentTime(); |
| 400 |
|
| 401 |
if ( $this->liveRing === null || min( $this->ejectExpiryByLocation ) <= $now ) { |
| 402 |
|
| 403 |
$this->ejectExpiryByLocation = array_filter( |
| 404 |
$this->ejectExpiryByLocation, |
| 405 |
static function ( $expiry ) use ( $now ) { |
| 406 |
return ( $expiry > $now ); |
| 407 |
} |
| 408 |
); |
| 409 |
|
| 410 |
if ( count( $this->ejectExpiryByLocation ) ) { |
| 411 |
|
| 412 |
$liveRing = []; |
| 413 |
foreach ( $this->baseRing as $nodeInfo ) { |
| 414 |
$location = $nodeInfo[self::KEY_LOCATION]; |
| 415 |
if ( !isset( $this->ejectExpiryByLocation[$location] ) ) { |
| 416 |
$liveRing[] = $nodeInfo; |
| 417 |
} |
| 418 |
} |
| 419 |
} else { |
| 420 |
$liveRing = $this->baseRing; |
| 421 |
} |
| 422 |
|
| 423 |
$this->liveRing = $liveRing; |
| 424 |
} |
| 425 |
|
| 426 |
if ( !$this->liveRing ) { |
| 427 |
throw new UnexpectedValueException( "The live ring is currently empty." ); |
| 428 |
} |
| 429 |
|
| 430 |
return $this->liveRing; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
protected function getCurrentTime() { |
| 437 |
return time(); |
| 438 |
} |
| 439 |
|
| 440 |
public function __serialize() { |
| 441 |
return [ |
| 442 |
'algorithm' => $this->algo, |
| 443 |
'locations' => $this->weightByLocation, |
| 444 |
'ejections' => $this->ejectExpiryByLocation |
| 445 |
]; |
| 446 |
} |
| 447 |
|
| 448 |
public function __unserialize( $data ) { |
| 449 |
if ( is_array( $data ) ) { |
| 450 |
$this->init( $data['locations'] ?? [], $data['algorithm'] ?? 'sha1', $data['ejections'] ?? [] ); |
| 451 |
} else { |
| 452 |
throw new UnexpectedValueException( __METHOD__ . ": unable to decode JSON." ); |
| 453 |
} |
| 454 |
} |
| 455 |
} |