Introduce includes/utils directory
[lhc/web/wiklou.git] / includes / utils / HashRing.php
1 <?php
2 /**
3 * Convenience class for weighted consistent hash rings.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Convenience class for weighted consistent hash rings
26 *
27 * @since 1.22
28 */
29 class HashRing {
30 /** @var Array (location => weight) */
31 protected $sourceMap = array();
32 /** @var Array (location => (start, end)) */
33 protected $ring = array();
34
35 const RING_SIZE = 268435456; // 2^28
36
37 /**
38 * @param array $map (location => weight)
39 */
40 public function __construct( array $map ) {
41 $map = array_filter( $map, function( $w ) { return $w > 0; } );
42 if ( !count( $map ) ) {
43 throw new MWException( "Ring is empty or all weights are zero." );
44 }
45 $this->sourceMap = $map;
46 // Sort the locations based on the hash of their names
47 $hashes = array();
48 foreach ( $map as $location => $weight ) {
49 $hashes[$location] = sha1( $location );
50 }
51 uksort( $map, function ( $a, $b ) use ( $hashes ) {
52 return strcmp( $hashes[$a], $hashes[$b] );
53 } );
54 // Fit the map to weight-proportionate one with a space of size RING_SIZE
55 $sum = array_sum( $map );
56 $standardMap = array();
57 foreach ( $map as $location => $weight ) {
58 $standardMap[$location] = (int)floor( $weight / $sum * self::RING_SIZE );
59 }
60 // Build a ring of RING_SIZE spots, with each location at a spot in location hash order
61 $index = 0;
62 foreach ( $standardMap as $location => $weight ) {
63 // Location covers half-closed interval [$index,$index + $weight)
64 $this->ring[$location] = array( $index, $index + $weight );
65 $index += $weight;
66 }
67 // Make sure the last location covers what is left
68 end( $this->ring );
69 $this->ring[key( $this->ring )][1] = self::RING_SIZE;
70 }
71
72 /**
73 * Get the location of an item on the ring
74 *
75 * @param string $item
76 * @return string Location
77 */
78 public function getLocation( $item ) {
79 $locations = $this->getLocations( $item, 1 );
80 return $locations[0];
81 }
82
83 /**
84 * Get the location of an item on the ring, as well as the next clockwise locations
85 *
86 * @param string $item
87 * @param integer $limit Maximum number of locations to return
88 * @return array List of locations
89 */
90 public function getLocations( $item, $limit ) {
91 $locations = array();
92 $primaryLocation = null;
93 $spot = hexdec( substr( sha1( $item ), 0, 7 ) ); // first 28 bits
94 foreach ( $this->ring as $location => $range ) {
95 if ( count( $locations ) >= $limit ) {
96 break;
97 }
98 // The $primaryLocation is the location the item spot is in.
99 // After that is reached, keep appending the next locations.
100 if ( ( $range[0] <= $spot && $spot < $range[1] ) || $primaryLocation !== null ) {
101 if ( $primaryLocation === null ) {
102 $primaryLocation = $location;
103 }
104 $locations[] = $location;
105 }
106 }
107 // If more locations are requested, wrap-around and keep adding them
108 reset( $this->ring );
109 while ( count( $locations ) < $limit ) {
110 list( $location, ) = each( $this->ring );
111 if ( $location === $primaryLocation ) {
112 break; // don't go in circles
113 }
114 $locations[] = $location;
115 }
116 return $locations;
117 }
118
119 /**
120 * Get the map of locations to weight (ignores 0-weight items)
121 *
122 * @return array
123 */
124 public function getLocationWeights() {
125 return $this->sourceMap;
126 }
127
128 /**
129 * Get a new hash ring with a location removed from the ring
130 *
131 * @param string $location
132 * @return HashRing|bool Returns false if no non-zero weighted spots are left
133 */
134 public function newWithoutLocation( $location ) {
135 $map = $this->sourceMap;
136 unset( $map[$location] );
137 if ( count( $map ) ) {
138 return new self( $map );
139 }
140 return false;
141 }
142 }