Skip to content

Commit 619a011

Browse files
authored
Remove multiple paths from elasticsearch-node tool (#72351)
relates #71205
1 parent cbb2ca2 commit 619a011

11 files changed

Lines changed: 74 additions & 85 deletions

File tree

server/src/internalClusterTest/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommandIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ public void testResolvePath() throws Exception {
567567
final Path indexPath = indexPathByNodeName.get(nodeName);
568568
final OptionSet options = parser.parse("--dir", indexPath.toAbsolutePath().toString());
569569
command.findAndProcessShardPath(options, environmentByNodeName.get(nodeName),
570-
new Path[] { environmentByNodeName.get(nodeName).dataFile() },
570+
environmentByNodeName.get(nodeName).dataFile(),
571571
state, shardPath -> assertThat(shardPath.resolveIndex(), equalTo(indexPath)));
572572
}
573573
}

server/src/main/java/org/elasticsearch/cluster/coordination/DetachClusterCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public DetachClusterCommand() {
3838

3939

4040
@Override
41-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) throws IOException {
42-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
41+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env) throws IOException {
42+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
4343

4444
terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
4545
final ClusterState oldClusterState = loadTermAndClusterState(persistedClusterStateService, env).v2();

server/src/main/java/org/elasticsearch/cluster/coordination/ElasticsearchNodeCommand.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.io.IOException;
4141
import java.nio.file.Files;
4242
import java.nio.file.Path;
43-
import java.util.Arrays;
4443
import java.util.EnumSet;
4544
import java.util.Map;
4645

@@ -100,14 +99,14 @@ public ElasticsearchNodeCommand(String description) {
10099
super(description);
101100
}
102101

103-
public static PersistedClusterStateService createPersistedClusterStateService(Settings settings, Path... dataPaths) throws IOException {
104-
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPaths);
102+
public static PersistedClusterStateService createPersistedClusterStateService(Settings settings, Path dataPath) throws IOException {
103+
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPath);
105104
if (nodeMetadata == null) {
106105
throw new ElasticsearchException(NO_NODE_METADATA_FOUND_MSG);
107106
}
108107

109108
String nodeId = nodeMetadata.nodeId();
110-
return new PersistedClusterStateService(dataPaths, nodeId, namedXContentRegistry, BigArrays.NON_RECYCLING_INSTANCE,
109+
return new PersistedClusterStateService(new Path[] { dataPath }, nodeId, namedXContentRegistry, BigArrays.NON_RECYCLING_INSTANCE,
111110
new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L);
112111
}
113112

@@ -134,7 +133,7 @@ protected void processNodePaths(Terminal terminal, OptionSet options, Environmen
134133
if (dataPath == null) {
135134
throw new ElasticsearchException(NO_NODE_FOLDER_FOUND_MSG);
136135
}
137-
processNodePaths(terminal, new Path[] { dataPath.path }, options, env);
136+
processNodePaths(terminal, dataPath.path, options, env);
138137
} catch (LockObtainFailedException e) {
139138
throw new ElasticsearchException(FAILED_TO_OBTAIN_NODE_LOCK_MSG, e);
140139
}
@@ -170,18 +169,14 @@ protected boolean validateBeforeLock(Terminal terminal, Environment env) {
170169
/**
171170
* Process the paths. Locks for the paths is held during this method invocation.
172171
* @param terminal the terminal to use for messages
173-
* @param dataPaths the paths of the node to process
172+
* @param dataPath the path of the node to process
174173
* @param options the command line options
175174
* @param env the env of the node to process
176175
*/
177-
protected abstract void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env)
176+
protected abstract void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env)
178177
throws IOException, UserException;
179178

180-
protected NodeEnvironment.NodePath[] toNodePaths(Path[] dataPaths) {
181-
return Arrays.stream(dataPaths).map(ElasticsearchNodeCommand::createNodePath).toArray(NodeEnvironment.NodePath[]::new);
182-
}
183-
184-
private static NodeEnvironment.NodePath createNodePath(Path path) {
179+
protected static NodeEnvironment.NodePath createNodePath(Path path) {
185180
try {
186181
return new NodeEnvironment.NodePath(path);
187182
} catch (IOException e) {

server/src/main/java/org/elasticsearch/cluster/coordination/RemoveCustomsCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public RemoveCustomsCommand() {
4444
}
4545

4646
@Override
47-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env)
47+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env)
4848
throws IOException, UserException {
4949
final List<String> customsToRemove = arguments.values(options);
5050
if (customsToRemove.isEmpty()) {
5151
throw new UserException(ExitCodes.USAGE, "Must supply at least one custom metadata name to remove");
5252
}
5353

54-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
54+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
5555

5656
terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
5757
final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);

server/src/main/java/org/elasticsearch/cluster/coordination/RemoveSettingsCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public RemoveSettingsCommand() {
4444
}
4545

4646
@Override
47-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env)
47+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env)
4848
throws IOException, UserException {
4949
final List<String> settingsToRemove = arguments.values(options);
5050
if (settingsToRemove.isEmpty()) {
5151
throw new UserException(ExitCodes.USAGE, "Must supply at least one setting to remove");
5252
}
5353

54-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
54+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
5555

5656
terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
5757
final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);

