You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kh...@apache.org on 2018/06/21 16:04:47 UTC

[geode] branch develop updated: GEODE-5333: Destroy failed connection in ConnectionConnector (#2072)

This is an automated email from the ASF dual-hosted git repository.

khowe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 13087e2  GEODE-5333: Destroy failed connection in ConnectionConnector (#2072)
13087e2 is described below

commit 13087e2a23983ea43f5406512e9eef1fae1876c0
Author: Kenneth Howe <kh...@pivotal.io>
AuthorDate: Thu Jun 21 09:04:41 2018 -0700

    GEODE-5333: Destroy failed connection in ConnectionConnector (#2072)
    
    Added unit test for new functionality in ConnectionConnector
    
    Refactored ConnectionFactoryImpl, ConnectionConnector, and
    ClientSideHandshakeImpl improve testability of ConnectionConnector
---
 .../cache/client/internal/ConnectionConnector.java |  63 ++++++++----
 .../client/internal/ConnectionFactoryImpl.java     |  23 +++--
 .../client/internal/ConnectionConnectorTest.java   | 114 +++++++++++++++++++++
 3 files changed, 168 insertions(+), 32 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionConnector.java b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionConnector.java
index 2ef35da..dda185d 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionConnector.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionConnector.java
@@ -16,6 +16,8 @@ package org.apache.geode.cache.client.internal;
 
 import java.io.IOException;
 
+import org.apache.logging.log4j.Logger;
+
 import org.apache.geode.CancelCriterion;
 import org.apache.geode.cache.wan.GatewaySender;
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
@@ -23,12 +25,12 @@ import org.apache.geode.distributed.internal.ServerLocation;
 import org.apache.geode.internal.cache.tier.ClientSideHandshake;
 import org.apache.geode.internal.cache.tier.CommunicationMode;
 import org.apache.geode.internal.cache.tier.sockets.CacheClientUpdater;
-import org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID;
+import org.apache.geode.internal.logging.LogService;
 import org.apache.geode.internal.net.SocketCreator;
-import org.apache.geode.internal.net.SocketCreatorFactory;
-import org.apache.geode.internal.security.SecurableCommunicationChannel;
 
 public class ConnectionConnector {
+  private static final Logger logger = LogService.getLogger();
+
   private final ClientSideHandshakeImpl handshake;
   private final int socketBufferSize;
   private final int handshakeTimeout;
@@ -41,12 +43,11 @@ public class ConnectionConnector {
   private GatewaySender gatewaySender;
 
   public ConnectionConnector(EndpointManager endpointManager, InternalDistributedSystem sys,
-      int socketBufferSize, int handshakeTimeout, int readTimeout, ClientProxyMembershipID proxyId,
-      CancelCriterion cancelCriterion, boolean usedByGateway, GatewaySender sender,
-      boolean multiuserSecureMode) {
+      int socketBufferSize, int handshakeTimeout, int readTimeout, CancelCriterion cancelCriterion,
+      boolean usedByGateway, GatewaySender sender, SocketCreator socketCreator,
+      ClientSideHandshakeImpl handshake) {
 
-    this.handshake =
-        new ClientSideHandshakeImpl(proxyId, sys, sys.getSecurityService(), multiuserSecureMode);
+    this.handshake = handshake;
     this.handshake.setClientReadTimeout(readTimeout);
     this.endpointManager = endpointManager;
     this.ds = sys;
@@ -56,27 +57,47 @@ public class ConnectionConnector {
     this.usedByGateway = usedByGateway;
     this.gatewaySender = sender;
     this.cancelCriterion = cancelCriterion;
-    if (this.usedByGateway || (this.gatewaySender != null)) {
-      this.socketCreator =
-          SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.GATEWAY);
+    this.socketCreator = socketCreator;
+    if (this.socketCreator != null && (this.usedByGateway || (this.gatewaySender != null))) {
       if (sender != null && !sender.getGatewayTransportFilters().isEmpty()) {
         this.socketCreator.initializeTransportFilterClientSocketFactory(sender);
       }
-    } else {
-      // If configured use SSL properties for cache-server
-      this.socketCreator =
-          SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.SERVER);
     }
   }
 
   public ConnectionImpl connectClientToServer(ServerLocation location, boolean forQueue)
       throws IOException {
-    ConnectionImpl connection = new ConnectionImpl(this.ds, this.cancelCriterion);
-    ClientSideHandshake connHandShake = new ClientSideHandshakeImpl(handshake);
-    connection.connect(endpointManager, location, connHandShake, socketBufferSize, handshakeTimeout,
-        readTimeout, getCommMode(forQueue), this.gatewaySender, this.socketCreator);
-    connection.setHandshake(connHandShake);
-    return connection;
+    ConnectionImpl connection = null;
+    boolean initialized = false;
+    try {
+      connection = getConnection(this.ds, this.cancelCriterion);
+      ClientSideHandshake connHandShake = getClientSideHandshake(handshake);
+      connection.connect(endpointManager, location, connHandShake, socketBufferSize,
+          handshakeTimeout, readTimeout, getCommMode(forQueue), this.gatewaySender,
+          this.socketCreator);
+      connection.setHandshake(connHandShake);
+      initialized = true;
+      return connection;
+    } finally {
+      if (!initialized && connection != null) {
+        if (logger.isDebugEnabled()) {
+          logger.debug("Destroy failed connection to {}", location);
+        }
+        destroyConnection(connection);
+      }
+    }
+  }
+
+  void destroyConnection(ConnectionImpl connection) {
+    connection.destroy();
+  }
+
+  ConnectionImpl getConnection(InternalDistributedSystem ds, CancelCriterion cancelCriterion) {
+    return new ConnectionImpl(ds, cancelCriterion);
+  }
+
+  ClientSideHandshake getClientSideHandshake(ClientSideHandshakeImpl handshake) {
+    return new ClientSideHandshakeImpl(handshake);
   }
 
   public CacheClientUpdater connectServerToClient(Endpoint endpoint, QueueManager qManager,
diff --git a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionFactoryImpl.java b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionFactoryImpl.java
index e3abb31..635aee8 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionFactoryImpl.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ConnectionFactoryImpl.java
@@ -34,6 +34,8 @@ import org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID;
 import org.apache.geode.internal.i18n.LocalizedStrings;
 import org.apache.geode.internal.logging.LogService;
 import org.apache.geode.internal.logging.log4j.LocalizedMessage;
+import org.apache.geode.internal.net.SocketCreatorFactory;
+import org.apache.geode.internal.security.SecurableCommunicationChannel;
 import org.apache.geode.security.GemFireSecurityException;
 
 /**
@@ -55,6 +57,7 @@ public class ConnectionFactoryImpl implements ConnectionFactory {
   private final CancelCriterion cancelCriterion;
   private final ConnectionConnector connectionConnector;
 
+
   /**
    * Test hook for client version support
    *
@@ -69,7 +72,13 @@ public class ConnectionFactoryImpl implements ConnectionFactory {
       GatewaySender sender, long pingInterval, boolean multiuserSecureMode, PoolImpl pool) {
     this(
         new ConnectionConnector(endpointManager, sys, socketBufferSize, handshakeTimeout,
-            readTimeout, proxyId, cancelCriterion, usedByGateway, sender, multiuserSecureMode),
+            readTimeout, cancelCriterion, usedByGateway, sender,
+            (usedByGateway || sender != null) ? SocketCreatorFactory
+                .getSocketCreatorForComponent(SecurableCommunicationChannel.GATEWAY)
+                : SocketCreatorFactory
+                    .getSocketCreatorForComponent(SecurableCommunicationChannel.SERVER),
+            new ClientSideHandshakeImpl(proxyId, sys, sys.getSecurityService(),
+                multiuserSecureMode)),
         source, pingInterval, pool, cancelCriterion);
   }
 
@@ -101,16 +110,8 @@ public class ConnectionFactoryImpl implements ConnectionFactory {
       initialized = true;
       failureTracker.reset();
       authenticateIfRequired(connection);
-    } catch (GemFireConfigException e) {
-      throw e;
-    } catch (CancelException e) {
-      // propagate this up, don't retry
-      throw e;
-    } catch (GemFireSecurityException e) {
-      // propagate this up, don't retry
-      throw e;
-    } catch (GatewayConfigurationException e) {
-      // propagate this up, don't retry
+    } catch (GemFireConfigException | CancelException | GemFireSecurityException
+        | GatewayConfigurationException e) {
       throw e;
     } catch (ServerRefusedConnectionException src) {
       // propagate this up, don't retry
diff --git a/geode-core/src/test/java/org/apache/geode/cache/client/internal/ConnectionConnectorTest.java b/geode-core/src/test/java/org/apache/geode/cache/client/internal/ConnectionConnectorTest.java
new file mode 100644
index 0000000..fb2ed90
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/cache/client/internal/ConnectionConnectorTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.geode.cache.client.internal;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.CancelCriterion;
+import org.apache.geode.distributed.PoolCancelledException;
+import org.apache.geode.distributed.internal.InternalDistributedSystem;
+import org.apache.geode.distributed.internal.ServerLocation;
+import org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID;
+import org.apache.geode.internal.net.SocketCreator;
+import org.apache.geode.security.GemFireSecurityException;
+import org.apache.geode.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class ConnectionConnectorTest {
+  private CancelCriterion cancelCriterion;
+  private EndpointManager endpointManager;
+  private InternalDistributedSystem ds;
+  private ClientProxyMembershipID proxyId;
+  private ClientSideHandshakeImpl handshake;
+  private SocketCreator socketCreator;
+  private ConnectionImpl connection;
+
+
+  @Before
+  public void setUp() throws Exception {
+    cancelCriterion = mock(CancelCriterion.class);
+    endpointManager = mock(EndpointManager.class);
+    ds = mock(InternalDistributedSystem.class);
+    proxyId = mock(ClientProxyMembershipID.class);
+    handshake = mock(ClientSideHandshakeImpl.class);
+    socketCreator = mock(SocketCreator.class);
+    connection = mock(ConnectionImpl.class);
+
+    // mocks don't seem to work well with CancelCriterion so let's create a real one
+    cancelCriterion = new CancelCriterion() {
+      @Override
+      public String cancelInProgress() {
+        return "shutting down for test";
+      }
+
+      @Override
+      public RuntimeException generateCancelledException(Throwable throwable) {
+        return new PoolCancelledException(cancelInProgress(), throwable);
+      }
+    };
+
+  }
+
+  @After
+  public void tearDown() throws Exception {}
+
+  @Test(expected = GemFireSecurityException.class)
+  public void failedConnectionIsDestroyed() throws IOException {
+
+    ConnectionConnector spyConnector =
+        spy(new ConnectionConnector(endpointManager, ds, 0, 0, 0, cancelCriterion, false,
+            null, socketCreator, handshake));
+    doReturn(connection).when(spyConnector).getConnection(ds, cancelCriterion);
+    doReturn(handshake).when(spyConnector).getClientSideHandshake(handshake);
+
+    when(connection.connect(any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), any(), any()))
+        .thenThrow(new GemFireSecurityException("Expected exception"));
+    try {
+      spyConnector.connectClientToServer(mock(ServerLocation.class), false);
+    } finally {
+      verify(spyConnector).destroyConnection(any());
+    }
+  }
+
+  @Test
+  public void successfulConnectionIsNotDestroyed() throws IOException {
+
+    ConnectionConnector spyConnector =
+        spy(new ConnectionConnector(endpointManager, ds, 0, 0, 0, cancelCriterion, false,
+            null, socketCreator, handshake));
+    doReturn(connection).when(spyConnector).getConnection(ds, cancelCriterion);
+    doReturn(handshake).when(spyConnector).getClientSideHandshake(handshake);
+
+    try {
+      spyConnector.connectClientToServer(mock(ServerLocation.class), false);
+    } finally {
+      verify(spyConnector, times(0)).destroyConnection(any());
+    }
+  }
+}