|
| 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 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | +package org.elasticsearch.cluster.coordination; |
| 9 | + |
| 10 | +import joptsimple.OptionSet; |
| 11 | + |
| 12 | +import org.elasticsearch.ElasticsearchException; |
| 13 | +import org.elasticsearch.cli.MockTerminal; |
| 14 | +import org.elasticsearch.cli.UserException; |
| 15 | +import org.elasticsearch.common.collect.ImmutableOpenMap; |
| 16 | +import org.elasticsearch.common.settings.Setting; |
| 17 | +import org.elasticsearch.common.settings.Settings; |
| 18 | +import org.elasticsearch.common.util.CollectionUtils; |
| 19 | +import org.elasticsearch.env.Environment; |
| 20 | +import org.elasticsearch.env.TestEnvironment; |
| 21 | +import org.elasticsearch.plugins.Plugin; |
| 22 | +import org.elasticsearch.test.ESIntegTestCase; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static org.hamcrest.Matchers.containsString; |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
| 30 | +import static org.hamcrest.Matchers.not; |
| 31 | + |
| 32 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false) |
| 33 | +public class RemoveIndexSettingsCommandIT extends ESIntegTestCase { |
| 34 | + |
| 35 | + static final Setting<Integer> FOO = Setting.intSetting("index.foo", 1, Setting.Property.IndexScope, Setting.Property.Dynamic); |
| 36 | + static final Setting<Integer> BAR = Setting.intSetting("index.bar", 2, Setting.Property.IndexScope, Setting.Property.Final); |
| 37 | + |
| 38 | + public static class ExtraSettingsPlugin extends Plugin { |
| 39 | + @Override |
| 40 | + public List<Setting<?>> getSettings() { |
| 41 | + return Arrays.asList(FOO, BAR); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 47 | + return CollectionUtils.appendToCopy(super.nodePlugins(), ExtraSettingsPlugin.class); |
| 48 | + } |
| 49 | + |
| 50 | + public void testRemoveSettingsAbortedByUser() throws Exception { |
| 51 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 52 | + String node = internalCluster().startNode(); |
| 53 | + createIndex("test-index", Settings.builder().put(FOO.getKey(), 101).put(BAR.getKey(), 102).build()); |
| 54 | + ensureYellow("test-index"); |
| 55 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 56 | + ensureStableCluster(1); |
| 57 | + internalCluster().stopRandomDataNode(); |
| 58 | + |
| 59 | + Settings nodeSettings = Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build(); |
| 60 | + ElasticsearchException error = expectThrows( |
| 61 | + ElasticsearchException.class, |
| 62 | + () -> removeIndexSettings(TestEnvironment.newEnvironment(nodeSettings), true, "index.foo") |
| 63 | + ); |
| 64 | + assertThat(error.getMessage(), equalTo(ElasticsearchNodeCommand.ABORTED_BY_USER_MSG)); |
| 65 | + internalCluster().startNode(nodeSettings); |
| 66 | + } |
| 67 | + |
| 68 | + public void testRemoveSettingsSuccessful() throws Exception { |
| 69 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 70 | + String node = internalCluster().startNode(); |
| 71 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 72 | + |
| 73 | + int numIndices = randomIntBetween(1, 10); |
| 74 | + int[] barValues = new int[numIndices]; |
| 75 | + for (int i = 0; i < numIndices; i++) { |
| 76 | + String index = "test-index-" + i; |
| 77 | + barValues[i] = between(1, 1000); |
| 78 | + createIndex(index, Settings.builder().put(FOO.getKey(), between(1, 1000)).put(BAR.getKey(), barValues[i]).build()); |
| 79 | + } |
| 80 | + int moreIndices = randomIntBetween(1, 10); |
| 81 | + for (int i = 0; i < moreIndices; i++) { |
| 82 | + createIndex("more-index-" + i, Settings.EMPTY); |
| 83 | + } |
| 84 | + internalCluster().stopNode(node); |
| 85 | + |
| 86 | + Environment environment = TestEnvironment.newEnvironment( |
| 87 | + Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build() |
| 88 | + ); |
| 89 | + |
| 90 | + MockTerminal terminal = removeIndexSettings(environment, false, "index.foo"); |
| 91 | + assertThat(terminal.getOutput(), containsString(RemoveIndexSettingsCommand.SETTINGS_REMOVED_MSG)); |
| 92 | + for (int i = 0; i < numIndices; i++) { |
| 93 | + assertThat(terminal.getOutput(), containsString("Index setting [index.foo] will be removed from index [[test-index-" + i)); |
| 94 | + } |
| 95 | + for (int i = 0; i < moreIndices; i++) { |
| 96 | + assertThat(terminal.getOutput(), not(containsString("Index setting [index.foo] will be removed from index [[more-index-" + i))); |
| 97 | + } |
| 98 | + Settings nodeSettings = Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build(); |
| 99 | + internalCluster().startNode(nodeSettings); |
| 100 | + |
| 101 | + ImmutableOpenMap<String, Settings> getIndexSettings = client().admin() |
| 102 | + .indices() |
| 103 | + .prepareGetSettings("test-index-*") |
| 104 | + .get() |
| 105 | + .getIndexToSettings(); |
| 106 | + for (int i = 0; i < numIndices; i++) { |
| 107 | + String index = "test-index-" + i; |
| 108 | + Settings indexSettings = getIndexSettings.get(index); |
| 109 | + assertFalse(indexSettings.hasValue("index.foo")); |
| 110 | + assertThat(indexSettings.get("index.bar"), equalTo(Integer.toString(barValues[i]))); |
| 111 | + } |
| 112 | + getIndexSettings = client().admin().indices().prepareGetSettings("more-index-*").get().getIndexToSettings(); |
| 113 | + for (int i = 0; i < moreIndices; i++) { |
| 114 | + assertNotNull(getIndexSettings.get("more-index-" + i)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void testSettingDoesNotMatch() throws Exception { |
| 119 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 120 | + String node = internalCluster().startNode(); |
| 121 | + createIndex("test-index", Settings.builder().put(FOO.getKey(), 101).put(BAR.getKey(), 102).build()); |
| 122 | + ensureYellow("test-index"); |
| 123 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 124 | + ensureStableCluster(1); |
| 125 | + internalCluster().stopRandomDataNode(); |
| 126 | + |
| 127 | + Settings nodeSettings = Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build(); |
| 128 | + UserException error = expectThrows( |
| 129 | + UserException.class, |
| 130 | + () -> removeIndexSettings(TestEnvironment.newEnvironment(nodeSettings), true, "index.not_foo") |
| 131 | + ); |
| 132 | + assertThat(error.getMessage(), containsString("No index setting matching [index.not_foo] were found on this node")); |
| 133 | + internalCluster().startNode(nodeSettings); |
| 134 | + } |
| 135 | + |
| 136 | + private MockTerminal executeCommand(ElasticsearchNodeCommand command, Environment environment, boolean abort, String... args) |
| 137 | + throws Exception { |
| 138 | + final MockTerminal terminal = new MockTerminal(); |
| 139 | + final OptionSet options = command.getParser().parse(args); |
| 140 | + final String input; |
| 141 | + |
| 142 | + if (abort) { |
| 143 | + input = randomValueOtherThanMany(c -> c.equalsIgnoreCase("y"), () -> randomAlphaOfLength(1)); |
| 144 | + } else { |
| 145 | + input = randomBoolean() ? "y" : "Y"; |
| 146 | + } |
| 147 | + |
| 148 | + terminal.addTextInput(input); |
| 149 | + |
| 150 | + try { |
| 151 | + command.execute(terminal, options, environment); |
| 152 | + } finally { |
| 153 | + assertThat(terminal.getOutput(), containsString(ElasticsearchNodeCommand.STOP_WARNING_MSG)); |
| 154 | + } |
| 155 | + |
| 156 | + return terminal; |
| 157 | + } |
| 158 | + |
| 159 | + private MockTerminal removeIndexSettings(Environment environment, boolean abort, String... args) throws Exception { |
| 160 | + final MockTerminal terminal = executeCommand(new RemoveIndexSettingsCommand(), environment, abort, args); |
| 161 | + assertThat(terminal.getOutput(), containsString(RemoveIndexSettingsCommand.CONFIRMATION_MSG)); |
| 162 | + assertThat(terminal.getOutput(), containsString(RemoveIndexSettingsCommand.SETTINGS_REMOVED_MSG)); |
| 163 | + return terminal; |
| 164 | + } |
| 165 | +} |
0 commit comments