server/src/main/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapMasterCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ protected boolean validateBeforeLock(Terminal terminal, Environment env) {
6969
return true;
7070
}
7171

72-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) throws IOException {
73-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
72+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env) throws IOException {
73+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
7474

7575
final Tuple<Long, ClusterState> state = loadTermAndClusterState(persistedClusterStateService, env);
7676
final ClusterState oldClusterState = state.v2();

server/src/main/java/org/elasticsearch/env/NodeRepurposeCommand.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.List;
3131
import java.util.Set;
3232
import java.util.stream.Collectors;
33-
import java.util.stream.Stream;
3433
import java.util.stream.StreamSupport;
3534

3635
import static org.elasticsearch.env.NodeEnvironment.INDICES_FOLDER;
@@ -63,28 +62,28 @@ protected boolean validateBeforeLock(Terminal terminal, Environment env) {
6362
}
6463

6564
@Override
66-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) throws IOException {
65+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env) throws IOException {
6766
assert DiscoveryNode.canContainData(env.settings()) == false;
6867

6968
if (DiscoveryNode.isMasterNode(env.settings()) == false) {
70-
processNoMasterNoDataNode(terminal, dataPaths, env);
69+
processNoMasterNoDataNode(terminal, dataPath, env);
7170
} else {
72-
processMasterNoDataNode(terminal, dataPaths, env);
71+
processMasterNoDataNode(terminal, dataPath, env);
7372
}
7473
}
7574

76-
private void processNoMasterNoDataNode(Terminal terminal, Path[] dataPaths, Environment env) throws IOException {
77-
NodeEnvironment.NodePath[] nodePaths = toNodePaths(dataPaths);
75+
private void processNoMasterNoDataNode(Terminal terminal, Path dataPath, Environment env) throws IOException {
76+
NodeEnvironment.NodePath nodePath = createNodePath(dataPath);
7877

7978
terminal.println(Terminal.Verbosity.VERBOSE, "Collecting shard data paths");
80-
List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePaths[0]);
79+
List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePath);
8180

8281
terminal.println(Terminal.Verbosity.VERBOSE, "Collecting index metadata paths");
83-
List<Path> indexMetadataPaths = NodeEnvironment.collectIndexMetadataPaths(nodePaths[0]);
82+
List<Path> indexMetadataPaths = NodeEnvironment.collectIndexMetadataPaths(nodePath);
8483

8584
Set<Path> indexPaths = uniqueParentPaths(shardDataPaths, indexMetadataPaths);
8685

87-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
86+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
8887

8988
final Metadata metadata = loadClusterState(terminal, env, persistedClusterStateService).metadata();
9089
if (indexPaths.isEmpty() && metadata.indices().isEmpty()) {
@@ -106,23 +105,23 @@ private void processNoMasterNoDataNode(Terminal terminal, Path[] dataPaths, Envi
106105

107106
removePaths(terminal, indexPaths); // clean-up shard dirs
108107
// clean-up all metadata dirs
109-
MetadataStateFormat.deleteMetaState(dataPaths);
110-
IOUtils.rm(Stream.of(dataPaths).map(path -> path.resolve(INDICES_FOLDER)).toArray(Path[]::new));
108+
MetadataStateFormat.deleteMetaState(dataPath);
109+
IOUtils.rm(dataPath.resolve(INDICES_FOLDER));
111110

112111
terminal.println("Node successfully repurposed to no-master and no-data.");
113112
}
114113

115-
private void processMasterNoDataNode(Terminal terminal, Path[] dataPaths, Environment env) throws IOException {
116-
NodeEnvironment.NodePath[] nodePaths = toNodePaths(dataPaths);
114+
private void processMasterNoDataNode(Terminal terminal, Path dataPath, Environment env) throws IOException {
115+
NodeEnvironment.NodePath nodePath = createNodePath(dataPath);
117116

118117
terminal.println(Terminal.Verbosity.VERBOSE, "Collecting shard data paths");
119-
List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePaths[0]);
118+
List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePath);
120119
if (shardDataPaths.isEmpty()) {
121120
terminal.println(NO_SHARD_DATA_TO_CLEAN_UP_FOUND);
122121
return;
123122
}
124123

125-
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
124+
final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPath);
126125

