From 6ff8752fd0eb1e2b665719df32a93595e4464c32 Mon Sep 17 00:00:00 2001 From: Koushik Das Date: Fri, 30 Jan 2015 10:27:10 +0530 Subject: [PATCH] volume-upload: gc thread to clean partially uploaded files Fixed multiple issues in the template code path --- api/src/com/cloud/template/VirtualMachineTemplate.java | 4 ++++ .../storage/image/db/TemplateDataStoreDaoImpl.java | 3 ++- .../baremetal/manager/BareMetalTemplateAdapter.java | 3 ++- .../com/cloud/template/HypervisorTemplateAdapter.java | 6 +++--- server/src/com/cloud/template/TemplateAdapterBase.java | 3 ++- server/src/com/cloud/template/TemplateManagerImpl.java | 10 +++++++++- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/src/com/cloud/template/VirtualMachineTemplate.java b/api/src/com/cloud/template/VirtualMachineTemplate.java index 39229e5f6af..54d61a4597b 100755 --- a/api/src/com/cloud/template/VirtualMachineTemplate.java +++ b/api/src/com/cloud/template/VirtualMachineTemplate.java @@ -26,6 +26,8 @@ import org.apache.cloudstack.api.InternalIdentity; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.TemplateType; +import com.cloud.storage.Volume.Event; +import com.cloud.storage.Volume.State; import com.cloud.utils.fsm.StateMachine2; import com.cloud.utils.fsm.StateObject; @@ -46,6 +48,8 @@ public interface VirtualMachineTemplate extends ControlledEntity, Identity, Inte static { s_fsm.addTransition(new StateMachine2.Transition(NotUploaded, Event.OperationTimeout, UploadAbandoned, null)); s_fsm.addTransition(new StateMachine2.Transition(NotUploaded, Event.UploadRequested, UploadInProgress, null)); + s_fsm.addTransition(new StateMachine2.Transition(NotUploaded, Event.OperationSucceeded, Active, null)); + s_fsm.addTransition(new StateMachine2.Transition(NotUploaded, Event.OperationFailed, UploadError, null)); s_fsm.addTransition(new StateMachine2.Transition(UploadInProgress, Event.OperationSucceeded, Active, null)); s_fsm.addTransition(new StateMachine2.Transition(UploadInProgress, Event.OperationFailed, UploadError, null)); s_fsm.addTransition(new StateMachine2.Transition(UploadInProgress, Event.OperationTimeout, UploadError, null)); diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java index 1c3f4858d58..066503b0208 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java @@ -68,7 +68,7 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase downloadTemplateSearch; private SearchBuilder uploadTemplateStateSearch; private SearchBuilder templateOnlySearch; - private static final String EXPIRE_DOWNLOAD_URLS_FOR_ZONE = "update template_store_ref set download_url_created=? where store_id in (select id from image_store where data_center_id=?)"; + private static final String EXPIRE_DOWNLOAD_URLS_FOR_ZONE = "update template_store_ref set download_url_created=? where download_url_created is not null and store_id in (select id from image_store where data_center_id=?)"; @Inject private DataStoreManager _storeMgr; @@ -139,6 +139,7 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase createTemplateForPostUpload(TemplateProfile profile) { // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync. - VMTemplateVO template = persistTemplate(profile); + VMTemplateVO template = persistTemplate(profile, State.NotUploaded); if (template == null) { throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate()); diff --git a/server/src/com/cloud/template/TemplateAdapterBase.java b/server/src/com/cloud/template/TemplateAdapterBase.java index e70683b40f9..8bb93dd41ad 100755 --- a/server/src/com/cloud/template/TemplateAdapterBase.java +++ b/server/src/com/cloud/template/TemplateAdapterBase.java @@ -316,13 +316,14 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat owner, null, false, cmd.getImageStoreUuid(), cmd.isDynamicallyScalable(), TemplateType.USER); } - protected VMTemplateVO persistTemplate(TemplateProfile profile) { + protected VMTemplateVO persistTemplate(TemplateProfile profile, VirtualMachineTemplate.State initialState) { Long zoneId = profile.getZoneId(); VMTemplateVO template = new VMTemplateVO(profile.getTemplateId(), profile.getName(), profile.getFormat(), profile.getIsPublic(), profile.getFeatured(), profile.getIsExtractable(), profile.getTemplateType(), profile.getUrl(), profile.getRequiresHVM(), profile.getBits(), profile.getAccountId(), profile.getCheckSum(), profile.getDisplayText(), profile.getPasswordEnabled(), profile.getGuestOsId(), profile.getBootable(), profile.getHypervisorType(), profile.getTemplateTag(), profile.getDetails(), profile.getSshKeyEnabled(), profile.IsDynamicallyScalable()); + template.setState(initialState); if (zoneId == null || zoneId.longValue() == -1) { List dcs = _dcDao.listAll(); diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index bf7ba1165b4..6385789ae3d 100755 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -36,12 +36,12 @@ import javax.naming.ConfigurationException; import com.cloud.utils.EncryptionUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder; + import org.apache.cloudstack.api.command.user.template.GetUploadParamsForTemplateCmd; import org.apache.cloudstack.api.response.GetUploadParamsResponse; import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand; import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd; @@ -188,6 +188,7 @@ import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDao; + import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -350,6 +351,13 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, String url = "https://" + firstCommand.getRemoteEndPoint() + "/upload/" + firstCommand.getEntityUUID(); response.setPostURL(new URL(url)); + // set the post url, this is used in the monitoring thread to determine the SSVM + TemplateDataStoreVO templateStore = _tmplStoreDao.findByTemplate(firstCommand.getEntityId(), DataStoreRole.getRole(firstCommand.getDataToRole())); + if (templateStore != null) { + templateStore.setExtractUrl(url); + _tmplStoreDao.persist(templateStore); + } + response.setId(UUID.fromString(firstCommand.getEntityUUID())); /*