Permalink
Browse files

Release httpclient resources

  • Loading branch information...
1 parent d350008 commit 6304967f834f7a061b8e7569446d7d34b89e2de6 @akkumar akkumar committed Dec 31, 2011
@@ -43,6 +43,7 @@
import voldemort.store.http.HttpStore;
import voldemort.store.metadata.MetadataStore;
import voldemort.utils.ByteArray;
+import voldemort.utils.VoldemortIOUtils;
/**
* A {@link voldemort.client.StoreClientFactory StoreClientFactory} that creates
@@ -137,6 +138,7 @@ protected void validateUrl(URI url) {
public void close() {
super.close();
// should timeout connections on its own
+ VoldemortIOUtils.closeQuietly(this.httpClient);
}
}
@@ -14,7 +14,6 @@
import org.apache.commons.io.FileUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.log4j.Logger;
@@ -24,6 +23,7 @@
import voldemort.cluster.Cluster;
import voldemort.utils.CmdUtils;
import voldemort.utils.Time;
+import voldemort.utils.VoldemortIOUtils;
import voldemort.xml.ClusterMapper;
import com.google.common.base.Joiner;
@@ -32,8 +32,8 @@
/**
* A helper class to invoke the FETCH and SWAP operations on a remote store via
* HTTP.
- *
- *
+ *
+ *
*/
public abstract class StoreSwapper {
@@ -116,21 +116,22 @@ public static void main(String[] args) throws Exception {
StoreSwapper swapper = null;
AdminClient adminClient = null;
+ DefaultHttpClient httpClient = null;
if(useAdminServices) {
adminClient = new AdminClient(cluster, new AdminClientConfig());
swapper = new AdminStoreSwapper(cluster, executor, adminClient, timeoutMs);
} else {
int numConnections = cluster.getNumberOfNodes() + 3;
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
- DefaultHttpClient client = new DefaultHttpClient(connectionManager);
+ httpClient = new DefaultHttpClient(connectionManager);
- HttpParams clientParams = client.getParams();
+ HttpParams clientParams = httpClient.getParams();
connectionManager.setMaxTotal(numConnections);
connectionManager.setDefaultMaxPerRoute(numConnections);
HttpConnectionParams.setSoTimeout(clientParams, timeoutMs);
- swapper = new HttpStoreSwapper(cluster, executor, client, mgmtPath);
+ swapper = new HttpStoreSwapper(cluster, executor, httpClient, mgmtPath);
}
try {
@@ -148,6 +149,7 @@ public static void main(String[] args) throws Exception {
adminClient.stop();
executor.shutdownNow();
executor.awaitTermination(1, TimeUnit.SECONDS);
+ VoldemortIOUtils.closeQuietly(httpClient);
}
System.exit(0);
}
@@ -25,6 +25,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
import org.apache.log4j.Logger;
public class VoldemortIOUtils {
@@ -51,6 +52,12 @@ public static void closeQuietly(final HttpResponse httpResponse) {
closeQuietly(httpResponse, "");
}
+ public static void closeQuietly(HttpClient httpClient) {
+ if(httpClient != null) {
+ httpClient.getConnectionManager().shutdown();
+ }
+ }
+
public static String toString(final InputStream input, final String encoding, final long limit)
throws IOException {
StringWriter sw = new StringWriter();
@@ -1,12 +1,12 @@
/*
* Copyright 2008-2009 LinkedIn, Inc
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -32,6 +32,7 @@
import java.util.Set;
import org.apache.commons.io.FileUtils;
+import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
@@ -81,8 +82,8 @@
/**
* Helper functions for testing with real server implementations
- *
- *
+ *
+ *
*/
public class ServerTestUtils {
@@ -210,11 +211,12 @@ public static Context getJettyServer(String clusterXml,
return context;
}
- public static HttpStore getHttpStore(String storeName, RequestFormatType format, int port) {
+ public static HttpStore getHttpStore(String storeName, RequestFormatType format, int port,
+ final HttpClient httpClient) {
return new HttpStore(storeName,
"localhost",
port,
- new DefaultHttpClient(),
+ httpClient,
new RequestFormatFactory().getRequestFormat(format),
false);
}
@@ -286,7 +288,7 @@ public static Cluster getLocalCluster(int numberOfNodes, int[] ports, int[][] pa
/**
* Update a cluster by replacing the specified server with a new host, i.e.
* new ports since they are all localhost
- *
+ *
* @param original The original cluster to be updated
* @param serverIds The ids of the server to be replaced with new hosts
* @return updated cluster
@@ -326,7 +328,7 @@ public static Cluster updateClusterWithNewHost(Cluster original, int... serverId
/**
* Returns a list of zones with their proximity list being in increasing
* order
- *
+ *
* @param numberOfZones The number of zones to return
* @return List of zones
*/
@@ -350,7 +352,7 @@ public static Cluster updateClusterWithNewHost(Cluster original, int... serverId
* Returns a cluster with <b>numberOfNodes</b> nodes in <b>numberOfZones</b>
* zones. It is important that <b>numberOfNodes</b> be divisible by
* <b>numberOfZones</b>
- *
+ *
* @param numberOfNodes Number of nodes in the cluster
* @param partitionsPerNode Number of partitions in one node
* @param numberOfZones Number of zones
@@ -73,7 +73,7 @@ public void doOperation(int index) {
};
perfTest.run(numRequests, numThreads);
perfTest.printStats();
-
+ VoldemortIOUtils.closeQuietly(client);
System.exit(1);
}
@@ -45,6 +45,7 @@
import voldemort.store.socket.clientrequest.ClientRequestExecutorPool;
import voldemort.utils.ByteArray;
import voldemort.utils.Utils;
+import voldemort.utils.VoldemortIOUtils;
import voldemort.versioning.Versioned;
public class RemoteStoreComparisonTest {
@@ -209,5 +210,8 @@ public void doOperation(int i) {
httpReadTest.printStats();
httpService.stop();
+
+ VoldemortIOUtils.closeQuietly(httpClient);
+
}
}
@@ -16,6 +16,8 @@
package voldemort.client;
+import org.apache.http.client.HttpClient;
+import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.After;
import org.junit.Before;
import org.mortbay.jetty.Server;
@@ -25,16 +27,18 @@
import voldemort.client.protocol.RequestFormatType;
import voldemort.serialization.SerializerFactory;
import voldemort.store.http.HttpStore;
+import voldemort.utils.VoldemortIOUtils;
/**
- *
+ *
*/
public class HttpStoreClientFactoryTest extends AbstractStoreClientFactoryTest {
private HttpStore httpStore;
private Server server;
private Context context;
private String url;
+ private HttpClient httpClient;
@Override
@Before
@@ -46,9 +50,11 @@ public void setUp() throws Exception {
RequestFormatType.VOLDEMORT_V1,
getLocalNode().getHttpPort());
server = context.getServer();
+ httpClient = new DefaultHttpClient();
httpStore = ServerTestUtils.getHttpStore(getValidStoreName(),
RequestFormatType.VOLDEMORT_V1,
- getLocalNode().getHttpPort());
+ getLocalNode().getHttpPort(),
+ httpClient);
url = getLocalNode().getHttpUrl().toString();
}
@@ -58,6 +64,8 @@ public void tearDown() throws Exception {
httpStore.close();
server.stop();
context.destroy();
+ VoldemortIOUtils.closeQuietly(httpClient);
+
}
@Override
@@ -18,6 +18,7 @@
import java.util.concurrent.TimeUnit;
+import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
@@ -37,6 +38,7 @@
import voldemort.store.Store;
import voldemort.store.UnreachableStoreException;
import voldemort.utils.ByteArray;
+import voldemort.utils.VoldemortIOUtils;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Versioned;
import voldemort.xml.ClusterMapper;
@@ -51,6 +53,7 @@
private HttpStore httpStore;
private Server server;
private Context context;
+ private HttpClient httpClient;
@Override
public void setUp() throws Exception {
@@ -65,7 +68,8 @@ public void setUp() throws Exception {
server = context.getServer();
httpStore = ServerTestUtils.getHttpStore("users",
RequestFormatType.VOLDEMORT_V1,
- node.getHttpPort());
+ node.getHttpPort(),
+ httpClient);
}
public <T extends Exception> void testBadUrlOrPort(String url, int port, Class<T> expected) {
@@ -98,6 +102,8 @@ public void setUp() throws Exception {
} catch(Exception e) {
assertTrue(e.getClass().equals(expected));
}
+
+ client.getConnectionManager().shutdown();
}
public void testBadUrl() {
@@ -118,6 +124,7 @@ public void tearDown() throws Exception {
httpStore.close();
server.stop();
context.destroy();
+ VoldemortIOUtils.closeQuietly(httpClient);
}
@Override
@@ -50,6 +50,7 @@
import voldemort.store.socket.SocketStoreFactory;
import voldemort.store.socket.clientrequest.ClientRequestExecutorPool;
import voldemort.utils.Utils;
+import voldemort.utils.VoldemortIOUtils;
import voldemort.xml.StoreDefinitionsMapper;
import com.google.common.collect.Lists;
@@ -151,15 +152,15 @@ public void testAdminStoreSwapper() throws Exception {
@Test
public void testHttpStoreSwapper() throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
-
+ DefaultHttpClient client = null;
try {
// Use the http store swapper
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
connectionManager.setMaxTotal(10);
connectionManager.setDefaultMaxPerRoute(10);
- DefaultHttpClient client = new DefaultHttpClient(connectionManager);
+ client = new DefaultHttpClient(connectionManager);
StoreSwapper swapper = new HttpStoreSwapper(cluster,
executor,
@@ -170,6 +171,7 @@ public void testHttpStoreSwapper() throws Exception {
testFetchSwap(swapper);
} finally {
executor.shutdown();
+ VoldemortIOUtils.closeQuietly(client);
}
}
@@ -194,7 +196,7 @@ public void testAdminStoreSwapperWithoutRollback() throws Exception {
@Test
public void testHttpStoreSwapperWithoutRollback() throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
-
+ DefaultHttpClient client = null;
try {
// Use the http store swapper
@@ -203,7 +205,7 @@ public void testHttpStoreSwapperWithoutRollback() throws Exception {
connectionManager.setMaxTotal(10);
connectionManager.setDefaultMaxPerRoute(10);
- DefaultHttpClient client = new DefaultHttpClient(connectionManager);
+ client = new DefaultHttpClient(connectionManager);
StoreSwapper swapper = new HttpStoreSwapper(cluster,
executor,
client,
@@ -213,6 +215,7 @@ public void testHttpStoreSwapperWithoutRollback() throws Exception {
testFetchSwapWithoutRollback(swapper);
} finally {
executor.shutdown();
+ VoldemortIOUtils.closeQuietly(client);
}
}

0 comments on commit 6304967

Please sign in to comment.