hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rang...@apache.org
Subject svn commit: r637985 - in /hadoop/core/trunk: CHANGES.txt src/java/org/apache/hadoop/net/SocketIOWithTimeout.java src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java
Date Mon, 17 Mar 2008 17:40:02 GMT
Author: rangadi
Date: Mon Mar 17 10:40:00 2008
New Revision: 637985

URL: http://svn.apache.org/viewvc?rev=637985&view=rev
Log:
HADOOP-3008. SocketIOWithTimeout throws InterruptedIOException if the
thread is interrupted while it is waiting. (rangadi)

Modified:
    hadoop/core/trunk/CHANGES.txt
    hadoop/core/trunk/src/java/org/apache/hadoop/net/SocketIOWithTimeout.java
    hadoop/core/trunk/src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java

Modified: hadoop/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/CHANGES.txt?rev=637985&r1=637984&r2=637985&view=diff
==============================================================================
--- hadoop/core/trunk/CHANGES.txt (original)
+++ hadoop/core/trunk/CHANGES.txt Mon Mar 17 10:40:00 2008
@@ -257,6 +257,9 @@
     hodlib/Hod/hod.py for Python < 2.5.1.
     (Vinod Kumar Vavilapalli via ddas)
 
+    HADOOP-3008. SocketIOWithTimeout throws InterruptedIOException if the
+    thread is interrupted while it is waiting. (rangadi)
+    
 Release 0.16.1 - 2008-03-13
 
   INCOMPATIBLE CHANGES

Modified: hadoop/core/trunk/src/java/org/apache/hadoop/net/SocketIOWithTimeout.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/java/org/apache/hadoop/net/SocketIOWithTimeout.java?rev=637985&r1=637984&r2=637985&view=diff
==============================================================================
--- hadoop/core/trunk/src/java/org/apache/hadoop/net/SocketIOWithTimeout.java (original)
+++ hadoop/core/trunk/src/java/org/apache/hadoop/net/SocketIOWithTimeout.java Mon Mar 17 10:40:00
2008
@@ -19,16 +19,12 @@
 package org.apache.hadoop.net;
 
 import java.io.IOException;
-import java.net.Socket;
-import java.net.SocketAddress;
+import java.io.InterruptedIOException;
 import java.net.SocketTimeoutException;
 import java.nio.ByteBuffer;
-import java.nio.channels.DatagramChannel;
-import java.nio.channels.Pipe;
 import java.nio.channels.SelectableChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
-import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -252,6 +248,13 @@
             if (timeout <= 0) {
               return 0;
             }
+          }
+          
+          if (Thread.currentThread().isInterrupted()) {
+            throw new InterruptedIOException("Interruped while waiting for " +
+                                             "IO on channel " + channel +
+                                             ". " + timeout + 
+                                             " millis timeout left.");
           }
         }
       } finally {

Modified: hadoop/core/trunk/src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java?rev=637985&r1=637984&r2=637985&view=diff
==============================================================================
--- hadoop/core/trunk/src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java (original)
+++ hadoop/core/trunk/src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java Mon Mar
17 10:40:00 2008
@@ -38,7 +38,7 @@
  */
 public class TestSocketIOWithTimeout extends TestCase {
 
-  Log LOG = LogFactory.getLog(TestSocketIOWithTimeout.class);
+  static Log LOG = LogFactory.getLog(TestSocketIOWithTimeout.class);
   
   private static int TIMEOUT = 1*1000; 
   private static String TEST_STRING = "1234567890";
@@ -61,12 +61,33 @@
         long diff = System.currentTimeMillis() - start;
         LOG.info("Got SocketTimeoutException as expected after " + 
                  diff + " millis : " + e.getMessage());
-        assertTrue(Math.abs(TIMEOUT - diff) <= 100);
+        assertTrue(Math.abs(TIMEOUT - diff) <= 200);
         break;
       }
     }
   }
   
+  /**
+   * Just reads one byte from the input stream.
+   */
+  static class ReadRunnable implements Runnable {
+    private InputStream in;
+
+    public ReadRunnable(InputStream in) {
+      this.in = in;
+    }
+    public void run() {
+      try {
+        in.read();
+      } catch (IOException e) {
+        LOG.info("Got expection while reading as expected : " + 
+                 e.getMessage());
+        return;
+      }
+      assertTrue(false);
+    }
+  }
+  
   public void testSocketIOWithTimeout() throws IOException {
     
     // first open pipe:
@@ -87,6 +108,27 @@
       in.read(readBytes);
       assertTrue(Arrays.equals(writeBytes, readBytes));
       doIO(in, null);
+      
+      /*
+       * Verify that it handles interrupted threads properly.
+       * Use a large timeout and expect the thread to return quickly.
+       */
+      in = new SocketInputStream(source, 0);
+      Thread thread = new Thread(new ReadRunnable(in));
+      thread.start();
+      
+      try {
+        Thread.sleep(1000);
+      } catch (InterruptedException ignored) {}
+      
+      thread.interrupt();
+      
+      try {
+        thread.join();
+      } catch (InterruptedException e) {
+        throw new IOException("Unexpected InterruptedException : " + e);
+      }
+      
     } finally {
       if (source != null) {
         source.close();



Mime
View raw message