Skip to content

Apache Lucene open-source search software

LUCENE-4976: add missing sync / delete old save files

Michael McCandless8c6e38ee6054
diff --git a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
index c9f31aad1ad..052302d469c 100644
--- a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
@@ -19,6 +19,7 @@ package org.apache.lucene.index;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
@@ -201,6 +202,15 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {
}
}
+ dir.sync(Collections.singletonList(fileName));
+
+ if (nextWriteGen > 0) {
+ String lastSaveFile = SNAPSHOTS_PREFIX + (nextWriteGen-1);
+ if (dir.fileExists(lastSaveFile)) {
+ dir.deleteFile(lastSaveFile);
+ }
+ }
+
nextWriteGen++;
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java
index 52441f7319f..82183ae06f8 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java
@@ -49,7 +49,7 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPo
@Test
public void testExistingSnapshots() throws Exception {
int numSnapshots = 3;
- Directory dir = newDirectory();
+ MockDirectoryWrapper dir = newMockDirectory();
IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(dir)));
PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
assertNull(psdp.getLastSaveFile());
@@ -57,6 +57,19 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPo
assertNotNull(psdp.getLastSaveFile());
writer.close();
+ // Make sure only 1 save file exists:
+ int count = 0;
+ for(String file : dir.listAll()) {
+ if (file.startsWith(PersistentSnapshotDeletionPolicy.SNAPSHOTS_PREFIX)) {
+ count++;
+ }
+ }
+ assertEquals(1, count);
+
+ // Make sure we fsync:
+ dir.crash();
+ dir.clearCrash();
+
// Re-initialize and verify snapshots were persisted
psdp = new PersistentSnapshotDeletionPolicy(
new KeepOnlyLastCommitDeletionPolicy(), dir, OpenMode.APPEND);