Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensible RetainableByteBufferPool #6322 #6513

Closed
Closed
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
03f4469
use RetainableByteBuffer
lorban May 28, 2021
b22c8cb
fix checkstyle
lorban May 28, 2021
a093959
replace toDetailString with straight RetainableByteBuffer.toString
lorban Jun 1, 2021
4c5404d
use RetainableByteBuffer for encrypted input buffer
lorban Jun 1, 2021
36794d9
add managed attributes
lorban Jun 7, 2021
9e58a2a
add a bit more monitoring to ArrayByteBufferPool
lorban Jun 7, 2021
ea19cae
modify SslConnectionFactory to add ability to use Pool<RetainableByte…
lorban Jun 7, 2021
2ed875c
remove unneeded counters
lorban Jun 9, 2021
2e2a90c
first shot at RetainableByteBuffer pool
lorban Jun 9, 2021
45a9b59
cleanup acquire/retain mechanism
lorban Jun 9, 2021
2ad1c63
use pool-specific ctor
lorban Jun 9, 2021
8eb2284
add optional ability to use RetainableByteBufferPool on the server
lorban Jun 9, 2021
9b2001d
revert old ByteBufferPool changes
lorban Jun 9, 2021
914a932
fix compilation error
lorban Jun 9, 2021
d2ead65
register RetainableByteBufferPool bean in AbstractConnector
lorban Jun 9, 2021
01b86c0
add RetainableByteBufferPool registration use in websocket
lorban Jun 9, 2021
dac025c
add RetainableByteBufferPool registration use in client
lorban Jun 9, 2021
114418e
add RetainableByteBufferPool registration use in upgrade
lorban Jun 9, 2021
8efd144
make RetainableByteBufferPool never null
lorban Jun 9, 2021
db9ba6c
make RetainableByteBufferPool never null
lorban Jun 9, 2021
f8314ef
make H2 connection use RetainableByteBuffer pool
lorban Jun 9, 2021
27a94db
make H2 connection use RetainableByteBuffer pool
lorban Jun 9, 2021
c437784
add missing header
lorban Jun 9, 2021
6eef227
generalize RetainableByteBuffer
lorban Jun 9, 2021
2f6000a
enable thread-local cache on the pool
lorban Jun 9, 2021
1879cc9
do not handle null retainableByteBufferPool
lorban Jun 10, 2021
5fc4f51
make http client register RetainableByteBufferPool
lorban Jun 10, 2021
06580bf
fix concurrency
lorban Jun 11, 2021
e4606a5
Merge remote-tracking branch 'origin/jetty-10.0.x' into jetty-10.0.x-…
lorban Jun 15, 2021
6085a77
add & fix javadoc
lorban Jun 15, 2021
c00a37a
improve javadoc
lorban Jun 17, 2021
3a98a3f
pre-allocate buckets
lorban Jun 17, 2021
3412ee7
add helper method that either finds or wraps a memory pool
lorban Jun 17, 2021
066343c
handle review comments
lorban Jun 18, 2021
34bbce0
handle review comments
lorban Jun 18, 2021
5f0b549
rename
lorban Jun 21, 2021
9557f46
rename
lorban Jun 21, 2021
378bfe7
change bean lookup
lorban Jun 21, 2021
435b6bb
fix checkstyle
lorban Jun 21, 2021
d8099d6
fix error message
lorban Jun 21, 2021
fb8c215
add memory calculation + clear + tests
lorban Jun 21, 2021
f06f511
add available memory calculation + tests
lorban Jun 21, 2021
3140091
rename
lorban Jun 21, 2021
cb727ba
add buffer eviction mechanism
lorban Jul 7, 2021
13f152d
use atomics to keep track of memory
lorban Jul 7, 2021
2378c09
simplify buffer eviction
lorban Jul 8, 2021
80e4c60
do not expose retain counter in API anymore
lorban Jul 8, 2021
d1d7688
set the initial retain counter to 0 + improve javadoc
lorban Jul 8, 2021
7df2b85
code alignment
lorban Jul 8, 2021
9dd531b
move findOrAdapt to interface
lorban Jul 8, 2021
eaef593
remove magic constant
lorban Jul 8, 2021
7c15fca
move findOrAdapt to interface
lorban Jul 8, 2021
3bfffc4
add retain test
lorban Jul 8, 2021
18b92dd
make getRequestBuffer private
lorban Jul 8, 2021
dab16b4
simplify if/else
lorban Jul 8, 2021
b7d5e0d
set the initial retain counter to 0 + improve javadoc
lorban Jul 8, 2021
df0d357
force releasing the buffer on exception
lorban Jul 8, 2021
491b4c6
fix checkstyle
lorban Jul 8, 2021
d5aa412
add back retain/release safety check
lorban Jul 8, 2021
920d515
fix tests visibility
lorban Jul 8, 2021
71b75fd
review comments
lorban Jul 8, 2021
e99698d
review comments
lorban Jul 8, 2021
1173d0d
rename to ArrayRetainableByteBufferPool
lorban Jul 9, 2021
8578783
do bound checking in an atomic function to avoid exposing illegal values
lorban Jul 9, 2021
410d402
fix checkstyle
lorban Jul 9, 2021
f2b545c
Improve #6322 log buckets in RetainableByteBufferPool
gregw Jul 12, 2021
8feecc0
Improve #6322 log buckets in RetainableByteBufferPool
gregw Jul 12, 2021
3b1700c
Cleanups
gregw Jul 12, 2021
cfc0a9b
#6322 Use RetainableByteBuffer and write a new pool for it
lorban May 28, 2021
42d4a98
Improve #6322 log buckets in RetainableByteBufferPool
gregw Jul 13, 2021
791403b
Merge branch 'jetty-10.0.x-6322-UseRetainableByteBuffer-LogBuckets' o…
gregw Jul 13, 2021
d607f21
Improve #6322 log buckets in RetainableByteBufferPool
gregw Jul 13, 2021
56449d9
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 13, 2021
ed11f6c
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 16, 2021
15c825f
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 20, 2021
b70b315
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 20, 2021
5e21903
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 21, 2021
9b10d54
Merge remote-tracking branch 'origin/jetty-10.0.x-6322-UseRetainableB…
gregw Jul 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -54,10 +54,12 @@
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ArrayRetainableByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.io.ssl.SslClientConnectionFactory;
import org.eclipse.jetty.util.Fields;
import org.eclipse.jetty.util.Jetty;
Expand Down Expand Up @@ -193,12 +195,14 @@ protected void doStart() throws Exception
threadPool.setName(name);
setExecutor(threadPool);
}
int maxBucketSize = executor instanceof ThreadPool.SizedThreadPool
? ((ThreadPool.SizedThreadPool)executor).getMaxThreads() / 2
: ProcessorUtils.availableProcessors() * 2;
ByteBufferPool byteBufferPool = getByteBufferPool();
if (byteBufferPool == null)
setByteBufferPool(new MappedByteBufferPool(2048,
executor instanceof ThreadPool.SizedThreadPool
? ((ThreadPool.SizedThreadPool)executor).getMaxThreads() / 2
: ProcessorUtils.availableProcessors() * 2));
setByteBufferPool(new MappedByteBufferPool(2048, maxBucketSize));
if (getBean(RetainableByteBufferPool.class) == null)
addBean(new ArrayRetainableByteBufferPool(0, 2048, 65536, maxBucketSize));
Scheduler scheduler = getScheduler();
if (scheduler == null)
setScheduler(new ScheduledExecutorScheduler(name + "-scheduler", false));
Expand Down
Expand Up @@ -29,9 +29,9 @@
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBuffer;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.slf4j.Logger;
Expand All @@ -43,6 +43,7 @@ public class HttpReceiverOverHTTP extends HttpReceiver implements HttpParser.Res

