From 55c783bdd4975638b3073da9f666e18e8b9dc92d Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 10 Jul 2018 09:22:02 -0500 Subject: [PATCH 1/2] Watcher: cleanup ensureWatchExists use Previously, the ensureWatchExists was overridable. This commit makes it final so that it cannot be overridden, and cleans up some redundant code in the process. --- .../execution/WatchExecutionContext.java | 2 +- .../execution/ManualExecutionContext.java | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java index 4cdd4bb0e3575..62216ff681e82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java @@ -82,7 +82,7 @@ public Watch watch() { return watch; } - public void ensureWatchExists(CheckedSupplier supplier) throws Exception { + public final void ensureWatchExists(CheckedSupplier supplier) throws Exception { if (watch == null) { watch = supplier.get(); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java index c161b24e85619..3b8c832898172 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.execution; -import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; @@ -29,18 +28,16 @@ public class ManualExecutionContext extends WatchExecutionContext { private final Map actionModes; private final boolean recordExecution; private final boolean knownWatch; - private final Watch watch; ManualExecutionContext(Watch watch, boolean knownWatch, DateTime executionTime, ManualTriggerEvent triggerEvent, TimeValue defaultThrottlePeriod, Input.Result inputResult, Condition.Result conditionResult, - Map actionModes, boolean recordExecution) { + Map actionModes, boolean recordExecution) throws Exception { super(watch.id(), executionTime, triggerEvent, defaultThrottlePeriod); this.actionModes = actionModes; this.recordExecution = recordExecution; this.knownWatch = knownWatch; - this.watch = watch; if (inputResult != null) { onInputResult(inputResult); @@ -64,11 +61,6 @@ public class ManualExecutionContext extends WatchExecutionContext { } } } - } - - // a noop operation, as the watch is already loaded via ctor - @Override - public void ensureWatchExists(CheckedSupplier supplier) throws Exception { super.ensureWatchExists(() -> watch); } @@ -107,11 +99,6 @@ public final boolean recordExecution() { return recordExecution; } - @Override - public Watch watch() { - return watch; - } - public static Builder builder(Watch watch, boolean knownWatch, ManualTriggerEvent event, TimeValue defaultThrottlePeriod) { return new Builder(watch, knownWatch, event, defaultThrottlePeriod); } @@ -173,7 +160,7 @@ public Builder withCondition(Condition.Result conditionResult) { return this; } - public ManualExecutionContext build() { + public ManualExecutionContext build() throws Exception { if (executionTime == null) { executionTime = DateTime.now(DateTimeZone.UTC); } From f6e041a35e65da1f6dff4f39d2d43ce7d025b6e5 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 10 Jul 2018 11:08:43 -0500 Subject: [PATCH 2/2] Move the call to ensure watch to the begin of the ctor --- .../xpack/watcher/execution/ManualExecutionContext.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java index 3b8c832898172..abf1e5aec0da4 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java @@ -39,6 +39,9 @@ public class ManualExecutionContext extends WatchExecutionContext { this.recordExecution = recordExecution; this.knownWatch = knownWatch; + // set the watch early to ensure calls to watch() below succeed. + super.ensureWatchExists(() -> watch); + if (inputResult != null) { onInputResult(inputResult); } @@ -61,7 +64,6 @@ public class ManualExecutionContext extends WatchExecutionContext { } } } - super.ensureWatchExists(() -> watch); } @Override