made default properties for NIO

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1918 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-24 23:25:01 +00:00
parent 95409990b1
commit e680ce6302
6 changed files with 49 additions and 46 deletions

View File

@ -31,11 +31,13 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.http.HttpCommandRendezvous; import org.jclouds.http.HttpCommandRendezvous;
import org.jclouds.lifecycle.BaseLifeCycle; import org.jclouds.lifecycle.BaseLifeCycle;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import javax.inject.Named;
/** /**
* // TODO: Adrian: Document this! * // TODO: Adrian: Document this!
@ -63,11 +65,18 @@ public abstract class HttpCommandConnectionPool<C> extends BaseLifeCycle {
* inputOnly: nothing is taken from this queue. * inputOnly: nothing is taken from this queue.
*/ */
protected final BlockingQueue<HttpCommandRendezvous<?>> resubmitQueue; protected final BlockingQueue<HttpCommandRendezvous<?>> resubmitQueue;
protected final int maxConnectionReuse;
protected final AtomicInteger currentSessionFailures = new AtomicInteger(0); protected final AtomicInteger currentSessionFailures = new AtomicInteger(0);
protected volatile boolean hitBottom = false; protected volatile boolean hitBottom = false;
protected final URI endPoint; protected final URI endPoint;
@Inject(optional = true)
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE)
protected int maxConnectionReuse = 75;
@Inject(optional = true)
@Named(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES)
protected int maxSessionFailures = 2;
public URI getEndPoint() { public URI getEndPoint() {
return endPoint; return endPoint;
} }
@ -77,13 +86,11 @@ public abstract class HttpCommandConnectionPool<C> extends BaseLifeCycle {
} }
public HttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections, public HttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections,
BlockingQueue<HttpCommandRendezvous<?>> rendezvousQueue, BlockingQueue<HttpCommandRendezvous<?>> rendezvousQueue, BlockingQueue<C> available,
@Named("maxConnectionReuse") int maxConnectionReuse, BlockingQueue<C> available,
@Assisted URI endPoint, BaseLifeCycle... dependencies) { @Assisted URI endPoint, BaseLifeCycle... dependencies) {
super(executor, dependencies); super(executor, dependencies);
this.allConnections = allConnections; this.allConnections = allConnections;
this.resubmitQueue = rendezvousQueue; this.resubmitQueue = rendezvousQueue;
this.maxConnectionReuse = maxConnectionReuse;
this.available = available; this.available = available;
this.endPoint = endPoint; this.endPoint = endPoint;
} }

View File

