geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject [geode] branch develop updated: Revert "[GEODE-5591] If there is a BindException we should always throw. (#2351)"
Date Wed, 05 Sep 2018 17:16:15 GMT
This is an automated email from the ASF dual-hosted git repository.

nnag 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 901da27  Revert "[GEODE-5591] If there is a BindException we should always throw.
(#2351)"
901da27 is described below

commit 901da27f227a8ce2b7d6b681619782a1accd9330
Author: nabarun <nag@cs.wisc.edu>
AuthorDate: Wed Sep 5 09:59:02 2018 -0700

    Revert "[GEODE-5591] If there is a BindException we should always throw. (#2351)"
    
    This reverts commit b6eb8fa
---
 .../geode/internal/cache/wan/WANTestBase.java      | 14 ++++--
 .../cache/wan/misc/WANConfigurationJUnitTest.java  | 15 ++++--
 .../internal/cache/wan/GatewayReceiverImpl.java    | 54 +++++++++++++++++-----
 3 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/geode-wan/src/distributedTest/java/org/apache/geode/internal/cache/wan/WANTestBase.java
b/geode-wan/src/distributedTest/java/org/apache/geode/internal/cache/wan/WANTestBase.java
index a091942..0b1af66 100644
--- a/geode-wan/src/distributedTest/java/org/apache/geode/internal/cache/wan/WANTestBase.java
+++ b/geode-wan/src/distributedTest/java/org/apache/geode/internal/cache/wan/WANTestBase.java
@@ -37,7 +37,6 @@ import static org.apache.geode.distributed.ConfigurationProperties.REMOTE_LOCATO
 import static org.apache.geode.distributed.ConfigurationProperties.START_LOCATOR;
 import static org.apache.geode.test.dunit.Host.getHost;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.hamcrest.Matchers.greaterThan;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -2018,9 +2017,16 @@ public class WANTestBase extends DistributedTestCase {
     fact.setManualStart(true);
     fact.setBindAddress("200.112.204.10");
     GatewayReceiver receiver = fact.create();
-    assertThatThrownBy(receiver::start)
-        .isInstanceOf(GatewayReceiverException.class)
-        .hasMessageContaining("No available free port found in the given range");
+    try {
+      receiver.start();
+      fail("Expected GatewayReceiver Exception");
+    } catch (GatewayReceiverException gRE) {
+      logger.debug("Got the GatewayReceiverException", gRE);
+      assertTrue(gRE.getMessage().contains("Failed to create server socket on"));
+    } catch (IOException e) {
+      e.printStackTrace();
+      fail("Test " + test.getName() + " failed to start GatewayReceiver on port " + port);
+    }
   }
 
   public static int createReceiverWithSSL(int locPort) {
diff --git a/geode-wan/src/integrationTest/java/org/apache/geode/internal/cache/wan/misc/WANConfigurationJUnitTest.java
b/geode-wan/src/integrationTest/java/org/apache/geode/internal/cache/wan/misc/WANConfigurationJUnitTest.java
index 038b759..edcd55f 100644
--- a/geode-wan/src/integrationTest/java/org/apache/geode/internal/cache/wan/misc/WANConfigurationJUnitTest.java
+++ b/geode-wan/src/integrationTest/java/org/apache/geode/internal/cache/wan/misc/WANConfigurationJUnitTest.java
@@ -437,18 +437,25 @@ public class WANConfigurationJUnitTest {
   public void test_ValidateGatewayReceiverAttributes_WrongBindAddress() {
     cache = new CacheFactory().set(MCAST_PORT, "0").create();
     GatewayReceiverFactory fact = cache.createGatewayReceiverFactory();
-    fact.setStartPort(50505);
+    fact.setStartPort(50504);
     fact.setMaximumTimeBetweenPings(1000);
     fact.setSocketBufferSize(4000);
-    fact.setEndPort(50505);
+    fact.setEndPort(70707);
     fact.setManualStart(true);
     fact.setBindAddress("200.112.204.10");
     GatewayTransportFilter myStreamFilter1 = new MyGatewayTransportFilter1();
     fact.addGatewayTransportFilter(myStreamFilter1);
 
-
     GatewayReceiver receiver = fact.create();
-    assertThatThrownBy(() -> receiver.start()).isInstanceOf(GatewayReceiverException.class);
+    try {
+      receiver.start();
+      fail("Expected GatewayReceiverException");
+    } catch (GatewayReceiverException gRE) {
+      assertTrue(gRE.getMessage().contains("Failed to create server socket on"));
+    } catch (IOException e) {
+      e.printStackTrace();
+      fail("The test failed with IOException");
+    }
   }
 
   @Test
diff --git a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
index cd27029..0f0fc63 100644
--- a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
+++ b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
@@ -15,6 +15,8 @@
 package org.apache.geode.internal.cache.wan;
 
 import java.io.IOException;
+import java.net.BindException;
+import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.List;
@@ -26,6 +28,7 @@ import org.apache.geode.cache.wan.GatewayReceiver;
 import org.apache.geode.cache.wan.GatewayTransportFilter;
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
 import org.apache.geode.distributed.internal.ResourceEvent;
+import org.apache.geode.internal.AvailablePort;
 import org.apache.geode.internal.cache.CacheServerImpl;
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.internal.i18n.LocalizedStrings;
@@ -143,9 +146,10 @@ public class GatewayReceiverImpl implements GatewayReceiver {
       logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_IS_ALREADY_RUNNING));
       return;
     }
-
-    for (int port = this.startPort; port <= this.endPort; port++) {
-      receiver.setPort(port);
+    boolean started = false;
+    this.port = getPortToStart();
+    while (!started && this.port != -1) {
+      receiver.setPort(this.port);
       receiver.setSocketBufferSize(socketBufferSize);
       receiver.setMaximumTimeBetweenPings(timeBetPings);
       if (hostnameForSenders != null && !hostnameForSenders.isEmpty()) {
@@ -156,19 +160,33 @@ public class GatewayReceiverImpl implements GatewayReceiver {
       ((CacheServerImpl) receiver).setGatewayTransportFilter(this.filters);
       try {
         receiver.start();
-        this.port = port;
-        break;
-      } catch (IOException e) {
-        if (port == this.endPort) {
-          throw new GatewayReceiverException("No available free port found in the given range
(" +
-              this.startPort +
-              "-" +
-              this.endPort +
-              ")", e);
+        started = true;
+      } catch (BindException be) {
+        if (be.getCause() != null
+            && be.getCause().getMessage().contains("assign requested address")) {
+          throw new GatewayReceiverException(
+              LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
+                  .toLocalizedString(new Object[] {bindAdd, this.port}));
+        }
+        // ignore as this port might have been used by other threads.
+        logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_Address_Already_In_Use,
+            this.port));
+        this.port = getPortToStart();
+      } catch (SocketException se) {
+        if (se.getMessage().contains("Address already in use")) {
+          logger.warn(LocalizedMessage
+              .create(LocalizedStrings.GatewayReceiver_Address_Already_In_Use, this.port));
+          this.port = getPortToStart();
+
+        } else {
+          throw se;
         }
       }
 
     }
+    if (!started) {
+      throw new IllegalStateException("No available free port found in the given range.");
+    }
     logger
         .info(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_STARTED_ON_PORT, this.port));
 
@@ -177,6 +195,18 @@ public class GatewayReceiverImpl implements GatewayReceiver {
 
   }
 
+  private int getPortToStart() {
+    // choose a random port from the given port range
+    int rPort;
+    if (this.startPort == this.endPort) {
+      rPort = this.startPort;
+    } else {
+      rPort = AvailablePort.getRandomAvailablePortInRange(this.startPort, this.endPort,
+          AvailablePort.SOCKET);
+    }
+    return rPort;
+  }
+
   public void stop() {
     if (!isRunning()) {
       throw new GatewayReceiverException(


Mime
View raw message