db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r1073595 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/services/memory/ iapi/util/ impl/db/ impl/jdbc/ impl/services/daemon/ impl/services/monitor/ impl/store/replication/net/
Date Wed, 23 Feb 2011 05:05:16 GMT
Author: dag
Date: Wed Feb 23 05:05:15 2011
New Revision: 1073595

URL: http://svn.apache.org/viewvc?rev=1073595&view=rev
Log:
DERBY-4741 Make embedded Derby work reliably in the presence of thread interrupts

Patch derby-4741-sleeps-waits-more, which "regularizes" a few more
instances of interrrupt handling to follow the idiom established in
this issue's patches. 

This leaves a few instances in BasicDaemon.java (as far as embedded
code is concerned), which will need more consideration. In any case,
interrupting the demon threads is less of a valid use case I believe,
i.e. Derby's ability to tolerate that is less crucial that tolerating
interrupts to the user's connection threads.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/memory/LowMemory.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/InterruptStatus.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/db/SlaveDatabase.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/daemon/IndexStatisticsDaemonImpl.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/TopService.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/replication/net/ReplicationMessageTransmit.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/memory/LowMemory.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/memory/LowMemory.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/memory/LowMemory.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/memory/LowMemory.java Wed
Feb 23 05:05:15 2011
@@ -90,14 +90,21 @@ public class LowMemory {
             // up some memory that throws off our calcuation. This is
             // avoided by clearing lowMemory some time later on an
             // isLowMemory() call.
+            boolean interrupted = false;
+
             for (int i = 0; i < 5; i++) {
                 System.gc();
                 System.runFinalization();
                 try {
                     Thread.sleep(50L);
                 } catch (InterruptedException e) {
+                    interrupted = true;
                 }
             }
+            if (interrupted) {
+                // reinstate flag
+                Thread.currentThread().interrupt();
+            }
         }
         synchronized (this) {
             if (lowMemory == 0L) {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/InterruptStatus.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/InterruptStatus.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/InterruptStatus.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/InterruptStatus.java Wed Feb
23 05:05:15 2011
@@ -58,7 +58,7 @@ public class InterruptStatus {
 
     /**
      * Use thread local variable to store interrupt status flag *only* if we
-     * don't have lcc, e.g. during database creation.
+     * don't have lcc, e.g. during database creation, shutdown etc.
      */
     private static final ThreadLocal exception = new ThreadLocal ();
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/db/SlaveDatabase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/db/SlaveDatabase.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/db/SlaveDatabase.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/db/SlaveDatabase.java Wed Feb 23
05:05:15 2011
@@ -30,6 +30,7 @@ import org.apache.derby.iapi.jdbc.Authen
 import org.apache.derby.iapi.services.context.ContextManager;
 import org.apache.derby.iapi.services.context.ContextService;
 import org.apache.derby.iapi.services.monitor.Monitor;
+import org.apache.derby.iapi.util.InterruptStatus;
 import org.apache.derby.iapi.store.replication.slave.SlaveFactory;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 
@@ -271,7 +272,7 @@ public class SlaveDatabase extends Basic
                 try {
                     Thread.sleep(500);
                 } catch (InterruptedException ie) {
-                // do nothing
+                    InterruptStatus.setInterrupted();
                 }
             }
         } else {
@@ -347,7 +348,7 @@ public class SlaveDatabase extends Basic
                 try {
                     Thread.sleep(500);
                 } catch (InterruptedException ie) {
-                    // do nothing
+                    InterruptStatus.setInterrupted();
                 }
             }
         }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java Wed Feb
