db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r1151614 - in /db/derby/code/branches/10.8: ./ java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java
Date Wed, 27 Jul 2011 20:19:20 GMT
Author: dag
Date: Wed Jul 27 20:19:20 2011
New Revision: 1151614

URL: http://svn.apache.org/viewvc?rev=1151614&view=rev
Log:
DERBY-5325 Checkpoint fails with ClosedChannelException in InterruptResilienceTest

Back-ported from trunk as:

  svn merge -c 1151612 https://svn.apache.org/repos/asf/db/derby/code/trunk

Patch derby-5325-refactor-b, which refactors redundant code and cleans
up some comments and Javadoc.

Modified:
    db/derby/code/branches/10.8/   (props changed)
    db/derby/code/branches/10.8/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java

Propchange: db/derby/code/branches/10.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jul 27 20:19:20 2011
@@ -1,2 +1,2 @@
 /db/derby/code/branches/10.7:1061570,1061578,1082235
-/db/derby/code/trunk:1063809,1088633,1091000,1091221,1091285,1092067,1092795,1094315,1094572,1094728,1096741,1096890,1097247,1097249,1097460,1097469,1097471,1101059,1101839,1102620,1102826,1103681,1103718,1103742,1125305,1126358,1126468,1127825,1127883,1129136,1129764,1129797,1130077,1130084,1130632,1130895,1131030,1131272,1132546,1132664,1132860,1132928,1133304,1133317,1133741,1133752,1136363,1136371,1136397,1136844,1138201,1138341,1138444,1138787,1138795,1139449,1139451,1141924,1142583,1142635,1146915,1146962,1148344,1148354,1148658,1151101
+/db/derby/code/trunk:1063809,1088633,1091000,1091221,1091285,1092067,1092795,1094315,1094572,1094728,1096741,1096890,1097247,1097249,1097460,1097469,1097471,1101059,1101839,1102620,1102826,1103681,1103718,1103742,1125305,1126358,1126468,1127825,1127883,1129136,1129764,1129797,1130077,1130084,1130632,1130895,1131030,1131272,1132546,1132664,1132860,1132928,1133304,1133317,1133741,1133752,1136363,1136371,1136397,1136844,1138201,1138341,1138444,1138787,1138795,1139449,1139451,1141924,1142583,1142635,1146915,1146962,1148344,1148354,1148658,1151101,1151612

Modified: db/derby/code/branches/10.8/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.8/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java?rev=1151614&r1=1151613&r2=1151614&view=diff
==============================================================================
--- db/derby/code/branches/10.8/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java
(original)
+++ db/derby/code/branches/10.8/java/engine/org/apache/derby/impl/store/raw/data/RAFContainer4.java
Wed Jul 27 20:19:20 2011
@@ -292,7 +292,7 @@ class RAFContainer4 extends RAFContainer
         //
         // we cannot grab channelCleanupMonitor lest another thread is one
         // doing recovery, since the recovery thread will try to grab both
-        // those monitors during container resurrection.  So, just forge ahead
+        // those monitors during container recovery.  So, just forge ahead
         // in stealth mode (i.e. the recovery thread doesn't see us). If we see
         // ClosedChannelException, throw InterruptDetectedException, so we can
         // retry from RAFContainer releasing "this", or FileContainer
@@ -371,52 +371,8 @@ class RAFContainer4 extends RAFContainer
 
                 success = true;
 