@ -27,12 +27,14 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.http.pool.PoolConstants; import org.jclouds.http.pool.PoolConstants;
import org.jclouds.lifecycle.config.LifeCycleModule; import org.jclouds.lifecycle.config.LifeCycleModule;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides; import com.google.inject.Provides;
import javax.inject.Named;
/** /**
* *
@ -40,15 +42,23 @@ import javax.inject.Named;
*/ */
public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends AbstractModule { public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends AbstractModule {
@Inject(optional = true)
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS)
protected int maxConnections = 12;
@Inject(optional = true)
@Named(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS)
protected int maxWorkerThreads = 12;
protected void configure() { protected void configure() {
install(new LifeCycleModule()); install(new LifeCycleModule());
bind(AtomicInteger.class).toInstance(new AtomicInteger());// max errors bind(AtomicInteger.class).toInstance(new AtomicInteger());// max errors
binder().requestInjection(this);
} }
@Provides @Provides
// @Singleton per uri... // @Singleton per uri...
public abstract BlockingQueue<C> provideAvailablePool( public abstract BlockingQueue<C> provideAvailablePool() throws Exception;
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception;
/** /**
* controls production and destruction of real connections. * controls production and destruction of real connections.
@ -60,8 +70,7 @@ public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends Abst
*/ */
@Provides @Provides
// @Singleton per uri... // @Singleton per uri...
public Semaphore provideTotalConnectionSemaphore( public Semaphore provideTotalConnectionSemaphore() throws Exception {
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception { return new Semaphore(maxConnections, true);
return new Semaphore(max, true);
} }
} }

View File

@ -28,7 +28,6 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.apache.http.ConnectionReuseStrategy; import org.apache.http.ConnectionReuseStrategy;
@ -58,7 +57,6 @@ import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool; import org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool;
import org.jclouds.http.httpnio.pool.NioHttpCommandExecutionHandler; import org.jclouds.http.httpnio.pool.NioHttpCommandExecutionHandler;
import org.jclouds.http.httpnio.pool.NioTransformingHttpCommandExecutorService; import org.jclouds.http.httpnio.pool.NioTransformingHttpCommandExecutorService;
import org.jclouds.http.pool.PoolConstants;
import org.jclouds.http.pool.config.ConnectionPoolCommandExecutorServiceModule; import org.jclouds.http.pool.config.ConnectionPoolCommandExecutorServiceModule;
import com.google.inject.Provides; import com.google.inject.Provides;
@ -140,17 +138,15 @@ public class NioTransformingHttpCommandExecutorServiceModule extends
} }
@Override @Override
public BlockingQueue<NHttpConnection> provideAvailablePool( public BlockingQueue<NHttpConnection> provideAvailablePool() throws Exception {
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception { return new ArrayBlockingQueue<NHttpConnection>(maxConnections, true);
return new ArrayBlockingQueue<NHttpConnection>(max, true);
} }
@Provides @Provides
// @Singleton per uri... // @Singleton per uri...
public DefaultConnectingIOReactor provideDefaultConnectingIOReactor( public DefaultConnectingIOReactor provideDefaultConnectingIOReactor(HttpParams params)
@Named(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS) int ioWorkerThreads, throws IOReactorException {
HttpParams params) throws IOReactorException { return new DefaultConnectingIOReactor(maxWorkerThreads, params);
return new DefaultConnectingIOReactor(ioWorkerThreads, params);
} }
} }

View File