private final LongAdder inMessages = new LongAdder();
private final HttpParser parser;
private final RetainableByteBufferPool retainableByteBufferPool;
private RetainableByteBuffer networkBuffer;
private boolean shutdown;
private boolean complete;
Expand All @@ -61,6 +62,8 @@ public HttpReceiverOverHTTP(HttpChannelOverHTTP channel)
parser.setHeaderCacheSize(httpTransport.getHeaderCacheSize());
parser.setHeaderCacheCaseSensitive(httpTransport.isHeaderCacheCaseSensitive());
}

this.retainableByteBufferPool = RetainableByteBufferPool.findOrAdapt(httpClient, httpClient.getByteBufferPool());
}

@Override
Expand Down Expand Up @@ -111,9 +114,8 @@ private void reacquireNetworkBuffer()
private RetainableByteBuffer newNetworkBuffer()
{
HttpClient client = getHttpDestination().getHttpClient();
ByteBufferPool bufferPool = client.getByteBufferPool();
boolean direct = client.isUseInputDirectByteBuffers();
return new RetainableByteBuffer(bufferPool, client.getResponseBufferSize(), direct);
return retainableByteBufferPool.acquire(client.getResponseBufferSize(), direct);
}

private void releaseNetworkBuffer()
Expand Down Expand Up @@ -166,7 +168,7 @@ private void process()
return;
}

