Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Introduced Failsafe library for retry completable futures

Rich Turner3d5c4b8eff0c
diff --git a/agent/build.gradle b/agent/build.gradle
index f374f6450..95b174da0 100644
--- a/agent/build.gradle
+++ b/agent/build.gradle
@@ -39,6 +39,8 @@ dependencies {
// Conflicts with asm version bundled with GWT
exclude group: "org.ow2.asm"
}
+
+ implementation "dev.failsafe:failsafe:$failsafeVersion"
}
jar {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
index 55f115efc..ad1e17e4a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
@@ -19,18 +19,20 @@
*/
package org.openremote.agent.protocol.io;
+import dev.failsafe.Failsafe;
+import dev.failsafe.RetryPolicy;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import org.openremote.agent.protocol.udp.UDPIOClient;
import org.openremote.agent.protocol.websocket.WebsocketIOClient;
import org.openremote.container.Container;
-import org.openremote.model.util.Retry;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.syslog.SyslogCategory;
import javax.validation.constraints.NotNull;
import java.net.SocketAddress;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -169,7 +171,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
protected Bootstrap bootstrap;
protected EventLoopGroup workerGroup;
protected ScheduledExecutorService executorService;
- protected Retry connectRetry;
+ protected CompletableFuture<Void> connectRetry;
protected boolean permanentError;
protected Supplier<ChannelHandler[]> encoderDecoderProvider;
@@ -213,28 +215,28 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
}
protected void scheduleDoConnect() {
- connectRetry = new Retry("Connect to '" + getClientUri() + "'", executorService, () -> {
- boolean success = false;
- try {
- success = doConnect().get();
- if (success) {
- onConnectionStatusChanged(ConnectionStatus.CONNECTED);
- } else {
- // Cleanup resources ready for next connection attempt
- doDisconnect();
- }
- } catch (Exception e) {
- LOG.log(Level.INFO, "An exception was thrown during connection attempt", e);
+ RetryPolicy<Object> retryPolicy = RetryPolicy.builder()
+ .withJitter(Duration.ofMillis(Math.max(50, RECONNECT_DELAY_JITTER_MILLIS)))
+ .withBackoff(Duration.ofMillis(Math.max(50, RECONNECT_DELAY_INITIAL_MILLIS)), Duration.ofMillis(Math.max(100, RECONNECT_DELAY_MAX_MILLIS)))
+ .build();
+
+
+ connectRetry = Failsafe.with(retryPolicy).with(executorService).runAsyncExecution((execution) -> {
+ boolean success = doConnect().get();
+
+ if (connectRetry.isCancelled()) {
+ execution.recordResult(null);
+ return;
+ }
+
+ if (success) {
+ onConnectionStatusChanged(ConnectionStatus.CONNECTED);
+ execution.recordResult(null);
+ } else {
+ // Cleanup resources ready for next connection attempt
+ doDisconnect();
}
- return success;
- })
- .setSuccessCallback(() -> this.connectRetry = null)
- .setLogger(LOG)
- .setInitialDelay(RECONNECT_DELAY_INITIAL_MILLIS)
- .setMaxDelay(RECONNECT_DELAY_MAX_MILLIS)
- .setJitterMargin(RECONNECT_DELAY_JITTER_MILLIS);
-
- connectRetry.run();
+ });
}
protected Future<Boolean> doConnect() {
@@ -306,21 +308,26 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
@Override
public void disconnect() {
synchronized (this) {
- if (connectionStatus == ConnectionStatus.DISCONNECTED) {
+ if (connectionStatus != ConnectionStatus.CONNECTED && connectionStatus != ConnectionStatus.CONNECTING) {
LOG.finest("Already disconnected: " + getClientUri());
return;
}
LOG.finest("Disconnecting IO client: " + getClientUri());
- onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTING);
}
if (connectRetry != null) {
- connectRetry.cancel(true);
+ connectRetry.cancel(false);
+ try {
+ connectRetry.get();
+ } catch (InterruptedException | ExecutionException e) {
+ LOG.log(Level.INFO, "Failed to wait for connection retry future: " + getClientUri());
+ }
connectRetry = null;
}
-
doDisconnect();
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
}
protected void doDisconnect() {
diff --git a/gradle.properties b/gradle.properties
index 335c14ed0..6728624b8 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -17,9 +17,9 @@ jacksonVersion = 2.13.1
groovyVersion = 3.0.9
groovySandboxVersion = 1.19
sqliteVersion = 3.34.0
-hibernateVersion = 5.4.25.Final
-hibernateValidatorVersion = 6.1.6.Final
-postgresJdbcVersion = 42.2.18
+hibernateVersion = 5.6.5.Final
+hibernateValidatorVersion = 6.2.1.Final
+postgresJdbcVersion = 42.3.1
hikaricpVersion = 3.4.5
junitVersion = 4.13.2
spockVersion = 2.0-groovy-3.0
@@ -49,10 +49,11 @@ moquetteVersion = 0.15
californiumCoreVersion = 2.1.0
californiumScandiumVersion = 2.1.0
zwaveVersion = 3.4.0-SNAPSHOT
-hibernateTypesVersion = 2.10.0
+hibernateTypesVersion = 2.14.0
reflectionsVersion=0.9.12
swaggerVersion=2.1.9
jsonSchemaVersion=4.18.0
hiveMQClientVersion=1.2.2
bluetoothVersion = 0.39
nashornVersion=15.3
+failsafeVersion=3.1.0
diff --git a/model/src/main/java/org/openremote/model/util/Retry.java b/model/src/main/java/org/openremote/model/util/Retry.java
deleted file mode 100644
index f25c53d28..000000000
--- a/model/src/main/java/org/openremote/model/util/Retry.java
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Copyright 2020, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.model.util;
-
-import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-import java.util.function.Supplier;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-public class Retry {
-
- protected String name;
- protected ScheduledExecutorService executorService;
- protected TimeUnit timeUnit = TimeUnit.MILLISECONDS;
- protected Logger logger;
- protected long initialDelay;
- protected long maxDelay = 5*60000L;
- protected long backoffMultiplier = 2L;
- protected long jitterMargin = 10000L;
- protected int maxRetries = Integer.MAX_VALUE;
- protected boolean running;
- protected int retries;
- Supplier<Boolean> task;
- Future<?> future;
- Runnable successCallback;
- Runnable failureCallback;
-
- public Retry(String name, ScheduledExecutorService executorService, Supplier<Boolean> task) {
- this.name = name;
- this.executorService = executorService;
- this.task = task;
- }
-
- public Retry setTimeUnit(TimeUnit timeUnit) {
- this.timeUnit = timeUnit;
- return this;
- }
-
- public Retry setLogger(Logger logger) {
- this.logger = logger;
- return this;
- }
-
- public Retry setInitialDelay(long initialDelay) {
- this.initialDelay = initialDelay;
- return this;
- }
-
- public Retry setMaxDelay(long maxDelay) {
- this.maxDelay = maxDelay;
- return this;
- }
-
- public Retry setBackoffMultiplier(long backoffMultiplier) {
- this.backoffMultiplier = backoffMultiplier;
- return this;
- }
-
- public Retry setJitterMargin(long jitterMargin) {
- this.jitterMargin = jitterMargin;
- return this;
- }
-
- public Retry setMaxRetries(int maxRetries) {
- this.maxRetries = maxRetries;
- return this;
- }
-
- public Retry setSuccessCallback(Runnable successCallback) {
- this.successCallback = successCallback;
- return this;
- }
-
- public Retry setFailureCallback(Runnable failureCallback) {
- this.failureCallback = failureCallback;
- return this;
- }
-
- public synchronized void run() {
- if (running) {
- throw new IllegalStateException(name + ": already running");
- }
-
- retries = 1;
- running = true;
-
- if (initialDelay > 0) {
- future = executorService.schedule(this::doAttempt, initialDelay, timeUnit);
- } else {
- future = executorService.submit(this::doAttempt);
- }
- }
-
- public synchronized void cancel(boolean mayInterrupt) {
- if (!running) {
- return;
- }
-
- running = false;
- future.cancel(mayInterrupt);
- }
-
- protected void doAttempt() {
- boolean success = false;
- boolean retry = retries < maxRetries;
-
- try {
- log(Level.INFO, name + ": running, attempt=" + retries, null);
- success = task.get();
- } catch (Exception e) {
- log(Level.INFO, name + ": threw an exception", e);
- }
-
- if (!running) {
- return;
- }
-
- if (success) {
- if (successCallback != null) {
- successCallback.run();
- }
- return;
- }
-
- if (!retry) {
- if (failureCallback != null) {
- failureCallback.run();
- }
- return;
- }
-
- long adjustedMax = Math.max(maxDelay - jitterMargin, jitterMargin);
- long delay = Math.max(initialDelay, Math.min(initialDelay * (long)Math.pow(2, retries), adjustedMax));
- delay += Math.random()*jitterMargin;
- retries++;
-
- log(Level.INFO, name + ": scheduling retry in " + delay + " " + timeUnit.name(), null);
- future = executorService.schedule(this::doAttempt, delay, timeUnit);
- }
-
- protected void log(Level level, String log, Throwable e) {
- if (logger != null) {
- logger.log(level, log, e);
- }
- }
-}