Skip to content

Commit 9c301b9

Browse files
committed
delete state files directly
1 parent 2881d5d commit 9c301b9

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

server/src/main/java/org/elasticsearch/gateway/MetaStateService.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import org.apache.logging.log4j.LogManager;
1313
import org.apache.logging.log4j.Logger;
14-
import org.apache.lucene.store.NIOFSDirectory;
1514
import org.elasticsearch.Version;
1615
import org.elasticsearch.cluster.metadata.IndexMetadata;
1716
import org.elasticsearch.cluster.metadata.Manifest;
@@ -22,6 +21,8 @@
2221
import org.elasticsearch.xcontent.NamedXContentRegistry;
2322

2423
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728
import java.util.function.Predicate;
@@ -84,7 +85,23 @@ List<IndexMetadata> loadIndicesStates(Predicate<String> excludeIndexPathIdsPredi
8485
*/
8586
public void unreferenceAll() throws IOException {
8687
Manifest.FORMAT.writeAndCleanup(Manifest.empty(), nodeEnv.nodeDataPaths()); // write empty file so that indices become unreferenced
87-
MetadataStateFormat.cleanupOldFiles(GLOBAL_STATE_FILE_PREFIX, Long.MAX_VALUE, nodeEnv.nodeDataPaths(), NIOFSDirectory::new);
88+
cleanUpGlobalStateFiles();
89+
}
90+
91+
private void cleanUpGlobalStateFiles() throws IOException {
92+
for (Path location : nodeEnv.nodeDataPaths()) {
93+
logger.trace("cleanupOldFiles: cleaning up {} for global state files", location);
94+
final Path stateLocation = location.resolve(MetadataStateFormat.STATE_DIR_NAME);
95+
try (var paths = Files.list(stateLocation)) {
96+
paths.filter(file -> file.getFileName().toString().startsWith(GLOBAL_STATE_FILE_PREFIX)).forEach(file -> {
97+
try {
98+
Files.deleteIfExists(file);
99+
} catch (IOException e) {
100+
logger.trace("failed to delete global state file: {}", file);
101+
}
102+
});
103+
}
104+
}
88105
}
89106

90107
/**

server/src/main/java/org/elasticsearch/gateway/MetadataStateFormat.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.common.lucene.store.IndexOutputOutputStream;
2424
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
2525
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
26-
import org.elasticsearch.core.CheckedFunction;
2726
import org.elasticsearch.core.IOUtils;
2827
import org.elasticsearch.core.Nullable;
2928
import org.elasticsearch.core.Tuple;
@@ -351,28 +350,11 @@ protected Directory newDirectory(Path dir) throws IOException {
351350
* @param locations state paths.
352351
*/
353352
public void cleanupOldFiles(final long currentGeneration, final Path[] locations) {
354-
cleanupOldFiles(prefix, currentGeneration, locations, this::newDirectory);
355-
}
356-
357-
/**
358-
* Clean ups all state files not matching passed generation.
359-
*
360-
* @param prefix filename prefix for filtering state files.
361-
* @param currentGeneration state generation to keep.
362-
* @param locations state paths.
363-
* @param directoryFunc function to create a {@link Directory} for the given path.
364-
*/
365-
public static void cleanupOldFiles(
366-
final String prefix,
367-
final long currentGeneration,
368-
final Path[] locations,
369-
final CheckedFunction<Path, Directory, IOException> directoryFunc
370-
) {
371-
final String fileNameToKeep = getStateFileName(prefix, currentGeneration);
353+
final String fileNameToKeep = getStateFileName(currentGeneration);
372354
for (Path location : locations) {
373355
logger.trace("cleanupOldFiles: cleaning up {}", location);
374356
Path stateLocation = location.resolve(STATE_DIR_NAME);
375-
try (Directory stateDir = directoryFunc.apply(stateLocation)) {
357+
try (Directory stateDir = newDirectory(stateLocation)) {
376358
for (String file : stateDir.listAll()) {
377359
if (file.startsWith(prefix) && file.equals(fileNameToKeep) == false) {
378360
deleteFileIgnoreExceptions(stateLocation, stateDir, file);
@@ -384,14 +366,6 @@ public static void cleanupOldFiles(
384366
}
385367
}
386368

387-
/**
388-
* Finds state file with maximum id.
389-
*
390-
* @param prefix - filename prefix
391-
* @param locations - paths to directories with state folder
392-
* @return maximum id of state file or -1 if no such files are found
393-
* @throws IOException if IOException occurs
394-
*/
395369
long findMaxGenerationId(final String prefix, Path... locations) throws IOException {
396370
long maxId = -1;
397371
for (Path dataLocation : locations) {
@@ -430,10 +404,6 @@ List<Path> findStateFilesByGeneration(final long generation, Path... locations)
430404
}
431405

432406
public String getStateFileName(long generation) {
433-
return getStateFileName(prefix, generation);
434-
}
435-
436-
public static String getStateFileName(String prefix, long generation) {
437407
return prefix + generation + STATE_FILE_EXTENSION;
438408
}
439409

0 commit comments

Comments
 (0)