-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Remove custom metadata tool #50813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Remove custom metadata tool #50813
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
server/src/main/java/org/elasticsearch/cluster/coordination/RemoveCustomsCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.elasticsearch.cluster.coordination; | ||
|
|
||
| import com.carrotsearch.hppc.cursors.ObjectCursor; | ||
| import joptsimple.OptionSet; | ||
| import joptsimple.OptionSpec; | ||
| import org.elasticsearch.cli.ExitCodes; | ||
| import org.elasticsearch.cli.Terminal; | ||
| import org.elasticsearch.cli.UserException; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.metadata.MetaData; | ||
| import org.elasticsearch.common.collect.Tuple; | ||
| import org.elasticsearch.common.regex.Regex; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.gateway.PersistedClusterStateService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| public class RemoveCustomsCommand extends ElasticsearchNodeCommand { | ||
|
|
||
| static final String CUSTOMS_REMOVED_MSG = "Customs were successfully removed from the cluster state"; | ||
| static final String CONFIRMATION_MSG = | ||
| DELIMITER + | ||
| "\n" + | ||
| "You should only run this tool if you have broken custom metadata in the\n" + | ||
| "cluster state that prevents the cluster state from being loaded.\n" + | ||
| "This tool can cause data loss and its use should be your last resort.\n" + | ||
| "\n" + | ||
| "Do you want to proceed?\n"; | ||
|
|
||
| private final OptionSpec<String> arguments; | ||
|
|
||
| public RemoveCustomsCommand() { | ||
| super("Removes custom metadata from the cluster state"); | ||
| arguments = parser.nonOptions("custom metadata names"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) | ||
| throws IOException, UserException { | ||
| final List<String> customsToRemove = arguments.values(options); | ||
| if (customsToRemove.isEmpty()) { | ||
| throw new UserException(ExitCodes.USAGE, "Must supply at least one custom metadata name to remove"); | ||
| } | ||
|
|
||
| final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(dataPaths); | ||
|
|
||
| terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state"); | ||
| final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env); | ||
| final ClusterState oldClusterState = termAndClusterState.v2(); | ||
| terminal.println(Terminal.Verbosity.VERBOSE, "custom metadata names: " + oldClusterState.metaData().customs().keys()); | ||
| final MetaData.Builder metaDataBuilder = MetaData.builder(oldClusterState.metaData()); | ||
| for (String customToRemove : customsToRemove) { | ||
| boolean matched = false; | ||
| for (ObjectCursor<String> customKeyCur : oldClusterState.metaData().customs().keys()) { | ||
| final String customKey = customKeyCur.value; | ||
| if (Regex.simpleMatch(customToRemove, customKey)) { | ||
DaveCTurner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| metaDataBuilder.removeCustom(customKey); | ||
| if (matched == false) { | ||
| terminal.println("The following customs will be removed:"); | ||
| } | ||
| matched = true; | ||
| terminal.println(customKey); | ||
| } | ||
| } | ||
| if (matched == false) { | ||
| throw new UserException(ExitCodes.USAGE, | ||
| "No custom metadata matching [" + customToRemove + "] were found on this node"); | ||
| } | ||
| } | ||
| final ClusterState newClusterState = ClusterState.builder(oldClusterState).metaData(metaDataBuilder.build()).build(); | ||
| terminal.println(Terminal.Verbosity.VERBOSE, | ||
| "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]"); | ||
|
|
||
| confirm(terminal, CONFIRMATION_MSG); | ||
|
|
||
| try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) { | ||
| writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState); | ||
| } | ||
|
|
||
| terminal.println(CUSTOMS_REMOVED_MSG); | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
server/src/test/java/org/elasticsearch/cluster/coordination/RemoveCustomsCommandIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.elasticsearch.cluster.coordination; | ||
|
|
||
| import joptsimple.OptionSet; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.cli.MockTerminal; | ||
| import org.elasticsearch.cli.UserException; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.env.TestEnvironment; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false) | ||
| public class RemoveCustomsCommandIT extends ESIntegTestCase { | ||
|
|
||
| public void testRemoveCustomsAbortedByUser() throws Exception { | ||
| internalCluster().setBootstrapMasterNodeIndex(0); | ||
| String node = internalCluster().startNode(); | ||
| Settings dataPathSettings = internalCluster().dataPathSettings(node); | ||
| ensureStableCluster(1); | ||
| internalCluster().stopRandomDataNode(); | ||
|
|
||
| Environment environment = TestEnvironment.newEnvironment( | ||
| Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); | ||
| expectThrows(() -> removeCustoms(environment, true, new String[]{ "index-graveyard" }), | ||
| ElasticsearchNodeCommand.ABORTED_BY_USER_MSG); | ||
| } | ||
|
|
||
| public void testRemoveCustomsSuccessful() throws Exception { | ||
| internalCluster().setBootstrapMasterNodeIndex(0); | ||
| String node = internalCluster().startNode(); | ||
| createIndex("test"); | ||
| client().admin().indices().prepareDelete("test").get(); | ||
| assertEquals(1, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); | ||
| Settings dataPathSettings = internalCluster().dataPathSettings(node); | ||
| ensureStableCluster(1); | ||
| internalCluster().stopRandomDataNode(); | ||
|
|
||
| Environment environment = TestEnvironment.newEnvironment( | ||
| Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); | ||
| MockTerminal terminal = removeCustoms(environment, false, | ||
| randomBoolean() ? | ||
| new String[]{ "index-graveyard" } : | ||
| new String[]{ "index-*" } | ||
| ); | ||
| assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CUSTOMS_REMOVED_MSG)); | ||
| assertThat(terminal.getOutput(), containsString("The following customs will be removed:")); | ||
| assertThat(terminal.getOutput(), containsString("index-graveyard")); | ||
|
|
||
| internalCluster().startNode(dataPathSettings); | ||
| assertEquals(0, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); | ||
| } | ||
|
|
||
| public void testCustomDoesNotMatch() throws Exception { | ||
| internalCluster().setBootstrapMasterNodeIndex(0); | ||
| String node = internalCluster().startNode(); | ||
| createIndex("test"); | ||
| client().admin().indices().prepareDelete("test").get(); | ||
| assertEquals(1, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); | ||
| Settings dataPathSettings = internalCluster().dataPathSettings(node); | ||
| ensureStableCluster(1); | ||
| internalCluster().stopRandomDataNode(); | ||
|
|
||
| Environment environment = TestEnvironment.newEnvironment( | ||
| Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); | ||
| UserException ex = expectThrows(UserException.class, () -> removeCustoms(environment, false, | ||
| new String[]{ "index-greveyard-with-typos" })); | ||
| assertThat(ex.getMessage(), containsString("No custom metadata matching [index-greveyard-with-typos] were " + | ||
| "found on this node")); | ||
| } | ||
|
|
||
| private MockTerminal executeCommand(ElasticsearchNodeCommand command, Environment environment, boolean abort, String... args) | ||
| throws Exception { | ||
| final MockTerminal terminal = new MockTerminal(); | ||
| final OptionSet options = command.getParser().parse(args); | ||
| final String input; | ||
|
|
||
| if (abort) { | ||
| input = randomValueOtherThanMany(c -> c.equalsIgnoreCase("y"), () -> randomAlphaOfLength(1)); | ||
| } else { | ||
| input = randomBoolean() ? "y" : "Y"; | ||
| } | ||
|
|
||
| terminal.addTextInput(input); | ||
|
|
||
| try { | ||
| command.execute(terminal, options, environment); | ||
| } finally { | ||
| assertThat(terminal.getOutput(), containsString(ElasticsearchNodeCommand.STOP_WARNING_MSG)); | ||
| } | ||
|
|
||
| return terminal; | ||
| } | ||
|
|
||
| private MockTerminal removeCustoms(Environment environment, boolean abort, String... args) throws Exception { | ||
| final MockTerminal terminal = executeCommand(new RemoveCustomsCommand(), environment, abort, args); | ||
| assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CONFIRMATION_MSG)); | ||
| assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CUSTOMS_REMOVED_MSG)); | ||
| return terminal; | ||
| } | ||
|
|
||
| private void expectThrows(ThrowingRunnable runnable, String message) { | ||
| ElasticsearchException ex = expectThrows(ElasticsearchException.class, runnable); | ||
| assertThat(ex.getMessage(), containsString(message)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.