Tweaked logging in caching components

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@942073 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-05-07 13:53:13 +00:00
parent fd2e7a79a9
commit f749748401
6 changed files with 95 additions and 65 deletions

View File

@ -395,4 +395,10 @@ public Set<String> getVariantURIs() {
return Collections.unmodifiableSet(this.variantURIs); return Collections.unmodifiableSet(this.variantURIs);
} }
@Override
public String toString() {
return "[request date=" + requestDate + "; response date=" + responseDate
+ "; status=" + status + "]";
}
} }

View File

@ -48,7 +48,7 @@ public class CacheInvalidator {
private final HttpCache<CacheEntry> cache; private final HttpCache<CacheEntry> cache;
private final URIExtractor uriExtractor; private final URIExtractor uriExtractor;
private final Log LOG = LogFactory.getLog(CacheInvalidator.class); private final Log log = LogFactory.getLog(getClass());
/** /**
* *
@ -68,17 +68,15 @@ public CacheInvalidator(URIExtractor uriExtractor, HttpCache<CacheEntry> cache)
* @param req The HttpRequest to that host * @param req The HttpRequest to that host
*/ */
public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req) { public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req) {
LOG.debug("CacheInvalidator: flushInvalidatedCacheEntries, BEGIN");
if (requestShouldNotBeCached(req)) { if (requestShouldNotBeCached(req)) {
LOG.debug("CacheInvalidator: flushInvalidatedCacheEntries, Request should not be cached"); log.debug("Request should not be cached");
try { try {
String theUri = uriExtractor.getURI(host, req); String theUri = uriExtractor.getURI(host, req);
CacheEntry parent = cache.getEntry(theUri); CacheEntry parent = cache.getEntry(theUri);
LOG.debug("CacheInvalidator: flushInvalidatedCacheEntries: " + parent); log.debug("parent entry: " + parent);
if (parent != null) { if (parent != null) {
for (String variantURI : parent.getVariantURIs()) { for (String variantURI : parent.getVariantURIs()) {
@ -86,9 +84,9 @@ public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req) {
} }
cache.removeEntry(theUri); cache.removeEntry(theUri);
} }
} catch (HttpCacheOperationException coe) { } catch (HttpCacheOperationException ex) {
LOG.warn("Cache: Was unable to REMOVE an entry from the cache based on the uri provided.", coe); log.debug("Was unable to REMOVE an entry from the cache based on the uri provided",
// TODO: track failed state ex);
} }
} }
} }

View File

