From issues-return-87972-archive-asf-public=cust-asf.ponee.io@cloudstack.apache.org Thu Jan 4 08:39:20 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 7C3B1180657 for ; Thu, 4 Jan 2018 08:39:20 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 6C2EB160C2B; Thu, 4 Jan 2018 07:39:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 8B9AD160C28 for ; Thu, 4 Jan 2018 08:39:19 +0100 (CET) Received: (qmail 59735 invoked by uid 500); 4 Jan 2018 07:39:13 -0000 Mailing-List: contact issues-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list issues@cloudstack.apache.org Received: (qmail 59726 invoked by uid 500); 4 Jan 2018 07:39:13 -0000 Delivered-To: apmail-incubator-cloudstack-issues@incubator.apache.org Received: (qmail 59723 invoked by uid 99); 4 Jan 2018 07:39:13 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Jan 2018 07:39:13 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 34FE5180582 for ; Thu, 4 Jan 2018 07:39:13 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.21 X-Spam-Level: X-Spam-Status: No, score=-99.21 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id GzOacOYFy1RH for ; Thu, 4 Jan 2018 07:39:07 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 0017D5FB16 for ; Thu, 4 Jan 2018 07:39:06 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id A5ACDE0D34 for ; Thu, 4 Jan 2018 07:39:02 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 1F5A0240EE for ; Thu, 4 Jan 2018 07:39:00 +0000 (UTC) Date: Thu, 4 Jan 2018 07:39:00 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: cloudstack-issues@incubator.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (CLOUDSTACK-10146) Bypass Secondary Storage for KVM templates MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/CLOUDSTACK-10146?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16310910#comment-16310910 ] ASF GitHub Bot commented on CLOUDSTACK-10146: --------------------------------------------- marcaurele 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_r159593522 ########## File path: agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java ########## @@ -0,0 +1,158 @@ +// +// 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.httpclient.Header; +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.log4j.Logger; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +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 + private final HttpMethodRetryHandler myretryhandler; + public static final Logger s_logger = Logger.getLogger(HttpDirectTemplateDownloader.class.getName()); + private GetMethod request; + private long remoteSize; + + public HttpDirectTemplateDownloader(String url, Long templateId, String destPoolPath, String checksum, Map headers) { + super(url, destPoolPath, templateId, checksum); + client = new HttpClient(s_httpClientManager); + myretryhandler = createRetryTwiceHandler(); + request = createRequest(url, headers); + String downloadDir = getDirectDownloadTempPath(templateId); + createTemporaryDirectoryAndFile(downloadDir); + } + + private void createTemporaryDirectoryAndFile(String downloadDir) { + createFolder(getDestPoolPath() + File.separator + downloadDir); + File f = new File(getDestPoolPath() + File.separator + downloadDir + File.separator + getFileNameFromUrl()); + setDownloadedFilePath(f.getAbsolutePath()); + } + + private GetMethod createRequest(String downloadUrl, Map headers) { + GetMethod request = new GetMethod(downloadUrl); + request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); + request.setFollowRedirects(true); + for (String key : headers.keySet()) { + request.setRequestHeader(key, headers.get(key)); + } + return request; + } + + private long getRemoteSize(GetMethod request) { + Header contentLengthHeader = request.getResponseHeader("Content-Length"); + long reportedRemoteSize = 0; + if (contentLengthHeader == null) { + Header chunkedHeader = request.getResponseHeader("Transfer-Encoding"); + if (chunkedHeader == null || !"chunked".equalsIgnoreCase(chunkedHeader.getValue())) { + throw new CloudRuntimeException("Error template remote size"); + } + } else { + reportedRemoteSize = Long.parseLong(contentLengthHeader.getValue()); + } + return reportedRemoteSize; + } + + private 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() { + InputStream in = null; + RandomAccessFile out = null; + try { + client.executeMethod(request); + remoteSize = getRemoteSize(request); + in = request.getResponseBodyAsStream(); + File f = new File(getDownloadedFilePath()); + out = new RandomAccessFile(f, "rw"); + out.seek(0); + copyBytes(in, out); + } catch (IOException e) { + s_logger.error("Error downloading template " + getTemplateId() + e.getMessage()); + return false; + } finally { + try { + out.close(); + in.close(); Review comment: Would be nicer to move the code in a try with resource statement (https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) ---------------------------------------------------------------- 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)