elasticSearch6源码分析ActionModule

Posted davidwang456

tags:

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

1.ActionModule概述

/**
 * Builds and binds the generic action map, all {@link TransportAction}s, and {@link ActionFilters}.
 */

1.1 创建TransportAction

 static Map<String, ActionHandler<?, ?>> setupActions(List<ActionPlugin> actionPlugins) {
        // Subclass NamedRegistry for easy registration
        class ActionRegistry extends NamedRegistry<ActionHandler<?, ?>> {
            ActionRegistry() {
                super("action");
            }

            public void register(ActionHandler<?, ?> handler) {
                register(handler.getAction().name(), handler);
            }

            public <Request extends ActionRequest, Response extends ActionResponse> void register(
                Action<Response> action, Class<? extends TransportAction<Request, Response>> transportAction,
                Class<?>... supportTransportActions) {
                register(new ActionHandler<>(action, transportAction, supportTransportActions));
            }
        }
        ActionRegistry actions = new ActionRegistry();

        actions.register(MainAction.INSTANCE, TransportMainAction.class);
        actions.register(NodesInfoAction.INSTANCE, TransportNodesInfoAction.class);
        actions.register(RemoteInfoAction.INSTANCE, TransportRemoteInfoAction.class);
        actions.register(NodesStatsAction.INSTANCE, TransportNodesStatsAction.class);
        actions.register(NodesUsageAction.INSTANCE, TransportNodesUsageAction.class);
        actions.register(NodesHotThreadsAction.INSTANCE, TransportNodesHotThreadsAction.class);
        actions.register(ListTasksAction.INSTANCE, TransportListTasksAction.class);
        actions.register(GetTaskAction.INSTANCE, TransportGetTaskAction.class);
        actions.register(CancelTasksAction.INSTANCE, TransportCancelTasksAction.class);

        actions.register(ClusterAllocationExplainAction.INSTANCE, TransportClusterAllocationExplainAction.class);
        actions.register(ClusterStatsAction.INSTANCE, TransportClusterStatsAction.class);
        actions.register(ClusterStateAction.INSTANCE, TransportClusterStateAction.class);
        actions.register(ClusterHealthAction.INSTANCE, TransportClusterHealthAction.class);
        actions.register(ClusterUpdateSettingsAction.INSTANCE, TransportClusterUpdateSettingsAction.class);
        actions.register(ClusterRerouteAction.INSTANCE, TransportClusterRerouteAction.class);
        actions.register(ClusterSearchShardsAction.INSTANCE, TransportClusterSearchShardsAction.class);
        actions.register(PendingClusterTasksAction.INSTANCE, TransportPendingClusterTasksAction.class);
        actions.register(PutRepositoryAction.INSTANCE, TransportPutRepositoryAction.class);
        actions.register(GetRepositoriesAction.INSTANCE, TransportGetRepositoriesAction.class);
        actions.register(DeleteRepositoryAction.INSTANCE, TransportDeleteRepositoryAction.class);
        actions.register(VerifyRepositoryAction.INSTANCE, TransportVerifyRepositoryAction.class);
        actions.register(GetSnapshotsAction.INSTANCE, TransportGetSnapshotsAction.class);
        actions.register(DeleteSnapshotAction.INSTANCE, TransportDeleteSnapshotAction.class);
        actions.register(CreateSnapshotAction.INSTANCE, TransportCreateSnapshotAction.class);
        actions.register(RestoreSnapshotAction.INSTANCE, TransportRestoreSnapshotAction.class);
        actions.register(SnapshotsStatusAction.INSTANCE, TransportSnapshotsStatusAction.class);

        actions.register(IndicesStatsAction.INSTANCE, TransportIndicesStatsAction.class);
        actions.register(IndicesSegmentsAction.INSTANCE, TransportIndicesSegmentsAction.class);
        actions.register(IndicesShardStoresAction.INSTANCE, TransportIndicesShardStoresAction.class);
        actions.register(CreateIndexAction.INSTANCE, TransportCreateIndexAction.class);
        actions.register(ResizeAction.INSTANCE, TransportResizeAction.class);
        actions.register(RolloverAction.INSTANCE, TransportRolloverAction.class);
        actions.register(DeleteIndexAction.INSTANCE, TransportDeleteIndexAction.class);
        actions.register(GetIndexAction.INSTANCE, TransportGetIndexAction.class);
        actions.register(OpenIndexAction.INSTANCE, TransportOpenIndexAction.class);
        actions.register(CloseIndexAction.INSTANCE, TransportCloseIndexAction.class);
        actions.register(IndicesExistsAction.INSTANCE, TransportIndicesExistsAction.class);
        actions.register(TypesExistsAction.INSTANCE, TransportTypesExistsAction.class);
        actions.register(GetMappingsAction.INSTANCE, TransportGetMappingsAction.class);
        actions.register(GetFieldMappingsAction.INSTANCE, TransportGetFieldMappingsAction.class,
                TransportGetFieldMappingsIndexAction.class);
        actions.register(PutMappingAction.INSTANCE, TransportPutMappingAction.class);
        actions.register(IndicesAliasesAction.INSTANCE, TransportIndicesAliasesAction.class);
        actions.register(UpdateSettingsAction.INSTANCE, TransportUpdateSettingsAction.class);
        actions.register(AnalyzeAction.INSTANCE, TransportAnalyzeAction.class);
        actions.register(PutIndexTemplateAction.INSTANCE, TransportPutIndexTemplateAction.class);
        actions.register(GetIndexTemplatesAction.INSTANCE, TransportGetIndexTemplatesAction.class);
        actions.register(DeleteIndexTemplateAction.INSTANCE, TransportDeleteIndexTemplateAction.class);
        actions.register(ValidateQueryAction.INSTANCE, TransportValidateQueryAction.class);
        actions.register(RefreshAction.INSTANCE, TransportRefreshAction.class);
        actions.register(FlushAction.INSTANCE, TransportFlushAction.class);
        actions.register(SyncedFlushAction.INSTANCE, TransportSyncedFlushAction.class);
        actions.register(ForceMergeAction.INSTANCE, TransportForceMergeAction.class);
        actions.register(UpgradeAction.INSTANCE, TransportUpgradeAction.class);
        actions.register(UpgradeStatusAction.INSTANCE, TransportUpgradeStatusAction.class);
        actions.register(UpgradeSettingsAction.INSTANCE, TransportUpgradeSettingsAction.class);
        actions.register(ClearIndicesCacheAction.INSTANCE, TransportClearIndicesCacheAction.class);
        actions.register(GetAliasesAction.INSTANCE, TransportGetAliasesAction.class);
        actions.register(AliasesExistAction.INSTANCE, TransportAliasesExistAction.class);
        actions.register(GetSettingsAction.INSTANCE, TransportGetSettingsAction.class);

        actions.register(IndexAction.INSTANCE, TransportIndexAction.class);
        actions.register(GetAction.INSTANCE, TransportGetAction.class);
        actions.register(TermVectorsAction.INSTANCE, TransportTermVectorsAction.class);
        actions.register(MultiTermVectorsAction.INSTANCE, TransportMultiTermVectorsAction.class,
                TransportShardMultiTermsVectorAction.class);
        actions.register(DeleteAction.INSTANCE, TransportDeleteAction.class);
        actions.register(UpdateAction.INSTANCE, TransportUpdateAction.class);
        actions.register(MultiGetAction.INSTANCE, TransportMultiGetAction.class,
                TransportShardMultiGetAction.class);
        actions.register(BulkAction.INSTANCE, TransportBulkAction.class,
                TransportShardBulkAction.class);
        actions.register(SearchAction.INSTANCE, TransportSearchAction.class);
        actions.register(SearchScrollAction.INSTANCE, TransportSearchScrollAction.class);
        actions.register(MultiSearchAction.INSTANCE, TransportMultiSearchAction.class);
        actions.register(ExplainAction.INSTANCE, TransportExplainAction.class);
        actions.register(ClearScrollAction.INSTANCE, TransportClearScrollAction.class);
        actions.register(RecoveryAction.INSTANCE, TransportRecoveryAction.class);
        actions.register(NodesReloadSecureSettingsAction.INSTANCE, TransportNodesReloadSecureSettingsAction.class);

        //Indexed scripts
        actions.register(PutStoredScriptAction.INSTANCE, TransportPutStoredScriptAction.class);
        actions.register(GetStoredScriptAction.INSTANCE, TransportGetStoredScriptAction.class);
        actions.register(DeleteStoredScriptAction.INSTANCE, TransportDeleteStoredScriptAction.class);

        actions.register(FieldCapabilitiesAction.INSTANCE, TransportFieldCapabilitiesAction.class,
            TransportFieldCapabilitiesIndexAction.class);

        actions.register(PutPipelineAction.INSTANCE, PutPipelineTransportAction.class);
        actions.register(GetPipelineAction.INSTANCE, GetPipelineTransportAction.class);
        actions.register(DeletePipelineAction.INSTANCE, DeletePipelineTransportAction.class);
        actions.register(SimulatePipelineAction.INSTANCE, SimulatePipelineTransportAction.class);

        actionPlugins.stream().flatMap(p -> p.getActions().stream()).forEach(actions::register);

        // Persistent tasks:
        actions.register(StartPersistentTaskAction.INSTANCE, StartPersistentTaskAction.TransportAction.class);
        actions.register(UpdatePersistentTaskStatusAction.INSTANCE, UpdatePersistentTaskStatusAction.TransportAction.class);
        actions.register(CompletionPersistentTaskAction.INSTANCE, CompletionPersistentTaskAction.TransportAction.class);
        actions.register(RemovePersistentTaskAction.INSTANCE, RemovePersistentTaskAction.TransportAction.class);

        return unmodifiableMap(actions.getRegistry());
    }

