diff --git a/CHANGES.txt b/CHANGES.txt index 132b8fb5139..4e2ee3d8ca6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -236,6 +236,8 @@ Release 0.91.0 - Unreleased HBASE-3931 Allow adding attributes to Get HBASE-3942 The thrift scannerOpen functions should support row caching (Adam Worthington) + HBASE-2556 Add convenience method to HBaseAdmin to get a collection of + HRegionInfo objects for each table (Ming Ma) TASKS HBASE-3559 Move report of split to master OFF the heartbeat channel diff --git a/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java b/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java index 3c1bcb38540..66a2fd91e27 100644 --- a/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java +++ b/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java @@ -21,8 +21,10 @@ package org.apache.hadoop.hbase.client; import java.io.Closeable; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; +import java.util.ArrayList; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -1240,6 +1242,27 @@ public class HBaseAdmin implements Abortable, Closeable { new HBaseAdmin(copyOfConf); } + /** + * get the regions of a given table. + * + * @param tableName the name of the table + * @return Ordered list of {@link HRegionInfo}. * + * @throws IOException + */ + public List getTableRegions(final byte[] tableName) throws IOException + { + CatalogTracker ct = getCatalogTracker(); + List Regions; + try { + Regions = MetaReader.getTableRegions(ct, tableName, true); + } finally { + cleanupCatalogTracker(ct); + } + + return Regions; + } + + public void close() throws IOException { if (this.connection != null) { this.connection.close(); diff --git a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java index b3eb465d6d0..d551478d607 100644 --- a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java +++ b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java @@ -270,11 +270,10 @@ public class ThriftServer { public List getTableRegions(ByteBuffer tableName) throws IOError { try{ - HTable table = getTable(tableName); - Map regionsInfo = table.getRegionsInfo(); + List HRegions = this.admin.getTableRegions(tableName.array()); List regions = new ArrayList(); - for (HRegionInfo regionInfo : regionsInfo.keySet()){ + for (HRegionInfo regionInfo : HRegions){ TRegionInfo region = new TRegionInfo(); region.startKey = ByteBuffer.wrap(regionInfo.getStartKey()); region.endKey = ByteBuffer.wrap(regionInfo.getEndKey()); diff --git a/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java b/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java index 83878f8655e..a0489e13f8d 100644 --- a/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java +++ b/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java @@ -830,4 +830,35 @@ public class TestAdmin { this.admin.deleteTable(tableName); } } -} \ No newline at end of file + + + /** + * For HBASE-2556 + * @throws IOException + */ + @Test + public void testGetTableRegions() throws IOException { + + byte[] tableName = Bytes.toBytes("testGetTableRegions"); + + int expectedRegions = 10; + + // Use 80 bit numbers to make sure we aren't limited + byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + byte [] endKey = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; + + + HTableDescriptor desc = new HTableDescriptor(tableName); + desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY)); + admin = new HBaseAdmin(TEST_UTIL.getConfiguration()); + admin.createTable(desc, startKey, endKey, expectedRegions); + + List RegionInfos = admin.getTableRegions(tableName); + + assertEquals("Tried to create " + expectedRegions + " regions " + + "but only found " + RegionInfos.size(), + expectedRegions, RegionInfos.size()); + + } + +} diff --git a/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java b/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java index 84ba4c817e6..a38d5fa5013 100644 --- a/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java +++ b/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java @@ -66,6 +66,7 @@ public class TestThriftServer extends HBaseClusterTestCase { doTestTableMutations(); doTestTableTimestampsAndColumns(); doTestTableScanners(); + doTestGetTableRegions(); } /** @@ -98,7 +99,8 @@ public class TestThriftServer extends HBaseClusterTestCase { handler.disableTable(tableAname);*/ handler.deleteTable(tableAname); } - + + /** * Tests adding a series of Mutations and BatchMutations, including a * delete mutation. Also tests data retrieval, and getting back multiple @@ -310,6 +312,27 @@ public class TestThriftServer extends HBaseClusterTestCase { handler.deleteTable(tableAname); } + + /** + * For HBASE-2556 + * Tests for GetTableRegions + * + * @throws Exception + */ + public void doTestGetTableRegions() throws Exception { + ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(this.conf); + + handler.createTable(tableAname, getColumnDescriptors()); + int RegionCount = handler.getTableRegions(tableAname).size(); + assertEquals("empty table should have only 1 region, " + + "but found " + RegionCount, RegionCount, 1); + handler.disableTable(tableAname); + handler.deleteTable(tableAname); + RegionCount = handler.getTableRegions(tableAname).size(); + assertEquals("non-existing table should have 0 region, " + + "but found " + RegionCount, RegionCount, 0); + } + /** * * @return a List of ColumnDescriptors for use in creating a table. Has one