diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java index a5a2e8cdd223e..7857c6430a5e3 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java @@ -35,14 +35,10 @@ import org.opensearch.monitor.fs.FsInfo; import org.opensearch.repositories.fs.FsRepository; -import java.io.IOException; import java.nio.file.Files; -import java.nio.file.NoSuchFileException; import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.util.List; import java.util.Map; -import java.util.stream.Stream; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; @@ -559,22 +555,15 @@ public void testCacheFilesAreClosedAfterUse() throws Exception { // The local cache files should be closed by deleting the restored index deleteIndicesAndEnsureGreen(client, restoredIndexName); - logger.info("--> validate all the cache files are closed"); + logger.info("--> validate cache file path is deleted"); // Get path of cache files final NodeEnvironment nodeEnv = internalCluster().getInstance(NodeEnvironment.class); Path fileCachePath = nodeEnv.fileCacheNodePath().fileCachePath; - // Find all the files in the path - try (Stream paths = Files.walk(fileCachePath)) { - paths.filter(Files::isRegularFile).forEach(path -> { - // Testing moving the file to check the file is closed or not. - try { - Files.move(path, path, StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - fail("No exception is expected. The file can't be moved, so it may not be closed."); - } - }); - } catch (NoSuchFileException e) { - logger.debug("--> the path for the cache files doesn't exist"); + + if (Files.exists(fileCachePath)) { + fail("Cache file path isn't deleted."); + } else { + logger.info("--> validated that the cache file path doesn't exist"); } } } diff --git a/server/src/main/java/org/opensearch/index/store/remote/filecache/FileCacheCleaner.java b/server/src/main/java/org/opensearch/index/store/remote/filecache/FileCacheCleaner.java index e94de6a9d5b33..838e6f2bf2fd2 100644 --- a/server/src/main/java/org/opensearch/index/store/remote/filecache/FileCacheCleaner.java +++ b/server/src/main/java/org/opensearch/index/store/remote/filecache/FileCacheCleaner.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; +import org.opensearch.common.io.FileSystemUtils; import org.opensearch.common.settings.Settings; import org.opensearch.env.NodeEnvironment; import org.opensearch.index.IndexModule; @@ -60,6 +61,7 @@ public void beforeIndexShardDeleted(ShardId shardId, Settings settings) { fileCache.remove(subPath.toRealPath()); } } + FileSystemUtils.deleteSubDirectories(shardPath.getRootDataPath()); } } catch (IOException ioe) { log.error(() -> new ParameterizedMessage("Error removing items from cache during shard deletion {})", shardId), ioe);