Search in sources :

Example 6 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class TransportClient method buildTemplate.

private static ClientTemplate buildTemplate(Settings providedSettings, Settings defaultSettings, Collection<Class<? extends Plugin>> plugins, HostFailureListener failureListner) {
    if (Node.NODE_NAME_SETTING.exists(providedSettings) == false) {
        providedSettings = Settings.builder().put(providedSettings).put(Node.NODE_NAME_SETTING.getKey(), "_client_").build();
    }
    final PluginsService pluginsService = newPluginService(providedSettings, plugins);
    final Settings settings = Settings.builder().put(defaultSettings).put(pluginsService.updatedSettings()).build();
    final List<Closeable> resourcesToClose = new ArrayList<>();
    final ThreadPool threadPool = new ThreadPool(settings);
    resourcesToClose.add(() -> ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS));
    final NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    try {
        final List<Setting<?>> additionalSettings = new ArrayList<>();
        final List<String> additionalSettingsFilter = new ArrayList<>();
        additionalSettings.addAll(pluginsService.getPluginSettings());
        additionalSettingsFilter.addAll(pluginsService.getPluginSettingsFilter());
        for (final ExecutorBuilder<?> builder : threadPool.builders()) {
            additionalSettings.addAll(builder.getRegisteredSettings());
        }
        SettingsModule settingsModule = new SettingsModule(settings, additionalSettings, additionalSettingsFilter);
        SearchModule searchModule = new SearchModule(settings, true, pluginsService.filterPlugins(SearchPlugin.class));
        List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
        entries.addAll(NetworkModule.getNamedWriteables());
        entries.addAll(searchModule.getNamedWriteables());
        entries.addAll(ClusterModule.getNamedWriteables());
        entries.addAll(pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedWriteables().stream()).collect(Collectors.toList()));
        NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries);
        NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(Stream.of(searchModule.getNamedXContents().stream(), pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getNamedXContent().stream())).flatMap(Function.identity()).collect(toList()));
        ModulesBuilder modules = new ModulesBuilder();
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.createGuiceModules()) {
            modules.add(pluginModule);
        }
        modules.add(b -> b.bind(ThreadPool.class).toInstance(threadPool));
        ActionModule actionModule = new ActionModule(true, settings, null, settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(), threadPool, pluginsService.filterPlugins(ActionPlugin.class), null, null);
        modules.add(actionModule);
        CircuitBreakerService circuitBreakerService = Node.createCircuitBreakerService(settingsModule.getSettings(), settingsModule.getClusterSettings());
        resourcesToClose.add(circuitBreakerService);
        BigArrays bigArrays = new BigArrays(settings, circuitBreakerService);
        resourcesToClose.add(bigArrays);
        modules.add(settingsModule);
        NetworkModule networkModule = new NetworkModule(settings, true, pluginsService.filterPlugins(NetworkPlugin.class), threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, xContentRegistry, networkService, null);
        final Transport transport = networkModule.getTransportSupplier().get();
        final TransportService transportService = new TransportService(settings, transport, threadPool, networkModule.getTransportInterceptor(), boundTransportAddress -> DiscoveryNode.createLocal(settings, new TransportAddress(TransportAddress.META_ADDRESS, 0), UUIDs.randomBase64UUID()), null);
        modules.add((b -> {
            b.bind(BigArrays.class).toInstance(bigArrays);
            b.bind(PluginsService.class).toInstance(pluginsService);
            b.bind(CircuitBreakerService.class).toInstance(circuitBreakerService);
            b.bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
            b.bind(Transport.class).toInstance(transport);
            b.bind(TransportService.class).toInstance(transportService);
            b.bind(NetworkService.class).toInstance(networkService);
        }));
        Injector injector = modules.createInjector();
        final TransportClientNodesService nodesService = new TransportClientNodesService(settings, transportService, threadPool, failureListner == null ? (t, e) -> {
        } : failureListner);
        final TransportProxyClient proxy = new TransportProxyClient(settings, transportService, nodesService, actionModule.getActions().values().stream().map(x -> x.getAction()).collect(Collectors.toList()));
        List<LifecycleComponent> pluginLifecycleComponents = new ArrayList<>();
        pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream().map(injector::getInstance).collect(Collectors.toList()));
        resourcesToClose.addAll(pluginLifecycleComponents);
        transportService.start();
        transportService.acceptIncomingRequests();
        ClientTemplate transportClient = new ClientTemplate(injector, pluginLifecycleComponents, nodesService, proxy, namedWriteableRegistry);
        resourcesToClose.clear();
        return transportClient;
    } finally {
        IOUtils.closeWhileHandlingException(resourcesToClose);
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Arrays(java.util.Arrays) Injector(org.elasticsearch.common.inject.Injector) NetworkModule(org.elasticsearch.common.network.NetworkModule) BigArrays(org.elasticsearch.common.util.BigArrays) Settings(org.elasticsearch.common.settings.Settings) TimeValue.timeValueSeconds(org.elasticsearch.common.unit.TimeValue.timeValueSeconds) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) ActionRequest(org.elasticsearch.action.ActionRequest) PluginsService(org.elasticsearch.plugins.PluginsService) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Collection(java.util.Collection) UUIDs(org.elasticsearch.common.UUIDs) ClusterModule(org.elasticsearch.cluster.ClusterModule) Collectors(java.util.stream.Collectors) AbstractClient(org.elasticsearch.client.support.AbstractClient) List(java.util.List) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) Module(org.elasticsearch.common.inject.Module) InternalSettingsPreparer(org.elasticsearch.node.InternalSettingsPreparer) Function(java.util.function.Function) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) ActionModule(org.elasticsearch.action.ActionModule) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TcpTransport(org.elasticsearch.transport.TcpTransport) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) ExecutorBuilder(org.elasticsearch.threadpool.ExecutorBuilder) TransportService(org.elasticsearch.transport.TransportService) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) SearchModule(org.elasticsearch.search.SearchModule) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ActionResponse(org.elasticsearch.action.ActionResponse) IOUtils(org.apache.lucene.util.IOUtils) Plugin(org.elasticsearch.plugins.Plugin) Action(org.elasticsearch.action.Action) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Closeable(java.io.Closeable) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Closeable(java.io.Closeable) ActionModule(org.elasticsearch.action.ActionModule) ArrayList(java.util.ArrayList) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) Injector(org.elasticsearch.common.inject.Injector) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) PluginsService(org.elasticsearch.plugins.PluginsService) Setting(org.elasticsearch.common.settings.Setting) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) BigArrays(org.elasticsearch.common.util.BigArrays) TransportService(org.elasticsearch.transport.TransportService) SettingsModule(org.elasticsearch.common.settings.SettingsModule) NetworkService(org.elasticsearch.common.network.NetworkService) SearchModule(org.elasticsearch.search.SearchModule) ModulesBuilder(org.elasticsearch.common.inject.ModulesBuilder) NetworkModule(org.elasticsearch.common.network.NetworkModule) ClusterModule(org.elasticsearch.cluster.ClusterModule) Module(org.elasticsearch.common.inject.Module) ActionModule(org.elasticsearch.action.ActionModule) SettingsModule(org.elasticsearch.common.settings.SettingsModule) SearchModule(org.elasticsearch.search.SearchModule) Transport(org.elasticsearch.transport.Transport) TcpTransport(org.elasticsearch.transport.TcpTransport) NetworkModule(org.elasticsearch.common.network.NetworkModule)

