Search in sources :

Example 96 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project OpenSearch by opensearch-project.

the class SettingsUpdaterTests method testUpdateOfValidationDependentSettings.

public void testUpdateOfValidationDependentSettings() {
    final ClusterSettings settings = new ClusterSettings(Settings.EMPTY, new HashSet<>(asList(SETTING_FOO_LOW, SETTING_FOO_HIGH)));
    final SettingsUpdater updater = new SettingsUpdater(settings);
    final Metadata.Builder metadata = Metadata.builder().persistentSettings(Settings.EMPTY).transientSettings(Settings.EMPTY);
    ClusterState cluster = ClusterState.builder(new ClusterName("cluster")).metadata(metadata).build();
    cluster = updater.updateSettings(cluster, Settings.builder().put(SETTING_FOO_LOW.getKey(), 20).build(), Settings.EMPTY, logger);
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_LOW.getKey()), equalTo("20"));
    cluster = updater.updateSettings(cluster, Settings.builder().put(SETTING_FOO_HIGH.getKey(), 40).build(), Settings.EMPTY, logger);
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_LOW.getKey()), equalTo("20"));
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_HIGH.getKey()), equalTo("40"));
    cluster = updater.updateSettings(cluster, Settings.builder().put(SETTING_FOO_LOW.getKey(), 5).build(), Settings.EMPTY, logger);
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_LOW.getKey()), equalTo("5"));
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_HIGH.getKey()), equalTo("40"));
    cluster = updater.updateSettings(cluster, Settings.builder().put(SETTING_FOO_HIGH.getKey(), 8).build(), Settings.EMPTY, logger);
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_LOW.getKey()), equalTo("5"));
    assertThat(cluster.getMetadata().settings().get(SETTING_FOO_HIGH.getKey()), equalTo("8"));
    final ClusterState finalCluster = cluster;
    Exception exception = expectThrows(IllegalArgumentException.class, () -> updater.updateSettings(finalCluster, Settings.builder().put(SETTING_FOO_HIGH.getKey(), 2).build(), Settings.EMPTY, logger));
    assertThat(exception.getMessage(), equalTo("[high]=2 is lower than [low]=5"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterName(org.opensearch.cluster.ClusterName)

Example 97 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project OpenSearch by opensearch-project.

the class SettingsUpdaterTests method testDeprecationLogging.

public void testDeprecationLogging() {
    Setting<String> deprecatedSetting = Setting.simpleString("deprecated.setting", Property.Dynamic, Property.NodeScope, Property.Deprecated);
    final Settings settings = Settings.builder().put("deprecated.setting", "foo").build();
    final Set<Setting<?>> settingsSet = Stream.concat(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), Stream.of(deprecatedSetting)).collect(Collectors.toSet());
    final ClusterSettings clusterSettings = new ClusterSettings(settings, settingsSet);
    clusterSettings.addSettingsUpdateConsumer(deprecatedSetting, s -> {
    });
    final SettingsUpdater settingsUpdater = new SettingsUpdater(clusterSettings);
    final ClusterState clusterState = ClusterState.builder(new ClusterName("foo")).metadata(Metadata.builder().persistentSettings(settings).build()).build();
    final Settings toApplyDebug = Settings.builder().put("logger.org.opensearch", "debug").build();
    final ClusterState afterDebug = settingsUpdater.updateSettings(clusterState, toApplyDebug, Settings.EMPTY, logger);
    assertSettingDeprecationsAndWarnings(new Setting<?>[] { deprecatedSetting });
    final Settings toApplyUnset = Settings.builder().putNull("logger.org.opensearch").build();
    final ClusterState afterUnset = settingsUpdater.updateSettings(afterDebug, toApplyUnset, Settings.EMPTY, logger);
    assertNoDeprecationWarnings();
    // we also check that if no settings are changed, deprecation logging still occurs
    settingsUpdater.updateSettings(afterUnset, toApplyUnset, Settings.EMPTY, logger);
    assertNoDeprecationWarnings();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Setting(org.opensearch.common.settings.Setting) ClusterName(org.opensearch.cluster.ClusterName) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 98 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project OpenSearch by opensearch-project.

the class ActionModuleTests method testPluginCanRegisterRestHandler.

public void testPluginCanRegisterRestHandler() {
    class FakeHandler implements RestHandler {

        @Override
        public List<Route> routes() {
            return singletonList(new Route(Method.GET, "/_dummy"));
        }

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        }
    }
    ActionPlugin registersFakeHandler = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new FakeHandler());
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        UsageService usageService = new UsageService();
        ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(threadPool.getThreadContext()), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null, usageService, null);
        actionModule.initRestHandlers(null);
        // At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(new RestHandler() {

            @Override
            public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            }

            @Override
            public List<Route> routes() {
                return singletonList(new Route(Method.GET, "/_dummy"));
            }
        }));
        assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/_dummy] for method: GET"));
    } finally {
        threadPool.shutdown();
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ActionPlugin(org.opensearch.plugins.ActionPlugin) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) RestChannel(org.opensearch.rest.RestChannel) RestController(org.opensearch.rest.RestController) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.opensearch.common.settings.SettingsFilter) RestRequest(org.opensearch.rest.RestRequest) RestHandler(org.opensearch.rest.RestHandler) UsageService(org.opensearch.usage.UsageService) SettingsModule(org.opensearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 99 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project OpenSearch by opensearch-project.

the class ActionModuleTests method testPluginCantOverwriteBuiltinRestHandler.

public void testPluginCantOverwriteBuiltinRestHandler() throws IOException {
    ActionPlugin dupsMainAction = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new RestMainAction() {

                @Override
                public String getName() {
                    return "duplicated_" + super.getName();
                }
            });
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        UsageService usageService = new UsageService();
        ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(threadPool.getThreadContext()), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(dupsMainAction), null, null, usageService, null);
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.initRestHandlers(null));
        assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/] for method: GET"));
    } finally {
        threadPool.shutdown();
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ActionPlugin(org.opensearch.plugins.ActionPlugin) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) RestController(org.opensearch.rest.RestController) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.opensearch.common.settings.SettingsFilter) RestMainAction(org.opensearch.rest.action.RestMainAction) RestHandler(org.opensearch.rest.RestHandler) UsageService(org.opensearch.usage.UsageService) SettingsModule(org.opensearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 100 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project OpenSearch by opensearch-project.

the class MainActionTests method testMainActionClusterAvailable.

public void testMainActionClusterAvailable() {
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterName clusterName = new ClusterName("opensearch");
    final Settings settings = Settings.builder().put("node.name", "my-node").build();
    when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    ClusterBlocks blocks;
    if (randomBoolean()) {
        if (randomBoolean()) {
            blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        } else {
            blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 400", randomBoolean(), randomBoolean(), false, RestStatus.BAD_REQUEST, ClusterBlockLevel.ALL)).build();
        }
    } else {
        blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 503", randomBoolean(), randomBoolean(), false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL)).build();
    }
    ClusterState state = ClusterState.builder(clusterName).blocks(blocks).build();
    when(clusterService.state()).thenReturn(state);
    TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    TransportMainAction action = new TransportMainAction(settings, transportService, mock(ActionFilters.class), clusterService);
    AtomicReference<MainResponse> responseRef = new AtomicReference<>();
    action.doExecute(mock(Task.class), new MainRequest(), new ActionListener<MainResponse>() {

        @Override
        public void onResponse(MainResponse mainResponse) {
            responseRef.set(mainResponse);
        }

        @Override
        public void onFailure(Exception e) {
            logger.error("unexpected error", e);
        }
    });
    assertNotNull(responseRef.get());
    verify(clusterService, times(1)).state();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Task(org.opensearch.tasks.Task) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) AtomicReference(java.util.concurrent.atomic.AtomicReference) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) ClusterName(org.opensearch.cluster.ClusterName) Transport(org.opensearch.transport.Transport) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Aggregations

ClusterSettings (org.opensearch.common.settings.ClusterSettings)218 Settings (org.opensearch.common.settings.Settings)136 ClusterState (org.opensearch.cluster.ClusterState)70 ClusterService (org.opensearch.cluster.service.ClusterService)67 HashSet (java.util.HashSet)46 Metadata (org.opensearch.cluster.metadata.Metadata)37 Matchers.containsString (org.hamcrest.Matchers.containsString)36 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)36 Before (org.junit.Before)33 ArrayList (java.util.ArrayList)31 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)31 ClusterName (org.opensearch.cluster.ClusterName)30 ThreadPool (org.opensearch.threadpool.ThreadPool)29 RoutingTable (org.opensearch.cluster.routing.RoutingTable)26 TestThreadPool (org.opensearch.threadpool.TestThreadPool)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 Setting (org.opensearch.common.settings.Setting)23 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)23 IOException (java.io.IOException)19 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)19