From b2f0a0bce26e342f18da42a51ae19d51c238a762 Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Tue, 26 Nov 2013 14:52:19 +0530 Subject: [PATCH] CLOUDSTACK-4737: handling usage events for dynamic compute offering Signed-off-by: Koushik Das --- .../event/dao/UsageEventDetailsDaoImpl.java | 44 ++++++++++++++++--- .../com/cloud/usage/UsageVMInstanceVO.java | 37 ++++++++++++++++ setup/db/db/schema-421to430.sql | 3 ++ .../src/com/cloud/usage/UsageManagerImpl.java | 36 +++++++++++++-- 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/engine/schema/src/com/cloud/event/dao/UsageEventDetailsDaoImpl.java b/engine/schema/src/com/cloud/event/dao/UsageEventDetailsDaoImpl.java index b2be1c934eb..2126fff45c6 100644 --- a/engine/schema/src/com/cloud/event/dao/UsageEventDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/event/dao/UsageEventDetailsDaoImpl.java @@ -16,12 +16,17 @@ // under the License. package com.cloud.event.dao; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ejb.Local; +import com.cloud.utils.exception.CloudRuntimeException; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -36,6 +41,8 @@ import com.cloud.utils.db.TransactionLegacy; public class UsageEventDetailsDaoImpl extends GenericDaoBase implements UsageEventDetailsDao { public static final Logger s_logger = Logger.getLogger(UsageEventDetailsDaoImpl.class.getName()); + private static final String EVENT_DETAILS_QUERY = "SELECT details.id, details.usage_event_id, details.name, details.value FROM `cloud`.`usage_event_details` details WHERE details.usage_event_id = ?"; + protected final SearchBuilder EventDetailsSearch; protected final SearchBuilder DetailSearch; @@ -74,13 +81,38 @@ public class UsageEventDetailsDaoImpl extends GenericDaoBase findDetails(long eventId) { - SearchCriteria sc = EventDetailsSearch.create(); - sc.setParameters("eventId", eventId); + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + Map details = new HashMap(); + try { + conn = TransactionLegacy.getStandaloneConnection(); - List results = search(sc, null); - Map details = new HashMap(results.size()); - for (UsageEventDetailsVO result : results) { - details.put(result.getKey(), result.getValue()); + pstmt = conn.prepareStatement(EVENT_DETAILS_QUERY); + pstmt.setLong(1, eventId); + resultSet = pstmt.executeQuery(); + + while (resultSet.next()) { + details.put(resultSet.getString(3), resultSet.getString(4)); + } + + } catch (SQLException e) { + throw new CloudRuntimeException("Error while executing SQL prepared statement", e); + } catch (Throwable e) { + throw new CloudRuntimeException("Caught: " + e); + } finally { + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + } + } } return details; diff --git a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java index 06a7beeecf0..cd746c2ca9d 100644 --- a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java +++ b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java @@ -27,6 +27,10 @@ import javax.persistence.TemporalType; @Entity @Table(name = "usage_vm_instance") public class UsageVMInstanceVO { + public enum DynamicParameters { + cpuSpeed, cpuNumber, memory + }; + @Column(name = "usage_type") private int usageType; @@ -45,6 +49,15 @@ public class UsageVMInstanceVO { @Column(name = "service_offering_id") private long serviceOfferingId; + @Column(name="cpu_cores") + private Long cpuCores; + + @Column(name="memory") + private Long memory; + + @Column(name="cpu_speed") + private Long cpuSpeed; + @Column(name = "template_id") private long templateId; @@ -127,4 +140,28 @@ public class UsageVMInstanceVO { public void setEndDate(Date endDate) { this.endDate = endDate; } + + public Long getMemory() { + return memory; + } + + public void setMemory(Long memory) { + this.memory = memory; + } + + public Long getCpuCores() { + return cpuCores; + } + + public void setCpuCores(Long cpuCores) { + this.cpuCores = cpuCores; + } + + public Long getCpuSpeed() { + return cpuSpeed; + } + + public void setCpuSpeed(Long cpuSpeed) { + this.cpuSpeed = cpuSpeed; + } } diff --git a/setup/db/db/schema-421to430.sql b/setup/db/db/schema-421to430.sql index 8be0fb14896..521ac168231 100644 --- a/setup/db/db/schema-421to430.sql +++ b/setup/db/db/schema-421to430.sql @@ -755,6 +755,9 @@ CREATE VIEW `cloud`.`domain_router_view` AS INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 'management-server', "vmware.vcenter.session.timeout", "1200", "VMware client timeout in seconds", "1200", NULL,NULL,0); INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 'management-server', "mgt.server.vendor", "ACS", "the vendor of management server", "ACS", NULL,NULL,0); +ALTER TABLE `cloud_usage`.`usage_vm_instance` ADD COLUMN `cpu_speed` INT(10) UNSIGNED NULL COMMENT 'speed per core in Mhz', + ADD COLUMN `cpu_cores` INT(10) UNSIGNED NULL COMMENT 'number of cpu cores', + ADD COLUMN `memory` INT(10) UNSIGNED NULL COMMENT 'memory in MB'; CREATE TABLE `cloud`.`vpc_details` ( `id` bigint unsigned NOT NULL auto_increment, diff --git a/usage/src/com/cloud/usage/UsageManagerImpl.java b/usage/src/com/cloud/usage/UsageManagerImpl.java index 37131e0a47a..81e7892f1a7 100644 --- a/usage/src/com/cloud/usage/UsageManagerImpl.java +++ b/usage/src/com/cloud/usage/UsageManagerImpl.java @@ -85,6 +85,7 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.TransactionLegacy; +import com.cloud.event.dao.UsageEventDetailsDao; @Component @Local(value = {UsageManager.class}) @@ -137,6 +138,8 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna @Inject protected UsageEventDao _usageEventDao; @Inject + protected UsageEventDetailsDao _usageEventDetailsDao; + @Inject ConfigurationDao _configDao; @Inject private UsageVMSnapshotDao m_usageVMSnapshotDao; @@ -1102,10 +1105,37 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna try { Long templateId = event.getTemplateId(); String hypervisorType = event.getResourceType(); + Long cpuCores= null; + Long memory = null; + Long cpuSpeed = null; + + //populate the cpu, memory and cpuSpeed of the vm when created from a dynamic offering. + Map usageDetails = _usageEventDetailsDao.findDetails(event.getId()); + + if (usageDetails != null && usageDetails.size() != 0) { + if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuNumber.name()) != null) { + cpuCores = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuNumber.name())); + } + if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuSpeed.name()) != null) { + cpuSpeed = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuSpeed.name())); + } + if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.memory.name()) != null) { + memory = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.memory.name())); + } + } + // add this VM to the usage helper table - UsageVMInstanceVO usageInstanceNew = - new UsageVMInstanceVO(UsageTypes.ALLOCATED_VM, zoneId, event.getAccountId(), vmId, vmName, soId, templateId, hypervisorType, event.getCreateDate(), - null); + UsageVMInstanceVO usageInstanceNew = new UsageVMInstanceVO(UsageTypes.ALLOCATED_VM, zoneId, event.getAccountId(), vmId, vmName, + soId, templateId, hypervisorType, event.getCreateDate(), null); + if (cpuCores != null) { + usageInstanceNew.setCpuCores(cpuCores); + } + if (cpuSpeed != null) { + usageInstanceNew.setCpuSpeed(cpuSpeed); + } + if (memory != null) { + usageInstanceNew.setMemory(memory); + } m_usageInstanceDao.persist(usageInstanceNew); } catch (Exception ex) { s_logger.error("Error saving usage instance for vm: " + vmId, ex);