Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions java/org/apache/catalina/webresources/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public class Cache {

private final ConcurrentMap<String,CachedResource> resourceCache = new ConcurrentHashMap<>();

/*
* Time before which no cache entry can be evicted. Set when an eviction has removed every expired entry so that
* requests do not scan the whole cache again before an entry can expire. Entries added later expire no earlier,
* since every cached entry is checked against the current time plus the TTL.
*/
private volatile long noEvictionBefore = 0;

/**
* Constructs a cache for the given root.
*
Expand Down Expand Up @@ -125,12 +132,7 @@ protected WebResource getResource(String path, boolean useClassLoaderResources)
}

if (size.get() > maxSize) {
// Process resources unordered for speed. Trades cache
// efficiency (younger entries may be evicted before older
// ones) for speed since this is on the critical path for
// request processing
long targetSize = maxSize * (100 - TARGET_FREE_PERCENT_GET) / 100;
long newSize = evict(targetSize, resourceCache.values().iterator());
long newSize = evictOnRequest();
if (newSize > maxSize) {
// Unable to create sufficient space for this resource
// Remove it from the cache
Expand Down Expand Up @@ -213,12 +215,7 @@ protected WebResource[] getResources(String path, boolean useClassLoaderResource
}

if (size.get() > maxSize) {
// Process resources unordered for speed. Trades cache
// efficiency (younger entries may be evicted before older
// ones) for speed since this is on the critical path for
// request processing
long targetSize = maxSize * (100 - TARGET_FREE_PERCENT_GET) / 100;
long newSize = evict(targetSize, resourceCache.values().iterator());
long newSize = evictOnRequest();
if (newSize > maxSize) {
// Unable to create sufficient space for this resource
// Remove it from the cache
Expand Down Expand Up @@ -276,17 +273,32 @@ private boolean noCache(String path) {
(path.startsWith("/WEB-INF/lib/") && path.endsWith(".jar"));
}

private long evictOnRequest() {
if (System.currentTimeMillis() < noEvictionBefore) {
// No entry has expired since the last eviction removed every expired entry
return size.get();
}

// Process resources unordered for speed. Trades cache efficiency (younger entries may be evicted before older
// ones) for speed since this is on the critical path for request processing
long targetSize = maxSize * (100 - TARGET_FREE_PERCENT_GET) / 100;
return evict(targetSize, resourceCache.values().iterator());
}

private long evict(long targetSize, Iterator<CachedResource> iter) {

long now = System.currentTimeMillis();

long newSize = size.get();
long earliestNextCheck = Long.MAX_VALUE;

while (newSize > targetSize && iter.hasNext()) {
CachedResource resource = iter.next();

// Don't expire anything that has been checked within the TTL
if (resource.getNextCheck() > now) {
long nextCheck = resource.getNextCheck();
if (nextCheck > now) {
earliestNextCheck = Math.min(earliestNextCheck, nextCheck);
continue;
}

Expand All @@ -296,6 +308,11 @@ private long evict(long targetSize, Iterator<CachedResource> iter) {
newSize = size.get();
}

if (newSize > targetSize && earliestNextCheck != Long.MAX_VALUE) {
// Every expired entry has been removed
noEvictionBefore = earliestNextCheck;
}

return newSize;
}

Expand Down Expand Up @@ -347,6 +364,7 @@ public long getTtl() {
*/
public void setTtl(long ttl) {
this.ttl = ttl;
noEvictionBefore = 0;
}

/**
Expand Down
165 changes: 165 additions & 0 deletions test/org/apache/catalina/webresources/TestCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.WebResource;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;

public class TestCache extends TomcatBaseTest {

// Enough files to fill a 100 KB cache several times over
private static final int FILE_COUNT = 1000;
private static final long MAX_SIZE_KB = 100;

private StandardRoot root;

@Before
public void createWebapp() throws Exception {
File docBase = new File(getTemporaryDirectory(), "cache-eviction");
Assert.assertTrue(docBase.mkdirs());
addDeleteOnTearDown(docBase);
for (int i = 0; i < FILE_COUNT; i++) {
Files.writeString(new File(docBase, fileName(i)).toPath(), Integer.toString(i), StandardCharsets.UTF_8);
}

Tomcat tomcat = getTomcatInstance();
Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
tomcat.start();

root = (StandardRoot) ctx.getResources();
}


@Test
public void testFullCacheOfUnexpiredEntries() throws Exception {
Cache cache = createCache(60000);

for (int i = 0; i < FILE_COUNT; i++) {
assertContent(cache.getResource("/" + fileName(i), false), i);
Assert.assertTrue(cache.getSize() <= MAX_SIZE_KB);
}

// None of the entries has expired so the last resource could not be cached
long hits = cache.getHitCount();
assertContent(cache.getResource("/" + fileName(FILE_COUNT - 1), false), FILE_COUNT - 1);
Assert.assertEquals(hits, cache.getHitCount());

// The first resource was cached before the cache was full
assertContent(cache.getResource("/" + fileName(0), false), 0);
Assert.assertEquals(hits + 1, cache.getHitCount());
}


@Test
public void testEvictionResumesAfterEntriesExpire() throws Exception {
Cache cache = createCache(1000);

for (int i = 0; i < FILE_COUNT / 2; i++) {
cache.getResource("/" + fileName(i), false);
}

// Wait for every cached entry to expire
Thread.sleep(1500);

String path = "/" + fileName(FILE_COUNT - 1);
assertContent(cache.getResource(path, false), FILE_COUNT - 1);
long hits = cache.getHitCount();
assertContent(cache.getResource(path, false), FILE_COUNT - 1);
Assert.assertEquals(hits + 1, cache.getHitCount());
}


@Test
public void testEvictionResumesWhenOldestEntriesExpire() throws Exception {
Cache cache = createCache(1500);

for (int i = 0; i < 100; i++) {
cache.getResource("/" + fileName(i), false);
}
Thread.sleep(1000);
// Fill the cache with entries that expire later than the first ones
for (int i = 100; i < 400; i++) {
cache.getResource("/" + fileName(i), false);
}
Assert.assertFalse(isCached(cache, "/" + fileName(FILE_COUNT - 2)));

// Only the first entries have expired
Thread.sleep(700);
Assert.assertTrue(isCached(cache, "/" + fileName(FILE_COUNT - 1)));
}


@Test
public void testEvictionForDirectoryListing() throws Exception {
Cache cache = createCache(1000);

for (int i = 0; i < FILE_COUNT / 2; i++) {
cache.getResource("/" + fileName(i), false);
}

long hits = cache.getHitCount();
cache.getResources("/", false);
cache.getResources("/", false);
Assert.assertEquals(hits, cache.getHitCount());

// Wait for every cached entry to expire
Thread.sleep(1500);

cache.getResources("/", false);
hits = cache.getHitCount();
cache.getResources("/", false);
Assert.assertEquals(hits + 1, cache.getHitCount());
}


private static boolean isCached(Cache cache, String path) {
cache.getResource(path, false);
long hits = cache.getHitCount();
cache.getResource(path, false);
return cache.getHitCount() == hits + 1;
}


private Cache createCache(long ttl) {
Cache cache = new Cache(root);
cache.setMaxSize(MAX_SIZE_KB);
cache.setObjectMaxSize((int) MAX_SIZE_KB / 20);
cache.setTtl(ttl);
return cache;
}


private static void assertContent(WebResource resource, int expected) {
Assert.assertTrue(resource.exists());
Assert.assertEquals(Integer.toString(expected), new String(resource.getContent(), StandardCharsets.UTF_8));
}


private static String fileName(int i) {
return String.format("f%04d.txt", Integer.valueOf(i));
}
}
6 changes: 6 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@
and connects to the host named in the URL using the credentials of the
realm. (markt)
</fix>
<fix>
When the static resource cache is full and none of the cached entries
has expired, do not scan the whole cache for entries to evict on every
cache miss until the first entry expires. Pull request
<pr>1275</pr> provided by Arturo Bernal.
</fix>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down
Loading