21
21
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
22
import static org .junit .jupiter .api .Assertions .assertFalse ;
23
23
import static org .junit .jupiter .api .Assertions .assertNotNull ;
24
- import static org .junit .jupiter .api .Assertions .assertTrue ;
25
24
import static org .junit .jupiter .api .Assertions .fail ;
26
25
27
26
import java .util .ArrayList ;
28
27
import java .util .Collections ;
29
28
import java .util .HashMap ;
30
- import java .util .HashSet ;
31
29
import java .util .List ;
32
30
import java .util .Map ;
33
31
import java .util .Map .Entry ;
@@ -248,32 +246,88 @@ public void testUnevenAssignment2() {
248
246
for (int i = 0 ; i < 10 ; i ++) {
249
247
shortServer .getValue ().tablets .add (makeTablet (table2 , null , null ));
250
248
}
251
- Set <TableId > migratedTables ;
249
+ Map <TableId , Integer > migratedTables ;
252
250
// average is 58, with 2 at 59: we need 48 more moved to the short server
253
- migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ), 48 );
254
- assertEquals (1 , migratedTables .size ());
255
- assertTrue (migratedTables .contains (t1Id ));
251
+ migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ));
252
+ assertEquals (48 , migratedTables .get (t1Id ));
256
253
257
254
// ShortTable tablets are only on the short server: 9 should be migrated off
258
- migratedTables = balanceTables (Map .of (table2 , t2Id ), 9 );
255
+ migratedTables = balanceTables (Map .of (table2 , t2Id ));
259
256
assertEquals (1 , migratedTables .size ());
260
- assertTrue ( migratedTables .contains (t2Id ));
257
+ assertEquals ( 9 , migratedTables .get (t2Id ));
261
258
262
259
// ShortServer now has 9 less LongTable tablets: 9 LongTable tablets should migrate back
263
- migratedTables = balanceTables (Map .of (table1 , t1Id ), 9 );
260
+ migratedTables = balanceTables (Map .of (table1 , t1Id ));
264
261
assertEquals (1 , migratedTables .size ());
265
- assertTrue ( migratedTables .contains (t1Id ));
262
+ assertEquals ( 9 , migratedTables .get (t1Id ));
266
263
267
264
// Servers are now fully balanced. Attempt to balance both tables again
268
- migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ), 0 );
265
+ migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ));
266
+ assertEquals (0 , migratedTables .size (), "Migrated Tables: " + migratedTables );
267
+
268
+ }
269
+
270
+ @ Test
271
+ public void testBalanceSubset () {
272
+ // make 26 servers
273
+ for (char c : "abcdefghijklmnopqrstuvwxyz" .toCharArray ()) {
274
+ TabletServerId tsid = new TabletServerIdImpl ("127.0.0.1" , c , Character .toString (c ));
275
+ FakeTServer fakeTServer = new FakeTServer ();
276
+ servers .put (tsid , fakeTServer );
277
+ }
278
+ String table1 = "Long" ;
279
+ String table2 = "Short" ;
280
+ String table3 = "neverMigrate" ;
281
+
282
+ // Generate Table Ids
283
+ TableId t1Id = TableId .of (table1 );
284
+ TableId t2Id = TableId .of (table2 );
285
+ TableId t3Id = TableId .of (table3 );
286
+
287
+ // put 10 tablets on 8 of the servers that should never migrate
288
+ int randServer = 0 ;
289
+ for (Entry <TabletServerId ,FakeTServer > entry : servers .entrySet ()) {
290
+ randServer ++;
291
+ if (randServer % 3 == 0 ) {
292
+ for (int i = 0 ; i < 10 ; i ++) {
293
+ entry .getValue ().tablets .add (makeTablet (table3 , null , null ));
294
+ }
295
+ }
296
+ }
297
+
298
+ // put 60 tablets on 25 of them
299
+ List <Entry <TabletServerId ,FakeTServer >> shortList = new ArrayList <>(servers .entrySet ());
300
+ Entry <TabletServerId ,FakeTServer > shortServer = shortList .remove (0 );
301
+ for (int i = 0 ; i < 60 ; i ++) {
302
+ for (Entry <TabletServerId ,FakeTServer > entry : shortList ) {
303
+ entry .getValue ().tablets .add (makeTablet (table1 , null , null ));
304
+ }
305
+ }
306
+ // put 10 on the that short server:
307
+ for (int i = 0 ; i < 10 ; i ++) {
308
+ shortServer .getValue ().tablets .add (makeTablet (table2 , null , null ));
309
+ }
310
+
311
+ Map <TableId ,Integer > migratedTables ;
312
+ // average is 58, with 2 at 59: we need 48 more moved to the short server
313
+ migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ));
314
+ assertEquals (1 , migratedTables .size ());
315
+ assertEquals (48 , migratedTables .get (t1Id ));
316
+
317
+ // Servers are now fully balanced. Attempt to balance both tables again
318
+ migratedTables = balanceTables (Map .of (table1 , t1Id , table2 , t2Id ));
269
319
assertEquals (0 , migratedTables .size ());
320
+
321
+ // Only balance the neverMigrate table
322
+ migratedTables = balanceTables (Map .of (table3 , t3Id ));
323
+ assertEquals (1 , migratedTables .size (), "Too many tables migrated: " + migratedTables );
324
+ assertEquals (54 , migratedTables .get (t3Id ));
270
325
}
271
326
272
- private Set <TableId > balanceTables (Map <String ,TableId > tablesToBalance , int expectedMigrations ) {
327
+ private Map <TableId , Integer > balanceTables (Map <String ,TableId > tablesToBalance ) {
273
328
TestSimpleLoadBalancer balancer = new TestSimpleLoadBalancer ();
274
- Set <TableId > migratedTables = new HashSet <>();
329
+ Map <TableId , Integer > migratedTables = new HashMap <>();
275
330
Set <TabletId > migrations = Collections .emptySet ();
276
- int moved = 0 ;
277
331
// balance until we can't balance no more!
278
332
while (true ) {
279
333
List <TabletMigration > migrationsOut = new ArrayList <>();
@@ -284,15 +338,18 @@ private Set<TableId> balanceTables(Map<String,TableId> tablesToBalance, int expe
284
338
}
285
339
for (TabletMigration migration : migrationsOut ) {
286
340
if (servers .get (migration .getOldTabletServer ()).tablets .remove (migration .getTablet ())) {
287
- moved ++;
288
- migratedTables .add (migration .getTablet ().getTable ());
341
+ TableId tableId = migration .getTablet ().getTable ();
342
+ Integer value = 0 ;
343
+ if (migratedTables .containsKey (tableId )) {
344
+ value = migratedTables .get (tableId );
345
+ }
346
+ migratedTables .put (tableId , value + 1 );
289
347
}
290
348
last .remove (migration .getTablet ());
291
349
servers .get (migration .getNewTabletServer ()).tablets .add (migration .getTablet ());
292
350
last .put (migration .getTablet (), migration .getNewTabletServer ());
293
351
}
294
352
}
295
- assertEquals (expectedMigrations , moved );
296
353
return migratedTables ;
297
354
}
298
355
0 commit comments