Apache Lucene open-source search software
LUCENE-5585: Add IOUtils.fsync
Simon Willnauerfa4669caf92d
diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.javaindex ff8fae9c0dd..074fae633ec 100644--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java@@ -17,23 +17,20 @@ package org.apache.lucene.store;* limitations under the License.*/+import org.apache.lucene.util.Constants;+import org.apache.lucene.util.IOUtils;+import java.io.File;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;import java.util.HashSet;import java.util.Set;import java.util.concurrent.Future;-import org.apache.lucene.util.ThreadInterruptedException;-import org.apache.lucene.util.Constants;-import org.apache.lucene.util.IOUtils;+import static java.util.Collections.synchronizedSet;/*** Base class for Directory implementations that store index@@ -398,35 +395,6 @@ public abstract class FSDirectory extends BaseDirectory {}protected void fsync(String name) throws IOException {- File fullFile = new File(directory, name);- boolean success = false;- int retryCount = 0;- IOException exc = null;- while (!success && retryCount < 5) {- retryCount++;- FileChannel file = null;- try {- try {- 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)- file.close();- }- } catch (IOException ioe) {- if (exc == null)- exc = ioe;- try {- // Pause 5 msec- Thread.sleep(5);- } catch (InterruptedException ie) {- throw new ThreadInterruptedException(ie);- }- }- }- if (!success)- // Throw original exception- throw exc;+ IOUtils.fsync(new File(directory, name));}}diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.javaindex 2e183c42348..2b8d4a8c2cc 100644--- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java+++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java@@ -28,10 +28,12 @@ import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;+import java.nio.channels.FileChannel;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.nio.charset.CodingErrorAction;import java.nio.charset.StandardCharsets;+import java.nio.file.StandardOpenOption;/** This class emulates the new Java 7 "Try-With-Resources" statement.* Remove once Lucene is on Java 7.@@ -366,4 +368,34 @@ public final class IOUtils {throw new RuntimeException(th);}}++ /**+ * Ensure that any writes to the given file is written to the storage device that contains it.+ * @param fileToSync the file to fsync+ */+ public static void fsync(File fileToSync) throws IOException {+ IOException exc = null;+ for (int retry = 0; retry < 5; retry++) {+ try {+ try (FileChannel file = FileChannel.open(fileToSync.toPath(), StandardOpenOption.WRITE)) {+ file.force(true); // TODO: we probably dont care about metadata, but this is what we did before...+ return;+ }+ } catch (IOException ioe) {+ if (exc == null) {+ exc = ioe;+ }+ try {+ // Pause 5 msec+ Thread.sleep(5);+ } catch (InterruptedException ie) {+ ThreadInterruptedException ex = new ThreadInterruptedException(ie);+ ex.addSuppressed(exc);+ throw ex;+ }+ }+ }+ // Throw original exception+ throw exc;+ }}