if (networkBuffer.getReferences() > 1)
if (networkBuffer.isRetained())
reacquireNetworkBuffer();

// The networkBuffer may have been reacquired.
Expand Down
Expand Up @@ -44,9 +44,9 @@
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBuffer;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.util.Attachable;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
Expand All @@ -71,6 +71,7 @@ public class HttpConnectionOverFCGI extends AbstractConnection implements IConne
private final ClientParser parser;
private RetainableByteBuffer networkBuffer;
private Object attachment;
private final RetainableByteBufferPool retainableByteBufferPool;

public HttpConnectionOverFCGI(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise)
{
Expand All @@ -81,6 +82,9 @@ public HttpConnectionOverFCGI(EndPoint endPoint, HttpDestination destination, Pr
this.delegate = new Delegate(destination);
this.parser = new ClientParser(new ResponseListener());
requests.addLast(0);

HttpClient client = destination.getHttpClient();
this.retainableByteBufferPool = RetainableByteBufferPool.findOrAdapt(client, client.getByteBufferPool());
}

public HttpDestination getHttpDestination()
Expand Down Expand Up @@ -135,8 +139,7 @@ private void reacquireNetworkBuffer()
private RetainableByteBuffer newNetworkBuffer()
{
HttpClient client = destination.getHttpClient();
ByteBufferPool bufferPool = client.getByteBufferPool();
return new RetainableByteBuffer(bufferPool, client.getResponseBufferSize(), client.isUseInputDirectByteBuffers());
return retainableByteBufferPool.acquire(client.getResponseBufferSize(), client.isUseInputDirectByteBuffers());
}

private void releaseNetworkBuffer()
Expand All @@ -161,7 +164,7 @@ void process()
if (parse(networkBuffer.getBuffer()))
return;

if (networkBuffer.getReferences() > 1)
if (networkBuffer.isRetained())
reacquireNetworkBuffer();

// The networkBuffer may have been reacquired.
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.component.LifeCycle;
Expand Down Expand Up @@ -67,7 +68,9 @@ public Connection newConnection(EndPoint endPoint, Map<String, Object> context)
parser.setMaxFrameLength(client.getMaxFrameLength());
parser.setMaxSettingsKeys(client.getMaxSettingsKeys());

HTTP2ClientConnection connection = new HTTP2ClientConnection(client, byteBufferPool, executor, endPoint,
RetainableByteBufferPool retainableByteBufferPool = RetainableByteBufferPool.findOrAdapt(client, byteBufferPool);

HTTP2ClientConnection connection = new HTTP2ClientConnection(client, retainableByteBufferPool, executor, endPoint,
parser, session, client.getInputBufferSize(), promise, listener);
connection.setUseInputDirectByteBuffers(client.isUseInputDirectByteBuffers());
connection.setUseOutputDirectByteBuffers(client.isUseOutputDirectByteBuffers());
Expand All @@ -81,9 +84,9 @@ private static class HTTP2ClientConnection extends HTTP2Connection implements Ca
private final Promise<Session> promise;
private final Session.Listener listener;

private HTTP2ClientConnection(HTTP2Client client, ByteBufferPool byteBufferPool, Executor executor, EndPoint endpoint, Parser parser, ISession session, int bufferSize, Promise<Session> promise, Session.Listener listener)
private HTTP2ClientConnection(HTTP2Client client, RetainableByteBufferPool retainableByteBufferPool, Executor executor, EndPoint endpoint, Parser parser, ISession session, int bufferSize, Promise<Session> promise, Session.Listener listener)
{
super(byteBufferPool, executor, endpoint, parser, session, bufferSize);
super(retainableByteBufferPool, executor, endpoint, parser, session, bufferSize);
this.client = client;
this.promise = promise;
this.listener = listener;
Expand Down
Expand Up @@ -23,10 +23,10 @@
import org.eclipse.jetty.http2.frames.DataFrame;
import org.eclipse.jetty.http2.parser.Parser;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBuffer;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.io.WriteFlusher;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
Expand All @@ -45,18 +45,18 @@ public class HTTP2Connection extends AbstractConnection implements WriteFlusher.
private final Queue<Runnable> tasks = new ArrayDeque<>();
private final HTTP2Producer producer = new HTTP2Producer();
private final AtomicLong bytesIn = new AtomicLong();
private final ByteBufferPool byteBufferPool;
private final RetainableByteBufferPool retainableByteBufferPool;
private final Parser parser;
private final ISession session;
private final int bufferSize;
private final ExecutionStrategy strategy;
private boolean useInputDirectByteBuffers;
private boolean useOutputDirectByteBuffers;

public HTTP2Connection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, Parser parser, ISession session, int bufferSize)
protected HTTP2Connection(RetainableByteBufferPool retainableByteBufferPool, Executor executor, EndPoint endPoint, Parser parser, ISession session, int bufferSize)
{
super(endPoint, executor);
this.byteBufferPool = byteBufferPool;
this.retainableByteBufferPool = retainableByteBufferPool;
this.parser = parser;
this.session = session;
this.bufferSize = bufferSize;
Expand Down Expand Up @@ -287,7 +287,7 @@ public Runnable produce()
return task;

// If more references than 1 (ie not just us), don't refill into buffer and risk compaction.
if (networkBuffer.getReferences() > 1)
if (networkBuffer.isRetained())
reacquireNetworkBuffer();
}

Expand Down Expand Up @@ -415,16 +415,43 @@ public void onConnectionFailure(int error, String reason)
}
}

