diff --git a/server/src/main/java/org/opensearch/index/engine/NRTReplicationEngine.java b/server/src/main/java/org/opensearch/index/engine/NRTReplicationEngine.java index 4b5160838dfca..472269641ceb2 100644 --- a/server/src/main/java/org/opensearch/index/engine/NRTReplicationEngine.java +++ b/server/src/main/java/org/opensearch/index/engine/NRTReplicationEngine.java @@ -126,7 +126,7 @@ private NRTReplicationReaderManager buildReaderManager() throws IOException { (files) -> { store.decRefFileDeleter(files); try { - store.cleanupAndPreserveLatestCommitPoint("On reader closed", getLatestSegmentInfos()); + store.cleanupAndPreserveLatestCommitPoint("On reader closed", getLatestSegmentInfos(), getLastCommittedSegmentInfos()); } catch (IOException e) { // Log but do not rethrow - we can try cleaning up again after next replication cycle. // If that were to fail, the shard will as well. 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 0beffc0824d07..f651fb1e23d56 100644 --- a/server/src/main/java/org/opensearch/index/store/Store.java +++ b/server/src/main/java/org/opensearch/index/store/Store.java @@ -798,12 +798,17 @@ public void cleanupAndVerify(String reason, MetadataSnapshot sourceMetadata) thr * @throws IllegalStateException if the latest snapshot in this store differs from the given one after the cleanup. */ public void cleanupAndPreserveLatestCommitPoint(String reason, SegmentInfos infos) throws IOException { + this.cleanupAndPreserveLatestCommitPoint(reason, infos, readLastCommittedSegmentsInfo()); + } + + 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, getMetadata(readLastCommittedSegmentsInfo()), infos.files(true)); + cleanupFiles(reason, getMetadata(lastCommittedSegmentInfos), infos.files(true)); } finally { metadataLock.writeLock().unlock(); }