You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by li...@apache.org on 2014/04/02 23:09:01 UTC

svn commit: r1584187 - in /hbase/branches/0.89-fb/src: main/java/org/apache/hadoop/hbase/client/ main/java/org/apache/hadoop/hbase/regionserver/ test/java/org/apache/hadoop/hbase/ test/java/org/apache/hadoop/hbase/client/

Author: liyin
Date: Wed Apr  2 21:09:00 2014
New Revision: 1584187

URL: http://svn.apache.org/r1584187
Log:
[master] Fix bug in TableServers.prefetchRegionCache that fails TestHTable

Author: daviddeng

Summary: In `getCachedHRegionLocations` a `null` is set to `row` because it doesn't care about the result. So add a check in `prefetchRegionCache`.

Test Plan: `TestHTable`

Reviewers: manukranthk, liyintang, fan, gauravm

Reviewed By: fan

CC: hbase-eng@

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

Task ID: 3296253

Modified:
    hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/MetaCache.java
    hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/TableServers.java
    hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java
    hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
    hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestHTable.java

Modified: hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/MetaCache.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/MetaCache.java?rev=1584187&r1=1584186&r2=1584187&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/MetaCache.java (original)
+++ hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/MetaCache.java Wed Apr  2 21:09:00 2014
@@ -87,7 +87,7 @@ public class MetaCache {
    * into the Soft Reference SortedMap.  Improve.
    *
    * @param tableName
-   * @param row
+   * @param row  a non-null byte array
    * @return Null or region location found in cache.
    */
   HRegionLocation getForRow(final byte[] tableName, final byte[] row) {
@@ -199,15 +199,20 @@ public class MetaCache {
    */
   public void add(final byte[] tableName, final HRegionLocation location) {
     byte[] startKey = location.getRegionInfo().getStartKey();
-    boolean hasNewCache;
+    HRegionLocation oldLocation;
     synchronized (this.tableToCache) {
       servers.add(location.getServerAddress().toString());
-      hasNewCache = (getForTable(tableName).put(startKey, location) == null);
+      oldLocation = getForTable(tableName).put(startKey, location);
     }
-    if (hasNewCache) {
+    if (oldLocation == null) {
       LOG.debug("Cached location for "
           + location.getRegionInfo().getRegionNameAsString() + " is "
           + location.getServerAddress().toString());
+    } else if (!oldLocation.equals(location)) {
+      LOG.debug("Cached location for "
+          + location.getRegionInfo().getRegionNameAsString() + " is changed"
+          + " from " + oldLocation.getServerAddress()
+          + " to " + location.getServerAddress());
     }
   }
 

Modified: hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/TableServers.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/TableServers.java?rev=1584187&r1=1584186&r2=1584187&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/TableServers.java (original)
+++ hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/client/TableServers.java Wed Apr  2 21:09:00 2014
@@ -642,6 +642,9 @@ public class TableServers implements Ser
    * Search .META. for the HRegionLocation info that contains the table and
    * row we're seeking. It will prefetch certain number of regions info and
    * save them to the global region cache.
+   *
+   * @param the row in the .META. table. If it is null, all cache will be
+   *        prefetched and null is returned.
    */
   public HRegionLocation prefetchRegionCache(final byte[] tableName,
       final byte[] row, int prefetchRegionLimit) {
@@ -694,8 +697,11 @@ public class TableServers implements Ser
       }
     };
     try {
-      // pre-fetch certain number of regions info at region cache.
+      // prefetch certain number of regions info at region cache.
       MetaScanner.metaScan(conf, visitor, tableName, row, prefetchRegionLimit);
+      if (row == null) {
+        return null;
+      }
       return metaCache.getForRow(tableName, row);
     } catch (IOException e) {
       LOG.warn("Encounted problems when prefetch META table: ", e);

Modified: hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java?rev=1584187&r1=1584186&r2=1584187&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (original)
+++ hbase/branches/0.89-fb/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java Wed Apr  2 21:09:00 2014
@@ -821,7 +821,7 @@ public class HRegionServer implements HR
         long now = System.currentTimeMillis();
         // Drop into the send loop if msgInterval has elapsed or if something
         // to send.  If we fail talking to the master, then we'll sleep below
-        // on poll of the outboundMsgs blockingqueue.
+        // on poll of the outboundMsgs blocking queue.
         if ((now - lastMsg) >= msgInterval || !outboundMessages.isEmpty()) {
           try {
             MemoryUsage memory =

Modified: hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java?rev=1584187&r1=1584186&r2=1584187&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java (original)
+++ hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java Wed Apr  2 21:09:00 2014
@@ -61,6 +61,8 @@ import org.apache.hadoop.hbase.client.HB
 import org.apache.hadoop.hbase.client.HConnection;
 import org.apache.hadoop.hbase.client.HTable;
 import org.apache.hadoop.hbase.client.HTableAsync;
+import org.apache.hadoop.hbase.client.MetaScanner;
+import org.apache.hadoop.hbase.client.MetaScanner.MetaScannerVisitor;
 import org.apache.hadoop.hbase.client.NoServerForRegionException;
 import org.apache.hadoop.hbase.client.PreemptiveFastFailException;
 import org.apache.hadoop.hbase.client.Put;
@@ -945,6 +947,7 @@ public class HBaseTestingUtility {
    * It first searches for the meta rows that contain the region of the
    * specified table, then gets the index of that RS, and finally retrieves
    * the RS's reference.
+   *
    * @param tableName user table to lookup in .META.
    * @return region server that holds it, null if the row doesn't exist
    * @throws IOException
@@ -955,9 +958,9 @@ public class HBaseTestingUtility {
     if (metaRows == null || metaRows.isEmpty()) {
       return null;
     }
-    LOG.debug("Found " + metaRows.size() + " rows for table " +
-      Bytes.toString(tableName));
-    byte [] firstrow = metaRows.get(0);
+    LOG.debug("Found " + metaRows.size() + " rows for table "
+        + Bytes.toString(tableName));
+    byte[] firstrow = metaRows.get(0);
     LOG.debug("FirstRow=" + Bytes.toString(firstrow));
     int index = hbaseCluster.getServerWith(firstrow);
     if (index < 0) {
@@ -1329,22 +1332,113 @@ public class HBaseTestingUtility {
   public void waitForOnlineRegionsToBeAssigned(int expectedUserRegions) {
     int actualRegions;
 
-    do {
+    while (true) {
+      actualRegions = 0;
+
+      List<HRegionServer> onlineServers = getOnlineRegionServers();
+      LOG.info("Online servers: " + onlineServers);
+      for (HRegionServer server : onlineServers) {
+        for (HRegion region : server.getOnlineRegions()) {
+          // Ignore META or ROOT regions.
+          if (region.getRegionInfo().isMetaRegion()
+              || region.getRegionInfo().isRootRegion()) {
+            continue;
+          }
+
+          LOG.info("Region " + region + " at " + server);
+          actualRegions++;
+        }
+      }
+      if (actualRegions == expectedUserRegions) {
+        break;
+      }
+
       try {
         Thread.sleep(1000);
-      } catch (InterruptedException e) {} // Ignore
+      } catch (InterruptedException e) {
+        // Ignore
+        Thread.currentThread().interrupt();
+      }
+    }
 
-      actualRegions = 0;
+    LOG.info(expectedUserRegions + " regions asssigned");
+  }
 
-      for (HRegionServer server : getOnlineRegionServers()) {
-        for (HRegion region : server.getOnlineRegions()) {
-          if (!region.getRegionInfo().isMetaRegion() && !region.getRegionInfo().isRootRegion()) {
-            actualRegions += 1;
+  /**
+   * Waits until the META table has same contents as the region servers.
+   */
+  public void waitForTableConsistent() throws IOException {
+    while (true) {
+      // Location info from META table
+      final Map<String, Map<String, HServerAddress>> fromMeta = new HashMap<>();
+      MetaScannerVisitor visitor = new MetaScannerVisitor() {
+        @Override
+        public boolean processRow(Result rowResult) throws IOException {
+          HRegionInfo info =
+              Writables.getHRegionInfo(rowResult.getValue(
+                  HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER));
+
+          String tableName = info.getTableDesc().getNameAsString();
+
+          HServerAddress server = new HServerAddress();
+          byte[] value =
+              rowResult.getValue(HConstants.CATALOG_FAMILY,
+                  HConstants.SERVER_QUALIFIER);
+          if (value != null && value.length > 0) {
+            String address = Bytes.toString(value);
+            server = new HServerAddress(address);
+          }
+
+          if (!(info.isOffline() || info.isSplit())) {
+            Map<String, HServerAddress> regionToAddr =
+                fromMeta.get(tableName);
+            if (regionToAddr == null) {
+              regionToAddr = new HashMap<>();
+              fromMeta.put(tableName, regionToAddr);
+            }
+            regionToAddr.put(info.getRegionNameAsString(), server);
           }
+          return true;
         }
+
+      };
+      MetaScanner.metaScan(conf, visitor, null);
+
+      // Location info from region servers
+      final Map<String, Map<String, HServerAddress>> fromRS = new HashMap<>();
+      List<HRegionServer> onlineServers = getOnlineRegionServers();
+      for (HRegionServer rs : onlineServers) {
+        for (HRegion region : rs.getOnlineRegions()) {
+          // Ignore META or ROOT regions.
+          if (region.getRegionInfo().isMetaRegion()
+              || region.getRegionInfo().isRootRegion()) {
+            continue;
+          }
+
+          HRegionInfo info = region.getRegionInfo();
+          String tableName = info.getTableDesc().getNameAsString();
+          Map<String, HServerAddress> regionToAddr = fromRS.get(tableName);
+          if (regionToAddr == null) {
+            regionToAddr = new HashMap<>();
+            fromRS.put(tableName, regionToAddr);
+          }
+          regionToAddr.put(info.getRegionNameAsString(), rs.getServerInfo()
+              .getServerAddress());
+        }
+      }
+
+      if (fromMeta.equals(fromRS)) {
+        // Consistent now, quit
+        break;
       }
-    } while (actualRegions != expectedUserRegions);
 
+      // Sleep for a while before next check
+      try {
+        Thread.sleep(1000);
+      } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
+      }
+    }
   }
 
   public List<HRegionServer> getOnlineRegionServers() {

Modified: hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestHTable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestHTable.java?rev=1584187&r1=1584186&r2=1584187&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestHTable.java (original)
+++ hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/client/TestHTable.java Wed Apr  2 21:09:00 2014
@@ -1,22 +1,28 @@
 package org.apache.hadoop.hbase.client;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.hbase.*;
-import org.apache.hadoop.hbase.regionserver.HRegion;
-import org.apache.hadoop.hbase.regionserver.HRegionServer;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.junit.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.NavigableMap;
 import java.util.concurrent.ThreadPoolExecutor;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.HRegionInfo;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.HServerAddress;
+import org.apache.hadoop.hbase.TableNotFoundException;
+import org.apache.hadoop.hbase.regionserver.HRegion;
+import org.apache.hadoop.hbase.regionserver.HRegionServer;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
 
 public class TestHTable {
   final Log LOG = LogFactory.getLog(getClass());
@@ -31,6 +37,9 @@ public class TestHTable {
    */
   @Before
   public void setUp() throws Exception {
+    TEST_UTIL.getConfiguration().set(HBaseTestingUtility.FS_TYPE_KEY,
+        HBaseTestingUtility.FS_TYPE_LFS);
+
     TEST_UTIL.startMiniCluster(SLAVES);
   }
 
@@ -43,7 +52,7 @@ public class TestHTable {
   }
 
   @Test
-  public void testCachedHRegionLocations() throws IOException {
+  public void testCachedHRegionLocations() throws Exception {
     final int NUM_REGIONS = 20;
     byte[] tableName = Bytes.toBytes("testCachedHRegionLocations");
     HTable table = TEST_UTIL.createTable(tableName, new byte[][]{FAMILY},
@@ -57,28 +66,37 @@ public class TestHTable {
     // Make sure allRegions and regionLocations containing the same information
     verifyOnlineRegionsAndRegionLocations(allRegionsInfoMap, regionLocations, NUM_REGIONS);
 
-    HRegionServer killedServer = TEST_UTIL.getRSForFirstRegionInTable(tableName);
+    HRegionServer killedServer =
+        TEST_UTIL.getRSForFirstRegionInTable(tableName);
     Collection<HRegion> regionsToBeMoved = killedServer.getOnlineRegions();
-    List<HRegionInfo> testRegionInfos = new ArrayList();
+    List<HRegionInfo> testRegionInfos = new ArrayList<>();
+
+    LOG.info(killedServer + " at "
+        + killedServer.getServerInfo().getServerAddress()
+        + " is going to be killed");
 
     for (HRegion region : regionsToBeMoved) {
       HRegionInfo regionInfo = region.getRegionInfo();
-      if (Bytes.equals(regionInfo.getTableDesc().getName(),tableName)) {
+      if (Bytes.equals(regionInfo.getTableDesc().getName(), tableName)) {
         testRegionInfos.add(regionInfo);
       }
     }
-    Assert.assertTrue(testRegionInfos.size() > 0);
+    Assert.assertTrue("testRegionInfos should be non-empty",
+        testRegionInfos.size() > 0);
 
     HServerAddress killedAddress = killedServer.getHServerInfo().getServerAddress();
-    killedServer.stop("Testing");  // Stop this region server
+    killedServer.stop("Testing"); // Stop this region server
 
     TEST_UTIL.waitForOnlineRegionsToBeAssigned(NUM_REGIONS);
+    TEST_UTIL.waitForTableConsistent();
 
-    Assert.assertTrue(killedServer.getOnlineRegions().size() == 0);
+    Assert.assertEquals("Killed server online region count", 0,
+        killedServer.getOnlineRegions().size());
 
     regionLocations = table.getCachedHRegionLocations(false);
     // Make sure allRegions and regionLocations containing the same information
-    verifyOnlineRegionsAndRegionLocations(allRegionsInfoMap, regionLocations, NUM_REGIONS);
+    verifyOnlineRegionsAndRegionLocations(allRegionsInfoMap, regionLocations,
+        NUM_REGIONS);
     // Also, it has the stale information about the region location
     for (HRegionInfo info : testRegionInfos) {
       Assert.assertTrue(allRegionsInfoMap.get(info).equals(killedAddress));
@@ -88,29 +106,34 @@ public class TestHTable {
     allRegionsInfoMap = table.getRegionsInfo();
     regionLocations = table.getCachedHRegionLocations(true);
     // Make sure allRegions and regionLocations containing the same information
-    verifyOnlineRegionsAndRegionLocations(allRegionsInfoMap, regionLocations, NUM_REGIONS);
+    verifyOnlineRegionsAndRegionLocations(allRegionsInfoMap, regionLocations,
+        NUM_REGIONS);
 
-    Assert.assertTrue(killedServer.getOnlineRegions().size() == 0);
+    Assert.assertEquals("killedServer.getOnlineRegions.size", 0,
+        killedServer.getOnlineRegions().size());
     // Verify the new allRegionsInfoMap has been updated without the stale information
     for (HRegionInfo info : testRegionInfos) {
-      Assert.assertFalse((allRegionsInfoMap.get(info).equals(killedAddress)));
+      Assert.assertFalse("Region " + info + " is still in killed address "
+          + killedAddress, allRegionsInfoMap.get(info).equals(killedAddress));
     }
   }
 
-  private void verifyOnlineRegionsAndRegionLocations(NavigableMap<HRegionInfo,HServerAddress> allRegions,
-                                                     Collection<HRegionLocation> regionLocation,
-                                                     int numRegions) {
+  private void verifyOnlineRegionsAndRegionLocations(
+      NavigableMap<HRegionInfo, HServerAddress> allRegions,
+      Collection<HRegionLocation> regionLocation, int numRegions) {
     Assert.assertEquals(numRegions, allRegions.size());
     Assert.assertEquals(numRegions, regionLocation.size());
     for (HRegionLocation location : regionLocation) {
       HRegionInfo regionInfo = location.getRegionInfo();
       HServerAddress address = location.getServerAddress();
       HServerAddress address2 = allRegions.get(regionInfo);
-      Assert.assertTrue(address2 != null);
-      Assert.assertEquals(address, address2);
+      Assert.assertNotNull("Address of " + location + " in allRegions",
+          address2);
+      Assert.assertEquals("Address of " + regionInfo + " in allRegions",
+          address, address2);
     }
   }
-  
+
   @Test
   public void testHTableMultiPutThreadPool() throws Exception {
     byte [] TABLE = Bytes.toBytes("testHTableMultiputThreadPool");
@@ -122,7 +145,7 @@ public class TestHTable {
     int previousPoolSize = pool.getPoolSize();
     int previousLargestPoolSize = pool.getLargestPoolSize();
     long previousCompletedTaskCount = pool.getCompletedTaskCount();
-    
+
     for (int i = 0; i < NUM_REGIONS; i++) {
       Put put = new Put(ROWS[i]);
       put.add(FAMILY, QUALIFIER, VALUE);
@@ -135,7 +158,7 @@ public class TestHTable {
     assertEquals(previousPoolSize, pool.getPoolSize());
     assertEquals(previousLargestPoolSize, pool.getLargestPoolSize());
     assertEquals(previousCompletedTaskCount, pool.getCompletedTaskCount());
-    
+
     ArrayList<Put> multiput = new ArrayList<Put>();
     for (int i = 0; i < NUM_REGIONS; i++) {
       Put put = new Put(ROWS[i]);
@@ -144,11 +167,11 @@ public class TestHTable {
     }
     ht.put(multiput);
     ht.flushCommits();
-    
+
     // verify that HTable does use thread pool for multi put requests.
     assertTrue((SLAVES >= pool.getLargestPoolSize())
       && (pool.getLargestPoolSize() >= previousLargestPoolSize));
-    assertEquals(SLAVES, 
+    assertEquals(SLAVES,
         (pool.getCompletedTaskCount() - previousCompletedTaskCount));
   }