[ 
https://issues.apache.org/jira/browse/TINKERPOP-2245?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17319557#comment-17319557
 ] 

ASF GitHub Bot commented on TINKERPOP-2245:
-------------------------------------------

spmallette commented on a change in pull request #1414:
URL: https://github.com/apache/tinkerpop/pull/1414#discussion_r611786751



##########
File path: 
gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/MultiTaskSession.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.server.handler;
+
+import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage;
+import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode;
+import org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GroovyCompilerGremlinPlugin;
+import org.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngine;
+import org.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngineManager;
+import org.apache.tinkerpop.gremlin.server.Settings;
+import org.apache.tinkerpop.gremlin.structure.Transaction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.script.Bindings;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static 
org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor.CONFIG_GLOBAL_FUNCTION_CACHE_ENABLED;
+
+/**
+ * A {@link Session} implementation that queues tasks given to it and executes 
them in a serial fashion within the
+ * same thread which thus allows multiple tasks to be executed in the same 
transaction. The first {@link SessionTask}
+ * to execute is supplied on the constructor and additional ones may be added 
as they arrive with
+ * {@link #submitTask(SessionTask)} where they will be added to a queue where 
they will await execution in the thread
+ * bound to this session.
+ */
+public class MultiTaskSession extends AbstractSession {
+    private static final Logger logger = 
LoggerFactory.getLogger(MultiTaskSession.class);
+    protected final BlockingQueue<SessionTask> queue;
+    private final AtomicBoolean ending = new AtomicBoolean(false);
+    private final ScheduledExecutorService scheduledExecutorService;
+    private final GremlinScriptEngineManager scriptEngineManager;
+    private ScheduledFuture<?> requestCancelFuture;
+    private Bindings bindings;
+
+    /**
+     * Creates a new {@code MultiTaskSession} object providing the initial 
starting {@link SessionTask} that gets
+     * executed by the session when it starts.
+     *
+     * @param initialSessionTask The tasks that starts the session.
+     * @param sessionId The id of the session
+     * @param sessions The session id to {@link Session} instances mapping
+     */
+    public MultiTaskSession(final SessionTask initialSessionTask, final String 
sessionId,
+                     final ConcurrentMap<String, Session> sessions) {
+        super(initialSessionTask, sessionId, false, sessions);
+
+        queue = new 
LinkedBlockingQueue<>(initialSessionTask.getSettings().maxSessionTaskQueueSize);
+
+        // using a global function cache is cheaper than creating a new on per 
session especially if you have to
+        // create a lot of sessions. it will generate a ton of throw-away 
objects. mostly keeping the option open
+        // to not use it to preserve the ability to use the old functionality 
if wanted or if there is some specific
+        // use case with sessions that needs it. if we wanted this could 
eventually become a per-request option
+        // so that the client could control it as necessary and get 
scriptengine isolation if they need it.
+        if (initialSessionTask.getSettings().useCommonEngineForSessions)
+            scriptEngineManager = 
initialSessionTask.getGremlinExecutor().getScriptEngineManager();
+        else
+            scriptEngineManager = 
initializeGremlinExecutor(initialSessionTask).getScriptEngineManager();
+
+        scheduledExecutorService = 
initialSessionTask.getScheduledExecutorService();
+        submitTask(initialSessionTask);
+    }
+
+    /**
+     * Gets the script engine specific to this session which is dependent on 
the
+     * {@link Settings#useCommonEngineForSessions} configuration.
+     */
+    @Override
+    public GremlinScriptEngine getScriptEngine(final SessionTask sessionTask, 
final String language) {
+        return scriptEngineManager.getEngineByName(language);
+    }
+
+    @Override
+    public boolean isAcceptingTasks() {
+        return !ending.get();
+    }
+
+    @Override
+    public boolean submitTask(final SessionTask sessionTask) throws 
RejectedExecutionException {
+        try {
+            return isAcceptingTasks() && queue.add(sessionTask);
+        } catch (IllegalStateException ise) {
+            final String msg = String.format("Task %s rejected from session 
%s",
+                    sessionTask.getRequestMessage().getRequestId(), 
getSessionId());
+            throw new RejectedExecutionException(msg);
+        }
+    }
+
+    @Override
+    public void run() {
+        // allow the Session to know about the thread that is running it - the 
thread really only has relevance
+        // once the session has started.
+        this.sessionThread = Thread.currentThread();
+
+        // there must be one item in the queue at least since addTask() gets 
called before the worker
+        // is ever started
+        SessionTask sessionTask = queue.poll();
+        if (null == sessionTask)
+            throw new IllegalStateException(String.format("Worker has no 
initial context for session: %s", getSessionId()));
+
+        try {
+            startTransaction(sessionTask);

Review comment:
       the answer to this might be more clear given my improved comments here: 
https://github.com/apache/tinkerpop/pull/1414/files#r611785656 
   
   but basically, that method is only called when the session is started.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Consolidate the executor for bytecode & string based client
> -----------------------------------------------------------
>
>                 Key: TINKERPOP-2245
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP-2245
>             Project: TinkerPop
>          Issue Type: Improvement
>          Components: server
>    Affects Versions: 3.4.2
>            Reporter: Divij Vaidya
>            Assignee: Stephen Mallette
>            Priority: Minor
>
> We have two code paths in the server which perform (more or less) the same 
> functions. One is the executor for string based queries and other is the 
> executor for bytecode. This code can be refactored together so that the logic 
> to handle timeout, handle exception during execution, handle exception before 
> execution and others can consolidated. 
> [https://github.com/apache/tinkerpop/blob/master/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java#L246]
> and
> [https://github.com/apache/tinkerpop/blob/master/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/traversal/TraversalOpProcessor.java#L333]



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to