diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java index 7010fc2..ee34b03 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java @@ -1008,7 +1008,7 @@ class ConnectionManager { public List locateRegions(final TableName tableName, final boolean useCache, final boolean offlined) throws IOException { NavigableMap regions = MetaScanner.allTableRegions(conf, this, - tableName, offlined); + tableName); final List locations = new ArrayList(); for (HRegionInfo regionInfo : regions.keySet()) { RegionLocations list = locateRegion(tableName, regionInfo.getStartKey(), useCache, true); diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java index 0143b7e..fafa604 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.NavigableMap; import java.util.TreeMap; import java.util.concurrent.Callable; @@ -606,12 +607,29 @@ public class HTable implements HTableInterface, RegionLocator { * This is mainly useful for the MapReduce integration. * @return A map of HRegionInfo with it's server address * @throws IOException if a remote or network exception occurs - * @deprecated This is no longer a public API + * @deprecated This is no longer a public API. Use {@link #getAllRegionLocations()} instead. */ @Deprecated public NavigableMap getRegionLocations() throws IOException { // TODO: Odd that this returns a Map of HRI to SN whereas getRegionLocator, singular, returns an HRegionLocation. - return MetaScanner.allTableRegions(getConfiguration(), this.connection, getName(), false); + return MetaScanner.allTableRegions(getConfiguration(), this.connection, getName()); + } + + /** + * Gets all the regions and their address for this table. + *

+ * This is mainly useful for the MapReduce integration. + * @return A map of HRegionInfo with it's server address + * @throws IOException if a remote or network exception occurs + */ + @Override + public List getAllRegionLocations() throws IOException { + NavigableMap locations = getRegionLocations(); + ArrayList regions = new ArrayList<>(locations.size()); + for (Entry entry : locations.entrySet()) { + regions.add(new HRegionLocation(entry.getKey(), entry.getValue())); + } + return regions; } /** diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java index a488573..ec85e34 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java @@ -132,8 +132,10 @@ public class MetaScanner { final byte[] row, final int rowLimit, final TableName metaTableName) throws IOException { + boolean closeConnection = false; if (connection == null){ connection = ConnectionManager.getConnectionInternal(configuration); + closeConnection = true; } int rowUpperLimit = rowLimit > 0 ? rowLimit: Integer.MAX_VALUE; @@ -217,7 +219,9 @@ public class MetaScanner { LOG.debug("Got exception in closing meta table", t); } } - + if (closeConnection) { + connection.close(); + } } } @@ -276,10 +280,24 @@ public class MetaScanner { * leave out offlined regions from returned list. * @return Map of all user-space regions to servers * @throws IOException + * @deprecated Use {@link #allTableRegions(Configuration, Connection, TableName)} instead */ + @Deprecated public static NavigableMap allTableRegions(Configuration conf, - Connection connection, final TableName tableName, - final boolean offlined) throws IOException { + Connection connection, final TableName tableName, boolean offlined) throws IOException { + return allTableRegions(conf, connection, tableName); + } + + /** + * Lists all of the table regions currently in META. + * @param conf + * @param offlined True if we are to include offlined regions, false and we'll + * leave out offlined regions from returned list. + * @return Map of all user-space regions to servers + * @throws IOException + */ + public static NavigableMap allTableRegions(Configuration conf, + Connection connection, final TableName tableName) throws IOException { final NavigableMap regions = new TreeMap(); MetaScannerVisitor visitor = new TableMetaScannerVisitor(tableName) { diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java index 505e841..2d56c0e 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java @@ -26,6 +26,7 @@ import org.apache.hadoop.hbase.util.Pair; import java.io.Closeable; import java.io.IOException; +import java.util.List; /** * Used to view region location information for a single HBase table. @@ -55,6 +56,14 @@ public interface RegionLocator extends Closeable { throws IOException; /** + * Retrieves all of the regions associated with this table. + * @return a {@link List} of all regions associated with this table. + * @throws IOException if a remote or network exception occurs + */ + public List getAllRegionLocations() + throws IOException; + + /** * Gets the starting row key for every region in the currently open table. *

* This is mainly useful for the MapReduce integration. diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/MultiTableInputFormatBase.java hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/MultiTableInputFormatBase.java index 1bae235..6a66d37 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/MultiTableInputFormatBase.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/MultiTableInputFormatBase.java @@ -157,7 +157,8 @@ public abstract class MultiTableInputFormatBase extends byte[] startRow = scan.getStartRow(); byte[] stopRow = scan.getStopRow(); - RegionSizeCalculator sizeCalculator = new RegionSizeCalculator((HTable) table); + RegionSizeCalculator sizeCalculator = new RegionSizeCalculator( + regionLocator, conn.getAdmin()); for (int i = 0; i < keys.getFirst().length; i++) { if (!includeRegionInSplit(keys.getFirst()[i], keys.getSecond()[i])) { @@ -184,7 +185,7 @@ public abstract class MultiTableInputFormatBase extends long regionSize = sizeCalculator.getRegionSize(regionInfo.getRegionName()); TableSplit split = - new TableSplit(table.getName(), + new TableSplit(regionLocator.getName(), scan, splitStart, splitStop, regionHostname, regionSize); splits.add(split); diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormat.java hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormat.java index c7fa29e..448c48e 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormat.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormat.java @@ -22,12 +22,14 @@ import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.classification.InterfaceAudience; -import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.conf.Configurable; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; @@ -98,8 +100,11 @@ implements Configurable { public void setConf(Configuration configuration) { this.conf = configuration; TableName tableName = TableName.valueOf(conf.get(INPUT_TABLE)); + Connection connection = null; try { - setHTable(new HTable(new Configuration(conf), tableName)); + // TODO: Close the Connection somewhere. + connection = ConnectionFactory.createConnection(new Configuration(conf)); + initializeTable(connection, tableName); } catch (Exception e) { LOG.error(StringUtils.stringifyException(e)); } diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormatBase.java hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormatBase.java index c196eed..81f2184 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormatBase.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableInputFormatBase.java @@ -35,6 +35,7 @@ import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.RegionLocator; @@ -99,6 +100,8 @@ extends InputFormat { private Table table; /** The {@link RegionLocator} of the table. */ private RegionLocator regionLocator; + /** The {@link Admin}. */ + private Admin admin; /** The reader scanning the table, can be a custom one. */ private TableRecordReader tableRecordReader = null; @@ -159,7 +162,7 @@ extends InputFormat { throw new IOException("No table was provided."); } - RegionSizeCalculator sizeCalculator = new RegionSizeCalculator((HTable) table); + RegionSizeCalculator sizeCalculator = new RegionSizeCalculator(regionLocator, admin); Pair keys = regionLocator.getStartEndKeys(); if (keys == null || keys.getFirst() == null || @@ -283,12 +286,27 @@ extends InputFormat { * Allows subclasses to set the {@link HTable}. * * @param table The table to get the data from. + * @throws IOExceptfion * @deprecated Use {@link #initializeTable(Connection, TableName)} instead. */ @Deprecated - protected void setHTable(HTable table) { + protected void setHTable(HTable table) throws IOException { this.table = table; this.regionLocator = table; + this.admin = table.getConnection().getAdmin(); + } + + /** + * Allows subclasses to initalize the table information. + * + * @param connection The {@link Connection} to the HBase cluster. + * @param tableName The {@link TableName} of the table to process. + * @throws IOExceptfion + */ + protected void initializeTable(Connection connection, TableName tableName) throws IOException { + this.table = connection.getTable(tableName); + this.regionLocator = connection.getRegionLocator(tableName); + this.admin = connection.getAdmin(); } /** diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java index ddc2f56..1ecb7c6 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java @@ -77,7 +77,7 @@ public class RegionsResource extends ResourceBase { TableName tableName = TableName.valueOf(tableResource.getName()); TableInfoModel model = new TableInfoModel(tableName.getNameAsString()); Map regions = MetaScanner.allTableRegions( - servlet.getConfiguration(), null, tableName, false); + servlet.getConfiguration(), null, tableName); for (Map.Entry e: regions.entrySet()) { HRegionInfo hri = e.getKey(); ServerName addr = e.getValue(); diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionSizeCalculator.java hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionSizeCalculator.java index f9c4c92..92c4410 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionSizeCalculator.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionSizeCalculator.java @@ -17,28 +17,30 @@ */ package org.apache.hadoop.hbase.util; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.classification.InterfaceAudience; -import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.ClusterStatus; -import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.RegionLoad; import org.apache.hadoop.hbase.ServerLoad; import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; +import org.apache.hadoop.hbase.client.RegionLocator; /** * Computes size of each region for given table and given column families. @@ -59,57 +61,65 @@ public class RegionSizeCalculator { /** * Computes size of each region for table and given column families. - * */ + * + * @deprecated Use {@link #RegionSizeCalculator(RegionLocator, Admin)} instead. + */ + @Deprecated public RegionSizeCalculator(HTable table) throws IOException { - this(table, new HBaseAdmin(table.getConfiguration())); + HBaseAdmin admin = new HBaseAdmin(table.getConfiguration()); + try { + init(table, admin); + } finally { + admin.close(); + } } - /** ctor for unit testing */ - RegionSizeCalculator (HTable table, Admin admin) throws IOException { + /** + * Computes size of each region for table and given column families. + * */ + public RegionSizeCalculator(RegionLocator regionLocator, Admin admin) throws IOException { + init(regionLocator, admin); + } - try { - if (!enabled(table.getConfiguration())) { - LOG.info("Region size calculation disabled."); - return; - } + private void init(RegionLocator regionLocator, Admin admin) + throws IOException { + if (!enabled(admin.getConfiguration())) { + LOG.info("Region size calculation disabled."); + return; + } - LOG.info("Calculating region sizes for table \"" + new String(table.getTableName()) + "\"."); + LOG.info("Calculating region sizes for table \"" + regionLocator.getName() + "\"."); - //get regions for table - Set tableRegionInfos = table.getRegionLocations().keySet(); - Set tableRegions = new TreeSet(Bytes.BYTES_COMPARATOR); - for (HRegionInfo regionInfo : tableRegionInfos) { - tableRegions.add(regionInfo.getRegionName()); - } + //get regions for table + List tableRegionInfos = regionLocator.getAllRegionLocations(); + Set tableRegions = new TreeSet(Bytes.BYTES_COMPARATOR); + for (HRegionLocation regionInfo : tableRegionInfos) { + tableRegions.add(regionInfo.getRegionInfo().getRegionName()); + } - ClusterStatus clusterStatus = admin.getClusterStatus(); - Collection servers = clusterStatus.getServers(); - final long megaByte = 1024L * 1024L; + ClusterStatus clusterStatus = admin.getClusterStatus(); + Collection servers = clusterStatus.getServers(); + final long megaByte = 1024L * 1024L; - //iterate all cluster regions, filter regions from our table and compute their size - for (ServerName serverName: servers) { - ServerLoad serverLoad = clusterStatus.getLoad(serverName); + //iterate all cluster regions, filter regions from our table and compute their size + for (ServerName serverName: servers) { + ServerLoad serverLoad = clusterStatus.getLoad(serverName); - for (RegionLoad regionLoad: serverLoad.getRegionsLoad().values()) { - byte[] regionId = regionLoad.getName(); + for (RegionLoad regionLoad: serverLoad.getRegionsLoad().values()) { + byte[] regionId = regionLoad.getName(); - if (tableRegions.contains(regionId)) { + if (tableRegions.contains(regionId)) { - long regionSizeBytes = regionLoad.getStorefileSizeMB() * megaByte; - sizeMap.put(regionId, regionSizeBytes); + long regionSizeBytes = regionLoad.getStorefileSizeMB() * megaByte; + sizeMap.put(regionId, regionSizeBytes); - if (LOG.isDebugEnabled()) { - LOG.debug("Region " + regionLoad.getNameAsString() + " has size " + regionSizeBytes); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Region " + regionLoad.getNameAsString() + " has size " + regionSizeBytes); } } } - LOG.debug("Region sizes calculated"); - - } finally { - admin.close(); } - + LOG.debug("Region sizes calculated"); } boolean enabled(Configuration configuration) { diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/TestMultiVersions.java hbase-server/src/test/java/org/apache/hadoop/hbase/TestMultiVersions.java index 2a9b953..3491d72 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/TestMultiVersions.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/TestMultiVersions.java @@ -212,8 +212,7 @@ public class TestMultiVersions { NavigableMap locations = table.getRegionLocations(); assertEquals(2, locations.size()); int index = 0; - for (Map.Entry e: locations.entrySet()) { - HRegionInfo hri = e.getKey(); + for (HRegionInfo hri: locations.keySet()) { if (index == 0) { assertTrue(Bytes.equals(HConstants.EMPTY_START_ROW, hri.getStartKey())); assertTrue(Bytes.equals(hri.getEndKey(), splitRows[0])); diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java index f0bc1ec..91028a9 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java @@ -190,7 +190,7 @@ public class TestMetaScanner { while(!isStopped()) { try { NavigableMap regions = - MetaScanner.allTableRegions(TEST_UTIL.getConfiguration(), null, TABLENAME, false); + MetaScanner.allTableRegions(TEST_UTIL.getConfiguration(), null, TABLENAME); LOG.info("-------"); byte[] lastEndKey = HConstants.EMPTY_START_ROW; diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java index 67a8f19..df56e5a 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java @@ -237,7 +237,7 @@ public class TestEndToEndSplitTransaction { Random random = new Random(); for (int i=0; i< 5; i++) { NavigableMap regions = MetaScanner.allTableRegions(conf, null, - tableName, false); + tableName); if (regions.size() == 0) { continue; } @@ -310,7 +310,7 @@ public class TestEndToEndSplitTransaction { //MetaScanner.allTableRegions() NavigableMap regions = MetaScanner.allTableRegions(conf, null, - tableName, false); + tableName); verifyTableRegions(regions.keySet()); //MetaScanner.listAllRegions() diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestHBaseFsck.java hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestHBaseFsck.java index 70b53c5..76ddd9b 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestHBaseFsck.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestHBaseFsck.java @@ -1264,7 +1264,7 @@ public class TestHBaseFsck { setupTableWithRegionReplica(table, 2); assertEquals(ROWKEYS.length, countRows()); NavigableMap map = MetaScanner.allTableRegions(conf, null, - tbl.getName(), false); + tbl.getName()); int i = 0; // store the HRIs of the regions we will mess up for (Map.Entry m : map.entrySet()) { @@ -1296,7 +1296,7 @@ public class TestHBaseFsck { i = 0; HRegionInfo[] newHris = new HRegionInfo[2]; // get all table's regions from meta - map = MetaScanner.allTableRegions(conf, null, tbl.getName(), false); + map = MetaScanner.allTableRegions(conf, null, tbl.getName()); // get the HRIs of the new regions (hbck created new regions for fixing the hdfs mess-up) for (Map.Entry m : map.entrySet()) { if (m.getKey().getStartKey().length > 0 && diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionSizeCalculator.java hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionSizeCalculator.java index a8e0a55..8b74112 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionSizeCalculator.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionSizeCalculator.java @@ -20,14 +20,15 @@ package org.apache.hadoop.hbase.util; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.ClusterStatus; import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.RegionLoad; import org.apache.hadoop.hbase.ServerLoad; import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.SmallTests; -import org.apache.hadoop.hbase.client.HBaseAdmin; -import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.client.RegionLocator; import org.junit.Test; import org.junit.experimental.categories.Category; import org.mockito.Mockito; @@ -36,7 +37,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.NavigableMap; import java.util.TreeMap; import static org.junit.Assert.assertEquals; @@ -52,7 +52,7 @@ public class TestRegionSizeCalculator { @Test public void testSimpleTestCase() throws Exception { - HTable table = mockTable("region1", "region2", "region3"); + RegionLocator regionLocator = mockRegionLocator("region1", "region2", "region3"); Admin admin = mockAdmin( mockServer( @@ -65,7 +65,7 @@ public class TestRegionSizeCalculator { ) ); - RegionSizeCalculator calculator = new RegionSizeCalculator(table, admin); + RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin); assertEquals(123 * megabyte, calculator.getRegionSize("region1".getBytes())); assertEquals(54321 * megabyte, calculator.getRegionSize("region2".getBytes())); @@ -84,7 +84,7 @@ public class TestRegionSizeCalculator { @Test public void testLargeRegion() throws Exception { - HTable table = mockTable("largeRegion"); + RegionLocator regionLocator = mockRegionLocator("largeRegion"); Admin admin = mockAdmin( mockServer( @@ -92,7 +92,7 @@ public class TestRegionSizeCalculator { ) ); - RegionSizeCalculator calculator = new RegionSizeCalculator(table, admin); + RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin); assertEquals(((long) Integer.MAX_VALUE) * megabyte, calculator.getRegionSize("largeRegion".getBytes())); } @@ -101,7 +101,7 @@ public class TestRegionSizeCalculator { @Test public void testDisabled() throws Exception { String regionName = "cz.goout:/index.html"; - HTable table = mockTable(regionName); + RegionLocator table = mockRegionLocator(regionName); Admin admin = mockAdmin( mockServer( @@ -124,29 +124,29 @@ public class TestRegionSizeCalculator { /** * Makes some table with given region names. * */ - private HTable mockTable(String... regionNames) throws IOException { - HTable mockedTable = Mockito.mock(HTable.class); - when(mockedTable.getConfiguration()).thenReturn(configuration); - when(mockedTable.getTableName()).thenReturn("sizeTestTable".getBytes()); - NavigableMap regionLocations = new TreeMap(); - when(mockedTable.getRegionLocations()).thenReturn(regionLocations); + private RegionLocator mockRegionLocator(String... regionNames) throws IOException { + RegionLocator mockedTable = Mockito.mock(RegionLocator.class); + when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable")); + List regionLocations = new ArrayList<>(); + when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations); for (String regionName : regionNames) { HRegionInfo info = Mockito.mock(HRegionInfo.class); when(info.getRegionName()).thenReturn(regionName.getBytes()); - regionLocations.put(info, null);//we are not interested in values + regionLocations.add(new HRegionLocation(info, null));//we are not interested in values } return mockedTable; } /** - * Creates mock returing ClusterStatus info about given servers. + * Creates mock returning ClusterStatus info about given servers. */ private Admin mockAdmin(ServerLoad... servers) throws Exception { //get clusterstatus - Admin mockAdmin = Mockito.mock(HBaseAdmin.class); + Admin mockAdmin = Mockito.mock(Admin.class); ClusterStatus clusterStatus = mockCluster(servers); + when(mockAdmin.getConfiguration()).thenReturn(configuration); when(mockAdmin.getClusterStatus()).thenReturn(clusterStatus); return mockAdmin; }