24
24
import java .util .ArrayList ;
25
25
import java .util .Collections ;
26
26
import java .util .HashMap ;
27
+ import java .util .HashSet ;
27
28
import java .util .Iterator ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
@@ -151,6 +152,7 @@ public int compareTo(ServerCounts obj) {
151
152
}
152
153
153
154
public boolean getMigrations (BalanceParameters params ) {
155
+ Set <TableId > tableIdsToBalance = new HashSet <>(params .getTablesToBalance ().values ());
154
156
List <TabletMigration > resultingMigrations = new ArrayList <>();
155
157
boolean moreBalancingNeeded = false ;
156
158
try {
@@ -171,10 +173,8 @@ public boolean getMigrations(BalanceParameters params) {
171
173
* The check below was on entry.getKey(), but that resolves to a tabletserver not a
172
174
* tablename. Believe it should be e.getKey() which is a tablename
173
175
*/
174
- for (TableId tableId : params .getTablesToBalance ().values ()) {
175
- if (tableId .canonical ().equals (e .getKey ())) {
176
- serverTotal += e .getValue ().getOnlineTabletCount ();
177
- }
176
+ if (tableIdsToBalance .contains (TableId .of (e .getKey ()))) {
177
+ serverTotal += e .getValue ().getOnlineTabletCount ();
178
178
}
179
179
}
180
180
}
@@ -209,11 +209,13 @@ public boolean getMigrations(BalanceParameters params) {
209
209
break ;
210
210
}
211
211
if (needToUnload >= needToLoad ) {
212
- resultingMigrations .addAll (move (tooMany , tooLittle , needToLoad , donerTabletStats ));
212
+ resultingMigrations
213
+ .addAll (move (tooMany , tooLittle , needToLoad , donerTabletStats , tableIdsToBalance ));
213
214
end --;
214
215
movedAlready = 0 ;
215
216
} else {
216
- resultingMigrations .addAll (move (tooMany , tooLittle , needToUnload , donerTabletStats ));
217
+ resultingMigrations
218
+ .addAll (move (tooMany , tooLittle , needToUnload , donerTabletStats , tableIdsToBalance ));
217
219
movedAlready += needToUnload ;
218
220
}
219
221
if (needToUnload > needToLoad ) {
@@ -236,16 +238,17 @@ public boolean getMigrations(BalanceParameters params) {
236
238
* busiest table
237
239
*/
238
240
List <TabletMigration > move (ServerCounts tooMuch , ServerCounts tooLittle , int count ,
239
- Map <TableId ,Map <TabletId ,TabletStatistics >> donerTabletStats ) {
241
+ Map <TableId ,Map <TabletId ,TabletStatistics >> donerTabletStats ,
242
+ Set <TableId > tableIdsToBalance ) {
240
243
241
244
if (count == 0 ) {
242
245
return Collections .emptyList ();
243
246
}
244
247
245
248
List <TabletMigration > result = new ArrayList <>();
246
249
// Copy counts so we can update them as we propose migrations
247
- Map <TableId ,Integer > tooMuchMap = tabletCountsPerTable (tooMuch .status );
248
- Map <TableId ,Integer > tooLittleMap = tabletCountsPerTable (tooLittle .status );
250
+ Map <TableId ,Integer > tooMuchMap = tabletCountsPerTable (tooMuch .status , tableIdsToBalance );
251
+ Map <TableId ,Integer > tooLittleMap = tabletCountsPerTable (tooLittle .status , tableIdsToBalance );
249
252
250
253
for (int i = 0 ; i < count ; i ++) {
251
254
TableId table = getTableToMigrate (tooMuch , tooMuchMap , tooLittleMap );
@@ -291,6 +294,10 @@ List<TabletMigration> move(ServerCounts tooMuch, ServerCounts tooLittle, int cou
291
294
private TableId getTableToMigrate (ServerCounts tooMuch , Map <TableId ,Integer > tooMuchMap ,
292
295
Map <TableId ,Integer > tooLittleMap ) {
293
296
297
+ if (tooMuchMap .size () == 1 ) {
298
+ return tooMuchMap .keySet ().iterator ().next ();
299
+ }
300
+
294
301
// find a table to migrate
295
302
// look for an uneven table count
296
303
Entry <TableId ,Integer > biggestEntry = tooMuchMap .entrySet ().stream ().map (entry -> {
@@ -312,12 +319,16 @@ protected List<TabletStatistics> getOnlineTabletsForTable(TabletServerId tabletS
312
319
return environment .listOnlineTabletsForTable (tabletServerId , tableId );
313
320
}
314
321
315
- static Map <TableId ,Integer > tabletCountsPerTable (TServerStatus status ) {
322
+ static Map <TableId ,Integer > tabletCountsPerTable (TServerStatus status ,
323
+ Set <TableId > tableIdsToBalance ) {
316
324
Map <TableId ,Integer > result = new HashMap <>();
317
325
if (status != null && status .getTableMap () != null ) {
318
326
Map <String ,TableStatistics > tableMap = status .getTableMap ();
319
327
for (Entry <String ,TableStatistics > entry : tableMap .entrySet ()) {
320
- result .put (TableId .of (entry .getKey ()), entry .getValue ().getOnlineTabletCount ());
328
+ var tableId = TableId .of (entry .getKey ());
329
+ if (tableIdsToBalance .contains (tableId )) {
330
+ result .put (tableId , entry .getValue ().getOnlineTabletCount ());
331
+ }
321
332
}
322
333
}
323
334
return result ;
0 commit comments