Apache Lucene open-source search software
LUCENE-2239: Documented NIOFSDirectory and MMapDirectory limitations due to Java NIO behavior when a Thread is interrupted while blocking on IO.
Simon Willnauerde49549d8895
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex b2f81c63ebb..1951c442b87 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -226,6 +226,10 @@ Documentation* LUCENE-2579: Fix oal.search's package.html description of abstractmethods. (Santiago M. Mola via Mike McCandless)++* LUCENE-2239: Documented limitations in NIOFSDirectory and MMapDirectory due to+ Java NOI behavior when a Thread is interrupted while blocking on IO.+ (Simon Willnauer, Robert Muir)======================= Lucene 3.x (not yet released) =======================diff --git a/lucene/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/src/java/org/apache/lucene/store/FSDirectory.javaindex d04b77d9a61..b7ffb54db86 100644--- a/lucene/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/src/java/org/apache/lucene/store/FSDirectory.java@@ -31,6 +31,7 @@ import java.util.Collections;import static java.util.Collections.synchronizedSet;import java.util.HashSet;import java.util.Set;+import java.util.concurrent.Future;import org.apache.lucene.store.SimpleFSDirectory.SimpleFSIndexInput;import org.apache.lucene.util.ThreadInterruptedException;@@ -58,7 +59,12 @@ import org.apache.lucene.util.Constants;* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6265734">Sun* JRE bug</a> this is a poor choice for Windows, but* on all other platforms this is the preferred- * choice.+ * choice. Applications using {@link Thread#interrupt()} or+ * {@link Future#cancel(boolean)} should use+ * {@link SimpleFSDirectory} instead. See {@link NIOFSDirectory} java doc+ * for details.+ *+ *** <li> {@link MMapDirectory} uses memory-mapped IO when* reading. This is a good choice if you have plenty@@ -84,6 +90,11 @@ import org.apache.lucene.util.Constants;* an important limitation to be aware of. This class supplies a* (possibly dangerous) workaround mentioned in the bug report,* which may fail on non-Sun JVMs.+ *+ * Applications using {@link Thread#interrupt()} or+ * {@link Future#cancel(boolean)} should use+ * {@link SimpleFSDirectory} instead. See {@link MMapDirectory}+ * java doc for details.* </ul>** Unfortunately, because of system peculiarities, there is@@ -175,7 +186,9 @@ public abstract class FSDirectory extends Directory {** <p>Currently this returns {@link NIOFSDirectory}* on non-Windows JREs and {@link SimpleFSDirectory}- * on Windows.+ * on Windows. It is highly recommended that you consult the+ * implementation's documentation for your platform before+ * using this method.** <p><b>NOTE</b>: this method may suddenly change which* implementation is returned from release to release, indiff --git a/lucene/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/src/java/org/apache/lucene/store/MMapDirectory.javaindex dc7a7855ef6..a9346ecf9a6 100644--- a/lucene/src/java/org/apache/lucene/store/MMapDirectory.java+++ b/lucene/src/java/org/apache/lucene/store/MMapDirectory.java@@ -68,6 +68,13 @@ import org.apache.lucene.util.Constants;* an undocumented internal cleanup functionality.* {@link #UNMAP_SUPPORTED} is <code>true</code>, if the workaround* can be enabled (with no guarantees).+ * <p>+ * <b>NOTE:</b> Accessing this class either directly or+ * indirectly from a thread while it's interrupted can close the+ * underlying channel immediately if at the same time the thread is+ * blocked on IO. The channel will remain closed and subsequent access+ * to {@link MMapDirectory} will throw a {@link ClosedChannelException}.+ * </p>*/public class MMapDirectory extends FSDirectory {private boolean useUnmapHack = false;diff --git a/lucene/src/java/org/apache/lucene/store/NIOFSDirectory.java b/lucene/src/java/org/apache/lucene/store/NIOFSDirectory.javaindex 69d1fbead04..5e28cf501aa 100644--- a/lucene/src/java/org/apache/lucene/store/NIOFSDirectory.java+++ b/lucene/src/java/org/apache/lucene/store/NIOFSDirectory.java@@ -21,22 +21,32 @@ import java.io.File;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;+import java.util.concurrent.Future; // javadoc/**- * An {@link FSDirectory} implementation that uses- * java.nio's FileChannel's positional read, which allows- * multiple threads to read from the same file without- * synchronizing.- *- * <p>This class only uses FileChannel when reading; writing- * is achieved with {@link FSDirectory.FSIndexOutput}.- *- * <p><b>NOTE</b>: NIOFSDirectory is not recommended on Windows because of a bug- * in how FileChannel.read is implemented in Sun's JRE.- * Inside of the implementation the position is apparently- * synchronized. See <a+ * An {@link FSDirectory} implementation that uses java.nio's FileChannel's+ * positional read, which allows multiple threads to read from the same file+ * without synchronizing.+ * <p>+ * This class only uses FileChannel when reading; writing is achieved with+ * {@link SimpleFSDirectory.SimpleFSIndexOutput}.+ * <p>+ * <b>NOTE</b>: NIOFSDirectory is not recommended on Windows because of a bug in+ * how FileChannel.read is implemented in Sun's JRE. Inside of the+ * implementation the position is apparently synchronized. See <a* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6265734">here</a>* for details.+ * </p>+ * <p>+ * <font color="red"><b>NOTE:</b> Accessing this class either directly or+ * indirectly from a thread while it's interrupted can close the+ * underlying file descriptor immediately if at the same time the thread is+ * blocked on IO. The file descriptor will remain closed and subsequent access+ * to {@link NIOFSDirectory} will throw a {@link ClosedChannelException}. If+ * your application uses either {@link Thread#interrupt()} or+ * {@link Future#cancel(boolean)} you should use {@link SimpleFSDirectory} in+ * favor of {@link NIOFSDirectory}.</font>+ * </p>*/public class NIOFSDirectory extends FSDirectory {