Repository: hbase Updated Branches: refs/heads/master 58798fc09 -> 4793988ae
HBASE-15856 Don't cache unresolved addresses for connections Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/4793988a Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/4793988a Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/4793988a Branch: refs/heads/master Commit: 4793988aef849625c565e1cc410af39040881f6d Parents: 58798fc Author: Gary Helmling <[email protected]> Authored: Thu May 19 12:43:18 2016 -0700 Committer: Gary Helmling <[email protected]> Committed: Fri May 20 09:36:11 2016 -0700 ---------------------------------------------------------------------- .../hadoop/hbase/ipc/AbstractRpcClient.java | 9 +- .../hadoop/hbase/client/TestClientTimeouts.java | 5 +- .../client/TestConnectionImplementation.java | 94 ++++++++++++++++++++ .../hbase/master/TestMasterNoCluster.java | 22 ++--- 4 files changed, 116 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/4793988a/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java ---------------------------------------------------------------------- diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java index 71c8875..3d3339a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/AbstractRpcClient.java @@ -30,6 +30,7 @@ import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketTimeoutException; +import java.net.UnknownHostException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -266,7 +267,7 @@ public abstract class AbstractRpcClient implements RpcClient { @Override public BlockingRpcChannel createBlockingRpcChannel(final ServerName sn, final User ticket, - int defaultOperationTimeout) { + int defaultOperationTimeout) throws UnknownHostException { return new BlockingRpcChannelImplementation(this, sn, ticket, defaultOperationTimeout); } @@ -332,8 +333,12 @@ public abstract class AbstractRpcClient implements RpcClient { * @param channelOperationTimeout - the default timeout when no timeout is given */ protected BlockingRpcChannelImplementation(final AbstractRpcClient rpcClient, - final ServerName sn, final User ticket, int channelOperationTimeout) { + final ServerName sn, final User ticket, int channelOperationTimeout) + throws UnknownHostException { this.isa = new InetSocketAddress(sn.getHostname(), sn.getPort()); + if (this.isa.isUnresolved()) { + throw new UnknownHostException(sn.getHostname()); + } this.rpcClient = rpcClient; this.ticket = ticket; this.channelOperationTimeout = channelOperationTimeout; http://git-wip-us.apache.org/repos/asf/hbase/blob/4793988a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestClientTimeouts.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestClientTimeouts.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestClientTimeouts.java index 36276fa..109e416 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestClientTimeouts.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestClientTimeouts.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import java.net.SocketAddress; import java.net.SocketTimeoutException; +import java.net.UnknownHostException; import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; @@ -142,7 +143,7 @@ public class TestClientTimeouts { // Return my own instance, one that does random timeouts @Override public BlockingRpcChannel createBlockingRpcChannel(ServerName sn, - User ticket, int rpcTimeout) { + User ticket, int rpcTimeout) throws UnknownHostException { return new RandomTimeoutBlockingRpcChannel(this, sn, ticket, rpcTimeout); } } @@ -157,7 +158,7 @@ public class TestClientTimeouts { private static AtomicInteger invokations = new AtomicInteger(); RandomTimeoutBlockingRpcChannel(final RpcClientImpl rpcClient, final ServerName sn, - final User ticket, final int rpcTimeout) { + final User ticket, final int rpcTimeout) throws UnknownHostException { super(rpcClient, sn, ticket, rpcTimeout); } http://git-wip-us.apache.org/repos/asf/hbase/blob/4793988a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionImplementation.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionImplementation.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionImplementation.java new file mode 100644 index 0000000..af7b652 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionImplementation.java @@ -0,0 +1,94 @@ +/* + * 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.fail; + +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.net.UnknownHostException; + +/** + * Tests that we fail fast when hostname resolution is not working and do not cache + * unresolved InetSocketAddresses. + */ +@Category({MediumTests.class, ClientTests.class}) +public class TestConnectionImplementation { + private static HBaseTestingUtility testUtil; + private static ConnectionImplementation conn; + + @BeforeClass + public static void setupBeforeClass() throws Exception { + testUtil = HBaseTestingUtility.createLocalHTU(); + testUtil.startMiniCluster(); + conn = (ConnectionImplementation) testUtil.getConnection(); + } + + @AfterClass + public static void teardownAfterClass() throws Exception { + conn.close(); + testUtil.shutdownMiniCluster(); + } + + @Test(expected = UnknownHostException.class) + public void testGetAdminBadHostname() throws Exception { + // verify that we can get an instance with the cluster hostname + ServerName master = testUtil.getHBaseCluster().getMaster().getServerName(); + try { + conn.getAdmin(master); + } catch (UnknownHostException uhe) { + fail("Obtaining admin to the cluster master should have succeeded"); + } + + // test that we fail to get a client to an unresolvable hostname, which + // means it won't be cached + ServerName badHost = + ServerName.valueOf("unknownhost.example.com:" + HConstants.DEFAULT_MASTER_PORT, + System.currentTimeMillis()); + conn.getAdmin(badHost); + fail("Obtaining admin to unresolvable hostname should have failed"); + } + + @Test(expected = UnknownHostException.class) + public void testGetClientBadHostname() throws Exception { + // verify that we can get an instance with the cluster hostname + ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName(); + try { + conn.getClient(rs); + } catch (UnknownHostException uhe) { + fail("Obtaining client to the cluster regionserver should have succeeded"); + } + + // test that we fail to get a client to an unresolvable hostname, which + // means it won't be cached + ServerName badHost = + ServerName.valueOf("unknownhost.example.com:" + HConstants.DEFAULT_REGIONSERVER_PORT, + System.currentTimeMillis()); + conn.getAdmin(badHost); + fail("Obtaining client to unresolvable hostname should have failed"); + } +} http://git-wip-us.apache.org/repos/asf/hbase/blob/4793988a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterNoCluster.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterNoCluster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterNoCluster.java index 374c773..3ebf31e 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterNoCluster.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterNoCluster.java @@ -185,6 +185,12 @@ public class TestMasterNoCluster { // of the 'remote' mocked up regionservers. CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager( TESTUTIL.getConfiguration()); + // Insert a mock for the connection, use TESTUTIL.getConfiguration rather than + // the conf from the master; the conf will already have an HConnection + // associate so the below mocking of a connection will fail. + final ClusterConnection mockedConnection = HConnectionTestingUtility.getMockedConnectionAndDecorate( + TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(), + HRegionInfo.FIRST_META_REGIONINFO); HMaster master = new HMaster(conf, cp) { InetAddress getRemoteInetAddress(final int port, final long serverStartCode) throws UnknownHostException { @@ -215,16 +221,12 @@ public class TestMasterNoCluster { @Override public ClusterConnection getConnection() { - // Insert a mock for the connection, use TESTUTIL.getConfiguration rather than - // the conf from the master; the conf will already have an HConnection - // associate so the below mocking of a connection will fail. - try { - return HConnectionTestingUtility.getMockedConnectionAndDecorate( - TESTUTIL.getConfiguration(), rs0, rs0, rs0.getServerName(), - HRegionInfo.FIRST_META_REGIONINFO); - } catch (IOException e) { - return null; - } + return mockedConnection; + } + + @Override + public ClusterConnection getClusterConnection() { + return mockedConnection; } }; master.start();
