Index: src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (revision 1162672) +++ src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (working copy) @@ -824,6 +824,44 @@ cleanupCatalogTracker(ct); } } + /** + * For expert-admins. Runs close on the regionserver. Closes a region based on + * the encoded region name. The region server name is mandatory. If the + * servername is provided then based on the online regions in the specified + * regionserver the specified region will be closed. The master will not be + * informed of the close. Note that the regionname is the encoded regionname. + * + * @param encodedRegionName + * The encoded region name; i.e. the hash that makes up the region + * name suffix: e.g. if regionname is + * TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396. + * , then the encoded region name is: + * 527db22f95c8a9e0116f0cc13c680396. + * @param serverName + * The servername of the regionserver. A server name is made of host + * and port. This is mandatory. Here is an example: + * host187.example.com:60020 + * @return true if the region was closed, false if not. + * @throws IOException + * if a remote or network exception occurs + */ + public boolean closeRegionWithEncodedRegionName( + final String encodedRegionName, final String serverName) + throws IOException { + byte[] encodedRegionNameInBytes = Bytes.toBytes(encodedRegionName); + if (null == serverName || ("").equals(serverName.trim())) { + throw new IllegalArgumentException( + "The servername cannot be null or empty."); + } + HServerAddress hsa = new HServerAddress(serverName); + HRegionInterface rs = this.connection.getHRegionConnection(hsa, true); + // Close the region without updating zk state. + boolean regionClosed = rs.closeRegion(encodedRegionNameInBytes, false); + if (false == regionClosed) { + LOG.error("Not able to close the region " + encodedRegionName + "."); + } + return regionClosed; + } public void closeRegion(final HServerAddress hsa, final HRegionInfo hri) throws IOException { Index: src/main/java/org/apache/hadoop/hbase/ipc/HRegionInterface.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/ipc/HRegionInterface.java (revision 1162672) +++ src/main/java/org/apache/hadoop/hbase/ipc/HRegionInterface.java (working copy) @@ -343,6 +343,20 @@ public boolean closeRegion(final HRegionInfo region, final boolean zk) throws IOException; + /** + * Closes the region in the RS with the specified encoded regionName and will + * use or not use ZK during the close according to the specified flag. Note + * that the encoded region name is in byte format. + * + * @param encodedRegionName + * in bytes + * @param zk + * true if to use zookeeper, false if need not. + * @return true if region is closed, false if not. + * @throws IOException + */ + public boolean closeRegion(byte[] encodedRegionName, final boolean zk) + throws IOException; // Region administrative methods /** Index: src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (revision 1162672) +++ src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (working copy) @@ -2153,6 +2153,37 @@ } return closeRegion(region, false, zk); } + + @Override + @QosPriority(priority = HIGH_QOS) + public boolean closeRegion(byte[] encodedRegionName, boolean zk) + throws IOException { + return closeRegion(encodedRegionName, false, zk); + } + + /** + * @param encodedRegionName + * encodedregionName to close + * @param abort + * True if we are aborting + * @param zk + * True if we are to update zk about the region close; if the close + * was orchestrated by master, then update zk. If the close is being + * run by the regionserver because its going down, don't update zk. + * @return True if closed a region. + */ + protected boolean closeRegion(byte[] encodedRegionName, final boolean abort, + final boolean zk) throws IOException { + String encodedRegionNameStr = Bytes.toString(encodedRegionName); + HRegion region = this.getFromOnlineRegions(encodedRegionNameStr); + if (null != region) { + return closeRegion(region.getRegionInfo(), abort, zk); + } + LOG + .error("Unable to close the region " + encodedRegionNameStr + + ". The specified region does not exist."); + return false; + } /** * @param region Region to close Index: src/main/ruby/hbase/admin.rb =================================================================== --- src/main/ruby/hbase/admin.rb (revision 1162672) +++ src/main/ruby/hbase/admin.rb (working copy) @@ -159,9 +159,13 @@ end #---------------------------------------------------------------------------------------------- - # Closes a region - def close_region(region_name, server = nil) - @admin.closeRegion(region_name, server) + # Closes a region. + # If server name is nil, we presume region_name is full region name (HRegionInfo.getRegionName). + # If server name is not nil, we presume it is the region's encoded name (HRegionInfo.getEncodedName) + def close_region(region_name, server) + if (server == nil || !closeEncodedRegion?(region_name, server)) + @admin.closeRegion(region_name, server) + end end #---------------------------------------------------------------------------------------------- @@ -340,6 +344,12 @@ end #---------------------------------------------------------------------------------------------- + #Is supplied region name is encoded region name + def closeEncodedRegion?(region_name, server) + @admin.closeRegionWithEncodedRegionName(region_name, server) + end + + #---------------------------------------------------------------------------------------------- # Return a new HColumnDescriptor made of passed args def hcd(arg, htd) # String arg, single parameter constructor Index: src/main/ruby/shell/commands/close_region.rb =================================================================== --- src/main/ruby/shell/commands/close_region.rb (revision 1162672) +++ src/main/ruby/shell/commands/close_region.rb (working copy) @@ -23,13 +23,26 @@ class CloseRegion < Command def help return <<-EOF -Close a single region. Optionally specify regionserver. Connects to the -regionserver and runs close on hosting regionserver. The close is done -without the master's involvement (It will not know of the close). Once -closed, region will stay closed. Use assign to reopen/reassign. Use -unassign or move to assign the region elsewhere on cluster. Use with -caution. For experts only. Examples: +Close a single region. Ask the master to close a region out on the cluster +or if 'SERVER_NAME' is supplied, ask the designated hosting regionserver to +close the region directly. Closing a region, the master expects 'REGIONNAME' +to be a fully qualified region name. When asking the hosting regionserver to +directly close a region, you pass the regions' encoded name only. A region +name looks like this: + + TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396. +The trailing period is part of the regionserver name. A region's encoded name +is the hash at the end of a region name; e.g. 527db22f95c8a9e0116f0cc13c680396 +(without the period). A 'SERVER_NAME' is its host and port. For +example: host187.example.com:60020 (find servername in master ui +or when you do detailed status in shell). This command will end up running +close on the region hosting regionserver. The close is done without the +master's involvement (It will not know of the close). Once closed, region will +stay closed. Use assign to reopen/reassign. Use unassign or move to assign +the region elsewhere on cluster. Use with caution. For experts only. +Examples: + hbase> close_region 'REGIONNAME' hbase> close_region 'REGIONNAME', 'REGIONSERVER_IP:PORT' EOF Index: src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java (revision 1162672) +++ src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java (working copy) @@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; import java.util.ArrayList; @@ -35,6 +36,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseTestingUtility; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; @@ -50,6 +52,7 @@ import org.apache.hadoop.hbase.executor.EventHandler.EventType; import org.apache.hadoop.hbase.master.AssignmentManager; import org.apache.hadoop.hbase.master.MasterServices; +import org.apache.hadoop.hbase.regionserver.HRegionServer; import org.apache.hadoop.hbase.util.Bytes; import org.junit.AfterClass; import org.junit.Before; @@ -801,8 +804,160 @@ new HTable(TEST_UTIL.getConfiguration(), "testTableNotFoundExceptionWithoutAnyTables"); } + @Test + public void testShouldCloseTheRegionBasedOnTheEncodedRegionName() + throws Exception { + String tbName = "TestHBACloseRegion"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + HRegionInfo info = null; + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + info = regionInfo; + admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(), + rs.getServerInfo().getHostnamePort()); + } + } + Thread.sleep(1000); + onlineRegions = rs.getOnlineRegions(); + assertFalse("The region should not be present in online regions list.", + onlineRegions.contains(info)); + } + @Test + public void testCloseRegionIfInvalidRegionNameIsPassed() throws Exception { + String tbName = "TestCloseRegionIfInvalidRegionName"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + + HRegionInfo info = null; + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + if (regionInfo.getRegionNameAsString().contains(tbName)) { + info = regionInfo; + admin.closeRegionWithEncodedRegionName("sample", + rs.getServerInfo().getHostnamePort()); + } + } + } + onlineRegions = rs.getOnlineRegions(); + assertTrue("The region should be present in online regions list.", + onlineRegions.contains(info)); + } + + @Test + public void testCloseRegionThatFetchesTheHRIFromMeta() throws Exception { + String tbName = "TestCloseRegionThatFetchesTheHRIFromMeta"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + + HRegionInfo info = null; + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + + if (regionInfo.getRegionNameAsString().contains(tbName)) { + info = regionInfo; + admin.closeRegion(regionInfo.getRegionNameAsString(), + rs.getServerInfo().getHostnamePort()); + } + } + } + Thread.sleep(1000); + onlineRegions = rs.getOnlineRegions(); + assertFalse("The region should not be present in online regions list.", + onlineRegions.contains(info)); + } + + @Test + public void testCloseRegionWhenServerNameIsNull() throws Exception { + String tbName = "TestCloseRegionWhenServerNameIsNull"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + + try { + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + if (regionInfo.getRegionNameAsString().contains(tbName)) { + admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(), + null); + } + } + } + fail("The test should throw exception if the servername passed is null."); + } catch (IllegalArgumentException e) { + } + } + + @Test + public void testCloseRegionWhenServerNameIsEmpty() throws Exception { + String tbName = "TestHBACloseRegionWhenServerNameIsEmpty"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + + try { + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + if (regionInfo.getRegionNameAsString().contains(tbName)) { + admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(), + " "); + } + } + } + fail("The test should throw exception if the servername passed is empty."); + } catch (IllegalArgumentException e) { + } + } + + @Test + public void testCloseRegionWhenEncodedRegionNameIsNotGiven() throws Exception { + String tbName = "TestCloseRegionWhenEncodedRegionNameIsNotGiven"; + byte[] TABLENAME = Bytes.toBytes(tbName); + HBaseAdmin admin = createTable(TABLENAME); + + HRegionInfo info = null; + HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME); + + List onlineRegions = rs.getOnlineRegions(); + for (HRegionInfo regionInfo : onlineRegions) { + if (!regionInfo.isMetaRegion() && !regionInfo.isRootRegion()) { + if (regionInfo.getRegionNameAsString().contains(tbName)) { + info = regionInfo; + admin.closeRegionWithEncodedRegionName(regionInfo.getRegionNameAsString(), + rs.getServerInfo().getHostnamePort()); + } + } + } + onlineRegions = rs.getOnlineRegions(); + assertTrue("The region should be present in online regions list.", + onlineRegions.contains(info)); + } + + private HBaseAdmin createTable(byte[] TABLENAME) throws IOException { + + Configuration config = TEST_UTIL.getConfiguration(); + HBaseAdmin admin = new HBaseAdmin(config); + + HTableDescriptor htd = new HTableDescriptor(TABLENAME); + HColumnDescriptor hcd = new HColumnDescriptor("value"); + + htd.addFamily(hcd); + admin.createTable(htd, null); + return admin; + } + @Test public void testHundredsOfTable() throws IOException{ final int times = 100; HColumnDescriptor fam1 = new HColumnDescriptor("fam1");