diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/ConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/ConnectionPool.java index ecf45697e1a..fc95421a0be 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/ConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/ConnectionPool.java @@ -18,425 +18,17 @@ package org.eclipse.jetty.client; -import java.io.Closeable; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Deque; -import java.util.List; -import java.util.Queue; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.ReentrantLock; - -import org.eclipse.jetty.client.api.Connection; import org.eclipse.jetty.client.api.Destination; -import org.eclipse.jetty.util.BlockingArrayQueue; import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.util.Promise; -import org.eclipse.jetty.util.annotation.ManagedAttribute; -import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.ContainerLifeCycle; -import org.eclipse.jetty.util.component.Dumpable; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.util.thread.Sweeper; -@ManagedObject("The connection pool") -public class ConnectionPool implements Closeable, Dumpable, Sweeper.Sweepable +/** + * @deprecated use {@link DuplexConnectionPool} instead + */ +@Deprecated +public class ConnectionPool extends DuplexConnectionPool { - private static final Logger LOG = Log.getLogger(ConnectionPool.class); - - private final AtomicInteger connectionCount = new AtomicInteger(); - private final ReentrantLock lock = new ReentrantLock(); - private final Destination destination; - private final int maxConnections; - private final Callback requester; - private final Deque idleConnections; - private final Queue activeConnections; - public ConnectionPool(Destination destination, int maxConnections, Callback requester) { - this.destination = destination; - this.maxConnections = maxConnections; - this.requester = requester; - this.idleConnections = new LinkedBlockingDeque<>(maxConnections); - this.activeConnections = new BlockingArrayQueue<>(maxConnections); - } - - @ManagedAttribute(value = "The number of connections", readonly = true) - public int getConnectionCount() - { - return connectionCount.get(); - } - - @ManagedAttribute(value = "The number of idle connections", readonly = true) - public int getIdleConnectionCount() - { - return idleConnections.size(); - } - - @ManagedAttribute(value = "The number of active connections", readonly = true) - public int getActiveConnectionCount() - { - return activeConnections.size(); - } - - public Queue getIdleConnections() - { - return idleConnections; - } - - public Queue getActiveConnections() - { - return activeConnections; - } - - public Connection acquire() - { - Connection connection = activateIdle(); - if (connection == null) - connection = tryCreate(); - return connection; - } - - private Connection tryCreate() - { - while (true) - { - int current = getConnectionCount(); - final int next = current + 1; - - if (next > maxConnections) - { - if (LOG.isDebugEnabled()) - LOG.debug("Max connections {}/{} reached", current, maxConnections); - // Try again the idle connections - return activateIdle(); - } - - if (connectionCount.compareAndSet(current, next)) - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection {}/{} creation", next, maxConnections); - - destination.newConnection(new Promise() - { - @Override - public void succeeded(Connection connection) - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection {}/{} creation succeeded {}", next, maxConnections, connection); - - idleCreated(connection); - - proceed(); - } - - @Override - public void failed(Throwable x) - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection " + next + "/" + maxConnections + " creation failed", x); - - connectionCount.decrementAndGet(); - - requester.failed(x); - } - }); - - // Try again the idle connections - return activateIdle(); - } - } - } - - protected void proceed() - { - requester.succeeded(); - } - - protected void idleCreated(Connection connection) - { - boolean idle; - lock(); - try - { - // Use "cold" new connections as last. - idle = idleConnections.offerLast(connection); - } - finally - { - unlock(); - } - - idle(connection, idle); - } - - private Connection activateIdle() - { - boolean acquired; - Connection connection; - lock(); - try - { - connection = idleConnections.pollFirst(); - if (connection == null) - return null; - acquired = activeConnections.offer(connection); - } - finally - { - unlock(); - } - - if (acquired) - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection active {}", connection); - acquired(connection); - return connection; - } - else - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection active overflow {}", connection); - connection.close(); - return null; - } - } - - protected void acquired(Connection connection) - { - } - - public boolean release(Connection connection) - { - boolean idle; - lock(); - try - { - if (!activeConnections.remove(connection)) - return false; - // Make sure we use "hot" connections first. - idle = offerIdle(connection); - } - finally - { - unlock(); - } - - released(connection); - return idle(connection, idle); - } - - protected boolean offerIdle(Connection connection) - { - return idleConnections.offerFirst(connection); - } - - protected boolean idle(Connection connection, boolean idle) - { - if (idle) - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection idle {}", connection); - return true; - } - else - { - if (LOG.isDebugEnabled()) - LOG.debug("Connection idle overflow {}", connection); - connection.close(); - return false; - } - } - - protected void released(Connection connection) - { - } - - public boolean remove(Connection connection) - { - return remove(connection, false); - } - - protected boolean remove(Connection connection, boolean force) - { - boolean activeRemoved; - boolean idleRemoved; - lock(); - try - { - activeRemoved = activeConnections.remove(connection); - idleRemoved = idleConnections.remove(connection); - } - finally - { - unlock(); - } - - if (activeRemoved) - released(connection); - boolean removed = activeRemoved || idleRemoved || force; - if (removed) - { - int pooled = connectionCount.decrementAndGet(); - if (LOG.isDebugEnabled()) - LOG.debug("Connection removed {} - pooled: {}", connection, pooled); - } - return removed; - } - - public boolean isActive(Connection connection) - { - lock(); - try - { - return activeConnections.contains(connection); - } - finally - { - unlock(); - } - } - - public boolean isIdle(Connection connection) - { - lock(); - try - { - return idleConnections.contains(connection); - } - finally - { - unlock(); - } - } - - public boolean isEmpty() - { - return connectionCount.get() == 0; - } - - public void close() - { - List idles = new ArrayList<>(); - List actives = new ArrayList<>(); - lock(); - try - { - idles.addAll(idleConnections); - idleConnections.clear(); - actives.addAll(activeConnections); - activeConnections.clear(); - } - finally - { - unlock(); - } - - connectionCount.set(0); - - for (Connection connection : idles) - connection.close(); - - // A bit drastic, but we cannot wait for all requests to complete - for (Connection connection : actives) - connection.close(); - } - - @Override - public String dump() - { - return ContainerLifeCycle.dump(this); - } - - @Override - public void dump(Appendable out, String indent) throws IOException - { - List actives = new ArrayList<>(); - List idles = new ArrayList<>(); - lock(); - try - { - actives.addAll(activeConnections); - idles.addAll(idleConnections); - } - finally - { - unlock(); - } - - ContainerLifeCycle.dumpObject(out, this); - ContainerLifeCycle.dump(out, indent, actives, idles); - } - - @Override - public boolean sweep() - { - List toSweep = new ArrayList<>(); - lock(); - try - { - for (Connection connection : getActiveConnections()) - { - if (connection instanceof Sweeper.Sweepable) - toSweep.add(((Sweeper.Sweepable)connection)); - } - } - finally - { - unlock(); - } - - for (Sweeper.Sweepable candidate : toSweep) - { - if (candidate.sweep()) - { - boolean removed = getActiveConnections().remove(candidate); - LOG.warn("Connection swept: {}{}{} from active connections{}{}", - candidate, - System.lineSeparator(), - removed ? "Removed" : "Not removed", - System.lineSeparator(), - dump()); - } - } - - return false; - } - - protected void lock() - { - lock.lock(); - } - - protected void unlock() - { - lock.unlock(); - } - - @Override - public String toString() - { - int activeSize; - int idleSize; - lock(); - try - { - activeSize = activeConnections.size(); - idleSize = idleConnections.size(); - } - finally - { - unlock(); - } - - return String.format("%s[c=%d/%d,a=%d,i=%d]", - getClass().getSimpleName(), - connectionCount.get(), - maxConnections, - activeSize, - idleSize); + super(destination, maxConnections, requester); } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java new file mode 100644 index 00000000000..d887923e1ce --- /dev/null +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java @@ -0,0 +1,442 @@ +// +// ======================================================================== +// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.client; + +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantLock; + +import org.eclipse.jetty.client.api.Connection; +import org.eclipse.jetty.client.api.Destination; +import org.eclipse.jetty.util.BlockingArrayQueue; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.util.Promise; +import org.eclipse.jetty.util.annotation.ManagedAttribute; +import org.eclipse.jetty.util.annotation.ManagedObject; +import org.eclipse.jetty.util.component.ContainerLifeCycle; +import org.eclipse.jetty.util.component.Dumpable; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.util.thread.Sweeper; + +@ManagedObject("The connection pool") +public class DuplexConnectionPool implements Closeable, Dumpable, Sweeper.Sweepable +{ + private static final Logger LOG = Log.getLogger(DuplexConnectionPool.class); + + private final AtomicInteger connectionCount = new AtomicInteger(); + private final ReentrantLock lock = new ReentrantLock(); + private final Destination destination; + private final int maxConnections; + private final Callback requester; + private final Deque idleConnections; + private final Queue activeConnections; + + public DuplexConnectionPool(Destination destination, int maxConnections, Callback requester) + { + this.destination = destination; + this.maxConnections = maxConnections; + this.requester = requester; + this.idleConnections = new LinkedBlockingDeque<>(maxConnections); + this.activeConnections = new BlockingArrayQueue<>(maxConnections); + } + + @ManagedAttribute(value = "The number of connections", readonly = true) + public int getConnectionCount() + { + return connectionCount.get(); + } + + @ManagedAttribute(value = "The number of idle connections", readonly = true) + public int getIdleConnectionCount() + { + return idleConnections.size(); + } + + @ManagedAttribute(value = "The number of active connections", readonly = true) + public int getActiveConnectionCount() + { + return activeConnections.size(); + } + + public Queue getIdleConnections() + { + return idleConnections; + } + + public Queue getActiveConnections() + { + return activeConnections; + } + + public Connection acquire() + { + Connection connection = activateIdle(); + if (connection == null) + connection = tryCreate(); + return connection; + } + + private Connection tryCreate() + { + while (true) + { + int current = getConnectionCount(); + final int next = current + 1; + + if (next > maxConnections) + { + if (LOG.isDebugEnabled()) + LOG.debug("Max connections {}/{} reached", current, maxConnections); + // Try again the idle connections + return activateIdle(); + } + + if (connectionCount.compareAndSet(current, next)) + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection {}/{} creation", next, maxConnections); + + destination.newConnection(new Promise() + { + @Override + public void succeeded(Connection connection) + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection {}/{} creation succeeded {}", next, maxConnections, connection); + + idleCreated(connection); + + proceed(); + } + + @Override + public void failed(Throwable x) + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection " + next + "/" + maxConnections + " creation failed", x); + + connectionCount.decrementAndGet(); + + requester.failed(x); + } + }); + + // Try again the idle connections + return activateIdle(); + } + } + } + + protected void proceed() + { + requester.succeeded(); + } + + protected void idleCreated(Connection connection) + { + boolean idle; + lock(); + try + { + // Use "cold" new connections as last. + idle = idleConnections.offerLast(connection); + } + finally + { + unlock(); + } + + idle(connection, idle); + } + + private Connection activateIdle() + { + boolean acquired; + Connection connection; + lock(); + try + { + connection = idleConnections.pollFirst(); + if (connection == null) + return null; + acquired = activeConnections.offer(connection); + } + finally + { + unlock(); + } + + if (acquired) + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection active {}", connection); + acquired(connection); + return connection; + } + else + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection active overflow {}", connection); + connection.close(); + return null; + } + } + + protected void acquired(Connection connection) + { + } + + public boolean release(Connection connection) + { + boolean idle; + lock(); + try + { + if (!activeConnections.remove(connection)) + return false; + // Make sure we use "hot" connections first. + idle = offerIdle(connection); + } + finally + { + unlock(); + } + + released(connection); + return idle(connection, idle); + } + + protected boolean offerIdle(Connection connection) + { + return idleConnections.offerFirst(connection); + } + + protected boolean idle(Connection connection, boolean idle) + { + if (idle) + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection idle {}", connection); + return true; + } + else + { + if (LOG.isDebugEnabled()) + LOG.debug("Connection idle overflow {}", connection); + connection.close(); + return false; + } + } + + protected void released(Connection connection) + { + } + + public boolean remove(Connection connection) + { + return remove(connection, false); + } + + protected boolean remove(Connection connection, boolean force) + { + boolean activeRemoved; + boolean idleRemoved; + lock(); + try + { + activeRemoved = activeConnections.remove(connection); + idleRemoved = idleConnections.remove(connection); + } + finally + { + unlock(); + } + + if (activeRemoved) + released(connection); + boolean removed = activeRemoved || idleRemoved || force; + if (removed) + { + int pooled = connectionCount.decrementAndGet(); + if (LOG.isDebugEnabled()) + LOG.debug("Connection removed {} - pooled: {}", connection, pooled); + } + return removed; + } + + public boolean isActive(Connection connection) + { + lock(); + try + { + return activeConnections.contains(connection); + } + finally + { + unlock(); + } + } + + public boolean isIdle(Connection connection) + { + lock(); + try + { + return idleConnections.contains(connection); + } + finally + { + unlock(); + } + } + + public boolean isEmpty() + { + return connectionCount.get() == 0; + } + + public void close() + { + List idles = new ArrayList<>(); + List actives = new ArrayList<>(); + lock(); + try + { + idles.addAll(idleConnections); + idleConnections.clear(); + actives.addAll(activeConnections); + activeConnections.clear(); + } + finally + { + unlock(); + } + + connectionCount.set(0); + + for (Connection connection : idles) + connection.close(); + + // A bit drastic, but we cannot wait for all requests to complete + for (Connection connection : actives) + connection.close(); + } + + @Override + public String dump() + { + return ContainerLifeCycle.dump(this); + } + + @Override + public void dump(Appendable out, String indent) throws IOException + { + List actives = new ArrayList<>(); + List idles = new ArrayList<>(); + lock(); + try + { + actives.addAll(activeConnections); + idles.addAll(idleConnections); + } + finally + { + unlock(); + } + + ContainerLifeCycle.dumpObject(out, this); + ContainerLifeCycle.dump(out, indent, actives, idles); + } + + @Override + public boolean sweep() + { + List toSweep = new ArrayList<>(); + lock(); + try + { + for (Connection connection : getActiveConnections()) + { + if (connection instanceof Sweeper.Sweepable) + toSweep.add(((Sweeper.Sweepable)connection)); + } + } + finally + { + unlock(); + } + + for (Sweeper.Sweepable candidate : toSweep) + { + if (candidate.sweep()) + { + boolean removed = getActiveConnections().remove(candidate); + LOG.warn("Connection swept: {}{}{} from active connections{}{}", + candidate, + System.lineSeparator(), + removed ? "Removed" : "Not removed", + System.lineSeparator(), + dump()); + } + } + + return false; + } + + protected void lock() + { + lock.lock(); + } + + protected void unlock() + { + lock.unlock(); + } + + @Override + public String toString() + { + int activeSize; + int idleSize; + lock(); + try + { + activeSize = activeConnections.size(); + idleSize = idleConnections.size(); + } + finally + { + unlock(); + } + + return String.format("%s[c=%d/%d,a=%d,i=%d]", + getClass().getSimpleName(), + connectionCount.get(), + maxConnections, + activeSize, + idleSize); + } +} diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 93337516af7..c86a3e2defd 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -994,7 +994,7 @@ public class HttpClient extends ContainerLifeCycle * anymore and leave space for new destinations. * * @param removeIdleDestinations whether destinations that have no connections should be removed - * @see org.eclipse.jetty.client.ConnectionPool + * @see org.eclipse.jetty.client.DuplexConnectionPool */ public void setRemoveIdleDestinations(boolean removeIdleDestinations) { diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/PoolingHttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/PoolingHttpDestination.java index 9f0aff8703d..f6bdd9451e7 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/PoolingHttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/PoolingHttpDestination.java @@ -32,7 +32,7 @@ import org.eclipse.jetty.util.thread.Sweeper; @ManagedObject public abstract class PoolingHttpDestination extends HttpDestination implements Callback { - private final ConnectionPool connectionPool; + private final DuplexConnectionPool connectionPool; public PoolingHttpDestination(HttpClient client, Origin origin) { @@ -44,13 +44,13 @@ public abstract class PoolingHttpDestination extends HttpD sweeper.offer(connectionPool); } - protected ConnectionPool newConnectionPool(HttpClient client) + protected DuplexConnectionPool newConnectionPool(HttpClient client) { - return new ConnectionPool(this, client.getMaxConnectionsPerDestination(), this); + return new DuplexConnectionPool(this, client.getMaxConnectionsPerDestination(), this); } @ManagedAttribute(value = "The connection pool", readonly = true) - public ConnectionPool getConnectionPool() + public DuplexConnectionPool getConnectionPool() { return connectionPool; } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/ValidatingConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/ValidatingConnectionPool.java index 9ddc97a4f0c..2235f75dab4 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/ValidatingConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/ValidatingConnectionPool.java @@ -34,7 +34,7 @@ import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.thread.Scheduler; /** - *

A {@link ConnectionPool} that validates connections before + *

A connection pool that validates connections before * making them available for use.

*

Connections that have just been opened are not validated. * Connections that are {@link #release(Connection) released} will diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientExplicitConnectionTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientExplicitConnectionTest.java index 34c0b0ffa9f..c54f88981e7 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientExplicitConnectionTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientExplicitConnectionTest.java @@ -59,7 +59,7 @@ public class HttpClientExplicitConnectionTest extends AbstractHttpClientServerTe Assert.assertEquals(200, response.getStatus()); HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP)destination; - ConnectionPool connectionPool = httpDestination.getConnectionPool(); + DuplexConnectionPool connectionPool = httpDestination.getConnectionPool(); Assert.assertTrue(connectionPool.getActiveConnections().isEmpty()); Assert.assertTrue(connectionPool.getIdleConnections().isEmpty()); } @@ -94,7 +94,7 @@ public class HttpClientExplicitConnectionTest extends AbstractHttpClientServerTe Assert.assertFalse(httpConnection.getEndPoint().isOpen()); HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP)destination; - ConnectionPool connectionPool = httpDestination.getConnectionPool(); + DuplexConnectionPool connectionPool = httpDestination.getConnectionPool(); Assert.assertTrue(connectionPool.getActiveConnections().isEmpty()); Assert.assertTrue(connectionPool.getIdleConnections().isEmpty()); } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientFailureTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientFailureTest.java index 2584701e5a6..0db34443c2f 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientFailureTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientFailureTest.java @@ -106,7 +106,7 @@ public class HttpClientFailureTest // Expected. } - ConnectionPool connectionPool = connectionRef.get().getHttpDestination().getConnectionPool(); + DuplexConnectionPool connectionPool = connectionRef.get().getHttpDestination().getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -170,7 +170,7 @@ public class HttpClientFailureTest Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS)); Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS)); - ConnectionPool connectionPool = connectionRef.get().getHttpDestination().getConnectionPool(); + DuplexConnectionPool connectionPool = connectionRef.get().getHttpDestination().getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java index e36432aa9c7..7ddeea8b90f 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java @@ -18,9 +18,6 @@ package org.eclipse.jetty.client; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -61,9 +58,12 @@ import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.Scheduler; +import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertThat; + public class HttpClientLoadTest extends AbstractHttpClientServerTest { private final Logger logger = Log.getLogger(HttpClientLoadTest.class); @@ -100,7 +100,7 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest return new HttpDestinationOverHTTP(getHttpClient(), origin) { @Override - protected ConnectionPool newConnectionPool(HttpClient client) + protected DuplexConnectionPool newConnectionPool(HttpClient client) { return new LeakTrackingConnectionPool(this, client.getMaxConnectionsPerDestination(), this) { @@ -143,15 +143,15 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest System.gc(); - assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), is(0L)); - assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), is(0L)); - assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), is(0L)); + assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), Matchers.is(0L)); + assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), Matchers.is(0L)); + assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), Matchers.is(0L)); - assertThat("Client BufferPool - leaked acquires", clientBufferPool.getLeakedAcquires(), is(0L)); - assertThat("Client BufferPool - leaked releases", clientBufferPool.getLeakedReleases(), is(0L)); - assertThat("Client BufferPool - unreleased", clientBufferPool.getLeakedResources(), is(0L)); + assertThat("Client BufferPool - leaked acquires", clientBufferPool.getLeakedAcquires(), Matchers.is(0L)); + assertThat("Client BufferPool - leaked releases", clientBufferPool.getLeakedReleases(), Matchers.is(0L)); + assertThat("Client BufferPool - unreleased", clientBufferPool.getLeakedResources(), Matchers.is(0L)); - assertThat("Connection Leaks", connectionLeaks.get(), is(0L)); + assertThat("Connection Leaks", connectionLeaks.get(), Matchers.is(0L)); } private void run(Random random, int iterations) throws InterruptedException @@ -173,7 +173,7 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest for (String host : Arrays.asList("localhost", "127.0.0.1")) { HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); for (Connection connection : new ArrayList<>(connectionPool.getActiveConnections())) { HttpConnectionOverHTTP active = (HttpConnectionOverHTTP)connection; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java index 662b32a34e0..bff369f79d9 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java @@ -111,7 +111,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertEquals(200, response.getStatus()); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); long start = System.nanoTime(); HttpConnectionOverHTTP connection = null; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientUploadDuringServerShutdown.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientUploadDuringServerShutdown.java index 61e8cf53d8e..b899a1f2115 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientUploadDuringServerShutdown.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientUploadDuringServerShutdown.java @@ -272,7 +272,7 @@ public class HttpClientUploadDuringServerShutdown Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS)); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination("http", "localhost", connector.getLocalPort()); - ConnectionPool pool = destination.getConnectionPool(); + DuplexConnectionPool pool = destination.getConnectionPool(); Assert.assertEquals(0, pool.getConnectionCount()); Assert.assertEquals(0, pool.getIdleConnections().size()); Assert.assertEquals(0, pool.getActiveConnections().size()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java index 771f2368dfc..f8cbb04c5a0 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java @@ -69,7 +69,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -130,7 +130,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -181,7 +181,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -241,7 +241,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -314,7 +314,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -367,7 +367,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -417,7 +417,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -467,7 +467,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); @@ -499,7 +499,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest String host = "localhost"; int port = connector.getLocalPort(); HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, host, port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); final Queue idleConnections = connectionPool.getIdleConnections(); Assert.assertEquals(0, idleConnections.size()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java index c4d6010c515..45b78e7d18e 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java @@ -88,7 +88,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -135,7 +135,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -182,7 +182,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -225,7 +225,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -289,7 +289,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -344,7 +344,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); @@ -454,7 +454,7 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination(scheme, "localhost", connector.getLocalPort()); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnections().size()); Assert.assertEquals(0, connectionPool.getIdleConnections().size()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ServerConnectionCloseTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ServerConnectionCloseTest.java index 00d0cdc1955..23d72de6016 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ServerConnectionCloseTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ServerConnectionCloseTest.java @@ -151,7 +151,7 @@ public class ServerConnectionCloseTest // Connection should have been removed from pool. HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination("http", "localhost", port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getIdleConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnectionCount()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/TLSServerConnectionCloseTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/TLSServerConnectionCloseTest.java index 0cdb795d0fd..79a9cd18799 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/TLSServerConnectionCloseTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/TLSServerConnectionCloseTest.java @@ -183,7 +183,7 @@ public class TLSServerConnectionCloseTest // Connection should have been removed from pool. HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP)client.getDestination("http", "localhost", port); - ConnectionPool connectionPool = destination.getConnectionPool(); + DuplexConnectionPool connectionPool = destination.getConnectionPool(); Assert.assertEquals(0, connectionPool.getConnectionCount()); Assert.assertEquals(0, connectionPool.getIdleConnectionCount()); Assert.assertEquals(0, connectionPool.getActiveConnectionCount()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ValidatingConnectionPoolTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ValidatingConnectionPoolTest.java index d6b707f0d56..a9ad8174fcb 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ValidatingConnectionPoolTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ValidatingConnectionPoolTest.java @@ -194,7 +194,7 @@ public class ValidatingConnectionPoolTest extends AbstractHttpClientServerTest return new HttpDestinationOverHTTP(getHttpClient(), origin) { @Override - protected ConnectionPool newConnectionPool(HttpClient client) + protected DuplexConnectionPool newConnectionPool(HttpClient client) { return new ValidatingConnectionPool(this, client.getMaxConnectionsPerDestination(), this, client.getScheduler(), timeout); } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java index 4b6075b5aa3..aa3b4f5e746 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java @@ -24,7 +24,7 @@ import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.client.AbstractHttpClientServerTest; -import org.eclipse.jetty.client.ConnectionPool; +import org.eclipse.jetty.client.DuplexConnectionPool; import org.eclipse.jetty.client.EmptyServerHandler; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.Origin; @@ -97,9 +97,9 @@ public class HttpDestinationOverHTTPTest extends AbstractHttpClientServerTest HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort())) { @Override - protected ConnectionPool newConnectionPool(HttpClient client) + protected DuplexConnectionPool newConnectionPool(HttpClient client) { - return new ConnectionPool(this, client.getMaxConnectionsPerDestination(), this) + return new DuplexConnectionPool(this, client.getMaxConnectionsPerDestination(), this) { @Override protected void idleCreated(Connection connection) diff --git a/jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java b/jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java index 9c83445108f..35b75f5dda6 100644 --- a/jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java +++ b/jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java @@ -18,12 +18,9 @@ package org.eclipse.jetty.fcgi.server; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - import java.util.concurrent.atomic.AtomicLong; -import org.eclipse.jetty.client.ConnectionPool; +import org.eclipse.jetty.client.DuplexConnectionPool; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpDestination; import org.eclipse.jetty.client.LeakTrackingConnectionPool; @@ -40,9 +37,12 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.util.LeakDetector; import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.hamcrest.Matchers; import org.junit.After; import org.junit.Rule; +import static org.junit.Assert.assertThat; + public abstract class AbstractHttpClientServerTest { @Rule @@ -71,7 +71,7 @@ public abstract class AbstractHttpClientServerTest QueuedThreadPool executor = new QueuedThreadPool(); executor.setName(executor.getName() + "-client"); - + client = new HttpClient(new HttpClientTransportOverFCGI(1, false, "") { @Override @@ -80,7 +80,7 @@ public abstract class AbstractHttpClientServerTest return new HttpDestinationOverFCGI(client, origin) { @Override - protected ConnectionPool newConnectionPool(HttpClient client) + protected DuplexConnectionPool newConnectionPool(HttpClient client) { return new LeakTrackingConnectionPool(this, client.getMaxConnectionsPerDestination(), this) { @@ -105,15 +105,15 @@ public abstract class AbstractHttpClientServerTest { System.gc(); - assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), is(0L)); - assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), is(0L)); - assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), is(0L)); - - assertThat("Client BufferPool - leaked acquires", clientBufferPool.getLeakedAcquires(), is(0L)); - assertThat("Client BufferPool - leaked releases", clientBufferPool.getLeakedReleases(), is(0L)); - assertThat("Client BufferPool - unreleased", clientBufferPool.getLeakedResources(), is(0L)); - - assertThat("Connection Leaks", connectionLeaks.get(), is(0L)); + assertThat("Server BufferPool - leaked acquires", serverBufferPool.getLeakedAcquires(), Matchers.is(0L)); + assertThat("Server BufferPool - leaked releases", serverBufferPool.getLeakedReleases(), Matchers.is(0L)); + assertThat("Server BufferPool - unreleased", serverBufferPool.getLeakedResources(), Matchers.is(0L)); + + assertThat("Client BufferPool - leaked acquires", clientBufferPool.getLeakedAcquires(), Matchers.is(0L)); + assertThat("Client BufferPool - leaked releases", clientBufferPool.getLeakedReleases(), Matchers.is(0L)); + assertThat("Client BufferPool - unreleased", clientBufferPool.getLeakedResources(), Matchers.is(0L)); + + assertThat("Connection Leaks", connectionLeaks.get(), Matchers.is(0L)); if (client != null) client.stop();