cloudstack-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CLOUDSTACK-10146) Bypass Secondary Storage for KVM templates
Date Fri, 05 Jan 2018 19:38:00 GMT

    [ https://issues.apache.org/jira/browse/CLOUDSTACK-10146?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16313735#comment-16313735
] 

ASF GitHub Bot commented on CLOUDSTACK-10146:
---------------------------------------------

rafaelweingartner commented on a change in pull request #2379: CLOUDSTACK-10146: Bypass Secondary
Storage for KVM templates
URL: https://github.com/apache/cloudstack/pull/2379#discussion_r159961885
 
 

 ##########
 File path: agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java
 ##########
 @@ -0,0 +1,129 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+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.methods.GetMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.util.Map;
+
+public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
+
+    private HttpClient client;
+    private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
+    private static final int CHUNK_SIZE = 1024 * 1024; //1M
+    protected HttpMethodRetryHandler myretryhandler;
+    public static final Logger s_logger = Logger.getLogger(HttpDirectTemplateDownloader.class.getName());
+    protected GetMethod request;
+
+    public HttpDirectTemplateDownloader(String url, Long templateId, String destPoolPath,
String checksum, Map<String, String> headers) {
+        super(url, destPoolPath, templateId, checksum);
+        client = new HttpClient(s_httpClientManager);
+        myretryhandler = createRetryTwiceHandler();
+        request = createRequest(url, headers);
+        String downloadDir = getDirectDownloadTempPath(templateId);
+        createTemporaryDirectoryAndFile(downloadDir);
+    }
+
+    protected void createTemporaryDirectoryAndFile(String downloadDir) {
+        createFolder(getDestPoolPath() + File.separator + downloadDir);
+        File f = new File(getDestPoolPath() + File.separator + downloadDir + File.separator
+ getFileNameFromUrl());
+        setDownloadedFilePath(f.getAbsolutePath());
+    }
+
+    protected GetMethod createRequest(String downloadUrl, Map<String, String> 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()) {
+                request.setRequestHeader(key, headers.get(key));
+            }
+        }
+        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);
+        } catch (IOException e) {
+            throw new CloudRuntimeException("Error on HTTP request: " + e.getMessage());
+        }
+        return performDownload();
+    }
+
+    protected boolean performDownload() {
+        try (
+                InputStream in = request.getResponseBodyAsStream();
+                OutputStream out = new FileOutputStream(getDownloadedFilePath());
+        ) {
+            copyBytes(in, out);
+        } catch (IOException e) {
+            s_logger.error("Error downloading template " + getTemplateId() + " due to: "
+ e.getMessage());
+            return false;
+        }
+        return true;
+    }
+
+    protected void copyBytes(InputStream in, OutputStream out) throws IOException {
+        byte[] block = new byte[CHUNK_SIZE];
+        while (IOUtils.read(in, block) > 0) {
 
 Review comment:
   You do not need this while. As a matter of fact, you do not need this method. You can simply
use `IOUtils.copy(in, out)` at line 115
   
     

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Bypass Secondary Storage for KVM templates
> ------------------------------------------
>
>                 Key: CLOUDSTACK-10146
>                 URL: https://issues.apache.org/jira/browse/CLOUDSTACK-10146
>             Project: CloudStack
>          Issue Type: Bug
>      Security Level: Public(Anyone can view this level - this is the default.) 
>    Affects Versions: 4.11.0.0
>            Reporter: Nicolas Vazquez
>            Assignee: Nicolas Vazquez
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message