23 05:05:15 2011
@@ -2614,22 +2614,27 @@ public abstract class EmbedConnection im
     }
 
     /**
-     * Puts the current thread to sleep and sets the interrupt flag of the
-     * thread if an {@code InterruptedException} is thrown while sleeping.
+     * Puts the current thread to sleep.
      * <p>
-     * <em>NOTE</em>: This method does not guarantee that the thread sleeps at
+     * <em>NOTE</em>: This method guarantees that the thread sleeps at
      * least {@code millis} milliseconds.
      *
      * @param millis milliseconds to sleep
      */
     private static void sleep(long millis) {
-        try {
-            Thread.sleep(millis);
-        } catch (InterruptedException ie) {
-            // Set the interrupt flag of the thread to allow code higher up the
-            // stack to detect the interruption.
-            Thread.currentThread().interrupt();
+        long startMillis = System.currentTimeMillis();
+        long waited = 0L;
+        while (waited < millis) {
+            try {
+                Thread.sleep(millis - waited);
+            } catch (InterruptedException ie) {
+                InterruptStatus.setInterrupted();
+                waited = System.currentTimeMillis() - startMillis;
+                continue;
+            }
+            break;
         }
+
     }
 
     /**

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/daemon/IndexStatisticsDaemonImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/daemon/IndexStatisticsDaemonImpl.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/daemon/IndexStatisticsDaemonImpl.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/daemon/IndexStatisticsDaemonImpl.java
Wed Feb 23 05:05:15 2011
@@ -51,6 +51,7 @@ import org.apache.derby.iapi.store.acces
 import org.apache.derby.iapi.store.access.ScanController;
 import org.apache.derby.iapi.store.access.TransactionController;
 import org.apache.derby.iapi.types.DataValueDescriptor;
+import org.apache.derby.iapi.util.InterruptStatus;
 
 /**
  * Daemon acting as a coordinator for creating and updating index cardinality
@@ -959,8 +960,7 @@ public class IndexStatisticsDaemonImpl
         try {
             Thread.sleep(ms);
         } catch (InterruptedException ie) {
-            // Set the interrupt flag again.
-            Thread.currentThread().interrupt();
+            InterruptStatus.setInterrupted();
         }
     }
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/TopService.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/TopService.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/TopService.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/TopService.java
Wed Feb 23 05:05:15 2011
@@ -28,6 +28,7 @@ import org.apache.derby.iapi.services.mo
 import org.apache.derby.iapi.error.StandardException;
 
 import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.util.InterruptStatus;
 import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.reference.EngineType;
 
@@ -187,7 +188,7 @@ final class TopService {
 				try {
 					wait();
 				} catch (InterruptedException ioe) {
-					return false;
+                    InterruptStatus.setInterrupted();
 				}
 			}
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/replication/net/ReplicationMessageTransmit.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/replication/net/ReplicationMessageTransmit.java?rev=1073595&r1=1073594&r2=1073595&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/replication/net/ReplicationMessageTransmit.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/replication/net/ReplicationMessageTransmit.java
Wed Feb 23 05:05:15 2011
@@ -30,6 +30,7 @@ import java.security.PrivilegedException
 import javax.net.SocketFactory;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.reference.SQLState;
+import org.apache.derby.iapi.util.InterruptStatus;
 import org.apache.derby.shared.common.reference.MessageId;
 
 /**
@@ -202,12 +203,25 @@ public class ReplicationMessageTransmit 
         receivedMsg = null;
         checkSocketConnection();
         socketConn.writeMessage(message);
-        synchronized (receiveSemaphore) {
-            try {
-                receiveSemaphore.wait(DEFAULT_MESSAGE_RESPONSE_TIMEOUT);
-            } catch (InterruptedException ie) {
+        long startMillis = System.currentTimeMillis();
+        long waited = 0L;
+
+        while (receivedMsg == null &&
+               waited < DEFAULT_MESSAGE_RESPONSE_TIMEOUT) {
+
+            synchronized (receiveSemaphore) {
+                try {
+                    receiveSemaphore.wait(
+                        DEFAULT_MESSAGE_RESPONSE_TIMEOUT - waited);
+                } catch (InterruptedException ie) {
+                    InterruptStatus.setInterrupted();
+                    waited = System.currentTimeMillis() - startMillis;
+                    continue;
+                }
+                break;
             }
         }
+
         if (receivedMsg == null) {
             throw StandardException.
                 newException(SQLState.REPLICATION_CONNECTION_LOST, dbname);



Mime
View raw message