gremlin driver/server 基於netty的 session實現

來源:互聯網
上載者:User

標籤: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實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.