hadoop git commit: HADOOP-10219. ipc.Client.setupIOstreams() needs to check for ClientCache.stopClient requested shutdowns. Contributed by Kihwal Lee and Lukas Majercak.

2018-09-04 Thread weichiu
Repository: hadoop
Updated Branches:
  refs/heads/branch-3.0 25337c045 -> 9ee9d68e4


HADOOP-10219. ipc.Client.setupIOstreams() needs to check for 
ClientCache.stopClient requested shutdowns.
Contributed by Kihwal Lee and Lukas Majercak.

(cherry picked from commit 9e96ac666d783376a8cdea9c3cc84098c5bdcb56)
(cherry picked from commit 142d878c902da6bc02d0c21cdd5ca8674da7f5c8)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/9ee9d68e
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/9ee9d68e
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/9ee9d68e

Branch: refs/heads/branch-3.0
Commit: 9ee9d68e4b8a86e265a7dc9b6a3ad71b6ceb169b
Parents: 25337c0
Author: Steve Loughran 
Authored: Tue Sep 4 16:46:12 2018 +0100
Committer: Wei-Chiu Chuang 
Committed: Tue Sep 4 22:25:24 2018 -0700

--
 .../main/java/org/apache/hadoop/ipc/Client.java | 14 ++
 .../java/org/apache/hadoop/ipc/TestIPC.java | 45 
 2 files changed, 59 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/hadoop/blob/9ee9d68e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
index a0417d6..9ee0647 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
@@ -70,6 +70,7 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID;
 import static org.apache.hadoop.ipc.RpcConstants.PING_CALL_ID;
@@ -438,6 +439,8 @@ public class Client implements AutoCloseable {
 
 private final Object sendRpcRequestLock = new Object();
 
+private AtomicReference connectingThread = new AtomicReference<>();
+
 public Connection(ConnectionId remoteId, int serviceClass) throws 
IOException {
   this.remoteId = remoteId;
   this.server = remoteId.getAddress();
@@ -775,6 +778,7 @@ public class Client implements AutoCloseable {
 }
   }
   try {
+connectingThread.set(Thread.currentThread());
 if (LOG.isDebugEnabled()) {
   LOG.debug("Connecting to "+server);
 }
@@ -860,6 +864,8 @@ public class Client implements AutoCloseable {
   markClosed(new IOException("Couldn't set up IO streams: " + t, t));
 }
 close();
+  } finally {
+connectingThread.set(null);
   }
 }
 
@@ -1213,6 +1219,13 @@ public class Client implements AutoCloseable {
 notifyAll();
   }
 }
