This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit e124a5bb90e080d62910b8feb090d992ba656d5f Author: Alex Heneveld <[email protected]> AuthorDate: Mon Mar 6 22:48:13 2023 +0000 improve errors and resolving for some immediate execution cases --- .../spi/dsl/methods/BrooklynDslCommon.java | 2 +- .../brooklyn/spi/dsl/methods/DslComponent.java | 3 +- .../brooklyn/util/core/task/ImmediateSupplier.java | 6 ++++ .../brooklyn/util/core/task/ValueResolver.java | 34 +++++++++++++++------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java index 1f7944100c..0468930717 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java @@ -816,7 +816,7 @@ public class BrooklynDslCommon { } return Maybe.of(result); } catch (ImmediateValueNotAvailableException e) { - return ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(); + return Maybe.absent(e); } } diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java index 030eb7d8d2..6838013374 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java @@ -712,7 +712,8 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> implements targetSensor = Sensors.newSensor(Object.class, sensorNameS); } Object result = targetEntity.sensors().get(targetSensor); - return JavaGroovyEquivalents.groovyTruth(result) ? Maybe.of(result) : ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(); + AttributeSensor<?> ts2 = targetSensor; + return JavaGroovyEquivalents.groovyTruth(result) ? Maybe.of(result) : ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(() -> "Sensor '"+ts2+"' on "+targetEntity+" not immediately available"); } @SuppressWarnings("unchecked") diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/ImmediateSupplier.java b/core/src/main/java/org/apache/brooklyn/util/core/task/ImmediateSupplier.java index 522c3610dd..458fec6933 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/task/ImmediateSupplier.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/task/ImmediateSupplier.java @@ -66,6 +66,12 @@ public interface ImmediateSupplier<T> extends Supplier<T> { public static <T> Maybe<T> newAbsentWithExceptionSupplier() { return Maybe.Absent.changeExceptionSupplier(Maybe.<T>absent(), ImmediateValueNotAvailableException.class); } + public static <T> Maybe<T> newAbsentWithExceptionSupplier(String message) { + return Maybe.Absent.absent(() -> new ImmediateValueNotAvailableException(message)); + } + public static <T> Maybe<T> newAbsentWithExceptionSupplier(Supplier<String> message) { + return Maybe.Absent.absent(() -> new ImmediateValueNotAvailableException(message.get())); + } public static <T> Maybe<T> newAbsentWrapping(String message, Maybe<?> inner) { return Maybe.absent(new ImmediateValueNotAvailableException(message, Maybe.getException(inner))); } diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java b/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java index 137c9ff757..03e9ede5cb 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/task/ValueResolver.java @@ -498,7 +498,7 @@ public class ValueResolver<T> implements DeferredSupplier<T>, Iterable<Maybe<Obj } } catch (ImmediateSupplier.ImmediateValueNotAvailableException e) { // definitively not available - return ImmediateSupplier.ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(); + return Maybe.absent(e); } } @@ -529,18 +529,32 @@ public class ValueResolver<T> implements DeferredSupplier<T>, Iterable<Maybe<Obj //including tasks, above if (!vfuture.isDone()) { + Maybe vm; if (isEvaluatingImmediately()) { - return ImmediateSupplier.ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(); - } + if (vfuture instanceof Task<?> && ((Task<?>)vfuture).isSubmitted()) { + try { + // not sure if this ever gives a better result than the below - DST's still aren't supported - but might do for some tasks + vm = exec.getImmediately(vfuture); + } catch (ImmediateSupplier.ImmediateValueNotAvailableException e) { + return Maybe.absent(e); + } catch (Exception e) { + return Maybe.absent(() -> new ImmediateSupplier.ImmediateValueNotAvailableException("Future " + vfuture + " cannot be resolved in immediate context", e)); + } + } else { + return ImmediateSupplier.ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier(() -> "Future " + vfuture + " cannot be resolved in immediate context"); + } + } else { + Callable<Maybe> callable = new Callable<Maybe>() { + @Override + public Maybe call() throws Exception { + return Durations.get(vfuture, timer); + } + }; - Callable<Maybe> callable = new Callable<Maybe>() { - @Override - public Maybe call() throws Exception { - return Durations.get(vfuture, timer); - } }; + String description = getDescription(); + vm = Tasks.withBlockingDetails("Waiting for " + description, callable); + } - String description = getDescription(); - Maybe vm = Tasks.withBlockingDetails("Waiting for "+description, callable); if (vm.isAbsent()) return vm; v = vm.get();
