You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ns...@apache.org on 2012/01/20 03:22:56 UTC

svn commit: r1233730 - in /hbase/branches/0.89-fb/src: main/java/org/apache/hadoop/hbase/client/HTable.java test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java

Author: nspiegelberg
Date: Fri Jan 20 02:22:56 2012
New Revision: 1233730

URL: http://svn.apache.org/viewvc?rev=1233730&view=rev
Log:
[HBASE-5177] Add a non-caching version of getRegionLocation.

Summary:
There is a need for a non caching version of getRegionLocation
on the client side. This API is needed for HBase backups to quickly lookup the
regionserver that hosts a particular region without using the heavy weight
getRegionsInfo() method.

Test Plan: 1) Unit test added.

Reviewers: kannan, kranganathan

Reviewed By: kannan

CC: hbase-eng@lists, madhuvaidya, kannan

Differential Revision: https://phabricator.fb.com/D387804

Modified:
    hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/HTable.java
    hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java

Modified: hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/HTable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/HTable.java?rev=1233730&r1=1233729&r2=1233730&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/HTable.java (original)
+++ hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/HTable.java Fri Jan 20 02:22:56 2012
@@ -257,6 +257,19 @@ public class HTable implements HTableInt
     return connection.getRegionLocation(tableName, row, false);
   }
 
+  /**
+   * Finds the region on which the given row is being served.
+   * @param row Row to find.
+   * @param reload whether or not to reload information or just use cached
+   * information
+   * @return Location of the row.
+   * @throws IOException if a remote or network exception occurs
+   */
+  public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
+  throws IOException {
+    return connection.getRegionLocation(tableName, row, reload);
+  }
+
   public byte [] getTableName() {
     return this.tableName;
   }

Modified: hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java?rev=1233730&r1=1233729&r2=1233730&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java (original)
+++ hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java Fri Jan 20 02:22:56 2012
@@ -21,6 +21,7 @@ package org.apache.hadoop.hbase.client;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -69,6 +70,7 @@ import org.apache.hadoop.hbase.filter.Si
 import org.apache.hadoop.hbase.filter.WhileMatchFilter;
 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
 import org.apache.hadoop.hbase.ipc.HRegionInterface;
+import org.apache.hadoop.hbase.regionserver.HRegionServer;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.hbase.util.Pair;
 import org.junit.After;
@@ -91,13 +93,14 @@ public class TestFromClientSide {
   private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
   private static byte [] VALUE = Bytes.toBytes("testValue");
   private static Random random = new Random();
+  private static int SLAVES = 3;
 
   /**
    * @throws java.lang.Exception
    */
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
-    TEST_UTIL.startMiniCluster(3);
+    TEST_UTIL.startMiniCluster(SLAVES);
   }
 
   /**
@@ -325,7 +328,7 @@ public class TestFromClientSide {
       CompareFilter.CompareOp.GREATER_OR_EQUAL));
     assertEquals(rowCount - endKeyCount, countGreater);
   }
-  
+
   /*
    * Load table with rows from 'aaa' to 'zzz'.
    * @param t
@@ -1575,7 +1578,7 @@ public class TestFromClientSide {
     get.addFamily(FAMILIES[0]);
     get.setMaxVersions(Integer.MAX_VALUE);
     result = ht.get(get);
-    assertNResult(result, ROW, FAMILIES[0], QUALIFIER, 
+    assertNResult(result, ROW, FAMILIES[0], QUALIFIER,
         new long [] {ts[1], ts[2], ts[3]},
         new byte[][] {VALUES[1], VALUES[2], VALUES[3]},
         0, 2);
@@ -4138,5 +4141,56 @@ public class TestFromClientSide {
   public void testMajorCompactCFTable() throws Exception {
     compactCFTable(1);
   }
+
+  @Test
+  /**
+   * Tests the non cached version of getRegionLocation by moving a region.
+   */
+  public void testNonCachedGetRegionLocation() throws Exception {
+    // Test Initialization.
+    String tableName = "testNonCachedGetRegionLocation";
+    byte [] TABLE = Bytes.toBytes(tableName);
+    byte [] family1 = Bytes.toBytes("f1");
+    byte [] family2 = Bytes.toBytes("f2");
+    HTable table = TEST_UTIL.createTable(TABLE, new byte[][] {family1, family2}, 10);
+    HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
+    Map <HRegionInfo, HServerAddress> regionsMap = table.getRegionsInfo();
+    assertEquals(1, regionsMap.size());
+    HRegionInfo regionInfo = regionsMap.keySet().iterator().next();
+    HServerAddress addrBefore = regionsMap.get(regionInfo);
+    // Verify region location before move.
+    HServerAddress addrCache =
+      table.getRegionLocation(regionInfo.getStartKey()).getServerAddress();
+    HServerAddress addrNoCache =
+      table.getRegionLocation(regionInfo.getStartKey(),
+          true).getServerAddress();
+
+    assertEquals(addrBefore.getPort(), addrCache.getPort());
+    assertEquals(addrBefore.getPort(), addrNoCache.getPort());
+
+    HServerAddress addrAfter = null;
+    // Now move the region to a different server.
+    for (int i = 0; i < SLAVES; i++) {
+      HRegionServer regionServer = TEST_UTIL.getHBaseCluster().getRegionServer(i);
+      HServerAddress addr = regionServer.getServerInfo().getServerAddress();
+      if (addr.getPort() != addrBefore.getPort()) {
+        admin.moveRegion(regionInfo.getRegionName(), addr.toString());
+        // Wait for the region to move.
+        Thread.sleep(5000);
+        addrAfter = addr;
+        break;
+      }
+    }
+
+    // Verify the region was moved.
+    addrCache =
+      table.getRegionLocation(regionInfo.getStartKey()).getServerAddress();
+    addrNoCache =
+      table.getRegionLocation(regionInfo.getStartKey(),
+          true).getServerAddress();
+    assertNotNull(addrAfter);
+    assertTrue(addrAfter.getPort() != addrCache.getPort());
+    assertEquals(addrAfter.getPort(), addrNoCache.getPort());
+  }
 }