Debian Codegraph @

tests/phpunit/unit/includes/libs/HashRingTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests;
4
5 use HashRing;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @group HashRing
12 * @covers \HashRing
13 */
14 class HashRingTest extends TestCase {
15
16 use MediaWikiCoversValidator;
17
18 public function testHashRingSerialize() {
19 $map = [ 's1' => 3, 's2' => 10, 's3' => 2, 's4' => 10, 's5' => 2, 's6' => 3 ];
20 $ring = new HashRing( $map, 'md5' );
21
22 $serialized = serialize( $ring );
23 $ringRemade = unserialize( $serialized );
24
25 for ( $i = 0; $i < 100; $i++ ) {
26 $this->assertEquals(
27 $ring->getLocation( "hello$i" ),
28 $ringRemade->getLocation( "hello$i" ),
29 'Items placed at proper locations'
30 );
31 }
32 }
33
34 public function testHashRingSingleLocation() {
35 // SHA-1 based and weighted
36 $ring = new HashRing( [ 's1' => 1 ], 'sha1' );
37
38 $this->assertEquals(
39 [ 's1' => 1 ],
40 $ring->getLocationWeights(),
41 'Normalized location weights'
42 );
43
44 for ( $i = 0; $i < 5; $i++ ) {
45 $this->assertEquals(
46 's1',
47 $ring->getLocation( "hello$i" ),
48 'Items placed at proper locations'
49 );
50 $this->assertEquals(
51 [ 's1' ],
52 $ring->getLocations( "hello$i", 2 ),
53 'Items placed at proper locations'
54 );
55 }
56
57 $this->assertEquals( [], $ring->getLocations( "helloX", 0 ), "Limit of 0" );
58 }
59
60 public function testHashRingMapping() {
61 // SHA-1 based and weighted
62 $ring = new HashRing(
63 [ 's1' => 1, 's2' => 1, 's3' => 2, 's4' => 2, 's5' => 2, 's6' => 3, 's7' => 0 ],
64 'sha1'
65 );
66
67 $this->assertEquals(
68 [ 's1' => 1, 's2' => 1, 's3' => 2, 's4' => 2, 's5' => 2, 's6' => 3 ],
69 $ring->getLocationWeights(),
70 'Normalized location weights'
71 );
72
73 $locations = [];
74 for ( $i = 0; $i < 25; $i++ ) {
75 $locations[ "hello$i"] = $ring->getLocation( "hello$i" );
76 }
77 $expectedLocations = [
78 "hello0" => "s4",
79 "hello1" => "s6",
80 "hello2" => "s3",
81 "hello3" => "s6",
82 "hello4" => "s6",
83 "hello5" => "s4",
84 "hello6" => "s3",
85 "hello7" => "s4",
86 "hello8" => "s3",
87 "hello9" => "s3",
88 "hello10" => "s3",
89 "hello11" => "s5",
90 "hello12" => "s4",
91 "hello13" => "s5",
92 "hello14" => "s2",
93 "hello15" => "s5",
94 "hello16" => "s6",
95 "hello17" => "s5",
96 "hello18" => "s1",
97 "hello19" => "s1",
98 "hello20" => "s6",
99 "hello21" => "s5",
100 "hello22" => "s3",
101 "hello23" => "s4",
102 "hello24" => "s1"
103 ];
104 $this->assertEquals( $expectedLocations, $locations, 'Items placed at proper locations' );
105
106 $locations = [];
107 for ( $i = 0; $i < 5; $i++ ) {
108 $locations[ "hello$i"] = $ring->getLocations( "hello$i", 2 );
109 }
110
111 $expectedLocations = [
112 "hello0" => [ "s4", "s5" ],
113 "hello1" => [ "s6", "s5" ],
114 "hello2" => [ "s3", "s1" ],
115 "hello3" => [ "s6", "s5" ],
116 "hello4" => [ "s6", "s3" ],
117 ];
118 $this->assertEquals( $expectedLocations, $locations, 'Items placed at proper locations' );
119 }
120
121 /**
122 * @dataProvider providor_getHashLocationWeights
123 */
124 public function testHashRingRatios( $locations, $expectedHits ) {
125 $ring = new HashRing( $locations, 'whirlpool' );
126
127 $locationStats = array_fill_keys( array_keys( $locations ), 0 );
128 for ( $i = 0; $i < 10000; ++$i ) {
129 ++$locationStats[$ring->getLocation( "key-$i" )];
130 }
131 $this->assertEquals( $expectedHits, $locationStats );
132 }
133
134 public static function providor_getHashLocationWeights() {
135 return [
136 [
137 [ 'big' => 10, 'medium' => 5, 'small' => 1 ],
138 [ 'big' => 6037, 'medium' => 3314, 'small' => 649 ]
139 ]
140 ];
141 }
142
143 /**
144 * @dataProvider providor_getHashLocationWeights2
145 */
146 public function testHashRingRatios2( $locations, $expected ) {
147 $ring = new HashRing( $locations, 'sha1' );
148 $locationStats = array_fill_keys( array_keys( $locations ), 0 );
149 for ( $i = 0; $i < 1000; ++$i ) {
150 foreach ( $ring->getLocations( "key-$i", 3 ) as $location ) {
151 ++$locationStats[$location];
152 }
153 }
154 $this->assertEquals( $expected, $locationStats );
155 }
156
157 public static function providor_getHashLocationWeights2() {
158 return [
159 [
160 [ 'big1' => 10, 'big2' => 10, 'big3' => 10, 'small1' => 1, 'small2' => 1 ],
161 [ 'big1' => 929, 'big2' => 899, 'big3' => 887, 'small1' => 143, 'small2' => 142 ]
162 ]
163 ];
164 }
165
166 public function testHashRingEjection() {
167 $map = [ 's1' => 5, 's2' => 5, 's3' => 10, 's4' => 10, 's5' => 5, 's6' => 5 ];
168 $ring = new HashRing( $map, 'md5' );
169
170 $ring->ejectFromLiveRing( 's3', 30 );
171 $ring->ejectFromLiveRing( 's6', 15 );
172
173 $this->assertEquals(
174 [ 's1' => 5, 's2' => 5, 's4' => 10, 's5' => 5 ],
175 $ring->getLiveLocationWeights(),
176 'Live location weights'
177 );
178
179 for ( $i = 0; $i < 100; ++$i ) {
180 $key = "key-$i";
181
182 $this->assertNotEquals( 's3', $ring->getLiveLocation( $key ), 'ejected' );
183 $this->assertNotEquals( 's6', $ring->getLiveLocation( $key ), 'ejected' );
184
185 if ( !in_array( $ring->getLocation( $key ), [ 's3', 's6' ], true ) ) {
186 $this->assertEquals(
187 $ring->getLocation( $key ),
188 $ring->getLiveLocation( $key ),
189 "Live ring otherwise matches (#$i)"
190 );
191 $this->assertEquals(
192 $ring->getLocations( $key, 1 ),
193 $ring->getLiveLocations( $key, 1 ),
194 "Live ring otherwise matches (#$i)"
195 );
196 }
197 }
198 }
199
200 public function testHashRingCollision() {
201 $ring1 = new HashRing( [ 0 => 1, 6497 => 1 ] );
202 $ring2 = new HashRing( [ 6497 => 1, 0 => 1 ] );
203
204 for ( $i = 0; $i < 100; ++$i ) {
205 $this->assertEquals( $ring1->getLocation( $i ), $ring2->getLocation( $i ) );
206 }
207 }
208
209 public function testHashRingKetamaMode() {
210 // Same as https://github.com/RJ/ketama/blob/master/ketama.servers
211 $map = [
212 '10.0.1.1:11211' => 600,
213 '10.0.1.2:11211' => 300,
214 '10.0.1.3:11211' => 200,
215 '10.0.1.4:11211' => 350,
216 '10.0.1.5:11211' => 1000,
217 '10.0.1.6:11211' => 800,
218 '10.0.1.7:11211' => 950,
219 '10.0.1.8:11211' => 100
220 ];
221 $ring = new HashRing( $map, 'md5' );
222 $wrapper = TestingAccessWrapper::newFromObject( $ring );
223
224 $ketama_test = static function ( $count ) use ( $wrapper ) {
225 $baseRing = $wrapper->baseRing;
226
227 $lines = [];
228 for ( $key = 0; $key < $count; ++$key ) {
229 $location = $wrapper->getLocation( $key );
230
231 $itemPos = $wrapper->getItemPosition( $key );
232 $nodeIndex = $wrapper->findNodeIndexForPosition( $itemPos, $baseRing );
233 $nodePos = $baseRing[$nodeIndex][HashRing::KEY_POS];
234
235 $lines[] = sprintf( "%u %u %s\n", $itemPos, $nodePos, $location );
236 }
237
238 return "\n" . implode( '', $lines );
239 };
240
241 // Known correct values generated from C code:
242 // https://github.com/RJ/ketama/blob/master/libketama/ketama_test.c
243 $expected = <<<EOT
244
245 2216742351 2217271743 10.0.1.1:11211
246 943901380 949045552 10.0.1.5:11211
247 2373066440 2374693370 10.0.1.6:11211
248 2127088620 2130338203 10.0.1.6:11211
249 2046197672 2051996197 10.0.1.7:11211
250 2134629092 2135172435 10.0.1.1:11211
251 470382870 472541453 10.0.1.7:11211
252 1608782991 1609789509 10.0.1.3:11211
253 2516119753 2520092206 10.0.1.2:11211
254 3465331781 3466294492 10.0.1.4:11211
255 1749342675 1753760600 10.0.1.5:11211
256 1136464485 1137779711 10.0.1.1:11211
257 3620997826 3621580689 10.0.1.7:11211
258 283385029 285581365 10.0.1.6:11211
259 2300818346 2302165654 10.0.1.5:11211
260 2132603803 2134614475 10.0.1.8:11211
261 2962705863 2969767984 10.0.1.2:11211
262 786427760 786565633 10.0.1.5:11211
263 4095887727 4096760944 10.0.1.6:11211
264 2906459679 2906987515 10.0.1.6:11211
265 137884056 138922607 10.0.1.4:11211
266 81549628 82491298 10.0.1.6:11211
267 3530020790 3530525869 10.0.1.6:11211
268 4231817527 4234960467 10.0.1.7:11211
269 2011099423 2014738083 10.0.1.7:11211
270 107620750 120968799 10.0.1.6:11211
271 3979113294 3981926993 10.0.1.4:11211
272 273671938 276355738 10.0.1.4:11211
273 4032816947 4033300359 10.0.1.5:11211
274 464234862 466093615 10.0.1.1:11211
275 3007059764 3007671127 10.0.1.5:11211
276 542337729 542491760 10.0.1.7:11211
277 4040385635 4044064727 10.0.1.5:11211
278 3319802648 3320661601 10.0.1.7:11211
279 1032153571 1035085391 10.0.1.1:11211
280 3543939100 3545608820 10.0.1.5:11211
281 3876899353 3885324049 10.0.1.2:11211
282 3771318181 3773259708 10.0.1.8:11211
283 3457906597 3459285639 10.0.1.5:11211
284 3028975062 3031083168 10.0.1.7:11211
285 244467158 250943416 10.0.1.5:11211
286 1604785716 1609789509 10.0.1.3:11211
287 3905343649 3905751132 10.0.1.1:11211
288 1713497623 1725056963 10.0.1.5:11211
289 1668356087 1668827816 10.0.1.5:11211
290 3427369836 3438933308 10.0.1.1:11211
291 2515850457 2520092206 10.0.1.2:11211
292 3886138983 3887390208 10.0.1.1:11211
293 4019334756 4023153300 10.0.1.8:11211
294 1170561012 1170785765 10.0.1.7:11211
295 1841809344 1848425105 10.0.1.6:11211
296 973223976 973369204 10.0.1.1:11211
297 358093210 359562433 10.0.1.6:11211
298 378350808 380841931 10.0.1.5:11211
299 4008477862 4012085095 10.0.1.7:11211
300 1027226549 1028630030 10.0.1.6:11211
301 2386583967 2387706118 10.0.1.1:11211
302 522892146 524831677 10.0.1.7:11211
303 3779194982 3788912803 10.0.1.5:11211
304 3764731657 3771312500 10.0.1.7:11211
305 184756999 187529415 10.0.1.6:11211
306 838351231 845886003 10.0.1.3:11211
307 2827220548 2828019973 10.0.1.6:11211
308 3604721411 3607668249 10.0.1.6:11211
309 472866282 475506254 10.0.1.5:11211
310 2752268796 2754833471 10.0.1.5:11211
311 1791464754 1795042583 10.0.1.7:11211
312 3029359475 3031083168 10.0.1.7:11211
313 3633378211 3639985542 10.0.1.6:11211
314 3148267284 3149217023 10.0.1.6:11211
315 163887996 166705043 10.0.1.7:11211
316 3642803426 3649125922 10.0.1.7:11211
317 3901799218 3902199881 10.0.1.7:11211
318 418045394 425867331 10.0.1.6:11211
319 346775981 348578169 10.0.1.6:11211
320 368352208 372224616 10.0.1.7:11211
321 2643711995 2644259911 10.0.1.5:11211
322 2032983336 2033860601 10.0.1.6:11211
323 3567842357 3572867530 10.0.1.2:11211
324 1024982737 1028630030 10.0.1.6:11211
325 933966832 938106828 10.0.1.7:11211
326 2102520899 2103402846 10.0.1.7:11211
327 3537205399 3538094881 10.0.1.7:11211
328 2311233534 2314593262 10.0.1.1:11211
329 2500514664 2503565236 10.0.1.7:11211
330 1091958846 1093484995 10.0.1.6:11211
331 3984972691 3987453644 10.0.1.1:11211
332 2669994439 2670911201 10.0.1.4:11211
333 2846111786 2846115813 10.0.1.5:11211
334 1805010806 1808593732 10.0.1.8:11211
335 1587024774 1587746378 10.0.1.5:11211
336 3214549588 3215619351 10.0.1.2:11211
337 1965214866 1970922428 10.0.1.7:11211
338 1038671000 1040777775 10.0.1.7:11211
339 820820468 823114475 10.0.1.6:11211
340 2722835329 2723166435 10.0.1.5:11211
341 1602053414 1604196066 10.0.1.5:11211
342 1330835426 1335097278 10.0.1.5:11211
343 556547565 557075710 10.0.1.4:11211
344 2977587884 2978402952 10.0.1.1:11211
345
346 EOT;
347
348 $this->assertEquals( $expected, $ketama_test( 100 ), 'Ketama mode (diff check)' );
349
350 // Hash of known correct values from C code
351 $this->assertEquals(
352 'd1a4912a80e4654ec2e4e462c8b911c6',
353 md5( $ketama_test( 1e3 ) ),
354 'Ketama mode (large, MD5 check)'
355 );
356
357 // Slower, full upstream MD5 check, manually verified 3/21/2018
358 // $this->assertEquals( '5672b131391f5aa2b280936aec1eea74', md5( $ketama_test( 1e6 ) ) );
359 }
360 }