HBASE-25662 Fix spotbugs warning in RoundRobinTableInputFormat (#3050)

This commit is contained in:
Duo Zhang 2021-03-15 23:13:23 +08:00 committed by GitHub
parent aeec8ca64b
commit 8337fb2a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 24 deletions

View File

@ -19,11 +19,11 @@ package org.apache.hadoop.hbase.mapreduce;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
@ -88,11 +88,11 @@ public class RoundRobinTableInputFormat extends TableInputFormat {
List<InputSplit> result = new ArrayList<>(inputs.size()); List<InputSplit> result = new ArrayList<>(inputs.size());
// Prepare a hashmap with each region server as key and list of Input Splits as value // Prepare a hashmap with each region server as key and list of Input Splits as value
Map<String, List<InputSplit>> regionServerSplits = new HashMap<>(); Map<String, List<InputSplit>> regionServerSplits = new HashMap<>();
for (InputSplit is: inputs) { for (InputSplit is : inputs) {
if (is instanceof TableSplit) { if (is instanceof TableSplit) {
String regionServer = ((TableSplit)is).getRegionLocation(); String regionServer = ((TableSplit) is).getRegionLocation();
if (regionServer != null && !regionServer.isEmpty()) { if (regionServer != null && !StringUtils.isBlank(regionServer)) {
regionServerSplits.computeIfAbsent(regionServer, k -> new LinkedList<>()).add(is); regionServerSplits.computeIfAbsent(regionServer, k -> new ArrayList<>()).add(is);
continue; continue;
} }
} }
@ -101,15 +101,14 @@ public class RoundRobinTableInputFormat extends TableInputFormat {
} }
// Write out splits in a manner that spreads splits for a RegionServer to avoid 'clumping'. // Write out splits in a manner that spreads splits for a RegionServer to avoid 'clumping'.
while (!regionServerSplits.isEmpty()) { while (!regionServerSplits.isEmpty()) {
Iterator<String> iterator = regionServerSplits.keySet().iterator(); Iterator<List<InputSplit>> iter = regionServerSplits.values().iterator();
while (iterator.hasNext()) { while (iter.hasNext()) {
String regionServer = iterator.next(); List<InputSplit> inputSplitListForRegion = iter.next();
List<InputSplit> inputSplitListForRegion = regionServerSplits.get(regionServer);
if (!inputSplitListForRegion.isEmpty()) { if (!inputSplitListForRegion.isEmpty()) {
result.add(inputSplitListForRegion.remove(0)); result.add(inputSplitListForRegion.remove(0));
} }
if (inputSplitListForRegion.isEmpty()) { if (inputSplitListForRegion.isEmpty()) {
iterator.remove(); iter.remove();
} }
} }
} }

View File

@ -103,32 +103,32 @@ public class TestTableSplit {
new TableSplit(TableName.valueOf(name.getMethodName()), Bytes.toBytes("row-start"), new TableSplit(TableName.valueOf(name.getMethodName()), Bytes.toBytes("row-start"),
Bytes.toBytes("row-end"), "location"); Bytes.toBytes("row-end"), "location");
String str = String str =
"HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, " "Split(tablename=" + name.getMethodName() + ", startrow=row-start, "
+ "end row: row-end, region location: location, " + "endrow=row-end, regionLocation=location, "
+ "encoded region name: )"; + "regionname=)";
Assert.assertEquals(str, split.toString()); Assert.assertEquals(str, split.toString());
split = split =
new TableSplit(TableName.valueOf(name.getMethodName()), null, Bytes.toBytes("row-start"), new TableSplit(TableName.valueOf(name.getMethodName()), null, Bytes.toBytes("row-start"),
Bytes.toBytes("row-end"), "location", "encoded-region-name", 1000L); Bytes.toBytes("row-end"), "location", "encoded-region-name", 1000L);
str = str =
"HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, " "Split(tablename=" + name.getMethodName() + ", startrow=row-start, "
+ "end row: row-end, region location: location, " + "endrow=row-end, regionLocation=location, "
+ "encoded region name: encoded-region-name)"; + "regionname=encoded-region-name)";
Assert.assertEquals(str, split.toString()); Assert.assertEquals(str, split.toString());
split = new TableSplit(null, null, null, null); split = new TableSplit(null, null, null, null);
str = str =
"HBase table split(table name: null, scan: , start row: null, " "Split(tablename=null, startrow=null, "
+ "end row: null, region location: null, " + "endrow=null, regionLocation=null, "
+ "encoded region name: )"; + "regionname=)";
Assert.assertEquals(str, split.toString()); Assert.assertEquals(str, split.toString());
split = new TableSplit(null, null, null, null, null, null, 1000L); split = new TableSplit(null, null, null, null, null, null, 1000L);
str = str =
"HBase table split(table name: null, scan: , start row: null, " "Split(tablename=null, startrow=null, "
+ "end row: null, region location: null, " + "endrow=null, regionLocation=null, "
+ "encoded region name: null)"; + "regionname=null)";
Assert.assertEquals(str, split.toString()); Assert.assertEquals(str, split.toString());
} }
} }