From 1e81a907fd3722601e3d1e765baf4a9360b95e99 Mon Sep 17 00:00:00 2001 From: Kyle Nusbaum Date: Mon, 18 Sep 2017 15:06:07 -0500 Subject: [PATCH] Fix FD leak --- .../handler/LogviewerLogSearchHandler.java | 140 +++++++++--------- 1 file changed, 73 insertions(+), 67 deletions(-) diff --git a/storm-webapp/src/main/java/org/apache/storm/daemon/logviewer/handler/LogviewerLogSearchHandler.java b/storm-webapp/src/main/java/org/apache/storm/daemon/logviewer/handler/LogviewerLogSearchHandler.java index 76f168335da..ffa1bc982a1 100644 --- a/storm-webapp/src/main/java/org/apache/storm/daemon/logviewer/handler/LogviewerLogSearchHandler.java +++ b/storm-webapp/src/main/java/org/apache/storm/daemon/logviewer/handler/LogviewerLogSearchHandler.java @@ -272,90 +272,96 @@ private Map substringSearch(File file, String searchString, boole } boolean isZipFile = file.getName().endsWith(".gz"); - FileInputStream fis = new FileInputStream(file); - InputStream gzippedInputStream; - if (isZipFile) { - gzippedInputStream = new GZIPInputStream(fis); - } else { - gzippedInputStream = fis; - } - BufferedInputStream stream = new BufferedInputStream(gzippedInputStream); + InputStream gzippedInputStream = null; + try { + FileInputStream fis = new FileInputStream(file); + if (isZipFile) { + gzippedInputStream = new GZIPInputStream(fis); + } else { + gzippedInputStream = fis; + } - int fileLength; - if (isZipFile) { - fileLength = (int) ServerUtils.zipFileSize(file); - } else { - fileLength = (int) file.length(); - } + BufferedInputStream stream = new BufferedInputStream(gzippedInputStream); - ByteBuffer buf = ByteBuffer.allocate(GREP_BUF_SIZE); - final byte[] bufArray = buf.array(); - final byte[] searchBytes = searchString.getBytes("UTF-8"); - numMatches = numMatches != null ? numMatches : 10; - startByteOffset = startByteOffset != null ? startByteOffset : 0; + int fileLength; + if (isZipFile) { + fileLength = (int) ServerUtils.zipFileSize(file); + } else { + fileLength = (int) file.length(); + } - // Start at the part of the log file we are interested in. - // Allow searching when start-byte-offset == file-len so it doesn't blow up on 0-length files - if (startByteOffset > fileLength) { - throw new InvalidRequestException("Cannot search past the end of the file"); - } + ByteBuffer buf = ByteBuffer.allocate(GREP_BUF_SIZE); + final byte[] bufArray = buf.array(); + final byte[] searchBytes = searchString.getBytes("UTF-8"); + numMatches = numMatches != null ? numMatches : 10; + startByteOffset = startByteOffset != null ? startByteOffset : 0; - if (startByteOffset > 0) { - StreamUtil.skipBytes(stream, startByteOffset); - } + // Start at the part of the log file we are interested in. + // Allow searching when start-byte-offset == file-len so it doesn't blow up on 0-length files + if (startByteOffset > fileLength) { + throw new InvalidRequestException("Cannot search past the end of the file"); + } - Arrays.fill(bufArray, (byte) 0); + if (startByteOffset > 0) { + StreamUtil.skipBytes(stream, startByteOffset); + } - int totalBytesRead = 0; - int bytesRead = stream.read(bufArray, 0, Math.min((int) fileLength, GREP_BUF_SIZE)); - buf.limit(bytesRead); - totalBytesRead += bytesRead; + Arrays.fill(bufArray, (byte) 0); - List> initialMatches = new ArrayList<>(); - int initBufOffset = 0; - int byteOffset = startByteOffset; - byte[] beforeBytes = null; + int totalBytesRead = 0; + int bytesRead = stream.read(bufArray, 0, Math.min((int) fileLength, GREP_BUF_SIZE)); + buf.limit(bytesRead); + totalBytesRead += bytesRead; - Map ret = new HashMap<>(); - while (true) { - SubstringSearchResult searchRet = bufferSubstringSearch(isDaemon, file, fileLength, byteOffset, initBufOffset, - stream, startByteOffset, totalBytesRead, buf, searchBytes, initialMatches, numMatches, beforeBytes); + List> initialMatches = new ArrayList<>(); + int initBufOffset = 0; + int byteOffset = startByteOffset; + byte[] beforeBytes = null; - List> matches = searchRet.getMatches(); - Integer newByteOffset = searchRet.getNewByteOffset(); - byte[] newBeforeBytes = searchRet.getNewBeforeBytes(); + Map ret = new HashMap<>(); + while (true) { + SubstringSearchResult searchRet = bufferSubstringSearch(isDaemon, file, fileLength, byteOffset, initBufOffset, + stream, startByteOffset, totalBytesRead, buf, searchBytes, initialMatches, numMatches, beforeBytes); - if (matches.size() < numMatches && totalBytesRead + startByteOffset < fileLength) { - // The start index is positioned to find any possible - // occurrence search string that did not quite fit in the - // buffer on the previous read. - final int newBufOffset = Math.min(buf.limit(), GREP_MAX_SEARCH_SIZE) - searchBytes.length; + List> matches = searchRet.getMatches(); + Integer newByteOffset = searchRet.getNewByteOffset(); + byte[] newBeforeBytes = searchRet.getNewBeforeBytes(); - totalBytesRead = rotateGrepBuffer(buf, stream, totalBytesRead, file, fileLength); - if (totalBytesRead < 0) { - throw new InvalidRequestException("Cannot search past the end of the file"); - } + if (matches.size() < numMatches && totalBytesRead + startByteOffset < fileLength) { + // The start index is positioned to find any possible + // occurrence search string that did not quite fit in the + // buffer on the previous read. + final int newBufOffset = Math.min(buf.limit(), GREP_MAX_SEARCH_SIZE) - searchBytes.length; - initialMatches = matches; - initBufOffset = newBufOffset; - byteOffset = newByteOffset; - beforeBytes = newBeforeBytes; - } else { - ret.put("isDaemon", isDaemon ? "yes" : "no"); - Integer nextByteOffset = null; - if (matches.size() >= numMatches || totalBytesRead < fileLength) { - nextByteOffset = (Integer) last(matches).get("byteOffset") + searchBytes.length; - if (fileLength <= nextByteOffset) { - nextByteOffset = null; + totalBytesRead = rotateGrepBuffer(buf, stream, totalBytesRead, file, fileLength); + if (totalBytesRead < 0) { + throw new InvalidRequestException("Cannot search past the end of the file"); + } + + initialMatches = matches; + initBufOffset = newBufOffset; + byteOffset = newByteOffset; + beforeBytes = newBeforeBytes; + } else { + ret.put("isDaemon", isDaemon ? "yes" : "no"); + Integer nextByteOffset = null; + if (matches.size() >= numMatches || totalBytesRead < fileLength) { + nextByteOffset = (Integer) last(matches).get("byteOffset") + searchBytes.length; + if (fileLength <= nextByteOffset) { + nextByteOffset = null; + } } + ret.putAll(mkGrepResponse(searchBytes, startByteOffset, matches, nextByteOffset)); + break; } - ret.putAll(mkGrepResponse(searchBytes, startByteOffset, matches, nextByteOffset)); - break; + } + return ret; + } finally { + if (gzippedInputStream != null) { + gzippedInputStream.close(); } } - - return ret; } catch (IOException e) { throw new RuntimeException(e); }