From ccd5ce6597e9abe06722e56ee328df3546793db8 Mon Sep 17 00:00:00 2001 From: HantingZHANG Date: Thu, 12 Mar 2020 17:39:37 +0100 Subject: [PATCH 1/5] Refactor of the RMI Socket timeout fix --- .gitignore | 1 + .../datadog/jmxfetch/RemoteConnection.java | 3 +++ .../org/datadog/jmxfetch/SocketFactory.java | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/main/java/org/datadog/jmxfetch/SocketFactory.java diff --git a/.gitignore b/.gitignore index 8bdb411fa..5e7460450 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ target/* # jenv .java_version +.factorypath diff --git a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java index 99073f57b..03f6bec28 100644 --- a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java +++ b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java @@ -10,6 +10,7 @@ import javax.management.remote.JMXServiceURL; import javax.management.remote.rmi.RMIConnectorServer; import javax.rmi.ssl.SslRMIClientSocketFactory; +import java.rmi.server.RMISocketFactory; @Slf4j public class RemoteConnection extends Connection { @@ -89,6 +90,8 @@ public RemoteConnection(Map connectionParams) throws IOException // Set an RMI timeout so we don't get stuck waiting for a bean to report a value System.setProperty("sun.rmi.transport.tcp.responseTimeout", rmiTimeout); + RMISocketFactory.setSocketFactory(new SocketFactory(Integer.parseInt(rmiTimeout))); + createConnection(); } diff --git a/src/main/java/org/datadog/jmxfetch/SocketFactory.java b/src/main/java/org/datadog/jmxfetch/SocketFactory.java new file mode 100644 index 000000000..a5bf3360a --- /dev/null +++ b/src/main/java/org/datadog/jmxfetch/SocketFactory.java @@ -0,0 +1,27 @@ +package org.datadog.jmxfetch; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.rmi.server.RMISocketFactory; + +public class SocketFactory extends RMISocketFactory { + private final int timeout; + + SocketFactory(final int timeout) { + this.timeout = timeout; + } + + @Override + public Socket createSocket(final String host, final int port) throws IOException { + final Socket socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), timeout); + return socket; + } + + @Override + public ServerSocket createServerSocket(final int port) throws IOException { + return new ServerSocket(port); + } +} \ No newline at end of file From 8bb067d25b4987e4546576fa3e1ebaef3e68743d Mon Sep 17 00:00:00 2001 From: HantingZHANG Date: Thu, 12 Mar 2020 19:09:29 +0100 Subject: [PATCH 2/5] Change the import order --- src/main/java/org/datadog/jmxfetch/RemoteConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java index 03f6bec28..0ed1df79f 100644 --- a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java +++ b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java @@ -4,13 +4,13 @@ import java.io.IOException; import java.net.MalformedURLException; +import java.rmi.server.RMISocketFactory; import java.util.HashMap; import java.util.Map; import javax.management.remote.JMXConnector; import javax.management.remote.JMXServiceURL; import javax.management.remote.rmi.RMIConnectorServer; import javax.rmi.ssl.SslRMIClientSocketFactory; -import java.rmi.server.RMISocketFactory; @Slf4j public class RemoteConnection extends Connection { From c17eb48e4570b5127aa8ca8747736d5f8f6b8bfd Mon Sep 17 00:00:00 2001 From: Pierre Rognant Date: Wed, 25 Mar 2020 12:01:46 +0100 Subject: [PATCH 3/5] minor adjustments --- .../org/datadog/jmxfetch/RemoteConnection.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java index 0ed1df79f..35ccf1473 100644 --- a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java +++ b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java @@ -1,10 +1,11 @@ package org.datadog.jmxfetch; +import static java.rmi.server.RMISocketFactory.setSocketFactory; + import lombok.extern.slf4j.Slf4j; import java.io.IOException; import java.net.MalformedURLException; -import java.rmi.server.RMISocketFactory; import java.util.HashMap; import java.util.Map; import javax.management.remote.JMXConnector; @@ -28,6 +29,8 @@ public class RemoteConnection extends Connection { private static final String KEY_STORE_PASSWORD_KEY = "key_store_password"; private static final String DEFAULT_RMI_RESPONSE_TIMEOUT = "15000"; // Match the collection period default + // Match the default RMI connection timeout value + private static final Integer DEFAULT_RMI_CONNECTION_TIMEOUT = Integer.valueOf(0); /** RemoteConnection constructor for specified remote connection parameters. */ public RemoteConnection(Map connectionParams) throws IOException { @@ -90,7 +93,16 @@ public RemoteConnection(Map connectionParams) throws IOException // Set an RMI timeout so we don't get stuck waiting for a bean to report a value System.setProperty("sun.rmi.transport.tcp.responseTimeout", rmiTimeout); - RMISocketFactory.setSocketFactory(new SocketFactory(Integer.parseInt(rmiTimeout))); + Integer rmiConnectionTimeout = DEFAULT_RMI_CONNECTION_TIMEOUT; + try { + rmiConnectionTimeout = (Integer) connectionParams.get("rmi_connection_timeout"); + } catch (final ClassCastException e) { + rmiConnectionTimeout = Integer.parseInt((String) connectionParams.get("rmi_connection_timeout")); + } + if (rmiConnectionTimeout == null) { + rmiConnectionTimeout = DEFAULT_RMI_CONNECTION_TIMEOUT; + } + setSocketFactory(new SocketFactory(rmiConnectionTimeout)); createConnection(); } From 61e174a3a6366148ac552dfba0c0eb6fba74ee80 Mon Sep 17 00:00:00 2001 From: Pierre Rognant Date: Wed, 25 Mar 2020 12:31:21 +0100 Subject: [PATCH 4/5] coding style compliance --- src/main/java/org/datadog/jmxfetch/RemoteConnection.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java index 35ccf1473..156477856 100644 --- a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java +++ b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java @@ -97,7 +97,8 @@ public RemoteConnection(Map connectionParams) throws IOException try { rmiConnectionTimeout = (Integer) connectionParams.get("rmi_connection_timeout"); } catch (final ClassCastException e) { - rmiConnectionTimeout = Integer.parseInt((String) connectionParams.get("rmi_connection_timeout")); + rmiConnectionTimeout = + Integer.parseInt((String) connectionParams.get("rmi_connection_timeout")); } if (rmiConnectionTimeout == null) { rmiConnectionTimeout = DEFAULT_RMI_CONNECTION_TIMEOUT; From 801df7558b55be28b8a1eab000c8f6608fa41f4f Mon Sep 17 00:00:00 2001 From: Pierre Rognant Date: Wed, 25 Mar 2020 15:48:17 +0100 Subject: [PATCH 5/5] change timeout value 0s->15s --- src/main/java/org/datadog/jmxfetch/RemoteConnection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java index 156477856..c1bccb336 100644 --- a/src/main/java/org/datadog/jmxfetch/RemoteConnection.java +++ b/src/main/java/org/datadog/jmxfetch/RemoteConnection.java @@ -29,8 +29,8 @@ public class RemoteConnection extends Connection { private static final String KEY_STORE_PASSWORD_KEY = "key_store_password"; private static final String DEFAULT_RMI_RESPONSE_TIMEOUT = "15000"; // Match the collection period default - // Match the default RMI connection timeout value - private static final Integer DEFAULT_RMI_CONNECTION_TIMEOUT = Integer.valueOf(0); + // The default behaviour is to wait indefinitely, we change that and only wait for 15sec. + private static final Integer DEFAULT_RMI_CONNECTION_TIMEOUT = Integer.valueOf(15000); /** RemoteConnection constructor for specified remote connection parameters. */ public RemoteConnection(Map connectionParams) throws IOException {