-          //} catch (ClosedByInterruptException e) {
-          // Java NIO Bug 6979009:
-          // http://bugs.sun.com/view_bug.do?bug_id=6979009
-          // Sometimes NIO throws AsynchronousCloseException instead of
-          // ClosedByInterruptException
-            } catch (AsynchronousCloseException e) {
-                // Subsumes ClosedByInterruptException
-
-                // The interrupted thread may or may not get back here
-                // before other concurrent writers that will see
-                // ClosedChannelException, we have logic to handle that.
-                if (Thread.currentThread().isInterrupted()) {
-                    // Normal case
-                    if (recoverContainerAfterInterrupt(
-                                e.toString(),
-                                stealthMode)) {
-                        continue; // do I/O over again
-                    }
-                }
-
-
-                // Recovery is in progress, wait for another
-                // interrupted thread to clean up, i.e. act as if we
-                // had seen ClosedChannelException.
-
-                awaitRestoreChannel(e, stealthMode);
-
             } catch (ClosedChannelException e) {
-                // We are not the thread that first saw the channel interrupt,
-                // so no recovery attempt.
-
-                // if we also have seen an interrupt, we might as well take
-                // notice now.
-                InterruptStatus.noteAndClearInterrupt(
-                    "readPage in ClosedChannelException",
-                    threadsInPageIO,
-                    hashCode());
-
-                // Recovery is in progress, wait for another interrupted thread
-                // to clean up.
-                awaitRestoreChannel(e, stealthMode);
-
-                if (retries-- == 0) {
-                    throw StandardException.newException(
-                        SQLState.FILE_IO_INTERRUPTED);
-                }
+                handleClosedChannel(e, stealthMode, retries--);
             }
         }
       } finally {
@@ -522,7 +478,7 @@ class RAFContainer4 extends RAFContainer
         //
         // we cannot grab channelCleanupMonitor lest another thread is one
         // doing recovery, since the recovery thread will try to grab both
-        // those monitors during container resurrection.  So, just forge ahead
+        // those monitors during container recovery.  So, just forge ahead
         // in stealth mode (i.e. the recovery thread doesn't see us). If we see
         // ClosedChannelException, throw InterruptDetectedException, so we can
         // retry from FileContainer releasing allocCache, so the recovery
@@ -587,46 +543,8 @@ class RAFContainer4 extends RAFContainer
 
                 success = true;
 
-          //} catch (ClosedByInterruptException e) {
-          // Java NIO Bug 6979009:
-          // http://bugs.sun.com/view_bug.do?bug_id=6979009
-          // Sometimes NIO throws AsynchronousCloseException instead of
-          // ClosedByInterruptException
-            } catch (AsynchronousCloseException e) {
-                // Subsumes ClosedByInterruptException
-
-                // The interrupted thread may or may not get back here
-                // before other concurrent writers that will see
-                // ClosedChannelException, we have logic to handle that.
-
-                if (Thread.currentThread().isInterrupted()) {
-                    // Normal case
-                    if (recoverContainerAfterInterrupt(
-                                e.toString(),
-                                stealthMode)) {
-                        continue; // do I/O over again
-                    }
-                }
-                // Recovery is in progress, wait for another
-                // interrupted thread to clean up, i.e. act as if we
-                // had seen ClosedChannelException.
-
-                awaitRestoreChannel(e, stealthMode);
-
             } catch (ClosedChannelException e) {
-                // We are not the thread that first saw the channel interrupt,
-                // so no recovery attempt.
-
-                InterruptStatus.noteAndClearInterrupt(
-                    "writePage in ClosedChannelException",
-                    threadsInPageIO,
-                    hashCode());
-
-                awaitRestoreChannel(e, stealthMode);
-                if (retries-- == 0) {
-                    throw StandardException.newException(
-                        SQLState.FILE_IO_INTERRUPTED);
-                }
+                handleClosedChannel(e, stealthMode, retries--);
             }
         }
       } finally {
@@ -641,6 +559,85 @@ class RAFContainer4 extends RAFContainer
     }
 
     /**
+     * This method handles what to do when, during a NIO operation we receive a
+     * {@code ClosedChannelException}. Note the specialization hierarchy:
+     * <p/>
+     * {@code ClosedChannelException} -> {@code AsynchronousCloseException} ->
+     * {@code ClosedByInterruptException}
+     * <p/>
+     * If {@code e} is a ClosedByInterruptException, we normally start
+     * container recovery, i.e. we need to reopen the random access file so we
+     * get get a new interruptible channel and continue IO.
+     * <p/>
+     * If {@code e} is a {@code AsynchronousCloseException} or a plain {@code
+     * ClosedChannelException}, the behavior depends of {@code stealthMode}:
+     * <p/>
+     * If {@code stealthMode == false}, the method will wait for
+     * another thread tp finish recovering the IO channel before returning.
+     * <p/>
+     * If {@code stealthMode == true}, the method throws {@code
+     * InterruptDetectedException}, allowing retry at a higher level in the
+     * code.  The reason for this is that we sometimes need to release monitors
+     * on objects needed by the recovery thread.
+     *
+     * @param e Should be an instance of {@code ClosedChannelException}.
+     * @param stealthMode If {@code true}, do retry at a higher level
+     * @param retries Give up waiting for another thread to reopen the channel
+     *                when {@code retries} reaches 0. Only applicable if {@code
+     *                stealthMode == false}.
+     * @throws InterruptDetectedException if retry at higher level is required
+     *         {@code stealthMode == true}.
+     * @throws StandardException standard error policy, incl. when we give up
+     *                           waiting for another thread to reopen channel
+     */
+    private void handleClosedChannel(ClosedChannelException e,
+                                     boolean stealthMode,
+                                     int retries)
+            throws StandardException {
+
+        // if (e instanceof ClosedByInterruptException e) {
+        // Java NIO Bug 6979009:
+        // http://bugs.sun.com/view_bug.do?bug_id=6979009
+        // Sometimes NIO throws AsynchronousCloseException instead of
+        // ClosedByInterruptException
+
+        if (e instanceof AsynchronousCloseException) {
+            // Subsumes ClosedByInterruptException
+
+            // The interrupted thread may or may not get back here to try
+            // recovery before other concurrent IO threads will see (the
+            // secondary) ClosedChannelException, but we have logic to handle
+            // that, cf threadsInPageIO.
+
+            if (Thread.currentThread().isInterrupted()) {
+                if (recoverContainerAfterInterrupt(
+                            e.toString(),
+                            stealthMode)) {
+                    return; // do I/O over again
+                }
+            }
+
+            // Recovery is in progress, wait for another interrupted thread to
+            // clean up.
+
+            awaitRestoreChannel(e, stealthMode);
+        } else {
+            // According to the exception type, We are not the thread that
+            // first saw the channel interrupt, so no recovery attempt.
+            InterruptStatus.noteAndClearInterrupt(
+                "ClosedChannelException",
+                threadsInPageIO,
+                hashCode());
+
+            awaitRestoreChannel(e, stealthMode);
+            if (retries == 0) {
+                throw StandardException.newException(
+                    SQLState.FILE_IO_INTERRUPTED);
+            }
+        }
+    }
+
+    /**
      * Use when seeing an exception during IO and when another thread is
      * presumably doing the recovery.
      * <p/>
@@ -659,10 +656,10 @@ class RAFContainer4 extends RAFContainer
      * If for some reason the recovering thread has given up on resurrecting
      * the container, cf {@code #giveUpIO}, the method throws {@code
      * FILE_IO_INTERRUPTED}.
-     * 
+     *
      * @param e the exception we saw during IO
      * @param stealthMode true if the thread doing IO in stealth mode
-
+     *
      * @throws StandardException {@code InterruptDetectedException} and normal
      *                            error policy
      */
