Browse Source

Ignore failures to set socket options on Mac (#44355)

Brings some temporary relief for test failures until #41071 is addressed.
Yannick Welsch 6 years ago
parent
commit
855d27e374

+ 0 - 0
server/src/main/java/org/elasticsearch/common/CheckedRunnable.java → libs/core/src/main/java/org/elasticsearch/common/CheckedRunnable.java


+ 21 - 5
libs/nio/src/main/java/org/elasticsearch/nio/ChannelFactory.java

@@ -19,10 +19,13 @@
 
 package org.elasticsearch.nio;
 
+import org.elasticsearch.common.CheckedRunnable;
+
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.net.InetSocketAddress;
+import java.net.SocketException;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.security.AccessController;
@@ -206,17 +209,30 @@ public abstract class ChannelFactory<ServerSocket extends NioServerSocketChannel
             return serverSocketChannel;
         }
 
+        private static final boolean MAC_OS_X = System.getProperty("os.name").startsWith("Mac OS X");
+
+        private static void setSocketOption(CheckedRunnable<SocketException> runnable) throws SocketException {
+            try {
+                runnable.run();
+            } catch (SocketException e) {
+                if (MAC_OS_X == false) {
+                    // ignore on Mac, see https://github.com/elastic/elasticsearch/issues/41071
+                    throw e;
+                }
+            }
+        }
+
         private void configureSocketChannel(SocketChannel channel) throws IOException {
             channel.configureBlocking(false);
             java.net.Socket socket = channel.socket();
-            socket.setTcpNoDelay(tcpNoDelay);
-            socket.setKeepAlive(tcpKeepAlive);
-            socket.setReuseAddress(tcpReusedAddress);
+            setSocketOption(() -> socket.setTcpNoDelay(tcpNoDelay));
+            setSocketOption(() -> socket.setKeepAlive(tcpKeepAlive));
+            setSocketOption(() -> socket.setReuseAddress(tcpReusedAddress));
             if (tcpSendBufferSize > 0) {
-                socket.setSendBufferSize(tcpSendBufferSize);
+                setSocketOption(() -> socket.setSendBufferSize(tcpSendBufferSize));
             }
             if (tcpReceiveBufferSize > 0) {
-                socket.setSendBufferSize(tcpReceiveBufferSize);
+                setSocketOption(() -> socket.setSendBufferSize(tcpReceiveBufferSize));
             }
         }