private class NetworkBuffer extends RetainableByteBuffer implements Callback
private class NetworkBuffer implements Callback
{
private final RetainableByteBuffer delegate;

private NetworkBuffer()
{
super(byteBufferPool, bufferSize, isUseInputDirectByteBuffers());
delegate = retainableByteBufferPool.acquire(bufferSize, isUseInputDirectByteBuffers());
}

public ByteBuffer getBuffer()
{
return delegate.getBuffer();
}

public boolean isRetained()
{
return delegate.isRetained();
}

public boolean hasRemaining()
{
return delegate.hasRemaining();
}

public boolean release()
{
return delegate.release();
}

public void retain()
{
delegate.retain();
}

private void put(ByteBuffer source)
{
BufferUtil.append(getBuffer(), source);
BufferUtil.append(delegate.getBuffer(), source);
}

@Override
Expand All @@ -441,7 +468,7 @@ public void failed(Throwable failure)

private void completed(Throwable failure)
{
if (release() == 0)
if (delegate.release())
{
if (LOG.isDebugEnabled())
LOG.debug("Released retained {}", this, failure);
Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.jetty.http2.parser.WindowRateControl;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.server.AbstractConnectionFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
Expand Down Expand Up @@ -279,7 +280,9 @@ public Connection newConnection(Connector connector, EndPoint endPoint)
parser.setMaxFrameLength(getMaxFrameLength());
parser.setMaxSettingsKeys(getMaxSettingsKeys());

HTTP2Connection connection = new HTTP2ServerConnection(connector.getByteBufferPool(), connector.getExecutor(),
RetainableByteBufferPool retainableByteBufferPool = RetainableByteBufferPool.findOrAdapt(connector, connector.getByteBufferPool());

HTTP2Connection connection = new HTTP2ServerConnection(retainableByteBufferPool, connector.getExecutor(),
endPoint, httpConfiguration, parser, session, getInputBufferSize(), listener);
connection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers());
connection.setUseOutputDirectByteBuffers(isUseOutputDirectByteBuffers());
Expand Down
Expand Up @@ -45,8 +45,8 @@
import org.eclipse.jetty.http2.frames.SettingsFrame;
import org.eclipse.jetty.http2.parser.ServerParser;
import org.eclipse.jetty.http2.parser.SettingsBodyParser;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBufferPool;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.util.BufferUtil;
Expand Down Expand Up @@ -87,9 +87,9 @@ public static boolean isSupportedProtocol(String protocol)
private final HttpConfiguration httpConfig;
private boolean recycleHttpChannels = true;

public HTTP2ServerConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, HttpConfiguration httpConfig, ServerParser parser, ISession session, int inputBufferSize, ServerSessionListener listener)
public HTTP2ServerConnection(RetainableByteBufferPool retainableByteBufferPool, Executor executor, EndPoint endPoint, HttpConfiguration httpConfig, ServerParser parser, ISession session, int inputBufferSize, ServerSessionListener listener)
{
super(byteBufferPool, executor, endPoint, parser, session, inputBufferSize);
super(retainableByteBufferPool, executor, endPoint, parser, session, inputBufferSize);
this.listener = listener;
this.httpConfig = httpConfig;
}
Expand Down