47 $map = array_filter( $map,
function ( $w ) {
50 if ( !count( $map ) ) {
51 throw new UnexpectedValueException(
"Ring is empty or all weights are zero." );
53 $this->sourceMap = $map;
56 foreach ( $map as $location => $weight ) {
57 $hashes[$location] = sha1( $location );
59 uksort( $map,
function ( $a, $b ) use (
$hashes ) {
63 $sum = array_sum( $map );
65 foreach ( $map as $location => $weight ) {
66 $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
70 foreach ( $standardMap as $location => $weight ) {
72 $this->ring[$location] = [ $index, $index + $weight ];
101 $primaryLocation =
null;
102 $spot = hexdec( substr( sha1( $item ), 0, 7 ) );
103 foreach ( $this->ring as $location => $range ) {
104 if ( count( $locations ) >= $limit ) {
109 if ( ( $range[0] <= $spot && $spot < $range[1] ) || $primaryLocation !==
null ) {
110 if ( $primaryLocation ===
null ) {
111 $primaryLocation = $location;
113 $locations[] = $location;
117 reset( $this->ring );
118 while ( count( $locations ) < $limit ) {
119 list( $location, ) = each( $this->ring );
120 if ( $location === $primaryLocation ) {
123 $locations[] = $location;
146 unset( $map[$location] );
148 return count( $map ) ?
new self( $map ) :
false;
159 if ( !isset( $this->sourceMap[$location] ) ) {
160 throw new UnexpectedValueException(
"No location '$location' in the ring." );
162 $expiry = time() + $ttl;
163 $this->liveRing =
null;
164 $this->ejectionExpiries[$location] = $expiry;
165 $this->ejectionNextExpiry = min( $expiry, $this->ejectionNextExpiry );
167 return ( count( $this->ejectionExpiries ) < count( $this->sourceMap ) );
178 if ( $this->liveRing ===
null || $this->ejectionNextExpiry <= $now ) {
179 $this->ejectionExpiries = array_filter(
180 $this->ejectionExpiries,
181 function ( $expiry ) use ( $now ) {
182 return ( $expiry > $now );
185 if ( count( $this->ejectionExpiries ) ) {
186 $map = array_diff_key( $this->sourceMap, $this->ejectionExpiries );
187 $this->liveRing = count( $map ) ?
new self( $map ) :
false;
189 $this->ejectionNextExpiry = min( $this->ejectionExpiries );
191 $this->liveRing = clone $this;
192 $this->liveRing->ejectionExpiries = [];
193 $this->liveRing->ejectionNextExpiry = INF;
194 $this->liveRing->liveRing =
null;
196 $this->ejectionNextExpiry = INF;
199 if ( !$this->liveRing ) {
200 throw new UnexpectedValueException(
"The live ring is currently empty." );
226 return $this->
getLiveRing()->getLocations( $item, $limit );
Convenience class for weighted consistent hash rings.
getLiveLocations( $item, $limit)
Get the location of an item on the "live" ring, as well as the next locations.
Array $sourceMap
(location => weight)
getLiveLocationWeights()
Get the map of "live" locations to weight (ignores 0-weight items)
int $ejectionNextExpiry
UNIX timestamp.
getLiveLocation( $item)
Get the location of an item on the "live" ring.
newWithoutLocation( $location)
Get a new hash ring with a location removed from the ring.
getLocation( $item)
Get the location of an item on the ring.
getLocations( $item, $limit)
Get the location of an item on the ring, as well as the next locations.
Array $ejectionExpiries
(location => UNIX timestamp)
Array $ring
(location => (start, end))
getLocationWeights()
Get the map of locations to weight (ignores 0-weight items)
getLiveRing()
Get the "live" hash ring (which does not include ejected locations)
ejectFromLiveRing( $location, $ttl)
Remove a location from the "live" hash ring.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key