mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	CLOUDSTACK-4737: handling usage events for dynamic compute offering
Signed-off-by: Koushik Das <koushik@apache.org>
This commit is contained in:
		
							parent
							
								
									ef40e156ed
								
							
						
					
					
						commit
						b2f0a0bce2
					
				| @ -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<UsageEventDetailsVO, Long> 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<UsageEventDetailsVO> EventDetailsSearch; | ||||
|     protected final SearchBuilder<UsageEventDetailsVO> DetailSearch; | ||||
| 
 | ||||
| @ -74,13 +81,38 @@ public class UsageEventDetailsDaoImpl extends GenericDaoBase<UsageEventDetailsVO | ||||
| 
 | ||||
|     @Override | ||||
|     public Map<String, String> findDetails(long eventId) { | ||||
|         SearchCriteria<UsageEventDetailsVO> sc = EventDetailsSearch.create(); | ||||
|         sc.setParameters("eventId", eventId); | ||||
|         Connection conn = null; | ||||
|         PreparedStatement pstmt = null; | ||||
|         ResultSet resultSet = null; | ||||
|         Map<String, String> details = new HashMap<String, String>(); | ||||
|         try { | ||||
|             conn = TransactionLegacy.getStandaloneConnection(); | ||||
| 
 | ||||
|         List<UsageEventDetailsVO> results = search(sc, null); | ||||
|         Map<String, String> details = new HashMap<String, String>(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; | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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<String, String> 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); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user