From a2bc1aefc88e3ff155e45d66bec8e4f4474a8ce8 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 22 Jan 2014 17:24:16 +0100 Subject: [PATCH] Make configuration push timeout configurable This patch adds the option to specify a the pusher timeout, such that it can be made either shorter or longer than the default 2 minutes. Another improvement is the use of System.nanoTime(), making the deadline work in face of calender time changing. Change-Id: Ia8b596c7cd285695696fa5074f7d1f23c2b8b560 Signed-off-by: Robert Varga --- .../netconf/persist/impl/ConfigPusher.java | 34 ++++++++++--------- .../impl/osgi/ConfigPersisterActivator.java | 7 ++++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java index a47d6dd842..e45b092d08 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java @@ -35,6 +35,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; @Immutable public class ConfigPusher { @@ -45,20 +46,24 @@ public class ConfigPusher { private final InetSocketAddress address; private final EventLoopGroup nettyThreadgroup; - - public static final long DEFAULT_TIMEOUT = 120000L;// 120 seconds until netconf must be stable - private final long timeout; + // Default timeout for netconf becoming stable + public static final long DEFAULT_TIMEOUT = TimeUnit.MINUTES.toNanos(2); + private final int delayMillis = 5000; + private final long timeoutNanos; public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup) { - this(address, DEFAULT_TIMEOUT, nettyThreadgroup); + this(address, nettyThreadgroup, DEFAULT_TIMEOUT); + } + @Deprecated + public ConfigPusher(InetSocketAddress address, long timeoutMillis, EventLoopGroup nettyThreadgroup) { + this(address, nettyThreadgroup, TimeUnit.MILLISECONDS.toNanos(timeoutMillis)); } - public ConfigPusher(InetSocketAddress address, long timeout, EventLoopGroup nettyThreadgroup) { + public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup, long timeoutNanos) { this.address = address; - this.timeout = timeout; - this.nettyThreadgroup = nettyThreadgroup; + this.timeoutNanos = timeoutNanos; } public synchronized NetconfClient init(List configs) throws InterruptedException { @@ -121,28 +126,25 @@ public class ConfigPusher { // TODO think about moving capability subset check to netconf client // could be utilized by integration tests - long pollingStart = System.currentTimeMillis(); - int delay = 5000; - + final long pollingStart = System.nanoTime(); + final long deadline = pollingStart + timeoutNanos; int attempt = 0; - long deadline = pollingStart + timeout; - String additionalHeader = NetconfMessageAdditionalHeader.toString("unknown", address.getAddress().getHostAddress(), Integer.toString(address.getPort()), "tcp", Optional.of("persister")); Set latestCapabilities = new HashSet<>(); - while (System.currentTimeMillis() < deadline) { + while (System.nanoTime() < deadline) { attempt++; NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, additionalHeader); NetconfClient netconfClient; try { - netconfClient = new NetconfClient(this.toString(), address, delay, netconfClientDispatcher); + netconfClient = new NetconfClient(this.toString(), address, delayMillis, netconfClientDispatcher); } catch (IllegalStateException e) { logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e); netconfClientDispatcher.close(); - Thread.sleep(delay); + Thread.sleep(delayMillis); continue; } latestCapabilities = netconfClient.getCapabilities(); @@ -153,7 +155,7 @@ public class ConfigPusher { } logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, latestCapabilities); Util.closeClientAndDispatcher(netconfClient); - Thread.sleep(delay); + Thread.sleep(delayMillis); } Set allNotFound = new HashSet<>(expectedCaps); allNotFound.removeAll(latestCapabilities); diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java index 3c901c3f59..857912021b 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java @@ -25,6 +25,7 @@ import javax.management.MBeanServer; import java.lang.management.ManagementFactory; import java.net.InetSocketAddress; import java.util.regex.Pattern; +import java.util.concurrent.TimeUnit; public class ConfigPersisterActivator implements BundleActivator { @@ -33,6 +34,8 @@ public class ConfigPersisterActivator implements BundleActivator { private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex"; + private static final String PUSH_TIMEOUT = "pushTimeout"; + public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister"; public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass"; @@ -59,6 +62,10 @@ public class ConfigPersisterActivator implements BundleActivator { } else { regex = DEFAULT_IGNORED_REGEX; } + + String timeoutProperty = propertiesProvider.getProperty(PUSH_TIMEOUT); + long timeout = timeoutProperty == null ? ConfigPusher.DEFAULT_TIMEOUT : TimeUnit.SECONDS.toNanos(Integer.valueOf(timeoutProperty)); + final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex); nettyThreadgroup = new NioEventLoopGroup(); -- 2.36.6