elasticsearch源码之Transport

elasticsearch源码之Transport

es使用netty来实现client和server,netty的启动在NettyTransport.java中,在此类中使用ChannelPipeline初始化了ClientBootstrap和ServerBootstrap,关于channelpipline这边不再多介绍,是netty中的一项功能。es的每个节点既是一个client也是一个server,先看一下client端的实现,client的主要功能是sendRequest和handle response。

client

client的主要功能是发送请求,并处理response,一般来说这是一个阻塞的过程,es的发送请求和处理response是分开的。

发送数据

es的各个模块在需要发请求是通过调用TransportService中的sendRequest方法。

public <T extends TransportResponse> void sendRequest(final DiscoveryNode node, final String action, final TransportRequest request,
                                                      final TransportRequestOptions options, TransportResponseHandler<T> handler) throws TransportException {
    if (node == null) {
        throw new ElasticsearchIllegalStateException("can't send request to a null node");
    }
    final long requestId = newRequestId();  //通过原子类生成一个唯一的requestID
    TimeoutHandler timeoutHandler = null;
    try {
        if (options.timeout() != null) {
            timeoutHandler = new TimeoutHandler(requestId);
            timeoutHandler.future = threadPool.schedule(options.timeout(), ThreadPool.Names.GENERIC, timeoutHandler);
        }
        clientHandlers.put(requestId, new RequestHolder<T>(handler, node, action, timeoutHandler));
        transport.sendRequest(node, requestId, action, request, options);
    } catch (final Throwable e) {

        final RequestHolder holderToNotify = clientHandlers.remove(requestId);
        if (timeoutHandler != null) {
            timeoutHandler.future.cancel(false);
        }
        if (holderToNotify != null) {
            final SendRequestTransportException sendRequestException = new SendRequestTransportException(node, action, e);
            threadPool.executor(ThreadPool.Names.GENERIC).execute(new Runnable() {
                @Override
                public void run() {
                    holderToNotify.handler().handleException(sendRequestException);
                }
            });
        }

        if (throwConnectException) {
            if (e instanceof ConnectTransportException) {
                throw (ConnectTransportException) e;
            }
        }
    }

DiscoveryNode是节点对象,action代表此请求的action类型,TransportRequest存储的是请求的数据,TransportRequestOptions代表请求选项,包括请求的类型(用于后边请求时使用不同的连接),timeout等信息,TransportResponseHandler是请求返回的response处理对象。

1.先使用AtomicLong生成了一个requestId
2.判断是否有超时设置,若是有的话,启动一个定时任务,使用timeoutHandler 处理这个requestId。
3.将requestId 和RequestHolder对象的映射关系存入到clientHandlers中(用于处理response),RequestHolder对象将请求返回的response处理对象,action,timeoutHandler,node进行封装。
4,最后调用Transport中的sendRequest方法,这里的transport实际上就是NettyT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值