@ -42,7 +42,7 @@
@Immutable @Immutable
public class CacheableRequestPolicy { public class CacheableRequestPolicy {
private final Log LOG = LogFactory.getLog(CacheableRequestPolicy.class); private final Log log = LogFactory.getLog(getClass());
/** /**
* Determines if an HttpRequest can be served from the cache. * Determines if an HttpRequest can be served from the cache.
@ -56,17 +56,17 @@ public boolean isServableFromCache(HttpRequest request) {
ProtocolVersion pv = request.getRequestLine().getProtocolVersion(); ProtocolVersion pv = request.getRequestLine().getProtocolVersion();
if (CachingHttpClient.HTTP_1_1.compareToVersion(pv) != 0) { if (CachingHttpClient.HTTP_1_1.compareToVersion(pv) != 0) {
LOG.debug("CacheableRequestPolicy: Request WAS NOT serveable from Cache."); log.debug("Request was not serveable from cache");
return false; return false;
} }
if (!method.equals(HeaderConstants.GET_METHOD)) { if (!method.equals(HeaderConstants.GET_METHOD)) {
LOG.debug("CacheableRequestPolicy: Request WAS NOT serveable from Cache."); log.debug("Request was not serveable from cache");
return false; return false;
} }
if (request.getHeaders(HeaderConstants.PRAGMA).length > 0) { if (request.getHeaders(HeaderConstants.PRAGMA).length > 0) {
LOG.debug("CacheableRequestPolicy: Request WAS NOT serveable from Cache."); log.debug("Request was not serveable from cache");
return false; return false;
} }
@ -75,19 +75,19 @@ public boolean isServableFromCache(HttpRequest request) {
for (HeaderElement cacheControlElement : cacheControl.getElements()) { for (HeaderElement cacheControlElement : cacheControl.getElements()) {
if (HeaderConstants.CACHE_CONTROL_NO_STORE.equalsIgnoreCase(cacheControlElement if (HeaderConstants.CACHE_CONTROL_NO_STORE.equalsIgnoreCase(cacheControlElement
.getName())) { .getName())) {
LOG.debug("CacheableRequestPolicy: Request WAS NOT serveable from Cache."); log.debug("Request was not serveable from Cache");
return false; return false;
} }
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(cacheControlElement if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(cacheControlElement
.getName())) { .getName())) {
LOG.debug("CacheableRequestPolicy: Request WAS NOT serveable from Cache."); log.debug("Request was not serveable from cache");
return false; return false;
} }
} }
} }
LOG.debug("CacheableRequestPolicy: Request WAS serveable from Cache."); log.debug("Request was serveable from cache");
return true; return true;
} }

View File

@ -43,7 +43,7 @@
@Immutable @Immutable
public class CachedResponseSuitabilityChecker { public class CachedResponseSuitabilityChecker {
private final Log LOG = LogFactory.getLog(CachedResponseSuitabilityChecker.class); private final Log log = LogFactory.getLog(getClass());
/** /**
* @param host * @param host
@ -56,29 +56,29 @@ public class CachedResponseSuitabilityChecker {
*/ */
public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, CacheEntry entry) { public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, CacheEntry entry) {
if (!entry.isResponseFresh()) { if (!entry.isResponseFresh()) {
LOG.debug("CachedResponseSuitabilityChecker: Cache Entry was NOT fresh enough"); log.debug("Cache entry was not fresh enough");
return false; return false;
} }
if (!entry.contentLengthHeaderMatchesActualLength()) { if (!entry.contentLengthHeaderMatchesActualLength()) {
LOG.debug("CachedResponseSuitabilityChecker: Cache Entry Content Length and header information DO NOT match."); log.debug("Cache entry Content-Length and header information do not match");
return false; return false;
} }
if (entry.modifiedSince(request)) { if (entry.modifiedSince(request)) {
LOG.debug("CachedResponseSuitabilityChecker: Cache Entry modified times didn't line up. Cache Entry should NOT be used."); log.debug("Cache entry modified times didn't line up. Cache Entry should not be used");
return false; return false;
} }
for (Header ccHdr : request.getHeaders(HeaderConstants.CACHE_CONTROL)) { for (Header ccHdr : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for (HeaderElement elt : ccHdr.getElements()) { for (HeaderElement elt : ccHdr.getElements()) {
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) { if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
LOG.debug("CachedResponseSuitabilityChecker: Response contained NO CACHE directive, cache was NOT suitable."); log.debug("Response contained NO CACHE directive, cache was not suitable");
return false; return false;
} }
if (HeaderConstants.CACHE_CONTROL_NO_STORE.equals(elt.getName())) { if (HeaderConstants.CACHE_CONTROL_NO_STORE.equals(elt.getName())) {
LOG.debug("CachedResponseSuitabilityChecker: Response contained NO SORE directive, cache was NOT suitable."); log.debug("Response contained NO SORE directive, cache was not suitable");
return false; return false;
} }
@ -86,12 +86,12 @@ public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, Cache
try { try {
int maxage = Integer.parseInt(elt.getValue()); int maxage = Integer.parseInt(elt.getValue());
if (entry.getCurrentAgeSecs() > maxage) { if (entry.getCurrentAgeSecs() > maxage) {
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable due to max age."); log.debug("Response from cache was NOT suitable due to max age");
return false; return false;
} }
} catch (NumberFormatException nfe) { } catch (NumberFormatException ex) {
// err conservatively // err conservatively
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable. "+nfe); log.debug("Response from cache was malformed: " + ex.getMessage());
return false; return false;
} }
} }
@ -100,12 +100,12 @@ public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, Cache
try { try {
int maxstale = Integer.parseInt(elt.getValue()); int maxstale = Integer.parseInt(elt.getValue());
if (entry.getFreshnessLifetimeSecs() > maxstale) { if (entry.getFreshnessLifetimeSecs() > maxstale) {
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable due to Max stale freshness"); log.debug("Response from cache was not suitable due to Max stale freshness");
return false; return false;
} }
} catch (NumberFormatException nfe) { } catch (NumberFormatException ex) {
// err conservatively // err conservatively
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable. "+nfe); log.debug("Response from cache was malformed: " + ex.getMessage());
return false; return false;
} }
} }
@ -114,19 +114,20 @@ public boolean canCachedResponseBeUsed(HttpHost host, HttpRequest request, Cache
try { try {
int minfresh = Integer.parseInt(elt.getValue()); int minfresh = Integer.parseInt(elt.getValue());
if (entry.getFreshnessLifetimeSecs() < minfresh) { if (entry.getFreshnessLifetimeSecs() < minfresh) {
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable due to min fresh freshness requirement"); log.debug("Response from cache was not suitable due to min fresh " +
"freshness requirement");
return false; return false;
} }
} catch (NumberFormatException nfe) { } catch (NumberFormatException ex) {
// err conservatively // err conservatively
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was NOT suitable. " + nfe); log.debug("Response from cache was malformed: " + ex.getMessage());
return false; return false;
} }
} }
} }
} }
LOG.debug("CachedResponseSuitabilityChecker: Response from cache was suitable."); log.debug("Response from cache was suitable");
return true; return true;
} }
} }