@@ -690,7 +687,7 @@ class RAFContainer4 extends RAFContainer
                             "giving up retry, another thread gave up " +
                             "resurrecting container ");
                     }
-                
+
                     throw StandardException.newException(
                         SQLState.FILE_IO_INTERRUPTED);
                 }
@@ -707,7 +704,7 @@ class RAFContainer4 extends RAFContainer
             threadsInPageIO--;
         }
 
-        // Wait here till the interrupted thread does container resurrection.
+        // Wait here till the interrupted thread does container recovery.
         // If we get a channel exception for some other reason, this will never
         // happen, so throw after waiting long enough (60s).
 
@@ -776,10 +773,11 @@ class RAFContainer4 extends RAFContainer
 
 
     /**
-     * Use this when the thread has received a AsynchronousCloseException
+     * Use this when the thread has received a ClosedByInterruptException (or,
+     * prior to JDK 1.7 it may also be AsynchronousCloseException - a bug)
      * exception during IO and its interruped flag is also set. This makes this
-     * thread a likely candicate to do container recovery (aka resurrection),
-     * unless another thread started it already, cf. return value.
+     * thread a likely candicate to do container recovery, unless another
+     * thread started it already, cf. return value.
      *
      * @param whence caller site (debug info)
      * @param stealthMode don't update threadsInPageIO if true
@@ -1072,9 +1070,13 @@ class RAFContainer4 extends RAFContainer
     }
 
     /**
-     * Write a sequence of bytes at the given offset in a file.
+     * Write a sequence of bytes at the given offset in a file.  This method
+     * operates in <em>stealth mode</em>, see doc for {@link
+     * #handleClosedChannel handleClosedChannel}.
+     * This presumes that IO retry happens at a higher level, i.e. the
+     * caller(s) must be prepared to handle {@code InterruptDetectedException}.
      * <p/>
-     * override of FileContainer#writeAtOffset
+     * This method overrides FileContainer#writeAtOffset.
      * <p/>
      * @param file the file to write to
      * @param bytes the bytes to write
@@ -1094,7 +1096,7 @@ class RAFContainer4 extends RAFContainer
         ourChannel = ioChannel;
 
         boolean success = false;
-        boolean stealthMode = true;
+        final boolean stealthMode = true;
 
         while (!success) {
 
@@ -1107,45 +1109,8 @@ class RAFContainer4 extends RAFContainer
             try {
                 writeFull(ByteBuffer.wrap(bytes), ioChannel, offset);
                 success = true;
-            //} catch (ClosedByInterruptException e) {
-            // Java NIO Bug 6979009:
-            // http://bugs.sun.com/view_bug.do?bug_id=6979009
-            // Sometimes NIO throws AsynchronousCloseException instead of
-            // ClosedByInterruptException
-            } catch (AsynchronousCloseException e) {
-                // Subsumes ClosedByInterruptException
-
-                // The interrupted thread may or may not get back here
-                // before other concurrent writers that will see
-                // ClosedChannelException, we have logic to handle that.
-
-                if (Thread.currentThread().isInterrupted()) {
-                    // Normal case
-                    if (recoverContainerAfterInterrupt(
-                                e.toString(),
-                                stealthMode)) {
-                        continue; // do I/O over again
-                    }
-                }
-                // Recovery is in progress, wait for another
-                // interrupted thread to clean up, i.e. act as if we
-                // had seen ClosedChannelException.
-
-                // stealthMode == true, so this will throw
-                // InterruptDetectedException
-                awaitRestoreChannel(e, stealthMode);
             } catch (ClosedChannelException e) {
-                // We are not the thread that first saw the channel interrupt,
-                // so no recovery attempt.
-
-                InterruptStatus.noteAndClearInterrupt(
-                    "writeAtOffset in ClosedChannelException",
-                    threadsInPageIO,
-                    hashCode());
-
-                // stealthMode == true, so this will throw
-                // InterruptDetectedException
-                awaitRestoreChannel(e, stealthMode);
+                handleClosedChannel(e, stealthMode, -1 /* NA */);
             }
         }
     }



Mime
View raw message