From 103cdbf6efe714136b082950ad2d54a5616f950f Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 23 Jul 2014 11:35:47 +0200 Subject: [PATCH] Guarded calls to LOG.debug() with if (LOG.isDebugEnabled()) to reduce allocation of varargs Object[]. --- .../jetty/proxy/AsyncProxyServlet.java | 32 +++++++++---- .../eclipse/jetty/proxy/BalancerServlet.java | 4 +- .../eclipse/jetty/proxy/ProxyConnection.java | 3 +- .../org/eclipse/jetty/proxy/ProxyServlet.java | 46 +++++++++++-------- 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AsyncProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AsyncProxyServlet.java index 6ebc85ca219..518051d6901 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AsyncProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AsyncProxyServlet.java @@ -60,7 +60,8 @@ public class AsyncProxyServlet extends ProxyServlet { try { - _log.debug("{} proxying content to downstream: {} bytes", getRequestId(request), length); + if (_log.isDebugEnabled()) + _log.debug("{} proxying content to downstream: {} bytes", getRequestId(request), length); StreamWriter writeListener = (StreamWriter)request.getAttribute(WRITE_LISTENER_ATTRIBUTE); if (writeListener == null) { @@ -130,24 +131,30 @@ public class AsyncProxyServlet extends ProxyServlet { int requestId = getRequestId(request); ServletInputStream input = request.getInputStream(); - _log.debug("{} asynchronous read start on {}", requestId, input); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous read start on {}", requestId, input); // First check for isReady() because it has // side effects, and then for isFinished(). while (input.isReady() && !input.isFinished()) { int read = input.read(buffer); - _log.debug("{} asynchronous read {} bytes on {}", requestId, read, input); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous read {} bytes on {}", requestId, read, input); if (read > 0) { - _log.debug("{} proxying content to upstream: {} bytes", requestId, read); + if (_log.isDebugEnabled()) + _log.debug("{} proxying content to upstream: {} bytes", requestId, read); onRequestContent(proxyRequest, request, provider, buffer, 0, read, this); // Do not call isReady() so that we can apply backpressure. break; } } if (!input.isFinished()) - _log.debug("{} asynchronous read pending on {}", requestId, input); + { + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous read pending on {}", requestId, input); + } } protected void onRequestContent(Request proxyRequest, HttpServletRequest request, DeferredContentProvider provider, byte[] buffer, int offset, int length, Callback callback) @@ -158,7 +165,8 @@ public class AsyncProxyServlet extends ProxyServlet @Override public void onAllDataRead() throws IOException { - _log.debug("{} proxying content to upstream completed", getRequestId(request)); + if (_log.isDebugEnabled()) + _log.debug("{} proxying content to upstream completed", getRequestId(request)); provider.close(); } @@ -225,23 +233,27 @@ public class AsyncProxyServlet extends ProxyServlet if (state == WriteState.READY) { // There is data to write. - _log.debug("{} asynchronous write start of {} bytes on {}", requestId, length, output); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous write start of {} bytes on {}", requestId, length, output); output.write(buffer, offset, length); state = WriteState.PENDING; if (output.isReady()) { - _log.debug("{} asynchronous write of {} bytes completed on {}", requestId, length, output); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous write of {} bytes completed on {}", requestId, length, output); complete(); } else { - _log.debug("{} asynchronous write of {} bytes pending on {}", requestId, length, output); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous write of {} bytes pending on {}", requestId, length, output); } } else if (state == WriteState.PENDING) { // The write blocked but is now complete. - _log.debug("{} asynchronous write of {} bytes completing on {}", requestId, length, output); + if (_log.isDebugEnabled()) + _log.debug("{} asynchronous write of {} bytes completing on {}", requestId, length, output); complete(); } else diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/BalancerServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/BalancerServlet.java index fed4bf402b7..ff4ae867dbb 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/BalancerServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/BalancerServlet.java @@ -26,7 +26,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; - import javax.servlet.ServletException; import javax.servlet.UnavailableException; import javax.servlet.http.Cookie; @@ -133,7 +132,8 @@ public class BalancerServlet extends ProxyServlet protected URI rewriteURI(HttpServletRequest request) { BalancerMember balancerMember = selectBalancerMember(request); - _log.debug("Selected {}", balancerMember); + if (_log.isDebugEnabled()) + _log.debug("Selected {}", balancerMember); String path = request.getRequestURI(); String query = request.getQueryString(); if (query != null) diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyConnection.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyConnection.java index 5eb72ec059d..3a620f43a17 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyConnection.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyConnection.java @@ -119,7 +119,8 @@ public abstract class ProxyConnection extends AbstractConnection } catch (IOException x) { - LOG.debug(ProxyConnection.this + " could not fill", x); + if (LOG.isDebugEnabled()) + LOG.debug(ProxyConnection.this + " could not fill", x); disconnect(); return Action.SUCCEEDED; } diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java index 367badf876d..ed00af25641 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java @@ -32,7 +32,6 @@ import java.util.Set; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - import javax.servlet.AsyncContext; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; @@ -52,7 +51,6 @@ import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpVersion; -import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.HttpCookieStore; import org.eclipse.jetty.util.log.Log; @@ -203,7 +201,8 @@ public class ProxyServlet extends HttpServlet } catch (Exception x) { - _log.debug(x); + if (_log.isDebugEnabled()) + _log.debug(x); } } @@ -364,7 +363,8 @@ public class ProxyServlet extends HttpServlet { if (!_whiteList.contains(hostPort)) { - _log.debug("Host {}:{} not whitelisted", host, port); + if (_log.isDebugEnabled()) + _log.debug("Host {}:{} not whitelisted", host, port); return false; } } @@ -372,7 +372,8 @@ public class ProxyServlet extends HttpServlet { if (_blackList.contains(hostPort)) { - _log.debug("Host {}:{} blacklisted", host, port); + if (_log.isDebugEnabled()) + _log.debug("Host {}:{} blacklisted", host, port); return false; } } @@ -391,7 +392,8 @@ public class ProxyServlet extends HttpServlet StringBuffer uri = request.getRequestURL(); if (request.getQueryString() != null) uri.append("?").append(request.getQueryString()); - _log.debug("{} rewriting: {} -> {}", requestId, uri, rewrittenURI); + if (_log.isDebugEnabled()) + _log.debug("{} rewriting: {} -> {}", requestId, uri, rewrittenURI); } if (rewrittenURI == null) @@ -495,7 +497,8 @@ public class ProxyServlet extends HttpServlet protected void onClientRequestFailure(Request proxyRequest, HttpServletRequest request, Throwable failure) { - _log.debug(getRequestId(request) + " client request failure", failure); + if (_log.isDebugEnabled()) + _log.debug(getRequestId(request) + " client request failure", failure); proxyRequest.abort(failure); } @@ -538,7 +541,8 @@ public class ProxyServlet extends HttpServlet { try { - _log.debug("{} proxying content to downstream: {} bytes", getRequestId(request), length); + if (_log.isDebugEnabled()) + _log.debug("{} proxying content to downstream: {} bytes", getRequestId(request), length); response.getOutputStream().write(buffer, offset, length); callback.succeeded(); } @@ -550,27 +554,30 @@ public class ProxyServlet extends HttpServlet protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) { - _log.debug("{} proxying successful", getRequestId(request)); + if (_log.isDebugEnabled()) + _log.debug("{} proxying successful", getRequestId(request)); AsyncContext asyncContext = request.getAsyncContext(); asyncContext.complete(); } protected void onResponseFailure(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, Throwable failure) { - _log.debug(getRequestId(request) + " proxying failed", failure); + if (_log.isDebugEnabled()) + _log.debug(getRequestId(request) + " proxying failed", failure); if (response.isCommitted()) { - // Use Jetty specific behavior to close connection try { + // Use Jetty specific behavior to close connection. response.sendError(-1); + AsyncContext asyncContext = request.getAsyncContext(); + asyncContext.complete(); } - catch (IOException e) + catch (IOException x) { - getServletContext().log("close failed", e); + if (_log.isDebugEnabled()) + _log.debug(getRequestId(request) + " could not close the connection", failure); } - AsyncContext asyncContext = request.getAsyncContext(); - asyncContext.complete(); } else { @@ -687,7 +694,8 @@ public class ProxyServlet extends HttpServlet String contextPath = config.getServletContext().getContextPath(); _prefix = _prefix == null ? contextPath : (contextPath + _prefix); - proxyServlet._log.debug(config.getServletName() + " @ " + _prefix + " to " + _proxyTo); + if (proxyServlet._log.isDebugEnabled()) + proxyServlet._log.debug(config.getServletName() + " @ " + _prefix + " to " + _proxyTo); } protected URI rewriteURI(HttpServletRequest request) @@ -807,7 +815,8 @@ public class ProxyServlet extends HttpServlet onResponseSuccess(request, response, result.getResponse()); else onResponseFailure(request, response, result.getResponse(), result.getFailure()); - _log.debug("{} proxying complete", getRequestId(request)); + if (_log.isDebugEnabled()) + _log.debug("{} proxying complete", getRequestId(request)); } } @@ -832,7 +841,8 @@ public class ProxyServlet extends HttpServlet @Override protected ByteBuffer onRead(byte[] buffer, int offset, int length) { - _log.debug("{} proxying content to upstream: {} bytes", getRequestId(request), length); + if (_log.isDebugEnabled()) + _log.debug("{} proxying content to upstream: {} bytes", getRequestId(request), length); return onRequestContent(proxyRequest, request, buffer, offset, length); }