ES源码分析Transport模块之REST的解析与处理

Posted 顧棟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ES源码分析Transport模块之REST的解析与处理相关的知识,希望对你有一定的参考价值。

文章目录

Transport模块之REST的解析与处理

基于ES源码6.7.2

注册REST处理

Node ActionModule NetworkModule Netty4Plugin new ActionModule setupActions register new RestController ActionModule new NetworkModule plugin.getHttpTransports registerHttpTransport plugin.getTransports registerTransport plugin.getTransportInterceptors registerTransportInterceptor NetworkModule actionModule.initRestHandlers Node ActionModule NetworkModule Netty4Plugin

借由上图回顾一下通信模块的初始化过程,在ActionModule下对Rest请求处理进行了注册,注册过程在initRestHandlers方法中。可以发现对REST请求执行处理的类的命名是Rest*Action,同时可以发现这些处理类都继承了BaseRestHandler,而BaseRestHandler继承了RestHandler

    public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) 
        List<AbstractCatAction> catActions = new ArrayList<>();
        Consumer<RestHandler> registerHandler = a -> 
            if (a instanceof AbstractCatAction) 
                catActions.add((AbstractCatAction) a);
            
        ;
        registerHandler.accept(new RestMainAction(settings, restController));
        registerHandler.accept(new RestNodesInfoAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestRemoteClusterInfoAction(settings, restController));
        registerHandler.accept(new RestNodesStatsAction(settings, restController));
        registerHandler.accept(new RestNodesUsageAction(settings, restController));
        registerHandler.accept(new RestNodesHotThreadsAction(settings, restController));
        registerHandler.accept(new RestClusterAllocationExplainAction(settings, restController));
        registerHandler.accept(new RestClusterStatsAction(settings, restController));
        registerHandler.accept(new RestClusterStateAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestClusterHealthAction(settings, restController));
        ...
        ...
        ...
        for (ActionPlugin plugin : actionPlugins) 
            for (RestHandler handler : plugin.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings,
                    settingsFilter, indexNameExpressionResolver, nodesInCluster)) 
                registerHandler.accept(handler);
            
        
        registerHandler.accept(new RestCatAction(settings, restController, catActions));
    

RestClusterHealthAction为例,在其构造函数中对请求头中请求方法为GET,URI为/_cluster/health和拥有占位符/_cluster/health/index的处理类为this(即自己)。

    public RestClusterHealthAction(Settings settings, RestController controller) 
        super(settings);

        controller.registerHandler(RestRequest.Method.GET, "/_cluster/health", this);
        controller.registerHandler(RestRequest.Method.GET, "/_cluster/health/index", this);
    

由于继承了BaseRestHandler,所以必须实现prepareRequest方法,用于在接收到请求时,做一些前置工作,比如验证参数,转换为内部RPC请求等。

处理请求

HTTP请求执行路径

Netty4HttpRequestHandler Netty4HttpServerTransport RestController BaseRestHandler Rest*Action 内部Action channelRead0 dispatchRequest dispatchRequest tryAllHandlers dispatchRequest handleRequest prepareRequest RestChannelConsumer action.accept(channel) execute(...) sendResponse Netty4HttpRequestHandler Netty4HttpServerTransport RestController BaseRestHandler Rest*Action 内部Action

BaseRestHandlerhandleRequest方法

    @Override
    public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception 
        // 对请求的预处理
        final RestChannelConsumer action = prepareRequest(request, client);

        // validate unconsumed params, but we must exclude params used to format the response
        // use a sorted set so the unconsumed parameters appear in a reliable sorted order
        final SortedSet<String> unconsumedParams =
            request.unconsumedParams().stream().filter(p -> !responseParams().contains(p)).collect(Collectors.toCollection(TreeSet::new));

        // validate the non-response params
        if (!unconsumedParams.isEmpty()) 
            final Set<String> candidateParams = new HashSet<>();
            candidateParams.addAll(request.consumedParams());
            candidateParams.addAll(responseParams());
            throw new IllegalArgumentException(unrecognized(request, unconsumedParams《Elasticsearch 源码解析与优化实战》第15章:Transport模块分析

ES源码分析Transport模块的初始化

Elasticsearch的底层模块深入解析之node

ElasticSearchEs 源码之 Transport 和 TransportService 源码解读

Django REST framework之解析器实例以及源码流程分析

elasticSearch6源码分析http和transport模块