|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.ilm; |
| 7 | + |
| 8 | +import org.elasticsearch.client.Client; |
| 9 | +import org.elasticsearch.cluster.ClusterState; |
| 10 | +import org.elasticsearch.cluster.ClusterStateObserver; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 12 | +import org.elasticsearch.cluster.service.ClusterService; |
| 13 | +import org.elasticsearch.common.inject.AbstractModule; |
| 14 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 15 | +import org.elasticsearch.common.settings.Setting; |
| 16 | +import org.elasticsearch.common.settings.Setting.Property; |
| 17 | +import org.elasticsearch.common.settings.Settings; |
| 18 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 19 | +import org.elasticsearch.env.Environment; |
| 20 | +import org.elasticsearch.env.NodeEnvironment; |
| 21 | +import org.elasticsearch.index.IndexModule; |
| 22 | +import org.elasticsearch.plugins.Plugin; |
| 23 | +import org.elasticsearch.script.ScriptService; |
| 24 | +import org.elasticsearch.test.ESIntegTestCase; |
| 25 | +import org.elasticsearch.test.ESIntegTestCase.ClusterScope; |
| 26 | +import org.elasticsearch.threadpool.ThreadPool; |
| 27 | +import org.elasticsearch.watcher.ResourceWatcherService; |
| 28 | +import org.elasticsearch.xpack.core.ilm.AsyncActionStep; |
| 29 | +import org.elasticsearch.xpack.core.ilm.Step.StepKey; |
| 30 | +import org.elasticsearch.xpack.core.ilm.UpdateSettingsStep; |
| 31 | +import org.junit.After; |
| 32 | + |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE; |
| 41 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 42 | +import static org.elasticsearch.xpack.ilm.UpdateSettingsStepTests.SettingsTestingService.INVALID_VALUE; |
| 43 | +import static org.hamcrest.Matchers.is; |
| 44 | + |
| 45 | +@ClusterScope(scope = SUITE, supportsDedicatedMasters = false, numDataNodes = 1, numClientNodes = 0) |
| 46 | +public class UpdateSettingsStepTests extends ESIntegTestCase { |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 50 | + return Arrays.asList(SettingsListenerPlugin.class); |
| 51 | + } |
| 52 | + |
| 53 | + private static final SettingsTestingService service = new SettingsTestingService(); |
| 54 | + |
| 55 | + public static class SettingsListenerPlugin extends Plugin { |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<Setting<?>> getSettings() { |
| 59 | + return Arrays.asList(SettingsTestingService.VALUE); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onIndexModule(IndexModule module) { |
| 64 | + module.addSettingsUpdateConsumer(SettingsTestingService.VALUE, service::setValue, service::validate); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, |
| 69 | + ResourceWatcherService resourceWatcherService, ScriptService scriptService, |
| 70 | + NamedXContentRegistry xContentRegistry, Environment environment, |
| 71 | + NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) { |
| 72 | + return Collections.singletonList(service); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static class SettingsListenerModule extends AbstractModule { |
| 77 | + private final SettingsTestingService service; |
| 78 | + |
| 79 | + SettingsListenerModule(SettingsTestingService service) { |
| 80 | + this.service = service; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void configure() { |
| 85 | + bind(SettingsTestingService.class).toInstance(service); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static class SettingsTestingService { |
| 90 | + public static final String INVALID_VALUE = "INVALID"; |
| 91 | + static Setting<String> VALUE = Setting.simpleString("index.test.setting", Property.Dynamic, Property.IndexScope); |
| 92 | + |
| 93 | + public volatile String value; |
| 94 | + |
| 95 | + void setValue(String value) { |
| 96 | + this.value = value; |
| 97 | + } |
| 98 | + |
| 99 | + void validate(String value) { |
| 100 | + if (value.equals(INVALID_VALUE)) { |
| 101 | + throw new IllegalArgumentException("[" + INVALID_VALUE + "] is not supported"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void resetValues() { |
| 106 | + this.value = ""; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @After |
| 111 | + public void resetSettingValue() { |
| 112 | + service.resetValues(); |
| 113 | + } |
| 114 | + |
| 115 | + public void testUpdateSettingsStepRetriesOnError() throws InterruptedException { |
| 116 | + assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder() |
| 117 | + .build()).get()); |
| 118 | + |
| 119 | + ClusterState state = clusterService().state(); |
| 120 | + IndexMetaData indexMetaData = state.metaData().index("test"); |
| 121 | + ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class); |
| 122 | + ClusterStateObserver observer = new ClusterStateObserver(clusterService(), null, logger, threadPool.getThreadContext()); |
| 123 | + |
| 124 | + CountDownLatch latch = new CountDownLatch(2); |
| 125 | + |
| 126 | + // fail the first setting update by using an invalid valid |
| 127 | + Settings invalidValueSetting = Settings.builder().put("index.test.setting", INVALID_VALUE).build(); |
| 128 | + UpdateSettingsStep step = new UpdateSettingsStep( |
| 129 | + new StepKey("hot", "action", "updateSetting"), new StepKey("hot", "action", "validate"), client(), |
| 130 | + invalidValueSetting); |
| 131 | + |
| 132 | + step.performAction(indexMetaData, state, observer, new AsyncActionStep.Listener() { |
| 133 | + @Override |
| 134 | + public void onResponse(boolean complete) { |
| 135 | + latch.countDown(); |
| 136 | + fail("expected the test to fail when updating the setting to an invalid value"); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onFailure(Exception e) { |
| 141 | + latch.countDown(); |
| 142 | + |
| 143 | + // use a valid setting value so the second update call is successful |
| 144 | + Settings validIndexSetting = Settings.builder().put("index.test.setting", "valid").build(); |
| 145 | + UpdateSettingsStep step = new UpdateSettingsStep( |
| 146 | + new StepKey("hot", "action", "updateSetting"), new StepKey("hot", "action", "validate"), client(), |
| 147 | + validIndexSetting); |
| 148 | + |
| 149 | + step.performAction(indexMetaData, state, observer, new AsyncActionStep.Listener() { |
| 150 | + @Override |
| 151 | + public void onResponse(boolean complete) { |
| 152 | + latch.countDown(); |
| 153 | + assertThat(complete, is(true)); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void onFailure(Exception e) { |
| 158 | + latch.countDown(); |
| 159 | + fail("unexpected failure when trying to update setting to a valid value"); |
| 160 | + } |
| 161 | + }); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + |
| 166 | + latch.await(10, TimeUnit.SECONDS); |
| 167 | + |
| 168 | + for (SettingsTestingService instance : internalCluster().getDataNodeInstances(SettingsTestingService.class)) { |
| 169 | + assertThat(instance.value, is("valid")); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments