Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1784 15a967c4b -> 76a88db89 (forced update)
TINKERPOP-1761: Cancel script evaluation timeout in when script evaluation finished. Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/2a8c92f2 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/2a8c92f2 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/2a8c92f2 Branch: refs/heads/TINKERPOP-1784 Commit: 2a8c92f245aa28b66c616e62230bfd3972e7e5b9 Parents: 60a34d1 Author: Konstantin Mueller <[email protected]> Authored: Mon Sep 11 11:25:12 2017 +0200 Committer: Konstantin Mueller <[email protected]> Committed: Mon Sep 11 11:25:12 2017 +0200 ---------------------------------------------------------------------- CHANGELOG.asciidoc | 1 + .../gremlin/groovy/engine/GremlinExecutor.java | 14 +++++++++++++- .../gremlin/groovy/engine/GremlinExecutorTest.java | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2a8c92f2/CHANGELOG.asciidoc ---------------------------------------------------------------------- diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index ff0948a..fe1da7a 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -34,6 +34,7 @@ TinkerPop 3.2.7 (Release Date: NOT OFFICIALLY RELEASED YET) * Fixed a bug in `Neo4jGremlinPlugin` that prevented it from loading properly in the `GremlinPythonScriptEngine`. * Fixed a bug in `ComputerVerificationStrategy` where child traversals were being analyzed prior to compilation. * Fixed a bug that prevented Gremlin from ordering lists and streams made of mixed number types. +* Cancel script evaluation timeout in `GremlinExecutor` when script evaluation finished. [[release-3-2-6]] TinkerPop 3.2.6 (Release Date: August 21, 2017) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2a8c92f2/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java ---------------------------------------------------------------------- diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java index fabc8cb..dd01f74 100644 --- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java +++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java @@ -56,6 +56,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; @@ -329,7 +330,7 @@ public class GremlinExecutor implements AutoCloseable { final Future<?> executionFuture = executorService.submit(evalFuture); if (scriptEvalTimeOut > 0) { // Schedule a timeout in the thread pool for future execution - scheduledExecutorService.schedule(() -> { + final ScheduledFuture<?> sf = scheduledExecutorService.schedule(() -> { if (executionFuture.cancel(true)) { final CompletableFuture<Object> ef = evaluationFutureRef.get(); if (ef != null) { @@ -338,6 +339,17 @@ public class GremlinExecutor implements AutoCloseable { } } }, scriptEvalTimeOut, TimeUnit.MILLISECONDS); + + // Cancel the scheduled timeout if the eval future is complete or the script evaluation failed with exception + evaluationFuture.handleAsync((v, t) -> { + if (!sf.isDone()) { + logger.debug("Killing scheduled timeout on script evaluation - {} - as the eval completed (possibly with exception).", script); + sf.cancel(true); + } + + // no return is necessary - nothing downstream is concerned with what happens in here + return null; + }, scheduledExecutorService); } return evaluationFuture; http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2a8c92f2/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java ---------------------------------------------------------------------- diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java index 8aed8a6..ca361a0 100644 --- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java +++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java @@ -418,6 +418,19 @@ public class GremlinExecutorTest { } @Test + public void shouldCancelTimeoutOnSuccessfulEval() throws Exception { + final long scriptEvaluationTimeout = 5_000; + final GremlinExecutor gremlinExecutor = GremlinExecutor.build() + .scriptEvaluationTimeout(scriptEvaluationTimeout) + .create(); + + final long now = System.currentTimeMillis(); + assertEquals(2, gremlinExecutor.eval("1+1").get()); + gremlinExecutor.close(); + assertTrue(System.currentTimeMillis() - now < scriptEvaluationTimeout); + } + + @Test public void shouldEvalInMultipleThreads() throws Exception { final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();