Example 7 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class NativeScriptTests method testNativeScript.

public void testNativeScript() throws InterruptedException {
    Settings settings = Settings.builder().put("node.name", "testNativeScript").put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build();
    ScriptModule scriptModule = new ScriptModule(settings, new Environment(settings), null, singletonList(new NativeScriptEngineService(settings, singletonMap("my", new MyNativeScriptFactory()))), emptyList());
    List<Setting<?>> scriptSettings = scriptModule.getSettings();
    scriptSettings.add(InternalSettingsPlugin.VERSION_CREATED);
    ExecutableScript executable = scriptModule.getScriptService().executable(new Script(ScriptType.INLINE, NativeScriptEngineService.NAME, "my", Collections.emptyMap()), ScriptContext.Standard.SEARCH);
    assertThat(executable.run().toString(), equalTo("test"));
}
Also used : Setting(org.elasticsearch.common.settings.Setting) Environment(org.elasticsearch.env.Environment) Settings(org.elasticsearch.common.settings.Settings)

Example 8 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class TestDeprecationHeaderRestAction method prepareRequest.

// List<String> casts
@SuppressWarnings("unchecked")
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    final List<String> settings;
    try (XContentParser parser = request.contentParser()) {
        final Map<String, Object> source = parser.map();
        if (source.containsKey("deprecated_settings")) {
            deprecationLogger.deprecated(DEPRECATED_USAGE);
            settings = (List<String>) source.get("deprecated_settings");
        } else {
            settings = (List<String>) source.get("settings");
        }
    }
    return channel -> {
        final XContentBuilder builder = channel.newBuilder();
        builder.startObject().startArray("settings");
        for (String setting : settings) {
            builder.startObject().field(setting, SETTINGS.get(setting).getRaw(this.settings)).endObject();
        }
        builder.endArray().endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Setting(org.elasticsearch.common.settings.Setting) IOException(java.io.IOException) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Collections(java.util.Collections) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 9 with Setting

use of org.elasticsearch.common.settings.Setting in project elasticsearch by elastic.

the class DeprecationHttpIT method doTestDeprecationWarningsAppearInHeaders.

/**
     * Run a request that receives a predictably randomized number of deprecation warnings.
     * <p>
     * Re-running this back-to-back helps to ensure that warnings are not being maintained across requests.
     */
private void doTestDeprecationWarningsAppearInHeaders() throws IOException {
    final boolean useDeprecatedField = randomBoolean();
    final boolean useNonDeprecatedSetting = randomBoolean();
    // deprecated settings should also trigger a deprecation warning
    final List<Setting<Boolean>> settings = new ArrayList<>(3);
    settings.add(TEST_DEPRECATED_SETTING_TRUE1);
    if (randomBoolean()) {
        settings.add(TEST_DEPRECATED_SETTING_TRUE2);
    }
    if (useNonDeprecatedSetting) {
        settings.add(TEST_NOT_DEPRECATED_SETTING);
    }
    Collections.shuffle(settings, random());
    // trigger all deprecations
    Response response = getRestClient().performRequest("GET", "/_test_cluster/deprecated_settings", Collections.emptyMap(), buildSettingsRequest(settings, useDeprecatedField));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(OK.getStatus()));
    final List<String> deprecatedWarnings = getWarningHeaders(response.getHeaders());
    final List<Matcher<String>> headerMatchers = new ArrayList<>(4);
    headerMatchers.add(equalTo(TestDeprecationHeaderRestAction.DEPRECATED_ENDPOINT));
    if (useDeprecatedField) {
        headerMatchers.add(equalTo(TestDeprecationHeaderRestAction.DEPRECATED_USAGE));
    }
    for (Setting<?> setting : settings) {
        if (setting.isDeprecated()) {
            headerMatchers.add(equalTo("[" + setting.getKey() + "] setting was deprecated in Elasticsearch and will be removed in a future release! " + "See the breaking changes documentation for the next major version."));
        }
    }
    assertThat(deprecatedWarnings, hasSize(headerMatchers.size()));
    for (final String deprecatedWarning : deprecatedWarnings) {
        assertThat(deprecatedWarning, matches(WARNING_HEADER_PATTERN.pattern()));
    }
    final List<String> actualWarningValues = deprecatedWarnings.stream().map(DeprecationLogger::extractWarningValueFromWarningHeader).collect(Collectors.toList());
    for (Matcher<String> headerMatcher : headerMatchers) {
        assertThat(actualWarningValues, hasItem(headerMatcher));
    }
}
Also used : Response(org.elasticsearch.client.Response) Matcher(org.hamcrest.Matcher) Setting(org.elasticsearch.common.settings.Setting) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

Setting (org.elasticsearch.common.settings.Setting)9 Settings (org.elasticsearch.common.settings.Settings)8 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)4 HashSet (java.util.HashSet)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Version (org.elasticsearch.Version)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 StringContains.containsString (org.hamcrest.core.StringContains.containsString)2 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)2