From 9811e70077aef2ff596b247af813a7f30d3d1a70 Mon Sep 17 00:00:00 2001 From: Ashish Singhi Date: Fri, 31 Mar 2017 17:50:47 +0530 Subject: [PATCH] HBASE-9393 Hbase does not closing a closed socket resulting in many CLOSE_WAIT --- .../hadoop/hbase/io/FSDataInputStreamWrapper.java | 71 +++++++++++++++++++++- .../org/apache/hadoop/hbase/io/hfile/HFile.java | 16 +++-- .../apache/hadoop/hbase/io/hfile/HFileBlock.java | 19 ++++++ .../hadoop/hbase/io/hfile/HFileReaderImpl.java | 9 +++ 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/FSDataInputStreamWrapper.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/FSDataInputStreamWrapper.java index b06be6b..c17919c 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/FSDataInputStreamWrapper.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/FSDataInputStreamWrapper.java @@ -18,7 +18,12 @@ package org.apache.hadoop.hbase.io; import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; @@ -32,6 +37,8 @@ import com.google.common.annotations.VisibleForTesting; * see method comments. */ public class FSDataInputStreamWrapper { + private static final Log LOG = LogFactory.getLog(FSDataInputStreamWrapper.class); + private final HFileSystem hfs; private final Path path; private final FileLink link; @@ -73,6 +80,11 @@ public class FSDataInputStreamWrapper { // In the case of a checksum failure, do these many succeeding // reads without hbase checksum verification. private volatile int hbaseChecksumOffCount = -1; + + private Boolean instanceOfCanUnbuffer = null; + // Using reflection to get org.apache.hadoop.fs.CanUnbuffer#unbuffer method to avoid compilation + // errors against Hadoop pre 2.6.4 and 2.7.1 versions. + private Method unbuffer = null; public FSDataInputStreamWrapper(FileSystem fs, Path path) throws IOException { this(fs, null, path, false); @@ -219,4 +231,61 @@ public class FSDataInputStreamWrapper { public HFileSystem getHfs() { return this.hfs; } -} + + /** + * This will free sockets and file descriptors held by the stream only when the stream implements + * org.apache.hadoop.fs.CanUnbuffer. NOT THREAD SAFE. Must be called only when all the clients + * using this stream to read the blocks have finished reading. If by chance the stream is + * unbuffered and there are clients still holding this stream for read then on next client read + * request a new socket will be opened by Datanode without client knowing about it and will serve + * its read request. Note: If this socket is idle for some time then the DataNode will close the + * socket and the socket will move into CLOSE_WAIT state and on the next client request on this + * stream, the current socket will be closed and a new socket will be opened to serve the + * requests. + */ + @SuppressWarnings({ "rawtypes" }) + public void unbuffer() { + FSDataInputStream stream = this.getStream(this.shouldUseHBaseChecksum()); + if (stream != null) { + InputStream wrappedStream = stream.getWrappedStream(); + // CanUnbuffer interface was added as part of HDFS-7694 and the fix is available in Hadoop + // 2.6.4+ and 2.7.1+ versions only so check whether the stream object implements the + // CanUnbuffer interface or not and based on that call the unbuffer api. + final Class streamClass = wrappedStream.getClass(); + if (this.instanceOfCanUnbuffer == null) { + // To ensure we compute whether the stream is instance of CanUnbuffer only once. + this.instanceOfCanUnbuffer = false; + Class[] streamInterfaces = streamClass.getInterfaces(); + for (Class c : streamInterfaces) { + if (c.getCanonicalName().toString().equals("org.apache.hadoop.fs.CanUnbuffer")) { + try { + this.unbuffer = streamClass.getDeclaredMethod("unbuffer"); + } catch (NoSuchMethodException | SecurityException e) { + LOG.warn("Failed to find 'unbuffer' method in class " + streamClass + + " . So there may be a TCP socket connection " + + "left open in CLOSE_WAIT state.", + e); + return; + } + this.instanceOfCanUnbuffer = true; + break; + } + } + } + if (this.instanceOfCanUnbuffer) { + try { + this.unbuffer.invoke(wrappedStream); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + LOG.warn("Failed to invoke 'unbuffer' method in class " + streamClass + + " . So there may be a TCP socket connection left open in CLOSE_WAIT state.", + e); + } + } else { + LOG.warn("Failed to find 'unbuffer' method in class " + streamClass + + " . So there may be a TCP socket connection " + + "left open in CLOSE_WAIT state. For more details check " + + "https://issues.apache.org/jira/browse/HBASE-9393"); + } + } + } +} \ No newline at end of file diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFile.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFile.java index c5b334a..e871dff 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFile.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFile.java @@ -475,6 +475,12 @@ public class HFile { @VisibleForTesting boolean prefetchComplete(); + + /** + * To close the stream's socket. Note: This can be concurrently called from multiple threads and + * implementation should take care of thread safety. + */ + void unbufferStream(); } /** @@ -491,7 +497,7 @@ public class HFile { */ @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SF_SWITCH_FALLTHROUGH", justification="Intentional") - private static Reader pickReaderVersion(Path path, FSDataInputStreamWrapper fsdis, + private static Reader openReader(Path path, FSDataInputStreamWrapper fsdis, long size, CacheConfig cacheConf, HFileSystem hfs, Configuration conf) throws IOException { FixedFileTrailer trailer = null; try { @@ -514,6 +520,8 @@ public class HFile { LOG.warn("Error closing fsdis FSDataInputStreamWrapper", t2); } throw new CorruptHFileException("Problem reading HFile Trailer from file " + path, t); + } finally { + fsdis.unbuffer(); } } @@ -542,7 +550,7 @@ public class HFile { } else { hfs = (HFileSystem)fs; } - return pickReaderVersion(path, fsdis, size, cacheConf, hfs, conf); + return openReader(path, fsdis, size, cacheConf, hfs, conf); } /** @@ -570,7 +578,7 @@ public class HFile { FileSystem fs, Path path, CacheConfig cacheConf, Configuration conf) throws IOException { Preconditions.checkNotNull(cacheConf, "Cannot create Reader with null CacheConf"); FSDataInputStreamWrapper stream = new FSDataInputStreamWrapper(fs, path); - return pickReaderVersion(path, stream, fs.getFileStatus(path).getLen(), + return openReader(path, stream, fs.getFileStatus(path).getLen(), cacheConf, stream.getHfs(), conf); } @@ -581,7 +589,7 @@ public class HFile { FSDataInputStream fsdis, long size, CacheConfig cacheConf, Configuration conf) throws IOException { FSDataInputStreamWrapper wrapper = new FSDataInputStreamWrapper(fsdis); - return pickReaderVersion(path, wrapper, size, cacheConf, null, conf); + return openReader(path, wrapper, size, cacheConf, null, conf); } /** diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlock.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlock.java index 066a9fa..302d8bf 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlock.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlock.java @@ -1369,6 +1369,12 @@ public class HFileBlock implements Cacheable { void setIncludesMemstoreTS(boolean includesMemstoreTS); void setDataBlockEncoder(HFileDataBlockEncoder encoder); + + /** + * To close the stream's socket. Note: This can be concurrently called from multiple threads and + * implementation should take care of thread safety. + */ + void unbufferStream(); } /** @@ -1827,6 +1833,19 @@ public class HFileBlock implements Cacheable { public void closeStreams() throws IOException { streamWrapper.close(); } + + @Override + public void unbufferStream() { + // To handle concurrent reads, ensure that no other client is accessing the streams while we + // unbuffer it. + if (streamLock.tryLock()) { + try { + this.streamWrapper.unbuffer(); + } finally { + streamLock.unlock(); + } + } + } @Override public String toString() { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java index 4e8cbaa..7a93282 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java @@ -586,6 +586,10 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { @Override public void close() { + if (!pread) { + // For seek + pread stream socket should be closed when the scanner is closed. HBASE-9393 + reader.unbufferStream(); + } this.returnBlocks(true); } @@ -1856,4 +1860,9 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { public int getMajorVersion() { return 3; } + + @Override + public void unbufferStream() { + fsBlockReader.unbufferStream(); + } } -- 1.9.1