From 0e2e6995b7ae92c253c1fcea0d7e3fb93689f827 Mon Sep 17 00:00:00 2001 From: Nitin Mehta Date: Thu, 30 Jan 2014 17:59:55 -0800 Subject: [PATCH] CLOUDSTACK-5997: Template state changes side affects --- .../cloud/storage/dao/VMTemplateDaoImpl.java | 31 ++++++++++++------- .../template/HypervisorTemplateAdapter.java | 18 ++++++----- .../cloud/template/TemplateManagerImpl.java | 6 ---- setup/db/db/schema-421to430.sql | 2 +- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java index 9a22466c99e..aab32560717 100755 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java @@ -28,6 +28,7 @@ import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; +import com.cloud.template.VirtualMachineTemplate; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -147,7 +148,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem sb.and("format", sb.entity().getFormat(), SearchCriteria.Op.EQ); sb.and("type", sb.entity().getTemplateType(), SearchCriteria.Op.EQ); sb.and("bootable", sb.entity().isBootable(), SearchCriteria.Op.EQ); - sb.and("removed", sb.entity().getRemoved(), SearchCriteria.Op.EQ); + sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ); SearchBuilder tagSearch = _tagsDao.createSearchBuilder(); for (int count = 0; count < tags.size(); count++) { @@ -170,7 +171,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem } if (!listRemoved) { - sc.setParameters("removed", (Object)null); + sc.setParameters("state", VirtualMachineTemplate.State.Active); } if (tags != null && !tags.isEmpty()) { @@ -197,7 +198,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem sc.setParameters("type", TemplateType.USER.toString()); if (!listRemoved) { - sc.setParameters("removed", (Object)null); + sc.setParameters("state", VirtualMachineTemplate.State.Active); } return listBy(sc); @@ -217,7 +218,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem String sql = "select * from template_host_ref as thr INNER JOIN vm_template as t ON t.id=thr.template_id " - + "where thr.host_id=? and t.public=0 and t.featured=0 and t.type='USER' and t.removed is NULL"; + + "where thr.host_id=? and t.public=0 and t.featured=0 and t.type='USER' and t.state='Active'"; List l = new ArrayList(); @@ -265,6 +266,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem public List listByAccountId(long accountId) { SearchCriteria sc = AccountIdSearch.create(); sc.setParameters("accountId", accountId); + sc.setParameters("state", VirtualMachineTemplate.State.Active); return listBy(sc); } @@ -312,12 +314,12 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem PublicIsoSearch.and("format", PublicIsoSearch.entity().getFormat(), SearchCriteria.Op.EQ); PublicIsoSearch.and("type", PublicIsoSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); PublicIsoSearch.and("bootable", PublicIsoSearch.entity().isBootable(), SearchCriteria.Op.EQ); - PublicIsoSearch.and("removed", PublicIsoSearch.entity().getRemoved(), SearchCriteria.Op.EQ); + PublicIsoSearch.and("state", PublicIsoSearch.entity().getState(), SearchCriteria.Op.EQ); UserIsoSearch = createSearchBuilder(); UserIsoSearch.and("format", UserIsoSearch.entity().getFormat(), SearchCriteria.Op.EQ); UserIsoSearch.and("type", UserIsoSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); - UserIsoSearch.and("removed", UserIsoSearch.entity().getRemoved(), SearchCriteria.Op.EQ); + UserIsoSearch.and("state", UserIsoSearch.entity().getState(), SearchCriteria.Op.EQ); tmpltTypeHyperSearch = createSearchBuilder(); tmpltTypeHyperSearch.and("templateType", tmpltTypeHyperSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); @@ -332,7 +334,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem tmpltTypeHyperSearch.done(); readySystemTemplateSearch = createSearchBuilder(); - readySystemTemplateSearch.and("removed", readySystemTemplateSearch.entity().getRemoved(), SearchCriteria.Op.NULL); + readySystemTemplateSearch.and("state", readySystemTemplateSearch.entity().getState(), SearchCriteria.Op.EQ); readySystemTemplateSearch.and("templateType", readySystemTemplateSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); SearchBuilder templateDownloadSearch = _templateDataStoreDao.createSearchBuilder(); templateDownloadSearch.and("downloadState", templateDownloadSearch.entity().getDownloadState(), SearchCriteria.Op.EQ); @@ -355,13 +357,13 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem tmpltTypeHyperSearch2.and("templateName", tmpltTypeHyperSearch2.entity().getName(), SearchCriteria.Op.EQ); tmpltTypeSearch = createSearchBuilder(); - tmpltTypeSearch.and("removed", tmpltTypeSearch.entity().getRemoved(), SearchCriteria.Op.NULL); + tmpltTypeSearch.and("state", tmpltTypeSearch.entity().getState(), SearchCriteria.Op.EQ); tmpltTypeSearch.and("templateType", tmpltTypeSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); AccountIdSearch = createSearchBuilder(); AccountIdSearch.and("accountId", AccountIdSearch.entity().getAccountId(), SearchCriteria.Op.EQ); AccountIdSearch.and("publicTemplate", AccountIdSearch.entity().isPublicTemplate(), SearchCriteria.Op.EQ); - AccountIdSearch.and("removed", AccountIdSearch.entity().getRemoved(), SearchCriteria.Op.NULL); // only list not removed templates for this account + AccountIdSearch.and("state", AccountIdSearch.entity().getState(), SearchCriteria.Op.EQ); // only list not removed templates for this account AccountIdSearch.done(); SearchBuilder tmpltZoneSearch = _templateZoneDao.createSearchBuilder(); @@ -369,7 +371,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem tmpltZoneSearch.and("zoneId", tmpltZoneSearch.entity().getZoneId(), SearchCriteria.Op.EQ); TmpltsInZoneSearch = createSearchBuilder(); - TmpltsInZoneSearch.and("removed", TmpltsInZoneSearch.entity().getRemoved(), SearchCriteria.Op.NULL); + TmpltsInZoneSearch.and("state", TmpltsInZoneSearch.entity().getState(), SearchCriteria.Op.EQ); TmpltsInZoneSearch.and().op("avoidtype", TmpltsInZoneSearch.entity().getTemplateType(), SearchCriteria.Op.NEQ); TmpltsInZoneSearch.or("templateType", TmpltsInZoneSearch.entity().getTemplateType(), SearchCriteria.Op.NULL); TmpltsInZoneSearch.cp(); @@ -378,12 +380,12 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem TmpltsInZoneSearch.done(); ActiveTmpltSearch = createSearchBuilder(); - ActiveTmpltSearch.and("removed", ActiveTmpltSearch.entity().getRemoved(), SearchCriteria.Op.NULL); + ActiveTmpltSearch.and("state", ActiveTmpltSearch.entity().getState(), SearchCriteria.Op.EQ); CountTemplatesByAccount = createSearchBuilder(Long.class); CountTemplatesByAccount.select(null, Func.COUNT, null); CountTemplatesByAccount.and("account", CountTemplatesByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); - CountTemplatesByAccount.and("removed", CountTemplatesByAccount.entity().getRemoved(), SearchCriteria.Op.NULL); + CountTemplatesByAccount.and("state", CountTemplatesByAccount.entity().getState(), SearchCriteria.Op.EQ); CountTemplatesByAccount.done(); // updateStateSearch = this.createSearchBuilder(); @@ -774,6 +776,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem public List listAllInZone(long dataCenterId) { SearchCriteria sc = TmpltsInZoneSearch.create(); sc.setParameters("avoidtype", TemplateType.PERHOST.toString()); + sc.setParameters("state", VirtualMachineTemplate.State.Active); sc.setJoinParameters("tmpltzone", "zoneId", dataCenterId); return listBy(sc); } @@ -781,6 +784,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem @Override public List listAllActive() { SearchCriteria sc = ActiveTmpltSearch.create(); + sc.setParameters("state", VirtualMachineTemplate.State.Active.toString()); return listBy(sc); } @@ -788,6 +792,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem public List listDefaultBuiltinTemplates() { SearchCriteria sc = tmpltTypeSearch.create(); sc.setParameters("templateType", Storage.TemplateType.BUILTIN); + sc.setParameters("state", VirtualMachineTemplate.State.Active); return listBy(sc); } @@ -813,6 +818,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem public VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType) { SearchCriteria sc = readySystemTemplateSearch.create(); sc.setParameters("templateType", Storage.TemplateType.SYSTEM); + sc.setParameters("state", VirtualMachineTemplate.State.Active); sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing); sc.setJoinParameters("tmplHyper", "zoneId", zoneId); sc.setJoinParameters("vmTemplateJoinTemplateStoreRef", "downloadState", VMTemplateStorageResourceAssoc.Status.DOWNLOADED); @@ -873,6 +879,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem public Long countTemplatesForAccount(long accountId) { SearchCriteria sc = CountTemplatesByAccount.create(); sc.setParameters("account", accountId); + sc.setParameters("state", VirtualMachineTemplate.State.Active.toString()); return customSearch(sc, null).get(0); } diff --git a/server/src/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/com/cloud/template/HypervisorTemplateAdapter.java index 25e79db2bbe..96bf9a34703 100755 --- a/server/src/com/cloud/template/HypervisorTemplateAdapter.java +++ b/server/src/com/cloud/template/HypervisorTemplateAdapter.java @@ -382,14 +382,16 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { // find all eligible image stores for this template List iStores = templateMgr.getImageStoreByTemplate(template.getId(), null); if (iStores == null || iStores.size() == 0) { - // remove template from vm_templates table - if (_tmpltDao.remove(template.getId())) { - // Decrement the number of templates and total secondary storage - // space used by the account - Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); - _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template); - _resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal()); - } + // Mark template as Inactive. + template.setState(VirtualMachineTemplate.State.Inactive); + _tmpltDao.update(template.getId(), template); + + // Decrement the number of templates and total secondary storage + // space used by the account + Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); + _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template); + _resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal()); + } } return success; diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index 56232d27609..c2ce344f78c 100755 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -1084,9 +1084,6 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, throw new InvalidParameterValueException("Please specify a valid template."); } - template.setState(VirtualMachineTemplate.State.Inactive); - _tmpltDao.update(template.getId(), template); - TemplateAdapter adapter = getAdapter(template.getHypervisorType()); TemplateProfile profile = adapter.prepareDelete(cmd); return adapter.delete(profile); @@ -1119,9 +1116,6 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, throw new InvalidParameterValueException("Failed to find a secondary storage store in the specified zone."); } - template.setState(VirtualMachineTemplate.State.Inactive); - _tmpltDao.update(template.getId(), template); - TemplateAdapter adapter = getAdapter(template.getHypervisorType()); TemplateProfile profile = adapter.prepareDelete(cmd); boolean result = adapter.delete(profile); diff --git a/setup/db/db/schema-421to430.sql b/setup/db/db/schema-421to430.sql index afad06c128f..a0c45062736 100644 --- a/setup/db/db/schema-421to430.sql +++ b/setup/db/db/schema-421to430.sql @@ -299,7 +299,7 @@ CREATE VIEW `cloud`.`template_view` AS left join `cloud`.`vm_template` source_template ON source_template.id = vm_template.source_template_id left join - `cloud`.`template_store_ref` ON template_store_ref.template_id = vm_template.id and template_store_ref.store_role = 'Image' + `cloud`.`template_store_ref` ON template_store_ref.template_id = vm_template.id and template_store_ref.store_role = 'Image' and template_store_ref.destroyed=0 left join `cloud`.`image_store` ON image_store.removed is NULL AND template_store_ref.store_id is not NULL AND image_store.id = template_store_ref.store_id left join