標籤:this 機制 bin group exce div null sum server
gremlin-server中 實現session需要兩點保證:
- session 綁定了 變數列表;
- 每一個session必須 在同一台 server進程的同一個 線程中運行。 這是又tinkpop graph transaction的threadlocal 機制要求的。
1. SessionOpProcessor.java 中維護了 id -》 session的列表, 每個session 維護 Binding變數, 這即是 java ScriptEngine 的binding。
protected static ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>();http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html#eval(java.lang.String,%20javax.script.Bindings)
2. gremlin driver 端每一個session 固定在一台機器:Client.java
/** * Randomly choose an available {@link Host} to bind the session too and initialize the {@link ConnectionPool}. */ @Override protected void initializeImplementation() { // chooses an available host at random final List<Host> hosts = cluster.allHosts() .stream().filter(Host::isAvailable).collect(Collectors.toList()); if (hosts.isEmpty()) throw new IllegalStateException("No available host in the cluster"); Collections.shuffle(hosts); final Host host = hosts.get(0); connectionPool = new ConnectionPool(host, this, Optional.of(1), Optional.of(1)); }
3. 每個session 有自己的 SingleThreadExecutor,保證單線程執行. Session.java
/** * By binding the session to run ScriptEngine evaluations in a specific thread, each request will respect * the ThreadLocal nature of Graph implementations. */ private final ExecutorService executor = Executors.newSingleThreadExecutor(threadFactoryWorker);
4. 保證 每個Encoder 都在session對應的thread執行。 這樣保證了 netty的pipeline 在同一個thread執行:
GremlinResponseFrameEncoder.java
// if the request came in on a session then the serialization must occur in that same thread, except// in the case of an error where we can free the session executor from having to do that job. the// problem here is that if the session executor is used in the case of an error and the executor is// blocked by parallel requests then there is 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 = new Frame(serializer.serializeResponseAsBinary(o, ctx.alloc()));else serialized = new Frame(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 is fully asynchronous or finished very quickly, you don‘t // need to specify a group. pipeline.addLast(group, "handler", new MyBusinessLogicHandler());
gremlin driver/server 基於netty的 session實現