其类层次结构如下

 

1.2 创建ActionFilter

    private ActionFilters setupActionFilters(List<ActionPlugin> actionPlugins) {
        return new ActionFilters(
            Collections.unmodifiableSet(actionPlugins.stream().flatMap(p -> p.getActionFilters().stream()).collect(Collectors.toSet())));
    }

2.ActionModule使用

            ActionModule actionModule = new ActionModule(false, settings, clusterModule.getIndexNameExpressionResolver(),
                settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(),
                threadPool, pluginsService.filterPlugins(ActionPlugin.class), client, circuitBreakerService, usageService);
            modules.add(actionModule);

            final RestController restController = actionModule.getRestController();
actionModule.initRestHandlers(() -> clusterService.state().nodes());

增加了rest服务的处理handler

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));
        registerHandler.accept(new RestClusterUpdateSettingsAction(settings, restController));
        registerHandler.accept(new RestClusterGetSettingsAction(settings, restController, clusterSettings, settingsFilter));
        registerHandler.accept(new RestClusterRerouteAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestClusterSearchShardsAction(settings, restController));
        registerHandler.accept(new RestPendingClusterTasksAction(settings, restController));
        registerHandler.accept(new RestPutRepositoryAction(settings, restController));
        registerHandler.accept(new RestGetRepositoriesAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestDeleteRepositoryAction(settings, restController));
        registerHandler.accept(new RestVerifyRepositoryAction(settings, restController));
        registerHandler.accept(new RestGetSnapshotsAction(settings, restController));
        registerHandler.accept(new RestCreateSnapshotAction(settings, restController));
        registerHandler.accept(new RestRestoreSnapshotAction(settings, restController));
        registerHandler.accept(new RestDeleteSnapshotAction(settings, restController));
        registerHandler.accept(new RestSnapshotsStatusAction(settings, restController));
        registerHandler.accept(new RestGetIndicesAction(settings, restController));
        registerHandler.accept(new RestIndicesStatsAction(settings, restController));
        registerHandler.accept(new RestIndicesSegmentsAction(settings, restController));
        registerHandler.accept(new RestIndicesShardStoresAction(settings, restController));
        registerHandler.accept(new RestGetAliasesAction(settings, restController));
        registerHandler.accept(new RestIndexDeleteAliasesAction(settings, restController));
        registerHandler.accept(new RestIndexPutAliasAction(settings, restController));
        registerHandler.accept(new RestIndicesAliasesAction(settings, restController));
        registerHandler.accept(new RestCreateIndexAction(settings, restController));
        registerHandler.accept(new RestResizeHandler.RestShrinkIndexAction(settings, restController));
        registerHandler.accept(new RestResizeHandler.RestSplitIndexAction(settings, restController));
        registerHandler.accept(new RestRolloverIndexAction(settings, restController));
        registerHandler.accept(new RestDeleteIndexAction(settings, restController));
        registerHandler.accept(new RestCloseIndexAction(settings, restController));
        registerHandler.accept(new RestOpenIndexAction(settings, restController));

        registerHandler.accept(new RestUpdateSettingsAction(settings, restController));
        registerHandler.accept(new RestGetSettingsAction(settings, restController));

        registerHandler.accept(new RestAnalyzeAction(settings, restController));
        registerHandler.accept(new RestGetIndexTemplateAction(settings, restController));
        registerHandler.accept(new RestPutIndexTemplateAction(settings, restController));
        registerHandler.accept(new RestDeleteIndexTemplateAction(settings, restController));

        registerHandler.accept(new RestPutMappingAction(settings, restController));
        registerHandler.accept(new RestGetMappingAction(settings, restController));
        registerHandler.accept(new RestGetFieldMappingAction(settings, restController));

        registerHandler.accept(new RestRefreshAction(settings, restController));
        registerHandler.accept(new RestFlushAction(settings, restController));
        registerHandler.accept(new RestSyncedFlushAction(settings, restController));
        registerHandler.accept(new RestForceMergeAction(settings, restController));
        registerHandler.accept(new RestUpgradeAction(settings, restController));
        registerHandler.accept(new RestUpgradeStatusAction(settings, restController));
        registerHandler.accept(new RestClearIndicesCacheAction(settings, restController));

        registerHandler.accept(new RestIndexAction(settings, restController));
        registerHandler.accept(new RestGetAction(settings, restController));
        registerHandler.accept(new RestGetSourceAction(settings, restController));
        registerHandler.accept(new RestMultiGetAction(settings, restController));
        registerHandler.accept(new RestDeleteAction(settings, restController));
        registerHandler.accept(new org.elasticsearch.rest.action.document.RestCountAction(settings, restController));
        registerHandler.accept(new RestTermVectorsAction(settings, restController));
        registerHandler.accept(new RestMultiTermVectorsAction(settings, restController));
        registerHandler.accept(new RestBulkAction(settings, restController));
        registerHandler.accept(new RestUpdateAction(settings, restController));

        registerHandler.accept(new RestSearchAction(settings, restController));
        registerHandler.accept(new RestSearchScrollAction(settings, restController));
        registerHandler.accept(new RestClearScrollAction(settings, restController));
        registerHandler.accept(new RestMultiSearchAction(settings, restController));

        registerHandler.accept(new RestValidateQueryAction(settings, restController));

        registerHandler.accept(new RestExplainAction(settings, restController));

        registerHandler.accept(new RestRecoveryAction(settings, restController));

        registerHandler.accept(new RestReloadSecureSettingsAction(settings, restController));

        // Scripts API
        registerHandler.accept(new RestGetStoredScriptAction(settings, restController));
        registerHandler.accept(new RestPutStoredScriptAction(settings, restController));
        registerHandler.accept(new RestDeleteStoredScriptAction(settings, restController));

        registerHandler.accept(new RestFieldCapabilitiesAction(settings, restController));

        // Tasks API
        registerHandler.accept(new RestListTasksAction(settings, restController, nodesInCluster));
        registerHandler.accept(new RestGetTaskAction(settings, restController));
        registerHandler.accept(new RestCancelTasksAction(settings, restController, nodesInCluster));

        // Ingest API
        registerHandler.accept(new RestPutPipelineAction(settings, restController));
        registerHandler.accept(new RestGetPipelineAction(settings, restController));
        registerHandler.accept(new RestDeletePipelineAction(settings, restController));
        registerHandler.accept(new RestSimulatePipelineAction(settings, restController));

        // CAT API
        registerHandler.accept(new RestAllocationAction(settings, restController));
        registerHandler.accept(new RestShardsAction(settings, restController));
        registerHandler.accept(new RestMasterAction(settings, restController));
        registerHandler.accept(new RestNodesAction(settings, restController));
        registerHandler.accept(new RestTasksAction(settings, restController, nodesInCluster));
        registerHandler.accept(new RestIndicesAction(settings, restController, indexNameExpressionResolver));
        registerHandler.accept(new RestSegmentsAction(settings, restController));
        // Fully qualified to prevent interference with rest.action.count.RestCountAction
        registerHandler.accept(new org.elasticsearch.rest.action.cat.RestCountAction(settings, restController));
        // Fully qualified to prevent interference with rest.action.indices.RestRecoveryAction
        registerHandler.accept(new org.elasticsearch.rest.action.cat.RestRecoveryAction(settings, restController));
        registerHandler.accept(new RestHealthAction(settings, restController));
        registerHandler.accept(new org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction(settings, restController));
        registerHandler.accept(new RestAliasAction(settings, restController));
        registerHandler.accept(new RestThreadPoolAction(settings, restController));
        registerHandler.accept(new RestPluginsAction(settings, restController));
        registerHandler.accept(new RestFielddataAction(settings, restController));
        registerHandler.accept(new RestNodeAttrsAction(settings, restController));
        registerHandler.accept(new RestRepositoriesAction(settings, restController));
        registerHandler.accept(new RestSnapshotAction(settings, restController));
        registerHandler.accept(new RestTemplatesAction(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));
    }

类层次关系如下:

 

3.小结

actionModule定义了处理tcp和http服务的handler。

 

以上是关于elasticSearch6源码分析ActionModule的主要内容,如果未能解决你的问题,请参考以下文章

elasticSearch6源码分析http和transport模块

ElasticSearch 启动时加载 Analyzer 源码分析

Elasticsearch6.1.2源码下载和编译构建

Elasticsearch源码分析-索引分析(一)

2018年ElasticSearch6.2.2教程ELK搭建日志采集分析系统(目录)

实战Elasticsearch6的join类型