A two-point guarantee is required to implement the session in Gremlin-server:
- The session is bound to a variable list;
- Each session must be run in the same thread as the same server process. This is also Tinkpop graph transaction the threadlocal mechanism requirements.
1. Sessionopprocessor.java maintains the list of the ID-"session, each session maintains the binding variable, which is the binding of the Java ScriptEngine.
protected Static New Concurrenthashmap<>(); http://docs.oracle.com/javase/7/docs/api/javax/script/ Scriptengine.html#eval (java.lang.string,%20javax.script.bindings)
2. Gremlin driver Terminal Each session is fixed on a machine: Client.java
/*** Randomly choose an available {@linkHost} to bind the session too and initialize the {@linkConnectionPool}. */@Overrideprotected voidinitializeimplementation () {//chooses an available host at random FinalListcluster.allhosts (). Stream (). Filter (host::isavailable). Collect (Collectors.tolist ()); if(Hosts.isempty ())Throw NewIllegalStateException ("No available host in the cluster"); Collections.shuffle (hosts); FinalHost host = Hosts.get (0); ConnectionPool=NewConnectionPool (Host, This, Optional.of (1), Optional.of (1)); }
3. Each session has its own singlethreadexecutor, which guarantees single-threaded execution. Session.java
/** * By binding the session to run ScriptEngine evaluations in a specific thread, each request would respect * th e ThreadLocal nature of Graph implementations. */ Private Final Executorservice executor = Executors.newsinglethreadexecutor (Threadfactoryworker);
4. Ensure that each encoder is executed with the thread corresponding to the session. This ensures that the Netty pipeline is executed on the same thread:
Gremlinresponseframeencoder.java
//If the request came in to a session then the serialization must occur in that same thread, except//In the case of a error where we can free the session executor from have to does that job. The//Problem here is the if the session executor are used in the case of an error and the executor is//blocked by parallel requests then there are no thread available to serialize the result and send//Back the response as the workers get all tied up behind the session executor.if(NULL= = Session | | !o.getstatus (). GetCode (). Issuccess ()) serialized=NewFrame (Serializer.serializeresponseasbinary (O, Ctx.alloc ()));Elseserialized=NewFrame (Session.getexecutor (). Submit ((), Serializer.serializeresponseasbinary (O, Ctx.alloc ())). get ());
Http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html
Tell the pipeline to run Mybusinesslogichandler's event handler methods//in a different thread than an I/O thread so That the I/O thread is not blocked by//a time-consuming task. If Your business logic was fully asynchronous or finished very quickly, you don ' t//need to specify a group. Pipeline.addlast (Group, "handler", New Mybusinesslogichandler ());
Gremlin Driver/server session implementation based on Netty