You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/08/20 22:15:43 UTC

[3/8] incubator-tinkerpop git commit: Minor refactoring of ScriptEngines class.

Minor refactoring of ScriptEngines class.

Stopped blocking threads with sleep for control operations (didn't realize that this still worked this way).


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/5401e942
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/5401e942
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/5401e942

Branch: refs/heads/master
Commit: 5401e942b3fe7cd1fc9e985be55916c5529a4c5d
Parents: 8ab99e6
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Aug 20 15:23:29 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Aug 20 15:23:29 2015 -0400

----------------------------------------------------------------------
 .../gremlin/groovy/engine/ScriptEngines.java    | 200 ++++++++++---------
 .../groovy/engine/ScriptEnginesTest.java        |  78 ++++++++
 2 files changed, 189 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5401e942/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index c83a532..70a47a6 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -34,8 +34,6 @@ import javax.script.CompiledScript;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
-import java.io.Closeable;
-import java.io.IOException;
 import java.io.Reader;
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
@@ -45,9 +43,12 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Queue;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.LockSupport;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -61,18 +62,24 @@ public class ScriptEngines implements AutoCloseable {
     private static final Logger logger = LoggerFactory.getLogger(ScriptEngines.class);
 
     private final static ScriptEngineManager SCRIPT_ENGINE_MANAGER = new ScriptEngineManager();
+    private static final GremlinGroovyScriptEngineFactory gremlinGroovyScriptEngineFactory = new GremlinGroovyScriptEngineFactory();
 
     /**
      * {@code ScriptEngine} objects configured for the server keyed on the language name.
      */
     private final Map<String, ScriptEngine> scriptEngines = new ConcurrentHashMap<>();
 
-    private final AtomicInteger evaluationCount = new AtomicInteger(0);
-    private volatile boolean controlOperationExecuting = false;
+    private final AtomicBoolean controlOperationExecuting = new AtomicBoolean(false);
+    private final Queue<Thread> controlWaiters = new ConcurrentLinkedQueue<>();
+    private final Queue<Thread> evalWaiters = new ConcurrentLinkedQueue<>();
 
-    private static final GremlinGroovyScriptEngineFactory gremlinGroovyScriptEngineFactory = new GremlinGroovyScriptEngineFactory();
     private final Consumer<ScriptEngines> initializer;
 
+    /**
+     * Creates a new object.
+     *
+     * @param initializer allows for external initialization of the newly created {@code ScriptEngines} object
+     */
     public ScriptEngines(final Consumer<ScriptEngines> initializer) {
         this.initializer = initializer;
         this.initializer.accept(this);
@@ -85,13 +92,8 @@ public class ScriptEngines implements AutoCloseable {
         if (!scriptEngines.containsKey(language))
             throw new IllegalArgumentException(String.format("Language [%s] not supported", language));
 
-        try {
-            awaitControlOp();
-            evaluationCount.incrementAndGet();
-            return scriptEngines.get(language).eval(script, bindings);
-        } finally {
-            evaluationCount.decrementAndGet();
-        }
+        awaitControlOp();
+        return scriptEngines.get(language).eval(script, bindings);
     }
 
     /**
@@ -102,58 +104,46 @@ public class ScriptEngines implements AutoCloseable {
         if (!scriptEngines.containsKey(language))
             throw new IllegalArgumentException("Language [%s] not supported");
 
-        try {
-            evaluationCount.incrementAndGet();
-            return scriptEngines.get(language).eval(reader, bindings);
-        } finally {
-            evaluationCount.decrementAndGet();
-        }
+        awaitControlOp();
+        return scriptEngines.get(language).eval(reader, bindings);
     }
 
     /**
      * Compiles a script without executing it.
      *
-     * @throws java.lang.UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
+     * @throws UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
      * the {@link javax.script.Compilable} interface.
      */
     public CompiledScript compile(final String script, final String language) throws ScriptException {
         if (!scriptEngines.containsKey(language))
             throw new IllegalArgumentException("Language [%s] not supported");
 
-        try {
-            evaluationCount.incrementAndGet();
-            final ScriptEngine scriptEngine = scriptEngines.get(language);
-            if (!Compilable.class.isAssignableFrom(scriptEngine.getClass()))
-                throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
+        awaitControlOp();
+        final ScriptEngine scriptEngine = scriptEngines.get(language);
+        if (!Compilable.class.isAssignableFrom(scriptEngine.getClass()))
+            throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
 
-            final Compilable compilable = (Compilable) scriptEngine;
-            return compilable.compile(script);
-        } finally {
-            evaluationCount.decrementAndGet();
-        }
+        final Compilable compilable = (Compilable) scriptEngine;
+        return compilable.compile(script);
     }
 
     /**
      * Compiles a script without executing it.
      *
-     * @throws java.lang.UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
+     * @throws UnsupportedOperationException if the {@link ScriptEngine} implementation does not implement
      * the {@link javax.script.Compilable} interface.
      */
     public CompiledScript compile(final Reader script, final String language) throws ScriptException {
         if (!scriptEngines.containsKey(language))
             throw new IllegalArgumentException("Language [%s] not supported");
 
-        try {
-            evaluationCount.incrementAndGet();
-            final ScriptEngine scriptEngine = scriptEngines.get(language);
-            if (scriptEngine instanceof Compilable)
-                throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
+        awaitControlOp();
+        final ScriptEngine scriptEngine = scriptEngines.get(language);
+        if (scriptEngine instanceof Compilable)
+            throw new UnsupportedOperationException(String.format("ScriptEngine for %s does not implement %s", language, Compilable.class.getName()));
 
-            final Compilable compilable = (Compilable) scriptEngine;
-            return compilable.compile(script);
-        } finally {
-            evaluationCount.decrementAndGet();
-        }
+        final Compilable compilable = (Compilable) scriptEngine;
+        return compilable.compile(script);
     }
 
     /**
@@ -162,9 +152,8 @@ public class ScriptEngines implements AutoCloseable {
      */
     public void reload(final String language, final Set<String> imports, final Set<String> staticImports,
                        final Map<String, Object> config) {
-        signalControlOp();
-
         try {
+            signalControlOpStart();
             if (scriptEngines.containsKey(language))
                 scriptEngines.remove(language);
 
@@ -174,7 +163,7 @@ public class ScriptEngines implements AutoCloseable {
 
             logger.info("Loaded {} ScriptEngine", language);
         } finally {
-            controlOperationExecuting = false;
+            signalControlOpEnd();
         }
     }
 
@@ -184,12 +173,11 @@ public class ScriptEngines implements AutoCloseable {
      * then blocks other operations until complete.
      */
     public void addImports(final Set<String> imports) {
-        signalControlOp();
-
         try {
+            signalControlOpStart();
             getDependencyManagers().forEach(dm -> dm.addImports(imports));
         } finally {
-            controlOperationExecuting = false;
+            signalControlOpEnd();
         }
     }
 
@@ -199,10 +187,9 @@ public class ScriptEngines implements AutoCloseable {
      * it up.  Waits for any existing script evaluations to complete but then blocks other operations until complete.
      */
     public List<GremlinPlugin> use(final String group, final String artifact, final String version) {
-        signalControlOp();
-
         final List<GremlinPlugin> pluginsToLoad = new ArrayList<>();
         try {
+            signalControlOpStart();
             getDependencyManagers().forEach(dm -> {
                 try {
                     pluginsToLoad.addAll(dm.use(group, artifact, version));
@@ -211,44 +198,52 @@ public class ScriptEngines implements AutoCloseable {
                 }
             });
         } finally {
-            controlOperationExecuting = false;
+            signalControlOpEnd();
         }
 
         return pluginsToLoad;
     }
 
+    /**
+     * For each {@link DependencyManager} try to load the specified plugins.
+     */
     public void loadPlugins(final List<GremlinPlugin> plugins) {
-        signalControlOp();
-
-        getDependencyManagers().forEach(dm -> {
-            try {
-                dm.loadPlugins(plugins);
-            } catch (IllegalEnvironmentException iee) {
-                logger.warn("Some plugins may not have been loaded to {} - {}", dm.getClass().getSimpleName(), iee.getMessage());
-            } catch (Exception ex) {
-                logger.error(String.format("Some plugins may not have been loaded to %s", dm.getClass().getSimpleName()), ex);
-            } finally {
-                controlOperationExecuting = false;
-            }
-        });
+        try {
+            signalControlOpStart();
+            getDependencyManagers().forEach(dm -> {
+                try {
+                    dm.loadPlugins(plugins);
+                } catch (IllegalEnvironmentException iee) {
+                    logger.warn("Some plugins may not have been loaded to {} - {}", dm.getClass().getSimpleName(), iee.getMessage());
+                } catch (Exception ex) {
+                    logger.error(String.format("Some plugins may not have been loaded to %s", dm.getClass().getSimpleName()), ex);
+                }
+            });
+        } finally {
+            signalControlOpEnd();
+        }
     }
 
+    /**
+     * Iterate through all the {@link ScriptEngine} implementations and if they implement {@link AutoCloseable}
+     * then call the {@link AutoCloseable#close()} method. After that is complete, the script engine cache will be
+     * cleared.
+     */
     @Override
     public void close() throws Exception {
-        signalControlOp();
-
         try {
+            signalControlOpStart();
             scriptEngines.values().stream()
-                    .filter(se -> se instanceof Closeable)
-                    .map(se -> (Closeable) se).forEach(c -> {
+                    .filter(se -> se instanceof AutoCloseable)
+                    .map(se -> (AutoCloseable) se).forEach(c -> {
                 try {
                     c.close();
-                } catch (IOException ignored) {
+                } catch (Exception ignored) {
                 }
             });
             scriptEngines.clear();
         } finally {
-            controlOperationExecuting = false;
+            signalControlOpEnd();
         }
     }
 
@@ -257,12 +252,11 @@ public class ScriptEngines implements AutoCloseable {
      * then blocks other operations until complete.
      */
     public void reset() {
-        signalControlOp();
-
         try {
+            signalControlOpStart();
             getDependencyManagers().forEach(DependencyManager::reset);
         } finally {
-            controlOperationExecuting = false;
+            signalControlOpEnd();
             this.initializer.accept(this);
         }
     }
@@ -278,6 +272,9 @@ public class ScriptEngines implements AutoCloseable {
         return m;
     }
 
+    /**
+     * List the imports for those {@code ScriptEngine} objects that implement the {@link DependencyManager} interface.
+     */
     public Map<String, List<Map>> imports() {
         final Map<String, List<Map>> m = new HashMap<>();
         scriptEngines.entrySet().stream()
@@ -297,29 +294,54 @@ public class ScriptEngines implements AutoCloseable {
                 .collect(Collectors.<DependencyManager>toSet());
     }
 
-    private void signalControlOp() {
-        awaitControlOp();
-        controlOperationExecuting = true;
-        awaitEvalOp();
-    }
+    /**
+     * Called when a control operation starts.  Attempts to grab a lock on the "control" process and parks the
+     * current thread if it cannot.  Parked threads are queued until released by {@link #signalControlOpEnd()}.
+     */
+    private void signalControlOpStart() {
+        boolean wasInterrupted = false;
+        final Thread current = Thread.currentThread();
+        controlWaiters.add(current);
+
+        // Block while not first in queue or cannot acquire lock
+        while (controlWaiters.peek() != current || !controlOperationExecuting.compareAndSet(false, true)) {
+            LockSupport.park(this);
+            if (Thread.interrupted())
+                wasInterrupted = true;
+        }
 
-    private void awaitControlOp() {
-        while (controlOperationExecuting) {
-            try {
-                Thread.sleep(5);
-            } catch (Exception ignored) {
+        controlWaiters.remove();
+        if (wasInterrupted)
+            current.interrupt();
+    }
 
+    /**
+     * Called when a control operation is finished.  Releases the lock for the next thread trying to call a
+     * control operation.  If there are no additional threads trying to call control operations then unpark
+     * any evaluation threads that are waiting.
+     */
+    private void signalControlOpEnd() {
+        controlOperationExecuting.set(false);
+        LockSupport.unpark(controlWaiters.peek());
+
+        // let the eval threads proceed as long as the control functions waiting are all complete
+        if (controlWaiters.size() == 0) {
+            Thread t = evalWaiters.poll();
+            while (t != null) {
+                LockSupport.unpark(t);
+                t = evalWaiters.poll();
             }
         }
     }
 
-    private void awaitEvalOp() {
-        while (evaluationCount.get() > 0) {
-            try {
-                Thread.sleep(5);
-            } catch (Exception ignored) {
-
-            }
+    /**
+     * If a control operation is executing or there are some in the queue to be executed, then block the current
+     * thread until that process completes.
+     */
+    private void awaitControlOp() {
+        if(controlWaiters.size() > 0 || controlOperationExecuting.get()) {
+            evalWaiters.add(Thread.currentThread());
+            LockSupport.park(this);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5401e942/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEnginesTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEnginesTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEnginesTest.java
new file mode 100644
index 0000000..39e14fd
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEnginesTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy.engine;
+
+import org.junit.Test;
+
+import javax.script.SimpleBindings;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.IntStream;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class ScriptEnginesTest {
+    @Test
+    public void shouldFailUntilImportExecutes() throws Exception {
+        final ScriptEngines engines = new ScriptEngines(se -> {});
+        engines.reload("gremlin-groovy", Collections.<String>emptySet(),
+                Collections.<String>emptySet(), Collections.emptyMap());
+
+        final Set<String> imports = new HashSet<String>() {{
+            add("import java.awt.Color");
+        }};
+
+        final AtomicInteger successes = new AtomicInteger(0);
+        final AtomicInteger failures = new AtomicInteger(0);
+
+        final Thread threadImport = new Thread(() -> {
+            engines.addImports(imports);
+        });
+
+        // issue 1000 scripts in one thread using a class that isn't imported.  this will result in failure.
+        // while that thread is running start a new thread that issues an addImports to include that class.
+        // this should block further evals in the first thread until the import is complete at which point
+        // evals in the first thread will resume and start to succeed
+        final Thread threadEvalAndTriggerImport = new Thread(() ->
+            IntStream.range(0, 1000).forEach(i -> {
+                try {
+                    engines.eval("Color.BLACK", new SimpleBindings(), "gremlin-groovy");
+                    successes.incrementAndGet();
+                } catch (Exception ex) {
+                    if (failures.incrementAndGet() == 500) threadImport.start();
+                }
+            })
+        );
+
+        threadEvalAndTriggerImport.start();
+
+        threadEvalAndTriggerImport.join();
+        threadImport.join();
+
+        assertTrue(successes.intValue() > 0);
+        assertTrue(failures.intValue() >= 500);
+
+        engines.close();
+    }
+}