From 4a37a5e4a4c022101556f9d4a6b4e28985b0c82f Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 6 Jun 2017 13:14:43 +0300 Subject: [PATCH] dnsMonitoring turned on by default for SingleServerConfig --- .../redisson/config/SingleServerConfig.java | 10 +++--- .../connection/SingleConnectionManager.java | 36 +++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/redisson/src/main/java/org/redisson/config/SingleServerConfig.java b/redisson/src/main/java/org/redisson/config/SingleServerConfig.java index 758c35422..03bc5a4dc 100644 --- a/redisson/src/main/java/org/redisson/config/SingleServerConfig.java +++ b/redisson/src/main/java/org/redisson/config/SingleServerConfig.java @@ -64,7 +64,7 @@ public class SingleServerConfig extends BaseConfig { * NB: applications must ensure the JVM DNS cache TTL is low enough to support this. * e.g., http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-jvm-ttl.html */ - private boolean dnsMonitoring = false; + private boolean dnsMonitoring = true; /** * Interval in milliseconds to check DNS @@ -144,8 +144,10 @@ public class SingleServerConfig extends BaseConfig { /** * Monitoring of the endpoint address for DNS changes. - * - * Default is false + *

+ * Applications must ensure the JVM DNS cache TTL is low enough to support this + *

+ * Default is true * * @param dnsMonitoring flag * @return config @@ -161,7 +163,7 @@ public class SingleServerConfig extends BaseConfig { /** * Interval in milliseconds to check the endpoint DNS if {@link #isDnsMonitoring()} is true. * - * Default is 5000 + * Default is 5000 * * @param dnsMonitoringInterval time * @return config diff --git a/redisson/src/main/java/org/redisson/connection/SingleConnectionManager.java b/redisson/src/main/java/org/redisson/connection/SingleConnectionManager.java index 71027b9e0..83d7c1e5c 100644 --- a/redisson/src/main/java/org/redisson/connection/SingleConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/SingleConnectionManager.java @@ -93,27 +93,33 @@ public class SingleConnectionManager extends MasterSlaveConnectionManager { } private void monitorDnsChange(final SingleServerConfig cfg) { - monitorFuture = GlobalEventExecutor.INSTANCE.scheduleWithFixedDelay(new Runnable() { + monitorFuture = GlobalEventExecutor.INSTANCE.schedule(new Runnable() { @Override public void run() { - try { - InetAddress master = currentMaster.get(); - InetAddress now = InetAddress.getByName(cfg.getAddress().getHost()); - if (!now.getHostAddress().equals(master.getHostAddress())) { - log.info("Detected DNS change. {} has changed from {} to {}", cfg.getAddress().getHost(), master.getHostAddress(), now.getHostAddress()); - if (currentMaster.compareAndSet(master, now)) { - changeMaster(singleSlotRange.getStartSlot(), cfg.getAddress()); - log.info("Master has been changed"); + // As InetAddress.getByName call is blocking. Method should be run in dedicated thread + getExecutor().execute(new Runnable() { + @Override + public void run() { + try { + InetAddress master = currentMaster.get(); + InetAddress now = InetAddress.getByName(cfg.getAddress().getHost()); + if (!now.getHostAddress().equals(master.getHostAddress())) { + log.info("Detected DNS change. {} has changed from {} to {}", cfg.getAddress().getHost(), master.getHostAddress(), now.getHostAddress()); + if (currentMaster.compareAndSet(master, now)) { + changeMaster(singleSlotRange.getStartSlot(), cfg.getAddress()); + log.info("Master has been changed"); + } + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } finally { + monitorDnsChange(cfg); } } - - } catch (Exception e) { - log.error(e.getMessage(), e); - } - + }); } - }, cfg.getDnsMonitoringInterval(), cfg.getDnsMonitoringInterval(), TimeUnit.MILLISECONDS); + }, cfg.getDnsMonitoringInterval(), TimeUnit.MILLISECONDS); } @Override