Skip to content

Commit b2f0a0b

Browse files
Harikrishna Patnalakoushik-das
authored andcommitted
CLOUDSTACK-4737: handling usage events for dynamic compute offering
Signed-off-by: Koushik Das <koushik@apache.org>
1 parent ef40e15 commit b2f0a0b

4 files changed

Lines changed: 112 additions & 10 deletions

File tree

engine/schema/src/com/cloud/event/dao/UsageEventDetailsDaoImpl.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
// under the License.
1717
package com.cloud.event.dao;
1818

19+
import java.sql.Connection;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
1923
import java.util.HashMap;
2024
import java.util.List;
2125
import java.util.Map;
2226

2327
import javax.ejb.Local;
2428

29+
import com.cloud.utils.exception.CloudRuntimeException;
2530
import org.apache.log4j.Logger;
2631
import org.springframework.stereotype.Component;
2732

@@ -36,6 +41,8 @@
3641
public class UsageEventDetailsDaoImpl extends GenericDaoBase<UsageEventDetailsVO, Long> implements UsageEventDetailsDao {
3742
public static final Logger s_logger = Logger.getLogger(UsageEventDetailsDaoImpl.class.getName());
3843

44+
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 = ?";
45+
3946
protected final SearchBuilder<UsageEventDetailsVO> EventDetailsSearch;
4047
protected final SearchBuilder<UsageEventDetailsVO> DetailSearch;
4148

@@ -74,13 +81,38 @@ public UsageEventDetailsVO findDetail(long eventId, String key) {
7481

7582
@Override
7683
public Map<String, String> findDetails(long eventId) {
77-
SearchCriteria<UsageEventDetailsVO> sc = EventDetailsSearch.create();
78-
sc.setParameters("eventId", eventId);
79-
80-
List<UsageEventDetailsVO> results = search(sc, null);
81-
Map<String, String> details = new HashMap<String, String>(results.size());
82-
for (UsageEventDetailsVO result : results) {
83-
details.put(result.getKey(), result.getValue());
84+
Connection conn = null;
85+
PreparedStatement pstmt = null;
86+
ResultSet resultSet = null;
87+
Map<String, String> details = new HashMap<String, String>();
88+
try {
89+
conn = TransactionLegacy.getStandaloneConnection();
90+
91+
pstmt = conn.prepareStatement(EVENT_DETAILS_QUERY);
92+
pstmt.setLong(1, eventId);
93+
resultSet = pstmt.executeQuery();
94+
95+
while (resultSet.next()) {
96+
details.put(resultSet.getString(3), resultSet.getString(4));
97+
}
98+
99+
} catch (SQLException e) {
100+
throw new CloudRuntimeException("Error while executing SQL prepared statement", e);
101+
} catch (Throwable e) {
102+
throw new CloudRuntimeException("Caught: " + e);
103+
} finally {
104+
if (pstmt != null) {
105+
try {
106+
pstmt.close();
107+
} catch (SQLException e) {
108+
}
109+
}
110+
if (conn != null) {
111+
try {
112+
conn.close();
113+
} catch (SQLException e) {
114+
}
115+
}
84116
}
85117

86118
return details;

engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
@Entity
2828
@Table(name = "usage_vm_instance")
2929
public class UsageVMInstanceVO {
30+
public enum DynamicParameters {
31+
cpuSpeed, cpuNumber, memory
32+
};
33+
3034
@Column(name = "usage_type")
3135
private int usageType;
3236

@@ -45,6 +49,15 @@ public class UsageVMInstanceVO {
4549
@Column(name = "service_offering_id")
4650
private long serviceOfferingId;
4751

52+
@Column(name="cpu_cores")
53+
private Long cpuCores;
54+
55+
@Column(name="memory")
56+
private Long memory;
57+
58+
@Column(name="cpu_speed")
59+
private Long cpuSpeed;
60+
4861
@Column(name = "template_id")
4962
private long templateId;
5063

@@ -127,4 +140,28 @@ public Date getEndDate() {
127140
public void setEndDate(Date endDate) {
128141
this.endDate = endDate;
129142
}
143+
144+
public Long getMemory() {
145+
return memory;
146+
}
147+
148+
public void setMemory(Long memory) {
149+
this.memory = memory;
150+
}
151+
152+
public Long getCpuCores() {
153+
return cpuCores;
154+
}
155+
156+
public void setCpuCores(Long cpuCores) {
157+
this.cpuCores = cpuCores;
158+
}
159+
160+
public Long getCpuSpeed() {
161+
return cpuSpeed;
162+
}
163+
164+
public void setCpuSpeed(Long cpuSpeed) {
165+
this.cpuSpeed = cpuSpeed;
166+
}
130167
}

setup/db/db/schema-421to430.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ CREATE VIEW `cloud`.`domain_router_view` AS
755755
INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 'management-server', "vmware.vcenter.session.timeout", "1200", "VMware client timeout in seconds", "1200", NULL,NULL,0);
756756
INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 'management-server', "mgt.server.vendor", "ACS", "the vendor of management server", "ACS", NULL,NULL,0);
757757

758+
ALTER TABLE `cloud_usage`.`usage_vm_instance` ADD COLUMN `cpu_speed` INT(10) UNSIGNED NULL COMMENT 'speed per core in Mhz',
759+
ADD COLUMN `cpu_cores` INT(10) UNSIGNED NULL COMMENT 'number of cpu cores',
760+
ADD COLUMN `memory` INT(10) UNSIGNED NULL COMMENT 'memory in MB';
758761

759762
CREATE TABLE `cloud`.`vpc_details` (
760763
`id` bigint unsigned NOT NULL auto_increment,

usage/src/com/cloud/usage/UsageManagerImpl.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.cloud.utils.db.GlobalLock;
8686
import com.cloud.utils.db.SearchCriteria;
8787
import com.cloud.utils.db.TransactionLegacy;
88+
import com.cloud.event.dao.UsageEventDetailsDao;
8889

8990
@Component
9091
@Local(value = {UsageManager.class})
@@ -137,6 +138,8 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
137138
@Inject
138139
protected UsageEventDao _usageEventDao;
139140
@Inject
141+
protected UsageEventDetailsDao _usageEventDetailsDao;
142+
@Inject
140143
ConfigurationDao _configDao;
141144
@Inject
142145
private UsageVMSnapshotDao m_usageVMSnapshotDao;
@@ -1102,10 +1105,37 @@ private void createVMHelperEvent(UsageEventVO event) {
11021105
try {
11031106
Long templateId = event.getTemplateId();
11041107
String hypervisorType = event.getResourceType();
1108+
Long cpuCores= null;
1109+
Long memory = null;
1110+
Long cpuSpeed = null;
1111+
1112+
//populate the cpu, memory and cpuSpeed of the vm when created from a dynamic offering.
1113+
Map<String, String> usageDetails = _usageEventDetailsDao.findDetails(event.getId());
1114+
1115+
if (usageDetails != null && usageDetails.size() != 0) {
1116+
if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuNumber.name()) != null) {
1117+
cpuCores = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuNumber.name()));
1118+
}
1119+
if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuSpeed.name()) != null) {
1120+
cpuSpeed = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.cpuSpeed.name()));
1121+
}
1122+
if (usageDetails.get(UsageVMInstanceVO.DynamicParameters.memory.name()) != null) {
1123+
memory = Long.parseLong(usageDetails.get(UsageVMInstanceVO.DynamicParameters.memory.name()));
1124+
}
1125+
}
1126+
11051127
// add this VM to the usage helper table
1106-
UsageVMInstanceVO usageInstanceNew =
1107-
new UsageVMInstanceVO(UsageTypes.ALLOCATED_VM, zoneId, event.getAccountId(), vmId, vmName, soId, templateId, hypervisorType, event.getCreateDate(),
1108-
null);
1128+
UsageVMInstanceVO usageInstanceNew = new UsageVMInstanceVO(UsageTypes.ALLOCATED_VM, zoneId, event.getAccountId(), vmId, vmName,
1129+
soId, templateId, hypervisorType, event.getCreateDate(), null);
1130+
if (cpuCores != null) {
1131+
usageInstanceNew.setCpuCores(cpuCores);
1132+
}
1133+
if (cpuSpeed != null) {
1134+
usageInstanceNew.setCpuSpeed(cpuSpeed);
1135+
}
1136+
if (memory != null) {
1137+
usageInstanceNew.setMemory(memory);
1138+
}
11091139
m_usageInstanceDao.persist(usageInstanceNew);
11101140
} catch (Exception ex) {
11111141
s_logger.error("Error saving usage instance for vm: " + vmId, ex);

0 commit comments

Comments
 (0)