View File

@ -91,7 +91,7 @@ public class CachingHttpClient implements HttpClient {
private final ResponseProtocolCompliance responseCompliance; private final ResponseProtocolCompliance responseCompliance;
private final RequestProtocolCompliance requestCompliance; private final RequestProtocolCompliance requestCompliance;
private final Log LOG = LogFactory.getLog(CachingHttpClient.class); private final Log log = LogFactory.getLog(getClass());
public CachingHttpClient() { public CachingHttpClient() {
this.backend = new DefaultHttpClient(); this.backend = new DefaultHttpClient();
@ -330,8 +330,8 @@ protected CacheEntry getCacheEntry(HttpHost target, HttpRequest request) {
CacheEntry entry = null; CacheEntry entry = null;
try { try {
entry = responseCache.getEntry(uri); entry = responseCache.getEntry(uri);
} catch (HttpCacheOperationException probablyIgnore) { } catch (HttpCacheOperationException ex) {
LOG.warn("Cache: Was unable to get an entry from the cache based on the uri provided.", probablyIgnore); log.debug("Was unable to get an entry from the cache based on the uri provided", ex);
} }
if (entry == null || !entry.hasVariants()) if (entry == null || !entry.hasVariants())
@ -340,7 +340,7 @@ protected CacheEntry getCacheEntry(HttpHost target, HttpRequest request) {
String variantUri = uriExtractor.getVariantURI(target, request, entry); String variantUri = uriExtractor.getVariantURI(target, request, entry);
try { try {
return responseCache.getEntry(variantUri); return responseCache.getEntry(variantUri);
} catch (HttpCacheOperationException probablyIgnore) { } catch (HttpCacheOperationException ex) {
return null; return null;
} }
} }
@ -384,11 +384,19 @@ public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext co
CacheEntry entry = getCacheEntry(target, request); CacheEntry entry = getCacheEntry(target, request);
if (entry == null) { if (entry == null) {
cacheMisses.getAndIncrement(); cacheMisses.getAndIncrement();
LOG.debug("CLIENT: Cache Miss."); if (log.isDebugEnabled()) {
RequestLine rl = request.getRequestLine();
log.debug("Cache miss [host: " + target + "; uri: " + rl.getUri() + "]");
}
return callBackend(target, request, context); return callBackend(target, request, context);
} }
LOG.debug("CLIENT: Cache HIT."); if (log.isDebugEnabled()) {
RequestLine rl = request.getRequestLine();
log.debug("Cache hit [host: " + target + "; uri: " + rl.getUri() + "]");
}
cacheHits.getAndIncrement(); cacheHits.getAndIncrement();
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry)) { if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry)) {
@ -396,14 +404,14 @@ public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext co
} }
if (entry.isRevalidatable()) { if (entry.isRevalidatable()) {
LOG.debug("CLIENT: Revalidate the entry."); log.debug("Revalidating the cache entry");
try { try {
return revalidateCacheEntry(target, request, context, entry); return revalidateCacheEntry(target, request, context, entry);
} catch (IOException ioex) { } catch (IOException ioex) {
HttpResponse response = responseGenerator.generateResponse(entry); HttpResponse response = responseGenerator.generateResponse(entry);
response.addHeader(HeaderConstants.WARNING, "111 Revalidation Failed - " + ioex.getMessage()); response.addHeader(HeaderConstants.WARNING, "111 Revalidation Failed - " + ioex.getMessage());
LOG.debug("111 revalidation failed due to exception: " + ioex); log.debug("111 revalidation failed due to exception: " + ioex);
return response; return response;
} catch (ProtocolException e) { } catch (ProtocolException e) {
throw new ClientProtocolException(e); throw new ClientProtocolException(e);
@ -433,7 +441,7 @@ protected HttpResponse callBackend(HttpHost target, HttpRequest request, HttpCon
Date requestDate = getCurrentDate(); Date requestDate = getCurrentDate();
try { try {
LOG.debug("CLIENT: Calling the backend."); log.debug("Calling the backend");
HttpResponse backendResponse = backend.execute(target, request, context); HttpResponse backendResponse = backend.execute(target, request, context);
return handleBackendResponse(target, request, requestDate, getCurrentDate(), return handleBackendResponse(target, request, requestDate, getCurrentDate(),
backendResponse); backendResponse);
@ -447,8 +455,11 @@ protected HttpResponse callBackend(HttpHost target, HttpRequest request, HttpCon
} }
protected HttpResponse revalidateCacheEntry(HttpHost target, HttpRequest request, protected HttpResponse revalidateCacheEntry(
HttpContext context, CacheEntry cacheEntry) throws IOException, ProtocolException { HttpHost target,
HttpRequest request,
HttpContext context,
CacheEntry cacheEntry) throws IOException, ProtocolException {
HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(request, cacheEntry); HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(request, cacheEntry);
Date requestDate = getCurrentDate(); Date requestDate = getCurrentDate();
@ -472,10 +483,12 @@ protected void storeInCache(HttpHost target, HttpRequest request, CacheEntry ent
if (entry.hasVariants()) { if (entry.hasVariants()) {
try { try {
String uri = uriExtractor.getURI(target, request); String uri = uriExtractor.getURI(target, request);
HttpCacheUpdateCallback<CacheEntry> callback = storeVariantEntry(target, request, entry); HttpCacheUpdateCallback<CacheEntry> callback = storeVariantEntry(
target, request, entry);
responseCache.updateCacheEntry(uri, callback); responseCache.updateCacheEntry(uri, callback);
} catch (HttpCacheOperationException probablyIgnore) { } catch (HttpCacheOperationException ex) {
LOG.warn("Cache: Was unable to PUT/UPDATE an entry into the cache based on the uri provided.", probablyIgnore); log.debug("Was unable to PUT/UPDATE an entry into the cache based on the uri provided",
ex);
} }
} else { } else {
storeNonVariantEntry(target, request, entry); storeNonVariantEntry(target, request, entry);
@ -486,12 +499,14 @@ private void storeNonVariantEntry(HttpHost target, HttpRequest req, CacheEntry e
String uri = uriExtractor.getURI(target, req); String uri = uriExtractor.getURI(target, req);
try { try {
responseCache.putEntry(uri, entry); responseCache.putEntry(uri, entry);
} catch (HttpCacheOperationException probablyIgnore) { } catch (HttpCacheOperationException ex) {
LOG.warn("Cache: Was unable to PUT an entry into the cache based on the uri provided.", probablyIgnore); log.debug("Was unable to PUT an entry into the cache based on the uri provided", ex);
} }
} }
protected HttpCacheUpdateCallback<CacheEntry> storeVariantEntry(final HttpHost target, final HttpRequest req, protected HttpCacheUpdateCallback<CacheEntry> storeVariantEntry(
final HttpHost target,
final HttpRequest req,
final CacheEntry entry) { final CacheEntry entry) {
return new HttpCacheUpdateCallback<CacheEntry>() { return new HttpCacheUpdateCallback<CacheEntry>() {
@ -502,7 +517,11 @@ public CacheEntry getUpdatedEntry(CacheEntry existing) throws HttpCacheOperation
}; };
} }
protected CacheEntry doGetUpdatedParentEntry(CacheEntry existing, HttpHost target, HttpRequest req, CacheEntry entry) throws HttpCacheOperationException { protected CacheEntry doGetUpdatedParentEntry(
CacheEntry existing,
HttpHost target,
HttpRequest req,
CacheEntry entry) throws HttpCacheOperationException {
String variantURI = uriExtractor.getVariantURI(target, req, entry); String variantURI = uriExtractor.getVariantURI(target, req, entry);
responseCache.putEntry(variantURI, entry); responseCache.putEntry(variantURI, entry);
@ -514,10 +533,14 @@ protected CacheEntry doGetUpdatedParentEntry(CacheEntry existing, HttpHost targe
} }
} }
protected HttpResponse handleBackendResponse(HttpHost target, HttpRequest request, protected HttpResponse handleBackendResponse(
Date requestDate, Date responseDate, HttpResponse backendResponse) throws IOException { HttpHost target,
HttpRequest request,
Date requestDate,
Date responseDate,
HttpResponse backendResponse) throws IOException {
LOG.debug("CLIENT: Handling Backend response."); log.debug("Handling Backend response");
responseCompliance.ensureProtocolCompliance(request, backendResponse); responseCompliance.ensureProtocolCompliance(request, backendResponse);
boolean cacheable = responseCachingPolicy.isResponseCacheable(request, backendResponse); boolean cacheable = responseCachingPolicy.isResponseCacheable(request, backendResponse);
@ -530,8 +553,11 @@ protected HttpResponse handleBackendResponse(HttpHost target, HttpRequest reques
return responseReader.getReconstructedResponse(); return responseReader.getReconstructedResponse();
} }
CacheEntry entry = cacheEntryGenerator.generateEntry(requestDate, responseDate, CacheEntry entry = cacheEntryGenerator.generateEntry(
backendResponse, responseReader.getResponseBytes()); requestDate,
responseDate,
backendResponse,
responseReader.getResponseBytes());
storeInCache(target, request, entry); storeInCache(target, request, entry);
return responseGenerator.generateResponse(entry); return responseGenerator.generateResponse(entry);
} }
@ -539,9 +565,8 @@ protected HttpResponse handleBackendResponse(HttpHost target, HttpRequest reques
String uri = uriExtractor.getURI(target, request); String uri = uriExtractor.getURI(target, request);
try { try {
responseCache.removeEntry(uri); responseCache.removeEntry(uri);
} catch (HttpCacheOperationException coe) { } catch (HttpCacheOperationException ex) {
LOG.warn("Cache: Was unable to remove an entry from the cache based on the uri provided.", coe); log.debug("Was unable to remove an entry from the cache based on the uri provided", ex);
// TODO: track failed state
} }
return backendResponse; return backendResponse;
} }

View File

@ -46,7 +46,7 @@
public class ResponseCachingPolicy { public class ResponseCachingPolicy {
private final int maxObjectSizeBytes; private final int maxObjectSizeBytes;
private final Log LOG = LogFactory.getLog(ResponseCachingPolicy.class); private final Log log = LogFactory.getLog(getClass());
/** /**
* *
@ -67,7 +67,7 @@ public boolean isResponseCacheable(String httpMethod, HttpResponse response) {
boolean cacheable = false; boolean cacheable = false;
if (!HeaderConstants.GET_METHOD.equals(httpMethod)) { if (!HeaderConstants.GET_METHOD.equals(httpMethod)) {
LOG.debug("ResponseCachingPolicy: response was not cacheable."); log.debug("Response was not cacheable.");
return false; return false;
} }
@ -79,18 +79,18 @@ public boolean isResponseCacheable(String httpMethod, HttpResponse response) {
case HttpStatus.SC_GONE: case HttpStatus.SC_GONE:
// these response codes MAY be cached // these response codes MAY be cached
cacheable = true; cacheable = true;
LOG.debug("ResponseCachingPolicy: response WAS cacheable."); log.debug("Response was cacheable");
break; break;
case HttpStatus.SC_PARTIAL_CONTENT: case HttpStatus.SC_PARTIAL_CONTENT:
// we don't implement Range requests and hence are not // we don't implement Range requests and hence are not
// allowed to cache partial content // allowed to cache partial content
LOG.debug("ResponseCachingPolicy: response was not cacheable (Partial Content)."); log.debug("Response was not cacheable (Partial Content)");
return cacheable; return cacheable;
default: default:
// If the status code is not one of the recognized // If the status code is not one of the recognized
// available codes in HttpStatus Don't Cache // available codes in HttpStatus Don't Cache
LOG.debug("ResponseCachingPolicy: response was not cacheable (Unknown Status code)."); log.debug("Response was not cacheable (Unknown Status code)");
return cacheable; return cacheable;
} }
@ -175,12 +175,12 @@ protected boolean isExplicitlyCacheable(HttpResponse response) {
*/ */
public boolean isResponseCacheable(HttpRequest request, HttpResponse response) { public boolean isResponseCacheable(HttpRequest request, HttpResponse response) {
if (requestProtocolGreaterThanAccepted(request)) { if (requestProtocolGreaterThanAccepted(request)) {
LOG.debug("ResponseCachingPolicy: response was not cacheable."); log.debug("Response was not cacheable.");
return false; return false;
} }
if (request.getRequestLine().getUri().contains("?") && !isExplicitlyCacheable(response)) { if (request.getRequestLine().getUri().contains("?") && !isExplicitlyCacheable(response)) {
LOG.debug("ResponseCachingPolicy: response was not cacheable."); log.debug("Response was not cacheable.");
return false; return false;
} }