127126
final Metadata metadata = loadClusterState(terminal, env, persistedClusterStateService).metadata();
128127

server/src/main/java/org/elasticsearch/env/OverrideNodeVersionCommand.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.io.IOException;
1919
import java.nio.file.Path;
20-
import java.util.Arrays;
2120

2221
public class OverrideNodeVersionCommand extends ElasticsearchNodeCommand {
2322
private static final String TOO_NEW_MESSAGE =
@@ -58,9 +57,9 @@ public OverrideNodeVersionCommand() {
5857
}
5958

6059
@Override
61-
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) throws IOException {
62-
final Path[] nodePaths = Arrays.stream(toNodePaths(dataPaths)).map(p -> p.path).toArray(Path[]::new);
63-
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(nodePaths);
60+
protected void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment env) throws IOException {
61+
final Path nodePath = createNodePath(dataPath).path;
62+
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(nodePath);
6463
if (nodeMetadata == null) {
6564
throw new ElasticsearchException(NO_METADATA_MESSAGE);
6665
}
@@ -78,7 +77,7 @@ protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet o
7877
.replace("V_NEW", nodeMetadata.nodeVersion().toString())
7978
.replace("V_CUR", Version.CURRENT.toString()));
8079

81-
PersistedClusterStateService.overrideVersion(Version.CURRENT, dataPaths);
80+
PersistedClusterStateService.overrideVersion(Version.CURRENT, dataPath);
8281

8382
terminal.println(SUCCESS_MESSAGE);
8483
}

server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected Path getPath(String dirValue) {
108108
return PathUtils.get(dirValue, "", "");
109109
}
110110

111-
protected void findAndProcessShardPath(OptionSet options, Environment environment, Path[] dataPaths, ClusterState clusterState,
111+
protected void findAndProcessShardPath(OptionSet options, Environment environment, Path dataPath, ClusterState clusterState,
112112
CheckedConsumer<ShardPath, IOException> consumer)
113113
throws IOException {
114114
final Settings settings = environment.settings();
@@ -154,18 +154,15 @@ protected void findAndProcessShardPath(OptionSet options, Environment environmen
154154
final Index index = indexMetadata.getIndex();
155155
final ShardId shId = new ShardId(index, shardId);
156156

157-
for (Path dataPath : dataPaths) {
158-
final Path shardPathLocation = dataPath
159-
.resolve(NodeEnvironment.INDICES_FOLDER)
160-
.resolve(index.getUUID())
161-
.resolve(Integer.toString(shId.id()));
162-
if (Files.exists(shardPathLocation)) {
163-
final ShardPath shardPath = ShardPath.loadShardPath(logger, shId, indexSettings.customDataPath(),
164-
new Path[]{shardPathLocation}, dataPath);
165-
if (shardPath != null) {
166-
consumer.accept(shardPath);
167-
return;
168-
}
157+
final Path shardPathLocation = dataPath
158+
.resolve(NodeEnvironment.INDICES_FOLDER)
159+
.resolve(index.getUUID())
160+
.resolve(Integer.toString(shId.id()));
161+
if (Files.exists(shardPathLocation)) {
162+
final ShardPath shardPath = ShardPath.loadShardPath(logger, shId, indexSettings.customDataPath(),
163+
new Path[]{shardPathLocation}, dataPath);
164+
if (shardPath != null) {
165+
consumer.accept(shardPath);
169166
}
170167
}
171168
}
@@ -228,13 +225,13 @@ private void warnAboutIndexBackup(Terminal terminal) {
228225

229226
// Visible for testing
230227
@Override
231-
public void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment environment) throws IOException {
228+
public void processNodePaths(Terminal terminal, Path dataPath, OptionSet options, Environment environment) throws IOException {
232229
warnAboutIndexBackup(terminal);
233230

234231
final ClusterState clusterState =
235-
loadTermAndClusterState(createPersistedClusterStateService(environment.settings(), dataPaths), environment).v2();
232+
loadTermAndClusterState(createPersistedClusterStateService(environment.settings(), dataPath), environment).v2();
236233

237-
findAndProcessShardPath(options, environment, dataPaths, clusterState, shardPath -> {
234+
findAndProcessShardPath(options, environment, dataPath, clusterState, shardPath -> {
238235
final Path indexPath = shardPath.resolveIndex();
239236
final Path translogPath = shardPath.resolveTranslog();
240237
if (Files.exists(translogPath) == false || Files.isDirectory(translogPath) == false) {

0 commit comments

Comments
 (0)