Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/jetty-12.1.x/12681-cached-co…
Browse files Browse the repository at this point in the history
…ntent-already-released' into fix/jetty-12.1.x/serlvet6-demos

# Conflicts:
#	jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/CachingHttpContentFactory.java
  • Loading branch information
gregw committed Jan 13, 2025
2 parents 0d5038a + 00c249f commit 5744f05
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

import java.io.IOException;
import java.time.Instant;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jetty.http.CompressedContentFormat;
import org.eclipse.jetty.http.HttpField;
Expand Down Expand Up @@ -71,7 +69,6 @@ public class CachingHttpContentFactory implements HttpContent.Factory
private final HttpContent.Factory _authority;
private final ConcurrentHashMap<String, CachingHttpContent> _cache = new ConcurrentHashMap<>();
private final AtomicLong _cachedSize = new AtomicLong();
private final AtomicBoolean _shrinking = new AtomicBoolean();
private final ByteBufferPool.Sized _bufferPool;
private final AtomicBoolean _shrinking = new AtomicBoolean();
private int _maxCachedFileSize = DEFAULT_MAX_CACHED_FILE_SIZE;
Expand Down Expand Up @@ -244,7 +241,6 @@ public HttpContent getContent(String path) throws IOException
if (!isCacheable(httpContent))
return httpContent;

// The re-mapping function may be run multiple times by compute.
AtomicBoolean added = new AtomicBoolean();
cachingHttpContent = _cache.computeIfAbsent(path, key ->
{
Expand Down Expand Up @@ -304,7 +300,7 @@ protected interface CachingHttpContent extends HttpContent

protected class CachedHttpContent extends HttpContent.Wrapper implements CachingHttpContent
{
private final AtomicReference<RetainableByteBuffer> _buffer = new AtomicReference<>();
private final RetainableByteBuffer _buffer;
private final String _cacheKey;
private final HttpField _etagField;
private volatile long _lastAccessed;
Expand Down Expand Up @@ -337,7 +333,7 @@ public CachedHttpContent(String key, HttpContent httpContent)
throw new IllegalArgumentException("Resource is too large: length " + contentLengthValue + " > " + _maxCachedFileSize);

// Read the content into memory
_buffer.set(Objects.requireNonNull(IOResources.toRetainableByteBuffer(httpContent.getResource(), _bufferPool)));
_buffer = IOResources.toRetainableByteBuffer(httpContent.getResource(), _bufferPool);

_characterEncoding = httpContent.getCharacterEncoding();
_compressedFormats = httpContent.getPreCompressedContentFormats();
Expand Down Expand Up @@ -368,44 +364,43 @@ public String getKey()
@Override
public void writeTo(Content.Sink sink, long offset, long length, Callback callback)
{
RetainableByteBuffer buffer = _buffer.get();
if (buffer != null)
boolean retained = false;
try
{
try
{
buffer.retain();
try
{
sink.write(true, BufferUtil.slice(buffer.getByteBuffer(), (int)offset, (int)length), Callback.from(() ->
{
if (buffer.release())
_buffer.set(null);
}, callback));
}
catch (Throwable x)
{
// BufferUtil.slice() may fail if offset and/or length are out of bounds.
if (buffer.release())
_buffer.set(null);
callback.failed(x);
}
return;
}
catch (Throwable ignored)
{
LOG.trace("ignored", ignored);
}
retained = retain();
if (retained)
sink.write(true, BufferUtil.slice(_buffer.getByteBuffer(), Math.toIntExact(offset), Math.toIntExact(length)), Callback.from(this::release, callback));
else
getWrapped().writeTo(sink, offset, length, callback);
}
catch (Throwable x)
{
// BufferUtil.slice() may fail if offset and/or length are out of bounds,
// Math.toIntExact may fail too if offset or length are > Integer.MAX_VALUE.
if (retained)
release();
callback.failed(x);
}
}

getWrapped().writeTo(sink, offset, length, callback);
/**
* Atomically checks that this content still is in cache (so it hasn't been released yet and is still usable) and retain
* its internal buffer if it is.
* @return true if this content can be used and has been retained, false otherwise.
*/
private boolean retain()
{
return _cache.computeIfPresent(_cacheKey, (s, cachingHttpContent) ->
{
_buffer.retain();
return cachingHttpContent;
}) != null;
}

@Override
public void release()
{
RetainableByteBuffer buffer = _buffer.get();
if (buffer != null && buffer.release())
_buffer.set(null);
_buffer.release();
}

@Override
Expand Down Expand Up @@ -441,8 +436,7 @@ public HttpField getContentLength()
@Override
public long getContentLengthValue()
{
RetainableByteBuffer buffer = _buffer.get();
return buffer == null ? getWrapped().getContentLengthValue() : buffer.remaining();
return _buffer.remaining();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.http.content;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@ExtendWith(WorkDirExtension.class)
public class CachingHttpContentFactoryTest
{
public WorkDir workDir;
private ArrayByteBufferPool.Tracking trackingPool;
private ByteBufferPool.Sized sizedPool;

@BeforeEach
public void setUp()
{
trackingPool = new ArrayByteBufferPool.Tracking();
sizedPool = new ByteBufferPool.Sized(trackingPool);
}

@AfterEach
public void tearDown()
{
assertThat("Leaks: " + trackingPool.dumpLeaks(), trackingPool.getLeaks().size(), is(0));
}

@Test
public void testWriteEvictedContent() throws Exception
{
Path file = Files.writeString(workDir.getEmptyPathDir().resolve("file.txt"), "0123456789abcdefghijABCDEFGHIJ");
ResourceHttpContentFactory resourceHttpContentFactory = new ResourceHttpContentFactory(ResourceFactory.root().newResource(file.getParent()), MimeTypes.DEFAULTS, sizedPool);
CachingHttpContentFactory cachingHttpContentFactory = new CachingHttpContentFactory(resourceHttpContentFactory, sizedPool);

HttpContent content = cachingHttpContentFactory.getContent("file.txt");

// Empty the cache so 'content' gets released.
cachingHttpContentFactory.flushCache();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Blocker.Callback cb = Blocker.callback())
{
content.writeTo(Content.Sink.from(baos), 0L, -1L, cb);
cb.block();
}
assertThat(baos.toString(StandardCharsets.UTF_8), is("0123456789abcdefghijABCDEFGHIJ"));
}

@Test
public void testEvictBetweenWriteToAndSinkWrite() throws Exception
{
Path file = Files.writeString(workDir.getEmptyPathDir().resolve("file.txt"), "0123456789abcdefghijABCDEFGHIJ");
ResourceHttpContentFactory resourceHttpContentFactory = new ResourceHttpContentFactory(ResourceFactory.root().newResource(file.getParent()), MimeTypes.DEFAULTS, sizedPool);
CachingHttpContentFactory cachingHttpContentFactory = new CachingHttpContentFactory(resourceHttpContentFactory, sizedPool);

HttpContent content = cachingHttpContentFactory.getContent("file.txt");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Blocker.Callback cb = Blocker.callback())
{
content.writeTo((last, byteBuffer, callback) ->
{
// Empty the cache so 'content' gets released.
cachingHttpContentFactory.flushCache();

Content.Sink.from(baos).write(last, byteBuffer, callback);
}, 0L, -1L, cb);
cb.block();
}
assertThat(baos.toString(StandardCharsets.UTF_8), is("0123456789abcdefghijABCDEFGHIJ"));
}
}

0 comments on commit 5744f05

Please sign in to comment.