From 98a61766286321468bf801a9f17a843d7eae8d9e Mon Sep 17 00:00:00 2001 From: Jing Zhao Date: Thu, 30 Apr 2015 19:27:07 -0700 Subject: [PATCH] HDFS-8300. Fix unit test failures and findbugs warning caused by HDFS-8283. Contributed by Jing Zhao. --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 ++ .../apache/hadoop/hdfs/DFSOutputStream.java | 2 +- .../org/apache/hadoop/hdfs/DataStreamer.java | 32 +++++++++++-------- .../hadoop/hdfs/TestDFSOutputStream.java | 9 ++++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 7e4a6f2cb3..3bee85223f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -592,6 +592,9 @@ Release 2.8.0 - UNRELEASED HDFS-8214. Secondary NN Web UI shows wrong date for Last Checkpoint. (clamb via wang) + HDFS-8300. Fix unit test failures and findbugs warning caused by HDFS-8283. + (jing9) + Release 2.7.1 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSOutputStream.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSOutputStream.java index 4646b60bf1..ae5d3eb6a3 100755 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSOutputStream.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSOutputStream.java @@ -762,7 +762,7 @@ public synchronized void close() throws IOException { protected synchronized void closeImpl() throws IOException { if (isClosed()) { - streamer.getLastException().check(); + streamer.getLastException().check(true); return; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DataStreamer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DataStreamer.java index 3727d20ea9..8e874ebfb7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DataStreamer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DataStreamer.java @@ -174,13 +174,13 @@ private static void releaseBuffer(List packets, ByteArrayManager bam) packets.clear(); } - static class LastException { - private Throwable thrown; + static class LastExceptionInStreamer { + private IOException thrown; synchronized void set(Throwable t) { - Preconditions.checkNotNull(t); - Preconditions.checkState(thrown == null); - this.thrown = t; + assert t != null; + this.thrown = t instanceof IOException ? + (IOException) t : new IOException(t); } synchronized void clear() { @@ -188,17 +188,23 @@ synchronized void clear() { } /** Check if there already is an exception. */ - synchronized void check() throws IOException { + synchronized void check(boolean resetToNull) throws IOException { if (thrown != null) { - throw new IOException(thrown); + if (LOG.isTraceEnabled()) { + // wrap and print the exception to know when the check is called + LOG.trace("Got Exception while checking", new Throwable(thrown)); + } + final IOException e = thrown; + if (resetToNull) { + thrown = null; + } + throw e; } } synchronized void throwException4Close() throws IOException { - check(); - final IOException ioe = new ClosedChannelException(); - thrown = ioe; - throw ioe; + check(false); + throw new ClosedChannelException(); } } @@ -234,7 +240,7 @@ synchronized void throwException4Close() throws IOException { private long lastQueuedSeqno = -1; private long lastAckedSeqno = -1; private long bytesCurBlock = 0; // bytes written in current block - private final LastException lastException = new LastException(); + private final LastExceptionInStreamer lastException = new LastExceptionInStreamer(); private Socket s; private final DFSClient dfsClient; @@ -1741,7 +1747,7 @@ boolean getAppendChunk(){ /** * @return the last exception */ - LastException getLastException(){ + LastExceptionInStreamer getLastException(){ return lastException; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSOutputStream.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSOutputStream.java index eac1fcd289..e6274556f9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSOutputStream.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSOutputStream.java @@ -28,6 +28,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hdfs.DataStreamer.LastExceptionInStreamer; import org.apache.hadoop.hdfs.client.impl.DfsClientConf; import org.apache.hadoop.hdfs.protocol.DatanodeInfo; import org.apache.hadoop.hdfs.protocol.ExtendedBlock; @@ -65,9 +66,10 @@ public void testCloseTwice() throws IOException { DataStreamer streamer = (DataStreamer) Whitebox .getInternalState(dos, "streamer"); @SuppressWarnings("unchecked") - AtomicReference ex = (AtomicReference) Whitebox + LastExceptionInStreamer ex = (LastExceptionInStreamer) Whitebox .getInternalState(streamer, "lastException"); - Assert.assertEquals(null, ex.get()); + Throwable thrown = (Throwable) Whitebox.getInternalState(ex, "thrown"); + Assert.assertNull(thrown); dos.close(); @@ -78,7 +80,8 @@ public void testCloseTwice() throws IOException { } catch (IOException e) { Assert.assertEquals(e, dummy); } - Assert.assertEquals(null, ex.get()); + thrown = (Throwable) Whitebox.getInternalState(ex, "thrown"); + Assert.assertNull(thrown); dos.close(); }