Skip to content

Commit 05b46d4

Browse files
committed
Set specific keepalive options by default on supported platforms
1 parent 7f5daaa commit 05b46d4

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"Default TCP keepalive settings":
3+
- do:
4+
cluster.get_settings:
5+
include_defaults: true
6+
7+
- match: { defaults.network.tcp.keep_idle: "60" }
8+
- match: { defaults.network.tcp.keep_interval: "10" }
9+
- match: { defaults.network.tcp.keep_count: "3" }

docs/reference/modules/transport.asciidoc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,22 @@ example above:
8888
determines the time in seconds that a connection must be idle before
8989
starting to send TCP keepalive probes.
9090
Only applicable on Linux and Mac, and requires JDK 11 or newer.
91-
Defaults to -1, which does not set this option at the socket level, but
92-
uses default system configuration instead.
91+
Defaults to 60 on applicable configurations, and -1 otherwise, which does
92+
not set this option at the socket level, but uses the default system
93+
configuration instead.
9394
* `tcp.keep_interval`: Configures the `TCP_KEEPINTVL` option for this socket,
9495
which determines the time in seconds between sending TCP keepalive probes.
9596
Only applicable on Linux and Mac, and requires JDK 11 or newer.
96-
Defaults to -1, which does not set this option at the socket level, but
97-
uses default system configuration instead.
97+
Defaults to 10 on applicable configurations, and -1 otherwise, which does
98+
not set this option at the socket level, but uses the default system
99+
configuration instead.
98100
* `tcp.keep_count`: Configures the `TCP_KEEPCNT` option for this socket, which
99101
determines the number of unacknowledged TCP keepalive probes that may be
100102
sent on a connection before it is dropped.
101103
Only applicable on Linux and Mac, and requires JDK 11 or newer.
102-
Defaults to -1, which does not set this option at the socket level, but
103-
uses default system configuration instead.
104+
Defaults to 3 on applicable configurations, and -1 otherwise, which does
105+
not set this option at the socket level, but uses the default system
106+
configuration instead.
104107
* `tcp.reuse_address`: Configures the `SO_REUSEADDR` option for this socket
105108
* `tcp.send_buffer_size`: Configures the send buffer size of the socket
106109
* `tcp.receive_buffer_size`: Configures the receive buffer size of the socket

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,23 @@ private Bootstrap createClientBootstrap(SharedGroupFactory.SharedGroup sharedGro
149149
bootstrap.option(ChannelOption.TCP_NODELAY, TransportSettings.TCP_NO_DELAY.get(settings));
150150
bootstrap.option(ChannelOption.SO_KEEPALIVE, TransportSettings.TCP_KEEP_ALIVE.get(settings));
151151
if (TransportSettings.TCP_KEEP_ALIVE.get(settings)) {
152-
// Netty logs a warning if it can't set the option, so try this only on supported platforms
153-
if (IOUtils.LINUX || IOUtils.MAC_OS_X) {
154-
if (TransportSettings.TCP_KEEP_IDLE.get(settings) >= 0) {
155-
final SocketOption<Integer> keepIdleOption = NetUtils.getTcpKeepIdleSocketOptionOrNull();
156-
if (keepIdleOption != null) {
157-
bootstrap.option(NioChannelOption.of(keepIdleOption), TransportSettings.TCP_KEEP_IDLE.get(settings));
158-
}
152+
// Note that Netty logs a warning if it can't set the option
153+
if (TransportSettings.TCP_KEEP_IDLE.get(settings) >= 0) {
154+
final SocketOption<Integer> keepIdleOption = NetUtils.getTcpKeepIdleSocketOptionOrNull();
155+
if (keepIdleOption != null) {
156+
bootstrap.option(NioChannelOption.of(keepIdleOption), TransportSettings.TCP_KEEP_IDLE.get(settings));
159157
}
160-
if (TransportSettings.TCP_KEEP_INTERVAL.get(settings) >= 0) {
161-
final SocketOption<Integer> keepIntervalOption = NetUtils.getTcpKeepIntervalSocketOptionOrNull();
162-
if (keepIntervalOption != null) {
163-
bootstrap.option(NioChannelOption.of(keepIntervalOption), TransportSettings.TCP_KEEP_INTERVAL.get(settings));
164-
}
158+
}
159+
if (TransportSettings.TCP_KEEP_INTERVAL.get(settings) >= 0) {
160+
final SocketOption<Integer> keepIntervalOption = NetUtils.getTcpKeepIntervalSocketOptionOrNull();
161+
if (keepIntervalOption != null) {
162+
bootstrap.option(NioChannelOption.of(keepIntervalOption), TransportSettings.TCP_KEEP_INTERVAL.get(settings));
165163
}
166-
if (TransportSettings.TCP_KEEP_COUNT.get(settings) >= 0) {
167-
final SocketOption<Integer> keepCountOption = NetUtils.getTcpKeepCountSocketOptionOrNull();
168-
if (keepCountOption != null) {
169-
bootstrap.option(NioChannelOption.of(keepCountOption), TransportSettings.TCP_KEEP_COUNT.get(settings));
170-
}
164+
}
165+
if (TransportSettings.TCP_KEEP_COUNT.get(settings) >= 0) {
166+
final SocketOption<Integer> keepCountOption = NetUtils.getTcpKeepCountSocketOptionOrNull();
167+
if (keepCountOption != null) {
168+
bootstrap.option(NioChannelOption.of(keepCountOption), TransportSettings.TCP_KEEP_COUNT.get(settings));
171169
}
172170
}
173171
}

server/src/main/java/org/elasticsearch/common/network/NetworkService.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
package org.elasticsearch.common.network;
2121

22+
import org.elasticsearch.bootstrap.JavaVersion;
23+
import org.elasticsearch.common.Booleans;
2224
import org.elasticsearch.common.settings.Setting;
2325
import org.elasticsearch.common.settings.Setting.Property;
2426
import org.elasticsearch.common.unit.ByteSizeValue;
27+
import org.elasticsearch.core.internal.io.IOUtils;
2528

2629
import java.io.IOException;
2730
import java.net.InetAddress;
@@ -50,12 +53,16 @@ public final class NetworkService {
5053
Setting.boolSetting("network.tcp.no_delay", true, Property.NodeScope);
5154
public static final Setting<Boolean> TCP_KEEP_ALIVE =
5255
Setting.boolSetting("network.tcp.keep_alive", true, Property.NodeScope);
56+
public static final boolean SET_EXTRA_KEEP_ALIVE_OPTIONS =
57+
Booleans.parseBoolean(System.getProperty("es.network.tcp.extra_keep_alive_options", "false")) == false &&
58+
(IOUtils.LINUX || IOUtils.MAC_OS_X) &&
59+
JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0;
5360
public static final Setting<Integer> TCP_KEEP_IDLE =
54-
Setting.intSetting("network.tcp.keep_idle", -1, -1, Property.NodeScope);
61+
Setting.intSetting("network.tcp.keep_idle", SET_EXTRA_KEEP_ALIVE_OPTIONS ? 60 : -1, -1, Property.NodeScope);
5562
public static final Setting<Integer> TCP_KEEP_INTERVAL =
56-
Setting.intSetting("network.tcp.keep_interval", -1, -1, Property.NodeScope);
63+
Setting.intSetting("network.tcp.keep_interval", SET_EXTRA_KEEP_ALIVE_OPTIONS ? 10 : -1, -1, Property.NodeScope);
5764
public static final Setting<Integer> TCP_KEEP_COUNT =
58-
Setting.intSetting("network.tcp.keep_count", -1, -1, Property.NodeScope);
65+
Setting.intSetting("network.tcp.keep_count", SET_EXTRA_KEEP_ALIVE_OPTIONS ? 3 : -1, -1, Property.NodeScope);
5966
public static final Setting<Boolean> TCP_REUSE_ADDRESS =
6067
Setting.boolSetting("network.tcp.reuse_address", NetworkUtils.defaultReuseAddress(), Property.NodeScope);
6168
public static final Setting<ByteSizeValue> TCP_SEND_BUFFER_SIZE =

server/src/main/java/org/elasticsearch/transport/TransportSettings.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
*/
1919
package org.elasticsearch.transport;
2020

21+
import org.elasticsearch.bootstrap.JavaVersion;
2122
import org.elasticsearch.common.network.NetworkService;
2223
import org.elasticsearch.common.settings.Setting;
2324
import org.elasticsearch.common.settings.Settings;
2425
import org.elasticsearch.common.unit.ByteSizeValue;
2526
import org.elasticsearch.common.unit.TimeValue;
27+
import org.elasticsearch.core.internal.io.IOUtils;
2628

2729
import java.util.Arrays;
2830
import java.util.List;
2931
import java.util.concurrent.TimeUnit;
3032
import java.util.function.Function;
3133

3234
import static java.util.Collections.emptyList;
35+
import static org.elasticsearch.common.network.NetworkService.SET_EXTRA_KEEP_ALIVE_OPTIONS;
3336
import static org.elasticsearch.common.settings.Setting.affixKeySetting;
3437
import static org.elasticsearch.common.settings.Setting.boolSetting;
3538
import static org.elasticsearch.common.settings.Setting.intSetting;
@@ -81,20 +84,20 @@ public final class TransportSettings {
8184
affixKeySetting("transport.profiles.", "tcp.keep_alive",
8285
key -> boolSetting(key, TCP_KEEP_ALIVE, Setting.Property.NodeScope));
8386
public static final Setting<Integer> TCP_KEEP_IDLE =
84-
intSetting("transport.tcp.keep_idle", NetworkService.TCP_KEEP_IDLE, -1, Setting.Property.NodeScope);
87+
intSetting("transport.tcp.keep_idle", NetworkService.TCP_KEEP_IDLE, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 60 : -1, Setting.Property.NodeScope);
8588
public static final Setting.AffixSetting<Integer> TCP_KEEP_IDLE_PROFILE =
8689
affixKeySetting("transport.profiles.", "tcp.keep_idle",
87-
key -> intSetting(key, TCP_KEEP_IDLE, -1, Setting.Property.NodeScope));
90+
key -> intSetting(key, TCP_KEEP_IDLE, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 60 : -1, Setting.Property.NodeScope));
8891
public static final Setting<Integer> TCP_KEEP_INTERVAL =
89-
intSetting("transport.tcp.keep_interval", NetworkService.TCP_KEEP_INTERVAL, -1, Setting.Property.NodeScope);
92+
intSetting("transport.tcp.keep_interval", NetworkService.TCP_KEEP_INTERVAL, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 10 : -1, Setting.Property.NodeScope);
9093
public static final Setting.AffixSetting<Integer> TCP_KEEP_INTERVAL_PROFILE =
9194
affixKeySetting("transport.profiles.", "tcp.keep_interval",
92-
key -> intSetting(key, TCP_KEEP_INTERVAL, -1, Setting.Property.NodeScope));
95+
key -> intSetting(key, TCP_KEEP_INTERVAL, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 10 : -1, Setting.Property.NodeScope));
9396
public static final Setting<Integer> TCP_KEEP_COUNT =
94-
intSetting("transport.tcp.keep_count", NetworkService.TCP_KEEP_COUNT, -1, Setting.Property.NodeScope);
97+
intSetting("transport.tcp.keep_count", NetworkService.TCP_KEEP_COUNT, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 3 : -1, Setting.Property.NodeScope);
9598
public static final Setting.AffixSetting<Integer> TCP_KEEP_COUNT_PROFILE =
9699
affixKeySetting("transport.profiles.", "tcp.keep_count",
97-
key -> intSetting(key, TCP_KEEP_COUNT, -1, Setting.Property.NodeScope));
100+
key -> intSetting(key, TCP_KEEP_COUNT, SET_EXTRA_KEEP_ALIVE_OPTIONS ? 3 : -1, Setting.Property.NodeScope));
98101
public static final Setting<Boolean> TCP_REUSE_ADDRESS =
99102
boolSetting("transport.tcp.reuse_address", NetworkService.TCP_REUSE_ADDRESS, Setting.Property.NodeScope);
100103
public static final Setting.AffixSetting<Boolean> TCP_REUSE_ADDRESS_PROFILE =

0 commit comments

Comments
 (0)