Skip to content

Commit a28f4ca

Browse files
iHiroakiKawaispark404
authored andcommitted
HttpClient needs releaseConnection method call
reviewboard: https://reviews.apache.org/r/8186/ Signed-off-by: Hugo Trippaers <trippie@gmail.com>
1 parent fefec37 commit a28f4ca

5 files changed

Lines changed: 57 additions & 38 deletions

File tree

agent/src/com/cloud/agent/AgentShell.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ public static void wget(String url, File file) throws IOException {
231231
int response;
232232
response = client.executeMethod(method);
233233
if (response != HttpURLConnection.HTTP_OK) {
234+
method.releaseConnection();
234235
s_logger.warn("Retrieving from " + url + " gives response code: "
235236
+ response);
236237
throw new CloudRuntimeException("Unable to download from " + url
@@ -253,6 +254,7 @@ public static void wget(String url, File file) throws IOException {
253254
s_logger.warn("Exception while closing download stream from "
254255
+ url + ", ", e);
255256
}
257+
method.releaseConnection();
256258
}
257259

258260
private void loadProperties() throws ConfigurationException {

awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.apache.commons.httpclient.HttpClient;
5959
import org.apache.commons.httpclient.methods.GetMethod;
6060
import org.apache.commons.httpclient.Header;
61+
import org.apache.commons.httpclient.HttpException;
6162
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
6263

6364
/**
@@ -413,23 +414,26 @@ public void deleteObject(String mountedRoot, String bucket, String fileName) {
413414
}
414415

415416
public class ScspDataSource implements DataSource {
416-
GetMethod method;
417-
public ScspDataSource(GetMethod m) {
418-
method = m;
417+
String content_type = null;
418+
byte content[] = null;
419+
public ScspDataSource(GetMethod method) {
420+
Header h = method.getResponseHeader("Content-type");
421+
if (h != null) {
422+
content_type = h.getValue();
423+
}
424+
try{
425+
content = method.getResponseBody();
426+
}catch(IOException e){
427+
s_logger.error("CAStor loadObjectRange getInputStream error", e);
428+
}
419429
}
420430
@Override
421431
public String getContentType() {
422-
Header h = method.getResponseHeader("Content-type");
423-
return h==null ? null : h.getValue();
432+
return content_type;
424433
}
425434
@Override
426-
public InputStream getInputStream() throws IOException {
427-
try {
428-
return method.getResponseBodyAsStream();
429-
} catch (Exception e) {
430-
s_logger.error("CAStor loadObjectRange getInputStream error", e);
431-
return null;
432-
}
435+
public InputStream getInputStream() {
436+
return new ByteArrayInputStream(content);
433437
}
434438
@Override
435439
public String getName() {
@@ -445,21 +449,27 @@ public OutputStream getOutputStream() throws IOException {
445449

446450
@Override
447451
public DataHandler loadObjectRange(String mountedRoot, String bucket, String fileName, long startPos, long endPos) {
452+
HttpClient httpClient = new HttpClient(s_httpClientManager);
453+
// Create a method instance.
454+
GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName));
455+
method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos);
456+
int statusCode;
448457
try {
449-
HttpClient httpClient = new HttpClient(s_httpClientManager);
450-
// Create a method instance.
451-
GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName));
452-
method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos);
453-
int statusCode = httpClient.executeMethod(method);
454-
if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
455-
s_logger.error("CAStor loadObjectRange response: "+ statusCode);
456-
throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
457-
}
458-
return new DataHandler(new ScspDataSource(method));
459-
} catch (Exception e) {
458+
statusCode = httpClient.executeMethod(method);
459+
} catch (HttpException e) {
460460
s_logger.error("CAStor loadObjectRange failure", e);
461461
throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
462+
} catch (IOException e) {
463+
s_logger.error("CAStor loadObjectRange failure", e);
464+
throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
465+
}
466+
if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
467+
s_logger.error("CAStor loadObjectRange response: "+ statusCode);
468+
throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
462469
}
470+
DataHandler ret = new DataHandler(new ScspDataSource(method));
471+
method.releaseConnection();
472+
return ret;
463473
}
464474

465475
@Override

plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ private void login() throws NiciraNvpApiException {
119119
throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
120120
} catch (IOException e) {
121121
throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
122+
} finally {
123+
pm.releaseConnection();
122124
}
123125

124126
if (pm.getStatusCode() != HttpStatus.SC_OK) {
@@ -322,10 +324,11 @@ private <T> void executeUpdateObject(T newObject, String uri, Map<String,String>
322324

323325
if (pm.getStatusCode() != HttpStatus.SC_OK) {
324326
String errorMessage = responseToErrorMessage(pm);
327+
pm.releaseConnection();
325328
s_logger.error("Failed to update object : " + errorMessage);
326329
throw new NiciraNvpApiException("Failed to update object : " + errorMessage);
327330
}
328-
331+
pm.releaseConnection();
329332
}
330333

331334
private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException {
@@ -352,6 +355,7 @@ private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri
352355

353356
if (pm.getStatusCode() != HttpStatus.SC_CREATED) {
354357
String errorMessage = responseToErrorMessage(pm);
358+
pm.releaseConnection();
355359
s_logger.error("Failed to create object : " + errorMessage);
356360
throw new NiciraNvpApiException("Failed to create object : " + errorMessage);
357361
}
@@ -361,6 +365,8 @@ private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri
361365
result = (T)gson.fromJson(pm.getResponseBodyAsString(), TypeToken.get(newObject.getClass()).getType());
362366
} catch (IOException e) {
363367
throw new NiciraNvpApiException("Failed to decode json response body", e);
368+
} finally {
369+
pm.releaseConnection();
364370
}
365371

366372
return result;
@@ -382,9 +388,11 @@ private void executeDeleteObject(String uri) throws NiciraNvpApiException {
382388

383389
if (dm.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
384390
String errorMessage = responseToErrorMessage(dm);
391+
dm.releaseConnection();
385392
s_logger.error("Failed to delete object : " + errorMessage);
386393
throw new NiciraNvpApiException("Failed to delete object : " + errorMessage);
387394
}
395+
dm.releaseConnection();
388396
}
389397

390398
private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException {
@@ -410,6 +418,7 @@ private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<Strin
410418

411419
if (gm.getStatusCode() != HttpStatus.SC_OK) {
412420
String errorMessage = responseToErrorMessage(gm);
421+
gm.releaseConnection();
413422
s_logger.error("Failed to retrieve object : " + errorMessage);
414423
throw new NiciraNvpApiException("Failed to retrieve object : " + errorMessage);
415424
}
@@ -421,15 +430,17 @@ private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<Strin
421430
} catch (IOException e) {
422431
s_logger.error("IOException while retrieving response body",e);
423432
throw new NiciraNvpApiException(e);
433+
} finally {
434+
gm.releaseConnection();
424435
}
425-
426436
return returnValue;
427437
}
428438

429439
private void executeMethod(HttpMethodBase method) throws NiciraNvpApiException {
430440
try {
431441
_client.executeMethod(method);
432442
if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
443+
method.releaseConnection();
433444
// login and try again
434445
login();
435446
_client.executeMethod(method);

server/src/com/cloud/cluster/ClusterServiceServletImpl.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ public String execute(ClusterServicePdu pdu) throws RemoteException {
6262
method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0");
6363
method.addParameter("pduType", Integer.toString(pdu.getPduType()));
6464

65-
try {
66-
return executePostMethod(client, method);
67-
} finally {
68-
method.releaseConnection();
69-
}
65+
return executePostMethod(client, method);
7066
}
7167

7268
@Override
@@ -81,15 +77,11 @@ public boolean ping(String callingPeer) throws RemoteException {
8177
method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING));
8278
method.addParameter("callingPeer", callingPeer);
8379

84-
try {
85-
String returnVal = executePostMethod(client, method);
86-
if("true".equalsIgnoreCase(returnVal)) {
87-
return true;
88-
}
89-
return false;
90-
} finally {
91-
method.releaseConnection();
80+
String returnVal = executePostMethod(client, method);
81+
if("true".equalsIgnoreCase(returnVal)) {
82+
return true;
9283
}
84+
return false;
9385
}
9486

9587
private String executePostMethod(HttpClient client, PostMethod method) {
@@ -115,6 +107,8 @@ private String executePostMethod(HttpClient client, PostMethod method) {
115107
s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method"));
116108
} catch(Throwable e) {
117109
s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method") + ", exception :", e);
110+
} finally {
111+
method.releaseConnection();
118112
}
119113

120114
return result;

server/src/com/cloud/maint/UpgradeManagerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public String deployNewAgent(String url) {
131131
return "Unable to retrieve the file from " + url;
132132
} catch (final IOException e) {
133133
return "Unable to retrieve the file from " + url;
134+
} finally {
135+
method.releaseConnection();
134136
}
135137

136138
file.delete();

0 commit comments

Comments
 (0)