diff --git a/server/src/main/java/org/opensearch/index/store/Store.java b/server/src/main/java/org/opensearch/index/store/Store.java index 62bb6050900df..e9c05768bc509 100644 --- a/server/src/main/java/org/opensearch/index/store/Store.java +++ b/server/src/main/java/org/opensearch/index/store/Store.java @@ -821,17 +821,14 @@ public void cleanupAndPreserveLatestCommitPoint(String reason, SegmentInfos info * * @throws IllegalStateException if the latest snapshot in this store differs from the given one after the cleanup. */ - public void cleanupAndPreserveLatestCommitPoint( - String reason, - SegmentInfos infos, - SegmentInfos lastCommittedSegmentInfos - ) throws IOException { + public void cleanupAndPreserveLatestCommitPoint(String reason, SegmentInfos infos, SegmentInfos lastCommittedSegmentInfos) + throws IOException { assert indexSettings.isSegRepEnabled(); // fetch a snapshot from the latest on disk Segments_N file. This can be behind // the passed in local in memory snapshot, so we want to ensure files it references are not removed. metadataLock.writeLock().lock(); try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) { - cleanupFiles(reason, lastCommittedSegmentInfos.files(true), infos.files(true)); + cleanupFiles(List.of(directory.listAll()), reason, lastCommittedSegmentInfos.files(true), infos.files(true)); } finally { metadataLock.writeLock().unlock(); } @@ -843,31 +840,34 @@ public void cleanupAndPreserveLatestCommitPoint( * Performs cleanup of un-referenced files intended to be used after reader close action * * @param reason Reason for cleanup - * @param filesToConsider Files to consider for clean up + * @param filesToCleanUp Files to consider for clean up * @throws IOException Exception from cleanup operation */ - public void cleanupUnReferencedFiles(String reason, Collection filesToConsider) throws IOException { + public void cleanupUnReferencedFiles(String reason, Collection filesToCleanUp) throws IOException { assert indexSettings.isSegRepEnabled(); metadataLock.writeLock().lock(); try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) { - cleanupFiles(reason, null, filesToConsider); + cleanupFiles(filesToCleanUp, reason, null, null); } finally { metadataLock.writeLock().unlock(); } } private void cleanupFiles( + Collection filesToCleanUp, String reason, Collection localSnapshot, @Nullable Collection additionalFiles ) throws IOException { assert metadataLock.isWriteLockedByCurrentThread(); - for (String existingFile : directory.listAll()) { + for (String existingFile : filesToCleanUp) { if (Store.isAutogenerated(existingFile) || localSnapshot != null && localSnapshot.contains(existingFile) || (additionalFiles != null && additionalFiles.contains(existingFile)) // also ensure we are not deleting a file referenced by an active reader. - || replicaFileTracker != null && replicaFileTracker.canDelete(existingFile) == false) { + || replicaFileTracker != null && replicaFileTracker.canDelete(existingFile) == false + // Prevent temporary replication files as it should be cleaned up MultiFileWriter + || existingFile.startsWith(REPLICATION_PREFIX)) { // don't delete snapshot file, or the checksums file (note, this is extra protection since the Store won't delete // checksum) continue;