Repository: cloudstack
Updated Branches:
  refs/heads/master 30cfeb49d -> 8ae4da89d


CLOUDSTACK-9100: ISO.CREATE/TEMPLATE.CREATE event missing for usage_event by 
template sync thread


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c3353257
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c3353257
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c3353257

Branch: refs/heads/master
Commit: c3353257efcc249f9d50becdf8f3d0de0238ae92
Parents: 3393555
Author: SudharmaJain <[email protected]>
Authored: Thu Dec 3 14:11:14 2015 +0530
Committer: SudharmaJain <[email protected]>
Committed: Wed Apr 20 18:11:46 2016 +0530

----------------------------------------------------------------------
 .../storage/image/TemplateServiceImpl.java      | 75 +++++++++++++++++++-
 1 file changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c3353257/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java
----------------------------------------------------------------------
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 a1d10e3..df2d37f 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<T> extends AsyncRpcContext<T> {
@@ -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<TemplateApiResult> context = 
new TemplateOpContext<>(null,(TemplateObject)tmpl, null);
+                                AsyncCallbackDispatcher<TemplateServiceImpl, 
TemplateApiResult> 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<TemplateServiceImpl, 
TemplateApiResult> callback,
+                                               
TemplateOpContext<TemplateApiResult> 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<String, TemplateProp> listTemplate(DataStore ssStore) {
         ListTemplateCommand cmd = new ListTemplateCommand(ssStore.getTO(), 
imageStoreDetailsUtil.getNfsVersion(ssStore.getId()));
         EndPoint ep = _epSelector.select(ssStore);

Reply via email to