hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jxi...@apache.org
Subject svn commit: r1377877 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java test/java/org/apache/hadoop/hbase/client/TestAdmin.java
Date Mon, 27 Aug 2012 21:23:54 GMT
Author: jxiang
Date: Mon Aug 27 21:23:54 2012
New Revision: 1377877

URL: http://svn.apache.org/viewvc?rev=1377877&view=rev
Log:
HBASE-6643 Accept encoded region name in compacting/spliting region from shell

Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java?rev=1377877&r1=1377876&r2=1377877&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java Mon Aug
27 21:23:54 2012
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
 
 import org.apache.commons.logging.Log;
@@ -61,7 +62,6 @@ import org.apache.hadoop.hbase.regionser
 import org.apache.hadoop.hbase.util.Addressing;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.hbase.util.Pair;
-import org.apache.hadoop.hbase.util.Writables;
 import org.apache.hadoop.ipc.RemoteException;
 import org.apache.hadoop.util.StringUtils;
 
@@ -408,11 +408,13 @@ public class HBaseAdmin implements Abort
         MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
           @Override
           public boolean processRow(Result rowResult) throws IOException {
-            HRegionInfo info = Writables.getHRegionInfoOrNull(
-              rowResult.getValue(HConstants.CATALOG_FAMILY,
-              HConstants.REGIONINFO_QUALIFIER));
-            //If regioninfo is null, skip this row
-            if (null == info) {
+            if (rowResult == null || rowResult.size() <= 0) {
+              return true;
+            }
+            HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
+              rowResult, HConstants.REGIONINFO_QUALIFIER);
+            if (info == null) {
+              LOG.warn("No serialized HRegionInfo in " + rowResult);
               return true;
             }
             if (!(Bytes.equals(info.getTableName(), desc.getName()))) {
@@ -1179,16 +1181,15 @@ public class HBaseAdmin implements Abort
   public void flush(final byte [] tableNameOrRegionName)
   throws IOException, InterruptedException {
     CatalogTracker ct = getCatalogTracker();
-    boolean isRegionName = isRegionName(tableNameOrRegionName, ct);
     try {
-      if (isRegionName) {
-        Pair<HRegionInfo, ServerName> pair =
-          MetaReader.getRegion(ct, tableNameOrRegionName);
-        if (pair == null || pair.getSecond() == null) {
+      Pair<HRegionInfo, ServerName> regionServerPair
+        = getRegion(tableNameOrRegionName, ct);
+      if (regionServerPair != null) {
+        if (regionServerPair.getSecond() == null) {
           LOG.info("No server in .META. for " +
-            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair);
+            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
         } else {
-          flush(pair.getSecond(), pair.getFirst());
+          flush(regionServerPair.getSecond(), regionServerPair.getFirst());
         }
       } else {
         final String tableName = tableNameString(tableNameOrRegionName, ct);
@@ -1285,14 +1286,14 @@ public class HBaseAdmin implements Abort
   throws IOException, InterruptedException {
     CatalogTracker ct = getCatalogTracker();
     try {
-      if (isRegionName(tableNameOrRegionName, ct)) {
-        Pair<HRegionInfo, ServerName> pair =
-          MetaReader.getRegion(ct, tableNameOrRegionName);
-        if (pair == null || pair.getSecond() == null) {
+      Pair<HRegionInfo, ServerName> regionServerPair
+        = getRegion(tableNameOrRegionName, ct);
+      if (regionServerPair != null) {
+        if (regionServerPair.getSecond() == null) {
           LOG.info("No server in .META. for " +
-            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair);
+            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
         } else {
-          compact(pair.getSecond(), pair.getFirst(), major);
+          compact(regionServerPair.getSecond(), regionServerPair.getFirst(), major);
         }
       } else {
         final String tableName = tableNameString(tableNameOrRegionName, ct);
@@ -1458,15 +1459,14 @@ public class HBaseAdmin implements Abort
       final byte [] splitPoint) throws IOException, InterruptedException {
     CatalogTracker ct = getCatalogTracker();
     try {
-      if (isRegionName(tableNameOrRegionName, ct)) {
-        // Its a possible region name.
-        Pair<HRegionInfo, ServerName> pair =
-          MetaReader.getRegion(ct, tableNameOrRegionName);
-        if (pair == null || pair.getSecond() == null) {
+      Pair<HRegionInfo, ServerName> regionServerPair
+        = getRegion(tableNameOrRegionName, ct);
+      if (regionServerPair != null) {
+        if (regionServerPair.getSecond() == null) {
           LOG.info("No server in .META. for " +
-            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair);
+            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
         } else {
-          split(pair.getSecond(), pair.getFirst(), splitPoint);
+          split(regionServerPair.getSecond(), regionServerPair.getFirst(), splitPoint);
         }
       } else {
         final String tableName = tableNameString(tableNameOrRegionName, ct);
@@ -1520,20 +1520,46 @@ public class HBaseAdmin implements Abort
 
   /**
    * @param tableNameOrRegionName Name of a table or name of a region.
-   * @param ct A {@link #CatalogTracker} instance (caller of this method usually has one).
-   * @return True if <code>tableNameOrRegionName</code> is a verified region
-   * name (we call {@link #MetaReader.getRegion(CatalogTracker catalogTracker,
-   * byte [] regionName)};) else false.
+   * @param ct A {@link CatalogTracker} instance (caller of this method usually has one).
+   * @return a pair of HRegionInfo and ServerName if <code>tableNameOrRegionName</code>
is
+   *  a verified region name (we call {@link  MetaReader#getRegion( CatalogTracker, byte[])}
+   *  else null.
    * Throw an exception if <code>tableNameOrRegionName</code> is null.
    * @throws IOException
    */
-  private boolean isRegionName(final byte[] tableNameOrRegionName,
-      CatalogTracker ct)
-  throws IOException {
+  Pair<HRegionInfo, ServerName> getRegion(final byte[] tableNameOrRegionName,
+      final CatalogTracker ct) throws IOException {
     if (tableNameOrRegionName == null) {
       throw new IllegalArgumentException("Pass a table name or region name");
     }
-    return (MetaReader.getRegion(ct, tableNameOrRegionName) != null);
+    Pair<HRegionInfo, ServerName> pair = MetaReader.getRegion(ct, tableNameOrRegionName);
+    if (pair == null) {
+      final AtomicReference<Pair<HRegionInfo, ServerName>> result =
+        new AtomicReference<Pair<HRegionInfo, ServerName>>(null);
+      final String encodedName = Bytes.toString(tableNameOrRegionName);
+      MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
+        @Override
+        public boolean processRow(Result data) throws IOException {
+          if (data == null || data.size() <= 0) {
+            return true;
+          }
+          HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
+            data, HConstants.REGIONINFO_QUALIFIER);
+          if (info == null) {
+            LOG.warn("No serialized HRegionInfo in " + data);
+            return true;
+          }
+          if (!encodedName.equals(info.getEncodedName())) return true;
+          ServerName sn = MetaReader.getServerNameFromCatalogResult(data);
+          result.set(new Pair<HRegionInfo, ServerName>(info, sn));
+          return false; // found the region, stop
+        }
+      };
+
+      MetaScanner.metaScan(conf, visitor);
+      pair = result.get();
+    }
+    return pair;
   }
 
   /**
@@ -1731,18 +1757,18 @@ public class HBaseAdmin implements Abort
     CompactionState state = CompactionState.NONE;
     CatalogTracker ct = getCatalogTracker();
     try {
-      if (isRegionName(tableNameOrRegionName, ct)) {
-        Pair<HRegionInfo, ServerName> pair =
-          MetaReader.getRegion(ct, tableNameOrRegionName);
-        if (pair == null || pair.getSecond() == null) {
+      Pair<HRegionInfo, ServerName> regionServerPair
+        = getRegion(tableNameOrRegionName, ct);
+      if (regionServerPair != null) {
+        if (regionServerPair.getSecond() == null) {
           LOG.info("No server in .META. for " +
-            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + pair);
+            Bytes.toStringBinary(tableNameOrRegionName) + "; pair=" + regionServerPair);
         } else {
-          ServerName sn = pair.getSecond();
+          ServerName sn = regionServerPair.getSecond();
           HRegionInterface rs =
             this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
           return CompactionState.valueOf(
-            rs.getCompactionState(pair.getFirst().getRegionName()));
+            rs.getCompactionState(regionServerPair.getFirst().getRegionName()));
         }
       } else {
         final String tableName = tableNameString(tableNameOrRegionName, ct);

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java?rev=1377877&r1=1377876&r2=1377877&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java Mon Aug
27 21:23:54 2012
@@ -40,6 +40,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.*;
+import org.apache.hadoop.hbase.catalog.CatalogTracker;
 import org.apache.hadoop.hbase.executor.EventHandler;
 import org.apache.hadoop.hbase.executor.EventHandler.EventType;
 import org.apache.hadoop.hbase.executor.ExecutorService;
@@ -49,6 +50,7 @@ import org.apache.hadoop.hbase.regionser
 import org.apache.hadoop.hbase.regionserver.wal.HLogUtilsForTests;
 import org.apache.hadoop.hbase.InvalidFamilyOperationException;
 import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.Pair;
 import org.apache.hadoop.hbase.zookeeper.ZKTable;
 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
 import org.junit.*;
@@ -1551,7 +1553,29 @@ public class TestAdmin {
     htd.addFamily(hcd);
     TEST_UTIL.getHBaseAdmin().createTable(htd);
   }
-  
+
+  @Test
+  public void testGetRegion() throws Exception {
+    final String name = "testGetRegion";
+    LOG.info("Started " + name);
+    final byte [] nameBytes = Bytes.toBytes(name);
+    HTable t = TEST_UTIL.createTable(nameBytes, HConstants.CATALOG_FAMILY);
+    TEST_UTIL.createMultiRegions(t, HConstants.CATALOG_FAMILY);
+    CatalogTracker ct = new CatalogTracker(TEST_UTIL.getConfiguration());
+    ct.start();
+    try {
+      HRegionLocation regionLocation = t.getRegionLocation("mmm");
+      HRegionInfo region = regionLocation.getRegionInfo();
+      byte[] regionName = region.getRegionName();
+      Pair<HRegionInfo, ServerName> pair = admin.getRegion(regionName, ct);
+      assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
+      pair = admin.getRegion(region.getEncodedNameAsBytes(), ct);
+      assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
+    } finally {
+      ct.stop();
+    }
+  }
+
   @org.junit.Rule
   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();



Mime
View raw message