Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Greg Wilkins 2017-01-17 15:27:22 +11:00
commit fb8ecf9970
2 changed files with 37 additions and 41 deletions

View File

@ -68,9 +68,9 @@ public class CachedContentFactory implements HttpContent.ContentFactory
private final CompressedContentFormat[] _precompressedFormats;
private final boolean _useFileMappedBuffer;
private int _maxCachedFileSize =128*1024*1024;
private int _maxCachedFiles=2048;
private int _maxCacheSize =256*1024*1024;
private int _maxCachedFileSize = 128*1024*1024;
private int _maxCachedFiles= 2048;
private int _maxCacheSize = 256*1024*1024;
/* ------------------------------------------------------------ */
/** Constructor.
@ -375,7 +375,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory
}
/* ------------------------------------------------------------ */
protected ByteBuffer getDirectBuffer(Resource resource)
protected ByteBuffer getMappedBuffer(Resource resource)
{
// Only use file mapped buffers for cached resources, otherwise too much virtual memory commitment for
// a non shared resource. Also ignore max buffer size
@ -383,14 +383,26 @@ public class CachedContentFactory implements HttpContent.ContentFactory
{
if (_useFileMappedBuffer && resource.getFile()!=null && resource.length()<Integer.MAX_VALUE)
return BufferUtil.toMappedBuffer(resource.getFile());
}
catch(IOException|IllegalArgumentException e)
{
LOG.warn(e);
}
return null;
}
/* ------------------------------------------------------------ */
protected ByteBuffer getDirectBuffer(Resource resource)
{
try
{
return BufferUtil.toBuffer(resource,true);
}
catch(IOException|IllegalArgumentException e)
{
LOG.warn(e);
return null;
}
return null;
}
/* ------------------------------------------------------------ */
@ -524,6 +536,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory
_cachedSize.addAndGet(-BufferUtil.length(indirect));
ByteBuffer direct=_directBuffer.get();
if (direct!=null && !BufferUtil.isMappedBuffer(direct) && _directBuffer.compareAndSet(direct,null))
_cachedSize.addAndGet(-BufferUtil.length(direct));
@ -625,15 +638,15 @@ public class CachedContentFactory implements HttpContent.ContentFactory
ByteBuffer buffer = _directBuffer.get();
if (buffer==null)
{
ByteBuffer buffer2=CachedContentFactory.this.getDirectBuffer(_resource);
ByteBuffer mapped = CachedContentFactory.this.getMappedBuffer(_resource);
ByteBuffer direct = mapped==null?CachedContentFactory.this.getDirectBuffer(_resource):mapped;
if (buffer2==null)
if (direct==null)
LOG.warn("Could not load "+this);
else if (_directBuffer.compareAndSet(null,buffer2))
else if (_directBuffer.compareAndSet(null,direct))
{
buffer=buffer2;
if (!BufferUtil.isMappedBuffer(buffer) && _cachedSize.addAndGet(BufferUtil.length(buffer))>_maxCacheSize)
buffer=direct;
if (mapped==null && _cachedSize.addAndGet(BufferUtil.length(buffer))>_maxCacheSize)
shrinkCache();
}
else

View File

@ -913,39 +913,22 @@ public class BufferUtil
}
}
static final Field fdMappedByteBuffer;
static
{
Field fd = null;
try
{
fd=MappedByteBuffer.class.getDeclaredField("fd");
fd.setAccessible(true);
}
catch(Exception e)
{
}
fdMappedByteBuffer=fd;
}
public static boolean isMappedBuffer(ByteBuffer buffer)
{
if (!(buffer instanceof MappedByteBuffer))
return false;
MappedByteBuffer mapped = (MappedByteBuffer) buffer;
if (fdMappedByteBuffer!=null)
try
{
try
{
if (fdMappedByteBuffer.get(mapped) instanceof FileDescriptor)
return true;
}
catch(Exception e)
{
}
// Check if it really is a mapped buffer
mapped.isLoaded();
return true;
}
catch(UnsupportedOperationException e)
{
return false;
}
return false;
}