MediaWiki master
HashRing.php
Go to the documentation of this file.
1<?php
10
11use InvalidArgumentException;
12use RuntimeException;
13use UnexpectedValueException;
14
36class HashRing {
38 protected $algo;
43
45 protected $baseRing;
47 protected $liveRing;
48
50 private const HASHES_PER_LOCATION = 40;
52 private const SECTORS_PER_HASH = 4;
53
54 public const KEY_POS = 0;
55 public const KEY_LOCATION = 1;
56
58 public const RING_ALL = 0;
60 public const RING_LIVE = 1;
61
70 public function __construct( array $map, $algo = 'sha1', array $ejections = [] ) {
71 $this->init( $map, $algo, $ejections );
72 }
73
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." );
82 }
83
84 $weightByLocation = array_filter( $map );
85 if ( $weightByLocation === [] ) {
86 throw new UnexpectedValueException( "No locations with non-zero weight." );
87 } elseif ( min( $map ) < 0 ) {
88 throw new InvalidArgumentException( "Location weight cannot be negative." );
89 }
90
91 $this->algo = $algo;
92 $this->weightByLocation = $weightByLocation;
93 $this->ejectExpiryByLocation = $ejections;
94 $this->baseRing = $this->buildLocationRing( $this->weightByLocation );
95 }
96
104 final public function getLocation( $item ) {
105 return $this->getLocations( $item, 1 )[0];
106 }
107
117 public function getLocations( $item, $limit, $from = self::RING_ALL ) {
118 if ( $from === self::RING_ALL ) {
119 $ring = $this->baseRing;
120 } elseif ( $from === self::RING_LIVE ) {
121 $ring = $this->getLiveRing();
122 } else {
123 throw new InvalidArgumentException( "Invalid ring source specified." );
124 }
125
126 // Short-circuit for the common single-location case. Note that if there was only one
127 // location and it was ejected from the live ring, getLiveRing() would have error out.
128 if ( count( $this->weightByLocation ) == 1 ) {
129 return ( $limit > 0 ) ? [ $ring[0][self::KEY_LOCATION] ] : [];
130 }
131
132 // Locate the node index for this item's position on the hash ring
133 $itemIndex = $this->findNodeIndexForPosition( $this->getItemPosition( $item ), $ring );
134
135 $locations = [];
136 $currentIndex = null;
137 while ( count( $locations ) < $limit ) {
138 if ( $currentIndex === null ) {
139 $currentIndex = $itemIndex;
140 } else {
141 $currentIndex = $this->getNextClockwiseNodeIndex( $currentIndex, $ring );
142 if ( $currentIndex === $itemIndex ) {
143 break; // all nodes visited
144 }
145 }
146 // @phan-suppress-next-line PhanTypeMismatchDimFetchNullable False positive
147 $nodeLocation = $ring[$currentIndex][self::KEY_LOCATION];
148 if ( !in_array( $nodeLocation, $locations, true ) ) {
149 // Ignore other nodes for the same locations already added
150 $locations[] = $nodeLocation;
151 }
152 }
153
154 return $locations;
155 }
156
162 private function findNodeIndexForPosition( $position, $ring ) {
163 $count = count( $ring );
164 if ( $count === 0 ) {
165 return null;
166 }
167
168 $index = null;
169 $lowPos = 0;
170 $highPos = $count;
171 while ( true ) {
172 $midPos = (int)( ( $lowPos + $highPos ) / 2 );
173 if ( $midPos === $count ) {
174 $index = 0;
175 break;
176 }
177
178 $midVal = $ring[$midPos][self::KEY_POS];
179 $midMinusOneVal = ( $midPos === 0 ) ? 0 : $ring[$midPos - 1][self::KEY_POS];
180 if ( $position <= $midVal && $position > $midMinusOneVal ) {
181 $index = $midPos;
182 break;
183 }
184
185 if ( $midVal < $position ) {
186 $lowPos = $midPos + 1;
187 } else {
188 $highPos = $midPos - 1;
189 }
190
191 if ( $lowPos > $highPos ) {
192 $index = 0;
193 break;
194 }
195 }
196
197 return $index;
198 }
199
205 public function getLocationWeights() {
207 }
208
217 public function ejectFromLiveRing( $location, $ttl ) {
218 if ( !isset( $this->weightByLocation[$location] ) ) {
219 throw new UnexpectedValueException( "No location '$location' in the ring." );
220 }
221
222 $expiry = $this->getCurrentTime() + $ttl;
223 $this->ejectExpiryByLocation[$location] = $expiry;
224
225 $this->liveRing = null; // invalidate ring cache
226
227 return ( count( $this->ejectExpiryByLocation ) < count( $this->weightByLocation ) );
228 }
229
237 final public function getLiveLocation( $item ) {
238 return $this->getLocations( $item, 1, self::RING_LIVE )[0];
239 }
240
249 final public function getLiveLocations( $item, $limit ) {
250 return $this->getLocations( $item, $limit, self::RING_LIVE );
251 }
252
259 public function getLiveLocationWeights() {
260 $now = $this->getCurrentTime();
261
262 return array_diff_key(
263 $this->weightByLocation,
264 array_filter(
265 $this->ejectExpiryByLocation,
266 static function ( $expiry ) use ( $now ) {
267 return ( $expiry > $now );
268 }
269 )
270 );
271 }
272
277 private function buildLocationRing( array $weightByLocation ) {
278 $locationCount = count( $weightByLocation );
279 $totalWeight = array_sum( $weightByLocation );
280
281 $ring = [];
282 // Assign nodes to all locations based on location weight
283 $claimed = []; // (position as string => (node, index))
284 foreach ( $weightByLocation as $location => $weight ) {
285 $ratio = $weight / $totalWeight;
286 // There $locationCount * (HASHES_PER_LOCATION * 4) nodes available;
287 // assign a few groups of nodes to this location based on its weight.
288 $nodesQuartets = intval( $ratio * self::HASHES_PER_LOCATION * $locationCount );
289 for ( $qi = 0; $qi < $nodesQuartets; ++$qi ) {
290 // For efficiency, get 4 points per hash call and 4X node count.
291 // If $algo is MD5, then this matches that of with libketama.
292 // See https://github.com/RJ/ketama/blob/master/libketama/ketama.c
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; // large integer
297 if ( isset( $claimed[$posKey] ) ) {
298 // Disallow duplicates (name decides precedence)
299 if ( $claimed[$posKey]['node'] > $node ) {
300 continue;
301 } else {
302 unset( $ring[$claimed[$posKey]['index']] );
303 }
304 }
305 $ring[] = [
306 self::KEY_POS => $position,
307 self::KEY_LOCATION => $location
308 ];
309 $claimed[$posKey] = [ 'node' => $node, 'index' => count( $ring ) - 1 ];
310 }
311 }
312 }
313 // Sort the locations into clockwise order based on the hash ring position
314 usort( $ring, static function ( $a, $b ) {
315 if ( $a[self::KEY_POS] === $b[self::KEY_POS] ) {
316 throw new UnexpectedValueException( 'Duplicate node positions.' );
317 }
318
319 return ( $a[self::KEY_POS] < $b[self::KEY_POS] ? -1 : 1 );
320 } );
321
322 return $ring;
323 }
324
329 private function getItemPosition( $item ) {
330 // If $algo is MD5, then this matches that of with libketama.
331 // See https://github.com/RJ/ketama/blob/master/libketama/ketama.c
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." );
335 }
336
337 $pos = unpack( 'V', $octets )[1];
338 if ( $pos < 0 ) {
339 // Most-significant-bit is set, causing unpack() to return a negative integer due
340 // to the fact that it returns a signed int. Cast it to an unsigned integer string.
341 $pos = sprintf( '%u', $pos );
342 }
343
344 return (float)$pos;
345 }
346
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." );
355 }
356
357 $positions = [];
358 foreach ( unpack( 'V4', $octets ) as $signed ) {
359 $positions[] = (float)sprintf( '%u', $signed );
360 }
361
362 return $positions;
363 }
364
370 private function getNextClockwiseNodeIndex( $i, $ring ) {
371 if ( !isset( $ring[$i] ) ) {
372 throw new UnexpectedValueException( __METHOD__ . ": reference index is invalid." );
373 }
374
375 $next = $i + 1;
376
377 return ( $next < count( $ring ) ) ? $next : 0;
378 }
379
386 protected function getLiveRing() {
387 if ( !$this->ejectExpiryByLocation ) {
388 return $this->baseRing; // nothing ejected
389 }
390
391 $now = $this->getCurrentTime();
392
393 if ( $this->liveRing === null || min( $this->ejectExpiryByLocation ) <= $now ) {
394 // Live ring needs to be regenerated...
395 $this->ejectExpiryByLocation = array_filter(
396 $this->ejectExpiryByLocation,
397 static function ( $expiry ) use ( $now ) {
398 return ( $expiry > $now );
399 }
400 );
401
402 if ( count( $this->ejectExpiryByLocation ) ) {
403 // Some locations are still ejected from the ring
404 $liveRing = [];
405 foreach ( $this->baseRing as $nodeInfo ) {
406 $location = $nodeInfo[self::KEY_LOCATION];
407 if ( !isset( $this->ejectExpiryByLocation[$location] ) ) {
408 $liveRing[] = $nodeInfo;
409 }
410 }
411 } else {
413 }
414
415 $this->liveRing = $liveRing;
416 }
417
418 if ( !$this->liveRing ) {
419 throw new UnexpectedValueException( "The live ring is currently empty." );
420 }
421
422 return $this->liveRing;
423 }
424
428 protected function getCurrentTime() {
429 return time();
430 }
431
432 public function __serialize() {
433 return [
434 'algorithm' => $this->algo,
435 'locations' => $this->weightByLocation,
436 'ejections' => $this->ejectExpiryByLocation
437 ];
438 }
439
440 public function __unserialize( $data ) {
441 if ( is_array( $data ) ) {
442 $this->init( $data['locations'] ?? [], $data['algorithm'] ?? 'sha1', $data['ejections'] ?? [] );
443 } else {
444 throw new UnexpectedValueException( __METHOD__ . ": unable to decode JSON." );
445 }
446 }
447}
448
450class_alias( HashRing::class, 'HashRing' );
Convenience class for weighted consistent hash rings.
Definition HashRing.php:36
ejectFromLiveRing( $location, $ttl)
Remove a location from the "live" hash ring.
Definition HashRing.php:217
array[] $baseRing
Non-empty position-ordered list of (position, location name)
Definition HashRing.php:45
getLiveLocationWeights()
Get the map of "live" locations to weight (does not include zero weight items)
Definition HashRing.php:259
int[] $ejectExpiryByLocation
Map of (location => UNIX timestamp)
Definition HashRing.php:42
__construct(array $map, $algo='sha1', array $ejections=[])
Make a consistent hash ring given a set of locations and their weight values.
Definition HashRing.php:70
array[] null $liveRing
Non-empty position-ordered list of (position, location name)
Definition HashRing.php:47
int[] $weightByLocation
Non-empty (location => integer weight)
Definition HashRing.php:40
init(array $map, $algo, array $ejections)
Definition HashRing.php:79
getLocationWeights()
Get the map of locations to weight (does not include zero weight items)
Definition HashRing.php:205
getLiveRing()
Get the "live" hash ring (which does not include ejected locations)
Definition HashRing.php:386
getLiveLocations( $item, $limit)
Get the location of an item on the "live" ring, as well as the next locations.
Definition HashRing.php:249
getLiveLocation( $item)
Get the location of an item on the "live" ring.
Definition HashRing.php:237
getLocation( $item)
Get the location of an item on the ring.
Definition HashRing.php:104
getLocations( $item, $limit, $from=self::RING_ALL)
Get the location of an item on the ring followed by the next ring locations.
Definition HashRing.php:117
string $algo
Hashing algorithm for hash()
Definition HashRing.php:38