@ -36,6 +36,7 @@ import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import org.apache.http.HttpException; import org.apache.http.HttpException;
@ -55,12 +56,9 @@ import org.jclouds.http.TransformingHttpCommand;
import org.jclouds.http.TransformingHttpCommandExecutorService; import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.http.pool.HttpCommandConnectionHandle; import org.jclouds.http.pool.HttpCommandConnectionHandle;
import org.jclouds.http.pool.HttpCommandConnectionPool; import org.jclouds.http.pool.HttpCommandConnectionPool;
import org.jclouds.http.pool.PoolConstants;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import javax.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import javax.inject.Named;
/** /**
* Connection Pool for HTTP requests that utilizes Apache HTTPNio * Connection Pool for HTTP requests that utilizes Apache HTTPNio
@ -74,7 +72,6 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool<NHtt
private final DefaultConnectingIOReactor ioReactor; private final DefaultConnectingIOReactor ioReactor;
private final IOEventDispatch dispatch; private final IOEventDispatch dispatch;
private final InetSocketAddress target; private final InetSocketAddress target;
private final int maxSessionFailures;
public static interface Factory extends HttpCommandConnectionPool.Factory<NHttpConnection> { public static interface Factory extends HttpCommandConnectionPool.Factory<NHttpConnection> {
NioHttpCommandConnectionPool create(URI endPoint); NioHttpCommandConnectionPool create(URI endPoint);
@ -84,11 +81,9 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool<NHtt
public NioHttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections, public NioHttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections,
BlockingQueue<HttpCommandRendezvous<?>> commandQueue, BlockingQueue<HttpCommandRendezvous<?>> commandQueue,
BlockingQueue<NHttpConnection> available, AsyncNHttpClientHandler clientHandler, BlockingQueue<NHttpConnection> available, AsyncNHttpClientHandler clientHandler,
DefaultConnectingIOReactor ioReactor, HttpParams params, DefaultConnectingIOReactor ioReactor, HttpParams params, @Assisted URI endPoint)
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE) int maxConnectionReuse, throws Exception {
@Named(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES) int maxSessionFailures, super(executor, allConnections, commandQueue, available, endPoint);
@Assisted URI endPoint) throws Exception {
super(executor, allConnections, commandQueue, maxConnectionReuse, available, endPoint);
String host = checkNotNull(checkNotNull(endPoint, "endPoint").getHost(), String.format( String host = checkNotNull(checkNotNull(endPoint, "endPoint").getHost(), String.format(
"Host null for endpoint %s", endPoint)); "Host null for endpoint %s", endPoint));
int port = endPoint.getPort(); int port = endPoint.getPort();
@ -104,7 +99,6 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool<NHtt
checkArgument(port > 0, String.format("Port %d not in range for endpoint %s", endPoint checkArgument(port > 0, String.format("Port %d not in range for endpoint %s", endPoint
.getPort(), endPoint)); .getPort(), endPoint));
this.ioReactor = ioReactor; this.ioReactor = ioReactor;
this.maxSessionFailures = maxSessionFailures;
this.sessionCallback = new NHttpClientConnectionPoolSessionRequestCallback(); this.sessionCallback = new NHttpClientConnectionPoolSessionRequestCallback();
this.target = new InetSocketAddress(host, port); this.target = new InetSocketAddress(host, port);
clientHandler.setEventListener(this); clientHandler.setEventListener(this);

View File

@ -44,28 +44,28 @@ public class NioHttpCommandConnectionPoolTest {
public void testConstructorGoodPort() throws Exception { public void testConstructorGoodPort() throws Exception {
NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, URI.create("http://localhost:80")); createNiceMock(HttpParams.class), URI.create("http://localhost:80"));
assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80)); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80));
} }
public void testConstructorGoodSSLPort() throws Exception { public void testConstructorGoodSSLPort() throws Exception {
NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, URI.create("https://localhost:443")); createNiceMock(HttpParams.class), URI.create("https://localhost:443"));
assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443)); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443));
} }
public void testConstructorUnspecifiedPort() throws Exception { public void testConstructorUnspecifiedPort() throws Exception {
NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, URI.create("http://localhost")); createNiceMock(HttpParams.class), URI.create("http://localhost"));
assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80)); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80));
} }
public void testConstructorUnspecifiedSSLPort() throws Exception { public void testConstructorUnspecifiedSSLPort() throws Exception {
NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, URI.create("https://localhost")); createNiceMock(HttpParams.class), URI.create("https://localhost"));
assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443)); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443));
} }
@ -73,13 +73,13 @@ public class NioHttpCommandConnectionPoolTest {
public void testConstructorNullURI() throws Exception { public void testConstructorNullURI() throws Exception {
new NioHttpCommandConnectionPool(null, null, null, null, new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, null); createNiceMock(HttpParams.class), null);
} }
public void testConstructorWeirdName() throws Exception { public void testConstructorWeirdName() throws Exception {
NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(AsyncNHttpClientHandler.class), null,
createNiceMock(HttpParams.class), 0, 0, URI createNiceMock(HttpParams.class), URI
.create("http://adriancole.blobstore1138eu.s3-external-3.amazonaws.com")); .create("http://adriancole.blobstore1138eu.s3-external-3.amazonaws.com"));
assertEquals(pool.getTarget(), new InetSocketAddress( assertEquals(pool.getTarget(), new InetSocketAddress(
"adriancole.blobstore1138eu.s3-external-3.amazonaws.com", 80)); "adriancole.blobstore1138eu.s3-external-3.amazonaws.com", 80));

View File

@ -27,7 +27,6 @@ import java.util.Properties;
import org.jclouds.http.BaseHttpCommandExecutorServiceTest; import org.jclouds.http.BaseHttpCommandExecutorServiceTest;
import org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule; import org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule;
import org.jclouds.http.pool.PoolConstants;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.inject.Module; import com.google.inject.Module;
@ -38,18 +37,16 @@ import com.google.inject.Module;
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test @Test
public class NioTransformingHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorServiceTest { public class NioTransformingHttpCommandExecutorServiceTest extends
BaseHttpCommandExecutorServiceTest {
protected void addConnectionProperties(Properties properties) {
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE, "75");
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES, "2");
properties.setProperty(PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1");
properties.setProperty(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS, "2");
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS, "12");
}
protected Module createClientModule() { protected Module createClientModule() {
return new NioTransformingHttpCommandExecutorServiceModule(); return new NioTransformingHttpCommandExecutorServiceModule();
} }
@Override
protected void addConnectionProperties(Properties props) {
}
} }