From 8c9216d11b63d2edbb4c5dbc30c974f25357a4b8 Mon Sep 17 00:00:00 2001 From: dahn Date: Thu, 17 Apr 2025 12:55:21 +0200 Subject: [PATCH] only clean details and annotations when this tamplate no longer exists on any zone (#10728) --- .../template/HypervisorTemplateAdapter.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java index 0090b35fb21..47a8f0a5468 100644 --- a/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java +++ b/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java @@ -295,7 +295,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { } /** - * For each zone ID in {@link TemplateProfile#zoneIdList}, verifies if there is active heuristic rules for allocating template and returns the + * For each zone ID in {@link TemplateProfile#getZoneIdList()}, verifies if there is active heuristic rules for allocating template and returns the * {@link DataStore} returned by the heuristic rule. If there is not an active heuristic rule, then allocate it to a random {@link DataStore}, if the ISO/template is private * or allocate it to all {@link DataStore} in the zone, if it is public. * @param profile @@ -456,10 +456,10 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { /** * If the template/ISO is marked as private, then it is allocated to a random secondary storage; otherwise, allocates to every storage pool in every zone given by the - * {@link TemplateProfile#zoneIdList}. + * {@link TemplateProfile#getZoneIdList()}. */ private void postUploadAllocation(List imageStores, VMTemplateVO template, List payloads) { - Set zoneSet = new HashSet(); + Set zoneSet = new HashSet<>(); Collections.shuffle(imageStores); for (DataStore imageStore : imageStores) { Long zoneId_is = imageStore.getScope().getScopeId(); @@ -702,8 +702,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { } // delete all cache entries for this template - List cacheTmpls = imageFactory.listTemplateOnCache(template.getId()); - for (TemplateInfo tmplOnCache : cacheTmpls) { + List cachedTemplates = imageFactory.listTemplateOnCache(template.getId()); + for (TemplateInfo tmplOnCache : cachedTemplates) { s_logger.info("Delete template: " + tmplOnCache.getId() + " from image cache store: " + tmplOnCache.getDataStore().getName()); tmplOnCache.delete(); } @@ -732,27 +732,32 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { } // remove its related ACL permission - Pair, Long> tmplt = new Pair, Long>(VirtualMachineTemplate.class, template.getId()); - _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt); - - checkAndRemoveTemplateDetails(template); - - // Remove comments (if any) - AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ? - AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE; - annotationDao.removeByEntityType(entityType.name(), template.getUuid()); + Pair, Long> templateClassForId = new Pair<>(VirtualMachineTemplate.class, template.getId()); + _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, templateClassForId); + List zoneRegistrations = templateZoneDao.listByTemplateId(template.getId()); + if (zoneRegistrations.isEmpty()) { + removeTemplateDetails(template); + removeTemplateAnnotations(template); + } } return success; } + private void removeTemplateAnnotations(VMTemplateVO template) { + // Remove comments (if any) + AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ? + AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE; + annotationDao.removeByEntityType(entityType.name(), template.getUuid()); + } + /** * removes details of the template and * if the template is registered as deploy as is, * then it also deletes the details related to deploy as is only if there are no VMs using the template * @param template */ - void checkAndRemoveTemplateDetails(VMTemplateVO template) { + private void removeTemplateDetails(VMTemplateVO template) { templateDetailsDao.removeDetails(template.getId()); if (template.isDeployAsIs()) {