+
+private void interruptConnectingThread() {
+  Thread connThread = connectingThread.get();
+  if (connThread != null) {
+connThread.interrupt();
+  }
+}
 
 /** Close the connection. */
 private synchronized void close() {
@@ -1315,6 +1328,7 @@ public class Client implements AutoCloseable {
 // wake up all connections
 for (Connection conn : connections.values()) {
   conn.interrupt();
+  conn.interruptConnectingThread();
 }
 
 // wait until all connections are closed

http://git-wip-us.apache.org/repos/asf/hadoop/blob/9ee9d68e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
index a4577f2..cdbaea4 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -1398,6 +1399,50 @@ public class TestIPC {
 assertEquals(Client.getTimeout(config), -1);
   }
 
+  @Test(timeout=6)
+  public void testSetupConnectionShouldNotBlockShutdown() throws Exception {
+// Start server
+SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
+  

hadoop git commit: HADOOP-10219. ipc.Client.setupIOstreams() needs to check for ClientCache.stopClient requested shutdowns. Contributed by Kihwal Lee and Lukas Majercak.

2018-09-04 Thread inigoiri
Repository: hadoop
Updated Branches:
  refs/heads/branch-3.1 9cf35d99b -> 142d878c9


HADOOP-10219. ipc.Client.setupIOstreams() needs to check for 
ClientCache.stopClient requested shutdowns.
Contributed by Kihwal Lee and Lukas Majercak.

(cherry picked from commit 9e96ac666d783376a8cdea9c3cc84098c5bdcb56)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/142d878c
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/142d878c
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/142d878c

Branch: refs/heads/branch-3.1
Commit: 142d878c902da6bc02d0c21cdd5ca8674da7f5c8
Parents: 9cf35d9
Author: Steve Loughran 
Authored: Tue Sep 4 16:46:12 2018 +0100
Committer: Inigo Goiri 
Committed: Tue Sep 4 12:00:31 2018 -0700

--
 .../main/java/org/apache/hadoop/ipc/Client.java | 14 ++
 .../java/org/apache/hadoop/ipc/TestIPC.java | 45 
 2 files changed, 59 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/hadoop/blob/142d878c/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
index e147048..07a2f13 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
@@ -70,6 +70,7 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID;
 import static org.apache.hadoop.ipc.RpcConstants.PING_CALL_ID;
@@ -439,6 +440,8 @@ public class Client implements AutoCloseable {
 
 private final Object sendRpcRequestLock = new Object();
 
+private AtomicReference connectingThread = new AtomicReference<>();
+
 public Connection(ConnectionId remoteId, int serviceClass) throws 
IOException {
   this.remoteId = remoteId;
   this.server = remoteId.getAddress();
@@ -777,6 +780,7 @@ public class Client implements AutoCloseable {
 }
   }
   try {
+connectingThread.set(Thread.currentThread());
 if (LOG.isDebugEnabled()) {
   LOG.debug("Connecting to "+server);
 }
@@ -862,6 +866,8 @@ public class Client implements AutoCloseable {
   markClosed(new IOException("Couldn't set up IO streams: " + t, t));
 }
 close();
+  } finally {
+connectingThread.set(null);
   }
 }
 
@@ -1215,6 +1221,13 @@ public class Client implements AutoCloseable {
 notifyAll();
   }
 }
+
+private void interruptConnectingThread() {
+  Thread connThread = connectingThread.get();
+  if (connThread != null) {
+connThread.interrupt();
+  }
+}
 
 /** Close the connection. */
 private synchronized void close() {
@@ -1321,6 +1334,7 @@ public class Client implements AutoCloseable {
 // wake up all connections
 for (Connection conn : connections.values()) {
   conn.interrupt();
+  conn.interruptConnectingThread();
 }
 
 // wait until all connections are closed

http://git-wip-us.apache.org/repos/asf/hadoop/blob/142d878c/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
index a4577f2..cdbaea4 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -1398,6 +1399,50 @@ public class TestIPC {
 assertEquals(Client.getTimeout(config), -1);
   }
 
+  @Test(timeout=6)
+  public void testSetupConnectionShouldNotBlockShutdown() throws Exception {
+// Start server
+SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
+Server server = new TestServer(1, true);
+final InetSocketAddress 

[1/2] hadoop git commit: HADOOP-10219. ipc.Client.setupIOstreams() needs to check for ClientCache.stopClient requested shutdowns. Contributed by Kihwal Lee and Lukas Majercak.

2018-09-04 Thread inigoiri
Repository: hadoop
Updated Branches:
  refs/heads/branch-2 4e342f603 -> 5902c0658
  refs/heads/branch-2.9 809faede9 -> 6ed97eba2


HADOOP-10219. ipc.Client.setupIOstreams() needs to check for 
ClientCache.stopClient requested shutdowns.
Contributed by Kihwal Lee and Lukas Majercak.

(cherry picked from commit 9e96ac666d783376a8cdea9c3cc84098c5bdcb56)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/5902c065
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/5902c065
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/5902c065

Branch: refs/heads/branch-2
Commit: 5902c0658eb7ad7092c2bdeb10c2296015c84c08
Parents: 4e342f6
Author: Steve Loughran 
Authored: Tue Sep 4 16:46:12 2018 +0100
Committer: Inigo Goiri 
Committed: Tue Sep 4 11:58:56 2018 -0700

--
 .../main/java/org/apache/hadoop/ipc/Client.java | 14 ++
 .../java/org/apache/hadoop/ipc/TestIPC.java | 45 
 2 files changed, 59 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/hadoop/blob/5902c065/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
index 533b6ca..2636adb 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
@@ -70,6 +70,7 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID;
 import static org.apache.hadoop.ipc.RpcConstants.PING_CALL_ID;
@@ -440,6 +441,8 @@ public class Client implements AutoCloseable {
 
 private final Object sendRpcRequestLock = new Object();
 
+private AtomicReference connectingThread = new AtomicReference<>();
+
 public Connection(ConnectionId remoteId, int serviceClass) throws 
IOException {
   this.remoteId = remoteId;
   this.server = remoteId.getAddress();
@@ -777,6 +780,7 @@ public class Client implements AutoCloseable {
 }
   }
   try {
+connectingThread.set(Thread.currentThread());
 if (LOG.isDebugEnabled()) {
   LOG.debug("Connecting to "+server);
 }
@@ -862,6 +866,8 @@ public class Client implements AutoCloseable {
   markClosed(new IOException("Couldn't set up IO streams: " + t, t));
 }
 close();
+  } finally {
+connectingThread.set(null);
   }
 }
 
@@ -1215,6 +1221,13 @@ public class Client implements AutoCloseable {
 notifyAll();
   }
 }
+
+private void interruptConnectingThread() {
+  Thread connThread = connectingThread.get();
+  if (connThread != null) {
+connThread.interrupt();
+  }
+}
 
 /** Close the connection. */
 private synchronized void close() {
@@ -1317,6 +1330,7 @@ public class Client implements AutoCloseable {
 // wake up all connections
 for (Connection conn : connections.values()) {
   conn.interrupt();
+  conn.interruptConnectingThread();
 }
 
 // wait until all connections are closed

http://git-wip-us.apache.org/repos/asf/hadoop/blob/5902c065/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
index a6c57fe..95e76f7 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -1398,6 +1399,50 @@ public class TestIPC {
 assertEquals(Client.getTimeout(config), -1);
   }
 
+  @Test(timeout=6)
+  public void testSetupConnectionShouldNotBlockShutdown() throws Exception {
+// Start server
+SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
+Server server = new 

[2/2] hadoop git commit: HADOOP-10219. ipc.Client.setupIOstreams() needs to check for ClientCache.stopClient requested shutdowns. Contributed by Kihwal Lee and Lukas Majercak.

2018-09-04 Thread inigoiri
HADOOP-10219. ipc.Client.setupIOstreams() needs to check for 
ClientCache.stopClient requested shutdowns.
Contributed by Kihwal Lee and Lukas Majercak.

(cherry picked from commit 9e96ac666d783376a8cdea9c3cc84098c5bdcb56)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/6ed97eba
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/6ed97eba
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/6ed97eba

Branch: refs/heads/branch-2.9
Commit: 6ed97eba2237b16295e873beb99379e12f116a6e
Parents: 809faed
Author: Steve Loughran 
Authored: Tue Sep 4 16:46:12 2018 +0100
Committer: Inigo Goiri 
Committed: Tue Sep 4 11:59:23 2018 -0700

--
 .../main/java/org/apache/hadoop/ipc/Client.java | 14 ++
 .../java/org/apache/hadoop/ipc/TestIPC.java | 45 
 2 files changed, 59 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/hadoop/blob/6ed97eba/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
index 533b6ca..2636adb 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
@@ -70,6 +70,7 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID;
 import static org.apache.hadoop.ipc.RpcConstants.PING_CALL_ID;
@@ -440,6 +441,8 @@ public class Client implements AutoCloseable {
 
 private final Object sendRpcRequestLock = new Object();
 
+private AtomicReference connectingThread = new AtomicReference<>();
+
 public Connection(ConnectionId remoteId, int serviceClass) throws 
IOException {
   this.remoteId = remoteId;
   this.server = remoteId.getAddress();
@@ -777,6 +780,7 @@ public class Client implements AutoCloseable {
 }
   }
   try {
+connectingThread.set(Thread.currentThread());
 if (LOG.isDebugEnabled()) {
   LOG.debug("Connecting to "+server);
 }
@@ -862,6 +866,8 @@ public class Client implements AutoCloseable {
   markClosed(new IOException("Couldn't set up IO streams: " + t, t));
 }
 close();
+  } finally {
+connectingThread.set(null);
   }
 }
 
@@ -1215,6 +1221,13 @@ public class Client implements AutoCloseable {
 notifyAll();
   }
 }
+
+private void interruptConnectingThread() {
+  Thread connThread = connectingThread.get();
+  if (connThread != null) {
+connThread.interrupt();
+  }
+}
 
 /** Close the connection. */
 private synchronized void close() {
@@ -1317,6 +1330,7 @@ public class Client implements AutoCloseable {
 // wake up all connections
 for (Connection conn : connections.values()) {
   conn.interrupt();
+  conn.interruptConnectingThread();
 }
 
 // wait until all connections are closed

http://git-wip-us.apache.org/repos/asf/hadoop/blob/6ed97eba/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
index a6c57fe..95e76f7 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -1398,6 +1399,50 @@ public class TestIPC {
 assertEquals(Client.getTimeout(config), -1);
   }
 
+  @Test(timeout=6)
+  public void testSetupConnectionShouldNotBlockShutdown() throws Exception {
+// Start server
+SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
+Server server = new TestServer(1, true);
+final InetSocketAddress addr = NetUtils.getConnectAddress(server);
+
+// Track how many times we retried 

hadoop git commit: HADOOP-10219. ipc.Client.setupIOstreams() needs to check for ClientCache.stopClient requested shutdowns. Contributed by Kihwal Lee and Lukas Majercak.

2018-09-04 Thread stevel
Repository: hadoop
Updated Branches:
  refs/heads/trunk 6e5ffb74d -> 9e96ac666


HADOOP-10219. ipc.Client.setupIOstreams() needs to check for 
ClientCache.stopClient requested shutdowns.
Contributed by Kihwal Lee and Lukas Majercak.


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/9e96ac66
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/9e96ac66
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/9e96ac66

Branch: refs/heads/trunk
Commit: 9e96ac666d783376a8cdea9c3cc84098c5bdcb56
Parents: 6e5ffb7
Author: Steve Loughran 
Authored: Tue Sep 4 16:46:12 2018 +0100
Committer: Steve Loughran 
Committed: Tue Sep 4 16:46:12 2018 +0100

--
 .../main/java/org/apache/hadoop/ipc/Client.java | 14 ++
 .../java/org/apache/hadoop/ipc/TestIPC.java | 45 
 2 files changed, 59 insertions(+)
--


http://git-wip-us.apache.org/repos/asf/hadoop/blob/9e96ac66/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
index e147048..07a2f13 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java
@@ -70,6 +70,7 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID;
 import static org.apache.hadoop.ipc.RpcConstants.PING_CALL_ID;
@@ -439,6 +440,8 @@ public class Client implements AutoCloseable {
 
 private final Object sendRpcRequestLock = new Object();
 
+private AtomicReference connectingThread = new AtomicReference<>();
+
 public Connection(ConnectionId remoteId, int serviceClass) throws 
IOException {
   this.remoteId = remoteId;
   this.server = remoteId.getAddress();
@@ -777,6 +780,7 @@ public class Client implements AutoCloseable {
 }
   }
   try {
+connectingThread.set(Thread.currentThread());
 if (LOG.isDebugEnabled()) {
   LOG.debug("Connecting to "+server);
 }
@@ -862,6 +866,8 @@ public class Client implements AutoCloseable {
   markClosed(new IOException("Couldn't set up IO streams: " + t, t));
 }
 close();
+  } finally {
+connectingThread.set(null);
   }
 }
 
@@ -1215,6 +1221,13 @@ public class Client implements AutoCloseable {
 notifyAll();
   }
 }
+
+private void interruptConnectingThread() {
+  Thread connThread = connectingThread.get();
+  if (connThread != null) {
+connThread.interrupt();
+  }
+}
 
 /** Close the connection. */
 private synchronized void close() {
@@ -1321,6 +1334,7 @@ public class Client implements AutoCloseable {
 // wake up all connections
 for (Connection conn : connections.values()) {
   conn.interrupt();
+  conn.interruptConnectingThread();
 }
 
 // wait until all connections are closed

http://git-wip-us.apache.org/repos/asf/hadoop/blob/9e96ac66/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
--
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
index 84b82e2..19314c1 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -1398,6 +1399,50 @@ public class TestIPC {
 assertEquals(Client.getTimeout(config), -1);
   }
 
+  @Test(timeout=6)
+  public void testSetupConnectionShouldNotBlockShutdown() throws Exception {
+// Start server
+SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
+Server server = new TestServer(1, true);
+final InetSocketAddress addr = NetUtils.getConnectAddress(server);
+
+// Track how many times we