Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-5570: don't let fsync create new zero-byte files

Robert Muirf8c30e27ae29
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7fd40137df9..8fa367f320c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -243,6 +243,9 @@ Bug fixes
directory outside of Lucene. (Simon Willnauer, Shai Erera, Robert
Muir, Mike McCandless)
+* LUCENE-5570: Don't let FSDirectory.sync() create new zero-byte files, instead throw
+ exception if a file is missing. (Uwe Schindler, Mike McCandless, Robert Muir)
+
Test Framework
* LUCENE-5567: When a suite fails with zombie threads failure marker and count
diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
index 262330decf6..ff8fae9c0dd 100644
--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
@@ -22,6 +22,8 @@ import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
import java.util.Collection;
import static java.util.Collections.synchronizedSet;
@@ -402,11 +404,11 @@ public abstract class FSDirectory extends BaseDirectory {
IOException exc = null;
while (!success && retryCount < 5) {
retryCount++;
- RandomAccessFile file = null;
+ FileChannel file = null;
try {
try {
- file = new RandomAccessFile(fullFile, "rw");
- file.getFD().sync();
+ file = FileChannel.open(fullFile.toPath(), StandardOpenOption.WRITE);
+ file.force(true); // TODO: we probably dont care about metadata, but this is what we did before...
success = true;
} finally {
if (file != null)
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
index 6f107b493b3..7e0b4d308c2 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
@@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
+import java.util.Collections;
import org.apache.lucene.store.MockDirectoryWrapper.Throttling;
import org.apache.lucene.util.LuceneTestCase;
@@ -287,5 +288,34 @@ public class TestDirectory extends LuceneTestCase {
TestUtil.rmDir(path);
}
}
+
+ public void testFsyncDoesntCreateNewFiles() throws Exception {
+ File path = TestUtil.getTempDir("nocreate");
+ Directory fsdir = new SimpleFSDirectory(path);
+
+ // write a file
+ IndexOutput out = fsdir.createOutput("afile", newIOContext(random()));
+ out.writeString("boo");
+ out.close();
+
+ // delete it
+ assertTrue(new File(path, "afile").delete());
+
+ // directory is empty
+ assertEquals(0, fsdir.listAll().length);
+
+ // fsync it
+ try {
+ fsdir.sync(Collections.singleton("afile"));
+ fail("didn't get expected exception, instead fsync created new files: " + Arrays.asList(fsdir.listAll()));
+ } catch (FileNotFoundException | NoSuchFileException expected) {
+ // ok
+ }
+
+ // directory is still empty
+ assertEquals(0, fsdir.listAll().length);
+
+ fsdir.close();
+ }
}