From c0c32b4d9b822b95156119efa1dc1080b86420b2 Mon Sep 17 00:00:00 2001 From: nvazquez Date: Thu, 1 Mar 2018 16:36:18 -0300 Subject: [PATCH] Release connections fix --- .../HttpDirectTemplateDownloader.java | 40 ++++--------------- utils/src/com/cloud/utils/UriUtils.java | 9 +++++ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java b/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java index 3a971f6d538..da3ac3a1737 100644 --- a/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java +++ b/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java @@ -22,12 +22,9 @@ package com.cloud.agent.direct.download; import com.cloud.utils.exception.CloudRuntimeException; import org.apache.commons.collections.MapUtils; import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpMethodRetryHandler; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; -import org.apache.commons.httpclient.NoHttpResponseException; +import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; @@ -43,7 +40,6 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl { protected HttpClient client; private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager(); - protected HttpMethodRetryHandler myretryhandler; public static final Logger s_logger = Logger.getLogger(HttpDirectTemplateDownloader.class.getName()); protected GetMethod request; protected Map reqHeaders = new HashMap<>(); @@ -51,8 +47,8 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl { public HttpDirectTemplateDownloader(String url, Long templateId, String destPoolPath, String checksum, Map headers) { super(url, destPoolPath, templateId, checksum); s_httpClientManager.getParams().setConnectionTimeout(5000); + s_httpClientManager.getParams().setSoTimeout(5000); client = new HttpClient(s_httpClientManager); - myretryhandler = createRetryTwiceHandler(); request = createRequest(url, headers); String downloadDir = getDirectDownloadTempPath(templateId); createTemporaryDirectoryAndFile(downloadDir); @@ -66,7 +62,6 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl { protected GetMethod createRequest(String downloadUrl, Map headers) { GetMethod request = new GetMethod(downloadUrl); - request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); request.setFollowRedirects(true); if (MapUtils.isNotEmpty(headers)) { for (String key : headers.keySet()) { @@ -77,37 +72,18 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl { return request; } - protected HttpMethodRetryHandler createRetryTwiceHandler() { - return new HttpMethodRetryHandler() { - @Override - public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { - if (executionCount >= 2) { - // Do not retry if over max retry count - return false; - } - if (exception instanceof NoHttpResponseException) { - // Retry if the server dropped connection on us - return true; - } - if (!method.isRequestSent()) { - // Retry if the request has not been sent fully or - // if it's OK to retry methods that have been sent - return true; - } - // otherwise do not retry - return false; - } - }; - } - @Override public boolean downloadTemplate() { try { - client.executeMethod(request); + int status = client.executeMethod(request); + if (status != HttpStatus.SC_OK) { + s_logger.warn("Not able to download template, status code: " + status); + return false; + } + return performDownload(); } catch (IOException e) { throw new CloudRuntimeException("Error on HTTP request: " + e.getMessage()); } - return performDownload(); } protected boolean performDownload() { diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index 7bd4234e310..20c319abe40 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -353,6 +353,8 @@ public class UriUtils { } } catch (IOException e) { e.printStackTrace(); + } finally { + getMethod.releaseConnection(); } return null; } @@ -424,6 +426,8 @@ public class UriUtils { } } catch (IOException e) { s_logger.warn(e.getMessage()); + } finally { + getMethod.releaseConnection(); } return false; } @@ -444,6 +448,7 @@ public class UriUtils { status = httpClient.executeMethod(getMethod); } catch (IOException e) { s_logger.error("Error retrieving urls form metalink: " + metalinkUrl); + getMethod.releaseConnection(); return null; } try { @@ -457,6 +462,8 @@ public class UriUtils { } } catch (IOException e) { s_logger.warn(e.getMessage()); + } finally { + getMethod.releaseConnection(); } return urls; } @@ -477,6 +484,8 @@ public class UriUtils { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + hte.getMessage()); } catch (IOException ioe) { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + ioe.getMessage()); + } finally { + httphead.releaseConnection(); } } }