From 35db0993d2456d8db6d01ec301c3499b9ce02ad7 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 18 Jan 2023 18:45:37 +0100 Subject: [PATCH] Fixes #9166 - Jetty 12: review/remove ByteBufferPool * Replaced usages of ByteBufferPool with RetainableByteBufferPool. * Removed ByteBufferPool and related classes. Signed-off-by: Simone Bordet --- .../client/client-io-arch.adoc | 2 +- .../client/ClientConnectorDocs.java | 2 +- .../client/http/HTTPClientDocs.java | 8 +- .../AbstractConnectorHttpClientTransport.java | 2 +- .../eclipse/jetty/client/ContentDecoder.java | 18 +- .../jetty/client/GZIPContentDecoder.java | 23 +- .../org/eclipse/jetty/client/HttpClient.java | 26 +- .../jetty/client/internal/HttpReceiver.java | 11 +- .../internal/HttpReceiverOverHTTP.java | 16 +- .../internal/HttpSenderOverHTTP.java | 83 ++--- .../jetty/client/HttpClientGZIPTest.java | 14 +- .../jetty/client/HttpClientTLSTest.java | 210 +++++------ .../jetty/client/ssl/SslConnectionTest.java | 8 +- .../util/MultiPartRequestContentTest.java | 2 +- .../internal/HttpChannelOverFCGI.java | 7 +- .../internal/HttpConnectionOverFCGI.java | 6 +- .../internal/HttpSenderOverFCGI.java | 19 +- .../jetty/fcgi/generator/ClientGenerator.java | 74 ++-- .../eclipse/jetty/fcgi/generator/Flusher.java | 73 ++-- .../jetty/fcgi/generator/Generator.java | 131 ++----- .../jetty/fcgi/generator/ServerGenerator.java | 56 ++- .../fcgi/generator/ClientGeneratorTest.java | 76 ++-- .../jetty/fcgi/parser/ClientParserTest.java | 126 +++---- .../server/internal/HttpStreamOverFCGI.java | 38 +- .../server/internal/ServerFCGIConnection.java | 10 +- .../server/AbstractHttpClientServerTest.java | 33 +- .../jetty/fcgi/server/HttpClientTest.java | 6 - .../jetty/http/GZIPContentDecoder.java | 103 ++---- .../content/CachingHttpContentFactory.java | 38 +- .../ValidatingCachingHttpContentFactory.java | 14 +- .../jetty/http/GZIPContentDecoderTest.java | 105 +++--- .../HttpClientTransportOverHTTP2.java | 2 +- .../jetty/http2/client/HTTP2Client.java | 12 +- .../client/HTTP2ClientConnectionFactory.java | 11 +- .../jetty/http2/internal/HTTP2Connection.java | 4 +- .../jetty/http2/internal/HTTP2Flusher.java | 22 +- .../jetty/http2/internal/HTTP2Session.java | 10 +- .../internal/generator/DataGenerator.java | 23 +- .../internal/generator/FrameGenerator.java | 19 +- .../http2/internal/generator/Generator.java | 28 +- .../internal/generator/GoAwayGenerator.java | 22 +- .../internal/generator/HeaderGenerator.java | 10 +- .../internal/generator/HeadersGenerator.java | 70 ++-- .../internal/generator/NoOpGenerator.java | 4 +- .../internal/generator/PingGenerator.java | 18 +- .../internal/generator/PrefaceGenerator.java | 7 +- .../internal/generator/PriorityGenerator.java | 18 +- .../generator/PushPromiseGenerator.java | 29 +- .../internal/generator/ResetGenerator.java | 18 +- .../internal/generator/SettingsGenerator.java | 20 +- .../generator/WindowUpdateGenerator.java | 18 +- .../parser/ContinuationBodyParser.java | 7 +- .../internal/parser/HeaderBlockFragments.java | 42 +-- .../internal/parser/HeaderBlockParser.java | 31 +- .../jetty/http2/internal/parser/Parser.java | 16 +- .../http2/internal/parser/ServerParser.java | 6 +- .../http2/frames/ContinuationParseTest.java | 16 +- .../http2/frames/DataGenerateParseTest.java | 22 +- .../jetty/http2/frames/FrameFloodTest.java | 10 +- .../http2/frames/GoAwayGenerateParseTest.java | 22 +- .../frames/HeadersGenerateParseTest.java | 22 +- .../frames/HeadersTooLargeParseTest.java | 14 +- .../http2/frames/MaxFrameSizeParseTest.java | 8 +- .../http2/frames/PingGenerateParseTest.java | 30 +- .../frames/PriorityGenerateParseTest.java | 22 +- .../frames/PushPromiseGenerateParseTest.java | 22 +- .../http2/frames/ResetGenerateParseTest.java | 22 +- .../frames/SettingsGenerateParseTest.java | 50 +-- .../jetty/http2/frames/UnknownParseTest.java | 10 +- .../frames/WindowUpdateGenerateParseTest.java | 22 +- .../AbstractHTTP2ServerConnectionFactory.java | 6 +- .../jetty/http2/tests/AbstractServerTest.java | 10 +- .../eclipse/jetty/http2/tests/BadURITest.java | 22 +- .../eclipse/jetty/http2/tests/CloseTest.java | 40 +- .../jetty/http2/tests/DataDemandTest.java | 14 +- .../http2/tests/FlowControlStrategyTest.java | 14 +- .../jetty/http2/tests/HTTP2CServerTest.java | 46 +-- .../jetty/http2/tests/HTTP2ServerTest.java | 199 +++++----- .../HttpClientTransportOverHTTP2Test.java | 24 +- .../jetty/http2/tests/PrefaceTest.java | 38 +- .../jetty/http2/tests/StreamCountTest.java | 10 +- .../jetty/http2/tests/StreamResetTest.java | 50 +-- .../HttpClientTransportOverHTTP3.java | 2 +- .../client/internal/ClientHTTP3Session.java | 4 +- .../internal/ClientHTTP3StreamConnection.java | 2 +- .../jetty/http3/internal/ControlFlusher.java | 41 +-- .../internal/ControlStreamConnection.java | 36 +- .../internal/DecoderStreamConnection.java | 6 +- .../internal/EncoderStreamConnection.java | 6 +- .../http3/internal/HTTP3StreamConnection.java | 15 +- .../http3/internal/InstructionFlusher.java | 36 +- .../internal/InstructionStreamConnection.java | 36 +- .../jetty/http3/internal/MessageFlusher.java | 22 +- .../UnidirectionalStreamConnection.java | 38 +- .../generator/CancelPushGenerator.java | 4 +- .../internal/generator/ControlGenerator.java | 6 +- .../internal/generator/DataGenerator.java | 22 +- .../internal/generator/FrameGenerator.java | 4 +- .../internal/generator/GoAwayGenerator.java | 22 +- .../internal/generator/HeadersGenerator.java | 34 +- .../generator/MaxPushIdGenerator.java | 4 +- .../internal/generator/MessageGenerator.java | 6 +- .../generator/PushPromiseGenerator.java | 4 +- .../internal/generator/SettingsGenerator.java | 24 +- .../http3/internal/DataGenerateParseTest.java | 8 +- .../internal/GoAwayGenerateParseTest.java | 8 +- .../internal/HeadersGenerateParseTest.java | 8 +- .../internal/SettingsGenerateParseTest.java | 8 +- .../jetty/http3/qpack/Instruction.java | 4 +- .../instruction/DuplicateInstruction.java | 16 +- .../IndexedNameEntryInstruction.java | 28 +- .../InsertCountIncrementInstruction.java | 16 +- .../LiteralNameEntryInstruction.java | 36 +- .../SectionAcknowledgmentInstruction.java | 16 +- .../instruction/SetCapacityInstruction.java | 16 +- .../StreamCancellationInstruction.java | 16 +- .../http3/qpack/InstructionGeneratorTest.java | 14 +- .../jetty/http3/qpack/QpackTestUtil.java | 24 +- .../http3/server/HTTP3ServerConnector.java | 4 +- .../server/internal/ServerHTTP3Session.java | 4 +- .../internal/ServerHTTP3StreamConnection.java | 2 +- .../jetty/io/AbstractByteBufferPool.java | 276 -------------- .../io/AbstractRetainableByteBuffer.java | 77 ++++ .../eclipse/jetty/io/ArrayByteBufferPool.java | 342 ------------------ .../io/ArrayRetainableByteBufferPool.java | 131 +++++-- .../jetty/io/ByteBufferAccumulator.java | 89 ++--- .../jetty/io/ByteBufferOutputStream2.java | 23 +- .../org/eclipse/jetty/io/ByteBufferPool.java | 152 -------- .../org/eclipse/jetty/io/ClientConnector.java | 16 +- .../jetty/io/LeakTrackingByteBufferPool.java | 163 --------- .../io/LogarithmicArrayByteBufferPool.java | 164 --------- .../jetty/io/MappedByteBufferPool.java | 340 ----------------- .../java/org/eclipse/jetty/io/Retainable.java | 12 +- .../jetty/io/RetainableByteBuffer.java | 170 ++++++--- .../jetty/io/RetainableByteBufferPool.java | 160 ++++++-- .../io/content/InputStreamContentSource.java | 19 +- .../jetty/io/content/PathContentSource.java | 12 +- .../NonRetainableByteBuffer.java} | 24 +- .../io/ssl/SslClientConnectionFactory.java | 14 +- .../eclipse/jetty/io/ssl/SslConnection.java | 125 ++++--- .../jetty/io/ArrayByteBufferPoolTest.java | 255 ------------- .../io/ArrayRetainableByteBufferPoolTest.java | 44 +-- .../jetty/io/ByteBufferAccumulatorTest.java | 182 +++++----- .../jetty/io/MappedByteBufferPoolTest.java | 185 ---------- .../jetty/io/SocketChannelEndPointTest.java | 4 +- .../eclipse/jetty/io/SslConnectionTest.java | 5 +- .../ForwardProxyWithDynamicTransportTest.java | 8 +- .../quic/client/ClientQuicConnection.java | 4 +- .../jetty/quic/client/ClientQuicSession.java | 6 +- .../jetty/quic/common/QuicConnection.java | 27 +- .../jetty/quic/common/QuicSession.java | 34 +- .../quic/server/QuicServerConnector.java | 4 +- .../quic/server/ServerQuicConnection.java | 22 +- .../jetty/quic/server/ServerQuicSession.java | 6 +- ...xml => jetty-bytebufferpool-quadratic.xml} | 6 +- .../main/config/etc/jetty-bytebufferpool.xml | 6 +- ...thmic.mod => bytebufferpool-quadratic.mod} | 5 +- .../main/config/modules/bytebufferpool.mod | 5 +- .../jetty/server/AbstractConnector.java | 64 ++-- .../server/AbstractNetworkConnector.java | 6 +- .../org/eclipse/jetty/server/Components.java | 4 +- .../jetty/server/ConnectionMetaData.java | 2 +- .../org/eclipse/jetty/server/Connector.java | 6 +- .../server/DetectorConnectionFactory.java | 29 +- .../eclipse/jetty/server/LocalConnector.java | 6 +- .../jetty/server/LowResourceMonitor.java | 2 +- .../server/NetworkTrafficServerConnector.java | 6 +- .../jetty/server/ProxyConnectionFactory.java | 96 ++--- .../eclipse/jetty/server/ResourceService.java | 15 +- .../java/org/eclipse/jetty/server/Server.java | 31 +- .../eclipse/jetty/server/ServerConnector.java | 4 +- .../jetty/server/SslConnectionFactory.java | 14 +- .../handler/BufferedResponseHandler.java | 31 +- .../jetty/server/handler/ConnectHandler.java | 48 ++- .../jetty/server/handler/ErrorProcessor.java | 114 +++--- .../jetty/server/handler/ResourceHandler.java | 22 +- .../server/handler/gzip/GzipRequest.java | 27 +- .../handler/gzip/GzipResponseAndCallback.java | 24 +- .../server/internal/HttpChannelState.java | 8 +- .../jetty/server/internal/HttpConnection.java | 54 +-- .../jetty/server/AbstractHttpTest.java | 4 +- .../jetty/server/ConnectorTimeoutTest.java | 4 +- .../jetty/server/DetectorConnectionTest.java | 4 - .../jetty/server/HttpServerTestBase.java | 2 +- .../eclipse/jetty/server/MockConnector.java | 23 +- .../eclipse/jetty/server/MockHttpStream.java | 2 +- .../jetty/server/ThreadStarvationTest.java | 9 +- .../handler/BufferedResponseHandlerTest.java | 2 - .../server/handler/DebugHandlerTest.java | 9 +- .../server/handler/ResourceHandlerTest.java | 2 +- .../jetty/server/ssl/SSLEngineTest.java | 2 +- .../ssl/ServerConnectorSslServerTest.java | 9 +- .../jetty/session/AbstractSessionManager.java | 2 +- .../eclipse/jetty/session/HouseKeeper.java | 6 +- .../jetty/session/HouseKeeperTest.java | 9 +- .../test/client/transport/AbstractTest.java | 4 +- .../ForwardProxyWithDynamicTransportTest.java | 8 +- .../transport/HttpClientDemandTest.java | 6 +- .../client/transport/HttpClientLoadTest.java | 30 +- .../transport/HttpClientTimeoutTest.java | 8 +- .../server/UnixDomainServerConnector.java | 6 +- .../core/client/CoreClientUpgradeRequest.java | 6 +- .../websocket/core/AbstractExtension.java | 8 +- .../jetty/websocket/core/CoreSession.java | 8 +- .../websocket/core/WebSocketComponents.java | 16 +- .../core/internal/FrameCaptureExtension.java | 11 +- .../websocket/core/internal/FrameFlusher.java | 64 ++-- .../jetty/websocket/core/internal/Parser.java | 64 ++-- .../internal/PerMessageDeflateExtension.java | 58 +-- .../core/internal/WebSocketConnection.java | 34 +- .../core/internal/WebSocketCoreSession.java | 6 +- .../messages/ByteBufferMessageSink.java | 14 +- .../messages/MessageOutputStream.java | 27 +- .../core/internal/messages/MessageWriter.java | 6 +- .../server/WebSocketServerComponents.java | 12 +- .../server/internal/AbstractHandshaker.java | 5 +- .../server/internal/RFC6455Handshaker.java | 6 +- .../server/internal/RFC8441Handshaker.java | 6 +- .../core/GeneratorParserRoundTripTest.java | 30 +- .../websocket/core/MessageHandlerTest.java | 10 +- .../core/ParserBadCloseStatusCodesTest.java | 4 - .../websocket/core/ParserBadOpCodesTest.java | 4 - .../jetty/websocket/core/ParserCapture.java | 2 +- .../jetty/websocket/core/WebSocketTester.java | 33 +- .../extensions/AbstractExtensionTest.java | 4 +- .../core/extensions/ExtensionTool.java | 7 +- .../core/internal/FrameFlusherTest.java | 10 +- .../core/util/MessageWriterTest.java | 6 +- .../maven/plugin/MavenServerConnector.java | 6 +- .../ee10/proxy/AsyncMiddleManServlet.java | 15 +- .../jetty/ee10/servlet/DefaultServlet.java | 16 +- .../jetty/ee10/servlet/ErrorHandler.java | 2 +- .../jetty/ee10/servlet/HttpOutput.java | 172 ++++----- .../jetty/ee10/servlet/ServletChannel.java | 2 +- .../common/JakartaWebSocketContainer.java | 6 +- .../JakartaWebSocketRemoteEndpoint.java | 4 +- .../jakarta/common/AbstractSessionTest.java | 6 +- .../common/messages/MessageWriterTest.java | 6 +- .../websocket/jakarta/tests/LocalServer.java | 6 +- .../tests/JakartaClientClassLoaderTest.java | 4 +- ...rtaClientShutdownWithServerWebAppTest.java | 4 +- .../websocket/client/WebSocketClient.java | 6 +- .../common/MessageInputStreamTest.java | 9 - .../common/MessageOutputStreamTest.java | 8 +- .../common/OutgoingMessageCapture.java | 7 +- .../TestableLeakTrackingBufferPool.java | 43 --- .../tests/JettyClientClassLoaderTest.java | 4 +- .../maven/plugin/MavenServerConnector.java | 6 +- .../ee9/nested/BufferedResponseHandler.java | 2 +- .../jetty/ee9/nested/ErrorHandler.java | 2 +- .../eclipse/jetty/ee9/nested/HttpChannel.java | 8 +- .../eclipse/jetty/ee9/nested/HttpOutput.java | 170 ++++----- .../jetty/ee9/nested/ResourceHandler.java | 18 +- .../jetty/ee9/nested/AbstractHttpTest.java | 4 +- .../jetty/ee9/nested/HttpOutputTest.java | 3 - .../jetty/ee9/nested/HttpWriterTest.java | 11 - .../jetty/ee9/nested/MockConnector.java | 23 +- .../ee9/proxy/AsyncMiddleManServlet.java | 13 +- .../jetty/ee9/servlet/DefaultServlet.java | 16 +- .../common/JakartaWebSocketContainer.java | 6 +- .../JakartaWebSocketRemoteEndpoint.java | 4 +- .../jakarta/common/AbstractSessionTest.java | 6 +- .../common/messages/MessageWriterTest.java | 6 +- .../websocket/jakarta/tests/LocalServer.java | 6 +- .../tests/JakartaClientClassLoaderTest.java | 4 +- ...rtaClientShutdownWithServerWebAppTest.java | 4 +- .../ee9/websocket/client/WebSocketClient.java | 6 +- .../common/MessageInputStreamTest.java | 9 - .../common/MessageOutputStreamTest.java | 42 ++- .../common/OutgoingMessageCapture.java | 7 +- .../TestableLeakTrackingBufferPool.java | 43 --- .../tests/JettyClientClassLoaderTest.java | 4 +- .../util/ArrayByteBufferPoolBenchmark.java | 67 ---- 273 files changed, 3199 insertions(+), 5133 deletions(-) delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java create mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractRetainableByteBuffer.java delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LeakTrackingByteBufferPool.java delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LogarithmicArrayByteBufferPool.java delete mode 100644 jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java rename jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/{NoopByteBufferPool.java => internal/NonRetainableByteBuffer.java} (52%) delete mode 100644 jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java delete mode 100644 jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/MappedByteBufferPoolTest.java rename jetty-core/jetty-server/src/main/config/etc/{jetty-bytebufferpool-logarithmic.xml => jetty-bytebufferpool-quadratic.xml} (62%) rename jetty-core/jetty-server/src/main/config/modules/{bytebufferpool-logarithmic.mod => bytebufferpool-quadratic.mod} (86%) delete mode 100644 jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/TestableLeakTrackingBufferPool.java delete mode 100644 jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/TestableLeakTrackingBufferPool.java delete mode 100644 tests/jetty-jmh/src/main/java/org/eclipse/jetty/util/ArrayByteBufferPoolBenchmark.java diff --git a/documentation/jetty-documentation/src/main/asciidoc/programming-guide/client/client-io-arch.adoc b/documentation/jetty-documentation/src/main/asciidoc/programming-guide/client/client-io-arch.adoc index 1a23358bf2ce..abc80009d302 100644 --- a/documentation/jetty-documentation/src/main/asciidoc/programming-guide/client/client-io-arch.adoc +++ b/documentation/jetty-documentation/src/main/asciidoc/programming-guide/client/client-io-arch.adoc @@ -33,7 +33,7 @@ The `ClientConnector` primarily wraps the link:{javadoc-url}/org/eclipse/jetty/i * a thread pool (in form of an `java.util.concurrent.Executor`) * a scheduler (in form of `org.eclipse.jetty.util.thread.Scheduler`) -* a byte buffer pool (in form of `org.eclipse.jetty.io.ByteBufferPool`) +* a byte buffer pool (in form of `org.eclipse.jetty.io.RetainableByteBufferPool`) * a TLS factory (in form of `org.eclipse.jetty.util.ssl.SslContextFactory.Client`) The `ClientConnector` is where you want to set those components after you have configured them. diff --git a/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java b/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java index e75148d384b4..3cbb717b3919 100644 --- a/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java +++ b/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java @@ -387,7 +387,7 @@ public void writeLine(String line, Callback callback) // Wrap the "telnet" ClientConnectionFactory with the SslClientConnectionFactory. connectionFactory = new SslClientConnectionFactory(clientConnector.getSslContextFactory(), - clientConnector.getByteBufferPool(), clientConnector.getExecutor(), connectionFactory); + clientConnector.getRetainableByteBufferPool(), clientConnector.getExecutor(), connectionFactory); // We will obtain a SslConnection now. CompletableFuture connectionPromise = new Promise.Completable<>(); diff --git a/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java b/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java index 9612d5204e57..90857287c2fe 100644 --- a/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java +++ b/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java @@ -63,7 +63,6 @@ import org.eclipse.jetty.http2.client.transport.HttpClientTransportOverHTTP2; import org.eclipse.jetty.http3.client.HTTP3Client; import org.eclipse.jetty.http3.client.transport.HttpClientTransportOverHTTP3; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.io.Content; @@ -334,14 +333,13 @@ public void asyncRequestContent() throws Exception // An event happens in some other class, in some other thread. class ContentPublisher { - void publish(ByteBufferPool bufferPool, byte[] bytes, boolean lastContent) + void publish(byte[] bytes, boolean lastContent) { // Wrap the bytes into a new ByteBuffer. ByteBuffer buffer = ByteBuffer.wrap(bytes); - // Offer the content, and release the ByteBuffer - // to the pool when the Callback is completed. - content.write(buffer, Callback.from(() -> bufferPool.release(buffer))); + // Write the content. + content.write(buffer, Callback.NOOP); // Close AsyncRequestContent when all the content is arrived. if (lastContent) diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectorHttpClientTransport.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectorHttpClientTransport.java index 801d6e13acd0..8e70421dc535 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectorHttpClientTransport.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectorHttpClientTransport.java @@ -52,7 +52,7 @@ protected void doStart() throws Exception { HttpClient httpClient = getHttpClient(); connector.setBindAddress(httpClient.getBindAddress()); - connector.setByteBufferPool(httpClient.getByteBufferPool()); + connector.setRetainableByteBufferPool(httpClient.getRetainableByteBufferPool()); connector.setConnectBlocking(httpClient.isConnectBlocking()); connector.setConnectTimeout(Duration.ofMillis(httpClient.getConnectTimeout())); connector.setExecutor(httpClient.getExecutor()); diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/ContentDecoder.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/ContentDecoder.java index 537a74f540ea..88e734451e1b 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/ContentDecoder.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/ContentDecoder.java @@ -20,6 +20,7 @@ import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.io.RetainableByteBuffer; /** * {@link ContentDecoder} decodes content bytes of a response. @@ -29,21 +30,14 @@ public interface ContentDecoder { /** - *

Decodes the bytes in the given {@code buffer} and returns decoded bytes, if any.

+ *

Decodes the bytes in the given {@code buffer} and returns the decoded bytes.

+ *

The returned {@link RetainableByteBuffer} containing the decoded bytes may + * be empty and must be released via {@link RetainableByteBuffer#release()}.

* * @param buffer the buffer containing encoded bytes - * @return a buffer containing decoded bytes, if any + * @return a buffer containing decoded bytes that must be released */ - public abstract ByteBuffer decode(ByteBuffer buffer); - - /** - *

Releases the ByteBuffer returned by {@link #decode(ByteBuffer)}.

- * - * @param decoded the ByteBuffer returned by {@link #decode(ByteBuffer)} - */ - public default void release(ByteBuffer decoded) - { - } + public abstract RetainableByteBuffer decode(ByteBuffer buffer); /** * Factory for {@link ContentDecoder}s; subclasses must implement {@link #newContentDecoder()}. diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/GZIPContentDecoder.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/GZIPContentDecoder.java index 304f2d3765e1..aa09fa56785c 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/GZIPContentDecoder.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/GZIPContentDecoder.java @@ -13,9 +13,8 @@ package org.eclipse.jetty.client; -import java.nio.ByteBuffer; - -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; /** * {@link ContentDecoder} for the "gzip" encoding. @@ -34,13 +33,13 @@ public GZIPContentDecoder(int bufferSize) this(null, bufferSize); } - public GZIPContentDecoder(ByteBufferPool byteBufferPool, int bufferSize) + public GZIPContentDecoder(RetainableByteBufferPool retainableByteBufferPool, int bufferSize) { - super(byteBufferPool, bufferSize); + super(retainableByteBufferPool, bufferSize); } @Override - protected boolean decodedChunk(ByteBuffer chunk) + protected boolean decodedChunk(RetainableByteBuffer chunk) { super.decodedChunk(chunk); return true; @@ -51,8 +50,8 @@ protected boolean decodedChunk(ByteBuffer chunk) */ public static class Factory extends ContentDecoder.Factory { + private final RetainableByteBufferPool retainableByteBufferPool; private final int bufferSize; - private final ByteBufferPool byteBufferPool; public Factory() { @@ -64,22 +63,22 @@ public Factory(int bufferSize) this(null, bufferSize); } - public Factory(ByteBufferPool byteBufferPool) + public Factory(RetainableByteBufferPool retainableByteBufferPool) { - this(byteBufferPool, DEFAULT_BUFFER_SIZE); + this(retainableByteBufferPool, DEFAULT_BUFFER_SIZE); } - public Factory(ByteBufferPool byteBufferPool, int bufferSize) + public Factory(RetainableByteBufferPool retainableByteBufferPool, int bufferSize) { super("gzip"); - this.byteBufferPool = byteBufferPool; + this.retainableByteBufferPool = retainableByteBufferPool; this.bufferSize = bufferSize; } @Override public ContentDecoder newContentDecoder() { - return new GZIPContentDecoder(byteBufferPool, bufferSize); + return new GZIPContentDecoder(retainableByteBufferPool, bufferSize); } } } diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 881b797fdbf1..ee7660f96f7f 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -49,10 +49,8 @@ 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; @@ -201,11 +199,9 @@ protected void doStart() throws Exception int maxBucketSize = executor instanceof ThreadPool.SizedThreadPool ? ((ThreadPool.SizedThreadPool)executor).getMaxThreads() / 2 : ProcessorUtils.availableProcessors() * 2; - ByteBufferPool byteBufferPool = getByteBufferPool(); - if (byteBufferPool == null) - setByteBufferPool(new MappedByteBufferPool(2048, maxBucketSize)); - if (getBean(RetainableByteBufferPool.class) == null) - addBean(new ArrayRetainableByteBufferPool(0, 2048, 65536, maxBucketSize)); + RetainableByteBufferPool retainableByteBufferPool = getRetainableByteBufferPool(); + if (retainableByteBufferPool == null) + setRetainableByteBufferPool(new ArrayRetainableByteBufferPool(0, 2048, 65536, maxBucketSize)); Scheduler scheduler = getScheduler(); if (scheduler == null) { @@ -224,7 +220,7 @@ protected void doStart() throws Exception handlers.put(new ProxyAuthenticationProtocolHandler(this)); handlers.put(new UpgradeProtocolHandler()); - decoderFactories.put(new GZIPContentDecoder.Factory(byteBufferPool)); + decoderFactories.put(new GZIPContentDecoder.Factory(retainableByteBufferPool)); cookieManager = newCookieManager(); cookieStore = cookieManager.getCookieStore(); @@ -650,19 +646,19 @@ public ProtocolHandler findProtocolHandler(Request request, Response response) } /** - * @return the {@link ByteBufferPool} of this HttpClient + * @return the {@link RetainableByteBufferPool} of this HttpClient */ - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return connector.getByteBufferPool(); + return connector.getRetainableByteBufferPool(); } /** - * @param byteBufferPool the {@link ByteBufferPool} of this HttpClient + * @param retainableByteBufferPool the {@link RetainableByteBufferPool} of this HttpClient */ - public void setByteBufferPool(ByteBufferPool byteBufferPool) + public void setRetainableByteBufferPool(RetainableByteBufferPool retainableByteBufferPool) { - connector.setByteBufferPool(byteBufferPool); + connector.setRetainableByteBufferPool(retainableByteBufferPool); } /** @@ -1156,6 +1152,6 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory); + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory); } } diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/internal/HttpReceiver.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/internal/HttpReceiver.java index 0bc75047e443..575bcab02ba8 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/internal/HttpReceiver.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/internal/HttpReceiver.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.client.internal; import java.net.URI; -import java.nio.ByteBuffer; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -27,8 +26,8 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.io.content.ContentSourceTransformer; -import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Promise; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.util.thread.SerializedInvoker; @@ -578,17 +577,17 @@ protected Content.Chunk transform(Content.Chunk inputChunk) _chunk.retain(); if (LOG.isDebugEnabled()) LOG.debug("decoding: {}", _chunk); - ByteBuffer decodedBuffer = _decoder.decode(_chunk.getByteBuffer()); + RetainableByteBuffer decodedBuffer = _decoder.decode(_chunk.getByteBuffer()); if (LOG.isDebugEnabled()) - LOG.debug("decoded: {}", BufferUtil.toDetailString(decodedBuffer)); + LOG.debug("decoded: {}", decodedBuffer); - if (BufferUtil.hasContent(decodedBuffer)) + if (decodedBuffer != null && decodedBuffer.hasRemaining()) { // The decoded ByteBuffer is a transformed "copy" of the // compressed one, so it has its own reference counter. if (LOG.isDebugEnabled()) LOG.debug("returning decoded content"); - return Content.Chunk.from(decodedBuffer, false, _decoder::release); + return Content.Chunk.asChunk(decodedBuffer.getByteBuffer(), false, decodedBuffer); } else { diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpReceiverOverHTTP.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpReceiverOverHTTP.java index 3d81b80a0e11..c1ebf22175cb 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpReceiverOverHTTP.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpReceiverOverHTTP.java @@ -66,7 +66,7 @@ public HttpReceiverOverHTTP(HttpChannelOverHTTP channel) parser.setHeaderCacheSize(httpTransport.getHeaderCacheSize()); parser.setHeaderCacheCaseSensitive(httpTransport.isHeaderCacheCaseSensitive()); } - retainableByteBufferPool = httpClient.getByteBufferPool().asRetainableByteBufferPool(); + retainableByteBufferPool = httpClient.getRetainableByteBufferPool(); } void receive() @@ -169,7 +169,7 @@ private HttpConnectionOverHTTP getHttpConnection() protected ByteBuffer getResponseBuffer() { - return networkBuffer == null ? null : networkBuffer.getBuffer(); + return networkBuffer == null ? null : networkBuffer.getByteBuffer(); } private void acquireNetworkBuffer() @@ -222,7 +222,7 @@ protected ByteBuffer onUpgradeFrom() HttpClient client = getHttpDestination().getHttpClient(); upgradeBuffer = BufferUtil.allocate(networkBuffer.remaining(), client.isUseInputDirectByteBuffers()); BufferUtil.clearToFill(upgradeBuffer); - BufferUtil.put(networkBuffer.getBuffer(), upgradeBuffer); + BufferUtil.put(networkBuffer.getByteBuffer(), upgradeBuffer); BufferUtil.flipToFlush(upgradeBuffer, 0); } releaseNetworkBuffer(); @@ -245,7 +245,7 @@ private boolean parseAndFill() while (true) { if (LOG.isDebugEnabled()) - LOG.debug("Parsing {} in {}", BufferUtil.toDetailString(networkBuffer.getBuffer()), this); + LOG.debug("Parsing {} in {}", BufferUtil.toDetailString(networkBuffer.getByteBuffer()), this); // Always parse even empty buffers to advance the parser. if (parse()) { @@ -269,7 +269,7 @@ private boolean parseAndFill() reacquireNetworkBuffer(); // The networkBuffer may have been reacquired. - int read = endPoint.fill(networkBuffer.getBuffer()); + int read = endPoint.fill(networkBuffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Read {} bytes in {} from {} in {}", read, networkBuffer, endPoint, this); @@ -309,7 +309,7 @@ private boolean parse() { while (true) { - boolean handle = parser.parseNext(networkBuffer.getBuffer()); + boolean handle = parser.parseNext(networkBuffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Parse result={} on {}", handle, this); Runnable action = getAndSetAction(null); @@ -347,7 +347,7 @@ private boolean parse() if (getHttpChannel().isTunnel(method, status)) return true; - if (networkBuffer.isEmpty()) + if (!networkBuffer.hasRemaining()) return false; if (!HttpStatus.isInformational(status)) @@ -359,7 +359,7 @@ private boolean parse() return false; } - if (networkBuffer.isEmpty()) + if (!networkBuffer.hasRemaining()) return false; } } diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpSenderOverHTTP.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpSenderOverHTTP.java index b7ae70d47cbf..b9bf13897cec 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpSenderOverHTTP.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/internal/HttpSenderOverHTTP.java @@ -23,9 +23,10 @@ import org.eclipse.jetty.http.HttpGenerator; import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.http.MetaData; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; 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.eclipse.jetty.util.IteratingCallback; @@ -41,7 +42,7 @@ public class HttpSenderOverHTTP extends HttpSender private final HttpGenerator generator = new HttpGenerator(); private HttpExchange exchange; private MetaData.Request metaData; - private ByteBuffer contentBuffer; + private ByteBuffer contentByteBuffer; private boolean lastContent; private Callback callback; private boolean shutdown; @@ -63,7 +64,7 @@ protected void sendHeaders(HttpExchange exchange, ByteBuffer contentBuffer, bool try { this.exchange = exchange; - this.contentBuffer = contentBuffer; + this.contentByteBuffer = contentBuffer; this.lastContent = lastContent; this.callback = callback; HttpRequest request = exchange.getRequest(); @@ -92,7 +93,7 @@ protected void sendContent(HttpExchange exchange, ByteBuffer contentBuffer, bool try { this.exchange = exchange; - this.contentBuffer = contentBuffer; + this.contentByteBuffer = contentBuffer; this.lastContent = lastContent; this.callback = callback; if (LOG.isDebugEnabled()) @@ -144,8 +145,8 @@ public String toString() private class HeadersCallback extends IteratingCallback { - private ByteBuffer headerBuffer; - private ByteBuffer chunkBuffer; + private RetainableByteBuffer headerBuffer; + private RetainableByteBuffer chunkBuffer; private boolean generated; private HeadersCallback() @@ -157,52 +158,54 @@ private HeadersCallback() protected Action process() throws Exception { HttpClient httpClient = getHttpChannel().getHttpDestination().getHttpClient(); - ByteBufferPool byteBufferPool = httpClient.getByteBufferPool(); + RetainableByteBufferPool bufferPool = httpClient.getRetainableByteBufferPool(); boolean useDirectByteBuffers = httpClient.isUseOutputDirectByteBuffers(); while (true) { - HttpGenerator.Result result = generator.generateRequest(metaData, headerBuffer, chunkBuffer, contentBuffer, lastContent); + ByteBuffer headerByteBuffer = headerBuffer == null ? null : headerBuffer.getByteBuffer(); + ByteBuffer chunkByteBuffer = chunkBuffer == null ? null : chunkBuffer.getByteBuffer(); + HttpGenerator.Result result = generator.generateRequest(metaData, headerByteBuffer, chunkByteBuffer, contentByteBuffer, lastContent); if (LOG.isDebugEnabled()) LOG.debug("Generated headers ({} bytes), chunk ({} bytes), content ({} bytes) - {}/{} for {}", - headerBuffer == null ? -1 : headerBuffer.remaining(), - chunkBuffer == null ? -1 : chunkBuffer.remaining(), - contentBuffer == null ? -1 : contentBuffer.remaining(), + headerByteBuffer == null ? -1 : headerByteBuffer.remaining(), + chunkByteBuffer == null ? -1 : chunkByteBuffer.remaining(), + contentByteBuffer == null ? -1 : contentByteBuffer.remaining(), result, generator, exchange.getRequest()); switch (result) { case NEED_HEADER: { - headerBuffer = byteBufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers); + headerBuffer = bufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers); break; } case HEADER_OVERFLOW: { - httpClient.getByteBufferPool().release(headerBuffer); + headerBuffer.release(); headerBuffer = null; throw new IllegalArgumentException("Request header too large"); } case NEED_CHUNK: { - chunkBuffer = byteBufferPool.acquire(HttpGenerator.CHUNK_SIZE, useDirectByteBuffers); + chunkBuffer = bufferPool.acquire(HttpGenerator.CHUNK_SIZE, useDirectByteBuffers); break; } case NEED_CHUNK_TRAILER: { - chunkBuffer = byteBufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers); + chunkBuffer = bufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers); break; } case FLUSH: { EndPoint endPoint = getHttpChannel().getHttpConnection().getEndPoint(); - if (headerBuffer == null) - headerBuffer = BufferUtil.EMPTY_BUFFER; - if (chunkBuffer == null) - chunkBuffer = BufferUtil.EMPTY_BUFFER; - if (contentBuffer == null) - contentBuffer = BufferUtil.EMPTY_BUFFER; - long bytes = headerBuffer.remaining() + chunkBuffer.remaining() + contentBuffer.remaining(); + if (headerByteBuffer == null) + headerByteBuffer = BufferUtil.EMPTY_BUFFER; + if (chunkByteBuffer == null) + chunkByteBuffer = BufferUtil.EMPTY_BUFFER; + if (contentByteBuffer == null) + contentByteBuffer = BufferUtil.EMPTY_BUFFER; + long bytes = headerByteBuffer.remaining() + chunkByteBuffer.remaining() + contentByteBuffer.remaining(); getHttpChannel().getHttpConnection().addBytesOut(bytes); - endPoint.write(this, headerBuffer, chunkBuffer, contentBuffer); + endPoint.write(this, headerByteBuffer, chunkByteBuffer, contentByteBuffer); generated = true; return Action.SCHEDULED; } @@ -263,21 +266,19 @@ protected void onCompleteFailure(Throwable cause) private void release() { - HttpClient httpClient = getHttpChannel().getHttpDestination().getHttpClient(); - ByteBufferPool bufferPool = httpClient.getByteBufferPool(); - if (!BufferUtil.isTheEmptyBuffer(headerBuffer)) - bufferPool.release(headerBuffer); + if (headerBuffer != null) + headerBuffer.release(); headerBuffer = null; - if (!BufferUtil.isTheEmptyBuffer(chunkBuffer)) - bufferPool.release(chunkBuffer); + if (chunkBuffer != null) + chunkBuffer.release(); chunkBuffer = null; - contentBuffer = null; + contentByteBuffer = null; } } private class ContentCallback extends IteratingCallback { - private ByteBuffer chunkBuffer; + private RetainableByteBuffer chunkBuffer; public ContentCallback() { @@ -288,14 +289,15 @@ public ContentCallback() protected Action process() throws Exception { HttpClient httpClient = getHttpChannel().getHttpDestination().getHttpClient(); - ByteBufferPool bufferPool = httpClient.getByteBufferPool(); + RetainableByteBufferPool bufferPool = httpClient.getRetainableByteBufferPool(); boolean useDirectByteBuffers = httpClient.isUseOutputDirectByteBuffers(); while (true) { - HttpGenerator.Result result = generator.generateRequest(null, null, chunkBuffer, contentBuffer, lastContent); + ByteBuffer chunkByteBuffer = chunkBuffer == null ? null : chunkBuffer.getByteBuffer(); + HttpGenerator.Result result = generator.generateRequest(null, null, chunkByteBuffer, contentByteBuffer, lastContent); if (LOG.isDebugEnabled()) LOG.debug("Generated content ({} bytes, last={}) - {}/{}", - contentBuffer == null ? -1 : contentBuffer.remaining(), + contentByteBuffer == null ? -1 : contentByteBuffer.remaining(), lastContent, result, generator); switch (result) { @@ -312,10 +314,10 @@ protected Action process() throws Exception case FLUSH: { EndPoint endPoint = getHttpChannel().getHttpConnection().getEndPoint(); - if (chunkBuffer != null) - endPoint.write(this, chunkBuffer, contentBuffer); + if (chunkByteBuffer != null) + endPoint.write(this, chunkByteBuffer, contentByteBuffer); else - endPoint.write(this, contentBuffer); + endPoint.write(this, contentByteBuffer); return Action.SCHEDULED; } case SHUTDOWN_OUT: @@ -350,11 +352,10 @@ protected void onCompleteFailure(Throwable cause) private void release() { - HttpClient httpClient = getHttpChannel().getHttpDestination().getHttpClient(); - ByteBufferPool bufferPool = httpClient.getByteBufferPool(); - bufferPool.release(chunkBuffer); + if (chunkBuffer != null) + chunkBuffer.release(); chunkBuffer = null; - contentBuffer = null; + contentByteBuffer = null; } } } diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java index 3083aaad235f..c9824032702e 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java @@ -27,9 +27,9 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.Callback; @@ -242,9 +242,9 @@ protected void service(Request request, org.eclipse.jetty.server.Response respon } }); - ByteBufferPool pool = client.getByteBufferPool(); - assumeTrue(pool instanceof MappedByteBufferPool); - MappedByteBufferPool bufferPool = (MappedByteBufferPool)pool; + RetainableByteBufferPool pool = client.getRetainableByteBufferPool(); + assumeTrue(pool instanceof ArrayRetainableByteBufferPool); + ArrayRetainableByteBufferPool bufferPool = (ArrayRetainableByteBufferPool)pool; ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) .scheme(scenario.getScheme()) @@ -254,9 +254,9 @@ protected void service(Request request, org.eclipse.jetty.server.Response respon assertEquals(HttpStatus.OK_200, response.getStatus()); assertArrayEquals(content, response.getContent()); - long directMemory = bufferPool.getMemory(true); + long directMemory = bufferPool.getDirectMemory(); assertThat(directMemory, lessThan((long)content.length)); - long heapMemory = bufferPool.getMemory(false); + long heapMemory = bufferPool.getHeapMemory(); assertThat(heapMemory, lessThan((long)content.length)); } diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTLSTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTLSTest.java index 28374c541c3d..74cd82f04e01 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTLSTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTLSTest.java @@ -44,9 +44,7 @@ import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ArrayByteBufferPool; 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.Connection; @@ -632,7 +630,7 @@ public void testNeverUsedConnectionThenServerIdleTimeout() throws Exception @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(connector.getRetainableByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected int networkFill(ByteBuffer input) throws IOException @@ -666,12 +664,12 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory) + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory) { @Override - protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) + protected SslConnection newSslConnection(RetainableByteBufferPool bufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(byteBufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(bufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected int networkFill(ByteBuffer input) throws IOException @@ -707,50 +705,6 @@ protected int networkFill(ByteBuffer input) throws IOException assertEquals(0, clientBytes.get()); } - protected class TestRetained extends ArrayRetainableByteBufferPool - { - private final ByteBufferPool _pool; - - public TestRetained(ByteBufferPool pool, int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - super(0, factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - _pool = pool; - } - - @Override - protected ByteBuffer allocate(int capacity) - { - return _pool.acquire(capacity, false); - } - - @Override - protected ByteBuffer allocateDirect(int capacity) - { - return _pool.acquire(capacity, true); - } - - @Override - protected void removed(RetainableByteBuffer retainedBuffer) - { - _pool.release(retainedBuffer.getBuffer()); - } - - @Override - public Pool poolFor(int capacity, boolean direct) - { - return super.poolFor(capacity, direct); - } - } - - private class TestByteBufferPool extends ArrayByteBufferPool - { - @Override - protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - return new TestRetained(this, factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - } - @Test public void testEncryptedInputBufferRepooling() throws Exception { @@ -759,9 +713,6 @@ public void testEncryptedInputBufferRepooling() throws Exception serverThreads.setName("server"); server = new Server(serverThreads); - ArrayByteBufferPool byteBufferPool = new TestByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = byteBufferPool.asRetainableByteBufferPool(); - server.addBean(byteBufferPool); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.addCustomizer(new SecureRequestCustomizer()); HttpConnectionFactory http = new HttpConnectionFactory(httpConfig); @@ -770,9 +721,8 @@ public void testEncryptedInputBufferRepooling() throws Exception @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = connector.getBean(RetainableByteBufferPool.class); - return new SslConnection(retainableByteBufferPool, byteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + RetainableByteBufferPool bufferPool = connector.getRetainableByteBufferPool(); + return new SslConnection(bufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected int networkFill(ByteBuffer input) throws IOException @@ -803,11 +753,12 @@ protected int networkFill(ByteBuffer input) throws IOException assertThrows(Exception.class, () -> client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).send()); - Pool bucket = ((TestRetained)retainableByteBufferPool).poolFor(16 * 1024 + 1, connector.getConnectionFactory(HttpConnectionFactory.class).isUseInputDirectByteBuffers()); + ArrayRetainableByteBufferPool bufferPool = (ArrayRetainableByteBufferPool)server.getRetainableByteBufferPool(); + Pool bucket = bufferPool.poolFor(16 * 1024 + 1, connector.getConnectionFactory(HttpConnectionFactory.class).isUseInputDirectByteBuffers()); assertEquals(1, bucket.size()); assertEquals(1, bucket.getIdleCount()); - long count = ssl.isDirectBuffersForDecryption() ? byteBufferPool.getDirectByteBufferCount() : byteBufferPool.getHeapByteBufferCount(); + long count = ssl.isDirectBuffersForDecryption() ? bufferPool.getDirectByteBufferCount() : bufferPool.getHeapByteBufferCount(); assertEquals(1, count); } @@ -817,26 +768,28 @@ public void testEncryptedOutputBufferRepooling() throws Exception SslContextFactory.Server serverTLSFactory = createServerSslContextFactory(); QueuedThreadPool serverThreads = new QueuedThreadPool(); serverThreads.setName("server"); - server = new Server(serverThreads); - List leakedBuffers = new CopyOnWriteArrayList<>(); - ArrayByteBufferPool byteBufferPool = new ArrayByteBufferPool() + List leakedBuffers = new CopyOnWriteArrayList<>(); + RetainableByteBufferPool bufferPool = new RetainableByteBufferPool.Wrapper(new ArrayRetainableByteBufferPool()) { @Override - public ByteBuffer acquire(int size, boolean direct) + public RetainableByteBuffer acquire(int size, boolean direct) { - ByteBuffer acquired = super.acquire(size, direct); - leakedBuffers.add(acquired); - return acquired; - } - - @Override - public void release(ByteBuffer buffer) - { - leakedBuffers.remove(buffer); - super.release(buffer); + RetainableByteBuffer.Wrapper buffer = new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + leakedBuffers.remove(this); + return released; + } + }; + leakedBuffers.add(buffer); + return buffer; } }; - server.addBean(byteBufferPool); + server = new Server(serverThreads, null, bufferPool); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.addCustomizer(new SecureRequestCustomizer()); HttpConnectionFactory http = new HttpConnectionFactory(httpConfig); @@ -845,9 +798,8 @@ public void release(ByteBuffer buffer) @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = connector.getBean(RetainableByteBufferPool.class); - return new SslConnection(retainableByteBufferPool, byteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + RetainableByteBufferPool bufferPool = connector.getRetainableByteBufferPool(); + return new SslConnection(bufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected boolean networkFlush(ByteBuffer output) throws IOException @@ -875,7 +827,7 @@ protected boolean networkFlush(ByteBuffer output) throws IOException assertThrows(Exception.class, () -> client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).send()); - byteBufferPool.asRetainableByteBufferPool().clear(); + bufferPool.clear(); await().atMost(5, TimeUnit.SECONDS).until(() -> leakedBuffers, is(empty())); } @@ -886,26 +838,28 @@ public void testEncryptedOutputBufferRepoolingAfterNetworkFlushReturnsFalse(bool SslContextFactory.Server serverTLSFactory = createServerSslContextFactory(); QueuedThreadPool serverThreads = new QueuedThreadPool(); serverThreads.setName("server"); - server = new Server(serverThreads); - List leakedBuffers = new CopyOnWriteArrayList<>(); - ArrayByteBufferPool byteBufferPool = new ArrayByteBufferPool() + List leakedBuffers = new CopyOnWriteArrayList<>(); + RetainableByteBufferPool bufferPool = new RetainableByteBufferPool.Wrapper(new ArrayRetainableByteBufferPool()) { @Override - public ByteBuffer acquire(int size, boolean direct) + public RetainableByteBuffer acquire(int size, boolean direct) { - ByteBuffer acquired = super.acquire(size, direct); - leakedBuffers.add(acquired); - return acquired; - } - - @Override - public void release(ByteBuffer buffer) - { - leakedBuffers.remove(buffer); - super.release(buffer); + RetainableByteBuffer.Wrapper buffer = new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + leakedBuffers.remove(this); + return released; + } + }; + leakedBuffers.add(buffer); + return buffer; } }; - server.addBean(byteBufferPool); + server = new Server(serverThreads, null, bufferPool); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.addCustomizer(new SecureRequestCustomizer()); HttpConnectionFactory http = new HttpConnectionFactory(httpConfig); @@ -915,9 +869,8 @@ public void release(ByteBuffer buffer) @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = connector.getBean(RetainableByteBufferPool.class); - return new SslConnection(retainableByteBufferPool, byteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + RetainableByteBufferPool bufferPool = connector.getRetainableByteBufferPool(); + return new SslConnection(bufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected boolean networkFlush(ByteBuffer output) throws IOException @@ -934,7 +887,7 @@ protected boolean networkFlush(ByteBuffer output) throws IOException server.setHandler(new EmptyServerHandler() { @Override - protected void service(Request request, Response response) throws Throwable + protected void service(Request request, Response response) { failFlush.set(true); EndPoint endPoint = request.getConnectionMetaData().getConnection().getEndPoint(); @@ -959,7 +912,7 @@ protected void service(Request request, Response response) throws Throwable assertThrows(Exception.class, () -> client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).send()); - byteBufferPool.asRetainableByteBufferPool().clear(); + bufferPool.clear(); await().atMost(5, TimeUnit.SECONDS).until(() -> leakedBuffers, is(empty())); } @@ -970,26 +923,28 @@ public void testEncryptedOutputBufferRepoolingAfterNetworkFlushThrows(boolean cl SslContextFactory.Server serverTLSFactory = createServerSslContextFactory(); QueuedThreadPool serverThreads = new QueuedThreadPool(); serverThreads.setName("server"); - server = new Server(serverThreads); - List leakedBuffers = new CopyOnWriteArrayList<>(); - ArrayByteBufferPool byteBufferPool = new ArrayByteBufferPool() + List leakedBuffers = new CopyOnWriteArrayList<>(); + RetainableByteBufferPool bufferPool = new RetainableByteBufferPool.Wrapper(new ArrayRetainableByteBufferPool()) { @Override - public ByteBuffer acquire(int size, boolean direct) + public RetainableByteBuffer acquire(int size, boolean direct) { - ByteBuffer acquired = super.acquire(size, direct); - leakedBuffers.add(acquired); - return acquired; - } - - @Override - public void release(ByteBuffer buffer) - { - leakedBuffers.remove(buffer); - super.release(buffer); + RetainableByteBuffer.Wrapper buffer = new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + leakedBuffers.remove(this); + return released; + } + }; + leakedBuffers.add(buffer); + return buffer; } }; - server.addBean(byteBufferPool); + server = new Server(serverThreads, null, bufferPool); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.addCustomizer(new SecureRequestCustomizer()); HttpConnectionFactory http = new HttpConnectionFactory(httpConfig); @@ -999,9 +954,8 @@ public void release(ByteBuffer buffer) @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = connector.getBean(RetainableByteBufferPool.class); - return new SslConnection(retainableByteBufferPool, byteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + RetainableByteBufferPool bufferPool = connector.getRetainableByteBufferPool(); + return new SslConnection(bufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected boolean networkFlush(ByteBuffer output) throws IOException @@ -1018,7 +972,7 @@ protected boolean networkFlush(ByteBuffer output) throws IOException server.setHandler(new EmptyServerHandler() { @Override - protected void service(Request request, Response response) throws Throwable + protected void service(Request request, Response response) { failFlush.set(true); EndPoint endPoint = request.getConnectionMetaData().getConnection().getEndPoint(); @@ -1043,7 +997,7 @@ protected void service(Request request, Response response) throws Throwable assertThrows(Exception.class, () -> client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).send()); - byteBufferPool.asRetainableByteBufferPool().clear(); + bufferPool.clear(); await().atMost(5, TimeUnit.SECONDS).until(() -> leakedBuffers, is(empty())); } @@ -1063,7 +1017,7 @@ public void testNeverUsedConnectionThenClientIdleTimeout() throws Exception @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(connector.getRetainableByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected int networkFill(ByteBuffer input) throws IOException @@ -1098,12 +1052,12 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory) + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory) { @Override - protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) + protected SslConnection newSslConnection(RetainableByteBufferPool bufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(byteBufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(bufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected int networkFill(ByteBuffer input) throws IOException @@ -1160,12 +1114,12 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory) + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory) { @Override - protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) + protected SslConnection newSslConnection(RetainableByteBufferPool bufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(byteBufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(bufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected SSLEngineResult wrap(SSLEngine sslEngine, ByteBuffer[] input, ByteBuffer output) throws SSLException @@ -1204,7 +1158,7 @@ public void testTLSLargeFragments() throws Exception @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(connector.getRetainableByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected SSLEngineResult unwrap(SSLEngine sslEngine, ByteBuffer input, ByteBuffer output) throws SSLException @@ -1240,12 +1194,12 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory) + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory) { @Override - protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) + protected SslConnection newSslConnection(RetainableByteBufferPool bufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(byteBufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(bufferPool, executor, endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected SSLEngineResult wrap(SSLEngine sslEngine, ByteBuffer[] input, ByteBuffer output) throws SSLException diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java index b519f1af452e..bfbd8a1e8a1b 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslConnectionTest.java @@ -19,10 +19,10 @@ import javax.net.ssl.SSLHandshakeException; import org.eclipse.jetty.io.AbstractConnection; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.ByteArrayEndPoint; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.BufferUtil; @@ -43,13 +43,13 @@ public void testSslConnectionClosedBeforeFill() throws Exception sslContextFactory.setKeyStorePassword("storepwd"); sslContextFactory.start(); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.start(); ByteArrayEndPoint endPoint = new ByteArrayEndPoint(); SSLEngine sslEngine = sslContextFactory.newSSLEngine(); sslEngine.setUseClientMode(false); - SslConnection sslConnection = new SslConnection(byteBufferPool, threadPool, endPoint, sslEngine); + SslConnection sslConnection = new SslConnection(bufferPool, threadPool, endPoint, sslEngine); EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint(); sslEndPoint.setConnection(new AbstractConnection(sslEndPoint, threadPool) { diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/util/MultiPartRequestContentTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/util/MultiPartRequestContentTest.java index c43a57d58c26..198d054346f9 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/util/MultiPartRequestContentTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/util/MultiPartRequestContentTest.java @@ -283,7 +283,7 @@ protected void process(MultiPartFormData.Parts parts) throws Exception }); MultiPartRequestContent multiPart = new MultiPartRequestContent(); - PathRequestContent content = new PathRequestContent(contentType, tmpPath, client.getByteBufferPool().asRetainableByteBufferPool()); + PathRequestContent content = new PathRequestContent(contentType, tmpPath, client.getRetainableByteBufferPool()); content.setUseDirectByteBuffers(client.isUseOutputDirectByteBuffers()); multiPart.addPart(new MultiPart.ContentSourcePart(name, tmpPath.getFileName().toString(), null, content)); multiPart.close(); diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpChannelOverFCGI.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpChannelOverFCGI.java index f45e1b762e33..13276396f499 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpChannelOverFCGI.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpChannelOverFCGI.java @@ -21,12 +21,13 @@ import org.eclipse.jetty.client.internal.HttpReceiver; import org.eclipse.jetty.client.internal.HttpSender; import org.eclipse.jetty.fcgi.generator.Flusher; -import org.eclipse.jetty.fcgi.generator.Generator; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.IdleTimeout; +import org.eclipse.jetty.io.RetainableByteBufferPool; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Promise; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -161,9 +162,9 @@ else if (!connection.closeByHTTP(responseHeaders)) release(); } - protected void flush(Generator.Result... results) + protected void flush(RetainableByteBufferPool.Accumulator accumulator, Callback callback) { - flusher.flush(results); + flusher.flush(accumulator, callback); } void receive() diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpConnectionOverFCGI.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpConnectionOverFCGI.java index d91360e1505a..7fe15be90753 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpConnectionOverFCGI.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpConnectionOverFCGI.java @@ -81,7 +81,7 @@ public HttpConnectionOverFCGI(EndPoint endPoint, Destination destination, Promis this.parser = new ClientParser(new ResponseListener()); requests.addLast(0); HttpClient client = destination.getHttpClient(); - this.networkByteBufferPool = client.getByteBufferPool().asRetainableByteBufferPool(); + this.networkByteBufferPool = client.getRetainableByteBufferPool(); } public HttpDestination getHttpDestination() @@ -161,14 +161,14 @@ boolean parseAndFill() { while (true) { - if (parse(networkBuffer.getBuffer())) + if (parse(networkBuffer.getByteBuffer())) return false; if (networkBuffer.isRetained()) reacquireNetworkBuffer(); // The networkBuffer may have been reacquired. - int read = endPoint.fill(networkBuffer.getBuffer()); + int read = endPoint.fill(networkBuffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Read {} bytes from {}", read, endPoint); diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpSenderOverFCGI.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpSenderOverFCGI.java index 1ebf73917938..d41bf64e6f44 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpSenderOverFCGI.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/client/transport/internal/HttpSenderOverFCGI.java @@ -26,10 +26,10 @@ import org.eclipse.jetty.fcgi.FCGI; import org.eclipse.jetty.fcgi.client.transport.HttpClientTransportOverFCGI; import org.eclipse.jetty.fcgi.generator.ClientGenerator; -import org.eclipse.jetty.fcgi.generator.Generator; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Jetty; import org.eclipse.jetty.util.StringUtil; @@ -42,7 +42,7 @@ public HttpSenderOverFCGI(HttpChannel channel) { super(channel); HttpClient httpClient = channel.getHttpDestination().getHttpClient(); - this.generator = new ClientGenerator(httpClient.getByteBufferPool(), httpClient.isUseOutputDirectByteBuffers()); + this.generator = new ClientGenerator(httpClient.getRetainableByteBufferPool(), httpClient.isUseOutputDirectByteBuffers()); } @Override @@ -98,18 +98,18 @@ protected void sendHeaders(HttpExchange exchange, ByteBuffer contentBuffer, bool HttpClientTransportOverFCGI transport = (HttpClientTransportOverFCGI)getHttpChannel().getHttpDestination().getHttpClient().getTransport(); transport.customize(request, fcgiHeaders); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(generator.getRetainableByteBufferPool()); int id = getHttpChannel().getRequest(); if (contentBuffer.hasRemaining() || lastContent) { - Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders, Callback.NOOP); - Generator.Result contentResult = generator.generateRequestContent(id, contentBuffer, lastContent, callback); - getHttpChannel().flush(headersResult, contentResult); + generator.generateRequestHeaders(accumulator, id, fcgiHeaders); + generator.generateRequestContent(accumulator, id, contentBuffer, lastContent); } else { - Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders, callback); - getHttpChannel().flush(headersResult); + generator.generateRequestHeaders(accumulator, id, fcgiHeaders); } + getHttpChannel().flush(accumulator, callback); } @Override @@ -117,9 +117,10 @@ protected void sendContent(HttpExchange exchange, ByteBuffer contentBuffer, bool { if (contentBuffer.hasRemaining() || lastContent) { + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(generator.getRetainableByteBufferPool()); int request = getHttpChannel().getRequest(); - Generator.Result result = generator.generateRequestContent(request, contentBuffer, lastContent, callback); - getHttpChannel().flush(result); + generator.generateRequestContent(accumulator, request, contentBuffer, lastContent); + getHttpChannel().flush(accumulator, callback); } else { diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ClientGenerator.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ClientGenerator.java index 205f13294683..d7eef604a068 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ClientGenerator.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ClientGenerator.java @@ -22,9 +22,9 @@ import org.eclipse.jetty.fcgi.FCGI; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; public class ClientGenerator extends Generator { @@ -33,17 +33,17 @@ public class ClientGenerator extends Generator // 0x7F_FF - 4 (the 4 is to make room for the name (or value) length). public static final int MAX_PARAM_LENGTH = 0x7F_FF - 4; - public ClientGenerator(ByteBufferPool byteBufferPool) + public ClientGenerator(RetainableByteBufferPool bufferPool) { - this(byteBufferPool, true); + this(bufferPool, true); } - public ClientGenerator(ByteBufferPool byteBufferPool, boolean useDirectByteBuffers) + public ClientGenerator(RetainableByteBufferPool bufferPool, boolean useDirectByteBuffers) { - super(byteBufferPool, useDirectByteBuffers); + super(bufferPool, useDirectByteBuffers); } - public Result generateRequestHeaders(int request, HttpFields fields, Callback callback) + public void generateRequestHeaders(RetainableByteBufferPool.Accumulator accumulator, int request, HttpFields fields) { request &= 0xFF_FF; @@ -79,30 +79,31 @@ public Result generateRequestHeaders(int request, HttpFields fields, Callback ca // One FCGI_BEGIN_REQUEST + N FCGI_PARAMS + one last FCGI_PARAMS - ByteBuffer beginRequestBuffer = acquire(16); - BufferUtil.clearToFill(beginRequestBuffer); - Result result = new Result(getByteBufferPool(), callback); - result = result.append(beginRequestBuffer, true); + RetainableByteBuffer beginBuffer = accumulator.acquire(16, isUseDirectByteBuffers()); + accumulator.append(beginBuffer); + ByteBuffer beginByteBuffer = beginBuffer.getByteBuffer(); + BufferUtil.clearToFill(beginByteBuffer); // Generate the FCGI_BEGIN_REQUEST frame - beginRequestBuffer.putInt(0x01_01_00_00 + request); - beginRequestBuffer.putInt(0x00_08_00_00); + beginByteBuffer.putInt(0x01_01_00_00 + request); + beginByteBuffer.putInt(0x00_08_00_00); // Hardcode RESPONDER role and KEEP_ALIVE flag - beginRequestBuffer.putLong(0x00_01_01_00_00_00_00_00L); - BufferUtil.flipToFlush(beginRequestBuffer, 0); + beginByteBuffer.putLong(0x00_01_01_00_00_00_00_00L); + BufferUtil.flipToFlush(beginByteBuffer, 0); int index = 0; while (fieldsLength > 0) { int capacity = 8 + Math.min(maxCapacity, fieldsLength); - ByteBuffer buffer = acquire(capacity); - BufferUtil.clearToFill(buffer); - result = result.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(capacity, isUseDirectByteBuffers()); + accumulator.append(buffer); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + BufferUtil.clearToFill(byteBuffer); // Generate the FCGI_PARAMS frame - buffer.putInt(0x01_04_00_00 + request); - buffer.putShort((short)0); - buffer.putShort((short)0); + byteBuffer.putInt(0x01_04_00_00 + request); + byteBuffer.putShort((short)0); + byteBuffer.putShort((short)0); capacity -= 8; int length = 0; @@ -117,10 +118,10 @@ public Result generateRequestHeaders(int request, HttpFields fields, Callback ca if (required > capacity) break; - putParamLength(buffer, nameLength); - putParamLength(buffer, valueLength); - buffer.put(nameBytes); - buffer.put(valueBytes); + putParamLength(byteBuffer, nameLength); + putParamLength(byteBuffer, valueLength); + byteBuffer.put(nameBytes); + byteBuffer.put(valueBytes); length += required; fieldsLength -= required; @@ -128,20 +129,19 @@ public Result generateRequestHeaders(int request, HttpFields fields, Callback ca index += 2; } - buffer.putShort(4, (short)length); - BufferUtil.flipToFlush(buffer, 0); + byteBuffer.putShort(4, (short)length); + BufferUtil.flipToFlush(byteBuffer, 0); } - ByteBuffer lastParamsBuffer = acquire(8); - BufferUtil.clearToFill(lastParamsBuffer); - result = result.append(lastParamsBuffer, true); + RetainableByteBuffer lastBuffer = accumulator.acquire(8, isUseDirectByteBuffers()); + accumulator.append(lastBuffer); + ByteBuffer lastByteBuffer = lastBuffer.getByteBuffer(); + BufferUtil.clearToFill(lastByteBuffer); // Generate the last FCGI_PARAMS frame - lastParamsBuffer.putInt(0x01_04_00_00 + request); - lastParamsBuffer.putInt(0x00_00_00_00); - BufferUtil.flipToFlush(lastParamsBuffer, 0); - - return result; + lastByteBuffer.putInt(0x01_04_00_00 + request); + lastByteBuffer.putInt(0x00_00_00_00); + BufferUtil.flipToFlush(lastByteBuffer, 0); } private int putParamLength(ByteBuffer buffer, int length) @@ -159,8 +159,8 @@ private int bytesForLength(int length) return length > 127 ? 4 : 1; } - public Result generateRequestContent(int request, ByteBuffer content, boolean lastContent, Callback callback) + public void generateRequestContent(RetainableByteBufferPool.Accumulator accumulator, int request, ByteBuffer content, boolean lastContent) { - return generateContent(request, content, false, lastContent, callback, FCGI.FrameType.STDIN); + generateContent(accumulator, request, content, lastContent, FCGI.FrameType.STDIN); } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Flusher.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Flusher.java index 49b2865f3a3b..58ebcb0e74df 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Flusher.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Flusher.java @@ -15,9 +15,12 @@ import java.nio.ByteBuffer; import java.util.ArrayDeque; +import java.util.List; import java.util.Queue; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IteratingCallback; import org.eclipse.jetty.util.thread.AutoLock; import org.slf4j.Logger; @@ -28,7 +31,7 @@ public class Flusher private static final Logger LOG = LoggerFactory.getLogger(Flusher.class); private final AutoLock lock = new AutoLock(); - private final Queue queue = new ArrayDeque<>(); + private final Queue queue = new ArrayDeque<>(); private final IteratingCallback flushCallback = new FlushCallback(); private final EndPoint endPoint; @@ -37,24 +40,21 @@ public Flusher(EndPoint endPoint) this.endPoint = endPoint; } - public void flush(Generator.Result... results) + public void flush(RetainableByteBufferPool.Accumulator accumulator, Callback callback) { - for (Generator.Result result : results) - { - offer(result); - } + offer(new Entry(accumulator, callback)); flushCallback.iterate(); } - private void offer(Generator.Result result) + private void offer(Entry entry) { try (AutoLock ignored = lock.lock()) { - queue.offer(result); + queue.offer(entry); } } - private Generator.Result poll() + private Entry poll() { try (AutoLock ignored = lock.lock()) { @@ -64,35 +64,32 @@ private Generator.Result poll() public void shutdown() { - flush(new ShutdownResult()); + flush(null, Callback.from(() -> + { + if (LOG.isDebugEnabled()) + LOG.debug("Shutting down {}", endPoint); + endPoint.shutdownOutput(); + })); } private class FlushCallback extends IteratingCallback { - private Generator.Result active; + private Entry active; @Override protected Action process() throws Exception { // Look if other writes are needed. - Generator.Result result = poll(); - if (result == null) + Entry entry = poll(); + if (entry == null) { // No more writes to do, return. return Action.IDLE; } - // Attempt to gather another result. - // Most often there is another result in the - // queue so this is a real optimization because - // it sends both results in just one TCP packet. - Generator.Result other = poll(); - if (other != null) - result = result.join(other); - - active = result; - ByteBuffer[] buffers = result.getByteBuffers(); - endPoint.write(this, buffers); + active = entry; + List buffers = entry.accumulator.getByteBuffers(); + endPoint.write(this, buffers.toArray(ByteBuffer[]::new)); return Action.SCHEDULED; } @@ -121,38 +118,30 @@ public void onCompleteFailure(Throwable x) while (true) { - Generator.Result result = poll(); - if (result == null) + Entry entry = poll(); + if (entry == null) break; - result.failed(x); + entry.failed(x); } } } - private class ShutdownResult extends Generator.Result + private record Entry(RetainableByteBufferPool.Accumulator accumulator, Callback callback) implements Callback { - private ShutdownResult() - { - super(null, null); - } - @Override public void succeeded() { - shutdown(); + if (accumulator != null) + accumulator.release(); + callback.succeeded(); } @Override public void failed(Throwable x) { - shutdown(); - } - - private void shutdown() - { - if (LOG.isDebugEnabled()) - LOG.debug("Shutting down {}", endPoint); - endPoint.shutdownOutput(); + if (accumulator != null) + accumulator.release(); + callback.failed(x); } } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java index 72e5ae2d513e..b96612d21e9d 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java @@ -14,150 +14,69 @@ package org.eclipse.jetty.fcgi.generator; import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; import org.eclipse.jetty.fcgi.FCGI; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; public class Generator { public static final int MAX_CONTENT_LENGTH = 0xFF_FF; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final boolean useDirectByteBuffers; - public Generator(ByteBufferPool byteBufferPool, boolean useDirectByteBuffers) + public Generator(RetainableByteBufferPool bufferPool, boolean useDirectByteBuffers) { - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.useDirectByteBuffers = useDirectByteBuffers; } - ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return bufferPool; } - ByteBuffer acquire(int capacity) + public boolean isUseDirectByteBuffers() { - return byteBufferPool.acquire(capacity, useDirectByteBuffers); + return useDirectByteBuffers; } - protected Result generateContent(int id, ByteBuffer content, boolean recycle, boolean lastContent, Callback callback, FCGI.FrameType frameType) + protected void generateContent(RetainableByteBufferPool.Accumulator accumulator, int id, ByteBuffer content, boolean lastContent, FCGI.FrameType frameType) { id &= 0xFF_FF; int contentLength = content == null ? 0 : content.remaining(); - Result result = new Result(byteBufferPool, callback); while (contentLength > 0 || lastContent) { - ByteBuffer buffer = acquire(8); - BufferUtil.clearToFill(buffer); - result = result.append(buffer, true); - - // Generate the frame header - buffer.put((byte)0x01); - buffer.put((byte)frameType.code); - buffer.putShort((short)id); + RetainableByteBuffer buffer = accumulator.acquire(8, isUseDirectByteBuffers()); + accumulator.append(buffer); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + BufferUtil.clearToFill(byteBuffer); + + // Generate the frame header. + byteBuffer.put((byte)0x01); + byteBuffer.put((byte)frameType.code); + byteBuffer.putShort((short)id); int length = Math.min(MAX_CONTENT_LENGTH, contentLength); - buffer.putShort((short)length); - buffer.putShort((short)0); - BufferUtil.flipToFlush(buffer, 0); + byteBuffer.putShort((short)length); + byteBuffer.putShort((short)0); + BufferUtil.flipToFlush(byteBuffer, 0); if (contentLength == 0) break; - // Slice the content to avoid copying + // Slice the content to avoid copying. int limit = content.limit(); content.limit(content.position() + length); ByteBuffer slice = content.slice(); - // Don't recycle the slice - result = result.append(slice, false); + // Don't recycle the slice. + accumulator.append(RetainableByteBuffer.asNonRetainable(slice)); content.position(content.limit()); content.limit(limit); contentLength -= length; - // Recycle the content buffer if needed - if (recycle && contentLength == 0) - result = result.append(content, true); - } - - return result; - } - - // TODO: rewrite this class in light of ByteBufferPool.Lease. - public static class Result implements Callback - { - private final List callbacks = new ArrayList<>(2); - private final List buffers = new ArrayList<>(8); - private final List recycles = new ArrayList<>(8); - private final ByteBufferPool byteBufferPool; - - public Result(ByteBufferPool byteBufferPool, Callback callback) - { - this.byteBufferPool = byteBufferPool; - this.callbacks.add(callback); - } - - public Result append(ByteBuffer buffer, boolean recycle) - { - if (buffer != null) - { - buffers.add(buffer); - recycles.add(recycle); - } - return this; - } - - public Result join(Result that) - { - callbacks.addAll(that.callbacks); - buffers.addAll(that.buffers); - recycles.addAll(that.recycles); - return this; - } - - public ByteBuffer[] getByteBuffers() - { - return buffers.toArray(new ByteBuffer[0]); - } - - @Override - @SuppressWarnings("ForLoopReplaceableByForEach") - public void succeeded() - { - recycle(); - for (int i = 0; i < callbacks.size(); ++i) - { - Callback callback = callbacks.get(i); - if (callback != null) - callback.succeeded(); - } - } - - @Override - @SuppressWarnings("ForLoopReplaceableByForEach") - public void failed(Throwable x) - { - recycle(); - for (int i = 0; i < callbacks.size(); ++i) - { - Callback callback = callbacks.get(i); - if (callback != null) - callback.failed(x); - } - } - - protected void recycle() - { - for (int i = 0; i < buffers.size(); ++i) - { - ByteBuffer buffer = buffers.get(i); - if (recycles.get(i)) - byteBufferPool.release(buffer); - } } } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ServerGenerator.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ServerGenerator.java index cd75698f92c1..0efde62a5653 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ServerGenerator.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/ServerGenerator.java @@ -23,9 +23,9 @@ import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; public class ServerGenerator extends Generator { @@ -35,18 +35,18 @@ public class ServerGenerator extends Generator private final boolean sendStatus200; - public ServerGenerator(ByteBufferPool byteBufferPool) + public ServerGenerator(RetainableByteBufferPool bufferPool) { - this(byteBufferPool, true, true); + this(bufferPool, true, true); } - public ServerGenerator(ByteBufferPool byteBufferPool, boolean useDirectByteBuffers, boolean sendStatus200) + public ServerGenerator(RetainableByteBufferPool bufferPool, boolean useDirectByteBuffers, boolean sendStatus200) { - super(byteBufferPool, useDirectByteBuffers); + super(bufferPool, useDirectByteBuffers); this.sendStatus200 = sendStatus200; } - public Result generateResponseHeaders(int request, int code, String reason, HttpFields fields, Callback callback) + public void generateResponseHeaders(RetainableByteBufferPool.Accumulator accumulator, int request, int code, String reason, HttpFields fields) { request &= 0xFF_FF; @@ -83,50 +83,48 @@ public Result generateResponseHeaders(int request, int code, String reason, Http // End of headers length += EOL.length; - ByteBuffer buffer = acquire(length); - BufferUtil.clearToFill(buffer); + ByteBuffer byteBuffer = BufferUtil.allocate(length, isUseDirectByteBuffers()); + BufferUtil.clearToFill(byteBuffer); for (int i = 0; i < bytes.size(); i += 2) { - buffer.put(bytes.get(i)).put(COLON).put(bytes.get(i + 1)).put(EOL); + byteBuffer.put(bytes.get(i)).put(COLON).put(bytes.get(i + 1)).put(EOL); } - buffer.put(EOL); + byteBuffer.put(EOL); - BufferUtil.flipToFlush(buffer, 0); + BufferUtil.flipToFlush(byteBuffer, 0); - return generateContent(request, buffer, true, false, callback, FCGI.FrameType.STDOUT); + generateContent(accumulator, request, byteBuffer, false, FCGI.FrameType.STDOUT); } - public Result generateResponseContent(int request, ByteBuffer content, boolean lastContent, boolean aborted, Callback callback) + public void generateResponseContent(RetainableByteBufferPool.Accumulator accumulator, int request, ByteBuffer content, boolean lastContent, boolean aborted) { if (aborted) { - Result result = new Result(getByteBufferPool(), callback); if (lastContent) - result.append(generateEndRequest(request, true), true); + accumulator.append(generateEndRequest(accumulator, request, true)); else - result.append(BufferUtil.EMPTY_BUFFER, false); - return result; + accumulator.append(RetainableByteBuffer.asNonRetainable(BufferUtil.EMPTY_BUFFER)); } else { - Result result = generateContent(request, content, false, lastContent, callback, FCGI.FrameType.STDOUT); + generateContent(accumulator, request, content, lastContent, FCGI.FrameType.STDOUT); if (lastContent) - result.append(generateEndRequest(request, false), true); - return result; + accumulator.append(generateEndRequest(accumulator, request, false)); } } - private ByteBuffer generateEndRequest(int request, boolean aborted) + private RetainableByteBuffer generateEndRequest(RetainableByteBufferPool.Accumulator accumulator, int request, boolean aborted) { request &= 0xFF_FF; - ByteBuffer endRequestBuffer = acquire(16); - BufferUtil.clearToFill(endRequestBuffer); - endRequestBuffer.putInt(0x01_03_00_00 + request); - endRequestBuffer.putInt(0x00_08_00_00); - endRequestBuffer.putInt(aborted ? 1 : 0); - endRequestBuffer.putInt(0); - BufferUtil.flipToFlush(endRequestBuffer, 0); + RetainableByteBuffer endRequestBuffer = accumulator.acquire(16, isUseDirectByteBuffers()); + ByteBuffer byteBuffer = endRequestBuffer.getByteBuffer(); + BufferUtil.clearToFill(byteBuffer); + byteBuffer.putInt(0x01_03_00_00 + request); + byteBuffer.putInt(0x00_08_00_00); + byteBuffer.putInt(aborted ? 1 : 0); + byteBuffer.putInt(0); + BufferUtil.flipToFlush(byteBuffer, 0); return endRequestBuffer; } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/generator/ClientGeneratorTest.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/generator/ClientGeneratorTest.java index 27a65fd10add..12b772464861 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/generator/ClientGeneratorTest.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/generator/ClientGeneratorTest.java @@ -21,8 +21,8 @@ import org.eclipse.jetty.fcgi.parser.ServerParser; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,50 +31,51 @@ public class ClientGeneratorTest { @Test - public void testGenerateRequestHeaders() throws Exception + public void testGenerateRequestHeaders() { HttpFields.Mutable fields = HttpFields.build(); // Short name, short value final String shortShortName = "REQUEST_METHOD"; - final String shortShortValue = "GET"; + String shortShortValue = "GET"; fields.put(new HttpField(shortShortName, shortShortValue)); // Short name, long value final String shortLongName = "REQUEST_URI"; // Be sure it's longer than 127 chars to test the large value - final String shortLongValue = "/api/0.6/map?bbox=-64.217736,-31.456810,-64.187736,-31.432322,filler=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + String shortLongValue = "/api/0.6/map?bbox=-64.217736,-31.456810,-64.187736,-31.432322,filler=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; fields.put(new HttpField(shortLongName, shortLongValue)); // Long name, short value // Be sure it's longer than 127 chars to test the large name final String longShortName = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210"; - final String longShortValue = "api.openstreetmap.org"; + String longShortValue = "api.openstreetmap.org"; fields.put(new HttpField(longShortName, longShortValue)); // Long name, long value char[] chars = new char[ClientGenerator.MAX_PARAM_LENGTH]; Arrays.fill(chars, 'z'); - final String longLongName = new String(chars); - final String longLongValue = new String(chars); + String longLongName = new String(chars); + String longLongValue = new String(chars); fields.put(new HttpField(longLongName, longLongValue)); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ClientGenerator generator = new ClientGenerator(byteBufferPool); - final int id = 13; - Generator.Result result = generator.generateRequestHeaders(id, fields, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ClientGenerator generator = new ClientGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + int id = 13; + generator.generateRequestHeaders(accumulator, id, fields); // Use the fundamental theorem of arithmetic to test the results. // This way we know onHeader() has been called the right number of // times with the right arguments, and so onHeaders(). - final int[] primes = new int[]{2, 3, 5, 7, 11}; + int[] primes = new int[]{2, 3, 5, 7, 11}; int value = 1; for (int prime : primes) { value *= prime; } - final AtomicInteger params = new AtomicInteger(1); + AtomicInteger params = new AtomicInteger(1); ServerParser parser = new ServerParser(new ServerParser.Listener.Adapter() { @Override @@ -83,23 +84,27 @@ public void onHeader(int request, HttpField field) assertEquals(id, request); switch (field.getName()) { - case shortShortName: + case shortShortName -> + { assertEquals(shortShortValue, field.getValue()); params.set(params.get() * primes[0]); - break; - case shortLongName: + } + case shortLongName -> + { assertEquals(shortLongValue, field.getValue()); params.set(params.get() * primes[1]); - break; - case longShortName: + } + case longShortName -> + { assertEquals(longShortValue, field.getValue()); params.set(params.get() * primes[2]); - break; - default: + } + default -> + { assertEquals(longLongName, field.getName()); assertEquals(longLongValue, field.getValue()); params.set(params.get() * primes[3]); - break; + } } } @@ -112,7 +117,7 @@ public boolean onHeaders(int request) } }); - for (ByteBuffer buffer : result.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); @@ -122,17 +127,18 @@ public boolean onHeaders(int request) // Parse again byte by byte params.set(1); - for (ByteBuffer buffer : result.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { buffer.flip(); while (buffer.hasRemaining()) { parser.parse(ByteBuffer.wrap(new byte[]{buffer.get()})); } - assertFalse(buffer.hasRemaining()); } assertEquals(value, params.get()); + + accumulator.release(); } @Test @@ -147,16 +153,17 @@ public void testGenerateLargeRequestContent() throws Exception testGenerateRequestContent(128 * 1024); } - private void testGenerateRequestContent(final int contentLength) throws Exception + private void testGenerateRequestContent(int contentLength) throws Exception { ByteBuffer content = ByteBuffer.allocate(contentLength); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ClientGenerator generator = new ClientGenerator(byteBufferPool); - final int id = 13; - Generator.Result result = generator.generateRequestContent(id, content, true, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ClientGenerator generator = new ClientGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + int id = 13; + generator.generateRequestContent(accumulator, id, content, true); - final AtomicInteger totalLength = new AtomicInteger(); + AtomicInteger totalLength = new AtomicInteger(); ServerParser parser = new ServerParser(new ServerParser.Listener.Adapter() { @Override @@ -175,21 +182,22 @@ public void onEnd(int request) } }); - for (ByteBuffer buffer : result.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); } // Parse again one byte at a time - for (ByteBuffer buffer : result.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { buffer.flip(); while (buffer.hasRemaining()) { parser.parse(ByteBuffer.wrap(new byte[]{buffer.get()})); } - assertFalse(buffer.hasRemaining()); } + + accumulator.release(); } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/parser/ClientParserTest.java b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/parser/ClientParserTest.java index cd673377953c..093314d4a7e8 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/parser/ClientParserTest.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-client/src/test/java/org/eclipse/jetty/fcgi/parser/ClientParserTest.java @@ -19,12 +19,11 @@ import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.jetty.fcgi.FCGI; -import org.eclipse.jetty.fcgi.generator.Generator; import org.eclipse.jetty.fcgi.generator.ServerGenerator; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,32 +33,33 @@ public class ClientParserTest { @Test - public void testParseResponseHeaders() throws Exception + public void testParseResponseHeaders() { - final int id = 13; + int id = 13; HttpFields.Mutable fields = HttpFields.build(); - final int statusCode = 200; - final String statusMessage = "OK"; - final String contentTypeName = "Content-Type"; - final String contentTypeValue = "text/html;charset=utf-8"; + int statusCode = 200; + String statusMessage = "OK"; + String contentTypeName = "Content-Type"; + String contentTypeValue = "text/html;charset=utf-8"; fields.put(contentTypeName, contentTypeValue); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ServerGenerator generator = new ServerGenerator(byteBufferPool); - Generator.Result result = generator.generateResponseHeaders(id, statusCode, statusMessage, fields, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ServerGenerator generator = new ServerGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateResponseHeaders(accumulator, id, statusCode, statusMessage, fields); // Use the fundamental theorem of arithmetic to test the results. // This way we know onHeader() has been called the right number of // times with the right arguments, and so onHeaders(). - final int[] primes = new int[]{2, 3, 5}; + int[] primes = new int[]{2, 3, 5}; int value = 1; for (int prime : primes) { value *= prime; } - final AtomicInteger params = new AtomicInteger(1); + AtomicInteger params = new AtomicInteger(1); ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() { @Override @@ -74,14 +74,10 @@ public void onBegin(int request, int code, String reason) public void onHeader(int request, HttpField field) { assertEquals(id, request); - switch (field.getName()) + if (field.getName().equals(contentTypeName)) { - case contentTypeName: - assertEquals(contentTypeValue, field.getValue().toLowerCase(Locale.ENGLISH)); - params.set(params.get() * primes[1]); - break; - default: - break; + assertEquals(contentTypeValue, field.getValue().toLowerCase(Locale.ENGLISH)); + params.set(params.get() * primes[1]); } } @@ -94,28 +90,31 @@ public boolean onHeaders(int request) } }); - for (ByteBuffer buffer : result.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); } assertEquals(value, params.get()); + + accumulator.release(); } @Test public void testParseNoResponseContent() throws Exception { - final int id = 13; + int id = 13; HttpFields fields = HttpFields.build() .put("Content-Length", "0"); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ServerGenerator generator = new ServerGenerator(byteBufferPool); - Generator.Result result1 = generator.generateResponseHeaders(id, 200, "OK", fields, null); - Generator.Result result2 = generator.generateResponseContent(id, null, true, false, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ServerGenerator generator = new ServerGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateResponseHeaders(accumulator, id, 200, "OK", fields); + generator.generateResponseContent(accumulator, id, null, true, false); - final AtomicInteger verifier = new AtomicInteger(); + AtomicInteger verifier = new AtomicInteger(); ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() { @Override @@ -134,40 +133,38 @@ public void onEnd(int request) } }); - for (ByteBuffer buffer : result1.getByteBuffers()) - { - parser.parse(buffer); - assertFalse(buffer.hasRemaining()); - } - for (ByteBuffer buffer : result2.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); } assertEquals(3, verifier.get()); + + accumulator.release(); } @Test public void testParseSmallResponseContent() throws Exception { - final int id = 13; + int id = 13; HttpFields.Mutable fields = HttpFields.build(); ByteBuffer content = ByteBuffer.wrap(new byte[1024]); - final int contentLength = content.remaining(); + int contentLength = content.remaining(); - final int code = 200; - final String contentTypeName = "Content-Length"; - final String contentTypeValue = String.valueOf(contentLength); + int code = 200; + String contentTypeName = "Content-Length"; + String contentTypeValue = String.valueOf(contentLength); fields.put(contentTypeName, contentTypeValue); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ServerGenerator generator = new ServerGenerator(byteBufferPool); - Generator.Result result1 = generator.generateResponseHeaders(id, code, "OK", fields, null); - Generator.Result result2 = generator.generateResponseContent(id, content, true, false, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ServerGenerator generator = new ServerGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateResponseHeaders(accumulator, id, code, "OK", fields); + generator.generateResponseContent(accumulator, id, content, true, false); - final AtomicInteger verifier = new AtomicInteger(); + AtomicInteger verifier = new AtomicInteger(); ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() { @Override @@ -187,41 +184,39 @@ public void onEnd(int request) } }); - for (ByteBuffer buffer : result1.getByteBuffers()) - { - parser.parse(buffer); - assertFalse(buffer.hasRemaining()); - } - for (ByteBuffer buffer : result2.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); } assertEquals(5, verifier.get()); + + accumulator.release(); } @Test public void testParseLargeResponseContent() throws Exception { - final int id = 13; + int id = 13; HttpFields.Mutable fields = HttpFields.build(); ByteBuffer content = ByteBuffer.wrap(new byte[128 * 1024]); - final int contentLength = content.remaining(); + int contentLength = content.remaining(); - final int code = 200; - final String contentTypeName = "Content-Length"; - final String contentTypeValue = String.valueOf(contentLength); + int code = 200; + String contentTypeName = "Content-Length"; + String contentTypeValue = String.valueOf(contentLength); fields.put(contentTypeName, contentTypeValue); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ServerGenerator generator = new ServerGenerator(byteBufferPool); - Generator.Result result1 = generator.generateResponseHeaders(id, code, "OK", fields, null); - Generator.Result result2 = generator.generateResponseContent(id, content, true, false, null); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + ServerGenerator generator = new ServerGenerator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateResponseHeaders(accumulator, id, code, "OK", fields); + generator.generateResponseContent(accumulator, id, content, true, false); - final AtomicInteger totalLength = new AtomicInteger(); - final AtomicBoolean verifier = new AtomicBoolean(); + AtomicInteger totalLength = new AtomicInteger(); + AtomicBoolean verifier = new AtomicBoolean(); ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() { @Override @@ -241,17 +236,14 @@ public void onEnd(int request) } }); - for (ByteBuffer buffer : result1.getByteBuffers()) - { - parser.parse(buffer); - assertFalse(buffer.hasRemaining()); - } - for (ByteBuffer buffer : result2.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); } assertTrue(verifier.get()); + + accumulator.release(); } } diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/HttpStreamOverFCGI.java b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/HttpStreamOverFCGI.java index 885d6924c87f..a3fb2073b889 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/HttpStreamOverFCGI.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/HttpStreamOverFCGI.java @@ -18,7 +18,6 @@ import org.eclipse.jetty.fcgi.FCGI; import org.eclipse.jetty.fcgi.generator.Flusher; -import org.eclipse.jetty.fcgi.generator.Generator; import org.eclipse.jetty.fcgi.generator.ServerGenerator; import org.eclipse.jetty.http.HostPortHttpField; import org.eclipse.jetty.http.HttpField; @@ -30,6 +29,7 @@ import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.MetaData; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.HttpStream; import org.eclipse.jetty.util.BufferUtil; @@ -229,8 +229,9 @@ public void send(MetaData.Request request, MetaData.Response response, boolean l { if (last) { - Generator.Result result = generateResponseContent(true, BufferUtil.EMPTY_BUFFER, callback); - flusher.flush(result); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(_generator.getRetainableByteBufferPool()); + generateResponseContent(accumulator, true, BufferUtil.EMPTY_BUFFER); + flusher.flush(accumulator, callback); } else { @@ -240,8 +241,9 @@ public void send(MetaData.Request request, MetaData.Response response, boolean l } else { - Generator.Result result = generateResponseContent(last, content, callback); - flusher.flush(result); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(_generator.getRetainableByteBufferPool()); + generateResponseContent(accumulator, last, content); + flusher.flush(accumulator, callback); } if (last && _shutdown) @@ -258,40 +260,42 @@ private void commit(MetaData.Response info, boolean head, boolean last, ByteBuff boolean shutdown = _shutdown = info.getFields().contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()); + RetainableByteBufferPool bufferPool = _generator.getRetainableByteBufferPool(); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); Flusher flusher = _connection.getFlusher(); if (head) { if (last) { - Generator.Result headersResult = generateResponseHeaders(info, Callback.NOOP); - Generator.Result contentResult = generateResponseContent(true, BufferUtil.EMPTY_BUFFER, callback); - flusher.flush(headersResult, contentResult); + generateResponseHeaders(accumulator, info); + generateResponseContent(accumulator, true, BufferUtil.EMPTY_BUFFER); + flusher.flush(accumulator, callback); } else { - Generator.Result headersResult = generateResponseHeaders(info, callback); - flusher.flush(headersResult); + generateResponseHeaders(accumulator, info); + flusher.flush(accumulator, callback); } } else { - Generator.Result headersResult = generateResponseHeaders(info, Callback.NOOP); - Generator.Result contentResult = generateResponseContent(last, content, callback); - flusher.flush(headersResult, contentResult); + generateResponseHeaders(accumulator, info); + generateResponseContent(accumulator, last, content); + flusher.flush(accumulator, callback); } if (last && shutdown) flusher.shutdown(); } - private Generator.Result generateResponseHeaders(MetaData.Response info, Callback callback) + private void generateResponseHeaders(RetainableByteBufferPool.Accumulator accumulator, MetaData.Response info) { - return _generator.generateResponseHeaders(_id, info.getStatus(), info.getReason(), info.getFields(), callback); + _generator.generateResponseHeaders(accumulator, _id, info.getStatus(), info.getReason(), info.getFields()); } - private Generator.Result generateResponseContent(boolean last, ByteBuffer buffer, Callback callback) + private void generateResponseContent(RetainableByteBufferPool.Accumulator accumulator, boolean last, ByteBuffer buffer) { - return _generator.generateResponseContent(_id, buffer, last, _aborted, callback); + _generator.generateResponseContent(accumulator, _id, buffer, last, _aborted); } @Override diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/ServerFCGIConnection.java b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/ServerFCGIConnection.java index c6bc575bfc08..c1c6fc75eba1 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/ServerFCGIConnection.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/ServerFCGIConnection.java @@ -63,7 +63,7 @@ public ServerFCGIConnection(Connector connector, EndPoint endPoint, HttpConfigur { super(endPoint, connector.getExecutor()); this.connector = connector; - this.networkByteBufferPool = connector.getByteBufferPool().asRetainableByteBufferPool(); + this.networkByteBufferPool = connector.getRetainableByteBufferPool(); this.flusher = new Flusher(endPoint); this.configuration = configuration; this.sendStatus200 = sendStatus200; @@ -212,7 +212,7 @@ public void onFillable() LOG.debug("Read {} bytes from {} {}", read, getEndPoint(), this); if (read > 0) { - if (parse(networkBuffer.getBuffer())) + if (parse(networkBuffer.getByteBuffer())) return; } else if (read == 0) @@ -252,7 +252,7 @@ void parseAndFill() // See also HttpConnection.parseAndFillForContent(). while (stream != null) { - if (parse(networkBuffer.getBuffer())) + if (parse(networkBuffer.getByteBuffer())) return; // Check if the request was completed by the parsing. if (stream == null) @@ -281,7 +281,7 @@ private int fillInputBuffer() { try { - return getEndPoint().fill(networkBuffer.getBuffer()); + return getEndPoint().fill(networkBuffer.getByteBuffer()); } catch (Throwable x) { @@ -333,7 +333,7 @@ public void onStart(int request, FCGI.Role role, int flags) if (stream != null) throw new UnsupportedOperationException("FastCGI Multiplexing"); HttpChannel channel = httpChannelFactory.newHttpChannel(ServerFCGIConnection.this); - ServerGenerator generator = new ServerGenerator(connector.getByteBufferPool(), isUseOutputDirectByteBuffers(), sendStatus200); + ServerGenerator generator = new ServerGenerator(connector.getRetainableByteBufferPool(), isUseOutputDirectByteBuffers(), sendStatus200); stream = new HttpStreamOverFCGI(ServerFCGIConnection.this, generator, channel, request); channel.setHttpStream(stream); if (LOG.isDebugEnabled()) diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java index 6c202372cbb6..d35e81805b55 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java @@ -21,10 +21,9 @@ import org.eclipse.jetty.client.LeakTrackingConnectionPool; import org.eclipse.jetty.fcgi.client.transport.HttpClientTransportOverFCGI; import org.eclipse.jetty.http.HttpScheme; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.ClientConnector; -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.Server; @@ -39,8 +38,8 @@ public abstract class AbstractHttpClientServerTest { - private LeakTrackingByteBufferPool serverBufferPool; - protected ByteBufferPool clientBufferPool; + private RetainableByteBufferPool serverBufferPool; + protected RetainableByteBufferPool clientBufferPool; private final AtomicLong connectionLeaks = new AtomicLong(); protected Server server; protected ServerConnector connector; @@ -53,7 +52,8 @@ public void start(Handler handler) throws Exception serverThreads.setName("server"); server = new Server(serverThreads); ServerFCGIConnectionFactory fcgiConnectionFactory = new ServerFCGIConnectionFactory(new HttpConfiguration()); - serverBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + // TODO: restore leak tracking. + serverBufferPool = new ArrayRetainableByteBufferPool(); connector = new ServerConnector(server, null, null, serverBufferPool, 1, Math.max(1, ProcessorUtils.availableProcessors() / 2), fcgiConnectionFactory); server.addConnector(connector); @@ -65,9 +65,10 @@ public void start(Handler handler) throws Exception QueuedThreadPool clientThreads = new QueuedThreadPool(); clientThreads.setName("client"); clientConnector.setExecutor(clientThreads); + // TODO: restore leak tracking. if (clientBufferPool == null) - clientBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); - clientConnector.setByteBufferPool(clientBufferPool); + clientBufferPool = new ArrayRetainableByteBufferPool(); + clientConnector.setRetainableByteBufferPool(clientBufferPool); HttpClientTransport transport = new HttpClientTransportOverFCGI(clientConnector, ""); transport.setConnectionPoolFactory(destination -> new LeakTrackingConnectionPool(destination, client.getMaxConnectionsPerDestination()) { @@ -86,22 +87,6 @@ public void dispose() throws Exception { System.gc(); - if (serverBufferPool != null) - { - assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), Matchers.is(0L)); - assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), Matchers.is(0L)); - assertThat("Server BufferPool - leaked removes", serverBufferPool.getLeakedRemoves(), Matchers.is(0L)); - assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), Matchers.is(0L)); - } - - if ((clientBufferPool != null) && (clientBufferPool instanceof LeakTrackingByteBufferPool pool)) - { - assertThat("Client BufferPool - leaked acquires", pool.getLeakedAcquires(), Matchers.is(0L)); - assertThat("Client BufferPool - leaked releases", pool.getLeakedReleases(), Matchers.is(0L)); - assertThat("Client BufferPool - leaked removes", pool.getLeakedRemoves(), Matchers.is(0L)); - assertThat("Client BufferPool - unreleased", pool.getLeakedResources(), Matchers.is(0L)); - } - assertThat("Connection Leaks", connectionLeaks.get(), Matchers.is(0L)); if (client != null) diff --git a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/HttpClientTest.java b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/HttpClientTest.java index b33600070d30..119245797720 100644 --- a/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/HttpClientTest.java +++ b/jetty-core/jetty-fcgi/jetty-fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/HttpClientTest.java @@ -36,7 +36,6 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpChannel; @@ -400,11 +399,6 @@ public boolean process(org.eclipse.jetty.server.Request request, org.eclipse.jet @Test public void testGZIPContentEncoding() throws Exception { - // GZIPContentDecoder returns to application pooled - // buffers, which is fine, but in this test they will - // appear as "leaked", so we use a normal ByteBufferPool. - clientBufferPool = new MappedByteBufferPool.Tagged(); - byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; start(new Handler.Abstract() { diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java index 7a84fc3ca5d4..b176ba3bb826 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.http; -import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; @@ -21,7 +20,8 @@ import java.util.zip.Inflater; import java.util.zip.ZipException; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.component.Destroyable; import org.eclipse.jetty.util.compression.InflaterPool; @@ -36,8 +36,8 @@ public class GZIPContentDecoder implements Destroyable // Unsigned Integer Max == 2^32 private static final long UINT_MAX = 0xFFFFFFFFL; - private final List _inflateds = new ArrayList<>(); - private final ByteBufferPool _pool; + private final List _inflateds = new ArrayList<>(); + private final RetainableByteBufferPool _pool; private final int _bufferSize; private InflaterPool.Entry _inflaterEntry; private Inflater _inflater; @@ -45,7 +45,7 @@ public class GZIPContentDecoder implements Destroyable private int _size; private long _value; private byte _flags; - private ByteBuffer _inflated; + private RetainableByteBuffer _inflated; public GZIPContentDecoder() { @@ -57,24 +57,24 @@ public GZIPContentDecoder(int bufferSize) this(null, bufferSize); } - public GZIPContentDecoder(ByteBufferPool pool, int bufferSize) + public GZIPContentDecoder(RetainableByteBufferPool retainableByteBufferPool, int bufferSize) { - this(new InflaterPool(0, true), pool, bufferSize); + this(new InflaterPool(0, true), retainableByteBufferPool, bufferSize); } - public GZIPContentDecoder(InflaterPool inflaterPool, ByteBufferPool pool, int bufferSize) + public GZIPContentDecoder(InflaterPool inflaterPool, RetainableByteBufferPool retainableByteBufferPool, int bufferSize) { _inflaterEntry = inflaterPool.acquire(); _inflater = _inflaterEntry.get(); _bufferSize = bufferSize; - _pool = pool; + _pool = retainableByteBufferPool != null ? retainableByteBufferPool : new RetainableByteBufferPool.NonPooling(); reset(); } /** *

Inflates compressed data from a buffer.

- *

The buffers returned by this method should be released - * via {@link #release(ByteBuffer)}.

+ *

The {@link RetainableByteBuffer} returned by this method + * must be released via {@link RetainableByteBuffer#release()}.

*

This method may fully consume the input buffer, but return * only a chunk of the inflated bytes, to allow applications to * consume the inflated chunk before performing further inflation, @@ -83,21 +83,21 @@ public GZIPContentDecoder(InflaterPool inflaterPool, ByteBufferPool pool, int bu * it's already fully consumed) and that will produce another * chunk of inflated bytes. Termination happens when the input * buffer is fully consumed, and the returned buffer is empty.

- *

See {@link #decodedChunk(ByteBuffer)} to perform inflating + *

See {@link #decodedChunk(RetainableByteBuffer)} to perform inflating * in a non-blocking way that allows to apply backpressure.

* * @param compressed the buffer containing compressed data. * @return a buffer containing inflated data. */ - public ByteBuffer decode(ByteBuffer compressed) + public RetainableByteBuffer decode(ByteBuffer compressed) { decodeChunks(compressed); if (_inflateds.isEmpty()) { - if (BufferUtil.isEmpty(_inflated) || _state == State.CRC || _state == State.ISIZE) - return BufferUtil.EMPTY_BUFFER; - ByteBuffer result = _inflated; + if ((_inflated == null || !_inflated.hasRemaining()) || _state == State.CRC || _state == State.ISIZE) + return acquire(0); + RetainableByteBuffer result = _inflated; _inflated = null; return result; } @@ -105,12 +105,12 @@ public ByteBuffer decode(ByteBuffer compressed) { _inflateds.add(_inflated); _inflated = null; - int length = _inflateds.stream().mapToInt(Buffer::remaining).sum(); - ByteBuffer result = acquire(length); - for (ByteBuffer buffer : _inflateds) + int length = _inflateds.stream().mapToInt(RetainableByteBuffer::remaining).sum(); + RetainableByteBuffer result = acquire(length); + for (RetainableByteBuffer buffer : _inflateds) { - BufferUtil.append(result, buffer); - release(buffer); + BufferUtil.append(result.getByteBuffer(), buffer.getByteBuffer()); + buffer.release(); } _inflateds.clear(); return result; @@ -132,38 +132,26 @@ public ByteBuffer decode(ByteBuffer compressed) * should return, allowing to consume the inflated chunk and apply * backpressure */ - protected boolean decodedChunk(ByteBuffer chunk) + protected boolean decodedChunk(RetainableByteBuffer chunk) { - if (_inflated == null) - { - _inflated = chunk; - } - else - { - if (BufferUtil.space(_inflated) >= chunk.remaining()) - { - BufferUtil.append(_inflated, chunk); - release(chunk); - } - else - { - _inflateds.add(_inflated); - _inflated = chunk; - } - } + // Retain the chunk because it is stored for later use. + chunk.retain(); + if (_inflated != null) + _inflateds.add(_inflated); + _inflated = chunk; return false; } /** *

Inflates compressed data.

*

Inflation continues until the compressed block end is reached, there is no - * more compressed data or a call to {@link #decodedChunk(ByteBuffer)} returns true.

+ * more compressed data or a call to {@link #decodedChunk(RetainableByteBuffer)} returns true.

* * @param compressed the buffer of compressed data to inflate */ protected void decodeChunks(ByteBuffer compressed) { - ByteBuffer buffer = null; + RetainableByteBuffer buffer = null; try { while (true) @@ -211,9 +199,10 @@ else if ((_flags & 0x2) == 0x2) try { - int pos = BufferUtil.flipToFill(buffer); - _inflater.inflate(buffer); - BufferUtil.flipToFlush(buffer, pos); + ByteBuffer decoded = buffer.getByteBuffer(); + int pos = BufferUtil.flipToFill(decoded); + _inflater.inflate(decoded); + BufferUtil.flipToFlush(decoded, pos); } catch (DataFormatException x) { @@ -222,9 +211,10 @@ else if ((_flags & 0x2) == 0x2) if (buffer.hasRemaining()) { - ByteBuffer decoded = buffer; + boolean stop = decodedChunk(buffer); + buffer.release(); buffer = null; - if (decodedChunk(decoded)) + if (stop) return; } else if (_inflater.needsInput()) @@ -395,7 +385,7 @@ else if (_inflater.finished()) finally { if (buffer != null) - release(buffer); + buffer.release(); } } @@ -430,23 +420,8 @@ private enum State * @param capacity capacity of the ByteBuffer to acquire * @return a heap buffer of the configured capacity either from the pool or freshly allocated. */ - public ByteBuffer acquire(int capacity) - { - return _pool == null ? BufferUtil.allocate(capacity) : _pool.acquire(capacity, false); - } - - /** - *

Releases an allocated buffer.

- *

This method calls {@link ByteBufferPool#release(ByteBuffer)} if a buffer pool has - * been configured.

- *

This method should be called once for all buffers returned from {@link #decode(ByteBuffer)} - * or passed to {@link #decodedChunk(ByteBuffer)}.

- * - * @param buffer the buffer to release. - */ - public void release(ByteBuffer buffer) + public RetainableByteBuffer acquire(int capacity) { - if (_pool != null && !BufferUtil.isTheEmptyBuffer(buffer)) - _pool.release(buffer); + return _pool.acquire(capacity, false); } } diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/CachingHttpContentFactory.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/CachingHttpContentFactory.java index cce551591d80..91f66c1f413a 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/CachingHttpContentFactory.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/CachingHttpContentFactory.java @@ -30,9 +30,9 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.PreEncodedHttpField; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.io.Retainable; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.NanoTime; import org.eclipse.jetty.util.StringUtil; @@ -69,16 +69,16 @@ public class CachingHttpContentFactory implements HttpContent.Factory private final HttpContent.Factory _authority; private final ConcurrentHashMap _cache = new ConcurrentHashMap<>(); private final AtomicLong _cachedSize = new AtomicLong(); - private final ByteBufferPool _byteBufferPool; + private final RetainableByteBufferPool _bufferPool; private int _maxCachedFileSize = DEFAULT_MAX_CACHED_FILE_SIZE; private int _maxCachedFiles = DEFAULT_MAX_CACHED_FILES; private long _maxCacheSize = DEFAULT_MAX_CACHE_SIZE; private boolean _useDirectByteBuffers = true; - public CachingHttpContentFactory(HttpContent.Factory authority, ByteBufferPool byteBufferPool) + public CachingHttpContentFactory(HttpContent.Factory authority, RetainableByteBufferPool bufferPool) { _authority = authority; - _byteBufferPool = (byteBufferPool == null) ? new NoopByteBufferPool() : byteBufferPool; + _bufferPool = bufferPool != null ? bufferPool : new RetainableByteBufferPool.NonPooling(); } protected ConcurrentMap getCache() @@ -294,7 +294,7 @@ protected interface CachingHttpContent extends HttpContent protected class CachedHttpContent extends HttpContent.Wrapper implements CachingHttpContent { - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private final String _cacheKey; private final HttpField _etagField; private final long _contentLengthValue; @@ -327,21 +327,23 @@ public CachedHttpContent(String key, HttpContent httpContent) boolean isValid = true; // Read the content into memory if the HttpContent does not already have a buffer. + RetainableByteBuffer buffer; ByteBuffer byteBuffer = httpContent.getByteBuffer(); if (byteBuffer == null) { + buffer = _bufferPool.acquire((int)_contentLengthValue, _useDirectByteBuffers); try { if (_contentLengthValue <= _maxCachedFileSize) { - byteBuffer = _byteBufferPool.acquire((int)_contentLengthValue, _useDirectByteBuffers); try (ReadableByteChannel readableByteChannel = httpContent.getResource().newReadableByteChannel()) { + byteBuffer = buffer.getByteBuffer(); int read = BufferUtil.readFrom(readableByteChannel, byteBuffer); if (read != _contentLengthValue) { - _byteBufferPool.release(byteBuffer); - byteBuffer = null; + buffer.release(); + buffer = null; isValid = false; } } @@ -349,17 +351,19 @@ public CachedHttpContent(String key, HttpContent httpContent) } catch (Throwable t) { + if (buffer != null) + buffer.release(); + buffer = null; isValid = false; - if (byteBuffer != null) - { - _byteBufferPool.release(byteBuffer); - byteBuffer = null; - } LOG.warn("Failed to read Resource", t); } } + else + { + buffer = RetainableByteBuffer.asNonRetainable(byteBuffer); + } - _buffer = byteBuffer; + _buffer = buffer; _isValid = isValid; _bytesOccupied = httpContent.getBytesOccupied(); _lastModifiedValue = httpContent.getLastModifiedValue(); @@ -381,7 +385,7 @@ public long getContentLengthValue() @Override public ByteBuffer getByteBuffer() { - return _buffer == null ? null : _buffer.asReadOnlyBuffer(); + return _buffer == null ? null : _buffer.getByteBuffer().asReadOnlyBuffer(); } @Override @@ -420,7 +424,7 @@ public void release() if (_referenceCount.release()) { if (_buffer != null) - _byteBufferPool.release(_buffer); + _buffer.release(); super.release(); } } diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/ValidatingCachingHttpContentFactory.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/ValidatingCachingHttpContentFactory.java index 4581b0d4144e..b3dea606cf70 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/ValidatingCachingHttpContentFactory.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/ValidatingCachingHttpContentFactory.java @@ -19,7 +19,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.NanoTime; import org.eclipse.jetty.util.annotation.Name; import org.eclipse.jetty.util.thread.Scheduler; @@ -55,13 +55,13 @@ public class ValidatingCachingHttpContentFactory extends CachingHttpContentFacto * * @param authority the wrapped {@link HttpContent.Factory} to use. * @param validationPeriod time between filesystem checks in ms to see if an {@link HttpContent} is still valid (-1 never validate, 0 always validate). - * @param byteBufferPool the {@link org.eclipse.jetty.io.ByteBufferPool} to use. + * @param bufferPool the {@link org.eclipse.jetty.io.RetainableByteBufferPool} to use. */ public ValidatingCachingHttpContentFactory(@Name("authority") HttpContent.Factory authority, @Name("validationPeriod") long validationPeriod, - @Name("byteBufferPool") ByteBufferPool byteBufferPool) + @Name("bufferPool") RetainableByteBufferPool bufferPool) { - this(authority, validationPeriod, byteBufferPool, null, -1, -1); + this(authority, validationPeriod, bufferPool, null, -1, -1); } /** @@ -70,19 +70,19 @@ public ValidatingCachingHttpContentFactory(@Name("authority") HttpContent.Factor * * @param authority the wrapped {@link HttpContent.Factory} to use. * @param validationPeriod time between filesystem checks in ms to see if an {@link HttpContent} is still valid (-1 never validate, 0 always validate). - * @param byteBufferPool the {@link org.eclipse.jetty.io.ByteBufferPool} to use. + * @param bufferPool the {@link org.eclipse.jetty.io.RetainableByteBufferPool} to use. * @param scheduler scheduler to use for the sweeper, can be null to not use sweeper. * @param sweepPeriod time between runs of the sweeper in ms (if 0 never sweep for invalid entries). * @param idleTimeout amount of time in ms an entry can be unused before evicted by the sweeper (if 0 never evict unused entries). */ public ValidatingCachingHttpContentFactory(@Name("authority") HttpContent.Factory authority, @Name("validationPeriod") long validationPeriod, - @Name("byteBufferPool") ByteBufferPool byteBufferPool, + @Name("byteBufferPool") RetainableByteBufferPool bufferPool, @Name("scheduler") Scheduler scheduler, @Name("sweepPeriod") long sweepPeriod, @Name("idleTimeout") long idleTimeout) { - super(authority, byteBufferPool); + super(authority, bufferPool); _validationTime = validationPeriod; _scheduler = scheduler; _sweepDelay = sweepPeriod; diff --git a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/GZIPContentDecoderTest.java b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/GZIPContentDecoderTest.java index b0f2916f43cc..75e4161d4232 100644 --- a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/GZIPContentDecoderTest.java +++ b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/GZIPContentDecoderTest.java @@ -23,7 +23,9 @@ import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -import org.eclipse.jetty.io.ArrayByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,28 +41,29 @@ public class GZIPContentDecoderTest { - private ArrayByteBufferPool pool; - private AtomicInteger buffers = new AtomicInteger(0); + private final AtomicInteger counter = new AtomicInteger(); + private RetainableByteBufferPool pool; @BeforeEach public void before() { - buffers.set(0); - pool = new ArrayByteBufferPool() + pool = new RetainableByteBufferPool.Wrapper(new ArrayRetainableByteBufferPool()) { - - @Override - public ByteBuffer acquire(int size, boolean direct) - { - buffers.incrementAndGet(); - return super.acquire(size, direct); - } - @Override - public void release(ByteBuffer buffer) + public RetainableByteBuffer acquire(int size, boolean direct) { - buffers.decrementAndGet(); - super.release(buffer); + counter.incrementAndGet(); + return new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + counter.decrementAndGet(); + return released; + } + }; } }; } @@ -68,7 +71,7 @@ public void release(ByteBuffer buffer) @AfterEach public void after() { - assertEquals(0, buffers.get()); + assertThat(counter.get(), is(0)); } @Test @@ -126,8 +129,9 @@ public void testNoBlocks() throws Exception byte[] bytes = baos.toByteArray(); GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); assertEquals(0, decoded.remaining()); + decoded.release(); } @Test @@ -142,9 +146,9 @@ public void testSmallBlock() throws Exception byte[] bytes = baos.toByteArray(); GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); - assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); - decoder.release(decoded); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); + decoded.release(); } @Test @@ -165,11 +169,12 @@ public void testSmallBlockWithGZIPChunkedAtBegin() throws Exception System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length); GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); - assertEquals(0, decoded.capacity()); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); + assertEquals(0, decoded.remaining()); + decoded.release(); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); - decoder.release(decoded); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); + decoded.release(); } @Test @@ -190,14 +195,14 @@ public void testSmallBlockWithGZIPChunkedAtEnd() throws Exception System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length); GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); - assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); assertFalse(decoder.isFinished()); - decoder.release(decoded); + decoded.release(); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); assertEquals(0, decoded.remaining()); assertTrue(decoder.isFinished()); - decoder.release(decoded); + decoded.release(); } @Test @@ -218,12 +223,12 @@ public void testSmallBlockWithGZIPTrailerChunked() throws Exception System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length); GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); - assertEquals(0, decoded.capacity()); - decoder.release(decoded); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); + assertEquals(0, decoded.remaining()); + decoded.release(); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); - decoder.release(decoded); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); + decoded.release(); } @Test @@ -249,16 +254,16 @@ public void testTwoSmallBlocks() throws Exception GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048); ByteBuffer buffer = ByteBuffer.wrap(bytes); - ByteBuffer decoded = decoder.decode(buffer); - assertEquals(data1, StandardCharsets.UTF_8.decode(decoded).toString()); + RetainableByteBuffer decoded = decoder.decode(buffer); + assertEquals(data1, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); assertTrue(decoder.isFinished()); assertTrue(buffer.hasRemaining()); - decoder.release(decoded); + decoded.release(); decoded = decoder.decode(buffer); - assertEquals(data2, StandardCharsets.UTF_8.decode(decoded).toString()); + assertEquals(data2, StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString()); assertTrue(decoder.isFinished()); assertFalse(buffer.hasRemaining()); - decoder.release(decoded); + decoded.release(); } @Test @@ -280,9 +285,9 @@ public void testBigBlock() throws Exception ByteBuffer buffer = ByteBuffer.wrap(bytes); while (buffer.hasRemaining()) { - ByteBuffer decoded = decoder.decode(buffer); - result += StandardCharsets.UTF_8.decode(decoded).toString(); - decoder.release(decoded); + RetainableByteBuffer decoded = decoder.decode(buffer); + result += StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString(); + decoded.release(); } assertEquals(data, result); } @@ -306,10 +311,10 @@ public void testBigBlockOneByteAtATime() throws Exception ByteBuffer buffer = ByteBuffer.wrap(bytes); while (buffer.hasRemaining()) { - ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[]{buffer.get()})); + RetainableByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[]{buffer.get()})); if (decoded.hasRemaining()) - result += StandardCharsets.UTF_8.decode(decoded).toString(); - decoder.release(decoded); + result += StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString(); + decoded.release(); } assertEquals(data, result); assertTrue(decoder.isFinished()); @@ -341,10 +346,10 @@ public void testBigBlockWithExtraBytes() throws Exception ByteBuffer buffer = ByteBuffer.wrap(bytes); while (buffer.hasRemaining()) { - ByteBuffer decoded = decoder.decode(buffer); + RetainableByteBuffer decoded = decoder.decode(buffer); if (decoded.hasRemaining()) - result += StandardCharsets.UTF_8.decode(decoded).toString(); - decoder.release(decoded); + result += StandardCharsets.UTF_8.decode(decoded.getByteBuffer()).toString(); + decoded.release(); if (decoder.isFinished()) break; } @@ -414,12 +419,12 @@ public void write(byte[] b, int off, int len) throws IOException ByteBuffer buf = ByteBuffer.wrap(b, off, len); while (buf.hasRemaining()) { - ByteBuffer decoded = decoder.decode(buf); + RetainableByteBuffer decoded = decoder.decode(buf); if (decoded.hasRemaining()) { decodedByteCount += decoded.remaining(); } - decoder.release(decoded); + decoded.release(); } } diff --git a/jetty-core/jetty-http2/jetty-http2-client-transport/src/main/java/org/eclipse/jetty/http2/client/transport/HttpClientTransportOverHTTP2.java b/jetty-core/jetty-http2/jetty-http2-client-transport/src/main/java/org/eclipse/jetty/http2/client/transport/HttpClientTransportOverHTTP2.java index 7bd521271650..23fd9c2cfa94 100644 --- a/jetty-core/jetty-http2/jetty-http2-client-transport/src/main/java/org/eclipse/jetty/http2/client/transport/HttpClientTransportOverHTTP2.java +++ b/jetty-core/jetty-http2/jetty-http2-client-transport/src/main/java/org/eclipse/jetty/http2/client/transport/HttpClientTransportOverHTTP2.java @@ -90,7 +90,7 @@ protected void doStart() throws Exception HttpClient httpClient = getHttpClient(); client.setExecutor(httpClient.getExecutor()); client.setScheduler(httpClient.getScheduler()); - client.setByteBufferPool(httpClient.getByteBufferPool()); + client.setRetainableByteBufferPool(httpClient.getRetainableByteBufferPool()); client.setConnectTimeout(httpClient.getConnectTimeout()); client.setIdleTimeout(httpClient.getIdleTimeout()); client.setInputBufferSize(httpClient.getResponseBufferSize()); diff --git a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java index 368d3009d600..2d488a23e8c9 100644 --- a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java +++ b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java @@ -28,9 +28,9 @@ import org.eclipse.jetty.http2.api.Session; import org.eclipse.jetty.http2.frames.Frame; import org.eclipse.jetty.http2.frames.SettingsFrame; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnector; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.ssl.SslClientConnectionFactory; import org.eclipse.jetty.util.Promise; import org.eclipse.jetty.util.annotation.ManagedAttribute; @@ -153,14 +153,14 @@ public void setScheduler(Scheduler scheduler) connector.setScheduler(scheduler); } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return connector.getByteBufferPool(); + return connector.getRetainableByteBufferPool(); } - public void setByteBufferPool(ByteBufferPool bufferPool) + public void setRetainableByteBufferPool(RetainableByteBufferPool bufferPool) { - connector.setByteBufferPool(bufferPool); + connector.setRetainableByteBufferPool(bufferPool); } public FlowControlStrategy.Factory getFlowControlStrategyFactory() @@ -435,7 +435,7 @@ private ClientConnectionFactory newClientConnectionFactory(SslContextFactory ssl { if (isUseALPN()) factory = new ALPNClientConnectionFactory(getExecutor(), factory, getProtocols()); - factory = new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), factory); + factory = new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), factory); } return factory; } diff --git a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2ClientConnectionFactory.java b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2ClientConnectionFactory.java index 3d665b1575f8..6937d0db6e42 100644 --- a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2ClientConnectionFactory.java +++ b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2ClientConnectionFactory.java @@ -28,7 +28,6 @@ import org.eclipse.jetty.http2.internal.HTTP2Session; import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; @@ -49,14 +48,14 @@ public class HTTP2ClientConnectionFactory implements ClientConnectionFactory public Connection newConnection(EndPoint endPoint, Map context) { HTTP2Client client = (HTTP2Client)context.get(CLIENT_CONTEXT_KEY); - ByteBufferPool byteBufferPool = client.getByteBufferPool(); + RetainableByteBufferPool bufferPool = client.getRetainableByteBufferPool(); Executor executor = client.getExecutor(); Scheduler scheduler = client.getScheduler(); Session.Listener listener = (Session.Listener)context.get(SESSION_LISTENER_CONTEXT_KEY); @SuppressWarnings("unchecked") Promise promise = (Promise)context.get(SESSION_PROMISE_CONTEXT_KEY); - Generator generator = new Generator(byteBufferPool, client.getMaxDynamicTableSize(), client.getMaxHeaderBlockFragment()); + Generator generator = new Generator(bufferPool, client.getMaxDynamicTableSize(), client.getMaxHeaderBlockFragment()); FlowControlStrategy flowControl = client.getFlowControlStrategyFactory().newFlowControlStrategy(); HTTP2ClientSession session = new HTTP2ClientSession(scheduler, endPoint, generator, listener, flowControl); session.setMaxRemoteStreams(client.getMaxConcurrentPushedStreams()); @@ -64,13 +63,11 @@ public Connection newConnection(EndPoint endPoint, Map context) if (streamIdleTimeout > 0) session.setStreamIdleTimeout(streamIdleTimeout); - Parser parser = new Parser(byteBufferPool, session, 4096, 8192); + Parser parser = new Parser(bufferPool, session, 4096, 8192); parser.setMaxFrameLength(client.getMaxFrameLength()); parser.setMaxSettingsKeys(client.getMaxSettingsKeys()); - RetainableByteBufferPool retainableByteBufferPool = byteBufferPool.asRetainableByteBufferPool(); - - HTTP2ClientConnection connection = new HTTP2ClientConnection(client, retainableByteBufferPool, executor, endPoint, + HTTP2ClientConnection connection = new HTTP2ClientConnection(client, bufferPool, executor, endPoint, parser, session, client.getInputBufferSize(), promise, listener); connection.setUseInputDirectByteBuffers(client.isUseInputDirectByteBuffers()); connection.setUseOutputDirectByteBuffers(client.isUseOutputDirectByteBuffers()); diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Connection.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Connection.java index bf7ee9e5b0eb..7199617f238b 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Connection.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Connection.java @@ -455,7 +455,7 @@ private NetworkBuffer() public ByteBuffer getBuffer() { - return delegate.getBuffer(); + return delegate.getByteBuffer(); } public boolean isRetained() @@ -494,7 +494,7 @@ public boolean release() private void put(ByteBuffer source) { - BufferUtil.append(delegate.getBuffer(), source); + BufferUtil.append(delegate.getByteBuffer(), source); } } } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Flusher.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Flusher.java index dc960759ca48..44aa5f2c043a 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Flusher.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Flusher.java @@ -30,8 +30,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.WindowUpdateFrame; import org.eclipse.jetty.http2.hpack.HpackException; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EofException; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IteratingCallback; import org.eclipse.jetty.util.component.Dumpable; @@ -51,7 +51,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable private final Queue pendingEntries = new ArrayDeque<>(); private final Collection processedEntries = new ArrayList<>(); private final HTTP2Session session; - private final ByteBufferPool.Lease lease; + private final RetainableByteBufferPool.Accumulator accumulator; private InvocationType invocationType = InvocationType.NON_BLOCKING; private Throwable terminated; private Entry stalledEntry; @@ -59,7 +59,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable public HTTP2Flusher(HTTP2Session session) { this.session = session; - this.lease = new ByteBufferPool.Lease(session.getGenerator().getByteBufferPool()); + this.accumulator = new RetainableByteBufferPool.Accumulator(session.getGenerator().getRetainableByteBufferPool()); } @Override @@ -213,7 +213,7 @@ protected Action process() throws Throwable try { - if (entry.generate(lease)) + if (entry.generate(accumulator)) { if (LOG.isDebugEnabled()) LOG.debug("Generated {} frame bytes for {}", entry.getFrameBytesGenerated(), entry); @@ -266,7 +266,7 @@ protected Action process() throws Throwable break; int writeThreshold = session.getWriteThreshold(); - if (lease.getTotalLength() >= writeThreshold) + if (accumulator.getTotalLength() >= writeThreshold) { if (LOG.isDebugEnabled()) LOG.debug("Write threshold {} exceeded", writeThreshold); @@ -274,7 +274,7 @@ protected Action process() throws Throwable } } - List byteBuffers = lease.getByteBuffers(); + List byteBuffers = accumulator.getByteBuffers(); if (byteBuffers.isEmpty()) { finish(); @@ -284,7 +284,7 @@ protected Action process() throws Throwable if (LOG.isDebugEnabled()) LOG.debug("Writing {} buffers ({} bytes) - entries processed/pending {}/{}: {}/{}", byteBuffers.size(), - lease.getTotalLength(), + accumulator.getTotalLength(), processedEntries.size(), pendingEntries.size(), processedEntries, @@ -308,7 +308,7 @@ public void succeeded() { if (LOG.isDebugEnabled()) LOG.debug("Written {} buffers - entries processed/pending {}/{}: {}/{}", - lease.getByteBuffers().size(), + accumulator.getByteBuffers().size(), processedEntries.size(), pendingEntries.size(), processedEntries, @@ -319,7 +319,7 @@ public void succeeded() private void finish() { - lease.recycle(); + accumulator.release(); processedEntries.forEach(Entry::succeeded); processedEntries.clear(); @@ -349,7 +349,7 @@ protected void onCompleteSuccess() @Override protected void onCompleteFailure(Throwable x) { - lease.recycle(); + accumulator.release(); Throwable closed; Set allEntries; @@ -440,7 +440,7 @@ public int getDataBytesRemaining() return 0; } - protected abstract boolean generate(ByteBufferPool.Lease lease) throws HpackException; + protected abstract boolean generate(RetainableByteBufferPool.Accumulator accumulator) throws HpackException; public abstract long onFlushed(long bytes) throws IOException; diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Session.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Session.java index 9f21c12c23f8..27c5079cdd82 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Session.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/HTTP2Session.java @@ -57,9 +57,9 @@ import org.eclipse.jetty.http2.hpack.HpackException; import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.CyclicTimeouts; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.WriteFlusher; import org.eclipse.jetty.util.AtomicBiInteger; import org.eclipse.jetty.util.Atomics; @@ -1206,9 +1206,9 @@ public int getFrameBytesGenerated() } @Override - protected boolean generate(ByteBufferPool.Lease lease) throws HpackException + protected boolean generate(RetainableByteBufferPool.Accumulator accumulator) throws HpackException { - frameBytes = generator.control(lease, frame); + frameBytes = generator.control(accumulator, frame); beforeSend(); return true; } @@ -1320,7 +1320,7 @@ public int getDataBytesRemaining() } @Override - protected boolean generate(ByteBufferPool.Lease lease) + protected boolean generate(RetainableByteBufferPool.Accumulator accumulator) { int dataRemaining = getDataBytesRemaining(); @@ -1334,7 +1334,7 @@ protected boolean generate(ByteBufferPool.Lease lease) // Only one DATA frame is generated. DataFrame dataFrame = (DataFrame)frame; - int frameBytes = generator.data(lease, dataFrame, length); + int frameBytes = generator.data(accumulator, dataFrame, length); this.frameBytes += frameBytes; this.frameRemaining += frameBytes; diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/DataGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/DataGenerator.java index 5de967599a50..23d5ceb68f51 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/DataGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/DataGenerator.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http2.frames.Frame; import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class DataGenerator @@ -31,12 +32,12 @@ public DataGenerator(HeaderGenerator headerGenerator) this.headerGenerator = headerGenerator; } - public int generate(ByteBufferPool.Lease lease, DataFrame frame, int maxLength) + public int generate(RetainableByteBufferPool.Accumulator accumulator, DataFrame frame, int maxLength) { - return generateData(lease, frame.getStreamId(), frame.getData(), frame.isEndStream(), maxLength); + return generateData(accumulator, frame.getStreamId(), frame.getData(), frame.isEndStream(), maxLength); } - public int generateData(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last, int maxLength) + public int generateData(RetainableByteBufferPool.Accumulator accumulator, int streamId, ByteBuffer data, boolean last, int maxLength) { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); @@ -46,7 +47,7 @@ public int generateData(ByteBufferPool.Lease lease, int streamId, ByteBuffer dat int length = Math.min(dataLength, Math.min(maxFrameSize, maxLength)); if (length == dataLength) { - generateFrame(lease, streamId, data, last); + generateFrame(accumulator, streamId, data, last); } else { @@ -56,12 +57,12 @@ public int generateData(ByteBufferPool.Lease lease, int streamId, ByteBuffer dat ByteBuffer slice = data.slice(); data.position(newLimit); data.limit(limit); - generateFrame(lease, streamId, slice, false); + generateFrame(accumulator, streamId, slice, false); } return Frame.HEADER_LENGTH + length; } - private void generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last) + private void generateFrame(RetainableByteBufferPool.Accumulator accumulator, int streamId, ByteBuffer data, boolean last) { int length = data.remaining(); @@ -69,11 +70,11 @@ private void generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer if (last) flags |= Flags.END_STREAM; - ByteBuffer header = headerGenerator.generate(lease, FrameType.DATA, Frame.HEADER_LENGTH + length, length, flags, streamId); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + RetainableByteBuffer header = headerGenerator.generate(accumulator, FrameType.DATA, Frame.HEADER_LENGTH + length, length, flags, streamId); + BufferUtil.flipToFlush(header.getByteBuffer(), 0); + accumulator.append(header); // Skip empty data buffers. if (data.remaining() > 0) - lease.append(data, false); + accumulator.append(RetainableByteBuffer.asNonRetainable(data)); } } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/FrameGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/FrameGenerator.java index 0c0b6bfe4b16..a8f9586a42bf 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/FrameGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/FrameGenerator.java @@ -13,14 +13,13 @@ package org.eclipse.jetty.http2.internal.generator; -import java.nio.ByteBuffer; - import org.eclipse.jetty.http.MetaData; import org.eclipse.jetty.http2.frames.Frame; import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.hpack.HpackException; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public abstract class FrameGenerator { @@ -31,11 +30,11 @@ protected FrameGenerator(HeaderGenerator headerGenerator) this.headerGenerator = headerGenerator; } - public abstract int generate(ByteBufferPool.Lease lease, Frame frame) throws HpackException; + public abstract int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) throws HpackException; - protected ByteBuffer generateHeader(ByteBufferPool.Lease lease, FrameType frameType, int length, int flags, int streamId) + protected RetainableByteBuffer generateHeader(RetainableByteBufferPool.Accumulator accumulator, FrameType frameType, int length, int flags, int streamId) { - return headerGenerator.generate(lease, frameType, Frame.HEADER_LENGTH + length, length, flags, streamId); + return headerGenerator.generate(accumulator, frameType, Frame.HEADER_LENGTH + length, length, flags, streamId); } public int getMaxFrameSize() @@ -48,17 +47,17 @@ public boolean isUseDirectByteBuffers() return headerGenerator.isUseDirectByteBuffers(); } - protected ByteBuffer encode(HpackEncoder encoder, ByteBufferPool.Lease lease, MetaData metaData, int maxFrameSize) throws HpackException + protected RetainableByteBuffer encode(HpackEncoder encoder, RetainableByteBufferPool.Accumulator accumulator, MetaData metaData, int maxFrameSize) throws HpackException { - ByteBuffer hpacked = lease.acquire(maxFrameSize, isUseDirectByteBuffers()); + RetainableByteBuffer hpacked = accumulator.acquire(maxFrameSize, isUseDirectByteBuffers()); try { - encoder.encode(hpacked, metaData); + encoder.encode(hpacked.getByteBuffer(), metaData); return hpacked; } catch (HpackException x) { - lease.release(hpacked); + hpacked.release(); throw x; } } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/Generator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/Generator.java index aa7d39281259..63525a0e3400 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/Generator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/Generator.java @@ -18,29 +18,29 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.hpack.HpackException; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class Generator { - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final HeaderGenerator headerGenerator; private final HpackEncoder hpackEncoder; private final FrameGenerator[] generators; private final DataGenerator dataGenerator; - public Generator(ByteBufferPool byteBufferPool) + public Generator(RetainableByteBufferPool bufferPool) { - this(byteBufferPool, 4096, 0); + this(bufferPool, 4096, 0); } - public Generator(ByteBufferPool byteBufferPool, int maxDynamicTableSize, int maxHeaderBlockFragment) + public Generator(RetainableByteBufferPool bufferPool, int maxDynamicTableSize, int maxHeaderBlockFragment) { - this(byteBufferPool, true, maxDynamicTableSize, maxHeaderBlockFragment); + this(bufferPool, true, maxDynamicTableSize, maxHeaderBlockFragment); } - public Generator(ByteBufferPool byteBufferPool, boolean useDirectByteBuffers, int maxDynamicTableSize, int maxHeaderBlockFragment) + public Generator(RetainableByteBufferPool bufferPool, boolean useDirectByteBuffers, int maxDynamicTableSize, int maxHeaderBlockFragment) { - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; headerGenerator = new HeaderGenerator(useDirectByteBuffers); hpackEncoder = new HpackEncoder(maxDynamicTableSize); @@ -61,9 +61,9 @@ public Generator(ByteBufferPool byteBufferPool, boolean useDirectByteBuffers, in this.dataGenerator = new DataGenerator(headerGenerator); } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return bufferPool; } public void setValidateHpackEncoding(boolean validateEncoding) @@ -81,14 +81,14 @@ public void setMaxFrameSize(int maxFrameSize) headerGenerator.setMaxFrameSize(maxFrameSize); } - public int control(ByteBufferPool.Lease lease, Frame frame) throws HpackException + public int control(RetainableByteBufferPool.Accumulator accumulator, Frame frame) throws HpackException { - return generators[frame.getType().getType()].generate(lease, frame); + return generators[frame.getType().getType()].generate(accumulator, frame); } - public int data(ByteBufferPool.Lease lease, DataFrame frame, int maxLength) + public int data(RetainableByteBufferPool.Accumulator accumulator, DataFrame frame, int maxLength) { - return dataGenerator.generate(lease, frame, maxLength); + return dataGenerator.generate(accumulator, frame, maxLength); } public void setMaxHeaderListSize(int value) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/GoAwayGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/GoAwayGenerator.java index 8468d29db22b..d1ed268b43b4 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/GoAwayGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/GoAwayGenerator.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.GoAwayFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class GoAwayGenerator extends FrameGenerator @@ -31,13 +32,13 @@ public GoAwayGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { GoAwayFrame goAwayFrame = (GoAwayFrame)frame; - return generateGoAway(lease, goAwayFrame.getLastStreamId(), goAwayFrame.getError(), goAwayFrame.getPayload()); + return generateGoAway(accumulator, goAwayFrame.getLastStreamId(), goAwayFrame.getError(), goAwayFrame.getPayload()); } - public int generateGoAway(ByteBufferPool.Lease lease, int lastStreamId, int error, byte[] payload) + public int generateGoAway(RetainableByteBufferPool.Accumulator accumulator, int lastStreamId, int error, byte[] payload) { if (lastStreamId < 0) lastStreamId = 0; @@ -51,16 +52,17 @@ public int generateGoAway(ByteBufferPool.Lease lease, int lastStreamId, int erro payload = Arrays.copyOfRange(payload, 0, maxPayloadLength); int length = fixedLength + (payload != null ? payload.length : 0); - ByteBuffer header = generateHeader(lease, FrameType.GO_AWAY, length, Flags.NONE, 0); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.GO_AWAY, length, Flags.NONE, 0); + ByteBuffer byteBuffer = header.getByteBuffer(); - header.putInt(lastStreamId); - header.putInt(error); + byteBuffer.putInt(lastStreamId); + byteBuffer.putInt(error); if (payload != null) - header.put(payload); + byteBuffer.put(payload); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + length; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeaderGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeaderGenerator.java index 1d1592c19e99..e27db2ec0b65 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeaderGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeaderGenerator.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http2.frames.Frame; import org.eclipse.jetty.http2.frames.FrameType; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class HeaderGenerator { @@ -39,16 +40,17 @@ public boolean isUseDirectByteBuffers() return useDirectByteBuffers; } - public ByteBuffer generate(ByteBufferPool.Lease lease, FrameType frameType, int capacity, int length, int flags, int streamId) + public RetainableByteBuffer generate(RetainableByteBufferPool.Accumulator accumulator, FrameType frameType, int capacity, int length, int flags, int streamId) { - ByteBuffer header = lease.acquire(capacity, isUseDirectByteBuffers()); + RetainableByteBuffer buffer = accumulator.acquire(capacity, isUseDirectByteBuffers()); + ByteBuffer header = buffer.getByteBuffer(); header.put((byte)((length & 0x00_FF_00_00) >>> 16)); header.put((byte)((length & 0x00_00_FF_00) >>> 8)); header.put((byte)((length & 0x00_00_00_FF))); header.put((byte)frameType.getType()); header.put((byte)flags); header.putInt(streamId); - return header; + return buffer; } public int getMaxFrameSize() diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeadersGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeadersGenerator.java index c1afa0a46ee8..59da09d6824d 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeadersGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/HeadersGenerator.java @@ -23,7 +23,8 @@ import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.hpack.HpackException; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class HeadersGenerator extends FrameGenerator @@ -46,13 +47,13 @@ public HeadersGenerator(HeaderGenerator headerGenerator, HpackEncoder encoder, i } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) throws HpackException + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) throws HpackException { HeadersFrame headersFrame = (HeadersFrame)frame; - return generateHeaders(lease, headersFrame.getStreamId(), headersFrame.getMetaData(), headersFrame.getPriority(), headersFrame.isEndStream()); + return generateHeaders(accumulator, headersFrame.getStreamId(), headersFrame.getMetaData(), headersFrame.getPriority(), headersFrame.isEndStream()); } - public int generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData metaData, PriorityFrame priority, boolean endStream) throws HpackException + public int generateHeaders(RetainableByteBufferPool.Accumulator accumulator, int streamId, MetaData metaData, PriorityFrame priority, boolean endStream) throws HpackException { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); @@ -62,12 +63,13 @@ public int generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData me if (priority != null) flags = Flags.PRIORITY; - ByteBuffer hpacked = encode(encoder, lease, metaData, getMaxFrameSize()); - int hpackedLength = hpacked.position(); - BufferUtil.flipToFlush(hpacked, 0); + RetainableByteBuffer hpack = encode(encoder, accumulator, metaData, getMaxFrameSize()); + ByteBuffer hpackByteBuffer = hpack.getByteBuffer(); + int hpackLength = hpackByteBuffer.position(); + BufferUtil.flipToFlush(hpackByteBuffer, 0); // Split into CONTINUATION frames if necessary. - if (maxHeaderBlockFragment > 0 && hpackedLength > maxHeaderBlockFragment) + if (maxHeaderBlockFragment > 0 && hpackLength > maxHeaderBlockFragment) { if (endStream) flags |= Flags.END_STREAM; @@ -76,35 +78,38 @@ public int generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData me if (priority != null) length += PriorityFrame.PRIORITY_LENGTH; - ByteBuffer header = generateHeader(lease, FrameType.HEADERS, length, flags, streamId); - generatePriority(header, priority); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); - hpacked.limit(maxHeaderBlockFragment); - lease.append(hpacked.slice(), false); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.HEADERS, length, flags, streamId); + ByteBuffer headerByteBuffer = header.getByteBuffer(); + generatePriority(headerByteBuffer, priority); + BufferUtil.flipToFlush(headerByteBuffer, 0); + accumulator.append(header); + hpackByteBuffer.limit(maxHeaderBlockFragment); + accumulator.append(RetainableByteBuffer.asNonRetainable(hpackByteBuffer.slice())); int totalLength = Frame.HEADER_LENGTH + length; int position = maxHeaderBlockFragment; int limit = position + maxHeaderBlockFragment; - while (limit < hpackedLength) + while (limit < hpackLength) { - hpacked.position(position).limit(limit); - header = generateHeader(lease, FrameType.CONTINUATION, maxHeaderBlockFragment, Flags.NONE, streamId); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); - lease.append(hpacked.slice(), false); + hpackByteBuffer.position(position).limit(limit); + header = generateHeader(accumulator, FrameType.CONTINUATION, maxHeaderBlockFragment, Flags.NONE, streamId); + headerByteBuffer = header.getByteBuffer(); + BufferUtil.flipToFlush(headerByteBuffer, 0); + accumulator.append(header); + accumulator.append(RetainableByteBuffer.asNonRetainable(hpackByteBuffer.slice())); position += maxHeaderBlockFragment; limit += maxHeaderBlockFragment; totalLength += Frame.HEADER_LENGTH + maxHeaderBlockFragment; } - hpacked.position(position).limit(hpackedLength); - header = generateHeader(lease, FrameType.CONTINUATION, hpacked.remaining(), Flags.END_HEADERS, streamId); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); - lease.append(hpacked, true); - totalLength += Frame.HEADER_LENGTH + hpacked.remaining(); + hpackByteBuffer.position(position).limit(hpackLength); + header = generateHeader(accumulator, FrameType.CONTINUATION, hpack.remaining(), Flags.END_HEADERS, streamId); + headerByteBuffer = header.getByteBuffer(); + BufferUtil.flipToFlush(headerByteBuffer, 0); + accumulator.append(header); + accumulator.append(hpack); + totalLength += Frame.HEADER_LENGTH + hpack.remaining(); return totalLength; } @@ -114,15 +119,16 @@ public int generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData me if (endStream) flags |= Flags.END_STREAM; - int length = hpackedLength; + int length = hpackLength; if (priority != null) length += PriorityFrame.PRIORITY_LENGTH; - ByteBuffer header = generateHeader(lease, FrameType.HEADERS, length, flags, streamId); - generatePriority(header, priority); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); - lease.append(hpacked, true); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.HEADERS, length, flags, streamId); + ByteBuffer headerByteBuffer = header.getByteBuffer(); + generatePriority(headerByteBuffer, priority); + BufferUtil.flipToFlush(headerByteBuffer, 0); + accumulator.append(header); + accumulator.append(hpack); return Frame.HEADER_LENGTH + length; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/NoOpGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/NoOpGenerator.java index 388bb582cbc7..0361b466dc97 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/NoOpGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/NoOpGenerator.java @@ -14,7 +14,7 @@ package org.eclipse.jetty.http2.internal.generator; import org.eclipse.jetty.http2.frames.Frame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class NoOpGenerator extends FrameGenerator { @@ -24,7 +24,7 @@ public NoOpGenerator() } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { return 0; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PingGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PingGenerator.java index fd12c31433da..2d8876d7a699 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PingGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PingGenerator.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.PingFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class PingGenerator extends FrameGenerator @@ -30,23 +31,24 @@ public PingGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { PingFrame pingFrame = (PingFrame)frame; - return generatePing(lease, pingFrame.getPayload(), pingFrame.isReply()); + return generatePing(accumulator, pingFrame.getPayload(), pingFrame.isReply()); } - public int generatePing(ByteBufferPool.Lease lease, byte[] payload, boolean reply) + public int generatePing(RetainableByteBufferPool.Accumulator accumulator, byte[] payload, boolean reply) { if (payload.length != PingFrame.PING_LENGTH) throw new IllegalArgumentException("Invalid payload length: " + payload.length); - ByteBuffer header = generateHeader(lease, FrameType.PING, PingFrame.PING_LENGTH, reply ? Flags.ACK : Flags.NONE, 0); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.PING, PingFrame.PING_LENGTH, reply ? Flags.ACK : Flags.NONE, 0); + ByteBuffer byteBuffer = header.getByteBuffer(); - header.put(payload); + byteBuffer.put(payload); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + PingFrame.PING_LENGTH; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PrefaceGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PrefaceGenerator.java index b2cddd592862..ac75f29c8de5 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PrefaceGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PrefaceGenerator.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http2.frames.Frame; import org.eclipse.jetty.http2.frames.PrefaceFrame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class PrefaceGenerator extends FrameGenerator { @@ -27,9 +28,9 @@ public PrefaceGenerator() } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { - lease.append(ByteBuffer.wrap(PrefaceFrame.PREFACE_BYTES), false); + accumulator.append(RetainableByteBuffer.asNonRetainable(ByteBuffer.wrap(PrefaceFrame.PREFACE_BYTES))); return PrefaceFrame.PREFACE_BYTES.length; } } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PriorityGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PriorityGenerator.java index 8ddb988de0c1..ea0dfd192816 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PriorityGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PriorityGenerator.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.PriorityFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class PriorityGenerator extends FrameGenerator @@ -30,18 +31,19 @@ public PriorityGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { PriorityFrame priorityFrame = (PriorityFrame)frame; - return generatePriority(lease, priorityFrame.getStreamId(), priorityFrame.getParentStreamId(), priorityFrame.getWeight(), priorityFrame.isExclusive()); + return generatePriority(accumulator, priorityFrame.getStreamId(), priorityFrame.getParentStreamId(), priorityFrame.getWeight(), priorityFrame.isExclusive()); } - public int generatePriority(ByteBufferPool.Lease lease, int streamId, int parentStreamId, int weight, boolean exclusive) + public int generatePriority(RetainableByteBufferPool.Accumulator accumulator, int streamId, int parentStreamId, int weight, boolean exclusive) { - ByteBuffer header = generateHeader(lease, FrameType.PRIORITY, PriorityFrame.PRIORITY_LENGTH, Flags.NONE, streamId); - generatePriorityBody(header, streamId, parentStreamId, weight, exclusive); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.PRIORITY, PriorityFrame.PRIORITY_LENGTH, Flags.NONE, streamId); + ByteBuffer byteBuffer = header.getByteBuffer(); + generatePriorityBody(byteBuffer, streamId, parentStreamId, weight, exclusive); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + PriorityFrame.PRIORITY_LENGTH; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PushPromiseGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PushPromiseGenerator.java index a100a5d173a0..47babc400499 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PushPromiseGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/PushPromiseGenerator.java @@ -22,7 +22,8 @@ import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.hpack.HpackException; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class PushPromiseGenerator extends FrameGenerator @@ -36,13 +37,13 @@ public PushPromiseGenerator(HeaderGenerator headerGenerator, HpackEncoder encode } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) throws HpackException + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) throws HpackException { PushPromiseFrame pushPromiseFrame = (PushPromiseFrame)frame; - return generatePushPromise(lease, pushPromiseFrame.getStreamId(), pushPromiseFrame.getPromisedStreamId(), pushPromiseFrame.getMetaData()); + return generatePushPromise(accumulator, pushPromiseFrame.getStreamId(), pushPromiseFrame.getPromisedStreamId(), pushPromiseFrame.getMetaData()); } - public int generatePushPromise(ByteBufferPool.Lease lease, int streamId, int promisedStreamId, MetaData metaData) throws HpackException + public int generatePushPromise(RetainableByteBufferPool.Accumulator accumulator, int streamId, int promisedStreamId, MetaData metaData) throws HpackException { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); @@ -54,19 +55,21 @@ public int generatePushPromise(ByteBufferPool.Lease lease, int streamId, int pro int extraSpace = 4; maxFrameSize -= extraSpace; - ByteBuffer hpacked = encode(encoder, lease, metaData, maxFrameSize); - int hpackedLength = hpacked.position(); - BufferUtil.flipToFlush(hpacked, 0); + RetainableByteBuffer hpack = encode(encoder, accumulator, metaData, maxFrameSize); + ByteBuffer hpackByteBuffer = hpack.getByteBuffer(); + int hpackLength = hpackByteBuffer.position(); + BufferUtil.flipToFlush(hpackByteBuffer, 0); - int length = hpackedLength + extraSpace; + int length = hpackLength + extraSpace; int flags = Flags.END_HEADERS; - ByteBuffer header = generateHeader(lease, FrameType.PUSH_PROMISE, length, flags, streamId); - header.putInt(promisedStreamId); - BufferUtil.flipToFlush(header, 0); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.PUSH_PROMISE, length, flags, streamId); + ByteBuffer headerByteBuffer = header.getByteBuffer(); + headerByteBuffer.putInt(promisedStreamId); + BufferUtil.flipToFlush(headerByteBuffer, 0); - lease.append(header, true); - lease.append(hpacked, true); + accumulator.append(header); + accumulator.append(hpack); return Frame.HEADER_LENGTH + length; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/ResetGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/ResetGenerator.java index ed55d23d5cd9..a183338b143b 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/ResetGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/ResetGenerator.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.ResetFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class ResetGenerator extends FrameGenerator @@ -30,21 +31,22 @@ public ResetGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { ResetFrame resetFrame = (ResetFrame)frame; - return generateReset(lease, resetFrame.getStreamId(), resetFrame.getError()); + return generateReset(accumulator, resetFrame.getStreamId(), resetFrame.getError()); } - public int generateReset(ByteBufferPool.Lease lease, int streamId, int error) + public int generateReset(RetainableByteBufferPool.Accumulator accumulator, int streamId, int error) { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); - ByteBuffer header = generateHeader(lease, FrameType.RST_STREAM, ResetFrame.RESET_LENGTH, Flags.NONE, streamId); - header.putInt(error); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.RST_STREAM, ResetFrame.RESET_LENGTH, Flags.NONE, streamId); + ByteBuffer byteBuffer = header.getByteBuffer(); + byteBuffer.putInt(error); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + ResetFrame.RESET_LENGTH; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/SettingsGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/SettingsGenerator.java index e31fe3f5756f..a8170bf506d3 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/SettingsGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/SettingsGenerator.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.SettingsFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class SettingsGenerator extends FrameGenerator @@ -31,13 +32,13 @@ public SettingsGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { SettingsFrame settingsFrame = (SettingsFrame)frame; - return generateSettings(lease, settingsFrame.getSettings(), settingsFrame.isReply()); + return generateSettings(accumulator, settingsFrame.getSettings(), settingsFrame.isReply()); } - public int generateSettings(ByteBufferPool.Lease lease, Map settings, boolean reply) + public int generateSettings(RetainableByteBufferPool.Accumulator accumulator, Map settings, boolean reply) { // Two bytes for the identifier, four bytes for the value. int entryLength = 2 + 4; @@ -45,16 +46,17 @@ public int generateSettings(ByteBufferPool.Lease lease, Map se if (length > getMaxFrameSize()) throw new IllegalArgumentException("Invalid settings, too big"); - ByteBuffer header = generateHeader(lease, FrameType.SETTINGS, length, reply ? Flags.ACK : Flags.NONE, 0); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.SETTINGS, length, reply ? Flags.ACK : Flags.NONE, 0); + ByteBuffer byteBuffer = header.getByteBuffer(); for (Map.Entry entry : settings.entrySet()) { - header.putShort(entry.getKey().shortValue()); - header.putInt(entry.getValue()); + byteBuffer.putShort(entry.getKey().shortValue()); + byteBuffer.putInt(entry.getValue()); } - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + length; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/WindowUpdateGenerator.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/WindowUpdateGenerator.java index 960dcd20bb90..ad2399db4257 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/WindowUpdateGenerator.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/generator/WindowUpdateGenerator.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.frames.WindowUpdateFrame; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class WindowUpdateGenerator extends FrameGenerator @@ -30,21 +31,22 @@ public WindowUpdateGenerator(HeaderGenerator headerGenerator) } @Override - public int generate(ByteBufferPool.Lease lease, Frame frame) + public int generate(RetainableByteBufferPool.Accumulator accumulator, Frame frame) { WindowUpdateFrame windowUpdateFrame = (WindowUpdateFrame)frame; - return generateWindowUpdate(lease, windowUpdateFrame.getStreamId(), windowUpdateFrame.getWindowDelta()); + return generateWindowUpdate(accumulator, windowUpdateFrame.getStreamId(), windowUpdateFrame.getWindowDelta()); } - public int generateWindowUpdate(ByteBufferPool.Lease lease, int streamId, int windowUpdate) + public int generateWindowUpdate(RetainableByteBufferPool.Accumulator accumulator, int streamId, int windowUpdate) { if (windowUpdate < 0) throw new IllegalArgumentException("Invalid window update: " + windowUpdate); - ByteBuffer header = generateHeader(lease, FrameType.WINDOW_UPDATE, WindowUpdateFrame.WINDOW_UPDATE_LENGTH, Flags.NONE, streamId); - header.putInt(windowUpdate); - BufferUtil.flipToFlush(header, 0); - lease.append(header, true); + RetainableByteBuffer header = generateHeader(accumulator, FrameType.WINDOW_UPDATE, WindowUpdateFrame.WINDOW_UPDATE_LENGTH, Flags.NONE, streamId); + ByteBuffer byteBuffer = header.getByteBuffer(); + byteBuffer.putInt(windowUpdate); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(header); return Frame.HEADER_LENGTH + WindowUpdateFrame.WINDOW_UPDATE_LENGTH; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ContinuationBodyParser.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ContinuationBodyParser.java index 7ec64d869b75..5315b8ce67ab 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ContinuationBodyParser.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ContinuationBodyParser.java @@ -20,6 +20,7 @@ import org.eclipse.jetty.http2.frames.HeadersFrame; import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.Flags; +import org.eclipse.jetty.io.RetainableByteBuffer; public class ContinuationBodyParser extends BodyParser { @@ -100,9 +101,9 @@ public boolean parse(ByteBuffer buffer) private boolean onHeaders(ByteBuffer buffer) { - ByteBuffer headerBlock = headerBlockFragments.complete(); - MetaData metaData = headerBlockParser.parse(headerBlock, headerBlock.remaining()); - headerBlockFragments.getByteBufferPool().release(headerBlock); + RetainableByteBuffer headerBlock = headerBlockFragments.complete(); + MetaData metaData = headerBlockParser.parse(headerBlock.getByteBuffer(), headerBlock.remaining()); + headerBlock.release(); if (metaData == null) return true; if (metaData == HeaderBlockParser.SESSION_FAILURE) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockFragments.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockFragments.java index 8e4e892002ca..e247606a0a3c 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockFragments.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockFragments.java @@ -16,24 +16,21 @@ import java.nio.ByteBuffer; import org.eclipse.jetty.http2.frames.PriorityFrame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; +import org.eclipse.jetty.util.BufferUtil; public class HeaderBlockFragments { - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private PriorityFrame priorityFrame; private boolean endStream; private int streamId; - private ByteBuffer storage; + private RetainableByteBuffer storage; - public HeaderBlockFragments(ByteBufferPool byteBufferPool) + public HeaderBlockFragments(RetainableByteBufferPool bufferPool) { - this.byteBufferPool = byteBufferPool; - } - - public ByteBufferPool getByteBufferPool() - { - return byteBufferPool; + this.bufferPool = bufferPool; } public void storeFragment(ByteBuffer fragment, int length, boolean last) @@ -41,27 +38,28 @@ public void storeFragment(ByteBuffer fragment, int length, boolean last) if (storage == null) { int space = last ? length : length * 2; - storage = byteBufferPool.acquire(space, fragment.isDirect()); - storage.clear(); + storage = bufferPool.acquire(space, fragment.isDirect()); + BufferUtil.flipToFill(storage.getByteBuffer()); } // Grow the storage if necessary. if (storage.remaining() < length) { + ByteBuffer byteBuffer = storage.getByteBuffer(); int space = last ? length : length * 2; - int capacity = storage.position() + space; - ByteBuffer newStorage = byteBufferPool.acquire(capacity, storage.isDirect()); - newStorage.clear(); - storage.flip(); - newStorage.put(storage); - byteBufferPool.release(storage); + int capacity = byteBuffer.position() + space; + RetainableByteBuffer newStorage = bufferPool.acquire(capacity, storage.isDirect()); + BufferUtil.flipToFill(newStorage.getByteBuffer()); + byteBuffer.flip(); + newStorage.getByteBuffer().put(byteBuffer); + storage.release(); storage = newStorage; } // Copy the fragment into the storage. int limit = fragment.limit(); fragment.limit(fragment.position() + length); - storage.put(fragment); + storage.getByteBuffer().put(fragment); fragment.limit(limit); } @@ -85,11 +83,11 @@ public void setEndStream(boolean endStream) this.endStream = endStream; } - public ByteBuffer complete() + public RetainableByteBuffer complete() { - ByteBuffer result = storage; + RetainableByteBuffer result = storage; storage = null; - result.flip(); + result.getByteBuffer().flip(); return result; } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockParser.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockParser.java index 8ab182354220..2c8baf850a87 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockParser.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/HeaderBlockParser.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http2.hpack.HpackDecoder; import org.eclipse.jetty.http2.hpack.HpackException; import org.eclipse.jetty.http2.internal.ErrorCode; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,15 +33,15 @@ public class HeaderBlockParser private static final Logger LOG = LoggerFactory.getLogger(HeaderBlockParser.class); private final HeaderParser headerParser; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final HpackDecoder hpackDecoder; private final BodyParser notifier; - private ByteBuffer blockBuffer; + private RetainableByteBuffer blockBuffer; - public HeaderBlockParser(HeaderParser headerParser, ByteBufferPool byteBufferPool, HpackDecoder hpackDecoder, BodyParser notifier) + public HeaderBlockParser(HeaderParser headerParser, RetainableByteBufferPool bufferPool, HpackDecoder hpackDecoder, BodyParser notifier) { this.headerParser = headerParser; - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.hpackDecoder = hpackDecoder; this.notifier = notifier; } @@ -61,17 +62,19 @@ public MetaData parse(ByteBuffer buffer, int blockLength) // If they are not all available, accumulate them. // When all are available, decode them. - int accumulated = blockBuffer == null ? 0 : blockBuffer.position(); + ByteBuffer byteBuffer = blockBuffer == null ? null : blockBuffer.getByteBuffer(); + int accumulated = byteBuffer == null ? 0 : byteBuffer.position(); int remaining = blockLength - accumulated; if (buffer.remaining() < remaining) { if (blockBuffer == null) { - blockBuffer = byteBufferPool.acquire(blockLength, buffer.isDirect()); - BufferUtil.clearToFill(blockBuffer); + blockBuffer = bufferPool.acquire(blockLength, buffer.isDirect()); + byteBuffer = blockBuffer.getByteBuffer(); + BufferUtil.flipToFill(byteBuffer); } - blockBuffer.put(buffer); + byteBuffer.put(buffer); return null; } else @@ -79,11 +82,11 @@ public MetaData parse(ByteBuffer buffer, int blockLength) int limit = buffer.limit(); buffer.limit(buffer.position() + remaining); ByteBuffer toDecode; - if (blockBuffer != null) + if (byteBuffer != null) { - blockBuffer.put(buffer); - BufferUtil.flipToFlush(blockBuffer, 0); - toDecode = blockBuffer; + byteBuffer.put(buffer); + BufferUtil.flipToFlush(byteBuffer, 0); + toDecode = byteBuffer; } else { @@ -121,7 +124,7 @@ public MetaData parse(ByteBuffer buffer, int blockLength) if (blockBuffer != null) { - byteBufferPool.release(blockBuffer); + blockBuffer.release(); blockBuffer = null; } } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/Parser.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/Parser.java index e5fd3fd74a1c..9e616da7b613 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/Parser.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/Parser.java @@ -31,7 +31,7 @@ import org.eclipse.jetty.http2.hpack.HpackDecoder; import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +44,7 @@ public class Parser { private static final Logger LOG = LoggerFactory.getLogger(Parser.class); - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final Listener listener; private final HeaderParser headerParser; private final HpackDecoder hpackDecoder; @@ -55,14 +55,14 @@ public class Parser private boolean continuation; private State state = State.HEADER; - public Parser(ByteBufferPool byteBufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize) + public Parser(RetainableByteBufferPool bufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize) { - this(byteBufferPool, listener, maxDynamicTableSize, maxHeaderSize, RateControl.NO_RATE_CONTROL); + this(bufferPool, listener, maxDynamicTableSize, maxHeaderSize, RateControl.NO_RATE_CONTROL); } - public Parser(ByteBufferPool byteBufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize, RateControl rateControl) + public Parser(RetainableByteBufferPool bufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize, RateControl rateControl) { - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.listener = listener; this.headerParser = new HeaderParser(rateControl == null ? RateControl.NO_RATE_CONTROL : rateControl); this.hpackDecoder = new HpackDecoder(maxDynamicTableSize, maxHeaderSize); @@ -73,8 +73,8 @@ public void init(UnaryOperator wrapper) { Listener listener = wrapper.apply(this.listener); unknownBodyParser = new UnknownBodyParser(headerParser, listener); - HeaderBlockParser headerBlockParser = new HeaderBlockParser(headerParser, byteBufferPool, hpackDecoder, unknownBodyParser); - HeaderBlockFragments headerBlockFragments = new HeaderBlockFragments(byteBufferPool); + HeaderBlockParser headerBlockParser = new HeaderBlockParser(headerParser, bufferPool, hpackDecoder, unknownBodyParser); + HeaderBlockFragments headerBlockFragments = new HeaderBlockFragments(bufferPool); bodyParsers[FrameType.DATA.getType()] = new DataBodyParser(headerParser, listener); bodyParsers[FrameType.HEADERS.getType()] = new HeadersBodyParser(headerParser, listener, headerBlockParser, headerBlockFragments); bodyParsers[FrameType.PRIORITY.getType()] = new PriorityBodyParser(headerParser, listener); diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ServerParser.java b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ServerParser.java index d7dbfa980953..8023fd4a8a42 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ServerParser.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/internal/parser/ServerParser.java @@ -19,7 +19,7 @@ import org.eclipse.jetty.http2.frames.FrameType; import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.Flags; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,9 +33,9 @@ public class ServerParser extends Parser private State state = State.PREFACE; private boolean notifyPreface = true; - public ServerParser(ByteBufferPool byteBufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize, RateControl rateControl) + public ServerParser(RetainableByteBufferPool bufferPool, Listener listener, int maxDynamicTableSize, int maxHeaderSize, RateControl rateControl) { - super(byteBufferPool, listener, maxDynamicTableSize, maxHeaderSize, rateControl); + super(bufferPool, listener, maxDynamicTableSize, maxHeaderSize, rateControl); this.listener = listener; this.prefaceParser = new PrefaceParser(listener); } diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ContinuationParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ContinuationParseTest.java index 1c68695415c3..8b2bb5ece578 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ContinuationParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ContinuationParseTest.java @@ -29,8 +29,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.HeadersGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -42,11 +42,11 @@ public class ContinuationParseTest @Test public void testParseOneByteAtATime() throws Exception { - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); HeadersGenerator generator = new HeadersGenerator(new HeaderGenerator(), new HpackEncoder()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -71,10 +71,10 @@ public void onConnectionFailure(int error, String reason) .put("User-Agent", "Jetty"); MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP.asString(), new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields, -1); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateHeaders(lease, streamId, metaData, null, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateHeaders(accumulator, streamId, metaData, null, true); - List byteBuffers = lease.getByteBuffers(); + List byteBuffers = accumulator.getByteBuffers(); assertEquals(2, byteBuffers.size()); ByteBuffer headersBody = byteBuffers.remove(1); @@ -131,7 +131,7 @@ public void onConnectionFailure(int error, String reason) byteBuffers.add(headersBody.slice()); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/DataGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/DataGenerateParseTest.java index 23f17926dc6a..d045301067cf 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/DataGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/DataGenerateParseTest.java @@ -22,8 +22,8 @@ import org.eclipse.jetty.http2.internal.generator.DataGenerator; import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.junit.jupiter.api.Test; @@ -34,7 +34,7 @@ public class DataGenerateParseTest { private final byte[] smallContent = new byte[128]; private final byte[] largeContent = new byte[128 * 1024]; - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); public DataGenerateParseTest() { @@ -88,7 +88,7 @@ private List testGenerateParse(ByteBuffer data) DataGenerator generator = new DataGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onData(DataFrame frame) @@ -101,19 +101,19 @@ public void onData(DataFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); ByteBuffer slice = data.slice(); int generated = 0; while (true) { - generated += generator.generateData(lease, 13, slice, true, slice.remaining()); + generated += generator.generateData(accumulator, 13, slice, true, slice.remaining()); generated -= Frame.HEADER_LENGTH; if (generated == data.remaining()) break; } frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); } @@ -128,7 +128,7 @@ public void testGenerateParseOneByteAtATime() DataGenerator generator = new DataGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onData(DataFrame frame) @@ -141,20 +141,20 @@ public void onData(DataFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); ByteBuffer data = ByteBuffer.wrap(largeContent); ByteBuffer slice = data.slice(); int generated = 0; while (true) { - generated += generator.generateData(lease, 13, slice, true, slice.remaining()); + generated += generator.generateData(accumulator, 13, slice, true, slice.remaining()); generated -= Frame.HEADER_LENGTH; if (generated == data.remaining()) break; } frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/FrameFloodTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/FrameFloodTest.java index b52e9e35e52f..47b80c64c6ac 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/FrameFloodTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/FrameFloodTest.java @@ -24,8 +24,8 @@ import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.internal.Flags; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -33,7 +33,7 @@ public class FrameFloodTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); // Frame structure: // | Len0 | Len1 | Len2 | Type | Flags | StreamID0 |StreamID1 |StreamID2 |StreamID3 | Payload... | @@ -72,7 +72,7 @@ public void testHeadersFrameFlood() public void testInvalidHeadersFrameFlood() throws Exception { // Invalid MetaData (no method, no scheme, etc). - MetaData.Request metadata = new MetaData.Request(null, (String)null, null, null, HttpVersion.HTTP_2, null, -1); + MetaData.Request metadata = new MetaData.Request(null, null, null, null, HttpVersion.HTTP_2, null, -1); HpackEncoder encoder = new HpackEncoder(); ByteBuffer buffer = ByteBuffer.allocate(1024); encoder.encode(buffer, metadata); @@ -123,7 +123,7 @@ public void testUnknownFrameFlood() private void testFrameFlood(byte[] preamble, byte[] bytes) { AtomicBoolean failed = new AtomicBoolean(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/GoAwayGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/GoAwayGenerateParseTest.java index df27cdc3417f..9fa596579e0c 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/GoAwayGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/GoAwayGenerateParseTest.java @@ -22,8 +22,8 @@ import org.eclipse.jetty.http2.internal.generator.GoAwayGenerator; import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -32,7 +32,7 @@ public class GoAwayGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -40,7 +40,7 @@ public void testGenerateParse() throws Exception GoAwayGenerator generator = new GoAwayGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onGoAway(GoAwayFrame frame) @@ -56,11 +56,11 @@ public void onGoAway(GoAwayFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateGoAway(lease, lastStreamId, error, null); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateGoAway(accumulator, lastStreamId, error, null); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -82,7 +82,7 @@ public void testGenerateParseOneByteAtATime() throws Exception GoAwayGenerator generator = new GoAwayGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onGoAway(GoAwayFrame frame) @@ -100,11 +100,11 @@ public void onGoAway(GoAwayFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateGoAway(lease, lastStreamId, error, payload); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateGoAway(accumulator, lastStreamId, error, payload); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersGenerateParseTest.java index 90f89f05cd09..9970eb54ea3e 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersGenerateParseTest.java @@ -28,8 +28,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.HeadersGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,7 +38,7 @@ public class HeadersGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -52,7 +52,7 @@ public void testGenerateParse() throws Exception MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP.asString(), new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields, -1); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -65,12 +65,12 @@ public void onHeaders(HeadersFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); PriorityFrame priorityFrame = new PriorityFrame(streamId, 3 * streamId, 200, true); - generator.generateHeaders(lease, streamId, metaData, priorityFrame, true); + generator.generateHeaders(accumulator, streamId, metaData, priorityFrame, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -105,7 +105,7 @@ public void testGenerateParseOneByteAtATime() throws Exception HeadersGenerator generator = new HeadersGenerator(new HeaderGenerator(), new HpackEncoder()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -124,12 +124,12 @@ public void onHeaders(HeadersFrame frame) .put("User-Agent", "Jetty"); MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP.asString(), new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields, -1); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); PriorityFrame priorityFrame = new PriorityFrame(streamId, 3 * streamId, 200, true); - generator.generateHeaders(lease, streamId, metaData, priorityFrame, true); + generator.generateHeaders(accumulator, streamId, metaData, priorityFrame, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { buffer = buffer.slice(); while (buffer.hasRemaining()) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersTooLargeParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersTooLargeParseTest.java index 141176f4efd9..fa2817f5313d 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersTooLargeParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/HeadersTooLargeParseTest.java @@ -28,8 +28,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.HeadersGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,7 +37,7 @@ public class HeadersTooLargeParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testProtocolErrorURITooLong() throws HpackException @@ -67,7 +67,7 @@ private void assertProtocolError(int maxHeaderSize, MetaData.Request metaData) t HeadersGenerator generator = new HeadersGenerator(new HeaderGenerator(), new HpackEncoder()); AtomicInteger failure = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) @@ -78,11 +78,11 @@ public void onConnectionFailure(int error, String reason) parser.init(UnaryOperator.identity()); int streamId = 48; - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); PriorityFrame priorityFrame = new PriorityFrame(streamId, 3 * streamId, 200, true); - int len = generator.generateHeaders(lease, streamId, metaData, priorityFrame, true); + int len = generator.generateHeaders(accumulator, streamId, metaData, priorityFrame, true); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining() && failure.get() == 0) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/MaxFrameSizeParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/MaxFrameSizeParseTest.java index 478a6f8e4f53..126151780fb7 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/MaxFrameSizeParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/MaxFrameSizeParseTest.java @@ -19,15 +19,15 @@ import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class MaxFrameSizeParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testMaxFrameSize() @@ -35,7 +35,7 @@ public void testMaxFrameSize() int maxFrameLength = Frame.DEFAULT_MAX_LENGTH + 16; AtomicInteger failure = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PingGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PingGenerateParseTest.java index 39f1f8c0d3b9..a64a57e555d9 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PingGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PingGenerateParseTest.java @@ -22,8 +22,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.PingGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.NanoTime; import org.junit.jupiter.api.Test; @@ -33,7 +33,7 @@ public class PingGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -41,7 +41,7 @@ public void testGenerateParse() throws Exception PingGenerator generator = new PingGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPing(PingFrame frame) @@ -57,11 +57,11 @@ public void onPing(PingFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePing(lease, payload, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePing(accumulator, payload, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -82,7 +82,7 @@ public void testGenerateParseOneByteAtATime() throws Exception PingGenerator generator = new PingGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPing(PingFrame frame) @@ -98,11 +98,11 @@ public void onPing(PingFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePing(lease, payload, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePing(accumulator, payload, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -123,7 +123,7 @@ public void testPayloadAsLong() throws Exception PingGenerator generator = new PingGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPing(PingFrame frame) @@ -133,11 +133,11 @@ public void onPing(PingFrame frame) }, 4096, 8192); parser.init(UnaryOperator.identity()); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); PingFrame ping = new PingFrame(NanoTime.now(), true); - generator.generate(lease, ping); + generator.generate(accumulator, ping); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PriorityGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PriorityGenerateParseTest.java index 8d048fbaa002..182cc0fbc675 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PriorityGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PriorityGenerateParseTest.java @@ -21,15 +21,15 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.PriorityGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class PriorityGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -37,7 +37,7 @@ public void testGenerateParse() throws Exception PriorityGenerator generator = new PriorityGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPriority(PriorityFrame frame) @@ -55,11 +55,11 @@ public void onPriority(PriorityFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePriority(lease, streamId, parentStreamId, weight, exclusive); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePriority(accumulator, streamId, parentStreamId, weight, exclusive); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -82,7 +82,7 @@ public void testGenerateParseOneByteAtATime() throws Exception PriorityGenerator generator = new PriorityGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPriority(PriorityFrame frame) @@ -100,11 +100,11 @@ public void onPriority(PriorityFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePriority(lease, streamId, parentStreamId, weight, exclusive); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePriority(accumulator, streamId, parentStreamId, weight, exclusive); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PushPromiseGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PushPromiseGenerateParseTest.java index 812d4ff2f891..2e1e6a1bded1 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PushPromiseGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/PushPromiseGenerateParseTest.java @@ -28,8 +28,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.PushPromiseGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,7 +37,7 @@ public class PushPromiseGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -45,7 +45,7 @@ public void testGenerateParse() throws Exception PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPushPromise(PushPromiseFrame frame) @@ -65,11 +65,11 @@ public void onPushPromise(PushPromiseFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePushPromise(lease, streamId, promisedStreamId, metaData); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePushPromise(accumulator, streamId, promisedStreamId, metaData); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -98,7 +98,7 @@ public void testGenerateParseOneByteAtATime() throws Exception PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onPushPromise(PushPromiseFrame frame) @@ -118,11 +118,11 @@ public void onPushPromise(PushPromiseFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generatePushPromise(lease, streamId, promisedStreamId, metaData); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generatePushPromise(accumulator, streamId, promisedStreamId, metaData); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ResetGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ResetGenerateParseTest.java index 5bbbb44c109d..68b356e1012f 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ResetGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/ResetGenerateParseTest.java @@ -21,15 +21,15 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.ResetGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class ResetGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -37,7 +37,7 @@ public void testGenerateParse() throws Exception ResetGenerator generator = new ResetGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onReset(ResetFrame frame) @@ -53,11 +53,11 @@ public void onReset(ResetFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateReset(lease, streamId, error); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateReset(accumulator, streamId, error); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -78,7 +78,7 @@ public void testGenerateParseOneByteAtATime() throws Exception ResetGenerator generator = new ResetGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onReset(ResetFrame frame) @@ -94,11 +94,11 @@ public void onReset(ResetFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateReset(lease, streamId, error); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateReset(accumulator, streamId, error); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/SettingsGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/SettingsGenerateParseTest.java index a56892450511..5055e31dd5f3 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/SettingsGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/SettingsGenerateParseTest.java @@ -26,8 +26,8 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.SettingsGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,7 +35,7 @@ public class SettingsGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParseNoSettings() @@ -72,7 +72,7 @@ private List testGenerateParse(Map settings) SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator()); List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -85,11 +85,11 @@ public void onSettings(SettingsFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateSettings(lease, settings, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateSettings(accumulator, settings, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -107,7 +107,7 @@ public void testGenerateParseInvalidSettings() SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator()); AtomicInteger errorRef = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) @@ -119,13 +119,13 @@ public void onConnectionFailure(int error, String reason) Map settings1 = new HashMap<>(); settings1.put(13, 17); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateSettings(lease, settings1, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateSettings(accumulator, settings1, true); // Modify the length of the frame to make it invalid - ByteBuffer bytes = lease.getByteBuffers().get(0); + ByteBuffer bytes = accumulator.getByteBuffers().get(0); bytes.putShort(1, (short)(bytes.getShort(1) - 1)); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -142,7 +142,7 @@ public void testGenerateParseOneByteAtATime() SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator()); List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -160,11 +160,11 @@ public void onSettings(SettingsFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateSettings(lease, settings1, true); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateSettings(accumulator, settings1, true); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -187,7 +187,7 @@ public void testGenerateParseTooManyDifferentSettingsInOneFrame() SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator()); AtomicInteger errorRef = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) @@ -205,10 +205,10 @@ public void onConnectionFailure(int error, String reason) settings.put(i + 10, i); } - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateSettings(lease, settings, false); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateSettings(accumulator, settings, false); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -227,7 +227,7 @@ public void testGenerateParseTooManySameSettingsInOneFrame() throws Exception int maxSettingsKeys = pairs / 2; AtomicInteger errorRef = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter(), 4096, 8192); parser.setMaxSettingsKeys(maxSettingsKeys); parser.setMaxFrameLength(Frame.DEFAULT_MAX_LENGTH); parser.init(listener -> new Parser.Listener.Wrapper(listener) @@ -268,7 +268,7 @@ public void testGenerateParseTooManySettingsInMultipleFrames() SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator()); AtomicInteger errorRef = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) @@ -283,13 +283,13 @@ public void onConnectionFailure(int error, String reason) Map settings = new HashMap<>(); settings.put(13, 17); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); for (int i = 0; i < maxSettingsKeys + 1; ++i) { - generator.generateSettings(lease, settings, false); + generator.generateSettings(accumulator, settings, false); } - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/UnknownParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/UnknownParseTest.java index bd55440be649..68336fb08cf5 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/UnknownParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/UnknownParseTest.java @@ -21,8 +21,8 @@ import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,7 +30,7 @@ public class UnknownParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testParse() @@ -48,7 +48,7 @@ public void testParseOneByteAtATime() public void testInvalidFrameSize() { AtomicInteger failure = new AtomicInteger(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter(), 4096, 8192); parser.init(listener -> new Parser.Listener.Wrapper(listener) { @Override @@ -73,7 +73,7 @@ public void onConnectionFailure(int error, String reason) private void testParse(Function fn) { AtomicBoolean failure = new AtomicBoolean(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onConnectionFailure(int error, String reason) diff --git a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/WindowUpdateGenerateParseTest.java b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/WindowUpdateGenerateParseTest.java index f87496558c98..dde8e41a4176 100644 --- a/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/WindowUpdateGenerateParseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-common/src/test/java/org/eclipse/jetty/http2/frames/WindowUpdateGenerateParseTest.java @@ -21,15 +21,15 @@ import org.eclipse.jetty.http2.internal.generator.HeaderGenerator; import org.eclipse.jetty.http2.internal.generator.WindowUpdateGenerator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class WindowUpdateGenerateParseTest { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testGenerateParse() throws Exception @@ -37,7 +37,7 @@ public void testGenerateParse() throws Exception WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onWindowUpdate(WindowUpdateFrame frame) @@ -53,11 +53,11 @@ public void onWindowUpdate(WindowUpdateFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateWindowUpdate(lease, streamId, windowUpdate); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateWindowUpdate(accumulator, streamId, windowUpdate); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { @@ -78,7 +78,7 @@ public void testGenerateParseOneByteAtATime() throws Exception WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator()); final List frames = new ArrayList<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onWindowUpdate(WindowUpdateFrame frame) @@ -94,11 +94,11 @@ public void onWindowUpdate(WindowUpdateFrame frame) // Iterate a few times to be sure generator and parser are properly reset. for (int i = 0; i < 2; ++i) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.generateWindowUpdate(lease, streamId, windowUpdate); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.generateWindowUpdate(accumulator, streamId, windowUpdate); frames.clear(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { while (buffer.hasRemaining()) { diff --git a/jetty-core/jetty-http2/jetty-http2-server/src/main/java/org/eclipse/jetty/http2/server/AbstractHTTP2ServerConnectionFactory.java b/jetty-core/jetty-http2/jetty-http2-server/src/main/java/org/eclipse/jetty/http2/server/AbstractHTTP2ServerConnectionFactory.java index b49b9cccaedf..b85935bc9eee 100644 --- a/jetty-core/jetty-http2/jetty-http2-server/src/main/java/org/eclipse/jetty/http2/server/AbstractHTTP2ServerConnectionFactory.java +++ b/jetty-core/jetty-http2/jetty-http2-server/src/main/java/org/eclipse/jetty/http2/server/AbstractHTTP2ServerConnectionFactory.java @@ -271,7 +271,7 @@ public Connection newConnection(Connector connector, EndPoint endPoint) { ServerSessionListener listener = newSessionListener(connector, endPoint); - Generator generator = new Generator(connector.getByteBufferPool(), isUseOutputDirectByteBuffers(), getMaxDynamicTableSize(), getMaxHeaderBlockFragment()); + Generator generator = new Generator(connector.getRetainableByteBufferPool(), isUseOutputDirectByteBuffers(), getMaxDynamicTableSize(), getMaxHeaderBlockFragment()); FlowControlStrategy flowControl = getFlowControlStrategyFactory().newFlowControlStrategy(); HTTP2ServerSession session = new HTTP2ServerSession(connector.getScheduler(), endPoint, generator, listener, flowControl); session.setMaxLocalStreams(getMaxConcurrentStreams()); @@ -291,7 +291,7 @@ public Connection newConnection(Connector connector, EndPoint endPoint) parser.setMaxFrameLength(getMaxFrameLength()); parser.setMaxSettingsKeys(getMaxSettingsKeys()); - RetainableByteBufferPool retainableByteBufferPool = connector.getByteBufferPool().asRetainableByteBufferPool(); + RetainableByteBufferPool retainableByteBufferPool = connector.getRetainableByteBufferPool(); HTTP2Connection connection = new HTTP2ServerConnection(retainableByteBufferPool, connector, endPoint, httpConfiguration, parser, session, getInputBufferSize(), listener); @@ -305,7 +305,7 @@ public Connection newConnection(Connector connector, EndPoint endPoint) private ServerParser newServerParser(Connector connector, ServerParser.Listener listener, RateControl rateControl) { - return new ServerParser(connector.getByteBufferPool(), listener, getMaxDynamicTableSize(), getHttpConfiguration().getRequestHeaderSize(), rateControl); + return new ServerParser(connector.getRetainableByteBufferPool(), listener, getMaxDynamicTableSize(), getHttpConfiguration().getRequestHeaderSize(), rateControl); } @ManagedObject("The container of HTTP/2 sessions") diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/AbstractServerTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/AbstractServerTest.java index aa6fd997ee3e..cbf4b859cbba 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/AbstractServerTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/AbstractServerTest.java @@ -29,8 +29,8 @@ import org.eclipse.jetty.http2.internal.parser.Parser; import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; import org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; @@ -42,7 +42,7 @@ public class AbstractServerTest { protected ServerConnector connector; - protected ByteBufferPool byteBufferPool; + protected RetainableByteBufferPool bufferPool; protected Generator generator; protected Server server; protected String path; @@ -68,8 +68,8 @@ private void prepareServer(ConnectionFactory connectionFactory) connector = new ServerConnector(server, connectionFactory); server.addConnector(connector); path = "/test"; - byteBufferPool = new MappedByteBufferPool(); - generator = new Generator(byteBufferPool); + bufferPool = new ArrayRetainableByteBufferPool(); + generator = new Generator(bufferPool); } protected MetaData.Request newRequest(String method, HttpFields fields) diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/BadURITest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/BadURITest.java index 0607b266261b..6b367df4670f 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/BadURITest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/BadURITest.java @@ -31,7 +31,7 @@ import org.eclipse.jetty.http2.frames.SettingsFrame; import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.Request; @@ -97,8 +97,8 @@ public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable } }); - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - Generator generator = new Generator(byteBufferPool); + RetainableByteBufferPool bufferPool = connector.getRetainableByteBufferPool(); + Generator generator = new Generator(bufferPool); // Craft a request with a bad URI, it will not hit the Handler. MetaData.Request metaData1 = new MetaData.Request( @@ -111,15 +111,15 @@ public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable HttpFields.EMPTY, -1 ); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); - generator.control(lease, new HeadersFrame(1, metaData1, null, true)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); + generator.control(accumulator, new HeadersFrame(1, metaData1, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } @@ -128,7 +128,7 @@ public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable Thread.sleep(1000); // Send a second request and verify that it hits the Handler. - lease.recycle(); + accumulator.release(); MetaData.Request metaData2 = new MetaData.Request( HttpMethod.GET.asString(), HttpScheme.HTTP.asString(), @@ -138,8 +138,8 @@ public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable HttpFields.EMPTY, -1 ); - generator.control(lease, new HeadersFrame(3, metaData2, null, true)); - for (ByteBuffer buffer : lease.getByteBuffers()) + generator.control(accumulator, new HeadersFrame(3, metaData2, null, true)); + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/CloseTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/CloseTest.java index f82498cf7f02..9df1de70a216 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/CloseTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/CloseTest.java @@ -37,7 +37,7 @@ import org.eclipse.jetty.http2.internal.ErrorCode; import org.eclipse.jetty.http2.internal.HTTP2Session; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; @@ -74,21 +74,21 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -135,17 +135,17 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); - generator.control(lease, new GoAwayFrame(1, ErrorCode.NO_ERROR.code, "OK".getBytes(StandardCharsets.UTF_8))); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new GoAwayFrame(1, ErrorCode.NO_ERROR.code, "OK".getBytes(StandardCharsets.UTF_8))); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } @@ -153,7 +153,7 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) // Don't close the connection; the server should close. final CountDownLatch responseLatch = new CountDownLatch(1); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -202,23 +202,23 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) }); connector.setIdleTimeout(idleTimeout); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } final CountDownLatch responseLatch = new CountDownLatch(1); final CountDownLatch closeLatch = new CountDownLatch(1); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/DataDemandTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/DataDemandTest.java index 2d3e98b95263..6ca1396465f2 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/DataDemandTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/DataDemandTest.java @@ -35,8 +35,8 @@ import org.eclipse.jetty.http2.frames.HeadersFrame; import org.eclipse.jetty.http2.internal.HTTP2Session; import org.eclipse.jetty.http2.internal.generator.Generator; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FuturePromise; import org.eclipse.jetty.util.Promise; @@ -365,17 +365,17 @@ public void onHeaders(Stream stream, HeadersFrame frame) // Generate a lot of small DATA frames and write them in a single // write so that the server will continuously be notified and demand, // which will test that it won't throw StackOverflowError. - MappedByteBufferPool byteBufferPool = new MappedByteBufferPool(); - Generator generator = new Generator(byteBufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + Generator generator = new Generator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); for (int i = 512; i >= 0; --i) - generator.data(lease, new DataFrame(clientStream.getId(), ByteBuffer.allocate(1), i == 0), 1); + generator.data(accumulator, new DataFrame(clientStream.getId(), ByteBuffer.allocate(1), i == 0), 1); // Since this is a naked write, we need to wait that the // client finishes writing the SETTINGS reply to the server // during connection initialization, or we risk a WritePendingException. Thread.sleep(1000); - ((HTTP2Session)clientStream.getSession()).getEndPoint().write(Callback.NOOP, lease.getByteBuffers().toArray(new ByteBuffer[0])); + ((HTTP2Session)clientStream.getSession()).getEndPoint().write(Callback.NOOP, accumulator.getByteBuffers().toArray(ByteBuffer[]::new)); assertTrue(latch.await(15, TimeUnit.SECONDS)); } diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/FlowControlStrategyTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/FlowControlStrategyTest.java index b23bbb3c8506..89e53de720be 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/FlowControlStrategyTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/FlowControlStrategyTest.java @@ -51,7 +51,7 @@ import org.eclipse.jetty.http2.internal.HTTP2Session; import org.eclipse.jetty.http2.internal.HTTP2Stream; import org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; @@ -844,10 +844,10 @@ public void succeeded() // Now the client is supposed to not send more frames. // If it does, the connection must be closed. HTTP2Session http2Session = (HTTP2Session)session; - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(connector.getByteBufferPool()); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(connector.getRetainableByteBufferPool()); ByteBuffer extraData = ByteBuffer.allocate(1024); - http2Session.getGenerator().data(lease, new DataFrame(stream.getId(), extraData, true), extraData.remaining()); - List buffers = lease.getByteBuffers(); + http2Session.getGenerator().data(accumulator, new DataFrame(stream.getId(), extraData, true), extraData.remaining()); + List buffers = accumulator.getByteBuffers(); http2Session.getEndPoint().write(Callback.NOOP, buffers.toArray(new ByteBuffer[0])); // Expect the connection to be closed. @@ -949,10 +949,10 @@ public void succeeded() // Now the client is supposed to not send more frames. // If it does, the connection must be closed. HTTP2Session http2Session = (HTTP2Session)session; - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(connector.getByteBufferPool()); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(connector.getRetainableByteBufferPool()); ByteBuffer extraData = ByteBuffer.allocate(1024); - http2Session.getGenerator().data(lease, new DataFrame(stream.getId(), extraData, true), extraData.remaining()); - List buffers = lease.getByteBuffers(); + http2Session.getGenerator().data(accumulator, new DataFrame(stream.getId(), extraData, true), extraData.remaining()); + List buffers = accumulator.getByteBuffers(); http2Session.getEndPoint().write(Callback.NOOP, buffers.toArray(new ByteBuffer[0])); // Expect the connection to be closed. diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2CServerTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2CServerTest.java index ae23a8b12e7e..17530aa56584 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2CServerTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2CServerTest.java @@ -38,10 +38,10 @@ import org.eclipse.jetty.http2.frames.SettingsFrame; import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.Parser; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.ServerConnector; @@ -150,13 +150,13 @@ public void testHTTP11Upgrade() throws Exception assertTrue(upgrade.toString().startsWith("HTTP/1.1 101 ")); - byteBufferPool = new MappedByteBufferPool(); - generator = new Generator(byteBufferPool); + bufferPool = new ArrayRetainableByteBufferPool(); + generator = new Generator(bufferPool); final AtomicReference headersRef = new AtomicReference<>(); final AtomicReference dataRef = new AtomicReference<>(); final AtomicReference latchRef = new AtomicReference<>(new CountDownLatch(2)); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) @@ -196,12 +196,12 @@ public void onData(DataFrame frame) headersRef.set(null); dataRef.set(null); latchRef.set(new CountDownLatch(2)); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP.asString(), new HostPortHttpField("localhost:" + connector.getLocalPort()), "/two", HttpVersion.HTTP_2, HttpFields.EMPTY, -1); - generator.control(lease, new HeadersFrame(3, metaData, null, true)); - for (ByteBuffer buffer : lease.getByteBuffers()) + generator.control(accumulator, new HeadersFrame(3, metaData, null, true)); + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } @@ -231,28 +231,28 @@ public void testHTTP20Direct() throws Exception { final CountDownLatch latch = new CountDownLatch(3); - byteBufferPool = new MappedByteBufferPool(); - generator = new Generator(byteBufferPool); + bufferPool = new ArrayRetainableByteBufferPool(); + generator = new Generator(bufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP.asString(), new HostPortHttpField("localhost:" + connector.getLocalPort()), "/test", HttpVersion.HTTP_2, HttpFields.EMPTY, -1); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { client.setSoTimeout(5000); OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } final AtomicReference headersRef = new AtomicReference<>(); final AtomicReference dataRef = new AtomicReference<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -328,18 +328,18 @@ public void onFillable() // Now send an HTTP/2 direct request, which // will have the PRI * HTTP/2.0 preface. - byteBufferPool = new MappedByteBufferPool(); - generator = new Generator(byteBufferPool); + bufferPool = new ArrayRetainableByteBufferPool(); + generator = new Generator(bufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); try (Socket client = new Socket("localhost", connector.getLocalPort())) { client.setSoTimeout(5000); OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2ServerTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2ServerTest.java index 106d8f7d3dd1..333766797a5f 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2ServerTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HTTP2ServerTest.java @@ -47,8 +47,9 @@ import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.Parser; import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ManagedSelector; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SocketChannelEndPoint; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.Handler; @@ -84,19 +85,19 @@ public boolean process(Request request, Response response, Callback callback) // No preface bytes. MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - final CountDownLatch latch = new CountDownLatch(1); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + CountDownLatch latch = new CountDownLatch(1); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onGoAway(GoAwayFrame frame) @@ -115,7 +116,7 @@ public void onGoAway(GoAwayFrame frame) @Test public void testRequestResponseNoContent() throws Exception { - final CountDownLatch latch = new CountDownLatch(3); + CountDownLatch latch = new CountDownLatch(3); startServer(new Handler.Abstract() { @Override @@ -127,22 +128,22 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - final AtomicReference frameRef = new AtomicReference<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + AtomicReference frameRef = new AtomicReference<>(); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -173,8 +174,8 @@ public void onHeaders(HeadersFrame frame) @Test public void testRequestResponseContent() throws Exception { - final byte[] content = "Hello, world!".getBytes(StandardCharsets.UTF_8); - final CountDownLatch latch = new CountDownLatch(4); + byte[] content = "Hello, world!".getBytes(StandardCharsets.UTF_8); + CountDownLatch latch = new CountDownLatch(4); startServer(new Handler.Abstract() { @Override @@ -186,23 +187,23 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - final AtomicReference headersRef = new AtomicReference<>(); - final AtomicReference dataRef = new AtomicReference<>(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + AtomicReference headersRef = new AtomicReference<>(); + AtomicReference dataRef = new AtomicReference<>(); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -254,23 +255,23 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); - generator.control(lease, new PingFrame(new byte[8], false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); + generator.control(accumulator, new PingFrame(new byte[8], false)); // Modify the length of the frame to a wrong one. - lease.getByteBuffers().get(2).putShort(0, (short)7); + accumulator.getByteBuffers().get(2).putShort(0, (short)7); - final CountDownLatch latch = new CountDownLatch(1); + CountDownLatch latch = new CountDownLatch(1); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onGoAway(GoAwayFrame frame) @@ -300,23 +301,23 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); - generator.control(lease, new PingFrame(new byte[8], false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); + generator.control(accumulator, new PingFrame(new byte[8], false)); // Modify the streamId of the frame to non zero. - lease.getByteBuffers().get(2).putInt(4, 1); + accumulator.getByteBuffers().get(2).putInt(4, 1); - final CountDownLatch latch = new CountDownLatch(1); + CountDownLatch latch = new CountDownLatch(1); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onGoAway(GoAwayFrame frame) @@ -336,8 +337,8 @@ public void onGoAway(GoAwayFrame frame) @Test public void testCommitFailure() throws Exception { - final long delay = 1000; - final AtomicBoolean broken = new AtomicBoolean(); + long delay = 1000; + AtomicBoolean broken = new AtomicBoolean(); startServer(new Handler.Abstract() { @Override @@ -373,22 +374,22 @@ public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateE server.addConnector(connector2); server.start(); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector2.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } // The server will close the connection abruptly since it // cannot write and therefore cannot even send the GO_AWAY. - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter(), 4096, 8192); parser.init(UnaryOperator.identity()); boolean closed = parseResponse(client, parser, 2 * delay); assertTrue(closed); @@ -413,22 +414,22 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } output.flush(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter(), 4096, 8192); parser.init(UnaryOperator.identity()); boolean closed = parseResponse(client, parser); @@ -442,12 +443,12 @@ public void testRequestWithContinuationFrames() throws Exception { testRequestWithContinuationFrames(null, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); - return lease; + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); + return accumulator; }); } @@ -457,12 +458,12 @@ public void testRequestWithPriorityWithContinuationFrames() throws Exception PriorityFrame priority = new PriorityFrame(1, 13, 200, true); testRequestWithContinuationFrames(priority, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, priority, true)); - return lease; + generator.control(accumulator, new HeadersFrame(1, metaData, priority, true)); + return accumulator; }); } @@ -471,19 +472,19 @@ public void testRequestWithContinuationFramesWithEmptyHeadersFrame() throws Exce { testRequestWithContinuationFrames(null, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); // Take the HeadersFrame header and set the length to zero. - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); ByteBuffer headersFrameHeader = buffers.get(2); headersFrameHeader.put(0, (byte)0); headersFrameHeader.putShort(1, (short)0); // Insert a CONTINUATION frame header for the body of the HEADERS frame. - lease.insert(3, buffers.get(4).slice(), false); - return lease; + accumulator.insert(3, RetainableByteBuffer.asNonRetainable(buffers.get(4).slice())); + return accumulator; }); } @@ -493,19 +494,19 @@ public void testRequestWithPriorityWithContinuationFramesWithEmptyHeadersFrame() PriorityFrame priority = new PriorityFrame(1, 13, 200, true); testRequestWithContinuationFrames(null, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, priority, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, priority, true)); // Take the HeadersFrame header and set the length to just the priority frame. - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); ByteBuffer headersFrameHeader = buffers.get(2); headersFrameHeader.put(0, (byte)0); headersFrameHeader.putShort(1, (short)PriorityFrame.PRIORITY_LENGTH); // Insert a CONTINUATION frame header for the body of the HEADERS frame. - lease.insert(3, buffers.get(4).slice(), false); - return lease; + accumulator.insert(3, RetainableByteBuffer.asNonRetainable(buffers.get(4).slice())); + return accumulator; }); } @@ -514,13 +515,13 @@ public void testRequestWithContinuationFramesWithEmptyContinuationFrame() throws { testRequestWithContinuationFrames(null, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); // Take the ContinuationFrame header, duplicate it, and set the length to zero. - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); ByteBuffer continuationFrameHeader = buffers.get(4); ByteBuffer duplicate = ByteBuffer.allocate(continuationFrameHeader.remaining()); duplicate.put(continuationFrameHeader).flip(); @@ -528,8 +529,8 @@ public void testRequestWithContinuationFramesWithEmptyContinuationFrame() throws continuationFrameHeader.put(0, (byte)0); continuationFrameHeader.putShort(1, (short)0); // Insert a CONTINUATION frame header for the body of the previous CONTINUATION frame. - lease.insert(5, duplicate, false); - return lease; + accumulator.insert(5, RetainableByteBuffer.asNonRetainable(duplicate)); + return accumulator; }); } @@ -538,13 +539,13 @@ public void testRequestWithContinuationFramesWithEmptyLastContinuationFrame() th { testRequestWithContinuationFrames(null, () -> { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); MetaData.Request metaData = newRequest("GET", HttpFields.EMPTY); - generator.control(lease, new HeadersFrame(1, metaData, null, true)); + generator.control(accumulator, new HeadersFrame(1, metaData, null, true)); // Take the last CONTINUATION frame and reset the flag. - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); ByteBuffer continuationFrameHeader = buffers.get(buffers.size() - 2); continuationFrameHeader.put(4, (byte)0); // Add a last, empty, CONTINUATION frame. @@ -554,14 +555,14 @@ public void testRequestWithContinuationFramesWithEmptyLastContinuationFrame() th (byte)Flags.END_HEADERS, 0, 0, 0, 1 // Stream ID }); - lease.append(last, false); - return lease; + accumulator.append(RetainableByteBuffer.asNonRetainable(last)); + return accumulator; }); } - private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable frames) throws Exception + private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable frames) throws Exception { - final CountDownLatch serverLatch = new CountDownLatch(1); + CountDownLatch serverLatch = new CountDownLatch(1); startServer(new ServerSessionListener() { @Override @@ -585,14 +586,14 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) return null; } }); - generator = new Generator(byteBufferPool, 4096, 4); + generator = new Generator(bufferPool, 4096, 4); - ByteBufferPool.Lease lease = frames.call(); + RetainableByteBufferPool.Accumulator accumulator = frames.call(); try (Socket client = new Socket("localhost", connector.getLocalPort())) { OutputStream output = client.getOutputStream(); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } @@ -600,8 +601,8 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) assertTrue(serverLatch.await(5, TimeUnit.SECONDS)); - final CountDownLatch clientLatch = new CountDownLatch(1); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + CountDownLatch clientLatch = new CountDownLatch(1); + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onHeaders(HeadersFrame frame) diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java index 20ab97593017..254c3c44ec01 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java @@ -67,10 +67,10 @@ import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.ServerParser; import org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.Request; @@ -111,7 +111,7 @@ public void testPropertiesAreForwarded() throws Exception assertTrue(http2Client.isStarted()); assertSame(httpClient.getExecutor(), http2Client.getExecutor()); assertSame(httpClient.getScheduler(), http2Client.getScheduler()); - assertSame(httpClient.getByteBufferPool(), http2Client.getByteBufferPool()); + assertSame(httpClient.getRetainableByteBufferPool(), http2Client.getRetainableByteBufferPool()); assertEquals(httpClient.getConnectTimeout(), http2Client.getConnectTimeout()); assertEquals(httpClient.getIdleTimeout(), http2Client.getIdleTimeout()); assertEquals(httpClient.isUseInputDirectByteBuffers(), http2Client.isUseInputDirectByteBuffers()); @@ -541,9 +541,9 @@ protected Connection newConnection(Destination destination, Session session) resultLatch.countDown(); }); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - Generator generator = new Generator(byteBufferPool); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + Generator generator = new Generator(bufferPool); try (Socket socket = server.accept()) { @@ -551,7 +551,7 @@ protected Connection newConnection(Destination destination, Session session) OutputStream output = socket.getOutputStream(); InputStream input = socket.getInputStream(); - ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() + ServerParser parser = new ServerParser(bufferPool, new ServerParser.Listener.Adapter() { @Override public void onPreface() @@ -559,9 +559,9 @@ public void onPreface() try { // Server's preface. - generator.control(lease, new SettingsFrame(new HashMap<>(), false)); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), false)); // Reply to client's SETTINGS. - generator.control(lease, new SettingsFrame(new HashMap<>(), true)); + generator.control(accumulator, new SettingsFrame(new HashMap<>(), true)); writeFrames(); } catch (HpackException x) @@ -578,7 +578,7 @@ public void onHeaders(HeadersFrame request) // Response. MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, HttpFields.EMPTY); HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true); - generator.control(lease, response); + generator.control(accumulator, response); writeFrames(); } catch (HpackException x) @@ -592,11 +592,11 @@ private void writeFrames() try { // Write the frames. - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { output.write(BufferUtil.toArray(buffer)); } - lease.recycle(); + accumulator.release(); } catch (Throwable x) { diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/PrefaceTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/PrefaceTest.java index 306b634d46bd..a167c73cad34 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/PrefaceTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/PrefaceTest.java @@ -50,9 +50,9 @@ import org.eclipse.jetty.http2.internal.generator.Generator; import org.eclipse.jetty.http2.internal.parser.Parser; import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.EndPoint; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -148,26 +148,26 @@ public void onPing(Session session, PingFrame frame) } }); - ByteBufferPool byteBufferPool = http2Client.getByteBufferPool(); + RetainableByteBufferPool bufferPool = http2Client.getRetainableByteBufferPool(); try (SocketChannel socket = SocketChannel.open()) { socket.connect(new InetSocketAddress("localhost", connector.getLocalPort())); - Generator generator = new Generator(byteBufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); + Generator generator = new Generator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); Map clientSettings = new HashMap<>(); clientSettings.put(SettingsFrame.ENABLE_PUSH, 0); - generator.control(lease, new SettingsFrame(clientSettings, false)); + generator.control(accumulator, new SettingsFrame(clientSettings, false)); // The PING frame just to make sure the client stops reading. - generator.control(lease, new PingFrame(true)); + generator.control(accumulator, new PingFrame(true)); - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); Queue settings = new ArrayDeque<>(); AtomicBoolean closed = new AtomicBoolean(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) @@ -183,7 +183,7 @@ public void onGoAway(GoAwayFrame frame) }, 4096, 8192); parser.init(UnaryOperator.identity()); - ByteBuffer buffer = byteBufferPool.acquire(1024, true); + ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { BufferUtil.clearToFill(buffer); @@ -247,7 +247,7 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) }); server.start(); - ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); try (SocketChannel socket = SocketChannel.open()) { socket.connect(new InetSocketAddress("localhost", connector.getLocalPort())); @@ -268,7 +268,7 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) assertTrue(serverSettingsLatch.get().await(5, TimeUnit.SECONDS)); // The 101 response is the reply to the client preface SETTINGS frame. - ByteBuffer buffer = byteBufferPool.acquire(1024, true); + ByteBuffer buffer = ByteBuffer.allocateDirect(1024); http1: while (true) { @@ -295,13 +295,13 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) serverSettingsLatch.set(new CountDownLatch(1)); // After the 101, the client must send the connection preface. - Generator generator = new Generator(byteBufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); + Generator generator = new Generator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); Map clientSettings = new HashMap<>(); clientSettings.put(SettingsFrame.ENABLE_PUSH, 1); - generator.control(lease, new SettingsFrame(clientSettings, false)); - List buffers = lease.getByteBuffers(); + generator.control(accumulator, new SettingsFrame(clientSettings, false)); + List buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); // However, we should not call onPreface() again. @@ -311,7 +311,7 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) CountDownLatch clientSettingsLatch = new CountDownLatch(1); AtomicBoolean responded = new AtomicBoolean(); - Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() + Parser parser = new Parser(bufferPool, new Parser.Listener.Adapter() { @Override public void onSettings(SettingsFrame frame) diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamCountTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamCountTest.java index 82e5220abf2e..0202d85b8b97 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamCountTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamCountTest.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.http2.frames.SettingsFrame; import org.eclipse.jetty.http2.internal.HTTP2Session; import org.eclipse.jetty.http2.internal.generator.Generator; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FuturePromise; @@ -201,10 +201,10 @@ public void onReset(Stream stream, ResetFrame frame, Callback callback) HeadersFrame frame3 = new HeadersFrame(streamId3, metaData, null, false); DataFrame data3 = new DataFrame(streamId3, BufferUtil.EMPTY_BUFFER, true); Generator generator = ((HTTP2Session)session).getGenerator(); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(generator.getByteBufferPool()); - generator.control(lease, frame3); - generator.data(lease, data3, data3.remaining()); - ((HTTP2Session)session).getEndPoint().write(Callback.NOOP, lease.getByteBuffers().toArray(new ByteBuffer[0])); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(generator.getRetainableByteBufferPool()); + generator.control(accumulator, frame3); + generator.data(accumulator, data3, data3.remaining()); + ((HTTP2Session)session).getEndPoint().write(Callback.NOOP, accumulator.getByteBuffers().toArray(ByteBuffer[]::new)); // Expect 2 RST_STREAM frames. assertTrue(sessionResetLatch.await(5, TimeUnit.SECONDS)); diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamResetTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamResetTest.java index 38b22b006efc..f6088ef293cf 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamResetTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/StreamResetTest.java @@ -64,8 +64,8 @@ import org.eclipse.jetty.http2.server.AbstractHTTP2ServerConnectionFactory; import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; import org.eclipse.jetty.io.AbstractEndPoint; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.WriteFlusher; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.Handler; @@ -887,30 +887,30 @@ public boolean process(Request request, Response response, Callback callback) } }); - ByteBufferPool byteBufferPool = http2Client.getByteBufferPool(); + RetainableByteBufferPool bufferPool = http2Client.getRetainableByteBufferPool(); try (SocketChannel socket = SocketChannel.open()) { String host = "localhost"; int port = connector.getLocalPort(); socket.connect(new InetSocketAddress(host, port)); - Generator generator = new Generator(byteBufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); + Generator generator = new Generator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); Map clientSettings = new HashMap<>(); // Max stream HTTP/2 flow control window. clientSettings.put(SettingsFrame.INITIAL_WINDOW_SIZE, Integer.MAX_VALUE); - generator.control(lease, new SettingsFrame(clientSettings, false)); + generator.control(accumulator, new SettingsFrame(clientSettings, false)); // Max session HTTP/2 flow control window. - generator.control(lease, new WindowUpdateFrame(0, Integer.MAX_VALUE - FlowControlStrategy.DEFAULT_WINDOW_SIZE)); + generator.control(accumulator, new WindowUpdateFrame(0, Integer.MAX_VALUE - FlowControlStrategy.DEFAULT_WINDOW_SIZE)); HttpURI uri = HttpURI.from("http", host, port, "/"); MetaData.Request request = new MetaData.Request(HttpMethod.GET.asString(), uri, HttpVersion.HTTP_2, HttpFields.EMPTY); int streamId = 3; HeadersFrame headersFrame = new HeadersFrame(streamId, request, null, true); - generator.control(lease, headersFrame); + generator.control(accumulator, headersFrame); - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); // Wait until the server is TCP congested. @@ -918,9 +918,9 @@ public boolean process(Request request, Response response, Callback callback) WriteFlusher flusher = flusherRef.get(); waitUntilTCPCongested(flusher); - lease.recycle(); - generator.control(lease, new ResetFrame(streamId, ErrorCode.CANCEL_STREAM_ERROR.code)); - buffers = lease.getByteBuffers(); + accumulator.release(); + generator.control(accumulator, new ResetFrame(streamId, ErrorCode.CANCEL_STREAM_ERROR.code)); + buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); assertTrue(writeLatch1.await(5, TimeUnit.SECONDS)); @@ -978,29 +978,29 @@ private void service2(Response response, Callback callback) throws Exception } }); - ByteBufferPool byteBufferPool = http2Client.getByteBufferPool(); + RetainableByteBufferPool bufferPool = http2Client.getRetainableByteBufferPool(); try (SocketChannel socket = SocketChannel.open()) { String host = "localhost"; int port = connector.getLocalPort(); socket.connect(new InetSocketAddress(host, port)); - Generator generator = new Generator(byteBufferPool); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool); - generator.control(lease, new PrefaceFrame()); + Generator generator = new Generator(bufferPool); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); + generator.control(accumulator, new PrefaceFrame()); Map clientSettings = new HashMap<>(); // Max stream HTTP/2 flow control window. clientSettings.put(SettingsFrame.INITIAL_WINDOW_SIZE, Integer.MAX_VALUE); - generator.control(lease, new SettingsFrame(clientSettings, false)); + generator.control(accumulator, new SettingsFrame(clientSettings, false)); // Max session HTTP/2 flow control window. - generator.control(lease, new WindowUpdateFrame(0, Integer.MAX_VALUE - FlowControlStrategy.DEFAULT_WINDOW_SIZE)); + generator.control(accumulator, new WindowUpdateFrame(0, Integer.MAX_VALUE - FlowControlStrategy.DEFAULT_WINDOW_SIZE)); HttpURI uri = HttpURI.from("http", host, port, "/1"); MetaData.Request request = new MetaData.Request(HttpMethod.GET.asString(), uri, HttpVersion.HTTP_2, HttpFields.EMPTY); HeadersFrame headersFrame = new HeadersFrame(3, request, null, true); - generator.control(lease, headersFrame); + generator.control(accumulator, headersFrame); - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); waitUntilTCPCongested(exchanger.exchange(null)); @@ -1010,15 +1010,15 @@ private void service2(Response response, Callback callback) throws Exception request = new MetaData.Request(HttpMethod.GET.asString(), uri, HttpVersion.HTTP_2, HttpFields.EMPTY); int streamId = 5; headersFrame = new HeadersFrame(streamId, request, null, true); - generator.control(lease, headersFrame); - buffers = lease.getByteBuffers(); + generator.control(accumulator, headersFrame); + buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); assertTrue(requestLatch1.await(5, TimeUnit.SECONDS)); // Now reset the second request, which has not started writing yet. - lease.recycle(); - generator.control(lease, new ResetFrame(streamId, ErrorCode.CANCEL_STREAM_ERROR.code)); - buffers = lease.getByteBuffers(); + accumulator.release(); + generator.control(accumulator, new ResetFrame(streamId, ErrorCode.CANCEL_STREAM_ERROR.code)); + buffers = accumulator.getByteBuffers(); socket.write(buffers.toArray(new ByteBuffer[0])); // Wait to be sure that the server processed the reset. Thread.sleep(1000); diff --git a/jetty-core/jetty-http3/jetty-http3-client-transport/src/main/java/org/eclipse/jetty/http3/client/transport/HttpClientTransportOverHTTP3.java b/jetty-core/jetty-http3/jetty-http3-client-transport/src/main/java/org/eclipse/jetty/http3/client/transport/HttpClientTransportOverHTTP3.java index ccf4346e4abf..54487f5c0732 100644 --- a/jetty-core/jetty-http3/jetty-http3-client-transport/src/main/java/org/eclipse/jetty/http3/client/transport/HttpClientTransportOverHTTP3.java +++ b/jetty-core/jetty-http3/jetty-http3-client-transport/src/main/java/org/eclipse/jetty/http3/client/transport/HttpClientTransportOverHTTP3.java @@ -69,7 +69,7 @@ protected void doStart() throws Exception ClientConnector clientConnector = this.client.getClientConnector(); clientConnector.setExecutor(httpClient.getExecutor()); clientConnector.setScheduler(httpClient.getScheduler()); - clientConnector.setByteBufferPool(httpClient.getByteBufferPool()); + clientConnector.setRetainableByteBufferPool(httpClient.getRetainableByteBufferPool()); clientConnector.setConnectTimeout(Duration.ofMillis(httpClient.getConnectTimeout())); clientConnector.setConnectBlocking(httpClient.isConnectBlocking()); clientConnector.setBindAddress(httpClient.getBindAddress()); diff --git a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3Session.java b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3Session.java index 5fd2d2a58892..62cddef7a60f 100644 --- a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3Session.java +++ b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3Session.java @@ -83,7 +83,7 @@ public ClientHTTP3Session(HTTP3Configuration configuration, ClientQuicSession qu if (LOG.isDebugEnabled()) LOG.debug("created control stream #{} on {}", controlStreamId, controlEndPoint); - this.messageFlusher = new MessageFlusher(quicSession.getByteBufferPool(), encoder, configuration.getMaxRequestHeadersSize(), configuration.isUseOutputDirectByteBuffers()); + this.messageFlusher = new MessageFlusher(quicSession.getRetainableByteBufferPool(), encoder, configuration.getMaxRequestHeadersSize(), configuration.isUseOutputDirectByteBuffers()); addBean(messageFlusher); } @@ -197,7 +197,7 @@ protected void onClose(long error, String reason) private void openUnidirectionalStreamEndPoint(QuicStreamEndPoint endPoint) { - UnidirectionalStreamConnection connection = new UnidirectionalStreamConnection(endPoint, getQuicSession().getExecutor(), getQuicSession().getByteBufferPool(), encoder, decoder, session); + UnidirectionalStreamConnection connection = new UnidirectionalStreamConnection(endPoint, getQuicSession().getExecutor(), getQuicSession().getRetainableByteBufferPool(), encoder, decoder, session); endPoint.setConnection(connection); endPoint.opened(); } diff --git a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3StreamConnection.java b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3StreamConnection.java index b225dd79ff43..15cdbf200339 100644 --- a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3StreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/internal/ClientHTTP3StreamConnection.java @@ -21,6 +21,6 @@ public class ClientHTTP3StreamConnection extends HTTP3StreamConnection { public ClientHTTP3StreamConnection(QuicStreamEndPoint endPoint, ClientHTTP3Session session, MessageParser parser) { - super(endPoint, session.getQuicSession().getExecutor(), session.getQuicSession().getByteBufferPool(), parser); + super(endPoint, session.getQuicSession().getExecutor(), session.getQuicSession().getRetainableByteBufferPool(), parser); } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlFlusher.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlFlusher.java index f75ab50316f8..34af71b6652e 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlFlusher.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlFlusher.java @@ -21,7 +21,8 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.internal.generator.ControlGenerator; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicSession; import org.eclipse.jetty.quic.common.QuicStreamEndPoint; import org.eclipse.jetty.util.Callback; @@ -37,7 +38,7 @@ public class ControlFlusher extends IteratingCallback private final AutoLock lock = new AutoLock(); private final Queue queue = new ArrayDeque<>(); - private final ByteBufferPool.Lease lease; + private final RetainableByteBufferPool.Accumulator accumulator; private final ControlGenerator generator; private final QuicStreamEndPoint endPoint; private boolean initialized; @@ -47,7 +48,7 @@ public class ControlFlusher extends IteratingCallback public ControlFlusher(QuicSession session, QuicStreamEndPoint endPoint, boolean useDirectByteBuffers) { - this.lease = new ByteBufferPool.Lease(session.getByteBufferPool()); + this.accumulator = new RetainableByteBufferPool.Accumulator(session.getRetainableByteBufferPool()); this.endPoint = endPoint; this.generator = new ControlGenerator(useDirectByteBuffers); } @@ -55,7 +56,7 @@ public ControlFlusher(QuicSession session, QuicStreamEndPoint endPoint, boolean public boolean offer(Frame frame, Callback callback) { Throwable closed; - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { closed = terminated; if (closed == null) @@ -70,7 +71,7 @@ public boolean offer(Frame frame, Callback callback) @Override protected Action process() { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { if (queue.isEmpty()) return Action.IDLE; @@ -83,7 +84,7 @@ protected Action process() for (Entry entry : entries) { - generator.generate(lease, endPoint.getStreamId(), entry.frame, null); + generator.generate(accumulator, endPoint.getStreamId(), entry.frame, null); invocationType = Invocable.combine(invocationType, entry.callback.getInvocationType()); } @@ -93,12 +94,12 @@ protected Action process() ByteBuffer buffer = ByteBuffer.allocate(VarLenInt.length(ControlStreamConnection.STREAM_TYPE)); VarLenInt.encode(buffer, ControlStreamConnection.STREAM_TYPE); buffer.flip(); - lease.insert(0, buffer, false); + accumulator.insert(0, RetainableByteBuffer.asNonRetainable(buffer)); } - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); if (LOG.isDebugEnabled()) - LOG.debug("writing {} buffers ({} bytes) on {}", buffers.size(), lease.getTotalLength(), this); + LOG.debug("writing {} buffers ({} bytes) on {}", buffers.size(), accumulator.getTotalLength(), this); endPoint.write(this, buffers.toArray(ByteBuffer[]::new)); return Action.SCHEDULED; } @@ -109,7 +110,7 @@ public void succeeded() if (LOG.isDebugEnabled()) LOG.debug("succeeded to write {} on {}", entries, this); - lease.recycle(); + accumulator.release(); entries.forEach(e -> e.callback.succeeded()); entries.clear(); @@ -125,11 +126,11 @@ protected void onCompleteFailure(Throwable failure) if (LOG.isDebugEnabled()) LOG.debug("failed to write {} on {}", entries, this, failure); - lease.recycle(); + accumulator.release(); List allEntries = new ArrayList<>(entries); entries.clear(); - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { terminated = failure; allEntries.addAll(queue); @@ -157,21 +158,7 @@ public String toString() return String.format("%s#%s", super.toString(), endPoint.getStreamId()); } - private static class Entry + private record Entry(Frame frame, Callback callback) { - private final Frame frame; - private final Callback callback; - - private Entry(Frame frame, Callback callback) - { - this.frame = frame; - this.callback = callback; - } - - @Override - public String toString() - { - return frame.toString(); - } } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlStreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlStreamConnection.java index 0003d604b77c..7e0aa7baab8a 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlStreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/ControlStreamConnection.java @@ -18,9 +18,10 @@ import org.eclipse.jetty.http3.internal.parser.ControlParser; 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.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,15 +32,15 @@ public class ControlStreamConnection extends AbstractConnection implements Conne public static final long STREAM_TYPE = 0x00; private static final Logger LOG = LoggerFactory.getLogger(ControlStreamConnection.class); - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final ControlParser parser; private boolean useInputDirectByteBuffers = true; - private ByteBuffer buffer; + private RetainableByteBuffer buffer; - public ControlStreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool, ControlParser parser) + public ControlStreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, ControlParser parser) { super(endPoint, executor); - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.parser = parser; } @@ -57,17 +58,18 @@ public void setUseInputDirectByteBuffers(boolean useInputDirectByteBuffers) public void onUpgradeTo(ByteBuffer upgrade) { int capacity = Math.max(upgrade.remaining(), getInputBufferSize()); - buffer = byteBufferPool.acquire(capacity, isUseInputDirectByteBuffers()); - int position = BufferUtil.flipToFill(buffer); - buffer.put(upgrade); - BufferUtil.flipToFlush(buffer, position); + buffer = bufferPool.acquire(capacity, isUseInputDirectByteBuffers()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + int position = BufferUtil.flipToFill(byteBuffer); + byteBuffer.put(upgrade); + BufferUtil.flipToFlush(byteBuffer, position); } @Override public void onOpen() { super.onOpen(); - if (BufferUtil.hasContent(buffer)) + if (buffer != null && buffer.hasRemaining()) onFillable(); else fillInterested(); @@ -79,28 +81,28 @@ public void onFillable() try { if (buffer == null) - buffer = byteBufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); - + buffer = bufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); while (true) { // Parse first in case of bytes from the upgrade. - parser.parse(buffer); + parser.parse(byteBuffer); // Then read from the EndPoint. - int filled = getEndPoint().fill(buffer); + int filled = getEndPoint().fill(byteBuffer); if (LOG.isDebugEnabled()) LOG.debug("filled {} on {}", filled, this); if (filled == 0) { - byteBufferPool.release(buffer); + buffer.release(); buffer = null; fillInterested(); break; } else if (filled < 0) { - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(); break; @@ -111,7 +113,7 @@ else if (filled < 0) { if (LOG.isDebugEnabled()) LOG.debug("could not process control stream {}", getEndPoint(), x); - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(x); } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/DecoderStreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/DecoderStreamConnection.java index 1c58aa6b2fab..cfff5bc350be 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/DecoderStreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/DecoderStreamConnection.java @@ -18,8 +18,8 @@ import org.eclipse.jetty.http3.qpack.QpackEncoder; import org.eclipse.jetty.http3.qpack.QpackException; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class DecoderStreamConnection extends InstructionStreamConnection { @@ -28,9 +28,9 @@ public class DecoderStreamConnection extends InstructionStreamConnection private final QpackEncoder encoder; - public DecoderStreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool, QpackEncoder encoder) + public DecoderStreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, QpackEncoder encoder) { - super(endPoint, executor, byteBufferPool); + super(endPoint, executor, bufferPool); this.encoder = encoder; } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/EncoderStreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/EncoderStreamConnection.java index 98985ea7c60c..fbc29ce3f239 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/EncoderStreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/EncoderStreamConnection.java @@ -18,8 +18,8 @@ import org.eclipse.jetty.http3.qpack.QpackDecoder; import org.eclipse.jetty.http3.qpack.QpackException; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class EncoderStreamConnection extends InstructionStreamConnection { @@ -28,9 +28,9 @@ public class EncoderStreamConnection extends InstructionStreamConnection private final QpackDecoder decoder; - public EncoderStreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool, QpackDecoder decoder) + public EncoderStreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, QpackDecoder decoder) { - super(endPoint, executor, byteBufferPool); + super(endPoint, executor, bufferPool); this.decoder = decoder; } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/HTTP3StreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/HTTP3StreamConnection.java index 21766cc042a7..bafd858f8b48 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/HTTP3StreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/HTTP3StreamConnection.java @@ -27,7 +27,6 @@ import org.eclipse.jetty.http3.internal.parser.MessageParser; import org.eclipse.jetty.http3.internal.parser.ParserListener; import org.eclipse.jetty.io.AbstractConnection; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicStreamEndPoint; @@ -41,7 +40,7 @@ public abstract class HTTP3StreamConnection extends AbstractConnection private static final ByteBuffer EMPTY_DATA_FRAME = ByteBuffer.allocate(2); private final AtomicReference action = new AtomicReference<>(); - private final RetainableByteBufferPool buffers; + private final RetainableByteBufferPool bufferPool; private final MessageParser parser; private boolean useInputDirectByteBuffers = true; private HTTP3Stream stream; @@ -49,10 +48,10 @@ public abstract class HTTP3StreamConnection extends AbstractConnection private boolean applicationMode; private boolean remotelyClosed; - public HTTP3StreamConnection(QuicStreamEndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool, MessageParser parser) + public HTTP3StreamConnection(QuicStreamEndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, MessageParser parser) { super(endPoint, executor); - this.buffers = byteBufferPool.asRetainableByteBufferPool(); + this.bufferPool = bufferPool; this.parser = parser; parser.init(MessageListener::new); } @@ -239,7 +238,7 @@ private void tryAcquireBuffer() { if (networkBuffer == null) { - networkBuffer = buffers.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + networkBuffer = bufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); if (LOG.isDebugEnabled()) LOG.debug("acquired {}", networkBuffer); } @@ -270,7 +269,7 @@ private MessageParser.Result parseAndFill(boolean setFillInterest) while (true) { - ByteBuffer byteBuffer = networkBuffer.getBuffer(); + ByteBuffer byteBuffer = networkBuffer.getByteBuffer(); MessageParser.Result result = parser.parse(byteBuffer); if (LOG.isDebugEnabled()) LOG.debug("parsed {} on {} with buffer {}", result, this, networkBuffer); @@ -280,11 +279,11 @@ private MessageParser.Result parseAndFill(boolean setFillInterest) if (networkBuffer.isRetained()) { networkBuffer.release(); - RetainableByteBuffer newBuffer = buffers.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + RetainableByteBuffer newBuffer = bufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); if (LOG.isDebugEnabled()) LOG.debug("reacquired {} for retained {}", newBuffer, networkBuffer); networkBuffer = newBuffer; - byteBuffer = networkBuffer.getBuffer(); + byteBuffer = networkBuffer.getByteBuffer(); } int filled = fill(byteBuffer); diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionFlusher.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionFlusher.java index 9f746b0a5ecf..1a1cc09f7c83 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionFlusher.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionFlusher.java @@ -20,7 +20,8 @@ import java.util.Queue; import org.eclipse.jetty.http3.qpack.Instruction; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicSession; import org.eclipse.jetty.quic.common.QuicStreamEndPoint; import org.eclipse.jetty.util.IteratingCallback; @@ -38,7 +39,7 @@ public class InstructionFlusher extends IteratingCallback private final AutoLock lock = new AutoLock(); private final Queue queue = new ArrayDeque<>(); - private final ByteBufferPool.Lease lease; + private final RetainableByteBufferPool.Accumulator accumulator; private final QuicStreamEndPoint endPoint; private final long streamType; private boolean initialized; @@ -46,7 +47,7 @@ public class InstructionFlusher extends IteratingCallback public InstructionFlusher(QuicSession session, QuicStreamEndPoint endPoint, long streamType) { - this.lease = new ByteBufferPool.Lease(session.getByteBufferPool()); + this.accumulator = new RetainableByteBufferPool.Accumulator(session.getRetainableByteBufferPool()); this.endPoint = endPoint; this.streamType = streamType; } @@ -54,7 +55,7 @@ public InstructionFlusher(QuicSession session, QuicStreamEndPoint endPoint, long public boolean offer(List instructions) { Throwable closed; - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { closed = terminated; if (closed == null) @@ -67,7 +68,7 @@ public boolean offer(List instructions) protected Action process() { List instructions; - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { if (queue.isEmpty()) return Action.IDLE; @@ -78,20 +79,21 @@ protected Action process() if (LOG.isDebugEnabled()) LOG.debug("flushing {} on {}", instructions, this); - instructions.forEach(i -> i.encode(lease)); + instructions.forEach(i -> i.encode(accumulator)); if (!initialized) { initialized = true; - ByteBuffer buffer = ByteBuffer.allocate(VarLenInt.length(streamType)); - VarLenInt.encode(buffer, streamType); - buffer.flip(); - lease.insert(0, buffer, false); + RetainableByteBuffer buffer = accumulator.acquire(VarLenInt.length(streamType), false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + VarLenInt.encode(byteBuffer, streamType); + byteBuffer.flip(); + accumulator.insert(0, buffer); } - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); if (LOG.isDebugEnabled()) - LOG.debug("writing {} buffers ({} bytes) on {}", buffers.size(), lease.getTotalLength(), this); + LOG.debug("writing {} buffers ({} bytes) on {}", buffers.size(), accumulator.getTotalLength(), this); endPoint.write(this, buffers.toArray(ByteBuffer[]::new)); return Action.SCHEDULED; } @@ -100,9 +102,9 @@ protected Action process() public void succeeded() { if (LOG.isDebugEnabled()) - LOG.debug("succeeded to write {} on {}", lease.getByteBuffers(), this); + LOG.debug("succeeded to write {} buffers on {}", accumulator.getByteBuffers().size(), this); - lease.recycle(); + accumulator.release(); super.succeeded(); } @@ -111,11 +113,11 @@ public void succeeded() protected void onCompleteFailure(Throwable failure) { if (LOG.isDebugEnabled()) - LOG.debug("failed to write {} on {}", lease.getByteBuffers(), this, failure); + LOG.debug("failed to write {} buffers on {}", accumulator.getByteBuffers().size(), this, failure); - lease.recycle(); + accumulator.release(); - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { terminated = failure; queue.clear(); diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionStreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionStreamConnection.java index 24eaeed82b54..2f96607226e0 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionStreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/InstructionStreamConnection.java @@ -18,9 +18,10 @@ import org.eclipse.jetty.http3.qpack.QpackException; 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.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,14 +29,14 @@ public abstract class InstructionStreamConnection extends AbstractConnection implements Connection.UpgradeTo { private static final Logger LOG = LoggerFactory.getLogger(InstructionStreamConnection.class); - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private boolean useInputDirectByteBuffers = true; - private ByteBuffer buffer; + private RetainableByteBuffer buffer; - public InstructionStreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool) + public InstructionStreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool) { super(endPoint, executor); - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; } public boolean isUseInputDirectByteBuffers() @@ -52,17 +53,18 @@ public void setUseInputDirectByteBuffers(boolean useInputDirectByteBuffers) public void onUpgradeTo(ByteBuffer upgrade) { int capacity = Math.max(upgrade.remaining(), getInputBufferSize()); - buffer = byteBufferPool.acquire(capacity, isUseInputDirectByteBuffers()); - int position = BufferUtil.flipToFill(buffer); - buffer.put(upgrade); - BufferUtil.flipToFlush(buffer, position); + buffer = bufferPool.acquire(capacity, isUseInputDirectByteBuffers()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + int position = BufferUtil.flipToFill(byteBuffer); + byteBuffer.put(upgrade); + BufferUtil.flipToFlush(byteBuffer, position); } @Override public void onOpen() { super.onOpen(); - if (BufferUtil.hasContent(buffer)) + if (buffer != null && buffer.hasRemaining()) onFillable(); else fillInterested(); @@ -74,28 +76,28 @@ public void onFillable() try { if (buffer == null) - buffer = byteBufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); - + buffer = bufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); while (true) { // Parse first in case of bytes from the upgrade. - parseInstruction(buffer); + parseInstruction(byteBuffer); // Then read from the EndPoint. - int filled = getEndPoint().fill(buffer); + int filled = getEndPoint().fill(byteBuffer); if (LOG.isDebugEnabled()) LOG.debug("filled {} on {}", filled, this); if (filled == 0) { - byteBufferPool.release(buffer); + buffer.release(); buffer = null; fillInterested(); break; } else if (filled < 0) { - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(); break; @@ -106,7 +108,7 @@ else if (filled < 0) { if (LOG.isDebugEnabled()) LOG.debug("could not process decoder stream {}", getEndPoint(), x); - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(x); } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/MessageFlusher.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/MessageFlusher.java index ab8be1802eef..c920402bb48c 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/MessageFlusher.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/MessageFlusher.java @@ -21,7 +21,7 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.internal.generator.MessageGenerator; import org.eclipse.jetty.http3.qpack.QpackEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicStreamEndPoint; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IteratingCallback; @@ -35,19 +35,19 @@ public class MessageFlusher extends IteratingCallback private final AutoLock lock = new AutoLock(); private final Queue entries = new ArrayDeque<>(); - private final ByteBufferPool.Lease lease; + private final RetainableByteBufferPool.Accumulator accumulator; private final MessageGenerator generator; private Entry entry; - public MessageFlusher(ByteBufferPool byteBufferPool, QpackEncoder encoder, int maxHeadersLength, boolean useDirectByteBuffers) + public MessageFlusher(RetainableByteBufferPool bufferPool, QpackEncoder encoder, int maxHeadersLength, boolean useDirectByteBuffers) { - this.lease = new ByteBufferPool.Lease(byteBufferPool); + this.accumulator = new RetainableByteBufferPool.Accumulator(bufferPool); this.generator = new MessageGenerator(encoder, maxHeadersLength, useDirectByteBuffers); } public boolean offer(QuicStreamEndPoint endPoint, Frame frame, Callback callback) { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { entries.offer(new Entry(endPoint, frame, callback)); } @@ -57,7 +57,7 @@ public boolean offer(QuicStreamEndPoint endPoint, Frame frame, Callback callback @Override protected Action process() { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { entry = entries.poll(); if (entry == null) @@ -75,14 +75,14 @@ protected Action process() return Action.SCHEDULED; } - int generated = generator.generate(lease, entry.endPoint.getStreamId(), frame, this::failed); + int generated = generator.generate(accumulator, entry.endPoint.getStreamId(), frame, this::failed); if (generated < 0) return Action.SCHEDULED; QuicStreamEndPoint endPoint = entry.endPoint; - List buffers = lease.getByteBuffers(); + List buffers = accumulator.getByteBuffers(); if (LOG.isDebugEnabled()) - LOG.debug("writing {} buffers ({} bytes) for stream #{} on {}", buffers.size(), lease.getTotalLength(), endPoint.getStreamId(), this); + LOG.debug("writing {} buffers ({} bytes) for stream #{} on {}", buffers.size(), accumulator.getTotalLength(), endPoint.getStreamId(), this); endPoint.write(this, buffers, Frame.isLast(frame)); return Action.SCHEDULED; @@ -94,7 +94,7 @@ public void succeeded() if (LOG.isDebugEnabled()) LOG.debug("succeeded to write {} on {}", entry, this); - lease.recycle(); + accumulator.release(); entry.callback.succeeded(); entry = null; @@ -108,7 +108,7 @@ public void failed(Throwable x) if (LOG.isDebugEnabled()) LOG.debug("failed to write {} on {}", entry, this, x); - lease.recycle(); + accumulator.release(); entry.callback.failed(x); entry = null; diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/UnidirectionalStreamConnection.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/UnidirectionalStreamConnection.java index 536668c0ffe2..6bf48e18a6df 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/UnidirectionalStreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/UnidirectionalStreamConnection.java @@ -21,11 +21,11 @@ import org.eclipse.jetty.http3.qpack.QpackDecoder; import org.eclipse.jetty.http3.qpack.QpackEncoder; import org.eclipse.jetty.io.AbstractConnection; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicStreamEndPoint; import org.eclipse.jetty.quic.common.StreamType; -import org.eclipse.jetty.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,18 +33,18 @@ public class UnidirectionalStreamConnection extends AbstractConnection implement { private static final Logger LOG = LoggerFactory.getLogger(UnidirectionalStreamConnection.class); - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final QpackEncoder encoder; private final QpackDecoder decoder; private final ParserListener listener; private final VarLenInt parser = new VarLenInt(); private boolean useInputDirectByteBuffers = true; - private ByteBuffer buffer; + private RetainableByteBuffer buffer; - public UnidirectionalStreamConnection(QuicStreamEndPoint endPoint, Executor executor, ByteBufferPool byteBufferPool, QpackEncoder encoder, QpackDecoder decoder, ParserListener listener) + public UnidirectionalStreamConnection(QuicStreamEndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, QpackEncoder encoder, QpackDecoder decoder, ParserListener listener) { super(endPoint, executor); - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.encoder = encoder; this.decoder = decoder; this.listener = listener; @@ -78,8 +78,8 @@ public ByteBuffer onUpgradeFrom() { int remaining = buffer.remaining(); ByteBuffer copy = buffer.isDirect() ? ByteBuffer.allocateDirect(remaining) : ByteBuffer.allocate(remaining); - copy.put(buffer); - byteBufferPool.release(buffer); + copy.put(buffer.getByteBuffer()); + buffer.release(); buffer = null; copy.flip(); return copy; @@ -91,28 +91,28 @@ public void onFillable() try { if (buffer == null) - buffer = byteBufferPool.acquire(2048, isUseInputDirectByteBuffers()); - + buffer = bufferPool.acquire(2048, isUseInputDirectByteBuffers()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); while (true) { - int filled = getEndPoint().fill(buffer); + int filled = getEndPoint().fill(byteBuffer); if (LOG.isDebugEnabled()) - LOG.debug("filled {} on {}: {}", filled, this, BufferUtil.toDetailString(buffer)); + LOG.debug("filled {} on {}: {}", filled, this, buffer); if (filled > 0) { - if (parser.decode(buffer, this::detectAndUpgrade)) + if (parser.decode(byteBuffer, this::detectAndUpgrade)) break; } else if (filled == 0) { - byteBufferPool.release(buffer); + buffer.release(); fillInterested(); break; } else { - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(); break; @@ -123,7 +123,7 @@ else if (filled == 0) { if (LOG.isDebugEnabled()) LOG.debug("could not process stream {}", getEndPoint(), x); - byteBufferPool.release(buffer); + buffer.release(); buffer = null; getEndPoint().close(x); } @@ -134,7 +134,7 @@ private void detectAndUpgrade(long streamType) if (streamType == ControlStreamConnection.STREAM_TYPE) { ControlParser parser = new ControlParser(listener); - ControlStreamConnection newConnection = new ControlStreamConnection(getEndPoint(), getExecutor(), byteBufferPool, parser); + ControlStreamConnection newConnection = new ControlStreamConnection(getEndPoint(), getExecutor(), bufferPool, parser); newConnection.setInputBufferSize(getInputBufferSize()); newConnection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers()); if (LOG.isDebugEnabled()) @@ -143,7 +143,7 @@ private void detectAndUpgrade(long streamType) } else if (streamType == EncoderStreamConnection.STREAM_TYPE) { - EncoderStreamConnection newConnection = new EncoderStreamConnection(getEndPoint(), getExecutor(), byteBufferPool, decoder); + EncoderStreamConnection newConnection = new EncoderStreamConnection(getEndPoint(), getExecutor(), bufferPool, decoder); newConnection.setInputBufferSize(getInputBufferSize()); newConnection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers()); if (LOG.isDebugEnabled()) @@ -152,7 +152,7 @@ else if (streamType == EncoderStreamConnection.STREAM_TYPE) } else if (streamType == DecoderStreamConnection.STREAM_TYPE) { - DecoderStreamConnection newConnection = new DecoderStreamConnection(getEndPoint(), getExecutor(), byteBufferPool, encoder); + DecoderStreamConnection newConnection = new DecoderStreamConnection(getEndPoint(), getExecutor(), bufferPool, encoder); newConnection.setInputBufferSize(getInputBufferSize()); newConnection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers()); if (LOG.isDebugEnabled()) diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/CancelPushGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/CancelPushGenerator.java index 464476625e8e..ee9ecf4ec1ea 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/CancelPushGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/CancelPushGenerator.java @@ -16,12 +16,12 @@ import java.util.function.Consumer; import org.eclipse.jetty.http3.frames.Frame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class CancelPushGenerator extends FrameGenerator { @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { return 0; } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/ControlGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/ControlGenerator.java index 002d7fc2b292..5bd9caa711bb 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/ControlGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/ControlGenerator.java @@ -17,7 +17,7 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.frames.FrameType; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class ControlGenerator { @@ -31,8 +31,8 @@ public ControlGenerator(boolean useDirectByteBuffers) generators[FrameType.MAX_PUSH_ID.type()] = new MaxPushIdGenerator(); } - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { - return generators[frame.getFrameType().type()].generate(lease, streamId, frame, fail); + return generators[frame.getFrameType().type()].generate(accumulator, streamId, frame, fail); } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/DataGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/DataGenerator.java index 46e07ca2a7ec..084094def7cd 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/DataGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/DataGenerator.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.frames.FrameType; import org.eclipse.jetty.http3.internal.VarLenInt; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class DataGenerator extends FrameGenerator { @@ -32,23 +33,24 @@ public DataGenerator(boolean useDirectByteBuffers) } @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { DataFrame dataFrame = (DataFrame)frame; - return generateDataFrame(lease, dataFrame); + return generateDataFrame(accumulator, dataFrame); } - private int generateDataFrame(ByteBufferPool.Lease lease, DataFrame frame) + private int generateDataFrame(RetainableByteBufferPool.Accumulator accumulator, DataFrame frame) { ByteBuffer data = frame.getByteBuffer(); int dataLength = data.remaining(); int headerLength = VarLenInt.length(FrameType.DATA.type()) + VarLenInt.length(dataLength); - ByteBuffer header = lease.acquire(headerLength, useDirectByteBuffers); - VarLenInt.encode(header, FrameType.DATA.type()); - VarLenInt.encode(header, dataLength); - header.flip(); - lease.append(header, true); - lease.append(data, false); + RetainableByteBuffer header = accumulator.acquire(headerLength, useDirectByteBuffers); + ByteBuffer byteBuffer = header.getByteBuffer(); + VarLenInt.encode(byteBuffer, FrameType.DATA.type()); + VarLenInt.encode(byteBuffer, dataLength); + byteBuffer.flip(); + accumulator.append(header); + accumulator.append(RetainableByteBuffer.asNonRetainable(data)); return headerLength + dataLength; } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/FrameGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/FrameGenerator.java index 1dbf3c9eeef2..162132e82eac 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/FrameGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/FrameGenerator.java @@ -16,9 +16,9 @@ import java.util.function.Consumer; import org.eclipse.jetty.http3.frames.Frame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public abstract class FrameGenerator { - public abstract int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail); + public abstract int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail); } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/GoAwayGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/GoAwayGenerator.java index 2d67c63c0c35..889a0d10dd22 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/GoAwayGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/GoAwayGenerator.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http3.frames.FrameType; import org.eclipse.jetty.http3.frames.GoAwayFrame; import org.eclipse.jetty.http3.internal.VarLenInt; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class GoAwayGenerator extends FrameGenerator { @@ -32,23 +33,24 @@ public GoAwayGenerator(boolean useDirectByteBuffers) } @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { GoAwayFrame goAwayFrame = (GoAwayFrame)frame; - return generateGoAwayFrame(lease, goAwayFrame); + return generateGoAwayFrame(accumulator, goAwayFrame); } - private int generateGoAwayFrame(ByteBufferPool.Lease lease, GoAwayFrame frame) + private int generateGoAwayFrame(RetainableByteBufferPool.Accumulator accumulator, GoAwayFrame frame) { long lastId = frame.getLastId(); int lastIdLength = VarLenInt.length(lastId); int length = VarLenInt.length(FrameType.GOAWAY.type()) + VarLenInt.length(lastIdLength) + lastIdLength; - ByteBuffer buffer = lease.acquire(length, useDirectByteBuffers); - VarLenInt.encode(buffer, FrameType.GOAWAY.type()); - VarLenInt.encode(buffer, lastIdLength); - VarLenInt.encode(buffer, lastId); - buffer.flip(); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(length, useDirectByteBuffers); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + VarLenInt.encode(byteBuffer, FrameType.GOAWAY.type()); + VarLenInt.encode(byteBuffer, lastIdLength); + VarLenInt.encode(byteBuffer, lastId); + byteBuffer.flip(); + accumulator.append(buffer); return length; } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/HeadersGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/HeadersGenerator.java index fcfe269811f5..2d10c3b386a9 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/HeadersGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/HeadersGenerator.java @@ -22,7 +22,8 @@ import org.eclipse.jetty.http3.internal.VarLenInt; import org.eclipse.jetty.http3.qpack.QpackEncoder; import org.eclipse.jetty.http3.qpack.QpackException; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class HeadersGenerator extends FrameGenerator { @@ -38,13 +39,13 @@ public HeadersGenerator(QpackEncoder encoder, int maxLength, boolean useDirectBy } @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { HeadersFrame headersFrame = (HeadersFrame)frame; - return generateHeadersFrame(lease, streamId, headersFrame, fail); + return generateHeadersFrame(accumulator, streamId, headersFrame, fail); } - private int generateHeadersFrame(ByteBufferPool.Lease lease, long streamId, HeadersFrame frame, Consumer fail) + private int generateHeadersFrame(RetainableByteBufferPool.Accumulator accumulator, long streamId, HeadersFrame frame, Consumer fail) { try { @@ -52,21 +53,22 @@ private int generateHeadersFrame(ByteBufferPool.Lease lease, long streamId, Head int frameTypeLength = VarLenInt.length(FrameType.HEADERS.type()); int maxHeaderLength = frameTypeLength + VarLenInt.MAX_LENGTH; // The capacity of the buffer is larger than maxLength, but we need to enforce at most maxLength. - ByteBuffer buffer = lease.acquire(maxHeaderLength + maxLength, useDirectByteBuffers); - buffer.position(maxHeaderLength); - buffer.limit(buffer.position() + maxLength); + RetainableByteBuffer buffer = accumulator.acquire(maxHeaderLength + maxLength, useDirectByteBuffers); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.position(maxHeaderLength); + byteBuffer.limit(byteBuffer.position() + maxLength); // Encode after the maxHeaderLength. - encoder.encode(buffer, streamId, frame.getMetaData()); - buffer.flip(); - buffer.position(maxHeaderLength); + encoder.encode(byteBuffer, streamId, frame.getMetaData()); + byteBuffer.flip(); + byteBuffer.position(maxHeaderLength); int dataLength = buffer.remaining(); int headerLength = frameTypeLength + VarLenInt.length(dataLength); - int position = buffer.position() - headerLength; - buffer.position(position); - VarLenInt.encode(buffer, FrameType.HEADERS.type()); - VarLenInt.encode(buffer, dataLength); - buffer.position(position); - lease.append(buffer, true); + int position = byteBuffer.position() - headerLength; + byteBuffer.position(position); + VarLenInt.encode(byteBuffer, FrameType.HEADERS.type()); + VarLenInt.encode(byteBuffer, dataLength); + byteBuffer.position(position); + accumulator.append(buffer); return headerLength + dataLength; } catch (QpackException x) diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MaxPushIdGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MaxPushIdGenerator.java index ee132a0d9eac..b435ea498d8a 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MaxPushIdGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MaxPushIdGenerator.java @@ -16,12 +16,12 @@ import java.util.function.Consumer; import org.eclipse.jetty.http3.frames.Frame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class MaxPushIdGenerator extends FrameGenerator { @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { return 0; } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MessageGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MessageGenerator.java index 8b6330cc9712..0eafd05a3d01 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MessageGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/MessageGenerator.java @@ -18,7 +18,7 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.frames.FrameType; import org.eclipse.jetty.http3.qpack.QpackEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class MessageGenerator { @@ -31,8 +31,8 @@ public MessageGenerator(QpackEncoder encoder, int maxHeadersLength, boolean useD generators[FrameType.PUSH_PROMISE.type()] = new PushPromiseGenerator(); } - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { - return generators[frame.getFrameType().type()].generate(lease, streamId, frame, fail); + return generators[frame.getFrameType().type()].generate(accumulator, streamId, frame, fail); } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/PushPromiseGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/PushPromiseGenerator.java index 0acfb70bec87..8fe200f09615 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/PushPromiseGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/PushPromiseGenerator.java @@ -16,12 +16,12 @@ import java.util.function.Consumer; import org.eclipse.jetty.http3.frames.Frame; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class PushPromiseGenerator extends FrameGenerator { @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { return 0; } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/SettingsGenerator.java b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/SettingsGenerator.java index 12a5f18b814f..95ad2c4e9f3c 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/SettingsGenerator.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/main/java/org/eclipse/jetty/http3/internal/generator/SettingsGenerator.java @@ -20,7 +20,8 @@ import org.eclipse.jetty.http3.frames.Frame; import org.eclipse.jetty.http3.frames.SettingsFrame; import org.eclipse.jetty.http3.internal.VarLenInt; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class SettingsGenerator extends FrameGenerator @@ -33,13 +34,13 @@ public SettingsGenerator(boolean useDirectByteBuffers) } @Override - public int generate(ByteBufferPool.Lease lease, long streamId, Frame frame, Consumer fail) + public int generate(RetainableByteBufferPool.Accumulator accumulator, long streamId, Frame frame, Consumer fail) { SettingsFrame settingsFrame = (SettingsFrame)frame; - return generateSettings(lease, settingsFrame); + return generateSettings(accumulator, settingsFrame); } - private int generateSettings(ByteBufferPool.Lease lease, SettingsFrame frame) + private int generateSettings(RetainableByteBufferPool.Accumulator accumulator, SettingsFrame frame) { int length = 0; Map settings = frame.getSettings(); @@ -48,16 +49,17 @@ private int generateSettings(ByteBufferPool.Lease lease, SettingsFrame frame) length += VarLenInt.length(e.getKey()) + VarLenInt.length(e.getValue()); } int capacity = VarLenInt.length(frame.getFrameType().type()) + VarLenInt.length(length) + length; - ByteBuffer buffer = lease.acquire(capacity, useDirectByteBuffers); - VarLenInt.encode(buffer, frame.getFrameType().type()); - VarLenInt.encode(buffer, length); + RetainableByteBuffer buffer = accumulator.acquire(capacity, useDirectByteBuffers); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + VarLenInt.encode(byteBuffer, frame.getFrameType().type()); + VarLenInt.encode(byteBuffer, length); for (Map.Entry e : settings.entrySet()) { - VarLenInt.encode(buffer, e.getKey()); - VarLenInt.encode(buffer, e.getValue()); + VarLenInt.encode(byteBuffer, e.getKey()); + VarLenInt.encode(byteBuffer, e.getValue()); } - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); return capacity; } } diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/DataGenerateParseTest.java b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/DataGenerateParseTest.java index 9e2f65bdd368..5275a94d0a62 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/DataGenerateParseTest.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/DataGenerateParseTest.java @@ -23,7 +23,7 @@ import org.eclipse.jetty.http3.internal.generator.MessageGenerator; import org.eclipse.jetty.http3.internal.parser.MessageParser; import org.eclipse.jetty.http3.internal.parser.ParserListener; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.junit.jupiter.api.Test; @@ -53,8 +53,8 @@ private void testGenerateParse(ByteBuffer byteBuffer) byteBuffer.get(inputBytes); DataFrame input = new DataFrame(ByteBuffer.wrap(inputBytes), true); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); - new MessageGenerator(null, 8192, true).generate(lease, 0, input, null); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); + new MessageGenerator(null, 8192, true).generate(accumulator, 0, input, null); List frames = new ArrayList<>(); MessageParser parser = new MessageParser(new ParserListener() @@ -66,7 +66,7 @@ public void onData(long streamId, DataFrame frame) } }, null, 13, () -> true); parser.init(UnaryOperator.identity()); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/GoAwayGenerateParseTest.java b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/GoAwayGenerateParseTest.java index 315d20510789..3393527bf3dd 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/GoAwayGenerateParseTest.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/GoAwayGenerateParseTest.java @@ -21,7 +21,7 @@ import org.eclipse.jetty.http3.internal.generator.ControlGenerator; import org.eclipse.jetty.http3.internal.parser.ControlParser; import org.eclipse.jetty.http3.internal.parser.ParserListener; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,8 +34,8 @@ public void testGenerateParse() { GoAwayFrame input = GoAwayFrame.CLIENT_GRACEFUL; - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); - new ControlGenerator(true).generate(lease, 0, input, null); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); + new ControlGenerator(true).generate(accumulator, 0, input, null); List frames = new ArrayList<>(); ControlParser parser = new ControlParser(new ParserListener() @@ -46,7 +46,7 @@ public void onGoAway(GoAwayFrame frame) frames.add(frame); } }); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/HeadersGenerateParseTest.java b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/HeadersGenerateParseTest.java index 6230acfa41bf..85746114267b 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/HeadersGenerateParseTest.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/HeadersGenerateParseTest.java @@ -29,7 +29,7 @@ import org.eclipse.jetty.http3.internal.parser.ParserListener; import org.eclipse.jetty.http3.qpack.QpackDecoder; import org.eclipse.jetty.http3.qpack.QpackEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -47,8 +47,8 @@ public void testGenerateParse() HeadersFrame input = new HeadersFrame(new MetaData.Request(HttpMethod.GET.asString(), uri, HttpVersion.HTTP_3, fields), true); QpackEncoder encoder = new QpackEncoder(instructions -> {}, 100); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); - new MessageGenerator(encoder, 8192, true).generate(lease, 0, input, null); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); + new MessageGenerator(encoder, 8192, true).generate(accumulator, 0, input, null); QpackDecoder decoder = new QpackDecoder(instructions -> {}, 8192); List frames = new ArrayList<>(); @@ -61,7 +61,7 @@ public void onHeaders(long streamId, HeadersFrame frame) } }, decoder, 13, () -> true); parser.init(UnaryOperator.identity()); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); diff --git a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/SettingsGenerateParseTest.java b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/SettingsGenerateParseTest.java index 53e8c70402a3..a9a712b59d4a 100644 --- a/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/SettingsGenerateParseTest.java +++ b/jetty-core/jetty-http3/jetty-http3-common/src/test/java/org/eclipse/jetty/http3/internal/SettingsGenerateParseTest.java @@ -22,7 +22,7 @@ import org.eclipse.jetty.http3.internal.generator.ControlGenerator; import org.eclipse.jetty.http3.internal.parser.ControlParser; import org.eclipse.jetty.http3.internal.parser.ParserListener; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -46,8 +46,8 @@ private void testGenerateParse(Map settings) { SettingsFrame input = new SettingsFrame(settings); - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); - new ControlGenerator(true).generate(lease, 0, input, null); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); + new ControlGenerator(true).generate(accumulator, 0, input, null); List frames = new ArrayList<>(); ControlParser parser = new ControlParser(new ParserListener() @@ -58,7 +58,7 @@ public void onSettings(SettingsFrame frame) frames.add(frame); } }); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { parser.parse(buffer); assertFalse(buffer.hasRemaining()); diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/Instruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/Instruction.java index b3f472184c6c..66824705efaf 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/Instruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/Instruction.java @@ -15,11 +15,11 @@ import java.util.List; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; public interface Instruction { - void encode(ByteBufferPool.Lease lease); + void encode(RetainableByteBufferPool.Accumulator accumulator); /** *

A handler for instructions issued by an {@link QpackEncoder} or {@link QpackDecoder}.

diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/DuplicateInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/DuplicateInstruction.java index 8d88d81c2bc2..08fbfc336bb0 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/DuplicateInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/DuplicateInstruction.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class DuplicateInstruction implements Instruction @@ -35,14 +36,15 @@ public int getIndex() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(5, _index) + 1; - ByteBuffer buffer = lease.acquire(size, false); - buffer.put((byte)0x00); - NBitIntegerEncoder.encode(buffer, 5, _index); - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.put((byte)0x00); + NBitIntegerEncoder.encode(byteBuffer, 5, _index); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/IndexedNameEntryInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/IndexedNameEntryInstruction.java index 68da23fa1201..b7b5c12fa95c 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/IndexedNameEntryInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/IndexedNameEntryInstruction.java @@ -18,7 +18,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.HuffmanEncoder; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class IndexedNameEntryInstruction implements Instruction @@ -52,31 +53,32 @@ public String getValue() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(6, _index) + (_huffman ? HuffmanEncoder.octetsNeeded(_value) : _value.length()) + 2; - ByteBuffer buffer = lease.acquire(size, false); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); // First bit indicates the instruction, second bit is whether it is a dynamic table reference or not. - buffer.put((byte)(0x80 | (_dynamic ? 0x00 : 0x40))); - NBitIntegerEncoder.encode(buffer, 6, _index); + byteBuffer.put((byte)(0x80 | (_dynamic ? 0x00 : 0x40))); + NBitIntegerEncoder.encode(byteBuffer, 6, _index); // We will not huffman encode the string. if (_huffman) { - buffer.put((byte)(0x80)); - NBitIntegerEncoder.encode(buffer, 7, HuffmanEncoder.octetsNeeded(_value)); - HuffmanEncoder.encode(buffer, _value); + byteBuffer.put((byte)(0x80)); + NBitIntegerEncoder.encode(byteBuffer, 7, HuffmanEncoder.octetsNeeded(_value)); + HuffmanEncoder.encode(byteBuffer, _value); } else { - buffer.put((byte)(0x00)); - NBitIntegerEncoder.encode(buffer, 7, _value.length()); - buffer.put(_value.getBytes()); + byteBuffer.put((byte)(0x00)); + NBitIntegerEncoder.encode(byteBuffer, 7, _value.length()); + byteBuffer.put(_value.getBytes()); } - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/InsertCountIncrementInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/InsertCountIncrementInstruction.java index d9b02db55f47..c8d40294af04 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/InsertCountIncrementInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/InsertCountIncrementInstruction.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class InsertCountIncrementInstruction implements Instruction @@ -35,14 +36,15 @@ public int getIncrement() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(6, _increment) + 1; - ByteBuffer buffer = lease.acquire(size, false); - buffer.put((byte)0x00); - NBitIntegerEncoder.encode(buffer, 6, _increment); - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.put((byte)0x00); + NBitIntegerEncoder.encode(byteBuffer, 6, _increment); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/LiteralNameEntryInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/LiteralNameEntryInstruction.java index 623951545f47..c929c1adb128 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/LiteralNameEntryInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/LiteralNameEntryInstruction.java @@ -19,7 +19,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.HuffmanEncoder; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class LiteralNameEntryInstruction implements Instruction @@ -53,40 +54,41 @@ public String getValue() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = (_huffmanName ? HuffmanEncoder.octetsNeeded(_name) : _name.length()) + (_huffmanValue ? HuffmanEncoder.octetsNeeded(_value) : _value.length()) + 2; - ByteBuffer buffer = lease.acquire(size, false); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); if (_huffmanName) { - buffer.put((byte)(0x40 | 0x20)); - NBitIntegerEncoder.encode(buffer, 5, HuffmanEncoder.octetsNeeded(_name)); - HuffmanEncoder.encode(buffer, _name); + byteBuffer.put((byte)(0x40 | 0x20)); + NBitIntegerEncoder.encode(byteBuffer, 5, HuffmanEncoder.octetsNeeded(_name)); + HuffmanEncoder.encode(byteBuffer, _name); } else { - buffer.put((byte)(0x40)); - NBitIntegerEncoder.encode(buffer, 5, _name.length()); - buffer.put(_name.getBytes()); + byteBuffer.put((byte)(0x40)); + NBitIntegerEncoder.encode(byteBuffer, 5, _name.length()); + byteBuffer.put(_name.getBytes()); } if (_huffmanValue) { - buffer.put((byte)(0x80)); - NBitIntegerEncoder.encode(buffer, 7, HuffmanEncoder.octetsNeeded(_value)); - HuffmanEncoder.encode(buffer, _value); + byteBuffer.put((byte)(0x80)); + NBitIntegerEncoder.encode(byteBuffer, 7, HuffmanEncoder.octetsNeeded(_value)); + HuffmanEncoder.encode(byteBuffer, _value); } else { - buffer.put((byte)(0x00)); - NBitIntegerEncoder.encode(buffer, 5, _value.length()); - buffer.put(_value.getBytes()); + byteBuffer.put((byte)(0x00)); + NBitIntegerEncoder.encode(byteBuffer, 5, _value.length()); + byteBuffer.put(_value.getBytes()); } - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SectionAcknowledgmentInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SectionAcknowledgmentInstruction.java index 9cd0dba17f18..3d8dea4690d7 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SectionAcknowledgmentInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SectionAcknowledgmentInstruction.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class SectionAcknowledgmentInstruction implements Instruction @@ -35,14 +36,15 @@ public long getStreamId() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(7, _streamId) + 1; - ByteBuffer buffer = lease.acquire(size, false); - buffer.put((byte)0x80); - NBitIntegerEncoder.encode(buffer, 7, _streamId); - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.put((byte)0x80); + NBitIntegerEncoder.encode(byteBuffer, 7, _streamId); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SetCapacityInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SetCapacityInstruction.java index 68bc69d0f49c..c3b8a9c628f7 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SetCapacityInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/SetCapacityInstruction.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class SetCapacityInstruction implements Instruction @@ -35,14 +36,15 @@ public int getCapacity() } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(5, _capacity) + 1; - ByteBuffer buffer = lease.acquire(size, false); - buffer.put((byte)0x20); - NBitIntegerEncoder.encode(buffer, 5, _capacity); - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.put((byte)0x20); + NBitIntegerEncoder.encode(byteBuffer, 5, _capacity); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/StreamCancellationInstruction.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/StreamCancellationInstruction.java index fc8ce46e0cd9..ca52ee9d34fc 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/StreamCancellationInstruction.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/main/java/org/eclipse/jetty/http3/qpack/internal/instruction/StreamCancellationInstruction.java @@ -17,7 +17,8 @@ import org.eclipse.jetty.http3.qpack.Instruction; import org.eclipse.jetty.http3.qpack.internal.util.NBitIntegerEncoder; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; public class StreamCancellationInstruction implements Instruction @@ -30,14 +31,15 @@ public StreamCancellationInstruction(long streamId) } @Override - public void encode(ByteBufferPool.Lease lease) + public void encode(RetainableByteBufferPool.Accumulator accumulator) { int size = NBitIntegerEncoder.octetsNeeded(6, _streamId) + 1; - ByteBuffer buffer = lease.acquire(size, false); - buffer.put((byte)0x40); - NBitIntegerEncoder.encode(buffer, 6, _streamId); - BufferUtil.flipToFlush(buffer, 0); - lease.append(buffer, true); + RetainableByteBuffer buffer = accumulator.acquire(size, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + byteBuffer.put((byte)0x40); + NBitIntegerEncoder.encode(byteBuffer, 6, _streamId); + BufferUtil.flipToFlush(byteBuffer, 0); + accumulator.append(buffer); } @Override diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/InstructionGeneratorTest.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/InstructionGeneratorTest.java index 03352c447b4b..dfb94008ee2c 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/InstructionGeneratorTest.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/InstructionGeneratorTest.java @@ -15,8 +15,8 @@ import org.eclipse.jetty.http3.qpack.internal.instruction.IndexedNameEntryInstruction; import org.eclipse.jetty.http3.qpack.internal.instruction.SectionAcknowledgmentInstruction; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.junit.jupiter.api.Test; @@ -26,14 +26,14 @@ public class InstructionGeneratorTest { - private final ByteBufferPool _bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool _bufferPool = new ArrayRetainableByteBufferPool(); private String toHexString(Instruction instruction) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(_bufferPool); - instruction.encode(lease); - assertThat(lease.getSize(), is(1)); - return BufferUtil.toHexString(lease.getByteBuffers().get(0)); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(_bufferPool); + instruction.encode(accumulator); + assertThat(accumulator.getSize(), is(1)); + return BufferUtil.toHexString(accumulator.getByteBuffers().get(0)); } @Test diff --git a/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/QpackTestUtil.java b/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/QpackTestUtil.java index 5cacd32c9721..d146736de46e 100644 --- a/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/QpackTestUtil.java +++ b/jetty-core/jetty-http3/jetty-http3-qpack/src/test/java/org/eclipse/jetty/http3/qpack/QpackTestUtil.java @@ -20,9 +20,9 @@ import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.MetaData; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.TypeUtil; +import org.eclipse.jetty.util.StringUtil; import org.hamcrest.Matcher; import static org.hamcrest.MatcherAssert.assertThat; @@ -32,14 +32,14 @@ public class QpackTestUtil { public static ByteBuffer toBuffer(Instruction... instructions) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); for (Instruction instruction : instructions) { - instruction.encode(lease); + instruction.encode(accumulator); } - ByteBuffer combinedBuffer = BufferUtil.allocate(Math.toIntExact(lease.getTotalLength())); + ByteBuffer combinedBuffer = BufferUtil.allocate(Math.toIntExact(accumulator.getTotalLength())); BufferUtil.clearToFill(combinedBuffer); - for (ByteBuffer buffer : lease.getByteBuffers()) + for (ByteBuffer buffer : accumulator.getByteBuffers()) { combinedBuffer.put(buffer); } @@ -55,12 +55,12 @@ public static Matcher equalsHex(String expectedString) public static ByteBuffer toBuffer(List instructions) { - ByteBufferPool.Lease lease = new ByteBufferPool.Lease(ByteBufferPool.NOOP); - instructions.forEach(i -> i.encode(lease)); - assertThat(lease.getSize(), is(instructions.size())); - ByteBuffer combinedBuffer = BufferUtil.allocate(Math.toIntExact(lease.getTotalLength()), false); + RetainableByteBufferPool.Accumulator accumulator = new RetainableByteBufferPool.Accumulator(new RetainableByteBufferPool.NonPooling()); + instructions.forEach(i -> i.encode(accumulator)); + assertThat(accumulator.getSize(), is(instructions.size())); + ByteBuffer combinedBuffer = BufferUtil.allocate(Math.toIntExact(accumulator.getTotalLength()), false); BufferUtil.clearToFill(combinedBuffer); - lease.getByteBuffers().forEach(combinedBuffer::put); + accumulator.getByteBuffers().forEach(combinedBuffer::put); BufferUtil.flipToFlush(combinedBuffer, 0); return combinedBuffer; } @@ -68,7 +68,7 @@ public static ByteBuffer toBuffer(List instructions) public static ByteBuffer hexToBuffer(String hexString) { hexString = hexString.replaceAll("\\s+", ""); - return ByteBuffer.wrap(TypeUtil.fromHexString(hexString)); + return ByteBuffer.wrap(StringUtil.fromHexString(hexString)); } public static String toHexString(Instruction instruction) diff --git a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/HTTP3ServerConnector.java b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/HTTP3ServerConnector.java index b8aa6230be2f..dc83937ae022 100644 --- a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/HTTP3ServerConnector.java +++ b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/HTTP3ServerConnector.java @@ -18,7 +18,7 @@ import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.PreEncodedHttpField; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.server.QuicServerConnector; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.Server; @@ -44,7 +44,7 @@ public HTTP3ServerConnector(Server server, SslContextFactory.Server sslContextFa this(server, null, null, null, sslContextFactory, factories); } - public HTTP3ServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool bufferPool, SslContextFactory.Server sslContextFactory, ConnectionFactory... factories) + public HTTP3ServerConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, SslContextFactory.Server sslContextFactory, ConnectionFactory... factories) { super(server, executor, scheduler, bufferPool, sslContextFactory, factories); // Max concurrent streams that a client can open. diff --git a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3Session.java b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3Session.java index adcf58e44d49..2247593c228a 100644 --- a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3Session.java +++ b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3Session.java @@ -82,7 +82,7 @@ public ServerHTTP3Session(HTTP3Configuration configuration, ServerQuicSession qu if (LOG.isDebugEnabled()) LOG.debug("created control stream #{} on {}", controlStreamId, controlEndPoint); - this.messageFlusher = new MessageFlusher(quicSession.getByteBufferPool(), encoder, configuration.getMaxResponseHeadersSize(), configuration.isUseOutputDirectByteBuffers()); + this.messageFlusher = new MessageFlusher(quicSession.getRetainableByteBufferPool(), encoder, configuration.getMaxResponseHeadersSize(), configuration.isUseOutputDirectByteBuffers()); addBean(messageFlusher); } @@ -189,7 +189,7 @@ protected void onClose(long error, String reason) private void openUnidirectionalStreamEndPoint(QuicStreamEndPoint endPoint) { - UnidirectionalStreamConnection connection = new UnidirectionalStreamConnection(endPoint, getQuicSession().getExecutor(), getQuicSession().getByteBufferPool(), encoder, decoder, session); + UnidirectionalStreamConnection connection = new UnidirectionalStreamConnection(endPoint, getQuicSession().getExecutor(), getQuicSession().getRetainableByteBufferPool(), encoder, decoder, session); endPoint.setConnection(connection); endPoint.opened(); } diff --git a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3StreamConnection.java b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3StreamConnection.java index 01d08089491d..0539977f9625 100644 --- a/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3StreamConnection.java +++ b/jetty-core/jetty-http3/jetty-http3-server/src/main/java/org/eclipse/jetty/http3/server/internal/ServerHTTP3StreamConnection.java @@ -42,7 +42,7 @@ public class ServerHTTP3StreamConnection extends HTTP3StreamConnection implement public ServerHTTP3StreamConnection(Connector connector, HttpConfiguration httpConfiguration, QuicStreamEndPoint endPoint, ServerHTTP3Session session, MessageParser parser) { - super(endPoint, connector.getExecutor(), connector.getByteBufferPool(), parser); + super(endPoint, connector.getExecutor(), connector.getRetainableByteBufferPool(), parser); this.connector = connector; this.httpConfiguration = httpConfiguration; this.session = session; diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java deleted file mode 100644 index 748ec94a3a3a..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java +++ /dev/null @@ -1,276 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.nio.ByteBuffer; -import java.util.Objects; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.Consumer; -import java.util.function.IntConsumer; - -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.NanoTime; -import org.eclipse.jetty.util.annotation.ManagedAttribute; -import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.annotation.ManagedOperation; - -/** - * The {@code maxHeapMemory} and {@code maxDirectMemory} default heuristic is to use {@link Runtime#maxMemory()} - * divided by 4. - */ -@ManagedObject -abstract class AbstractByteBufferPool implements ByteBufferPool -{ - public static final int DEFAULT_FACTOR = 4096; - public static final int DEFAULT_MAX_CAPACITY_BY_FACTOR = 16; - private final int _factor; - private final int _maxCapacity; - private final int _maxBucketSize; - private final long _maxHeapMemory; - private final long _maxDirectMemory; - private final AtomicLong _heapMemory = new AtomicLong(); - private final AtomicLong _directMemory = new AtomicLong(); - private final RetainableByteBufferPool _retainableByteBufferPool; - - /** - * Creates a new ByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer queue length - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic - * @param retainedHeapMemory the max heap memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - * @param retainedDirectMemory the max direct memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - */ - protected AbstractByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, long retainedHeapMemory, long retainedDirectMemory) - { - _factor = factor <= 0 ? DEFAULT_FACTOR : factor; - _maxCapacity = maxCapacity > 0 ? maxCapacity : DEFAULT_MAX_CAPACITY_BY_FACTOR * _factor; - _maxBucketSize = maxBucketSize; - _maxHeapMemory = memorySize(maxHeapMemory); - _maxDirectMemory = memorySize(maxDirectMemory); - _retainableByteBufferPool = (retainedHeapMemory == -2 && retainedDirectMemory == -2) - ? RetainableByteBufferPool.from(this) - : newRetainableByteBufferPool(factor, maxCapacity, maxBucketSize, retainedSize(retainedHeapMemory), retainedSize(retainedDirectMemory)); - } - - static long retainedSize(long size) - { - if (size == -2) - return 0; - return memorySize(size); - } - - static long memorySize(long size) - { - if (size < 0) - return -1; - if (size == 0) - return Runtime.getRuntime().maxMemory() / 4; - return size; - } - - protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - return RetainableByteBufferPool.from(this); - } - - @Override - public RetainableByteBufferPool asRetainableByteBufferPool() - { - return _retainableByteBufferPool; - } - - protected int getCapacityFactor() - { - return _factor; - } - - protected int getMaxCapacity() - { - return _maxCapacity; - } - - protected int getMaxBucketSize() - { - return _maxBucketSize; - } - - @Deprecated - protected void decrementMemory(ByteBuffer buffer) - { - updateMemory(buffer, false); - } - - @Deprecated - protected void incrementMemory(ByteBuffer buffer) - { - updateMemory(buffer, true); - } - - private void updateMemory(ByteBuffer buffer, boolean addOrSub) - { - AtomicLong memory = buffer.isDirect() ? _directMemory : _heapMemory; - int capacity = buffer.capacity(); - memory.addAndGet(addOrSub ? capacity : -capacity); - } - - protected void releaseExcessMemory(boolean direct, Consumer clearFn) - { - long maxMemory = direct ? _maxDirectMemory : _maxHeapMemory; - if (maxMemory > 0) - { - while (getMemory(direct) > maxMemory) - { - clearFn.accept(direct); - } - } - } - - @ManagedAttribute("The bytes retained by direct ByteBuffers") - public long getDirectMemory() - { - return getMemory(true); - } - - @ManagedAttribute("The bytes retained by heap ByteBuffers") - public long getHeapMemory() - { - return getMemory(false); - } - - @ManagedAttribute("The max num of bytes that can be retained from direct ByteBuffers") - public long getMaxDirectMemory() - { - return _maxDirectMemory; - } - - @ManagedAttribute("The max num of bytes that can be retained from heap ByteBuffers") - public long getMaxHeapMemory() - { - return _maxHeapMemory; - } - - public long getMemory(boolean direct) - { - AtomicLong memory = direct ? _directMemory : _heapMemory; - return memory.get(); - } - - protected static class Bucket - { - private final Queue _queue = new ConcurrentLinkedQueue<>(); - private final int _capacity; - private final int _maxSize; - private final AtomicInteger _size; - private final AtomicLong _lastUpdate = new AtomicLong(NanoTime.now()); - private final IntConsumer _memoryFunction; - - @Deprecated - public Bucket(int capacity, int maxSize) - { - this(capacity, maxSize, i -> {}); - } - - public Bucket(int capacity, int maxSize, IntConsumer memoryFunction) - { - _capacity = capacity; - _maxSize = maxSize; - _size = maxSize > 0 ? new AtomicInteger() : null; - _memoryFunction = Objects.requireNonNull(memoryFunction); - } - - public ByteBuffer acquire() - { - ByteBuffer buffer = _queue.poll(); - if (buffer != null) - { - if (_size != null) - _size.decrementAndGet(); - _memoryFunction.accept(-buffer.capacity()); - } - - return buffer; - } - - public void release(ByteBuffer buffer) - { - resetUpdateTime(); - BufferUtil.reset(buffer); - if (_size == null || _size.incrementAndGet() <= _maxSize) - { - _queue.offer(buffer); - _memoryFunction.accept(buffer.capacity()); - } - else - { - _size.decrementAndGet(); - } - } - - void resetUpdateTime() - { - _lastUpdate.lazySet(NanoTime.now()); - } - - public void clear() - { - int size = _size == null ? 0 : _size.get() - 1; - while (size >= 0) - { - ByteBuffer buffer = acquire(); - if (buffer == null) - break; - if (_size != null) - --size; - } - } - - boolean isEmpty() - { - return _queue.isEmpty(); - } - - int size() - { - return _queue.size(); - } - - long getLastUpdate() - { - return _lastUpdate.getOpaque(); - } - - @Override - public String toString() - { - return String.format("%s@%x{capacity=%d, size=%d, maxSize=%d}", getClass().getSimpleName(), hashCode(), _capacity, size(), _maxSize); - } - } - - IntConsumer updateMemory(boolean direct) - { - return (direct) ? _directMemory::addAndGet : _heapMemory::addAndGet; - } - - @ManagedOperation(value = "Clears this ByteBufferPool", impact = "ACTION") - public void clear() - { - _heapMemory.set(0); - _directMemory.set(0); - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractRetainableByteBuffer.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractRetainableByteBuffer.java new file mode 100644 index 000000000000..7fe565ec774a --- /dev/null +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractRetainableByteBuffer.java @@ -0,0 +1,77 @@ +// +// ======================================================================== +// Copyright (c) 1995-2022 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.io; + +import java.nio.ByteBuffer; + +import org.eclipse.jetty.util.BufferUtil; + +/** + *

Abstract implementation of {@link RetainableByteBuffer} with + * reference counting.

+ */ +public abstract class AbstractRetainableByteBuffer implements RetainableByteBuffer +{ + private final ReferenceCounter refCount = new ReferenceCounter(0); + private final ByteBuffer byteBuffer; + + public AbstractRetainableByteBuffer(ByteBuffer byteBuffer) + { + this.byteBuffer = byteBuffer; + } + + /** + * @see ReferenceCounter#acquire() + */ + protected void acquire() + { + refCount.acquire(); + } + + @Override + public boolean canRetain() + { + return refCount.canRetain(); + } + + @Override + public void retain() + { + refCount.retain(); + } + + @Override + public boolean release() + { + return refCount.release(); + } + + @Override + public boolean isRetained() + { + return refCount.isRetained(); + } + + @Override + public ByteBuffer getByteBuffer() + { + return byteBuffer; + } + + @Override + public String toString() + { + return "%s@%x[r=%d,%s]".formatted(getClass().getSimpleName(), hashCode(), refCount.get(), BufferUtil.toDetailString(byteBuffer)); + } +} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java deleted file mode 100644 index 57600ea4514c..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java +++ /dev/null @@ -1,342 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.NanoTime; -import org.eclipse.jetty.util.annotation.ManagedAttribute; -import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.Dumpable; -import org.eclipse.jetty.util.component.DumpableCollection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - *

A ByteBuffer pool where ByteBuffers are held in queues that are held in array elements.

- *

Given a capacity {@code factor} of 4096, the first array element holds a bucket of ByteBuffers - * each of capacity 4096, the second array element holds a bucket of ByteBuffers each of capacity - * 8192, and so on.

- *

The {@code maxHeapMemory} and {@code maxDirectMemory} default heuristic is to use {@link Runtime#maxMemory()} - * divided by 4.

- */ -@ManagedObject -public class ArrayByteBufferPool extends AbstractByteBufferPool implements Dumpable -{ - private static final Logger LOG = LoggerFactory.getLogger(ArrayByteBufferPool.class); - - private final int _maxCapacity; - private final int _minCapacity; - private final Bucket[] _direct; - private final Bucket[] _indirect; - private boolean _detailedDump = false; - - /** - * Creates a new ArrayByteBufferPool with a default configuration. - * Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic. - */ - public ArrayByteBufferPool() - { - this(-1, -1, -1); - } - - /** - * Creates a new ArrayByteBufferPool with the given configuration. - * Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param factor the capacity factor - * @param maxCapacity the maximum ByteBuffer capacity - */ - public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity) - { - this(minCapacity, factor, maxCapacity, -1, 0, 0); - } - - /** - * Creates a new ArrayByteBufferPool with the given configuration. - * Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param factor the capacity factor - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxQueueLength the maximum ByteBuffer queue length - */ - public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxQueueLength) - { - this(minCapacity, factor, maxCapacity, maxQueueLength, 0, 0); - } - - /** - * Creates a new ArrayByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param factor the capacity factor - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxBucketSize the maximum ByteBuffer queue length in a {@link Bucket} - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic - */ - public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory) - { - this(minCapacity, factor, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, maxHeapMemory, maxDirectMemory); - } - - /** - * Creates a new ArrayByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param factor the capacity factor - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxBucketSize the maximum ByteBuffer queue length in a {@link Bucket} - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic - * @param retainedHeapMemory the max heap memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - * @param retainedDirectMemory the max direct memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - */ - public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, long retainedHeapMemory, long retainedDirectMemory) - { - super(factor, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, retainedHeapMemory, retainedDirectMemory); - maxCapacity = getMaxCapacity(); - - factor = getCapacityFactor(); - if (minCapacity <= 0) - minCapacity = 0; - if ((maxCapacity % factor) != 0 || factor >= maxCapacity) - throw new IllegalArgumentException("The capacity factor must be a divisor of maxCapacity"); - _maxCapacity = maxCapacity; - _minCapacity = minCapacity; - - // Initialize all buckets in constructor and never modify the array again. - int length = bucketFor(maxCapacity) + 1; - _direct = new Bucket[length]; - _indirect = new Bucket[length]; - for (int i = 0; i < length; i++) - { - _direct[i] = newBucket(i, true); - _indirect[i] = newBucket(i, false); - } - } - - @Override - protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - return new Retained(factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - - @Override - public ByteBuffer acquire(int size, boolean direct) - { - int capacity = size < _minCapacity ? size : capacityFor(bucketFor(size)); - Bucket bucket = bucketFor(size, direct); - if (bucket == null) - return newByteBuffer(capacity, direct); - ByteBuffer buffer = bucket.acquire(); - if (buffer == null) - return newByteBuffer(capacity, direct); - return buffer; - } - - @Override - public void release(ByteBuffer buffer) - { - if (buffer == null) - return; - - int capacity = buffer.capacity(); - // Validate that this buffer is from this pool. - if (capacity != capacityFor(bucketFor(capacity))) - { - if (LOG.isDebugEnabled()) - LOG.debug("ByteBuffer {} does not belong to this pool, discarding it", BufferUtil.toDetailString(buffer)); - return; - } - - // Don't release into the pool if greater than the maximum ByteBuffer capacity. - if (capacity > _maxCapacity) - return; - - boolean direct = buffer.isDirect(); - Bucket bucket = bucketFor(capacity, direct); - if (bucket != null) - { - bucket.release(buffer); - releaseExcessMemory(direct, this::releaseMemory); - } - } - - private Bucket newBucket(int key, boolean direct) - { - return new Bucket(capacityFor(key), getMaxBucketSize(), updateMemory(direct)); - } - - @Override - public void clear() - { - super.clear(); - for (int i = 0; i < _direct.length; ++i) - { - _direct[i].clear(); - _indirect[i].clear(); - } - } - - protected void releaseMemory(boolean direct) - { - long oldest = Long.MAX_VALUE; - int index = -1; - Bucket[] buckets = bucketsFor(direct); - for (int i = 0; i < buckets.length; ++i) - { - Bucket bucket = buckets[i]; - if (bucket.isEmpty()) - continue; - long lastUpdateNanoTime = bucket.getLastUpdate(); - if (oldest == Long.MAX_VALUE || NanoTime.isBefore(lastUpdateNanoTime, oldest)) - { - oldest = lastUpdateNanoTime; - index = i; - } - } - if (index >= 0) - { - Bucket bucket = buckets[index]; - bucket.clear(); - } - } - - protected int bucketFor(int capacity) - { - return (int)Math.ceil((double)capacity / getCapacityFactor()); - } - - protected int capacityFor(int bucket) - { - return bucket * getCapacityFactor(); - } - - protected Bucket bucketFor(int capacity, boolean direct) - { - if (capacity < _minCapacity) - return null; - int bucket = bucketFor(capacity); - if (bucket >= _direct.length) - return null; - Bucket[] buckets = bucketsFor(direct); - return buckets[bucket]; - } - - @ManagedAttribute("The number of pooled direct ByteBuffers") - public long getDirectByteBufferCount() - { - return getByteBufferCount(true); - } - - @ManagedAttribute("The number of pooled heap ByteBuffers") - public long getHeapByteBufferCount() - { - return getByteBufferCount(false); - } - - private long getByteBufferCount(boolean direct) - { - return Arrays.stream(bucketsFor(direct)) - .filter(Objects::nonNull) - .mapToLong(Bucket::size) - .sum(); - } - - // Package local for testing - Bucket[] bucketsFor(boolean direct) - { - return direct ? _direct : _indirect; - } - - public boolean isDetailedDump() - { - return _detailedDump; - } - - public void setDetailedDump(boolean detailedDump) - { - _detailedDump = detailedDump; - } - - @Override - public void dump(Appendable out, String indent) throws IOException - { - List dump = new ArrayList<>(); - dump.add(String.format("HeapMemory: %d/%d", getHeapMemory(), getMaxHeapMemory())); - dump.add(String.format("DirectMemory: %d/%d", getDirectMemory(), getMaxDirectMemory())); - - List indirect = Arrays.stream(_indirect).filter(b -> !b.isEmpty()).collect(Collectors.toList()); - List direct = Arrays.stream(_direct).filter(b -> !b.isEmpty()).collect(Collectors.toList()); - if (isDetailedDump()) - { - dump.add(new DumpableCollection("Indirect Buckets", indirect)); - dump.add(new DumpableCollection("Direct Buckets", direct)); - } - else - { - dump.add("Indirect Buckets size=" + indirect.size()); - dump.add("Direct Buckets size=" + direct.size()); - } - dump.add(asRetainableByteBufferPool()); - Dumpable.dumpObjects(out, indent, this, dump); - } - - @Override - public String toString() - { - return String.format("%s@%x{minBufferCapacity=%s, maxBufferCapacity=%s, maxQueueLength=%s, factor=%s}", - this.getClass().getSimpleName(), hashCode(), - _minCapacity, - _maxCapacity, - getMaxBucketSize(), - getCapacityFactor()); - } - - protected class Retained extends ArrayRetainableByteBufferPool - { - public Retained(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - super(0, factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - - @Override - protected ByteBuffer allocate(int capacity) - { - return ArrayByteBufferPool.this.acquire(capacity, false); - } - - @Override - protected ByteBuffer allocateDirect(int capacity) - { - return ArrayByteBufferPool.this.acquire(capacity, true); - } - - @Override - protected void removed(RetainableByteBuffer retainedBuffer) - { - ArrayByteBufferPool.this.release(retainedBuffer.getBuffer()); - } - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPool.java index 6f0e74af1269..ac6972daf8d8 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPool.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPool.java @@ -36,15 +36,16 @@ * held in array elements.

*

Given a capacity {@code factor} of 1024, the first array element holds a Pool of RetainableByteBuffers * each of capacity 1024, the second array element holds a Pool of RetainableByteBuffers each of capacity - * 2048, and so on.

+ * 2048, and so on with capacities 3072, 4096, 5120, etc.

*

The {@code maxHeapMemory} and {@code maxDirectMemory} default heuristic is to use {@link Runtime#maxMemory()} - * divided by 4.

+ * divided by 8.

*/ -@SuppressWarnings("resource") @ManagedObject public class ArrayRetainableByteBufferPool implements RetainableByteBufferPool, Dumpable { private static final Logger LOG = LoggerFactory.getLogger(ArrayRetainableByteBufferPool.class); + static final int DEFAULT_FACTOR = 4096; + static final int DEFAULT_MAX_CAPACITY_BY_FACTOR = 16; private final RetainedBucket[] _direct; private final RetainedBucket[] _indirect; @@ -62,7 +63,20 @@ public class ArrayRetainableByteBufferPool implements RetainableByteBufferPool, */ public ArrayRetainableByteBufferPool() { - this(0, -1, -1, Integer.MAX_VALUE); + this(0, -1, -1); + } + + /** + * Creates a new ArrayRetainableByteBufferPool with the given configuration. + * Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic. + * + * @param minCapacity the minimum ByteBuffer capacity + * @param factor the capacity factor + * @param maxCapacity the maximum ByteBuffer capacity + */ + public ArrayRetainableByteBufferPool(int minCapacity, int factor, int maxCapacity) + { + this(minCapacity, factor, maxCapacity, Integer.MAX_VALUE); } /** @@ -110,9 +124,9 @@ protected ArrayRetainableByteBufferPool(int minCapacity, int factor, int maxCapa { if (minCapacity <= 0) minCapacity = 0; - factor = factor <= 0 ? AbstractByteBufferPool.DEFAULT_FACTOR : factor; + factor = factor <= 0 ? DEFAULT_FACTOR : factor; if (maxCapacity <= 0) - maxCapacity = AbstractByteBufferPool.DEFAULT_MAX_CAPACITY_BY_FACTOR * factor; + maxCapacity = DEFAULT_MAX_CAPACITY_BY_FACTOR * factor; if ((maxCapacity % factor) != 0 || factor >= maxCapacity) throw new IllegalArgumentException(String.format("The capacity factor(%d) must be a divisor of maxCapacity(%d)", factor, maxCapacity)); @@ -136,11 +150,20 @@ protected ArrayRetainableByteBufferPool(int minCapacity, int factor, int maxCapa _maxCapacity = maxCapacity; _direct = directArray; _indirect = indirectArray; - _maxHeapMemory = AbstractByteBufferPool.retainedSize(maxHeapMemory); - _maxDirectMemory = AbstractByteBufferPool.retainedSize(maxDirectMemory); + _maxHeapMemory = maxMemory(maxHeapMemory); + _maxDirectMemory = maxMemory(maxDirectMemory); _bucketIndexFor = bucketIndexFor; } + private long maxMemory(long maxMemory) + { + if (maxMemory < 0) + return -1; + if (maxMemory == 0) + return Runtime.getRuntime().maxMemory() / 8; + return maxMemory; + } + @ManagedAttribute("The minimum pooled buffer capacity") public int getMinCapacity() { @@ -159,17 +182,17 @@ public RetainableByteBuffer acquire(int size, boolean direct) RetainedBucket bucket = bucketFor(size, direct); if (bucket == null) return newRetainableByteBuffer(size, direct, this::removed); - RetainedBucket.Entry entry = bucket.acquire(); + Pool.Entry entry = bucket.getPool().acquire(); RetainableByteBuffer buffer; if (entry == null) { - RetainedBucket.Entry reservedEntry = bucket.reserve(); + Pool.Entry reservedEntry = bucket.getPool().reserve(); if (reservedEntry != null) { buffer = newRetainableByteBuffer(bucket._capacity, direct, retainedBuffer -> { - BufferUtil.reset(retainedBuffer.getBuffer()); + BufferUtil.reset(retainedBuffer.getByteBuffer()); reservedEntry.release(); }); reservedEntry.enable(buffer, true); @@ -187,7 +210,7 @@ public RetainableByteBuffer acquire(int size, boolean direct) else { buffer = entry.getPooled(); - buffer.acquire(); + ((Buffer)buffer).acquire(); } return buffer; } @@ -208,16 +231,16 @@ protected void removed(RetainableByteBuffer retainedBuffer) private RetainableByteBuffer newRetainableByteBuffer(int capacity, boolean direct, Consumer releaser) { - ByteBuffer buffer = direct ? allocateDirect(capacity) : allocate(capacity); - BufferUtil.clear(buffer); - RetainableByteBuffer retainableByteBuffer = new RetainableByteBuffer(buffer, releaser); + ByteBuffer buffer = BufferUtil.allocate(capacity, direct); + Buffer retainableByteBuffer = new Buffer(buffer, releaser); retainableByteBuffer.acquire(); return retainableByteBuffer; } - protected Pool poolFor(int capacity, boolean direct) + public Pool poolFor(int capacity, boolean direct) { - return bucketFor(capacity, direct); + RetainedBucket bucket = bucketFor(capacity, direct); + return bucket == null ? null : bucket.getPool(); } private RetainedBucket bucketFor(int capacity, boolean direct) @@ -246,7 +269,7 @@ public long getHeapByteBufferCount() private long getByteBufferCount(boolean direct) { RetainedBucket[] buckets = direct ? _direct : _indirect; - return Arrays.stream(buckets).mapToLong(RetainedBucket::size).sum(); + return Arrays.stream(buckets).mapToLong(bucket -> bucket.getPool().size()).sum(); } @ManagedAttribute("The number of pooled direct ByteBuffers that are available") @@ -264,7 +287,7 @@ public long getAvailableHeapByteBufferCount() private long getAvailableByteBufferCount(boolean direct) { RetainedBucket[] buckets = direct ? _direct : _indirect; - return Arrays.stream(buckets).mapToLong(bucket -> bucket.values().stream().filter(Pool.Entry::isIdle).count()).sum(); + return Arrays.stream(buckets).mapToLong(bucket -> bucket.getPool().values().stream().filter(Pool.Entry::isIdle).count()).sum(); } @ManagedAttribute("The bytes retained by direct ByteBuffers") @@ -306,7 +329,7 @@ private long getAvailableMemory(boolean direct) for (RetainedBucket bucket : buckets) { int capacity = bucket._capacity; - total += bucket.values().stream().filter(Pool.Entry::isIdle).count() * capacity; + total += bucket.getPool().values().stream().filter(Pool.Entry::isIdle).count() * capacity; } return total; } @@ -322,7 +345,7 @@ private void clearArray(RetainedBucket[] poolArray, AtomicLong memoryCounter) { for (RetainedBucket pool : poolArray) { - for (RetainedBucket.Entry entry : pool.values()) + for (Pool.Entry entry : pool.getPool().values()) { if (entry.remove()) { @@ -362,7 +385,7 @@ private void evict(boolean direct, long excess) { for (RetainedBucket bucket : buckets) { - RetainedBucket.Entry oldestEntry = findOldestEntry(now, bucket); + Pool.Entry oldestEntry = findOldestEntry(now, bucket.getPool()); if (oldestEntry == null) continue; @@ -408,13 +431,15 @@ public void dump(Appendable out, String indent) throws IOException private Pool.Entry findOldestEntry(long now, Pool bucket) { - RetainedBucket.Entry oldestEntry = null; - for (RetainedBucket.Entry entry : bucket.values()) + Pool.Entry oldestEntry = null; + for (Pool.Entry entry : bucket.values()) { if (oldestEntry != null) { - long entryAge = NanoTime.elapsed(entry.getPooled().getLastUpdate(), now); - if (entryAge > NanoTime.elapsed(oldestEntry.getPooled().getLastUpdate(), now)) + Buffer entryBuffer = (Buffer)entry.getPooled(); + long entryAge = NanoTime.elapsed(entryBuffer.getLastNanoTime(), now); + Buffer oldestEntryBuffer = (Buffer)oldestEntry.getPooled(); + if (entryAge > NanoTime.elapsed(oldestEntryBuffer.getLastNanoTime(), now)) oldestEntry = entry; } else @@ -425,22 +450,28 @@ private Pool.Entry findOldestEntry(long now, Pool + private static class RetainedBucket { + private final Pool _pool; private final int _capacity; - RetainedBucket(int capacity, int size) + private RetainedBucket(int capacity, int size) { - super(Pool.StrategyType.THREAD_ID, size, true); + _pool = new Pool<>(Pool.StrategyType.THREAD_ID, size, true); _capacity = capacity; } + public Pool getPool() + { + return _pool; + } + @Override public String toString() { int entries = 0; int inUse = 0; - for (Entry entry : values()) + for (Pool.Entry entry : _pool.values()) { entries++; if (entry.isInUse()) @@ -455,24 +486,53 @@ public String toString() } } + private static class Buffer extends AbstractRetainableByteBuffer + { + private final Consumer releaser; + private final AtomicLong lastNanoTime = new AtomicLong(NanoTime.now()); + + private Buffer(ByteBuffer buffer, Consumer releaser) + { + super(buffer); + this.releaser = releaser; + } + + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + { + lastNanoTime.setOpaque(NanoTime.now()); + releaser.accept(this); + } + return released; + } + + public long getLastNanoTime() + { + return lastNanoTime.getOpaque(); + } + } + /** * A variant of the {@link ArrayRetainableByteBufferPool} that * uses buckets of buffers that increase in size by a power of * 2 (eg 1k, 2k, 4k, 8k, etc.). */ - public static class ExponentialPool extends ArrayRetainableByteBufferPool + public static class Quadratic extends ArrayRetainableByteBufferPool { - public ExponentialPool() + public Quadratic() { this(0, -1, Integer.MAX_VALUE); } - public ExponentialPool(int minCapacity, int maxCapacity, int maxBucketSize) + public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize) { this(minCapacity, maxCapacity, maxBucketSize, -1L, -1L); } - public ExponentialPool(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory) + public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory) { super(minCapacity, -1, @@ -481,7 +541,8 @@ public ExponentialPool(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, maxDirectMemory, c -> 32 - Integer.numberOfLeadingZeros(c - 1), - i -> 1 << i); + i -> 1 << i + ); } } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java index 91b7ab623877..ac91515ec444 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java @@ -24,7 +24,7 @@ /** * Accumulates data into a list of ByteBuffers which can then be combined into a single buffer or written to an OutputStream. * The buffer list automatically grows as data is written to it, the buffers are taken from the - * supplied {@link ByteBufferPool} or freshly allocated if one is not supplied. + * supplied {@link RetainableByteBufferPool} or freshly allocated if one is not supplied. * * The method {@link #ensureBuffer(int, int)} is used to write directly to the last buffer stored in the buffer list, * if there is less than a certain amount of space available in that buffer then a new one will be allocated and returned instead. @@ -32,8 +32,8 @@ */ public class ByteBufferAccumulator implements AutoCloseable { - private final List _buffers = new ArrayList<>(); - private final ByteBufferPool _bufferPool; + private final List _buffers = new ArrayList<>(); + private final RetainableByteBufferPool _bufferPool; private final boolean _direct; public ByteBufferAccumulator() @@ -41,9 +41,9 @@ public ByteBufferAccumulator() this(null, false); } - public ByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct) + public ByteBufferAccumulator(RetainableByteBufferPool bufferPool, boolean direct) { - _bufferPool = (bufferPool == null) ? ByteBufferPool.NOOP : bufferPool; + _bufferPool = (bufferPool == null) ? new RetainableByteBufferPool.NonPooling() : bufferPool; _direct = direct; } @@ -55,22 +55,17 @@ public ByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct) public int getLength() { int length = 0; - for (ByteBuffer buffer : _buffers) + for (RetainableByteBuffer buffer : _buffers) length = Math.addExact(length, buffer.remaining()); return length; } - public ByteBufferPool getByteBufferPool() - { - return _bufferPool; - } - /** * Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator. * @param minAllocationSize new buffers will be allocated to have at least this size. * @return a buffer with at least {@code minSize} space to write into. */ - public ByteBuffer ensureBuffer(int minAllocationSize) + public RetainableByteBuffer ensureBuffer(int minAllocationSize) { return ensureBuffer(1, minAllocationSize); } @@ -81,15 +76,14 @@ public ByteBuffer ensureBuffer(int minAllocationSize) * @param minAllocationSize new buffers will be allocated to have at least this size. * @return a buffer with at least {@code minSize} space to write into. */ - public ByteBuffer ensureBuffer(int minSize, int minAllocationSize) + public RetainableByteBuffer ensureBuffer(int minSize, int minAllocationSize) { - ByteBuffer buffer = _buffers.isEmpty() ? BufferUtil.EMPTY_BUFFER : _buffers.get(_buffers.size() - 1); - if (BufferUtil.space(buffer) < minSize) + RetainableByteBuffer buffer = _buffers.isEmpty() ? null : _buffers.get(_buffers.size() - 1); + if (buffer == null || BufferUtil.space(buffer.getByteBuffer()) < minSize) { buffer = _bufferPool.acquire(minAllocationSize, _direct); _buffers.add(buffer); } - return buffer; } @@ -98,26 +92,27 @@ public void copyBytes(byte[] buf, int offset, int length) copyBuffer(BufferUtil.toBuffer(buf, offset, length)); } - public void copyBuffer(ByteBuffer buffer) + public void copyBuffer(ByteBuffer source) { - while (buffer.hasRemaining()) + while (source.hasRemaining()) { - ByteBuffer b = ensureBuffer(buffer.remaining()); - int pos = BufferUtil.flipToFill(b); - BufferUtil.put(buffer, b); - BufferUtil.flipToFlush(b, pos); + RetainableByteBuffer buffer = ensureBuffer(source.remaining()); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + int pos = BufferUtil.flipToFill(byteBuffer); + BufferUtil.put(source, byteBuffer); + BufferUtil.flipToFlush(byteBuffer, pos); } } /** * Take the combined buffer containing all content written to the accumulator. - * The caller is responsible for releasing this {@link ByteBuffer} back into the {@link ByteBufferPool}. + * The caller is responsible for releasing this {@link RetainableByteBuffer}. * @return a buffer containing all content written to the accumulator. - * @see #toByteBuffer() + * @see #toRetainableByteBuffer() */ - public ByteBuffer takeByteBuffer() + public RetainableByteBuffer takeRetainableByteBuffer() { - ByteBuffer combinedBuffer; + RetainableByteBuffer combinedBuffer; if (_buffers.size() == 1) { combinedBuffer = _buffers.get(0); @@ -127,28 +122,36 @@ public ByteBuffer takeByteBuffer() int length = getLength(); combinedBuffer = _bufferPool.acquire(length, _direct); - BufferUtil.clearToFill(combinedBuffer); - for (ByteBuffer buffer : _buffers) + ByteBuffer byteBuffer = combinedBuffer.getByteBuffer(); + BufferUtil.clearToFill(byteBuffer); + for (RetainableByteBuffer buffer : _buffers) { - combinedBuffer.put(buffer); - _bufferPool.release(buffer); + byteBuffer.put(buffer.getByteBuffer()); + buffer.release(); } - BufferUtil.flipToFlush(combinedBuffer, 0); + BufferUtil.flipToFlush(byteBuffer, 0); _buffers.clear(); return combinedBuffer; } + public ByteBuffer takeByteBuffer() + { + byte[] bytes = toByteArray(); + close(); + return ByteBuffer.wrap(bytes); + } + /** * Take the combined buffer containing all content written to the accumulator. - * The returned buffer is still contained within the accumulator and will be released back to the {@link ByteBufferPool} + * The returned buffer is still contained within the accumulator and will be released * when the accumulator is closed. * @return a buffer containing all content written to the accumulator. - * @see #takeByteBuffer() + * @see #takeRetainableByteBuffer() * @see #close() */ - public ByteBuffer toByteBuffer() + public RetainableByteBuffer toRetainableByteBuffer() { - ByteBuffer combinedBuffer = takeByteBuffer(); + RetainableByteBuffer combinedBuffer = takeRetainableByteBuffer(); _buffers.add(combinedBuffer); return combinedBuffer; } @@ -169,28 +172,28 @@ public byte[] toByteArray() return bytes; } - public void writeTo(ByteBuffer buffer) + public void writeTo(ByteBuffer byteBuffer) { - int pos = BufferUtil.flipToFill(buffer); - for (ByteBuffer bb : _buffers) + int pos = BufferUtil.flipToFill(byteBuffer); + for (RetainableByteBuffer buffer : _buffers) { - buffer.put(bb.slice()); + byteBuffer.put(buffer.getByteBuffer().slice()); } - BufferUtil.flipToFlush(buffer, pos); + BufferUtil.flipToFlush(byteBuffer, pos); } public void writeTo(OutputStream out) throws IOException { - for (ByteBuffer bb : _buffers) + for (RetainableByteBuffer buffer : _buffers) { - BufferUtil.writeTo(bb.slice(), out); + BufferUtil.writeTo(buffer.getByteBuffer().slice(), out); } } @Override public void close() { - _buffers.forEach(_bufferPool::release); + _buffers.forEach(RetainableByteBuffer::release); _buffers.clear(); } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java index 28f0fb2844dc..2bcf509cad7d 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java @@ -20,7 +20,7 @@ /** * This class implements an output stream in which the data is written into a list of ByteBuffer, * the buffer list automatically grows as data is written to it, the buffers are taken from the - * supplied {@link ByteBufferPool} or freshly allocated if one is not supplied. + * supplied {@link RetainableByteBufferPool} or freshly allocated if one is not supplied. * * Designed to mimic {@link java.io.ByteArrayOutputStream} but with better memory usage, and less copying. */ @@ -34,35 +34,30 @@ public ByteBufferOutputStream2() this(null, false); } - public ByteBufferOutputStream2(ByteBufferPool bufferPool, boolean direct) + public ByteBufferOutputStream2(RetainableByteBufferPool bufferPool, boolean direct) { - _accumulator = new ByteBufferAccumulator((bufferPool == null) ? ByteBufferPool.NOOP : bufferPool, direct); - } - - public ByteBufferPool getByteBufferPool() - { - return _accumulator.getByteBufferPool(); + _accumulator = new ByteBufferAccumulator(bufferPool == null ? new RetainableByteBufferPool.NonPooling() : bufferPool, direct); } /** * Take the combined buffer containing all content written to the OutputStream. - * The caller is responsible for releasing this {@link ByteBuffer} back into the {@link ByteBufferPool}. + * The caller is responsible for releasing this {@link ByteBuffer}. * @return a buffer containing all content written to the OutputStream. */ - public ByteBuffer takeByteBuffer() + public RetainableByteBuffer takeByteBuffer() { - return _accumulator.takeByteBuffer(); + return _accumulator.takeRetainableByteBuffer(); } /** * Take the combined buffer containing all content written to the OutputStream. - * The returned buffer is still contained within the OutputStream and will be released back to the {@link ByteBufferPool} + * The returned buffer is still contained within the OutputStream and will be released * when the OutputStream is closed. * @return a buffer containing all content written to the OutputStream. */ - public ByteBuffer toByteBuffer() + public RetainableByteBuffer toByteBuffer() { - return _accumulator.toByteBuffer(); + return _accumulator.toRetainableByteBuffer(); } /** diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java deleted file mode 100644 index 852f5512c56a..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java +++ /dev/null @@ -1,152 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.jetty.util.BufferUtil; - -/** - *

A {@link ByteBuffer} pool.

- *

Acquired buffers may be {@link #release(ByteBuffer) released} but they do not need to; - * if they are released, they may be recycled and reused, otherwise they will be garbage - * collected as usual.

- */ -public interface ByteBufferPool -{ - ByteBufferPool NOOP = new NoopByteBufferPool(); - - /** - *

Requests a {@link ByteBuffer} of the given size.

- *

The returned buffer may have a bigger capacity than the size being requested.

- * - * @param size the size of the buffer - * @param direct whether the buffer must be direct or not - * @return a buffer with at least the requested capacity, with position and limit set to 0. - * @see #release(ByteBuffer) - */ - ByteBuffer acquire(int size, boolean direct); - - /** - *

Returns a {@link ByteBuffer}, usually obtained with {@link #acquire(int, boolean)} - * (but not necessarily), making it available for recycling and reuse.

- * - * @param buffer the buffer to return - * @see #acquire(int, boolean) - */ - void release(ByteBuffer buffer); - - /** - *

Removes a {@link ByteBuffer} that was previously obtained with {@link #acquire(int, boolean)}.

- *

The buffer will not be available for further reuse.

- * - * @param buffer the buffer to remove - * @see #acquire(int, boolean) - * @see #release(ByteBuffer) - */ - default void remove(ByteBuffer buffer) - { - } - - /** - *

Creates a new ByteBuffer of the given capacity and the given directness.

- * - * @param capacity the ByteBuffer capacity - * @param direct the ByteBuffer directness - * @return a newly allocated ByteBuffer - */ - default ByteBuffer newByteBuffer(int capacity, boolean direct) - { - return direct ? BufferUtil.allocateDirect(capacity) : BufferUtil.allocate(capacity); - } - - /** - * Get this pool as a {@link RetainableByteBufferPool}, which supports reference counting of the - * buffers and possibly a more efficient lookup mechanism based on the {@link org.eclipse.jetty.util.Pool} class. - * @return This pool as a RetainableByteBufferPool. The same instance is always returned by multiple calls to this method. - */ - RetainableByteBufferPool asRetainableByteBufferPool(); - - class Lease - { - private final ByteBufferPool byteBufferPool; - private final List buffers; - private final List recycles; - - public Lease(ByteBufferPool byteBufferPool) - { - this.byteBufferPool = byteBufferPool; - this.buffers = new ArrayList<>(); - this.recycles = new ArrayList<>(); - } - - public ByteBuffer acquire(int capacity, boolean direct) - { - ByteBuffer buffer = byteBufferPool.acquire(capacity, direct); - BufferUtil.clearToFill(buffer); - return buffer; - } - - public void append(ByteBuffer buffer, boolean recycle) - { - buffers.add(buffer); - recycles.add(recycle); - } - - public void insert(int index, ByteBuffer buffer, boolean recycle) - { - buffers.add(index, buffer); - recycles.add(index, recycle); - } - - public List getByteBuffers() - { - return buffers; - } - - public long getTotalLength() - { - long length = 0; - for (ByteBuffer buffer : buffers) - { - length += buffer.remaining(); - } - return length; - } - - public int getSize() - { - return buffers.size(); - } - - public void recycle() - { - for (int i = 0; i < buffers.size(); ++i) - { - ByteBuffer buffer = buffers.get(i); - if (recycles.get(i)) - release(buffer); - } - buffers.clear(); - recycles.clear(); - } - - public void release(ByteBuffer buffer) - { - byteBufferPool.release(buffer); - } - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java index 1129e7bc6816..df900ad99b88 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java @@ -94,7 +94,7 @@ public static ClientConnector forUnixDomain(Path path) private final Configurator configurator; private Executor executor; private Scheduler scheduler; - private ByteBufferPool byteBufferPool; + private RetainableByteBufferPool retainableByteBufferPool; private SslContextFactory.Client sslContextFactory; private SelectorManager selectorManager; private int selectors = 1; @@ -155,17 +155,17 @@ public void setScheduler(Scheduler scheduler) this.scheduler = scheduler; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return retainableByteBufferPool; } - public void setByteBufferPool(ByteBufferPool byteBufferPool) + public void setRetainableByteBufferPool(RetainableByteBufferPool retainableByteBufferPool) { if (isStarted()) throw new IllegalStateException(); - updateBean(this.byteBufferPool, byteBufferPool); - this.byteBufferPool = byteBufferPool; + updateBean(this.retainableByteBufferPool, retainableByteBufferPool); + this.retainableByteBufferPool = retainableByteBufferPool; } public SslContextFactory.Client getSslContextFactory() @@ -366,8 +366,8 @@ protected void doStart() throws Exception } if (scheduler == null) setScheduler(new ScheduledExecutorScheduler(String.format("client-scheduler@%x", hashCode()), false)); - if (byteBufferPool == null) - setByteBufferPool(new MappedByteBufferPool()); + if (retainableByteBufferPool == null) + setRetainableByteBufferPool(new ArrayRetainableByteBufferPool()); if (sslContextFactory == null) setSslContextFactory(newSslContextFactory()); selectorManager = newSelectorManager(); diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LeakTrackingByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LeakTrackingByteBufferPool.java deleted file mode 100644 index 07a430e22da3..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LeakTrackingByteBufferPool.java +++ /dev/null @@ -1,163 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.nio.ByteBuffer; -import java.util.concurrent.atomic.AtomicLong; - -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.LeakDetector; -import org.eclipse.jetty.util.annotation.ManagedAttribute; -import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.ContainerLifeCycle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@ManagedObject -public class LeakTrackingByteBufferPool extends ContainerLifeCycle implements ByteBufferPool -{ - private static final Logger LOG = LoggerFactory.getLogger(LeakTrackingByteBufferPool.class); - - private final LeakDetector leakDetector = new LeakDetector() - { - @Override - public String id(ByteBuffer resource) - { - return BufferUtil.toIDString(resource); - } - - @Override - protected void leaked(LeakInfo leakInfo) - { - leaked.incrementAndGet(); - LeakTrackingByteBufferPool.this.leaked(leakInfo); - } - }; - - private final AtomicLong leakedAcquires = new AtomicLong(0); - private final AtomicLong leakedReleases = new AtomicLong(0); - private final AtomicLong leakedRemoves = new AtomicLong(0); - private final AtomicLong leaked = new AtomicLong(0); - private final ByteBufferPool delegate; - - public LeakTrackingByteBufferPool(ByteBufferPool delegate) - { - this.delegate = delegate; - addBean(leakDetector); - addBean(delegate); - } - - @Override - public RetainableByteBufferPool asRetainableByteBufferPool() - { - // the retainable pool is just a client of the normal pool, so no special handling required. - return delegate.asRetainableByteBufferPool(); - } - - @Override - public ByteBuffer acquire(int size, boolean direct) - { - ByteBuffer buffer = delegate.acquire(size, direct); - boolean acquired = leakDetector.acquired(buffer); - if (!acquired) - { - leakedAcquires.incrementAndGet(); - if (LOG.isDebugEnabled()) - LOG.debug("ByteBuffer leaked acquire for id {}", leakDetector.id(buffer), new Throwable("acquire")); - } - return buffer; - } - - @Override - public void release(ByteBuffer buffer) - { - if (buffer == null) - return; - boolean released = leakDetector.released(buffer); - if (!released) - { - leakedReleases.incrementAndGet(); - if (LOG.isDebugEnabled()) - LOG.debug("ByteBuffer leaked release for id {}", leakDetector.id(buffer), new Throwable("release")); - } - delegate.release(buffer); - } - - @Override - public void remove(ByteBuffer buffer) - { - if (buffer == null) - return; - boolean released = leakDetector.released(buffer); - if (!released) - { - leakedRemoves.incrementAndGet(); - if (LOG.isDebugEnabled()) - LOG.debug("ByteBuffer leaked remove for id {}", leakDetector.id(buffer), new Throwable("remove")); - } - delegate.remove(buffer); - } - - /** - * Clears the tracking data returned by {@link #getLeakedAcquires()}, - * {@link #getLeakedReleases()}, {@link #getLeakedResources()}. - */ - @ManagedAttribute("Clears the tracking data") - public void clearTracking() - { - leakedAcquires.set(0); - leakedReleases.set(0); - } - - /** - * @return count of ByteBufferPool.acquire() calls that detected a leak - */ - @ManagedAttribute("The number of acquires that produced a leak") - public long getLeakedAcquires() - { - return leakedAcquires.get(); - } - - /** - * @return count of ByteBufferPool.release() calls that detected a leak - */ - @ManagedAttribute("The number of releases that produced a leak") - public long getLeakedReleases() - { - return leakedReleases.get(); - } - - /** - * @return count of ByteBufferPool.remove() calls that detected a leak - */ - @ManagedAttribute("The number of removes that produced a leak") - public long getLeakedRemoves() - { - return leakedRemoves.get(); - } - - /** - * @return count of resources that were acquired but not released - */ - @ManagedAttribute("The number of resources that were leaked") - public long getLeakedResources() - { - return leaked.get(); - } - - protected void leaked(LeakDetector.LeakInfo leakInfo) - { - LOG.warn("ByteBuffer {} leaked at: {}", leakInfo.getResourceDescription(), leakInfo.getStackFrames()); - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LogarithmicArrayByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LogarithmicArrayByteBufferPool.java deleted file mode 100644 index 72dd85d21e91..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/LogarithmicArrayByteBufferPool.java +++ /dev/null @@ -1,164 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import org.eclipse.jetty.util.NanoTime; - -/** - * Extension of the {@link ArrayByteBufferPool} whose bucket sizes increase exponentially instead of linearly. - * Each bucket will be double the size of the previous bucket, this decreases the amounts of buckets required - * which can lower total memory usage if buffers are often being acquired of different sizes. However as there are - * fewer buckets this will also increase the contention on each bucket. - */ -public class LogarithmicArrayByteBufferPool extends ArrayByteBufferPool -{ - // TODO test this class and use it! - - /** - * Creates a new ByteBufferPool with a default configuration. - */ - public LogarithmicArrayByteBufferPool() - { - this(-1, -1, -1); - } - - /** - * Creates a new ByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param maxCapacity the maximum ByteBuffer capacity - */ - public LogarithmicArrayByteBufferPool(int minCapacity, int maxCapacity) - { - this(minCapacity, maxCapacity, -1, -1, -1); - } - - /** - * Creates a new ByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxQueueLength the maximum ByteBuffer queue length - */ - public LogarithmicArrayByteBufferPool(int minCapacity, int maxCapacity, int maxQueueLength) - { - this(minCapacity, maxCapacity, maxQueueLength, -1, -1); - } - - /** - * Creates a new ByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxQueueLength the maximum ByteBuffer queue length - * @param maxHeapMemory the max heap memory in bytes - * @param maxDirectMemory the max direct memory in bytes - */ - public LogarithmicArrayByteBufferPool(int minCapacity, int maxCapacity, int maxQueueLength, long maxHeapMemory, long maxDirectMemory) - { - this(minCapacity, maxCapacity, maxQueueLength, maxHeapMemory, maxDirectMemory, maxHeapMemory, maxDirectMemory); - } - - /** - * Creates a new ByteBufferPool with the given configuration. - * - * @param minCapacity the minimum ByteBuffer capacity - * @param maxCapacity the maximum ByteBuffer capacity - * @param maxQueueLength the maximum ByteBuffer queue length - * @param maxHeapMemory the max heap memory in bytes - * @param maxDirectMemory the max direct memory in bytes - * @param retainedHeapMemory the max heap memory in bytes, -1 for unlimited retained memory or 0 to use default heuristic - * @param retainedDirectMemory the max direct memory in bytes, -1 for unlimited retained memory or 0 to use default heuristic - */ - public LogarithmicArrayByteBufferPool(int minCapacity, int maxCapacity, int maxQueueLength, long maxHeapMemory, long maxDirectMemory, long retainedHeapMemory, long retainedDirectMemory) - { - super(minCapacity, -1, maxCapacity, maxQueueLength, maxHeapMemory, maxDirectMemory, retainedHeapMemory, retainedDirectMemory); - } - - @Override - protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - return new LogarithmicRetainablePool(0, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - - @Override - protected int bucketFor(int capacity) - { - return 32 - Integer.numberOfLeadingZeros(capacity - 1); - } - - @Override - protected int capacityFor(int bucket) - { - return 1 << bucket; - } - - @Override - protected void releaseMemory(boolean direct) - { - long oldest = Long.MAX_VALUE; - int index = -1; - Bucket[] buckets = bucketsFor(direct); - for (int i = 0; i < buckets.length; ++i) - { - Bucket bucket = buckets[i]; - if (bucket.isEmpty()) - continue; - long lastUpdateNanoTime = bucket.getLastUpdate(); - if (oldest == Long.MAX_VALUE || NanoTime.isBefore(lastUpdateNanoTime, oldest)) - { - oldest = lastUpdateNanoTime; - index = i; - } - } - if (index >= 0) - { - Bucket bucket = buckets[index]; - // Acquire a buffer but never return it to the pool. - bucket.acquire(); - bucket.resetUpdateTime(); - } - } - - /** - * A variant of the {@link ArrayRetainableByteBufferPool} that - * uses buckets of buffers that increase in size by a power of - * 2 (eg 1k, 2k, 4k, 8k, etc.). - */ - public static class LogarithmicRetainablePool extends ArrayRetainableByteBufferPool - { - public LogarithmicRetainablePool() - { - this(0, -1, Integer.MAX_VALUE); - } - - public LogarithmicRetainablePool(int minCapacity, int maxCapacity, int maxBucketSize) - { - this(minCapacity, maxCapacity, maxBucketSize, -1L, -1L); - } - - public LogarithmicRetainablePool(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory) - { - super(minCapacity, - -1, - maxCapacity, - maxBucketSize, - maxHeapMemory, - maxDirectMemory, - c -> 32 - Integer.numberOfLeadingZeros(c - 1), - i -> 1 << i - ); - } - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java deleted file mode 100644 index 6d484e120a5b..000000000000 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java +++ /dev/null @@ -1,340 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; - -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.NanoTime; -import org.eclipse.jetty.util.annotation.ManagedAttribute; -import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.Dumpable; -import org.eclipse.jetty.util.component.DumpableCollection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - *

A ByteBuffer pool where ByteBuffers are held in queues that are held in a Map.

- *

Given a capacity {@code factor} of 1024, the Map entry with key {@code 1} holds a - * queue of ByteBuffers each of capacity 1024, the Map entry with key {@code 2} holds a - * queue of ByteBuffers each of capacity 2048, and so on.

- */ -@ManagedObject -public class MappedByteBufferPool extends AbstractByteBufferPool implements Dumpable -{ - // TODO Logarithmic buffer sizes - - private static final Logger LOG = LoggerFactory.getLogger(MappedByteBufferPool.class); - - private final ConcurrentMap _directBuffers = new ConcurrentHashMap<>(); - private final ConcurrentMap _heapBuffers = new ConcurrentHashMap<>(); - private final Function _newBucket; - private boolean _detailedDump = false; - - /** - * Creates a new MappedByteBufferPool with a default configuration. - */ - public MappedByteBufferPool() - { - this(-1); - } - - /** - * Creates a new MappedByteBufferPool with the given capacity factor. - * - * @param factor the capacity factor - */ - public MappedByteBufferPool(int factor) - { - this(factor, -1); - } - - /** - * Creates a new MappedByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer bucket size - */ - public MappedByteBufferPool(int factor, int maxBucketSize) - { - this(factor, maxBucketSize, null); - } - - /** - * Creates a new MappedByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer bucket size - * @param newBucket the function that creates a Bucket - */ - private MappedByteBufferPool(int factor, int maxBucketSize, Function newBucket) - { - this(factor, maxBucketSize, newBucket, 0, 0, 0, 0); - } - - /** - * Creates a new MappedByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer bucket size - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - */ - public MappedByteBufferPool(int factor, int maxBucketSize, long maxHeapMemory, long maxDirectMemory) - { - this(factor, maxBucketSize, null, maxHeapMemory, maxDirectMemory, maxHeapMemory, maxDirectMemory); - } - - /** - * Creates a new MappedByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer bucket size - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - * @param retainedHeapMemory the max heap memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - * @param retainedDirectMemory the max direct memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - */ - public MappedByteBufferPool(int factor, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, long retainedHeapMemory, long retainedDirectMemory) - { - this(factor, maxBucketSize, null, maxHeapMemory, maxDirectMemory, retainedHeapMemory, retainedDirectMemory); - } - - /** - * Creates a new MappedByteBufferPool with the given configuration. - * - * @param factor the capacity factor - * @param maxBucketSize the maximum ByteBuffer bucket size - * @param newBucket the function that creates a Bucket - * @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - * @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic. - * @param retainedHeapMemory the max heap memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - * @param retainedDirectMemory the max direct memory in bytes, -2 for no retained memory, -1 for unlimited retained memory or 0 to use default heuristic - */ - private MappedByteBufferPool(int factor, int maxBucketSize, Function newBucket, long maxHeapMemory, long maxDirectMemory, long retainedHeapMemory, long retainedDirectMemory) - { - super(factor, 0, maxBucketSize, maxHeapMemory, maxDirectMemory, retainedHeapMemory, retainedDirectMemory); - _newBucket = newBucket; - } - - @Override - protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - return new Retained(factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - - private Bucket newBucket(int key, boolean direct) - { - return (_newBucket != null) ? _newBucket.apply(key) : new Bucket(capacityFor(key), getMaxBucketSize(), updateMemory(direct)); - } - - @Override - public ByteBuffer acquire(int size, boolean direct) - { - int b = bucketFor(size); - int capacity = capacityFor(b); - ConcurrentMap buffers = bucketsFor(direct); - Bucket bucket = buffers.get(b); - if (bucket == null) - return newByteBuffer(capacity, direct); - ByteBuffer buffer = bucket.acquire(); - if (buffer == null) - return newByteBuffer(capacity, direct); - return buffer; - } - - @Override - public void release(ByteBuffer buffer) - { - if (buffer == null) - return; // nothing to do - - int capacity = buffer.capacity(); - int b = bucketFor(capacity); - // Validate that this buffer is from this pool. - if (capacity != capacityFor(b)) - { - if (LOG.isDebugEnabled()) - LOG.debug("ByteBuffer {} does not belong to this pool, discarding it", BufferUtil.toDetailString(buffer)); - return; - } - - boolean direct = buffer.isDirect(); - ConcurrentMap buckets = bucketsFor(direct); - Bucket bucket = buckets.computeIfAbsent(b, i -> newBucket(i, direct)); - bucket.release(buffer); - releaseExcessMemory(direct, this::releaseMemory); - } - - @Override - public void clear() - { - super.clear(); - _directBuffers.values().forEach(Bucket::clear); - _directBuffers.clear(); - _heapBuffers.values().forEach(Bucket::clear); - _heapBuffers.clear(); - } - - protected void releaseMemory(boolean direct) - { - long oldest = Long.MAX_VALUE; - int index = -1; - ConcurrentMap buckets = bucketsFor(direct); - for (Map.Entry entry : buckets.entrySet()) - { - Bucket bucket = entry.getValue(); - if (bucket.isEmpty()) - continue; - - long lastUpdateNanoTime = bucket.getLastUpdate(); - if (oldest == Long.MAX_VALUE || NanoTime.isBefore(lastUpdateNanoTime, oldest)) - { - oldest = lastUpdateNanoTime; - index = entry.getKey(); - } - } - if (index >= 0) - { - Bucket bucket = buckets.remove(index); - // Null guard in case this.clear() is called concurrently. - if (bucket != null) - bucket.clear(); - } - } - - protected int bucketFor(int capacity) - { - return (int)Math.ceil((double)capacity / getCapacityFactor()); - } - - protected int capacityFor(int bucket) - { - return bucket * getCapacityFactor(); - } - - @ManagedAttribute("The number of pooled direct ByteBuffers") - public long getDirectByteBufferCount() - { - return getByteBufferCount(true); - } - - @ManagedAttribute("The number of pooled heap ByteBuffers") - public long getHeapByteBufferCount() - { - return getByteBufferCount(false); - } - - private long getByteBufferCount(boolean direct) - { - return bucketsFor(direct).values().stream() - .mapToLong(Bucket::size) - .sum(); - } - - // Package local for testing - ConcurrentMap bucketsFor(boolean direct) - { - return direct ? _directBuffers : _heapBuffers; - } - - public static class Tagged extends MappedByteBufferPool - { - private final AtomicInteger tag = new AtomicInteger(); - - @Override - public ByteBuffer newByteBuffer(int capacity, boolean direct) - { - ByteBuffer buffer = super.newByteBuffer(capacity + 4, direct); - buffer.limit(buffer.capacity()); - buffer.putInt(tag.incrementAndGet()); - ByteBuffer slice = buffer.slice(); - BufferUtil.clear(slice); - return slice; - } - } - - public boolean isDetailedDump() - { - return _detailedDump; - } - - public void setDetailedDump(boolean detailedDump) - { - _detailedDump = detailedDump; - } - - @Override - public void dump(Appendable out, String indent) throws IOException - { - List dump = new ArrayList<>(); - dump.add(String.format("HeapMemory: %d/%d", getHeapMemory(), getMaxHeapMemory())); - dump.add(String.format("DirectMemory: %d/%d", getDirectMemory(), getMaxDirectMemory())); - - if (isDetailedDump()) - { - dump.add(new DumpableCollection("Indirect Buckets", _heapBuffers.values())); - dump.add(new DumpableCollection("Direct Buckets", _directBuffers.values())); - } - else - { - dump.add("Indirect Buckets size=" + _heapBuffers.size()); - dump.add("Direct Buckets size=" + _directBuffers.size()); - } - Dumpable.dumpObjects(out, indent, this, dump); - } - - @Override - public String toString() - { - return String.format("%s@%x{maxQueueLength=%s, factor=%s}", - this.getClass().getSimpleName(), hashCode(), - getMaxBucketSize(), - getCapacityFactor()); - } - - protected class Retained extends ArrayRetainableByteBufferPool - { - public Retained(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory) - { - super(0, factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory); - } - - @Override - protected ByteBuffer allocate(int capacity) - { - return MappedByteBufferPool.this.acquire(capacity, false); - } - - @Override - protected ByteBuffer allocateDirect(int capacity) - { - return MappedByteBufferPool.this.acquire(capacity, true); - } - - @Override - protected void removed(RetainableByteBuffer retainedBuffer) - { - MappedByteBufferPool.this.release(retainedBuffer.getBuffer()); - } - } -} diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Retainable.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Retainable.java index d8e7dcabff5c..a6b771621218 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Retainable.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Retainable.java @@ -124,12 +124,20 @@ protected ReferenceCounter(int initialCount) references = new AtomicInteger(initialCount); } + /** + * @return the current reference count + */ + public int get() + { + return references.get(); + } + /** *

Updates the reference count from {@code 0} to {@code 1}.

*

This method should only be used when this resource is acquired * from a pool.

*/ - protected void acquire() + public void acquire() { if (references.getAndUpdate(c -> c == 0 ? 1 : c) != 0) throw new IllegalStateException("acquired while in use " + this); @@ -178,7 +186,7 @@ public boolean isRetained() @Override public String toString() { - return String.format("%s@%x[r=%d]", getClass().getSimpleName(), hashCode(), references.get()); + return String.format("%s@%x[r=%d]", getClass().getSimpleName(), hashCode(), get()); } } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBuffer.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBuffer.java index bba96a557779..cd88868c055b 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBuffer.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBuffer.java @@ -14,97 +14,151 @@ package org.eclipse.jetty.io; import java.nio.ByteBuffer; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.Consumer; +import org.eclipse.jetty.io.internal.NonRetainableByteBuffer; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.NanoTime; /** - *

A pooled ByteBuffer which maintains a reference count that is - * incremented with {@link #retain()} and decremented with {@link #release()}. The buffer - * is released to the pool when {@link #release()} is called one more time than {@link #retain()}.

- *

A {@code RetainableByteBuffer} can either be: + *

A pooled {@link ByteBuffer} which maintains a reference count that is + * incremented with {@link #retain()} and decremented with {@link #release()}.

+ *

The {@code ByteBuffer} is released to a {@link RetainableByteBufferPool} + * when {@link #release()} is called one more time than {@link #retain()}; + * in such case, the call to {@link #release()} returns {@code true}.

+ *

A {@code RetainableByteBuffer} can either be:

*
    - *
  • in pool; in this case {@link #isRetained()} returns {@code false} and calling {@link #release()} throws {@link IllegalStateException}
  • - *
  • out of pool but not retained; in this case {@link #isRetained()} returns {@code false} and calling {@link #release()} returns {@code true}
  • - *
  • out of pool and retained; in this case {@link #isRetained()} returns {@code true} and calling {@link #release()} returns {@code false}
  • + *
  • in pool; in this case {@link #isRetained()} returns {@code false} + * and calling {@link #release()} throws {@link IllegalStateException}
  • + *
  • out of pool but not retained; in this case {@link #isRetained()} + * returns {@code false} and calling {@link #release()} returns {@code true}
  • + *
  • out of pool and retained; in this case {@link #isRetained()} + * returns {@code true} and calling {@link #release()} returns {@code false}
  • *
- *

Calling {@link #release()} on a out of pool and retained instance does not re-pool it while that re-pools it on a out of pool but not retained instance.

*/ -public class RetainableByteBuffer extends Retainable.ReferenceCounter +public interface RetainableByteBuffer extends Retainable { - private final ByteBuffer buffer; - private final Consumer releaser; - private final AtomicLong lastUpdate = new AtomicLong(NanoTime.now()); - - RetainableByteBuffer(ByteBuffer buffer, Consumer releaser) + /** + *

Returns a non-retainable {@code RetainableByteBuffer} that wraps + * the given {@code ByteBuffer}.

+ *

Use this method to wrap user-provided {@code ByteBuffer}s, or + * {@code ByteBuffer}s that hold constant bytes, to make them look + * like {@code RetainableByteBuffer}s.

+ *

The returned {@code RetainableByteBuffer} {@link #canRetain()} + * method always returns {@code false}.

+ * + * @param byteBuffer the {@code ByteBuffer} to wrap + * @return a non-retainable {@code RetainableByteBuffer} + */ + public static RetainableByteBuffer asNonRetainable(ByteBuffer byteBuffer) { - super(0); - this.releaser = releaser; - this.buffer = buffer; + return new NonRetainableByteBuffer(byteBuffer); } - public int capacity() + /** + * @return whether this instance is retained + * @see ReferenceCounter#isRetained() + */ + public boolean isRetained(); + + /** + * @return the wrapped {@code ByteBuffer} + */ + public ByteBuffer getByteBuffer(); + + /** + * @return whether the {@code ByteBuffer} is direct + */ + public default boolean isDirect() { - return buffer.capacity(); + return getByteBuffer().isDirect(); } - public ByteBuffer getBuffer() + /** + * @return the number of remaining bytes in the {@code ByteBuffer} + */ + public default int remaining() { - return buffer; + return getByteBuffer().remaining(); } - public long getLastUpdate() + /** + * @return whether the {@code ByteBuffer} has remaining bytes + */ + public default boolean hasRemaining() { - return lastUpdate.getOpaque(); + return getByteBuffer().hasRemaining(); } - public boolean isDirect() + /** + * @return the {@code ByteBuffer} capacity + */ + public default int capacity() { - return buffer.isDirect(); + return getByteBuffer().capacity(); } - protected void acquire() + /** + * @see BufferUtil#clear(ByteBuffer) + */ + public default void clear() { - // Overridden for visibility. - super.acquire(); + BufferUtil.clear(getByteBuffer()); } - public boolean release() + /** + * A wrapper for {@link RetainableByteBuffer} instances + */ + public class Wrapper extends Retainable.Wrapper implements RetainableByteBuffer { - boolean released = super.release(); - if (released) + public Wrapper(RetainableByteBuffer wrapped) { - lastUpdate.setOpaque(NanoTime.now()); - releaser.accept(this); + super(wrapped); } - return released; - } - public int remaining() - { - return buffer.remaining(); - } + public RetainableByteBuffer getWrapped() + { + return (RetainableByteBuffer)super.getWrapped(); + } - public boolean hasRemaining() - { - return remaining() > 0; - } + @Override + public boolean isRetained() + { + return getWrapped().isRetained(); + } - public boolean isEmpty() - { - return !hasRemaining(); - } + @Override + public ByteBuffer getByteBuffer() + { + return getWrapped().getByteBuffer(); + } - public void clear() - { - BufferUtil.clear(buffer); - } + @Override + public boolean isDirect() + { + return getWrapped().isDirect(); + } - @Override - public String toString() - { - return "%s[%s]".formatted(super.toString(), BufferUtil.toDetailString(buffer)); + @Override + public int remaining() + { + return getWrapped().remaining(); + } + + @Override + public boolean hasRemaining() + { + return getWrapped().hasRemaining(); + } + + @Override + public int capacity() + { + return getWrapped().capacity(); + } + + @Override + public void clear() + { + getWrapped().clear(); + } } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java index eeaa8d8c16f2..314662b59757 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/RetainableByteBufferPool.java @@ -14,61 +14,177 @@ package org.eclipse.jetty.io; import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.util.BufferUtil; /** - *

A {@link RetainableByteBuffer} pool.

- *

Acquired buffers must be released by calling {@link RetainableByteBuffer#release()} otherwise the memory they hold will - * be leaked.

+ *

A pool for {@link RetainableByteBuffer} instances.

+ *

{@link RetainableByteBuffer} that are {@link #acquire(int, boolean) acquired} + * must be released by calling {@link RetainableByteBuffer#release()} + * otherwise the memory they hold will be leaked.

+ * + *

API NOTE

+ *

This interface does not have a symmetric {@code release(RetainableByteBuffer)} + * method, because it will be confusing to use due to the fact that the acquired instance + * is-a {@link Retainable}.

+ *

Imagine this (hypothetical) code sequence:

+ *
{@code
+ * RetainableByteBuffer buffer = pool.acquire(size, direct);
+ * buffer.retain();
+ * pool.release(buffer);
+ * }
+ *

The hypothetical call to {@code release(RetainableByteBuffer)} would appear to + * release the buffer to the pool, but in fact the buffer is retained one more time + * (and therefore still in use) and not really released to the pool. + * For this reason there is no {@code release(RetainableByteBuffer)} method.

+ *

Therefore, in order to track acquire/release counts both the pool and the + * buffer returned by {@link #acquire(int, boolean)} must be wrapped, see + * {@link RetainableByteBuffer.Wrapper}

*/ public interface RetainableByteBufferPool { /** - * Acquires a memory buffer from the pool. + *

Acquires a {@link RetainableByteBuffer} from this pool.

+ * * @param size The size of the buffer. The returned buffer will have at least this capacity. * @param direct true if a direct memory buffer is needed, false otherwise. - * @return a memory buffer with position and size set to 0. + * @return a {@link RetainableByteBuffer} with position and limit set to 0. */ RetainableByteBuffer acquire(int size, boolean direct); + /** + *

Removes all {@link RetainableByteBuffer#isRetained() non-retained} + * pooled instances from this pool.

+ */ void clear(); - static RetainableByteBufferPool from(ByteBufferPool byteBufferPool) + /** + *

A wrapper for {@link RetainableByteBufferPool} instances.

+ */ + class Wrapper implements RetainableByteBufferPool { - return new NotRetainedByteBufferPool(byteBufferPool); - } + private final RetainableByteBufferPool wrapped; - class NotRetainedByteBufferPool implements RetainableByteBufferPool - { - private final ByteBufferPool _byteBufferPool; + public Wrapper(RetainableByteBufferPool wrapped) + { + this.wrapped = wrapped; + } - public NotRetainedByteBufferPool(ByteBufferPool byteBufferPool) + public RetainableByteBufferPool getWrapped() { - _byteBufferPool = byteBufferPool; + return wrapped; } @Override public RetainableByteBuffer acquire(int size, boolean direct) { - ByteBuffer byteBuffer = _byteBufferPool.acquire(size, direct); - RetainableByteBuffer retainableByteBuffer = new RetainableByteBuffer(byteBuffer, this::release); - retainableByteBuffer.acquire(); - return retainableByteBuffer; + return getWrapped().acquire(size, direct); } - private void release(RetainableByteBuffer retainedBuffer) + @Override + public void clear() { - _byteBufferPool.release(retainedBuffer.getBuffer()); + getWrapped().clear(); } + } + /** + *

A {@link RetainableByteBufferPool} that does not pool its + * {@link RetainableByteBuffer}s.

+ *

The returned {@code RetainableByteBuffer}s are reference + * counted.

+ */ + class NonPooling implements RetainableByteBufferPool + { @Override - public void clear() + public RetainableByteBuffer acquire(int size, boolean direct) { + return new Buffer(BufferUtil.allocate(size, direct)); } @Override - public String toString() + public void clear() + { + } + + private static class Buffer extends AbstractRetainableByteBuffer + { + private Buffer(ByteBuffer byteBuffer) + { + super(byteBuffer); + acquire(); + } + } + } + + /** + *

Accumulates a sequence of {@link RetainableByteBuffer} that + * are typically created during the generation of protocol bytes.

+ *

{@code RetainableByteBuffer}s can be either + * {@link #append(RetainableByteBuffer) appended} to the sequence, + * or {@link #insert(int, RetainableByteBuffer) inserted} at a + * specific position in the sequence, and then + * {@link #release() released} when they are consumed.

+ */ + class Accumulator + { + private final RetainableByteBufferPool bufferPool; + private final List buffers; + private final List byteBuffers; + + public Accumulator(RetainableByteBufferPool bufferPool) + { + this.bufferPool = bufferPool; + this.buffers = new ArrayList<>(); + this.byteBuffers = new ArrayList<>(); + } + + public RetainableByteBuffer acquire(int capacity, boolean direct) + { + RetainableByteBuffer buffer = bufferPool.acquire(capacity, direct); + BufferUtil.clearToFill(buffer.getByteBuffer()); + return buffer; + } + + public void append(RetainableByteBuffer buffer) + { + buffers.add(buffer); + byteBuffers.add(buffer.getByteBuffer()); + } + + public void insert(int index, RetainableByteBuffer buffer) + { + buffers.add(index, buffer); + byteBuffers.add(index, buffer.getByteBuffer()); + } + + public List getByteBuffers() + { + return byteBuffers; + } + + public long getTotalLength() + { + long length = 0; + for (ByteBuffer buffer : byteBuffers) + { + length += buffer.remaining(); + } + return length; + } + + public int getSize() + { + return byteBuffers.size(); + } + + public void release() { - return String.format("NonRetainableByteBufferPool@%x{%s}", hashCode(), _byteBufferPool.toString()); + buffers.forEach(RetainableByteBuffer::release); + buffers.clear(); + byteBuffers.clear(); } } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/InputStreamContentSource.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/InputStreamContentSource.java index 167317f425aa..3f50132771d1 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/InputStreamContentSource.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/InputStreamContentSource.java @@ -16,9 +16,7 @@ import java.io.InputStream; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.IO; @@ -29,9 +27,9 @@ *

* A {@link Content.Source} that is backed by an {@link InputStream}. * Data is read from the {@link InputStream} into a buffer that is optionally acquired - * from a {@link ByteBufferPool}, and converted to a {@link Content.Chunk} that is - * returned from {@link #read()}. If no {@link ByteBufferPool} is provided, then - * a {@link NoopByteBufferPool} is used. + * from a {@link RetainableByteBufferPool}, and converted to a {@link Content.Chunk} that is + * returned from {@link #read()}. If no {@link RetainableByteBufferPool} is provided, then + * a {@link RetainableByteBufferPool.NonPooling} is used. *

*/ public class InputStreamContentSource implements Content.Source @@ -47,18 +45,13 @@ public class InputStreamContentSource implements Content.Source public InputStreamContentSource(InputStream inputStream) { - this(inputStream, (ByteBufferPool)null); - } - - public InputStreamContentSource(InputStream inputStream, ByteBufferPool bufferPool) - { - this(inputStream, (bufferPool == null ? ByteBufferPool.NOOP : bufferPool).asRetainableByteBufferPool()); + this(inputStream, null); } public InputStreamContentSource(InputStream inputStream, RetainableByteBufferPool bufferPool) { this.inputStream = inputStream; - this.bufferPool = bufferPool == null ? ByteBufferPool.NOOP.asRetainableByteBufferPool() : bufferPool; + this.bufferPool = bufferPool != null ? bufferPool : new RetainableByteBufferPool.NonPooling(); } public int getBufferSize() @@ -85,7 +78,7 @@ public Content.Chunk read() RetainableByteBuffer streamBuffer = bufferPool.acquire(getBufferSize(), false); try { - ByteBuffer buffer = streamBuffer.getBuffer(); + ByteBuffer buffer = streamBuffer.getByteBuffer(); int read = inputStream.read(buffer.array(), buffer.arrayOffset(), buffer.capacity()); if (read < 0) { diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/PathContentSource.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/PathContentSource.java index 5c3491c649b3..04e05a6eb700 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/PathContentSource.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/content/PathContentSource.java @@ -23,7 +23,6 @@ import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.io.RetainableByteBufferPool; @@ -51,12 +50,7 @@ public class PathContentSource implements Content.Source public PathContentSource(Path path) { - this(path, (ByteBufferPool)null); - } - - public PathContentSource(Path path, ByteBufferPool byteBufferPool) - { - this(path, (byteBufferPool == null ? ByteBufferPool.NOOP : byteBufferPool).asRetainableByteBufferPool()); + this(path, null); } public PathContentSource(Path path, RetainableByteBufferPool byteBufferPool) @@ -69,7 +63,7 @@ public PathContentSource(Path path, RetainableByteBufferPool byteBufferPool) throw new AccessDeniedException(path.toString()); this.path = path; this.length = Files.size(path); - this.byteBufferPool = byteBufferPool == null ? ByteBufferPool.NOOP.asRetainableByteBufferPool() : byteBufferPool; + this.byteBufferPool = byteBufferPool != null ? byteBufferPool : new RetainableByteBufferPool.NonPooling(); } catch (IOException x) { @@ -135,7 +129,7 @@ public Content.Chunk read() return Content.Chunk.EOF; RetainableByteBuffer retainableByteBuffer = byteBufferPool.acquire(getBufferSize(), isUseDirectByteBuffers()); - ByteBuffer byteBuffer = retainableByteBuffer.getBuffer(); + ByteBuffer byteBuffer = retainableByteBuffer.getByteBuffer(); int read; try diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NoopByteBufferPool.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/internal/NonRetainableByteBuffer.java similarity index 52% rename from jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NoopByteBufferPool.java rename to jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/internal/NonRetainableByteBuffer.java index 9c3ba95f26df..3c54bb4bfe21 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/NoopByteBufferPool.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/internal/NonRetainableByteBuffer.java @@ -11,34 +11,30 @@ // ======================================================================== // -package org.eclipse.jetty.io; +package org.eclipse.jetty.io.internal; import java.nio.ByteBuffer; -import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.io.RetainableByteBuffer; -public class NoopByteBufferPool implements ByteBufferPool +public class NonRetainableByteBuffer implements RetainableByteBuffer { - private final RetainableByteBufferPool _retainableByteBufferPool = RetainableByteBufferPool.from(this); + private final ByteBuffer byteBuffer; - @Override - public ByteBuffer acquire(int size, boolean direct) + public NonRetainableByteBuffer(ByteBuffer byteBuffer) { - if (direct) - return BufferUtil.allocateDirect(size); - else - return BufferUtil.allocate(size); + this.byteBuffer = byteBuffer; } @Override - public void release(ByteBuffer buffer) + public boolean isRetained() { - BufferUtil.clear(buffer); + return false; } @Override - public RetainableByteBufferPool asRetainableByteBufferPool() + public ByteBuffer getByteBuffer() { - return _retainableByteBufferPool; + return byteBuffer; } } diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslClientConnectionFactory.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslClientConnectionFactory.java index 9e166e495503..826cab328134 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslClientConnectionFactory.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslClientConnectionFactory.java @@ -24,11 +24,11 @@ import javax.net.ssl.SSLException; import javax.net.ssl.SSLPeerUnverifiedException; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -40,17 +40,17 @@ public class SslClientConnectionFactory implements ClientConnectionFactory public static final String SSL_ENGINE_CONTEXT_KEY = "org.eclipse.jetty.client.ssl.engine"; private final SslContextFactory sslContextFactory; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool retainableByteBufferPool; private final Executor executor; private final ClientConnectionFactory connectionFactory; private boolean _directBuffersForEncryption = true; private boolean _directBuffersForDecryption = true; private boolean _requireCloseMessage; - public SslClientConnectionFactory(SslContextFactory sslContextFactory, ByteBufferPool byteBufferPool, Executor executor, ClientConnectionFactory connectionFactory) + public SslClientConnectionFactory(SslContextFactory sslContextFactory, RetainableByteBufferPool retainableByteBufferPool, Executor executor, ClientConnectionFactory connectionFactory) { this.sslContextFactory = Objects.requireNonNull(sslContextFactory, "Missing SslContextFactory"); - this.byteBufferPool = byteBufferPool; + this.retainableByteBufferPool = retainableByteBufferPool; this.executor = executor; this.connectionFactory = connectionFactory; } @@ -119,7 +119,7 @@ public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map handshakeListeners = new ArrayList<>(); private final AtomicLong _bytesIn = new AtomicLong(); private final AtomicLong _bytesOut = new AtomicLong(); - private final ByteBufferPool _bufferPool; private final RetainableByteBufferPool _retainableByteBufferPool; private final SSLEngine _sslEngine; private final DecryptedEndPoint _decryptedEndPoint; - private ByteBuffer _decryptedInput; - private RetainableByteBuffer _encryptedInput; - private ByteBuffer _encryptedOutput; private final boolean _encryptedDirectBuffers; private final boolean _decryptedDirectBuffers; + private RetainableByteBuffer _decryptedInput; + private RetainableByteBuffer _encryptedInput; + private RetainableByteBuffer _encryptedOutput; private boolean _renegotiationAllowed; private int _renegotiationLimit = -1; private boolean _closedOutbound; @@ -166,24 +164,17 @@ public String toString() } }; - public SslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine) - { - this(byteBufferPool, executor, endPoint, sslEngine, false, false); - } - - public SslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine, - boolean useDirectBuffersForEncryption, boolean useDirectBuffersForDecryption) + public SslConnection(RetainableByteBufferPool retainableByteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine) { - this(byteBufferPool.asRetainableByteBufferPool(), byteBufferPool, executor, endPoint, sslEngine, useDirectBuffersForEncryption, useDirectBuffersForDecryption); + this(retainableByteBufferPool, executor, endPoint, sslEngine, false, false); } - public SslConnection(RetainableByteBufferPool retainableByteBufferPool, ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine, + public SslConnection(RetainableByteBufferPool retainableByteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine, boolean useDirectBuffersForEncryption, boolean useDirectBuffersForDecryption) { // This connection does not execute calls to onFillable(), so they will be called by the selector thread. // onFillable() does not block and will only wakeup another thread to do the actual reading and handling. super(endPoint, executor); - this._bufferPool = byteBufferPool; this._retainableByteBufferPool = retainableByteBufferPool; this._sslEngine = sslEngine; this._decryptedEndPoint = newDecryptedEndPoint(); @@ -324,15 +315,16 @@ private void acquireEncryptedInput() private void acquireEncryptedOutput() { + // TODO: before the output was done with the BBP only. if (_encryptedOutput == null) - _encryptedOutput = _bufferPool.acquire(getPacketBufferSize(), _encryptedDirectBuffers); + _encryptedOutput = _retainableByteBufferPool.acquire(getPacketBufferSize(), _encryptedDirectBuffers); } @Override public void onUpgradeTo(ByteBuffer buffer) { acquireEncryptedInput(); - BufferUtil.append(_encryptedInput.getBuffer(), buffer); + BufferUtil.append(_encryptedInput.getByteBuffer(), buffer); } @Override @@ -402,11 +394,11 @@ protected SSLEngineResult unwrap(SSLEngine sslEngine, ByteBuffer input, ByteBuff @Override public String toConnectionString() { - ByteBuffer b = _encryptedInput == null ? null : _encryptedInput.getBuffer(); + ByteBuffer b = _encryptedInput == null ? null : _encryptedInput.getByteBuffer(); int ei = b == null ? -1 : b.remaining(); - b = _encryptedOutput; + b = _encryptedOutput == null ? null : _encryptedOutput.getByteBuffer(); int eo = b == null ? -1 : b.remaining(); - b = _decryptedInput; + b = _decryptedInput == null ? null : _decryptedInput.getByteBuffer(); int di = b == null ? -1 : b.remaining(); Connection connection = _decryptedEndPoint.getConnection(); @@ -437,7 +429,7 @@ private void releaseEmptyDecryptedInputBuffer() throw new IllegalStateException(); if (_decryptedInput != null && !_decryptedInput.hasRemaining()) { - _bufferPool.release(_decryptedInput); + _decryptedInput.release(); _decryptedInput = null; } } @@ -448,7 +440,8 @@ private void discardInputBuffers() throw new IllegalStateException(); if (_encryptedInput != null) _encryptedInput.clear(); - BufferUtil.clear(_decryptedInput); + if (_decryptedInput != null) + _decryptedInput.clear(); releaseEmptyInputBuffers(); } @@ -462,7 +455,8 @@ private void discardEncryptedOutputBuffer() { if (!_lock.isHeldByCurrentThread()) throw new IllegalStateException(); - BufferUtil.clear(_encryptedOutput); + if (_encryptedOutput != null) + _encryptedOutput.clear(); releaseEmptyEncryptedOutputBuffer(); } @@ -472,7 +466,7 @@ private void releaseEmptyEncryptedOutputBuffer() throw new IllegalStateException(); if (_encryptedOutput != null && !_encryptedOutput.hasRemaining()) { - _bufferPool.release(_encryptedOutput); + _encryptedOutput.release(); _encryptedOutput = null; } } @@ -639,8 +633,8 @@ public int fill(ByteBuffer buffer) throws IOException return filled = 0; // Do we already have some decrypted data? - if (BufferUtil.hasContent(_decryptedInput)) - return filled = BufferUtil.append(buffer, _decryptedInput); + if (_decryptedInput != null && _decryptedInput.hasRemaining()) + return filled = BufferUtil.append(buffer, _decryptedInput.getByteBuffer()); // loop filling and unwrapping until we have something while (true) @@ -683,24 +677,29 @@ public int fill(ByteBuffer buffer) throws IOException if (_decryptedInput == null) { if (BufferUtil.space(buffer) > appBufferSize) + { appIn = buffer; + } else - appIn = _decryptedInput = _bufferPool.acquire(appBufferSize, _decryptedDirectBuffers); + { + _decryptedInput = _retainableByteBufferPool.acquire(appBufferSize, _decryptedDirectBuffers); + appIn = _decryptedInput.getByteBuffer(); + } } else { - appIn = _decryptedInput; + appIn = _decryptedInput.getByteBuffer(); } // Let's try reading some encrypted data... even if we have some already. - int netFilled = networkFill(_encryptedInput.getBuffer()); + int netFilled = networkFill(_encryptedInput.getByteBuffer()); if (netFilled > 0) _bytesIn.addAndGet(netFilled); if (LOG.isDebugEnabled()) LOG.debug("net filled={}", netFilled); // Workaround for Java 11 behavior. - if (netFilled < 0 && isHandshakeInitial() && (_encryptedInput == null || _encryptedInput.isEmpty())) + if (netFilled < 0 && isHandshakeInitial() && (_encryptedInput == null || !_encryptedInput.hasRemaining())) closeInbound(); if (netFilled > 0 && !isHandshakeComplete() && isOutboundDone()) @@ -719,7 +718,7 @@ public int fill(ByteBuffer buffer) throws IOException try { _underflown = false; - unwrapResult = SslConnection.this.unwrap(_sslEngine, _encryptedInput.getBuffer(), appIn); + unwrapResult = SslConnection.this.unwrap(_sslEngine, _encryptedInput.getByteBuffer(), appIn); } finally { @@ -750,13 +749,13 @@ public int fill(ByteBuffer buffer) throws IOException case BUFFER_UNDERFLOW: // Continue if we can compact? - if (BufferUtil.compact(_encryptedInput.getBuffer())) + if (BufferUtil.compact(_encryptedInput.getByteBuffer())) continue; // Are we out of space? - if (BufferUtil.space(_encryptedInput.getBuffer()) == 0) + if (BufferUtil.space(_encryptedInput.getByteBuffer()) == 0) { - BufferUtil.clear(_encryptedInput.getBuffer()); + BufferUtil.clear(_encryptedInput.getByteBuffer()); throw new SSLHandshakeException("Encrypted buffer max length exceeded"); } @@ -786,7 +785,7 @@ public int fill(ByteBuffer buffer) throws IOException // too small. Release the decrypted input buffer so it will be re-acquired // with the larger capacity. // See also system property "jsse.SSLEngine.acceptLargeFragments". - if (BufferUtil.isEmpty(_decryptedInput) && appBufferSize < getApplicationBufferSize()) + if ((_decryptedInput == null || !_decryptedInput.hasRemaining()) && appBufferSize < getApplicationBufferSize()) { releaseEmptyDecryptedInputBuffer(); continue; @@ -807,7 +806,7 @@ public int fill(ByteBuffer buffer) throws IOException { if (appIn == buffer) return filled = unwrapResult.bytesProduced(); - return filled = BufferUtil.append(buffer, _decryptedInput); + return filled = BufferUtil.append(buffer, _decryptedInput.getByteBuffer()); } break; @@ -867,14 +866,14 @@ protected void needsFillInterest() _fillState, _underflown, _encryptedInput, - BufferUtil.toDetailString(_decryptedInput), + _decryptedInput, SslConnection.this); if (_fillState != FillState.IDLE) return; // Fillable if we have decrypted input OR enough encrypted input. - fillable = BufferUtil.hasContent(_decryptedInput) || (_encryptedInput != null && _encryptedInput.hasRemaining() && !_underflown); + fillable = (_decryptedInput != null && _decryptedInput.hasRemaining()) || (_encryptedInput != null && _encryptedInput.hasRemaining() && !_underflown); HandshakeStatus status = _sslEngine.getHandshakeStatus(); switch (status) @@ -890,10 +889,10 @@ protected void needsFillInterest() { interest = true; _fillState = FillState.INTERESTED; - if (_flushState == FlushState.IDLE && BufferUtil.hasContent(_encryptedOutput)) + if (_flushState == FlushState.IDLE && (_encryptedOutput != null && _encryptedOutput.hasRemaining())) { _flushState = FlushState.WRITING; - write = _encryptedOutput; + write = _encryptedOutput.getByteBuffer(); } } break; @@ -905,7 +904,7 @@ protected void needsFillInterest() if (_flushState == FlushState.IDLE) { _flushState = FlushState.WRITING; - write = BufferUtil.hasContent(_encryptedOutput) ? _encryptedOutput : BufferUtil.EMPTY_BUFFER; + write = (_encryptedOutput != null && _encryptedOutput.hasRemaining()) ? _encryptedOutput.getByteBuffer() : BufferUtil.EMPTY_BUFFER; } } break; @@ -1024,7 +1023,7 @@ public boolean flush(ByteBuffer... appOuts) throws IOException int remaining = _encryptedOutput.remaining(); if (remaining > 0) { - boolean flushed = networkFlush(_encryptedOutput); + boolean flushed = networkFlush(_encryptedOutput.getByteBuffer()); int written = remaining - _encryptedOutput.remaining(); if (written > 0) _bytesOut.addAndGet(written); @@ -1083,21 +1082,22 @@ public boolean flush(ByteBuffer... appOuts) throws IOException // We call sslEngine.wrap to try to take bytes from appOuts // buffers and encrypt them into the _encryptedOutput buffer. - BufferUtil.compact(_encryptedOutput); - int pos = BufferUtil.flipToFill(_encryptedOutput); + ByteBuffer encryptedOutputBuffer = _encryptedOutput.getByteBuffer(); + BufferUtil.compact(encryptedOutputBuffer); + int pos = BufferUtil.flipToFill(encryptedOutputBuffer); SSLEngineResult wrapResult; try { - wrapResult = wrap(_sslEngine, appOuts, _encryptedOutput); + wrapResult = wrap(_sslEngine, appOuts, encryptedOutputBuffer); } finally { - BufferUtil.flipToFlush(_encryptedOutput, pos); + BufferUtil.flipToFlush(encryptedOutputBuffer, pos); } if (LOG.isDebugEnabled()) LOG.debug("wrap {} {} ioDone={}/{}", StringUtil.replace(wrapResult.toString(), '\n', ' '), - BufferUtil.toSummaryString(_encryptedOutput), + _encryptedOutput, _sslEngine.isInboundDone(), _sslEngine.isOutboundDone()); @@ -1106,16 +1106,13 @@ public boolean flush(ByteBuffer... appOuts) throws IOException // if we have net bytes, let's try to flush them boolean flushed = true; - if (_encryptedOutput != null) + int remaining = encryptedOutputBuffer.remaining(); + if (remaining > 0) { - int remaining = _encryptedOutput.remaining(); - if (remaining > 0) - { - flushed = networkFlush(_encryptedOutput); - int written = remaining - _encryptedOutput.remaining(); - if (written > 0) - _bytesOut.addAndGet(written); - } + flushed = networkFlush(encryptedOutputBuffer); + int written = remaining - encryptedOutputBuffer.remaining(); + if (written > 0) + _bytesOut.addAndGet(written); } if (LOG.isDebugEnabled()) @@ -1160,7 +1157,7 @@ public boolean flush(ByteBuffer... appOuts) throws IOException if (isRenegotiating() && !allowRenegotiate()) { getEndPoint().shutdownOutput(); - if (isEmpty && BufferUtil.isEmpty(_encryptedOutput)) + if (isEmpty && (_encryptedOutput == null || !_encryptedOutput.hasRemaining())) return result = true; throw new IOException("Broken pipe"); } @@ -1215,7 +1212,7 @@ protected void onIncompleteFlush() try (AutoLock l = _lock.lock()) { if (LOG.isDebugEnabled()) - LOG.debug(">onIncompleteFlush {} {}", SslConnection.this, BufferUtil.toDetailString(_encryptedOutput)); + LOG.debug(">onIncompleteFlush {} {}", SslConnection.this, _encryptedOutput); if (_flushState != FlushState.IDLE) return; @@ -1229,15 +1226,15 @@ protected void onIncompleteFlush() case NEED_WRAP: case NOT_HANDSHAKING: // write what we have or an empty buffer to reschedule a call to flush - write = BufferUtil.hasContent(_encryptedOutput) ? _encryptedOutput : BufferUtil.EMPTY_BUFFER; + write = (_encryptedOutput != null && _encryptedOutput.hasRemaining()) ? _encryptedOutput.getByteBuffer() : BufferUtil.EMPTY_BUFFER; _flushState = FlushState.WRITING; break; case NEED_UNWRAP: // If we have something to write, then write it and ignore the needed unwrap for now. - if (BufferUtil.hasContent(_encryptedOutput)) + if (_encryptedOutput != null && _encryptedOutput.hasRemaining()) { - write = _encryptedOutput; + write = _encryptedOutput.getByteBuffer(); _flushState = FlushState.WRITING; break; } @@ -1339,9 +1336,9 @@ private void doShutdownOutput(boolean close) ByteBuffer write = null; try (AutoLock l = _lock.lock()) { - if (BufferUtil.hasContent(_encryptedOutput)) + if (_encryptedOutput != null && _encryptedOutput.hasRemaining()) { - write = _encryptedOutput; + write = _encryptedOutput.getByteBuffer(); _flushState = FlushState.WRITING; } } @@ -1441,7 +1438,7 @@ public Object getTransport() @Override public boolean isInputShutdown() { - return BufferUtil.isEmpty(_decryptedInput) && (getEndPoint().isInputShutdown() || isInboundDone()); + return (_decryptedInput == null || !_decryptedInput.hasRemaining()) && (getEndPoint().isInputShutdown() || isInboundDone()); } private boolean isInboundDone() diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java deleted file mode 100644 index 528e28cccfb0..000000000000 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java +++ /dev/null @@ -1,255 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Arrays; -import java.util.Objects; - -import org.eclipse.jetty.io.AbstractByteBufferPool.Bucket; -import org.eclipse.jetty.util.StringUtil; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class ArrayByteBufferPoolTest -{ - @Test - public void testMinimumRelease() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(10, 100, 1000); - Bucket[] buckets = bufferPool.bucketsFor(true); - - for (int size = 1; size <= 9; size++) - { - ByteBuffer buffer = bufferPool.acquire(size, true); - - assertTrue(buffer.isDirect()); - assertEquals(size, buffer.capacity()); - for (Bucket bucket : buckets) - { - if (bucket != null) - assertTrue(bucket.isEmpty()); - } - - bufferPool.release(buffer); - - for (Bucket bucket : buckets) - { - if (bucket != null) - assertTrue(bucket.isEmpty()); - } - } - } - - @Test - public void testMaxRelease() - { - int minCapacity = 10; - int factor = 1; - int maxCapacity = 1024; - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(minCapacity, factor, maxCapacity); - Bucket[] buckets = bufferPool.bucketsFor(true); - - for (int size = maxCapacity - 1; size <= maxCapacity + 1; size++) - { - bufferPool.clear(); - ByteBuffer buffer = bufferPool.acquire(size, true); - - assertTrue(buffer.isDirect()); - assertThat(buffer.capacity(), greaterThanOrEqualTo(size)); - for (Bucket bucket : buckets) - { - if (bucket != null) - assertTrue(bucket.isEmpty()); - } - - bufferPool.release(buffer); - - int pooled = Arrays.stream(buckets) - .filter(Objects::nonNull) - .mapToInt(AbstractByteBufferPool.Bucket::size) - .sum(); - - if (size <= maxCapacity) - assertThat(pooled, is(1)); - else - assertThat(pooled, is(0)); - } - } - - @Test - public void testAcquireRelease() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(10, 100, 1000); - Bucket[] buckets = bufferPool.bucketsFor(true); - - for (int size = 390; size <= 510; size++) - { - bufferPool.clear(); - ByteBuffer buffer = bufferPool.acquire(size, true); - - assertTrue(buffer.isDirect()); - assertThat(buffer.capacity(), greaterThanOrEqualTo(size)); - for (Bucket bucket : buckets) - { - if (bucket != null) - assertTrue(bucket.isEmpty()); - } - - bufferPool.release(buffer); - - int pooled = Arrays.stream(buckets) - .filter(Objects::nonNull) - .mapToInt(AbstractByteBufferPool.Bucket::size) - .sum(); - assertEquals(1, pooled); - } - } - - @Test - public void testAcquireReleaseAcquire() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(10, 100, 1000); - Bucket[] buckets = bufferPool.bucketsFor(true); - - for (int size = 390; size <= 510; size++) - { - bufferPool.clear(); - ByteBuffer buffer1 = bufferPool.acquire(size, true); - bufferPool.release(buffer1); - ByteBuffer buffer2 = bufferPool.acquire(size, true); - bufferPool.release(buffer2); - ByteBuffer buffer3 = bufferPool.acquire(size, false); - bufferPool.release(buffer3); - - int pooled = Arrays.stream(buckets) - .filter(Objects::nonNull) - .mapToInt(AbstractByteBufferPool.Bucket::size) - .sum(); - assertEquals(1, pooled); - - assertSame(buffer1, buffer2); - assertNotSame(buffer1, buffer3); - } - } - - @Test - public void testReleaseNonPooledBuffer() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(); - - // Release a few small non-pool buffers - bufferPool.release(ByteBuffer.wrap(StringUtil.getUtf8Bytes("Hello"))); - - assertEquals(0, bufferPool.getHeapByteBufferCount()); - } - - @Test - public void testMaxQueue() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(-1, -1, -1, 2); - - ByteBuffer buffer1 = bufferPool.acquire(512, false); - ByteBuffer buffer2 = bufferPool.acquire(512, false); - ByteBuffer buffer3 = bufferPool.acquire(512, false); - - Bucket[] buckets = bufferPool.bucketsFor(false); - Arrays.stream(buckets) - .filter(Objects::nonNull) - .forEach(b -> assertEquals(0, b.size())); - - bufferPool.release(buffer1); - Bucket bucket = Arrays.stream(buckets) - .filter(Objects::nonNull) - .filter(b -> b.size() > 0) - .findFirst() - .orElseThrow(AssertionError::new); - assertEquals(1, bucket.size()); - - bufferPool.release(buffer2); - assertEquals(2, bucket.size()); - - bufferPool.release(buffer3); - assertEquals(2, bucket.size()); - } - - @Test - public void testMaxMemory() - { - int factor = 1024; - int maxMemory = 11 * factor; - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(-1, factor, -1, -1, -1, maxMemory); - Bucket[] buckets = bufferPool.bucketsFor(true); - - // Create the buckets - the oldest is the larger. - // 1+2+3+4=10 / maxMemory=11. - for (int i = 4; i >= 1; --i) - { - int capacity = factor * i; - ByteBuffer buffer = bufferPool.acquire(capacity, true); - assertThat(buffer.capacity(), equalTo(capacity)); - bufferPool.release(buffer); - } - - // Check state of buckets. - assertThat(bufferPool.getMemory(true), equalTo(10L * factor)); - assertThat(buckets[1].size(), equalTo(1)); - assertThat(buckets[2].size(), equalTo(1)); - assertThat(buckets[3].size(), equalTo(1)); - assertThat(buckets[4].size(), equalTo(1)); - - // Create and release a buffer to exceed the max memory. - int capacity = 2 * factor; - ByteBuffer buffer = bufferPool.newByteBuffer(capacity, true); - assertThat(buffer.capacity(), equalTo(capacity)); - bufferPool.release(buffer); - - // Now the oldest buffer should be gone and we have: 1+2x2+3=8 - assertThat(bufferPool.getMemory(true), equalTo(8L * factor)); - assertThat(buckets[1].size(), equalTo(1)); - assertThat(buckets[2].size(), equalTo(2)); - assertThat(buckets[3].size(), equalTo(1)); - - // Create and release a large buffer. - // Max memory is exceeded and buckets 3 and 1 are cleared. - // We will have 2x2+7=11. - capacity = 7 * factor; - buffer = bufferPool.newByteBuffer(capacity, true); - bufferPool.release(buffer); - - assertThat(bufferPool.getMemory(true), equalTo(11L * factor)); - assertThat(buckets[2].size(), equalTo(2)); - assertThat(buckets[7].size(), equalTo(1)); - } - - @Test - public void testEndiannessResetOnRelease() - { - ArrayByteBufferPool bufferPool = new ArrayByteBufferPool(); - ByteBuffer buffer = bufferPool.acquire(10, true); - assertThat(buffer.order(), is(ByteOrder.BIG_ENDIAN)); - buffer.order(ByteOrder.LITTLE_ENDIAN); - bufferPool.release(buffer); - assertThat(buffer.order(), is(ByteOrder.BIG_ENDIAN)); - } -} diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java index f201154b1523..0e5ebc02ebfe 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ArrayRetainableByteBufferPoolTest.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.io; import java.io.IOException; -import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.List; @@ -241,7 +240,7 @@ public void testClearUnlinksLeakedBuffers() pool.acquire(10, true); assertThat(pool.getDirectByteBufferCount(), is(2L)); - assertThat(pool.getDirectMemory(), is(2L * AbstractByteBufferPool.DEFAULT_FACTOR)); + assertThat(pool.getDirectMemory(), is(2L * ArrayRetainableByteBufferPool.DEFAULT_FACTOR)); assertThat(pool.getAvailableDirectByteBufferCount(), is(0L)); assertThat(pool.getAvailableDirectMemory(), is(0L)); @@ -285,16 +284,16 @@ public void testAcquireRelease() { RetainableByteBuffer buf1 = pool.acquire(10, true); assertThat(buf1, is(notNullValue())); - assertThat(buf1.capacity(), is(AbstractByteBufferPool.DEFAULT_FACTOR)); + assertThat(buf1.capacity(), is(ArrayRetainableByteBufferPool.DEFAULT_FACTOR)); RetainableByteBuffer buf2 = pool.acquire(10, true); assertThat(buf2, is(notNullValue())); - assertThat(buf2.capacity(), is(AbstractByteBufferPool.DEFAULT_FACTOR)); + assertThat(buf2.capacity(), is(ArrayRetainableByteBufferPool.DEFAULT_FACTOR)); buf1.release(); buf2.release(); RetainableByteBuffer buf3 = pool.acquire(16384 + 1, true); assertThat(buf3, is(notNullValue())); - assertThat(buf3.capacity(), is(16384 + AbstractByteBufferPool.DEFAULT_FACTOR)); + assertThat(buf3.capacity(), is(16384 + ArrayRetainableByteBufferPool.DEFAULT_FACTOR)); buf3.release(); RetainableByteBuffer buf4 = pool.acquire(32768, true); @@ -310,7 +309,7 @@ public void testAcquireRelease() assertThat(pool.getDirectByteBufferCount(), is(4L)); assertThat(pool.getHeapByteBufferCount(), is(1L)); - assertThat(pool.getDirectMemory(), is(AbstractByteBufferPool.DEFAULT_FACTOR * 3L + 16384 + 32768L)); + assertThat(pool.getDirectMemory(), is(ArrayRetainableByteBufferPool.DEFAULT_FACTOR * 3L + 16384 + 32768L)); assertThat(pool.getHeapMemory(), is(32768L)); pool.clear(); @@ -322,9 +321,9 @@ public void testAcquireRelease() } @Test - public void testExponentialPool() throws IOException + public void testQuadraticPool() throws IOException { - ArrayRetainableByteBufferPool pool = new ArrayRetainableByteBufferPool.ExponentialPool(); + ArrayRetainableByteBufferPool pool = new ArrayRetainableByteBufferPool.Quadratic(); assertThat(pool.acquire(1, false).capacity(), is(1)); assertThat(pool.acquire(2, false).capacity(), is(2)); RetainableByteBuffer b3 = pool.acquire(3, false); @@ -350,8 +349,8 @@ public void testExponentialPool() throws IOException } b3.release(); - b4.getBuffer().limit(b4.getBuffer().capacity() - 2); - assertThat(pool.dump(), containsString("]{capacity=4,inuse=3(75%)")); + b4.getByteBuffer().limit(b4.getByteBuffer().capacity() - 2); + assertThat(pool.dump(), containsString("{capacity=4,inuse=3(75%)")); } @Test @@ -359,33 +358,22 @@ public void testEndiannessResetOnRelease() { ArrayRetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); RetainableByteBuffer buffer = bufferPool.acquire(10, true); - assertThat(buffer.getBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN)); - buffer.getBuffer().order(ByteOrder.LITTLE_ENDIAN); + assertThat(buffer.getByteBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN)); + buffer.getByteBuffer().order(ByteOrder.LITTLE_ENDIAN); assertThat(buffer.release(), is(true)); - assertThat(buffer.getBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN)); + assertThat(buffer.getByteBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN)); } @Test public void testLogarithmic() { - LogarithmicArrayByteBufferPool pool = new LogarithmicArrayByteBufferPool(); - ByteBuffer buffer5 = pool.acquire(5, false); - pool.release(buffer5); - ByteBuffer buffer6 = pool.acquire(6, false); - assertThat(buffer6, sameInstance(buffer5)); - pool.release(buffer6); - ByteBuffer buffer9 = pool.acquire(9, false); - assertThat(buffer9, not(sameInstance(buffer5))); - pool.release(buffer9); - - RetainableByteBufferPool retainablePool = pool.asRetainableByteBufferPool(); - - RetainableByteBuffer retain5 = retainablePool.acquire(5, false); + ArrayRetainableByteBufferPool.Quadratic pool = new ArrayRetainableByteBufferPool.Quadratic(); + RetainableByteBuffer retain5 = pool.acquire(5, false); retain5.release(); - RetainableByteBuffer retain6 = retainablePool.acquire(6, false); + RetainableByteBuffer retain6 = pool.acquire(6, false); assertThat(retain6, sameInstance(retain5)); retain6.release(); - RetainableByteBuffer retain9 = retainablePool.acquire(9, false); + RetainableByteBuffer retain9 = pool.acquire(9, false); assertThat(retain9, not(sameInstance(retain5))); retain9.release(); } diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ByteBufferAccumulatorTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ByteBufferAccumulatorTest.java index 6d32ea12ff06..758c547762aa 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ByteBufferAccumulatorTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ByteBufferAccumulatorTest.java @@ -30,14 +30,14 @@ public class ByteBufferAccumulatorTest { - private CountingBufferPool byteBufferPool; + private CountingBufferPool bufferPool; private ByteBufferAccumulator accumulator; @BeforeEach public void before() { - byteBufferPool = new CountingBufferPool(); - accumulator = new ByteBufferAccumulator(byteBufferPool, false); + bufferPool = new CountingBufferPool(); + accumulator = new ByteBufferAccumulator(bufferPool, false); } @Test @@ -49,58 +49,64 @@ public void testToBuffer() ByteBuffer slice = content.slice(); // We completely fill up the internal buffer with the first write. - ByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); - assertThat(BufferUtil.space(internalBuffer), greaterThanOrEqualTo(allocationSize)); - writeInFlushMode(slice, internalBuffer); - assertThat(BufferUtil.space(internalBuffer), is(0)); + RetainableByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); + ByteBuffer byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), greaterThanOrEqualTo(allocationSize)); + writeInFlushMode(slice, byteBuffer); + assertThat(BufferUtil.space(byteBuffer), is(0)); // If we ask for min size of 0 we get the same buffer which is full. internalBuffer = accumulator.ensureBuffer(0, allocationSize); - assertThat(BufferUtil.space(internalBuffer), is(0)); + byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), is(0)); // If we need at least 1 minSpace we must allocate a new buffer. internalBuffer = accumulator.ensureBuffer(1, allocationSize); - assertThat(BufferUtil.space(internalBuffer), greaterThan(0)); - assertThat(BufferUtil.space(internalBuffer), greaterThanOrEqualTo(allocationSize)); + byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), greaterThan(0)); + assertThat(BufferUtil.space(byteBuffer), greaterThanOrEqualTo(allocationSize)); // Write 13 bytes from the end of the internal buffer. - int bytesToWrite = BufferUtil.space(internalBuffer) - 13; + int bytesToWrite = BufferUtil.space(byteBuffer) - 13; ByteBuffer buffer = BufferUtil.toBuffer(new byte[bytesToWrite]); BufferUtil.clear(buffer); assertThat(writeInFlushMode(slice, buffer), is(bytesToWrite)); - assertThat(writeInFlushMode(buffer, internalBuffer), is(bytesToWrite)); - assertThat(BufferUtil.space(internalBuffer), is(13)); + assertThat(writeInFlushMode(buffer, byteBuffer), is(bytesToWrite)); + assertThat(BufferUtil.space(byteBuffer), is(13)); // If we request anything under the amount remaining we get back the same buffer. for (int i = 0; i <= 13; i++) { internalBuffer = accumulator.ensureBuffer(i, allocationSize); - assertThat(BufferUtil.space(internalBuffer), is(13)); + byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), is(13)); } // If we request over 13 then we get a new buffer. internalBuffer = accumulator.ensureBuffer(14, allocationSize); - assertThat(BufferUtil.space(internalBuffer), greaterThanOrEqualTo(1024)); + byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), greaterThanOrEqualTo(1024)); // Copy the rest of the content. while (slice.hasRemaining()) { internalBuffer = accumulator.ensureBuffer(1, allocationSize); - assertThat(BufferUtil.space(internalBuffer), greaterThanOrEqualTo(1)); - writeInFlushMode(slice, internalBuffer); + byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), greaterThanOrEqualTo(1)); + writeInFlushMode(slice, byteBuffer); } // Check we have the same content as the original buffer. assertThat(accumulator.getLength(), is(size)); - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); - ByteBuffer combinedBuffer = accumulator.toByteBuffer(); - assertThat(byteBufferPool.getLeasedBuffers(), is(1L)); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); + RetainableByteBuffer combinedBuffer = accumulator.toRetainableByteBuffer(); + assertThat(bufferPool.getAcquireCount(), is(1L)); assertThat(accumulator.getLength(), is(size)); - assertThat(combinedBuffer, is(content)); + assertThat(combinedBuffer.getByteBuffer(), is(content)); // Close the accumulator and make sure all is returned to bufferPool. accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test @@ -114,24 +120,25 @@ public void testTakeBuffer() // Copy the content. while (slice.hasRemaining()) { - ByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); - assertThat(BufferUtil.space(internalBuffer), greaterThanOrEqualTo(1)); - writeInFlushMode(slice, internalBuffer); + RetainableByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); + ByteBuffer byteBuffer = internalBuffer.getByteBuffer(); + assertThat(BufferUtil.space(byteBuffer), greaterThanOrEqualTo(1)); + writeInFlushMode(slice, byteBuffer); } // Check we have the same content as the original buffer. assertThat(accumulator.getLength(), is(size)); - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); - ByteBuffer combinedBuffer = accumulator.takeByteBuffer(); - assertThat(byteBufferPool.getLeasedBuffers(), is(1L)); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); + RetainableByteBuffer combinedBuffer = accumulator.takeRetainableByteBuffer(); + assertThat(bufferPool.getAcquireCount(), is(1L)); assertThat(accumulator.getLength(), is(0)); accumulator.close(); - assertThat(byteBufferPool.getLeasedBuffers(), is(1L)); - assertThat(combinedBuffer, is(content)); + assertThat(bufferPool.getAcquireCount(), is(1L)); + assertThat(combinedBuffer.getByteBuffer(), is(content)); // Return the buffer and make sure all is returned to bufferPool. - byteBufferPool.release(combinedBuffer); - byteBufferPool.verifyClosed(); + combinedBuffer.release(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test @@ -145,42 +152,43 @@ public void testToByteArray() // Copy the content. while (slice.hasRemaining()) { - ByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); - writeInFlushMode(slice, internalBuffer); + RetainableByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); + ByteBuffer byteBuffer = internalBuffer.getByteBuffer(); + writeInFlushMode(slice, byteBuffer); } // Check we have the same content as the original buffer. assertThat(accumulator.getLength(), is(size)); - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); byte[] combinedBuffer = accumulator.toByteArray(); - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); assertThat(accumulator.getLength(), is(size)); assertThat(BufferUtil.toBuffer(combinedBuffer), is(content)); // Close the accumulator and make sure all is returned to bufferPool. accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test public void testEmptyToBuffer() { - ByteBuffer combinedBuffer = accumulator.toByteBuffer(); + RetainableByteBuffer combinedBuffer = accumulator.toRetainableByteBuffer(); assertThat(combinedBuffer.remaining(), is(0)); - assertThat(byteBufferPool.getLeasedBuffers(), is(1L)); + assertThat(bufferPool.getAcquireCount(), is(1L)); accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test public void testEmptyTakeBuffer() { - ByteBuffer combinedBuffer = accumulator.takeByteBuffer(); + RetainableByteBuffer combinedBuffer = accumulator.takeRetainableByteBuffer(); assertThat(combinedBuffer.remaining(), is(0)); accumulator.close(); - assertThat(byteBufferPool.getLeasedBuffers(), is(1L)); - byteBufferPool.release(combinedBuffer); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(1L)); + combinedBuffer.release(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test @@ -194,21 +202,22 @@ public void testWriteTo() // Copy the content. while (slice.hasRemaining()) { - ByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); - writeInFlushMode(slice, internalBuffer); + RetainableByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); + ByteBuffer byteBuffer = internalBuffer.getByteBuffer(); + writeInFlushMode(slice, byteBuffer); } // Check we have the same content as the original buffer. - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); - ByteBuffer combinedBuffer = byteBufferPool.acquire(accumulator.getLength(), false); - accumulator.writeTo(combinedBuffer); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); + RetainableByteBuffer combinedBuffer = bufferPool.acquire(accumulator.getLength(), false); + accumulator.writeTo(combinedBuffer.getByteBuffer()); assertThat(accumulator.getLength(), is(size)); - assertThat(combinedBuffer, is(content)); - byteBufferPool.release(combinedBuffer); + assertThat(combinedBuffer.getByteBuffer(), is(content)); + combinedBuffer.release(); // Close the accumulator and make sure all is returned to bufferPool. accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test @@ -222,19 +231,20 @@ public void testWriteToBufferTooSmall() // Copy the content. while (slice.hasRemaining()) { - ByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); - writeInFlushMode(slice, internalBuffer); + RetainableByteBuffer internalBuffer = accumulator.ensureBuffer(1, allocationSize); + ByteBuffer byteBuffer = internalBuffer.getByteBuffer(); + writeInFlushMode(slice, byteBuffer); } // Writing to a buffer too small gives buffer overflow. - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); ByteBuffer combinedBuffer = BufferUtil.toBuffer(new byte[accumulator.getLength() - 1]); BufferUtil.clear(combinedBuffer); assertThrows(BufferOverflowException.class, () -> accumulator.writeTo(combinedBuffer)); // Close the accumulator and make sure all is returned to bufferPool. accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } @Test @@ -255,16 +265,16 @@ public void testCopy() } // Check we have the same content as the original buffer. - assertThat(byteBufferPool.getLeasedBuffers(), greaterThan(1L)); - ByteBuffer combinedBuffer = byteBufferPool.acquire(accumulator.getLength(), false); - accumulator.writeTo(combinedBuffer); + assertThat(bufferPool.getAcquireCount(), greaterThan(1L)); + RetainableByteBuffer combinedBuffer = bufferPool.acquire(accumulator.getLength(), false); + accumulator.writeTo(combinedBuffer.getByteBuffer()); assertThat(accumulator.getLength(), is(size)); - assertThat(combinedBuffer, is(content)); - byteBufferPool.release(combinedBuffer); + assertThat(combinedBuffer.getByteBuffer(), is(content)); + combinedBuffer.release(); // Close the accumulator and make sure all is returned to bufferPool. accumulator.close(); - byteBufferPool.verifyClosed(); + assertThat(bufferPool.getAcquireCount(), is(0L)); } private ByteBuffer randomBytes(int size) @@ -282,47 +292,35 @@ private int writeInFlushMode(ByteBuffer from, ByteBuffer to) return written; } - public static class CountingBufferPool extends LeakTrackingByteBufferPool + private static class CountingBufferPool extends RetainableByteBufferPool.Wrapper { - private final AtomicLong _leasedBuffers = new AtomicLong(0); + private final AtomicLong _acquires = new AtomicLong(); public CountingBufferPool() { - this(new MappedByteBufferPool()); - } - - public CountingBufferPool(ByteBufferPool delegate) - { - super(delegate); - } - - @Override - public ByteBuffer acquire(int size, boolean direct) - { - _leasedBuffers.incrementAndGet(); - return super.acquire(size, direct); + super(new ArrayRetainableByteBufferPool()); } @Override - public void release(ByteBuffer buffer) - { - if (buffer != null) - _leasedBuffers.decrementAndGet(); - super.release(buffer); - } - - public long getLeasedBuffers() + public RetainableByteBuffer acquire(int size, boolean direct) { - return _leasedBuffers.get(); + _acquires.incrementAndGet(); + return new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + _acquires.decrementAndGet(); + return released; + } + }; } - public void verifyClosed() + public long getAcquireCount() { - assertThat(_leasedBuffers.get(), is(0L)); - assertThat(getLeakedAcquires(), is(0L)); - assertThat(getLeakedReleases(), is(0L)); - assertThat(getLeakedResources(), is(0L)); - assertThat(getLeakedRemoves(), is(0L)); + return _acquires.get(); } } } diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/MappedByteBufferPoolTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/MappedByteBufferPoolTest.java deleted file mode 100644 index 8fec072dc3d3..000000000000 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/MappedByteBufferPoolTest.java +++ /dev/null @@ -1,185 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.io; - -import java.nio.ByteBuffer; -import java.util.concurrent.ConcurrentMap; - -import org.eclipse.jetty.io.AbstractByteBufferPool.Bucket; -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.StringUtil; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class MappedByteBufferPoolTest -{ - @Test - public void testAcquireRelease() - { - MappedByteBufferPool bufferPool = new MappedByteBufferPool(); - ConcurrentMap buckets = bufferPool.bucketsFor(true); - - int size = 512; - ByteBuffer buffer = bufferPool.acquire(size, true); - - assertTrue(buffer.isDirect()); - assertThat(buffer.capacity(), greaterThanOrEqualTo(size)); - assertTrue(buckets.isEmpty()); - - bufferPool.release(buffer); - - assertEquals(1, buckets.size()); - assertEquals(1, buckets.values().iterator().next().size()); - } - - @Test - public void testAcquireReleaseAcquire() - { - MappedByteBufferPool bufferPool = new MappedByteBufferPool(); - ConcurrentMap buckets = bufferPool.bucketsFor(false); - - ByteBuffer buffer1 = bufferPool.acquire(512, false); - bufferPool.release(buffer1); - ByteBuffer buffer2 = bufferPool.acquire(512, false); - - assertSame(buffer1, buffer2); - assertEquals(1, buckets.size()); - assertEquals(0, buckets.values().iterator().next().size()); - - bufferPool.release(buffer2); - - assertEquals(1, buckets.size()); - assertEquals(1, buckets.values().iterator().next().size()); - } - - @Test - public void testAcquireReleaseClear() - { - MappedByteBufferPool bufferPool = new MappedByteBufferPool(); - ConcurrentMap buckets = bufferPool.bucketsFor(true); - - ByteBuffer buffer = bufferPool.acquire(512, true); - bufferPool.release(buffer); - - assertEquals(1, buckets.size()); - assertEquals(1, buckets.values().iterator().next().size()); - - bufferPool.clear(); - - assertTrue(buckets.isEmpty()); - } - - @Test - public void testReleaseNonPooledBuffer() - { - MappedByteBufferPool bufferPool = new MappedByteBufferPool(); - - // Release a few small non-pool buffers - bufferPool.release(ByteBuffer.wrap(StringUtil.getUtf8Bytes("Hello"))); - - assertEquals(0, bufferPool.getHeapByteBufferCount()); - } - - @Test - public void testTagged() - { - MappedByteBufferPool pool = new MappedByteBufferPool.Tagged(); - - ByteBuffer buffer = pool.acquire(1024, false); - - assertThat(BufferUtil.toDetailString(buffer), containsString("@T00000001")); - buffer = pool.acquire(1024, false); - assertThat(BufferUtil.toDetailString(buffer), containsString("@T00000002")); - } - - @Test - public void testMaxQueue() - { - MappedByteBufferPool bufferPool = new MappedByteBufferPool(-1, 2); - ConcurrentMap buckets = bufferPool.bucketsFor(false); - - ByteBuffer buffer1 = bufferPool.acquire(512, false); - ByteBuffer buffer2 = bufferPool.acquire(512, false); - ByteBuffer buffer3 = bufferPool.acquire(512, false); - assertEquals(0, buckets.size()); - - bufferPool.release(buffer1); - assertEquals(1, buckets.size()); - Bucket bucket = buckets.values().iterator().next(); - assertEquals(1, bucket.size()); - - bufferPool.release(buffer2); - assertEquals(2, bucket.size()); - - bufferPool.release(buffer3); - assertEquals(2, bucket.size()); - } - - @Test - public void testMaxMemory() - { - int factor = 1024; - int maxMemory = 11 * factor; - MappedByteBufferPool bufferPool = new MappedByteBufferPool(factor, -1, -1, maxMemory); - ConcurrentMap buckets = bufferPool.bucketsFor(true); - - // Create the buckets - the oldest is the larger. - // 1+2+3+4=10 / maxMemory=11. - for (int i = 4; i >= 1; --i) - { - int capacity = factor * i; - ByteBuffer buffer = bufferPool.acquire(capacity, true); - assertThat(buffer.capacity(), equalTo(capacity)); - bufferPool.release(buffer); - } - - // Check state of buckets. - assertThat(bufferPool.getMemory(true), equalTo(10L * factor)); - assertThat(buckets.get(1).size(), equalTo(1)); - assertThat(buckets.get(2).size(), equalTo(1)); - assertThat(buckets.get(3).size(), equalTo(1)); - assertThat(buckets.get(4).size(), equalTo(1)); - - // Create and release a buffer to exceed the max memory. - int capacity = 2 * factor; - ByteBuffer buffer = bufferPool.newByteBuffer(capacity, true); - assertThat(buffer.capacity(), equalTo(capacity)); - bufferPool.release(buffer); - - // Now the oldest buffer should be gone and we have: 1+2x2+3=8 - assertThat(bufferPool.getMemory(true), equalTo(8L * factor)); - assertThat(buckets.get(1).size(), equalTo(1)); - assertThat(buckets.get(2).size(), equalTo(2)); - assertThat(buckets.get(3).size(), equalTo(1)); - - // Create and release a large buffer. - // Max memory is exceeded and buckets 3 and 1 are cleared. - // We will have 2x2+7=11. - capacity = 7 * factor; - buffer = bufferPool.newByteBuffer(capacity, true); - assertThat(buffer.capacity(), equalTo(capacity)); - bufferPool.release(buffer); - - assertThat(bufferPool.getMemory(true), equalTo(11L * factor)); - assertThat(buckets.get(2).size(), equalTo(2)); - assertThat(buckets.get(7).size(), equalTo(1)); - } -} diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SocketChannelEndPointTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SocketChannelEndPointTest.java index 575cf988a109..afebbc2ae0f5 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SocketChannelEndPointTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SocketChannelEndPointTest.java @@ -612,7 +612,7 @@ public static class SslScenario implements Scenario { private final NormalScenario _normalScenario; private final SslContextFactory _sslCtxFactory = new SslContextFactory.Server(); - private final ByteBufferPool _byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool _bufferPool = new ArrayRetainableByteBufferPool(); public SslScenario(NormalScenario normalScenario) throws Exception { @@ -636,7 +636,7 @@ public Connection newConnection(SelectableChannel channel, EndPoint endpoint, Ex { SSLEngine engine = _sslCtxFactory.newSSLEngine(); engine.setUseClientMode(false); - SslConnection sslConnection = new SslConnection(_byteBufferPool, executor, endpoint, engine); + SslConnection sslConnection = new SslConnection(_bufferPool, executor, endpoint, engine); sslConnection.setRenegotiationAllowed(_sslCtxFactory.isRenegotiationAllowed()); sslConnection.setRenegotiationLimit(_sslCtxFactory.getRenegotiationLimit()); Connection appConnection = _normalScenario.newConnection(channel, sslConnection.getDecryptedEndPoint(), executor, blockAt, writeCount); diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java index a0e778d948e2..6adaff8ac1fb 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java @@ -59,8 +59,9 @@ public class SslConnectionTest private static final Logger LOG = LoggerFactory.getLogger(SslConnectionTest.class); private static final int TIMEOUT = 1000000; - private static ByteBufferPool __byteBufferPool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + // TODO: track leaks + private final RetainableByteBufferPool _bufferPool = new ArrayRetainableByteBufferPool(); private final SslContextFactory _sslCtxFactory = new SslContextFactory.Server(); protected volatile EndPoint _lastEndp; private volatile boolean _testFill = true; @@ -86,7 +87,7 @@ public Connection newConnection(SelectableChannel channel, EndPoint endpoint, Ob { SSLEngine engine = _sslCtxFactory.newSSLEngine(); engine.setUseClientMode(false); - SslConnection sslConnection = new SslConnection(__byteBufferPool, getExecutor(), endpoint, engine); + SslConnection sslConnection = new SslConnection(_bufferPool, getExecutor(), endpoint, engine); sslConnection.setRenegotiationAllowed(_sslCtxFactory.isRenegotiationAllowed()); sslConnection.setRenegotiationLimit(_sslCtxFactory.getRenegotiationLimit()); Connection appConnection = new TestConnection(sslConnection.getDecryptedEndPoint()); diff --git a/jetty-core/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ForwardProxyWithDynamicTransportTest.java b/jetty-core/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ForwardProxyWithDynamicTransportTest.java index 707dc98de087..0e44de6de06f 100644 --- a/jetty-core/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ForwardProxyWithDynamicTransportTest.java +++ b/jetty-core/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ForwardProxyWithDynamicTransportTest.java @@ -403,7 +403,7 @@ public void testHTTP2TunnelResetByClient() throws Exception @Override protected DownstreamConnection newDownstreamConnection(EndPoint endPoint, ConcurrentMap context) { - return new DownstreamConnection(endPoint, getExecutor(), getByteBufferPool(), context) + return new DownstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), context) { @Override protected void close(Throwable failure) @@ -417,7 +417,7 @@ protected void close(Throwable failure) @Override protected UpstreamConnection newUpstreamConnection(EndPoint endPoint, ConnectContext connectContext) { - return new UpstreamConnection(endPoint, getExecutor(), getByteBufferPool(), connectContext) + return new UpstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), connectContext) { @Override protected void close(Throwable failure) @@ -485,7 +485,7 @@ public void testHTTP2TunnelProxyStreamTimeout() throws Exception @Override protected DownstreamConnection newDownstreamConnection(EndPoint endPoint, ConcurrentMap context) { - return new DownstreamConnection(endPoint, getExecutor(), getByteBufferPool(), context) + return new DownstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), context) { @Override protected void close(Throwable failure) @@ -499,7 +499,7 @@ protected void close(Throwable failure) @Override protected UpstreamConnection newUpstreamConnection(EndPoint endPoint, ConnectContext connectContext) { - return new UpstreamConnection(endPoint, getExecutor(), getByteBufferPool(), connectContext) + return new UpstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), connectContext) { @Override protected void close(Throwable failure) diff --git a/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicConnection.java b/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicConnection.java index c33be6dddc93..a63456cb9c3f 100644 --- a/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicConnection.java +++ b/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicConnection.java @@ -55,7 +55,7 @@ public class ClientQuicConnection extends QuicConnection public ClientQuicConnection(ClientConnector connector, EndPoint endPoint, Map context) { - super(connector.getExecutor(), connector.getScheduler(), connector.getByteBufferPool(), endPoint); + super(connector.getExecutor(), connector.getScheduler(), connector.getRetainableByteBufferPool(), endPoint); this.connector = connector; this.context = context; } @@ -97,7 +97,7 @@ public void onOpen() LOG.debug("connecting to {} with protocols {}", remoteAddress, protocols); QuicheConnection quicheConnection = QuicheConnection.connect(quicheConfig, getEndPoint().getLocalAddress(), remoteAddress); - ClientQuicSession session = new ClientQuicSession(getExecutor(), getScheduler(), getByteBufferPool(), quicheConnection, this, remoteAddress, context); + ClientQuicSession session = new ClientQuicSession(getExecutor(), getScheduler(), getRetainableByteBufferPool(), quicheConnection, this, remoteAddress, context); pendingSessions.put(remoteAddress, session); if (LOG.isDebugEnabled()) LOG.debug("created {}", session); diff --git a/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicSession.java b/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicSession.java index 80ca5833cbbf..102a6ef75ef3 100644 --- a/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicSession.java +++ b/jetty-core/jetty-quic/jetty-quic-client/src/main/java/org/eclipse/jetty/quic/client/ClientQuicSession.java @@ -19,10 +19,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.quic.common.ProtocolSession; import org.eclipse.jetty.quic.common.QuicConnection; @@ -43,9 +43,9 @@ public class ClientQuicSession extends QuicSession private final Map context; private final AtomicReference task = new AtomicReference<>(); - protected ClientQuicSession(Executor executor, Scheduler scheduler, ByteBufferPool byteBufferPool, QuicheConnection quicheConnection, QuicConnection connection, InetSocketAddress remoteAddress, Map context) + protected ClientQuicSession(Executor executor, Scheduler scheduler, RetainableByteBufferPool retainableByteBufferPool, QuicheConnection quicheConnection, QuicConnection connection, InetSocketAddress remoteAddress, Map context) { - super(executor, scheduler, byteBufferPool, quicheConnection, connection, remoteAddress); + super(executor, scheduler, retainableByteBufferPool, quicheConnection, connection, remoteAddress); this.context = context; } diff --git a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicConnection.java b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicConnection.java index 94ed2bca2113..db6e418c87f9 100644 --- a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicConnection.java +++ b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicConnection.java @@ -27,10 +27,11 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.jetty.io.AbstractConnection; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.DatagramChannelEndPoint; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.internal.QuicErrorCode; import org.eclipse.jetty.quic.quiche.QuicheConnectionId; import org.eclipse.jetty.util.BufferUtil; @@ -59,7 +60,7 @@ public abstract class QuicConnection extends AbstractConnection private final ConcurrentMap sessions = new ConcurrentHashMap<>(); private final AtomicBoolean closed = new AtomicBoolean(); private final Scheduler scheduler; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool bufferPool; private final AdaptiveExecutionStrategy strategy; private final Flusher flusher = new Flusher(); private final Callback fillableCallback = new FillableCallback(); @@ -67,13 +68,13 @@ public abstract class QuicConnection extends AbstractConnection private boolean useInputDirectByteBuffers = true; private boolean useOutputDirectByteBuffers = true; - protected QuicConnection(Executor executor, Scheduler scheduler, ByteBufferPool byteBufferPool, EndPoint endPoint) + protected QuicConnection(Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, EndPoint endPoint) { super(endPoint, executor); if (!(endPoint instanceof DatagramChannelEndPoint)) throw new IllegalArgumentException("EndPoint must be a " + DatagramChannelEndPoint.class.getSimpleName()); this.scheduler = scheduler; - this.byteBufferPool = byteBufferPool; + this.bufferPool = bufferPool; this.strategy = new AdaptiveExecutionStrategy(new QuicProducer(), getExecutor()); } @@ -88,9 +89,9 @@ public Scheduler getScheduler() return scheduler; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return bufferPool; } public int getOutputBufferSize() @@ -223,7 +224,8 @@ private Runnable receiveAndProcess() if (interested) return null; - ByteBuffer cipherBuffer = byteBufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + RetainableByteBuffer buffer = bufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); + ByteBuffer cipherBuffer = buffer.getByteBuffer(); try { while (true) @@ -236,13 +238,13 @@ private Runnable receiveAndProcess() // DatagramChannelEndPoint will only return -1 if input is shut down. if (fill < 0) { - byteBufferPool.release(cipherBuffer); + buffer.release(); getEndPoint().shutdownOutput(); return null; } if (fill == 0) { - byteBufferPool.release(cipherBuffer); + buffer.release(); fillInterested(); return null; } @@ -282,7 +284,7 @@ private Runnable receiveAndProcess() LOG.debug("processing creation task {} on {}", task, session); if (task != null) { - byteBufferPool.release(cipherBuffer); + buffer.release(); return task; } } @@ -297,7 +299,7 @@ private Runnable receiveAndProcess() Runnable task = process(session, remoteAddress, cipherBuffer); if (task != null) { - byteBufferPool.release(cipherBuffer); + buffer.release(); return task; } } @@ -306,7 +308,7 @@ private Runnable receiveAndProcess() { if (LOG.isDebugEnabled()) LOG.debug("receiveAndProcess() failure", x); - byteBufferPool.release(cipherBuffer); + buffer.release(); onFailure(x); return null; } @@ -327,7 +329,6 @@ private Runnable process(QuicSession session, SocketAddress remoteAddress, ByteB { if (LOG.isDebugEnabled()) LOG.debug("process failure for {}", session, x); - byteBufferPool.release(cipherBuffer); session.onFailure(x); return null; } diff --git a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicSession.java b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicSession.java index aacabae5c451..14bb3634c42c 100644 --- a/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicSession.java +++ b/jetty-core/jetty-quic/jetty-quic-common/src/main/java/org/eclipse/jetty/quic/common/QuicSession.java @@ -30,10 +30,11 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.CyclicTimeout; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.internal.QuicErrorCode; import org.eclipse.jetty.quic.quiche.QuicheConnection; import org.eclipse.jetty.quic.quiche.QuicheConnectionId; @@ -65,7 +66,7 @@ public abstract class QuicSession extends ContainerLifeCycle private final ConcurrentMap endPoints = new ConcurrentHashMap<>(); private final Executor executor; private final Scheduler scheduler; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBufferPool retainableByteBufferPool; private final QuicheConnection quicheConnection; private final QuicConnection connection; private final Flusher flusher; @@ -74,11 +75,11 @@ public abstract class QuicSession extends ContainerLifeCycle private QuicheConnectionId quicheConnectionId; private long idleTimeout; - protected QuicSession(Executor executor, Scheduler scheduler, ByteBufferPool byteBufferPool, QuicheConnection quicheConnection, QuicConnection connection, SocketAddress remoteAddress) + protected QuicSession(Executor executor, Scheduler scheduler, RetainableByteBufferPool retainableByteBufferPool, QuicheConnection quicheConnection, QuicConnection connection, SocketAddress remoteAddress) { this.executor = executor; this.scheduler = scheduler; - this.byteBufferPool = byteBufferPool; + this.retainableByteBufferPool = retainableByteBufferPool; this.quicheConnection = quicheConnection; this.connection = connection; this.flusher = new Flusher(scheduler); @@ -149,9 +150,9 @@ public Scheduler getScheduler() return scheduler; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return retainableByteBufferPool; } public ProtocolSession getProtocolSession() @@ -435,7 +436,7 @@ public String toString() private class Flusher extends IteratingCallback { private final CyclicTimeout timeout; - private ByteBuffer cipherBuffer; + private RetainableByteBuffer cipherBuffer; public Flusher(Scheduler scheduler) { @@ -465,9 +466,10 @@ public void close() @Override protected Action process() throws IOException { - cipherBuffer = byteBufferPool.acquire(connection.getOutputBufferSize(), connection.isUseOutputDirectByteBuffers()); - int pos = BufferUtil.flipToFill(cipherBuffer); - int drained = quicheConnection.drainCipherBytes(cipherBuffer); + cipherBuffer = retainableByteBufferPool.acquire(connection.getOutputBufferSize(), connection.isUseOutputDirectByteBuffers()); + ByteBuffer cipherByteBuffer = cipherBuffer.getByteBuffer(); + int pos = BufferUtil.flipToFill(cipherByteBuffer); + int drained = quicheConnection.drainCipherBytes(cipherByteBuffer); if (LOG.isDebugEnabled()) LOG.debug("drained {} byte(s) of cipher bytes from {}", drained, QuicSession.this); long nextTimeoutInMs = quicheConnection.nextTimeout(); @@ -484,13 +486,13 @@ protected Action process() throws IOException if (LOG.isDebugEnabled()) LOG.debug("connection draining={} closed={}, action={} on {}", quicheConnection.isDraining(), connectionClosed, action, QuicSession.this); if (action == Action.IDLE) - byteBufferPool.release(cipherBuffer); + cipherBuffer.release(); return action; } - BufferUtil.flipToFlush(cipherBuffer, pos); + BufferUtil.flipToFlush(cipherByteBuffer, pos); if (LOG.isDebugEnabled()) LOG.debug("writing cipher bytes for {} on {}", remoteAddress, QuicSession.this); - connection.write(this, remoteAddress, cipherBuffer); + connection.write(this, remoteAddress, cipherByteBuffer); return Action.SCHEDULED; } @@ -499,7 +501,7 @@ public void succeeded() { if (LOG.isDebugEnabled()) LOG.debug("written cipher bytes on {}", QuicSession.this); - byteBufferPool.release(cipherBuffer); + cipherBuffer.release(); super.succeeded(); } @@ -514,7 +516,7 @@ protected void onCompleteSuccess() { if (LOG.isDebugEnabled()) LOG.debug("connection closed {}", QuicSession.this); - byteBufferPool.release(cipherBuffer); + cipherBuffer.release(); finishOutwardClose(new ClosedChannelException()); } @@ -523,7 +525,7 @@ protected void onCompleteFailure(Throwable failure) { if (LOG.isDebugEnabled()) LOG.debug("failed to write cipher bytes, closing session on {}", QuicSession.this, failure); - byteBufferPool.release(cipherBuffer); + cipherBuffer.release(); finishOutwardClose(failure); } } diff --git a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/QuicServerConnector.java b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/QuicServerConnector.java index 0d10efa4afad..5e73e81d46b9 100644 --- a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/QuicServerConnector.java +++ b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/QuicServerConnector.java @@ -26,11 +26,11 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.DatagramChannelEndPoint; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.ManagedSelector; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SelectorManager; import org.eclipse.jetty.quic.common.QuicConfiguration; import org.eclipse.jetty.quic.common.QuicSession; @@ -74,7 +74,7 @@ public QuicServerConnector(Server server, SslContextFactory.Server sslContextFac this(server, null, null, null, sslContextFactory, factories); } - public QuicServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool bufferPool, SslContextFactory.Server sslContextFactory, ConnectionFactory... factories) + public QuicServerConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, SslContextFactory.Server sslContextFactory, ConnectionFactory... factories) { super(server, executor, scheduler, bufferPool, 0, factories); this.selectorManager = new ServerDatagramSelectorManager(getExecutor(), getScheduler(), 1); diff --git a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicConnection.java b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicConnection.java index 6b2e8ac8e83e..58d5713d1042 100644 --- a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicConnection.java +++ b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicConnection.java @@ -19,9 +19,10 @@ import java.nio.ByteBuffer; import java.util.Iterator; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.CyclicTimeouts; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.quic.common.QuicConnection; import org.eclipse.jetty.quic.common.QuicSession; import org.eclipse.jetty.quic.quiche.QuicheConnection; @@ -45,7 +46,7 @@ public class ServerQuicConnection extends QuicConnection protected ServerQuicConnection(QuicServerConnector connector, EndPoint endPoint) { - super(connector.getExecutor(), connector.getScheduler(), connector.getByteBufferPool(), endPoint); + super(connector.getExecutor(), connector.getScheduler(), connector.getRetainableByteBufferPool(), endPoint); this.connector = connector; this.sessionTimeouts = new SessionTimeouts(connector.getScheduler()); } @@ -60,31 +61,32 @@ public void onOpen() @Override protected QuicSession createSession(SocketAddress remoteAddress, ByteBuffer cipherBuffer) throws IOException { - ByteBufferPool byteBufferPool = getByteBufferPool(); + RetainableByteBufferPool bufferPool = getRetainableByteBufferPool(); // TODO make the token validator configurable QuicheConnection quicheConnection = QuicheConnection.tryAccept(connector.newQuicheConfig(), new SimpleTokenValidator((InetSocketAddress)remoteAddress), cipherBuffer, getEndPoint().getLocalAddress(), remoteAddress); if (quicheConnection == null) { - ByteBuffer negotiationBuffer = byteBufferPool.acquire(getOutputBufferSize(), true); - int pos = BufferUtil.flipToFill(negotiationBuffer); + RetainableByteBuffer negotiationBuffer = bufferPool.acquire(getOutputBufferSize(), true); + ByteBuffer byteBuffer = negotiationBuffer.getByteBuffer(); + int pos = BufferUtil.flipToFill(byteBuffer); // TODO make the token minter configurable - if (!QuicheConnection.negotiate(new SimpleTokenMinter((InetSocketAddress)remoteAddress), cipherBuffer, negotiationBuffer)) + if (!QuicheConnection.negotiate(new SimpleTokenMinter((InetSocketAddress)remoteAddress), cipherBuffer, byteBuffer)) { if (LOG.isDebugEnabled()) LOG.debug("QUIC connection negotiation failed, dropping packet"); - byteBufferPool.release(negotiationBuffer); + negotiationBuffer.release(); return null; } - BufferUtil.flipToFlush(negotiationBuffer, pos); + BufferUtil.flipToFlush(byteBuffer, pos); - write(Callback.from(() -> byteBufferPool.release(negotiationBuffer)), remoteAddress, negotiationBuffer); + write(Callback.from(negotiationBuffer::release), remoteAddress, byteBuffer); if (LOG.isDebugEnabled()) LOG.debug("QUIC connection negotiation packet sent"); return null; } else { - QuicSession session = new ServerQuicSession(getExecutor(), getScheduler(), byteBufferPool, quicheConnection, this, remoteAddress, connector); + QuicSession session = new ServerQuicSession(getExecutor(), getScheduler(), bufferPool, quicheConnection, this, remoteAddress, connector); // Send the response packet(s) that tryAccept() generated. session.flush(); return session; diff --git a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicSession.java b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicSession.java index 24c1a6e1dcae..55cf5a1645d9 100644 --- a/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicSession.java +++ b/jetty-core/jetty-quic/jetty-quic-server/src/main/java/org/eclipse/jetty/quic/server/ServerQuicSession.java @@ -20,9 +20,9 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.CyclicTimeouts; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.quic.common.ProtocolSession; import org.eclipse.jetty.quic.common.QuicConnection; @@ -46,9 +46,9 @@ public class ServerQuicSession extends QuicSession implements CyclicTimeouts.Exp private final Connector connector; private long expireNanoTime; - protected ServerQuicSession(Executor executor, Scheduler scheduler, ByteBufferPool byteBufferPool, QuicheConnection quicheConnection, QuicConnection connection, SocketAddress remoteAddress, Connector connector) + protected ServerQuicSession(Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, QuicheConnection quicheConnection, QuicConnection connection, SocketAddress remoteAddress, Connector connector) { - super(executor, scheduler, byteBufferPool, quicheConnection, connection, remoteAddress); + super(executor, scheduler, bufferPool, quicheConnection, connection, remoteAddress); this.connector = connector; } diff --git a/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-logarithmic.xml b/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-quadratic.xml similarity index 62% rename from jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-logarithmic.xml rename to jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-quadratic.xml index 39a1e58911c9..c35799230dad 100644 --- a/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-logarithmic.xml +++ b/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool-quadratic.xml @@ -1,13 +1,11 @@ - + - + - - diff --git a/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml b/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml index f1d787a62018..dd8b4cd3bcef 100644 --- a/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml +++ b/jetty-core/jetty-server/src/main/config/etc/jetty-bytebufferpool.xml @@ -1,14 +1,12 @@ - + - + - - diff --git a/jetty-core/jetty-server/src/main/config/modules/bytebufferpool-logarithmic.mod b/jetty-core/jetty-server/src/main/config/modules/bytebufferpool-quadratic.mod similarity index 86% rename from jetty-core/jetty-server/src/main/config/modules/bytebufferpool-logarithmic.mod rename to jetty-core/jetty-server/src/main/config/modules/bytebufferpool-quadratic.mod index a54485b06f27..ee9d79476c84 100644 --- a/jetty-core/jetty-server/src/main/config/modules/bytebufferpool-logarithmic.mod +++ b/jetty-core/jetty-server/src/main/config/modules/bytebufferpool-quadratic.mod @@ -1,7 +1,8 @@ # DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html [description] -Configures the ByteBufferPool used by ServerConnectors whose bucket sizes increase exponentially instead of linearly. +Configures the RetainableByteBufferPool used by ServerConnectors. +The bucket sizes increase quadratically instead of linearly. [tags] bytebufferpool @@ -10,7 +11,7 @@ bytebufferpool bytebufferpool [xml] -etc/jetty-bytebufferpool-logarithmic.xml +etc/jetty-bytebufferpool-quadratic.xml [ini-template] ### Server ByteBufferPool Configuration diff --git a/jetty-core/jetty-server/src/main/config/modules/bytebufferpool.mod b/jetty-core/jetty-server/src/main/config/modules/bytebufferpool.mod index 35a347b77443..25db6dc6cc02 100644 --- a/jetty-core/jetty-server/src/main/config/modules/bytebufferpool.mod +++ b/jetty-core/jetty-server/src/main/config/modules/bytebufferpool.mod @@ -1,6 +1,7 @@ [description] -Configures the ByteBufferPool used by ServerConnectors. -Use module "bytebufferpool-logarithmic" for a pool may hold less granulated sized buffers. +Configures the RetainableByteBufferPool used by ServerConnectors. +The bucket sizes increase linearly. +Use module "bytebufferpool-quadratic" for a pool that holds more coarse sized buffers. [depends] logging diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java index 1fb5f5fb410b..75e2fbe7751b 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java @@ -15,7 +15,6 @@ import java.io.IOException; import java.net.Socket; -import java.nio.ByteBuffer; import java.nio.channels.ClosedByInterruptException; import java.util.ArrayList; import java.util.Collection; @@ -24,6 +23,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; @@ -31,10 +31,10 @@ import java.util.concurrent.locks.Condition; import java.util.stream.Collectors; -import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.EndPoint; -import org.eclipse.jetty.io.LogarithmicArrayByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.server.internal.HttpConnection; import org.eclipse.jetty.util.ProcessorUtils; @@ -65,8 +65,9 @@ * to the connections to time such things as asynchronous request timeouts. The default is to use a new * {@link ScheduledExecutorScheduler} instance. * - *
  • The {@link ByteBufferPool} service is made available to all connections to be used to acquire and release - * {@link ByteBuffer} instances from a pool. The default is to use a new {@link ArrayByteBufferPool} instance. + *
  • The {@link RetainableByteBufferPool} service is made available to all connections to be used to acquire and release + * {@link RetainableByteBuffer} instances from a pool. The default is to use a new {@link ArrayRetainableByteBufferPool} + * instance. *
  • * * These services are managed as aggregate beans by the {@link ContainerLifeCycle} super class and @@ -144,7 +145,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co private final Server _server; private final Executor _executor; private final Scheduler _scheduler; - private final ByteBufferPool _byteBufferPool; + private final RetainableByteBufferPool _retainableByteBufferPool; private final Thread[] _acceptors; private final Set _endpoints = Collections.newSetFromMap(new ConcurrentHashMap<>()); private final Set _immutableEndPoints = Collections.unmodifiableSet(_endpoints); @@ -160,43 +161,32 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co private ThreadPoolBudget.Lease _lease; /** - * @param server The server this connector will be added to. Must not be null. - * @param executor An executor for this connector or null to use the servers executor - * @param scheduler A scheduler for this connector or null to either a {@link Scheduler} set as a server bean or if none set, then a new {@link ScheduledExecutorScheduler} instance. - * @param pool A buffer pool for this connector or null to either a {@link ByteBufferPool} set as a server bean or none set, the new {@link ArrayByteBufferPool} instance. - * @param acceptors the number of acceptor threads to use, or -1 for a default value. If 0, then no acceptor threads will be launched and some other mechanism will need to be used to accept new connections. - * @param factories The Connection Factories to use. + * @param server The {@link Server} this connector will be added to, must not be null + * @param executor An {@link Executor} for this connector or null to use the Server's Executor + * @param scheduler A {@link Scheduler} for this connector or null to use the Server's Scheduler + * @param bufferPool A {@link RetainableByteBufferPool} for this connector or null to use the Server's RetainableByteBufferPool + * @param acceptors the number of acceptor threads to use, or -1 for a default value. + * If 0, then no acceptor threads will be launched and some other mechanism will need to be used to accept new connections. + * @param factories The {@link ConnectionFactory} instances to use */ public AbstractConnector( Server server, Executor executor, Scheduler scheduler, - ByteBufferPool pool, + RetainableByteBufferPool bufferPool, int acceptors, ConnectionFactory... factories) { - _server = server; + _server = Objects.requireNonNull(server); + _executor = executor != null ? executor : _server.getThreadPool(); - addBean(_executor); - if (executor == null) - unmanage(_executor); // inherited from server - if (scheduler == null) - scheduler = _server.getBean(Scheduler.class); - _scheduler = scheduler != null ? scheduler : new ScheduledExecutorScheduler(String.format("Connector-Scheduler-%x", hashCode()), false); - addBean(_scheduler); - - if (pool == null) - { - // Look for (and cache) a common pool on the server - pool = server.getBean(ByteBufferPool.class); - if (pool == null) - { - pool = new LogarithmicArrayByteBufferPool(); - server.addBean(pool, true); - } - } - _byteBufferPool = pool; - addBean(pool.asRetainableByteBufferPool()); + addBean(_executor, executor != null); + + _scheduler = scheduler != null ? scheduler : _server.getScheduler(); + addBean(_scheduler, scheduler != null); + + _retainableByteBufferPool = bufferPool != null ? bufferPool : server.getRetainableByteBufferPool(); + addBean(_retainableByteBufferPool, bufferPool != null); for (ConnectionFactory factory : factories) { @@ -224,9 +214,9 @@ public Executor getExecutor() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return _byteBufferPool; + return _retainableByteBufferPool; } @Override diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractNetworkConnector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractNetworkConnector.java index 679262ec1d17..3435ac7e1090 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractNetworkConnector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractNetworkConnector.java @@ -17,7 +17,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.thread.Scheduler; @@ -33,9 +33,9 @@ public abstract class AbstractNetworkConnector extends AbstractConnector impleme private volatile String _host; private volatile int _port = 0; - public AbstractNetworkConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, ConnectionFactory... factories) + public AbstractNetworkConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, int acceptors, ConnectionFactory... factories) { - super(server, executor, scheduler, pool, acceptors, factories); + super(server, executor, scheduler, bufferPool, acceptors, factories); } public void setHost(String host) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Components.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Components.java index e3ef0ec52d68..b605f45c533a 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Components.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Components.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.server; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Attributes; import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.ThreadPool; @@ -23,7 +23,7 @@ */ public interface Components { - ByteBufferPool getByteBufferPool(); + RetainableByteBufferPool getRetainableByteBufferPool(); Scheduler getScheduler(); diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionMetaData.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionMetaData.java index 8b7b68d42e5f..1fe6c1c44a13 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionMetaData.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionMetaData.java @@ -38,7 +38,7 @@ public interface ConnectionMetaData extends Attributes Connection getConnection(); // TODO should this be only here or only on HttpChannel, should not be on both. - // Currently mostly used to get stuff like ByteBufferPool and Scheduler - maybe provide those directly? + // Currently mostly used to get stuff like RetainableByteBufferPool and Scheduler - maybe provide those directly? Connector getConnector(); boolean isPersistent(); diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java index 4f6e72ba719e..59721ff09f7c 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java @@ -17,8 +17,8 @@ import java.util.List; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; @@ -51,9 +51,9 @@ public interface Connector extends LifeCycle, Container, Graceful public Scheduler getScheduler(); /** - * @return the {@link ByteBufferPool} to acquire buffers from and release buffers to + * @return the {@link RetainableByteBufferPool} to acquire buffers from and release buffers to */ - public ByteBufferPool getByteBufferPool(); + public RetainableByteBufferPool getRetainableByteBufferPool(); /** * @param nextProtocol the next protocol diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/DetectorConnectionFactory.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/DetectorConnectionFactory.java index f2528163b254..044312eae47d 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/DetectorConnectionFactory.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/DetectorConnectionFactory.java @@ -22,6 +22,7 @@ import org.eclipse.jetty.io.AbstractConnection; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.util.BufferUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -138,13 +139,13 @@ public Connection newConnection(Connector connector, EndPoint endPoint) private class DetectorConnection extends AbstractConnection implements Connection.UpgradeFrom, Connection.UpgradeTo { private final Connector _connector; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private DetectorConnection(EndPoint endp, Connector connector) { super(endp, connector.getExecutor()); _connector = connector; - _buffer = connector.getByteBufferPool().acquire(getInputBufferSize(), true); + _buffer = connector.getRetainableByteBufferPool().acquire(getInputBufferSize(), true); } @Override @@ -152,7 +153,7 @@ public void onUpgradeTo(ByteBuffer buffer) { if (LOG.isDebugEnabled()) LOG.debug("Detector {} copying unconsumed buffer {}", getProtocol(), BufferUtil.toDetailString(buffer)); - BufferUtil.append(_buffer, buffer); + BufferUtil.append(_buffer.getByteBuffer(), buffer); } @Override @@ -161,9 +162,9 @@ public ByteBuffer onUpgradeFrom() if (_buffer.hasRemaining()) { ByteBuffer unconsumed = ByteBuffer.allocateDirect(_buffer.remaining()); - unconsumed.put(_buffer); + unconsumed.put(_buffer.getByteBuffer()); unconsumed.flip(); - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); return unconsumed; } return null; @@ -182,15 +183,16 @@ public void onFillable() { try { - while (BufferUtil.space(_buffer) > 0) + ByteBuffer byteBuffer = _buffer.getByteBuffer(); + while (BufferUtil.space(byteBuffer) > 0) { // Read data - int fill = getEndPoint().fill(_buffer); + int fill = getEndPoint().fill(byteBuffer); if (LOG.isDebugEnabled()) LOG.debug("Detector {} filled buffer with {} bytes", getProtocol(), fill); if (fill < 0) { - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); getEndPoint().shutdownOutput(); return; } @@ -200,6 +202,7 @@ public void onFillable() return; } + // TODO: release the buffer before returning. if (detectAndUpgrade()) return; } @@ -220,7 +223,7 @@ public void onFillable() */ private boolean detectAndUpgrade() { - if (BufferUtil.isEmpty(_buffer)) + if (!_buffer.hasRemaining()) { if (LOG.isDebugEnabled()) LOG.debug("Detector {} skipping detection on an empty buffer", getProtocol()); @@ -232,9 +235,9 @@ private boolean detectAndUpgrade() boolean notRecognized = true; for (Detecting detectingConnectionFactory : _detectingConnectionFactories) { - Detection detection = detectingConnectionFactory.detect(_buffer); + Detection detection = detectingConnectionFactory.detect(_buffer.getByteBuffer()); if (LOG.isDebugEnabled()) - LOG.debug("Detector {} performed detection from {} with {} which returned {}", getProtocol(), BufferUtil.toDetailString(_buffer), detectingConnectionFactory, detection); + LOG.debug("Detector {} performed detection from {} with {} which returned {}", getProtocol(), _buffer, detectingConnectionFactory, detection); if (detection == Detection.RECOGNIZED) { try @@ -275,7 +278,7 @@ private boolean detectAndUpgrade() // No DetectingConnectionFactory recognized those bytes -> call unsuccessful detection callback. if (LOG.isDebugEnabled()) LOG.debug("Detector {} failed to detect a known protocol, falling back to nextProtocol()", getProtocol()); - nextProtocol(_connector, getEndPoint(), _buffer); + nextProtocol(_connector, getEndPoint(), _buffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Detector {} call to nextProtocol() succeeded, assuming upgrade performed", getProtocol()); return true; @@ -288,7 +291,7 @@ private void releaseAndClose() { if (LOG.isDebugEnabled()) LOG.debug("Detector {} releasing buffer and closing", getProtocol()); - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); close(); } } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java index 6b1795eebfd0..61733866f4af 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java @@ -26,8 +26,8 @@ import org.eclipse.jetty.http.HttpParser; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.io.ByteArrayEndPoint; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.ByteArrayOutputStream2; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -46,9 +46,9 @@ public class LocalConnector extends AbstractConnector { private final BlockingQueue _connects = new LinkedBlockingQueue<>(); - public LocalConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, ConnectionFactory... factories) + public LocalConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, int acceptors, ConnectionFactory... factories) { - super(server, executor, scheduler, pool, acceptors, factories); + super(server, executor, scheduler, bufferPool, acceptors, factories); setIdleTimeout(30000); } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LowResourceMonitor.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LowResourceMonitor.java index 07c7ab427d7e..b4db07f82ed6 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LowResourceMonitor.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/LowResourceMonitor.java @@ -322,7 +322,7 @@ protected void monitor() @Override protected void doStart() throws Exception { - _scheduler = _server.getBean(Scheduler.class); + _scheduler = _server.getScheduler(); if (_scheduler == null) { diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NetworkTrafficServerConnector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NetworkTrafficServerConnector.java index d0fdb50c5a5b..fd34d134e55e 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NetworkTrafficServerConnector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/NetworkTrafficServerConnector.java @@ -17,10 +17,10 @@ import java.nio.channels.SocketChannel; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ManagedSelector; import org.eclipse.jetty.io.NetworkTrafficListener; import org.eclipse.jetty.io.NetworkTrafficSocketChannelEndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SocketChannelEndPoint; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.Scheduler; @@ -49,9 +49,9 @@ public NetworkTrafficServerConnector(Server server, ConnectionFactory connection super(server, connectionFactory); } - public NetworkTrafficServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, int selectors, ConnectionFactory... factories) + public NetworkTrafficServerConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, int acceptors, int selectors, ConnectionFactory... factories) { - super(server, executor, scheduler, pool, acceptors, selectors, factories); + super(server, executor, scheduler, bufferPool, acceptors, selectors, factories); } public NetworkTrafficServerConnector(Server server, SslContextFactory.Server sslContextFactory) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java index cb0a85b4cb6c..20d8d8439ed5 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java @@ -29,10 +29,11 @@ import org.eclipse.jetty.io.AbstractConnection; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.util.Attributes; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.util.TypeUtil; +import org.eclipse.jetty.util.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -146,7 +147,7 @@ private static class ProxyProtocolV1Connection extends AbstractConnection implem private final Connector _connector; private final ConnectionFactory _next; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private final StringBuilder _builder = new StringBuilder(); private final String[] _fields = new String[6]; private int _index; @@ -157,7 +158,7 @@ private ProxyProtocolV1Connection(EndPoint endp, Connector connector, Connection super(endp, connector.getExecutor()); _connector = connector; _next = next; - _buffer = _connector.getByteBufferPool().acquire(getInputBufferSize(), true); + _buffer = _connector.getRetainableByteBufferPool().acquire(getInputBufferSize(), true); } @Override @@ -170,12 +171,12 @@ public void onFillable() while (_index < LF_INDEX) { // Read data - int fill = getEndPoint().fill(_buffer); + int fill = getEndPoint().fill(_buffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Proxy v1 filled buffer with {} bytes", fill); if (fill < 0) { - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); getEndPoint().shutdownOutput(); return; } @@ -239,9 +240,9 @@ public ByteBuffer onUpgradeFrom() if (_buffer.hasRemaining()) { ByteBuffer unconsumed = ByteBuffer.allocateDirect(_buffer.remaining()); - unconsumed.put(_buffer); + unconsumed.put(_buffer.getByteBuffer()); unconsumed.flip(); - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); return unconsumed; } return null; @@ -252,7 +253,7 @@ public void onUpgradeTo(ByteBuffer buffer) { if (LOG.isDebugEnabled()) LOG.debug("Proxy v1 copying unconsumed buffer {}", BufferUtil.toDetailString(buffer)); - BufferUtil.append(_buffer, buffer); + BufferUtil.append(_buffer.getByteBuffer(), buffer); } /** @@ -261,13 +262,13 @@ public void onUpgradeTo(ByteBuffer buffer) private boolean parse() throws IOException { if (LOG.isDebugEnabled()) - LOG.debug("Proxy v1 parsing {}", BufferUtil.toDetailString(_buffer)); + LOG.debug("Proxy v1 parsing {}", _buffer); _length += _buffer.remaining(); // Parse fields while (_buffer.hasRemaining()) { - byte b = _buffer.get(); + byte b = _buffer.getByteBuffer().get(); if (_index < CR_INDEX) { if (b == ' ' || b == '\r') @@ -310,7 +311,7 @@ private void releaseAndClose() { if (LOG.isDebugEnabled()) LOG.debug("Proxy v1 releasing buffer and closing"); - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); close(); } @@ -439,7 +440,7 @@ private class ProxyProtocolV2Connection extends AbstractConnection implements Co private final Connector _connector; private final ConnectionFactory _next; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private boolean _local; private Family _family; private int _length; @@ -450,7 +451,7 @@ protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, Connecti super(endp, connector.getExecutor()); _connector = connector; _next = next; - _buffer = _connector.getByteBufferPool().acquire(getInputBufferSize(), true); + _buffer = _connector.getRetainableByteBufferPool().acquire(getInputBufferSize(), true); } @Override @@ -458,7 +459,7 @@ public void onUpgradeTo(ByteBuffer buffer) { if (LOG.isDebugEnabled()) LOG.debug("Proxy v2 copying unconsumed buffer {}", BufferUtil.toDetailString(buffer)); - BufferUtil.append(_buffer, buffer); + BufferUtil.append(_buffer.getByteBuffer(), buffer); } @Override @@ -501,12 +502,12 @@ public void onFillable() while (!_headerParsed) { // Read data - int fill = getEndPoint().fill(_buffer); + int fill = getEndPoint().fill(_buffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Proxy v2 filled buffer with {} bytes", fill); if (fill < 0) { - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); getEndPoint().shutdownOutput(); return; } @@ -520,17 +521,17 @@ public void onFillable() } if (LOG.isDebugEnabled()) - LOG.debug("Proxy v2 onFillable header parsed, length = {}, buffer = {}", _length, BufferUtil.toDetailString(_buffer)); + LOG.debug("Proxy v2 onFillable header parsed, length = {}, buffer = {}", _length, _buffer); while (_buffer.remaining() < _length) { // Read data - int fill = getEndPoint().fill(_buffer); + int fill = getEndPoint().fill(_buffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("Proxy v2 filled buffer with {} bytes", fill); if (fill < 0) { - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); getEndPoint().shutdownOutput(); return; } @@ -558,9 +559,9 @@ public ByteBuffer onUpgradeFrom() if (_buffer.hasRemaining()) { ByteBuffer unconsumed = ByteBuffer.allocateDirect(_buffer.remaining()); - unconsumed.put(_buffer); + unconsumed.put(_buffer.getByteBuffer()); unconsumed.flip(); - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); return unconsumed; } return null; @@ -570,17 +571,18 @@ private void parseBodyAndUpgrade() throws IOException { int nonProxyRemaining = _buffer.remaining() - _length; if (LOG.isDebugEnabled()) - LOG.debug("Proxy v2 parsing body, length = {}, buffer = {}", _length, BufferUtil.toHexSummary(_buffer)); + LOG.debug("Proxy v2 parsing body, length = {}, buffer = {}", _length, _buffer); if (LOG.isDebugEnabled()) - LOG.debug("Proxy v2 body {} from {} for {}", _next, BufferUtil.toHexSummary(_buffer), this); + LOG.debug("Proxy v2 body {} from {} for {}", _next, _buffer, this); // Do we need to wrap the endpoint? + ByteBuffer byteBuffer = _buffer.getByteBuffer(); ProxyEndPoint proxyEndPoint; EndPoint endPoint = getEndPoint(); if (_local) { - _buffer.position(_buffer.position() + _length); + byteBuffer.position(byteBuffer.position() + _length); proxyEndPoint = new ProxyEndPoint(endPoint, endPoint.getLocalSocketAddress(), endPoint.getRemoteSocketAddress()); } else @@ -592,12 +594,12 @@ private void parseBodyAndUpgrade() throws IOException case INET: { byte[] addr = new byte[4]; - _buffer.get(addr); + byteBuffer.get(addr); InetAddress srcAddr = Inet4Address.getByAddress(addr); - _buffer.get(addr); + byteBuffer.get(addr); InetAddress dstAddr = Inet4Address.getByAddress(addr); - int srcPort = _buffer.getChar(); - int dstPort = _buffer.getChar(); + int srcPort = byteBuffer.getChar(); + int dstPort = byteBuffer.getChar(); local = new InetSocketAddress(dstAddr, dstPort); remote = new InetSocketAddress(srcAddr, srcPort); break; @@ -605,12 +607,12 @@ private void parseBodyAndUpgrade() throws IOException case INET6: { byte[] addr = new byte[16]; - _buffer.get(addr); + byteBuffer.get(addr); InetAddress srcAddr = Inet6Address.getByAddress(addr); - _buffer.get(addr); + byteBuffer.get(addr); InetAddress dstAddr = Inet6Address.getByAddress(addr); - int srcPort = _buffer.getChar(); - int dstPort = _buffer.getChar(); + int srcPort = byteBuffer.getChar(); + int dstPort = byteBuffer.getChar(); local = new InetSocketAddress(dstAddr, dstPort); remote = new InetSocketAddress(srcAddr, srcPort); break; @@ -618,9 +620,9 @@ private void parseBodyAndUpgrade() throws IOException case UNIX: { byte[] addr = new byte[108]; - _buffer.get(addr); + byteBuffer.get(addr); String src = UnixDomain.toPath(addr); - _buffer.get(addr); + byteBuffer.get(addr); String dst = UnixDomain.toPath(addr); local = UnixDomain.newSocketAddress(dst); remote = UnixDomain.newSocketAddress(src); @@ -634,15 +636,15 @@ private void parseBodyAndUpgrade() throws IOException proxyEndPoint = new ProxyEndPoint(endPoint, local, remote); // Any additional info? - while (_buffer.remaining() > nonProxyRemaining) + while (byteBuffer.remaining() > nonProxyRemaining) { - int type = 0xff & _buffer.get(); - int length = _buffer.getChar(); + int type = 0xff & byteBuffer.get(); + int length = byteBuffer.getChar(); byte[] value = new byte[length]; - _buffer.get(value); + byteBuffer.get(value); if (LOG.isDebugEnabled()) - LOG.debug(String.format("Proxy v2 T=%x L=%d V=%s for %s", type, length, TypeUtil.toHexString(value), this)); + LOG.debug(String.format("Proxy v2 T=%x L=%d V=%s for %s", type, length, StringUtil.toHexString(value), this)); // PP2_TYPE_NOOP is only used for byte alignment, skip them. if (type != ProxyEndPoint.PP2_TYPE_NOOP) @@ -683,12 +685,14 @@ private void parseBodyAndUpgrade() throws IOException private void parseHeader() throws IOException { if (LOG.isDebugEnabled()) - LOG.debug("Proxy v2 parsing fixed length packet part, buffer = {}", BufferUtil.toDetailString(_buffer)); + LOG.debug("Proxy v2 parsing fixed length packet part, buffer = {}", _buffer); if (_buffer.remaining() < HEADER_LENGTH) return; if (LOG.isDebugEnabled()) - LOG.debug("Proxy v2 header {} for {}", BufferUtil.toHexSummary(_buffer), this); + LOG.debug("Proxy v2 header {} for {}", _buffer, this); + + ByteBuffer byteBuffer = _buffer.getByteBuffer(); // struct proxy_hdr_v2 { // uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */ @@ -698,16 +702,16 @@ private void parseHeader() throws IOException // }; for (byte signatureByte : SIGNATURE) { - if (_buffer.get() != signatureByte) + if (byteBuffer.get() != signatureByte) throw new IOException("Proxy v2 bad PROXY signature"); } - int versionAndCommand = 0xFF & _buffer.get(); + int versionAndCommand = 0xFF & byteBuffer.get(); if ((versionAndCommand & 0xF0) != 0x20) throw new IOException("Proxy v2 bad PROXY version"); _local = (versionAndCommand & 0xF) == 0x00; - int transportAndFamily = 0xFF & _buffer.get(); + int transportAndFamily = 0xFF & byteBuffer.get(); switch (transportAndFamily >> 4) { case 0: @@ -742,7 +746,7 @@ private void parseHeader() throws IOException throw new IOException("Proxy v2 bad PROXY family"); } - _length = _buffer.getChar(); + _length = byteBuffer.getChar(); if (!_local && (_family == Family.UNSPEC || transport != Transport.STREAM)) throw new IOException(String.format("Proxy v2 unsupported PROXY mode 0x%x,0x%x", versionAndCommand, transportAndFamily)); @@ -757,7 +761,7 @@ private void parseHeader() throws IOException private void releaseAndClose() { - _connector.getByteBufferPool().release(_buffer); + _buffer.release(); close(); } } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java index d633b9a446c2..10b6cc240f79 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java @@ -40,8 +40,9 @@ import org.eclipse.jetty.http.QuotedQualityCSV; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContent; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; @@ -855,18 +856,17 @@ private static class ContentWriterIteratingCallback extends IteratingCallback private final ReadableByteChannel source; private final Content.Sink sink; private final Callback callback; - private final ByteBuffer byteBuffer; - private final ByteBufferPool byteBufferPool; + private final RetainableByteBuffer buffer; public ContentWriterIteratingCallback(HttpContent content, Response target, Callback callback) throws IOException { - this.byteBufferPool = target.getRequest().getComponents().getByteBufferPool(); this.source = content.getResource().newReadableByteChannel(); this.sink = target; this.callback = callback; + RetainableByteBufferPool bufferPool = target.getRequest().getComponents().getRetainableByteBufferPool(); int outputBufferSize = target.getRequest().getConnectionMetaData().getHttpConfiguration().getOutputBufferSize(); boolean useOutputDirectByteBuffers = target.getRequest().getConnectionMetaData().getHttpConfiguration().isUseOutputDirectByteBuffers(); - this.byteBuffer = byteBufferPool.acquire(outputBufferSize, useOutputDirectByteBuffers); + this.buffer = bufferPool.acquire(outputBufferSize, useOutputDirectByteBuffers); } @Override @@ -875,6 +875,7 @@ protected Action process() throws Throwable if (!source.isOpen()) return Action.SUCCEEDED; + ByteBuffer byteBuffer = buffer.getByteBuffer(); BufferUtil.clearToFill(byteBuffer); int read = source.read(byteBuffer); if (read == -1) @@ -891,14 +892,14 @@ protected Action process() throws Throwable @Override protected void onCompleteSuccess() { - byteBufferPool.release(byteBuffer); + buffer.release(); callback.succeeded(); } @Override protected void onCompleteFailure(Throwable x) { - byteBufferPool.release(byteBuffer); + buffer.release(); callback.failed(x); } } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index 37d5b385dcca..e94459baa63d 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -35,7 +35,9 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.PreEncodedHttpField; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ErrorProcessor; import org.eclipse.jetty.util.Attributes; @@ -57,6 +59,8 @@ import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; +import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.ShutdownThread; import org.eclipse.jetty.util.thread.ThreadPool; import org.slf4j.Logger; @@ -69,6 +73,8 @@ public class Server extends Handler.Wrapper implements Attributes private final AttributeContainerMap _attributes = new AttributeContainerMap(); private final ThreadPool _threadPool; + private final Scheduler _scheduler; + private final RetainableByteBufferPool _bufferPool; private final List _connectors = new CopyOnWriteArrayList<>(); private final Context _serverContext = new ServerContext(); private final AutoLock _dateLock = new AutoLock(); @@ -102,7 +108,7 @@ public Server(@Name("port") int port) this((ThreadPool)null); ServerConnector connector = new ServerConnector(this); connector.setPort(port); - setConnectors(new Connector[]{connector}); + addConnector(connector); addBean(_attributes); } @@ -119,13 +125,22 @@ public Server(@Name("address") InetSocketAddress addr) ServerConnector connector = new ServerConnector(this); connector.setHost(addr.getHostName()); connector.setPort(addr.getPort()); - setConnectors(new Connector[]{connector}); + addConnector(connector); } public Server(@Name("threadpool") ThreadPool pool) { - _threadPool = pool != null ? pool : new QueuedThreadPool(); + this(pool, null, null); + } + + public Server(ThreadPool threadPool, Scheduler scheduler, RetainableByteBufferPool bufferPool) + { + _threadPool = threadPool != null ? threadPool : new QueuedThreadPool(); addBean(_threadPool); + _scheduler = scheduler != null ? scheduler : new ScheduledExecutorScheduler(); + addBean(_scheduler); + _bufferPool = bufferPool != null ? bufferPool : new ArrayRetainableByteBufferPool(); + addBean(_bufferPool); setServer(this); addBean(FileSystemPool.INSTANCE, false); } @@ -361,6 +376,16 @@ public ThreadPool getThreadPool() return _threadPool; } + public Scheduler getScheduler() + { + return _scheduler; + } + + public RetainableByteBufferPool getRetainableByteBufferPool() + { + return _bufferPool; + } + /** * @return true if {@link #dumpStdErr()} is called after starting */ diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java index 8d688a749531..e7998bec1eda 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java @@ -30,10 +30,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.ManagedSelector; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SelectorManager; import org.eclipse.jetty.io.SocketChannelEndPoint; import org.eclipse.jetty.util.IO; @@ -204,7 +204,7 @@ public ServerConnector( @Name("server") Server server, @Name("executor") Executor executor, @Name("scheduler") Scheduler scheduler, - @Name("bufferPool") ByteBufferPool bufferPool, + @Name("bufferPool") RetainableByteBufferPool bufferPool, @Name("acceptors") int acceptors, @Name("selectors") int selectors, @Name("factories") ConnectionFactory... factories) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SslConnectionFactory.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SslConnectionFactory.java index d5256392feb9..407bacb068eb 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SslConnectionFactory.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SslConnectionFactory.java @@ -21,7 +21,6 @@ import org.eclipse.jetty.http.HttpVersion; 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.RetainableByteBufferPool; @@ -167,22 +166,17 @@ public Connection newConnection(Connector connector, EndPoint endPoint) protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = byteBufferPool.asRetainableByteBufferPool(); - return new SslConnection(retainableByteBufferPool, byteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()); + RetainableByteBufferPool retainableByteBufferPool = connector.getRetainableByteBufferPool(); + return new SslConnection(retainableByteBufferPool, connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()); } @Override protected AbstractConnection configure(AbstractConnection connection, Connector connector, EndPoint endPoint) { - if (connection instanceof SslConnection) + if (connection instanceof SslConnection sslConnection) { - SslConnection sslConnection = (SslConnection)connection; - if (connector instanceof ContainerLifeCycle) - { - ContainerLifeCycle container = (ContainerLifeCycle)connector; + if (connector instanceof ContainerLifeCycle container) container.getBeans(SslHandshakeListener.class).forEach(sslConnection::addHandshakeListener); - } getBeans(SslHandshakeListener.class).forEach(sslConnection::addHandshakeListener); } return super.configure(connection, connector, endPoint); diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/BufferedResponseHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/BufferedResponseHandler.java index ae2c8697c9e4..0f0dd3267fa6 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/BufferedResponseHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/BufferedResponseHandler.java @@ -21,7 +21,8 @@ import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.pathmap.PathSpecSet; import org.eclipse.jetty.io.ByteBufferAccumulator; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionMetaData; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; @@ -188,9 +189,9 @@ public void write(boolean last, ByteBuffer byteBuffer, Callback callback) if (shouldBuffer(this, last)) { ConnectionMetaData connectionMetaData = getRequest().getConnectionMetaData(); - ByteBufferPool byteBufferPool = connectionMetaData.getConnector().getByteBufferPool(); + RetainableByteBufferPool bufferPool = connectionMetaData.getConnector().getRetainableByteBufferPool(); boolean useOutputDirectByteBuffers = connectionMetaData.getHttpConfiguration().isUseOutputDirectByteBuffers(); - _accumulator = new CountingByteBufferAccumulator(byteBufferPool, useOutputDirectByteBuffers, getBufferSize()); + _accumulator = new CountingByteBufferAccumulator(bufferPool, useOutputDirectByteBuffers, getBufferSize()); } _firstWrite = false; } @@ -211,7 +212,8 @@ protected Action process() complete = last && !current.hasRemaining(); if (write || complete) { - BufferedResponse.super.write(complete, _accumulator.takeByteBuffer(), this); + RetainableByteBuffer buffer = _accumulator.takeRetainableByteBuffer(); + BufferedResponse.super.write(complete, buffer.getByteBuffer(), Callback.from(this, buffer::release)); return Action.SCHEDULED; } return Action.SUCCEEDED; @@ -236,9 +238,18 @@ public void succeeded() { // TODO pass all accumulated buffers as an array instead of allocating & copying into a single one. if (_accumulator != null) - super.write(true, _accumulator.takeByteBuffer(), Callback.from(_callback, _accumulator::close)); + { + RetainableByteBuffer buffer = _accumulator.takeRetainableByteBuffer(); + super.write(true, buffer.getByteBuffer(), Callback.from(_callback, () -> + { + buffer.release(); + _accumulator.close(); + })); + } else + { _callback.succeeded(); + } } @Override @@ -246,9 +257,7 @@ public void failed(Throwable x) { if (_accumulator != null) _accumulator.close(); - else - // TODO: this callback should always be failed! - _callback.failed(x); + _callback.failed(x); } } @@ -258,7 +267,7 @@ private static class CountingByteBufferAccumulator implements AutoCloseable private final int _maxSize; private int _accumulatedCount; - private CountingByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct, int maxSize) + private CountingByteBufferAccumulator(RetainableByteBufferPool bufferPool, boolean direct, int maxSize) { if (maxSize <= 0) throw new IllegalArgumentException("maxSize must be > 0, was: " + maxSize); @@ -290,10 +299,10 @@ private int space() return _maxSize - _accumulatedCount; } - private ByteBuffer takeByteBuffer() + private RetainableByteBuffer takeRetainableByteBuffer() { _accumulatedCount = 0; - return _accumulator.takeByteBuffer(); + return _accumulator.takeRetainableByteBuffer(); } @Override diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java index 8d999a7cfa90..a66b69b2c36f 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java @@ -32,11 +32,11 @@ import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpURI; 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.ManagedSelector; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SelectorManager; import org.eclipse.jetty.io.SocketChannelEndPoint; import org.eclipse.jetty.server.Handler; @@ -48,7 +48,6 @@ import org.eclipse.jetty.util.HostPort; import org.eclipse.jetty.util.IteratingCallback; import org.eclipse.jetty.util.Promise; -import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; import org.eclipse.jetty.util.thread.Scheduler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,7 +63,7 @@ public class ConnectHandler extends Handler.Wrapper private final Set blackList = new HashSet<>(); private Executor executor; private Scheduler scheduler; - private ByteBufferPool bufferPool; + private RetainableByteBufferPool bufferPool; private SelectorManager selector; private long connectTimeout = 15000; private long idleTimeout = 30000; @@ -101,12 +100,12 @@ public void setScheduler(Scheduler scheduler) this.scheduler = scheduler; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return bufferPool; } - public void setByteBufferPool(ByteBufferPool bufferPool) + public void setRetainableByteBufferPool(RetainableByteBufferPool bufferPool) { updateBean(this.bufferPool, bufferPool); this.bufferPool = bufferPool; @@ -162,15 +161,13 @@ protected void doStart() throws Exception if (scheduler == null) { - scheduler = getServer().getBean(Scheduler.class); - if (scheduler == null) - scheduler = new ScheduledExecutorScheduler(String.format("Proxy-Scheduler-%x", hashCode()), false); + scheduler = getServer().getScheduler(); addBean(scheduler); } if (bufferPool == null) { - bufferPool = new MappedByteBufferPool(); + bufferPool = getServer().getRetainableByteBufferPool(); addBean(bufferPool); } @@ -385,12 +382,12 @@ protected boolean handleAuthentication(Request request, Response response, Strin protected DownstreamConnection newDownstreamConnection(EndPoint endPoint, ConcurrentMap context) { - return new DownstreamConnection(endPoint, getExecutor(), getByteBufferPool(), context); + return new DownstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), context); } protected UpstreamConnection newUpstreamConnection(EndPoint endPoint, ConnectContext connectContext) { - return new UpstreamConnection(endPoint, getExecutor(), getByteBufferPool(), connectContext); + return new UpstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), connectContext); } protected void prepareContext(Request request, ConcurrentMap context) @@ -556,7 +553,7 @@ public class UpstreamConnection extends TunnelConnection { private final ConnectContext connectContext; - public UpstreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool bufferPool, ConnectContext connectContext) + public UpstreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, ConnectContext connectContext) { super(endPoint, executor, bufferPool, connectContext.getContext()); this.connectContext = connectContext; @@ -592,7 +589,7 @@ public class DownstreamConnection extends TunnelConnection implements Connection { private ByteBuffer buffer; - public DownstreamConnection(EndPoint endPoint, Executor executor, ByteBufferPool bufferPool, ConcurrentMap context) + public DownstreamConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, ConcurrentMap context) { super(endPoint, executor, bufferPool, context); } @@ -659,18 +656,18 @@ protected void write(EndPoint endPoint, ByteBuffer buffer, Callback callback) private abstract static class TunnelConnection extends AbstractConnection { private final IteratingCallback pipe = new ProxyIteratingCallback(); - private final ByteBufferPool bufferPool; + private final RetainableByteBufferPool bufferPool; private final ConcurrentMap context; private TunnelConnection connection; - protected TunnelConnection(EndPoint endPoint, Executor executor, ByteBufferPool bufferPool, ConcurrentMap context) + protected TunnelConnection(EndPoint endPoint, Executor executor, RetainableByteBufferPool bufferPool, ConcurrentMap context) { super(endPoint, executor); this.bufferPool = bufferPool; this.context = context; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return bufferPool; } @@ -718,7 +715,7 @@ public String toConnectionString() private class ProxyIteratingCallback extends IteratingCallback { - private ByteBuffer buffer; + private RetainableByteBuffer buffer; private int filled; @Override @@ -727,21 +724,22 @@ protected Action process() buffer = bufferPool.acquire(getInputBufferSize(), true); try { - int filled = this.filled = read(getEndPoint(), buffer); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + int filled = this.filled = read(getEndPoint(), byteBuffer); if (filled > 0) { - write(connection.getEndPoint(), buffer, this); + write(connection.getEndPoint(), byteBuffer, this); return Action.SCHEDULED; } else if (filled == 0) { - bufferPool.release(buffer); + buffer.release(); fillInterested(); return Action.IDLE; } else { - bufferPool.release(buffer); + buffer.release(); connection.getEndPoint().shutdownOutput(); return Action.SUCCEEDED; } @@ -750,7 +748,7 @@ else if (filled == 0) { if (LOG.isDebugEnabled()) LOG.debug("Could not fill {}", TunnelConnection.this, x); - bufferPool.release(buffer); + buffer.release(); disconnect(x); return Action.SUCCEEDED; } @@ -761,7 +759,7 @@ public void succeeded() { if (LOG.isDebugEnabled()) LOG.debug("Wrote {} bytes {}", filled, TunnelConnection.this); - bufferPool.release(buffer); + buffer.release(); super.succeeded(); } @@ -775,7 +773,7 @@ protected void onCompleteFailure(Throwable x) { if (LOG.isDebugEnabled()) LOG.debug("Failed to write {} bytes {}", filled, TunnelConnection.this, x); - bufferPool.release(buffer); + buffer.release(); disconnect(x); } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorProcessor.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorProcessor.java index 2d8bdd8edc95..97aba33ae590 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorProcessor.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorProcessor.java @@ -41,6 +41,7 @@ import org.eclipse.jetty.http.QuotedQualityCSV; import org.eclipse.jetty.io.ByteBufferOutputStream; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Server; @@ -202,74 +203,83 @@ else if (charsets.contains(StandardCharsets.ISO_8859_1)) int bufferSize = request.getConnectionMetaData().getHttpConfiguration().getOutputBufferSize(); bufferSize = Math.min(8192, bufferSize); // TODO ? - ByteBuffer buffer = request.getComponents().getByteBufferPool().acquire(bufferSize, false); + RetainableByteBuffer buffer = request.getComponents().getRetainableByteBufferPool().acquire(bufferSize, false); - // write into the response aggregate buffer and flush it asynchronously. - // Looping to reduce size if buffer overflows - boolean showStacks = _showStacks; - while (true) + try { - try + // write into the response aggregate buffer and flush it asynchronously. + // Looping to reduce size if buffer overflows + boolean showStacks = _showStacks; + while (true) { - BufferUtil.clear(buffer); - ByteBufferOutputStream out = new ByteBufferOutputStream(buffer); - PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, charset)); - - switch (type) + try { - case TEXT_HTML -> writeErrorHtml(request, writer, charset, code, message, cause, showStacks); - case TEXT_JSON -> writeErrorJson(request, writer, code, message, cause, showStacks); - case TEXT_PLAIN -> writeErrorPlain(request, writer, code, message, cause, showStacks); - default -> throw new IllegalStateException(); + buffer.clear(); + ByteBufferOutputStream out = new ByteBufferOutputStream(buffer.getByteBuffer()); + PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, charset)); + + switch (type) + { + case TEXT_HTML -> writeErrorHtml(request, writer, charset, code, message, cause, showStacks); + case TEXT_JSON -> writeErrorJson(request, writer, code, message, cause, showStacks); + case TEXT_PLAIN -> writeErrorPlain(request, writer, code, message, cause, showStacks); + default -> throw new IllegalStateException(); + } + + writer.flush(); + break; } - - writer.flush(); - break; - } - catch (BufferOverflowException e) - { - if (showStacks) + catch (BufferOverflowException e) { + if (showStacks) + { + if (LOG.isDebugEnabled()) + LOG.debug("Disable stacks for " + e.toString()); + + showStacks = false; + continue; + } if (LOG.isDebugEnabled()) - LOG.debug("Disable stacks for " + e.toString()); + LOG.warn("Error page too large: >{} {} {} {}", bufferSize, code, message, request, e); + else + LOG.warn("Error page too large: >{} {} {} {}", bufferSize, code, message, request); - showStacks = false; - continue; + break; } - if (LOG.isDebugEnabled()) - LOG.warn("Error page too large: >{} {} {} {}", bufferSize, code, message, request, e); - else - LOG.warn("Error page too large: >{} {} {} {}", bufferSize, code, message, request); - - break; } - } - if (!buffer.hasRemaining()) - { - callback.succeeded(); - return true; - } - - response.getHeaders().put(type.getContentTypeField(charset)); - response.write(true, buffer, new Callback.Nested(callback) - { - @Override - public void succeeded() + if (!buffer.hasRemaining()) { - request.getComponents().getByteBufferPool().release(buffer); - super.succeeded(); + buffer.release(); + callback.succeeded(); + return true; } - @Override - public void failed(Throwable x) + response.getHeaders().put(type.getContentTypeField(charset)); + response.write(true, buffer.getByteBuffer(), new Callback.Nested(callback) { - request.getComponents().getByteBufferPool().release(buffer); - super.failed(x); - } - }); + @Override + public void succeeded() + { + buffer.release(); + super.succeeded(); + } - return true; + @Override + public void failed(Throwable x) + { + buffer.release(); + super.failed(x); + } + }); + + return true; + } + catch (Throwable x) + { + buffer.release(); + throw x; + } } protected void writeErrorHtml(Request request, Writer writer, Charset charset, int code, String message, Throwable cause, boolean showStacks) throws IOException diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java index f43b7fb33f03..ba84e11e175d 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java @@ -25,8 +25,7 @@ import org.eclipse.jetty.http.content.ResourceHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; import org.eclipse.jetty.http.content.VirtualHttpContentFactory; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Context; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; @@ -58,7 +57,7 @@ public class ResourceHandler extends Handler.Wrapper { private final ResourceService _resourceService; - private ByteBufferPool _byteBufferPool; + private RetainableByteBufferPool _bufferPool; private Resource _resourceBase; private Resource _styleSheet; private MimeTypes _mimeTypes; @@ -81,22 +80,21 @@ public void doStart() throws Exception _mimeTypes = context == null ? MimeTypes.DEFAULTS : context.getMimeTypes(); - _byteBufferPool = getByteBufferPool(context); + _bufferPool = getRetainableByteBufferPool(context); _resourceService.setHttpContentFactory(newHttpContentFactory()); _resourceService.setWelcomeFactory(setupWelcomeFactory()); super.doStart(); } - private ByteBufferPool getByteBufferPool(Context context) + private RetainableByteBufferPool getRetainableByteBufferPool(Context context) { if (context == null) - return new NoopByteBufferPool(); + return new RetainableByteBufferPool.NonPooling(); Server server = getServer(); if (server == null) - return new NoopByteBufferPool(); - ByteBufferPool byteBufferPool = server.getBean(ByteBufferPool.class); - return (byteBufferPool == null) ? new NoopByteBufferPool() : byteBufferPool; + return new RetainableByteBufferPool.NonPooling(); + return server.getRetainableByteBufferPool(); } public HttpContent.Factory getHttpContentFactory() @@ -110,7 +108,7 @@ protected HttpContent.Factory newHttpContentFactory() contentFactory = new FileMappingHttpContentFactory(contentFactory); contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, getPrecompressedFormats()); - contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), getByteBufferPool()); + contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), getRetainableByteBufferPool()); return contentFactory; } @@ -161,9 +159,9 @@ public Resource getBaseResource() return _resourceBase; } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return _byteBufferPool; + return _bufferPool; } /** diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipRequest.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipRequest.java index a0d8be26483d..fecb4e308d9c 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipRequest.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipRequest.java @@ -22,12 +22,12 @@ import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.PreEncodedHttpField; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.content.ContentSourceTransformer; import org.eclipse.jetty.server.Components; import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.compression.InflaterPool; public class GzipRequest extends Request.Wrapper @@ -50,7 +50,7 @@ public GzipRequest(Request request, int inflateBufferSize) if (inflateBufferSize > 0) { Components components = getComponents(); - _decoder = new Decoder(__inflaterPool, components.getByteBufferPool(), inflateBufferSize); + _decoder = new Decoder(__inflaterPool, components.getRetainableByteBufferPool(), inflateBufferSize); _gzipTransformer = new GzipTransformer(getWrapped()); } } @@ -167,17 +167,18 @@ protected Content.Chunk transform(Content.Chunk inputChunk) // Retain the input chunk because its ByteBuffer will be referenced by the Inflater. if (retain) _chunk.retain(); - ByteBuffer decodedBuffer = _decoder.decode(_chunk); + RetainableByteBuffer decodedBuffer = _decoder.decode(_chunk); - if (BufferUtil.hasContent(decodedBuffer)) + if (decodedBuffer != null && decodedBuffer.hasRemaining()) { // The decoded ByteBuffer is a transformed "copy" of the // compressed one, so it has its own reference counter. - return Content.Chunk.from(decodedBuffer, _chunk.isLast() && !_chunk.hasRemaining(), _decoder::release); + return Content.Chunk.from(decodedBuffer.getByteBuffer(), _chunk.isLast() && !_chunk.hasRemaining(), decodedBuffer::release); } else { - _decoder.release(decodedBuffer); + if (decodedBuffer != null) + decodedBuffer.release(); // Could not decode more from this chunk, release it. Content.Chunk result = _chunk.isLast() ? Content.Chunk.EOF : null; _chunk.release(); @@ -189,24 +190,26 @@ protected Content.Chunk transform(Content.Chunk inputChunk) private static class Decoder extends GZIPContentDecoder { - private ByteBuffer _decoded; + private RetainableByteBuffer _decoded; - private Decoder(InflaterPool inflaterPool, ByteBufferPool bufferPool, int bufferSize) + private Decoder(InflaterPool inflaterPool, RetainableByteBufferPool bufferPool, int bufferSize) { super(inflaterPool, bufferPool, bufferSize); } - public ByteBuffer decode(Content.Chunk chunk) + public RetainableByteBuffer decode(Content.Chunk chunk) { decodeChunks(chunk.getByteBuffer()); - ByteBuffer decoded = _decoded; + RetainableByteBuffer decoded = _decoded; _decoded = null; return decoded; } @Override - protected boolean decodedChunk(ByteBuffer decoded) + protected boolean decodedChunk(RetainableByteBuffer decoded) { + // Retain the chunk because it is stored for later use. + decoded.retain(); _decoded = decoded; return true; } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipResponseAndCallback.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipResponseAndCallback.java index 38af7c8a9920..7eb03b43e6d3 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipResponseAndCallback.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipResponseAndCallback.java @@ -25,6 +25,7 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.PreEncodedHttpField; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.BufferUtil; @@ -77,7 +78,7 @@ private enum GZState private final boolean _syncFlush; private DeflaterPool.Entry _deflaterEntry; - private ByteBuffer _buffer; + private RetainableByteBuffer _buffer; public GzipResponseAndCallback(GzipHandler handler, Request request, Response response, Callback callback) { @@ -323,7 +324,7 @@ protected void onCompleteFailure(Throwable x) protected Action process() throws Exception { if (LOG.isDebugEnabled()) - LOG.debug("GzipBufferCB.process(): _last={}, _buffer={}, _content={}", _last, BufferUtil.toDetailString(_buffer), BufferUtil.toDetailString(_content)); + LOG.debug("GzipBufferCB.process(): _last={}, _buffer={}, _content={}", _last, _buffer, BufferUtil.toDetailString(_content)); GZState gzstate = _state.get(); @@ -340,25 +341,26 @@ protected Action process() throws Exception // If we have no buffer if (_buffer == null) { - _buffer = getRequest().getComponents().getByteBufferPool().acquire(_bufferSize, false); + _buffer = getRequest().getComponents().getRetainableByteBufferPool().acquire(_bufferSize, false); + ByteBuffer byteBuffer = _buffer.getByteBuffer(); // Per RFC-1952, GZIP is LITTLE_ENDIAN - _buffer.order(ByteOrder.LITTLE_ENDIAN); - BufferUtil.flipToFill(_buffer); + byteBuffer.order(ByteOrder.LITTLE_ENDIAN); + BufferUtil.flipToFill(byteBuffer); // Add GZIP Header - _buffer.put(GZIP_HEADER, 0, GZIP_HEADER.length); + byteBuffer.put(GZIP_HEADER, 0, GZIP_HEADER.length); } else { // otherwise clear the buffer as previous writes will always fully consume. - BufferUtil.clearToFill(_buffer); + BufferUtil.clearToFill(_buffer.getByteBuffer()); } Deflater deflater = _deflaterEntry.get(); return switch (gzstate) { - case COMPRESSING -> compressing(deflater, _buffer); - case FINISHING -> finishing(deflater, _buffer); + case COMPRESSING -> compressing(deflater, _buffer.getByteBuffer()); + case FINISHING -> finishing(deflater, _buffer.getByteBuffer()); default -> throw new IllegalStateException("Unexpected state [" + _state.get() + "]"); }; } @@ -373,7 +375,7 @@ private void cleanup() if (_buffer != null) { - getRequest().getComponents().getByteBufferPool().release(_buffer); + _buffer.release(); _buffer = null; } } @@ -483,7 +485,7 @@ public String toString() super.toString(), BufferUtil.toDetailString(_content), _last, - BufferUtil.toDetailString(_buffer), + _buffer, _deflaterEntry, _state.get()); } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java index 188bd60a23b5..0b93df23cc2a 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java @@ -37,10 +37,10 @@ import org.eclipse.jetty.http.PreEncodedHttpField; import org.eclipse.jetty.http.Trailers; import org.eclipse.jetty.http.UriCompliance; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Components; import org.eclipse.jetty.server.ConnectionMetaData; import org.eclipse.jetty.server.Connector; @@ -220,15 +220,15 @@ public EndPoint getEndPoint() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return getConnectionMetaData().getConnector().getByteBufferPool(); + return getConnectionMetaData().getConnector().getRetainableByteBufferPool(); } @Override public Scheduler getScheduler() { - return getServer().getBean(Scheduler.class); + return getServer().getScheduler(); } @Override diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java index 2a5196447b23..f2794fe7c44c 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java @@ -46,7 +46,6 @@ import org.eclipse.jetty.http.Trailers; import org.eclipse.jetty.http.UriCompliance; import org.eclipse.jetty.io.AbstractConnection; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EndPoint; @@ -96,7 +95,6 @@ public class HttpConnection extends AbstractConnection implements Runnable, Writ private final RequestHandler _requestHandler; private final HttpParser _parser; private final HttpGenerator _generator; - private final ByteBufferPool _bufferPool; private final RetainableByteBufferPool _retainableByteBufferPool; private final AtomicReference _stream = new AtomicReference<>(); private final Lazy _attributes = new Lazy(); @@ -141,8 +139,7 @@ public HttpConnection(HttpConfiguration configuration, Connector connector, EndP _id = __connectionIdGenerator.getAndIncrement(); _configuration = configuration; _connector = connector; - _bufferPool = _connector.getByteBufferPool(); - _retainableByteBufferPool = _bufferPool.asRetainableByteBufferPool(); + _retainableByteBufferPool = _connector.getRetainableByteBufferPool(); _generator = newHttpGenerator(); _httpChannel = newHttpChannel(connector.getServer(), configuration); _requestHandler = newRequestHandler(); @@ -367,7 +364,7 @@ public ByteBuffer onUpgradeFrom() if (!isRequestBufferEmpty()) { ByteBuffer unconsumed = ByteBuffer.allocateDirect(_retainableByteBuffer.remaining()); - unconsumed.put(_retainableByteBuffer.getBuffer()); + unconsumed.put(_retainableByteBuffer.getByteBuffer()); unconsumed.flip(); releaseRequestBuffer(); return unconsumed; @@ -405,12 +402,12 @@ private ByteBuffer getRequestBuffer() { if (_retainableByteBuffer == null) _retainableByteBuffer = _retainableByteBufferPool.acquire(getInputBufferSize(), isUseInputDirectByteBuffers()); - return _retainableByteBuffer.getBuffer(); + return _retainableByteBuffer.getByteBuffer(); } public boolean isRequestBufferEmpty() { - return _retainableByteBuffer == null || _retainableByteBuffer.isEmpty(); + return _retainableByteBuffer == null || !_retainableByteBuffer.hasRemaining(); } @Override @@ -597,7 +594,7 @@ private boolean parseRequestBuffer() if (LOG.isDebugEnabled()) LOG.debug("{} parse {}", this, _retainableByteBuffer); - boolean handle = _parser.parseNext(_retainableByteBuffer == null ? BufferUtil.EMPTY_BUFFER : _retainableByteBuffer.getBuffer()); + boolean handle = _parser.parseNext(_retainableByteBuffer == null ? BufferUtil.EMPTY_BUFFER : _retainableByteBuffer.getByteBuffer()); if (LOG.isDebugEnabled()) LOG.debug("{} parsed {} {}", this, handle, _parser); @@ -724,8 +721,8 @@ private class SendCallback extends IteratingCallback private ByteBuffer _content; private boolean _lastContent; private Callback _callback; - private ByteBuffer _header; - private ByteBuffer _chunk; + private RetainableByteBuffer _header; + private RetainableByteBuffer _chunk; private boolean _shutdownOut; private SendCallback() @@ -780,12 +777,14 @@ public Action process() throws Exception boolean useDirectByteBuffers = isUseOutputDirectByteBuffers(); while (true) { - HttpGenerator.Result result = _generator.generateResponse(_info, _head, _header, _chunk, _content, _lastContent); + ByteBuffer headerByteBuffer = _header == null ? null : _header.getByteBuffer(); + ByteBuffer chunkByteBuffer = _chunk == null ? null : _chunk.getByteBuffer(); + HttpGenerator.Result result = _generator.generateResponse(_info, _head, headerByteBuffer, chunkByteBuffer, _content, _lastContent); if (LOG.isDebugEnabled()) LOG.debug("generate: {} for {} ({},{},{})@{}", result, this, - BufferUtil.toSummaryString(_header), + BufferUtil.toSummaryString(headerByteBuffer), BufferUtil.toSummaryString(_content), _lastContent, _generator.getState()); @@ -797,7 +796,7 @@ public Action process() throws Exception case NEED_HEADER: { - _header = _bufferPool.acquire(Math.min(_configuration.getResponseHeaderSize(), _configuration.getOutputBufferSize()), useDirectByteBuffers); + _header = _retainableByteBufferPool.acquire(Math.min(_configuration.getResponseHeaderSize(), _configuration.getOutputBufferSize()), useDirectByteBuffers); continue; } case HEADER_OVERFLOW: @@ -805,18 +804,18 @@ public Action process() throws Exception if (_header.capacity() >= _configuration.getResponseHeaderSize()) throw new BadMessageException(INTERNAL_SERVER_ERROR_500, "Response header too large"); releaseHeader(); - _header = _bufferPool.acquire(_configuration.getResponseHeaderSize(), useDirectByteBuffers); + _header = _retainableByteBufferPool.acquire(_configuration.getResponseHeaderSize(), useDirectByteBuffers); continue; } case NEED_CHUNK: { - _chunk = _bufferPool.acquire(HttpGenerator.CHUNK_SIZE, useDirectByteBuffers); + _chunk = _retainableByteBufferPool.acquire(HttpGenerator.CHUNK_SIZE, useDirectByteBuffers); continue; } case NEED_CHUNK_TRAILER: { releaseChunk(); - _chunk = _bufferPool.acquire(_configuration.getResponseHeaderSize(), useDirectByteBuffers); + _chunk = _retainableByteBufferPool.acquire(_configuration.getResponseHeaderSize(), useDirectByteBuffers); continue; } case FLUSH: @@ -824,18 +823,19 @@ public Action process() throws Exception // Don't write the chunk or the content if this is a HEAD response, or any other type of response that should have no content if (_head || _generator.isNoContent()) { - BufferUtil.clear(_chunk); + if (_chunk != null) + _chunk.clear(); BufferUtil.clear(_content); } byte gatherWrite = 0; long bytes = 0; - if (BufferUtil.hasContent(_header)) + if (BufferUtil.hasContent(headerByteBuffer)) { gatherWrite += 4; bytes += _header.remaining(); } - if (BufferUtil.hasContent(_chunk)) + if (BufferUtil.hasContent(chunkByteBuffer)) { gatherWrite += 2; bytes += _chunk.remaining(); @@ -849,22 +849,22 @@ public Action process() throws Exception switch (gatherWrite) { case 7: - getEndPoint().write(this, _header, _chunk, _content); + getEndPoint().write(this, headerByteBuffer, chunkByteBuffer, _content); break; case 6: - getEndPoint().write(this, _header, _chunk); + getEndPoint().write(this, headerByteBuffer, chunkByteBuffer); break; case 5: - getEndPoint().write(this, _header, _content); + getEndPoint().write(this, headerByteBuffer, _content); break; case 4: - getEndPoint().write(this, _header); + getEndPoint().write(this, headerByteBuffer); break; case 3: - getEndPoint().write(this, _chunk, _content); + getEndPoint().write(this, chunkByteBuffer, _content); break; case 2: - getEndPoint().write(this, _chunk); + getEndPoint().write(this, chunkByteBuffer); break; case 1: getEndPoint().write(this, _content); @@ -916,14 +916,14 @@ private Callback release() private void releaseHeader() { if (_header != null) - _bufferPool.release(_header); + _header.release(); _header = null; } private void releaseChunk() { if (_chunk != null) - _bufferPool.release(_chunk); + _chunk.release(); _chunk = null; } diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java index 50b446701513..2d8577e66992 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java @@ -24,7 +24,7 @@ import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.http.HttpVersion; -import org.eclipse.jetty.io.ArrayByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.internal.HttpChannelState; import org.eclipse.jetty.util.Callback; @@ -46,7 +46,7 @@ public abstract class AbstractHttpTest public void setUp() throws Exception { server = new Server(); - connector = new ServerConnector(server, null, null, new ArrayByteBufferPool(64, 2048, 64 * 1024), 1, 1, new HttpConnectionFactory()); + connector = new ServerConnector(server, null, null, new ArrayRetainableByteBufferPool(64, 2048, 64 * 1024), 1, 1, new HttpConnectionFactory()); connector.setIdleTimeout(100000); server.addConnector(connector); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java index 379aca089f72..e9111b0032bc 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java @@ -29,6 +29,7 @@ import org.eclipse.jetty.io.ByteBufferAccumulator; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.server.handler.EchoHandler; import org.eclipse.jetty.util.Blocker; @@ -651,7 +652,8 @@ public void run() if (chunk.isLast()) { // write accumulated buffers - response.write(true, bufferAccumulator.toByteBuffer(), callback); + RetainableByteBuffer buffer = bufferAccumulator.toRetainableByteBuffer(); + response.write(true, buffer.getByteBuffer(), Callback.from(buffer::release, callback)); return; } } diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/DetectorConnectionTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/DetectorConnectionTest.java index 28fe12d78ed8..e333b908a6da 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/DetectorConnectionTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/DetectorConnectionTest.java @@ -375,10 +375,6 @@ protected void nextProtocol(Connector connector, EndPoint endPoint, ByteBuffer b { if (!detectionSuccessful.compareAndSet(true, false)) throw new AssertionError("DetectionUnsuccessful callback should only have been called once"); - - // omitting this will leak the buffer - connector.getByteBufferPool().release(buffer); - Callback.Completable.with(c -> endPoint.write(c, ByteBuffer.wrap("No upgrade for you".getBytes(StandardCharsets.US_ASCII)))) .whenComplete((r, x) -> endPoint.close()); } diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java index 36ea166e22aa..706b07077ca9 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java @@ -1766,7 +1766,7 @@ public void onClosed(Connection connection) long total = contents.stream().mapToLong(Content.Chunk::remaining).sum(); assertThat(total, equalTo(chunk.length * 4L)); - RetainableByteBufferPool rbbp = _connector.getBean(RetainableByteBufferPool.class); + RetainableByteBufferPool rbbp = _connector.getRetainableByteBufferPool(); if (rbbp instanceof ArrayRetainableByteBufferPool pool) { long buffersBeforeRelease = pool.getAvailableDirectByteBufferCount() + pool.getAvailableHeapByteBufferCount(); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockConnector.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockConnector.java index fa65ad26171c..82134dd418d1 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockConnector.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockConnector.java @@ -15,17 +15,13 @@ import java.io.IOException; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.util.thread.Scheduler; +import org.eclipse.jetty.io.RetainableByteBufferPool; public class MockConnector extends AbstractConnector { - private final Server _server; - public MockConnector(Server server) { - super(server, server.getThreadPool(), server.getBean(Scheduler.class), ByteBufferPool.NOOP, 0); - _server = server; + super(server, server.getThreadPool(), server.getScheduler(), new RetainableByteBufferPool.NonPooling(), 0); } @Override @@ -33,21 +29,6 @@ protected void accept(int acceptorID) throws IOException, InterruptedException { } - @Override - public Scheduler getScheduler() - { - return _server.getBean(Scheduler.class); - } - - @Override - public ByteBufferPool getByteBufferPool() - { - ByteBufferPool pool = _server.getBean(ByteBufferPool.class); - if (pool != null) - return pool; - return super.getByteBufferPool(); - } - @Override public Object getTransport() { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockHttpStream.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockHttpStream.java index a2ccaf1f0042..526d0129ae14 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockHttpStream.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/MockHttpStream.java @@ -206,7 +206,7 @@ public void send(MetaData.Request request, MetaData.Response response, boolean l _responseTrailers = HttpFields.build(trailers); } - if (!_out.compareAndSet(null, _accumulator.takeByteBuffer())) + if (!_out.compareAndSet(null, _accumulator.takeRetainableByteBuffer().getByteBuffer())) { if (response != null || content != null) { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ThreadStarvationTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ThreadStarvationTest.java index d06ecce4fd33..faf34e7da805 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ThreadStarvationTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ThreadStarvationTest.java @@ -31,9 +31,8 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IO; @@ -84,7 +83,9 @@ public static Stream scenarios() SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); sslContextFactory.setKeyStorePath(keystorePath.toString()); sslContextFactory.setKeyStorePassword("storepwd"); - ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + // TODO: restore leak tracking. +// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + RetainableByteBufferPool pool = new ArrayRetainableByteBufferPool(); HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(); ServerConnector connector = new ServerConnector(server, null, null, pool, acceptors, selectors, diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/BufferedResponseHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/BufferedResponseHandlerTest.java index 57ca9e0ece3b..5b2798a83669 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/BufferedResponseHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/BufferedResponseHandlerTest.java @@ -18,7 +18,6 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -45,7 +44,6 @@ public class BufferedResponseHandlerTest public void setUp() throws Exception { _server = new Server(); - _server.addBean(new NoopByteBufferPool()); // Avoid giving larger buffers than requested HttpConfiguration config = new HttpConfiguration(); config.setOutputBufferSize(1024); config.setOutputAggregationSize(256); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java index 141656a11298..85ba710e331a 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/DebugHandlerTest.java @@ -26,9 +26,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.AbstractConnectionFactory; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; @@ -71,7 +70,9 @@ public void startServer() throws Exception SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); sslContextFactory.setKeyStorePath(keystorePath.getAbsolutePath()); sslContextFactory.setKeyStorePassword("storepwd"); - ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + // TODO: restore leak tracking. +// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + RetainableByteBufferPool pool = new ArrayRetainableByteBufferPool(); ServerConnector sslConnector = new ServerConnector(server, null, null, pool, 1, 1, AbstractConnectionFactory.getFactories(sslContextFactory, new HttpConnectionFactory())); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java index 13807825810b..6c57427d4031 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java @@ -670,7 +670,7 @@ protected HttpContent.Factory newHttpContentFactory() contentFactory = new FileMappingHttpContentFactory(contentFactory); contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, getPrecompressedFormats()); - contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, 0, getByteBufferPool()); + contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, 0, getRetainableByteBufferPool()); return contentFactory; } }; diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLEngineTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLEngineTest.java index 757bd6760e3e..9690f293b681 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLEngineTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLEngineTest.java @@ -203,7 +203,7 @@ public void testInvalidLargeTLSFrame() throws Exception @Override protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) + return new SslConnection(connector.getRetainableByteBufferPool(), connector.getExecutor(), endPoint, engine, isDirectBuffersForEncryption(), isDirectBuffersForDecryption()) { @Override protected SSLEngineResult unwrap(SSLEngine sslEngine, ByteBuffer input, ByteBuffer output) throws SSLException diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/ServerConnectorSslServerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/ServerConnectorSslServerTest.java index cb6096b50a8e..aff49dfb563d 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/ServerConnectorSslServerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/ServerConnectorSslServerTest.java @@ -27,10 +27,9 @@ import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManagerFactory; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.AbstractConnectionFactory; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -74,7 +73,9 @@ public void init() throws Exception SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); sslContextFactory.setKeyStorePath(keystorePath); sslContextFactory.setKeyStorePassword("storepwd"); - ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + // TODO: restore leak tracking. +// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()); + RetainableByteBufferPool pool = new ArrayRetainableByteBufferPool(); HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(); ServerConnector connector = new ServerConnector(_server, null, null, pool, 1, 1, AbstractConnectionFactory.getFactories(sslContextFactory, httpConnectionFactory)); diff --git a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java index 6586d8ee3aaf..05261392f20d 100644 --- a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java +++ b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java @@ -289,7 +289,7 @@ public void doStart() throws Exception addBean(_sessionIdManager, false); } - _scheduler = server.getBean(Scheduler.class); + _scheduler = server.getScheduler(); if (_scheduler == null) { _scheduler = new ScheduledExecutorScheduler(String.format("Session-Scheduler-%x", hashCode()), false); diff --git a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/HouseKeeper.java b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/HouseKeeper.java index 2aec6f87dba7..5aedd172c72a 100644 --- a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/HouseKeeper.java +++ b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/HouseKeeper.java @@ -43,12 +43,8 @@ public class HouseKeeper extends AbstractLifeCycle protected boolean _ownScheduler = false; private long _intervalMs = DEFAULT_PERIOD_MS; - /** - * Runner - */ protected class Runner implements Runnable { - @Override public void run() { @@ -104,7 +100,7 @@ protected void startScavenging() throws Exception if (_sessionIdManager instanceof DefaultSessionIdManager) { //try and use a common scheduler, fallback to own - _scheduler = ((DefaultSessionIdManager)_sessionIdManager).getServer().getBean(Scheduler.class); + _scheduler = ((DefaultSessionIdManager)_sessionIdManager).getServer().getScheduler(); } if (_scheduler == null) diff --git a/jetty-core/jetty-session/src/test/java/org/eclipse/jetty/session/HouseKeeperTest.java b/jetty-core/jetty-session/src/test/java/org/eclipse/jetty/session/HouseKeeperTest.java index eddd4fedf5fd..36f2d16f1bd8 100644 --- a/jetty-core/jetty-session/src/test/java/org/eclipse/jetty/session/HouseKeeperTest.java +++ b/jetty-core/jetty-session/src/test/java/org/eclipse/jetty/session/HouseKeeperTest.java @@ -72,11 +72,14 @@ public Set getSessionManagers() @Test public void testHouseKeeper() throws Exception { + Server server = new Server(); + server.start(); + HouseKeeper t = new TestHouseKeeper(); assertThrows(IllegalStateException.class, () -> t.start()); TestHouseKeeper hk = new TestHouseKeeper(); - hk.setSessionIdManager(new TestSessionIdManager(new Server())); + hk.setSessionIdManager(new TestSessionIdManager(server)); hk.setIntervalSec(-1); hk.start(); //no scavenging @@ -103,8 +106,7 @@ public void testHouseKeeper() throws Exception assertNotNull(hk.getRunner()); assertNotNull(hk.getTask()); assertNotNull(hk.getScheduler()); - assertTrue(hk.isOwnScheduler()); - + //stop it hk.stop(); assertNull(hk.getRunner()); @@ -124,6 +126,5 @@ public void testHouseKeeper() throws Exception //provide that functionality. assertNotSame(oldTask, hk.getTask()); assertNotNull(hk.getScheduler()); - assertTrue(hk.isOwnScheduler()); } } diff --git a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/AbstractTest.java b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/AbstractTest.java index 91889fcae2c4..890a6bf86d8d 100644 --- a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/AbstractTest.java +++ b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/AbstractTest.java @@ -37,6 +37,7 @@ import org.eclipse.jetty.http3.server.AbstractHTTP3ServerConnectionFactory; import org.eclipse.jetty.http3.server.HTTP3ServerConnectionFactory; import org.eclipse.jetty.http3.server.HTTP3ServerConnector; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.quic.server.QuicServerConnector; import org.eclipse.jetty.server.AbstractConnector; @@ -138,7 +139,8 @@ protected Server newServer() { QueuedThreadPool serverThreads = new QueuedThreadPool(); serverThreads.setName("server"); - return new Server(serverThreads); + // TODO: restore leak tracking. + return new Server(serverThreads, null, new ArrayRetainableByteBufferPool()); } protected SslContextFactory.Server newSslContextFactoryServer() diff --git a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/ForwardProxyWithDynamicTransportTest.java b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/ForwardProxyWithDynamicTransportTest.java index e1298b228a6e..1f93668ac945 100644 --- a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/ForwardProxyWithDynamicTransportTest.java +++ b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/ForwardProxyWithDynamicTransportTest.java @@ -411,7 +411,7 @@ public void testHTTP2TunnelResetByClient() throws Exception @Override protected DownstreamConnection newDownstreamConnection(EndPoint endPoint, ConcurrentMap context) { - return new DownstreamConnection(endPoint, getExecutor(), getByteBufferPool(), context) + return new DownstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), context) { @Override protected void close(Throwable failure) @@ -425,7 +425,7 @@ protected void close(Throwable failure) @Override protected UpstreamConnection newUpstreamConnection(EndPoint endPoint, ConnectContext connectContext) { - return new UpstreamConnection(endPoint, getExecutor(), getByteBufferPool(), connectContext) + return new UpstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), connectContext) { @Override protected void close(Throwable failure) @@ -494,7 +494,7 @@ public void testHTTP2TunnelProxyStreamTimeout() throws Exception @Override protected DownstreamConnection newDownstreamConnection(EndPoint endPoint, ConcurrentMap context) { - return new DownstreamConnection(endPoint, getExecutor(), getByteBufferPool(), context) + return new DownstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), context) { @Override protected void close(Throwable failure) @@ -508,7 +508,7 @@ protected void close(Throwable failure) @Override protected UpstreamConnection newUpstreamConnection(EndPoint endPoint, ConnectContext connectContext) { - return new UpstreamConnection(endPoint, getExecutor(), getByteBufferPool(), connectContext) + return new UpstreamConnection(endPoint, getExecutor(), getRetainableByteBufferPool(), connectContext) { @Override protected void close(Throwable failure) diff --git a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientDemandTest.java b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientDemandTest.java index 83d3bd6433cb..d03171b4af61 100644 --- a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientDemandTest.java +++ b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientDemandTest.java @@ -33,8 +33,8 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.BufferUtil; @@ -140,7 +140,7 @@ public boolean process(Request request, org.eclipse.jetty.server.Response respon }); startClient(transport); client.stop(); - client.setByteBufferPool(new MappedByteBufferPool(bufferSize)); + client.setRetainableByteBufferPool(new ArrayRetainableByteBufferPool(0, bufferSize, -1)); client.setResponseBufferSize(bufferSize); client.start(); @@ -322,7 +322,7 @@ public boolean process(Request request, org.eclipse.jetty.server.Response respon }); startClient(transport); client.stop(); - client.setByteBufferPool(new MappedByteBufferPool(bufferSize)); + client.setRetainableByteBufferPool(new ArrayRetainableByteBufferPool(0, bufferSize, -1)); client.setResponseBufferSize(bufferSize); client.start(); diff --git a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientLoadTest.java b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientLoadTest.java index 22250afa4a1a..6c079f2014e5 100644 --- a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientLoadTest.java +++ b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientLoadTest.java @@ -34,11 +34,8 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpMethod; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.io.Content; -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.LogarithmicArrayByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.util.BufferUtil; @@ -67,11 +64,11 @@ public class HttpClientLoadTest extends AbstractTest public void testIterative(Transport transport) throws Exception { server = newServer(); - server.addBean(new LeakTrackingByteBufferPool(new LogarithmicArrayByteBufferPool())); start(transport, new LoadHandler()); setStreamIdleTimeout(120000); client.stop(); - client.setByteBufferPool(new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged())); + // TODO: restore leak tracking. + client.setRetainableByteBufferPool(new ArrayRetainableByteBufferPool()); client.setMaxConnectionsPerDestination(32768); client.setMaxRequestsQueuedPerDestination(1024 * 1024); client.setIdleTimeout(120000); @@ -120,7 +117,8 @@ public void testConcurrent(Transport transport) throws Exception start(transport, new LoadHandler()); client.stop(); - client.setByteBufferPool(new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged())); + // TODO: restore leak tracking. + client.setRetainableByteBufferPool(new ArrayRetainableByteBufferPool()); client.setMaxConnectionsPerDestination(32768); client.setMaxRequestsQueuedPerDestination(1024 * 1024); client.start(); @@ -138,24 +136,6 @@ private void assertLeaks() { System.gc(); - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - if (byteBufferPool instanceof LeakTrackingByteBufferPool serverBufferPool) - { - assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), Matchers.is(0L)); - assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), Matchers.is(0L)); - assertThat("Server BufferPool - leaked removes", serverBufferPool.getLeakedRemoves(), Matchers.is(0L)); - assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), Matchers.is(0L)); - } - - byteBufferPool = client.getByteBufferPool(); - if (byteBufferPool instanceof LeakTrackingByteBufferPool clientBufferPool) - { - assertThat("Client BufferPool - leaked acquires", clientBufferPool.getLeakedAcquires(), Matchers.is(0L)); - assertThat("Client BufferPool - leaked releases", clientBufferPool.getLeakedReleases(), Matchers.is(0L)); - assertThat("Client BufferPool - leaked removes", clientBufferPool.getLeakedRemoves(), Matchers.is(0L)); - assertThat("Client BufferPool - unreleased", clientBufferPool.getLeakedResources(), Matchers.is(0L)); - } - assertThat("Connection Leaks", connectionLeaks.get(), Matchers.is(0L)); } diff --git a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientTimeoutTest.java b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientTimeoutTest.java index 5d56a172565c..f8461a52f26c 100644 --- a/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientTimeoutTest.java +++ b/jetty-core/jetty-tests/jetty-test-client-transports/src/test/java/org/eclipse/jetty/test/client/transport/HttpClientTimeoutTest.java @@ -35,10 +35,10 @@ import org.eclipse.jetty.client.Response; import org.eclipse.jetty.client.Result; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.ssl.SslClientConnectionFactory; import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.server.Handler; @@ -238,12 +238,12 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C { if (sslContextFactory == null) sslContextFactory = getSslContextFactory(); - return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory) + return new SslClientConnectionFactory(sslContextFactory, getRetainableByteBufferPool(), getExecutor(), connectionFactory) { @Override - protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) + protected SslConnection newSslConnection(RetainableByteBufferPool bufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) { - return new SslConnection(byteBufferPool, executor, endPoint, engine) + return new SslConnection(bufferPool, executor, endPoint, engine) { @Override protected boolean onReadTimeout(Throwable timeout) diff --git a/jetty-core/jetty-unixdomain-server/src/main/java/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.java b/jetty-core/jetty-unixdomain-server/src/main/java/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.java index dce9a1c80b3e..b29dade57a05 100644 --- a/jetty-core/jetty-unixdomain-server/src/main/java/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.java +++ b/jetty-core/jetty-unixdomain-server/src/main/java/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.java @@ -29,10 +29,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.ManagedSelector; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.SelectorManager; import org.eclipse.jetty.io.SocketChannelEndPoint; import org.eclipse.jetty.server.AbstractConnector; @@ -78,9 +78,9 @@ public UnixDomainServerConnector(Server server, int acceptors, int selectors, Co this(server, null, null, null, acceptors, selectors, factories); } - public UnixDomainServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, int selectors, ConnectionFactory... factories) + public UnixDomainServerConnector(Server server, Executor executor, Scheduler scheduler, RetainableByteBufferPool bufferPool, int acceptors, int selectors, ConnectionFactory... factories) { - super(server, executor, scheduler, pool, acceptors, factories.length > 0 ? factories : new ConnectionFactory[]{new HttpConnectionFactory()}); + super(server, executor, scheduler, bufferPool, acceptors, factories.length > 0 ? factories : new ConnectionFactory[]{new HttpConnectionFactory()}); selectorManager = newSelectorManager(getExecutor(), getScheduler(), selectors); addBean(selectorManager, true); } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/CoreClientUpgradeRequest.java b/jetty-core/jetty-websocket/jetty-websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/CoreClientUpgradeRequest.java index cc681f3878a5..b91222f4f784 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/CoreClientUpgradeRequest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/CoreClientUpgradeRequest.java @@ -37,7 +37,6 @@ import org.eclipse.jetty.http.HttpScheme; 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.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; @@ -478,9 +477,8 @@ else if (values.length == 1) customizer.customize(coreSession); HttpClient httpClient = wsClient.getHttpClient(); - ByteBufferPool bufferPool = wsClient.getWebSocketComponents().getBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = bufferPool.asRetainableByteBufferPool(); - WebSocketConnection wsConnection = new WebSocketConnection(endPoint, httpClient.getExecutor(), httpClient.getScheduler(), bufferPool, retainableByteBufferPool, coreSession); + RetainableByteBufferPool bufferPool = wsClient.getWebSocketComponents().getRetainableByteBufferPool(); + WebSocketConnection wsConnection = new WebSocketConnection(endPoint, httpClient.getExecutor(), httpClient.getScheduler(), bufferPool, coreSession); wsClient.getEventListeners().forEach(wsConnection::addEventListener); coreSession.setWebSocketConnection(wsConnection); Throwable listenerError = notifyUpgradeListeners((listener) -> listener.onHandshakeResponse(request, response)); diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java index 7baee9ddfcf7..c9697965d12f 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.websocket.core; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; @@ -27,7 +27,7 @@ public class AbstractExtension implements Extension { private final Logger log; private CoreSession coreSession; - private ByteBufferPool bufferPool; + private RetainableByteBufferPool bufferPool; private ExtensionConfig config; private OutgoingFrames nextOutgoing; private IncomingFrames nextIncoming; @@ -43,7 +43,7 @@ public AbstractExtension() public void init(ExtensionConfig config, WebSocketComponents components) { this.config = config; - this.bufferPool = components.getBufferPool(); + this.bufferPool = components.getRetainableByteBufferPool(); this.deflaterPool = components.getDeflaterPool(); this.inflaterPool = components.getInflaterPool(); } @@ -60,7 +60,7 @@ public void sendFrame(Frame frame, Callback callback, boolean batch) nextOutgoingFrame(frame, callback, batch); } - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return bufferPool; } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/CoreSession.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/CoreSession.java index 0fb82bef2eab..37f72f2dedc6 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/CoreSession.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/CoreSession.java @@ -19,7 +19,7 @@ import java.util.List; import java.util.Map; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; /** @@ -82,9 +82,9 @@ public interface CoreSession extends OutgoingFrames, Configuration WebSocketComponents getWebSocketComponents(); /** - * @return The shared ByteBufferPool + * @return The shared RetainableByteBufferPool */ - ByteBufferPool getByteBufferPool(); + RetainableByteBufferPool getRetainableByteBufferPool(); /** * The Local Socket Address for the connection @@ -241,7 +241,7 @@ public WebSocketComponents getWebSocketComponents() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return null; } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketComponents.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketComponents.java index 7d609e0a9627..d32f59a7d6b0 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketComponents.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketComponents.java @@ -16,8 +16,8 @@ import java.util.concurrent.Executor; import java.util.zip.Deflater; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.compression.CompressionPool; @@ -27,14 +27,14 @@ /** * A collection of components which are the resources needed for websockets such as - * {@link ByteBufferPool}, {@link WebSocketExtensionRegistry}, and {@link DecoratedObjectFactory}. + * {@link RetainableByteBufferPool}, {@link WebSocketExtensionRegistry}, and {@link DecoratedObjectFactory}. */ public class WebSocketComponents extends ContainerLifeCycle { private final DecoratedObjectFactory _objectFactory; private final WebSocketExtensionRegistry _extensionRegistry; private final Executor _executor; - private final ByteBufferPool _bufferPool; + private final RetainableByteBufferPool _bufferPool; private final InflaterPool _inflaterPool; private final DeflaterPool _deflaterPool; @@ -44,17 +44,17 @@ public WebSocketComponents() } public WebSocketComponents(WebSocketExtensionRegistry extensionRegistry, DecoratedObjectFactory objectFactory, - ByteBufferPool bufferPool, InflaterPool inflaterPool, DeflaterPool deflaterPool) + RetainableByteBufferPool bufferPool, InflaterPool inflaterPool, DeflaterPool deflaterPool) { this (extensionRegistry, objectFactory, bufferPool, inflaterPool, deflaterPool, null); } public WebSocketComponents(WebSocketExtensionRegistry extensionRegistry, DecoratedObjectFactory objectFactory, - ByteBufferPool bufferPool, InflaterPool inflaterPool, DeflaterPool deflaterPool, Executor executor) + RetainableByteBufferPool bufferPool, InflaterPool inflaterPool, DeflaterPool deflaterPool, Executor executor) { _extensionRegistry = (extensionRegistry == null) ? new WebSocketExtensionRegistry() : extensionRegistry; _objectFactory = (objectFactory == null) ? new DecoratedObjectFactory() : objectFactory; - _bufferPool = (bufferPool == null) ? new MappedByteBufferPool() : bufferPool; + _bufferPool = (bufferPool == null) ? new ArrayRetainableByteBufferPool() : bufferPool; _inflaterPool = (inflaterPool == null) ? new InflaterPool(CompressionPool.DEFAULT_CAPACITY, true) : inflaterPool; _deflaterPool = (deflaterPool == null) ? new DeflaterPool(CompressionPool.DEFAULT_CAPACITY, Deflater.DEFAULT_COMPRESSION, true) : deflaterPool; @@ -77,7 +77,7 @@ public WebSocketComponents(WebSocketExtensionRegistry extensionRegistry, Decorat addBean(_executor); } - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return _bufferPool; } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameCaptureExtension.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameCaptureExtension.java index 72104c288ef2..f91ec562258f 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameCaptureExtension.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameCaptureExtension.java @@ -22,6 +22,7 @@ import java.util.Calendar; import java.util.concurrent.atomic.AtomicInteger; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.StringUtil; @@ -105,14 +106,14 @@ private void saveFrame(Frame frame, boolean outgoing) return; } - ByteBuffer buf = getBufferPool().acquire(BUFSIZE, false); - + RetainableByteBuffer buffer = getRetainableByteBufferPool().acquire(BUFSIZE, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); try { Frame f = Frame.copy(frame); f.setMask(null); // TODO is this needed? - generator.generateHeader(f, buf); - channel.write(buf); + generator.generateHeader(f, byteBuffer); + channel.write(byteBuffer); if (frame.hasPayload()) { channel.write(frame.getPayload().slice()); @@ -127,7 +128,7 @@ private void saveFrame(Frame frame, boolean outgoing) } finally { - getBufferPool().release(buf); + buffer.release(); } } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java index 0f2199375c4e..4c63f0853af3 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java @@ -24,8 +24,9 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.LongAdder; -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.eclipse.jetty.util.IteratingCallback; @@ -50,7 +51,7 @@ public class FrameFlusher extends IteratingCallback private final AutoLock lock = new AutoLock(); private final LongAdder messagesOut = new LongAdder(); private final LongAdder bytesOut = new LongAdder(); - private final ByteBufferPool bufferPool; + private final RetainableByteBufferPool bufferPool; private final EndPoint endPoint; private final int bufferSize; private final Generator generator; @@ -62,15 +63,15 @@ public class FrameFlusher extends IteratingCallback private final List previousEntries; private final List failedEntries; - private List releasableBuffers = new ArrayList<>(); - private ByteBuffer batchBuffer; + private List releasableBuffers = new ArrayList<>(); + private RetainableByteBuffer batchBuffer; private boolean canEnqueue = true; private boolean flushed = true; private Throwable closedCause; private long idleTimeout; private boolean useDirectByteBuffers; - public FrameFlusher(ByteBufferPool bufferPool, Scheduler scheduler, Generator generator, EndPoint endPoint, int bufferSize, int maxGather) + public FrameFlusher(RetainableByteBufferPool bufferPool, Scheduler scheduler, Generator generator, EndPoint endPoint, int bufferSize, int maxGather) { this.bufferPool = bufferPool; this.endPoint = endPoint; @@ -209,7 +210,7 @@ protected Action process() throws Throwable entries.clear(); if (flushed && batchBuffer != null) - BufferUtil.clear(batchBuffer); + batchBuffer.clear(); while (!queue.isEmpty() && entries.size() <= maxGather) { @@ -223,7 +224,7 @@ protected Action process() throws Throwable messagesOut.increment(); - int batchSpace = batchBuffer == null ? bufferSize : BufferUtil.space(batchBuffer); + int batchSpace = batchBuffer == null ? bufferSize : BufferUtil.space(batchBuffer.getByteBuffer()); boolean batch = entry.batch && !entry.frame.isControlFrame() && @@ -236,26 +237,26 @@ protected Action process() throws Throwable if (batchBuffer == null) { batchBuffer = acquireBuffer(bufferSize); - buffers.add(batchBuffer); + buffers.add(batchBuffer.getByteBuffer()); } // Generate the frame into the batchBuffer. - generator.generateWholeFrame(entry.frame, batchBuffer); + generator.generateWholeFrame(entry.frame, batchBuffer.getByteBuffer()); } else { if (batchBuffer != null && batchSpace >= Generator.MAX_HEADER_LENGTH) { // Use the batch space for our header. - generator.generateHeader(entry.frame, batchBuffer); + generator.generateHeader(entry.frame, batchBuffer.getByteBuffer()); } else { // Add headers to the list of buffers. - ByteBuffer headerBuffer = acquireBuffer(Generator.MAX_HEADER_LENGTH); + RetainableByteBuffer headerBuffer = acquireBuffer(Generator.MAX_HEADER_LENGTH); releasableBuffers.add(headerBuffer); - generator.generateHeader(entry.frame, headerBuffer); - buffers.add(headerBuffer); + generator.generateHeader(entry.frame, headerBuffer.getByteBuffer()); + buffers.add(headerBuffer.getByteBuffer()); } // Add the payload to the list of buffers. @@ -264,11 +265,11 @@ protected Action process() throws Throwable { if (entry.frame.isMasked()) { - payload = acquireBuffer(entry.frame.getPayloadLength()); - releasableBuffers.add(payload); + RetainableByteBuffer masked = acquireBuffer(entry.frame.getPayloadLength()); + payload = masked.getByteBuffer(); + releasableBuffers.add(masked); generator.generatePayload(entry.frame, payload); } - buffers.add(payload.slice()); } flush = true; @@ -280,15 +281,9 @@ protected Action process() throws Throwable // If we are going to flush we should release any buffers we have allocated after the callback completes. if (flush) { - final List callbackBuffers = releasableBuffers; + List callbackBuffers = releasableBuffers; releasableBuffers = new ArrayList<>(); - releasingCallback = Callback.from(releasingCallback, () -> - { - for (ByteBuffer buffer : callbackBuffers) - { - bufferPool.release(buffer); - } - }); + releasingCallback = Callback.from(releasingCallback, () -> callbackBuffers.forEach(RetainableByteBuffer::release)); } } @@ -297,7 +292,7 @@ protected Action process() throws Throwable this, entries.size(), flush, - BufferUtil.toDetailString(batchBuffer), + batchBuffer, entries); // succeed previous entries @@ -339,7 +334,7 @@ protected Action process() throws Throwable return Action.SCHEDULED; } - private ByteBuffer acquireBuffer(int capacity) + private RetainableByteBuffer acquireBuffer(int capacity) { return bufferPool.acquire(capacity, isUseDirectByteBuffers()); } @@ -404,7 +399,8 @@ public void timeoutExpired() @Override public void onCompleteFailure(Throwable failure) { - BufferUtil.clear(batchBuffer); + if (batchBuffer != null) + batchBuffer.clear(); releaseAggregate(); try (AutoLock l = lock.lock()) { @@ -414,10 +410,7 @@ public void onCompleteFailure(Throwable failure) failedEntries.addAll(entries); entries.clear(); - for (ByteBuffer buffer : releasableBuffers) - { - bufferPool.release(buffer); - } + releasableBuffers.forEach(RetainableByteBuffer::release); releasableBuffers.clear(); if (closedCause == null) @@ -437,9 +430,9 @@ else if (closedCause != failure) private void releaseAggregate() { - if (BufferUtil.isEmpty(batchBuffer)) + if (batchBuffer != null && !batchBuffer.hasRemaining()) { - bufferPool.release(batchBuffer); + batchBuffer.release(); batchBuffer = null; } } @@ -502,12 +495,11 @@ public String toString() return String.format("%s[queueSize=%d,aggregate=%s]", super.toString(), getQueueSize(), - BufferUtil.toDetailString(batchBuffer)); + batchBuffer); } - private class Entry extends FrameEntry + private static class Entry extends FrameEntry { - private ByteBuffer headerBuffer; private final long timeOfCreation = System.currentTimeMillis(); private Entry(Frame frame, Callback callback, boolean batch) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/Parser.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/Parser.java index ad9c64543218..a1654ff7c84e 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/Parser.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/Parser.java @@ -16,9 +16,10 @@ import java.io.Closeable; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.TypeUtil; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.Frame; @@ -46,7 +47,8 @@ private enum State } private static final Logger LOG = LoggerFactory.getLogger(Parser.class); - private final ByteBufferPool bufferPool; + + private final RetainableByteBufferPool bufferPool; private final Configuration configuration; // State specific @@ -55,14 +57,14 @@ private enum State private int cursor; private byte[] mask; private int payloadLength; - private ByteBuffer aggregate; + private RetainableByteBuffer aggregate; - public Parser(ByteBufferPool bufferPool) + public Parser(RetainableByteBufferPool bufferPool) { this(bufferPool, new Configuration.ConfigurationCustomizer()); } - public Parser(ByteBufferPool bufferPool, Configuration configuration) + public Parser(RetainableByteBufferPool bufferPool, Configuration configuration) { this.bufferPool = bufferPool; this.configuration = configuration; @@ -135,7 +137,7 @@ else if (mask != null) else if (payloadLength == 0) { state = State.START; - return newFrame(firstByte, mask, null, false); + return newFrame(firstByte, mask, null, null); } else { @@ -158,7 +160,7 @@ else if (payloadLength == 0) else if (payloadLength == 0) { state = State.START; - return newFrame(firstByte, mask, null, false); + return newFrame(firstByte, mask, null, null); } else { @@ -176,7 +178,7 @@ else if (payloadLength == 0) if (payloadLength == 0) { state = State.START; - return newFrame(firstByte, mask, null, false); + return newFrame(firstByte, mask, null, null); } state = State.PAYLOAD; } @@ -198,7 +200,7 @@ else if (payloadLength == 0) if (payloadLength == 0) { state = State.START; - return newFrame(firstByte, mask, null, false); + return newFrame(firstByte, mask, null, null); } state = State.PAYLOAD; } @@ -263,7 +265,7 @@ protected void checkFrameSize(byte opcode, int payloadLength) throws MessageTooL } } - protected ParsedFrame newFrame(byte firstByte, byte[] mask, ByteBuffer payload, boolean releaseable) + protected ParsedFrame newFrame(byte firstByte, byte[] mask, ByteBuffer payload, Runnable releaser) { // Validate OpCode byte opcode = OpCode.getOpCode(firstByte); @@ -275,7 +277,7 @@ protected ParsedFrame newFrame(byte firstByte, byte[] mask, ByteBuffer payload, if (OpCode.isControlFrame(opcode) && !fin) throw new ProtocolException("Fragmented Control Frame [" + OpCode.name(opcode) + "]"); - return new ParsedFrame(firstByte, mask, payload, releaseable); + return new ParsedFrame(firstByte, mask, payload, releaser); } private ParsedFrame autoFragment(ByteBuffer buffer, int fragmentSize) @@ -297,7 +299,7 @@ private ParsedFrame autoFragment(ByteBuffer buffer, int fragmentSize) content.limit(fragmentSize); buffer.position(buffer.position() + fragmentSize); - final ParsedFrame frame = newFrame((byte)(firstByte & 0x7F), mask, content, false); + final ParsedFrame frame = newFrame((byte)(firstByte & 0x7F), mask, content, null); mask = nextMask; firstByte = (byte)((firstByte & 0x80) | OpCode.CONTINUATION); state = State.FRAGMENT; @@ -331,14 +333,14 @@ private ParsedFrame parsePayload(ByteBuffer buffer) // No space in the buffer, so we have to copy the partial payload aggregate = bufferPool.acquire(payloadLength, false); - BufferUtil.append(aggregate, buffer); + BufferUtil.append(aggregate.getByteBuffer(), buffer); return null; } if (available == payloadLength) { // All the available data is for this frame and completes it - ParsedFrame frame = newFrame(firstByte, mask, buffer.slice(), false); + ParsedFrame frame = newFrame(firstByte, mask, buffer.slice(), null); buffer.position(buffer.limit()); state = State.START; return frame; @@ -349,7 +351,7 @@ private ParsedFrame parsePayload(ByteBuffer buffer) int limit = buffer.limit(); int end = buffer.position() + payloadLength; buffer.limit(end); - final ParsedFrame frame = newFrame(firstByte, mask, buffer.slice(), false); + final ParsedFrame frame = newFrame(firstByte, mask, buffer.slice(), null); buffer.position(end); buffer.limit(limit); state = State.START; @@ -363,26 +365,30 @@ private ParsedFrame parsePayload(ByteBuffer buffer) if (available < expecting) { // not enough data to complete this frame, just copy it - BufferUtil.append(aggregate, buffer); + BufferUtil.append(aggregate.getByteBuffer(), buffer); return null; } if (available == expecting) { // All the available data is for this frame and completes it - BufferUtil.append(aggregate, buffer); + BufferUtil.append(aggregate.getByteBuffer(), buffer); state = State.START; - return newFrame(firstByte, mask, aggregate, true); + // Capture the current aggregate to release it. + RetainableByteBuffer aggregate = this.aggregate; + return newFrame(firstByte, mask, aggregate.getByteBuffer(), aggregate::release); } // The buffer contains data for this frame and subsequent frames // Copy the first part of the buffer to the frame and complete it int limit = buffer.limit(); buffer.limit(buffer.position() + expecting); - BufferUtil.append(aggregate, buffer); + BufferUtil.append(aggregate.getByteBuffer(), buffer); buffer.limit(limit); state = State.START; - return newFrame(firstByte, mask, aggregate, true); + // Capture the current aggregate to release it. + RetainableByteBuffer aggregate = this.aggregate; + return newFrame(firstByte, mask, aggregate.getByteBuffer(), aggregate::release); } } @@ -390,19 +396,19 @@ private ParsedFrame parsePayload(ByteBuffer buffer) public String toString() { return String - .format("Parser@%x[s=%s,c=%d,o=0x%x,m=%s,l=%d]", hashCode(), state, cursor, firstByte, mask == null ? "-" : TypeUtil.toHexString(mask), payloadLength); + .format("Parser@%x[s=%s,c=%d,o=0x%x,m=%s,l=%d]", hashCode(), state, cursor, firstByte, mask == null ? "-" : StringUtil.toHexString(mask), payloadLength); } - public class ParsedFrame extends Frame implements Closeable, CloseStatus.Supplier + public static class ParsedFrame extends Frame implements Closeable, CloseStatus.Supplier { final CloseStatus closeStatus; - final boolean releaseable; + final Runnable releaser; - public ParsedFrame(byte firstByte, byte[] mask, ByteBuffer payload, boolean releaseable) + public ParsedFrame(byte firstByte, byte[] mask, ByteBuffer payload, Runnable releaser) { super(firstByte, mask, payload); demask(); - this.releaseable = releaseable; + this.releaser = releaser; if (getOpCode() == OpCode.CLOSE) { if (hasPayload()) @@ -419,8 +425,8 @@ public ParsedFrame(byte firstByte, byte[] mask, ByteBuffer payload, boolean rele @Override public void close() { - if (releaseable) - bufferPool.release(getPayload()); + if (releaser != null) + releaser.run(); } @Override @@ -431,7 +437,7 @@ public CloseStatus getCloseStatus() public boolean isReleaseable() { - return releaseable; + return releaser != null; } @Override diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java index beac35ad4596..c175ea4b668e 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java @@ -22,6 +22,7 @@ import java.util.zip.Deflater; import java.util.zip.Inflater; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.compression.DeflaterPool; @@ -294,25 +295,26 @@ protected boolean transform(Callback callback) private boolean deflate(Callback callback) { - // Get a buffer for the inflated payload. + // Get a buffer for the deflated payload. long maxFrameSize = getConfiguration().getMaxFrameSize(); int bufferSize = (maxFrameSize <= 0) ? deflateBufferSize : (int)Math.min(maxFrameSize, deflateBufferSize); - final ByteBuffer buffer = getBufferPool().acquire(bufferSize, false); - callback = Callback.from(callback, () -> getBufferPool().release(buffer)); - BufferUtil.clear(buffer); + RetainableByteBuffer buffer = getRetainableByteBufferPool().acquire(bufferSize, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + callback = Callback.from(callback, buffer::release); + buffer.clear(); // Fill up the buffer with a max length of bufferSize; boolean finished = false; Deflater deflater = getDeflater(); while (true) { - int compressed = deflater.deflate(buffer.array(), buffer.arrayOffset() + buffer.position(), - bufferSize - buffer.position(), Deflater.SYNC_FLUSH); - buffer.limit(buffer.limit() + compressed); + int compressed = deflater.deflate(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), + bufferSize - byteBuffer.position(), Deflater.SYNC_FLUSH); + byteBuffer.limit(byteBuffer.limit() + compressed); if (LOG.isDebugEnabled()) LOG.debug("Compressed {} bytes {}", compressed, toDetail(deflater)); - if (buffer.limit() == bufferSize) + if (byteBuffer.limit() == bufferSize) { // We need to fragment. if (!getConfiguration().isAutoFragment()) @@ -327,7 +329,7 @@ private boolean deflate(Callback callback) } } - ByteBuffer payload = buffer; + ByteBuffer payload = byteBuffer; if (payload.hasRemaining()) { // Handle tail bytes generated by SYNC_FLUSH. @@ -361,7 +363,7 @@ else if (_frame.isFin()) private class IncomingFlusher extends DemandingFlusher { private boolean _tailBytes; - private AtomicReference _payloadRef = new AtomicReference<>(); + private AtomicReference _payloadRef; public IncomingFlusher() { @@ -423,21 +425,22 @@ private boolean inflate(Frame frame, Callback callback, boolean first) throws Da // Get a buffer for the inflated payload. long maxFrameSize = getConfiguration().getMaxFrameSize(); int bufferSize = (maxFrameSize <= 0) ? inflateBufferSize : (int)Math.min(maxFrameSize, inflateBufferSize); - ByteBuffer payload = getBufferPool().acquire(bufferSize, false); + RetainableByteBuffer payload = getRetainableByteBufferPool().acquire(bufferSize, false); _payloadRef = new AtomicReference<>(payload); - BufferUtil.clear(payload); + ByteBuffer byteBuffer = payload.getByteBuffer(); + payload.clear(); // Fill up the ByteBuffer with a max length of bufferSize; Inflater inflater = getInflater(); boolean complete = false; while (true) { - int decompressed = inflater.inflate(payload.array(), payload.arrayOffset() + payload.position(), bufferSize - payload.position()); - payload.limit(payload.limit() + decompressed); + int decompressed = inflater.inflate(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), bufferSize - byteBuffer.position()); + byteBuffer.limit(byteBuffer.limit() + decompressed); if (LOG.isDebugEnabled()) LOG.debug("Decompress: read {} {}", decompressed, toDetail(inflater)); - if (payload.limit() == bufferSize) + if (byteBuffer.limit() == bufferSize) { // We need to fragment. if (!getConfiguration().isAutoFragment()) @@ -461,21 +464,17 @@ private boolean inflate(Frame frame, Callback callback, boolean first) throws Da Frame chunk = new Frame(first ? frame.getOpCode() : OpCode.CONTINUATION); chunk.setRsv1(false); - chunk.setPayload(payload); + chunk.setPayload(byteBuffer); chunk.setFin(frame.isFin() && complete); + // Capture the current AtomicReference. + AtomicReference payloadRef = _payloadRef; boolean succeedCallback = complete; - AtomicReference payloadRef = _payloadRef; - Callback payloadCallback = Callback.from(() -> + Callback payloadCallback = Callback.from(() -> releasePayload(payloadRef), Callback.from(() -> { - getBufferPool().release(payloadRef.getAndSet(null)); if (succeedCallback) callback.succeeded(); - }, t -> - { - getBufferPool().release(payloadRef.getAndSet(null)); - failFlusher(t); - }); + }, this::failFlusher)); emitFrame(chunk, payloadCallback); if (LOG.isDebugEnabled()) @@ -486,8 +485,17 @@ private boolean inflate(Frame frame, Callback callback, boolean first) throws Da @Override protected void onCompleteFailure(Throwable cause) { - getBufferPool().release(_payloadRef.getAndSet(null)); + releasePayload(_payloadRef); super.onCompleteFailure(cause); } + + private void releasePayload(AtomicReference reference) + { + if (reference == null) + return; + RetainableByteBuffer buffer = reference.getAndSet(null); + if (buffer != null) + buffer.release(); + } } } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketConnection.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketConnection.java index 61d328958077..896142046d67 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketConnection.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketConnection.java @@ -24,7 +24,6 @@ import java.util.concurrent.atomic.LongAdder; 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; @@ -54,7 +53,6 @@ public class WebSocketConnection extends AbstractConnection implements Connectio private static final int MIN_BUFFER_SIZE = Generator.MAX_HEADER_LENGTH; private final AutoLock lock = new AutoLock(); - private final ByteBufferPool bufferPool; private final RetainableByteBufferPool retainableByteBufferPool; private final Generator generator; private final Parser parser; @@ -80,11 +78,10 @@ public class WebSocketConnection extends AbstractConnection implements Connectio public WebSocketConnection(EndPoint endp, Executor executor, Scheduler scheduler, - ByteBufferPool bufferPool, RetainableByteBufferPool retainableByteBufferPool, WebSocketCoreSession coreSession) { - this(endp, executor, scheduler, bufferPool, retainableByteBufferPool, coreSession, null); + this(endp, executor, scheduler, retainableByteBufferPool, coreSession, null); } /** @@ -96,7 +93,6 @@ public WebSocketConnection(EndPoint endp, * @param endp The endpoint ever which Websockot is sent/received * @param executor A thread executor to use for WS callbacks. * @param scheduler A scheduler to use for timeouts - * @param bufferPool A pool of buffers to use. * @param retainableByteBufferPool A pool of retainable buffers to use. * @param coreSession The WC core session to which frames are delivered. * @param randomMask A Random used to mask frames. If null then SecureRandom will be created if needed. @@ -104,7 +100,6 @@ public WebSocketConnection(EndPoint endp, public WebSocketConnection(EndPoint endp, Executor executor, Scheduler scheduler, - ByteBufferPool bufferPool, RetainableByteBufferPool retainableByteBufferPool, WebSocketCoreSession coreSession, Random randomMask) @@ -114,14 +109,12 @@ public WebSocketConnection(EndPoint endp, Objects.requireNonNull(endp, "EndPoint"); Objects.requireNonNull(coreSession, "Session"); Objects.requireNonNull(executor, "Executor"); - Objects.requireNonNull(bufferPool, "ByteBufferPool"); Objects.requireNonNull(retainableByteBufferPool, "RetainableByteBufferPool"); - this.bufferPool = bufferPool; this.retainableByteBufferPool = retainableByteBufferPool; this.coreSession = coreSession; this.generator = new Generator(); - this.parser = new Parser(bufferPool, coreSession); + this.parser = new Parser(retainableByteBufferPool, coreSession); this.flusher = new Flusher(scheduler, coreSession.getOutputBufferSize(), generator, endp); this.setInputBufferSize(coreSession.getInputBufferSize()); @@ -136,11 +129,6 @@ public Executor getExecutor() return super.getExecutor(); } - public ByteBufferPool getBufferPool() - { - return bufferPool; - } - public Generator getGenerator() { return generator; @@ -305,7 +293,7 @@ private void reacquireNetworkBuffer() if (networkBuffer == null) throw new IllegalStateException(); - if (networkBuffer.getBuffer().hasRemaining()) + if (networkBuffer.getByteBuffer().hasRemaining()) throw new IllegalStateException(); networkBuffer.release(); @@ -392,7 +380,7 @@ public boolean moreDemand() return true; fillingAndParsing = false; - if (networkBuffer.isEmpty()) + if (!networkBuffer.hasRemaining()) releaseNetworkBuffer(); return false; @@ -437,9 +425,9 @@ private void fillAndParse() while (true) { // Parse and handle frames - while (!networkBuffer.isEmpty()) + while (networkBuffer.hasRemaining()) { - Parser.ParsedFrame frame = parser.parse(networkBuffer.getBuffer()); + Parser.ParsedFrame frame = parser.parse(networkBuffer.getByteBuffer()); if (frame == null) break; @@ -453,7 +441,7 @@ private void fillAndParse() } // buffer must be empty here because parser is fully consuming - assert (networkBuffer.isEmpty()); + assert (!networkBuffer.hasRemaining()); if (!getEndPoint().isOpen()) { @@ -465,7 +453,7 @@ private void fillAndParse() if (networkBuffer.isRetained()) reacquireNetworkBuffer(); - int filled = getEndPoint().fill(networkBuffer.getBuffer()); // TODO check if compact is possible. + int filled = getEndPoint().fill(networkBuffer.getByteBuffer()); // TODO check if compact is possible. if (LOG.isDebugEnabled()) LOG.debug("endpointFill() filled={}: {}", filled, networkBuffer); @@ -494,7 +482,7 @@ private void fillAndParse() if (networkBuffer != null) { - BufferUtil.clear(networkBuffer.getBuffer()); + BufferUtil.clear(networkBuffer.getByteBuffer()); releaseNetworkBuffer(); } coreSession.processConnectionError(t, Callback.NOOP); @@ -516,7 +504,7 @@ protected void setInitialBuffer(ByteBuffer initialBuffer) { networkBuffer = newNetworkBuffer(initialBuffer.remaining()); } - ByteBuffer buffer = networkBuffer.getBuffer(); + ByteBuffer buffer = networkBuffer.getByteBuffer(); BufferUtil.clearToFill(buffer); BufferUtil.put(initialBuffer, buffer); BufferUtil.flipToFlush(buffer, 0); @@ -638,7 +626,7 @@ private class Flusher extends FrameFlusher { private Flusher(Scheduler scheduler, int bufferSize, Generator generator, EndPoint endpoint) { - super(bufferPool, scheduler, generator, endpoint, bufferSize, 8); + super(retainableByteBufferPool, scheduler, generator, endpoint, bufferSize, 8); setUseDirectByteBuffers(isUseOutputDirectByteBuffers()); } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketCoreSession.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketCoreSession.java index 757c4a947687..3a3aa7121e23 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketCoreSession.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketCoreSession.java @@ -26,7 +26,7 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Utf8Appendable; import org.eclipse.jetty.util.component.Dumpable; @@ -240,9 +240,9 @@ private void close(CloseStatus closeStatus, Callback callback) } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return this.connection.getBufferPool(); + return components.getRetainableByteBufferPool(); } public void onEof() diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/ByteBufferMessageSink.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/ByteBufferMessageSink.java index ea8aa44b0d79..a3b945494c0f 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/ByteBufferMessageSink.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/ByteBufferMessageSink.java @@ -19,7 +19,8 @@ import java.util.Objects; import org.eclipse.jetty.io.ByteBufferCallbackAccumulator; -import org.eclipse.jetty.io.ByteBufferPool; +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.eclipse.jetty.websocket.core.CoreSession; @@ -83,17 +84,18 @@ public void accept(Frame frame, Callback callback) callback = Callback.NOOP; if (frame.isFin()) { - ByteBufferPool bufferPool = session.getByteBufferPool(); - ByteBuffer buffer = bufferPool.acquire(out.getLength(), false); - out.writeTo(buffer); + RetainableByteBufferPool bufferPool = session.getRetainableByteBufferPool(); + RetainableByteBuffer buffer = bufferPool.acquire(out.getLength(), false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + out.writeTo(byteBuffer); try { - methodHandle.invoke(buffer); + methodHandle.invoke(byteBuffer); } finally { - bufferPool.release(buffer); + buffer.release(); } } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageOutputStream.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageOutputStream.java index cf6921f4ca20..2d17db34e70c 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageOutputStream.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageOutputStream.java @@ -17,7 +17,8 @@ import java.io.OutputStream; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; +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.eclipse.jetty.util.FutureCallback; @@ -37,19 +38,17 @@ public class MessageOutputStream extends OutputStream private final AutoLock lock = new AutoLock(); private final CoreSession coreSession; - private final ByteBufferPool bufferPool; private final int bufferSize; + private final RetainableByteBuffer buffer; private long frameCount; private long bytesSent; - private ByteBuffer buffer; private Callback callback; private boolean closed; private byte messageOpCode = OpCode.BINARY; - public MessageOutputStream(CoreSession coreSession, ByteBufferPool bufferPool) + public MessageOutputStream(CoreSession coreSession, RetainableByteBufferPool bufferPool) { this.coreSession = coreSession; - this.bufferPool = bufferPool; this.bufferSize = coreSession.getOutputBufferSize(); this.buffer = bufferPool.acquire(bufferSize, true); } @@ -120,14 +119,14 @@ public void flush() throws IOException private void flush(boolean fin) throws IOException { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { if (closed) throw new IOException("Stream is closed"); closed = fin; Frame frame = new Frame(frameCount == 0 ? messageOpCode : OpCode.CONTINUATION); - frame.setPayload(buffer); + frame.setPayload(buffer.getByteBuffer()); frame.setFin(fin); int initialBufferSize = buffer.remaining(); @@ -143,7 +142,7 @@ private void flush(boolean fin) throws IOException try { assert buffer.remaining() == initialBufferSize; - BufferUtil.clear(buffer); + buffer.clear(); } catch (Throwable t) { @@ -154,7 +153,7 @@ private void flush(boolean fin) throws IOException private void send(ByteBuffer data) throws IOException { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { if (closed) throw new IOException("Stream is closed"); @@ -163,7 +162,7 @@ private void send(ByteBuffer data) throws IOException { int bufferRemainingSpace = bufferSize - buffer.remaining(); int copied = Math.min(bufferRemainingSpace, data.remaining()); - BufferUtil.append(buffer, data.array(), data.arrayOffset() + data.position(), copied); + BufferUtil.append(buffer.getByteBuffer(), data.array(), data.arrayOffset() + data.position(), copied); data.position(data.position() + copied); if (data.hasRemaining()) @@ -178,7 +177,7 @@ public void close() throws IOException try { flush(true); - bufferPool.release(buffer); + buffer.release(); if (LOG.isDebugEnabled()) LOG.debug("Stream closed, {} frames ({} bytes) sent", frameCount, bytesSent); // Notify without holding locks. @@ -194,7 +193,7 @@ public void close() throws IOException public void setCallback(Callback callback) { - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { this.callback = callback; } @@ -203,7 +202,7 @@ public void setCallback(Callback callback) private void notifySuccess() { Callback callback; - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { callback = this.callback; } @@ -214,7 +213,7 @@ private void notifySuccess() private void notifyFailure(Throwable failure) { Callback callback; - try (AutoLock l = lock.lock()) + try (AutoLock ignored = lock.lock()) { callback = this.callback; } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageWriter.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageWriter.java index 9bef7d664544..952b28c948b3 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageWriter.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/messages/MessageWriter.java @@ -19,7 +19,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.CodingErrorAction; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.OpCode; @@ -38,7 +38,7 @@ public class MessageWriter extends Writer .onUnmappableCharacter(CodingErrorAction.REPORT) .onMalformedInput(CodingErrorAction.REPORT); - public MessageWriter(CoreSession coreSession, ByteBufferPool bufferPool) + public MessageWriter(CoreSession coreSession, RetainableByteBufferPool bufferPool) { this.outputStream = new MessageOutputStream(coreSession, bufferPool); this.outputStream.setMessageType(OpCode.TEXT); @@ -67,4 +67,4 @@ public void setCallback(Callback callback) { outputStream.setCallback(callback); } -} \ No newline at end of file +} diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketServerComponents.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketServerComponents.java index dc34b96d1292..d3ec78cfd922 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketServerComponents.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketServerComponents.java @@ -15,7 +15,7 @@ import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.DecoratedObjectFactory; @@ -27,7 +27,7 @@ /** * A collection of components which are the resources needed for websockets such as - * {@link ByteBufferPool}, {@link WebSocketExtensionRegistry}, and {@link DecoratedObjectFactory}. + * {@link RetainableByteBufferPool}, {@link WebSocketExtensionRegistry}, and {@link DecoratedObjectFactory}. * * These components should be accessed through {@link WebSocketServerComponents#getWebSocketComponents} so that * the instance can be shared by being stored as a bean on the ContextHandler. @@ -39,7 +39,7 @@ public class WebSocketServerComponents extends WebSocketComponents public static final String WEBSOCKET_DEFLATER_POOL_ATTRIBUTE = "jetty.websocket.deflater"; public static final String WEBSOCKET_BUFFER_POOL_ATTRIBUTE = "jetty.websocket.bufferPool"; - WebSocketServerComponents(InflaterPool inflaterPool, DeflaterPool deflaterPool, ByteBufferPool bufferPool, DecoratedObjectFactory objectFactory, Executor executor) + WebSocketServerComponents(InflaterPool inflaterPool, DeflaterPool deflaterPool, RetainableByteBufferPool bufferPool, DecoratedObjectFactory objectFactory, Executor executor) { super(null, objectFactory, bufferPool, inflaterPool, deflaterPool, executor); } @@ -53,7 +53,7 @@ public class WebSocketServerComponents extends WebSocketComponents *

    *

    * Servlet context attributes can be set with {@link #WEBSOCKET_BUFFER_POOL_ATTRIBUTE}, {@link #WEBSOCKET_INFLATER_POOL_ATTRIBUTE} - * and {@link #WEBSOCKET_DEFLATER_POOL_ATTRIBUTE} to override the {@link ByteBufferPool}, {@link DeflaterPool} or + * and {@link #WEBSOCKET_DEFLATER_POOL_ATTRIBUTE} to override the {@link RetainableByteBufferPool}, {@link DeflaterPool} or * {@link InflaterPool} used by the components, otherwise this will try to use the pools shared on the {@link Server}. *

    * @param server the server. @@ -75,9 +75,9 @@ public static WebSocketComponents ensureWebSocketComponents(Server server, Conte if (deflaterPool == null) deflaterPool = DeflaterPool.ensurePool(server); - ByteBufferPool bufferPool = (ByteBufferPool)context.getAttribute(WEBSOCKET_BUFFER_POOL_ATTRIBUTE); + RetainableByteBufferPool bufferPool = (RetainableByteBufferPool)context.getAttribute(WEBSOCKET_BUFFER_POOL_ATTRIBUTE); if (bufferPool == null) - bufferPool = server.getBean(ByteBufferPool.class); + bufferPool = server.getRetainableByteBufferPool(); Executor executor = (Executor)context.getAttribute("org.eclipse.jetty.server.Executor"); if (executor == null) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java index 70e743c79edd..6c6fac9d3c78 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java @@ -20,7 +20,6 @@ import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.PreEncodedHttpField; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionMetaData; @@ -189,9 +188,9 @@ protected void handle(Runnable runnable) protected abstract WebSocketConnection createWebSocketConnection(Request baseRequest, WebSocketCoreSession coreSession); - protected WebSocketConnection newWebSocketConnection(EndPoint endPoint, Executor executor, Scheduler scheduler, ByteBufferPool byteBufferPool, RetainableByteBufferPool retainableByteBufferPool, WebSocketCoreSession coreSession) + protected WebSocketConnection newWebSocketConnection(EndPoint endPoint, Executor executor, Scheduler scheduler, RetainableByteBufferPool retainableByteBufferPool, WebSocketCoreSession coreSession) { - return new WebSocketConnection(endPoint, executor, scheduler, byteBufferPool, retainableByteBufferPool, coreSession); + return new WebSocketConnection(endPoint, executor, scheduler, retainableByteBufferPool, coreSession); } protected abstract void prepareResponse(Response response, WebSocketNegotiation negotiation); diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java index ee2dd2a546d3..6b6760641299 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java @@ -21,7 +21,6 @@ import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.PreEncodedHttpField; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionMetaData; import org.eclipse.jetty.server.Connector; @@ -80,9 +79,8 @@ protected WebSocketConnection createWebSocketConnection(Request baseRequest, Web { ConnectionMetaData connectionMetaData = baseRequest.getConnectionMetaData(); Connector connector = connectionMetaData.getConnector(); - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = byteBufferPool.asRetainableByteBufferPool(); - return newWebSocketConnection(connectionMetaData.getConnection().getEndPoint(), connector.getExecutor(), connector.getScheduler(), byteBufferPool, retainableByteBufferPool, coreSession); + RetainableByteBufferPool retainableByteBufferPool = connector.getRetainableByteBufferPool(); + return newWebSocketConnection(connectionMetaData.getConnection().getEndPoint(), connector.getExecutor(), connector.getScheduler(), retainableByteBufferPool, coreSession); } @Override diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java index 0722faf9a3f0..b838f05a939d 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java @@ -16,7 +16,6 @@ import org.eclipse.jetty.http.HttpMethod; 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.RetainableByteBufferPool; import org.eclipse.jetty.server.Connector; @@ -60,11 +59,10 @@ protected WebSocketNegotiation newNegotiation(Request request, Response response protected WebSocketConnection createWebSocketConnection(Request request, WebSocketCoreSession coreSession) { Connector connector = request.getConnectionMetaData().getConnector(); - ByteBufferPool byteBufferPool = connector.getByteBufferPool(); - RetainableByteBufferPool retainableByteBufferPool = byteBufferPool.asRetainableByteBufferPool(); + RetainableByteBufferPool retainableByteBufferPool = connector.getRetainableByteBufferPool(); TunnelSupport tunnelSupport = request.getTunnelSupport(); EndPoint endPoint = tunnelSupport.getEndPoint(); - return newWebSocketConnection(endPoint, connector.getExecutor(), connector.getScheduler(), byteBufferPool, retainableByteBufferPool, coreSession); + return newWebSocketConnection(endPoint, connector.getExecutor(), connector.getScheduler(), retainableByteBufferPool, coreSession); } @Override diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/GeneratorParserRoundTripTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/GeneratorParserRoundTripTest.java index 1f792181125a..725551fd793c 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/GeneratorParserRoundTripTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/GeneratorParserRoundTripTest.java @@ -17,8 +17,9 @@ import java.util.Arrays; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.websocket.core.internal.Generator; import org.junit.jupiter.api.Test; @@ -28,7 +29,7 @@ public class GeneratorParserRoundTripTest { - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testParserAndGenerator() throws Exception @@ -38,20 +39,21 @@ public void testParserAndGenerator() throws Exception String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; - ByteBuffer out = bufferPool.acquire(8192, false); + RetainableByteBuffer out = bufferPool.acquire(8192, false); try { // Generate Buffer + ByteBuffer byteBuffer = out.getByteBuffer(); Frame frame = new Frame(OpCode.TEXT).setPayload(message); - gen.generateHeader(frame, out); - gen.generatePayload(frame, out); + gen.generateHeader(frame, byteBuffer); + gen.generatePayload(frame, byteBuffer); // Parse Buffer - capture.parse(out); + capture.parse(byteBuffer); } finally { - bufferPool.release(out); + out.release(); } // Validate @@ -67,9 +69,11 @@ public void testParserAndGeneratorMasked() throws Exception String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; - ByteBuffer out = bufferPool.acquire(8192, false); + RetainableByteBuffer out = bufferPool.acquire(8192, false); try { + ByteBuffer byteBuffer = out.getByteBuffer(); + // Setup Frame Frame frame = new Frame(OpCode.TEXT).setPayload(message); @@ -79,15 +83,15 @@ public void testParserAndGeneratorMasked() throws Exception frame.setMask(mask); // Generate Buffer - gen.generateHeader(frame, out); - gen.generatePayload(frame, out); + gen.generateHeader(frame, byteBuffer); + gen.generatePayload(frame, byteBuffer); // Parse Buffer - capture.parse(out); + capture.parse(byteBuffer); } finally { - bufferPool.release(out); + out.release(); } // Validate diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/MessageHandlerTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/MessageHandlerTest.java index 8149fb2e78ec..04875c98dc06 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/MessageHandlerTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/MessageHandlerTest.java @@ -19,8 +19,8 @@ import java.util.List; import java.util.concurrent.ExecutionException; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; @@ -61,7 +61,7 @@ public void beforeEach() throws Exception coreSession = new CoreSession.Empty() { - private final ByteBufferPool byteBufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Override public void sendFrame(Frame frame, Callback callback, boolean batch) @@ -71,9 +71,9 @@ public void sendFrame(Frame frame, Callback callback, boolean batch) } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return byteBufferPool; + return bufferPool; } }; diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadCloseStatusCodesTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadCloseStatusCodesTest.java index b1cef8d7b7d2..47a0e2d8f70e 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadCloseStatusCodesTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadCloseStatusCodesTest.java @@ -16,8 +16,6 @@ import java.nio.ByteBuffer; import java.util.stream.Stream; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.core.exception.ProtocolException; @@ -60,8 +58,6 @@ public static Stream data() ); } - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); - @ParameterizedTest(name = "closeCode={0} {1}") @MethodSource("data") public void testBadStatusCode(int closeCode, String description) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadOpCodesTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadOpCodesTest.java index 8613e7fef160..5261233f7074 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadOpCodesTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserBadOpCodesTest.java @@ -16,8 +16,6 @@ import java.nio.ByteBuffer; import java.util.stream.Stream; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.core.exception.ProtocolException; @@ -52,8 +50,6 @@ public static Stream data() ); } - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); - @ParameterizedTest(name = "opcode={0} {1}") @MethodSource("data") public void testBadOpCode(byte opcode, String description) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserCapture.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserCapture.java index 7062a12e3081..ac53eedd30f8 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserCapture.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/ParserCapture.java @@ -55,7 +55,7 @@ public ParserCapture(boolean copy, Behavior behavior) this.coreSession = new WebSocketCoreSession(new TestMessageHandler(), behavior, Negotiated.from(exStack), components); coreSession.setAutoFragment(false); coreSession.setMaxFrameSize(0); - this.parser = new Parser(components.getBufferPool(), coreSession); + this.parser = new Parser(components.getRetainableByteBufferPool(), coreSession); } public void parse(ByteBuffer buffer) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketTester.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketTester.java index fd80a25ccfd6..692cec81bd31 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketTester.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketTester.java @@ -24,8 +24,9 @@ import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.websocket.core.internal.Parser; @@ -41,8 +42,8 @@ public class WebSocketTester { private static final String NON_RANDOM_KEY = Base64.getEncoder().encodeToString("0123456701234567".getBytes()); private static SslContextFactory.Client sslContextFactory; - protected ByteBufferPool bufferPool; - protected ByteBuffer buffer; + protected RetainableByteBufferPool bufferPool; + protected RetainableByteBuffer buffer; protected Parser parser; @BeforeAll @@ -62,7 +63,7 @@ public static void stopSslContextFactory() throws Exception @BeforeEach public void before() { - bufferPool = new ArrayByteBufferPool(); + bufferPool = new ArrayRetainableByteBufferPool(); parser = new Parser(bufferPool); } @@ -160,26 +161,28 @@ protected Parser.ParsedFrame receiveFrame(InputStream in) throws IOException while (true) { - Parser.ParsedFrame frame = parser.parse(buffer); - if (!buffer.hasRemaining()) - BufferUtil.clear(buffer); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + Parser.ParsedFrame frame = parser.parse(byteBuffer); + if (!byteBuffer.hasRemaining()) + BufferUtil.clear(byteBuffer); if (frame != null) return frame; - int p = BufferUtil.flipToFill(buffer); - int len = in.read(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); + int p = BufferUtil.flipToFill(byteBuffer); + int len = in.read(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining()); if (len < 0) return null; - buffer.position(buffer.position() + len); - BufferUtil.flipToFlush(buffer, p); + byteBuffer.position(byteBuffer.position() + len); + BufferUtil.flipToFlush(byteBuffer, p); } } protected void receiveEof(InputStream in) throws IOException { - ByteBuffer buffer = bufferPool.acquire(4096, false); - BufferUtil.clearToFill(buffer); - int len = in.read(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); + RetainableByteBuffer buffer = bufferPool.acquire(4096, false); + ByteBuffer byteBuffer = buffer.getByteBuffer(); + BufferUtil.clearToFill(byteBuffer); + int len = in.read(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), buffer.remaining()); if (len < 0) return; diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/AbstractExtensionTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/AbstractExtensionTest.java index 160f56c46a45..07e0b4992d2a 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/AbstractExtensionTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/AbstractExtensionTest.java @@ -26,7 +26,7 @@ public abstract class AbstractExtensionTest @BeforeEach public void init() { - clientExtensions = new ExtensionTool(components.getBufferPool()); - serverExtensions = new ExtensionTool(components.getBufferPool()); + clientExtensions = new ExtensionTool(); + serverExtensions = new ExtensionTool(); } } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java index eb51be10f021..99a53fea54c5 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java @@ -18,8 +18,7 @@ import java.util.List; import java.util.concurrent.ExecutionException; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.toolchain.test.ByteBufferAssert; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.FutureCallback; @@ -63,7 +62,7 @@ private Tester(String parameterizedExtension) assertThat(extensionStack.getExtensions().size(), equalTo(1)); this.capture = new IncomingFramesCapture(); - this.parser = new Parser(new MappedByteBufferPool()); + this.parser = new Parser(new ArrayRetainableByteBufferPool()); } public String getRequestedExtParams() @@ -161,7 +160,7 @@ public void assertHasFrames(Frame... expectedFrames) private final WebSocketComponents components; - public ExtensionTool(ByteBufferPool bufferPool) + public ExtensionTool() { this.components = new WebSocketComponents(); } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/internal/FrameFlusherTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/internal/FrameFlusherTest.java index 6677d875647c..321d6cc78eff 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/internal/FrameFlusherTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/internal/FrameFlusherTest.java @@ -26,8 +26,8 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; @@ -52,7 +52,7 @@ public class FrameFlusherTest { - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); private Scheduler scheduler; @BeforeEach @@ -231,7 +231,7 @@ public static class CapturingEndPoint extends MockEndpoint public Parser parser; public LinkedBlockingQueue incomingFrames = new LinkedBlockingQueue<>(); - public CapturingEndPoint(ByteBufferPool bufferPool) + public CapturingEndPoint(RetainableByteBufferPool bufferPool) { parser = new Parser(bufferPool); } @@ -282,7 +282,7 @@ public void setBlockTime(int time) blockTime = time; } - public BlockingEndpoint(ByteBufferPool bufferPool) + public BlockingEndpoint(RetainableByteBufferPool bufferPool) { super(bufferPool); } diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/util/MessageWriterTest.java b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/util/MessageWriterTest.java index 865199f4cef9..ca25168bef08 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/util/MessageWriterTest.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/util/MessageWriterTest.java @@ -20,8 +20,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.websocket.core.CoreSession; @@ -37,7 +37,7 @@ public class MessageWriterTest { private final CoreSession coreSession = new CoreSession.Empty(); - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testMultipleWrites() throws Exception diff --git a/jetty-ee10/jetty-ee10-maven-plugin/src/main/java/org/eclipse/jetty/ee10/maven/plugin/MavenServerConnector.java b/jetty-ee10/jetty-ee10-maven-plugin/src/main/java/org/eclipse/jetty/ee10/maven/plugin/MavenServerConnector.java index ac35c0577f27..2e24e306c50b 100644 --- a/jetty-ee10/jetty-ee10-maven-plugin/src/main/java/org/eclipse/jetty/ee10/maven/plugin/MavenServerConnector.java +++ b/jetty-ee10/jetty-ee10-maven-plugin/src/main/java/org/eclipse/jetty/ee10/maven/plugin/MavenServerConnector.java @@ -18,8 +18,8 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; @@ -146,9 +146,9 @@ public Scheduler getScheduler() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return checkDelegate().getByteBufferPool(); + return checkDelegate().getRetainableByteBufferPool(); } @Override diff --git a/jetty-ee10/jetty-ee10-proxy/src/main/java/org/eclipse/jetty/ee10/proxy/AsyncMiddleManServlet.java b/jetty-ee10/jetty-ee10-proxy/src/main/java/org/eclipse/jetty/ee10/proxy/AsyncMiddleManServlet.java index b488e85a338b..1adcff3860d2 100644 --- a/jetty-ee10/jetty-ee10-proxy/src/main/java/org/eclipse/jetty/ee10/proxy/AsyncMiddleManServlet.java +++ b/jetty-ee10/jetty-ee10-proxy/src/main/java/org/eclipse/jetty/ee10/proxy/AsyncMiddleManServlet.java @@ -43,8 +43,9 @@ import org.eclipse.jetty.client.Response; import org.eclipse.jetty.client.Result; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.server.handler.ConnectHandler; import org.eclipse.jetty.util.BufferUtil; @@ -774,8 +775,8 @@ public GZIPContentTransformer(HttpClient httpClient, ContentTransformer transfor try { this.transformer = transformer; - ByteBufferPool byteBufferPool = httpClient == null ? null : httpClient.getByteBufferPool(); - this.decoder = new GZIPContentDecoder(byteBufferPool, GZIPContentDecoder.DEFAULT_BUFFER_SIZE); + RetainableByteBufferPool bufferPool = httpClient == null ? null : httpClient.getRetainableByteBufferPool(); + this.decoder = new GZIPContentDecoder(bufferPool, GZIPContentDecoder.DEFAULT_BUFFER_SIZE); this.out = new ByteArrayOutputStream(); this.gzipOut = new GZIPOutputStream(out); } @@ -791,7 +792,7 @@ public void transform(ByteBuffer input, boolean finished, List outpu if (logger.isDebugEnabled()) logger.debug("Ungzipping {} bytes, finished={}", input.remaining(), finished); - List decodeds = Collections.emptyList(); + List decodeds = Collections.emptyList(); if (!input.hasRemaining()) { if (finished) @@ -802,14 +803,14 @@ public void transform(ByteBuffer input, boolean finished, List outpu decodeds = new ArrayList<>(); while (true) { - ByteBuffer decoded = decoder.decode(input); + RetainableByteBuffer decoded = decoder.decode(input); decodeds.add(decoded); boolean decodeComplete = !input.hasRemaining() && !decoded.hasRemaining(); boolean complete = finished && decodeComplete; if (logger.isDebugEnabled()) logger.debug("Ungzipped {} bytes, complete={}", decoded.remaining(), complete); if (decoded.hasRemaining() || complete) - transformer.transform(decoded, complete, buffers); + transformer.transform(decoded.getByteBuffer(), complete, buffers); if (decodeComplete) break; } @@ -822,7 +823,7 @@ public void transform(ByteBuffer input, boolean finished, List outpu output.add(result); } - decodeds.forEach(decoder::release); + decodeds.forEach(RetainableByteBuffer::release); } private ByteBuffer gzip(List buffers, boolean finished) throws IOException diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java index be2941e465f5..be3d25547777 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java @@ -60,8 +60,7 @@ import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.io.ByteBufferInputStream; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Context; import org.eclipse.jetty.server.HttpStream; import org.eclipse.jetty.server.Request; @@ -168,9 +167,9 @@ public void init() throws ServletException long cacheValidationTime = getInitParameter("cacheValidationTime") != null ? Long.parseLong(getInitParameter("cacheValidationTime")) : -2; if (maxCachedFiles != -2 || maxCacheSize != -2 || maxCachedFileSize != -2 || cacheValidationTime != -2) { - ByteBufferPool byteBufferPool = getByteBufferPool(servletContextHandler); + RetainableByteBufferPool bufferPool = getRetainableByteBufferPool(servletContextHandler); ValidatingCachingHttpContentFactory cached = new ValidatingCachingHttpContentFactory(contentFactory, - (cacheValidationTime > -2) ? cacheValidationTime : Duration.ofSeconds(1).toMillis(), byteBufferPool); + (cacheValidationTime > -2) ? cacheValidationTime : Duration.ofSeconds(1).toMillis(), bufferPool); contentFactory = cached; if (maxCacheSize >= 0) cached.setMaxCacheSize(maxCacheSize); @@ -239,15 +238,14 @@ public void init() throws ServletException } } - private static ByteBufferPool getByteBufferPool(ContextHandler contextHandler) + private static RetainableByteBufferPool getRetainableByteBufferPool(ContextHandler contextHandler) { if (contextHandler == null) - return new NoopByteBufferPool(); + return new RetainableByteBufferPool.NonPooling(); Server server = contextHandler.getServer(); if (server == null) - return new NoopByteBufferPool(); - ByteBufferPool byteBufferPool = server.getBean(ByteBufferPool.class); - return (byteBufferPool == null) ? new NoopByteBufferPool() : byteBufferPool; + return new RetainableByteBufferPool.NonPooling(); + return server.getRetainableByteBufferPool(); } private String getInitParameter(String name, String... deprecated) diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ErrorHandler.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ErrorHandler.java index ec345b7d0040..e1fbc4f3e9b8 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ErrorHandler.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ErrorHandler.java @@ -293,7 +293,7 @@ protected void generateAcceptableResponse(ServletContextRequest baseRequest, Htt // TODO error page may cause a BufferOverflow. In which case we try // TODO again with stacks disabled. If it still overflows, it is // TODO written without a body. - ByteBuffer buffer = baseRequest.getResponse().getHttpOutput().getBuffer(); + ByteBuffer buffer = baseRequest.getResponse().getHttpOutput().getByteBuffer(); ByteBufferOutputStream out = new ByteBufferOutputStream(buffer); PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, charset)); diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpOutput.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpOutput.java index 72fafee4ffde..315588b92f3e 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpOutput.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpOutput.java @@ -31,9 +31,10 @@ import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.WriteListener; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EofException; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionMetaData; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.Response; @@ -128,7 +129,7 @@ enum ApiState private final ConnectionMetaData _connectionMetaData; private final ServletChannel _servletChannel; private final Response _response; - private final ByteBufferPool _byteBufferPool; + private final RetainableByteBufferPool _bufferPool; private final ServletRequestState _channelState; private final SharedBlockingCallback _writeBlocker; private ApiState _apiState = ApiState.BLOCKING; @@ -137,7 +138,7 @@ enum ApiState private long _written; private long _flushed; private long _firstByteNanoTime = -1; - private ByteBuffer _aggregate; + private RetainableByteBuffer _aggregate; private int _bufferSize; private int _commitSize; private WriteListener _writeListener; @@ -149,7 +150,7 @@ public HttpOutput(Response response, ServletChannel channel) _response = response; _servletChannel = channel; _connectionMetaData = _response.getRequest().getConnectionMetaData(); - _byteBufferPool = _response.getRequest().getComponents().getByteBufferPool(); + _bufferPool = _response.getRequest().getComponents().getRetainableByteBufferPool(); _channelState = _servletChannel.getState(); _writeBlocker = new WriteBlocker(_servletChannel); @@ -230,7 +231,7 @@ private void onWriteComplete(boolean last, Throwable failure) _state = State.CLOSED; closedCallback = _closedCallback; _closedCallback = null; - releaseBuffer(failure); + releaseBuffer(); wake = updateApiState(failure); } else if (_state == State.CLOSE) @@ -239,7 +240,7 @@ else if (_state == State.CLOSE) // We can now send a (probably empty) last buffer and then when it completes // onWriteComplete will be called again to actually execute the _completeCallback _state = State.CLOSING; - closeContent = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + closeContent = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; } else { @@ -314,10 +315,12 @@ private int maximizeAggregateSpace() if (_aggregate == null) return getBufferSize(); + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); + // compact to maximize space - BufferUtil.compact(_aggregate); + BufferUtil.compact(byteBuffer); - return BufferUtil.space(_aggregate); + return BufferUtil.space(byteBuffer); } public void softClose() @@ -397,7 +400,7 @@ public void complete(Callback callback) // Output is idle blocking state, but we still do an async close _apiState = ApiState.BLOCKED; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case ASYNC: @@ -405,7 +408,7 @@ public void complete(Callback callback) // Output is idle in async state, so we can do an async close _apiState = ApiState.PENDING; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case UNREADY: @@ -451,7 +454,7 @@ public void completed(Throwable failure) try (AutoLock l = _channelState.lock()) { _state = State.CLOSED; - releaseBuffer(failure); + releaseBuffer(); } } @@ -503,7 +506,7 @@ public void close() throws IOException _apiState = ApiState.BLOCKED; _state = State.CLOSING; blocker = _writeBlocker.acquire(); - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case BLOCKED: @@ -521,7 +524,7 @@ public void close() throws IOException // Output is idle in async state, so we can do an async close _apiState = ApiState.PENDING; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case UNREADY: @@ -576,30 +579,27 @@ public void close() throws IOException } } - public ByteBuffer getBuffer() + public ByteBuffer getByteBuffer() { try (AutoLock l = _channelState.lock()) { - return acquireBuffer(); + return acquireBuffer().getByteBuffer(); } } - private ByteBuffer acquireBuffer() + private RetainableByteBuffer acquireBuffer() { boolean useOutputDirectByteBuffers = _connectionMetaData.getHttpConfiguration().isUseOutputDirectByteBuffers(); if (_aggregate == null) - _aggregate = _byteBufferPool.acquire(getBufferSize(), useOutputDirectByteBuffers); + _aggregate = _bufferPool.acquire(getBufferSize(), useOutputDirectByteBuffers); return _aggregate; } - private void releaseBuffer(Throwable failure) + private void releaseBuffer() { if (_aggregate != null) { - if (failure == null) - _byteBufferPool.release(_aggregate); - else - _byteBufferPool.remove(_aggregate); + _aggregate.release(); _aggregate = null; } } @@ -647,7 +647,7 @@ public void flush() throws IOException { case BLOCKING: _apiState = ApiState.BLOCKED; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case ASYNC: @@ -727,7 +727,7 @@ public void write(byte[] b, int off, int len) throws IOException // Write will be aggregated if: // + it is smaller than the commitSize // + is not the last one, or is last but will fit in an already allocated aggregate buffer. - aggregate = len <= _commitSize && (!last || BufferUtil.hasContent(_aggregate) && len <= space); + aggregate = len <= _commitSize && (!last || (_aggregate != null && _aggregate.hasRemaining()) && len <= space); flush = last || !aggregate || len >= space; if (last && _state == State.OPEN) @@ -762,14 +762,14 @@ public void write(byte[] b, int off, int len) throws IOException if (aggregate) { acquireBuffer(); - int filled = BufferUtil.fill(_aggregate, b, off, len); + int filled = BufferUtil.fill(_aggregate.getByteBuffer(), b, off, len); // return if we are not complete, not full and filled all the content if (!flush) { if (LOG.isDebugEnabled()) LOG.debug("write(array) {} aggregated !flush {}", - stateString(), BufferUtil.toDetailString(_aggregate)); + stateString(), _aggregate); return; } @@ -781,7 +781,7 @@ public void write(byte[] b, int off, int len) throws IOException if (LOG.isDebugEnabled()) LOG.debug("write(array) {} last={} agg={} flush=true async={}, len={} {}", - stateString(), last, aggregate, async, len, BufferUtil.toDetailString(_aggregate)); + stateString(), last, aggregate, async, len, _aggregate); if (async) { @@ -795,15 +795,17 @@ public void write(byte[] b, int off, int len) throws IOException { boolean complete = false; // flush any content from the aggregate - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); + complete = last && len == 0; - channelWrite(_aggregate, complete); + channelWrite(byteBuffer, complete); // should we fill aggregate again from the buffer? if (len > 0 && !last && len <= _commitSize && len <= maximizeAggregateSpace()) { - BufferUtil.append(_aggregate, b, off, len); + BufferUtil.append(byteBuffer, b, off, len); onWriteComplete(false, null); return; } @@ -856,7 +858,7 @@ public void write(ByteBuffer buffer) throws IOException checkWritable(); long written = _written + len; last = _servletChannel.getResponse().isAllContentWritten(written); - flush = last || len > 0 || BufferUtil.hasContent(_aggregate); + flush = last || len > 0 || (_aggregate != null && _aggregate.hasRemaining()); if (last && _state == State.OPEN) _state = State.CLOSING; @@ -900,10 +902,10 @@ public void write(ByteBuffer buffer) throws IOException // Blocking write // flush any content from the aggregate boolean complete = false; - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { complete = last && len == 0; - channelWrite(_aggregate, complete); + channelWrite(_aggregate.getByteBuffer(), complete); } // write any remaining content in the buffer directly @@ -965,7 +967,7 @@ public void write(int b) throws IOException _written = written; acquireBuffer(); - BufferUtil.append(_aggregate, (byte)b); + BufferUtil.append(_aggregate.getByteBuffer(), (byte)b); } // Check if all written or full @@ -979,7 +981,7 @@ public void write(int b) throws IOException { try { - channelWrite(_aggregate, last); + channelWrite(_aggregate.getByteBuffer(), last); onWriteComplete(last, null); } catch (Throwable t) @@ -1025,15 +1027,16 @@ private void print(String s, boolean eoln) throws IOException CharBuffer in = CharBuffer.wrap(s); CharBuffer crlf = eoln ? CharBuffer.wrap("\r\n") : null; - ByteBuffer out = _byteBufferPool.acquire((int)(1 + (s.length() + 2) * encoder.averageBytesPerChar()), false); - BufferUtil.flipToFill(out); + RetainableByteBuffer out = _bufferPool.acquire((int)(1 + (s.length() + 2) * encoder.averageBytesPerChar()), false); + ByteBuffer byteBuffer = out.getByteBuffer(); + BufferUtil.flipToFill(byteBuffer); while (true) { CoderResult result; if (in.hasRemaining()) { - result = encoder.encode(in, out, crlf == null); + result = encoder.encode(in, byteBuffer, crlf == null); if (result.isUnderflow()) if (crlf == null) break; @@ -1042,10 +1045,10 @@ private void print(String s, boolean eoln) throws IOException } else if (crlf != null && crlf.hasRemaining()) { - result = encoder.encode(crlf, out, true); + result = encoder.encode(crlf, byteBuffer, true); if (result.isUnderflow()) { - if (!encoder.flush(out).isUnderflow()) + if (!encoder.flush(byteBuffer).isUnderflow()) result.throwException(); break; } @@ -1055,19 +1058,22 @@ else if (crlf != null && crlf.hasRemaining()) if (result.isOverflow()) { - BufferUtil.flipToFlush(out, 0); - ByteBuffer bigger = BufferUtil.ensureCapacity(out, out.capacity() + s.length() + 2); - _byteBufferPool.release(out); - BufferUtil.flipToFill(bigger); + BufferUtil.flipToFlush(byteBuffer, 0); + RetainableByteBuffer bigger = _bufferPool.acquire(out.capacity() + s.length() + 2, out.isDirect()); + BufferUtil.flipToFill(bigger.getByteBuffer()); + bigger.getByteBuffer().put(byteBuffer); + out.release(); + BufferUtil.flipToFill(bigger.getByteBuffer()); out = bigger; + byteBuffer = bigger.getByteBuffer(); continue; } result.throwException(); } - BufferUtil.flipToFlush(out, 0); - write(out.array(), out.arrayOffset(), out.remaining()); - _byteBufferPool.release(out); + BufferUtil.flipToFlush(byteBuffer, 0); + write(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.remaining()); + out.release(); } /** @@ -1182,7 +1188,7 @@ private boolean prepareSendContent(int len, Callback callback) { try (AutoLock l = _channelState.lock()) { - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { callback.failed(new IOException("cannot sendContent() after write()")); return false; @@ -1267,7 +1273,7 @@ public void recycle() _commitSize = config.getOutputAggregationSize(); if (_commitSize > _bufferSize) _commitSize = _bufferSize; - releaseBuffer(null); + releaseBuffer(); _written = 0; _writeListener = null; _onError = null; @@ -1281,8 +1287,8 @@ public void resetBuffer() { try (AutoLock l = _channelState.lock()) { - if (BufferUtil.hasContent(_aggregate)) - BufferUtil.clear(_aggregate); + if (_aggregate != null) + _aggregate.clear(); _written = 0; } } @@ -1484,10 +1490,10 @@ private AsyncFlush(boolean last) @Override protected Action process() throws Exception { - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { _flushed = true; - channelWrite(_aggregate, false, this); + channelWrite(_aggregate.getByteBuffer(), false, this); return Action.SCHEDULED; } @@ -1536,19 +1542,20 @@ private AsyncWrite(ByteBuffer buffer, boolean last) protected Action process() throws Exception { // flush any content from the aggregate - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { _completed = _len == 0; - channelWrite(_aggregate, _last && _completed, this); + channelWrite(_aggregate.getByteBuffer(), _last && _completed, this); return Action.SCHEDULED; } // Can we just aggregate the remainder? if (!_last && _aggregate != null && _len < maximizeAggregateSpace() && _len < _commitSize) { - int position = BufferUtil.flipToFill(_aggregate); - BufferUtil.put(_buffer, _aggregate); - BufferUtil.flipToFlush(_aggregate, position); + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); + int position = BufferUtil.flipToFill(byteBuffer); + BufferUtil.put(_buffer, byteBuffer); + BufferUtil.flipToFlush(byteBuffer, position); return Action.SUCCEEDED; } @@ -1602,7 +1609,7 @@ protected Action process() throws Exception private class InputStreamWritingCB extends NestedChannelWriteCB { private final InputStream _in; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private boolean _eof; private boolean _closed; @@ -1611,7 +1618,7 @@ private InputStreamWritingCB(InputStream in, Callback callback) super(callback, true); _in = in; // Reading from InputStream requires byte[], don't use direct buffers. - _buffer = _byteBufferPool.acquire(getBufferSize(), false); + _buffer = _bufferPool.acquire(getBufferSize(), false); } @Override @@ -1626,18 +1633,20 @@ protected Action process() throws Exception if (!_closed) { _closed = true; - _byteBufferPool.release(_buffer); + _buffer.release(); IO.close(_in); } return Action.SUCCEEDED; } + ByteBuffer byteBuffer = _buffer.getByteBuffer(); + // Read until buffer full or EOF int len = 0; - while (len < _buffer.capacity() && !_eof) + while (len < byteBuffer.capacity() && !_eof) { - int r = _in.read(_buffer.array(), _buffer.arrayOffset() + len, _buffer.capacity() - len); + int r = _in.read(byteBuffer.array(), byteBuffer.arrayOffset() + len, byteBuffer.capacity() - len); if (r < 0) _eof = true; else @@ -1645,24 +1654,17 @@ protected Action process() throws Exception } // write what we have - _buffer.position(0); - _buffer.limit(len); + byteBuffer.position(0); + byteBuffer.limit(len); _written += len; - channelWrite(_buffer, _eof, this); + channelWrite(byteBuffer, _eof, this); return Action.SCHEDULED; } @Override public void onCompleteFailure(Throwable x) { - try - { - _byteBufferPool.release(_buffer); - } - finally - { - super.onCompleteFailure(x); - } + _buffer.release(); } } @@ -1678,7 +1680,7 @@ public void onCompleteFailure(Throwable x) private class ReadableByteChannelWritingCB extends NestedChannelWriteCB { private final ReadableByteChannel _in; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private boolean _eof; private boolean _closed; @@ -1687,7 +1689,7 @@ private ReadableByteChannelWritingCB(ReadableByteChannel in, Callback callback) super(callback, true); _in = in; boolean useOutputDirectByteBuffers = _connectionMetaData.getHttpConfiguration().isUseOutputDirectByteBuffers(); - _buffer = _byteBufferPool.acquire(getBufferSize(), useOutputDirectByteBuffers); + _buffer = _bufferPool.acquire(getBufferSize(), useOutputDirectByteBuffers); } @Override @@ -1702,23 +1704,25 @@ protected Action process() throws Exception if (!_closed) { _closed = true; - _byteBufferPool.release(_buffer); + _buffer.release(); IO.close(_in); } return Action.SUCCEEDED; } + ByteBuffer byteBuffer = _buffer.getByteBuffer(); + // Read from stream until buffer full or EOF - BufferUtil.clearToFill(_buffer); - while (_buffer.hasRemaining() && !_eof) + BufferUtil.clearToFill(byteBuffer); + while (byteBuffer.hasRemaining() && !_eof) { - _eof = (_in.read(_buffer)) < 0; + _eof = (_in.read(byteBuffer)) < 0; } // write what we have - BufferUtil.flipToFlush(_buffer, 0); - _written += _buffer.remaining(); - channelWrite(_buffer, _eof, this); + BufferUtil.flipToFlush(byteBuffer, 0); + _written += byteBuffer.remaining(); + channelWrite(byteBuffer, _eof, this); return Action.SCHEDULED; } @@ -1726,7 +1730,7 @@ protected Action process() throws Exception @Override public void onCompleteFailure(Throwable x) { - _byteBufferPool.release(_buffer); + _buffer.release(); IO.close(_in); super.onCompleteFailure(x); } diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletChannel.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletChannel.java index f35020774af9..b16994fba4f7 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletChannel.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletChannel.java @@ -743,7 +743,7 @@ public void sendResponseAndComplete() try { _state.completing(); - getResponse().write(true, getResponse().getHttpOutput().getBuffer(), Callback.from(() -> _state.completed(null), _state::completed)); + getResponse().write(true, getResponse().getHttpOutput().getByteBuffer(), Callback.from(() -> _state.completed(null), _state::completed)); } catch (Throwable x) { diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketContainer.java index f5e9fcea88d7..cd4ad79dc5b9 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketContainer.java @@ -24,7 +24,7 @@ import jakarta.websocket.Extension; import jakarta.websocket.WebSocketContainer; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; @@ -53,9 +53,9 @@ public JakartaWebSocketContainer(WebSocketComponents components) protected abstract JakartaWebSocketFrameHandlerFactory getFrameHandlerFactory(); - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return components.getBufferPool(); + return components.getRetainableByteBufferPool(); } public WebSocketExtensionRegistry getExtensionRegistry() diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java index 94c9c938bf5f..70ef22ee04fe 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java @@ -49,12 +49,12 @@ protected JakartaWebSocketRemoteEndpoint(JakartaWebSocketSession session, CoreSe protected MessageWriter newMessageWriter() { - return new MessageWriter(coreSession, session.getContainerImpl().getBufferPool()); + return new MessageWriter(coreSession, session.getContainerImpl().getRetainableByteBufferPool()); } protected MessageOutputStream newMessageOutputStream() { - return new MessageOutputStream(coreSession, session.getContainerImpl().getBufferPool()); + return new MessageOutputStream(coreSession, session.getContainerImpl().getRetainableByteBufferPool()); } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/AbstractSessionTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/AbstractSessionTest.java index 565b4d9cd57c..8a68ff66a040 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/AbstractSessionTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/AbstractSessionTest.java @@ -19,7 +19,7 @@ import jakarta.websocket.Endpoint; import jakarta.websocket.EndpointConfig; import jakarta.websocket.Session; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.junit.jupiter.api.AfterAll; @@ -64,9 +64,9 @@ public WebSocketComponents getWebSocketComponents() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return components.getBufferPool(); + return components.getRetainableByteBufferPool(); } public void waitForDemand(long timeout, TimeUnit timeUnit) throws InterruptedException diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/messages/MessageWriterTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/messages/MessageWriterTest.java index 0a1872ef0e8d..d44872ae9882 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/messages/MessageWriterTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/common/messages/MessageWriterTest.java @@ -19,8 +19,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.websocket.core.CoreSession; @@ -34,7 +34,7 @@ public class MessageWriterTest { - private ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testSingleByteArray512b() throws IOException, InterruptedException diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/LocalServer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/LocalServer.java index b2b564569d62..ede2a226289d 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/LocalServer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/LocalServer.java @@ -28,8 +28,8 @@ import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; import org.eclipse.jetty.ee10.websocket.jakarta.server.internal.JakartaWebSocketServerContainer; import org.eclipse.jetty.http.HttpVersion; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -60,7 +60,7 @@ public String echo(String msg) } private static final Logger LOG = LoggerFactory.getLogger(LocalServer.class); - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); private Server server; private ServerConnector connector; private LocalConnector localConnector; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientClassLoaderTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientClassLoaderTest.java index 2cfc3e338176..633d5653948b 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientClassLoaderTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientClassLoaderTest.java @@ -38,7 +38,7 @@ import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -150,7 +150,7 @@ public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exceptio app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); app.copyLib(XmlConfiguration.class, "jetty-xml.jar"); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java index 284ee1d3bc84..9eacc83d0049 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -99,7 +99,7 @@ public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exceptio app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); return app; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee10/websocket/client/WebSocketClient.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee10/websocket/client/WebSocketClient.java index c20c5c59846e..b23fb35da406 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee10/websocket/client/WebSocketClient.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee10/websocket/client/WebSocketClient.java @@ -39,8 +39,8 @@ import org.eclipse.jetty.ee10.websocket.common.JettyWebSocketFrameHandler; import org.eclipse.jetty.ee10.websocket.common.JettyWebSocketFrameHandlerFactory; import org.eclipse.jetty.ee10.websocket.common.SessionTracker; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Graceful; @@ -333,9 +333,9 @@ public void setCookieStore(CookieStore cookieStore) getHttpClient().setCookieStore(cookieStore); } - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return getHttpClient().getByteBufferPool(); + return getHttpClient().getRetainableByteBufferPool(); } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageInputStreamTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageInputStreamTest.java index b8780329aff0..c1659ae05be4 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageInputStreamTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageInputStreamTest.java @@ -28,7 +28,6 @@ import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.internal.messages.MessageInputStream; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -38,14 +37,6 @@ public class MessageInputStreamTest { - public TestableLeakTrackingBufferPool bufferPool = new TestableLeakTrackingBufferPool("Test"); - - @AfterEach - public void afterEach() - { - bufferPool.assertNoLeaks(); - } - @Test public void testBasicAppendRead() throws IOException { diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageOutputStreamTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageOutputStreamTest.java index 41df82486a5a..87b5f6931811 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageOutputStreamTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/MessageOutputStreamTest.java @@ -17,6 +17,8 @@ import java.util.Arrays; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.core.internal.messages.MessageOutputStream; import org.junit.jupiter.api.AfterEach; @@ -34,12 +36,14 @@ public class MessageOutputStreamTest private static final Logger LOG = LoggerFactory.getLogger(MessageOutputStreamTest.class); private static final int OUTPUT_BUFFER_SIZE = 4096; - public TestableLeakTrackingBufferPool bufferPool = new TestableLeakTrackingBufferPool("Test"); + // TODO: restore leak tracking. + public RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @AfterEach public void afterEach() { - bufferPool.assertNoLeaks(); + // TODO: restore leak tracking. +// bufferPool.assertNoLeaks(); } private OutgoingMessageCapture sessionCapture; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/OutgoingMessageCapture.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/OutgoingMessageCapture.java index 4bc43ce1972a..e0ac3175b60c 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/OutgoingMessageCapture.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/OutgoingMessageCapture.java @@ -20,8 +20,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.toolchain.test.Hex; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.CloseStatus; @@ -42,7 +41,7 @@ public class OutgoingMessageCapture extends CoreSession.Empty implements CoreSes public BlockingQueue binaryMessages = new LinkedBlockingDeque<>(); public BlockingQueue events = new LinkedBlockingDeque<>(); - private final ByteBufferPool bufferPool = new NoopByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new RetainableByteBufferPool.NonPooling(); private final MethodHandle wholeTextHandle; private final MethodHandle wholeBinaryHandle; private MessageSink messageSink; @@ -129,7 +128,7 @@ public void sendFrame(Frame frame, Callback callback, boolean batch) } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return bufferPool; } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/TestableLeakTrackingBufferPool.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/TestableLeakTrackingBufferPool.java deleted file mode 100644 index 373dcf8387dc..000000000000 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee10/websocket/common/TestableLeakTrackingBufferPool.java +++ /dev/null @@ -1,43 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.ee10.websocket.common; - -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -public class TestableLeakTrackingBufferPool extends LeakTrackingByteBufferPool -{ - private final String id; - - public TestableLeakTrackingBufferPool(Class clazz) - { - this(clazz.getSimpleName()); - } - - public TestableLeakTrackingBufferPool(String id) - { - super(new MappedByteBufferPool.Tagged()); - this.id = id; - } - - public void assertNoLeaks() - { - assertThat("Leaked Acquires Count for [" + id + "]", getLeakedAcquires(), is(0L)); - assertThat("Leaked Releases Count for [" + id + "]", getLeakedReleases(), is(0L)); - assertThat("Leaked Resource Count for [" + id + "]", getLeakedResources(), is(0L)); - } -} diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/JettyClientClassLoaderTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/JettyClientClassLoaderTest.java index ff9307626065..a6cd6984b92a 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/JettyClientClassLoaderTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/JettyClientClassLoaderTest.java @@ -39,7 +39,7 @@ import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -169,7 +169,7 @@ public WebAppTester.WebApp createWebSocketWebapp(String contextName) throws Exce app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); app.copyLib(XmlConfiguration.class, "jetty-xml.jar"); diff --git a/jetty-ee9/jetty-ee9-maven-plugin/src/main/java/org/eclipse/jetty/ee9/maven/plugin/MavenServerConnector.java b/jetty-ee9/jetty-ee9-maven-plugin/src/main/java/org/eclipse/jetty/ee9/maven/plugin/MavenServerConnector.java index b2b52b7d8359..52e902574dc5 100644 --- a/jetty-ee9/jetty-ee9-maven-plugin/src/main/java/org/eclipse/jetty/ee9/maven/plugin/MavenServerConnector.java +++ b/jetty-ee9/jetty-ee9-maven-plugin/src/main/java/org/eclipse/jetty/ee9/maven/plugin/MavenServerConnector.java @@ -18,8 +18,8 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; @@ -149,9 +149,9 @@ public Scheduler getScheduler() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return checkDelegate().getByteBufferPool(); + return checkDelegate().getRetainableByteBufferPool(); } @Override diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/BufferedResponseHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/BufferedResponseHandler.java index 14a8c8a20848..881480d6bfc0 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/BufferedResponseHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/BufferedResponseHandler.java @@ -266,7 +266,7 @@ public void write(ByteBuffer content, boolean last, Callback callback) // Do we need a new aggregate buffer. if (BufferUtil.space(_aggregate) == 0) { - // TODO: use a buffer pool always allocating with outputBufferSize to avoid polluting the ByteBufferPool. + // TODO: use a buffer pool always allocating with outputBufferSize to avoid polluting the ByteBuffer pool. int size = Math.max(_channel.getHttpConfiguration().getOutputBufferSize(), BufferUtil.length(content)); _aggregate = BufferUtil.allocate(size); _buffers.offer(_aggregate); diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ErrorHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ErrorHandler.java index da03475014b0..cb7a81af27da 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ErrorHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ErrorHandler.java @@ -282,7 +282,7 @@ protected void generateAcceptableResponse(Request baseRequest, HttpServletReques // write into the response aggregate buffer and flush it asynchronously. while (true) { - ByteBuffer buffer = baseRequest.getResponse().getHttpOutput().getBuffer(); + ByteBuffer buffer = baseRequest.getResponse().getHttpOutput().getByteBuffer(); try { ByteBufferOutputStream out = new ByteBufferOutputStream(buffer); diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpChannel.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpChannel.java index dc1fae1ab269..400b670ca92b 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpChannel.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpChannel.java @@ -43,11 +43,11 @@ import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.MetaData; import org.eclipse.jetty.http.Trailers; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.QuietException; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.ConnectionMetaData; import org.eclipse.jetty.server.Connector; @@ -312,9 +312,9 @@ public void setIdleTimeout(long timeoutMs) _endPoint.setIdleTimeout(timeoutMs); } - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return _connector.getByteBufferPool(); + return _connector.getRetainableByteBufferPool(); } public HttpConfiguration getHttpConfiguration() @@ -897,7 +897,7 @@ public void sendResponseAndComplete() { _request.setHandled(true); _state.completing(); - sendResponse(null, _response.getHttpOutput().getBuffer(), true, Callback.from(() -> _state.completed(null), _state::completed)); + sendResponse(null, _response.getHttpOutput().getByteBuffer(), true, Callback.from(() -> _state.completed(null), _state::completed)); } catch (Throwable x) { diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpOutput.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpOutput.java index e971b5fcaa6f..a1a7fe5cde74 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpOutput.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpOutput.java @@ -33,8 +33,8 @@ import jakarta.servlet.ServletResponse; import jakarta.servlet.WriteListener; import org.eclipse.jetty.http.content.HttpContent; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EofException; +import org.eclipse.jetty.io.RetainableByteBuffer; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; @@ -188,7 +188,7 @@ default void resetBuffer() throws IllegalStateException private long _written; private long _flushed; private long _firstByteNanoTime = -1; - private ByteBuffer _aggregate; + private RetainableByteBuffer _aggregate; private int _bufferSize; private int _commitSize; private WriteListener _writeListener; @@ -289,7 +289,7 @@ private void onWriteComplete(boolean last, Throwable failure) _state = State.CLOSED; closedCallback = _closedCallback; _closedCallback = null; - releaseBuffer(failure); + releaseBuffer(); wake = updateApiState(failure); } else if (_state == State.CLOSE) @@ -298,7 +298,7 @@ else if (_state == State.CLOSE) // We can now send a (probably empty) last buffer and then when it completes // onWriteComplete will be called again to actually execute the _completeCallback _state = State.CLOSING; - closeContent = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + closeContent = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; } else { @@ -373,10 +373,10 @@ private int maximizeAggregateSpace() if (_aggregate == null) return getBufferSize(); + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); // compact to maximize space - BufferUtil.compact(_aggregate); - - return BufferUtil.space(_aggregate); + BufferUtil.compact(byteBuffer); + return BufferUtil.space(byteBuffer); } public void softClose() @@ -456,7 +456,7 @@ public void complete(Callback callback) // Output is idle blocking state, but we still do an async close _apiState = ApiState.BLOCKED; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case ASYNC: @@ -464,7 +464,7 @@ public void complete(Callback callback) // Output is idle in async state, so we can do an async close _apiState = ApiState.PENDING; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case UNREADY: @@ -510,7 +510,7 @@ public void completed(Throwable failure) try (AutoLock l = _channelState.lock()) { _state = State.CLOSED; - releaseBuffer(failure); + releaseBuffer(); } } @@ -562,7 +562,7 @@ public void close() throws IOException _apiState = ApiState.BLOCKED; _state = State.CLOSING; blocker = _writeBlocker.acquire(); - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case BLOCKED: @@ -580,7 +580,7 @@ public void close() throws IOException // Output is idle in async state, so we can do an async close _apiState = ApiState.PENDING; _state = State.CLOSING; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case UNREADY: @@ -635,30 +635,26 @@ public void close() throws IOException } } - public ByteBuffer getBuffer() + public ByteBuffer getByteBuffer() { try (AutoLock l = _channelState.lock()) { - return acquireBuffer(); + return acquireBuffer().getByteBuffer(); } } - private ByteBuffer acquireBuffer() + private RetainableByteBuffer acquireBuffer() { if (_aggregate == null) - _aggregate = _channel.getByteBufferPool().acquire(getBufferSize(), _channel.isUseOutputDirectByteBuffers()); + _aggregate = _channel.getRetainableByteBufferPool().acquire(getBufferSize(), _channel.isUseOutputDirectByteBuffers()); return _aggregate; } - private void releaseBuffer(Throwable failure) + private void releaseBuffer() { if (_aggregate != null) { - ByteBufferPool bufferPool = _channel.getConnector().getByteBufferPool(); - if (failure == null) - bufferPool.release(_aggregate); - else - bufferPool.remove(_aggregate); + _aggregate.release(); _aggregate = null; } } @@ -706,7 +702,7 @@ public void flush() throws IOException { case BLOCKING: _apiState = ApiState.BLOCKED; - content = BufferUtil.hasContent(_aggregate) ? _aggregate : BufferUtil.EMPTY_BUFFER; + content = _aggregate != null && _aggregate.hasRemaining() ? _aggregate.getByteBuffer() : BufferUtil.EMPTY_BUFFER; break; case ASYNC: @@ -786,7 +782,7 @@ public void write(byte[] b, int off, int len) throws IOException // Write will be aggregated if: // + it is smaller than the commitSize // + is not the last one, or is last but will fit in an already allocated aggregate buffer. - aggregate = len <= _commitSize && (!last || BufferUtil.hasContent(_aggregate) && len <= space); + aggregate = len <= _commitSize && (!last || (_aggregate != null && _aggregate.hasRemaining()) && len <= space); flush = last || !aggregate || len >= space; if (last && _state == State.OPEN) @@ -821,14 +817,14 @@ public void write(byte[] b, int off, int len) throws IOException if (aggregate) { acquireBuffer(); - int filled = BufferUtil.fill(_aggregate, b, off, len); + int filled = BufferUtil.fill(_aggregate.getByteBuffer(), b, off, len); // return if we are not complete, not full and filled all the content if (!flush) { if (LOG.isDebugEnabled()) LOG.debug("write(array) {} aggregated !flush {}", - stateString(), BufferUtil.toDetailString(_aggregate)); + stateString(), _aggregate); return; } @@ -840,7 +836,7 @@ public void write(byte[] b, int off, int len) throws IOException if (LOG.isDebugEnabled()) LOG.debug("write(array) {} last={} agg={} flush=true async={}, len={} {}", - stateString(), last, aggregate, async, len, BufferUtil.toDetailString(_aggregate)); + stateString(), last, aggregate, async, len, _aggregate); if (async) { @@ -854,15 +850,17 @@ public void write(byte[] b, int off, int len) throws IOException { boolean complete = false; // flush any content from the aggregate - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); + complete = last && len == 0; - channelWrite(_aggregate, complete); + channelWrite(byteBuffer, complete); // should we fill aggregate again from the buffer? if (len > 0 && !last && len <= _commitSize && len <= maximizeAggregateSpace()) { - BufferUtil.append(_aggregate, b, off, len); + BufferUtil.append(byteBuffer, b, off, len); onWriteComplete(false, null); return; } @@ -915,7 +913,7 @@ public void write(ByteBuffer buffer) throws IOException checkWritable(); long written = _written + len; last = _channel.getResponse().isAllContentWritten(written); - flush = last || len > 0 || BufferUtil.hasContent(_aggregate); + flush = last || len > 0 || (_aggregate != null && _aggregate.hasRemaining()); if (last && _state == State.OPEN) _state = State.CLOSING; @@ -959,10 +957,10 @@ public void write(ByteBuffer buffer) throws IOException // Blocking write // flush any content from the aggregate boolean complete = false; - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { complete = last && len == 0; - channelWrite(_aggregate, complete); + channelWrite(_aggregate.getByteBuffer(), complete); } // write any remaining content in the buffer directly @@ -1024,7 +1022,7 @@ public void write(int b) throws IOException _written = written; acquireBuffer(); - BufferUtil.append(_aggregate, (byte)b); + BufferUtil.append(_aggregate.getByteBuffer(), (byte)b); } // Check if all written or full @@ -1032,13 +1030,15 @@ public void write(int b) throws IOException return; if (async) + { // Do the asynchronous writing from the callback new AsyncFlush(last).iterate(); + } else { try { - channelWrite(_aggregate, last); + channelWrite(_aggregate.getByteBuffer(), last); onWriteComplete(last, null); } catch (Throwable t) @@ -1084,15 +1084,16 @@ private void print(String s, boolean eoln) throws IOException CharBuffer in = CharBuffer.wrap(s); CharBuffer crlf = eoln ? CharBuffer.wrap("\r\n") : null; - ByteBuffer out = getHttpChannel().getByteBufferPool().acquire((int)(1 + (s.length() + 2) * encoder.averageBytesPerChar()), false); - BufferUtil.flipToFill(out); + RetainableByteBuffer out = getHttpChannel().getRetainableByteBufferPool().acquire((int)(1 + (s.length() + 2) * encoder.averageBytesPerChar()), false); + ByteBuffer byteBuffer = out.getByteBuffer(); + BufferUtil.flipToFill(byteBuffer); while (true) { CoderResult result; if (in.hasRemaining()) { - result = encoder.encode(in, out, crlf == null); + result = encoder.encode(in, byteBuffer, crlf == null); if (result.isUnderflow()) if (crlf == null) break; @@ -1101,10 +1102,10 @@ private void print(String s, boolean eoln) throws IOException } else if (crlf != null && crlf.hasRemaining()) { - result = encoder.encode(crlf, out, true); + result = encoder.encode(crlf, byteBuffer, true); if (result.isUnderflow()) { - if (!encoder.flush(out).isUnderflow()) + if (!encoder.flush(byteBuffer).isUnderflow()) result.throwException(); break; } @@ -1114,19 +1115,22 @@ else if (crlf != null && crlf.hasRemaining()) if (result.isOverflow()) { - BufferUtil.flipToFlush(out, 0); - ByteBuffer bigger = BufferUtil.ensureCapacity(out, out.capacity() + s.length() + 2); - getHttpChannel().getByteBufferPool().release(out); - BufferUtil.flipToFill(bigger); + BufferUtil.flipToFlush(byteBuffer, 0); + RetainableByteBuffer bigger = _channel.getRetainableByteBufferPool().acquire(out.capacity() + s.length() + 2, out.isDirect()); + BufferUtil.flipToFill(bigger.getByteBuffer()); + bigger.getByteBuffer().put(byteBuffer); + out.release(); + BufferUtil.flipToFill(bigger.getByteBuffer()); out = bigger; + byteBuffer = bigger.getByteBuffer(); continue; } result.throwException(); } - BufferUtil.flipToFlush(out, 0); - write(out.array(), out.arrayOffset(), out.remaining()); - getHttpChannel().getByteBufferPool().release(out); + BufferUtil.flipToFlush(byteBuffer, 0); + write(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.remaining()); + out.release(); } /** @@ -1256,7 +1260,7 @@ private boolean prepareSendContent(int len, Callback callback) { try (AutoLock l = _channelState.lock()) { - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { callback.failed(new IOException("cannot sendContent() after write()")); return false; @@ -1379,7 +1383,7 @@ public void recycle() _commitSize = config.getOutputAggregationSize(); if (_commitSize > _bufferSize) _commitSize = _bufferSize; - releaseBuffer(null); + releaseBuffer(); _written = 0; _writeListener = null; _onError = null; @@ -1394,8 +1398,8 @@ public void resetBuffer() try (AutoLock l = _channelState.lock()) { _interceptor.resetBuffer(); - if (BufferUtil.hasContent(_aggregate)) - BufferUtil.clear(_aggregate); + if (_aggregate != null) + _aggregate.clear(); _written = 0; } } @@ -1597,10 +1601,10 @@ private AsyncFlush(boolean last) @Override protected Action process() throws Exception { - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { _flushed = true; - channelWrite(_aggregate, false, this); + channelWrite(_aggregate.getByteBuffer(), false, this); return Action.SCHEDULED; } @@ -1649,19 +1653,20 @@ private AsyncWrite(ByteBuffer buffer, boolean last) protected Action process() throws Exception { // flush any content from the aggregate - if (BufferUtil.hasContent(_aggregate)) + if (_aggregate != null && _aggregate.hasRemaining()) { _completed = _len == 0; - channelWrite(_aggregate, _last && _completed, this); + channelWrite(_aggregate.getByteBuffer(), _last && _completed, this); return Action.SCHEDULED; } // Can we just aggregate the remainder? if (!_last && _aggregate != null && _len < maximizeAggregateSpace() && _len < _commitSize) { - int position = BufferUtil.flipToFill(_aggregate); - BufferUtil.put(_buffer, _aggregate); - BufferUtil.flipToFlush(_aggregate, position); + ByteBuffer byteBuffer = _aggregate.getByteBuffer(); + int position = BufferUtil.flipToFill(byteBuffer); + BufferUtil.put(_buffer, byteBuffer); + BufferUtil.flipToFlush(byteBuffer, position); return Action.SUCCEEDED; } @@ -1715,7 +1720,7 @@ protected Action process() throws Exception private class InputStreamWritingCB extends NestedChannelWriteCB { private final InputStream _in; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private boolean _eof; private boolean _closed; @@ -1724,7 +1729,7 @@ private InputStreamWritingCB(InputStream in, Callback callback) super(callback, true); _in = in; // Reading from InputStream requires byte[], don't use direct buffers. - _buffer = _channel.getByteBufferPool().acquire(getBufferSize(), false); + _buffer = _channel.getRetainableByteBufferPool().acquire(getBufferSize(), false); } @Override @@ -1739,18 +1744,20 @@ protected Action process() throws Exception if (!_closed) { _closed = true; - _channel.getByteBufferPool().release(_buffer); + _buffer.release(); IO.close(_in); } return Action.SUCCEEDED; } + ByteBuffer byteBuffer = _buffer.getByteBuffer(); + // Read until buffer full or EOF int len = 0; - while (len < _buffer.capacity() && !_eof) + while (len < byteBuffer.capacity() && !_eof) { - int r = _in.read(_buffer.array(), _buffer.arrayOffset() + len, _buffer.capacity() - len); + int r = _in.read(byteBuffer.array(), byteBuffer.arrayOffset() + len, byteBuffer.capacity() - len); if (r < 0) _eof = true; else @@ -1758,24 +1765,17 @@ protected Action process() throws Exception } // write what we have - _buffer.position(0); - _buffer.limit(len); + byteBuffer.position(0); + byteBuffer.limit(len); _written += len; - channelWrite(_buffer, _eof, this); + channelWrite(byteBuffer, _eof, this); return Action.SCHEDULED; } @Override public void onCompleteFailure(Throwable x) { - try - { - _channel.getByteBufferPool().release(_buffer); - } - finally - { - super.onCompleteFailure(x); - } + _buffer.release(); } } @@ -1791,7 +1791,7 @@ public void onCompleteFailure(Throwable x) private class ReadableByteChannelWritingCB extends NestedChannelWriteCB { private final ReadableByteChannel _in; - private final ByteBuffer _buffer; + private final RetainableByteBuffer _buffer; private boolean _eof; private boolean _closed; @@ -1799,7 +1799,7 @@ private ReadableByteChannelWritingCB(ReadableByteChannel in, Callback callback) { super(callback, true); _in = in; - _buffer = _channel.getByteBufferPool().acquire(getBufferSize(), _channel.isUseOutputDirectByteBuffers()); + _buffer = _channel.getRetainableByteBufferPool().acquire(getBufferSize(), _channel.isUseOutputDirectByteBuffers()); } @Override @@ -1814,23 +1814,25 @@ protected Action process() throws Exception if (!_closed) { _closed = true; - _channel.getByteBufferPool().release(_buffer); + _buffer.release(); IO.close(_in); } return Action.SUCCEEDED; } + ByteBuffer byteBuffer = _buffer.getByteBuffer(); + // Read from stream until buffer full or EOF - BufferUtil.clearToFill(_buffer); - while (_buffer.hasRemaining() && !_eof) + BufferUtil.clearToFill(byteBuffer); + while (byteBuffer.hasRemaining() && !_eof) { - _eof = (_in.read(_buffer)) < 0; + _eof = (_in.read(byteBuffer)) < 0; } // write what we have - BufferUtil.flipToFlush(_buffer, 0); - _written += _buffer.remaining(); - channelWrite(_buffer, _eof, this); + BufferUtil.flipToFlush(byteBuffer, 0); + _written += byteBuffer.remaining(); + channelWrite(byteBuffer, _eof, this); return Action.SCHEDULED; } @@ -1838,7 +1840,7 @@ protected Action process() throws Exception @Override public void onCompleteFailure(Throwable x) { - _channel.getByteBufferPool().release(_buffer); + _buffer.release(); IO.close(_in); super.onCompleteFailure(x); } diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java index e76679016c41..ec4913c5b7fc 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java @@ -35,8 +35,7 @@ import org.eclipse.jetty.http.content.ResourceHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; import org.eclipse.jetty.http.content.VirtualHttpContentFactory; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.resource.Resource; @@ -55,7 +54,7 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory, { private static final Logger LOG = LoggerFactory.getLogger(ResourceHandler.class); - private ByteBufferPool _byteBufferPool; + private RetainableByteBufferPool _bufferPool; Resource _baseResource; ContextHandler _context; Resource _defaultStyleSheet; @@ -106,7 +105,7 @@ public void doStart() throws Exception if (_mimeTypes == null) _mimeTypes = _context == null ? MimeTypes.DEFAULTS : _context.getMimeTypes(); - _byteBufferPool = getByteBufferPool(_context); + _bufferPool = getRetainableByteBufferPool(_context); if (_resourceService.getHttpContentFactory() == null) _resourceService.setHttpContentFactory(newHttpContentFactory()); _resourceService.setWelcomeFactory(this); @@ -114,15 +113,14 @@ public void doStart() throws Exception super.doStart(); } - private static ByteBufferPool getByteBufferPool(ContextHandler contextHandler) + private static RetainableByteBufferPool getRetainableByteBufferPool(ContextHandler contextHandler) { if (contextHandler == null) - return new NoopByteBufferPool(); + return new RetainableByteBufferPool.NonPooling(); Server server = contextHandler.getServer(); if (server == null) - return new NoopByteBufferPool(); - ByteBufferPool byteBufferPool = server.getBean(ByteBufferPool.class); - return (byteBufferPool == null) ? new NoopByteBufferPool() : byteBufferPool; + return new RetainableByteBufferPool.NonPooling(); + return server.getRetainableByteBufferPool(); } public HttpContent.Factory getHttpContentFactory() @@ -136,7 +134,7 @@ protected HttpContent.Factory newHttpContentFactory() contentFactory = new FileMappingHttpContentFactory(contentFactory); contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, _resourceService.getPrecompressedFormats()); - contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), _byteBufferPool); + contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), _bufferPool); return contentFactory; } diff --git a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/AbstractHttpTest.java b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/AbstractHttpTest.java index e059eed6ac41..1b82586e4079 100644 --- a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/AbstractHttpTest.java +++ b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/AbstractHttpTest.java @@ -24,7 +24,7 @@ import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.http.HttpVersion; -import org.eclipse.jetty.io.ArrayByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -51,7 +51,7 @@ public abstract class AbstractHttpTest public void setUp() throws Exception { server = new Server(); - connector = new ServerConnector(server, null, null, new ArrayByteBufferPool(64, 2048, 64 * 1024), 1, 1, new HttpConnectionFactory()); + connector = new ServerConnector(server, null, null, new ArrayRetainableByteBufferPool(64, 2048, 64 * 1024), 1, 1, new HttpConnectionFactory()); connector.setIdleTimeout(100000); server.addConnector(connector); diff --git a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpOutputTest.java b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpOutputTest.java index 82a4821a0572..6ad0c8ce7e41 100644 --- a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpOutputTest.java +++ b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpOutputTest.java @@ -31,7 +31,6 @@ import jakarta.servlet.WriteListener; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.LocalConnector.LocalEndPoint; @@ -73,8 +72,6 @@ public void init() throws Exception _server = new Server(); _contextHandler = new ContextHandler(_server, "/"); - _server.addBean(new NoopByteBufferPool()); - HttpConnectionFactory http = new HttpConnectionFactory(); http.getHttpConfiguration().setRequestHeaderSize(1024); http.getHttpConfiguration().setResponseHeaderSize(1024); diff --git a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpWriterTest.java b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpWriterTest.java index edfa61863dfd..081fd7cad188 100644 --- a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpWriterTest.java +++ b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/HttpWriterTest.java @@ -17,8 +17,6 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -42,25 +40,16 @@ public void init() throws Exception { _bytes = BufferUtil.allocate(2048); - final ByteBufferPool pool = new ArrayByteBufferPool(); Server server = new Server(); ContextHandler contextHandler = new ContextHandler(server); HttpChannel channel = new HttpChannel(contextHandler, new MockConnectionMetaData()) { - /* @Override public boolean failAllContent(Throwable failure) { return false; } - */ - - @Override - public ByteBufferPool getByteBufferPool() - { - return pool; - } @Override public boolean failed(Throwable x) diff --git a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/MockConnector.java b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/MockConnector.java index ed201ae5d736..a247a95607b3 100644 --- a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/MockConnector.java +++ b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/MockConnector.java @@ -15,15 +15,11 @@ import java.io.IOException; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.util.thread.Scheduler; public class MockConnector extends AbstractConnector { - private final Server _server; - public MockConnector() { this(new Server()); @@ -31,8 +27,7 @@ public MockConnector() public MockConnector(Server server) { - super(server, server.getThreadPool(), server.getBean(Scheduler.class), ByteBufferPool.NOOP, 0); - _server = server; + super(server, server.getThreadPool(), server.getScheduler(), server.getRetainableByteBufferPool(), 0); } @Override @@ -40,25 +35,9 @@ protected void accept(int acceptorID) throws IOException, InterruptedException { } - @Override - public Scheduler getScheduler() - { - return _server.getBean(Scheduler.class); - } - - @Override - public ByteBufferPool getByteBufferPool() - { - ByteBufferPool pool = _server.getBean(ByteBufferPool.class); - if (pool != null) - return pool; - return super.getByteBufferPool(); - } - @Override public Object getTransport() { return null; } } - diff --git a/jetty-ee9/jetty-ee9-proxy/src/main/java/org/eclipse/jetty/ee9/proxy/AsyncMiddleManServlet.java b/jetty-ee9/jetty-ee9-proxy/src/main/java/org/eclipse/jetty/ee9/proxy/AsyncMiddleManServlet.java index 0dd92330322c..74f4e6b9aecf 100644 --- a/jetty-ee9/jetty-ee9-proxy/src/main/java/org/eclipse/jetty/ee9/proxy/AsyncMiddleManServlet.java +++ b/jetty-ee9/jetty-ee9-proxy/src/main/java/org/eclipse/jetty/ee9/proxy/AsyncMiddleManServlet.java @@ -43,8 +43,9 @@ import org.eclipse.jetty.client.Response; import org.eclipse.jetty.client.Result; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.io.RuntimeIOException; import org.eclipse.jetty.server.handler.ConnectHandler; import org.eclipse.jetty.util.BufferUtil; @@ -774,7 +775,7 @@ public GZIPContentTransformer(HttpClient httpClient, ContentTransformer transfor try { this.transformer = transformer; - ByteBufferPool byteBufferPool = httpClient == null ? null : httpClient.getByteBufferPool(); + RetainableByteBufferPool byteBufferPool = httpClient == null ? null : httpClient.getRetainableByteBufferPool(); this.decoder = new GZIPContentDecoder(byteBufferPool, GZIPContentDecoder.DEFAULT_BUFFER_SIZE); this.out = new ByteArrayOutputStream(); this.gzipOut = new GZIPOutputStream(out); @@ -791,7 +792,7 @@ public void transform(ByteBuffer input, boolean finished, List outpu if (logger.isDebugEnabled()) logger.debug("Ungzipping {} bytes, finished={}", input.remaining(), finished); - List decodeds = Collections.emptyList(); + List decodeds = Collections.emptyList(); if (!input.hasRemaining()) { if (finished) @@ -802,14 +803,14 @@ public void transform(ByteBuffer input, boolean finished, List outpu decodeds = new ArrayList<>(); while (true) { - ByteBuffer decoded = decoder.decode(input); + RetainableByteBuffer decoded = decoder.decode(input); decodeds.add(decoded); boolean decodeComplete = !input.hasRemaining() && !decoded.hasRemaining(); boolean complete = finished && decodeComplete; if (logger.isDebugEnabled()) logger.debug("Ungzipped {} bytes, complete={}", decoded.remaining(), complete); if (decoded.hasRemaining() || complete) - transformer.transform(decoded, complete, buffers); + transformer.transform(decoded.getByteBuffer(), complete, buffers); if (decodeComplete) break; } @@ -822,7 +823,7 @@ public void transform(ByteBuffer input, boolean finished, List outpu output.add(result); } - decodeds.forEach(decoder::release); + decodeds.forEach(RetainableByteBuffer::release); } private ByteBuffer gzip(List buffers, boolean finished) throws IOException diff --git a/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java b/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java index e88832fde117..d47b8f18e91a 100644 --- a/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java +++ b/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java @@ -41,8 +41,7 @@ import org.eclipse.jetty.http.content.ResourceHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; import org.eclipse.jetty.http.content.VirtualHttpContentFactory; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.URIUtil; @@ -262,9 +261,9 @@ public void init() long cacheValidationTime = getInitParameter("cacheValidationTime") != null ? Long.parseLong(getInitParameter("cacheValidationTime")) : -2; if (maxCachedFiles != -2 || maxCacheSize != -2 || maxCachedFileSize != -2 || cacheValidationTime != -2) { - ByteBufferPool byteBufferPool = getByteBufferPool(_contextHandler); + RetainableByteBufferPool bufferPool = getRetainableByteBufferPool(_contextHandler); _cachingContentFactory = new ValidatingCachingHttpContentFactory(contentFactory, - (cacheValidationTime > -2) ? cacheValidationTime : Duration.ofSeconds(1).toMillis(), byteBufferPool); + (cacheValidationTime > -2) ? cacheValidationTime : Duration.ofSeconds(1).toMillis(), bufferPool); contentFactory = _cachingContentFactory; if (maxCacheSize >= 0) _cachingContentFactory.setMaxCacheSize(maxCacheSize); @@ -302,15 +301,14 @@ public void init() LOG.debug("resource base = {}", _baseResource); } - private static ByteBufferPool getByteBufferPool(ContextHandler contextHandler) + private static RetainableByteBufferPool getRetainableByteBufferPool(ContextHandler contextHandler) { if (contextHandler == null) - return new NoopByteBufferPool(); + return new RetainableByteBufferPool.NonPooling(); Server server = contextHandler.getServer(); if (server == null) - return new NoopByteBufferPool(); - ByteBufferPool byteBufferPool = server.getBean(ByteBufferPool.class); - return (byteBufferPool == null) ? new NoopByteBufferPool() : byteBufferPool; + return new RetainableByteBufferPool.NonPooling(); + return server.getRetainableByteBufferPool(); } private String getInitParameter(String name, String... deprecated) diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketContainer.java index 38f8bffc36e7..50a2e85ef837 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketContainer.java @@ -24,7 +24,7 @@ import jakarta.websocket.Extension; import jakarta.websocket.WebSocketContainer; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; @@ -53,9 +53,9 @@ public JakartaWebSocketContainer(WebSocketComponents components) protected abstract JakartaWebSocketFrameHandlerFactory getFrameHandlerFactory(); - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return components.getBufferPool(); + return components.getRetainableByteBufferPool(); } public WebSocketExtensionRegistry getExtensionRegistry() diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java index 515acec55051..fdb264cc1e52 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/common/JakartaWebSocketRemoteEndpoint.java @@ -49,12 +49,12 @@ protected JakartaWebSocketRemoteEndpoint(JakartaWebSocketSession session, CoreSe protected MessageWriter newMessageWriter() { - return new MessageWriter(coreSession, session.getContainerImpl().getBufferPool()); + return new MessageWriter(coreSession, session.getContainerImpl().getRetainableByteBufferPool()); } protected MessageOutputStream newMessageOutputStream() { - return new MessageOutputStream(coreSession, session.getContainerImpl().getBufferPool()); + return new MessageOutputStream(coreSession, session.getContainerImpl().getRetainableByteBufferPool()); } @Override diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/AbstractSessionTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/AbstractSessionTest.java index 23ae57530858..38ee82565930 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/AbstractSessionTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/AbstractSessionTest.java @@ -19,7 +19,7 @@ import jakarta.websocket.Endpoint; import jakarta.websocket.EndpointConfig; import jakarta.websocket.Session; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.junit.jupiter.api.AfterAll; @@ -64,9 +64,9 @@ public WebSocketComponents getWebSocketComponents() } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return components.getBufferPool(); + return components.getRetainableByteBufferPool(); } public void waitForDemand(long timeout, TimeUnit timeUnit) throws InterruptedException diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/messages/MessageWriterTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/messages/MessageWriterTest.java index d10645d5fe6a..9bfc413c9049 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/messages/MessageWriterTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-common/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/common/messages/MessageWriterTest.java @@ -19,8 +19,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.websocket.core.CoreSession; @@ -34,7 +34,7 @@ public class MessageWriterTest { - private ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); @Test public void testSingleByteArray512b() throws IOException, InterruptedException diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/LocalServer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/LocalServer.java index e2ae15be0a08..e0a1700b5d2a 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/LocalServer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/LocalServer.java @@ -28,8 +28,8 @@ import org.eclipse.jetty.ee9.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; import org.eclipse.jetty.ee9.websocket.jakarta.server.internal.JakartaWebSocketServerContainer; import org.eclipse.jetty.http.HttpVersion; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -60,7 +60,7 @@ public String echo(String msg) } private static final Logger LOG = LoggerFactory.getLogger(LocalServer.class); - private final ByteBufferPool bufferPool = new MappedByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool(); private Server server; private ServerConnector connector; private LocalConnector localConnector; diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientClassLoaderTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientClassLoaderTest.java index e1d24dfea14b..88931c4fa7f3 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientClassLoaderTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientClassLoaderTest.java @@ -38,7 +38,7 @@ import org.eclipse.jetty.ee9.websocket.jakarta.server.config.JakartaWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -150,7 +150,7 @@ public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exceptio app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); app.copyLib(XmlConfiguration.class, "jetty-xml.jar"); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java index e1fa02a93d5f..fd1af5245dac 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/JakartaClientShutdownWithServerWebAppTest.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.ee9.websocket.jakarta.server.config.JakartaWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -99,7 +99,7 @@ public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exceptio app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); return app; diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee9/websocket/client/WebSocketClient.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee9/websocket/client/WebSocketClient.java index 9d70f6eb01f2..d83492838d69 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee9/websocket/client/WebSocketClient.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-client/src/main/java/org/eclipse/jetty/ee9/websocket/client/WebSocketClient.java @@ -40,8 +40,8 @@ import org.eclipse.jetty.ee9.websocket.common.JettyWebSocketFrameHandler; import org.eclipse.jetty.ee9.websocket.common.JettyWebSocketFrameHandlerFactory; import org.eclipse.jetty.ee9.websocket.common.SessionTracker; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Graceful; @@ -334,9 +334,9 @@ public void setCookieStore(CookieStore cookieStore) getHttpClient().setCookieStore(cookieStore); } - public ByteBufferPool getBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { - return getHttpClient().getByteBufferPool(); + return getHttpClient().getRetainableByteBufferPool(); } @Override diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageInputStreamTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageInputStreamTest.java index 91cb935b0609..c2a02c9d3f2c 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageInputStreamTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageInputStreamTest.java @@ -28,7 +28,6 @@ import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.internal.messages.MessageInputStream; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,14 +38,6 @@ public class MessageInputStreamTest { - public TestableLeakTrackingBufferPool bufferPool = new TestableLeakTrackingBufferPool("Test"); - - @AfterEach - public void afterEach() - { - bufferPool.assertNoLeaks(); - } - @Test public void testBasicAppendRead() throws IOException { diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageOutputStreamTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageOutputStreamTest.java index 27d487ae1f6c..eb43d0acf25b 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageOutputStreamTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/MessageOutputStreamTest.java @@ -16,7 +16,11 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.eclipse.jetty.io.ArrayRetainableByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBuffer; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.core.internal.messages.MessageOutputStream; import org.junit.jupiter.api.AfterEach; @@ -28,29 +32,49 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; public class MessageOutputStreamTest { private static final Logger LOG = LoggerFactory.getLogger(MessageOutputStreamTest.class); private static final int OUTPUT_BUFFER_SIZE = 4096; - public TestableLeakTrackingBufferPool bufferPool = new TestableLeakTrackingBufferPool("Test"); - - @AfterEach - public void afterEach() - { - bufferPool.assertNoLeaks(); - } - + private final AtomicInteger leaks = new AtomicInteger(); + private RetainableByteBufferPool bufferPool; private OutgoingMessageCapture sessionCapture; @BeforeEach - public void setupTest() throws Exception + public void beforeEach() { + bufferPool = new ArrayRetainableByteBufferPool() + { + @Override + public RetainableByteBuffer acquire(int size, boolean direct) + { + leaks.incrementAndGet(); + return new RetainableByteBuffer.Wrapper(super.acquire(size, direct)) + { + @Override + public boolean release() + { + boolean released = super.release(); + if (released) + leaks.decrementAndGet(); + return released; + } + }; + } + }; sessionCapture = new OutgoingMessageCapture(); sessionCapture.setOutputBufferSize(OUTPUT_BUFFER_SIZE); } + @AfterEach + public void afterEach() + { + assertEquals(0, leaks.get(), "leak detected"); + } + @Test public void testMultipleWrites() throws Exception { diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/OutgoingMessageCapture.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/OutgoingMessageCapture.java index 64ab28a0eb5e..7e0a2c3853f0 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/OutgoingMessageCapture.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/OutgoingMessageCapture.java @@ -20,8 +20,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; -import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.NoopByteBufferPool; +import org.eclipse.jetty.io.RetainableByteBufferPool; import org.eclipse.jetty.toolchain.test.Hex; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.CloseStatus; @@ -42,7 +41,7 @@ public class OutgoingMessageCapture extends CoreSession.Empty implements CoreSes public BlockingQueue binaryMessages = new LinkedBlockingDeque<>(); public BlockingQueue events = new LinkedBlockingDeque<>(); - private final ByteBufferPool bufferPool = new NoopByteBufferPool(); + private final RetainableByteBufferPool bufferPool = new RetainableByteBufferPool.NonPooling(); private final MethodHandle wholeTextHandle; private final MethodHandle wholeBinaryHandle; private MessageSink messageSink; @@ -129,7 +128,7 @@ public void sendFrame(Frame frame, Callback callback, boolean batch) } @Override - public ByteBufferPool getByteBufferPool() + public RetainableByteBufferPool getRetainableByteBufferPool() { return bufferPool; } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/TestableLeakTrackingBufferPool.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/TestableLeakTrackingBufferPool.java deleted file mode 100644 index f9bff85ed325..000000000000 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-common/src/test/java/org/eclipse/jetty/ee9/websocket/common/TestableLeakTrackingBufferPool.java +++ /dev/null @@ -1,43 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.ee9.websocket.common; - -import org.eclipse.jetty.io.LeakTrackingByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -public class TestableLeakTrackingBufferPool extends LeakTrackingByteBufferPool -{ - private final String id; - - public TestableLeakTrackingBufferPool(Class clazz) - { - this(clazz.getSimpleName()); - } - - public TestableLeakTrackingBufferPool(String id) - { - super(new MappedByteBufferPool.Tagged()); - this.id = id; - } - - public void assertNoLeaks() - { - assertThat("Leaked Acquires Count for [" + id + "]", getLeakedAcquires(), is(0L)); - assertThat("Leaked Releases Count for [" + id + "]", getLeakedReleases(), is(0L)); - assertThat("Leaked Resource Count for [" + id + "]", getLeakedResources(), is(0L)); - } -} diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee9/websocket/tests/JettyClientClassLoaderTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee9/websocket/tests/JettyClientClassLoaderTest.java index 57cd40075eab..0d78984e7900 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee9/websocket/tests/JettyClientClassLoaderTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee9/websocket/tests/JettyClientClassLoaderTest.java @@ -39,7 +39,7 @@ import org.eclipse.jetty.ee9.websocket.server.config.JettyWebSocketConfiguration; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; @@ -169,7 +169,7 @@ public WebAppTester.WebApp createWebSocketWebapp(String contextName) throws Exce app.copyLib(CoreClientUpgradeRequest.class, "jetty-websocket-core-client.jar"); app.copyLib(WebSocketComponents.class, "jetty-websocket-core-common.jar"); app.copyLib(Response.class, "jetty-client.jar"); - app.copyLib(ByteBufferPool.class, "jetty-io.jar"); + app.copyLib(EndPoint.class, "jetty-io.jar"); app.copyLib(BadMessageException.class, "jetty-http.jar"); app.copyLib(XmlConfiguration.class, "jetty-xml.jar"); diff --git a/tests/jetty-jmh/src/main/java/org/eclipse/jetty/util/ArrayByteBufferPoolBenchmark.java b/tests/jetty-jmh/src/main/java/org/eclipse/jetty/util/ArrayByteBufferPoolBenchmark.java deleted file mode 100644 index 11a4b1d1ff7c..000000000000 --- a/tests/jetty-jmh/src/main/java/org/eclipse/jetty/util/ArrayByteBufferPoolBenchmark.java +++ /dev/null @@ -1,67 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2022 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.util; - -import java.nio.ByteBuffer; - -import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.ByteBufferPool; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.TearDown; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -@State(Scope.Benchmark) -public class ArrayByteBufferPoolBenchmark -{ - private ByteBufferPool pool; - - @Setup - public void setUp() throws Exception - { - pool = new ArrayByteBufferPool(); - } - - @TearDown - public void tearDown() - { - pool = null; - } - - @Benchmark - public void testAcquireRelease() - { - ByteBuffer buffer = pool.acquire(2048, true); - pool.release(buffer); - } - - public static void main(String[] args) throws RunnerException - { - Options opt = new OptionsBuilder() - .include(ArrayByteBufferPoolBenchmark.class.getSimpleName()) - .warmupIterations(3) - .measurementIterations(3) - .forks(1) - .threads(8) - // .addProfiler(GCProfiler.class) - .build(); - - new Runner(opt).run(); - } -}