Skip to content

Commit 37b9d7c

Browse files
DaanHooglanddhslove
authored andcommitted
only clean details and annotations when this tamplate no longer exists on any zone (apache#10728)
1 parent 61ea35c commit 37b9d7c

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public VMTemplateVO create(TemplateProfile profile) {
293293
}
294294

295295
/**
296-
* For each zone ID in {@link TemplateProfile#zoneIdList}, verifies if there is active heuristic rules for allocating template and returns the
296+
* For each zone ID in {@link TemplateProfile#getZoneIdList()}, verifies if there is active heuristic rules for allocating template and returns the
297297
* {@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
298298
* or allocate it to all {@link DataStore} in the zone, if it is public.
299299
* @param profile
@@ -453,10 +453,10 @@ public List<TemplateOrVolumePostUploadCommand> doInTransaction(TransactionStatus
453453

454454
/**
455455
* 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
456-
* {@link TemplateProfile#zoneIdList}.
456+
* {@link TemplateProfile#getZoneIdList()}.
457457
*/
458458
private void postUploadAllocation(List<DataStore> imageStores, VMTemplateVO template, List<TemplateOrVolumePostUploadCommand> payloads) {
459-
Set<Long> zoneSet = new HashSet<Long>();
459+
Set<Long> zoneSet = new HashSet<>();
460460
Collections.shuffle(imageStores);
461461
for (DataStore imageStore : imageStores) {
462462
Long zoneId_is = imageStore.getScope().getScopeId();
@@ -697,8 +697,8 @@ public boolean delete(TemplateProfile profile) {
697697
}
698698

699699
// delete all cache entries for this template
700-
List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
701-
for (TemplateInfo tmplOnCache : cacheTmpls) {
700+
List<TemplateInfo> cachedTemplates = imageFactory.listTemplateOnCache(template.getId());
701+
for (TemplateInfo tmplOnCache : cachedTemplates) {
702702
logger.info("Delete template: {} from image cache store: {}", tmplOnCache, tmplOnCache.getDataStore());
703703
tmplOnCache.delete();
704704
}
@@ -727,27 +727,32 @@ public boolean delete(TemplateProfile profile) {
727727
}
728728

729729
// remove its related ACL permission
730-
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
731-
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
732-
733-
checkAndRemoveTemplateDetails(template);
734-
735-
// Remove comments (if any)
736-
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
737-
AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;
738-
annotationDao.removeByEntityType(entityType.name(), template.getUuid());
730+
Pair<Class<?>, Long> templateClassForId = new Pair<>(VirtualMachineTemplate.class, template.getId());
731+
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, templateClassForId);
739732

733+
List<VMTemplateZoneVO> zoneRegistrations = templateZoneDao.listByTemplateId(template.getId());
734+
if (zoneRegistrations.isEmpty()) {
735+
removeTemplateDetails(template);
736+
removeTemplateAnnotations(template);
737+
}
740738
}
741739
return success;
742740
}
743741

742+
private void removeTemplateAnnotations(VMTemplateVO template) {
743+
// Remove comments (if any)
744+
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
745+
AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;
746+
annotationDao.removeByEntityType(entityType.name(), template.getUuid());
747+
}
748+
744749
/**
745750
* removes details of the template and
746751
* if the template is registered as deploy as is,
747752
* then it also deletes the details related to deploy as is only if there are no VMs using the template
748753
* @param template
749754
*/
750-
void checkAndRemoveTemplateDetails(VMTemplateVO template) {
755+
private void removeTemplateDetails(VMTemplateVO template) {
751756
templateDetailsDao.removeDetails(template.getId());
752757

753758
if (template.isDeployAsIs()) {

0 commit comments

Comments
 (0)