hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jya...@apache.org
Subject svn commit: r1524827 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java
Date Thu, 19 Sep 2013 20:32:40 GMT
Author: jyates
Date: Thu Sep 19 20:32:39 2013
New Revision: 1524827

URL: http://svn.apache.org/r1524827
Log:
HBASE-9584: Short-Circuit Coprocessor doesn't correctly lookup table when on server

Added:
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java
Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java?rev=1524827&r1=1524826&r2=1524827&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java
Thu Sep 19 20:32:39 2013
@@ -18,29 +18,16 @@
 package org.apache.hadoop.hbase.client;
 
 import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ExecutorService;
+import java.net.InetSocketAddress;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.CoprocessorEnvironment;
-import org.apache.hadoop.hbase.HRegionInfo;
-import org.apache.hadoop.hbase.HRegionLocation;
-import org.apache.hadoop.hbase.HServerAddress;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.MasterNotRunningException;
 import org.apache.hadoop.hbase.ServerName;
-import org.apache.hadoop.hbase.ZooKeeperConnectionException;
 import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation;
-import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
-import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
-import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
-import org.apache.hadoop.hbase.ipc.HMasterInterface;
 import org.apache.hadoop.hbase.ipc.HRegionInterface;
 import org.apache.hadoop.hbase.regionserver.HRegionServer;
 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
-import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
 
 /**
  * Connection to an HTable from within a Coprocessor. Can do some nice tricks since we know
we are
@@ -91,34 +78,33 @@ public class CoprocessorHConnection exte
     this.serverName = server.getServerName();
   }
 
-  public HRegionInterface getHRegionConnection(String hostname, int port) throws IOException
{
+  @Override
+  HRegionInterface getHRegionConnection(final String hostname, final int port,
+      final InetSocketAddress isa, final boolean master) throws IOException {
+    // check to see where the server is running
+    // need this isa stuff here since its what the HConnectionManager is doing too
+    boolean isRemote = false;
+    if (isa != null) {
+      isRemote = checkRemote(isa.getHostName(), isa.getPort());
+    } else {
+      isRemote = checkRemote(hostname, port);
+    }
     // if we aren't talking to the local HRegionServer, then do the usual thing
-    if (!this.serverName.getHostname().equals(hostname) || !(this.serverName.getPort() ==
port)) {
-      return super.getHRegionConnection(hostname, port);
+    if (isRemote) {
+      return super.getHRegionConnection(hostname, port, isa, master);
     }
 
-    // in the usual HConnectionImplementation we would check a cache for the server. However,
-    // because we can just return the actual server, we don't need to do anything special.
+    // local access, so just pass the actual server, rather than a proxy
     return this.server;
   }
 
-  @Deprecated
-  public HRegionInterface getHRegionConnection(HServerAddress regionServer) throws IOException
{
-    throw new UnsupportedOperationException(
-        "Coprocessors only find tables via #getHRegionConnection(String, int)");
-  }
-
-  @Deprecated
-  public HRegionInterface getHRegionConnection(HServerAddress regionServer, boolean getMaster)
-      throws IOException {
-    throw new UnsupportedOperationException(
-        "Coprocessors only find tables via #getHRegionConnection(String, int)");
-  }
-
-  @Deprecated
-  public HRegionInterface getHRegionConnection(String hostname, int port, boolean getMaster)
-      throws IOException {
-    throw new UnsupportedOperationException(
-        "Coprocessors only find tables via #getHRegionConnection(String, int)");
+  /**
+   * Check that the hostname and port map the the server on which we are currently running
+   * @param hostName hostname to check
+   * @param port port to check
+   * @return <tt>true</tt> the connection is <b>not</b> currently
running on the given host and port
+   */
+  private boolean checkRemote(String hostName, int port) {
+    return !(this.serverName.getHostname().equals(hostName) && this.serverName.getPort()
== port);
   }
 }
\ No newline at end of file

Added: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java?rev=1524827&view=auto
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java
(added)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestCoprocessorHConnection.java
Thu Sep 19 20:32:39 2013
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.MediumTests;
+import org.apache.hadoop.hbase.ServerName;
+import org.apache.hadoop.hbase.regionserver.HRegionServer;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.Mockito;
+
+@Category(MediumTests.class)
+public class TestCoprocessorHConnection {
+
+  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
+
+  @BeforeClass
+  public static void setupCluster() throws Exception {
+    UTIL.startMiniCluster();
+  }
+
+  @AfterClass
+  public static void shutdownCluster() throws Exception {
+    UTIL.shutdownMiniCluster();
+  }
+
+  /**
+   * Ensure that if the HRegion we are looking up isn't on this server (and not in the cache),
that
+   * we still correctly look it up.
+   * @throws Exception on failure
+   */
+  @Test
+  public void testNonServerLocalLookup() throws Exception {
+    Configuration conf = UTIL.getConfiguration();
+    // make a fake server that should never be called
+    HRegionServer server = Mockito.mock(HRegionServer.class);
+    ServerName name = new ServerName("not.a.server.hostname", 12345, -1L);
+    Mockito.when(server.getServerName()).thenReturn(name);
+    CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
+
+    // make sure we get the mock server when doing a direct lookup
+    assertEquals("Didn't get the mock server from the connection", server,
+      connection.getHRegionConnection(name.getHostname(), name.getPort()));
+
+    // create a table that exists
+    byte[] tableName = Bytes.toBytes("testNonServerLocalLookup");
+    byte[] family = Bytes.toBytes("family");
+    UTIL.createTable(tableName, family);
+
+    // if we can write to the table correctly, then our connection is doing the right thing
+    HTable table = new HTable(tableName, connection);
+    Put p = new Put(Bytes.toBytes("row"));
+    p.add(family, null, null);
+    table.put(p);
+    table.flushCommits();
+
+
+    // cleanup
+    table.close();
+    connection.close();
+  }
+
+  @Test
+  public void testLocalServerLookup() throws Exception {
+    Configuration conf = UTIL.getConfiguration();
+    // get a real rs
+    HRegionServer server =
+        UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
+    // fake the connection to look like we are living on that server
+    CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
+
+    // create a table that exists
+    byte[] tableName = Bytes.toBytes("testLocalServerLookup");
+    byte[] family = Bytes.toBytes("family");
+    UTIL.createTable(tableName, family);
+
+    // if we can write to the table correctly, then our connection is doing the right thing
+    HTable table = new HTable(tableName, connection);
+    Put p = new Put(Bytes.toBytes("row"));
+    p.add(family, null, null);
+    table.put(p);
+    table.flushCommits();
+
+    //make sure we get the actual server when doing a direct lookup
+    ServerName name = server.getServerName();
+    assertEquals("Didn't get the expected server from the connection", server,
+      connection.getHRegionConnection(name.getHostname(), name.getPort()));
+
+    // cleanup
+    table.close();
+    connection.close();
+  }
+}
\ No newline at end of file



Mime
View raw message