From c3353257efcc249f9d50becdf8f3d0de0238ae92 Mon Sep 17 00:00:00 2001 From: SudharmaJain Date: Thu, 3 Dec 2015 14:11:14 +0530 Subject: [PATCH] CLOUDSTACK-9100: ISO.CREATE/TEMPLATE.CREATE event missing for usage_event by template sync thread --- .../storage/image/TemplateServiceImpl.java | 75 ++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java b/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java index a1d10e32807..df2d37f05b0 100644 --- a/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java +++ b/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java @@ -27,6 +27,12 @@ import java.util.Set; import javax.inject.Inject; +import com.cloud.configuration.Resource; +import com.cloud.event.EventTypes; +import com.cloud.event.UsageEventUtils; +import org.apache.cloudstack.engine.subsystem.api.storage.Scope; +import org.apache.cloudstack.framework.messagebus.MessageBus; +import org.apache.cloudstack.framework.messagebus.PublishScope; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult; @@ -137,6 +143,8 @@ public class TemplateServiceImpl implements TemplateService { @Inject StorageCacheManager _cacheMgr; @Inject + MessageBus _messageBus; + @Inject ImageStoreDetailsUtil imageStoreDetailsUtil; class TemplateOpContext extends AsyncRpcContext { @@ -355,6 +363,16 @@ public class TemplateServiceImpl implements TemplateService { toBeDownloaded.add(tmplt); } } else { + if(tmpltStore.getDownloadState() != Status.DOWNLOADED) { + String etype = EventTypes.EVENT_TEMPLATE_CREATE; + if (tmplt.getFormat() == ImageFormat.ISO) { + etype = EventTypes.EVENT_ISO_CREATE; + } + + UsageEventUtils.publishUsageEvent(etype, tmplt.getAccountId(), zoneId, tmplt.getId(), tmplt.getName(), null, null, + tmpltInfo.getPhysicalSize(), tmpltInfo.getSize(), VirtualMachineTemplate.class.getName(), tmplt.getUuid()); + } + tmpltStore.setDownloadPercent(100); tmpltStore.setDownloadState(Status.DOWNLOADED); tmpltStore.setState(ObjectInDataStoreStateMachine.State.Ready); @@ -405,6 +423,14 @@ public class TemplateServiceImpl implements TemplateService { tmlpt.setSize(tmpltInfo.getSize()); _templateDao.update(tmplt.getId(), tmlpt); associateTemplateToZone(tmplt.getId(), zoneId); + + String etype = EventTypes.EVENT_TEMPLATE_CREATE; + if (tmplt.getFormat() == ImageFormat.ISO) { + etype = EventTypes.EVENT_ISO_CREATE; + } + + UsageEventUtils.publishUsageEvent(etype, tmplt.getAccountId(), zoneId, tmplt.getId(), tmplt.getName(), null, null, + tmpltInfo.getPhysicalSize(), tmpltInfo.getSize(), VirtualMachineTemplate.class.getName(), tmplt.getUuid()); } } else if (tmplt.getState() == VirtualMachineTemplate.State.NotUploaded || tmplt.getState() == VirtualMachineTemplate.State.UploadInProgress) { s_logger.info("Template Sync did not find " + uniqueName + " on image store " + storeId + " uploaded using SSVM, marking it as failed"); @@ -474,8 +500,12 @@ public class TemplateServiceImpl implements TemplateService { if (availHypers.contains(tmplt.getHypervisorType())) { s_logger.info("Downloading template " + tmplt.getUniqueName() + " to image store " + store.getName()); associateTemplateToZone(tmplt.getId(), zoneId); - TemplateInfo tmpl = _templateFactory.getTemplate(tmplt.getId(), DataStoreRole.Image); - createTemplateAsync(tmpl, store, null); + TemplateInfo tmpl = _templateFactory.getTemplate(tmplt.getId(), store); + TemplateOpContext context = new TemplateOpContext<>(null,(TemplateObject)tmpl, null); + AsyncCallbackDispatcher caller = AsyncCallbackDispatcher.create(this); + caller.setCallback(caller.getTarget().createTemplateAsyncCallBack(null, null)); + caller.setContext(context); + createTemplateAsync(tmpl, store, caller); } else { s_logger.info("Skip downloading template " + tmplt.getUniqueName() + " since current data center does not have hypervisor " + tmplt.getHypervisorType().toString()); @@ -566,6 +596,47 @@ public class TemplateServiceImpl implements TemplateService { } } + protected Void createTemplateAsyncCallBack(AsyncCallbackDispatcher callback, + TemplateOpContext context) { + TemplateInfo template = context.template; + TemplateApiResult result = callback.getResult(); + if (result.isSuccess()) { + VMTemplateVO tmplt = _templateDao.findById(template.getId()); + // need to grant permission for public templates + if (tmplt.isPublicTemplate()) { + _messageBus.publish(null, TemplateManager.MESSAGE_REGISTER_PUBLIC_TEMPLATE_EVENT, PublishScope.LOCAL, tmplt.getId()); + } + long accountId = tmplt.getAccountId(); + if (template.getSize() != null) { + // publish usage event + String etype = EventTypes.EVENT_TEMPLATE_CREATE; + if (tmplt.getFormat() == ImageFormat.ISO) { + etype = EventTypes.EVENT_ISO_CREATE; + } + // get physical size from template_store_ref table + long physicalSize = 0; + DataStore ds = template.getDataStore(); + TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(ds.getId(), template.getId()); + if (tmpltStore != null) { + physicalSize = tmpltStore.getPhysicalSize(); + } else { + s_logger.warn("No entry found in template_store_ref for template id: " + template.getId() + " and image store id: " + ds.getId() + + " at the end of registering template!"); + } + Scope dsScope = ds.getScope(); + if (dsScope.getScopeId() != null) { + UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), dsScope.getScopeId(), template.getId(), template.getName(), null, null, + physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid()); + } else { + s_logger.warn("Zone scope image store " + ds.getId() + " has a null scope id"); + } + _resourceLimitMgr.incrementResourceCount(accountId, Resource.ResourceType.secondary_storage, template.getSize()); + } + } + + return null; + } + private Map listTemplate(DataStore ssStore) { ListTemplateCommand cmd = new ListTemplateCommand(ssStore.getTO(), imageStoreDetailsUtil.getNfsVersion(ssStore.getId())); EndPoint ep = _epSelector.select(ssStore);