From 31bf279ec7bdc96eced2b2509a75285ecd586314 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 21 Dec 2016 10:49:33 -0500 Subject: [PATCH] Tell Netty not to be unsafe in transport client Today we ship with default jvm.options for server Elasticsearch that prevents Netty from using some unsafe optimizations. Yet, the settings do nothing for the transport client since it is embedded in other applications that will not read and use those settings. This commit adds these settings for the transport client, and is done so in a way that still enables users to go unsafe if they want to go unsafe (they shouldn't, but the option is there). Relates #22284 --- .../client/PreBuiltTransportClient.java | 27 +++++++++++++++++++ .../client/PreBuiltTransportClientTests.java | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/client/transport/src/main/java/org/elasticsearch/transport/client/PreBuiltTransportClient.java b/client/transport/src/main/java/org/elasticsearch/transport/client/PreBuiltTransportClient.java index ca90723ae82..a173d43a96c 100644 --- a/client/transport/src/main/java/org/elasticsearch/transport/client/PreBuiltTransportClient.java +++ b/client/transport/src/main/java/org/elasticsearch/transport/client/PreBuiltTransportClient.java @@ -22,6 +22,7 @@ package org.elasticsearch.transport.client; import io.netty.util.ThreadDeathWatcher; import io.netty.util.concurrent.GlobalEventExecutor; import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.reindex.ReindexPlugin; @@ -47,6 +48,32 @@ import java.util.concurrent.TimeUnit; @SuppressWarnings({"unchecked","varargs"}) public class PreBuiltTransportClient extends TransportClient { + static { + // initialize Netty system properties before triggering any Netty class loads + initializeNetty(); + } + + /** + * Netty wants to do some unsafe things like use unsafe and replace a private field. This method disables these things by default, but + * can be overridden by setting the corresponding system properties. + */ + @SuppressForbidden(reason = "set system properties to configure Netty") + private static void initializeNetty() { + final String noUnsafeKey = "io.netty.noUnsafe"; + final String noUnsafe = System.getProperty(noUnsafeKey); + if (noUnsafe == null) { + // disable Netty from using unsafe + System.setProperty(noUnsafeKey, Boolean.toString(true)); + } + + final String noKeySetOptimizationKey = "io.netty.noKeySetOptimization"; + final String noKeySetOptimization = System.getProperty(noKeySetOptimizationKey); + if (noKeySetOptimization == null) { + // disable Netty from replacing the selector key set + System.setProperty(noKeySetOptimizationKey, Boolean.toString(true)); + } + } + private static final Collection> PRE_INSTALLED_PLUGINS = Collections.unmodifiableList( Arrays.asList( diff --git a/client/transport/src/test/java/org/elasticsearch/transport/client/PreBuiltTransportClientTests.java b/client/transport/src/test/java/org/elasticsearch/transport/client/PreBuiltTransportClientTests.java index a1d95b68af7..dd628fad31f 100644 --- a/client/transport/src/test/java/org/elasticsearch/transport/client/PreBuiltTransportClientTests.java +++ b/client/transport/src/test/java/org/elasticsearch/transport/client/PreBuiltTransportClientTests.java @@ -41,7 +41,7 @@ public class PreBuiltTransportClientTests extends RandomizedTest { @Test public void testPluginInstalled() { - // TODO: remove when Netty 4.1.5 is upgraded to Netty 4.1.6 including https://github.com/netty/netty/pull/5778 + // TODO: remove when Netty 4.1.6 is upgraded to Netty 4.1.7 including https://github.com/netty/netty/pull/6068 assumeFalse(Constants.JRE_IS_MINIMUM_JAVA9); try (TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)) { Settings settings = client.settings();