MediaWiki REL1_41
|
Convenience class for weighted consistent hash rings. More...
Public Member Functions | |
__construct (array $map, $algo='sha1', array $ejections=[]) | |
Make a consistent hash ring given a set of locations and their weight values. | |
__serialize () | |
__unserialize ( $data) | |
ejectFromLiveRing ( $location, $ttl) | |
Remove a location from the "live" hash ring. | |
getLiveLocation ( $item) | |
Get the location of an item on the "live" ring. | |
getLiveLocations ( $item, $limit) | |
Get the location of an item on the "live" ring, as well as the next locations. | |
getLiveLocationWeights () | |
Get the map of "live" locations to weight (does not include zero weight items) | |
getLocation ( $item) | |
Get the location of an item on the ring. | |
getLocations ( $item, $limit, $from=self::RING_ALL) | |
Get the location of an item on the ring followed by the next ring locations. | |
getLocationWeights () | |
Get the map of locations to weight (does not include zero weight items) | |
Public Attributes | |
const | KEY_LOCATION = 1 |
const | KEY_POS = 0 |
Protected Member Functions | |
getCurrentTime () | |
getLiveRing () | |
Get the "live" hash ring (which does not include ejected locations) | |
init (array $map, $algo, array $ejections) | |
Protected Attributes | |
string | $algo |
Hashing algorithm for hash() | |
array[] | $baseRing |
Non-empty position-ordered list of (position, location name) | |
int[] | $ejectExpiryByLocation |
Map of (location => UNIX timestamp) | |
array[] null | $liveRing |
Non-empty position-ordered list of (position, location name) | |
int[] | $weightByLocation |
Non-empty (location => integer weight) | |
Convenience class for weighted consistent hash rings.
This deterministically maps "keys" to a set of "locations" while avoiding clumping
Each location is represented by a number of nodes on a ring proportionate to the ratio of its weight compared to the total location weight. Note positions are deterministically derived from the hash of the location name. Nodes are responsible for the portion of the ring, counter-clockwise, up until the next node. Locations are responsible for all portions of the ring that the location's nodes are responsible for.
A location that is temporarily "ejected" is said to be absent from the "live" ring. If no location ejections are active, then the base ring and live ring are identical.
This class is designed in a way that using the "md5" algorithm will make it compatible with libketama, e.g. OPT_LIBKETAMA_COMPATIBLE from the PECL memcached extension or "ketama" from twemproxy. This can simplify the process of switching client libraries. However, note that different clients might use incompatible 32-bit memcached value flag conventions.
Definition at line 44 of file HashRing.php.
HashRing::__construct | ( | array | $map, |
$algo = 'sha1', | |||
array | $ejections = [] ) |
Make a consistent hash ring given a set of locations and their weight values.
int[] | $map | Map of (location => weight) |
string | $algo | Hashing algorithm listed in hash_algos() [optional] |
int[] | $ejections | Map of (location => UNIX timestamp) for ejection expiries |
Definition at line 78 of file HashRing.php.
HashRing::__serialize | ( | ) |
Definition at line 441 of file HashRing.php.
References $algo, $ejectExpiryByLocation, and $weightByLocation.
HashRing::__unserialize | ( | $data | ) |
Definition at line 449 of file HashRing.php.
References init().
HashRing::ejectFromLiveRing | ( | $location, | |
$ttl ) |
Remove a location from the "live" hash ring.
string | $location | |
int | $ttl | Seconds |
UnexpectedValueException |
Definition at line 226 of file HashRing.php.
References getCurrentTime().
Referenced by JobQueueFederated\tryJobInsertions().
|
protected |
Definition at line 437 of file HashRing.php.
Referenced by ejectFromLiveRing(), getLiveLocationWeights(), and getLiveRing().
|
final |
Get the location of an item on the "live" ring.
string | $item |
UnexpectedValueException |
Definition at line 246 of file HashRing.php.
References getLocations().
Referenced by JobQueueFederated\tryJobInsertions().
|
final |
Get the location of an item on the "live" ring, as well as the next locations.
string | $item | |
int | $limit | Maximum number of locations to return |
UnexpectedValueException |
Definition at line 258 of file HashRing.php.
References getLocations().
HashRing::getLiveLocationWeights | ( | ) |
Get the map of "live" locations to weight (does not include zero weight items)
UnexpectedValueException |
Definition at line 268 of file HashRing.php.
References getCurrentTime().
Referenced by JobQueueFederated\doBatchPush(), and JobQueueFederated\tryJobInsertions().
|
protected |
Get the "live" hash ring (which does not include ejected locations)
UnexpectedValueException |
Definition at line 395 of file HashRing.php.
References $baseRing, $liveRing, getCurrentTime(), and KEY_LOCATION.
Referenced by getLocations().
|
final |
Get the location of an item on the ring.
string | $item |
UnexpectedValueException |
Definition at line 112 of file HashRing.php.
References getLocations().
HashRing::getLocations | ( | $item, | |
$limit, | |||
$from = self::RING_ALL ) |
Get the location of an item on the ring followed by the next ring locations.
string | $item | |
int | $limit | Maximum number of locations to return |
int | $from | One of the RING_* class constants |
InvalidArgumentException | |
UnexpectedValueException |
Definition at line 126 of file HashRing.php.
References $baseRing, getLiveRing(), and KEY_LOCATION.
Referenced by getLiveLocation(), getLiveLocations(), and getLocation().
HashRing::getLocationWeights | ( | ) |
Get the map of locations to weight (does not include zero weight items)
Definition at line 214 of file HashRing.php.
References $weightByLocation.
|
protected |
int[] | $map | Map of (location => integer) |
string | $algo | Hashing algorithm |
int[] | $ejections | Map of (location => UNIX timestamp) for ejection expires |
Definition at line 87 of file HashRing.php.
References $algo, and $weightByLocation.
Referenced by __construct(), and __unserialize().
|
protected |
Hashing algorithm for hash()
Definition at line 46 of file HashRing.php.
Referenced by __construct(), __serialize(), and init().
|
protected |
Non-empty position-ordered list of (position, location name)
Definition at line 53 of file HashRing.php.
Referenced by getLiveRing(), and getLocations().
|
protected |
Map of (location => UNIX timestamp)
Definition at line 50 of file HashRing.php.
Referenced by __serialize().
|
protected |
Non-empty position-ordered list of (position, location name)
Definition at line 55 of file HashRing.php.
Referenced by getLiveRing().
|
protected |
Non-empty (location => integer weight)
Definition at line 48 of file HashRing.php.
Referenced by __serialize(), getLocationWeights(), and init().
const HashRing::KEY_LOCATION = 1 |
Definition at line 63 of file HashRing.php.
Referenced by getLiveRing(), and getLocations().
const HashRing::KEY_POS = 0 |
Definition at line 62 of file HashRing.php.