diff --git a/src/main/java/com/actiontech/dble/DbleServer.java b/src/main/java/com/actiontech/dble/DbleServer.java index a8838d476..1f58613d6 100644 --- a/src/main/java/com/actiontech/dble/DbleServer.java +++ b/src/main/java/com/actiontech/dble/DbleServer.java @@ -305,7 +305,7 @@ public final class DbleServer { } private void checkJdkVersion() { - DbleSocketOptions.clean(); + DbleSocketOptions.getInstance().clean(); if (DbleSocketOptions.osName().contains("Windows")) { LOGGER.warn("current system version does not support the tcpKeepIdle,tcpKeepInterval,tcpKeepCount parameter."); return; diff --git a/src/main/java/com/actiontech/dble/config/loader/SystemConfigLoader.java b/src/main/java/com/actiontech/dble/config/loader/SystemConfigLoader.java index 56e9a25e8..3c2d83511 100644 --- a/src/main/java/com/actiontech/dble/config/loader/SystemConfigLoader.java +++ b/src/main/java/com/actiontech/dble/config/loader/SystemConfigLoader.java @@ -12,6 +12,7 @@ import com.actiontech.dble.config.model.SystemConfig; import com.actiontech.dble.config.util.ParameterMapping; import com.actiontech.dble.config.util.StartProblemReporter; import com.actiontech.dble.memory.unsafe.Platform; +import com.actiontech.dble.net.DbleSocketOptions; import com.actiontech.dble.server.status.SqlDumpLog; import com.actiontech.dble.services.manager.handler.WriteDynamicBootstrap; import com.actiontech.dble.util.ResourceUtil; @@ -209,7 +210,7 @@ public final class SystemConfigLoader { } - public static void verifyOtherParam() { + public static void verifyOtherParam() throws IOException { // other SqlDumpLog.getInstance().verify(); @@ -221,5 +222,6 @@ public final class SystemConfigLoader { if (home == null) { StartProblemReporter.getInstance().addError("homePath is not set."); } + DbleSocketOptions.getInstance().check(); } } diff --git a/src/main/java/com/actiontech/dble/config/model/SystemConfig.java b/src/main/java/com/actiontech/dble/config/model/SystemConfig.java index ef56f8673..524096e94 100644 --- a/src/main/java/com/actiontech/dble/config/model/SystemConfig.java +++ b/src/main/java/com/actiontech/dble/config/model/SystemConfig.java @@ -263,9 +263,9 @@ public final class SystemConfig { private int enableMemoryBufferMonitorRecordPool = 1; //tcp - private int tcpKeepIdle = 30; - private int tcpKeepInterval = 10; - private int tcpKeepCount = 3; + private int tcpKeepIdle = DbleSocketOptions.getInstance().getTcpKeepIdle(); + private int tcpKeepInterval = DbleSocketOptions.getInstance().getTcpKeepInterval(); + private int tcpKeepCount = DbleSocketOptions.getInstance().getTcpKeepCount(); //maximum number of rows in select result set in multi-table update @@ -1904,8 +1904,8 @@ public final class SystemConfig { return tcpKeepIdle; } - public void setTcpKeepIdle(int tcpKeepIdle) throws IOException { - if (tcpKeepIdle > 0 && DbleSocketOptions.check(DbleSocketOptions.TCP_KEEP_IDLE, tcpKeepIdle)) { + public void setTcpKeepIdle(int tcpKeepIdle) { + if (tcpKeepIdle > 0) { this.tcpKeepIdle = tcpKeepIdle; } else { problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepIdle", tcpKeepIdle, this.tcpKeepIdle)); @@ -1916,8 +1916,8 @@ public final class SystemConfig { return tcpKeepInterval; } - public void setTcpKeepInterval(int tcpKeepInterval) throws IOException { - if (tcpKeepInterval > 0 && DbleSocketOptions.check(DbleSocketOptions.TCP_KEEP_INTERVAL, tcpKeepInterval)) { + public void setTcpKeepInterval(int tcpKeepInterval) { + if (tcpKeepInterval > 0) { this.tcpKeepInterval = tcpKeepInterval; } else { problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepInterval", tcpKeepInterval, this.tcpKeepInterval)); @@ -1928,8 +1928,8 @@ public final class SystemConfig { return tcpKeepCount; } - public void setTcpKeepCount(int tcpKeepCount) throws IOException { - if (tcpKeepCount > 0 && DbleSocketOptions.check(DbleSocketOptions.TCP_KEEP_COUNT, tcpKeepCount)) { + public void setTcpKeepCount(int tcpKeepCount) { + if (tcpKeepCount > 0) { this.tcpKeepCount = tcpKeepCount; } else { problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepCount", tcpKeepCount, this.tcpKeepCount)); diff --git a/src/main/java/com/actiontech/dble/net/DbleSocketOptions.java b/src/main/java/com/actiontech/dble/net/DbleSocketOptions.java index 4047a6f83..34f87b3df 100644 --- a/src/main/java/com/actiontech/dble/net/DbleSocketOptions.java +++ b/src/main/java/com/actiontech/dble/net/DbleSocketOptions.java @@ -1,6 +1,8 @@ package com.actiontech.dble.net; +import com.actiontech.dble.config.ProblemReporter; import com.actiontech.dble.config.model.SystemConfig; +import com.actiontech.dble.config.util.StartProblemReporter; import com.actiontech.dble.util.CompareUtil; import com.actiontech.dble.util.StringUtil; import org.slf4j.Logger; @@ -17,6 +19,11 @@ import java.util.Set; public final class DbleSocketOptions { private static final Logger LOGGER = LoggerFactory.getLogger("DbleSocketOptions"); + private final ProblemReporter problemReporter = StartProblemReporter.getInstance(); + private static final String WARNING_FORMAT = "Property [ %s ] '%s' in bootstrap.cnf is illegal, you may need use the default value %s replaced"; + private static final DbleSocketOptions INSTANCE = new DbleSocketOptions(); + + private static final boolean KEEP_ALIVE_OPT_SUPPORTED; // https://bugs.openjdk.org/browse/JDK-8194298 @@ -27,6 +34,10 @@ public final class DbleSocketOptions { public static final String TCP_KEEP_COUNT = "TCP_KEEPCOUNT"; private static NetworkChannel networkChannel = null; + private int tcpKeepIdle = 30; + private int tcpKeepInterval = 10; + private int tcpKeepCount = 3; + private DbleSocketOptions() { } @@ -35,6 +46,18 @@ public final class DbleSocketOptions { KEEP_ALIVE_OPT_SUPPORTED = keepAliveOptSupported(); } + public void check() throws IOException { + SystemConfig instance = SystemConfig.getInstance(); + if (!checkHelp(DbleSocketOptions.TCP_KEEP_INTERVAL, instance.getTcpKeepInterval())) { + problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepInterval", instance.getTcpKeepInterval(), getTcpKeepInterval())); + } + if (!checkHelp(DbleSocketOptions.TCP_KEEP_IDLE, instance.getTcpKeepIdle())) { + problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepIdle", instance.getTcpKeepIdle(), getTcpKeepIdle())); + } + if (!checkHelp(DbleSocketOptions.TCP_KEEP_COUNT, instance.getTcpKeepCount())) { + problemReporter.warn(String.format(WARNING_FORMAT, "tcpKeepCount", instance.getTcpKeepCount(), getTcpKeepCount())); + } + } /** * did not added the note for upper-bound because values are @@ -46,7 +69,7 @@ public final class DbleSocketOptions { * @param value * @throws IOException */ - public static boolean check(String socketName, int value) throws IOException { + private boolean checkHelp(String socketName, int value) throws IOException { if (KEEP_ALIVE_OPT_SUPPORTED) { if (Objects.isNull(networkChannel)) { networkChannel = SocketChannel.open(); @@ -73,12 +96,12 @@ public final class DbleSocketOptions { * @throws IOException * @since https://bugs.openjdk.org/browse/JDK-8194298 */ - public static void setKeepAliveOptions(NetworkChannel channel) throws IOException { + public void setKeepAliveOptions(NetworkChannel channel) throws IOException { if (KEEP_ALIVE_OPT_SUPPORTED) { SystemConfig instance = SystemConfig.getInstance(); - int tcpKeepIdle = instance.getTcpKeepIdle(); - int tcpKeepInterval = instance.getTcpKeepInterval(); - int tcpKeepCount = instance.getTcpKeepCount(); + int curTcpKeepIdle = instance.getTcpKeepIdle(); + int curTcpKeepInterval = instance.getTcpKeepInterval(); + int curTcpKeepCount = instance.getTcpKeepCount(); Set> socketOptions = channel.supportedOptions(); //Compile compatibility SocketOption socket; @@ -87,15 +110,15 @@ public final class DbleSocketOptions { switch (socketOption.name()) { case TCP_KEEP_IDLE: socket = (SocketOption) socketOption; - channel.setOption(socket, tcpKeepIdle); + channel.setOption(socket, curTcpKeepIdle); break; case TCP_KEEP_INTERVAL: socket = (SocketOption) socketOption; - channel.setOption(socket, tcpKeepInterval); + channel.setOption(socket, curTcpKeepInterval); break; case TCP_KEEP_COUNT: socket = (SocketOption) socketOption; - channel.setOption(socket, tcpKeepCount); + channel.setOption(socket, curTcpKeepCount); break; default: break; @@ -117,7 +140,7 @@ public final class DbleSocketOptions { } } - public static void clean() { + public void clean() { if (Objects.nonNull(networkChannel)) { try { networkChannel.close(); @@ -135,5 +158,20 @@ public final class DbleSocketOptions { return KEEP_ALIVE_OPT_SUPPORTED; } + public int getTcpKeepIdle() { + return tcpKeepIdle; + } + + public int getTcpKeepInterval() { + return tcpKeepInterval; + } + + public int getTcpKeepCount() { + return tcpKeepCount; + } + + public static DbleSocketOptions getInstance() { + return INSTANCE; + } } diff --git a/src/main/java/com/actiontech/dble/net/connection/AbstractConnection.java b/src/main/java/com/actiontech/dble/net/connection/AbstractConnection.java index 59e16bc4d..b00af6191 100644 --- a/src/main/java/com/actiontech/dble/net/connection/AbstractConnection.java +++ b/src/main/java/com/actiontech/dble/net/connection/AbstractConnection.java @@ -430,7 +430,7 @@ public abstract class AbstractConnection implements Connection { channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); - DbleSocketOptions.setKeepAliveOptions(channel); + DbleSocketOptions.getInstance().setKeepAliveOptions(channel); this.setReadBufferChunk(soRcvBuf); }